blob: 0de11bab0b01201480c863253132e654e37d9c03 [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 Moolenaar3f987b52021-06-30 12:02:24 +020029 int tcd_save_in_catch; // saved ec_in_catch
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010030 int tcd_catch_idx; // instruction of the first :catch or :finally
31 int tcd_finally_idx; // instruction of the :finally block or zero
32 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010034 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010035 int tcd_return; // when TRUE return from end of :finally
36} trycmd_T;
37
Bram Moolenaar4c137212021-04-19 16:48:48 +020038// Data local to a function.
39// On a function call, if not empty, is saved on the stack and restored when
40// returning.
41typedef struct {
42 int floc_restore_cmdmod;
43 cmdmod_T floc_save_cmdmod;
44 int floc_restore_cmdmod_stacklen;
45} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010046
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020047// Structure to hold a reference to an outer_T, with information of whether it
48// was allocated.
49typedef struct {
50 outer_T *or_outer;
51 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
52 int or_outer_allocated; // free "or_outer" later
53} outer_ref_T;
54
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010055// A stack is used to store:
56// - arguments passed to a :def function
57// - info about the calling function, to use when returning
58// - local variables
59// - temporary values
60//
61// In detail (FP == Frame Pointer):
62// arg1 first argument from caller (if present)
63// arg2 second argument from caller (if present)
64// extra_arg1 any missing optional argument default value
65// FP -> cur_func calling function
66// current previous instruction pointer
67// frame_ptr previous Frame Pointer
68// var1 space for local variable
69// var2 space for local variable
70// .... fixed space for max. number of local variables
71// temp temporary values
72// .... flexible space for temporary values (can grow big)
73
74/*
75 * Execution context.
76 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010077struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020079 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020080 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020082 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020083 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 garray_T ec_trystack; // stack of trycmd_T values
86 int ec_in_catch; // when TRUE in catch or finally block
87
88 int ec_dfunc_idx; // current function index
89 isn_T *ec_instr; // array with instructions
90 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#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 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
125 */
126 static int
127exe_newlist(int count, ectx_T *ectx)
128{
129 list_T *list = list_alloc_with_items(count);
130 int idx;
131 typval_T *tv;
132
133 if (list == NULL)
134 return FAIL;
135 for (idx = 0; idx < count; ++idx)
136 list_set_item(list, idx, STACK_TV_BOT(idx - count));
137
138 if (count > 0)
139 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200140 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200141 return FAIL;
142 else
143 ++ectx->ec_stack.ga_len;
144 tv = STACK_TV_BOT(-1);
145 tv->v_type = VAR_LIST;
146 tv->vval.v_list = list;
147 ++list->lv_refcount;
148 return OK;
149}
150
151/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200152 * If debug_tick changed check if "ufunc" has a breakpoint and update
153 * "uf_has_breakpoint".
154 */
155 static void
156update_has_breakpoint(ufunc_T *ufunc)
157{
158 if (ufunc->uf_debug_tick != debug_tick)
159 {
160 linenr_T breakpoint;
161
162 ufunc->uf_debug_tick = debug_tick;
163 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
164 ufunc->uf_has_breakpoint = breakpoint > 0;
165 }
166}
167
168/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100170 * This adds a stack frame and sets the instruction pointer to the start of the
171 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200172 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 *
174 * Stack has:
175 * - current arguments (already there)
176 * - omitted optional argument (default values) added here
177 * - stack frame:
178 * - pointer to calling function
179 * - Index of next instruction in calling function
180 * - previous frame pointer
181 * - reserved space for local variables
182 */
183 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100184call_dfunc(
185 int cdf_idx,
186 partial_T *pt,
187 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100188 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100189{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100190 int argcount = argcount_arg;
191 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
192 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200193 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100194 int arg_to_add;
195 int vararg_count = 0;
196 int varcount;
197 int idx;
198 estack_T *entry;
199 funclocal_T *floc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100200
201 if (dfunc->df_deleted)
202 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100203 // don't use ufunc->uf_name, it may have been freed
204 emsg_funcname(e_func_deleted,
205 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206 return FAIL;
207 }
208
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100209#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100210 if (do_profiling == PROF_YES)
211 {
212 if (ga_grow(&profile_info_ga, 1) == OK)
213 {
214 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
215 + profile_info_ga.ga_len;
216 ++profile_info_ga.ga_len;
217 CLEAR_POINTER(info);
218 profile_may_start_func(info, ufunc,
219 (((dfunc_T *)def_functions.ga_data)
220 + ectx->ec_dfunc_idx)->df_ufunc);
221 }
222
223 // Profiling might be enabled/disabled along the way. This should not
224 // fail, since the function was compiled before and toggling profiling
225 // doesn't change any errors.
Bram Moolenaare99d4222021-06-13 14:01:26 +0200226 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
227 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100228 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100229 return FAIL;
230 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100231#endif
232
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200233 // Update uf_has_breakpoint if needed.
234 update_has_breakpoint(ufunc);
235
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200236 // When debugging and using "cont" switches to the not-debugged
237 // instructions, may need to still compile them.
238 if ((func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
239 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
240 == FAIL)
241 || INSTRUCTIONS(dfunc) == NULL)
242 {
243 if (did_emsg_cumul + did_emsg == did_emsg_before)
244 semsg(_(e_function_is_not_compiled_str),
245 printable_func_name(ufunc));
246 return FAIL;
247 }
248
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200249 if (ufunc->uf_va_name != NULL)
250 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200251 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200252 // Stack at time of call with 2 varargs:
253 // normal_arg
254 // optional_arg
255 // vararg_1
256 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200257 // After creating the list:
258 // normal_arg
259 // optional_arg
260 // vararg-list
261 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200262 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200263 // After creating the list
264 // normal_arg
265 // (space for optional_arg)
266 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200267 vararg_count = argcount - ufunc->uf_args.ga_len;
268 if (vararg_count < 0)
269 vararg_count = 0;
270 else
271 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200272 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200273 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200274
275 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200276 }
277
Bram Moolenaarfe270812020-04-11 22:31:27 +0200278 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200279 if (arg_to_add < 0)
280 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200281 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200282 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200283 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200284 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200285 return FAIL;
286 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200287
288 // Reserve space for:
289 // - missing arguments
290 // - stack frame
291 // - local variables
292 // - if needed: a counter for number of closures created in
293 // ectx->ec_funcrefs.
294 varcount = dfunc->df_varcount + dfunc->df_has_closure;
295 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
296 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 return FAIL;
298
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100299 // If depth of calling is getting too high, don't execute the function.
300 if (funcdepth_increment() == FAIL)
301 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200302 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100303
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100304 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200305 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100306 {
307 floc = ALLOC_ONE(funclocal_T);
308 if (floc == NULL)
309 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200310 *floc = ectx->ec_funclocal;
311 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100312 }
313
Bram Moolenaarfe270812020-04-11 22:31:27 +0200314 // Move the vararg-list to below the missing optional arguments.
315 if (vararg_count > 0 && arg_to_add > 0)
316 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100317
318 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200319 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200320 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200321 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100322
323 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100324 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
325 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200326 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200327 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
328 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100329 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100330 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200331 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332
333 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200334 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200336 if (dfunc->df_has_closure)
337 {
338 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
339
340 tv->v_type = VAR_NUMBER;
341 tv->vval.v_number = 0;
342 }
343 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100345 if (pt != NULL || ufunc->uf_partial != NULL
346 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100347 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200348 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100349
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200350 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100351 return FAIL;
352 if (pt != NULL)
353 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200354 ref->or_outer = &pt->pt_outer;
355 ++pt->pt_refcount;
356 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100357 }
358 else if (ufunc->uf_partial != NULL)
359 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200360 ref->or_outer = &ufunc->uf_partial->pt_outer;
361 ++ufunc->uf_partial->pt_refcount;
362 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100363 }
364 else
365 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200366 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
367 if (ref->or_outer == NULL)
368 {
369 vim_free(ref);
370 return FAIL;
371 }
372 ref->or_outer_allocated = TRUE;
373 ref->or_outer->out_stack = &ectx->ec_stack;
374 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
375 if (ectx->ec_outer_ref != NULL)
376 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100377 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200378 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100379 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100380 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200381 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100382
Bram Moolenaarc970e422021-03-17 15:03:04 +0100383 ++ufunc->uf_calls;
384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100385 // Set execution state to the start of the called function.
386 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100387 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100388 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200389 if (entry != NULL)
390 {
391 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200392 // Save the current context so it can be restored on return.
393 entry->es_save_sctx = current_sctx;
394 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200395 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100396
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200397 // Start execution at the first instruction.
398 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399
400 return OK;
401}
402
403// Get pointer to item in the stack.
404#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
405
406/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200407 * Used when returning from a function: Check if any closure is still
408 * referenced. If so then move the arguments and variables to a separate piece
409 * of stack to be used when the closure is called.
410 * When "free_arguments" is TRUE the arguments are to be freed.
411 * Returns FAIL when out of memory.
412 */
413 static int
414handle_closure_in_use(ectx_T *ectx, int free_arguments)
415{
416 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
417 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200418 int argcount;
419 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200420 int idx;
421 typval_T *tv;
422 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200423 garray_T *gap = &ectx->ec_funcrefs;
424 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200425
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200426 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200427 return OK; // function was freed
428 if (dfunc->df_has_closure == 0)
429 return OK; // no closures
430 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
431 closure_count = tv->vval.v_number;
432 if (closure_count == 0)
433 return OK; // no funcrefs created
434
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200435 argcount = ufunc_argcount(dfunc->df_ufunc);
436 top = ectx->ec_frame_idx - argcount;
437
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200438 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200439 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200440 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200441 partial_T *pt;
442 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200443
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200444 if (off < 0)
445 continue; // count is off or already done
446 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200447 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200448 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200449 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200450 int i;
451
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200452 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200453 // unreferenced on return.
454 for (i = 0; i < dfunc->df_varcount; ++i)
455 {
456 typval_T *stv = STACK_TV(ectx->ec_frame_idx
457 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200458 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200459 --refcount;
460 }
461 if (refcount > 1)
462 {
463 closure_in_use = TRUE;
464 break;
465 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200466 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200467 }
468
469 if (closure_in_use)
470 {
471 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
472 typval_T *stack;
473
474 // A closure is using the arguments and/or local variables.
475 // Move them to the called function.
476 if (funcstack == NULL)
477 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200478 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
479 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200480 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
481 funcstack->fs_ga.ga_data = stack;
482 if (stack == NULL)
483 {
484 vim_free(funcstack);
485 return FAIL;
486 }
487
488 // Move or copy the arguments.
489 for (idx = 0; idx < argcount; ++idx)
490 {
491 tv = STACK_TV(top + idx);
492 if (free_arguments)
493 {
494 *(stack + idx) = *tv;
495 tv->v_type = VAR_UNKNOWN;
496 }
497 else
498 copy_tv(tv, stack + idx);
499 }
500 // Move the local variables.
501 for (idx = 0; idx < dfunc->df_varcount; ++idx)
502 {
503 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200504
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200505 // A partial created for a local function, that is also used as a
506 // local variable, has a reference count for the variable, thus
507 // will never go down to zero. When all these refcounts are one
508 // then the funcstack is unused. We need to count how many we have
509 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200510 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
511 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200512 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200513
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200514 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200515 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
516 gap->ga_len - closure_count + i])
517 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200518 }
519
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200520 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200521 tv->v_type = VAR_UNKNOWN;
522 }
523
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200524 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200525 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200526 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
527 - closure_count + idx];
528 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200529 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200530 ++funcstack->fs_refcount;
531 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100532 pt->pt_outer.out_stack = &funcstack->fs_ga;
533 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200534 }
535 }
536 }
537
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200538 for (idx = 0; idx < closure_count; ++idx)
539 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
540 - closure_count + idx]);
541 gap->ga_len -= closure_count;
542 if (gap->ga_len == 0)
543 ga_clear(gap);
544
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200545 return OK;
546}
547
548/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200549 * Called when a partial is freed or its reference count goes down to one. The
550 * funcstack may be the only reference to the partials in the local variables.
551 * Go over all of them, the funcref and can be freed if all partials
552 * referencing the funcstack have a reference count of one.
553 */
554 void
555funcstack_check_refcount(funcstack_T *funcstack)
556{
557 int i;
558 garray_T *gap = &funcstack->fs_ga;
559 int done = 0;
560
561 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
562 return;
563 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
564 {
565 typval_T *tv = ((typval_T *)gap->ga_data) + i;
566
567 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
568 && tv->vval.v_partial->pt_funcstack == funcstack
569 && tv->vval.v_partial->pt_refcount == 1)
570 ++done;
571 }
572 if (done == funcstack->fs_min_refcount)
573 {
574 typval_T *stack = gap->ga_data;
575
576 // All partials referencing the funcstack have a reference count of
577 // one, thus the funcstack is no longer of use.
578 for (i = 0; i < gap->ga_len; ++i)
579 clear_tv(stack + i);
580 vim_free(stack);
581 vim_free(funcstack);
582 }
583}
584
585/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 * Return from the current function.
587 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200588 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200589func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100592 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200593 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
594 + ectx->ec_dfunc_idx;
595 int argcount = ufunc_argcount(dfunc->df_ufunc);
596 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200597 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100598 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
599 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200600 funclocal_T *floc;
601#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100602 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
603 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604
Bram Moolenaar12d26532021-02-19 19:13:21 +0100605 if (do_profiling == PROF_YES)
606 {
607 ufunc_T *caller = prev_dfunc->df_ufunc;
608
609 if (dfunc->df_ufunc->uf_profiling
610 || (caller != NULL && caller->uf_profiling))
611 {
612 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
613 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
614 --profile_info_ga.ga_len;
615 }
616 }
617#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100618 // TODO: when is it safe to delete the function when it is no longer used?
619 --dfunc->df_ufunc->uf_calls;
620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200622 entry = estack_pop();
623 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200624 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200626 if (handle_closure_in_use(ectx, TRUE) == FAIL)
627 return FAIL;
628
629 // Clear the arguments.
630 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
631 clear_tv(STACK_TV(idx));
632
633 // Clear local variables and temp values, but not the return value.
634 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100635 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100637
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100638 // The return value should be on top of the stack. However, when aborting
639 // it may not be there and ec_frame_idx is the top of the stack.
640 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100641 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100642 ret_idx = 0;
643
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200644 if (ectx->ec_outer_ref != NULL)
645 {
646 if (ectx->ec_outer_ref->or_outer_allocated)
647 vim_free(ectx->ec_outer_ref->or_outer);
648 partial_unref(ectx->ec_outer_ref->or_partial);
649 vim_free(ectx->ec_outer_ref);
650 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100651
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100652 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100653 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100654 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
655 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200656 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
657 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200658 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100659 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100660 floc = (void *)STACK_TV(ectx->ec_frame_idx
661 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200662 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100663 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
664 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200665
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100666 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200667 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100668 else
669 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200670 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100671 vim_free(floc);
672 }
673
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100674 if (ret_idx > 0)
675 {
676 // Reset the stack to the position before the call, with a spot for the
677 // return value, moved there from above the frame.
678 ectx->ec_stack.ga_len = top + 1;
679 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
680 }
681 else
682 // Reset the stack to the position before the call.
683 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200684
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100685 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200686 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200687 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688}
689
690#undef STACK_TV
691
692/*
693 * Prepare arguments and rettv for calling a builtin or user function.
694 */
695 static int
696call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
697{
698 int idx;
699 typval_T *tv;
700
701 // Move arguments from bottom of the stack to argvars[] and add terminator.
702 for (idx = 0; idx < argcount; ++idx)
703 argvars[idx] = *STACK_TV_BOT(idx - argcount);
704 argvars[argcount].v_type = VAR_UNKNOWN;
705
706 // Result replaces the arguments on the stack.
707 if (argcount > 0)
708 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200709 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710 return FAIL;
711 else
712 ++ectx->ec_stack.ga_len;
713
714 // Default return value is zero.
715 tv = STACK_TV_BOT(-1);
716 tv->v_type = VAR_NUMBER;
717 tv->vval.v_number = 0;
718
719 return OK;
720}
721
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200722// Ugly global to avoid passing the execution context around through many
723// layers.
724static ectx_T *current_ectx = NULL;
725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726/*
727 * Call a builtin function by index.
728 */
729 static int
730call_bfunc(int func_idx, int argcount, ectx_T *ectx)
731{
732 typval_T argvars[MAX_FUNC_ARGS];
733 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100734 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200735 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736
737 if (call_prepare(argcount, argvars, ectx) == FAIL)
738 return FAIL;
739
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200740 // Call the builtin function. Set "current_ectx" so that when it
741 // recursively invokes call_def_function() a closure context can be set.
742 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200744 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745
746 // Clear the arguments.
747 for (idx = 0; idx < argcount; ++idx)
748 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200749
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100750 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200751 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 return OK;
753}
754
755/*
756 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100757 * If the function is compiled this will add a stack frame and set the
758 * instruction pointer at the start of the function.
759 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200760 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100761 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 */
763 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100764call_ufunc(
765 ufunc_T *ufunc,
766 partial_T *pt,
767 int argcount,
768 ectx_T *ectx,
769 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770{
771 typval_T argvars[MAX_FUNC_ARGS];
772 funcexe_T funcexe;
773 int error;
774 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100775 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200776 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777
Bram Moolenaare99d4222021-06-13 14:01:26 +0200778 if (func_needs_compiling(ufunc, compile_type)
779 && compile_def_function(ufunc, FALSE, compile_type, NULL)
780 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200781 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200782 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100783 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100784 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100785 if (error != FCERR_UNKNOWN)
786 {
787 if (error == FCERR_TOOMANY)
788 semsg(_(e_toomanyarg), ufunc->uf_name);
789 else
790 semsg(_(e_toofewarg), ufunc->uf_name);
791 return FAIL;
792 }
793
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100794 // The function has been compiled, can call it quickly. For a function
795 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100796 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100797 if (iptr != NULL)
798 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100799 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100800 iptr->isn_type = ISN_DCALL;
801 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
802 iptr->isn_arg.dfunc.cdf_argcount = argcount;
803 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200804 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806
807 if (call_prepare(argcount, argvars, ectx) == FAIL)
808 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200809 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 funcexe.evaluate = TRUE;
811
812 // Call the user function. Result goes in last position on the stack.
813 // TODO: add selfdict if there is one
814 error = call_user_func_check(ufunc, argcount, argvars,
815 STACK_TV_BOT(-1), &funcexe, NULL);
816
817 // Clear the arguments.
818 for (idx = 0; idx < argcount; ++idx)
819 clear_tv(&argvars[idx]);
820
821 if (error != FCERR_NONE)
822 {
823 user_func_error(error, ufunc->uf_name);
824 return FAIL;
825 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100826 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200827 // Error other than from calling the function itself.
828 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 return OK;
830}
831
832/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100833 * If command modifiers were applied restore them.
834 */
835 static void
836may_restore_cmdmod(funclocal_T *funclocal)
837{
838 if (funclocal->floc_restore_cmdmod)
839 {
840 cmdmod.cmod_filter_regmatch.regprog = NULL;
841 undo_cmdmod(&cmdmod);
842 cmdmod = funclocal->floc_save_cmdmod;
843 funclocal->floc_restore_cmdmod = FALSE;
844 }
845}
846
847/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200848 * Return TRUE if an error was given or CTRL-C was pressed.
849 */
850 static int
851vim9_aborting(int prev_called_emsg)
852{
853 return called_emsg > prev_called_emsg || got_int || did_throw;
854}
855
856/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 * Execute a function by "name".
858 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100859 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 * Returns FAIL if not found without an error message.
861 */
862 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100863call_by_name(
864 char_u *name,
865 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100866 ectx_T *ectx,
867 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868{
869 ufunc_T *ufunc;
870
871 if (builtin_function(name, -1))
872 {
873 int func_idx = find_internal_func(name);
874
875 if (func_idx < 0)
876 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200877 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 return FAIL;
879 return call_bfunc(func_idx, argcount, ectx);
880 }
881
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200882 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200883
884 if (ufunc == NULL)
885 {
886 int called_emsg_before = called_emsg;
887
888 if (script_autoload(name, TRUE))
889 // loaded a package, search for the function again
890 ufunc = find_func(name, FALSE, NULL);
891 if (vim9_aborting(called_emsg_before))
892 return FAIL; // bail out if loading the script caused an error
893 }
894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100896 {
897 if (ufunc->uf_arg_types != NULL)
898 {
899 int i;
900 typval_T *argv = STACK_TV_BOT(0) - argcount;
901
902 // The function can change at runtime, check that the argument
903 // types are correct.
904 for (i = 0; i < argcount; ++i)
905 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100906 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100907
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100908 if (i < ufunc->uf_args.ga_len)
909 type = ufunc->uf_arg_types[i];
910 else if (ufunc->uf_va_type != NULL)
911 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100912 if (type != NULL && check_typval_arg_type(type,
913 &argv[i], i + 1) == FAIL)
914 return FAIL;
915 }
916 }
917
Bram Moolenaar4c137212021-04-19 16:48:48 +0200918 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920
921 return FAIL;
922}
923
924 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100925call_partial(
926 typval_T *tv,
927 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100928 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200930 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200931 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100933 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934
935 if (tv->v_type == VAR_PARTIAL)
936 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200937 partial_T *pt = tv->vval.v_partial;
938 int i;
939
940 if (pt->pt_argc > 0)
941 {
942 // Make space for arguments from the partial, shift the "argcount"
943 // arguments up.
944 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
945 return FAIL;
946 for (i = 1; i <= argcount; ++i)
947 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
948 ectx->ec_stack.ga_len += pt->pt_argc;
949 argcount += pt->pt_argc;
950
951 // copy the arguments from the partial onto the stack
952 for (i = 0; i < pt->pt_argc; ++i)
953 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955
956 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200957 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 name = pt->pt_name;
960 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200961 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200963 if (name != NULL)
964 {
965 char_u fname_buf[FLEN_FIXED + 1];
966 char_u *tofree = NULL;
967 int error = FCERR_NONE;
968 char_u *fname;
969
970 // May need to translate <SNR>123_ to K_SNR.
971 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
972 if (error != FCERR_NONE)
973 res = FAIL;
974 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200975 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200976 vim_free(tofree);
977 }
978
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100979 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 {
981 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200982 semsg(_(e_unknownfunc),
983 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984 return FAIL;
985 }
986 return OK;
987}
988
989/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200990 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
991 * TRUE.
992 */
993 static int
994error_if_locked(int lock, char *error)
995{
996 if (lock & (VAR_LOCKED | VAR_FIXED))
997 {
998 emsg(_(error));
999 return TRUE;
1000 }
1001 return FALSE;
1002}
1003
1004/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001005 * Give an error if "tv" is not a number and return FAIL.
1006 */
1007 static int
1008check_for_number(typval_T *tv)
1009{
1010 if (tv->v_type != VAR_NUMBER)
1011 {
1012 semsg(_(e_expected_str_but_got_str),
1013 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1014 return FAIL;
1015 }
1016 return OK;
1017}
1018
1019/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001020 * Store "tv" in variable "name".
1021 * This is for s: and g: variables.
1022 */
1023 static void
1024store_var(char_u *name, typval_T *tv)
1025{
1026 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001027 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001028
Bram Moolenaard877a572021-04-01 19:42:48 +02001029 if (tv->v_lock)
1030 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001031 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001032 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001033 restore_funccal();
1034}
1035
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001036/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001037 * Convert "tv" to a string.
1038 * Return FAIL if not allowed.
1039 */
1040 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001041do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001042{
1043 if (tv->v_type != VAR_STRING)
1044 {
1045 char_u *str;
1046
1047 if (is_2string_any)
1048 {
1049 switch (tv->v_type)
1050 {
1051 case VAR_SPECIAL:
1052 case VAR_BOOL:
1053 case VAR_NUMBER:
1054 case VAR_FLOAT:
1055 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001056
1057 case VAR_LIST:
1058 if (tolerant)
1059 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001060 char_u *s, *e, *p;
1061 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001062
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001063 ga_init2(&ga, sizeof(char_u *), 1);
1064
1065 // Convert to NL separated items, then
1066 // escape the items and replace the NL with
1067 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001068 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001069 if (str == NULL)
1070 return FAIL;
1071 s = str;
1072 while ((e = vim_strchr(s, '\n')) != NULL)
1073 {
1074 *e = NUL;
1075 p = vim_strsave_fnameescape(s, FALSE);
1076 if (p != NULL)
1077 {
1078 ga_concat(&ga, p);
1079 ga_concat(&ga, (char_u *)" ");
1080 vim_free(p);
1081 }
1082 s = e + 1;
1083 }
1084 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001085 clear_tv(tv);
1086 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001087 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001088 return OK;
1089 }
1090 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001091 default: to_string_error(tv->v_type);
1092 return FAIL;
1093 }
1094 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001095 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001096 clear_tv(tv);
1097 tv->v_type = VAR_STRING;
1098 tv->vval.v_string = str;
1099 }
1100 return OK;
1101}
1102
1103/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001104 * When the value of "sv" is a null list of dict, allocate it.
1105 */
1106 static void
1107allocate_if_null(typval_T *tv)
1108{
1109 switch (tv->v_type)
1110 {
1111 case VAR_LIST:
1112 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001113 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001114 break;
1115 case VAR_DICT:
1116 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001117 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001118 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001119 case VAR_BLOB:
1120 if (tv->vval.v_blob == NULL)
1121 (void)rettv_blob_alloc(tv);
1122 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001123 default:
1124 break;
1125 }
1126}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001127
Bram Moolenaare7525c52021-01-09 13:20:37 +01001128/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001129 * Return the character "str[index]" where "index" is the character index,
1130 * including composing characters.
1131 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001132 */
1133 char_u *
1134char_from_string(char_u *str, varnumber_T index)
1135{
1136 size_t nbyte = 0;
1137 varnumber_T nchar = index;
1138 size_t slen;
1139
1140 if (str == NULL)
1141 return NULL;
1142 slen = STRLEN(str);
1143
Bram Moolenaarff871402021-03-26 13:34:05 +01001144 // Do the same as for a list: a negative index counts from the end.
1145 // Optimization to check the first byte to be below 0x80 (and no composing
1146 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001147 if (index < 0)
1148 {
1149 int clen = 0;
1150
1151 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001152 {
1153 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1154 ++nbyte;
1155 else if (enc_utf8)
1156 nbyte += utfc_ptr2len(str + nbyte);
1157 else
1158 nbyte += mb_ptr2len(str + nbyte);
1159 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001160 nchar = clen + index;
1161 if (nchar < 0)
1162 // unlike list: index out of range results in empty string
1163 return NULL;
1164 }
1165
1166 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001167 {
1168 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1169 ++nbyte;
1170 else if (enc_utf8)
1171 nbyte += utfc_ptr2len(str + nbyte);
1172 else
1173 nbyte += mb_ptr2len(str + nbyte);
1174 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001175 if (nbyte >= slen)
1176 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001177 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001178}
1179
1180/*
1181 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001182 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001183 * If going over the end return "str_len".
1184 * If "idx" is negative count from the end, -1 is the last character.
1185 * When going over the start return -1.
1186 */
1187 static long
1188char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1189{
1190 varnumber_T nchar = idx;
1191 size_t nbyte = 0;
1192
1193 if (nchar >= 0)
1194 {
1195 while (nchar > 0 && nbyte < str_len)
1196 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001197 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001198 --nchar;
1199 }
1200 }
1201 else
1202 {
1203 nbyte = str_len;
1204 while (nchar < 0 && nbyte > 0)
1205 {
1206 --nbyte;
1207 nbyte -= mb_head_off(str, str + nbyte);
1208 ++nchar;
1209 }
1210 if (nchar < 0)
1211 return -1;
1212 }
1213 return (long)nbyte;
1214}
1215
1216/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001217 * Return the slice "str[first : last]" using character indexes. Composing
1218 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001219 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001220 * Return NULL when the result is empty.
1221 */
1222 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001223string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001224{
1225 long start_byte, end_byte;
1226 size_t slen;
1227
1228 if (str == NULL)
1229 return NULL;
1230 slen = STRLEN(str);
1231 start_byte = char_idx2byte(str, slen, first);
1232 if (start_byte < 0)
1233 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001234 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001235 end_byte = (long)slen;
1236 else
1237 {
1238 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001239 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001240 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001241 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001242 }
1243
1244 if (start_byte >= (long)slen || end_byte <= start_byte)
1245 return NULL;
1246 return vim_strnsave(str + start_byte, end_byte - start_byte);
1247}
1248
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001249 static svar_T *
1250get_script_svar(scriptref_T *sref, ectx_T *ectx)
1251{
1252 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1253 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1254 + ectx->ec_dfunc_idx;
1255 svar_T *sv;
1256
1257 if (sref->sref_seq != si->sn_script_seq)
1258 {
1259 // The script was reloaded after the function was
1260 // compiled, the script_idx may not be valid.
1261 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1262 dfunc->df_ufunc->uf_name_exp);
1263 return NULL;
1264 }
1265 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1266 if (!equal_type(sv->sv_type, sref->sref_type))
1267 {
1268 emsg(_(e_script_variable_type_changed));
1269 return NULL;
1270 }
1271 return sv;
1272}
1273
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001274/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001275 * Function passed to do_cmdline() for splitting a script joined by NL
1276 * characters.
1277 */
1278 static char_u *
1279get_split_sourceline(
1280 int c UNUSED,
1281 void *cookie,
1282 int indent UNUSED,
1283 getline_opt_T options UNUSED)
1284{
1285 source_cookie_T *sp = (source_cookie_T *)cookie;
1286 char_u *p;
1287 char_u *line;
1288
1289 if (*sp->nextline == NUL)
1290 return NULL;
1291 p = vim_strchr(sp->nextline, '\n');
1292 if (p == NULL)
1293 {
1294 line = vim_strsave(sp->nextline);
1295 sp->nextline += STRLEN(sp->nextline);
1296 }
1297 else
1298 {
1299 line = vim_strnsave(sp->nextline, p - sp->nextline);
1300 sp->nextline = p + 1;
1301 }
1302 return line;
1303}
1304
1305/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 * Execute a function by "name".
1307 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001308 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 */
1310 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001311call_eval_func(
1312 char_u *name,
1313 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001314 ectx_T *ectx,
1315 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316{
Bram Moolenaared677f52020-08-12 16:38:10 +02001317 int called_emsg_before = called_emsg;
1318 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319
Bram Moolenaar4c137212021-04-19 16:48:48 +02001320 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001321 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001323 dictitem_T *v;
1324
1325 v = find_var(name, NULL, FALSE);
1326 if (v == NULL)
1327 {
1328 semsg(_(e_unknownfunc), name);
1329 return FAIL;
1330 }
1331 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1332 {
1333 semsg(_(e_unknownfunc), name);
1334 return FAIL;
1335 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001336 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001338 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339}
1340
1341/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001342 * When a function reference is used, fill a partial with the information
1343 * needed, especially when it is used as a closure.
1344 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001345 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001346fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1347{
1348 pt->pt_func = ufunc;
1349 pt->pt_refcount = 1;
1350
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001351 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001352 {
1353 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1354 + ectx->ec_dfunc_idx;
1355
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001356 // The closure may need to find arguments and local variables in the
1357 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001358 pt->pt_outer.out_stack = &ectx->ec_stack;
1359 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001360 if (ectx->ec_outer_ref != NULL)
1361 {
1362 // The current context already has a context, link to that one.
1363 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1364 if (ectx->ec_outer_ref->or_partial != NULL)
1365 {
1366 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1367 ++pt->pt_outer.out_up_partial->pt_refcount;
1368 }
1369 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001370
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001371 // If this function returns and the closure is still being used, we
1372 // need to make a copy of the context (arguments and local variables).
1373 // Store a reference to the partial so we can handle that.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001374 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1375 {
1376 vim_free(pt);
1377 return FAIL;
1378 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001379 // Extra variable keeps the count of closures created in the current
1380 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001381 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1382 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1383
1384 ((partial_T **)ectx->ec_funcrefs.ga_data)
1385 [ectx->ec_funcrefs.ga_len] = pt;
1386 ++pt->pt_refcount;
1387 ++ectx->ec_funcrefs.ga_len;
1388 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001389 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001390 return OK;
1391}
1392
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001393// used for v_instr of typval of VAR_INSTR
1394struct instr_S {
1395 ectx_T *instr_ectx;
1396 isn_T *instr_instr;
1397};
1398
Bram Moolenaar4c137212021-04-19 16:48:48 +02001399// used for substitute_instr
1400typedef struct subs_expr_S {
1401 ectx_T *subs_ectx;
1402 isn_T *subs_instr;
1403 int subs_status;
1404} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405
1406// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001407#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408
1409// Get pointer to item at the bottom of the stack, -1 is the bottom.
1410#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001411#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 +01001412
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001413// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001414#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 +01001415
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001416// Set when calling do_debug().
1417static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001418static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001419
1420/*
1421 * When debugging lookup "name" and return the typeval.
1422 * When not found return NULL.
1423 */
1424 typval_T *
1425lookup_debug_var(char_u *name)
1426{
1427 int idx;
1428 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001429 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001430 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001431 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001432
1433 if (ectx == NULL)
1434 return NULL;
1435 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1436
1437 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001438 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001439 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001440 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001441 return STACK_TV_VAR(idx);
1442 }
1443
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001444 // Go through argument names.
1445 ufunc = dfunc->df_ufunc;
1446 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1447 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1448 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1449 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1450 - varargs_off + idx);
1451 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1452 return STACK_TV(ectx->ec_frame_idx - 1);
1453
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001454 return NULL;
1455}
1456
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001457 static void
1458handle_debug(isn_T *iptr, ectx_T *ectx)
1459{
1460 char_u *line;
1461 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1462 + ectx->ec_dfunc_idx)->df_ufunc;
1463 isn_T *ni;
1464 int end_lnum = iptr->isn_lnum;
1465 garray_T ga;
1466 int lnum;
1467
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001468 if (ex_nesting_level > debug_break_level)
1469 {
1470 linenr_T breakpoint;
1471
1472 if (!ufunc->uf_has_breakpoint)
1473 return;
1474
1475 // check for the next breakpoint if needed
1476 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001477 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001478 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1479 return;
1480 }
1481
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001482 SOURCING_LNUM = iptr->isn_lnum;
1483 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001484 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001485
1486 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1487 if (ni->isn_type == ISN_DEBUG
1488 || ni->isn_type == ISN_RETURN
1489 || ni->isn_type == ISN_RETURN_VOID)
1490 {
1491 end_lnum = ni->isn_lnum;
1492 break;
1493 }
1494
1495 if (end_lnum > iptr->isn_lnum)
1496 {
1497 ga_init2(&ga, sizeof(char_u *), 10);
1498 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001499 {
1500 char_u *p = skipwhite(
1501 ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1]);
1502
1503 if (*p == '#')
1504 break;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001505 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001506 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1507 if (STRNCMP(p, "def ", 4) == 0)
1508 break;
1509 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001510 line = ga_concat_strings(&ga, " ");
1511 vim_free(ga.ga_data);
1512 }
1513 else
1514 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001515
Dominique Pelle6e969552021-06-17 13:53:41 +02001516 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001517 debug_context = NULL;
1518
1519 if (end_lnum > iptr->isn_lnum)
1520 vim_free(line);
1521}
1522
Bram Moolenaar4c137212021-04-19 16:48:48 +02001523/*
1524 * Execute instructions in execution context "ectx".
1525 * Return OK or FAIL;
1526 */
1527 static int
1528exec_instructions(ectx_T *ectx)
1529{
1530 int breakcheck_count = 0;
1531 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001532 int ret = FAIL;
1533 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001534
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001535 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001536 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001537
Bram Moolenaarff652882021-05-16 15:24:49 +02001538 // Only catch exceptions in this instruction list.
1539 ectx->ec_trylevel_at_start = trylevel;
1540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 for (;;)
1542 {
1543 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001544
Bram Moolenaar270d0382020-05-15 21:42:53 +02001545 if (++breakcheck_count >= 100)
1546 {
1547 line_breakcheck();
1548 breakcheck_count = 0;
1549 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001550 if (got_int)
1551 {
1552 // Turn CTRL-C into an exception.
1553 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001554 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001555 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001556 did_throw = TRUE;
1557 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558
Bram Moolenaara26b9702020-04-18 19:53:28 +02001559 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1560 {
1561 // Turn an error message into an exception.
1562 did_emsg = FALSE;
1563 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001564 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001565 did_throw = TRUE;
1566 *msg_list = NULL;
1567 }
1568
Bram Moolenaar4c137212021-04-19 16:48:48 +02001569 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001571 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001572 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573
1574 // An exception jumps to the first catch, finally, or returns from
1575 // the current function.
1576 if (trystack->ga_len > 0)
1577 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001578 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 {
1580 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001581 ectx->ec_in_catch = TRUE;
1582 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 }
1584 else
1585 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001586 // Not inside try or need to return from current functions.
1587 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001588 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001589 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001590 tv = STACK_TV_BOT(0);
1591 tv->v_type = VAR_NUMBER;
1592 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001593 ++ectx->ec_stack.ga_len;
1594 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001596 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001597 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001598 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001599 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 goto done;
1601 }
1602
Bram Moolenaar4c137212021-04-19 16:48:48 +02001603 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001604 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 }
1606 continue;
1607 }
1608
Bram Moolenaar4c137212021-04-19 16:48:48 +02001609 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 switch (iptr->isn_type)
1611 {
1612 // execute Ex command line
1613 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001614 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001615 source_cookie_T cookie;
1616
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001617 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001618 // Pass getsourceline to get an error for a missing ":end"
1619 // command.
1620 CLEAR_FIELD(cookie);
1621 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1622 if (do_cmdline(iptr->isn_arg.string,
1623 getsourceline, &cookie,
1624 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1625 == FAIL
1626 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001627 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001628 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 break;
1630
Bram Moolenaar20677332021-06-06 17:02:53 +02001631 // execute Ex command line split at NL characters.
1632 case ISN_EXEC_SPLIT:
1633 {
1634 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001635 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001636
1637 SOURCING_LNUM = iptr->isn_lnum;
1638 CLEAR_FIELD(cookie);
1639 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1640 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001641 line = get_split_sourceline(0, &cookie, 0, 0);
1642 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001643 get_split_sourceline, &cookie,
1644 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1645 == FAIL
1646 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001647 {
1648 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001649 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001650 }
1651 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001652 }
1653 break;
1654
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001655 // Evaluate an expression with legacy syntax, push it onto the
1656 // stack.
1657 case ISN_LEGACY_EVAL:
1658 {
1659 char_u *arg = iptr->isn_arg.string;
1660 int res;
1661 int save_flags = cmdmod.cmod_flags;
1662
1663 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001664 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001665 tv = STACK_TV_BOT(0);
1666 init_tv(tv);
1667 cmdmod.cmod_flags |= CMOD_LEGACY;
1668 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1669 cmdmod.cmod_flags = save_flags;
1670 if (res == FAIL)
1671 goto on_error;
1672 ++ectx->ec_stack.ga_len;
1673 }
1674 break;
1675
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001676 // push typeval VAR_INSTR with instructions to be executed
1677 case ISN_INSTR:
1678 {
1679 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001680 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001681 tv = STACK_TV_BOT(0);
1682 tv->vval.v_instr = ALLOC_ONE(instr_T);
1683 if (tv->vval.v_instr == NULL)
1684 goto on_error;
1685 ++ectx->ec_stack.ga_len;
1686
1687 tv->v_type = VAR_INSTR;
1688 tv->vval.v_instr->instr_ectx = ectx;
1689 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1690 }
1691 break;
1692
Bram Moolenaar4c137212021-04-19 16:48:48 +02001693 // execute :substitute with an expression
1694 case ISN_SUBSTITUTE:
1695 {
1696 subs_T *subs = &iptr->isn_arg.subs;
1697 source_cookie_T cookie;
1698 struct subs_expr_S *save_instr = substitute_instr;
1699 struct subs_expr_S subs_instr;
1700 int res;
1701
1702 subs_instr.subs_ectx = ectx;
1703 subs_instr.subs_instr = subs->subs_instr;
1704 subs_instr.subs_status = OK;
1705 substitute_instr = &subs_instr;
1706
1707 SOURCING_LNUM = iptr->isn_lnum;
1708 // This is very much like ISN_EXEC
1709 CLEAR_FIELD(cookie);
1710 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1711 res = do_cmdline(subs->subs_cmd,
1712 getsourceline, &cookie,
1713 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1714 substitute_instr = save_instr;
1715
1716 if (res == FAIL || did_emsg
1717 || subs_instr.subs_status == FAIL)
1718 goto on_error;
1719 }
1720 break;
1721
1722 case ISN_FINISH:
1723 goto done;
1724
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001725 case ISN_REDIRSTART:
1726 // create a dummy entry for var_redir_str()
1727 if (alloc_redir_lval() == FAIL)
1728 goto on_error;
1729
1730 // The output is stored in growarray "redir_ga" until
1731 // redirection ends.
1732 init_redir_ga();
1733 redir_vname = 1;
1734 break;
1735
1736 case ISN_REDIREND:
1737 {
1738 char_u *res = get_clear_redir_ga();
1739
1740 // End redirection, put redirected text on the stack.
1741 clear_redir_lval();
1742 redir_vname = 0;
1743
1744 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1745 {
1746 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001747 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001748 }
1749 tv = STACK_TV_BOT(0);
1750 tv->v_type = VAR_STRING;
1751 tv->vval.v_string = res;
1752 ++ectx->ec_stack.ga_len;
1753 }
1754 break;
1755
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001756 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001757#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001758 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1759 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001760#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001761 break;
1762
1763 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001764#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001765 {
1766 exarg_T ea;
1767 int res;
1768
1769 CLEAR_FIELD(ea);
1770 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1771 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1772 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1773 --ectx->ec_stack.ga_len;
1774 tv = STACK_TV_BOT(0);
1775 res = cexpr_core(&ea, tv);
1776 clear_tv(tv);
1777 if (res == FAIL)
1778 goto on_error;
1779 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001780#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001781 break;
1782
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001783 // execute Ex command from pieces on the stack
1784 case ISN_EXECCONCAT:
1785 {
1786 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001787 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001788 int pass;
1789 int i;
1790 char_u *cmd = NULL;
1791 char_u *str;
1792
1793 for (pass = 1; pass <= 2; ++pass)
1794 {
1795 for (i = 0; i < count; ++i)
1796 {
1797 tv = STACK_TV_BOT(i - count);
1798 str = tv->vval.v_string;
1799 if (str != NULL && *str != NUL)
1800 {
1801 if (pass == 2)
1802 STRCPY(cmd + len, str);
1803 len += STRLEN(str);
1804 }
1805 if (pass == 2)
1806 clear_tv(tv);
1807 }
1808 if (pass == 1)
1809 {
1810 cmd = alloc(len + 1);
1811 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001812 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001813 len = 0;
1814 }
1815 }
1816
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001817 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001818 do_cmdline_cmd(cmd);
1819 vim_free(cmd);
1820 }
1821 break;
1822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 // execute :echo {string} ...
1824 case ISN_ECHO:
1825 {
1826 int count = iptr->isn_arg.echo.echo_count;
1827 int atstart = TRUE;
1828 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001829 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830
1831 for (idx = 0; idx < count; ++idx)
1832 {
1833 tv = STACK_TV_BOT(idx - count);
1834 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1835 &atstart, &needclr);
1836 clear_tv(tv);
1837 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001838 if (needclr)
1839 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001840 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 }
1842 break;
1843
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001844 // :execute {string} ...
1845 // :echomsg {string} ...
1846 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001847 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001848 case ISN_ECHOMSG:
1849 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001850 {
1851 int count = iptr->isn_arg.number;
1852 garray_T ga;
1853 char_u buf[NUMBUFLEN];
1854 char_u *p;
1855 int len;
1856 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001857 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001858
1859 ga_init2(&ga, 1, 80);
1860 for (idx = 0; idx < count; ++idx)
1861 {
1862 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001863 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001864 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001865 if (tv->v_type == VAR_CHANNEL
1866 || tv->v_type == VAR_JOB)
1867 {
1868 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001869 semsg(_(e_using_invalid_value_as_string_str),
1870 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001871 break;
1872 }
1873 else
1874 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001875 }
1876 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001877 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001878
1879 len = (int)STRLEN(p);
1880 if (ga_grow(&ga, len + 2) == FAIL)
1881 failed = TRUE;
1882 else
1883 {
1884 if (ga.ga_len > 0)
1885 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1886 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1887 ga.ga_len += len;
1888 }
1889 clear_tv(tv);
1890 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001891 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001892 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001893 {
1894 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001895 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001896 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001897
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001898 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001899 {
1900 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001901 {
1902 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001903 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001904 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001905 {
1906 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001907 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001908 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001909 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001910 else
1911 {
1912 msg_sb_eol();
1913 if (iptr->isn_type == ISN_ECHOMSG)
1914 {
1915 msg_attr(ga.ga_data, echo_attr);
1916 out_flush();
1917 }
1918 else
1919 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001920 SOURCING_LNUM = iptr->isn_lnum;
1921 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001922 }
1923 }
1924 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001925 ga_clear(&ga);
1926 }
1927 break;
1928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 // load local variable or argument
1930 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001931 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001932 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001934 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 break;
1936
1937 // load v: variable
1938 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001939 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001940 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001942 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 break;
1944
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001945 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 case ISN_LOADSCRIPT:
1947 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001948 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 svar_T *sv;
1950
Bram Moolenaar4c137212021-04-19 16:48:48 +02001951 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001952 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001953 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001954 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001955 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001956 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001958 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 }
1960 break;
1961
1962 // load s: variable in old script
1963 case ISN_LOADS:
1964 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001965 hashtab_T *ht = &SCRIPT_VARS(
1966 iptr->isn_arg.loadstore.ls_sid);
1967 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 if (di == NULL)
1971 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001972 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001973 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001974 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 }
1976 else
1977 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001978 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001979 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001981 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 }
1983 }
1984 break;
1985
Bram Moolenaard3aac292020-04-19 14:32:17 +02001986 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001988 case ISN_LOADB:
1989 case ISN_LOADW:
1990 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001992 dictitem_T *di = NULL;
1993 hashtab_T *ht = NULL;
1994 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001995
Bram Moolenaard3aac292020-04-19 14:32:17 +02001996 switch (iptr->isn_type)
1997 {
1998 case ISN_LOADG:
1999 ht = get_globvar_ht();
2000 namespace = 'g';
2001 break;
2002 case ISN_LOADB:
2003 ht = &curbuf->b_vars->dv_hashtab;
2004 namespace = 'b';
2005 break;
2006 case ISN_LOADW:
2007 ht = &curwin->w_vars->dv_hashtab;
2008 namespace = 'w';
2009 break;
2010 case ISN_LOADT:
2011 ht = &curtab->tp_vars->dv_hashtab;
2012 namespace = 't';
2013 break;
2014 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002015 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002016 }
2017 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 if (di == NULL)
2020 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002021 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002022 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002023 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002024 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 }
2026 else
2027 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002028 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002029 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002031 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 }
2033 }
2034 break;
2035
Bram Moolenaar03290b82020-12-19 16:30:44 +01002036 // load autoload variable
2037 case ISN_LOADAUTO:
2038 {
2039 char_u *name = iptr->isn_arg.string;
2040
Bram Moolenaar4c137212021-04-19 16:48:48 +02002041 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002042 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002043 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002044 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002045 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002046 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002047 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002048 }
2049 break;
2050
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002051 // load g:/b:/w:/t: namespace
2052 case ISN_LOADGDICT:
2053 case ISN_LOADBDICT:
2054 case ISN_LOADWDICT:
2055 case ISN_LOADTDICT:
2056 {
2057 dict_T *d = NULL;
2058
2059 switch (iptr->isn_type)
2060 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002061 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2062 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2063 case ISN_LOADWDICT: d = curwin->w_vars; break;
2064 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002065 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002066 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002067 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002068 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002069 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002070 tv = STACK_TV_BOT(0);
2071 tv->v_type = VAR_DICT;
2072 tv->v_lock = 0;
2073 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002074 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002075 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002076 }
2077 break;
2078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 // load &option
2080 case ISN_LOADOPT:
2081 {
2082 typval_T optval;
2083 char_u *name = iptr->isn_arg.string;
2084
Bram Moolenaara8c17702020-04-01 21:17:24 +02002085 // This is not expected to fail, name is checked during
2086 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002087 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002088 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002089 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002090 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002092 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 }
2094 break;
2095
2096 // load $ENV
2097 case ISN_LOADENV:
2098 {
2099 typval_T optval;
2100 char_u *name = iptr->isn_arg.string;
2101
Bram Moolenaar4c137212021-04-19 16:48:48 +02002102 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002103 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002104 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002105 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002107 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 }
2109 break;
2110
2111 // load @register
2112 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002113 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002114 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 tv = STACK_TV_BOT(0);
2116 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002117 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002118 // This may result in NULL, which should be equivalent to an
2119 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 tv->vval.v_string = get_reg_contents(
2121 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002122 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 break;
2124
2125 // store local variable
2126 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002127 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 tv = STACK_TV_VAR(iptr->isn_arg.number);
2129 clear_tv(tv);
2130 *tv = *STACK_TV_BOT(0);
2131 break;
2132
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002133 // store s: variable in old script
2134 case ISN_STORES:
2135 {
2136 hashtab_T *ht = &SCRIPT_VARS(
2137 iptr->isn_arg.loadstore.ls_sid);
2138 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002139 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002140
Bram Moolenaar4c137212021-04-19 16:48:48 +02002141 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002142 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002143 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002144 else
2145 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002146 SOURCING_LNUM = iptr->isn_lnum;
2147 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002148 {
2149 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002150 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002151 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002152 clear_tv(&di->di_tv);
2153 di->di_tv = *STACK_TV_BOT(0);
2154 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002155 }
2156 break;
2157
2158 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 case ISN_STORESCRIPT:
2160 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002161 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2162 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163
Bram Moolenaar4c137212021-04-19 16:48:48 +02002164 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002165 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002166 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002167 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002168
2169 // "const" and "final" are checked at compile time, locking
2170 // the value needs to be checked here.
2171 SOURCING_LNUM = iptr->isn_lnum;
2172 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002173 {
2174 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002175 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002176 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002178 clear_tv(sv->sv_tv);
2179 *sv->sv_tv = *STACK_TV_BOT(0);
2180 }
2181 break;
2182
2183 // store option
2184 case ISN_STOREOPT:
2185 {
2186 long n = 0;
2187 char_u *s = NULL;
2188 char *msg;
2189
Bram Moolenaar4c137212021-04-19 16:48:48 +02002190 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 tv = STACK_TV_BOT(0);
2192 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002193 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002195 if (s == NULL)
2196 s = (char_u *)"";
2197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002199 // must be VAR_NUMBER, CHECKTYPE makes sure
2200 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2202 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002203 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 if (msg != NULL)
2205 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002206 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002208 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 }
2211 break;
2212
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002213 // store $ENV
2214 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002215 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002216 tv = STACK_TV_BOT(0);
2217 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2218 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002219 break;
2220
2221 // store @r
2222 case ISN_STOREREG:
2223 {
2224 int reg = iptr->isn_arg.number;
2225
Bram Moolenaar4c137212021-04-19 16:48:48 +02002226 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002227 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002228 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002229 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002230 }
2231 break;
2232
2233 // store v: variable
2234 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002235 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002236 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2237 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002238 // should not happen, type is checked when compiling
2239 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002240 break;
2241
Bram Moolenaard3aac292020-04-19 14:32:17 +02002242 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002244 case ISN_STOREB:
2245 case ISN_STOREW:
2246 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002248 dictitem_T *di;
2249 hashtab_T *ht;
2250 char_u *name = iptr->isn_arg.string + 2;
2251
Bram Moolenaard3aac292020-04-19 14:32:17 +02002252 switch (iptr->isn_type)
2253 {
2254 case ISN_STOREG:
2255 ht = get_globvar_ht();
2256 break;
2257 case ISN_STOREB:
2258 ht = &curbuf->b_vars->dv_hashtab;
2259 break;
2260 case ISN_STOREW:
2261 ht = &curwin->w_vars->dv_hashtab;
2262 break;
2263 case ISN_STORET:
2264 ht = &curtab->tp_vars->dv_hashtab;
2265 break;
2266 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002267 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269
Bram Moolenaar4c137212021-04-19 16:48:48 +02002270 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002271 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002273 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 else
2275 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002276 SOURCING_LNUM = iptr->isn_lnum;
2277 if (var_check_permission(di, name) == FAIL)
2278 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 clear_tv(&di->di_tv);
2280 di->di_tv = *STACK_TV_BOT(0);
2281 }
2282 }
2283 break;
2284
Bram Moolenaar03290b82020-12-19 16:30:44 +01002285 // store an autoload variable
2286 case ISN_STOREAUTO:
2287 SOURCING_LNUM = iptr->isn_lnum;
2288 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2289 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002290 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002291 break;
2292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293 // store number in local variable
2294 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002295 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 clear_tv(tv);
2297 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002298 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 break;
2300
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002301 // store value in list or dict variable
2302 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002303 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002304 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002305 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002306 typval_T *tv_dest = STACK_TV_BOT(-1);
2307 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002308
Bram Moolenaar752fc692021-01-04 21:57:11 +01002309 // Stack contains:
2310 // -3 value to be stored
2311 // -2 index
2312 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002313 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002314 SOURCING_LNUM = iptr->isn_lnum;
2315 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002316 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002317 dest_type = tv_dest->v_type;
2318 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002319 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002320 else if (dest_type == VAR_LIST
2321 && tv_idx->v_type != VAR_NUMBER)
2322 {
2323 emsg(_(e_number_exp));
2324 status = FAIL;
2325 }
2326 }
2327 else if (dest_type != tv_dest->v_type)
2328 {
2329 // just in case, should be OK
2330 semsg(_(e_expected_str_but_got_str),
2331 vartype_name(dest_type),
2332 vartype_name(tv_dest->v_type));
2333 status = FAIL;
2334 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002335
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002336 if (status == OK && dest_type == VAR_LIST)
2337 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002338 long lidx = (long)tv_idx->vval.v_number;
2339 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002340
2341 if (list == NULL)
2342 {
2343 emsg(_(e_list_not_set));
2344 goto on_error;
2345 }
2346 if (lidx < 0 && list->lv_len + lidx >= 0)
2347 // negative index is relative to the end
2348 lidx = list->lv_len + lidx;
2349 if (lidx < 0 || lidx > list->lv_len)
2350 {
2351 semsg(_(e_listidx), lidx);
2352 goto on_error;
2353 }
2354 if (lidx < list->lv_len)
2355 {
2356 listitem_T *li = list_find(list, lidx);
2357
2358 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002359 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002360 goto on_error;
2361 // overwrite existing list item
2362 clear_tv(&li->li_tv);
2363 li->li_tv = *tv;
2364 }
2365 else
2366 {
2367 if (error_if_locked(list->lv_lock,
2368 e_cannot_change_list))
2369 goto on_error;
2370 // append to list, only fails when out of memory
2371 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002372 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002373 clear_tv(tv);
2374 }
2375 }
2376 else if (status == OK && dest_type == VAR_DICT)
2377 {
2378 char_u *key = tv_idx->vval.v_string;
2379 dict_T *dict = tv_dest->vval.v_dict;
2380 dictitem_T *di;
2381
2382 SOURCING_LNUM = iptr->isn_lnum;
2383 if (dict == NULL)
2384 {
2385 emsg(_(e_dictionary_not_set));
2386 goto on_error;
2387 }
2388 if (key == NULL)
2389 key = (char_u *)"";
2390 di = dict_find(dict, key, -1);
2391 if (di != NULL)
2392 {
2393 if (error_if_locked(di->di_tv.v_lock,
2394 e_cannot_change_dict_item))
2395 goto on_error;
2396 // overwrite existing value
2397 clear_tv(&di->di_tv);
2398 di->di_tv = *tv;
2399 }
2400 else
2401 {
2402 if (error_if_locked(dict->dv_lock,
2403 e_cannot_change_dict))
2404 goto on_error;
2405 // add to dict, only fails when out of memory
2406 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002407 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002408 clear_tv(tv);
2409 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002410 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002411 else if (status == OK && dest_type == VAR_BLOB)
2412 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002413 long lidx = (long)tv_idx->vval.v_number;
2414 blob_T *blob = tv_dest->vval.v_blob;
2415 varnumber_T nr;
2416 int error = FALSE;
2417 int len;
2418
2419 if (blob == NULL)
2420 {
2421 emsg(_(e_blob_not_set));
2422 goto on_error;
2423 }
2424 len = blob_len(blob);
2425 if (lidx < 0 && len + lidx >= 0)
2426 // negative index is relative to the end
2427 lidx = len + lidx;
2428
2429 // Can add one byte at the end.
2430 if (lidx < 0 || lidx > len)
2431 {
2432 semsg(_(e_blobidx), lidx);
2433 goto on_error;
2434 }
2435 if (value_check_lock(blob->bv_lock,
2436 (char_u *)"blob", FALSE))
2437 goto on_error;
2438 nr = tv_get_number_chk(tv, &error);
2439 if (error)
2440 goto on_error;
2441 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002442 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002443 else
2444 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002445 status = FAIL;
2446 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002447 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002448
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002449 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002450 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002451 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002452 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002453 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002454 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002455 goto on_error;
2456 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002457 }
2458 break;
2459
Bram Moolenaar68452172021-04-12 21:21:02 +02002460 // store value in blob range
2461 case ISN_STORERANGE:
2462 {
2463 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2464 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2465 typval_T *tv_dest = STACK_TV_BOT(-1);
2466 int status = OK;
2467
2468 // Stack contains:
2469 // -4 value to be stored
2470 // -3 first index or "none"
2471 // -2 second index or "none"
2472 // -1 destination blob
2473 tv = STACK_TV_BOT(-4);
2474 if (tv_dest->v_type != VAR_BLOB)
2475 {
2476 status = FAIL;
2477 emsg(_(e_blob_required));
2478 }
2479 else
2480 {
2481 varnumber_T n1;
2482 varnumber_T n2;
2483 int error = FALSE;
2484
2485 n1 = tv_get_number_chk(tv_idx1, &error);
2486 if (error)
2487 status = FAIL;
2488 else
2489 {
2490 if (tv_idx2->v_type == VAR_SPECIAL
2491 && tv_idx2->vval.v_number == VVAL_NONE)
2492 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2493 else
2494 n2 = tv_get_number_chk(tv_idx2, &error);
2495 if (error)
2496 status = FAIL;
2497 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002498 {
2499 long bloblen = blob_len(tv_dest->vval.v_blob);
2500
2501 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002502 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002503 || check_blob_range(bloblen,
2504 n1, n2, FALSE) == FAIL)
2505 status = FAIL;
2506 else
2507 status = blob_set_range(
2508 tv_dest->vval.v_blob, n1, n2, tv);
2509 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002510 }
2511 }
2512
2513 clear_tv(tv_idx1);
2514 clear_tv(tv_idx2);
2515 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002516 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002517 clear_tv(tv);
2518
2519 if (status == FAIL)
2520 goto on_error;
2521 }
2522 break;
2523
Bram Moolenaar0186e582021-01-10 18:33:11 +01002524 // load or store variable or argument from outer scope
2525 case ISN_LOADOUTER:
2526 case ISN_STOREOUTER:
2527 {
2528 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002529 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2530 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002531
2532 while (depth > 1 && outer != NULL)
2533 {
2534 outer = outer->out_up;
2535 --depth;
2536 }
2537 if (outer == NULL)
2538 {
2539 SOURCING_LNUM = iptr->isn_lnum;
2540 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002541 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002542 }
2543 tv = ((typval_T *)outer->out_stack->ga_data)
2544 + outer->out_frame_idx + STACK_FRAME_SIZE
2545 + iptr->isn_arg.outer.outer_idx;
2546 if (iptr->isn_type == ISN_LOADOUTER)
2547 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002548 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002549 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002550 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002551 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002552 }
2553 else
2554 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002555 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002556 clear_tv(tv);
2557 *tv = *STACK_TV_BOT(0);
2558 }
2559 }
2560 break;
2561
Bram Moolenaar752fc692021-01-04 21:57:11 +01002562 // unlet item in list or dict variable
2563 case ISN_UNLETINDEX:
2564 {
2565 typval_T *tv_idx = STACK_TV_BOT(-2);
2566 typval_T *tv_dest = STACK_TV_BOT(-1);
2567 int status = OK;
2568
2569 // Stack contains:
2570 // -2 index
2571 // -1 dict or list
2572 if (tv_dest->v_type == VAR_DICT)
2573 {
2574 // unlet a dict item, index must be a string
2575 if (tv_idx->v_type != VAR_STRING)
2576 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002577 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002578 semsg(_(e_expected_str_but_got_str),
2579 vartype_name(VAR_STRING),
2580 vartype_name(tv_idx->v_type));
2581 status = FAIL;
2582 }
2583 else
2584 {
2585 dict_T *d = tv_dest->vval.v_dict;
2586 char_u *key = tv_idx->vval.v_string;
2587 dictitem_T *di = NULL;
2588
2589 if (key == NULL)
2590 key = (char_u *)"";
2591 if (d != NULL)
2592 di = dict_find(d, key, (int)STRLEN(key));
2593 if (di == NULL)
2594 {
2595 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002596 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002597 semsg(_(e_dictkey), key);
2598 status = FAIL;
2599 }
2600 else
2601 {
2602 // TODO: check for dict or item locked
2603 dictitem_remove(d, di);
2604 }
2605 }
2606 }
2607 else if (tv_dest->v_type == VAR_LIST)
2608 {
2609 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002610 SOURCING_LNUM = iptr->isn_lnum;
2611 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002612 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002613 status = FAIL;
2614 }
2615 else
2616 {
2617 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002618 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002619 listitem_T *li = NULL;
2620
2621 li = list_find(l, n);
2622 if (li == NULL)
2623 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002624 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002625 semsg(_(e_listidx), n);
2626 status = FAIL;
2627 }
2628 else
2629 // TODO: check for list or item locked
2630 listitem_remove(l, li);
2631 }
2632 }
2633 else
2634 {
2635 status = FAIL;
2636 semsg(_(e_cannot_index_str),
2637 vartype_name(tv_dest->v_type));
2638 }
2639
2640 clear_tv(tv_idx);
2641 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002642 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002643 if (status == FAIL)
2644 goto on_error;
2645 }
2646 break;
2647
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002648 // unlet range of items in list variable
2649 case ISN_UNLETRANGE:
2650 {
2651 // Stack contains:
2652 // -3 index1
2653 // -2 index2
2654 // -1 dict or list
2655 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2656 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2657 typval_T *tv_dest = STACK_TV_BOT(-1);
2658 int status = OK;
2659
2660 if (tv_dest->v_type == VAR_LIST)
2661 {
2662 // indexes must be a number
2663 SOURCING_LNUM = iptr->isn_lnum;
2664 if (check_for_number(tv_idx1) == FAIL
2665 || check_for_number(tv_idx2) == FAIL)
2666 {
2667 status = FAIL;
2668 }
2669 else
2670 {
2671 list_T *l = tv_dest->vval.v_list;
2672 long n1 = (long)tv_idx1->vval.v_number;
2673 long n2 = (long)tv_idx2->vval.v_number;
2674 listitem_T *li;
2675
2676 li = list_find_index(l, &n1);
2677 if (li == NULL
2678 || list_unlet_range(l, li, NULL, n1,
2679 TRUE, n2) == FAIL)
2680 status = FAIL;
2681 }
2682 }
2683 else
2684 {
2685 status = FAIL;
2686 SOURCING_LNUM = iptr->isn_lnum;
2687 semsg(_(e_cannot_index_str),
2688 vartype_name(tv_dest->v_type));
2689 }
2690
2691 clear_tv(tv_idx1);
2692 clear_tv(tv_idx2);
2693 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002694 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002695 if (status == FAIL)
2696 goto on_error;
2697 }
2698 break;
2699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 // push constant
2701 case ISN_PUSHNR:
2702 case ISN_PUSHBOOL:
2703 case ISN_PUSHSPEC:
2704 case ISN_PUSHF:
2705 case ISN_PUSHS:
2706 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002707 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002708 case ISN_PUSHCHANNEL:
2709 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002710 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002711 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002713 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002714 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 switch (iptr->isn_type)
2716 {
2717 case ISN_PUSHNR:
2718 tv->v_type = VAR_NUMBER;
2719 tv->vval.v_number = iptr->isn_arg.number;
2720 break;
2721 case ISN_PUSHBOOL:
2722 tv->v_type = VAR_BOOL;
2723 tv->vval.v_number = iptr->isn_arg.number;
2724 break;
2725 case ISN_PUSHSPEC:
2726 tv->v_type = VAR_SPECIAL;
2727 tv->vval.v_number = iptr->isn_arg.number;
2728 break;
2729#ifdef FEAT_FLOAT
2730 case ISN_PUSHF:
2731 tv->v_type = VAR_FLOAT;
2732 tv->vval.v_float = iptr->isn_arg.fnumber;
2733 break;
2734#endif
2735 case ISN_PUSHBLOB:
2736 blob_copy(iptr->isn_arg.blob, tv);
2737 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002738 case ISN_PUSHFUNC:
2739 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002740 if (iptr->isn_arg.string == NULL)
2741 tv->vval.v_string = NULL;
2742 else
2743 tv->vval.v_string =
2744 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002745 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002746 case ISN_PUSHCHANNEL:
2747#ifdef FEAT_JOB_CHANNEL
2748 tv->v_type = VAR_CHANNEL;
2749 tv->vval.v_channel = iptr->isn_arg.channel;
2750 if (tv->vval.v_channel != NULL)
2751 ++tv->vval.v_channel->ch_refcount;
2752#endif
2753 break;
2754 case ISN_PUSHJOB:
2755#ifdef FEAT_JOB_CHANNEL
2756 tv->v_type = VAR_JOB;
2757 tv->vval.v_job = iptr->isn_arg.job;
2758 if (tv->vval.v_job != NULL)
2759 ++tv->vval.v_job->jv_refcount;
2760#endif
2761 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 default:
2763 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002764 tv->vval.v_string = vim_strsave(
2765 iptr->isn_arg.string == NULL
2766 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 }
2768 break;
2769
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002770 case ISN_UNLET:
2771 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2772 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002773 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002774 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002775 case ISN_UNLETENV:
2776 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2777 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002778
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002779 case ISN_LOCKCONST:
2780 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2781 break;
2782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 // create a list from items on the stack; uses a single allocation
2784 // for the list header and the items
2785 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002786 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002787 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 break;
2789
2790 // create a dict from items on the stack
2791 case ISN_NEWDICT:
2792 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002793 int count = iptr->isn_arg.number;
2794 dict_T *dict = dict_alloc();
2795 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002796 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002797 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798
2799 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002800 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 for (idx = 0; idx < count; ++idx)
2802 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002803 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002805 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002806 key = tv->vval.v_string == NULL
2807 ? (char_u *)"" : tv->vval.v_string;
2808 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002809 if (item != NULL)
2810 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002811 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002812 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002813 dict_unref(dict);
2814 goto on_error;
2815 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002816 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 clear_tv(tv);
2818 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002819 {
2820 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002821 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2824 item->di_tv.v_lock = 0;
2825 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002826 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002827 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002828 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002829 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 }
2832
2833 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002834 ectx->ec_stack.ga_len -= 2 * count - 1;
2835 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002836 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002838 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 tv = STACK_TV_BOT(-1);
2840 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002841 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 tv->vval.v_dict = dict;
2843 ++dict->dv_refcount;
2844 }
2845 break;
2846
2847 // call a :def function
2848 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002849 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002850 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2851 NULL,
2852 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002853 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002854 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 break;
2856
2857 // call a builtin function
2858 case ISN_BCALL:
2859 SOURCING_LNUM = iptr->isn_lnum;
2860 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2861 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002862 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002863 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 break;
2865
2866 // call a funcref or partial
2867 case ISN_PCALL:
2868 {
2869 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2870 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002871 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872
2873 SOURCING_LNUM = iptr->isn_lnum;
2874 if (pfunc->cpf_top)
2875 {
2876 // funcref is above the arguments
2877 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2878 }
2879 else
2880 {
2881 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002882 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002883 partial_tv = *STACK_TV_BOT(0);
2884 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002886 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002887 if (tv == &partial_tv)
2888 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002890 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 }
2892 break;
2893
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002894 case ISN_PCALL_END:
2895 // PCALL finished, arguments have been consumed and replaced by
2896 // the return value. Now clear the funcref from the stack,
2897 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002898 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002899 clear_tv(STACK_TV_BOT(-1));
2900 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2901 break;
2902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 // call a user defined function or funcref/partial
2904 case ISN_UCALL:
2905 {
2906 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2907
2908 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002909 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002910 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002911 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 }
2913 break;
2914
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002915 // return from a :def function call without a value
2916 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002917 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002918 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002919 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002920 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002921 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002922 tv->vval.v_number = 0;
2923 tv->v_lock = 0;
2924 // FALLTHROUGH
2925
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002926 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 case ISN_RETURN:
2928 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002929 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002930 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002931
2932 if (trystack->ga_len > 0)
2933 trycmd = ((trycmd_T *)trystack->ga_data)
2934 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002935 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002936 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002938 // jump to ":finally" or ":endtry"
2939 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002940 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002941 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002942 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 trycmd->tcd_return = TRUE;
2944 }
2945 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002946 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 }
2948 break;
2949
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002950 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 case ISN_FUNCREF:
2952 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002953 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2954 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2955 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002958 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002959 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002960 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002961 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002962 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002963 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002964 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002965 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002966 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002968 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 tv->vval.v_partial = pt;
2970 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002971 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 }
2973 break;
2974
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002975 // Create a global function from a lambda.
2976 case ISN_NEWFUNC:
2977 {
2978 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2979
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002980 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002981 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002982 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002983 }
2984 break;
2985
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002986 // List functions
2987 case ISN_DEF:
2988 if (iptr->isn_arg.string == NULL)
2989 list_functions(NULL);
2990 else
2991 {
2992 exarg_T ea;
2993
2994 CLEAR_FIELD(ea);
2995 ea.cmd = ea.arg = iptr->isn_arg.string;
2996 define_function(&ea, NULL);
2997 }
2998 break;
2999
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 // jump if a condition is met
3001 case ISN_JUMP:
3002 {
3003 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003004 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 int jump = TRUE;
3006
3007 if (when != JUMP_ALWAYS)
3008 {
3009 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003010 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003011 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003012 || when == JUMP_IF_COND_TRUE)
3013 {
3014 SOURCING_LNUM = iptr->isn_lnum;
3015 jump = tv_get_bool_chk(tv, &error);
3016 if (error)
3017 goto on_error;
3018 }
3019 else
3020 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003022 || when == JUMP_AND_KEEP_IF_FALSE
3023 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003025 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 {
3027 // drop the value from the stack
3028 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003029 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 }
3031 }
3032 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003033 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 }
3035 break;
3036
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003037 // Jump if an argument with a default value was already set and not
3038 // v:none.
3039 case ISN_JUMP_IF_ARG_SET:
3040 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3041 if (tv->v_type != VAR_UNKNOWN
3042 && !(tv->v_type == VAR_SPECIAL
3043 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003044 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003045 break;
3046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 // top of a for loop
3048 case ISN_FOR:
3049 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003050 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 typval_T *idxtv =
3052 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3053
Bram Moolenaar4c137212021-04-19 16:48:48 +02003054 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003055 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003056 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003057 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003058 list_T *list = ltv->vval.v_list;
3059
3060 // push the next item from the list
3061 ++idxtv->vval.v_number;
3062 if (list == NULL
3063 || idxtv->vval.v_number >= list->lv_len)
3064 {
3065 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003066 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3067 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003068 }
3069 else if (list->lv_first == &range_list_item)
3070 {
3071 // non-materialized range() list
3072 tv = STACK_TV_BOT(0);
3073 tv->v_type = VAR_NUMBER;
3074 tv->v_lock = 0;
3075 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003077 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003078 }
3079 else
3080 {
3081 listitem_T *li = list_find(list,
3082 idxtv->vval.v_number);
3083
3084 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003085 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003086 }
3087 }
3088 else if (ltv->v_type == VAR_STRING)
3089 {
3090 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003091
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003092 // The index is for the last byte of the previous
3093 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003094 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003095 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003096 {
3097 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003098 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3099 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003100 }
3101 else
3102 {
3103 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3104
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003105 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003106 tv = STACK_TV_BOT(0);
3107 tv->v_type = VAR_STRING;
3108 tv->vval.v_string = vim_strnsave(
3109 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003110 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003111 idxtv->vval.v_number += clen - 1;
3112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003114 else if (ltv->v_type == VAR_BLOB)
3115 {
3116 blob_T *blob = ltv->vval.v_blob;
3117
3118 // When we get here the first time make a copy of the
3119 // blob, so that the iteration still works when it is
3120 // changed.
3121 if (idxtv->vval.v_number == -1 && blob != NULL)
3122 {
3123 blob_copy(blob, ltv);
3124 blob_unref(blob);
3125 blob = ltv->vval.v_blob;
3126 }
3127
3128 // The index is for the previous byte.
3129 ++idxtv->vval.v_number;
3130 if (blob == NULL
3131 || idxtv->vval.v_number >= blob_len(blob))
3132 {
3133 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003134 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3135 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003136 }
3137 else
3138 {
3139 // Push the next byte from the blob.
3140 tv = STACK_TV_BOT(0);
3141 tv->v_type = VAR_NUMBER;
3142 tv->vval.v_number = blob_get(blob,
3143 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003144 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003145 }
3146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 else
3148 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003149 semsg(_(e_for_loop_on_str_not_supported),
3150 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003151 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 }
3153 }
3154 break;
3155
3156 // start of ":try" block
3157 case ISN_TRY:
3158 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003159 trycmd_T *trycmd = NULL;
3160
Bram Moolenaar4c137212021-04-19 16:48:48 +02003161 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003162 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003163 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3164 + ectx->ec_trystack.ga_len;
3165 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003167 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003168 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3169 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaar3f987b52021-06-30 12:02:24 +02003170 trycmd->tcd_save_in_catch = ectx->ec_in_catch;
3171 ectx->ec_in_catch = FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003172 trycmd->tcd_catch_idx =
3173 iptr->isn_arg.try.try_ref->try_catch;
3174 trycmd->tcd_finally_idx =
3175 iptr->isn_arg.try.try_ref->try_finally;
3176 trycmd->tcd_endtry_idx =
3177 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 }
3179 break;
3180
3181 case ISN_PUSHEXC:
3182 if (current_exception == NULL)
3183 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003184 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003186 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003188 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003189 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003191 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003193 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 tv->vval.v_string = vim_strsave(
3195 (char_u *)current_exception->value);
3196 break;
3197
3198 case ISN_CATCH:
3199 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003200 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201
Bram Moolenaar4c137212021-04-19 16:48:48 +02003202 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 if (trystack->ga_len > 0)
3204 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003205 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 + trystack->ga_len - 1;
3207 trycmd->tcd_caught = TRUE;
3208 }
3209 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003210 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 catch_exception(current_exception);
3212 }
3213 break;
3214
Bram Moolenaarc150c092021-02-13 15:02:46 +01003215 case ISN_TRYCONT:
3216 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003217 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003218 trycont_T *trycont = &iptr->isn_arg.trycont;
3219 int i;
3220 trycmd_T *trycmd;
3221 int iidx = trycont->tct_where;
3222
3223 if (trystack->ga_len < trycont->tct_levels)
3224 {
3225 siemsg("TRYCONT: expected %d levels, found %d",
3226 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003227 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003228 }
3229 // Make :endtry jump to any outer try block and the last
3230 // :endtry inside the loop to the loop start.
3231 for (i = trycont->tct_levels; i > 0; --i)
3232 {
3233 trycmd = ((trycmd_T *)trystack->ga_data)
3234 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003235 // Add one to tcd_cont to be able to jump to
3236 // instruction with index zero.
3237 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003238 iidx = trycmd->tcd_finally_idx == 0
3239 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003240 }
3241 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003242 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003243 }
3244 break;
3245
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003246 case ISN_FINALLY:
3247 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003248 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003249 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3250 + trystack->ga_len - 1;
3251
3252 // Reset the index to avoid a return statement jumps here
3253 // again.
3254 trycmd->tcd_finally_idx = 0;
3255 break;
3256 }
3257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 // end of ":try" block
3259 case ISN_ENDTRY:
3260 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003261 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262
3263 if (trystack->ga_len > 0)
3264 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003265 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 --trystack->ga_len;
3268 --trylevel;
3269 trycmd = ((trycmd_T *)trystack->ga_data)
3270 + trystack->ga_len;
Bram Moolenaar3f987b52021-06-30 12:02:24 +02003271 ectx->ec_in_catch = trycmd->tcd_save_in_catch;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003272 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 {
3274 // discard the exception
3275 if (caught_stack == current_exception)
3276 caught_stack = caught_stack->caught;
3277 discard_current_exception();
3278 }
3279
3280 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003281 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003282
Bram Moolenaar4c137212021-04-19 16:48:48 +02003283 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003284 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003285 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003286 clear_tv(STACK_TV_BOT(0));
3287 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003288 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003289 // handling :continue: jump to outer try block or
3290 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003291 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 }
3293 }
3294 break;
3295
3296 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003297 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003298 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003299
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003300 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3301 {
3302 // throwing an exception while using "silent!" causes
3303 // the function to abort but not display an error.
3304 tv = STACK_TV_BOT(-1);
3305 clear_tv(tv);
3306 tv->v_type = VAR_NUMBER;
3307 tv->vval.v_number = 0;
3308 goto done;
3309 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003310 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003311 tv = STACK_TV_BOT(0);
3312 if (tv->vval.v_string == NULL
3313 || *skipwhite(tv->vval.v_string) == NUL)
3314 {
3315 vim_free(tv->vval.v_string);
3316 SOURCING_LNUM = iptr->isn_lnum;
3317 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003318 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003319 }
3320
3321 // Inside a "catch" we need to first discard the caught
3322 // exception.
3323 if (trystack->ga_len > 0)
3324 {
3325 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3326 + trystack->ga_len - 1;
3327 if (trycmd->tcd_caught && current_exception != NULL)
3328 {
3329 // discard the exception
3330 if (caught_stack == current_exception)
3331 caught_stack = caught_stack->caught;
3332 discard_current_exception();
3333 trycmd->tcd_caught = FALSE;
3334 }
3335 }
3336
3337 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3338 == FAIL)
3339 {
3340 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003341 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003342 }
3343 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 break;
3346
3347 // compare with special values
3348 case ISN_COMPAREBOOL:
3349 case ISN_COMPARESPECIAL:
3350 {
3351 typval_T *tv1 = STACK_TV_BOT(-2);
3352 typval_T *tv2 = STACK_TV_BOT(-1);
3353 varnumber_T arg1 = tv1->vval.v_number;
3354 varnumber_T arg2 = tv2->vval.v_number;
3355 int res;
3356
3357 switch (iptr->isn_arg.op.op_type)
3358 {
3359 case EXPR_EQUAL: res = arg1 == arg2; break;
3360 case EXPR_NEQUAL: res = arg1 != arg2; break;
3361 default: res = 0; break;
3362 }
3363
Bram Moolenaar4c137212021-04-19 16:48:48 +02003364 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 tv1->v_type = VAR_BOOL;
3366 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3367 }
3368 break;
3369
3370 // Operation with two number arguments
3371 case ISN_OPNR:
3372 case ISN_COMPARENR:
3373 {
3374 typval_T *tv1 = STACK_TV_BOT(-2);
3375 typval_T *tv2 = STACK_TV_BOT(-1);
3376 varnumber_T arg1 = tv1->vval.v_number;
3377 varnumber_T arg2 = tv2->vval.v_number;
3378 varnumber_T res;
3379
3380 switch (iptr->isn_arg.op.op_type)
3381 {
3382 case EXPR_MULT: res = arg1 * arg2; break;
3383 case EXPR_DIV: res = arg1 / arg2; break;
3384 case EXPR_REM: res = arg1 % arg2; break;
3385 case EXPR_SUB: res = arg1 - arg2; break;
3386 case EXPR_ADD: res = arg1 + arg2; break;
3387
3388 case EXPR_EQUAL: res = arg1 == arg2; break;
3389 case EXPR_NEQUAL: res = arg1 != arg2; break;
3390 case EXPR_GREATER: res = arg1 > arg2; break;
3391 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3392 case EXPR_SMALLER: res = arg1 < arg2; break;
3393 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3394 default: res = 0; break;
3395 }
3396
Bram Moolenaar4c137212021-04-19 16:48:48 +02003397 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 if (iptr->isn_type == ISN_COMPARENR)
3399 {
3400 tv1->v_type = VAR_BOOL;
3401 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3402 }
3403 else
3404 tv1->vval.v_number = res;
3405 }
3406 break;
3407
3408 // Computation with two float arguments
3409 case ISN_OPFLOAT:
3410 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003411#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 {
3413 typval_T *tv1 = STACK_TV_BOT(-2);
3414 typval_T *tv2 = STACK_TV_BOT(-1);
3415 float_T arg1 = tv1->vval.v_float;
3416 float_T arg2 = tv2->vval.v_float;
3417 float_T res = 0;
3418 int cmp = FALSE;
3419
3420 switch (iptr->isn_arg.op.op_type)
3421 {
3422 case EXPR_MULT: res = arg1 * arg2; break;
3423 case EXPR_DIV: res = arg1 / arg2; break;
3424 case EXPR_SUB: res = arg1 - arg2; break;
3425 case EXPR_ADD: res = arg1 + arg2; break;
3426
3427 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3428 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3429 case EXPR_GREATER: cmp = arg1 > arg2; break;
3430 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3431 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3432 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3433 default: cmp = 0; break;
3434 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003435 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 if (iptr->isn_type == ISN_COMPAREFLOAT)
3437 {
3438 tv1->v_type = VAR_BOOL;
3439 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3440 }
3441 else
3442 tv1->vval.v_float = res;
3443 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003444#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 break;
3446
3447 case ISN_COMPARELIST:
3448 {
3449 typval_T *tv1 = STACK_TV_BOT(-2);
3450 typval_T *tv2 = STACK_TV_BOT(-1);
3451 list_T *arg1 = tv1->vval.v_list;
3452 list_T *arg2 = tv2->vval.v_list;
3453 int cmp = FALSE;
3454 int ic = iptr->isn_arg.op.op_ic;
3455
3456 switch (iptr->isn_arg.op.op_type)
3457 {
3458 case EXPR_EQUAL: cmp =
3459 list_equal(arg1, arg2, ic, FALSE); break;
3460 case EXPR_NEQUAL: cmp =
3461 !list_equal(arg1, arg2, ic, FALSE); break;
3462 case EXPR_IS: cmp = arg1 == arg2; break;
3463 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3464 default: cmp = 0; break;
3465 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003466 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 clear_tv(tv1);
3468 clear_tv(tv2);
3469 tv1->v_type = VAR_BOOL;
3470 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3471 }
3472 break;
3473
3474 case ISN_COMPAREBLOB:
3475 {
3476 typval_T *tv1 = STACK_TV_BOT(-2);
3477 typval_T *tv2 = STACK_TV_BOT(-1);
3478 blob_T *arg1 = tv1->vval.v_blob;
3479 blob_T *arg2 = tv2->vval.v_blob;
3480 int cmp = FALSE;
3481
3482 switch (iptr->isn_arg.op.op_type)
3483 {
3484 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3485 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3486 case EXPR_IS: cmp = arg1 == arg2; break;
3487 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3488 default: cmp = 0; break;
3489 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003490 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 clear_tv(tv1);
3492 clear_tv(tv2);
3493 tv1->v_type = VAR_BOOL;
3494 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3495 }
3496 break;
3497
3498 // TODO: handle separately
3499 case ISN_COMPARESTRING:
3500 case ISN_COMPAREDICT:
3501 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 case ISN_COMPAREANY:
3503 {
3504 typval_T *tv1 = STACK_TV_BOT(-2);
3505 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003506 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 int ic = iptr->isn_arg.op.op_ic;
3508
Bram Moolenaareb26f432020-09-14 16:50:05 +02003509 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003510 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003512 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 }
3514 break;
3515
3516 case ISN_ADDLIST:
3517 case ISN_ADDBLOB:
3518 {
3519 typval_T *tv1 = STACK_TV_BOT(-2);
3520 typval_T *tv2 = STACK_TV_BOT(-1);
3521
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003522 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 if (iptr->isn_type == ISN_ADDLIST)
3524 eval_addlist(tv1, tv2);
3525 else
3526 eval_addblob(tv1, tv2);
3527 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003528 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 }
3530 break;
3531
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003532 case ISN_LISTAPPEND:
3533 {
3534 typval_T *tv1 = STACK_TV_BOT(-2);
3535 typval_T *tv2 = STACK_TV_BOT(-1);
3536 list_T *l = tv1->vval.v_list;
3537
3538 // add an item to a list
3539 if (l == NULL)
3540 {
3541 SOURCING_LNUM = iptr->isn_lnum;
3542 emsg(_(e_cannot_add_to_null_list));
3543 goto on_error;
3544 }
3545 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003546 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003547 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003548 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003549 }
3550 break;
3551
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003552 case ISN_BLOBAPPEND:
3553 {
3554 typval_T *tv1 = STACK_TV_BOT(-2);
3555 typval_T *tv2 = STACK_TV_BOT(-1);
3556 blob_T *b = tv1->vval.v_blob;
3557 int error = FALSE;
3558 varnumber_T n;
3559
3560 // add a number to a blob
3561 if (b == NULL)
3562 {
3563 SOURCING_LNUM = iptr->isn_lnum;
3564 emsg(_(e_cannot_add_to_null_blob));
3565 goto on_error;
3566 }
3567 n = tv_get_number_chk(tv2, &error);
3568 if (error)
3569 goto on_error;
3570 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003571 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003572 }
3573 break;
3574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 // Computation with two arguments of unknown type
3576 case ISN_OPANY:
3577 {
3578 typval_T *tv1 = STACK_TV_BOT(-2);
3579 typval_T *tv2 = STACK_TV_BOT(-1);
3580 varnumber_T n1, n2;
3581#ifdef FEAT_FLOAT
3582 float_T f1 = 0, f2 = 0;
3583#endif
3584 int error = FALSE;
3585
3586 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3587 {
3588 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3589 {
3590 eval_addlist(tv1, tv2);
3591 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003592 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 break;
3594 }
3595 else if (tv1->v_type == VAR_BLOB
3596 && tv2->v_type == VAR_BLOB)
3597 {
3598 eval_addblob(tv1, tv2);
3599 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003600 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 break;
3602 }
3603 }
3604#ifdef FEAT_FLOAT
3605 if (tv1->v_type == VAR_FLOAT)
3606 {
3607 f1 = tv1->vval.v_float;
3608 n1 = 0;
3609 }
3610 else
3611#endif
3612 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003613 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 n1 = tv_get_number_chk(tv1, &error);
3615 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003616 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617#ifdef FEAT_FLOAT
3618 if (tv2->v_type == VAR_FLOAT)
3619 f1 = n1;
3620#endif
3621 }
3622#ifdef FEAT_FLOAT
3623 if (tv2->v_type == VAR_FLOAT)
3624 {
3625 f2 = tv2->vval.v_float;
3626 n2 = 0;
3627 }
3628 else
3629#endif
3630 {
3631 n2 = tv_get_number_chk(tv2, &error);
3632 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003633 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634#ifdef FEAT_FLOAT
3635 if (tv1->v_type == VAR_FLOAT)
3636 f2 = n2;
3637#endif
3638 }
3639#ifdef FEAT_FLOAT
3640 // if there is a float on either side the result is a float
3641 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3642 {
3643 switch (iptr->isn_arg.op.op_type)
3644 {
3645 case EXPR_MULT: f1 = f1 * f2; break;
3646 case EXPR_DIV: f1 = f1 / f2; break;
3647 case EXPR_SUB: f1 = f1 - f2; break;
3648 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003649 default: SOURCING_LNUM = iptr->isn_lnum;
3650 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003651 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 }
3653 clear_tv(tv1);
3654 clear_tv(tv2);
3655 tv1->v_type = VAR_FLOAT;
3656 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003657 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 }
3659 else
3660#endif
3661 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003662 int failed = FALSE;
3663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 switch (iptr->isn_arg.op.op_type)
3665 {
3666 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003667 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3668 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003669 goto on_error;
3670 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 case EXPR_SUB: n1 = n1 - n2; break;
3672 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003673 default: n1 = num_modulus(n1, n2, &failed);
3674 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003675 goto on_error;
3676 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 }
3678 clear_tv(tv1);
3679 clear_tv(tv2);
3680 tv1->v_type = VAR_NUMBER;
3681 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003682 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 }
3684 }
3685 break;
3686
3687 case ISN_CONCAT:
3688 {
3689 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3690 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3691 char_u *res;
3692
3693 res = concat_str(str1, str2);
3694 clear_tv(STACK_TV_BOT(-2));
3695 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003696 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 STACK_TV_BOT(-1)->vval.v_string = res;
3698 }
3699 break;
3700
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003701 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003702 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003703 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003704 int is_slice = iptr->isn_type == ISN_STRSLICE;
3705 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003706 char_u *res;
3707
3708 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003709 // string slice: string is at stack-3, first index at
3710 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003711 if (is_slice)
3712 {
3713 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003714 n1 = tv->vval.v_number;
3715 }
3716
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003717 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003718 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003719
Bram Moolenaar4c137212021-04-19 16:48:48 +02003720 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003721 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003722 if (is_slice)
3723 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003724 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003725 else
3726 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003727 // single character (including composing characters).
3728 // If the index is too big or negative the result is
3729 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003730 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003731 vim_free(tv->vval.v_string);
3732 tv->vval.v_string = res;
3733 }
3734 break;
3735
3736 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003737 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003738 case ISN_BLOBINDEX:
3739 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003741 int is_slice = iptr->isn_type == ISN_LISTSLICE
3742 || iptr->isn_type == ISN_BLOBSLICE;
3743 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3744 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003745 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003746 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747
3748 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003749 // list slice: list is at stack-3, indexes at stack-2 and
3750 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003751 // Same for blob.
3752 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753
3754 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003755 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003757
3758 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 {
Bram Moolenaared591872020-08-15 22:14:53 +02003760 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003761 n1 = tv->vval.v_number;
3762 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 }
Bram Moolenaared591872020-08-15 22:14:53 +02003764
Bram Moolenaar4c137212021-04-19 16:48:48 +02003765 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003766 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003767 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003768 if (is_blob)
3769 {
3770 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3771 n1, n2, FALSE, tv) == FAIL)
3772 goto on_error;
3773 }
3774 else
3775 {
3776 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3777 n1, n2, FALSE, tv, TRUE) == FAIL)
3778 goto on_error;
3779 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 }
3781 break;
3782
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003783 case ISN_ANYINDEX:
3784 case ISN_ANYSLICE:
3785 {
3786 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3787 typval_T *var1, *var2;
3788 int res;
3789
3790 // index: composite is at stack-2, index at stack-1
3791 // slice: composite is at stack-3, indexes at stack-2 and
3792 // stack-1
3793 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003794 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003795 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3796 goto on_error;
3797 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3798 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003799 res = eval_index_inner(tv, is_slice, var1, var2,
3800 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003801 clear_tv(var1);
3802 if (is_slice)
3803 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003804 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003805 if (res == FAIL)
3806 goto on_error;
3807 }
3808 break;
3809
Bram Moolenaar9af78762020-06-16 11:34:42 +02003810 case ISN_SLICE:
3811 {
3812 list_T *list;
3813 int count = iptr->isn_arg.number;
3814
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003815 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003816 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003817 list = tv->vval.v_list;
3818
3819 // no error for short list, expect it to be checked earlier
3820 if (list != NULL && list->lv_len >= count)
3821 {
3822 list_T *newlist = list_slice(list,
3823 count, list->lv_len - 1);
3824
3825 if (newlist != NULL)
3826 {
3827 list_unref(list);
3828 tv->vval.v_list = newlist;
3829 ++newlist->lv_refcount;
3830 }
3831 }
3832 }
3833 break;
3834
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003835 case ISN_GETITEM:
3836 {
3837 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003838 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003839
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003840 // Get list item: list is at stack-1, push item.
3841 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003842 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
3843 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003844
Bram Moolenaar4c137212021-04-19 16:48:48 +02003845 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003846 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003847 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003848 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003849
3850 // Useful when used in unpack assignment. Reset at
3851 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003852 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003853 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003854 }
3855 break;
3856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 case ISN_MEMBER:
3858 {
3859 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003860 char_u *key;
3861 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003862 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003863
3864 // dict member: dict is at stack-2, key at stack-1
3865 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003866 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003867 dict = tv->vval.v_dict;
3868
3869 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003870 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003871 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003872 if (key == NULL)
3873 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003874
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003875 if ((di = dict_find(dict, key, -1)) == NULL)
3876 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003877 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003878 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003879
3880 // If :silent! is used we will continue, make sure the
3881 // stack contents makes sense.
3882 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003883 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003884 tv = STACK_TV_BOT(-1);
3885 clear_tv(tv);
3886 tv->v_type = VAR_NUMBER;
3887 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003888 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003889 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003890 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003891 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003892 // Clear the dict only after getting the item, to avoid
3893 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003894 tv = STACK_TV_BOT(-1);
3895 temp_tv = *tv;
3896 copy_tv(&di->di_tv, tv);
3897 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003898 }
3899 break;
3900
3901 // dict member with string key
3902 case ISN_STRINGMEMBER:
3903 {
3904 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003906 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907
3908 tv = STACK_TV_BOT(-1);
3909 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3910 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003911 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003913 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 }
3915 dict = tv->vval.v_dict;
3916
3917 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3918 == NULL)
3919 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003920 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003922 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003924 // Clear the dict after getting the item, to avoid that it
3925 // make the item invalid.
3926 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003928 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 }
3930 break;
3931
3932 case ISN_NEGATENR:
3933 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003934 if (tv->v_type != VAR_NUMBER
3935#ifdef FEAT_FLOAT
3936 && tv->v_type != VAR_FLOAT
3937#endif
3938 )
3939 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003940 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003941 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003942 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003943 }
3944#ifdef FEAT_FLOAT
3945 if (tv->v_type == VAR_FLOAT)
3946 tv->vval.v_float = -tv->vval.v_float;
3947 else
3948#endif
3949 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 break;
3951
3952 case ISN_CHECKNR:
3953 {
3954 int error = FALSE;
3955
3956 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003957 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003959 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 (void)tv_get_number_chk(tv, &error);
3961 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003962 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 }
3964 break;
3965
3966 case ISN_CHECKTYPE:
3967 {
3968 checktype_T *ct = &iptr->isn_arg.type;
3969
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003970 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003971 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003972 if (!ectx->ec_where.wt_variable)
3973 ectx->ec_where.wt_index = ct->ct_arg_idx;
3974 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3975 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003976 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003977 if (!ectx->ec_where.wt_variable)
3978 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003979
3980 // number 0 is FALSE, number 1 is TRUE
3981 if (tv->v_type == VAR_NUMBER
3982 && ct->ct_type->tt_type == VAR_BOOL
3983 && (tv->vval.v_number == 0
3984 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003986 tv->v_type = VAR_BOOL;
3987 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003988 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 }
3990 }
3991 break;
3992
Bram Moolenaar9af78762020-06-16 11:34:42 +02003993 case ISN_CHECKLEN:
3994 {
3995 int min_len = iptr->isn_arg.checklen.cl_min_len;
3996 list_T *list = NULL;
3997
3998 tv = STACK_TV_BOT(-1);
3999 if (tv->v_type == VAR_LIST)
4000 list = tv->vval.v_list;
4001 if (list == NULL || list->lv_len < min_len
4002 || (list->lv_len > min_len
4003 && !iptr->isn_arg.checklen.cl_more_OK))
4004 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004005 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004006 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004007 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004008 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004009 }
4010 }
4011 break;
4012
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004013 case ISN_SETTYPE:
4014 {
4015 checktype_T *ct = &iptr->isn_arg.type;
4016
4017 tv = STACK_TV_BOT(-1);
4018 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4019 {
4020 free_type(tv->vval.v_dict->dv_type);
4021 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4022 }
4023 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4024 {
4025 free_type(tv->vval.v_list->lv_type);
4026 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4027 }
4028 }
4029 break;
4030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004032 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 {
4034 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004035 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004037 if (iptr->isn_type == ISN_2BOOL)
4038 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004039 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004040 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004041 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004042 n = !n;
4043 }
4044 else
4045 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004046 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004047 SOURCING_LNUM = iptr->isn_lnum;
4048 n = tv_get_bool_chk(tv, &error);
4049 if (error)
4050 goto on_error;
4051 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 clear_tv(tv);
4053 tv->v_type = VAR_BOOL;
4054 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4055 }
4056 break;
4057
4058 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004059 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004060 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004061 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4062 iptr->isn_type == ISN_2STRING_ANY,
4063 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004064 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 break;
4066
Bram Moolenaar08597872020-12-10 19:43:40 +01004067 case ISN_RANGE:
4068 {
4069 exarg_T ea;
4070 char *errormsg;
4071
Bram Moolenaarece0b872021-01-08 20:40:45 +01004072 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004073 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004074 ea.addr_type = ADDR_LINES;
4075 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004076 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004077 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004078 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004079
Bram Moolenaar4c137212021-04-19 16:48:48 +02004080 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004081 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004082 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004083 tv = STACK_TV_BOT(-1);
4084 tv->v_type = VAR_NUMBER;
4085 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004086 if (ea.addr_count == 0)
4087 tv->vval.v_number = curwin->w_cursor.lnum;
4088 else
4089 tv->vval.v_number = ea.line2;
4090 }
4091 break;
4092
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004093 case ISN_PUT:
4094 {
4095 int regname = iptr->isn_arg.put.put_regname;
4096 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4097 char_u *expr = NULL;
4098 int dir = FORWARD;
4099
Bram Moolenaar08597872020-12-10 19:43:40 +01004100 if (lnum < -2)
4101 {
4102 // line number was put on the stack by ISN_RANGE
4103 tv = STACK_TV_BOT(-1);
4104 curwin->w_cursor.lnum = tv->vval.v_number;
4105 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4106 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004107 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004108 }
4109 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004110 // :put! above cursor
4111 dir = BACKWARD;
4112 else if (lnum >= 0)
4113 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004114
4115 if (regname == '=')
4116 {
4117 tv = STACK_TV_BOT(-1);
4118 if (tv->v_type == VAR_STRING)
4119 expr = tv->vval.v_string;
4120 else
4121 {
4122 expr = typval2string(tv, TRUE); // allocates value
4123 clear_tv(tv);
4124 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004125 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004126 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004127 check_cursor();
4128 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4129 vim_free(expr);
4130 }
4131 break;
4132
Bram Moolenaar02194d22020-10-24 23:08:38 +02004133 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004134 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4135 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4136 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4137 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004138 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4139 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004140 break;
4141
Bram Moolenaar02194d22020-10-24 23:08:38 +02004142 case ISN_CMDMOD_REV:
4143 // filter regprog is owned by the instruction, don't free it
4144 cmdmod.cmod_filter_regmatch.regprog = NULL;
4145 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004146 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4147 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004148 break;
4149
Bram Moolenaar792f7862020-11-23 08:31:18 +01004150 case ISN_UNPACK:
4151 {
4152 int count = iptr->isn_arg.unpack.unp_count;
4153 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4154 list_T *l;
4155 listitem_T *li;
4156 int i;
4157
4158 // Check there is a valid list to unpack.
4159 tv = STACK_TV_BOT(-1);
4160 if (tv->v_type != VAR_LIST)
4161 {
4162 SOURCING_LNUM = iptr->isn_lnum;
4163 emsg(_(e_for_argument_must_be_sequence_of_lists));
4164 goto on_error;
4165 }
4166 l = tv->vval.v_list;
4167 if (l == NULL
4168 || l->lv_len < (semicolon ? count - 1 : count))
4169 {
4170 SOURCING_LNUM = iptr->isn_lnum;
4171 emsg(_(e_list_value_does_not_have_enough_items));
4172 goto on_error;
4173 }
4174 else if (!semicolon && l->lv_len > count)
4175 {
4176 SOURCING_LNUM = iptr->isn_lnum;
4177 emsg(_(e_list_value_has_more_items_than_targets));
4178 goto on_error;
4179 }
4180
4181 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004182 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004183 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004184 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004185
4186 // Variable after semicolon gets a list with the remaining
4187 // items.
4188 if (semicolon)
4189 {
4190 list_T *rem_list =
4191 list_alloc_with_items(l->lv_len - count + 1);
4192
4193 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004194 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004195 tv = STACK_TV_BOT(-count);
4196 tv->vval.v_list = rem_list;
4197 ++rem_list->lv_refcount;
4198 tv->v_lock = 0;
4199 li = l->lv_first;
4200 for (i = 0; i < count - 1; ++i)
4201 li = li->li_next;
4202 for (i = 0; li != NULL; ++i)
4203 {
4204 list_set_item(rem_list, i, &li->li_tv);
4205 li = li->li_next;
4206 }
4207 --count;
4208 }
4209
4210 // Produce the values in reverse order, first item last.
4211 li = l->lv_first;
4212 for (i = 0; i < count; ++i)
4213 {
4214 tv = STACK_TV_BOT(-i - 1);
4215 copy_tv(&li->li_tv, tv);
4216 li = li->li_next;
4217 }
4218
4219 list_unref(l);
4220 }
4221 break;
4222
Bram Moolenaarb2049902021-01-24 12:53:53 +01004223 case ISN_PROF_START:
4224 case ISN_PROF_END:
4225 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004226#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004227 funccall_T cookie;
4228 ufunc_T *cur_ufunc =
4229 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004230 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004231
4232 cookie.func = cur_ufunc;
4233 if (iptr->isn_type == ISN_PROF_START)
4234 {
4235 func_line_start(&cookie, iptr->isn_lnum);
4236 // if we get here the instruction is executed
4237 func_line_exec(&cookie);
4238 }
4239 else
4240 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004241#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004242 }
4243 break;
4244
Bram Moolenaare99d4222021-06-13 14:01:26 +02004245 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004246 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004247 break;
4248
Bram Moolenaar389df252020-07-09 21:20:47 +02004249 case ISN_SHUFFLE:
4250 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004251 typval_T tmp_tv;
4252 int item = iptr->isn_arg.shuffle.shfl_item;
4253 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004254
4255 tmp_tv = *STACK_TV_BOT(-item);
4256 for ( ; up > 0 && item > 1; --up)
4257 {
4258 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4259 --item;
4260 }
4261 *STACK_TV_BOT(-item) = tmp_tv;
4262 }
4263 break;
4264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004266 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004268 ectx->ec_where.wt_index = 0;
4269 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 break;
4271 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004272 continue;
4273
Bram Moolenaard032f342020-07-18 18:13:02 +02004274func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004275 // Restore previous function. If the frame pointer is where we started
4276 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004277 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004278 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004279
Bram Moolenaar4c137212021-04-19 16:48:48 +02004280 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004281 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004282 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004283 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004284
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004285on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004286 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004287 // If "emsg_silent" is set then ignore the error, unless it was set
4288 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004289 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004290 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004291 {
4292 // If a sequence of instructions causes an error while ":silent!"
4293 // was used, restore the stack length and jump ahead to restoring
4294 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004295 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004296 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004297 while (ectx->ec_stack.ga_len
4298 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004299 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004300 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004301 clear_tv(STACK_TV_BOT(0));
4302 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004303 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4304 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004305 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004306 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004307 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004308on_fatal_error:
4309 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004310 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004311 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004312 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 }
4314
4315done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004316 ret = OK;
4317theend:
4318 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4319 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004320}
4321
4322/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004323 * Execute the instructions from a VAR_INSTR typeval and put the result in
4324 * "rettv".
4325 * Return OK or FAIL.
4326 */
4327 int
4328exe_typval_instr(typval_T *tv, typval_T *rettv)
4329{
4330 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4331 isn_T *save_instr = ectx->ec_instr;
4332 int save_iidx = ectx->ec_iidx;
4333 int res;
4334
4335 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4336 res = exec_instructions(ectx);
4337 if (res == OK)
4338 {
4339 *rettv = *STACK_TV_BOT(-1);
4340 --ectx->ec_stack.ga_len;
4341 }
4342
4343 ectx->ec_instr = save_instr;
4344 ectx->ec_iidx = save_iidx;
4345
4346 return res;
4347}
4348
4349/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004350 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4351 * "substitute_instr".
4352 */
4353 char_u *
4354exe_substitute_instr(void)
4355{
4356 ectx_T *ectx = substitute_instr->subs_ectx;
4357 isn_T *save_instr = ectx->ec_instr;
4358 int save_iidx = ectx->ec_iidx;
4359 char_u *res;
4360
4361 ectx->ec_instr = substitute_instr->subs_instr;
4362 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004363 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004364 typval_T *tv = STACK_TV_BOT(-1);
4365
Bram Moolenaar27523602021-06-05 21:36:19 +02004366 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004367 --ectx->ec_stack.ga_len;
4368 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004369 }
4370 else
4371 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004372 substitute_instr->subs_status = FAIL;
4373 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004374 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375
Bram Moolenaar4c137212021-04-19 16:48:48 +02004376 ectx->ec_instr = save_instr;
4377 ectx->ec_iidx = save_iidx;
4378
4379 return res;
4380}
4381
4382/*
4383 * Call a "def" function from old Vim script.
4384 * Return OK or FAIL.
4385 */
4386 int
4387call_def_function(
4388 ufunc_T *ufunc,
4389 int argc_arg, // nr of arguments
4390 typval_T *argv, // arguments
4391 partial_T *partial, // optional partial for context
4392 typval_T *rettv) // return value
4393{
4394 ectx_T ectx; // execution context
4395 int argc = argc_arg;
4396 typval_T *tv;
4397 int idx;
4398 int ret = FAIL;
4399 int defcount = ufunc->uf_args.ga_len - argc;
4400 sctx_T save_current_sctx = current_sctx;
4401 int did_emsg_before = did_emsg_cumul + did_emsg;
4402 int save_suppress_errthrow = suppress_errthrow;
4403 msglist_T **saved_msg_list = NULL;
4404 msglist_T *private_msg_list = NULL;
4405 int save_emsg_silent_def = emsg_silent_def;
4406 int save_did_emsg_def = did_emsg_def;
4407 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004408 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004409
4410// Get pointer to item in the stack.
4411#undef STACK_TV
4412#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4413
4414// Get pointer to item at the bottom of the stack, -1 is the bottom.
4415#undef STACK_TV_BOT
4416#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4417
4418// Get pointer to a local variable on the stack. Negative for arguments.
4419#undef STACK_TV_VAR
4420#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4421
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004422 // Update uf_has_breakpoint if needed.
4423 update_has_breakpoint(ufunc);
4424
Bram Moolenaar4c137212021-04-19 16:48:48 +02004425 if (ufunc->uf_def_status == UF_NOT_COMPILED
4426 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004427 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4428 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004429 == FAIL))
4430 {
4431 if (did_emsg_cumul + did_emsg == did_emsg_before)
4432 semsg(_(e_function_is_not_compiled_str),
4433 printable_func_name(ufunc));
4434 return FAIL;
4435 }
4436
4437 {
4438 // Check the function was really compiled.
4439 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4440 + ufunc->uf_dfunc_idx;
4441 if (INSTRUCTIONS(dfunc) == NULL)
4442 {
4443 iemsg("using call_def_function() on not compiled function");
4444 return FAIL;
4445 }
4446 }
4447
4448 // If depth of calling is getting too high, don't execute the function.
4449 orig_funcdepth = funcdepth_get();
4450 if (funcdepth_increment() == FAIL)
4451 return FAIL;
4452
4453 CLEAR_FIELD(ectx);
4454 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4455 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4456 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4457 {
4458 funcdepth_decrement();
4459 return FAIL;
4460 }
4461 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4462 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4463 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004464 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004465
4466 idx = argc - ufunc->uf_args.ga_len;
4467 if (idx > 0 && ufunc->uf_va_name == NULL)
4468 {
4469 if (idx == 1)
4470 emsg(_(e_one_argument_too_many));
4471 else
4472 semsg(_(e_nr_arguments_too_many), idx);
4473 goto failed_early;
4474 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004475 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4476 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004477 {
4478 if (idx == -1)
4479 emsg(_(e_one_argument_too_few));
4480 else
4481 semsg(_(e_nr_arguments_too_few), -idx);
4482 goto failed_early;
4483 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004484
4485 // Put arguments on the stack, but no more than what the function expects.
4486 // A lambda can be called with more arguments than it uses.
4487 for (idx = 0; idx < argc
4488 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4489 ++idx)
4490 {
4491 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4492 && argv[idx].v_type == VAR_SPECIAL
4493 && argv[idx].vval.v_number == VVAL_NONE)
4494 {
4495 // Use the default value.
4496 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4497 }
4498 else
4499 {
4500 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4501 && check_typval_arg_type(
4502 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4503 goto failed_early;
4504 copy_tv(&argv[idx], STACK_TV_BOT(0));
4505 }
4506 ++ectx.ec_stack.ga_len;
4507 }
4508
4509 // Turn varargs into a list. Empty list if no args.
4510 if (ufunc->uf_va_name != NULL)
4511 {
4512 int vararg_count = argc - ufunc->uf_args.ga_len;
4513
4514 if (vararg_count < 0)
4515 vararg_count = 0;
4516 else
4517 argc -= vararg_count;
4518 if (exe_newlist(vararg_count, &ectx) == FAIL)
4519 goto failed_early;
4520
4521 // Check the type of the list items.
4522 tv = STACK_TV_BOT(-1);
4523 if (ufunc->uf_va_type != NULL
4524 && ufunc->uf_va_type != &t_list_any
4525 && ufunc->uf_va_type->tt_member != &t_any
4526 && tv->vval.v_list != NULL)
4527 {
4528 type_T *expected = ufunc->uf_va_type->tt_member;
4529 listitem_T *li = tv->vval.v_list->lv_first;
4530
4531 for (idx = 0; idx < vararg_count; ++idx)
4532 {
4533 if (check_typval_arg_type(expected, &li->li_tv,
4534 argc + idx + 1) == FAIL)
4535 goto failed_early;
4536 li = li->li_next;
4537 }
4538 }
4539
4540 if (defcount > 0)
4541 // Move varargs list to below missing default arguments.
4542 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4543 --ectx.ec_stack.ga_len;
4544 }
4545
4546 // Make space for omitted arguments, will store default value below.
4547 // Any varargs list goes after them.
4548 if (defcount > 0)
4549 for (idx = 0; idx < defcount; ++idx)
4550 {
4551 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4552 ++ectx.ec_stack.ga_len;
4553 }
4554 if (ufunc->uf_va_name != NULL)
4555 ++ectx.ec_stack.ga_len;
4556
4557 // Frame pointer points to just after arguments.
4558 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4559 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4560
4561 {
4562 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4563 + ufunc->uf_dfunc_idx;
4564 ufunc_T *base_ufunc = dfunc->df_ufunc;
4565
4566 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4567 // by copy_func().
4568 if (partial != NULL || base_ufunc->uf_partial != NULL)
4569 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004570 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4571 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004572 goto failed_early;
4573 if (partial != NULL)
4574 {
4575 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4576 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004577 if (current_ectx->ec_outer_ref != NULL
4578 && current_ectx->ec_outer_ref->or_outer != NULL)
4579 ectx.ec_outer_ref->or_outer =
4580 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004581 }
4582 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004583 {
4584 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4585 ++partial->pt_refcount;
4586 ectx.ec_outer_ref->or_partial = partial;
4587 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004588 }
4589 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004590 {
4591 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4592 ++base_ufunc->uf_partial->pt_refcount;
4593 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4594 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004595 }
4596 }
4597
4598 // dummy frame entries
4599 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4600 {
4601 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4602 ++ectx.ec_stack.ga_len;
4603 }
4604
4605 {
4606 // Reserve space for local variables and any closure reference count.
4607 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4608 + ufunc->uf_dfunc_idx;
4609
4610 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4611 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4612 ectx.ec_stack.ga_len += dfunc->df_varcount;
4613 if (dfunc->df_has_closure)
4614 {
4615 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4616 STACK_TV_VAR(idx)->vval.v_number = 0;
4617 ++ectx.ec_stack.ga_len;
4618 }
4619
4620 ectx.ec_instr = INSTRUCTIONS(dfunc);
4621 }
4622
4623 // Following errors are in the function, not the caller.
4624 // Commands behave like vim9script.
4625 estack_push_ufunc(ufunc, 1);
4626 current_sctx = ufunc->uf_script_ctx;
4627 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4628
4629 // Use a specific location for storing error messages to be converted to an
4630 // exception.
4631 saved_msg_list = msg_list;
4632 msg_list = &private_msg_list;
4633
4634 // Do turn errors into exceptions.
4635 suppress_errthrow = FALSE;
4636
4637 // Do not delete the function while executing it.
4638 ++ufunc->uf_calls;
4639
4640 // When ":silent!" was used before calling then we still abort the
4641 // function. If ":silent!" is used in the function then we don't.
4642 emsg_silent_def = emsg_silent;
4643 did_emsg_def = 0;
4644
4645 ectx.ec_where.wt_index = 0;
4646 ectx.ec_where.wt_variable = FALSE;
4647
4648 // Execute the instructions until done.
4649 ret = exec_instructions(&ectx);
4650 if (ret == OK)
4651 {
4652 // function finished, get result from the stack.
4653 if (ufunc->uf_ret_type == &t_void)
4654 {
4655 rettv->v_type = VAR_VOID;
4656 }
4657 else
4658 {
4659 tv = STACK_TV_BOT(-1);
4660 *rettv = *tv;
4661 tv->v_type = VAR_UNKNOWN;
4662 }
4663 }
4664
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004665 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004666 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4667 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004668
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004669 // Deal with any remaining closures, they may be in use somewhere.
4670 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004671 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004672 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004673 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4674 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004675
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004676 estack_pop();
4677 current_sctx = save_current_sctx;
4678
Bram Moolenaarc970e422021-03-17 15:03:04 +01004679 // TODO: when is it safe to delete the function if it is no longer used?
4680 --ufunc->uf_calls;
4681
Bram Moolenaar352134b2020-10-17 22:04:08 +02004682 if (*msg_list != NULL && saved_msg_list != NULL)
4683 {
4684 msglist_T **plist = saved_msg_list;
4685
4686 // Append entries from the current msg_list (uncaught exceptions) to
4687 // the saved msg_list.
4688 while (*plist != NULL)
4689 plist = &(*plist)->next;
4690
4691 *plist = *msg_list;
4692 }
4693 msg_list = saved_msg_list;
4694
Bram Moolenaar4c137212021-04-19 16:48:48 +02004695 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004696 {
4697 cmdmod.cmod_filter_regmatch.regprog = NULL;
4698 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004699 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004700 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004701 emsg_silent_def = save_emsg_silent_def;
4702 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004703
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004704failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004705 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4707 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004708 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004711 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004712 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004713 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004714 if (ectx.ec_outer_ref->or_outer_allocated)
4715 vim_free(ectx.ec_outer_ref->or_outer);
4716 partial_unref(ectx.ec_outer_ref->or_partial);
4717 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004718 }
4719
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004720 // Not sure if this is necessary.
4721 suppress_errthrow = save_suppress_errthrow;
4722
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004723 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004724 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004725 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004726 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 return ret;
4728}
4729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004731 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4732 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4733 * "pfx" is prefixed to every line.
4734 */
4735 static void
4736list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4737{
4738 int line_idx = 0;
4739 int prev_current = 0;
4740 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004741 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004742
4743 for (current = 0; current < instr_count; ++current)
4744 {
4745 isn_T *iptr = &instr[current];
4746 char *line;
4747
4748 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004749 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004750 while (line_idx < iptr->isn_lnum
4751 && line_idx < ufunc->uf_lines.ga_len)
4752 {
4753 if (current > prev_current)
4754 {
4755 msg_puts("\n\n");
4756 prev_current = current;
4757 }
4758 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4759 if (line != NULL)
4760 msg(line);
4761 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004762 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4763 {
4764 int first_def_arg = ufunc->uf_args.ga_len
4765 - ufunc->uf_def_args.ga_len;
4766
4767 if (def_arg_idx > 0)
4768 msg_puts("\n\n");
4769 msg_start();
4770 msg_puts(" ");
4771 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4772 first_def_arg + def_arg_idx]);
4773 msg_puts(" = ");
4774 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4775 msg_clr_eos();
4776 msg_end();
4777 }
4778 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004779
4780 switch (iptr->isn_type)
4781 {
4782 case ISN_EXEC:
4783 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4784 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004785 case ISN_EXEC_SPLIT:
4786 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4787 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004788 case ISN_LEGACY_EVAL:
4789 smsg("%s%4d EVAL legacy %s", pfx, current,
4790 iptr->isn_arg.string);
4791 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004792 case ISN_REDIRSTART:
4793 smsg("%s%4d REDIR", pfx, current);
4794 break;
4795 case ISN_REDIREND:
4796 smsg("%s%4d REDIR END%s", pfx, current,
4797 iptr->isn_arg.number ? " append" : "");
4798 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004799 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004800#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004801 smsg("%s%4d CEXPR pre %s", pfx, current,
4802 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004803#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004804 break;
4805 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004806#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004807 {
4808 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4809
4810 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4811 cexpr_get_auname(cer->cer_cmdidx),
4812 cer->cer_forceit ? "!" : "",
4813 cer->cer_cmdline);
4814 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004815#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004816 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004817 case ISN_INSTR:
4818 {
4819 smsg("%s%4d INSTR", pfx, current);
4820 list_instructions(" ", iptr->isn_arg.instr,
4821 INT_MAX, NULL);
4822 msg(" -------------");
4823 }
4824 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004825 case ISN_SUBSTITUTE:
4826 {
4827 subs_T *subs = &iptr->isn_arg.subs;
4828
4829 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4830 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4831 msg(" -------------");
4832 }
4833 break;
4834 case ISN_EXECCONCAT:
4835 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4836 (varnumber_T)iptr->isn_arg.number);
4837 break;
4838 case ISN_ECHO:
4839 {
4840 echo_T *echo = &iptr->isn_arg.echo;
4841
4842 smsg("%s%4d %s %d", pfx, current,
4843 echo->echo_with_white ? "ECHO" : "ECHON",
4844 echo->echo_count);
4845 }
4846 break;
4847 case ISN_EXECUTE:
4848 smsg("%s%4d EXECUTE %lld", pfx, current,
4849 (varnumber_T)(iptr->isn_arg.number));
4850 break;
4851 case ISN_ECHOMSG:
4852 smsg("%s%4d ECHOMSG %lld", pfx, current,
4853 (varnumber_T)(iptr->isn_arg.number));
4854 break;
4855 case ISN_ECHOERR:
4856 smsg("%s%4d ECHOERR %lld", pfx, current,
4857 (varnumber_T)(iptr->isn_arg.number));
4858 break;
4859 case ISN_LOAD:
4860 {
4861 if (iptr->isn_arg.number < 0)
4862 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4863 (varnumber_T)(iptr->isn_arg.number
4864 + STACK_FRAME_SIZE));
4865 else
4866 smsg("%s%4d LOAD $%lld", pfx, current,
4867 (varnumber_T)(iptr->isn_arg.number));
4868 }
4869 break;
4870 case ISN_LOADOUTER:
4871 {
4872 if (iptr->isn_arg.number < 0)
4873 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4874 iptr->isn_arg.outer.outer_depth,
4875 iptr->isn_arg.outer.outer_idx
4876 + STACK_FRAME_SIZE);
4877 else
4878 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4879 iptr->isn_arg.outer.outer_depth,
4880 iptr->isn_arg.outer.outer_idx);
4881 }
4882 break;
4883 case ISN_LOADV:
4884 smsg("%s%4d LOADV v:%s", pfx, current,
4885 get_vim_var_name(iptr->isn_arg.number));
4886 break;
4887 case ISN_LOADSCRIPT:
4888 {
4889 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4890 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4891 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4892 + sref->sref_idx;
4893
4894 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4895 sv->sv_name,
4896 sref->sref_idx,
4897 si->sn_name);
4898 }
4899 break;
4900 case ISN_LOADS:
4901 {
4902 scriptitem_T *si = SCRIPT_ITEM(
4903 iptr->isn_arg.loadstore.ls_sid);
4904
4905 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4906 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4907 }
4908 break;
4909 case ISN_LOADAUTO:
4910 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4911 break;
4912 case ISN_LOADG:
4913 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4914 break;
4915 case ISN_LOADB:
4916 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4917 break;
4918 case ISN_LOADW:
4919 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4920 break;
4921 case ISN_LOADT:
4922 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4923 break;
4924 case ISN_LOADGDICT:
4925 smsg("%s%4d LOAD g:", pfx, current);
4926 break;
4927 case ISN_LOADBDICT:
4928 smsg("%s%4d LOAD b:", pfx, current);
4929 break;
4930 case ISN_LOADWDICT:
4931 smsg("%s%4d LOAD w:", pfx, current);
4932 break;
4933 case ISN_LOADTDICT:
4934 smsg("%s%4d LOAD t:", pfx, current);
4935 break;
4936 case ISN_LOADOPT:
4937 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4938 break;
4939 case ISN_LOADENV:
4940 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4941 break;
4942 case ISN_LOADREG:
4943 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4944 break;
4945
4946 case ISN_STORE:
4947 if (iptr->isn_arg.number < 0)
4948 smsg("%s%4d STORE arg[%lld]", pfx, current,
4949 iptr->isn_arg.number + STACK_FRAME_SIZE);
4950 else
4951 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4952 break;
4953 case ISN_STOREOUTER:
4954 {
4955 if (iptr->isn_arg.number < 0)
4956 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4957 iptr->isn_arg.outer.outer_depth,
4958 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4959 else
4960 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4961 iptr->isn_arg.outer.outer_depth,
4962 iptr->isn_arg.outer.outer_idx);
4963 }
4964 break;
4965 case ISN_STOREV:
4966 smsg("%s%4d STOREV v:%s", pfx, current,
4967 get_vim_var_name(iptr->isn_arg.number));
4968 break;
4969 case ISN_STOREAUTO:
4970 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4971 break;
4972 case ISN_STOREG:
4973 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4974 break;
4975 case ISN_STOREB:
4976 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4977 break;
4978 case ISN_STOREW:
4979 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4980 break;
4981 case ISN_STORET:
4982 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4983 break;
4984 case ISN_STORES:
4985 {
4986 scriptitem_T *si = SCRIPT_ITEM(
4987 iptr->isn_arg.loadstore.ls_sid);
4988
4989 smsg("%s%4d STORES %s in %s", pfx, current,
4990 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4991 }
4992 break;
4993 case ISN_STORESCRIPT:
4994 {
4995 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4996 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4997 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4998 + sref->sref_idx;
4999
5000 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
5001 sv->sv_name,
5002 sref->sref_idx,
5003 si->sn_name);
5004 }
5005 break;
5006 case ISN_STOREOPT:
5007 smsg("%s%4d STOREOPT &%s", pfx, current,
5008 iptr->isn_arg.storeopt.so_name);
5009 break;
5010 case ISN_STOREENV:
5011 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5012 break;
5013 case ISN_STOREREG:
5014 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
5015 break;
5016 case ISN_STORENR:
5017 smsg("%s%4d STORE %lld in $%d", pfx, current,
5018 iptr->isn_arg.storenr.stnr_val,
5019 iptr->isn_arg.storenr.stnr_idx);
5020 break;
5021
5022 case ISN_STOREINDEX:
5023 smsg("%s%4d STOREINDEX %s", pfx, current,
5024 vartype_name(iptr->isn_arg.vartype));
5025 break;
5026
5027 case ISN_STORERANGE:
5028 smsg("%s%4d STORERANGE", pfx, current);
5029 break;
5030
5031 // constants
5032 case ISN_PUSHNR:
5033 smsg("%s%4d PUSHNR %lld", pfx, current,
5034 (varnumber_T)(iptr->isn_arg.number));
5035 break;
5036 case ISN_PUSHBOOL:
5037 case ISN_PUSHSPEC:
5038 smsg("%s%4d PUSH %s", pfx, current,
5039 get_var_special_name(iptr->isn_arg.number));
5040 break;
5041 case ISN_PUSHF:
5042#ifdef FEAT_FLOAT
5043 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5044#endif
5045 break;
5046 case ISN_PUSHS:
5047 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5048 break;
5049 case ISN_PUSHBLOB:
5050 {
5051 char_u *r;
5052 char_u numbuf[NUMBUFLEN];
5053 char_u *tofree;
5054
5055 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5056 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5057 vim_free(tofree);
5058 }
5059 break;
5060 case ISN_PUSHFUNC:
5061 {
5062 char *name = (char *)iptr->isn_arg.string;
5063
5064 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5065 name == NULL ? "[none]" : name);
5066 }
5067 break;
5068 case ISN_PUSHCHANNEL:
5069#ifdef FEAT_JOB_CHANNEL
5070 {
5071 channel_T *channel = iptr->isn_arg.channel;
5072
5073 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5074 channel == NULL ? 0 : channel->ch_id);
5075 }
5076#endif
5077 break;
5078 case ISN_PUSHJOB:
5079#ifdef FEAT_JOB_CHANNEL
5080 {
5081 typval_T tv;
5082 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005083 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005084
5085 tv.v_type = VAR_JOB;
5086 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005087 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005088 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5089 }
5090#endif
5091 break;
5092 case ISN_PUSHEXC:
5093 smsg("%s%4d PUSH v:exception", pfx, current);
5094 break;
5095 case ISN_UNLET:
5096 smsg("%s%4d UNLET%s %s", pfx, current,
5097 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5098 iptr->isn_arg.unlet.ul_name);
5099 break;
5100 case ISN_UNLETENV:
5101 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5102 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5103 iptr->isn_arg.unlet.ul_name);
5104 break;
5105 case ISN_UNLETINDEX:
5106 smsg("%s%4d UNLETINDEX", pfx, current);
5107 break;
5108 case ISN_UNLETRANGE:
5109 smsg("%s%4d UNLETRANGE", pfx, current);
5110 break;
5111 case ISN_LOCKCONST:
5112 smsg("%s%4d LOCKCONST", pfx, current);
5113 break;
5114 case ISN_NEWLIST:
5115 smsg("%s%4d NEWLIST size %lld", pfx, current,
5116 (varnumber_T)(iptr->isn_arg.number));
5117 break;
5118 case ISN_NEWDICT:
5119 smsg("%s%4d NEWDICT size %lld", pfx, current,
5120 (varnumber_T)(iptr->isn_arg.number));
5121 break;
5122
5123 // function call
5124 case ISN_BCALL:
5125 {
5126 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5127
5128 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5129 internal_func_name(cbfunc->cbf_idx),
5130 cbfunc->cbf_argcount);
5131 }
5132 break;
5133 case ISN_DCALL:
5134 {
5135 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5136 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5137 + cdfunc->cdf_idx;
5138
5139 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5140 df->df_ufunc->uf_name_exp != NULL
5141 ? df->df_ufunc->uf_name_exp
5142 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5143 }
5144 break;
5145 case ISN_UCALL:
5146 {
5147 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5148
5149 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5150 cufunc->cuf_name, cufunc->cuf_argcount);
5151 }
5152 break;
5153 case ISN_PCALL:
5154 {
5155 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5156
5157 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5158 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5159 }
5160 break;
5161 case ISN_PCALL_END:
5162 smsg("%s%4d PCALL end", pfx, current);
5163 break;
5164 case ISN_RETURN:
5165 smsg("%s%4d RETURN", pfx, current);
5166 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005167 case ISN_RETURN_VOID:
5168 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005169 break;
5170 case ISN_FUNCREF:
5171 {
5172 funcref_T *funcref = &iptr->isn_arg.funcref;
5173 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5174 + funcref->fr_func;
5175
5176 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5177 }
5178 break;
5179
5180 case ISN_NEWFUNC:
5181 {
5182 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5183
5184 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5185 newfunc->nf_lambda, newfunc->nf_global);
5186 }
5187 break;
5188
5189 case ISN_DEF:
5190 {
5191 char_u *name = iptr->isn_arg.string;
5192
5193 smsg("%s%4d DEF %s", pfx, current,
5194 name == NULL ? (char_u *)"" : name);
5195 }
5196 break;
5197
5198 case ISN_JUMP:
5199 {
5200 char *when = "?";
5201
5202 switch (iptr->isn_arg.jump.jump_when)
5203 {
5204 case JUMP_ALWAYS:
5205 when = "JUMP";
5206 break;
5207 case JUMP_AND_KEEP_IF_TRUE:
5208 when = "JUMP_AND_KEEP_IF_TRUE";
5209 break;
5210 case JUMP_IF_FALSE:
5211 when = "JUMP_IF_FALSE";
5212 break;
5213 case JUMP_AND_KEEP_IF_FALSE:
5214 when = "JUMP_AND_KEEP_IF_FALSE";
5215 break;
5216 case JUMP_IF_COND_FALSE:
5217 when = "JUMP_IF_COND_FALSE";
5218 break;
5219 case JUMP_IF_COND_TRUE:
5220 when = "JUMP_IF_COND_TRUE";
5221 break;
5222 }
5223 smsg("%s%4d %s -> %d", pfx, current, when,
5224 iptr->isn_arg.jump.jump_where);
5225 }
5226 break;
5227
5228 case ISN_JUMP_IF_ARG_SET:
5229 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5230 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5231 iptr->isn_arg.jump.jump_where);
5232 break;
5233
5234 case ISN_FOR:
5235 {
5236 forloop_T *forloop = &iptr->isn_arg.forloop;
5237
5238 smsg("%s%4d FOR $%d -> %d", pfx, current,
5239 forloop->for_idx, forloop->for_end);
5240 }
5241 break;
5242
5243 case ISN_TRY:
5244 {
5245 try_T *try = &iptr->isn_arg.try;
5246
5247 if (try->try_ref->try_finally == 0)
5248 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5249 pfx, current,
5250 try->try_ref->try_catch,
5251 try->try_ref->try_endtry);
5252 else
5253 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5254 pfx, current,
5255 try->try_ref->try_catch,
5256 try->try_ref->try_finally,
5257 try->try_ref->try_endtry);
5258 }
5259 break;
5260 case ISN_CATCH:
5261 // TODO
5262 smsg("%s%4d CATCH", pfx, current);
5263 break;
5264 case ISN_TRYCONT:
5265 {
5266 trycont_T *trycont = &iptr->isn_arg.trycont;
5267
5268 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5269 trycont->tct_levels,
5270 trycont->tct_levels == 1 ? "" : "s",
5271 trycont->tct_where);
5272 }
5273 break;
5274 case ISN_FINALLY:
5275 smsg("%s%4d FINALLY", pfx, current);
5276 break;
5277 case ISN_ENDTRY:
5278 smsg("%s%4d ENDTRY", pfx, current);
5279 break;
5280 case ISN_THROW:
5281 smsg("%s%4d THROW", pfx, current);
5282 break;
5283
5284 // expression operations on number
5285 case ISN_OPNR:
5286 case ISN_OPFLOAT:
5287 case ISN_OPANY:
5288 {
5289 char *what;
5290 char *ins;
5291
5292 switch (iptr->isn_arg.op.op_type)
5293 {
5294 case EXPR_MULT: what = "*"; break;
5295 case EXPR_DIV: what = "/"; break;
5296 case EXPR_REM: what = "%"; break;
5297 case EXPR_SUB: what = "-"; break;
5298 case EXPR_ADD: what = "+"; break;
5299 default: what = "???"; break;
5300 }
5301 switch (iptr->isn_type)
5302 {
5303 case ISN_OPNR: ins = "OPNR"; break;
5304 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5305 case ISN_OPANY: ins = "OPANY"; break;
5306 default: ins = "???"; break;
5307 }
5308 smsg("%s%4d %s %s", pfx, current, ins, what);
5309 }
5310 break;
5311
5312 case ISN_COMPAREBOOL:
5313 case ISN_COMPARESPECIAL:
5314 case ISN_COMPARENR:
5315 case ISN_COMPAREFLOAT:
5316 case ISN_COMPARESTRING:
5317 case ISN_COMPAREBLOB:
5318 case ISN_COMPARELIST:
5319 case ISN_COMPAREDICT:
5320 case ISN_COMPAREFUNC:
5321 case ISN_COMPAREANY:
5322 {
5323 char *p;
5324 char buf[10];
5325 char *type;
5326
5327 switch (iptr->isn_arg.op.op_type)
5328 {
5329 case EXPR_EQUAL: p = "=="; break;
5330 case EXPR_NEQUAL: p = "!="; break;
5331 case EXPR_GREATER: p = ">"; break;
5332 case EXPR_GEQUAL: p = ">="; break;
5333 case EXPR_SMALLER: p = "<"; break;
5334 case EXPR_SEQUAL: p = "<="; break;
5335 case EXPR_MATCH: p = "=~"; break;
5336 case EXPR_IS: p = "is"; break;
5337 case EXPR_ISNOT: p = "isnot"; break;
5338 case EXPR_NOMATCH: p = "!~"; break;
5339 default: p = "???"; break;
5340 }
5341 STRCPY(buf, p);
5342 if (iptr->isn_arg.op.op_ic == TRUE)
5343 strcat(buf, "?");
5344 switch(iptr->isn_type)
5345 {
5346 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5347 case ISN_COMPARESPECIAL:
5348 type = "COMPARESPECIAL"; break;
5349 case ISN_COMPARENR: type = "COMPARENR"; break;
5350 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5351 case ISN_COMPARESTRING:
5352 type = "COMPARESTRING"; break;
5353 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5354 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5355 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5356 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5357 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5358 default: type = "???"; break;
5359 }
5360
5361 smsg("%s%4d %s %s", pfx, current, type, buf);
5362 }
5363 break;
5364
5365 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5366 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5367
5368 // expression operations
5369 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5370 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5371 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5372 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5373 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5374 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5375 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5376 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5377 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5378 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5379 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5380 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5381 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005382 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5383 iptr->isn_arg.getitem.gi_index,
5384 iptr->isn_arg.getitem.gi_with_op ?
5385 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005386 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5387 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5388 iptr->isn_arg.string); break;
5389 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5390
5391 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5392 case ISN_CHECKTYPE:
5393 {
5394 checktype_T *ct = &iptr->isn_arg.type;
5395 char *tofree;
5396
5397 if (ct->ct_arg_idx == 0)
5398 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5399 type_name(ct->ct_type, &tofree),
5400 (int)ct->ct_off);
5401 else
5402 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5403 type_name(ct->ct_type, &tofree),
5404 (int)ct->ct_off,
5405 (int)ct->ct_arg_idx);
5406 vim_free(tofree);
5407 break;
5408 }
5409 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5410 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5411 iptr->isn_arg.checklen.cl_min_len);
5412 break;
5413 case ISN_SETTYPE:
5414 {
5415 char *tofree;
5416
5417 smsg("%s%4d SETTYPE %s", pfx, current,
5418 type_name(iptr->isn_arg.type.ct_type, &tofree));
5419 vim_free(tofree);
5420 break;
5421 }
5422 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005423 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5424 smsg("%s%4d INVERT %d (!val)", pfx, current,
5425 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005426 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005427 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5428 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005429 break;
5430 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005431 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005432 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005433 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5434 pfx, current,
5435 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005436 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005437 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5438 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005439 break;
5440 case ISN_PUT:
5441 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5442 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005443 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005444 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5445 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005446 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005447 else
5448 smsg("%s%4d PUT %c %ld", pfx, current,
5449 iptr->isn_arg.put.put_regname,
5450 (long)iptr->isn_arg.put.put_lnum);
5451 break;
5452
5453 // TODO: summarize modifiers
5454 case ISN_CMDMOD:
5455 {
5456 char_u *buf;
5457 size_t len = produce_cmdmods(
5458 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5459
5460 buf = alloc(len + 1);
5461 if (buf != NULL)
5462 {
5463 (void)produce_cmdmods(
5464 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5465 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5466 vim_free(buf);
5467 }
5468 break;
5469 }
5470 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5471
5472 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005473 smsg("%s%4d PROFILE START line %d", pfx, current,
5474 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005475 break;
5476
5477 case ISN_PROF_END:
5478 smsg("%s%4d PROFILE END", pfx, current);
5479 break;
5480
Bram Moolenaare99d4222021-06-13 14:01:26 +02005481 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005482 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5483 iptr->isn_arg.debug.dbg_break_lnum + 1,
5484 iptr->isn_lnum,
5485 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005486 break;
5487
Bram Moolenaar4c137212021-04-19 16:48:48 +02005488 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5489 iptr->isn_arg.unpack.unp_count,
5490 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5491 break;
5492 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5493 iptr->isn_arg.shuffle.shfl_item,
5494 iptr->isn_arg.shuffle.shfl_up);
5495 break;
5496 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5497
5498 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5499 return;
5500 }
5501
5502 out_flush(); // output one line at a time
5503 ui_breakcheck();
5504 if (got_int)
5505 break;
5506 }
5507}
5508
5509/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005510 * Handle command line completion for the :disassemble command.
5511 */
5512 void
5513set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5514{
5515 char_u *p;
5516
5517 // Default: expand user functions, "debug" and "profile"
5518 xp->xp_context = EXPAND_DISASSEMBLE;
5519 xp->xp_pattern = arg;
5520
5521 // first argument already typed: only user function names
5522 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5523 {
5524 xp->xp_context = EXPAND_USER_FUNC;
5525 xp->xp_pattern = skipwhite(p);
5526 }
5527}
5528
5529/*
5530 * Function given to ExpandGeneric() to obtain the list of :disassemble
5531 * arguments.
5532 */
5533 char_u *
5534get_disassemble_argument(expand_T *xp, int idx)
5535{
5536 if (idx == 0)
5537 return (char_u *)"debug";
5538 if (idx == 1)
5539 return (char_u *)"profile";
5540 return get_user_func_name(xp, idx - 2);
5541}
5542
5543/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005544 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005545 * We don't really need this at runtime, but we do have tests that require it,
5546 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005547 */
5548 void
5549ex_disassemble(exarg_T *eap)
5550{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005551 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005552 char_u *fname;
5553 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005554 dfunc_T *dfunc;
5555 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005556 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005557 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005558 compiletype_T compile_type = CT_NONE;
5559
5560 if (STRNCMP(arg, "profile", 7) == 0)
5561 {
5562 compile_type = CT_PROFILE;
5563 arg = skipwhite(arg + 7);
5564 }
5565 else if (STRNCMP(arg, "debug", 5) == 0)
5566 {
5567 compile_type = CT_DEBUG;
5568 arg = skipwhite(arg + 5);
5569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005570
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005571 if (STRNCMP(arg, "<lambda>", 8) == 0)
5572 {
5573 arg += 8;
5574 (void)getdigits(&arg);
5575 fname = vim_strnsave(eap->arg, arg - eap->arg);
5576 }
5577 else
5578 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005579 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005580 if (fname == NULL)
5581 {
5582 semsg(_(e_invarg2), eap->arg);
5583 return;
5584 }
5585
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005586 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005587 if (ufunc == NULL)
5588 {
5589 char_u *p = untrans_function_name(fname);
5590
5591 if (p != NULL)
5592 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005593 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005594 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005595 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005596 if (ufunc == NULL)
5597 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005598 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005599 return;
5600 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005601 if (func_needs_compiling(ufunc, compile_type)
5602 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005603 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005604 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005606 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607 return;
5608 }
5609 if (ufunc->uf_name_exp != NULL)
5610 msg((char *)ufunc->uf_name_exp);
5611 else
5612 msg((char *)ufunc->uf_name);
5613
5614 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005615 switch (compile_type)
5616 {
5617 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005618#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005619 instr = dfunc->df_instr_prof;
5620 instr_count = dfunc->df_instr_prof_count;
5621 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005622#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005623 // FALLTHROUGH
5624 case CT_NONE:
5625 instr = dfunc->df_instr;
5626 instr_count = dfunc->df_instr_count;
5627 break;
5628 case CT_DEBUG:
5629 instr = dfunc->df_instr_debug;
5630 instr_count = dfunc->df_instr_debug_count;
5631 break;
5632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633
Bram Moolenaar4c137212021-04-19 16:48:48 +02005634 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005635}
5636
5637/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005638 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005639 * list, etc. Mostly like what JavaScript does, except that empty list and
5640 * empty dictionary are FALSE.
5641 */
5642 int
5643tv2bool(typval_T *tv)
5644{
5645 switch (tv->v_type)
5646 {
5647 case VAR_NUMBER:
5648 return tv->vval.v_number != 0;
5649 case VAR_FLOAT:
5650#ifdef FEAT_FLOAT
5651 return tv->vval.v_float != 0.0;
5652#else
5653 break;
5654#endif
5655 case VAR_PARTIAL:
5656 return tv->vval.v_partial != NULL;
5657 case VAR_FUNC:
5658 case VAR_STRING:
5659 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5660 case VAR_LIST:
5661 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5662 case VAR_DICT:
5663 return tv->vval.v_dict != NULL
5664 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5665 case VAR_BOOL:
5666 case VAR_SPECIAL:
5667 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5668 case VAR_JOB:
5669#ifdef FEAT_JOB_CHANNEL
5670 return tv->vval.v_job != NULL;
5671#else
5672 break;
5673#endif
5674 case VAR_CHANNEL:
5675#ifdef FEAT_JOB_CHANNEL
5676 return tv->vval.v_channel != NULL;
5677#else
5678 break;
5679#endif
5680 case VAR_BLOB:
5681 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5682 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005683 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005684 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005685 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005686 break;
5687 }
5688 return FALSE;
5689}
5690
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005691 void
5692emsg_using_string_as(typval_T *tv, int as_number)
5693{
5694 semsg(_(as_number ? e_using_string_as_number_str
5695 : e_using_string_as_bool_str),
5696 tv->vval.v_string == NULL
5697 ? (char_u *)"" : tv->vval.v_string);
5698}
5699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005700/*
5701 * If "tv" is a string give an error and return FAIL.
5702 */
5703 int
5704check_not_string(typval_T *tv)
5705{
5706 if (tv->v_type == VAR_STRING)
5707 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005708 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005709 clear_tv(tv);
5710 return FAIL;
5711 }
5712 return OK;
5713}
5714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005715
5716#endif // FEAT_EVAL