blob: 4013c06cda2316fca72ca1be1a98efce29c4be8a [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 Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
67// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
74
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
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 Moolenaard3d8fee2021-06-30 19:54:43 +02001569 if (did_throw)
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 Moolenaard3d8fee2021-06-30 19:54:43 +02001573 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574
1575 // An exception jumps to the first catch, finally, or returns from
1576 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001577 while (index > 0)
1578 {
1579 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
1580 if (!trycmd->tcd_in_catch)
1581 break;
1582 // In the catch and finally block of this try we have to go up
1583 // one level.
1584 --index;
1585 trycmd = NULL;
1586 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001587 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 {
1589 // jump to ":catch" or ":finally"
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001590 trycmd->tcd_in_catch = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001591 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001592 did_throw = FALSE; // don't come back here until :endtry
1593 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 }
1595 else
1596 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001597 // Not inside try or need to return from current functions.
1598 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001599 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001600 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001601 tv = STACK_TV_BOT(0);
1602 tv->v_type = VAR_NUMBER;
1603 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001604 ++ectx->ec_stack.ga_len;
1605 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001607 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001608 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001609 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001610 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 goto done;
1612 }
1613
Bram Moolenaar4c137212021-04-19 16:48:48 +02001614 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001615 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 }
1617 continue;
1618 }
1619
Bram Moolenaar4c137212021-04-19 16:48:48 +02001620 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 switch (iptr->isn_type)
1622 {
1623 // execute Ex command line
1624 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001625 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001626 source_cookie_T cookie;
1627
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001628 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001629 // Pass getsourceline to get an error for a missing ":end"
1630 // command.
1631 CLEAR_FIELD(cookie);
1632 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1633 if (do_cmdline(iptr->isn_arg.string,
1634 getsourceline, &cookie,
1635 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1636 == FAIL
1637 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001638 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 break;
1641
Bram Moolenaar20677332021-06-06 17:02:53 +02001642 // execute Ex command line split at NL characters.
1643 case ISN_EXEC_SPLIT:
1644 {
1645 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001646 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001647
1648 SOURCING_LNUM = iptr->isn_lnum;
1649 CLEAR_FIELD(cookie);
1650 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1651 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001652 line = get_split_sourceline(0, &cookie, 0, 0);
1653 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001654 get_split_sourceline, &cookie,
1655 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1656 == FAIL
1657 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001658 {
1659 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001660 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001661 }
1662 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001663 }
1664 break;
1665
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001666 // Evaluate an expression with legacy syntax, push it onto the
1667 // stack.
1668 case ISN_LEGACY_EVAL:
1669 {
1670 char_u *arg = iptr->isn_arg.string;
1671 int res;
1672 int save_flags = cmdmod.cmod_flags;
1673
1674 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001675 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001676 tv = STACK_TV_BOT(0);
1677 init_tv(tv);
1678 cmdmod.cmod_flags |= CMOD_LEGACY;
1679 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1680 cmdmod.cmod_flags = save_flags;
1681 if (res == FAIL)
1682 goto on_error;
1683 ++ectx->ec_stack.ga_len;
1684 }
1685 break;
1686
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001687 // push typeval VAR_INSTR with instructions to be executed
1688 case ISN_INSTR:
1689 {
1690 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001691 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001692 tv = STACK_TV_BOT(0);
1693 tv->vval.v_instr = ALLOC_ONE(instr_T);
1694 if (tv->vval.v_instr == NULL)
1695 goto on_error;
1696 ++ectx->ec_stack.ga_len;
1697
1698 tv->v_type = VAR_INSTR;
1699 tv->vval.v_instr->instr_ectx = ectx;
1700 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1701 }
1702 break;
1703
Bram Moolenaar4c137212021-04-19 16:48:48 +02001704 // execute :substitute with an expression
1705 case ISN_SUBSTITUTE:
1706 {
1707 subs_T *subs = &iptr->isn_arg.subs;
1708 source_cookie_T cookie;
1709 struct subs_expr_S *save_instr = substitute_instr;
1710 struct subs_expr_S subs_instr;
1711 int res;
1712
1713 subs_instr.subs_ectx = ectx;
1714 subs_instr.subs_instr = subs->subs_instr;
1715 subs_instr.subs_status = OK;
1716 substitute_instr = &subs_instr;
1717
1718 SOURCING_LNUM = iptr->isn_lnum;
1719 // This is very much like ISN_EXEC
1720 CLEAR_FIELD(cookie);
1721 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1722 res = do_cmdline(subs->subs_cmd,
1723 getsourceline, &cookie,
1724 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1725 substitute_instr = save_instr;
1726
1727 if (res == FAIL || did_emsg
1728 || subs_instr.subs_status == FAIL)
1729 goto on_error;
1730 }
1731 break;
1732
1733 case ISN_FINISH:
1734 goto done;
1735
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001736 case ISN_REDIRSTART:
1737 // create a dummy entry for var_redir_str()
1738 if (alloc_redir_lval() == FAIL)
1739 goto on_error;
1740
1741 // The output is stored in growarray "redir_ga" until
1742 // redirection ends.
1743 init_redir_ga();
1744 redir_vname = 1;
1745 break;
1746
1747 case ISN_REDIREND:
1748 {
1749 char_u *res = get_clear_redir_ga();
1750
1751 // End redirection, put redirected text on the stack.
1752 clear_redir_lval();
1753 redir_vname = 0;
1754
1755 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1756 {
1757 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001758 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001759 }
1760 tv = STACK_TV_BOT(0);
1761 tv->v_type = VAR_STRING;
1762 tv->vval.v_string = res;
1763 ++ectx->ec_stack.ga_len;
1764 }
1765 break;
1766
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001767 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001768#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001769 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1770 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001771#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001772 break;
1773
1774 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001775#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001776 {
1777 exarg_T ea;
1778 int res;
1779
1780 CLEAR_FIELD(ea);
1781 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1782 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1783 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1784 --ectx->ec_stack.ga_len;
1785 tv = STACK_TV_BOT(0);
1786 res = cexpr_core(&ea, tv);
1787 clear_tv(tv);
1788 if (res == FAIL)
1789 goto on_error;
1790 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001791#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001792 break;
1793
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001794 // execute Ex command from pieces on the stack
1795 case ISN_EXECCONCAT:
1796 {
1797 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001798 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001799 int pass;
1800 int i;
1801 char_u *cmd = NULL;
1802 char_u *str;
1803
1804 for (pass = 1; pass <= 2; ++pass)
1805 {
1806 for (i = 0; i < count; ++i)
1807 {
1808 tv = STACK_TV_BOT(i - count);
1809 str = tv->vval.v_string;
1810 if (str != NULL && *str != NUL)
1811 {
1812 if (pass == 2)
1813 STRCPY(cmd + len, str);
1814 len += STRLEN(str);
1815 }
1816 if (pass == 2)
1817 clear_tv(tv);
1818 }
1819 if (pass == 1)
1820 {
1821 cmd = alloc(len + 1);
1822 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001823 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001824 len = 0;
1825 }
1826 }
1827
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001828 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001829 do_cmdline_cmd(cmd);
1830 vim_free(cmd);
1831 }
1832 break;
1833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 // execute :echo {string} ...
1835 case ISN_ECHO:
1836 {
1837 int count = iptr->isn_arg.echo.echo_count;
1838 int atstart = TRUE;
1839 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001840 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841
1842 for (idx = 0; idx < count; ++idx)
1843 {
1844 tv = STACK_TV_BOT(idx - count);
1845 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1846 &atstart, &needclr);
1847 clear_tv(tv);
1848 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001849 if (needclr)
1850 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001851 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 }
1853 break;
1854
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001855 // :execute {string} ...
1856 // :echomsg {string} ...
1857 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001858 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001859 case ISN_ECHOMSG:
1860 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001861 {
1862 int count = iptr->isn_arg.number;
1863 garray_T ga;
1864 char_u buf[NUMBUFLEN];
1865 char_u *p;
1866 int len;
1867 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001868 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001869
1870 ga_init2(&ga, 1, 80);
1871 for (idx = 0; idx < count; ++idx)
1872 {
1873 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001874 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001875 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001876 if (tv->v_type == VAR_CHANNEL
1877 || tv->v_type == VAR_JOB)
1878 {
1879 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001880 semsg(_(e_using_invalid_value_as_string_str),
1881 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001882 break;
1883 }
1884 else
1885 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001886 }
1887 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001888 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001889
1890 len = (int)STRLEN(p);
1891 if (ga_grow(&ga, len + 2) == FAIL)
1892 failed = TRUE;
1893 else
1894 {
1895 if (ga.ga_len > 0)
1896 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1897 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1898 ga.ga_len += len;
1899 }
1900 clear_tv(tv);
1901 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001902 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001903 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001904 {
1905 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001906 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001907 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001908
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001909 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001910 {
1911 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001912 {
1913 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001914 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001915 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001916 {
1917 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001918 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001919 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001920 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001921 else
1922 {
1923 msg_sb_eol();
1924 if (iptr->isn_type == ISN_ECHOMSG)
1925 {
1926 msg_attr(ga.ga_data, echo_attr);
1927 out_flush();
1928 }
1929 else
1930 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001931 SOURCING_LNUM = iptr->isn_lnum;
1932 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001933 }
1934 }
1935 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001936 ga_clear(&ga);
1937 }
1938 break;
1939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 // load local variable or argument
1941 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001942 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001943 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001945 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 break;
1947
1948 // load v: variable
1949 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001950 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001951 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001953 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954 break;
1955
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001956 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 case ISN_LOADSCRIPT:
1958 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001959 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 svar_T *sv;
1961
Bram Moolenaar4c137212021-04-19 16:48:48 +02001962 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001963 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001964 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001965 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001966 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001967 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001969 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 }
1971 break;
1972
1973 // load s: variable in old script
1974 case ISN_LOADS:
1975 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001976 hashtab_T *ht = &SCRIPT_VARS(
1977 iptr->isn_arg.loadstore.ls_sid);
1978 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 if (di == NULL)
1982 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001983 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001984 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001985 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 }
1987 else
1988 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001989 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001990 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001992 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 }
1994 }
1995 break;
1996
Bram Moolenaard3aac292020-04-19 14:32:17 +02001997 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001999 case ISN_LOADB:
2000 case ISN_LOADW:
2001 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002003 dictitem_T *di = NULL;
2004 hashtab_T *ht = NULL;
2005 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002006
Bram Moolenaard3aac292020-04-19 14:32:17 +02002007 switch (iptr->isn_type)
2008 {
2009 case ISN_LOADG:
2010 ht = get_globvar_ht();
2011 namespace = 'g';
2012 break;
2013 case ISN_LOADB:
2014 ht = &curbuf->b_vars->dv_hashtab;
2015 namespace = 'b';
2016 break;
2017 case ISN_LOADW:
2018 ht = &curwin->w_vars->dv_hashtab;
2019 namespace = 'w';
2020 break;
2021 case ISN_LOADT:
2022 ht = &curtab->tp_vars->dv_hashtab;
2023 namespace = 't';
2024 break;
2025 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002026 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002027 }
2028 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 if (di == NULL)
2031 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002032 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002033 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002034 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002035 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 }
2037 else
2038 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002039 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002040 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002042 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 }
2044 }
2045 break;
2046
Bram Moolenaar03290b82020-12-19 16:30:44 +01002047 // load autoload variable
2048 case ISN_LOADAUTO:
2049 {
2050 char_u *name = iptr->isn_arg.string;
2051
Bram Moolenaar4c137212021-04-19 16:48:48 +02002052 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002053 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002054 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002055 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002056 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002057 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002058 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002059 }
2060 break;
2061
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002062 // load g:/b:/w:/t: namespace
2063 case ISN_LOADGDICT:
2064 case ISN_LOADBDICT:
2065 case ISN_LOADWDICT:
2066 case ISN_LOADTDICT:
2067 {
2068 dict_T *d = NULL;
2069
2070 switch (iptr->isn_type)
2071 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002072 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2073 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2074 case ISN_LOADWDICT: d = curwin->w_vars; break;
2075 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002076 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002077 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002078 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002079 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002080 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002081 tv = STACK_TV_BOT(0);
2082 tv->v_type = VAR_DICT;
2083 tv->v_lock = 0;
2084 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002085 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002086 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002087 }
2088 break;
2089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 // load &option
2091 case ISN_LOADOPT:
2092 {
2093 typval_T optval;
2094 char_u *name = iptr->isn_arg.string;
2095
Bram Moolenaara8c17702020-04-01 21:17:24 +02002096 // This is not expected to fail, name is checked during
2097 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002098 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002099 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002100 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002101 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002103 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 }
2105 break;
2106
2107 // load $ENV
2108 case ISN_LOADENV:
2109 {
2110 typval_T optval;
2111 char_u *name = iptr->isn_arg.string;
2112
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 Moolenaar0bbf7222020-02-19 22:31:48 +01002115 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002116 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002118 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 }
2120 break;
2121
2122 // load @register
2123 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002124 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002125 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 tv = STACK_TV_BOT(0);
2127 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002128 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002129 // This may result in NULL, which should be equivalent to an
2130 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 tv->vval.v_string = get_reg_contents(
2132 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002133 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 break;
2135
2136 // store local variable
2137 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002138 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 tv = STACK_TV_VAR(iptr->isn_arg.number);
2140 clear_tv(tv);
2141 *tv = *STACK_TV_BOT(0);
2142 break;
2143
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002144 // store s: variable in old script
2145 case ISN_STORES:
2146 {
2147 hashtab_T *ht = &SCRIPT_VARS(
2148 iptr->isn_arg.loadstore.ls_sid);
2149 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002150 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002151
Bram Moolenaar4c137212021-04-19 16:48:48 +02002152 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002153 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002154 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002155 else
2156 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002157 SOURCING_LNUM = iptr->isn_lnum;
2158 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002159 {
2160 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002161 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002162 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002163 clear_tv(&di->di_tv);
2164 di->di_tv = *STACK_TV_BOT(0);
2165 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002166 }
2167 break;
2168
2169 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 case ISN_STORESCRIPT:
2171 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002172 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2173 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174
Bram Moolenaar4c137212021-04-19 16:48:48 +02002175 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002176 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002177 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002178 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002179
2180 // "const" and "final" are checked at compile time, locking
2181 // the value needs to be checked here.
2182 SOURCING_LNUM = iptr->isn_lnum;
2183 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002184 {
2185 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002186 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002187 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189 clear_tv(sv->sv_tv);
2190 *sv->sv_tv = *STACK_TV_BOT(0);
2191 }
2192 break;
2193
2194 // store option
2195 case ISN_STOREOPT:
2196 {
2197 long n = 0;
2198 char_u *s = NULL;
2199 char *msg;
2200
Bram Moolenaar4c137212021-04-19 16:48:48 +02002201 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 tv = STACK_TV_BOT(0);
2203 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002204 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002206 if (s == NULL)
2207 s = (char_u *)"";
2208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002210 // must be VAR_NUMBER, CHECKTYPE makes sure
2211 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2213 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002214 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 if (msg != NULL)
2216 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002217 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002219 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 }
2222 break;
2223
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002224 // store $ENV
2225 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002226 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002227 tv = STACK_TV_BOT(0);
2228 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2229 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002230 break;
2231
2232 // store @r
2233 case ISN_STOREREG:
2234 {
2235 int reg = iptr->isn_arg.number;
2236
Bram Moolenaar4c137212021-04-19 16:48:48 +02002237 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002238 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002239 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002240 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002241 }
2242 break;
2243
2244 // store v: variable
2245 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002246 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002247 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2248 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002249 // should not happen, type is checked when compiling
2250 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002251 break;
2252
Bram Moolenaard3aac292020-04-19 14:32:17 +02002253 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002255 case ISN_STOREB:
2256 case ISN_STOREW:
2257 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002259 dictitem_T *di;
2260 hashtab_T *ht;
2261 char_u *name = iptr->isn_arg.string + 2;
2262
Bram Moolenaard3aac292020-04-19 14:32:17 +02002263 switch (iptr->isn_type)
2264 {
2265 case ISN_STOREG:
2266 ht = get_globvar_ht();
2267 break;
2268 case ISN_STOREB:
2269 ht = &curbuf->b_vars->dv_hashtab;
2270 break;
2271 case ISN_STOREW:
2272 ht = &curwin->w_vars->dv_hashtab;
2273 break;
2274 case ISN_STORET:
2275 ht = &curtab->tp_vars->dv_hashtab;
2276 break;
2277 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002278 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002279 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280
Bram Moolenaar4c137212021-04-19 16:48:48 +02002281 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002282 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002284 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 else
2286 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002287 SOURCING_LNUM = iptr->isn_lnum;
2288 if (var_check_permission(di, name) == FAIL)
2289 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 clear_tv(&di->di_tv);
2291 di->di_tv = *STACK_TV_BOT(0);
2292 }
2293 }
2294 break;
2295
Bram Moolenaar03290b82020-12-19 16:30:44 +01002296 // store an autoload variable
2297 case ISN_STOREAUTO:
2298 SOURCING_LNUM = iptr->isn_lnum;
2299 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2300 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002301 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002302 break;
2303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 // store number in local variable
2305 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002306 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 clear_tv(tv);
2308 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002309 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 break;
2311
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002312 // store value in list or dict variable
2313 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002314 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002315 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002316 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002317 typval_T *tv_dest = STACK_TV_BOT(-1);
2318 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002319
Bram Moolenaar752fc692021-01-04 21:57:11 +01002320 // Stack contains:
2321 // -3 value to be stored
2322 // -2 index
2323 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002324 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002325 SOURCING_LNUM = iptr->isn_lnum;
2326 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002327 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002328 dest_type = tv_dest->v_type;
2329 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002330 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002331 else if (dest_type == VAR_LIST
2332 && tv_idx->v_type != VAR_NUMBER)
2333 {
2334 emsg(_(e_number_exp));
2335 status = FAIL;
2336 }
2337 }
2338 else if (dest_type != tv_dest->v_type)
2339 {
2340 // just in case, should be OK
2341 semsg(_(e_expected_str_but_got_str),
2342 vartype_name(dest_type),
2343 vartype_name(tv_dest->v_type));
2344 status = FAIL;
2345 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002346
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002347 if (status == OK && dest_type == VAR_LIST)
2348 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002349 long lidx = (long)tv_idx->vval.v_number;
2350 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002351
2352 if (list == NULL)
2353 {
2354 emsg(_(e_list_not_set));
2355 goto on_error;
2356 }
2357 if (lidx < 0 && list->lv_len + lidx >= 0)
2358 // negative index is relative to the end
2359 lidx = list->lv_len + lidx;
2360 if (lidx < 0 || lidx > list->lv_len)
2361 {
2362 semsg(_(e_listidx), lidx);
2363 goto on_error;
2364 }
2365 if (lidx < list->lv_len)
2366 {
2367 listitem_T *li = list_find(list, lidx);
2368
2369 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002370 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002371 goto on_error;
2372 // overwrite existing list item
2373 clear_tv(&li->li_tv);
2374 li->li_tv = *tv;
2375 }
2376 else
2377 {
2378 if (error_if_locked(list->lv_lock,
2379 e_cannot_change_list))
2380 goto on_error;
2381 // append to list, only fails when out of memory
2382 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002383 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002384 clear_tv(tv);
2385 }
2386 }
2387 else if (status == OK && dest_type == VAR_DICT)
2388 {
2389 char_u *key = tv_idx->vval.v_string;
2390 dict_T *dict = tv_dest->vval.v_dict;
2391 dictitem_T *di;
2392
2393 SOURCING_LNUM = iptr->isn_lnum;
2394 if (dict == NULL)
2395 {
2396 emsg(_(e_dictionary_not_set));
2397 goto on_error;
2398 }
2399 if (key == NULL)
2400 key = (char_u *)"";
2401 di = dict_find(dict, key, -1);
2402 if (di != NULL)
2403 {
2404 if (error_if_locked(di->di_tv.v_lock,
2405 e_cannot_change_dict_item))
2406 goto on_error;
2407 // overwrite existing value
2408 clear_tv(&di->di_tv);
2409 di->di_tv = *tv;
2410 }
2411 else
2412 {
2413 if (error_if_locked(dict->dv_lock,
2414 e_cannot_change_dict))
2415 goto on_error;
2416 // add to dict, only fails when out of memory
2417 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002418 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002419 clear_tv(tv);
2420 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002421 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002422 else if (status == OK && dest_type == VAR_BLOB)
2423 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002424 long lidx = (long)tv_idx->vval.v_number;
2425 blob_T *blob = tv_dest->vval.v_blob;
2426 varnumber_T nr;
2427 int error = FALSE;
2428 int len;
2429
2430 if (blob == NULL)
2431 {
2432 emsg(_(e_blob_not_set));
2433 goto on_error;
2434 }
2435 len = blob_len(blob);
2436 if (lidx < 0 && len + lidx >= 0)
2437 // negative index is relative to the end
2438 lidx = len + lidx;
2439
2440 // Can add one byte at the end.
2441 if (lidx < 0 || lidx > len)
2442 {
2443 semsg(_(e_blobidx), lidx);
2444 goto on_error;
2445 }
2446 if (value_check_lock(blob->bv_lock,
2447 (char_u *)"blob", FALSE))
2448 goto on_error;
2449 nr = tv_get_number_chk(tv, &error);
2450 if (error)
2451 goto on_error;
2452 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002453 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002454 else
2455 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002456 status = FAIL;
2457 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002458 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002459
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002460 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002461 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002462 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002463 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002464 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002465 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002466 goto on_error;
2467 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002468 }
2469 break;
2470
Bram Moolenaar68452172021-04-12 21:21:02 +02002471 // store value in blob range
2472 case ISN_STORERANGE:
2473 {
2474 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2475 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2476 typval_T *tv_dest = STACK_TV_BOT(-1);
2477 int status = OK;
2478
2479 // Stack contains:
2480 // -4 value to be stored
2481 // -3 first index or "none"
2482 // -2 second index or "none"
2483 // -1 destination blob
2484 tv = STACK_TV_BOT(-4);
2485 if (tv_dest->v_type != VAR_BLOB)
2486 {
2487 status = FAIL;
2488 emsg(_(e_blob_required));
2489 }
2490 else
2491 {
2492 varnumber_T n1;
2493 varnumber_T n2;
2494 int error = FALSE;
2495
2496 n1 = tv_get_number_chk(tv_idx1, &error);
2497 if (error)
2498 status = FAIL;
2499 else
2500 {
2501 if (tv_idx2->v_type == VAR_SPECIAL
2502 && tv_idx2->vval.v_number == VVAL_NONE)
2503 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2504 else
2505 n2 = tv_get_number_chk(tv_idx2, &error);
2506 if (error)
2507 status = FAIL;
2508 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002509 {
2510 long bloblen = blob_len(tv_dest->vval.v_blob);
2511
2512 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002513 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002514 || check_blob_range(bloblen,
2515 n1, n2, FALSE) == FAIL)
2516 status = FAIL;
2517 else
2518 status = blob_set_range(
2519 tv_dest->vval.v_blob, n1, n2, tv);
2520 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002521 }
2522 }
2523
2524 clear_tv(tv_idx1);
2525 clear_tv(tv_idx2);
2526 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002527 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002528 clear_tv(tv);
2529
2530 if (status == FAIL)
2531 goto on_error;
2532 }
2533 break;
2534
Bram Moolenaar0186e582021-01-10 18:33:11 +01002535 // load or store variable or argument from outer scope
2536 case ISN_LOADOUTER:
2537 case ISN_STOREOUTER:
2538 {
2539 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002540 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2541 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002542
2543 while (depth > 1 && outer != NULL)
2544 {
2545 outer = outer->out_up;
2546 --depth;
2547 }
2548 if (outer == NULL)
2549 {
2550 SOURCING_LNUM = iptr->isn_lnum;
2551 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002552 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002553 }
2554 tv = ((typval_T *)outer->out_stack->ga_data)
2555 + outer->out_frame_idx + STACK_FRAME_SIZE
2556 + iptr->isn_arg.outer.outer_idx;
2557 if (iptr->isn_type == ISN_LOADOUTER)
2558 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002559 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002560 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002561 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002562 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002563 }
2564 else
2565 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002566 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002567 clear_tv(tv);
2568 *tv = *STACK_TV_BOT(0);
2569 }
2570 }
2571 break;
2572
Bram Moolenaar752fc692021-01-04 21:57:11 +01002573 // unlet item in list or dict variable
2574 case ISN_UNLETINDEX:
2575 {
2576 typval_T *tv_idx = STACK_TV_BOT(-2);
2577 typval_T *tv_dest = STACK_TV_BOT(-1);
2578 int status = OK;
2579
2580 // Stack contains:
2581 // -2 index
2582 // -1 dict or list
2583 if (tv_dest->v_type == VAR_DICT)
2584 {
2585 // unlet a dict item, index must be a string
2586 if (tv_idx->v_type != VAR_STRING)
2587 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002588 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002589 semsg(_(e_expected_str_but_got_str),
2590 vartype_name(VAR_STRING),
2591 vartype_name(tv_idx->v_type));
2592 status = FAIL;
2593 }
2594 else
2595 {
2596 dict_T *d = tv_dest->vval.v_dict;
2597 char_u *key = tv_idx->vval.v_string;
2598 dictitem_T *di = NULL;
2599
2600 if (key == NULL)
2601 key = (char_u *)"";
2602 if (d != NULL)
2603 di = dict_find(d, key, (int)STRLEN(key));
2604 if (di == NULL)
2605 {
2606 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002607 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002608 semsg(_(e_dictkey), key);
2609 status = FAIL;
2610 }
2611 else
2612 {
2613 // TODO: check for dict or item locked
2614 dictitem_remove(d, di);
2615 }
2616 }
2617 }
2618 else if (tv_dest->v_type == VAR_LIST)
2619 {
2620 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002621 SOURCING_LNUM = iptr->isn_lnum;
2622 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002623 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002624 status = FAIL;
2625 }
2626 else
2627 {
2628 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002629 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002630 listitem_T *li = NULL;
2631
2632 li = list_find(l, n);
2633 if (li == NULL)
2634 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002635 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002636 semsg(_(e_listidx), n);
2637 status = FAIL;
2638 }
2639 else
2640 // TODO: check for list or item locked
2641 listitem_remove(l, li);
2642 }
2643 }
2644 else
2645 {
2646 status = FAIL;
2647 semsg(_(e_cannot_index_str),
2648 vartype_name(tv_dest->v_type));
2649 }
2650
2651 clear_tv(tv_idx);
2652 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002653 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002654 if (status == FAIL)
2655 goto on_error;
2656 }
2657 break;
2658
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002659 // unlet range of items in list variable
2660 case ISN_UNLETRANGE:
2661 {
2662 // Stack contains:
2663 // -3 index1
2664 // -2 index2
2665 // -1 dict or list
2666 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2667 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2668 typval_T *tv_dest = STACK_TV_BOT(-1);
2669 int status = OK;
2670
2671 if (tv_dest->v_type == VAR_LIST)
2672 {
2673 // indexes must be a number
2674 SOURCING_LNUM = iptr->isn_lnum;
2675 if (check_for_number(tv_idx1) == FAIL
2676 || check_for_number(tv_idx2) == FAIL)
2677 {
2678 status = FAIL;
2679 }
2680 else
2681 {
2682 list_T *l = tv_dest->vval.v_list;
2683 long n1 = (long)tv_idx1->vval.v_number;
2684 long n2 = (long)tv_idx2->vval.v_number;
2685 listitem_T *li;
2686
2687 li = list_find_index(l, &n1);
2688 if (li == NULL
2689 || list_unlet_range(l, li, NULL, n1,
2690 TRUE, n2) == FAIL)
2691 status = FAIL;
2692 }
2693 }
2694 else
2695 {
2696 status = FAIL;
2697 SOURCING_LNUM = iptr->isn_lnum;
2698 semsg(_(e_cannot_index_str),
2699 vartype_name(tv_dest->v_type));
2700 }
2701
2702 clear_tv(tv_idx1);
2703 clear_tv(tv_idx2);
2704 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002705 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002706 if (status == FAIL)
2707 goto on_error;
2708 }
2709 break;
2710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 // push constant
2712 case ISN_PUSHNR:
2713 case ISN_PUSHBOOL:
2714 case ISN_PUSHSPEC:
2715 case ISN_PUSHF:
2716 case ISN_PUSHS:
2717 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002718 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002719 case ISN_PUSHCHANNEL:
2720 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002721 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002722 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002724 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002725 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 switch (iptr->isn_type)
2727 {
2728 case ISN_PUSHNR:
2729 tv->v_type = VAR_NUMBER;
2730 tv->vval.v_number = iptr->isn_arg.number;
2731 break;
2732 case ISN_PUSHBOOL:
2733 tv->v_type = VAR_BOOL;
2734 tv->vval.v_number = iptr->isn_arg.number;
2735 break;
2736 case ISN_PUSHSPEC:
2737 tv->v_type = VAR_SPECIAL;
2738 tv->vval.v_number = iptr->isn_arg.number;
2739 break;
2740#ifdef FEAT_FLOAT
2741 case ISN_PUSHF:
2742 tv->v_type = VAR_FLOAT;
2743 tv->vval.v_float = iptr->isn_arg.fnumber;
2744 break;
2745#endif
2746 case ISN_PUSHBLOB:
2747 blob_copy(iptr->isn_arg.blob, tv);
2748 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002749 case ISN_PUSHFUNC:
2750 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002751 if (iptr->isn_arg.string == NULL)
2752 tv->vval.v_string = NULL;
2753 else
2754 tv->vval.v_string =
2755 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002756 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002757 case ISN_PUSHCHANNEL:
2758#ifdef FEAT_JOB_CHANNEL
2759 tv->v_type = VAR_CHANNEL;
2760 tv->vval.v_channel = iptr->isn_arg.channel;
2761 if (tv->vval.v_channel != NULL)
2762 ++tv->vval.v_channel->ch_refcount;
2763#endif
2764 break;
2765 case ISN_PUSHJOB:
2766#ifdef FEAT_JOB_CHANNEL
2767 tv->v_type = VAR_JOB;
2768 tv->vval.v_job = iptr->isn_arg.job;
2769 if (tv->vval.v_job != NULL)
2770 ++tv->vval.v_job->jv_refcount;
2771#endif
2772 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 default:
2774 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002775 tv->vval.v_string = vim_strsave(
2776 iptr->isn_arg.string == NULL
2777 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 }
2779 break;
2780
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002781 case ISN_UNLET:
2782 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2783 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002784 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002785 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002786 case ISN_UNLETENV:
2787 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2788 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002789
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002790 case ISN_LOCKCONST:
2791 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2792 break;
2793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 // create a list from items on the stack; uses a single allocation
2795 // for the list header and the items
2796 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002797 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002798 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 break;
2800
2801 // create a dict from items on the stack
2802 case ISN_NEWDICT:
2803 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002804 int count = iptr->isn_arg.number;
2805 dict_T *dict = dict_alloc();
2806 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002807 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002808 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809
2810 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002811 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 for (idx = 0; idx < count; ++idx)
2813 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002814 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002816 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002817 key = tv->vval.v_string == NULL
2818 ? (char_u *)"" : tv->vval.v_string;
2819 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002820 if (item != NULL)
2821 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002822 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002823 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002824 dict_unref(dict);
2825 goto on_error;
2826 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002827 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 clear_tv(tv);
2829 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002830 {
2831 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002832 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2835 item->di_tv.v_lock = 0;
2836 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002837 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002838 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002839 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002840 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002841 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 }
2843
2844 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002845 ectx->ec_stack.ga_len -= 2 * count - 1;
2846 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002847 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002849 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 tv = STACK_TV_BOT(-1);
2851 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002852 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 tv->vval.v_dict = dict;
2854 ++dict->dv_refcount;
2855 }
2856 break;
2857
2858 // call a :def function
2859 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002860 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002861 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2862 NULL,
2863 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002864 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002865 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 break;
2867
2868 // call a builtin function
2869 case ISN_BCALL:
2870 SOURCING_LNUM = iptr->isn_lnum;
2871 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2872 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002873 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002874 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 break;
2876
2877 // call a funcref or partial
2878 case ISN_PCALL:
2879 {
2880 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2881 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002882 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883
2884 SOURCING_LNUM = iptr->isn_lnum;
2885 if (pfunc->cpf_top)
2886 {
2887 // funcref is above the arguments
2888 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2889 }
2890 else
2891 {
2892 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002893 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002894 partial_tv = *STACK_TV_BOT(0);
2895 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002897 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002898 if (tv == &partial_tv)
2899 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002901 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 }
2903 break;
2904
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002905 case ISN_PCALL_END:
2906 // PCALL finished, arguments have been consumed and replaced by
2907 // the return value. Now clear the funcref from the stack,
2908 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002909 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002910 clear_tv(STACK_TV_BOT(-1));
2911 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2912 break;
2913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 // call a user defined function or funcref/partial
2915 case ISN_UCALL:
2916 {
2917 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2918
2919 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002920 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002921 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002922 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 }
2924 break;
2925
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002926 // return from a :def function call without a value
2927 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002928 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002929 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002930 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002931 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002932 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002933 tv->vval.v_number = 0;
2934 tv->v_lock = 0;
2935 // FALLTHROUGH
2936
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002937 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 case ISN_RETURN:
2939 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002940 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002941 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002942
2943 if (trystack->ga_len > 0)
2944 trycmd = ((trycmd_T *)trystack->ga_data)
2945 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002946 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002947 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002949 // jump to ":finally" or ":endtry"
2950 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002951 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002952 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002953 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 trycmd->tcd_return = TRUE;
2955 }
2956 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002957 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 }
2959 break;
2960
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002961 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 case ISN_FUNCREF:
2963 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002964 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2965 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2966 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002969 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002970 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002971 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002972 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002973 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002974 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002975 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002976 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002977 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002979 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 tv->vval.v_partial = pt;
2981 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002982 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 }
2984 break;
2985
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002986 // Create a global function from a lambda.
2987 case ISN_NEWFUNC:
2988 {
2989 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2990
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002991 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002992 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002993 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002994 }
2995 break;
2996
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002997 // List functions
2998 case ISN_DEF:
2999 if (iptr->isn_arg.string == NULL)
3000 list_functions(NULL);
3001 else
3002 {
3003 exarg_T ea;
3004
3005 CLEAR_FIELD(ea);
3006 ea.cmd = ea.arg = iptr->isn_arg.string;
3007 define_function(&ea, NULL);
3008 }
3009 break;
3010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 // jump if a condition is met
3012 case ISN_JUMP:
3013 {
3014 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003015 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 int jump = TRUE;
3017
3018 if (when != JUMP_ALWAYS)
3019 {
3020 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003021 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003022 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003023 || when == JUMP_IF_COND_TRUE)
3024 {
3025 SOURCING_LNUM = iptr->isn_lnum;
3026 jump = tv_get_bool_chk(tv, &error);
3027 if (error)
3028 goto on_error;
3029 }
3030 else
3031 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003033 || when == JUMP_AND_KEEP_IF_FALSE
3034 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003036 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 {
3038 // drop the value from the stack
3039 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003040 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 }
3042 }
3043 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003044 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 }
3046 break;
3047
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003048 // Jump if an argument with a default value was already set and not
3049 // v:none.
3050 case ISN_JUMP_IF_ARG_SET:
3051 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3052 if (tv->v_type != VAR_UNKNOWN
3053 && !(tv->v_type == VAR_SPECIAL
3054 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003055 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003056 break;
3057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 // top of a for loop
3059 case ISN_FOR:
3060 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003061 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 typval_T *idxtv =
3063 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3064
Bram Moolenaar4c137212021-04-19 16:48:48 +02003065 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003066 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003067 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003068 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003069 list_T *list = ltv->vval.v_list;
3070
3071 // push the next item from the list
3072 ++idxtv->vval.v_number;
3073 if (list == NULL
3074 || idxtv->vval.v_number >= list->lv_len)
3075 {
3076 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003077 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3078 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003079 }
3080 else if (list->lv_first == &range_list_item)
3081 {
3082 // non-materialized range() list
3083 tv = STACK_TV_BOT(0);
3084 tv->v_type = VAR_NUMBER;
3085 tv->v_lock = 0;
3086 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003088 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003089 }
3090 else
3091 {
3092 listitem_T *li = list_find(list,
3093 idxtv->vval.v_number);
3094
3095 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003096 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003097 }
3098 }
3099 else if (ltv->v_type == VAR_STRING)
3100 {
3101 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003102
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003103 // The index is for the last byte of the previous
3104 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003105 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003106 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003107 {
3108 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003109 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3110 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003111 }
3112 else
3113 {
3114 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3115
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003116 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003117 tv = STACK_TV_BOT(0);
3118 tv->v_type = VAR_STRING;
3119 tv->vval.v_string = vim_strnsave(
3120 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003121 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003122 idxtv->vval.v_number += clen - 1;
3123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003125 else if (ltv->v_type == VAR_BLOB)
3126 {
3127 blob_T *blob = ltv->vval.v_blob;
3128
3129 // When we get here the first time make a copy of the
3130 // blob, so that the iteration still works when it is
3131 // changed.
3132 if (idxtv->vval.v_number == -1 && blob != NULL)
3133 {
3134 blob_copy(blob, ltv);
3135 blob_unref(blob);
3136 blob = ltv->vval.v_blob;
3137 }
3138
3139 // The index is for the previous byte.
3140 ++idxtv->vval.v_number;
3141 if (blob == NULL
3142 || idxtv->vval.v_number >= blob_len(blob))
3143 {
3144 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003145 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3146 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003147 }
3148 else
3149 {
3150 // Push the next byte from the blob.
3151 tv = STACK_TV_BOT(0);
3152 tv->v_type = VAR_NUMBER;
3153 tv->vval.v_number = blob_get(blob,
3154 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003155 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003156 }
3157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 else
3159 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003160 semsg(_(e_for_loop_on_str_not_supported),
3161 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003162 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 }
3164 }
3165 break;
3166
3167 // start of ":try" block
3168 case ISN_TRY:
3169 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003170 trycmd_T *trycmd = NULL;
3171
Bram Moolenaar4c137212021-04-19 16:48:48 +02003172 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003173 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003174 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3175 + ectx->ec_trystack.ga_len;
3176 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003178 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003179 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3180 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003181 trycmd->tcd_catch_idx =
3182 iptr->isn_arg.try.try_ref->try_catch;
3183 trycmd->tcd_finally_idx =
3184 iptr->isn_arg.try.try_ref->try_finally;
3185 trycmd->tcd_endtry_idx =
3186 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 }
3188 break;
3189
3190 case ISN_PUSHEXC:
3191 if (current_exception == NULL)
3192 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003193 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003195 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003197 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003198 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003200 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003202 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 tv->vval.v_string = vim_strsave(
3204 (char_u *)current_exception->value);
3205 break;
3206
3207 case ISN_CATCH:
3208 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003209 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210
Bram Moolenaar4c137212021-04-19 16:48:48 +02003211 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 if (trystack->ga_len > 0)
3213 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003214 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 + trystack->ga_len - 1;
3216 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003217 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 }
3219 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003220 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 catch_exception(current_exception);
3222 }
3223 break;
3224
Bram Moolenaarc150c092021-02-13 15:02:46 +01003225 case ISN_TRYCONT:
3226 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003227 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003228 trycont_T *trycont = &iptr->isn_arg.trycont;
3229 int i;
3230 trycmd_T *trycmd;
3231 int iidx = trycont->tct_where;
3232
3233 if (trystack->ga_len < trycont->tct_levels)
3234 {
3235 siemsg("TRYCONT: expected %d levels, found %d",
3236 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003237 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003238 }
3239 // Make :endtry jump to any outer try block and the last
3240 // :endtry inside the loop to the loop start.
3241 for (i = trycont->tct_levels; i > 0; --i)
3242 {
3243 trycmd = ((trycmd_T *)trystack->ga_data)
3244 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003245 // Add one to tcd_cont to be able to jump to
3246 // instruction with index zero.
3247 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003248 iidx = trycmd->tcd_finally_idx == 0
3249 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003250 }
3251 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003252 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003253 }
3254 break;
3255
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003256 case ISN_FINALLY:
3257 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003258 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003259 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3260 + trystack->ga_len - 1;
3261
3262 // Reset the index to avoid a return statement jumps here
3263 // again.
3264 trycmd->tcd_finally_idx = 0;
3265 break;
3266 }
3267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 // end of ":try" block
3269 case ISN_ENDTRY:
3270 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003271 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272
3273 if (trystack->ga_len > 0)
3274 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003275 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 --trystack->ga_len;
3278 --trylevel;
3279 trycmd = ((trycmd_T *)trystack->ga_data)
3280 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003281 if (trycmd->tcd_did_throw)
3282 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003283 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 {
3285 // discard the exception
3286 if (caught_stack == current_exception)
3287 caught_stack = caught_stack->caught;
3288 discard_current_exception();
3289 }
3290
3291 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003292 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003293
Bram Moolenaar4c137212021-04-19 16:48:48 +02003294 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003295 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003296 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003297 clear_tv(STACK_TV_BOT(0));
3298 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003299 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003300 // handling :continue: jump to outer try block or
3301 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003302 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 }
3304 }
3305 break;
3306
3307 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003308 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003309 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003310
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003311 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3312 {
3313 // throwing an exception while using "silent!" causes
3314 // the function to abort but not display an error.
3315 tv = STACK_TV_BOT(-1);
3316 clear_tv(tv);
3317 tv->v_type = VAR_NUMBER;
3318 tv->vval.v_number = 0;
3319 goto done;
3320 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003321 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003322 tv = STACK_TV_BOT(0);
3323 if (tv->vval.v_string == NULL
3324 || *skipwhite(tv->vval.v_string) == NUL)
3325 {
3326 vim_free(tv->vval.v_string);
3327 SOURCING_LNUM = iptr->isn_lnum;
3328 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003329 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003330 }
3331
3332 // Inside a "catch" we need to first discard the caught
3333 // exception.
3334 if (trystack->ga_len > 0)
3335 {
3336 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3337 + trystack->ga_len - 1;
3338 if (trycmd->tcd_caught && current_exception != NULL)
3339 {
3340 // discard the exception
3341 if (caught_stack == current_exception)
3342 caught_stack = caught_stack->caught;
3343 discard_current_exception();
3344 trycmd->tcd_caught = FALSE;
3345 }
3346 }
3347
3348 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3349 == FAIL)
3350 {
3351 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003352 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003353 }
3354 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 break;
3357
3358 // compare with special values
3359 case ISN_COMPAREBOOL:
3360 case ISN_COMPARESPECIAL:
3361 {
3362 typval_T *tv1 = STACK_TV_BOT(-2);
3363 typval_T *tv2 = STACK_TV_BOT(-1);
3364 varnumber_T arg1 = tv1->vval.v_number;
3365 varnumber_T arg2 = tv2->vval.v_number;
3366 int res;
3367
3368 switch (iptr->isn_arg.op.op_type)
3369 {
3370 case EXPR_EQUAL: res = arg1 == arg2; break;
3371 case EXPR_NEQUAL: res = arg1 != arg2; break;
3372 default: res = 0; break;
3373 }
3374
Bram Moolenaar4c137212021-04-19 16:48:48 +02003375 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 tv1->v_type = VAR_BOOL;
3377 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3378 }
3379 break;
3380
3381 // Operation with two number arguments
3382 case ISN_OPNR:
3383 case ISN_COMPARENR:
3384 {
3385 typval_T *tv1 = STACK_TV_BOT(-2);
3386 typval_T *tv2 = STACK_TV_BOT(-1);
3387 varnumber_T arg1 = tv1->vval.v_number;
3388 varnumber_T arg2 = tv2->vval.v_number;
3389 varnumber_T res;
3390
3391 switch (iptr->isn_arg.op.op_type)
3392 {
3393 case EXPR_MULT: res = arg1 * arg2; break;
3394 case EXPR_DIV: res = arg1 / arg2; break;
3395 case EXPR_REM: res = arg1 % arg2; break;
3396 case EXPR_SUB: res = arg1 - arg2; break;
3397 case EXPR_ADD: res = arg1 + arg2; break;
3398
3399 case EXPR_EQUAL: res = arg1 == arg2; break;
3400 case EXPR_NEQUAL: res = arg1 != arg2; break;
3401 case EXPR_GREATER: res = arg1 > arg2; break;
3402 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3403 case EXPR_SMALLER: res = arg1 < arg2; break;
3404 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3405 default: res = 0; break;
3406 }
3407
Bram Moolenaar4c137212021-04-19 16:48:48 +02003408 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 if (iptr->isn_type == ISN_COMPARENR)
3410 {
3411 tv1->v_type = VAR_BOOL;
3412 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3413 }
3414 else
3415 tv1->vval.v_number = res;
3416 }
3417 break;
3418
3419 // Computation with two float arguments
3420 case ISN_OPFLOAT:
3421 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003422#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 {
3424 typval_T *tv1 = STACK_TV_BOT(-2);
3425 typval_T *tv2 = STACK_TV_BOT(-1);
3426 float_T arg1 = tv1->vval.v_float;
3427 float_T arg2 = tv2->vval.v_float;
3428 float_T res = 0;
3429 int cmp = FALSE;
3430
3431 switch (iptr->isn_arg.op.op_type)
3432 {
3433 case EXPR_MULT: res = arg1 * arg2; break;
3434 case EXPR_DIV: res = arg1 / arg2; break;
3435 case EXPR_SUB: res = arg1 - arg2; break;
3436 case EXPR_ADD: res = arg1 + arg2; break;
3437
3438 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3439 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3440 case EXPR_GREATER: cmp = arg1 > arg2; break;
3441 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3442 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3443 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3444 default: cmp = 0; break;
3445 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003446 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 if (iptr->isn_type == ISN_COMPAREFLOAT)
3448 {
3449 tv1->v_type = VAR_BOOL;
3450 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3451 }
3452 else
3453 tv1->vval.v_float = res;
3454 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003455#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 break;
3457
3458 case ISN_COMPARELIST:
3459 {
3460 typval_T *tv1 = STACK_TV_BOT(-2);
3461 typval_T *tv2 = STACK_TV_BOT(-1);
3462 list_T *arg1 = tv1->vval.v_list;
3463 list_T *arg2 = tv2->vval.v_list;
3464 int cmp = FALSE;
3465 int ic = iptr->isn_arg.op.op_ic;
3466
3467 switch (iptr->isn_arg.op.op_type)
3468 {
3469 case EXPR_EQUAL: cmp =
3470 list_equal(arg1, arg2, ic, FALSE); break;
3471 case EXPR_NEQUAL: cmp =
3472 !list_equal(arg1, arg2, ic, FALSE); break;
3473 case EXPR_IS: cmp = arg1 == arg2; break;
3474 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3475 default: cmp = 0; break;
3476 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003477 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 clear_tv(tv1);
3479 clear_tv(tv2);
3480 tv1->v_type = VAR_BOOL;
3481 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3482 }
3483 break;
3484
3485 case ISN_COMPAREBLOB:
3486 {
3487 typval_T *tv1 = STACK_TV_BOT(-2);
3488 typval_T *tv2 = STACK_TV_BOT(-1);
3489 blob_T *arg1 = tv1->vval.v_blob;
3490 blob_T *arg2 = tv2->vval.v_blob;
3491 int cmp = FALSE;
3492
3493 switch (iptr->isn_arg.op.op_type)
3494 {
3495 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3496 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3497 case EXPR_IS: cmp = arg1 == arg2; break;
3498 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3499 default: cmp = 0; break;
3500 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003501 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 clear_tv(tv1);
3503 clear_tv(tv2);
3504 tv1->v_type = VAR_BOOL;
3505 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3506 }
3507 break;
3508
3509 // TODO: handle separately
3510 case ISN_COMPARESTRING:
3511 case ISN_COMPAREDICT:
3512 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 case ISN_COMPAREANY:
3514 {
3515 typval_T *tv1 = STACK_TV_BOT(-2);
3516 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003517 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 int ic = iptr->isn_arg.op.op_ic;
3519
Bram Moolenaareb26f432020-09-14 16:50:05 +02003520 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003521 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003523 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 }
3525 break;
3526
3527 case ISN_ADDLIST:
3528 case ISN_ADDBLOB:
3529 {
3530 typval_T *tv1 = STACK_TV_BOT(-2);
3531 typval_T *tv2 = STACK_TV_BOT(-1);
3532
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003533 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 if (iptr->isn_type == ISN_ADDLIST)
3535 eval_addlist(tv1, tv2);
3536 else
3537 eval_addblob(tv1, tv2);
3538 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003539 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 }
3541 break;
3542
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003543 case ISN_LISTAPPEND:
3544 {
3545 typval_T *tv1 = STACK_TV_BOT(-2);
3546 typval_T *tv2 = STACK_TV_BOT(-1);
3547 list_T *l = tv1->vval.v_list;
3548
3549 // add an item to a list
3550 if (l == NULL)
3551 {
3552 SOURCING_LNUM = iptr->isn_lnum;
3553 emsg(_(e_cannot_add_to_null_list));
3554 goto on_error;
3555 }
3556 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003557 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003558 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003559 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003560 }
3561 break;
3562
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003563 case ISN_BLOBAPPEND:
3564 {
3565 typval_T *tv1 = STACK_TV_BOT(-2);
3566 typval_T *tv2 = STACK_TV_BOT(-1);
3567 blob_T *b = tv1->vval.v_blob;
3568 int error = FALSE;
3569 varnumber_T n;
3570
3571 // add a number to a blob
3572 if (b == NULL)
3573 {
3574 SOURCING_LNUM = iptr->isn_lnum;
3575 emsg(_(e_cannot_add_to_null_blob));
3576 goto on_error;
3577 }
3578 n = tv_get_number_chk(tv2, &error);
3579 if (error)
3580 goto on_error;
3581 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003582 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003583 }
3584 break;
3585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 // Computation with two arguments of unknown type
3587 case ISN_OPANY:
3588 {
3589 typval_T *tv1 = STACK_TV_BOT(-2);
3590 typval_T *tv2 = STACK_TV_BOT(-1);
3591 varnumber_T n1, n2;
3592#ifdef FEAT_FLOAT
3593 float_T f1 = 0, f2 = 0;
3594#endif
3595 int error = FALSE;
3596
3597 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3598 {
3599 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3600 {
3601 eval_addlist(tv1, tv2);
3602 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003603 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 break;
3605 }
3606 else if (tv1->v_type == VAR_BLOB
3607 && tv2->v_type == VAR_BLOB)
3608 {
3609 eval_addblob(tv1, tv2);
3610 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003611 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 break;
3613 }
3614 }
3615#ifdef FEAT_FLOAT
3616 if (tv1->v_type == VAR_FLOAT)
3617 {
3618 f1 = tv1->vval.v_float;
3619 n1 = 0;
3620 }
3621 else
3622#endif
3623 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003624 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 n1 = tv_get_number_chk(tv1, &error);
3626 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003627 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628#ifdef FEAT_FLOAT
3629 if (tv2->v_type == VAR_FLOAT)
3630 f1 = n1;
3631#endif
3632 }
3633#ifdef FEAT_FLOAT
3634 if (tv2->v_type == VAR_FLOAT)
3635 {
3636 f2 = tv2->vval.v_float;
3637 n2 = 0;
3638 }
3639 else
3640#endif
3641 {
3642 n2 = tv_get_number_chk(tv2, &error);
3643 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003644 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645#ifdef FEAT_FLOAT
3646 if (tv1->v_type == VAR_FLOAT)
3647 f2 = n2;
3648#endif
3649 }
3650#ifdef FEAT_FLOAT
3651 // if there is a float on either side the result is a float
3652 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3653 {
3654 switch (iptr->isn_arg.op.op_type)
3655 {
3656 case EXPR_MULT: f1 = f1 * f2; break;
3657 case EXPR_DIV: f1 = f1 / f2; break;
3658 case EXPR_SUB: f1 = f1 - f2; break;
3659 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003660 default: SOURCING_LNUM = iptr->isn_lnum;
3661 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003662 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 }
3664 clear_tv(tv1);
3665 clear_tv(tv2);
3666 tv1->v_type = VAR_FLOAT;
3667 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003668 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 }
3670 else
3671#endif
3672 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003673 int failed = FALSE;
3674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 switch (iptr->isn_arg.op.op_type)
3676 {
3677 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003678 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3679 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003680 goto on_error;
3681 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 case EXPR_SUB: n1 = n1 - n2; break;
3683 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003684 default: n1 = num_modulus(n1, n2, &failed);
3685 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003686 goto on_error;
3687 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 }
3689 clear_tv(tv1);
3690 clear_tv(tv2);
3691 tv1->v_type = VAR_NUMBER;
3692 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003693 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 }
3695 }
3696 break;
3697
3698 case ISN_CONCAT:
3699 {
3700 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3701 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3702 char_u *res;
3703
3704 res = concat_str(str1, str2);
3705 clear_tv(STACK_TV_BOT(-2));
3706 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003707 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 STACK_TV_BOT(-1)->vval.v_string = res;
3709 }
3710 break;
3711
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003712 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003713 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003714 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003715 int is_slice = iptr->isn_type == ISN_STRSLICE;
3716 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003717 char_u *res;
3718
3719 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003720 // string slice: string is at stack-3, first index at
3721 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003722 if (is_slice)
3723 {
3724 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003725 n1 = tv->vval.v_number;
3726 }
3727
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003728 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003729 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003730
Bram Moolenaar4c137212021-04-19 16:48:48 +02003731 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003732 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003733 if (is_slice)
3734 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003735 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003736 else
3737 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003738 // single character (including composing characters).
3739 // If the index is too big or negative the result is
3740 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003741 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003742 vim_free(tv->vval.v_string);
3743 tv->vval.v_string = res;
3744 }
3745 break;
3746
3747 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003748 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003749 case ISN_BLOBINDEX:
3750 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003752 int is_slice = iptr->isn_type == ISN_LISTSLICE
3753 || iptr->isn_type == ISN_BLOBSLICE;
3754 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3755 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003756 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003757 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758
3759 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003760 // list slice: list is at stack-3, indexes at stack-2 and
3761 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003762 // Same for blob.
3763 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764
3765 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003766 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003768
3769 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 {
Bram Moolenaared591872020-08-15 22:14:53 +02003771 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003772 n1 = tv->vval.v_number;
3773 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 }
Bram Moolenaared591872020-08-15 22:14:53 +02003775
Bram Moolenaar4c137212021-04-19 16:48:48 +02003776 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003777 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003778 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003779 if (is_blob)
3780 {
3781 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3782 n1, n2, FALSE, tv) == FAIL)
3783 goto on_error;
3784 }
3785 else
3786 {
3787 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3788 n1, n2, FALSE, tv, TRUE) == FAIL)
3789 goto on_error;
3790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 }
3792 break;
3793
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003794 case ISN_ANYINDEX:
3795 case ISN_ANYSLICE:
3796 {
3797 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3798 typval_T *var1, *var2;
3799 int res;
3800
3801 // index: composite is at stack-2, index at stack-1
3802 // slice: composite is at stack-3, indexes at stack-2 and
3803 // stack-1
3804 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003805 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003806 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3807 goto on_error;
3808 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3809 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003810 res = eval_index_inner(tv, is_slice, var1, var2,
3811 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003812 clear_tv(var1);
3813 if (is_slice)
3814 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003815 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003816 if (res == FAIL)
3817 goto on_error;
3818 }
3819 break;
3820
Bram Moolenaar9af78762020-06-16 11:34:42 +02003821 case ISN_SLICE:
3822 {
3823 list_T *list;
3824 int count = iptr->isn_arg.number;
3825
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003826 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003827 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003828 list = tv->vval.v_list;
3829
3830 // no error for short list, expect it to be checked earlier
3831 if (list != NULL && list->lv_len >= count)
3832 {
3833 list_T *newlist = list_slice(list,
3834 count, list->lv_len - 1);
3835
3836 if (newlist != NULL)
3837 {
3838 list_unref(list);
3839 tv->vval.v_list = newlist;
3840 ++newlist->lv_refcount;
3841 }
3842 }
3843 }
3844 break;
3845
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003846 case ISN_GETITEM:
3847 {
3848 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003849 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003850
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003851 // Get list item: list is at stack-1, push item.
3852 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003853 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
3854 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003855
Bram Moolenaar4c137212021-04-19 16:48:48 +02003856 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003857 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003858 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003859 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003860
3861 // Useful when used in unpack assignment. Reset at
3862 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003863 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003864 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003865 }
3866 break;
3867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 case ISN_MEMBER:
3869 {
3870 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003871 char_u *key;
3872 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003873 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003874
3875 // dict member: dict is at stack-2, key at stack-1
3876 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003877 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003878 dict = tv->vval.v_dict;
3879
3880 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003881 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003882 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003883 if (key == NULL)
3884 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003885
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003886 if ((di = dict_find(dict, key, -1)) == NULL)
3887 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003888 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003889 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003890
3891 // If :silent! is used we will continue, make sure the
3892 // stack contents makes sense.
3893 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003894 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003895 tv = STACK_TV_BOT(-1);
3896 clear_tv(tv);
3897 tv->v_type = VAR_NUMBER;
3898 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003899 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003900 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003901 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003902 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003903 // Clear the dict only after getting the item, to avoid
3904 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003905 tv = STACK_TV_BOT(-1);
3906 temp_tv = *tv;
3907 copy_tv(&di->di_tv, tv);
3908 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003909 }
3910 break;
3911
3912 // dict member with string key
3913 case ISN_STRINGMEMBER:
3914 {
3915 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003917 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918
3919 tv = STACK_TV_BOT(-1);
3920 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3921 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003922 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003924 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 }
3926 dict = tv->vval.v_dict;
3927
3928 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3929 == NULL)
3930 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003931 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003933 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003935 // Clear the dict after getting the item, to avoid that it
3936 // make the item invalid.
3937 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003939 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 }
3941 break;
3942
3943 case ISN_NEGATENR:
3944 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003945 if (tv->v_type != VAR_NUMBER
3946#ifdef FEAT_FLOAT
3947 && tv->v_type != VAR_FLOAT
3948#endif
3949 )
3950 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003951 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003952 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003953 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003954 }
3955#ifdef FEAT_FLOAT
3956 if (tv->v_type == VAR_FLOAT)
3957 tv->vval.v_float = -tv->vval.v_float;
3958 else
3959#endif
3960 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 break;
3962
3963 case ISN_CHECKNR:
3964 {
3965 int error = FALSE;
3966
3967 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003968 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003970 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 (void)tv_get_number_chk(tv, &error);
3972 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003973 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 }
3975 break;
3976
3977 case ISN_CHECKTYPE:
3978 {
3979 checktype_T *ct = &iptr->isn_arg.type;
3980
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003981 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003982 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003983 if (!ectx->ec_where.wt_variable)
3984 ectx->ec_where.wt_index = ct->ct_arg_idx;
3985 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3986 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003987 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003988 if (!ectx->ec_where.wt_variable)
3989 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003990
3991 // number 0 is FALSE, number 1 is TRUE
3992 if (tv->v_type == VAR_NUMBER
3993 && ct->ct_type->tt_type == VAR_BOOL
3994 && (tv->vval.v_number == 0
3995 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003997 tv->v_type = VAR_BOOL;
3998 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003999 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 }
4001 }
4002 break;
4003
Bram Moolenaar9af78762020-06-16 11:34:42 +02004004 case ISN_CHECKLEN:
4005 {
4006 int min_len = iptr->isn_arg.checklen.cl_min_len;
4007 list_T *list = NULL;
4008
4009 tv = STACK_TV_BOT(-1);
4010 if (tv->v_type == VAR_LIST)
4011 list = tv->vval.v_list;
4012 if (list == NULL || list->lv_len < min_len
4013 || (list->lv_len > min_len
4014 && !iptr->isn_arg.checklen.cl_more_OK))
4015 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004016 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004017 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004018 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004019 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004020 }
4021 }
4022 break;
4023
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004024 case ISN_SETTYPE:
4025 {
4026 checktype_T *ct = &iptr->isn_arg.type;
4027
4028 tv = STACK_TV_BOT(-1);
4029 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4030 {
4031 free_type(tv->vval.v_dict->dv_type);
4032 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4033 }
4034 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4035 {
4036 free_type(tv->vval.v_list->lv_type);
4037 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4038 }
4039 }
4040 break;
4041
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004043 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 {
4045 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004046 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004048 if (iptr->isn_type == ISN_2BOOL)
4049 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004050 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004051 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004052 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004053 n = !n;
4054 }
4055 else
4056 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004057 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004058 SOURCING_LNUM = iptr->isn_lnum;
4059 n = tv_get_bool_chk(tv, &error);
4060 if (error)
4061 goto on_error;
4062 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 clear_tv(tv);
4064 tv->v_type = VAR_BOOL;
4065 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4066 }
4067 break;
4068
4069 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004070 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004071 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004072 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4073 iptr->isn_type == ISN_2STRING_ANY,
4074 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004075 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 break;
4077
Bram Moolenaar08597872020-12-10 19:43:40 +01004078 case ISN_RANGE:
4079 {
4080 exarg_T ea;
4081 char *errormsg;
4082
Bram Moolenaarece0b872021-01-08 20:40:45 +01004083 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004084 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004085 ea.addr_type = ADDR_LINES;
4086 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004087 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004088 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004089 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004090
Bram Moolenaar4c137212021-04-19 16:48:48 +02004091 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004092 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004093 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004094 tv = STACK_TV_BOT(-1);
4095 tv->v_type = VAR_NUMBER;
4096 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004097 if (ea.addr_count == 0)
4098 tv->vval.v_number = curwin->w_cursor.lnum;
4099 else
4100 tv->vval.v_number = ea.line2;
4101 }
4102 break;
4103
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004104 case ISN_PUT:
4105 {
4106 int regname = iptr->isn_arg.put.put_regname;
4107 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4108 char_u *expr = NULL;
4109 int dir = FORWARD;
4110
Bram Moolenaar08597872020-12-10 19:43:40 +01004111 if (lnum < -2)
4112 {
4113 // line number was put on the stack by ISN_RANGE
4114 tv = STACK_TV_BOT(-1);
4115 curwin->w_cursor.lnum = tv->vval.v_number;
4116 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4117 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004118 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004119 }
4120 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004121 // :put! above cursor
4122 dir = BACKWARD;
4123 else if (lnum >= 0)
4124 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004125
4126 if (regname == '=')
4127 {
4128 tv = STACK_TV_BOT(-1);
4129 if (tv->v_type == VAR_STRING)
4130 expr = tv->vval.v_string;
4131 else
4132 {
4133 expr = typval2string(tv, TRUE); // allocates value
4134 clear_tv(tv);
4135 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004136 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004137 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004138 check_cursor();
4139 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4140 vim_free(expr);
4141 }
4142 break;
4143
Bram Moolenaar02194d22020-10-24 23:08:38 +02004144 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004145 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4146 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4147 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4148 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004149 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4150 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004151 break;
4152
Bram Moolenaar02194d22020-10-24 23:08:38 +02004153 case ISN_CMDMOD_REV:
4154 // filter regprog is owned by the instruction, don't free it
4155 cmdmod.cmod_filter_regmatch.regprog = NULL;
4156 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004157 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4158 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004159 break;
4160
Bram Moolenaar792f7862020-11-23 08:31:18 +01004161 case ISN_UNPACK:
4162 {
4163 int count = iptr->isn_arg.unpack.unp_count;
4164 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4165 list_T *l;
4166 listitem_T *li;
4167 int i;
4168
4169 // Check there is a valid list to unpack.
4170 tv = STACK_TV_BOT(-1);
4171 if (tv->v_type != VAR_LIST)
4172 {
4173 SOURCING_LNUM = iptr->isn_lnum;
4174 emsg(_(e_for_argument_must_be_sequence_of_lists));
4175 goto on_error;
4176 }
4177 l = tv->vval.v_list;
4178 if (l == NULL
4179 || l->lv_len < (semicolon ? count - 1 : count))
4180 {
4181 SOURCING_LNUM = iptr->isn_lnum;
4182 emsg(_(e_list_value_does_not_have_enough_items));
4183 goto on_error;
4184 }
4185 else if (!semicolon && l->lv_len > count)
4186 {
4187 SOURCING_LNUM = iptr->isn_lnum;
4188 emsg(_(e_list_value_has_more_items_than_targets));
4189 goto on_error;
4190 }
4191
4192 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004193 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004194 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004195 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004196
4197 // Variable after semicolon gets a list with the remaining
4198 // items.
4199 if (semicolon)
4200 {
4201 list_T *rem_list =
4202 list_alloc_with_items(l->lv_len - count + 1);
4203
4204 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004205 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004206 tv = STACK_TV_BOT(-count);
4207 tv->vval.v_list = rem_list;
4208 ++rem_list->lv_refcount;
4209 tv->v_lock = 0;
4210 li = l->lv_first;
4211 for (i = 0; i < count - 1; ++i)
4212 li = li->li_next;
4213 for (i = 0; li != NULL; ++i)
4214 {
4215 list_set_item(rem_list, i, &li->li_tv);
4216 li = li->li_next;
4217 }
4218 --count;
4219 }
4220
4221 // Produce the values in reverse order, first item last.
4222 li = l->lv_first;
4223 for (i = 0; i < count; ++i)
4224 {
4225 tv = STACK_TV_BOT(-i - 1);
4226 copy_tv(&li->li_tv, tv);
4227 li = li->li_next;
4228 }
4229
4230 list_unref(l);
4231 }
4232 break;
4233
Bram Moolenaarb2049902021-01-24 12:53:53 +01004234 case ISN_PROF_START:
4235 case ISN_PROF_END:
4236 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004237#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004238 funccall_T cookie;
4239 ufunc_T *cur_ufunc =
4240 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004241 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004242
4243 cookie.func = cur_ufunc;
4244 if (iptr->isn_type == ISN_PROF_START)
4245 {
4246 func_line_start(&cookie, iptr->isn_lnum);
4247 // if we get here the instruction is executed
4248 func_line_exec(&cookie);
4249 }
4250 else
4251 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004252#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004253 }
4254 break;
4255
Bram Moolenaare99d4222021-06-13 14:01:26 +02004256 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004257 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004258 break;
4259
Bram Moolenaar389df252020-07-09 21:20:47 +02004260 case ISN_SHUFFLE:
4261 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004262 typval_T tmp_tv;
4263 int item = iptr->isn_arg.shuffle.shfl_item;
4264 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004265
4266 tmp_tv = *STACK_TV_BOT(-item);
4267 for ( ; up > 0 && item > 1; --up)
4268 {
4269 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4270 --item;
4271 }
4272 *STACK_TV_BOT(-item) = tmp_tv;
4273 }
4274 break;
4275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004277 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004279 ectx->ec_where.wt_index = 0;
4280 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 break;
4282 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004283 continue;
4284
Bram Moolenaard032f342020-07-18 18:13:02 +02004285func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004286 // Restore previous function. If the frame pointer is where we started
4287 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004288 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004289 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004290
Bram Moolenaar4c137212021-04-19 16:48:48 +02004291 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004292 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004293 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004294 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004295
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004296on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004297 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004298 // If "emsg_silent" is set then ignore the error, unless it was set
4299 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004300 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004301 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004302 {
4303 // If a sequence of instructions causes an error while ":silent!"
4304 // was used, restore the stack length and jump ahead to restoring
4305 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004306 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004307 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004308 while (ectx->ec_stack.ga_len
4309 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004310 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004311 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004312 clear_tv(STACK_TV_BOT(0));
4313 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004314 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4315 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004316 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004317 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004318 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004319on_fatal_error:
4320 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004321 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004322 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004323 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 }
4325
4326done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004327 ret = OK;
4328theend:
4329 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4330 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004331}
4332
4333/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004334 * Execute the instructions from a VAR_INSTR typeval and put the result in
4335 * "rettv".
4336 * Return OK or FAIL.
4337 */
4338 int
4339exe_typval_instr(typval_T *tv, typval_T *rettv)
4340{
4341 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4342 isn_T *save_instr = ectx->ec_instr;
4343 int save_iidx = ectx->ec_iidx;
4344 int res;
4345
4346 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4347 res = exec_instructions(ectx);
4348 if (res == OK)
4349 {
4350 *rettv = *STACK_TV_BOT(-1);
4351 --ectx->ec_stack.ga_len;
4352 }
4353
4354 ectx->ec_instr = save_instr;
4355 ectx->ec_iidx = save_iidx;
4356
4357 return res;
4358}
4359
4360/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004361 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4362 * "substitute_instr".
4363 */
4364 char_u *
4365exe_substitute_instr(void)
4366{
4367 ectx_T *ectx = substitute_instr->subs_ectx;
4368 isn_T *save_instr = ectx->ec_instr;
4369 int save_iidx = ectx->ec_iidx;
4370 char_u *res;
4371
4372 ectx->ec_instr = substitute_instr->subs_instr;
4373 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004374 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004375 typval_T *tv = STACK_TV_BOT(-1);
4376
Bram Moolenaar27523602021-06-05 21:36:19 +02004377 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004378 --ectx->ec_stack.ga_len;
4379 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004380 }
4381 else
4382 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004383 substitute_instr->subs_status = FAIL;
4384 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004385 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386
Bram Moolenaar4c137212021-04-19 16:48:48 +02004387 ectx->ec_instr = save_instr;
4388 ectx->ec_iidx = save_iidx;
4389
4390 return res;
4391}
4392
4393/*
4394 * Call a "def" function from old Vim script.
4395 * Return OK or FAIL.
4396 */
4397 int
4398call_def_function(
4399 ufunc_T *ufunc,
4400 int argc_arg, // nr of arguments
4401 typval_T *argv, // arguments
4402 partial_T *partial, // optional partial for context
4403 typval_T *rettv) // return value
4404{
4405 ectx_T ectx; // execution context
4406 int argc = argc_arg;
4407 typval_T *tv;
4408 int idx;
4409 int ret = FAIL;
4410 int defcount = ufunc->uf_args.ga_len - argc;
4411 sctx_T save_current_sctx = current_sctx;
4412 int did_emsg_before = did_emsg_cumul + did_emsg;
4413 int save_suppress_errthrow = suppress_errthrow;
4414 msglist_T **saved_msg_list = NULL;
4415 msglist_T *private_msg_list = NULL;
4416 int save_emsg_silent_def = emsg_silent_def;
4417 int save_did_emsg_def = did_emsg_def;
4418 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004419 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004420
4421// Get pointer to item in the stack.
4422#undef STACK_TV
4423#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4424
4425// Get pointer to item at the bottom of the stack, -1 is the bottom.
4426#undef STACK_TV_BOT
4427#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4428
4429// Get pointer to a local variable on the stack. Negative for arguments.
4430#undef STACK_TV_VAR
4431#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4432
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004433 // Update uf_has_breakpoint if needed.
4434 update_has_breakpoint(ufunc);
4435
Bram Moolenaar4c137212021-04-19 16:48:48 +02004436 if (ufunc->uf_def_status == UF_NOT_COMPILED
4437 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004438 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4439 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004440 == FAIL))
4441 {
4442 if (did_emsg_cumul + did_emsg == did_emsg_before)
4443 semsg(_(e_function_is_not_compiled_str),
4444 printable_func_name(ufunc));
4445 return FAIL;
4446 }
4447
4448 {
4449 // Check the function was really compiled.
4450 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4451 + ufunc->uf_dfunc_idx;
4452 if (INSTRUCTIONS(dfunc) == NULL)
4453 {
4454 iemsg("using call_def_function() on not compiled function");
4455 return FAIL;
4456 }
4457 }
4458
4459 // If depth of calling is getting too high, don't execute the function.
4460 orig_funcdepth = funcdepth_get();
4461 if (funcdepth_increment() == FAIL)
4462 return FAIL;
4463
4464 CLEAR_FIELD(ectx);
4465 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4466 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4467 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4468 {
4469 funcdepth_decrement();
4470 return FAIL;
4471 }
4472 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4473 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4474 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004475 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004476
4477 idx = argc - ufunc->uf_args.ga_len;
4478 if (idx > 0 && ufunc->uf_va_name == NULL)
4479 {
4480 if (idx == 1)
4481 emsg(_(e_one_argument_too_many));
4482 else
4483 semsg(_(e_nr_arguments_too_many), idx);
4484 goto failed_early;
4485 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004486 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4487 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004488 {
4489 if (idx == -1)
4490 emsg(_(e_one_argument_too_few));
4491 else
4492 semsg(_(e_nr_arguments_too_few), -idx);
4493 goto failed_early;
4494 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004495
4496 // Put arguments on the stack, but no more than what the function expects.
4497 // A lambda can be called with more arguments than it uses.
4498 for (idx = 0; idx < argc
4499 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4500 ++idx)
4501 {
4502 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4503 && argv[idx].v_type == VAR_SPECIAL
4504 && argv[idx].vval.v_number == VVAL_NONE)
4505 {
4506 // Use the default value.
4507 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4508 }
4509 else
4510 {
4511 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4512 && check_typval_arg_type(
4513 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4514 goto failed_early;
4515 copy_tv(&argv[idx], STACK_TV_BOT(0));
4516 }
4517 ++ectx.ec_stack.ga_len;
4518 }
4519
4520 // Turn varargs into a list. Empty list if no args.
4521 if (ufunc->uf_va_name != NULL)
4522 {
4523 int vararg_count = argc - ufunc->uf_args.ga_len;
4524
4525 if (vararg_count < 0)
4526 vararg_count = 0;
4527 else
4528 argc -= vararg_count;
4529 if (exe_newlist(vararg_count, &ectx) == FAIL)
4530 goto failed_early;
4531
4532 // Check the type of the list items.
4533 tv = STACK_TV_BOT(-1);
4534 if (ufunc->uf_va_type != NULL
4535 && ufunc->uf_va_type != &t_list_any
4536 && ufunc->uf_va_type->tt_member != &t_any
4537 && tv->vval.v_list != NULL)
4538 {
4539 type_T *expected = ufunc->uf_va_type->tt_member;
4540 listitem_T *li = tv->vval.v_list->lv_first;
4541
4542 for (idx = 0; idx < vararg_count; ++idx)
4543 {
4544 if (check_typval_arg_type(expected, &li->li_tv,
4545 argc + idx + 1) == FAIL)
4546 goto failed_early;
4547 li = li->li_next;
4548 }
4549 }
4550
4551 if (defcount > 0)
4552 // Move varargs list to below missing default arguments.
4553 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4554 --ectx.ec_stack.ga_len;
4555 }
4556
4557 // Make space for omitted arguments, will store default value below.
4558 // Any varargs list goes after them.
4559 if (defcount > 0)
4560 for (idx = 0; idx < defcount; ++idx)
4561 {
4562 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4563 ++ectx.ec_stack.ga_len;
4564 }
4565 if (ufunc->uf_va_name != NULL)
4566 ++ectx.ec_stack.ga_len;
4567
4568 // Frame pointer points to just after arguments.
4569 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4570 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4571
4572 {
4573 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4574 + ufunc->uf_dfunc_idx;
4575 ufunc_T *base_ufunc = dfunc->df_ufunc;
4576
4577 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4578 // by copy_func().
4579 if (partial != NULL || base_ufunc->uf_partial != NULL)
4580 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004581 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4582 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004583 goto failed_early;
4584 if (partial != NULL)
4585 {
4586 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4587 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004588 if (current_ectx->ec_outer_ref != NULL
4589 && current_ectx->ec_outer_ref->or_outer != NULL)
4590 ectx.ec_outer_ref->or_outer =
4591 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004592 }
4593 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004594 {
4595 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4596 ++partial->pt_refcount;
4597 ectx.ec_outer_ref->or_partial = partial;
4598 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004599 }
4600 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004601 {
4602 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4603 ++base_ufunc->uf_partial->pt_refcount;
4604 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4605 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004606 }
4607 }
4608
4609 // dummy frame entries
4610 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4611 {
4612 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4613 ++ectx.ec_stack.ga_len;
4614 }
4615
4616 {
4617 // Reserve space for local variables and any closure reference count.
4618 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4619 + ufunc->uf_dfunc_idx;
4620
4621 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4622 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4623 ectx.ec_stack.ga_len += dfunc->df_varcount;
4624 if (dfunc->df_has_closure)
4625 {
4626 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4627 STACK_TV_VAR(idx)->vval.v_number = 0;
4628 ++ectx.ec_stack.ga_len;
4629 }
4630
4631 ectx.ec_instr = INSTRUCTIONS(dfunc);
4632 }
4633
4634 // Following errors are in the function, not the caller.
4635 // Commands behave like vim9script.
4636 estack_push_ufunc(ufunc, 1);
4637 current_sctx = ufunc->uf_script_ctx;
4638 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4639
4640 // Use a specific location for storing error messages to be converted to an
4641 // exception.
4642 saved_msg_list = msg_list;
4643 msg_list = &private_msg_list;
4644
4645 // Do turn errors into exceptions.
4646 suppress_errthrow = FALSE;
4647
4648 // Do not delete the function while executing it.
4649 ++ufunc->uf_calls;
4650
4651 // When ":silent!" was used before calling then we still abort the
4652 // function. If ":silent!" is used in the function then we don't.
4653 emsg_silent_def = emsg_silent;
4654 did_emsg_def = 0;
4655
4656 ectx.ec_where.wt_index = 0;
4657 ectx.ec_where.wt_variable = FALSE;
4658
4659 // Execute the instructions until done.
4660 ret = exec_instructions(&ectx);
4661 if (ret == OK)
4662 {
4663 // function finished, get result from the stack.
4664 if (ufunc->uf_ret_type == &t_void)
4665 {
4666 rettv->v_type = VAR_VOID;
4667 }
4668 else
4669 {
4670 tv = STACK_TV_BOT(-1);
4671 *rettv = *tv;
4672 tv->v_type = VAR_UNKNOWN;
4673 }
4674 }
4675
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004676 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004677 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4678 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004679
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004680 // Deal with any remaining closures, they may be in use somewhere.
4681 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004682 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004683 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004684 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4685 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004686
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004687 estack_pop();
4688 current_sctx = save_current_sctx;
4689
Bram Moolenaarc970e422021-03-17 15:03:04 +01004690 // TODO: when is it safe to delete the function if it is no longer used?
4691 --ufunc->uf_calls;
4692
Bram Moolenaar352134b2020-10-17 22:04:08 +02004693 if (*msg_list != NULL && saved_msg_list != NULL)
4694 {
4695 msglist_T **plist = saved_msg_list;
4696
4697 // Append entries from the current msg_list (uncaught exceptions) to
4698 // the saved msg_list.
4699 while (*plist != NULL)
4700 plist = &(*plist)->next;
4701
4702 *plist = *msg_list;
4703 }
4704 msg_list = saved_msg_list;
4705
Bram Moolenaar4c137212021-04-19 16:48:48 +02004706 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004707 {
4708 cmdmod.cmod_filter_regmatch.regprog = NULL;
4709 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004710 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004711 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004712 emsg_silent_def = save_emsg_silent_def;
4713 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004714
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004715failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004716 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4718 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004719 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004722 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004723 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004724 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004725 if (ectx.ec_outer_ref->or_outer_allocated)
4726 vim_free(ectx.ec_outer_ref->or_outer);
4727 partial_unref(ectx.ec_outer_ref->or_partial);
4728 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004729 }
4730
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004731 // Not sure if this is necessary.
4732 suppress_errthrow = save_suppress_errthrow;
4733
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004734 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004735 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004736 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004737 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 return ret;
4739}
4740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004742 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4743 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4744 * "pfx" is prefixed to every line.
4745 */
4746 static void
4747list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4748{
4749 int line_idx = 0;
4750 int prev_current = 0;
4751 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004752 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004753
4754 for (current = 0; current < instr_count; ++current)
4755 {
4756 isn_T *iptr = &instr[current];
4757 char *line;
4758
4759 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004760 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004761 while (line_idx < iptr->isn_lnum
4762 && line_idx < ufunc->uf_lines.ga_len)
4763 {
4764 if (current > prev_current)
4765 {
4766 msg_puts("\n\n");
4767 prev_current = current;
4768 }
4769 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4770 if (line != NULL)
4771 msg(line);
4772 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004773 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4774 {
4775 int first_def_arg = ufunc->uf_args.ga_len
4776 - ufunc->uf_def_args.ga_len;
4777
4778 if (def_arg_idx > 0)
4779 msg_puts("\n\n");
4780 msg_start();
4781 msg_puts(" ");
4782 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4783 first_def_arg + def_arg_idx]);
4784 msg_puts(" = ");
4785 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4786 msg_clr_eos();
4787 msg_end();
4788 }
4789 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004790
4791 switch (iptr->isn_type)
4792 {
4793 case ISN_EXEC:
4794 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4795 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004796 case ISN_EXEC_SPLIT:
4797 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4798 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004799 case ISN_LEGACY_EVAL:
4800 smsg("%s%4d EVAL legacy %s", pfx, current,
4801 iptr->isn_arg.string);
4802 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004803 case ISN_REDIRSTART:
4804 smsg("%s%4d REDIR", pfx, current);
4805 break;
4806 case ISN_REDIREND:
4807 smsg("%s%4d REDIR END%s", pfx, current,
4808 iptr->isn_arg.number ? " append" : "");
4809 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004810 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004811#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004812 smsg("%s%4d CEXPR pre %s", pfx, current,
4813 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004814#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004815 break;
4816 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004817#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004818 {
4819 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4820
4821 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4822 cexpr_get_auname(cer->cer_cmdidx),
4823 cer->cer_forceit ? "!" : "",
4824 cer->cer_cmdline);
4825 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004826#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004827 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004828 case ISN_INSTR:
4829 {
4830 smsg("%s%4d INSTR", pfx, current);
4831 list_instructions(" ", iptr->isn_arg.instr,
4832 INT_MAX, NULL);
4833 msg(" -------------");
4834 }
4835 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004836 case ISN_SUBSTITUTE:
4837 {
4838 subs_T *subs = &iptr->isn_arg.subs;
4839
4840 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4841 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4842 msg(" -------------");
4843 }
4844 break;
4845 case ISN_EXECCONCAT:
4846 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4847 (varnumber_T)iptr->isn_arg.number);
4848 break;
4849 case ISN_ECHO:
4850 {
4851 echo_T *echo = &iptr->isn_arg.echo;
4852
4853 smsg("%s%4d %s %d", pfx, current,
4854 echo->echo_with_white ? "ECHO" : "ECHON",
4855 echo->echo_count);
4856 }
4857 break;
4858 case ISN_EXECUTE:
4859 smsg("%s%4d EXECUTE %lld", pfx, current,
4860 (varnumber_T)(iptr->isn_arg.number));
4861 break;
4862 case ISN_ECHOMSG:
4863 smsg("%s%4d ECHOMSG %lld", pfx, current,
4864 (varnumber_T)(iptr->isn_arg.number));
4865 break;
4866 case ISN_ECHOERR:
4867 smsg("%s%4d ECHOERR %lld", pfx, current,
4868 (varnumber_T)(iptr->isn_arg.number));
4869 break;
4870 case ISN_LOAD:
4871 {
4872 if (iptr->isn_arg.number < 0)
4873 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4874 (varnumber_T)(iptr->isn_arg.number
4875 + STACK_FRAME_SIZE));
4876 else
4877 smsg("%s%4d LOAD $%lld", pfx, current,
4878 (varnumber_T)(iptr->isn_arg.number));
4879 }
4880 break;
4881 case ISN_LOADOUTER:
4882 {
4883 if (iptr->isn_arg.number < 0)
4884 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4885 iptr->isn_arg.outer.outer_depth,
4886 iptr->isn_arg.outer.outer_idx
4887 + STACK_FRAME_SIZE);
4888 else
4889 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4890 iptr->isn_arg.outer.outer_depth,
4891 iptr->isn_arg.outer.outer_idx);
4892 }
4893 break;
4894 case ISN_LOADV:
4895 smsg("%s%4d LOADV v:%s", pfx, current,
4896 get_vim_var_name(iptr->isn_arg.number));
4897 break;
4898 case ISN_LOADSCRIPT:
4899 {
4900 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4901 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4902 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4903 + sref->sref_idx;
4904
4905 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4906 sv->sv_name,
4907 sref->sref_idx,
4908 si->sn_name);
4909 }
4910 break;
4911 case ISN_LOADS:
4912 {
4913 scriptitem_T *si = SCRIPT_ITEM(
4914 iptr->isn_arg.loadstore.ls_sid);
4915
4916 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4917 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4918 }
4919 break;
4920 case ISN_LOADAUTO:
4921 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4922 break;
4923 case ISN_LOADG:
4924 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4925 break;
4926 case ISN_LOADB:
4927 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4928 break;
4929 case ISN_LOADW:
4930 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4931 break;
4932 case ISN_LOADT:
4933 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4934 break;
4935 case ISN_LOADGDICT:
4936 smsg("%s%4d LOAD g:", pfx, current);
4937 break;
4938 case ISN_LOADBDICT:
4939 smsg("%s%4d LOAD b:", pfx, current);
4940 break;
4941 case ISN_LOADWDICT:
4942 smsg("%s%4d LOAD w:", pfx, current);
4943 break;
4944 case ISN_LOADTDICT:
4945 smsg("%s%4d LOAD t:", pfx, current);
4946 break;
4947 case ISN_LOADOPT:
4948 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4949 break;
4950 case ISN_LOADENV:
4951 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4952 break;
4953 case ISN_LOADREG:
4954 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4955 break;
4956
4957 case ISN_STORE:
4958 if (iptr->isn_arg.number < 0)
4959 smsg("%s%4d STORE arg[%lld]", pfx, current,
4960 iptr->isn_arg.number + STACK_FRAME_SIZE);
4961 else
4962 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4963 break;
4964 case ISN_STOREOUTER:
4965 {
4966 if (iptr->isn_arg.number < 0)
4967 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4968 iptr->isn_arg.outer.outer_depth,
4969 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4970 else
4971 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4972 iptr->isn_arg.outer.outer_depth,
4973 iptr->isn_arg.outer.outer_idx);
4974 }
4975 break;
4976 case ISN_STOREV:
4977 smsg("%s%4d STOREV v:%s", pfx, current,
4978 get_vim_var_name(iptr->isn_arg.number));
4979 break;
4980 case ISN_STOREAUTO:
4981 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4982 break;
4983 case ISN_STOREG:
4984 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4985 break;
4986 case ISN_STOREB:
4987 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4988 break;
4989 case ISN_STOREW:
4990 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4991 break;
4992 case ISN_STORET:
4993 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4994 break;
4995 case ISN_STORES:
4996 {
4997 scriptitem_T *si = SCRIPT_ITEM(
4998 iptr->isn_arg.loadstore.ls_sid);
4999
5000 smsg("%s%4d STORES %s in %s", pfx, current,
5001 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5002 }
5003 break;
5004 case ISN_STORESCRIPT:
5005 {
5006 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5007 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5008 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
5009 + sref->sref_idx;
5010
5011 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
5012 sv->sv_name,
5013 sref->sref_idx,
5014 si->sn_name);
5015 }
5016 break;
5017 case ISN_STOREOPT:
5018 smsg("%s%4d STOREOPT &%s", pfx, current,
5019 iptr->isn_arg.storeopt.so_name);
5020 break;
5021 case ISN_STOREENV:
5022 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5023 break;
5024 case ISN_STOREREG:
5025 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
5026 break;
5027 case ISN_STORENR:
5028 smsg("%s%4d STORE %lld in $%d", pfx, current,
5029 iptr->isn_arg.storenr.stnr_val,
5030 iptr->isn_arg.storenr.stnr_idx);
5031 break;
5032
5033 case ISN_STOREINDEX:
5034 smsg("%s%4d STOREINDEX %s", pfx, current,
5035 vartype_name(iptr->isn_arg.vartype));
5036 break;
5037
5038 case ISN_STORERANGE:
5039 smsg("%s%4d STORERANGE", pfx, current);
5040 break;
5041
5042 // constants
5043 case ISN_PUSHNR:
5044 smsg("%s%4d PUSHNR %lld", pfx, current,
5045 (varnumber_T)(iptr->isn_arg.number));
5046 break;
5047 case ISN_PUSHBOOL:
5048 case ISN_PUSHSPEC:
5049 smsg("%s%4d PUSH %s", pfx, current,
5050 get_var_special_name(iptr->isn_arg.number));
5051 break;
5052 case ISN_PUSHF:
5053#ifdef FEAT_FLOAT
5054 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5055#endif
5056 break;
5057 case ISN_PUSHS:
5058 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5059 break;
5060 case ISN_PUSHBLOB:
5061 {
5062 char_u *r;
5063 char_u numbuf[NUMBUFLEN];
5064 char_u *tofree;
5065
5066 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5067 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5068 vim_free(tofree);
5069 }
5070 break;
5071 case ISN_PUSHFUNC:
5072 {
5073 char *name = (char *)iptr->isn_arg.string;
5074
5075 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5076 name == NULL ? "[none]" : name);
5077 }
5078 break;
5079 case ISN_PUSHCHANNEL:
5080#ifdef FEAT_JOB_CHANNEL
5081 {
5082 channel_T *channel = iptr->isn_arg.channel;
5083
5084 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5085 channel == NULL ? 0 : channel->ch_id);
5086 }
5087#endif
5088 break;
5089 case ISN_PUSHJOB:
5090#ifdef FEAT_JOB_CHANNEL
5091 {
5092 typval_T tv;
5093 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005094 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005095
5096 tv.v_type = VAR_JOB;
5097 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005098 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005099 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5100 }
5101#endif
5102 break;
5103 case ISN_PUSHEXC:
5104 smsg("%s%4d PUSH v:exception", pfx, current);
5105 break;
5106 case ISN_UNLET:
5107 smsg("%s%4d UNLET%s %s", pfx, current,
5108 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5109 iptr->isn_arg.unlet.ul_name);
5110 break;
5111 case ISN_UNLETENV:
5112 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5113 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5114 iptr->isn_arg.unlet.ul_name);
5115 break;
5116 case ISN_UNLETINDEX:
5117 smsg("%s%4d UNLETINDEX", pfx, current);
5118 break;
5119 case ISN_UNLETRANGE:
5120 smsg("%s%4d UNLETRANGE", pfx, current);
5121 break;
5122 case ISN_LOCKCONST:
5123 smsg("%s%4d LOCKCONST", pfx, current);
5124 break;
5125 case ISN_NEWLIST:
5126 smsg("%s%4d NEWLIST size %lld", pfx, current,
5127 (varnumber_T)(iptr->isn_arg.number));
5128 break;
5129 case ISN_NEWDICT:
5130 smsg("%s%4d NEWDICT size %lld", pfx, current,
5131 (varnumber_T)(iptr->isn_arg.number));
5132 break;
5133
5134 // function call
5135 case ISN_BCALL:
5136 {
5137 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5138
5139 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5140 internal_func_name(cbfunc->cbf_idx),
5141 cbfunc->cbf_argcount);
5142 }
5143 break;
5144 case ISN_DCALL:
5145 {
5146 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5147 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5148 + cdfunc->cdf_idx;
5149
5150 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5151 df->df_ufunc->uf_name_exp != NULL
5152 ? df->df_ufunc->uf_name_exp
5153 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5154 }
5155 break;
5156 case ISN_UCALL:
5157 {
5158 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5159
5160 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5161 cufunc->cuf_name, cufunc->cuf_argcount);
5162 }
5163 break;
5164 case ISN_PCALL:
5165 {
5166 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5167
5168 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5169 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5170 }
5171 break;
5172 case ISN_PCALL_END:
5173 smsg("%s%4d PCALL end", pfx, current);
5174 break;
5175 case ISN_RETURN:
5176 smsg("%s%4d RETURN", pfx, current);
5177 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005178 case ISN_RETURN_VOID:
5179 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005180 break;
5181 case ISN_FUNCREF:
5182 {
5183 funcref_T *funcref = &iptr->isn_arg.funcref;
5184 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5185 + funcref->fr_func;
5186
5187 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5188 }
5189 break;
5190
5191 case ISN_NEWFUNC:
5192 {
5193 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5194
5195 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5196 newfunc->nf_lambda, newfunc->nf_global);
5197 }
5198 break;
5199
5200 case ISN_DEF:
5201 {
5202 char_u *name = iptr->isn_arg.string;
5203
5204 smsg("%s%4d DEF %s", pfx, current,
5205 name == NULL ? (char_u *)"" : name);
5206 }
5207 break;
5208
5209 case ISN_JUMP:
5210 {
5211 char *when = "?";
5212
5213 switch (iptr->isn_arg.jump.jump_when)
5214 {
5215 case JUMP_ALWAYS:
5216 when = "JUMP";
5217 break;
5218 case JUMP_AND_KEEP_IF_TRUE:
5219 when = "JUMP_AND_KEEP_IF_TRUE";
5220 break;
5221 case JUMP_IF_FALSE:
5222 when = "JUMP_IF_FALSE";
5223 break;
5224 case JUMP_AND_KEEP_IF_FALSE:
5225 when = "JUMP_AND_KEEP_IF_FALSE";
5226 break;
5227 case JUMP_IF_COND_FALSE:
5228 when = "JUMP_IF_COND_FALSE";
5229 break;
5230 case JUMP_IF_COND_TRUE:
5231 when = "JUMP_IF_COND_TRUE";
5232 break;
5233 }
5234 smsg("%s%4d %s -> %d", pfx, current, when,
5235 iptr->isn_arg.jump.jump_where);
5236 }
5237 break;
5238
5239 case ISN_JUMP_IF_ARG_SET:
5240 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5241 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5242 iptr->isn_arg.jump.jump_where);
5243 break;
5244
5245 case ISN_FOR:
5246 {
5247 forloop_T *forloop = &iptr->isn_arg.forloop;
5248
5249 smsg("%s%4d FOR $%d -> %d", pfx, current,
5250 forloop->for_idx, forloop->for_end);
5251 }
5252 break;
5253
5254 case ISN_TRY:
5255 {
5256 try_T *try = &iptr->isn_arg.try;
5257
5258 if (try->try_ref->try_finally == 0)
5259 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5260 pfx, current,
5261 try->try_ref->try_catch,
5262 try->try_ref->try_endtry);
5263 else
5264 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5265 pfx, current,
5266 try->try_ref->try_catch,
5267 try->try_ref->try_finally,
5268 try->try_ref->try_endtry);
5269 }
5270 break;
5271 case ISN_CATCH:
5272 // TODO
5273 smsg("%s%4d CATCH", pfx, current);
5274 break;
5275 case ISN_TRYCONT:
5276 {
5277 trycont_T *trycont = &iptr->isn_arg.trycont;
5278
5279 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5280 trycont->tct_levels,
5281 trycont->tct_levels == 1 ? "" : "s",
5282 trycont->tct_where);
5283 }
5284 break;
5285 case ISN_FINALLY:
5286 smsg("%s%4d FINALLY", pfx, current);
5287 break;
5288 case ISN_ENDTRY:
5289 smsg("%s%4d ENDTRY", pfx, current);
5290 break;
5291 case ISN_THROW:
5292 smsg("%s%4d THROW", pfx, current);
5293 break;
5294
5295 // expression operations on number
5296 case ISN_OPNR:
5297 case ISN_OPFLOAT:
5298 case ISN_OPANY:
5299 {
5300 char *what;
5301 char *ins;
5302
5303 switch (iptr->isn_arg.op.op_type)
5304 {
5305 case EXPR_MULT: what = "*"; break;
5306 case EXPR_DIV: what = "/"; break;
5307 case EXPR_REM: what = "%"; break;
5308 case EXPR_SUB: what = "-"; break;
5309 case EXPR_ADD: what = "+"; break;
5310 default: what = "???"; break;
5311 }
5312 switch (iptr->isn_type)
5313 {
5314 case ISN_OPNR: ins = "OPNR"; break;
5315 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5316 case ISN_OPANY: ins = "OPANY"; break;
5317 default: ins = "???"; break;
5318 }
5319 smsg("%s%4d %s %s", pfx, current, ins, what);
5320 }
5321 break;
5322
5323 case ISN_COMPAREBOOL:
5324 case ISN_COMPARESPECIAL:
5325 case ISN_COMPARENR:
5326 case ISN_COMPAREFLOAT:
5327 case ISN_COMPARESTRING:
5328 case ISN_COMPAREBLOB:
5329 case ISN_COMPARELIST:
5330 case ISN_COMPAREDICT:
5331 case ISN_COMPAREFUNC:
5332 case ISN_COMPAREANY:
5333 {
5334 char *p;
5335 char buf[10];
5336 char *type;
5337
5338 switch (iptr->isn_arg.op.op_type)
5339 {
5340 case EXPR_EQUAL: p = "=="; break;
5341 case EXPR_NEQUAL: p = "!="; break;
5342 case EXPR_GREATER: p = ">"; break;
5343 case EXPR_GEQUAL: p = ">="; break;
5344 case EXPR_SMALLER: p = "<"; break;
5345 case EXPR_SEQUAL: p = "<="; break;
5346 case EXPR_MATCH: p = "=~"; break;
5347 case EXPR_IS: p = "is"; break;
5348 case EXPR_ISNOT: p = "isnot"; break;
5349 case EXPR_NOMATCH: p = "!~"; break;
5350 default: p = "???"; break;
5351 }
5352 STRCPY(buf, p);
5353 if (iptr->isn_arg.op.op_ic == TRUE)
5354 strcat(buf, "?");
5355 switch(iptr->isn_type)
5356 {
5357 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5358 case ISN_COMPARESPECIAL:
5359 type = "COMPARESPECIAL"; break;
5360 case ISN_COMPARENR: type = "COMPARENR"; break;
5361 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5362 case ISN_COMPARESTRING:
5363 type = "COMPARESTRING"; break;
5364 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5365 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5366 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5367 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5368 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5369 default: type = "???"; break;
5370 }
5371
5372 smsg("%s%4d %s %s", pfx, current, type, buf);
5373 }
5374 break;
5375
5376 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5377 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5378
5379 // expression operations
5380 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5381 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5382 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5383 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5384 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5385 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5386 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5387 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5388 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5389 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5390 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5391 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5392 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005393 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5394 iptr->isn_arg.getitem.gi_index,
5395 iptr->isn_arg.getitem.gi_with_op ?
5396 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005397 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5398 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5399 iptr->isn_arg.string); break;
5400 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5401
5402 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5403 case ISN_CHECKTYPE:
5404 {
5405 checktype_T *ct = &iptr->isn_arg.type;
5406 char *tofree;
5407
5408 if (ct->ct_arg_idx == 0)
5409 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5410 type_name(ct->ct_type, &tofree),
5411 (int)ct->ct_off);
5412 else
5413 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5414 type_name(ct->ct_type, &tofree),
5415 (int)ct->ct_off,
5416 (int)ct->ct_arg_idx);
5417 vim_free(tofree);
5418 break;
5419 }
5420 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5421 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5422 iptr->isn_arg.checklen.cl_min_len);
5423 break;
5424 case ISN_SETTYPE:
5425 {
5426 char *tofree;
5427
5428 smsg("%s%4d SETTYPE %s", pfx, current,
5429 type_name(iptr->isn_arg.type.ct_type, &tofree));
5430 vim_free(tofree);
5431 break;
5432 }
5433 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005434 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5435 smsg("%s%4d INVERT %d (!val)", pfx, current,
5436 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005437 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005438 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5439 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005440 break;
5441 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005442 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005443 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005444 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5445 pfx, current,
5446 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005447 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005448 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5449 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005450 break;
5451 case ISN_PUT:
5452 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5453 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005454 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005455 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5456 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005457 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005458 else
5459 smsg("%s%4d PUT %c %ld", pfx, current,
5460 iptr->isn_arg.put.put_regname,
5461 (long)iptr->isn_arg.put.put_lnum);
5462 break;
5463
5464 // TODO: summarize modifiers
5465 case ISN_CMDMOD:
5466 {
5467 char_u *buf;
5468 size_t len = produce_cmdmods(
5469 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5470
5471 buf = alloc(len + 1);
5472 if (buf != NULL)
5473 {
5474 (void)produce_cmdmods(
5475 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5476 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5477 vim_free(buf);
5478 }
5479 break;
5480 }
5481 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5482
5483 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005484 smsg("%s%4d PROFILE START line %d", pfx, current,
5485 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005486 break;
5487
5488 case ISN_PROF_END:
5489 smsg("%s%4d PROFILE END", pfx, current);
5490 break;
5491
Bram Moolenaare99d4222021-06-13 14:01:26 +02005492 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005493 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5494 iptr->isn_arg.debug.dbg_break_lnum + 1,
5495 iptr->isn_lnum,
5496 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005497 break;
5498
Bram Moolenaar4c137212021-04-19 16:48:48 +02005499 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5500 iptr->isn_arg.unpack.unp_count,
5501 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5502 break;
5503 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5504 iptr->isn_arg.shuffle.shfl_item,
5505 iptr->isn_arg.shuffle.shfl_up);
5506 break;
5507 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5508
5509 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5510 return;
5511 }
5512
5513 out_flush(); // output one line at a time
5514 ui_breakcheck();
5515 if (got_int)
5516 break;
5517 }
5518}
5519
5520/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005521 * Handle command line completion for the :disassemble command.
5522 */
5523 void
5524set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5525{
5526 char_u *p;
5527
5528 // Default: expand user functions, "debug" and "profile"
5529 xp->xp_context = EXPAND_DISASSEMBLE;
5530 xp->xp_pattern = arg;
5531
5532 // first argument already typed: only user function names
5533 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5534 {
5535 xp->xp_context = EXPAND_USER_FUNC;
5536 xp->xp_pattern = skipwhite(p);
5537 }
5538}
5539
5540/*
5541 * Function given to ExpandGeneric() to obtain the list of :disassemble
5542 * arguments.
5543 */
5544 char_u *
5545get_disassemble_argument(expand_T *xp, int idx)
5546{
5547 if (idx == 0)
5548 return (char_u *)"debug";
5549 if (idx == 1)
5550 return (char_u *)"profile";
5551 return get_user_func_name(xp, idx - 2);
5552}
5553
5554/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005555 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005556 * We don't really need this at runtime, but we do have tests that require it,
5557 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005558 */
5559 void
5560ex_disassemble(exarg_T *eap)
5561{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005562 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005563 char_u *fname;
5564 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565 dfunc_T *dfunc;
5566 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005567 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005568 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005569 compiletype_T compile_type = CT_NONE;
5570
5571 if (STRNCMP(arg, "profile", 7) == 0)
5572 {
5573 compile_type = CT_PROFILE;
5574 arg = skipwhite(arg + 7);
5575 }
5576 else if (STRNCMP(arg, "debug", 5) == 0)
5577 {
5578 compile_type = CT_DEBUG;
5579 arg = skipwhite(arg + 5);
5580 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005582 if (STRNCMP(arg, "<lambda>", 8) == 0)
5583 {
5584 arg += 8;
5585 (void)getdigits(&arg);
5586 fname = vim_strnsave(eap->arg, arg - eap->arg);
5587 }
5588 else
5589 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005590 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005591 if (fname == NULL)
5592 {
5593 semsg(_(e_invarg2), eap->arg);
5594 return;
5595 }
5596
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005597 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005598 if (ufunc == NULL)
5599 {
5600 char_u *p = untrans_function_name(fname);
5601
5602 if (p != NULL)
5603 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005604 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005605 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005606 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607 if (ufunc == NULL)
5608 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005609 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005610 return;
5611 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005612 if (func_needs_compiling(ufunc, compile_type)
5613 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005614 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005615 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005616 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005617 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618 return;
5619 }
5620 if (ufunc->uf_name_exp != NULL)
5621 msg((char *)ufunc->uf_name_exp);
5622 else
5623 msg((char *)ufunc->uf_name);
5624
5625 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005626 switch (compile_type)
5627 {
5628 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005629#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005630 instr = dfunc->df_instr_prof;
5631 instr_count = dfunc->df_instr_prof_count;
5632 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005633#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005634 // FALLTHROUGH
5635 case CT_NONE:
5636 instr = dfunc->df_instr;
5637 instr_count = dfunc->df_instr_count;
5638 break;
5639 case CT_DEBUG:
5640 instr = dfunc->df_instr_debug;
5641 instr_count = dfunc->df_instr_debug_count;
5642 break;
5643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644
Bram Moolenaar4c137212021-04-19 16:48:48 +02005645 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005646}
5647
5648/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005649 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005650 * list, etc. Mostly like what JavaScript does, except that empty list and
5651 * empty dictionary are FALSE.
5652 */
5653 int
5654tv2bool(typval_T *tv)
5655{
5656 switch (tv->v_type)
5657 {
5658 case VAR_NUMBER:
5659 return tv->vval.v_number != 0;
5660 case VAR_FLOAT:
5661#ifdef FEAT_FLOAT
5662 return tv->vval.v_float != 0.0;
5663#else
5664 break;
5665#endif
5666 case VAR_PARTIAL:
5667 return tv->vval.v_partial != NULL;
5668 case VAR_FUNC:
5669 case VAR_STRING:
5670 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5671 case VAR_LIST:
5672 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5673 case VAR_DICT:
5674 return tv->vval.v_dict != NULL
5675 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5676 case VAR_BOOL:
5677 case VAR_SPECIAL:
5678 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5679 case VAR_JOB:
5680#ifdef FEAT_JOB_CHANNEL
5681 return tv->vval.v_job != NULL;
5682#else
5683 break;
5684#endif
5685 case VAR_CHANNEL:
5686#ifdef FEAT_JOB_CHANNEL
5687 return tv->vval.v_channel != NULL;
5688#else
5689 break;
5690#endif
5691 case VAR_BLOB:
5692 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5693 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005694 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005695 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005696 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005697 break;
5698 }
5699 return FALSE;
5700}
5701
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005702 void
5703emsg_using_string_as(typval_T *tv, int as_number)
5704{
5705 semsg(_(as_number ? e_using_string_as_number_str
5706 : e_using_string_as_bool_str),
5707 tv->vval.v_string == NULL
5708 ? (char_u *)"" : tv->vval.v_string);
5709}
5710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711/*
5712 * If "tv" is a string give an error and return FAIL.
5713 */
5714 int
5715check_not_string(typval_T *tv)
5716{
5717 if (tv->v_type == VAR_STRING)
5718 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005719 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005720 clear_tv(tv);
5721 return FAIL;
5722 }
5723 return OK;
5724}
5725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005726
5727#endif // FEAT_EVAL