blob: b2c28a31bcce44b2832142985b56daff82fcc4ce [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010029 int tcd_catch_idx; // instruction of the first :catch or :finally
30 int tcd_finally_idx; // instruction of the :finally block or zero
31 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010032 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010033 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_return; // when TRUE return from end of :finally
35} trycmd_T;
36
37
38// A stack is used to store:
39// - arguments passed to a :def function
40// - info about the calling function, to use when returning
41// - local variables
42// - temporary values
43//
44// In detail (FP == Frame Pointer):
45// arg1 first argument from caller (if present)
46// arg2 second argument from caller (if present)
47// extra_arg1 any missing optional argument default value
48// FP -> cur_func calling function
49// current previous instruction pointer
50// frame_ptr previous Frame Pointer
51// var1 space for local variable
52// var2 space for local variable
53// .... fixed space for max. number of local variables
54// temp temporary values
55// .... flexible space for temporary values (can grow big)
56
57/*
58 * Execution context.
59 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010060struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010061 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020062 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010063
Bram Moolenaar0186e582021-01-10 18:33:11 +010064 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066 garray_T ec_trystack; // stack of trycmd_T values
67 int ec_in_catch; // when TRUE in catch or finally block
68
69 int ec_dfunc_idx; // current function index
70 isn_T *ec_instr; // array with instructions
71 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020072
73 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010074};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075
Bram Moolenaar12d26532021-02-19 19:13:21 +010076#ifdef FEAT_PROFILE
77// stack of profinfo_T used when profiling.
78static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
79#endif
80
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020082#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083
Bram Moolenaar418f1df2020-08-12 21:34:49 +020084 void
85to_string_error(vartype_T vartype)
86{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020087 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020088}
89
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010091 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092 */
93 static int
94ufunc_argcount(ufunc_T *ufunc)
95{
96 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
97}
98
99/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100100 * Set the instruction index, depending on omitted arguments, where the default
101 * values are to be computed. If all optional arguments are present, start
102 * with the function body.
103 * The expression evaluation is at the start of the instructions:
104 * 0 -> EVAL default1
105 * STORE arg[-2]
106 * 1 -> EVAL default2
107 * STORE arg[-1]
108 * 2 -> function body
109 */
110 static void
111init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
112{
113 if (ufunc->uf_def_args.ga_len == 0)
114 ectx->ec_iidx = 0;
115 else
116 {
117 int defcount = ufunc->uf_args.ga_len - argcount;
118
119 // If there is a varargs argument defcount can be negative, no defaults
120 // to evaluate then.
121 if (defcount < 0)
122 defcount = 0;
123 ectx->ec_iidx = ufunc->uf_def_arg_idx[
124 ufunc->uf_def_args.ga_len - defcount];
125 }
126}
127
128/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200129 * Create a new list from "count" items at the bottom of the stack.
130 * When "count" is zero an empty list is added to the stack.
131 */
132 static int
133exe_newlist(int count, ectx_T *ectx)
134{
135 list_T *list = list_alloc_with_items(count);
136 int idx;
137 typval_T *tv;
138
139 if (list == NULL)
140 return FAIL;
141 for (idx = 0; idx < count; ++idx)
142 list_set_item(list, idx, STACK_TV_BOT(idx - count));
143
144 if (count > 0)
145 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200146 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200147 return FAIL;
148 else
149 ++ectx->ec_stack.ga_len;
150 tv = STACK_TV_BOT(-1);
151 tv->v_type = VAR_LIST;
152 tv->vval.v_list = list;
153 ++list->lv_refcount;
154 return OK;
155}
156
157/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100159 * This adds a stack frame and sets the instruction pointer to the start of the
160 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100161 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 *
163 * Stack has:
164 * - current arguments (already there)
165 * - omitted optional argument (default values) added here
166 * - stack frame:
167 * - pointer to calling function
168 * - Index of next instruction in calling function
169 * - previous frame pointer
170 * - reserved space for local variables
171 */
172 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100173call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200175 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
177 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200178 int arg_to_add;
179 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200180 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200182 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
184 if (dfunc->df_deleted)
185 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100186 // don't use ufunc->uf_name, it may have been freed
187 emsg_funcname(e_func_deleted,
188 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100189 return FAIL;
190 }
191
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100192#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100193 if (do_profiling == PROF_YES)
194 {
195 if (ga_grow(&profile_info_ga, 1) == OK)
196 {
197 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
198 + profile_info_ga.ga_len;
199 ++profile_info_ga.ga_len;
200 CLEAR_POINTER(info);
201 profile_may_start_func(info, ufunc,
202 (((dfunc_T *)def_functions.ga_data)
203 + ectx->ec_dfunc_idx)->df_ufunc);
204 }
205
206 // Profiling might be enabled/disabled along the way. This should not
207 // fail, since the function was compiled before and toggling profiling
208 // doesn't change any errors.
209 if (func_needs_compiling(ufunc, PROFILING(ufunc))
210 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100211 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100212 return FAIL;
213 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100214#endif
215
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 if (ufunc->uf_va_name != NULL)
217 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200218 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200219 // Stack at time of call with 2 varargs:
220 // normal_arg
221 // optional_arg
222 // vararg_1
223 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200224 // After creating the list:
225 // normal_arg
226 // optional_arg
227 // vararg-list
228 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200229 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200230 // After creating the list
231 // normal_arg
232 // (space for optional_arg)
233 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200234 vararg_count = argcount - ufunc->uf_args.ga_len;
235 if (vararg_count < 0)
236 vararg_count = 0;
237 else
238 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200239 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200240 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200241
242 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200243 }
244
Bram Moolenaarfe270812020-04-11 22:31:27 +0200245 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200246 if (arg_to_add < 0)
247 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200248 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200249 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200250 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200251 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200252 return FAIL;
253 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200254
255 // Reserve space for:
256 // - missing arguments
257 // - stack frame
258 // - local variables
259 // - if needed: a counter for number of closures created in
260 // ectx->ec_funcrefs.
261 varcount = dfunc->df_varcount + dfunc->df_has_closure;
262 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
263 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 return FAIL;
265
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100266 // If depth of calling is getting too high, don't execute the function.
267 if (funcdepth_increment() == FAIL)
268 return FAIL;
269
Bram Moolenaarfe270812020-04-11 22:31:27 +0200270 // Move the vararg-list to below the missing optional arguments.
271 if (vararg_count > 0 && arg_to_add > 0)
272 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100273
274 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200275 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200276 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200277 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278
279 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100280 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
281 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100282 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
283 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200284 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285
286 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200287 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200289 if (dfunc->df_has_closure)
290 {
291 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
292
293 tv->v_type = VAR_NUMBER;
294 tv->vval.v_number = 0;
295 }
296 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100298 if (pt != NULL || ufunc->uf_partial != NULL
299 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100300 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100301 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
302
303 if (outer == NULL)
304 return FAIL;
305 if (pt != NULL)
306 {
307 *outer = pt->pt_outer;
308 outer->out_up_is_copy = TRUE;
309 }
310 else if (ufunc->uf_partial != NULL)
311 {
312 *outer = ufunc->uf_partial->pt_outer;
313 outer->out_up_is_copy = TRUE;
314 }
315 else
316 {
317 outer->out_stack = &ectx->ec_stack;
318 outer->out_frame_idx = ectx->ec_frame_idx;
319 outer->out_up = ectx->ec_outer;
320 }
321 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100322 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100323 else
324 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326 // Set execution state to the start of the called function.
327 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100328 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100329 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200330 if (entry != NULL)
331 {
332 // Set the script context to the script where the function was defined.
333 // TODO: save more than the SID?
334 entry->es_save_sid = current_sctx.sc_sid;
335 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
336 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100337
338 // Decide where to start execution, handles optional arguments.
339 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
341 return OK;
342}
343
344// Get pointer to item in the stack.
345#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
346
347/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200348 * Used when returning from a function: Check if any closure is still
349 * referenced. If so then move the arguments and variables to a separate piece
350 * of stack to be used when the closure is called.
351 * When "free_arguments" is TRUE the arguments are to be freed.
352 * Returns FAIL when out of memory.
353 */
354 static int
355handle_closure_in_use(ectx_T *ectx, int free_arguments)
356{
357 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
358 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200359 int argcount;
360 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200361 int idx;
362 typval_T *tv;
363 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200364 garray_T *gap = &ectx->ec_funcrefs;
365 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200366
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200367 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200368 return OK; // function was freed
369 if (dfunc->df_has_closure == 0)
370 return OK; // no closures
371 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
372 closure_count = tv->vval.v_number;
373 if (closure_count == 0)
374 return OK; // no funcrefs created
375
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200376 argcount = ufunc_argcount(dfunc->df_ufunc);
377 top = ectx->ec_frame_idx - argcount;
378
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200379 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200380 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200381 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200382 partial_T *pt;
383 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200384
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200385 if (off < 0)
386 continue; // count is off or already done
387 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200388 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200389 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200390 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200391 int i;
392
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200393 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200394 // unreferenced on return.
395 for (i = 0; i < dfunc->df_varcount; ++i)
396 {
397 typval_T *stv = STACK_TV(ectx->ec_frame_idx
398 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200399 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200400 --refcount;
401 }
402 if (refcount > 1)
403 {
404 closure_in_use = TRUE;
405 break;
406 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200407 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200408 }
409
410 if (closure_in_use)
411 {
412 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
413 typval_T *stack;
414
415 // A closure is using the arguments and/or local variables.
416 // Move them to the called function.
417 if (funcstack == NULL)
418 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200419 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
420 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200421 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
422 funcstack->fs_ga.ga_data = stack;
423 if (stack == NULL)
424 {
425 vim_free(funcstack);
426 return FAIL;
427 }
428
429 // Move or copy the arguments.
430 for (idx = 0; idx < argcount; ++idx)
431 {
432 tv = STACK_TV(top + idx);
433 if (free_arguments)
434 {
435 *(stack + idx) = *tv;
436 tv->v_type = VAR_UNKNOWN;
437 }
438 else
439 copy_tv(tv, stack + idx);
440 }
441 // Move the local variables.
442 for (idx = 0; idx < dfunc->df_varcount; ++idx)
443 {
444 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200445
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200446 // A partial created for a local function, that is also used as a
447 // local variable, has a reference count for the variable, thus
448 // will never go down to zero. When all these refcounts are one
449 // then the funcstack is unused. We need to count how many we have
450 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200451 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
452 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200453 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200454
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200455 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200456 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
457 gap->ga_len - closure_count + i])
458 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200459 }
460
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200461 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200462 tv->v_type = VAR_UNKNOWN;
463 }
464
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200465 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200467 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
468 - closure_count + idx];
469 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200470 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200471 ++funcstack->fs_refcount;
472 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100473 pt->pt_outer.out_stack = &funcstack->fs_ga;
474 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
475 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200476 }
477 }
478 }
479
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200480 for (idx = 0; idx < closure_count; ++idx)
481 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
482 - closure_count + idx]);
483 gap->ga_len -= closure_count;
484 if (gap->ga_len == 0)
485 ga_clear(gap);
486
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200487 return OK;
488}
489
490/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200491 * Called when a partial is freed or its reference count goes down to one. The
492 * funcstack may be the only reference to the partials in the local variables.
493 * Go over all of them, the funcref and can be freed if all partials
494 * referencing the funcstack have a reference count of one.
495 */
496 void
497funcstack_check_refcount(funcstack_T *funcstack)
498{
499 int i;
500 garray_T *gap = &funcstack->fs_ga;
501 int done = 0;
502
503 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
504 return;
505 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
506 {
507 typval_T *tv = ((typval_T *)gap->ga_data) + i;
508
509 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
510 && tv->vval.v_partial->pt_funcstack == funcstack
511 && tv->vval.v_partial->pt_refcount == 1)
512 ++done;
513 }
514 if (done == funcstack->fs_min_refcount)
515 {
516 typval_T *stack = gap->ga_data;
517
518 // All partials referencing the funcstack have a reference count of
519 // one, thus the funcstack is no longer of use.
520 for (i = 0; i < gap->ga_len; ++i)
521 clear_tv(stack + i);
522 vim_free(stack);
523 vim_free(funcstack);
524 }
525}
526
527/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 * Return from the current function.
529 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200530 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531func_return(ectx_T *ectx)
532{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100534 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200535 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
536 + ectx->ec_dfunc_idx;
537 int argcount = ufunc_argcount(dfunc->df_ufunc);
538 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200539 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100540 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
541 + STACK_FRAME_FUNC_OFF)->vval.v_number;
542 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
543 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544
Bram Moolenaar12d26532021-02-19 19:13:21 +0100545#ifdef FEAT_PROFILE
546 if (do_profiling == PROF_YES)
547 {
548 ufunc_T *caller = prev_dfunc->df_ufunc;
549
550 if (dfunc->df_ufunc->uf_profiling
551 || (caller != NULL && caller->uf_profiling))
552 {
553 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
554 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
555 --profile_info_ga.ga_len;
556 }
557 }
558#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200560 entry = estack_pop();
561 if (entry != NULL)
562 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200564 if (handle_closure_in_use(ectx, TRUE) == FAIL)
565 return FAIL;
566
567 // Clear the arguments.
568 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
569 clear_tv(STACK_TV(idx));
570
571 // Clear local variables and temp values, but not the return value.
572 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100573 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100575
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100576 // The return value should be on top of the stack. However, when aborting
577 // it may not be there and ec_frame_idx is the top of the stack.
578 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100579 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100580 ret_idx = 0;
581
Bram Moolenaar0186e582021-01-10 18:33:11 +0100582 vim_free(ectx->ec_outer);
583
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100584 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100585 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100586 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
587 + STACK_FRAME_IIDX_OFF)->vval.v_number;
588 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
589 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200590 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100591 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
592 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100593 ectx->ec_instr = INSTRUCTIONS(prev_dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100594
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100595 if (ret_idx > 0)
596 {
597 // Reset the stack to the position before the call, with a spot for the
598 // return value, moved there from above the frame.
599 ectx->ec_stack.ga_len = top + 1;
600 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
601 }
602 else
603 // Reset the stack to the position before the call.
604 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200605
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100606 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200607 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608}
609
610#undef STACK_TV
611
612/*
613 * Prepare arguments and rettv for calling a builtin or user function.
614 */
615 static int
616call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
617{
618 int idx;
619 typval_T *tv;
620
621 // Move arguments from bottom of the stack to argvars[] and add terminator.
622 for (idx = 0; idx < argcount; ++idx)
623 argvars[idx] = *STACK_TV_BOT(idx - argcount);
624 argvars[argcount].v_type = VAR_UNKNOWN;
625
626 // Result replaces the arguments on the stack.
627 if (argcount > 0)
628 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200629 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100630 return FAIL;
631 else
632 ++ectx->ec_stack.ga_len;
633
634 // Default return value is zero.
635 tv = STACK_TV_BOT(-1);
636 tv->v_type = VAR_NUMBER;
637 tv->vval.v_number = 0;
638
639 return OK;
640}
641
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200642// Ugly global to avoid passing the execution context around through many
643// layers.
644static ectx_T *current_ectx = NULL;
645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646/*
647 * Call a builtin function by index.
648 */
649 static int
650call_bfunc(int func_idx, int argcount, ectx_T *ectx)
651{
652 typval_T argvars[MAX_FUNC_ARGS];
653 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100654 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200655 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656
657 if (call_prepare(argcount, argvars, ectx) == FAIL)
658 return FAIL;
659
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200660 // Call the builtin function. Set "current_ectx" so that when it
661 // recursively invokes call_def_function() a closure context can be set.
662 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200664 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665
666 // Clear the arguments.
667 for (idx = 0; idx < argcount; ++idx)
668 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200669
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100670 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200671 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 return OK;
673}
674
675/*
676 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100677 * If the function is compiled this will add a stack frame and set the
678 * instruction pointer at the start of the function.
679 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100680 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100681 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 */
683 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100684call_ufunc(
685 ufunc_T *ufunc,
686 partial_T *pt,
687 int argcount,
688 ectx_T *ectx,
689 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690{
691 typval_T argvars[MAX_FUNC_ARGS];
692 funcexe_T funcexe;
693 int error;
694 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100695 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100696#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100697 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100698#else
699# define profiling FALSE
700#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701
Bram Moolenaarb2049902021-01-24 12:53:53 +0100702 if (func_needs_compiling(ufunc, profiling)
703 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200704 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200705 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100706 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100707 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100708 if (error != FCERR_UNKNOWN)
709 {
710 if (error == FCERR_TOOMANY)
711 semsg(_(e_toomanyarg), ufunc->uf_name);
712 else
713 semsg(_(e_toofewarg), ufunc->uf_name);
714 return FAIL;
715 }
716
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100717 // The function has been compiled, can call it quickly. For a function
718 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100719 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100720 if (iptr != NULL)
721 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100722 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100723 iptr->isn_type = ISN_DCALL;
724 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
725 iptr->isn_arg.dfunc.cdf_argcount = argcount;
726 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100727 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729
730 if (call_prepare(argcount, argvars, ectx) == FAIL)
731 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200732 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 funcexe.evaluate = TRUE;
734
735 // Call the user function. Result goes in last position on the stack.
736 // TODO: add selfdict if there is one
737 error = call_user_func_check(ufunc, argcount, argvars,
738 STACK_TV_BOT(-1), &funcexe, NULL);
739
740 // Clear the arguments.
741 for (idx = 0; idx < argcount; ++idx)
742 clear_tv(&argvars[idx]);
743
744 if (error != FCERR_NONE)
745 {
746 user_func_error(error, ufunc->uf_name);
747 return FAIL;
748 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100749 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200750 // Error other than from calling the function itself.
751 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 return OK;
753}
754
755/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200756 * Return TRUE if an error was given or CTRL-C was pressed.
757 */
758 static int
759vim9_aborting(int prev_called_emsg)
760{
761 return called_emsg > prev_called_emsg || got_int || did_throw;
762}
763
764/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 * Execute a function by "name".
766 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100767 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 * Returns FAIL if not found without an error message.
769 */
770 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100771call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772{
773 ufunc_T *ufunc;
774
775 if (builtin_function(name, -1))
776 {
777 int func_idx = find_internal_func(name);
778
779 if (func_idx < 0)
780 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200781 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 return FAIL;
783 return call_bfunc(func_idx, argcount, ectx);
784 }
785
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200786 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200787
788 if (ufunc == NULL)
789 {
790 int called_emsg_before = called_emsg;
791
792 if (script_autoload(name, TRUE))
793 // loaded a package, search for the function again
794 ufunc = find_func(name, FALSE, NULL);
795 if (vim9_aborting(called_emsg_before))
796 return FAIL; // bail out if loading the script caused an error
797 }
798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100800 {
801 if (ufunc->uf_arg_types != NULL)
802 {
803 int i;
804 typval_T *argv = STACK_TV_BOT(0) - argcount;
805
806 // The function can change at runtime, check that the argument
807 // types are correct.
808 for (i = 0; i < argcount; ++i)
809 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100810 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100811
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100812 if (i < ufunc->uf_args.ga_len)
813 type = ufunc->uf_arg_types[i];
814 else if (ufunc->uf_va_type != NULL)
815 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100816 if (type != NULL && check_typval_arg_type(type,
817 &argv[i], i + 1) == FAIL)
818 return FAIL;
819 }
820 }
821
Bram Moolenaar0186e582021-01-10 18:33:11 +0100822 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824
825 return FAIL;
826}
827
828 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200829call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200831 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200832 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100834 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835
836 if (tv->v_type == VAR_PARTIAL)
837 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200838 partial_T *pt = tv->vval.v_partial;
839 int i;
840
841 if (pt->pt_argc > 0)
842 {
843 // Make space for arguments from the partial, shift the "argcount"
844 // arguments up.
845 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
846 return FAIL;
847 for (i = 1; i <= argcount; ++i)
848 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
849 ectx->ec_stack.ga_len += pt->pt_argc;
850 argcount += pt->pt_argc;
851
852 // copy the arguments from the partial onto the stack
853 for (i = 0; i < pt->pt_argc; ++i)
854 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856
857 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100858 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 name = pt->pt_name;
861 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200862 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100863 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200864 if (name != NULL)
865 {
866 char_u fname_buf[FLEN_FIXED + 1];
867 char_u *tofree = NULL;
868 int error = FCERR_NONE;
869 char_u *fname;
870
871 // May need to translate <SNR>123_ to K_SNR.
872 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
873 if (error != FCERR_NONE)
874 res = FAIL;
875 else
876 res = call_by_name(fname, argcount, ectx, NULL);
877 vim_free(tofree);
878 }
879
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100880 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 {
882 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200883 semsg(_(e_unknownfunc),
884 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 return FAIL;
886 }
887 return OK;
888}
889
890/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200891 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
892 * TRUE.
893 */
894 static int
895error_if_locked(int lock, char *error)
896{
897 if (lock & (VAR_LOCKED | VAR_FIXED))
898 {
899 emsg(_(error));
900 return TRUE;
901 }
902 return FALSE;
903}
904
905/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100906 * Give an error if "tv" is not a number and return FAIL.
907 */
908 static int
909check_for_number(typval_T *tv)
910{
911 if (tv->v_type != VAR_NUMBER)
912 {
913 semsg(_(e_expected_str_but_got_str),
914 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
915 return FAIL;
916 }
917 return OK;
918}
919
920/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100921 * Store "tv" in variable "name".
922 * This is for s: and g: variables.
923 */
924 static void
925store_var(char_u *name, typval_T *tv)
926{
927 funccal_entry_T entry;
928
929 save_funccal(&entry);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100930 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100931 restore_funccal();
932}
933
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100934/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100935 * Convert "tv" to a string.
936 * Return FAIL if not allowed.
937 */
938 static int
939do_2string(typval_T *tv, int is_2string_any)
940{
941 if (tv->v_type != VAR_STRING)
942 {
943 char_u *str;
944
945 if (is_2string_any)
946 {
947 switch (tv->v_type)
948 {
949 case VAR_SPECIAL:
950 case VAR_BOOL:
951 case VAR_NUMBER:
952 case VAR_FLOAT:
953 case VAR_BLOB: break;
954 default: to_string_error(tv->v_type);
955 return FAIL;
956 }
957 }
Bram Moolenaar34453202021-01-31 13:08:38 +0100958 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100959 clear_tv(tv);
960 tv->v_type = VAR_STRING;
961 tv->vval.v_string = str;
962 }
963 return OK;
964}
965
966/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100967 * When the value of "sv" is a null list of dict, allocate it.
968 */
969 static void
970allocate_if_null(typval_T *tv)
971{
972 switch (tv->v_type)
973 {
974 case VAR_LIST:
975 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100976 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100977 break;
978 case VAR_DICT:
979 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100980 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100981 break;
982 default:
983 break;
984 }
985}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200986
Bram Moolenaare7525c52021-01-09 13:20:37 +0100987/*
988 * Return the character "str[index]" where "index" is the character index. If
989 * "index" is out of range NULL is returned.
990 */
991 char_u *
992char_from_string(char_u *str, varnumber_T index)
993{
994 size_t nbyte = 0;
995 varnumber_T nchar = index;
996 size_t slen;
997
998 if (str == NULL)
999 return NULL;
1000 slen = STRLEN(str);
1001
1002 // do the same as for a list: a negative index counts from the end
1003 if (index < 0)
1004 {
1005 int clen = 0;
1006
1007 for (nbyte = 0; nbyte < slen; ++clen)
1008 nbyte += MB_CPTR2LEN(str + nbyte);
1009 nchar = clen + index;
1010 if (nchar < 0)
1011 // unlike list: index out of range results in empty string
1012 return NULL;
1013 }
1014
1015 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
1016 nbyte += MB_CPTR2LEN(str + nbyte);
1017 if (nbyte >= slen)
1018 return NULL;
1019 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
1020}
1021
1022/*
1023 * Get the byte index for character index "idx" in string "str" with length
1024 * "str_len".
1025 * If going over the end return "str_len".
1026 * If "idx" is negative count from the end, -1 is the last character.
1027 * When going over the start return -1.
1028 */
1029 static long
1030char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1031{
1032 varnumber_T nchar = idx;
1033 size_t nbyte = 0;
1034
1035 if (nchar >= 0)
1036 {
1037 while (nchar > 0 && nbyte < str_len)
1038 {
1039 nbyte += MB_CPTR2LEN(str + nbyte);
1040 --nchar;
1041 }
1042 }
1043 else
1044 {
1045 nbyte = str_len;
1046 while (nchar < 0 && nbyte > 0)
1047 {
1048 --nbyte;
1049 nbyte -= mb_head_off(str, str + nbyte);
1050 ++nchar;
1051 }
1052 if (nchar < 0)
1053 return -1;
1054 }
1055 return (long)nbyte;
1056}
1057
1058/*
1059 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001060 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001061 * Return NULL when the result is empty.
1062 */
1063 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001064string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001065{
1066 long start_byte, end_byte;
1067 size_t slen;
1068
1069 if (str == NULL)
1070 return NULL;
1071 slen = STRLEN(str);
1072 start_byte = char_idx2byte(str, slen, first);
1073 if (start_byte < 0)
1074 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001075 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001076 end_byte = (long)slen;
1077 else
1078 {
1079 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001080 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001081 // end index is inclusive
1082 end_byte += MB_CPTR2LEN(str + end_byte);
1083 }
1084
1085 if (start_byte >= (long)slen || end_byte <= start_byte)
1086 return NULL;
1087 return vim_strnsave(str + start_byte, end_byte - start_byte);
1088}
1089
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001090 static svar_T *
1091get_script_svar(scriptref_T *sref, ectx_T *ectx)
1092{
1093 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1094 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1095 + ectx->ec_dfunc_idx;
1096 svar_T *sv;
1097
1098 if (sref->sref_seq != si->sn_script_seq)
1099 {
1100 // The script was reloaded after the function was
1101 // compiled, the script_idx may not be valid.
1102 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1103 dfunc->df_ufunc->uf_name_exp);
1104 return NULL;
1105 }
1106 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1107 if (!equal_type(sv->sv_type, sref->sref_type))
1108 {
1109 emsg(_(e_script_variable_type_changed));
1110 return NULL;
1111 }
1112 return sv;
1113}
1114
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001115/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 * Execute a function by "name".
1117 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001118 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 */
1120 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001121call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122{
Bram Moolenaared677f52020-08-12 16:38:10 +02001123 int called_emsg_before = called_emsg;
1124 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125
Bram Moolenaared677f52020-08-12 16:38:10 +02001126 res = call_by_name(name, argcount, ectx, iptr);
1127 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001129 dictitem_T *v;
1130
1131 v = find_var(name, NULL, FALSE);
1132 if (v == NULL)
1133 {
1134 semsg(_(e_unknownfunc), name);
1135 return FAIL;
1136 }
1137 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1138 {
1139 semsg(_(e_unknownfunc), name);
1140 return FAIL;
1141 }
1142 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001144 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145}
1146
1147/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001148 * When a function reference is used, fill a partial with the information
1149 * needed, especially when it is used as a closure.
1150 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001151 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001152fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1153{
1154 pt->pt_func = ufunc;
1155 pt->pt_refcount = 1;
1156
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001157 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001158 {
1159 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1160 + ectx->ec_dfunc_idx;
1161
1162 // The closure needs to find arguments and local
1163 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001164 pt->pt_outer.out_stack = &ectx->ec_stack;
1165 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1166 pt->pt_outer.out_up = ectx->ec_outer;
1167 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001168
1169 // If this function returns and the closure is still
1170 // being used, we need to make a copy of the context
1171 // (arguments and local variables). Store a reference
1172 // to the partial so we can handle that.
1173 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1174 {
1175 vim_free(pt);
1176 return FAIL;
1177 }
1178 // Extra variable keeps the count of closures created
1179 // in the current function call.
1180 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1181 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1182
1183 ((partial_T **)ectx->ec_funcrefs.ga_data)
1184 [ectx->ec_funcrefs.ga_len] = pt;
1185 ++pt->pt_refcount;
1186 ++ectx->ec_funcrefs.ga_len;
1187 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001188 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001189 return OK;
1190}
1191
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001192
Bram Moolenaarf112f302020-12-20 17:47:52 +01001193/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 * Call a "def" function from old Vim script.
1195 * Return OK or FAIL.
1196 */
1197 int
1198call_def_function(
1199 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001200 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001202 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 typval_T *rettv) // return value
1204{
1205 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001206 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001207 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 typval_T *tv;
1209 int idx;
1210 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001211 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001212 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001213 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001214 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001215 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001216 msglist_T **saved_msg_list = NULL;
1217 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001218 cmdmod_T save_cmdmod;
1219 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001220 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001221 int save_emsg_silent_def = emsg_silent_def;
1222 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001223 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001224 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001225 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226
1227// Get pointer to item in the stack.
1228#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1229
1230// Get pointer to item at the bottom of the stack, -1 is the bottom.
1231#undef STACK_TV_BOT
1232#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1233
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001234// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001235#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 +01001236
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001237 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001238 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1239 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001240 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001241 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001242 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001243 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001244 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001245 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001246 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001247
Bram Moolenaar09689a02020-05-09 22:50:08 +02001248 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001249 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001250 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1251 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001252 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001253 {
1254 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001255 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001256 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001259 // If depth of calling is getting too high, don't execute the function.
1260 orig_funcdepth = funcdepth_get();
1261 if (funcdepth_increment() == FAIL)
1262 return FAIL;
1263
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001264 CLEAR_FIELD(ectx);
1265 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1266 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1267 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001268 {
1269 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001270 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001273 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001275 // Put arguments on the stack, but no more than what the function expects.
1276 // A lambda can be called with more arguments than it uses.
1277 for (idx = 0; idx < argc
1278 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1279 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001281 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001282 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001283 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001284 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 copy_tv(&argv[idx], STACK_TV_BOT(0));
1286 ++ectx.ec_stack.ga_len;
1287 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001288
1289 // Turn varargs into a list. Empty list if no args.
1290 if (ufunc->uf_va_name != NULL)
1291 {
1292 int vararg_count = argc - ufunc->uf_args.ga_len;
1293
1294 if (vararg_count < 0)
1295 vararg_count = 0;
1296 else
1297 argc -= vararg_count;
1298 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001299 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001300
1301 // Check the type of the list items.
1302 tv = STACK_TV_BOT(-1);
1303 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001304 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001305 && ufunc->uf_va_type->tt_member != &t_any
1306 && tv->vval.v_list != NULL)
1307 {
1308 type_T *expected = ufunc->uf_va_type->tt_member;
1309 listitem_T *li = tv->vval.v_list->lv_first;
1310
1311 for (idx = 0; idx < vararg_count; ++idx)
1312 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001313 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001314 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001315 goto failed_early;
1316 li = li->li_next;
1317 }
1318 }
1319
Bram Moolenaar23e03252020-04-12 22:22:31 +02001320 if (defcount > 0)
1321 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001322 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001323 --ectx.ec_stack.ga_len;
1324 }
1325
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001326 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001327 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001328 if (defcount > 0)
1329 for (idx = 0; idx < defcount; ++idx)
1330 {
1331 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1332 ++ectx.ec_stack.ga_len;
1333 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001334 if (ufunc->uf_va_name != NULL)
1335 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336
1337 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001338 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1339 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001341 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001342 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1343 + ufunc->uf_dfunc_idx;
1344 ufunc_T *base_ufunc = dfunc->df_ufunc;
1345
1346 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1347 // by copy_func().
1348 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001349 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001350 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1351 if (ectx.ec_outer == NULL)
1352 goto failed_early;
1353 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001354 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001355 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1356 {
1357 if (current_ectx->ec_outer != NULL)
1358 *ectx.ec_outer = *current_ectx->ec_outer;
1359 }
1360 else
1361 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001362 }
1363 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001364 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1365 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001366 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001367 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 // dummy frame entries
1370 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1371 {
1372 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1373 ++ectx.ec_stack.ga_len;
1374 }
1375
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001376 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001377 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001378 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1379 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001381 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001382 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001383 ectx.ec_stack.ga_len += dfunc->df_varcount;
1384 if (dfunc->df_has_closure)
1385 {
1386 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1387 STACK_TV_VAR(idx)->vval.v_number = 0;
1388 ++ectx.ec_stack.ga_len;
1389 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001390
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001391 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001392 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001393
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001394 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001395 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001396 estack_push_ufunc(ufunc, 1);
1397 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001398 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1399
Bram Moolenaar352134b2020-10-17 22:04:08 +02001400 // Use a specific location for storing error messages to be converted to an
1401 // exception.
1402 saved_msg_list = msg_list;
1403 msg_list = &private_msg_list;
1404
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001405 // Do turn errors into exceptions.
1406 suppress_errthrow = FALSE;
1407
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001408 // When ":silent!" was used before calling then we still abort the
1409 // function. If ":silent!" is used in the function then we don't.
1410 emsg_silent_def = emsg_silent;
1411 did_emsg_def = 0;
1412
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001413 where.wt_index = 0;
1414 where.wt_variable = FALSE;
1415
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001416 // Decide where to start execution, handles optional arguments.
1417 init_instr_idx(ufunc, argc, &ectx);
1418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 for (;;)
1420 {
1421 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001422
Bram Moolenaar270d0382020-05-15 21:42:53 +02001423 if (++breakcheck_count >= 100)
1424 {
1425 line_breakcheck();
1426 breakcheck_count = 0;
1427 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001428 if (got_int)
1429 {
1430 // Turn CTRL-C into an exception.
1431 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001432 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001433 goto failed;
1434 did_throw = TRUE;
1435 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436
Bram Moolenaara26b9702020-04-18 19:53:28 +02001437 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1438 {
1439 // Turn an error message into an exception.
1440 did_emsg = FALSE;
1441 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1442 goto failed;
1443 did_throw = TRUE;
1444 *msg_list = NULL;
1445 }
1446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 if (did_throw && !ectx.ec_in_catch)
1448 {
1449 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001450 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451
1452 // An exception jumps to the first catch, finally, or returns from
1453 // the current function.
1454 if (trystack->ga_len > 0)
1455 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001456 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 {
1458 // jump to ":catch" or ":finally"
1459 ectx.ec_in_catch = TRUE;
1460 ectx.ec_iidx = trycmd->tcd_catch_idx;
1461 }
1462 else
1463 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001464 // Not inside try or need to return from current functions.
1465 // Push a dummy return value.
1466 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1467 goto failed;
1468 tv = STACK_TV_BOT(0);
1469 tv->v_type = VAR_NUMBER;
1470 tv->vval.v_number = 0;
1471 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001472 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001474 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001475 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001476 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1477 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 goto done;
1479 }
1480
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001481 if (func_return(&ectx) == FAIL)
1482 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 }
1484 continue;
1485 }
1486
1487 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1488 switch (iptr->isn_type)
1489 {
1490 // execute Ex command line
1491 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001492 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001493 source_cookie_T cookie;
1494
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001495 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001496 // Pass getsourceline to get an error for a missing ":end"
1497 // command.
1498 CLEAR_FIELD(cookie);
1499 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1500 if (do_cmdline(iptr->isn_arg.string,
1501 getsourceline, &cookie,
1502 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1503 == FAIL
1504 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001505 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001506 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 break;
1508
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001509 // execute Ex command from pieces on the stack
1510 case ISN_EXECCONCAT:
1511 {
1512 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001513 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001514 int pass;
1515 int i;
1516 char_u *cmd = NULL;
1517 char_u *str;
1518
1519 for (pass = 1; pass <= 2; ++pass)
1520 {
1521 for (i = 0; i < count; ++i)
1522 {
1523 tv = STACK_TV_BOT(i - count);
1524 str = tv->vval.v_string;
1525 if (str != NULL && *str != NUL)
1526 {
1527 if (pass == 2)
1528 STRCPY(cmd + len, str);
1529 len += STRLEN(str);
1530 }
1531 if (pass == 2)
1532 clear_tv(tv);
1533 }
1534 if (pass == 1)
1535 {
1536 cmd = alloc(len + 1);
1537 if (cmd == NULL)
1538 goto failed;
1539 len = 0;
1540 }
1541 }
1542
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001543 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001544 do_cmdline_cmd(cmd);
1545 vim_free(cmd);
1546 }
1547 break;
1548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 // execute :echo {string} ...
1550 case ISN_ECHO:
1551 {
1552 int count = iptr->isn_arg.echo.echo_count;
1553 int atstart = TRUE;
1554 int needclr = TRUE;
1555
1556 for (idx = 0; idx < count; ++idx)
1557 {
1558 tv = STACK_TV_BOT(idx - count);
1559 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1560 &atstart, &needclr);
1561 clear_tv(tv);
1562 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001563 if (needclr)
1564 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 ectx.ec_stack.ga_len -= count;
1566 }
1567 break;
1568
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001569 // :execute {string} ...
1570 // :echomsg {string} ...
1571 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001572 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001573 case ISN_ECHOMSG:
1574 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001575 {
1576 int count = iptr->isn_arg.number;
1577 garray_T ga;
1578 char_u buf[NUMBUFLEN];
1579 char_u *p;
1580 int len;
1581 int failed = FALSE;
1582
1583 ga_init2(&ga, 1, 80);
1584 for (idx = 0; idx < count; ++idx)
1585 {
1586 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001587 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001588 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001589 if (tv->v_type == VAR_CHANNEL
1590 || tv->v_type == VAR_JOB)
1591 {
1592 SOURCING_LNUM = iptr->isn_lnum;
1593 emsg(_(e_inval_string));
1594 break;
1595 }
1596 else
1597 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001598 }
1599 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001600 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001601
1602 len = (int)STRLEN(p);
1603 if (ga_grow(&ga, len + 2) == FAIL)
1604 failed = TRUE;
1605 else
1606 {
1607 if (ga.ga_len > 0)
1608 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1609 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1610 ga.ga_len += len;
1611 }
1612 clear_tv(tv);
1613 }
1614 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001615 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001616 {
1617 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001618 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001619 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001620
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001621 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001622 {
1623 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001624 {
1625 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001626 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001627 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001628 {
1629 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001630 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001631 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001632 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001633 else
1634 {
1635 msg_sb_eol();
1636 if (iptr->isn_type == ISN_ECHOMSG)
1637 {
1638 msg_attr(ga.ga_data, echo_attr);
1639 out_flush();
1640 }
1641 else
1642 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001643 SOURCING_LNUM = iptr->isn_lnum;
1644 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001645 }
1646 }
1647 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001648 ga_clear(&ga);
1649 }
1650 break;
1651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 // load local variable or argument
1653 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001654 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 goto failed;
1656 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1657 ++ectx.ec_stack.ga_len;
1658 break;
1659
1660 // load v: variable
1661 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001662 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 goto failed;
1664 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1665 ++ectx.ec_stack.ga_len;
1666 break;
1667
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001668 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 case ISN_LOADSCRIPT:
1670 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001671 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 svar_T *sv;
1673
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001674 sv = get_script_svar(sref, &ectx);
1675 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001676 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001677 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001678 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 goto failed;
1680 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1681 ++ectx.ec_stack.ga_len;
1682 }
1683 break;
1684
1685 // load s: variable in old script
1686 case ISN_LOADS:
1687 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001688 hashtab_T *ht = &SCRIPT_VARS(
1689 iptr->isn_arg.loadstore.ls_sid);
1690 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 if (di == NULL)
1694 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001695 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001696 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001697 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 }
1699 else
1700 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001701 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 goto failed;
1703 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1704 ++ectx.ec_stack.ga_len;
1705 }
1706 }
1707 break;
1708
Bram Moolenaard3aac292020-04-19 14:32:17 +02001709 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001711 case ISN_LOADB:
1712 case ISN_LOADW:
1713 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001715 dictitem_T *di = NULL;
1716 hashtab_T *ht = NULL;
1717 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001718
Bram Moolenaard3aac292020-04-19 14:32:17 +02001719 switch (iptr->isn_type)
1720 {
1721 case ISN_LOADG:
1722 ht = get_globvar_ht();
1723 namespace = 'g';
1724 break;
1725 case ISN_LOADB:
1726 ht = &curbuf->b_vars->dv_hashtab;
1727 namespace = 'b';
1728 break;
1729 case ISN_LOADW:
1730 ht = &curwin->w_vars->dv_hashtab;
1731 namespace = 'w';
1732 break;
1733 case ISN_LOADT:
1734 ht = &curtab->tp_vars->dv_hashtab;
1735 namespace = 't';
1736 break;
1737 default: // Cannot reach here
1738 goto failed;
1739 }
1740 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 if (di == NULL)
1743 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001744 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001745 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001746 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001747 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 }
1749 else
1750 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001751 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 goto failed;
1753 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1754 ++ectx.ec_stack.ga_len;
1755 }
1756 }
1757 break;
1758
Bram Moolenaar03290b82020-12-19 16:30:44 +01001759 // load autoload variable
1760 case ISN_LOADAUTO:
1761 {
1762 char_u *name = iptr->isn_arg.string;
1763
1764 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1765 goto failed;
1766 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001767 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001768 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001769 goto on_error;
1770 ++ectx.ec_stack.ga_len;
1771 }
1772 break;
1773
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001774 // load g:/b:/w:/t: namespace
1775 case ISN_LOADGDICT:
1776 case ISN_LOADBDICT:
1777 case ISN_LOADWDICT:
1778 case ISN_LOADTDICT:
1779 {
1780 dict_T *d = NULL;
1781
1782 switch (iptr->isn_type)
1783 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001784 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1785 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1786 case ISN_LOADWDICT: d = curwin->w_vars; break;
1787 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001788 default: // Cannot reach here
1789 goto failed;
1790 }
1791 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1792 goto failed;
1793 tv = STACK_TV_BOT(0);
1794 tv->v_type = VAR_DICT;
1795 tv->v_lock = 0;
1796 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001797 ++d->dv_refcount;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001798 ++ectx.ec_stack.ga_len;
1799 }
1800 break;
1801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 // load &option
1803 case ISN_LOADOPT:
1804 {
1805 typval_T optval;
1806 char_u *name = iptr->isn_arg.string;
1807
Bram Moolenaara8c17702020-04-01 21:17:24 +02001808 // This is not expected to fail, name is checked during
1809 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001810 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001812 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001813 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814 *STACK_TV_BOT(0) = optval;
1815 ++ectx.ec_stack.ga_len;
1816 }
1817 break;
1818
1819 // load $ENV
1820 case ISN_LOADENV:
1821 {
1822 typval_T optval;
1823 char_u *name = iptr->isn_arg.string;
1824
Bram Moolenaar270d0382020-05-15 21:42:53 +02001825 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001827 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001828 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 *STACK_TV_BOT(0) = optval;
1830 ++ectx.ec_stack.ga_len;
1831 }
1832 break;
1833
1834 // load @register
1835 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001836 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 goto failed;
1838 tv = STACK_TV_BOT(0);
1839 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001840 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001841 // This may result in NULL, which should be equivalent to an
1842 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 tv->vval.v_string = get_reg_contents(
1844 iptr->isn_arg.number, GREG_EXPR_SRC);
1845 ++ectx.ec_stack.ga_len;
1846 break;
1847
1848 // store local variable
1849 case ISN_STORE:
1850 --ectx.ec_stack.ga_len;
1851 tv = STACK_TV_VAR(iptr->isn_arg.number);
1852 clear_tv(tv);
1853 *tv = *STACK_TV_BOT(0);
1854 break;
1855
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001856 // store s: variable in old script
1857 case ISN_STORES:
1858 {
1859 hashtab_T *ht = &SCRIPT_VARS(
1860 iptr->isn_arg.loadstore.ls_sid);
1861 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001862 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001863
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001864 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001865 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001866 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001867 else
1868 {
1869 clear_tv(&di->di_tv);
1870 di->di_tv = *STACK_TV_BOT(0);
1871 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001872 }
1873 break;
1874
1875 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 case ISN_STORESCRIPT:
1877 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001878 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1879 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001881 sv = get_script_svar(sref, &ectx);
1882 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001883 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 --ectx.ec_stack.ga_len;
1885 clear_tv(sv->sv_tv);
1886 *sv->sv_tv = *STACK_TV_BOT(0);
1887 }
1888 break;
1889
1890 // store option
1891 case ISN_STOREOPT:
1892 {
1893 long n = 0;
1894 char_u *s = NULL;
1895 char *msg;
1896
1897 --ectx.ec_stack.ga_len;
1898 tv = STACK_TV_BOT(0);
1899 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001900 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001902 if (s == NULL)
1903 s = (char_u *)"";
1904 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001906 // must be VAR_NUMBER, CHECKTYPE makes sure
1907 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1909 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001910 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001911 if (msg != NULL)
1912 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001913 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001915 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 }
1918 break;
1919
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001920 // store $ENV
1921 case ISN_STOREENV:
1922 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001923 tv = STACK_TV_BOT(0);
1924 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1925 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001926 break;
1927
1928 // store @r
1929 case ISN_STOREREG:
1930 {
1931 int reg = iptr->isn_arg.number;
1932
1933 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001934 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001935 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001936 tv_get_string(tv), -1, FALSE);
1937 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001938 }
1939 break;
1940
1941 // store v: variable
1942 case ISN_STOREV:
1943 --ectx.ec_stack.ga_len;
1944 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1945 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001946 // should not happen, type is checked when compiling
1947 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001948 break;
1949
Bram Moolenaard3aac292020-04-19 14:32:17 +02001950 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001952 case ISN_STOREB:
1953 case ISN_STOREW:
1954 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001956 dictitem_T *di;
1957 hashtab_T *ht;
1958 char_u *name = iptr->isn_arg.string + 2;
1959
Bram Moolenaard3aac292020-04-19 14:32:17 +02001960 switch (iptr->isn_type)
1961 {
1962 case ISN_STOREG:
1963 ht = get_globvar_ht();
1964 break;
1965 case ISN_STOREB:
1966 ht = &curbuf->b_vars->dv_hashtab;
1967 break;
1968 case ISN_STOREW:
1969 ht = &curwin->w_vars->dv_hashtab;
1970 break;
1971 case ISN_STORET:
1972 ht = &curtab->tp_vars->dv_hashtab;
1973 break;
1974 default: // Cannot reach here
1975 goto failed;
1976 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977
1978 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001979 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001981 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 else
1983 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001984 SOURCING_LNUM = iptr->isn_lnum;
1985 if (var_check_permission(di, name) == FAIL)
1986 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 clear_tv(&di->di_tv);
1988 di->di_tv = *STACK_TV_BOT(0);
1989 }
1990 }
1991 break;
1992
Bram Moolenaar03290b82020-12-19 16:30:44 +01001993 // store an autoload variable
1994 case ISN_STOREAUTO:
1995 SOURCING_LNUM = iptr->isn_lnum;
1996 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1997 clear_tv(STACK_TV_BOT(-1));
1998 --ectx.ec_stack.ga_len;
1999 break;
2000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 // store number in local variable
2002 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002003 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 clear_tv(tv);
2005 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002006 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 break;
2008
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002009 // store value in list or dict variable
2010 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002011 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002012 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002013 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002014 typval_T *tv_dest = STACK_TV_BOT(-1);
2015 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002016
Bram Moolenaar752fc692021-01-04 21:57:11 +01002017 // Stack contains:
2018 // -3 value to be stored
2019 // -2 index
2020 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002021 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002022 SOURCING_LNUM = iptr->isn_lnum;
2023 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002024 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002025 dest_type = tv_dest->v_type;
2026 if (dest_type == VAR_DICT)
2027 status = do_2string(tv_idx, TRUE);
2028 else if (dest_type == VAR_LIST
2029 && tv_idx->v_type != VAR_NUMBER)
2030 {
2031 emsg(_(e_number_exp));
2032 status = FAIL;
2033 }
2034 }
2035 else if (dest_type != tv_dest->v_type)
2036 {
2037 // just in case, should be OK
2038 semsg(_(e_expected_str_but_got_str),
2039 vartype_name(dest_type),
2040 vartype_name(tv_dest->v_type));
2041 status = FAIL;
2042 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002043
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002044 if (status == OK && dest_type == VAR_LIST)
2045 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002046 long lidx = (long)tv_idx->vval.v_number;
2047 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002048
2049 if (list == NULL)
2050 {
2051 emsg(_(e_list_not_set));
2052 goto on_error;
2053 }
2054 if (lidx < 0 && list->lv_len + lidx >= 0)
2055 // negative index is relative to the end
2056 lidx = list->lv_len + lidx;
2057 if (lidx < 0 || lidx > list->lv_len)
2058 {
2059 semsg(_(e_listidx), lidx);
2060 goto on_error;
2061 }
2062 if (lidx < list->lv_len)
2063 {
2064 listitem_T *li = list_find(list, lidx);
2065
2066 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002067 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002068 goto on_error;
2069 // overwrite existing list item
2070 clear_tv(&li->li_tv);
2071 li->li_tv = *tv;
2072 }
2073 else
2074 {
2075 if (error_if_locked(list->lv_lock,
2076 e_cannot_change_list))
2077 goto on_error;
2078 // append to list, only fails when out of memory
2079 if (list_append_tv(list, tv) == FAIL)
2080 goto failed;
2081 clear_tv(tv);
2082 }
2083 }
2084 else if (status == OK && dest_type == VAR_DICT)
2085 {
2086 char_u *key = tv_idx->vval.v_string;
2087 dict_T *dict = tv_dest->vval.v_dict;
2088 dictitem_T *di;
2089
2090 SOURCING_LNUM = iptr->isn_lnum;
2091 if (dict == NULL)
2092 {
2093 emsg(_(e_dictionary_not_set));
2094 goto on_error;
2095 }
2096 if (key == NULL)
2097 key = (char_u *)"";
2098 di = dict_find(dict, key, -1);
2099 if (di != NULL)
2100 {
2101 if (error_if_locked(di->di_tv.v_lock,
2102 e_cannot_change_dict_item))
2103 goto on_error;
2104 // overwrite existing value
2105 clear_tv(&di->di_tv);
2106 di->di_tv = *tv;
2107 }
2108 else
2109 {
2110 if (error_if_locked(dict->dv_lock,
2111 e_cannot_change_dict))
2112 goto on_error;
2113 // add to dict, only fails when out of memory
2114 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2115 goto failed;
2116 clear_tv(tv);
2117 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002118 }
2119 else
2120 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002121 status = FAIL;
2122 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002123 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002124
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002125 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002126 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002127 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002128 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002129 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002130 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002131 goto on_error;
2132 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002133 }
2134 break;
2135
Bram Moolenaar0186e582021-01-10 18:33:11 +01002136 // load or store variable or argument from outer scope
2137 case ISN_LOADOUTER:
2138 case ISN_STOREOUTER:
2139 {
2140 int depth = iptr->isn_arg.outer.outer_depth;
2141 outer_T *outer = ectx.ec_outer;
2142
2143 while (depth > 1 && outer != NULL)
2144 {
2145 outer = outer->out_up;
2146 --depth;
2147 }
2148 if (outer == NULL)
2149 {
2150 SOURCING_LNUM = iptr->isn_lnum;
2151 iemsg("LOADOUTER depth more than scope levels");
2152 goto failed;
2153 }
2154 tv = ((typval_T *)outer->out_stack->ga_data)
2155 + outer->out_frame_idx + STACK_FRAME_SIZE
2156 + iptr->isn_arg.outer.outer_idx;
2157 if (iptr->isn_type == ISN_LOADOUTER)
2158 {
2159 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2160 goto failed;
2161 copy_tv(tv, STACK_TV_BOT(0));
2162 ++ectx.ec_stack.ga_len;
2163 }
2164 else
2165 {
2166 --ectx.ec_stack.ga_len;
2167 clear_tv(tv);
2168 *tv = *STACK_TV_BOT(0);
2169 }
2170 }
2171 break;
2172
Bram Moolenaar752fc692021-01-04 21:57:11 +01002173 // unlet item in list or dict variable
2174 case ISN_UNLETINDEX:
2175 {
2176 typval_T *tv_idx = STACK_TV_BOT(-2);
2177 typval_T *tv_dest = STACK_TV_BOT(-1);
2178 int status = OK;
2179
2180 // Stack contains:
2181 // -2 index
2182 // -1 dict or list
2183 if (tv_dest->v_type == VAR_DICT)
2184 {
2185 // unlet a dict item, index must be a string
2186 if (tv_idx->v_type != VAR_STRING)
2187 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002188 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002189 semsg(_(e_expected_str_but_got_str),
2190 vartype_name(VAR_STRING),
2191 vartype_name(tv_idx->v_type));
2192 status = FAIL;
2193 }
2194 else
2195 {
2196 dict_T *d = tv_dest->vval.v_dict;
2197 char_u *key = tv_idx->vval.v_string;
2198 dictitem_T *di = NULL;
2199
2200 if (key == NULL)
2201 key = (char_u *)"";
2202 if (d != NULL)
2203 di = dict_find(d, key, (int)STRLEN(key));
2204 if (di == NULL)
2205 {
2206 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002207 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002208 semsg(_(e_dictkey), key);
2209 status = FAIL;
2210 }
2211 else
2212 {
2213 // TODO: check for dict or item locked
2214 dictitem_remove(d, di);
2215 }
2216 }
2217 }
2218 else if (tv_dest->v_type == VAR_LIST)
2219 {
2220 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002221 SOURCING_LNUM = iptr->isn_lnum;
2222 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002223 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002224 status = FAIL;
2225 }
2226 else
2227 {
2228 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002229 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002230 listitem_T *li = NULL;
2231
2232 li = list_find(l, n);
2233 if (li == NULL)
2234 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002235 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002236 semsg(_(e_listidx), n);
2237 status = FAIL;
2238 }
2239 else
2240 // TODO: check for list or item locked
2241 listitem_remove(l, li);
2242 }
2243 }
2244 else
2245 {
2246 status = FAIL;
2247 semsg(_(e_cannot_index_str),
2248 vartype_name(tv_dest->v_type));
2249 }
2250
2251 clear_tv(tv_idx);
2252 clear_tv(tv_dest);
2253 ectx.ec_stack.ga_len -= 2;
2254 if (status == FAIL)
2255 goto on_error;
2256 }
2257 break;
2258
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002259 // unlet range of items in list variable
2260 case ISN_UNLETRANGE:
2261 {
2262 // Stack contains:
2263 // -3 index1
2264 // -2 index2
2265 // -1 dict or list
2266 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2267 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2268 typval_T *tv_dest = STACK_TV_BOT(-1);
2269 int status = OK;
2270
2271 if (tv_dest->v_type == VAR_LIST)
2272 {
2273 // indexes must be a number
2274 SOURCING_LNUM = iptr->isn_lnum;
2275 if (check_for_number(tv_idx1) == FAIL
2276 || check_for_number(tv_idx2) == FAIL)
2277 {
2278 status = FAIL;
2279 }
2280 else
2281 {
2282 list_T *l = tv_dest->vval.v_list;
2283 long n1 = (long)tv_idx1->vval.v_number;
2284 long n2 = (long)tv_idx2->vval.v_number;
2285 listitem_T *li;
2286
2287 li = list_find_index(l, &n1);
2288 if (li == NULL
2289 || list_unlet_range(l, li, NULL, n1,
2290 TRUE, n2) == FAIL)
2291 status = FAIL;
2292 }
2293 }
2294 else
2295 {
2296 status = FAIL;
2297 SOURCING_LNUM = iptr->isn_lnum;
2298 semsg(_(e_cannot_index_str),
2299 vartype_name(tv_dest->v_type));
2300 }
2301
2302 clear_tv(tv_idx1);
2303 clear_tv(tv_idx2);
2304 clear_tv(tv_dest);
2305 ectx.ec_stack.ga_len -= 3;
2306 if (status == FAIL)
2307 goto on_error;
2308 }
2309 break;
2310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 // push constant
2312 case ISN_PUSHNR:
2313 case ISN_PUSHBOOL:
2314 case ISN_PUSHSPEC:
2315 case ISN_PUSHF:
2316 case ISN_PUSHS:
2317 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002318 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002319 case ISN_PUSHCHANNEL:
2320 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002321 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 goto failed;
2323 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002324 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 ++ectx.ec_stack.ga_len;
2326 switch (iptr->isn_type)
2327 {
2328 case ISN_PUSHNR:
2329 tv->v_type = VAR_NUMBER;
2330 tv->vval.v_number = iptr->isn_arg.number;
2331 break;
2332 case ISN_PUSHBOOL:
2333 tv->v_type = VAR_BOOL;
2334 tv->vval.v_number = iptr->isn_arg.number;
2335 break;
2336 case ISN_PUSHSPEC:
2337 tv->v_type = VAR_SPECIAL;
2338 tv->vval.v_number = iptr->isn_arg.number;
2339 break;
2340#ifdef FEAT_FLOAT
2341 case ISN_PUSHF:
2342 tv->v_type = VAR_FLOAT;
2343 tv->vval.v_float = iptr->isn_arg.fnumber;
2344 break;
2345#endif
2346 case ISN_PUSHBLOB:
2347 blob_copy(iptr->isn_arg.blob, tv);
2348 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002349 case ISN_PUSHFUNC:
2350 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002351 if (iptr->isn_arg.string == NULL)
2352 tv->vval.v_string = NULL;
2353 else
2354 tv->vval.v_string =
2355 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002356 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002357 case ISN_PUSHCHANNEL:
2358#ifdef FEAT_JOB_CHANNEL
2359 tv->v_type = VAR_CHANNEL;
2360 tv->vval.v_channel = iptr->isn_arg.channel;
2361 if (tv->vval.v_channel != NULL)
2362 ++tv->vval.v_channel->ch_refcount;
2363#endif
2364 break;
2365 case ISN_PUSHJOB:
2366#ifdef FEAT_JOB_CHANNEL
2367 tv->v_type = VAR_JOB;
2368 tv->vval.v_job = iptr->isn_arg.job;
2369 if (tv->vval.v_job != NULL)
2370 ++tv->vval.v_job->jv_refcount;
2371#endif
2372 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 default:
2374 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002375 tv->vval.v_string = vim_strsave(
2376 iptr->isn_arg.string == NULL
2377 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 }
2379 break;
2380
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002381 case ISN_UNLET:
2382 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2383 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002384 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002385 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002386 case ISN_UNLETENV:
2387 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2388 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002389
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002390 case ISN_LOCKCONST:
2391 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2392 break;
2393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 // create a list from items on the stack; uses a single allocation
2395 // for the list header and the items
2396 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002397 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2398 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 break;
2400
2401 // create a dict from items on the stack
2402 case ISN_NEWDICT:
2403 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002404 int count = iptr->isn_arg.number;
2405 dict_T *dict = dict_alloc();
2406 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002407 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408
2409 if (dict == NULL)
2410 goto failed;
2411 for (idx = 0; idx < count; ++idx)
2412 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002413 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002415 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002416 key = tv->vval.v_string == NULL
2417 ? (char_u *)"" : tv->vval.v_string;
2418 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002419 if (item != NULL)
2420 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002421 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002422 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002423 dict_unref(dict);
2424 goto on_error;
2425 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002426 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 clear_tv(tv);
2428 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002429 {
2430 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2434 item->di_tv.v_lock = 0;
2435 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002436 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002437 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002438 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002440 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 }
2442
2443 if (count > 0)
2444 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002445 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 goto failed;
2447 else
2448 ++ectx.ec_stack.ga_len;
2449 tv = STACK_TV_BOT(-1);
2450 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002451 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452 tv->vval.v_dict = dict;
2453 ++dict->dv_refcount;
2454 }
2455 break;
2456
2457 // call a :def function
2458 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002459 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002460 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 iptr->isn_arg.dfunc.cdf_argcount,
2462 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002463 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 break;
2465
2466 // call a builtin function
2467 case ISN_BCALL:
2468 SOURCING_LNUM = iptr->isn_lnum;
2469 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2470 iptr->isn_arg.bfunc.cbf_argcount,
2471 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002472 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 break;
2474
2475 // call a funcref or partial
2476 case ISN_PCALL:
2477 {
2478 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2479 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002480 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481
2482 SOURCING_LNUM = iptr->isn_lnum;
2483 if (pfunc->cpf_top)
2484 {
2485 // funcref is above the arguments
2486 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2487 }
2488 else
2489 {
2490 // Get the funcref from the stack.
2491 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002492 partial_tv = *STACK_TV_BOT(0);
2493 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 }
2495 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002496 if (tv == &partial_tv)
2497 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002499 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 }
2501 break;
2502
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002503 case ISN_PCALL_END:
2504 // PCALL finished, arguments have been consumed and replaced by
2505 // the return value. Now clear the funcref from the stack,
2506 // and move the return value in its place.
2507 --ectx.ec_stack.ga_len;
2508 clear_tv(STACK_TV_BOT(-1));
2509 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2510 break;
2511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 // call a user defined function or funcref/partial
2513 case ISN_UCALL:
2514 {
2515 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2516
2517 SOURCING_LNUM = iptr->isn_lnum;
2518 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002519 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002520 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 }
2522 break;
2523
2524 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002525 case ISN_RETURN_ZERO:
2526 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2527 goto failed;
2528 tv = STACK_TV_BOT(0);
2529 ++ectx.ec_stack.ga_len;
2530 tv->v_type = VAR_NUMBER;
2531 tv->vval.v_number = 0;
2532 tv->v_lock = 0;
2533 // FALLTHROUGH
2534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 case ISN_RETURN:
2536 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002537 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002538 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002539
2540 if (trystack->ga_len > 0)
2541 trycmd = ((trycmd_T *)trystack->ga_data)
2542 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002543 if (trycmd != NULL
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002544 && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002546 // jump to ":finally" or ":endtry"
2547 if (trycmd->tcd_finally_idx != 0)
2548 ectx.ec_iidx = trycmd->tcd_finally_idx;
2549 else
2550 ectx.ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 trycmd->tcd_return = TRUE;
2552 }
2553 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002554 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 }
2556 break;
2557
2558 // push a function reference to a compiled function
2559 case ISN_FUNCREF:
2560 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002561 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2562 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2563 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 if (pt == NULL)
2566 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002567 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002568 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002569 vim_free(pt);
2570 goto failed;
2571 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002572 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2573 &ectx) == FAIL)
2574 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 tv = STACK_TV_BOT(0);
2577 ++ectx.ec_stack.ga_len;
2578 tv->vval.v_partial = pt;
2579 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002580 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 }
2582 break;
2583
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002584 // Create a global function from a lambda.
2585 case ISN_NEWFUNC:
2586 {
2587 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2588
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002589 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002590 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002591 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002592 }
2593 break;
2594
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002595 // List functions
2596 case ISN_DEF:
2597 if (iptr->isn_arg.string == NULL)
2598 list_functions(NULL);
2599 else
2600 {
2601 exarg_T ea;
2602
2603 CLEAR_FIELD(ea);
2604 ea.cmd = ea.arg = iptr->isn_arg.string;
2605 define_function(&ea, NULL);
2606 }
2607 break;
2608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 // jump if a condition is met
2610 case ISN_JUMP:
2611 {
2612 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002613 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 int jump = TRUE;
2615
2616 if (when != JUMP_ALWAYS)
2617 {
2618 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002619 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002620 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002621 || when == JUMP_IF_COND_TRUE)
2622 {
2623 SOURCING_LNUM = iptr->isn_lnum;
2624 jump = tv_get_bool_chk(tv, &error);
2625 if (error)
2626 goto on_error;
2627 }
2628 else
2629 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002631 || when == JUMP_AND_KEEP_IF_FALSE
2632 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002634 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 {
2636 // drop the value from the stack
2637 clear_tv(tv);
2638 --ectx.ec_stack.ga_len;
2639 }
2640 }
2641 if (jump)
2642 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2643 }
2644 break;
2645
2646 // top of a for loop
2647 case ISN_FOR:
2648 {
2649 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2650 typval_T *idxtv =
2651 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2652
2653 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002654 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002656 ++idxtv->vval.v_number;
2657 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 // past the end of the list, jump to "endfor"
2659 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2660 else if (list->lv_first == &range_list_item)
2661 {
2662 // non-materialized range() list
2663 tv = STACK_TV_BOT(0);
2664 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002665 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 tv->vval.v_number = list_find_nr(
2667 list, idxtv->vval.v_number, NULL);
2668 ++ectx.ec_stack.ga_len;
2669 }
2670 else
2671 {
2672 listitem_T *li = list_find(list, idxtv->vval.v_number);
2673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2675 ++ectx.ec_stack.ga_len;
2676 }
2677 }
2678 break;
2679
2680 // start of ":try" block
2681 case ISN_TRY:
2682 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002683 trycmd_T *trycmd = NULL;
2684
Bram Moolenaar270d0382020-05-15 21:42:53 +02002685 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 goto failed;
2687 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2688 + ectx.ec_trystack.ga_len;
2689 ++ectx.ec_trystack.ga_len;
2690 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002691 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002692 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002693 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002694 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_ref->try_catch;
2695 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_ref->try_finally;
2696 trycmd->tcd_endtry_idx = iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 }
2698 break;
2699
2700 case ISN_PUSHEXC:
2701 if (current_exception == NULL)
2702 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002703 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 iemsg("Evaluating catch while current_exception is NULL");
2705 goto failed;
2706 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002707 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 goto failed;
2709 tv = STACK_TV_BOT(0);
2710 ++ectx.ec_stack.ga_len;
2711 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002712 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 tv->vval.v_string = vim_strsave(
2714 (char_u *)current_exception->value);
2715 break;
2716
2717 case ISN_CATCH:
2718 {
2719 garray_T *trystack = &ectx.ec_trystack;
2720
Bram Moolenaar20a76292020-12-25 19:47:24 +01002721 if (restore_cmdmod)
2722 {
2723 cmdmod.cmod_filter_regmatch.regprog = NULL;
2724 undo_cmdmod(&cmdmod);
2725 cmdmod = save_cmdmod;
2726 restore_cmdmod = FALSE;
2727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 if (trystack->ga_len > 0)
2729 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002730 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 + trystack->ga_len - 1;
2732 trycmd->tcd_caught = TRUE;
2733 }
2734 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002735 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 catch_exception(current_exception);
2737 }
2738 break;
2739
Bram Moolenaarc150c092021-02-13 15:02:46 +01002740 case ISN_TRYCONT:
2741 {
2742 garray_T *trystack = &ectx.ec_trystack;
2743 trycont_T *trycont = &iptr->isn_arg.trycont;
2744 int i;
2745 trycmd_T *trycmd;
2746 int iidx = trycont->tct_where;
2747
2748 if (trystack->ga_len < trycont->tct_levels)
2749 {
2750 siemsg("TRYCONT: expected %d levels, found %d",
2751 trycont->tct_levels, trystack->ga_len);
2752 goto failed;
2753 }
2754 // Make :endtry jump to any outer try block and the last
2755 // :endtry inside the loop to the loop start.
2756 for (i = trycont->tct_levels; i > 0; --i)
2757 {
2758 trycmd = ((trycmd_T *)trystack->ga_data)
2759 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002760 // Add one to tcd_cont to be able to jump to
2761 // instruction with index zero.
2762 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002763 iidx = trycmd->tcd_finally_idx == 0
2764 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002765 }
2766 // jump to :finally or :endtry of current try statement
2767 ectx.ec_iidx = iidx;
2768 }
2769 break;
2770
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002771 case ISN_FINALLY:
2772 {
2773 garray_T *trystack = &ectx.ec_trystack;
2774 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2775 + trystack->ga_len - 1;
2776
2777 // Reset the index to avoid a return statement jumps here
2778 // again.
2779 trycmd->tcd_finally_idx = 0;
2780 break;
2781 }
2782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 // end of ":try" block
2784 case ISN_ENDTRY:
2785 {
2786 garray_T *trystack = &ectx.ec_trystack;
2787
2788 if (trystack->ga_len > 0)
2789 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002790 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 --trystack->ga_len;
2793 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002794 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 trycmd = ((trycmd_T *)trystack->ga_data)
2796 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002797 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 {
2799 // discard the exception
2800 if (caught_stack == current_exception)
2801 caught_stack = caught_stack->caught;
2802 discard_current_exception();
2803 }
2804
2805 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002806 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002807
2808 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2809 {
2810 --ectx.ec_stack.ga_len;
2811 clear_tv(STACK_TV_BOT(0));
2812 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002813 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002814 // handling :continue: jump to outer try block or
2815 // start of the loop
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002816 ectx.ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 }
2818 }
2819 break;
2820
2821 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002822 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002823 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002824
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002825 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2826 {
2827 // throwing an exception while using "silent!" causes
2828 // the function to abort but not display an error.
2829 tv = STACK_TV_BOT(-1);
2830 clear_tv(tv);
2831 tv->v_type = VAR_NUMBER;
2832 tv->vval.v_number = 0;
2833 goto done;
2834 }
2835 --ectx.ec_stack.ga_len;
2836 tv = STACK_TV_BOT(0);
2837 if (tv->vval.v_string == NULL
2838 || *skipwhite(tv->vval.v_string) == NUL)
2839 {
2840 vim_free(tv->vval.v_string);
2841 SOURCING_LNUM = iptr->isn_lnum;
2842 emsg(_(e_throw_with_empty_string));
2843 goto failed;
2844 }
2845
2846 // Inside a "catch" we need to first discard the caught
2847 // exception.
2848 if (trystack->ga_len > 0)
2849 {
2850 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2851 + trystack->ga_len - 1;
2852 if (trycmd->tcd_caught && current_exception != NULL)
2853 {
2854 // discard the exception
2855 if (caught_stack == current_exception)
2856 caught_stack = caught_stack->caught;
2857 discard_current_exception();
2858 trycmd->tcd_caught = FALSE;
2859 }
2860 }
2861
2862 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2863 == FAIL)
2864 {
2865 vim_free(tv->vval.v_string);
2866 goto failed;
2867 }
2868 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 break;
2871
2872 // compare with special values
2873 case ISN_COMPAREBOOL:
2874 case ISN_COMPARESPECIAL:
2875 {
2876 typval_T *tv1 = STACK_TV_BOT(-2);
2877 typval_T *tv2 = STACK_TV_BOT(-1);
2878 varnumber_T arg1 = tv1->vval.v_number;
2879 varnumber_T arg2 = tv2->vval.v_number;
2880 int res;
2881
2882 switch (iptr->isn_arg.op.op_type)
2883 {
2884 case EXPR_EQUAL: res = arg1 == arg2; break;
2885 case EXPR_NEQUAL: res = arg1 != arg2; break;
2886 default: res = 0; break;
2887 }
2888
2889 --ectx.ec_stack.ga_len;
2890 tv1->v_type = VAR_BOOL;
2891 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2892 }
2893 break;
2894
2895 // Operation with two number arguments
2896 case ISN_OPNR:
2897 case ISN_COMPARENR:
2898 {
2899 typval_T *tv1 = STACK_TV_BOT(-2);
2900 typval_T *tv2 = STACK_TV_BOT(-1);
2901 varnumber_T arg1 = tv1->vval.v_number;
2902 varnumber_T arg2 = tv2->vval.v_number;
2903 varnumber_T res;
2904
2905 switch (iptr->isn_arg.op.op_type)
2906 {
2907 case EXPR_MULT: res = arg1 * arg2; break;
2908 case EXPR_DIV: res = arg1 / arg2; break;
2909 case EXPR_REM: res = arg1 % arg2; break;
2910 case EXPR_SUB: res = arg1 - arg2; break;
2911 case EXPR_ADD: res = arg1 + arg2; break;
2912
2913 case EXPR_EQUAL: res = arg1 == arg2; break;
2914 case EXPR_NEQUAL: res = arg1 != arg2; break;
2915 case EXPR_GREATER: res = arg1 > arg2; break;
2916 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2917 case EXPR_SMALLER: res = arg1 < arg2; break;
2918 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2919 default: res = 0; break;
2920 }
2921
2922 --ectx.ec_stack.ga_len;
2923 if (iptr->isn_type == ISN_COMPARENR)
2924 {
2925 tv1->v_type = VAR_BOOL;
2926 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2927 }
2928 else
2929 tv1->vval.v_number = res;
2930 }
2931 break;
2932
2933 // Computation with two float arguments
2934 case ISN_OPFLOAT:
2935 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002936#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 {
2938 typval_T *tv1 = STACK_TV_BOT(-2);
2939 typval_T *tv2 = STACK_TV_BOT(-1);
2940 float_T arg1 = tv1->vval.v_float;
2941 float_T arg2 = tv2->vval.v_float;
2942 float_T res = 0;
2943 int cmp = FALSE;
2944
2945 switch (iptr->isn_arg.op.op_type)
2946 {
2947 case EXPR_MULT: res = arg1 * arg2; break;
2948 case EXPR_DIV: res = arg1 / arg2; break;
2949 case EXPR_SUB: res = arg1 - arg2; break;
2950 case EXPR_ADD: res = arg1 + arg2; break;
2951
2952 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2953 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2954 case EXPR_GREATER: cmp = arg1 > arg2; break;
2955 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2956 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2957 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2958 default: cmp = 0; break;
2959 }
2960 --ectx.ec_stack.ga_len;
2961 if (iptr->isn_type == ISN_COMPAREFLOAT)
2962 {
2963 tv1->v_type = VAR_BOOL;
2964 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2965 }
2966 else
2967 tv1->vval.v_float = res;
2968 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002969#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 break;
2971
2972 case ISN_COMPARELIST:
2973 {
2974 typval_T *tv1 = STACK_TV_BOT(-2);
2975 typval_T *tv2 = STACK_TV_BOT(-1);
2976 list_T *arg1 = tv1->vval.v_list;
2977 list_T *arg2 = tv2->vval.v_list;
2978 int cmp = FALSE;
2979 int ic = iptr->isn_arg.op.op_ic;
2980
2981 switch (iptr->isn_arg.op.op_type)
2982 {
2983 case EXPR_EQUAL: cmp =
2984 list_equal(arg1, arg2, ic, FALSE); break;
2985 case EXPR_NEQUAL: cmp =
2986 !list_equal(arg1, arg2, ic, FALSE); break;
2987 case EXPR_IS: cmp = arg1 == arg2; break;
2988 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2989 default: cmp = 0; break;
2990 }
2991 --ectx.ec_stack.ga_len;
2992 clear_tv(tv1);
2993 clear_tv(tv2);
2994 tv1->v_type = VAR_BOOL;
2995 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2996 }
2997 break;
2998
2999 case ISN_COMPAREBLOB:
3000 {
3001 typval_T *tv1 = STACK_TV_BOT(-2);
3002 typval_T *tv2 = STACK_TV_BOT(-1);
3003 blob_T *arg1 = tv1->vval.v_blob;
3004 blob_T *arg2 = tv2->vval.v_blob;
3005 int cmp = FALSE;
3006
3007 switch (iptr->isn_arg.op.op_type)
3008 {
3009 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3010 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3011 case EXPR_IS: cmp = arg1 == arg2; break;
3012 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3013 default: cmp = 0; break;
3014 }
3015 --ectx.ec_stack.ga_len;
3016 clear_tv(tv1);
3017 clear_tv(tv2);
3018 tv1->v_type = VAR_BOOL;
3019 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3020 }
3021 break;
3022
3023 // TODO: handle separately
3024 case ISN_COMPARESTRING:
3025 case ISN_COMPAREDICT:
3026 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 case ISN_COMPAREANY:
3028 {
3029 typval_T *tv1 = STACK_TV_BOT(-2);
3030 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003031 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 int ic = iptr->isn_arg.op.op_ic;
3033
Bram Moolenaareb26f432020-09-14 16:50:05 +02003034 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003035 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 --ectx.ec_stack.ga_len;
3038 }
3039 break;
3040
3041 case ISN_ADDLIST:
3042 case ISN_ADDBLOB:
3043 {
3044 typval_T *tv1 = STACK_TV_BOT(-2);
3045 typval_T *tv2 = STACK_TV_BOT(-1);
3046
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003047 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 if (iptr->isn_type == ISN_ADDLIST)
3049 eval_addlist(tv1, tv2);
3050 else
3051 eval_addblob(tv1, tv2);
3052 clear_tv(tv2);
3053 --ectx.ec_stack.ga_len;
3054 }
3055 break;
3056
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003057 case ISN_LISTAPPEND:
3058 {
3059 typval_T *tv1 = STACK_TV_BOT(-2);
3060 typval_T *tv2 = STACK_TV_BOT(-1);
3061 list_T *l = tv1->vval.v_list;
3062
3063 // add an item to a list
3064 if (l == NULL)
3065 {
3066 SOURCING_LNUM = iptr->isn_lnum;
3067 emsg(_(e_cannot_add_to_null_list));
3068 goto on_error;
3069 }
3070 if (list_append_tv(l, tv2) == FAIL)
3071 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003072 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003073 --ectx.ec_stack.ga_len;
3074 }
3075 break;
3076
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003077 case ISN_BLOBAPPEND:
3078 {
3079 typval_T *tv1 = STACK_TV_BOT(-2);
3080 typval_T *tv2 = STACK_TV_BOT(-1);
3081 blob_T *b = tv1->vval.v_blob;
3082 int error = FALSE;
3083 varnumber_T n;
3084
3085 // add a number to a blob
3086 if (b == NULL)
3087 {
3088 SOURCING_LNUM = iptr->isn_lnum;
3089 emsg(_(e_cannot_add_to_null_blob));
3090 goto on_error;
3091 }
3092 n = tv_get_number_chk(tv2, &error);
3093 if (error)
3094 goto on_error;
3095 ga_append(&b->bv_ga, (int)n);
3096 --ectx.ec_stack.ga_len;
3097 }
3098 break;
3099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 // Computation with two arguments of unknown type
3101 case ISN_OPANY:
3102 {
3103 typval_T *tv1 = STACK_TV_BOT(-2);
3104 typval_T *tv2 = STACK_TV_BOT(-1);
3105 varnumber_T n1, n2;
3106#ifdef FEAT_FLOAT
3107 float_T f1 = 0, f2 = 0;
3108#endif
3109 int error = FALSE;
3110
3111 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3112 {
3113 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3114 {
3115 eval_addlist(tv1, tv2);
3116 clear_tv(tv2);
3117 --ectx.ec_stack.ga_len;
3118 break;
3119 }
3120 else if (tv1->v_type == VAR_BLOB
3121 && tv2->v_type == VAR_BLOB)
3122 {
3123 eval_addblob(tv1, tv2);
3124 clear_tv(tv2);
3125 --ectx.ec_stack.ga_len;
3126 break;
3127 }
3128 }
3129#ifdef FEAT_FLOAT
3130 if (tv1->v_type == VAR_FLOAT)
3131 {
3132 f1 = tv1->vval.v_float;
3133 n1 = 0;
3134 }
3135 else
3136#endif
3137 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003138 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 n1 = tv_get_number_chk(tv1, &error);
3140 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003141 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142#ifdef FEAT_FLOAT
3143 if (tv2->v_type == VAR_FLOAT)
3144 f1 = n1;
3145#endif
3146 }
3147#ifdef FEAT_FLOAT
3148 if (tv2->v_type == VAR_FLOAT)
3149 {
3150 f2 = tv2->vval.v_float;
3151 n2 = 0;
3152 }
3153 else
3154#endif
3155 {
3156 n2 = tv_get_number_chk(tv2, &error);
3157 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003158 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159#ifdef FEAT_FLOAT
3160 if (tv1->v_type == VAR_FLOAT)
3161 f2 = n2;
3162#endif
3163 }
3164#ifdef FEAT_FLOAT
3165 // if there is a float on either side the result is a float
3166 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3167 {
3168 switch (iptr->isn_arg.op.op_type)
3169 {
3170 case EXPR_MULT: f1 = f1 * f2; break;
3171 case EXPR_DIV: f1 = f1 / f2; break;
3172 case EXPR_SUB: f1 = f1 - f2; break;
3173 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003174 default: SOURCING_LNUM = iptr->isn_lnum;
3175 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003176 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 }
3178 clear_tv(tv1);
3179 clear_tv(tv2);
3180 tv1->v_type = VAR_FLOAT;
3181 tv1->vval.v_float = f1;
3182 --ectx.ec_stack.ga_len;
3183 }
3184 else
3185#endif
3186 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003187 int failed = FALSE;
3188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 switch (iptr->isn_arg.op.op_type)
3190 {
3191 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003192 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3193 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003194 goto on_error;
3195 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 case EXPR_SUB: n1 = n1 - n2; break;
3197 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003198 default: n1 = num_modulus(n1, n2, &failed);
3199 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003200 goto on_error;
3201 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 }
3203 clear_tv(tv1);
3204 clear_tv(tv2);
3205 tv1->v_type = VAR_NUMBER;
3206 tv1->vval.v_number = n1;
3207 --ectx.ec_stack.ga_len;
3208 }
3209 }
3210 break;
3211
3212 case ISN_CONCAT:
3213 {
3214 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3215 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3216 char_u *res;
3217
3218 res = concat_str(str1, str2);
3219 clear_tv(STACK_TV_BOT(-2));
3220 clear_tv(STACK_TV_BOT(-1));
3221 --ectx.ec_stack.ga_len;
3222 STACK_TV_BOT(-1)->vval.v_string = res;
3223 }
3224 break;
3225
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003226 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003227 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003228 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003229 int is_slice = iptr->isn_type == ISN_STRSLICE;
3230 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003231 char_u *res;
3232
3233 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003234 // string slice: string is at stack-3, first index at
3235 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003236 if (is_slice)
3237 {
3238 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003239 n1 = tv->vval.v_number;
3240 }
3241
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003242 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003243 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003244
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003245 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003246 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003247 if (is_slice)
3248 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003249 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003250 else
3251 // Index: The resulting variable is a string of a
3252 // single character. If the index is too big or
3253 // negative the result is empty.
3254 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003255 vim_free(tv->vval.v_string);
3256 tv->vval.v_string = res;
3257 }
3258 break;
3259
3260 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003261 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 {
Bram Moolenaared591872020-08-15 22:14:53 +02003263 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003265 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266
3267 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003268 // list slice: list is at stack-3, indexes at stack-2 and
3269 // stack-1
3270 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 list = tv->vval.v_list;
3272
3273 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003274 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003276
3277 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 {
Bram Moolenaared591872020-08-15 22:14:53 +02003279 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003280 n1 = tv->vval.v_number;
3281 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 }
Bram Moolenaared591872020-08-15 22:14:53 +02003283
3284 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003285 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003286 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003287 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3288 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003289 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 }
3291 break;
3292
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003293 case ISN_ANYINDEX:
3294 case ISN_ANYSLICE:
3295 {
3296 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3297 typval_T *var1, *var2;
3298 int res;
3299
3300 // index: composite is at stack-2, index at stack-1
3301 // slice: composite is at stack-3, indexes at stack-2 and
3302 // stack-1
3303 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003304 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003305 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3306 goto on_error;
3307 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3308 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003309 res = eval_index_inner(tv, is_slice, var1, var2,
3310 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003311 clear_tv(var1);
3312 if (is_slice)
3313 clear_tv(var2);
3314 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3315 if (res == FAIL)
3316 goto on_error;
3317 }
3318 break;
3319
Bram Moolenaar9af78762020-06-16 11:34:42 +02003320 case ISN_SLICE:
3321 {
3322 list_T *list;
3323 int count = iptr->isn_arg.number;
3324
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003325 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003326 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003327 list = tv->vval.v_list;
3328
3329 // no error for short list, expect it to be checked earlier
3330 if (list != NULL && list->lv_len >= count)
3331 {
3332 list_T *newlist = list_slice(list,
3333 count, list->lv_len - 1);
3334
3335 if (newlist != NULL)
3336 {
3337 list_unref(list);
3338 tv->vval.v_list = newlist;
3339 ++newlist->lv_refcount;
3340 }
3341 }
3342 }
3343 break;
3344
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003345 case ISN_GETITEM:
3346 {
3347 listitem_T *li;
3348 int index = iptr->isn_arg.number;
3349
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003350 // Get list item: list is at stack-1, push item.
3351 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003352 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003353 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003354
3355 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3356 goto failed;
3357 ++ectx.ec_stack.ga_len;
3358 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003359
3360 // Useful when used in unpack assignment. Reset at
3361 // ISN_DROP.
3362 where.wt_index = index + 1;
3363 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003364 }
3365 break;
3366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 case ISN_MEMBER:
3368 {
3369 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003370 char_u *key;
3371 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003372 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003373
3374 // dict member: dict is at stack-2, key at stack-1
3375 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003376 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003377 dict = tv->vval.v_dict;
3378
3379 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003380 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003381 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003382 if (key == NULL)
3383 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003384
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003385 if ((di = dict_find(dict, key, -1)) == NULL)
3386 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003387 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003388 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003389
3390 // If :silent! is used we will continue, make sure the
3391 // stack contents makes sense.
3392 clear_tv(tv);
3393 --ectx.ec_stack.ga_len;
3394 tv = STACK_TV_BOT(-1);
3395 clear_tv(tv);
3396 tv->v_type = VAR_NUMBER;
3397 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003398 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003399 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003400 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003401 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003402 // Clear the dict only after getting the item, to avoid
3403 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003404 tv = STACK_TV_BOT(-1);
3405 temp_tv = *tv;
3406 copy_tv(&di->di_tv, tv);
3407 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003408 }
3409 break;
3410
3411 // dict member with string key
3412 case ISN_STRINGMEMBER:
3413 {
3414 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003416 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417
3418 tv = STACK_TV_BOT(-1);
3419 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3420 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003421 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003423 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 }
3425 dict = tv->vval.v_dict;
3426
3427 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3428 == NULL)
3429 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003430 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003432 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003434 // Clear the dict after getting the item, to avoid that it
3435 // make the item invalid.
3436 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003438 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 }
3440 break;
3441
3442 case ISN_NEGATENR:
3443 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003444 if (tv->v_type != VAR_NUMBER
3445#ifdef FEAT_FLOAT
3446 && tv->v_type != VAR_FLOAT
3447#endif
3448 )
3449 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003450 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003451 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003452 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003453 }
3454#ifdef FEAT_FLOAT
3455 if (tv->v_type == VAR_FLOAT)
3456 tv->vval.v_float = -tv->vval.v_float;
3457 else
3458#endif
3459 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 break;
3461
3462 case ISN_CHECKNR:
3463 {
3464 int error = FALSE;
3465
3466 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003467 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003469 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 (void)tv_get_number_chk(tv, &error);
3471 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003472 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 }
3474 break;
3475
3476 case ISN_CHECKTYPE:
3477 {
3478 checktype_T *ct = &iptr->isn_arg.type;
3479
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003480 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003481 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003482 if (!where.wt_variable)
3483 where.wt_index = ct->ct_arg_idx;
3484 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003485 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003486 if (!where.wt_variable)
3487 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003488
3489 // number 0 is FALSE, number 1 is TRUE
3490 if (tv->v_type == VAR_NUMBER
3491 && ct->ct_type->tt_type == VAR_BOOL
3492 && (tv->vval.v_number == 0
3493 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003495 tv->v_type = VAR_BOOL;
3496 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003497 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 }
3499 }
3500 break;
3501
Bram Moolenaar9af78762020-06-16 11:34:42 +02003502 case ISN_CHECKLEN:
3503 {
3504 int min_len = iptr->isn_arg.checklen.cl_min_len;
3505 list_T *list = NULL;
3506
3507 tv = STACK_TV_BOT(-1);
3508 if (tv->v_type == VAR_LIST)
3509 list = tv->vval.v_list;
3510 if (list == NULL || list->lv_len < min_len
3511 || (list->lv_len > min_len
3512 && !iptr->isn_arg.checklen.cl_more_OK))
3513 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003514 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003515 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003516 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003517 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003518 }
3519 }
3520 break;
3521
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003522 case ISN_SETTYPE:
3523 {
3524 checktype_T *ct = &iptr->isn_arg.type;
3525
3526 tv = STACK_TV_BOT(-1);
3527 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3528 {
3529 free_type(tv->vval.v_dict->dv_type);
3530 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3531 }
3532 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3533 {
3534 free_type(tv->vval.v_list->lv_type);
3535 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3536 }
3537 }
3538 break;
3539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003541 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 {
3543 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003544 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545
3546 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003547 if (iptr->isn_type == ISN_2BOOL)
3548 {
3549 n = tv2bool(tv);
3550 if (iptr->isn_arg.number) // invert
3551 n = !n;
3552 }
3553 else
3554 {
3555 SOURCING_LNUM = iptr->isn_lnum;
3556 n = tv_get_bool_chk(tv, &error);
3557 if (error)
3558 goto on_error;
3559 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 clear_tv(tv);
3561 tv->v_type = VAR_BOOL;
3562 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3563 }
3564 break;
3565
3566 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003567 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003568 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003569 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3570 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3571 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 break;
3573
Bram Moolenaar08597872020-12-10 19:43:40 +01003574 case ISN_RANGE:
3575 {
3576 exarg_T ea;
3577 char *errormsg;
3578
Bram Moolenaarece0b872021-01-08 20:40:45 +01003579 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003580 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003581 ea.addr_type = ADDR_LINES;
3582 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003583 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003584 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003585 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003586
3587 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3588 goto failed;
3589 ++ectx.ec_stack.ga_len;
3590 tv = STACK_TV_BOT(-1);
3591 tv->v_type = VAR_NUMBER;
3592 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003593 if (ea.addr_count == 0)
3594 tv->vval.v_number = curwin->w_cursor.lnum;
3595 else
3596 tv->vval.v_number = ea.line2;
3597 }
3598 break;
3599
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003600 case ISN_PUT:
3601 {
3602 int regname = iptr->isn_arg.put.put_regname;
3603 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3604 char_u *expr = NULL;
3605 int dir = FORWARD;
3606
Bram Moolenaar08597872020-12-10 19:43:40 +01003607 if (lnum < -2)
3608 {
3609 // line number was put on the stack by ISN_RANGE
3610 tv = STACK_TV_BOT(-1);
3611 curwin->w_cursor.lnum = tv->vval.v_number;
3612 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3613 dir = BACKWARD;
3614 --ectx.ec_stack.ga_len;
3615 }
3616 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003617 // :put! above cursor
3618 dir = BACKWARD;
3619 else if (lnum >= 0)
3620 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003621
3622 if (regname == '=')
3623 {
3624 tv = STACK_TV_BOT(-1);
3625 if (tv->v_type == VAR_STRING)
3626 expr = tv->vval.v_string;
3627 else
3628 {
3629 expr = typval2string(tv, TRUE); // allocates value
3630 clear_tv(tv);
3631 }
3632 --ectx.ec_stack.ga_len;
3633 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003634 check_cursor();
3635 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3636 vim_free(expr);
3637 }
3638 break;
3639
Bram Moolenaar02194d22020-10-24 23:08:38 +02003640 case ISN_CMDMOD:
3641 save_cmdmod = cmdmod;
3642 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003643 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003644 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3645 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003646 break;
3647
Bram Moolenaar02194d22020-10-24 23:08:38 +02003648 case ISN_CMDMOD_REV:
3649 // filter regprog is owned by the instruction, don't free it
3650 cmdmod.cmod_filter_regmatch.regprog = NULL;
3651 undo_cmdmod(&cmdmod);
3652 cmdmod = save_cmdmod;
3653 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003654 break;
3655
Bram Moolenaar792f7862020-11-23 08:31:18 +01003656 case ISN_UNPACK:
3657 {
3658 int count = iptr->isn_arg.unpack.unp_count;
3659 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3660 list_T *l;
3661 listitem_T *li;
3662 int i;
3663
3664 // Check there is a valid list to unpack.
3665 tv = STACK_TV_BOT(-1);
3666 if (tv->v_type != VAR_LIST)
3667 {
3668 SOURCING_LNUM = iptr->isn_lnum;
3669 emsg(_(e_for_argument_must_be_sequence_of_lists));
3670 goto on_error;
3671 }
3672 l = tv->vval.v_list;
3673 if (l == NULL
3674 || l->lv_len < (semicolon ? count - 1 : count))
3675 {
3676 SOURCING_LNUM = iptr->isn_lnum;
3677 emsg(_(e_list_value_does_not_have_enough_items));
3678 goto on_error;
3679 }
3680 else if (!semicolon && l->lv_len > count)
3681 {
3682 SOURCING_LNUM = iptr->isn_lnum;
3683 emsg(_(e_list_value_has_more_items_than_targets));
3684 goto on_error;
3685 }
3686
3687 CHECK_LIST_MATERIALIZE(l);
3688 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3689 goto failed;
3690 ectx.ec_stack.ga_len += count - 1;
3691
3692 // Variable after semicolon gets a list with the remaining
3693 // items.
3694 if (semicolon)
3695 {
3696 list_T *rem_list =
3697 list_alloc_with_items(l->lv_len - count + 1);
3698
3699 if (rem_list == NULL)
3700 goto failed;
3701 tv = STACK_TV_BOT(-count);
3702 tv->vval.v_list = rem_list;
3703 ++rem_list->lv_refcount;
3704 tv->v_lock = 0;
3705 li = l->lv_first;
3706 for (i = 0; i < count - 1; ++i)
3707 li = li->li_next;
3708 for (i = 0; li != NULL; ++i)
3709 {
3710 list_set_item(rem_list, i, &li->li_tv);
3711 li = li->li_next;
3712 }
3713 --count;
3714 }
3715
3716 // Produce the values in reverse order, first item last.
3717 li = l->lv_first;
3718 for (i = 0; i < count; ++i)
3719 {
3720 tv = STACK_TV_BOT(-i - 1);
3721 copy_tv(&li->li_tv, tv);
3722 li = li->li_next;
3723 }
3724
3725 list_unref(l);
3726 }
3727 break;
3728
Bram Moolenaarb2049902021-01-24 12:53:53 +01003729 case ISN_PROF_START:
3730 case ISN_PROF_END:
3731 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003732#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003733 funccall_T cookie;
3734 ufunc_T *cur_ufunc =
3735 (((dfunc_T *)def_functions.ga_data)
3736 + ectx.ec_dfunc_idx)->df_ufunc;
3737
3738 cookie.func = cur_ufunc;
3739 if (iptr->isn_type == ISN_PROF_START)
3740 {
3741 func_line_start(&cookie, iptr->isn_lnum);
3742 // if we get here the instruction is executed
3743 func_line_exec(&cookie);
3744 }
3745 else
3746 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003747#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003748 }
3749 break;
3750
Bram Moolenaar389df252020-07-09 21:20:47 +02003751 case ISN_SHUFFLE:
3752 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003753 typval_T tmp_tv;
3754 int item = iptr->isn_arg.shuffle.shfl_item;
3755 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003756
3757 tmp_tv = *STACK_TV_BOT(-item);
3758 for ( ; up > 0 && item > 1; --up)
3759 {
3760 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3761 --item;
3762 }
3763 *STACK_TV_BOT(-item) = tmp_tv;
3764 }
3765 break;
3766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 case ISN_DROP:
3768 --ectx.ec_stack.ga_len;
3769 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003770 where.wt_index = 0;
3771 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 break;
3773 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003774 continue;
3775
Bram Moolenaard032f342020-07-18 18:13:02 +02003776func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003777 // Restore previous function. If the frame pointer is where we started
3778 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003779 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003780 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003781
Bram Moolenaard032f342020-07-18 18:13:02 +02003782 if (func_return(&ectx) == FAIL)
3783 // only fails when out of memory
3784 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003785 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003786
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003787on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003788 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003789 // If "emsg_silent" is set then ignore the error, unless it was set
3790 // when calling the function.
3791 if (did_emsg_cumul + did_emsg == did_emsg_before
3792 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003793 {
3794 // If a sequence of instructions causes an error while ":silent!"
3795 // was used, restore the stack length and jump ahead to restoring
3796 // the cmdmod.
3797 if (restore_cmdmod)
3798 {
3799 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3800 {
3801 --ectx.ec_stack.ga_len;
3802 clear_tv(STACK_TV_BOT(0));
3803 }
3804 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3805 ++ectx.ec_iidx;
3806 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003807 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003808 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003809on_fatal_error:
3810 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003811 // If we are not inside a try-catch started here, abort execution.
3812 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003813 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 }
3815
3816done:
3817 // function finished, get result from the stack.
3818 tv = STACK_TV_BOT(-1);
3819 *rettv = *tv;
3820 tv->v_type = VAR_UNKNOWN;
3821 ret = OK;
3822
3823failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003824 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003825 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003826 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003827
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003828 // Deal with any remaining closures, they may be in use somewhere.
3829 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003830 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003831 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003832 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3833 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003834
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003835 estack_pop();
3836 current_sctx = save_current_sctx;
3837
Bram Moolenaar352134b2020-10-17 22:04:08 +02003838 if (*msg_list != NULL && saved_msg_list != NULL)
3839 {
3840 msglist_T **plist = saved_msg_list;
3841
3842 // Append entries from the current msg_list (uncaught exceptions) to
3843 // the saved msg_list.
3844 while (*plist != NULL)
3845 plist = &(*plist)->next;
3846
3847 *plist = *msg_list;
3848 }
3849 msg_list = saved_msg_list;
3850
Bram Moolenaar02194d22020-10-24 23:08:38 +02003851 if (restore_cmdmod)
3852 {
3853 cmdmod.cmod_filter_regmatch.regprog = NULL;
3854 undo_cmdmod(&cmdmod);
3855 cmdmod = save_cmdmod;
3856 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003857 emsg_silent_def = save_emsg_silent_def;
3858 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003859
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003860failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003861 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3863 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003866 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003867
Bram Moolenaar0186e582021-01-10 18:33:11 +01003868 while (ectx.ec_outer != NULL)
3869 {
3870 outer_T *up = ectx.ec_outer->out_up_is_copy
3871 ? NULL : ectx.ec_outer->out_up;
3872
3873 vim_free(ectx.ec_outer);
3874 ectx.ec_outer = up;
3875 }
3876
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003877 // Not sure if this is necessary.
3878 suppress_errthrow = save_suppress_errthrow;
3879
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003880 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003881 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003882 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003883 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 return ret;
3885}
3886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003888 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003889 * We don't really need this at runtime, but we do have tests that require it,
3890 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 */
3892 void
3893ex_disassemble(exarg_T *eap)
3894{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003895 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003896 char_u *fname;
3897 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 dfunc_T *dfunc;
3899 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003900 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 int current;
3902 int line_idx = 0;
3903 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003904 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003906 if (STRNCMP(arg, "<lambda>", 8) == 0)
3907 {
3908 arg += 8;
3909 (void)getdigits(&arg);
3910 fname = vim_strnsave(eap->arg, arg - eap->arg);
3911 }
3912 else
3913 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003914 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003915 if (fname == NULL)
3916 {
3917 semsg(_(e_invarg2), eap->arg);
3918 return;
3919 }
3920
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003921 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003922 if (ufunc == NULL)
3923 {
3924 char_u *p = untrans_function_name(fname);
3925
3926 if (p != NULL)
3927 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003928 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003929 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003930 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 if (ufunc == NULL)
3932 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003933 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 return;
3935 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003936 if (func_needs_compiling(ufunc, eap->forceit)
3937 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003938 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003939 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003941 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 return;
3943 }
3944 if (ufunc->uf_name_exp != NULL)
3945 msg((char *)ufunc->uf_name_exp);
3946 else
3947 msg((char *)ufunc->uf_name);
3948
3949 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003950#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003951 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3952 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3953 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003954#else
3955 instr = dfunc->df_instr;
3956 instr_count = dfunc->df_instr_count;
3957#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003958 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 {
3960 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003961 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962
3963 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3964 {
3965 if (current > prev_current)
3966 {
3967 msg_puts("\n\n");
3968 prev_current = current;
3969 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003970 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3971 if (line != NULL)
3972 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 }
3974
3975 switch (iptr->isn_type)
3976 {
3977 case ISN_EXEC:
3978 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3979 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003980 case ISN_EXECCONCAT:
3981 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003982 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003983 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 case ISN_ECHO:
3985 {
3986 echo_T *echo = &iptr->isn_arg.echo;
3987
3988 smsg("%4d %s %d", current,
3989 echo->echo_with_white ? "ECHO" : "ECHON",
3990 echo->echo_count);
3991 }
3992 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003993 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003994 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003995 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003996 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003997 case ISN_ECHOMSG:
3998 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003999 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004000 break;
4001 case ISN_ECHOERR:
4002 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004003 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004004 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004006 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004007 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004008 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004009 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004010 + STACK_FRAME_SIZE));
4011 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004012 smsg("%4d LOAD $%lld", current,
4013 (varnumber_T)(iptr->isn_arg.number));
4014 }
4015 break;
4016 case ISN_LOADOUTER:
4017 {
4018 if (iptr->isn_arg.number < 0)
4019 smsg("%4d LOADOUTER level %d arg[%d]", current,
4020 iptr->isn_arg.outer.outer_depth,
4021 iptr->isn_arg.outer.outer_idx
4022 + STACK_FRAME_SIZE);
4023 else
4024 smsg("%4d LOADOUTER level %d $%d", current,
4025 iptr->isn_arg.outer.outer_depth,
4026 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 break;
4029 case ISN_LOADV:
4030 smsg("%4d LOADV v:%s", current,
4031 get_vim_var_name(iptr->isn_arg.number));
4032 break;
4033 case ISN_LOADSCRIPT:
4034 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004035 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4036 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004038 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004040 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4041 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004042 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004043 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 }
4045 break;
4046 case ISN_LOADS:
4047 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004048 scriptitem_T *si = SCRIPT_ITEM(
4049 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050
4051 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004052 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 }
4054 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004055 case ISN_LOADAUTO:
4056 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4057 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 case ISN_LOADG:
4059 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4060 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004061 case ISN_LOADB:
4062 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4063 break;
4064 case ISN_LOADW:
4065 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4066 break;
4067 case ISN_LOADT:
4068 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4069 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004070 case ISN_LOADGDICT:
4071 smsg("%4d LOAD g:", current);
4072 break;
4073 case ISN_LOADBDICT:
4074 smsg("%4d LOAD b:", current);
4075 break;
4076 case ISN_LOADWDICT:
4077 smsg("%4d LOAD w:", current);
4078 break;
4079 case ISN_LOADTDICT:
4080 smsg("%4d LOAD t:", current);
4081 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 case ISN_LOADOPT:
4083 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4084 break;
4085 case ISN_LOADENV:
4086 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4087 break;
4088 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004089 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 break;
4091
4092 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004093 if (iptr->isn_arg.number < 0)
4094 smsg("%4d STORE arg[%lld]", current,
4095 iptr->isn_arg.number + STACK_FRAME_SIZE);
4096 else
4097 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4098 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004099 case ISN_STOREOUTER:
4100 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004101 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004102 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4103 iptr->isn_arg.outer.outer_depth,
4104 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004105 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004106 smsg("%4d STOREOUTER level %d $%d", current,
4107 iptr->isn_arg.outer.outer_depth,
4108 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004109 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004111 case ISN_STOREV:
4112 smsg("%4d STOREV v:%s", current,
4113 get_vim_var_name(iptr->isn_arg.number));
4114 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004115 case ISN_STOREAUTO:
4116 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4117 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004119 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4120 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004121 case ISN_STOREB:
4122 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4123 break;
4124 case ISN_STOREW:
4125 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4126 break;
4127 case ISN_STORET:
4128 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4129 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004130 case ISN_STORES:
4131 {
4132 scriptitem_T *si = SCRIPT_ITEM(
4133 iptr->isn_arg.loadstore.ls_sid);
4134
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004135 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004136 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 break;
4139 case ISN_STORESCRIPT:
4140 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004141 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4142 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004144 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004146 smsg("%4d STORESCRIPT %s-%d in %s", current,
4147 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004148 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004149 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 }
4151 break;
4152 case ISN_STOREOPT:
4153 smsg("%4d STOREOPT &%s", current,
4154 iptr->isn_arg.storeopt.so_name);
4155 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004156 case ISN_STOREENV:
4157 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4158 break;
4159 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004160 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004161 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 case ISN_STORENR:
4163 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004164 iptr->isn_arg.storenr.stnr_val,
4165 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 break;
4167
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004168 case ISN_STOREINDEX:
4169 switch (iptr->isn_arg.vartype)
4170 {
4171 case VAR_LIST:
4172 smsg("%4d STORELIST", current);
4173 break;
4174 case VAR_DICT:
4175 smsg("%4d STOREDICT", current);
4176 break;
4177 case VAR_ANY:
4178 smsg("%4d STOREINDEX", current);
4179 break;
4180 default: break;
4181 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004182 break;
4183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 // constants
4185 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004186 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004187 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188 break;
4189 case ISN_PUSHBOOL:
4190 case ISN_PUSHSPEC:
4191 smsg("%4d PUSH %s", current,
4192 get_var_special_name(iptr->isn_arg.number));
4193 break;
4194 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004195#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004197#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 break;
4199 case ISN_PUSHS:
4200 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4201 break;
4202 case ISN_PUSHBLOB:
4203 {
4204 char_u *r;
4205 char_u numbuf[NUMBUFLEN];
4206 char_u *tofree;
4207
4208 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004209 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 vim_free(tofree);
4211 }
4212 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004213 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004214 {
4215 char *name = (char *)iptr->isn_arg.string;
4216
4217 smsg("%4d PUSHFUNC \"%s\"", current,
4218 name == NULL ? "[none]" : name);
4219 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004220 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004221 case ISN_PUSHCHANNEL:
4222#ifdef FEAT_JOB_CHANNEL
4223 {
4224 channel_T *channel = iptr->isn_arg.channel;
4225
4226 smsg("%4d PUSHCHANNEL %d", current,
4227 channel == NULL ? 0 : channel->ch_id);
4228 }
4229#endif
4230 break;
4231 case ISN_PUSHJOB:
4232#ifdef FEAT_JOB_CHANNEL
4233 {
4234 typval_T tv;
4235 char_u *name;
4236
4237 tv.v_type = VAR_JOB;
4238 tv.vval.v_job = iptr->isn_arg.job;
4239 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004240 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004241 }
4242#endif
4243 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 case ISN_PUSHEXC:
4245 smsg("%4d PUSH v:exception", current);
4246 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004247 case ISN_UNLET:
4248 smsg("%4d UNLET%s %s", current,
4249 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4250 iptr->isn_arg.unlet.ul_name);
4251 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004252 case ISN_UNLETENV:
4253 smsg("%4d UNLETENV%s $%s", current,
4254 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4255 iptr->isn_arg.unlet.ul_name);
4256 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004257 case ISN_UNLETINDEX:
4258 smsg("%4d UNLETINDEX", current);
4259 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004260 case ISN_UNLETRANGE:
4261 smsg("%4d UNLETRANGE", current);
4262 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004263 case ISN_LOCKCONST:
4264 smsg("%4d LOCKCONST", current);
4265 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004267 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004268 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 break;
4270 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004271 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004272 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 break;
4274
4275 // function call
4276 case ISN_BCALL:
4277 {
4278 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4279
4280 smsg("%4d BCALL %s(argc %d)", current,
4281 internal_func_name(cbfunc->cbf_idx),
4282 cbfunc->cbf_argcount);
4283 }
4284 break;
4285 case ISN_DCALL:
4286 {
4287 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4288 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4289 + cdfunc->cdf_idx;
4290
4291 smsg("%4d DCALL %s(argc %d)", current,
4292 df->df_ufunc->uf_name_exp != NULL
4293 ? df->df_ufunc->uf_name_exp
4294 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4295 }
4296 break;
4297 case ISN_UCALL:
4298 {
4299 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4300
4301 smsg("%4d UCALL %s(argc %d)", current,
4302 cufunc->cuf_name, cufunc->cuf_argcount);
4303 }
4304 break;
4305 case ISN_PCALL:
4306 {
4307 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4308
4309 smsg("%4d PCALL%s (argc %d)", current,
4310 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4311 }
4312 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004313 case ISN_PCALL_END:
4314 smsg("%4d PCALL end", current);
4315 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 case ISN_RETURN:
4317 smsg("%4d RETURN", current);
4318 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004319 case ISN_RETURN_ZERO:
4320 smsg("%4d RETURN 0", current);
4321 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 case ISN_FUNCREF:
4323 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004324 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004326 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004328 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 }
4330 break;
4331
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004332 case ISN_NEWFUNC:
4333 {
4334 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4335
4336 smsg("%4d NEWFUNC %s %s", current,
4337 newfunc->nf_lambda, newfunc->nf_global);
4338 }
4339 break;
4340
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004341 case ISN_DEF:
4342 {
4343 char_u *name = iptr->isn_arg.string;
4344
4345 smsg("%4d DEF %s", current,
4346 name == NULL ? (char_u *)"" : name);
4347 }
4348 break;
4349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 case ISN_JUMP:
4351 {
4352 char *when = "?";
4353
4354 switch (iptr->isn_arg.jump.jump_when)
4355 {
4356 case JUMP_ALWAYS:
4357 when = "JUMP";
4358 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 case JUMP_AND_KEEP_IF_TRUE:
4360 when = "JUMP_AND_KEEP_IF_TRUE";
4361 break;
4362 case JUMP_IF_FALSE:
4363 when = "JUMP_IF_FALSE";
4364 break;
4365 case JUMP_AND_KEEP_IF_FALSE:
4366 when = "JUMP_AND_KEEP_IF_FALSE";
4367 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004368 case JUMP_IF_COND_FALSE:
4369 when = "JUMP_IF_COND_FALSE";
4370 break;
4371 case JUMP_IF_COND_TRUE:
4372 when = "JUMP_IF_COND_TRUE";
4373 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004375 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 iptr->isn_arg.jump.jump_where);
4377 }
4378 break;
4379
4380 case ISN_FOR:
4381 {
4382 forloop_T *forloop = &iptr->isn_arg.forloop;
4383
4384 smsg("%4d FOR $%d -> %d", current,
4385 forloop->for_idx, forloop->for_end);
4386 }
4387 break;
4388
4389 case ISN_TRY:
4390 {
4391 try_T *try = &iptr->isn_arg.try;
4392
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004393 if (try->try_ref->try_finally == 0)
4394 smsg("%4d TRY catch -> %d, endtry -> %d",
4395 current,
4396 try->try_ref->try_catch,
4397 try->try_ref->try_endtry);
4398 else
4399 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4400 current,
4401 try->try_ref->try_catch,
4402 try->try_ref->try_finally,
4403 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 }
4405 break;
4406 case ISN_CATCH:
4407 // TODO
4408 smsg("%4d CATCH", current);
4409 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004410 case ISN_TRYCONT:
4411 {
4412 trycont_T *trycont = &iptr->isn_arg.trycont;
4413
4414 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4415 trycont->tct_levels,
4416 trycont->tct_levels == 1 ? "" : "s",
4417 trycont->tct_where);
4418 }
4419 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004420 case ISN_FINALLY:
4421 smsg("%4d FINALLY", current);
4422 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 case ISN_ENDTRY:
4424 smsg("%4d ENDTRY", current);
4425 break;
4426 case ISN_THROW:
4427 smsg("%4d THROW", current);
4428 break;
4429
4430 // expression operations on number
4431 case ISN_OPNR:
4432 case ISN_OPFLOAT:
4433 case ISN_OPANY:
4434 {
4435 char *what;
4436 char *ins;
4437
4438 switch (iptr->isn_arg.op.op_type)
4439 {
4440 case EXPR_MULT: what = "*"; break;
4441 case EXPR_DIV: what = "/"; break;
4442 case EXPR_REM: what = "%"; break;
4443 case EXPR_SUB: what = "-"; break;
4444 case EXPR_ADD: what = "+"; break;
4445 default: what = "???"; break;
4446 }
4447 switch (iptr->isn_type)
4448 {
4449 case ISN_OPNR: ins = "OPNR"; break;
4450 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4451 case ISN_OPANY: ins = "OPANY"; break;
4452 default: ins = "???"; break;
4453 }
4454 smsg("%4d %s %s", current, ins, what);
4455 }
4456 break;
4457
4458 case ISN_COMPAREBOOL:
4459 case ISN_COMPARESPECIAL:
4460 case ISN_COMPARENR:
4461 case ISN_COMPAREFLOAT:
4462 case ISN_COMPARESTRING:
4463 case ISN_COMPAREBLOB:
4464 case ISN_COMPARELIST:
4465 case ISN_COMPAREDICT:
4466 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 case ISN_COMPAREANY:
4468 {
4469 char *p;
4470 char buf[10];
4471 char *type;
4472
4473 switch (iptr->isn_arg.op.op_type)
4474 {
4475 case EXPR_EQUAL: p = "=="; break;
4476 case EXPR_NEQUAL: p = "!="; break;
4477 case EXPR_GREATER: p = ">"; break;
4478 case EXPR_GEQUAL: p = ">="; break;
4479 case EXPR_SMALLER: p = "<"; break;
4480 case EXPR_SEQUAL: p = "<="; break;
4481 case EXPR_MATCH: p = "=~"; break;
4482 case EXPR_IS: p = "is"; break;
4483 case EXPR_ISNOT: p = "isnot"; break;
4484 case EXPR_NOMATCH: p = "!~"; break;
4485 default: p = "???"; break;
4486 }
4487 STRCPY(buf, p);
4488 if (iptr->isn_arg.op.op_ic == TRUE)
4489 strcat(buf, "?");
4490 switch(iptr->isn_type)
4491 {
4492 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4493 case ISN_COMPARESPECIAL:
4494 type = "COMPARESPECIAL"; break;
4495 case ISN_COMPARENR: type = "COMPARENR"; break;
4496 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4497 case ISN_COMPARESTRING:
4498 type = "COMPARESTRING"; break;
4499 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4500 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4501 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4502 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4504 default: type = "???"; break;
4505 }
4506
4507 smsg("%4d %s %s", current, type, buf);
4508 }
4509 break;
4510
4511 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4512 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4513
4514 // expression operations
4515 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004516 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004517 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004518 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004519 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004520 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004521 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004522 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4523 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004524 case ISN_SLICE: smsg("%4d SLICE %lld",
4525 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004526 case ISN_GETITEM: smsg("%4d ITEM %lld",
4527 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004528 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4529 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 iptr->isn_arg.string); break;
4531 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4532
4533 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004534 case ISN_CHECKTYPE:
4535 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004536 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004537 char *tofree;
4538
Bram Moolenaare32e5162021-01-21 20:21:29 +01004539 if (ct->ct_arg_idx == 0)
4540 smsg("%4d CHECKTYPE %s stack[%d]", current,
4541 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004542 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004543 else
4544 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4545 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004546 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004547 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004548 vim_free(tofree);
4549 break;
4550 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004551 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4552 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4553 iptr->isn_arg.checklen.cl_min_len);
4554 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004555 case ISN_SETTYPE:
4556 {
4557 char *tofree;
4558
4559 smsg("%4d SETTYPE %s", current,
4560 type_name(iptr->isn_arg.type.ct_type, &tofree));
4561 vim_free(tofree);
4562 break;
4563 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004564 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 case ISN_2BOOL: if (iptr->isn_arg.number)
4566 smsg("%4d INVERT (!val)", current);
4567 else
4568 smsg("%4d 2BOOL (!!val)", current);
4569 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004570 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004571 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004572 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004573 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004574 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004575 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004576 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4577 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004578 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004579 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4580 smsg("%4d PUT %c above range",
4581 current, iptr->isn_arg.put.put_regname);
4582 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4583 smsg("%4d PUT %c range",
4584 current, iptr->isn_arg.put.put_regname);
4585 else
4586 smsg("%4d PUT %c %ld", current,
4587 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004588 (long)iptr->isn_arg.put.put_lnum);
4589 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590
Bram Moolenaar02194d22020-10-24 23:08:38 +02004591 // TODO: summarize modifiers
4592 case ISN_CMDMOD:
4593 {
4594 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004595 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004596 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4597
4598 buf = alloc(len + 1);
4599 if (buf != NULL)
4600 {
4601 (void)produce_cmdmods(
4602 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4603 smsg("%4d CMDMOD %s", current, buf);
4604 vim_free(buf);
4605 }
4606 break;
4607 }
4608 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004609
Bram Moolenaarb2049902021-01-24 12:53:53 +01004610 case ISN_PROF_START:
4611 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4612 break;
4613
4614 case ISN_PROF_END:
4615 smsg("%4d PROFILE END", current);
4616 break;
4617
Bram Moolenaar792f7862020-11-23 08:31:18 +01004618 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4619 iptr->isn_arg.unpack.unp_count,
4620 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4621 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004622 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4623 iptr->isn_arg.shuffle.shfl_item,
4624 iptr->isn_arg.shuffle.shfl_up);
4625 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 case ISN_DROP: smsg("%4d DROP", current); break;
4627 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004628
4629 out_flush(); // output one line at a time
4630 ui_breakcheck();
4631 if (got_int)
4632 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634}
4635
4636/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004637 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 * list, etc. Mostly like what JavaScript does, except that empty list and
4639 * empty dictionary are FALSE.
4640 */
4641 int
4642tv2bool(typval_T *tv)
4643{
4644 switch (tv->v_type)
4645 {
4646 case VAR_NUMBER:
4647 return tv->vval.v_number != 0;
4648 case VAR_FLOAT:
4649#ifdef FEAT_FLOAT
4650 return tv->vval.v_float != 0.0;
4651#else
4652 break;
4653#endif
4654 case VAR_PARTIAL:
4655 return tv->vval.v_partial != NULL;
4656 case VAR_FUNC:
4657 case VAR_STRING:
4658 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4659 case VAR_LIST:
4660 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4661 case VAR_DICT:
4662 return tv->vval.v_dict != NULL
4663 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4664 case VAR_BOOL:
4665 case VAR_SPECIAL:
4666 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4667 case VAR_JOB:
4668#ifdef FEAT_JOB_CHANNEL
4669 return tv->vval.v_job != NULL;
4670#else
4671 break;
4672#endif
4673 case VAR_CHANNEL:
4674#ifdef FEAT_JOB_CHANNEL
4675 return tv->vval.v_channel != NULL;
4676#else
4677 break;
4678#endif
4679 case VAR_BLOB:
4680 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4681 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004682 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 case VAR_VOID:
4684 break;
4685 }
4686 return FALSE;
4687}
4688
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004689 void
4690emsg_using_string_as(typval_T *tv, int as_number)
4691{
4692 semsg(_(as_number ? e_using_string_as_number_str
4693 : e_using_string_as_bool_str),
4694 tv->vval.v_string == NULL
4695 ? (char_u *)"" : tv->vval.v_string);
4696}
4697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698/*
4699 * If "tv" is a string give an error and return FAIL.
4700 */
4701 int
4702check_not_string(typval_T *tv)
4703{
4704 if (tv->v_type == VAR_STRING)
4705 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004706 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 clear_tv(tv);
4708 return FAIL;
4709 }
4710 return OK;
4711}
4712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713
4714#endif // FEAT_EVAL