blob: b194e6a2a2e56ada5af39713219a86df31ceb998 [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;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001580 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001581 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 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001589 if (trycmd->tcd_in_catch)
1590 {
1591 // exception inside ":catch", jump to ":finally" once
1592 ectx->ec_iidx = trycmd->tcd_finally_idx;
1593 trycmd->tcd_finally_idx = 0;
1594 }
1595 else
1596 // jump to first ":catch"
1597 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001598 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001599 did_throw = FALSE; // don't come back here until :endtry
1600 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 }
1602 else
1603 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001604 // Not inside try or need to return from current functions.
1605 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001606 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001607 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001608 tv = STACK_TV_BOT(0);
1609 tv->v_type = VAR_NUMBER;
1610 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001611 ++ectx->ec_stack.ga_len;
1612 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001614 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001615 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001616 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001617 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 goto done;
1619 }
1620
Bram Moolenaar4c137212021-04-19 16:48:48 +02001621 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001622 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623 }
1624 continue;
1625 }
1626
Bram Moolenaar4c137212021-04-19 16:48:48 +02001627 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 switch (iptr->isn_type)
1629 {
1630 // execute Ex command line
1631 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001632 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001633 source_cookie_T cookie;
1634
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001635 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001636 // Pass getsourceline to get an error for a missing ":end"
1637 // command.
1638 CLEAR_FIELD(cookie);
1639 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1640 if (do_cmdline(iptr->isn_arg.string,
1641 getsourceline, &cookie,
1642 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1643 == FAIL
1644 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001645 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001646 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 break;
1648
Bram Moolenaar20677332021-06-06 17:02:53 +02001649 // execute Ex command line split at NL characters.
1650 case ISN_EXEC_SPLIT:
1651 {
1652 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001653 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001654
1655 SOURCING_LNUM = iptr->isn_lnum;
1656 CLEAR_FIELD(cookie);
1657 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1658 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001659 line = get_split_sourceline(0, &cookie, 0, 0);
1660 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001661 get_split_sourceline, &cookie,
1662 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1663 == FAIL
1664 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001665 {
1666 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001667 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001668 }
1669 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001670 }
1671 break;
1672
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001673 // Evaluate an expression with legacy syntax, push it onto the
1674 // stack.
1675 case ISN_LEGACY_EVAL:
1676 {
1677 char_u *arg = iptr->isn_arg.string;
1678 int res;
1679 int save_flags = cmdmod.cmod_flags;
1680
1681 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001682 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001683 tv = STACK_TV_BOT(0);
1684 init_tv(tv);
1685 cmdmod.cmod_flags |= CMOD_LEGACY;
1686 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1687 cmdmod.cmod_flags = save_flags;
1688 if (res == FAIL)
1689 goto on_error;
1690 ++ectx->ec_stack.ga_len;
1691 }
1692 break;
1693
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001694 // push typeval VAR_INSTR with instructions to be executed
1695 case ISN_INSTR:
1696 {
1697 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001698 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001699 tv = STACK_TV_BOT(0);
1700 tv->vval.v_instr = ALLOC_ONE(instr_T);
1701 if (tv->vval.v_instr == NULL)
1702 goto on_error;
1703 ++ectx->ec_stack.ga_len;
1704
1705 tv->v_type = VAR_INSTR;
1706 tv->vval.v_instr->instr_ectx = ectx;
1707 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1708 }
1709 break;
1710
Bram Moolenaar4c137212021-04-19 16:48:48 +02001711 // execute :substitute with an expression
1712 case ISN_SUBSTITUTE:
1713 {
1714 subs_T *subs = &iptr->isn_arg.subs;
1715 source_cookie_T cookie;
1716 struct subs_expr_S *save_instr = substitute_instr;
1717 struct subs_expr_S subs_instr;
1718 int res;
1719
1720 subs_instr.subs_ectx = ectx;
1721 subs_instr.subs_instr = subs->subs_instr;
1722 subs_instr.subs_status = OK;
1723 substitute_instr = &subs_instr;
1724
1725 SOURCING_LNUM = iptr->isn_lnum;
1726 // This is very much like ISN_EXEC
1727 CLEAR_FIELD(cookie);
1728 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1729 res = do_cmdline(subs->subs_cmd,
1730 getsourceline, &cookie,
1731 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1732 substitute_instr = save_instr;
1733
1734 if (res == FAIL || did_emsg
1735 || subs_instr.subs_status == FAIL)
1736 goto on_error;
1737 }
1738 break;
1739
1740 case ISN_FINISH:
1741 goto done;
1742
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001743 case ISN_REDIRSTART:
1744 // create a dummy entry for var_redir_str()
1745 if (alloc_redir_lval() == FAIL)
1746 goto on_error;
1747
1748 // The output is stored in growarray "redir_ga" until
1749 // redirection ends.
1750 init_redir_ga();
1751 redir_vname = 1;
1752 break;
1753
1754 case ISN_REDIREND:
1755 {
1756 char_u *res = get_clear_redir_ga();
1757
1758 // End redirection, put redirected text on the stack.
1759 clear_redir_lval();
1760 redir_vname = 0;
1761
1762 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1763 {
1764 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001765 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001766 }
1767 tv = STACK_TV_BOT(0);
1768 tv->v_type = VAR_STRING;
1769 tv->vval.v_string = res;
1770 ++ectx->ec_stack.ga_len;
1771 }
1772 break;
1773
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001774 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001775#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001776 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1777 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001778#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001779 break;
1780
1781 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001782#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001783 {
1784 exarg_T ea;
1785 int res;
1786
1787 CLEAR_FIELD(ea);
1788 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1789 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1790 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1791 --ectx->ec_stack.ga_len;
1792 tv = STACK_TV_BOT(0);
1793 res = cexpr_core(&ea, tv);
1794 clear_tv(tv);
1795 if (res == FAIL)
1796 goto on_error;
1797 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001798#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001799 break;
1800
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001801 // execute Ex command from pieces on the stack
1802 case ISN_EXECCONCAT:
1803 {
1804 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001805 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001806 int pass;
1807 int i;
1808 char_u *cmd = NULL;
1809 char_u *str;
1810
1811 for (pass = 1; pass <= 2; ++pass)
1812 {
1813 for (i = 0; i < count; ++i)
1814 {
1815 tv = STACK_TV_BOT(i - count);
1816 str = tv->vval.v_string;
1817 if (str != NULL && *str != NUL)
1818 {
1819 if (pass == 2)
1820 STRCPY(cmd + len, str);
1821 len += STRLEN(str);
1822 }
1823 if (pass == 2)
1824 clear_tv(tv);
1825 }
1826 if (pass == 1)
1827 {
1828 cmd = alloc(len + 1);
1829 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001830 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001831 len = 0;
1832 }
1833 }
1834
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001835 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001836 do_cmdline_cmd(cmd);
1837 vim_free(cmd);
1838 }
1839 break;
1840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 // execute :echo {string} ...
1842 case ISN_ECHO:
1843 {
1844 int count = iptr->isn_arg.echo.echo_count;
1845 int atstart = TRUE;
1846 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001847 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848
1849 for (idx = 0; idx < count; ++idx)
1850 {
1851 tv = STACK_TV_BOT(idx - count);
1852 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1853 &atstart, &needclr);
1854 clear_tv(tv);
1855 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001856 if (needclr)
1857 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001858 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 }
1860 break;
1861
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001862 // :execute {string} ...
1863 // :echomsg {string} ...
1864 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001865 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001866 case ISN_ECHOMSG:
1867 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001868 {
1869 int count = iptr->isn_arg.number;
1870 garray_T ga;
1871 char_u buf[NUMBUFLEN];
1872 char_u *p;
1873 int len;
1874 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001875 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001876
1877 ga_init2(&ga, 1, 80);
1878 for (idx = 0; idx < count; ++idx)
1879 {
1880 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001881 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001882 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001883 if (tv->v_type == VAR_CHANNEL
1884 || tv->v_type == VAR_JOB)
1885 {
1886 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001887 semsg(_(e_using_invalid_value_as_string_str),
1888 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001889 break;
1890 }
1891 else
1892 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001893 }
1894 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001895 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001896
1897 len = (int)STRLEN(p);
1898 if (ga_grow(&ga, len + 2) == FAIL)
1899 failed = TRUE;
1900 else
1901 {
1902 if (ga.ga_len > 0)
1903 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1904 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1905 ga.ga_len += len;
1906 }
1907 clear_tv(tv);
1908 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001909 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001910 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001911 {
1912 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001913 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001914 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001915
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001916 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001917 {
1918 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001919 {
1920 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001921 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001922 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001923 {
1924 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001925 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001926 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001927 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001928 else
1929 {
1930 msg_sb_eol();
1931 if (iptr->isn_type == ISN_ECHOMSG)
1932 {
1933 msg_attr(ga.ga_data, echo_attr);
1934 out_flush();
1935 }
1936 else
1937 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001938 SOURCING_LNUM = iptr->isn_lnum;
1939 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001940 }
1941 }
1942 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001943 ga_clear(&ga);
1944 }
1945 break;
1946
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 // load local variable or argument
1948 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001949 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001950 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001952 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 break;
1954
1955 // load v: variable
1956 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001957 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001958 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001960 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 break;
1962
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001963 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 case ISN_LOADSCRIPT:
1965 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001966 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 svar_T *sv;
1968
Bram Moolenaar4c137212021-04-19 16:48:48 +02001969 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001970 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001971 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001972 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001973 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001974 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001976 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 }
1978 break;
1979
1980 // load s: variable in old script
1981 case ISN_LOADS:
1982 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001983 hashtab_T *ht = &SCRIPT_VARS(
1984 iptr->isn_arg.loadstore.ls_sid);
1985 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 if (di == NULL)
1989 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001990 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001991 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001992 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 }
1994 else
1995 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001996 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001997 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001999 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 }
2001 }
2002 break;
2003
Bram Moolenaard3aac292020-04-19 14:32:17 +02002004 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002006 case ISN_LOADB:
2007 case ISN_LOADW:
2008 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002010 dictitem_T *di = NULL;
2011 hashtab_T *ht = NULL;
2012 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002013
Bram Moolenaard3aac292020-04-19 14:32:17 +02002014 switch (iptr->isn_type)
2015 {
2016 case ISN_LOADG:
2017 ht = get_globvar_ht();
2018 namespace = 'g';
2019 break;
2020 case ISN_LOADB:
2021 ht = &curbuf->b_vars->dv_hashtab;
2022 namespace = 'b';
2023 break;
2024 case ISN_LOADW:
2025 ht = &curwin->w_vars->dv_hashtab;
2026 namespace = 'w';
2027 break;
2028 case ISN_LOADT:
2029 ht = &curtab->tp_vars->dv_hashtab;
2030 namespace = 't';
2031 break;
2032 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002033 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002034 }
2035 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037 if (di == NULL)
2038 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002039 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002040 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002041 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002042 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 }
2044 else
2045 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002046 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002047 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002049 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 }
2051 }
2052 break;
2053
Bram Moolenaar03290b82020-12-19 16:30:44 +01002054 // load autoload variable
2055 case ISN_LOADAUTO:
2056 {
2057 char_u *name = iptr->isn_arg.string;
2058
Bram Moolenaar4c137212021-04-19 16:48:48 +02002059 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002060 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002061 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002062 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002063 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002064 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002065 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002066 }
2067 break;
2068
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002069 // load g:/b:/w:/t: namespace
2070 case ISN_LOADGDICT:
2071 case ISN_LOADBDICT:
2072 case ISN_LOADWDICT:
2073 case ISN_LOADTDICT:
2074 {
2075 dict_T *d = NULL;
2076
2077 switch (iptr->isn_type)
2078 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002079 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2080 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2081 case ISN_LOADWDICT: d = curwin->w_vars; break;
2082 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002083 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002084 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002085 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002086 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002087 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002088 tv = STACK_TV_BOT(0);
2089 tv->v_type = VAR_DICT;
2090 tv->v_lock = 0;
2091 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002092 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002093 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002094 }
2095 break;
2096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 // load &option
2098 case ISN_LOADOPT:
2099 {
2100 typval_T optval;
2101 char_u *name = iptr->isn_arg.string;
2102
Bram Moolenaara8c17702020-04-01 21:17:24 +02002103 // This is not expected to fail, name is checked during
2104 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002105 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002106 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002107 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002108 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002110 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 }
2112 break;
2113
2114 // load $ENV
2115 case ISN_LOADENV:
2116 {
2117 typval_T optval;
2118 char_u *name = iptr->isn_arg.string;
2119
Bram Moolenaar4c137212021-04-19 16:48:48 +02002120 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002121 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002122 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002123 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002125 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 }
2127 break;
2128
2129 // load @register
2130 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002131 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002132 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 tv = STACK_TV_BOT(0);
2134 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002135 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002136 // This may result in NULL, which should be equivalent to an
2137 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 tv->vval.v_string = get_reg_contents(
2139 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002140 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 break;
2142
2143 // store local variable
2144 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002145 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 tv = STACK_TV_VAR(iptr->isn_arg.number);
2147 clear_tv(tv);
2148 *tv = *STACK_TV_BOT(0);
2149 break;
2150
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002151 // store s: variable in old script
2152 case ISN_STORES:
2153 {
2154 hashtab_T *ht = &SCRIPT_VARS(
2155 iptr->isn_arg.loadstore.ls_sid);
2156 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002157 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002158
Bram Moolenaar4c137212021-04-19 16:48:48 +02002159 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002160 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002161 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002162 else
2163 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002164 SOURCING_LNUM = iptr->isn_lnum;
2165 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002166 {
2167 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002168 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002169 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002170 clear_tv(&di->di_tv);
2171 di->di_tv = *STACK_TV_BOT(0);
2172 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002173 }
2174 break;
2175
2176 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 case ISN_STORESCRIPT:
2178 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002179 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2180 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181
Bram Moolenaar4c137212021-04-19 16:48:48 +02002182 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002183 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002184 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002185 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002186
2187 // "const" and "final" are checked at compile time, locking
2188 // the value needs to be checked here.
2189 SOURCING_LNUM = iptr->isn_lnum;
2190 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002191 {
2192 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002193 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002194 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 clear_tv(sv->sv_tv);
2197 *sv->sv_tv = *STACK_TV_BOT(0);
2198 }
2199 break;
2200
2201 // store option
2202 case ISN_STOREOPT:
2203 {
2204 long n = 0;
2205 char_u *s = NULL;
2206 char *msg;
2207
Bram Moolenaar4c137212021-04-19 16:48:48 +02002208 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 tv = STACK_TV_BOT(0);
2210 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002211 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002213 if (s == NULL)
2214 s = (char_u *)"";
2215 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002217 // must be VAR_NUMBER, CHECKTYPE makes sure
2218 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2220 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002221 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 if (msg != NULL)
2223 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002224 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002226 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 }
2229 break;
2230
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002231 // store $ENV
2232 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002233 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002234 tv = STACK_TV_BOT(0);
2235 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2236 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002237 break;
2238
2239 // store @r
2240 case ISN_STOREREG:
2241 {
2242 int reg = iptr->isn_arg.number;
2243
Bram Moolenaar4c137212021-04-19 16:48:48 +02002244 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002245 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002246 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002247 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002248 }
2249 break;
2250
2251 // store v: variable
2252 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002253 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002254 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2255 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002256 // should not happen, type is checked when compiling
2257 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002258 break;
2259
Bram Moolenaard3aac292020-04-19 14:32:17 +02002260 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002262 case ISN_STOREB:
2263 case ISN_STOREW:
2264 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002266 dictitem_T *di;
2267 hashtab_T *ht;
2268 char_u *name = iptr->isn_arg.string + 2;
2269
Bram Moolenaard3aac292020-04-19 14:32:17 +02002270 switch (iptr->isn_type)
2271 {
2272 case ISN_STOREG:
2273 ht = get_globvar_ht();
2274 break;
2275 case ISN_STOREB:
2276 ht = &curbuf->b_vars->dv_hashtab;
2277 break;
2278 case ISN_STOREW:
2279 ht = &curwin->w_vars->dv_hashtab;
2280 break;
2281 case ISN_STORET:
2282 ht = &curtab->tp_vars->dv_hashtab;
2283 break;
2284 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002285 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002286 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287
Bram Moolenaar4c137212021-04-19 16:48:48 +02002288 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002289 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002291 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 else
2293 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002294 SOURCING_LNUM = iptr->isn_lnum;
2295 if (var_check_permission(di, name) == FAIL)
2296 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 clear_tv(&di->di_tv);
2298 di->di_tv = *STACK_TV_BOT(0);
2299 }
2300 }
2301 break;
2302
Bram Moolenaar03290b82020-12-19 16:30:44 +01002303 // store an autoload variable
2304 case ISN_STOREAUTO:
2305 SOURCING_LNUM = iptr->isn_lnum;
2306 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2307 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002308 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002309 break;
2310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 // store number in local variable
2312 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002313 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 clear_tv(tv);
2315 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002316 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 break;
2318
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002319 // store value in list or dict variable
2320 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002321 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002322 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002323 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002324 typval_T *tv_dest = STACK_TV_BOT(-1);
2325 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002326
Bram Moolenaar752fc692021-01-04 21:57:11 +01002327 // Stack contains:
2328 // -3 value to be stored
2329 // -2 index
2330 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002331 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002332 SOURCING_LNUM = iptr->isn_lnum;
2333 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002334 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002335 dest_type = tv_dest->v_type;
2336 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002337 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002338 else if (dest_type == VAR_LIST
2339 && tv_idx->v_type != VAR_NUMBER)
2340 {
2341 emsg(_(e_number_exp));
2342 status = FAIL;
2343 }
2344 }
2345 else if (dest_type != tv_dest->v_type)
2346 {
2347 // just in case, should be OK
2348 semsg(_(e_expected_str_but_got_str),
2349 vartype_name(dest_type),
2350 vartype_name(tv_dest->v_type));
2351 status = FAIL;
2352 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002353
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002354 if (status == OK && dest_type == VAR_LIST)
2355 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002356 long lidx = (long)tv_idx->vval.v_number;
2357 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002358
2359 if (list == NULL)
2360 {
2361 emsg(_(e_list_not_set));
2362 goto on_error;
2363 }
2364 if (lidx < 0 && list->lv_len + lidx >= 0)
2365 // negative index is relative to the end
2366 lidx = list->lv_len + lidx;
2367 if (lidx < 0 || lidx > list->lv_len)
2368 {
2369 semsg(_(e_listidx), lidx);
2370 goto on_error;
2371 }
2372 if (lidx < list->lv_len)
2373 {
2374 listitem_T *li = list_find(list, lidx);
2375
2376 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002377 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002378 goto on_error;
2379 // overwrite existing list item
2380 clear_tv(&li->li_tv);
2381 li->li_tv = *tv;
2382 }
2383 else
2384 {
2385 if (error_if_locked(list->lv_lock,
2386 e_cannot_change_list))
2387 goto on_error;
2388 // append to list, only fails when out of memory
2389 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002390 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002391 clear_tv(tv);
2392 }
2393 }
2394 else if (status == OK && dest_type == VAR_DICT)
2395 {
2396 char_u *key = tv_idx->vval.v_string;
2397 dict_T *dict = tv_dest->vval.v_dict;
2398 dictitem_T *di;
2399
2400 SOURCING_LNUM = iptr->isn_lnum;
2401 if (dict == NULL)
2402 {
2403 emsg(_(e_dictionary_not_set));
2404 goto on_error;
2405 }
2406 if (key == NULL)
2407 key = (char_u *)"";
2408 di = dict_find(dict, key, -1);
2409 if (di != NULL)
2410 {
2411 if (error_if_locked(di->di_tv.v_lock,
2412 e_cannot_change_dict_item))
2413 goto on_error;
2414 // overwrite existing value
2415 clear_tv(&di->di_tv);
2416 di->di_tv = *tv;
2417 }
2418 else
2419 {
2420 if (error_if_locked(dict->dv_lock,
2421 e_cannot_change_dict))
2422 goto on_error;
2423 // add to dict, only fails when out of memory
2424 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002425 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002426 clear_tv(tv);
2427 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002428 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002429 else if (status == OK && dest_type == VAR_BLOB)
2430 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002431 long lidx = (long)tv_idx->vval.v_number;
2432 blob_T *blob = tv_dest->vval.v_blob;
2433 varnumber_T nr;
2434 int error = FALSE;
2435 int len;
2436
2437 if (blob == NULL)
2438 {
2439 emsg(_(e_blob_not_set));
2440 goto on_error;
2441 }
2442 len = blob_len(blob);
2443 if (lidx < 0 && len + lidx >= 0)
2444 // negative index is relative to the end
2445 lidx = len + lidx;
2446
2447 // Can add one byte at the end.
2448 if (lidx < 0 || lidx > len)
2449 {
2450 semsg(_(e_blobidx), lidx);
2451 goto on_error;
2452 }
2453 if (value_check_lock(blob->bv_lock,
2454 (char_u *)"blob", FALSE))
2455 goto on_error;
2456 nr = tv_get_number_chk(tv, &error);
2457 if (error)
2458 goto on_error;
2459 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002460 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002461 else
2462 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002463 status = FAIL;
2464 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002465 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002466
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002467 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002468 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002469 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002470 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002471 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002472 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002473 goto on_error;
2474 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002475 }
2476 break;
2477
Bram Moolenaar68452172021-04-12 21:21:02 +02002478 // store value in blob range
2479 case ISN_STORERANGE:
2480 {
2481 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2482 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2483 typval_T *tv_dest = STACK_TV_BOT(-1);
2484 int status = OK;
2485
2486 // Stack contains:
2487 // -4 value to be stored
2488 // -3 first index or "none"
2489 // -2 second index or "none"
2490 // -1 destination blob
2491 tv = STACK_TV_BOT(-4);
2492 if (tv_dest->v_type != VAR_BLOB)
2493 {
2494 status = FAIL;
2495 emsg(_(e_blob_required));
2496 }
2497 else
2498 {
2499 varnumber_T n1;
2500 varnumber_T n2;
2501 int error = FALSE;
2502
2503 n1 = tv_get_number_chk(tv_idx1, &error);
2504 if (error)
2505 status = FAIL;
2506 else
2507 {
2508 if (tv_idx2->v_type == VAR_SPECIAL
2509 && tv_idx2->vval.v_number == VVAL_NONE)
2510 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2511 else
2512 n2 = tv_get_number_chk(tv_idx2, &error);
2513 if (error)
2514 status = FAIL;
2515 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002516 {
2517 long bloblen = blob_len(tv_dest->vval.v_blob);
2518
2519 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002520 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002521 || check_blob_range(bloblen,
2522 n1, n2, FALSE) == FAIL)
2523 status = FAIL;
2524 else
2525 status = blob_set_range(
2526 tv_dest->vval.v_blob, n1, n2, tv);
2527 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002528 }
2529 }
2530
2531 clear_tv(tv_idx1);
2532 clear_tv(tv_idx2);
2533 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002534 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002535 clear_tv(tv);
2536
2537 if (status == FAIL)
2538 goto on_error;
2539 }
2540 break;
2541
Bram Moolenaar0186e582021-01-10 18:33:11 +01002542 // load or store variable or argument from outer scope
2543 case ISN_LOADOUTER:
2544 case ISN_STOREOUTER:
2545 {
2546 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002547 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2548 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002549
2550 while (depth > 1 && outer != NULL)
2551 {
2552 outer = outer->out_up;
2553 --depth;
2554 }
2555 if (outer == NULL)
2556 {
2557 SOURCING_LNUM = iptr->isn_lnum;
2558 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002559 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002560 }
2561 tv = ((typval_T *)outer->out_stack->ga_data)
2562 + outer->out_frame_idx + STACK_FRAME_SIZE
2563 + iptr->isn_arg.outer.outer_idx;
2564 if (iptr->isn_type == ISN_LOADOUTER)
2565 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002566 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002567 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002568 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002569 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002570 }
2571 else
2572 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002573 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002574 clear_tv(tv);
2575 *tv = *STACK_TV_BOT(0);
2576 }
2577 }
2578 break;
2579
Bram Moolenaar752fc692021-01-04 21:57:11 +01002580 // unlet item in list or dict variable
2581 case ISN_UNLETINDEX:
2582 {
2583 typval_T *tv_idx = STACK_TV_BOT(-2);
2584 typval_T *tv_dest = STACK_TV_BOT(-1);
2585 int status = OK;
2586
2587 // Stack contains:
2588 // -2 index
2589 // -1 dict or list
2590 if (tv_dest->v_type == VAR_DICT)
2591 {
2592 // unlet a dict item, index must be a string
2593 if (tv_idx->v_type != VAR_STRING)
2594 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002595 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002596 semsg(_(e_expected_str_but_got_str),
2597 vartype_name(VAR_STRING),
2598 vartype_name(tv_idx->v_type));
2599 status = FAIL;
2600 }
2601 else
2602 {
2603 dict_T *d = tv_dest->vval.v_dict;
2604 char_u *key = tv_idx->vval.v_string;
2605 dictitem_T *di = NULL;
2606
2607 if (key == NULL)
2608 key = (char_u *)"";
2609 if (d != NULL)
2610 di = dict_find(d, key, (int)STRLEN(key));
2611 if (di == NULL)
2612 {
2613 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002614 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002615 semsg(_(e_dictkey), key);
2616 status = FAIL;
2617 }
2618 else
2619 {
2620 // TODO: check for dict or item locked
2621 dictitem_remove(d, di);
2622 }
2623 }
2624 }
2625 else if (tv_dest->v_type == VAR_LIST)
2626 {
2627 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002628 SOURCING_LNUM = iptr->isn_lnum;
2629 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002630 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002631 status = FAIL;
2632 }
2633 else
2634 {
2635 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002636 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002637 listitem_T *li = NULL;
2638
2639 li = list_find(l, n);
2640 if (li == NULL)
2641 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002642 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002643 semsg(_(e_listidx), n);
2644 status = FAIL;
2645 }
2646 else
2647 // TODO: check for list or item locked
2648 listitem_remove(l, li);
2649 }
2650 }
2651 else
2652 {
2653 status = FAIL;
2654 semsg(_(e_cannot_index_str),
2655 vartype_name(tv_dest->v_type));
2656 }
2657
2658 clear_tv(tv_idx);
2659 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002660 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002661 if (status == FAIL)
2662 goto on_error;
2663 }
2664 break;
2665
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002666 // unlet range of items in list variable
2667 case ISN_UNLETRANGE:
2668 {
2669 // Stack contains:
2670 // -3 index1
2671 // -2 index2
2672 // -1 dict or list
2673 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2674 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2675 typval_T *tv_dest = STACK_TV_BOT(-1);
2676 int status = OK;
2677
2678 if (tv_dest->v_type == VAR_LIST)
2679 {
2680 // indexes must be a number
2681 SOURCING_LNUM = iptr->isn_lnum;
2682 if (check_for_number(tv_idx1) == FAIL
2683 || check_for_number(tv_idx2) == FAIL)
2684 {
2685 status = FAIL;
2686 }
2687 else
2688 {
2689 list_T *l = tv_dest->vval.v_list;
2690 long n1 = (long)tv_idx1->vval.v_number;
2691 long n2 = (long)tv_idx2->vval.v_number;
2692 listitem_T *li;
2693
2694 li = list_find_index(l, &n1);
2695 if (li == NULL
2696 || list_unlet_range(l, li, NULL, n1,
2697 TRUE, n2) == FAIL)
2698 status = FAIL;
2699 }
2700 }
2701 else
2702 {
2703 status = FAIL;
2704 SOURCING_LNUM = iptr->isn_lnum;
2705 semsg(_(e_cannot_index_str),
2706 vartype_name(tv_dest->v_type));
2707 }
2708
2709 clear_tv(tv_idx1);
2710 clear_tv(tv_idx2);
2711 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002712 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002713 if (status == FAIL)
2714 goto on_error;
2715 }
2716 break;
2717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 // push constant
2719 case ISN_PUSHNR:
2720 case ISN_PUSHBOOL:
2721 case ISN_PUSHSPEC:
2722 case ISN_PUSHF:
2723 case ISN_PUSHS:
2724 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002725 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002726 case ISN_PUSHCHANNEL:
2727 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002728 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002729 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002731 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002732 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 switch (iptr->isn_type)
2734 {
2735 case ISN_PUSHNR:
2736 tv->v_type = VAR_NUMBER;
2737 tv->vval.v_number = iptr->isn_arg.number;
2738 break;
2739 case ISN_PUSHBOOL:
2740 tv->v_type = VAR_BOOL;
2741 tv->vval.v_number = iptr->isn_arg.number;
2742 break;
2743 case ISN_PUSHSPEC:
2744 tv->v_type = VAR_SPECIAL;
2745 tv->vval.v_number = iptr->isn_arg.number;
2746 break;
2747#ifdef FEAT_FLOAT
2748 case ISN_PUSHF:
2749 tv->v_type = VAR_FLOAT;
2750 tv->vval.v_float = iptr->isn_arg.fnumber;
2751 break;
2752#endif
2753 case ISN_PUSHBLOB:
2754 blob_copy(iptr->isn_arg.blob, tv);
2755 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002756 case ISN_PUSHFUNC:
2757 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002758 if (iptr->isn_arg.string == NULL)
2759 tv->vval.v_string = NULL;
2760 else
2761 tv->vval.v_string =
2762 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002763 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002764 case ISN_PUSHCHANNEL:
2765#ifdef FEAT_JOB_CHANNEL
2766 tv->v_type = VAR_CHANNEL;
2767 tv->vval.v_channel = iptr->isn_arg.channel;
2768 if (tv->vval.v_channel != NULL)
2769 ++tv->vval.v_channel->ch_refcount;
2770#endif
2771 break;
2772 case ISN_PUSHJOB:
2773#ifdef FEAT_JOB_CHANNEL
2774 tv->v_type = VAR_JOB;
2775 tv->vval.v_job = iptr->isn_arg.job;
2776 if (tv->vval.v_job != NULL)
2777 ++tv->vval.v_job->jv_refcount;
2778#endif
2779 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 default:
2781 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002782 tv->vval.v_string = vim_strsave(
2783 iptr->isn_arg.string == NULL
2784 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 }
2786 break;
2787
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002788 case ISN_UNLET:
2789 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2790 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002791 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002792 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002793 case ISN_UNLETENV:
2794 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2795 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002796
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002797 case ISN_LOCKCONST:
2798 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2799 break;
2800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 // create a list from items on the stack; uses a single allocation
2802 // for the list header and the items
2803 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002804 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002805 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 break;
2807
2808 // create a dict from items on the stack
2809 case ISN_NEWDICT:
2810 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002811 int count = iptr->isn_arg.number;
2812 dict_T *dict = dict_alloc();
2813 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002814 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002815 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816
2817 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002818 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 for (idx = 0; idx < count; ++idx)
2820 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002821 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002823 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002824 key = tv->vval.v_string == NULL
2825 ? (char_u *)"" : tv->vval.v_string;
2826 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002827 if (item != NULL)
2828 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002829 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002830 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002831 dict_unref(dict);
2832 goto on_error;
2833 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002834 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 clear_tv(tv);
2836 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002837 {
2838 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002839 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2842 item->di_tv.v_lock = 0;
2843 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002844 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002845 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002846 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002847 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 }
2850
2851 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002852 ectx->ec_stack.ga_len -= 2 * count - 1;
2853 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002854 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002856 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 tv = STACK_TV_BOT(-1);
2858 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002859 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 tv->vval.v_dict = dict;
2861 ++dict->dv_refcount;
2862 }
2863 break;
2864
2865 // call a :def function
2866 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002867 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002868 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2869 NULL,
2870 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002871 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002872 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 break;
2874
2875 // call a builtin function
2876 case ISN_BCALL:
2877 SOURCING_LNUM = iptr->isn_lnum;
2878 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2879 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002880 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002881 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 break;
2883
2884 // call a funcref or partial
2885 case ISN_PCALL:
2886 {
2887 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2888 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002889 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890
2891 SOURCING_LNUM = iptr->isn_lnum;
2892 if (pfunc->cpf_top)
2893 {
2894 // funcref is above the arguments
2895 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2896 }
2897 else
2898 {
2899 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002900 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002901 partial_tv = *STACK_TV_BOT(0);
2902 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002904 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002905 if (tv == &partial_tv)
2906 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002908 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 }
2910 break;
2911
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002912 case ISN_PCALL_END:
2913 // PCALL finished, arguments have been consumed and replaced by
2914 // the return value. Now clear the funcref from the stack,
2915 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002916 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002917 clear_tv(STACK_TV_BOT(-1));
2918 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2919 break;
2920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 // call a user defined function or funcref/partial
2922 case ISN_UCALL:
2923 {
2924 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2925
2926 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002927 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002928 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002929 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 }
2931 break;
2932
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002933 // return from a :def function call without a value
2934 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002935 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002936 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002937 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002938 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002939 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002940 tv->vval.v_number = 0;
2941 tv->v_lock = 0;
2942 // FALLTHROUGH
2943
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002944 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 case ISN_RETURN:
2946 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002947 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002948 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002949
2950 if (trystack->ga_len > 0)
2951 trycmd = ((trycmd_T *)trystack->ga_data)
2952 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002953 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002954 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002956 // jump to ":finally" or ":endtry"
2957 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002958 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002959 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002960 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 trycmd->tcd_return = TRUE;
2962 }
2963 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002964 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 }
2966 break;
2967
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002968 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 case ISN_FUNCREF:
2970 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002971 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2972 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2973 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002976 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002977 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002978 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002979 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002980 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002981 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002982 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002983 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002984 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002986 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 tv->vval.v_partial = pt;
2988 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002989 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 }
2991 break;
2992
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002993 // Create a global function from a lambda.
2994 case ISN_NEWFUNC:
2995 {
2996 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2997
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002998 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002999 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003000 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003001 }
3002 break;
3003
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003004 // List functions
3005 case ISN_DEF:
3006 if (iptr->isn_arg.string == NULL)
3007 list_functions(NULL);
3008 else
3009 {
3010 exarg_T ea;
3011
3012 CLEAR_FIELD(ea);
3013 ea.cmd = ea.arg = iptr->isn_arg.string;
3014 define_function(&ea, NULL);
3015 }
3016 break;
3017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 // jump if a condition is met
3019 case ISN_JUMP:
3020 {
3021 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003022 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 int jump = TRUE;
3024
3025 if (when != JUMP_ALWAYS)
3026 {
3027 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003028 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003029 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003030 || when == JUMP_IF_COND_TRUE)
3031 {
3032 SOURCING_LNUM = iptr->isn_lnum;
3033 jump = tv_get_bool_chk(tv, &error);
3034 if (error)
3035 goto on_error;
3036 }
3037 else
3038 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003040 || when == JUMP_AND_KEEP_IF_FALSE
3041 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003043 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 {
3045 // drop the value from the stack
3046 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003047 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 }
3049 }
3050 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003051 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 }
3053 break;
3054
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003055 // Jump if an argument with a default value was already set and not
3056 // v:none.
3057 case ISN_JUMP_IF_ARG_SET:
3058 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3059 if (tv->v_type != VAR_UNKNOWN
3060 && !(tv->v_type == VAR_SPECIAL
3061 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003062 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003063 break;
3064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 // top of a for loop
3066 case ISN_FOR:
3067 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003068 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 typval_T *idxtv =
3070 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3071
Bram Moolenaar4c137212021-04-19 16:48:48 +02003072 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003073 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003074 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003075 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003076 list_T *list = ltv->vval.v_list;
3077
3078 // push the next item from the list
3079 ++idxtv->vval.v_number;
3080 if (list == NULL
3081 || idxtv->vval.v_number >= list->lv_len)
3082 {
3083 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003084 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3085 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003086 }
3087 else if (list->lv_first == &range_list_item)
3088 {
3089 // non-materialized range() list
3090 tv = STACK_TV_BOT(0);
3091 tv->v_type = VAR_NUMBER;
3092 tv->v_lock = 0;
3093 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003095 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003096 }
3097 else
3098 {
3099 listitem_T *li = list_find(list,
3100 idxtv->vval.v_number);
3101
3102 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003103 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003104 }
3105 }
3106 else if (ltv->v_type == VAR_STRING)
3107 {
3108 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003109
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003110 // The index is for the last byte of the previous
3111 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003112 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003113 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003114 {
3115 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003116 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3117 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003118 }
3119 else
3120 {
3121 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3122
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003123 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003124 tv = STACK_TV_BOT(0);
3125 tv->v_type = VAR_STRING;
3126 tv->vval.v_string = vim_strnsave(
3127 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003128 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003129 idxtv->vval.v_number += clen - 1;
3130 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003132 else if (ltv->v_type == VAR_BLOB)
3133 {
3134 blob_T *blob = ltv->vval.v_blob;
3135
3136 // When we get here the first time make a copy of the
3137 // blob, so that the iteration still works when it is
3138 // changed.
3139 if (idxtv->vval.v_number == -1 && blob != NULL)
3140 {
3141 blob_copy(blob, ltv);
3142 blob_unref(blob);
3143 blob = ltv->vval.v_blob;
3144 }
3145
3146 // The index is for the previous byte.
3147 ++idxtv->vval.v_number;
3148 if (blob == NULL
3149 || idxtv->vval.v_number >= blob_len(blob))
3150 {
3151 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003152 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3153 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003154 }
3155 else
3156 {
3157 // Push the next byte from the blob.
3158 tv = STACK_TV_BOT(0);
3159 tv->v_type = VAR_NUMBER;
3160 tv->vval.v_number = blob_get(blob,
3161 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003162 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003163 }
3164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 else
3166 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003167 semsg(_(e_for_loop_on_str_not_supported),
3168 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003169 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 }
3171 }
3172 break;
3173
3174 // start of ":try" block
3175 case ISN_TRY:
3176 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003177 trycmd_T *trycmd = NULL;
3178
Bram Moolenaar4c137212021-04-19 16:48:48 +02003179 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003180 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003181 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3182 + ectx->ec_trystack.ga_len;
3183 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003185 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003186 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3187 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003188 trycmd->tcd_catch_idx =
3189 iptr->isn_arg.try.try_ref->try_catch;
3190 trycmd->tcd_finally_idx =
3191 iptr->isn_arg.try.try_ref->try_finally;
3192 trycmd->tcd_endtry_idx =
3193 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 }
3195 break;
3196
3197 case ISN_PUSHEXC:
3198 if (current_exception == NULL)
3199 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003200 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003202 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003204 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003205 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003207 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003209 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 tv->vval.v_string = vim_strsave(
3211 (char_u *)current_exception->value);
3212 break;
3213
3214 case ISN_CATCH:
3215 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003216 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217
Bram Moolenaar4c137212021-04-19 16:48:48 +02003218 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 if (trystack->ga_len > 0)
3220 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003221 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 + trystack->ga_len - 1;
3223 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003224 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 }
3226 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003227 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 catch_exception(current_exception);
3229 }
3230 break;
3231
Bram Moolenaarc150c092021-02-13 15:02:46 +01003232 case ISN_TRYCONT:
3233 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003234 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003235 trycont_T *trycont = &iptr->isn_arg.trycont;
3236 int i;
3237 trycmd_T *trycmd;
3238 int iidx = trycont->tct_where;
3239
3240 if (trystack->ga_len < trycont->tct_levels)
3241 {
3242 siemsg("TRYCONT: expected %d levels, found %d",
3243 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003244 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003245 }
3246 // Make :endtry jump to any outer try block and the last
3247 // :endtry inside the loop to the loop start.
3248 for (i = trycont->tct_levels; i > 0; --i)
3249 {
3250 trycmd = ((trycmd_T *)trystack->ga_data)
3251 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003252 // Add one to tcd_cont to be able to jump to
3253 // instruction with index zero.
3254 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003255 iidx = trycmd->tcd_finally_idx == 0
3256 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003257 }
3258 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003259 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003260 }
3261 break;
3262
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003263 case ISN_FINALLY:
3264 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003265 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003266 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3267 + trystack->ga_len - 1;
3268
3269 // Reset the index to avoid a return statement jumps here
3270 // again.
3271 trycmd->tcd_finally_idx = 0;
3272 break;
3273 }
3274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 // end of ":try" block
3276 case ISN_ENDTRY:
3277 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003278 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279
3280 if (trystack->ga_len > 0)
3281 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003282 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 --trystack->ga_len;
3285 --trylevel;
3286 trycmd = ((trycmd_T *)trystack->ga_data)
3287 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003288 if (trycmd->tcd_did_throw)
3289 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003290 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 {
3292 // discard the exception
3293 if (caught_stack == current_exception)
3294 caught_stack = caught_stack->caught;
3295 discard_current_exception();
3296 }
3297
3298 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003299 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003300
Bram Moolenaar4c137212021-04-19 16:48:48 +02003301 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003302 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003303 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003304 clear_tv(STACK_TV_BOT(0));
3305 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003306 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003307 // handling :continue: jump to outer try block or
3308 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003309 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 }
3311 }
3312 break;
3313
3314 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003315 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003316 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003317
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003318 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3319 {
3320 // throwing an exception while using "silent!" causes
3321 // the function to abort but not display an error.
3322 tv = STACK_TV_BOT(-1);
3323 clear_tv(tv);
3324 tv->v_type = VAR_NUMBER;
3325 tv->vval.v_number = 0;
3326 goto done;
3327 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003328 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003329 tv = STACK_TV_BOT(0);
3330 if (tv->vval.v_string == NULL
3331 || *skipwhite(tv->vval.v_string) == NUL)
3332 {
3333 vim_free(tv->vval.v_string);
3334 SOURCING_LNUM = iptr->isn_lnum;
3335 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003336 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003337 }
3338
3339 // Inside a "catch" we need to first discard the caught
3340 // exception.
3341 if (trystack->ga_len > 0)
3342 {
3343 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3344 + trystack->ga_len - 1;
3345 if (trycmd->tcd_caught && current_exception != NULL)
3346 {
3347 // discard the exception
3348 if (caught_stack == current_exception)
3349 caught_stack = caught_stack->caught;
3350 discard_current_exception();
3351 trycmd->tcd_caught = FALSE;
3352 }
3353 }
3354
3355 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3356 == FAIL)
3357 {
3358 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003359 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003360 }
3361 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 break;
3364
3365 // compare with special values
3366 case ISN_COMPAREBOOL:
3367 case ISN_COMPARESPECIAL:
3368 {
3369 typval_T *tv1 = STACK_TV_BOT(-2);
3370 typval_T *tv2 = STACK_TV_BOT(-1);
3371 varnumber_T arg1 = tv1->vval.v_number;
3372 varnumber_T arg2 = tv2->vval.v_number;
3373 int res;
3374
3375 switch (iptr->isn_arg.op.op_type)
3376 {
3377 case EXPR_EQUAL: res = arg1 == arg2; break;
3378 case EXPR_NEQUAL: res = arg1 != arg2; break;
3379 default: res = 0; break;
3380 }
3381
Bram Moolenaar4c137212021-04-19 16:48:48 +02003382 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 tv1->v_type = VAR_BOOL;
3384 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3385 }
3386 break;
3387
3388 // Operation with two number arguments
3389 case ISN_OPNR:
3390 case ISN_COMPARENR:
3391 {
3392 typval_T *tv1 = STACK_TV_BOT(-2);
3393 typval_T *tv2 = STACK_TV_BOT(-1);
3394 varnumber_T arg1 = tv1->vval.v_number;
3395 varnumber_T arg2 = tv2->vval.v_number;
3396 varnumber_T res;
3397
3398 switch (iptr->isn_arg.op.op_type)
3399 {
3400 case EXPR_MULT: res = arg1 * arg2; break;
3401 case EXPR_DIV: res = arg1 / arg2; break;
3402 case EXPR_REM: res = arg1 % arg2; break;
3403 case EXPR_SUB: res = arg1 - arg2; break;
3404 case EXPR_ADD: res = arg1 + arg2; break;
3405
3406 case EXPR_EQUAL: res = arg1 == arg2; break;
3407 case EXPR_NEQUAL: res = arg1 != arg2; break;
3408 case EXPR_GREATER: res = arg1 > arg2; break;
3409 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3410 case EXPR_SMALLER: res = arg1 < arg2; break;
3411 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3412 default: res = 0; break;
3413 }
3414
Bram Moolenaar4c137212021-04-19 16:48:48 +02003415 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 if (iptr->isn_type == ISN_COMPARENR)
3417 {
3418 tv1->v_type = VAR_BOOL;
3419 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3420 }
3421 else
3422 tv1->vval.v_number = res;
3423 }
3424 break;
3425
3426 // Computation with two float arguments
3427 case ISN_OPFLOAT:
3428 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003429#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 {
3431 typval_T *tv1 = STACK_TV_BOT(-2);
3432 typval_T *tv2 = STACK_TV_BOT(-1);
3433 float_T arg1 = tv1->vval.v_float;
3434 float_T arg2 = tv2->vval.v_float;
3435 float_T res = 0;
3436 int cmp = FALSE;
3437
3438 switch (iptr->isn_arg.op.op_type)
3439 {
3440 case EXPR_MULT: res = arg1 * arg2; break;
3441 case EXPR_DIV: res = arg1 / arg2; break;
3442 case EXPR_SUB: res = arg1 - arg2; break;
3443 case EXPR_ADD: res = arg1 + arg2; break;
3444
3445 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3446 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3447 case EXPR_GREATER: cmp = arg1 > arg2; break;
3448 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3449 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3450 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3451 default: cmp = 0; break;
3452 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003453 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 if (iptr->isn_type == ISN_COMPAREFLOAT)
3455 {
3456 tv1->v_type = VAR_BOOL;
3457 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3458 }
3459 else
3460 tv1->vval.v_float = res;
3461 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003462#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 break;
3464
3465 case ISN_COMPARELIST:
3466 {
3467 typval_T *tv1 = STACK_TV_BOT(-2);
3468 typval_T *tv2 = STACK_TV_BOT(-1);
3469 list_T *arg1 = tv1->vval.v_list;
3470 list_T *arg2 = tv2->vval.v_list;
3471 int cmp = FALSE;
3472 int ic = iptr->isn_arg.op.op_ic;
3473
3474 switch (iptr->isn_arg.op.op_type)
3475 {
3476 case EXPR_EQUAL: cmp =
3477 list_equal(arg1, arg2, ic, FALSE); break;
3478 case EXPR_NEQUAL: cmp =
3479 !list_equal(arg1, arg2, ic, FALSE); break;
3480 case EXPR_IS: cmp = arg1 == arg2; break;
3481 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3482 default: cmp = 0; break;
3483 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003484 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 clear_tv(tv1);
3486 clear_tv(tv2);
3487 tv1->v_type = VAR_BOOL;
3488 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3489 }
3490 break;
3491
3492 case ISN_COMPAREBLOB:
3493 {
3494 typval_T *tv1 = STACK_TV_BOT(-2);
3495 typval_T *tv2 = STACK_TV_BOT(-1);
3496 blob_T *arg1 = tv1->vval.v_blob;
3497 blob_T *arg2 = tv2->vval.v_blob;
3498 int cmp = FALSE;
3499
3500 switch (iptr->isn_arg.op.op_type)
3501 {
3502 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3503 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3504 case EXPR_IS: cmp = arg1 == arg2; break;
3505 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3506 default: cmp = 0; break;
3507 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003508 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 clear_tv(tv1);
3510 clear_tv(tv2);
3511 tv1->v_type = VAR_BOOL;
3512 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3513 }
3514 break;
3515
3516 // TODO: handle separately
3517 case ISN_COMPARESTRING:
3518 case ISN_COMPAREDICT:
3519 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 case ISN_COMPAREANY:
3521 {
3522 typval_T *tv1 = STACK_TV_BOT(-2);
3523 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003524 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 int ic = iptr->isn_arg.op.op_ic;
3526
Bram Moolenaareb26f432020-09-14 16:50:05 +02003527 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003528 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003530 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 }
3532 break;
3533
3534 case ISN_ADDLIST:
3535 case ISN_ADDBLOB:
3536 {
3537 typval_T *tv1 = STACK_TV_BOT(-2);
3538 typval_T *tv2 = STACK_TV_BOT(-1);
3539
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003540 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 if (iptr->isn_type == ISN_ADDLIST)
3542 eval_addlist(tv1, tv2);
3543 else
3544 eval_addblob(tv1, tv2);
3545 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003546 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 }
3548 break;
3549
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003550 case ISN_LISTAPPEND:
3551 {
3552 typval_T *tv1 = STACK_TV_BOT(-2);
3553 typval_T *tv2 = STACK_TV_BOT(-1);
3554 list_T *l = tv1->vval.v_list;
3555
3556 // add an item to a list
3557 if (l == NULL)
3558 {
3559 SOURCING_LNUM = iptr->isn_lnum;
3560 emsg(_(e_cannot_add_to_null_list));
3561 goto on_error;
3562 }
3563 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003564 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003565 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003566 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003567 }
3568 break;
3569
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003570 case ISN_BLOBAPPEND:
3571 {
3572 typval_T *tv1 = STACK_TV_BOT(-2);
3573 typval_T *tv2 = STACK_TV_BOT(-1);
3574 blob_T *b = tv1->vval.v_blob;
3575 int error = FALSE;
3576 varnumber_T n;
3577
3578 // add a number to a blob
3579 if (b == NULL)
3580 {
3581 SOURCING_LNUM = iptr->isn_lnum;
3582 emsg(_(e_cannot_add_to_null_blob));
3583 goto on_error;
3584 }
3585 n = tv_get_number_chk(tv2, &error);
3586 if (error)
3587 goto on_error;
3588 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003589 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003590 }
3591 break;
3592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 // Computation with two arguments of unknown type
3594 case ISN_OPANY:
3595 {
3596 typval_T *tv1 = STACK_TV_BOT(-2);
3597 typval_T *tv2 = STACK_TV_BOT(-1);
3598 varnumber_T n1, n2;
3599#ifdef FEAT_FLOAT
3600 float_T f1 = 0, f2 = 0;
3601#endif
3602 int error = FALSE;
3603
3604 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3605 {
3606 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3607 {
3608 eval_addlist(tv1, tv2);
3609 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003610 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 break;
3612 }
3613 else if (tv1->v_type == VAR_BLOB
3614 && tv2->v_type == VAR_BLOB)
3615 {
3616 eval_addblob(tv1, tv2);
3617 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003618 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 break;
3620 }
3621 }
3622#ifdef FEAT_FLOAT
3623 if (tv1->v_type == VAR_FLOAT)
3624 {
3625 f1 = tv1->vval.v_float;
3626 n1 = 0;
3627 }
3628 else
3629#endif
3630 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003631 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 n1 = tv_get_number_chk(tv1, &error);
3633 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003634 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635#ifdef FEAT_FLOAT
3636 if (tv2->v_type == VAR_FLOAT)
3637 f1 = n1;
3638#endif
3639 }
3640#ifdef FEAT_FLOAT
3641 if (tv2->v_type == VAR_FLOAT)
3642 {
3643 f2 = tv2->vval.v_float;
3644 n2 = 0;
3645 }
3646 else
3647#endif
3648 {
3649 n2 = tv_get_number_chk(tv2, &error);
3650 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003651 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652#ifdef FEAT_FLOAT
3653 if (tv1->v_type == VAR_FLOAT)
3654 f2 = n2;
3655#endif
3656 }
3657#ifdef FEAT_FLOAT
3658 // if there is a float on either side the result is a float
3659 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3660 {
3661 switch (iptr->isn_arg.op.op_type)
3662 {
3663 case EXPR_MULT: f1 = f1 * f2; break;
3664 case EXPR_DIV: f1 = f1 / f2; break;
3665 case EXPR_SUB: f1 = f1 - f2; break;
3666 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003667 default: SOURCING_LNUM = iptr->isn_lnum;
3668 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003669 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 }
3671 clear_tv(tv1);
3672 clear_tv(tv2);
3673 tv1->v_type = VAR_FLOAT;
3674 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003675 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 }
3677 else
3678#endif
3679 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003680 int failed = FALSE;
3681
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 switch (iptr->isn_arg.op.op_type)
3683 {
3684 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003685 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3686 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003687 goto on_error;
3688 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 case EXPR_SUB: n1 = n1 - n2; break;
3690 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003691 default: n1 = num_modulus(n1, n2, &failed);
3692 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003693 goto on_error;
3694 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 }
3696 clear_tv(tv1);
3697 clear_tv(tv2);
3698 tv1->v_type = VAR_NUMBER;
3699 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003700 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 }
3702 }
3703 break;
3704
3705 case ISN_CONCAT:
3706 {
3707 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3708 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3709 char_u *res;
3710
3711 res = concat_str(str1, str2);
3712 clear_tv(STACK_TV_BOT(-2));
3713 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003714 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 STACK_TV_BOT(-1)->vval.v_string = res;
3716 }
3717 break;
3718
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003719 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003720 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003721 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003722 int is_slice = iptr->isn_type == ISN_STRSLICE;
3723 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003724 char_u *res;
3725
3726 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003727 // string slice: string is at stack-3, first index at
3728 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003729 if (is_slice)
3730 {
3731 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003732 n1 = tv->vval.v_number;
3733 }
3734
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003735 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003736 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003737
Bram Moolenaar4c137212021-04-19 16:48:48 +02003738 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003739 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003740 if (is_slice)
3741 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003742 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003743 else
3744 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003745 // single character (including composing characters).
3746 // If the index is too big or negative the result is
3747 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003748 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003749 vim_free(tv->vval.v_string);
3750 tv->vval.v_string = res;
3751 }
3752 break;
3753
3754 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003755 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003756 case ISN_BLOBINDEX:
3757 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003759 int is_slice = iptr->isn_type == ISN_LISTSLICE
3760 || iptr->isn_type == ISN_BLOBSLICE;
3761 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3762 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003763 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003764 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765
3766 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003767 // list slice: list is at stack-3, indexes at stack-2 and
3768 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003769 // Same for blob.
3770 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771
3772 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003773 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003775
3776 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 {
Bram Moolenaared591872020-08-15 22:14:53 +02003778 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003779 n1 = tv->vval.v_number;
3780 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 }
Bram Moolenaared591872020-08-15 22:14:53 +02003782
Bram Moolenaar4c137212021-04-19 16:48:48 +02003783 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003784 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003785 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003786 if (is_blob)
3787 {
3788 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3789 n1, n2, FALSE, tv) == FAIL)
3790 goto on_error;
3791 }
3792 else
3793 {
3794 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3795 n1, n2, FALSE, tv, TRUE) == FAIL)
3796 goto on_error;
3797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 }
3799 break;
3800
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003801 case ISN_ANYINDEX:
3802 case ISN_ANYSLICE:
3803 {
3804 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3805 typval_T *var1, *var2;
3806 int res;
3807
3808 // index: composite is at stack-2, index at stack-1
3809 // slice: composite is at stack-3, indexes at stack-2 and
3810 // stack-1
3811 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003812 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003813 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3814 goto on_error;
3815 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3816 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003817 res = eval_index_inner(tv, is_slice, var1, var2,
3818 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003819 clear_tv(var1);
3820 if (is_slice)
3821 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003822 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003823 if (res == FAIL)
3824 goto on_error;
3825 }
3826 break;
3827
Bram Moolenaar9af78762020-06-16 11:34:42 +02003828 case ISN_SLICE:
3829 {
3830 list_T *list;
3831 int count = iptr->isn_arg.number;
3832
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003833 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003834 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003835 list = tv->vval.v_list;
3836
3837 // no error for short list, expect it to be checked earlier
3838 if (list != NULL && list->lv_len >= count)
3839 {
3840 list_T *newlist = list_slice(list,
3841 count, list->lv_len - 1);
3842
3843 if (newlist != NULL)
3844 {
3845 list_unref(list);
3846 tv->vval.v_list = newlist;
3847 ++newlist->lv_refcount;
3848 }
3849 }
3850 }
3851 break;
3852
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003853 case ISN_GETITEM:
3854 {
3855 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003856 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003857
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003858 // Get list item: list is at stack-1, push item.
3859 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003860 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
3861 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003862
Bram Moolenaar4c137212021-04-19 16:48:48 +02003863 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003864 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003865 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003866 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003867
3868 // Useful when used in unpack assignment. Reset at
3869 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003870 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003871 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003872 }
3873 break;
3874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 case ISN_MEMBER:
3876 {
3877 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003878 char_u *key;
3879 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003880 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003881
3882 // dict member: dict is at stack-2, key at stack-1
3883 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003884 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003885 dict = tv->vval.v_dict;
3886
3887 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003888 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003889 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003890 if (key == NULL)
3891 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003892
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003893 if ((di = dict_find(dict, key, -1)) == NULL)
3894 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003895 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003896 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003897
3898 // If :silent! is used we will continue, make sure the
3899 // stack contents makes sense.
3900 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003901 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003902 tv = STACK_TV_BOT(-1);
3903 clear_tv(tv);
3904 tv->v_type = VAR_NUMBER;
3905 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003906 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003907 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003908 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003909 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003910 // Clear the dict only after getting the item, to avoid
3911 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003912 tv = STACK_TV_BOT(-1);
3913 temp_tv = *tv;
3914 copy_tv(&di->di_tv, tv);
3915 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003916 }
3917 break;
3918
3919 // dict member with string key
3920 case ISN_STRINGMEMBER:
3921 {
3922 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003924 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925
3926 tv = STACK_TV_BOT(-1);
3927 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3928 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003929 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003931 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 }
3933 dict = tv->vval.v_dict;
3934
3935 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3936 == NULL)
3937 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003938 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003940 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003942 // Clear the dict after getting the item, to avoid that it
3943 // make the item invalid.
3944 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003946 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 }
3948 break;
3949
3950 case ISN_NEGATENR:
3951 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003952 if (tv->v_type != VAR_NUMBER
3953#ifdef FEAT_FLOAT
3954 && tv->v_type != VAR_FLOAT
3955#endif
3956 )
3957 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003958 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003959 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003960 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003961 }
3962#ifdef FEAT_FLOAT
3963 if (tv->v_type == VAR_FLOAT)
3964 tv->vval.v_float = -tv->vval.v_float;
3965 else
3966#endif
3967 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 break;
3969
3970 case ISN_CHECKNR:
3971 {
3972 int error = FALSE;
3973
3974 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003975 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003977 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 (void)tv_get_number_chk(tv, &error);
3979 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003980 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 }
3982 break;
3983
3984 case ISN_CHECKTYPE:
3985 {
3986 checktype_T *ct = &iptr->isn_arg.type;
3987
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003988 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003989 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003990 if (!ectx->ec_where.wt_variable)
3991 ectx->ec_where.wt_index = ct->ct_arg_idx;
3992 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3993 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003994 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003995 if (!ectx->ec_where.wt_variable)
3996 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003997
3998 // number 0 is FALSE, number 1 is TRUE
3999 if (tv->v_type == VAR_NUMBER
4000 && ct->ct_type->tt_type == VAR_BOOL
4001 && (tv->vval.v_number == 0
4002 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004004 tv->v_type = VAR_BOOL;
4005 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004006 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 }
4008 }
4009 break;
4010
Bram Moolenaar9af78762020-06-16 11:34:42 +02004011 case ISN_CHECKLEN:
4012 {
4013 int min_len = iptr->isn_arg.checklen.cl_min_len;
4014 list_T *list = NULL;
4015
4016 tv = STACK_TV_BOT(-1);
4017 if (tv->v_type == VAR_LIST)
4018 list = tv->vval.v_list;
4019 if (list == NULL || list->lv_len < min_len
4020 || (list->lv_len > min_len
4021 && !iptr->isn_arg.checklen.cl_more_OK))
4022 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004023 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004024 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004025 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004026 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004027 }
4028 }
4029 break;
4030
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004031 case ISN_SETTYPE:
4032 {
4033 checktype_T *ct = &iptr->isn_arg.type;
4034
4035 tv = STACK_TV_BOT(-1);
4036 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4037 {
4038 free_type(tv->vval.v_dict->dv_type);
4039 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4040 }
4041 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4042 {
4043 free_type(tv->vval.v_list->lv_type);
4044 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4045 }
4046 }
4047 break;
4048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004050 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 {
4052 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004053 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004055 if (iptr->isn_type == ISN_2BOOL)
4056 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004057 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004058 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004059 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004060 n = !n;
4061 }
4062 else
4063 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004064 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004065 SOURCING_LNUM = iptr->isn_lnum;
4066 n = tv_get_bool_chk(tv, &error);
4067 if (error)
4068 goto on_error;
4069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 clear_tv(tv);
4071 tv->v_type = VAR_BOOL;
4072 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4073 }
4074 break;
4075
4076 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004077 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004078 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004079 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4080 iptr->isn_type == ISN_2STRING_ANY,
4081 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004082 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 break;
4084
Bram Moolenaar08597872020-12-10 19:43:40 +01004085 case ISN_RANGE:
4086 {
4087 exarg_T ea;
4088 char *errormsg;
4089
Bram Moolenaarece0b872021-01-08 20:40:45 +01004090 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004091 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004092 ea.addr_type = ADDR_LINES;
4093 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004094 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004095 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004096 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004097
Bram Moolenaar4c137212021-04-19 16:48:48 +02004098 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004099 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004100 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004101 tv = STACK_TV_BOT(-1);
4102 tv->v_type = VAR_NUMBER;
4103 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004104 if (ea.addr_count == 0)
4105 tv->vval.v_number = curwin->w_cursor.lnum;
4106 else
4107 tv->vval.v_number = ea.line2;
4108 }
4109 break;
4110
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004111 case ISN_PUT:
4112 {
4113 int regname = iptr->isn_arg.put.put_regname;
4114 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4115 char_u *expr = NULL;
4116 int dir = FORWARD;
4117
Bram Moolenaar08597872020-12-10 19:43:40 +01004118 if (lnum < -2)
4119 {
4120 // line number was put on the stack by ISN_RANGE
4121 tv = STACK_TV_BOT(-1);
4122 curwin->w_cursor.lnum = tv->vval.v_number;
4123 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4124 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004125 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004126 }
4127 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004128 // :put! above cursor
4129 dir = BACKWARD;
4130 else if (lnum >= 0)
4131 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004132
4133 if (regname == '=')
4134 {
4135 tv = STACK_TV_BOT(-1);
4136 if (tv->v_type == VAR_STRING)
4137 expr = tv->vval.v_string;
4138 else
4139 {
4140 expr = typval2string(tv, TRUE); // allocates value
4141 clear_tv(tv);
4142 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004143 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004144 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004145 check_cursor();
4146 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4147 vim_free(expr);
4148 }
4149 break;
4150
Bram Moolenaar02194d22020-10-24 23:08:38 +02004151 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004152 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4153 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4154 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4155 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004156 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4157 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004158 break;
4159
Bram Moolenaar02194d22020-10-24 23:08:38 +02004160 case ISN_CMDMOD_REV:
4161 // filter regprog is owned by the instruction, don't free it
4162 cmdmod.cmod_filter_regmatch.regprog = NULL;
4163 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004164 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4165 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004166 break;
4167
Bram Moolenaar792f7862020-11-23 08:31:18 +01004168 case ISN_UNPACK:
4169 {
4170 int count = iptr->isn_arg.unpack.unp_count;
4171 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4172 list_T *l;
4173 listitem_T *li;
4174 int i;
4175
4176 // Check there is a valid list to unpack.
4177 tv = STACK_TV_BOT(-1);
4178 if (tv->v_type != VAR_LIST)
4179 {
4180 SOURCING_LNUM = iptr->isn_lnum;
4181 emsg(_(e_for_argument_must_be_sequence_of_lists));
4182 goto on_error;
4183 }
4184 l = tv->vval.v_list;
4185 if (l == NULL
4186 || l->lv_len < (semicolon ? count - 1 : count))
4187 {
4188 SOURCING_LNUM = iptr->isn_lnum;
4189 emsg(_(e_list_value_does_not_have_enough_items));
4190 goto on_error;
4191 }
4192 else if (!semicolon && l->lv_len > count)
4193 {
4194 SOURCING_LNUM = iptr->isn_lnum;
4195 emsg(_(e_list_value_has_more_items_than_targets));
4196 goto on_error;
4197 }
4198
4199 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004200 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004201 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004202 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004203
4204 // Variable after semicolon gets a list with the remaining
4205 // items.
4206 if (semicolon)
4207 {
4208 list_T *rem_list =
4209 list_alloc_with_items(l->lv_len - count + 1);
4210
4211 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004212 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004213 tv = STACK_TV_BOT(-count);
4214 tv->vval.v_list = rem_list;
4215 ++rem_list->lv_refcount;
4216 tv->v_lock = 0;
4217 li = l->lv_first;
4218 for (i = 0; i < count - 1; ++i)
4219 li = li->li_next;
4220 for (i = 0; li != NULL; ++i)
4221 {
4222 list_set_item(rem_list, i, &li->li_tv);
4223 li = li->li_next;
4224 }
4225 --count;
4226 }
4227
4228 // Produce the values in reverse order, first item last.
4229 li = l->lv_first;
4230 for (i = 0; i < count; ++i)
4231 {
4232 tv = STACK_TV_BOT(-i - 1);
4233 copy_tv(&li->li_tv, tv);
4234 li = li->li_next;
4235 }
4236
4237 list_unref(l);
4238 }
4239 break;
4240
Bram Moolenaarb2049902021-01-24 12:53:53 +01004241 case ISN_PROF_START:
4242 case ISN_PROF_END:
4243 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004244#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004245 funccall_T cookie;
4246 ufunc_T *cur_ufunc =
4247 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004248 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004249
4250 cookie.func = cur_ufunc;
4251 if (iptr->isn_type == ISN_PROF_START)
4252 {
4253 func_line_start(&cookie, iptr->isn_lnum);
4254 // if we get here the instruction is executed
4255 func_line_exec(&cookie);
4256 }
4257 else
4258 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004259#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004260 }
4261 break;
4262
Bram Moolenaare99d4222021-06-13 14:01:26 +02004263 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004264 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004265 break;
4266
Bram Moolenaar389df252020-07-09 21:20:47 +02004267 case ISN_SHUFFLE:
4268 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004269 typval_T tmp_tv;
4270 int item = iptr->isn_arg.shuffle.shfl_item;
4271 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004272
4273 tmp_tv = *STACK_TV_BOT(-item);
4274 for ( ; up > 0 && item > 1; --up)
4275 {
4276 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4277 --item;
4278 }
4279 *STACK_TV_BOT(-item) = tmp_tv;
4280 }
4281 break;
4282
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004284 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004286 ectx->ec_where.wt_index = 0;
4287 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 break;
4289 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004290 continue;
4291
Bram Moolenaard032f342020-07-18 18:13:02 +02004292func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004293 // Restore previous function. If the frame pointer is where we started
4294 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004295 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004296 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004297
Bram Moolenaar4c137212021-04-19 16:48:48 +02004298 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004299 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004300 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004301 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004302
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004303on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004304 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004305 // If "emsg_silent" is set then ignore the error, unless it was set
4306 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004307 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004308 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004309 {
4310 // If a sequence of instructions causes an error while ":silent!"
4311 // was used, restore the stack length and jump ahead to restoring
4312 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004313 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004314 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004315 while (ectx->ec_stack.ga_len
4316 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004317 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004318 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004319 clear_tv(STACK_TV_BOT(0));
4320 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004321 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4322 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004323 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004324 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004325 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004326on_fatal_error:
4327 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004328 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004329 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004330 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 }
4332
4333done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004334 ret = OK;
4335theend:
4336 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4337 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004338}
4339
4340/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004341 * Execute the instructions from a VAR_INSTR typeval and put the result in
4342 * "rettv".
4343 * Return OK or FAIL.
4344 */
4345 int
4346exe_typval_instr(typval_T *tv, typval_T *rettv)
4347{
4348 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4349 isn_T *save_instr = ectx->ec_instr;
4350 int save_iidx = ectx->ec_iidx;
4351 int res;
4352
4353 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4354 res = exec_instructions(ectx);
4355 if (res == OK)
4356 {
4357 *rettv = *STACK_TV_BOT(-1);
4358 --ectx->ec_stack.ga_len;
4359 }
4360
4361 ectx->ec_instr = save_instr;
4362 ectx->ec_iidx = save_iidx;
4363
4364 return res;
4365}
4366
4367/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004368 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4369 * "substitute_instr".
4370 */
4371 char_u *
4372exe_substitute_instr(void)
4373{
4374 ectx_T *ectx = substitute_instr->subs_ectx;
4375 isn_T *save_instr = ectx->ec_instr;
4376 int save_iidx = ectx->ec_iidx;
4377 char_u *res;
4378
4379 ectx->ec_instr = substitute_instr->subs_instr;
4380 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004381 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004382 typval_T *tv = STACK_TV_BOT(-1);
4383
Bram Moolenaar27523602021-06-05 21:36:19 +02004384 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004385 --ectx->ec_stack.ga_len;
4386 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004387 }
4388 else
4389 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004390 substitute_instr->subs_status = FAIL;
4391 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004392 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393
Bram Moolenaar4c137212021-04-19 16:48:48 +02004394 ectx->ec_instr = save_instr;
4395 ectx->ec_iidx = save_iidx;
4396
4397 return res;
4398}
4399
4400/*
4401 * Call a "def" function from old Vim script.
4402 * Return OK or FAIL.
4403 */
4404 int
4405call_def_function(
4406 ufunc_T *ufunc,
4407 int argc_arg, // nr of arguments
4408 typval_T *argv, // arguments
4409 partial_T *partial, // optional partial for context
4410 typval_T *rettv) // return value
4411{
4412 ectx_T ectx; // execution context
4413 int argc = argc_arg;
4414 typval_T *tv;
4415 int idx;
4416 int ret = FAIL;
4417 int defcount = ufunc->uf_args.ga_len - argc;
4418 sctx_T save_current_sctx = current_sctx;
4419 int did_emsg_before = did_emsg_cumul + did_emsg;
4420 int save_suppress_errthrow = suppress_errthrow;
4421 msglist_T **saved_msg_list = NULL;
4422 msglist_T *private_msg_list = NULL;
4423 int save_emsg_silent_def = emsg_silent_def;
4424 int save_did_emsg_def = did_emsg_def;
4425 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004426 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004427
4428// Get pointer to item in the stack.
4429#undef STACK_TV
4430#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4431
4432// Get pointer to item at the bottom of the stack, -1 is the bottom.
4433#undef STACK_TV_BOT
4434#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4435
4436// Get pointer to a local variable on the stack. Negative for arguments.
4437#undef STACK_TV_VAR
4438#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4439
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004440 // Update uf_has_breakpoint if needed.
4441 update_has_breakpoint(ufunc);
4442
Bram Moolenaar4c137212021-04-19 16:48:48 +02004443 if (ufunc->uf_def_status == UF_NOT_COMPILED
4444 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004445 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4446 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004447 == FAIL))
4448 {
4449 if (did_emsg_cumul + did_emsg == did_emsg_before)
4450 semsg(_(e_function_is_not_compiled_str),
4451 printable_func_name(ufunc));
4452 return FAIL;
4453 }
4454
4455 {
4456 // Check the function was really compiled.
4457 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4458 + ufunc->uf_dfunc_idx;
4459 if (INSTRUCTIONS(dfunc) == NULL)
4460 {
4461 iemsg("using call_def_function() on not compiled function");
4462 return FAIL;
4463 }
4464 }
4465
4466 // If depth of calling is getting too high, don't execute the function.
4467 orig_funcdepth = funcdepth_get();
4468 if (funcdepth_increment() == FAIL)
4469 return FAIL;
4470
4471 CLEAR_FIELD(ectx);
4472 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4473 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4474 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4475 {
4476 funcdepth_decrement();
4477 return FAIL;
4478 }
4479 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4480 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4481 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004482 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004483
4484 idx = argc - ufunc->uf_args.ga_len;
4485 if (idx > 0 && ufunc->uf_va_name == NULL)
4486 {
4487 if (idx == 1)
4488 emsg(_(e_one_argument_too_many));
4489 else
4490 semsg(_(e_nr_arguments_too_many), idx);
4491 goto failed_early;
4492 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004493 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4494 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004495 {
4496 if (idx == -1)
4497 emsg(_(e_one_argument_too_few));
4498 else
4499 semsg(_(e_nr_arguments_too_few), -idx);
4500 goto failed_early;
4501 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004502
4503 // Put arguments on the stack, but no more than what the function expects.
4504 // A lambda can be called with more arguments than it uses.
4505 for (idx = 0; idx < argc
4506 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4507 ++idx)
4508 {
4509 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4510 && argv[idx].v_type == VAR_SPECIAL
4511 && argv[idx].vval.v_number == VVAL_NONE)
4512 {
4513 // Use the default value.
4514 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4515 }
4516 else
4517 {
4518 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4519 && check_typval_arg_type(
4520 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4521 goto failed_early;
4522 copy_tv(&argv[idx], STACK_TV_BOT(0));
4523 }
4524 ++ectx.ec_stack.ga_len;
4525 }
4526
4527 // Turn varargs into a list. Empty list if no args.
4528 if (ufunc->uf_va_name != NULL)
4529 {
4530 int vararg_count = argc - ufunc->uf_args.ga_len;
4531
4532 if (vararg_count < 0)
4533 vararg_count = 0;
4534 else
4535 argc -= vararg_count;
4536 if (exe_newlist(vararg_count, &ectx) == FAIL)
4537 goto failed_early;
4538
4539 // Check the type of the list items.
4540 tv = STACK_TV_BOT(-1);
4541 if (ufunc->uf_va_type != NULL
4542 && ufunc->uf_va_type != &t_list_any
4543 && ufunc->uf_va_type->tt_member != &t_any
4544 && tv->vval.v_list != NULL)
4545 {
4546 type_T *expected = ufunc->uf_va_type->tt_member;
4547 listitem_T *li = tv->vval.v_list->lv_first;
4548
4549 for (idx = 0; idx < vararg_count; ++idx)
4550 {
4551 if (check_typval_arg_type(expected, &li->li_tv,
4552 argc + idx + 1) == FAIL)
4553 goto failed_early;
4554 li = li->li_next;
4555 }
4556 }
4557
4558 if (defcount > 0)
4559 // Move varargs list to below missing default arguments.
4560 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4561 --ectx.ec_stack.ga_len;
4562 }
4563
4564 // Make space for omitted arguments, will store default value below.
4565 // Any varargs list goes after them.
4566 if (defcount > 0)
4567 for (idx = 0; idx < defcount; ++idx)
4568 {
4569 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4570 ++ectx.ec_stack.ga_len;
4571 }
4572 if (ufunc->uf_va_name != NULL)
4573 ++ectx.ec_stack.ga_len;
4574
4575 // Frame pointer points to just after arguments.
4576 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4577 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4578
4579 {
4580 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4581 + ufunc->uf_dfunc_idx;
4582 ufunc_T *base_ufunc = dfunc->df_ufunc;
4583
4584 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4585 // by copy_func().
4586 if (partial != NULL || base_ufunc->uf_partial != NULL)
4587 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004588 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4589 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004590 goto failed_early;
4591 if (partial != NULL)
4592 {
4593 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4594 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004595 if (current_ectx->ec_outer_ref != NULL
4596 && current_ectx->ec_outer_ref->or_outer != NULL)
4597 ectx.ec_outer_ref->or_outer =
4598 current_ectx->ec_outer_ref->or_outer;
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 = &partial->pt_outer;
4603 ++partial->pt_refcount;
4604 ectx.ec_outer_ref->or_partial = partial;
4605 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004606 }
4607 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004608 {
4609 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4610 ++base_ufunc->uf_partial->pt_refcount;
4611 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4612 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004613 }
4614 }
4615
4616 // dummy frame entries
4617 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4618 {
4619 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4620 ++ectx.ec_stack.ga_len;
4621 }
4622
4623 {
4624 // Reserve space for local variables and any closure reference count.
4625 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4626 + ufunc->uf_dfunc_idx;
4627
4628 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4629 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4630 ectx.ec_stack.ga_len += dfunc->df_varcount;
4631 if (dfunc->df_has_closure)
4632 {
4633 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4634 STACK_TV_VAR(idx)->vval.v_number = 0;
4635 ++ectx.ec_stack.ga_len;
4636 }
4637
4638 ectx.ec_instr = INSTRUCTIONS(dfunc);
4639 }
4640
4641 // Following errors are in the function, not the caller.
4642 // Commands behave like vim9script.
4643 estack_push_ufunc(ufunc, 1);
4644 current_sctx = ufunc->uf_script_ctx;
4645 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4646
4647 // Use a specific location for storing error messages to be converted to an
4648 // exception.
4649 saved_msg_list = msg_list;
4650 msg_list = &private_msg_list;
4651
4652 // Do turn errors into exceptions.
4653 suppress_errthrow = FALSE;
4654
4655 // Do not delete the function while executing it.
4656 ++ufunc->uf_calls;
4657
4658 // When ":silent!" was used before calling then we still abort the
4659 // function. If ":silent!" is used in the function then we don't.
4660 emsg_silent_def = emsg_silent;
4661 did_emsg_def = 0;
4662
4663 ectx.ec_where.wt_index = 0;
4664 ectx.ec_where.wt_variable = FALSE;
4665
4666 // Execute the instructions until done.
4667 ret = exec_instructions(&ectx);
4668 if (ret == OK)
4669 {
4670 // function finished, get result from the stack.
4671 if (ufunc->uf_ret_type == &t_void)
4672 {
4673 rettv->v_type = VAR_VOID;
4674 }
4675 else
4676 {
4677 tv = STACK_TV_BOT(-1);
4678 *rettv = *tv;
4679 tv->v_type = VAR_UNKNOWN;
4680 }
4681 }
4682
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004683 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004684 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4685 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004686
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004687 // Deal with any remaining closures, they may be in use somewhere.
4688 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004689 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004690 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004691 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4692 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004693
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004694 estack_pop();
4695 current_sctx = save_current_sctx;
4696
Bram Moolenaarc970e422021-03-17 15:03:04 +01004697 // TODO: when is it safe to delete the function if it is no longer used?
4698 --ufunc->uf_calls;
4699
Bram Moolenaar352134b2020-10-17 22:04:08 +02004700 if (*msg_list != NULL && saved_msg_list != NULL)
4701 {
4702 msglist_T **plist = saved_msg_list;
4703
4704 // Append entries from the current msg_list (uncaught exceptions) to
4705 // the saved msg_list.
4706 while (*plist != NULL)
4707 plist = &(*plist)->next;
4708
4709 *plist = *msg_list;
4710 }
4711 msg_list = saved_msg_list;
4712
Bram Moolenaar4c137212021-04-19 16:48:48 +02004713 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004714 {
4715 cmdmod.cmod_filter_regmatch.regprog = NULL;
4716 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004717 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004718 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004719 emsg_silent_def = save_emsg_silent_def;
4720 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004721
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004722failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004723 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4725 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004726 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004729 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004730 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004731 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004732 if (ectx.ec_outer_ref->or_outer_allocated)
4733 vim_free(ectx.ec_outer_ref->or_outer);
4734 partial_unref(ectx.ec_outer_ref->or_partial);
4735 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004736 }
4737
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004738 // Not sure if this is necessary.
4739 suppress_errthrow = save_suppress_errthrow;
4740
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004741 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004742 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004743 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004744 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 return ret;
4746}
4747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004749 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4750 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4751 * "pfx" is prefixed to every line.
4752 */
4753 static void
4754list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4755{
4756 int line_idx = 0;
4757 int prev_current = 0;
4758 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004759 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004760
4761 for (current = 0; current < instr_count; ++current)
4762 {
4763 isn_T *iptr = &instr[current];
4764 char *line;
4765
4766 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004767 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004768 while (line_idx < iptr->isn_lnum
4769 && line_idx < ufunc->uf_lines.ga_len)
4770 {
4771 if (current > prev_current)
4772 {
4773 msg_puts("\n\n");
4774 prev_current = current;
4775 }
4776 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4777 if (line != NULL)
4778 msg(line);
4779 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004780 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4781 {
4782 int first_def_arg = ufunc->uf_args.ga_len
4783 - ufunc->uf_def_args.ga_len;
4784
4785 if (def_arg_idx > 0)
4786 msg_puts("\n\n");
4787 msg_start();
4788 msg_puts(" ");
4789 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4790 first_def_arg + def_arg_idx]);
4791 msg_puts(" = ");
4792 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4793 msg_clr_eos();
4794 msg_end();
4795 }
4796 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004797
4798 switch (iptr->isn_type)
4799 {
4800 case ISN_EXEC:
4801 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4802 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004803 case ISN_EXEC_SPLIT:
4804 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4805 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004806 case ISN_LEGACY_EVAL:
4807 smsg("%s%4d EVAL legacy %s", pfx, current,
4808 iptr->isn_arg.string);
4809 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004810 case ISN_REDIRSTART:
4811 smsg("%s%4d REDIR", pfx, current);
4812 break;
4813 case ISN_REDIREND:
4814 smsg("%s%4d REDIR END%s", pfx, current,
4815 iptr->isn_arg.number ? " append" : "");
4816 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004817 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004818#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004819 smsg("%s%4d CEXPR pre %s", pfx, current,
4820 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004821#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004822 break;
4823 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004824#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004825 {
4826 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4827
4828 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4829 cexpr_get_auname(cer->cer_cmdidx),
4830 cer->cer_forceit ? "!" : "",
4831 cer->cer_cmdline);
4832 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004833#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004834 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004835 case ISN_INSTR:
4836 {
4837 smsg("%s%4d INSTR", pfx, current);
4838 list_instructions(" ", iptr->isn_arg.instr,
4839 INT_MAX, NULL);
4840 msg(" -------------");
4841 }
4842 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004843 case ISN_SUBSTITUTE:
4844 {
4845 subs_T *subs = &iptr->isn_arg.subs;
4846
4847 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4848 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4849 msg(" -------------");
4850 }
4851 break;
4852 case ISN_EXECCONCAT:
4853 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4854 (varnumber_T)iptr->isn_arg.number);
4855 break;
4856 case ISN_ECHO:
4857 {
4858 echo_T *echo = &iptr->isn_arg.echo;
4859
4860 smsg("%s%4d %s %d", pfx, current,
4861 echo->echo_with_white ? "ECHO" : "ECHON",
4862 echo->echo_count);
4863 }
4864 break;
4865 case ISN_EXECUTE:
4866 smsg("%s%4d EXECUTE %lld", pfx, current,
4867 (varnumber_T)(iptr->isn_arg.number));
4868 break;
4869 case ISN_ECHOMSG:
4870 smsg("%s%4d ECHOMSG %lld", pfx, current,
4871 (varnumber_T)(iptr->isn_arg.number));
4872 break;
4873 case ISN_ECHOERR:
4874 smsg("%s%4d ECHOERR %lld", pfx, current,
4875 (varnumber_T)(iptr->isn_arg.number));
4876 break;
4877 case ISN_LOAD:
4878 {
4879 if (iptr->isn_arg.number < 0)
4880 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4881 (varnumber_T)(iptr->isn_arg.number
4882 + STACK_FRAME_SIZE));
4883 else
4884 smsg("%s%4d LOAD $%lld", pfx, current,
4885 (varnumber_T)(iptr->isn_arg.number));
4886 }
4887 break;
4888 case ISN_LOADOUTER:
4889 {
4890 if (iptr->isn_arg.number < 0)
4891 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4892 iptr->isn_arg.outer.outer_depth,
4893 iptr->isn_arg.outer.outer_idx
4894 + STACK_FRAME_SIZE);
4895 else
4896 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4897 iptr->isn_arg.outer.outer_depth,
4898 iptr->isn_arg.outer.outer_idx);
4899 }
4900 break;
4901 case ISN_LOADV:
4902 smsg("%s%4d LOADV v:%s", pfx, current,
4903 get_vim_var_name(iptr->isn_arg.number));
4904 break;
4905 case ISN_LOADSCRIPT:
4906 {
4907 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4908 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4909 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4910 + sref->sref_idx;
4911
4912 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4913 sv->sv_name,
4914 sref->sref_idx,
4915 si->sn_name);
4916 }
4917 break;
4918 case ISN_LOADS:
4919 {
4920 scriptitem_T *si = SCRIPT_ITEM(
4921 iptr->isn_arg.loadstore.ls_sid);
4922
4923 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4924 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4925 }
4926 break;
4927 case ISN_LOADAUTO:
4928 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4929 break;
4930 case ISN_LOADG:
4931 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4932 break;
4933 case ISN_LOADB:
4934 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4935 break;
4936 case ISN_LOADW:
4937 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4938 break;
4939 case ISN_LOADT:
4940 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4941 break;
4942 case ISN_LOADGDICT:
4943 smsg("%s%4d LOAD g:", pfx, current);
4944 break;
4945 case ISN_LOADBDICT:
4946 smsg("%s%4d LOAD b:", pfx, current);
4947 break;
4948 case ISN_LOADWDICT:
4949 smsg("%s%4d LOAD w:", pfx, current);
4950 break;
4951 case ISN_LOADTDICT:
4952 smsg("%s%4d LOAD t:", pfx, current);
4953 break;
4954 case ISN_LOADOPT:
4955 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4956 break;
4957 case ISN_LOADENV:
4958 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4959 break;
4960 case ISN_LOADREG:
4961 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4962 break;
4963
4964 case ISN_STORE:
4965 if (iptr->isn_arg.number < 0)
4966 smsg("%s%4d STORE arg[%lld]", pfx, current,
4967 iptr->isn_arg.number + STACK_FRAME_SIZE);
4968 else
4969 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4970 break;
4971 case ISN_STOREOUTER:
4972 {
4973 if (iptr->isn_arg.number < 0)
4974 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4975 iptr->isn_arg.outer.outer_depth,
4976 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4977 else
4978 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4979 iptr->isn_arg.outer.outer_depth,
4980 iptr->isn_arg.outer.outer_idx);
4981 }
4982 break;
4983 case ISN_STOREV:
4984 smsg("%s%4d STOREV v:%s", pfx, current,
4985 get_vim_var_name(iptr->isn_arg.number));
4986 break;
4987 case ISN_STOREAUTO:
4988 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4989 break;
4990 case ISN_STOREG:
4991 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4992 break;
4993 case ISN_STOREB:
4994 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4995 break;
4996 case ISN_STOREW:
4997 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4998 break;
4999 case ISN_STORET:
5000 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5001 break;
5002 case ISN_STORES:
5003 {
5004 scriptitem_T *si = SCRIPT_ITEM(
5005 iptr->isn_arg.loadstore.ls_sid);
5006
5007 smsg("%s%4d STORES %s in %s", pfx, current,
5008 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5009 }
5010 break;
5011 case ISN_STORESCRIPT:
5012 {
5013 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5014 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5015 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
5016 + sref->sref_idx;
5017
5018 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
5019 sv->sv_name,
5020 sref->sref_idx,
5021 si->sn_name);
5022 }
5023 break;
5024 case ISN_STOREOPT:
5025 smsg("%s%4d STOREOPT &%s", pfx, current,
5026 iptr->isn_arg.storeopt.so_name);
5027 break;
5028 case ISN_STOREENV:
5029 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5030 break;
5031 case ISN_STOREREG:
5032 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
5033 break;
5034 case ISN_STORENR:
5035 smsg("%s%4d STORE %lld in $%d", pfx, current,
5036 iptr->isn_arg.storenr.stnr_val,
5037 iptr->isn_arg.storenr.stnr_idx);
5038 break;
5039
5040 case ISN_STOREINDEX:
5041 smsg("%s%4d STOREINDEX %s", pfx, current,
5042 vartype_name(iptr->isn_arg.vartype));
5043 break;
5044
5045 case ISN_STORERANGE:
5046 smsg("%s%4d STORERANGE", pfx, current);
5047 break;
5048
5049 // constants
5050 case ISN_PUSHNR:
5051 smsg("%s%4d PUSHNR %lld", pfx, current,
5052 (varnumber_T)(iptr->isn_arg.number));
5053 break;
5054 case ISN_PUSHBOOL:
5055 case ISN_PUSHSPEC:
5056 smsg("%s%4d PUSH %s", pfx, current,
5057 get_var_special_name(iptr->isn_arg.number));
5058 break;
5059 case ISN_PUSHF:
5060#ifdef FEAT_FLOAT
5061 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5062#endif
5063 break;
5064 case ISN_PUSHS:
5065 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5066 break;
5067 case ISN_PUSHBLOB:
5068 {
5069 char_u *r;
5070 char_u numbuf[NUMBUFLEN];
5071 char_u *tofree;
5072
5073 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5074 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5075 vim_free(tofree);
5076 }
5077 break;
5078 case ISN_PUSHFUNC:
5079 {
5080 char *name = (char *)iptr->isn_arg.string;
5081
5082 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5083 name == NULL ? "[none]" : name);
5084 }
5085 break;
5086 case ISN_PUSHCHANNEL:
5087#ifdef FEAT_JOB_CHANNEL
5088 {
5089 channel_T *channel = iptr->isn_arg.channel;
5090
5091 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5092 channel == NULL ? 0 : channel->ch_id);
5093 }
5094#endif
5095 break;
5096 case ISN_PUSHJOB:
5097#ifdef FEAT_JOB_CHANNEL
5098 {
5099 typval_T tv;
5100 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005101 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005102
5103 tv.v_type = VAR_JOB;
5104 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005105 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005106 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5107 }
5108#endif
5109 break;
5110 case ISN_PUSHEXC:
5111 smsg("%s%4d PUSH v:exception", pfx, current);
5112 break;
5113 case ISN_UNLET:
5114 smsg("%s%4d UNLET%s %s", pfx, current,
5115 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5116 iptr->isn_arg.unlet.ul_name);
5117 break;
5118 case ISN_UNLETENV:
5119 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5120 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5121 iptr->isn_arg.unlet.ul_name);
5122 break;
5123 case ISN_UNLETINDEX:
5124 smsg("%s%4d UNLETINDEX", pfx, current);
5125 break;
5126 case ISN_UNLETRANGE:
5127 smsg("%s%4d UNLETRANGE", pfx, current);
5128 break;
5129 case ISN_LOCKCONST:
5130 smsg("%s%4d LOCKCONST", pfx, current);
5131 break;
5132 case ISN_NEWLIST:
5133 smsg("%s%4d NEWLIST size %lld", pfx, current,
5134 (varnumber_T)(iptr->isn_arg.number));
5135 break;
5136 case ISN_NEWDICT:
5137 smsg("%s%4d NEWDICT size %lld", pfx, current,
5138 (varnumber_T)(iptr->isn_arg.number));
5139 break;
5140
5141 // function call
5142 case ISN_BCALL:
5143 {
5144 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5145
5146 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5147 internal_func_name(cbfunc->cbf_idx),
5148 cbfunc->cbf_argcount);
5149 }
5150 break;
5151 case ISN_DCALL:
5152 {
5153 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5154 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5155 + cdfunc->cdf_idx;
5156
5157 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5158 df->df_ufunc->uf_name_exp != NULL
5159 ? df->df_ufunc->uf_name_exp
5160 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5161 }
5162 break;
5163 case ISN_UCALL:
5164 {
5165 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5166
5167 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5168 cufunc->cuf_name, cufunc->cuf_argcount);
5169 }
5170 break;
5171 case ISN_PCALL:
5172 {
5173 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5174
5175 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5176 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5177 }
5178 break;
5179 case ISN_PCALL_END:
5180 smsg("%s%4d PCALL end", pfx, current);
5181 break;
5182 case ISN_RETURN:
5183 smsg("%s%4d RETURN", pfx, current);
5184 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005185 case ISN_RETURN_VOID:
5186 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005187 break;
5188 case ISN_FUNCREF:
5189 {
5190 funcref_T *funcref = &iptr->isn_arg.funcref;
5191 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5192 + funcref->fr_func;
5193
5194 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5195 }
5196 break;
5197
5198 case ISN_NEWFUNC:
5199 {
5200 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5201
5202 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5203 newfunc->nf_lambda, newfunc->nf_global);
5204 }
5205 break;
5206
5207 case ISN_DEF:
5208 {
5209 char_u *name = iptr->isn_arg.string;
5210
5211 smsg("%s%4d DEF %s", pfx, current,
5212 name == NULL ? (char_u *)"" : name);
5213 }
5214 break;
5215
5216 case ISN_JUMP:
5217 {
5218 char *when = "?";
5219
5220 switch (iptr->isn_arg.jump.jump_when)
5221 {
5222 case JUMP_ALWAYS:
5223 when = "JUMP";
5224 break;
5225 case JUMP_AND_KEEP_IF_TRUE:
5226 when = "JUMP_AND_KEEP_IF_TRUE";
5227 break;
5228 case JUMP_IF_FALSE:
5229 when = "JUMP_IF_FALSE";
5230 break;
5231 case JUMP_AND_KEEP_IF_FALSE:
5232 when = "JUMP_AND_KEEP_IF_FALSE";
5233 break;
5234 case JUMP_IF_COND_FALSE:
5235 when = "JUMP_IF_COND_FALSE";
5236 break;
5237 case JUMP_IF_COND_TRUE:
5238 when = "JUMP_IF_COND_TRUE";
5239 break;
5240 }
5241 smsg("%s%4d %s -> %d", pfx, current, when,
5242 iptr->isn_arg.jump.jump_where);
5243 }
5244 break;
5245
5246 case ISN_JUMP_IF_ARG_SET:
5247 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5248 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5249 iptr->isn_arg.jump.jump_where);
5250 break;
5251
5252 case ISN_FOR:
5253 {
5254 forloop_T *forloop = &iptr->isn_arg.forloop;
5255
5256 smsg("%s%4d FOR $%d -> %d", pfx, current,
5257 forloop->for_idx, forloop->for_end);
5258 }
5259 break;
5260
5261 case ISN_TRY:
5262 {
5263 try_T *try = &iptr->isn_arg.try;
5264
5265 if (try->try_ref->try_finally == 0)
5266 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5267 pfx, current,
5268 try->try_ref->try_catch,
5269 try->try_ref->try_endtry);
5270 else
5271 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5272 pfx, current,
5273 try->try_ref->try_catch,
5274 try->try_ref->try_finally,
5275 try->try_ref->try_endtry);
5276 }
5277 break;
5278 case ISN_CATCH:
5279 // TODO
5280 smsg("%s%4d CATCH", pfx, current);
5281 break;
5282 case ISN_TRYCONT:
5283 {
5284 trycont_T *trycont = &iptr->isn_arg.trycont;
5285
5286 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5287 trycont->tct_levels,
5288 trycont->tct_levels == 1 ? "" : "s",
5289 trycont->tct_where);
5290 }
5291 break;
5292 case ISN_FINALLY:
5293 smsg("%s%4d FINALLY", pfx, current);
5294 break;
5295 case ISN_ENDTRY:
5296 smsg("%s%4d ENDTRY", pfx, current);
5297 break;
5298 case ISN_THROW:
5299 smsg("%s%4d THROW", pfx, current);
5300 break;
5301
5302 // expression operations on number
5303 case ISN_OPNR:
5304 case ISN_OPFLOAT:
5305 case ISN_OPANY:
5306 {
5307 char *what;
5308 char *ins;
5309
5310 switch (iptr->isn_arg.op.op_type)
5311 {
5312 case EXPR_MULT: what = "*"; break;
5313 case EXPR_DIV: what = "/"; break;
5314 case EXPR_REM: what = "%"; break;
5315 case EXPR_SUB: what = "-"; break;
5316 case EXPR_ADD: what = "+"; break;
5317 default: what = "???"; break;
5318 }
5319 switch (iptr->isn_type)
5320 {
5321 case ISN_OPNR: ins = "OPNR"; break;
5322 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5323 case ISN_OPANY: ins = "OPANY"; break;
5324 default: ins = "???"; break;
5325 }
5326 smsg("%s%4d %s %s", pfx, current, ins, what);
5327 }
5328 break;
5329
5330 case ISN_COMPAREBOOL:
5331 case ISN_COMPARESPECIAL:
5332 case ISN_COMPARENR:
5333 case ISN_COMPAREFLOAT:
5334 case ISN_COMPARESTRING:
5335 case ISN_COMPAREBLOB:
5336 case ISN_COMPARELIST:
5337 case ISN_COMPAREDICT:
5338 case ISN_COMPAREFUNC:
5339 case ISN_COMPAREANY:
5340 {
5341 char *p;
5342 char buf[10];
5343 char *type;
5344
5345 switch (iptr->isn_arg.op.op_type)
5346 {
5347 case EXPR_EQUAL: p = "=="; break;
5348 case EXPR_NEQUAL: p = "!="; break;
5349 case EXPR_GREATER: p = ">"; break;
5350 case EXPR_GEQUAL: p = ">="; break;
5351 case EXPR_SMALLER: p = "<"; break;
5352 case EXPR_SEQUAL: p = "<="; break;
5353 case EXPR_MATCH: p = "=~"; break;
5354 case EXPR_IS: p = "is"; break;
5355 case EXPR_ISNOT: p = "isnot"; break;
5356 case EXPR_NOMATCH: p = "!~"; break;
5357 default: p = "???"; break;
5358 }
5359 STRCPY(buf, p);
5360 if (iptr->isn_arg.op.op_ic == TRUE)
5361 strcat(buf, "?");
5362 switch(iptr->isn_type)
5363 {
5364 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5365 case ISN_COMPARESPECIAL:
5366 type = "COMPARESPECIAL"; break;
5367 case ISN_COMPARENR: type = "COMPARENR"; break;
5368 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5369 case ISN_COMPARESTRING:
5370 type = "COMPARESTRING"; break;
5371 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5372 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5373 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5374 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5375 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5376 default: type = "???"; break;
5377 }
5378
5379 smsg("%s%4d %s %s", pfx, current, type, buf);
5380 }
5381 break;
5382
5383 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5384 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5385
5386 // expression operations
5387 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5388 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5389 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5390 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5391 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5392 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5393 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5394 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5395 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5396 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5397 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5398 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5399 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005400 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5401 iptr->isn_arg.getitem.gi_index,
5402 iptr->isn_arg.getitem.gi_with_op ?
5403 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005404 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5405 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5406 iptr->isn_arg.string); break;
5407 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5408
5409 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5410 case ISN_CHECKTYPE:
5411 {
5412 checktype_T *ct = &iptr->isn_arg.type;
5413 char *tofree;
5414
5415 if (ct->ct_arg_idx == 0)
5416 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5417 type_name(ct->ct_type, &tofree),
5418 (int)ct->ct_off);
5419 else
5420 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5421 type_name(ct->ct_type, &tofree),
5422 (int)ct->ct_off,
5423 (int)ct->ct_arg_idx);
5424 vim_free(tofree);
5425 break;
5426 }
5427 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5428 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5429 iptr->isn_arg.checklen.cl_min_len);
5430 break;
5431 case ISN_SETTYPE:
5432 {
5433 char *tofree;
5434
5435 smsg("%s%4d SETTYPE %s", pfx, current,
5436 type_name(iptr->isn_arg.type.ct_type, &tofree));
5437 vim_free(tofree);
5438 break;
5439 }
5440 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005441 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5442 smsg("%s%4d INVERT %d (!val)", pfx, current,
5443 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005444 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005445 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5446 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005447 break;
5448 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005449 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005450 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005451 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5452 pfx, current,
5453 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005454 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005455 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5456 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005457 break;
5458 case ISN_PUT:
5459 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5460 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005461 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005462 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5463 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005464 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005465 else
5466 smsg("%s%4d PUT %c %ld", pfx, current,
5467 iptr->isn_arg.put.put_regname,
5468 (long)iptr->isn_arg.put.put_lnum);
5469 break;
5470
5471 // TODO: summarize modifiers
5472 case ISN_CMDMOD:
5473 {
5474 char_u *buf;
5475 size_t len = produce_cmdmods(
5476 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5477
5478 buf = alloc(len + 1);
5479 if (buf != NULL)
5480 {
5481 (void)produce_cmdmods(
5482 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5483 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5484 vim_free(buf);
5485 }
5486 break;
5487 }
5488 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5489
5490 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005491 smsg("%s%4d PROFILE START line %d", pfx, current,
5492 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005493 break;
5494
5495 case ISN_PROF_END:
5496 smsg("%s%4d PROFILE END", pfx, current);
5497 break;
5498
Bram Moolenaare99d4222021-06-13 14:01:26 +02005499 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005500 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5501 iptr->isn_arg.debug.dbg_break_lnum + 1,
5502 iptr->isn_lnum,
5503 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005504 break;
5505
Bram Moolenaar4c137212021-04-19 16:48:48 +02005506 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5507 iptr->isn_arg.unpack.unp_count,
5508 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5509 break;
5510 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5511 iptr->isn_arg.shuffle.shfl_item,
5512 iptr->isn_arg.shuffle.shfl_up);
5513 break;
5514 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5515
5516 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5517 return;
5518 }
5519
5520 out_flush(); // output one line at a time
5521 ui_breakcheck();
5522 if (got_int)
5523 break;
5524 }
5525}
5526
5527/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005528 * Handle command line completion for the :disassemble command.
5529 */
5530 void
5531set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5532{
5533 char_u *p;
5534
5535 // Default: expand user functions, "debug" and "profile"
5536 xp->xp_context = EXPAND_DISASSEMBLE;
5537 xp->xp_pattern = arg;
5538
5539 // first argument already typed: only user function names
5540 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5541 {
5542 xp->xp_context = EXPAND_USER_FUNC;
5543 xp->xp_pattern = skipwhite(p);
5544 }
5545}
5546
5547/*
5548 * Function given to ExpandGeneric() to obtain the list of :disassemble
5549 * arguments.
5550 */
5551 char_u *
5552get_disassemble_argument(expand_T *xp, int idx)
5553{
5554 if (idx == 0)
5555 return (char_u *)"debug";
5556 if (idx == 1)
5557 return (char_u *)"profile";
5558 return get_user_func_name(xp, idx - 2);
5559}
5560
5561/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005562 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005563 * We don't really need this at runtime, but we do have tests that require it,
5564 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565 */
5566 void
5567ex_disassemble(exarg_T *eap)
5568{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005569 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005570 char_u *fname;
5571 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005572 dfunc_T *dfunc;
5573 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005574 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005575 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005576 compiletype_T compile_type = CT_NONE;
5577
5578 if (STRNCMP(arg, "profile", 7) == 0)
5579 {
5580 compile_type = CT_PROFILE;
5581 arg = skipwhite(arg + 7);
5582 }
5583 else if (STRNCMP(arg, "debug", 5) == 0)
5584 {
5585 compile_type = CT_DEBUG;
5586 arg = skipwhite(arg + 5);
5587 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005589 if (STRNCMP(arg, "<lambda>", 8) == 0)
5590 {
5591 arg += 8;
5592 (void)getdigits(&arg);
5593 fname = vim_strnsave(eap->arg, arg - eap->arg);
5594 }
5595 else
5596 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005597 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005598 if (fname == NULL)
5599 {
5600 semsg(_(e_invarg2), eap->arg);
5601 return;
5602 }
5603
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005604 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005605 if (ufunc == NULL)
5606 {
5607 char_u *p = untrans_function_name(fname);
5608
5609 if (p != NULL)
5610 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005611 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005612 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005613 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005614 if (ufunc == NULL)
5615 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005616 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005617 return;
5618 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005619 if (func_needs_compiling(ufunc, compile_type)
5620 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005621 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005622 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005623 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005624 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625 return;
5626 }
5627 if (ufunc->uf_name_exp != NULL)
5628 msg((char *)ufunc->uf_name_exp);
5629 else
5630 msg((char *)ufunc->uf_name);
5631
5632 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005633 switch (compile_type)
5634 {
5635 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005636#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005637 instr = dfunc->df_instr_prof;
5638 instr_count = dfunc->df_instr_prof_count;
5639 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005640#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005641 // FALLTHROUGH
5642 case CT_NONE:
5643 instr = dfunc->df_instr;
5644 instr_count = dfunc->df_instr_count;
5645 break;
5646 case CT_DEBUG:
5647 instr = dfunc->df_instr_debug;
5648 instr_count = dfunc->df_instr_debug_count;
5649 break;
5650 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005651
Bram Moolenaar4c137212021-04-19 16:48:48 +02005652 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005653}
5654
5655/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005656 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657 * list, etc. Mostly like what JavaScript does, except that empty list and
5658 * empty dictionary are FALSE.
5659 */
5660 int
5661tv2bool(typval_T *tv)
5662{
5663 switch (tv->v_type)
5664 {
5665 case VAR_NUMBER:
5666 return tv->vval.v_number != 0;
5667 case VAR_FLOAT:
5668#ifdef FEAT_FLOAT
5669 return tv->vval.v_float != 0.0;
5670#else
5671 break;
5672#endif
5673 case VAR_PARTIAL:
5674 return tv->vval.v_partial != NULL;
5675 case VAR_FUNC:
5676 case VAR_STRING:
5677 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5678 case VAR_LIST:
5679 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5680 case VAR_DICT:
5681 return tv->vval.v_dict != NULL
5682 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5683 case VAR_BOOL:
5684 case VAR_SPECIAL:
5685 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5686 case VAR_JOB:
5687#ifdef FEAT_JOB_CHANNEL
5688 return tv->vval.v_job != NULL;
5689#else
5690 break;
5691#endif
5692 case VAR_CHANNEL:
5693#ifdef FEAT_JOB_CHANNEL
5694 return tv->vval.v_channel != NULL;
5695#else
5696 break;
5697#endif
5698 case VAR_BLOB:
5699 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5700 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005701 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005702 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005703 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005704 break;
5705 }
5706 return FALSE;
5707}
5708
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005709 void
5710emsg_using_string_as(typval_T *tv, int as_number)
5711{
5712 semsg(_(as_number ? e_using_string_as_number_str
5713 : e_using_string_as_bool_str),
5714 tv->vval.v_string == NULL
5715 ? (char_u *)"" : tv->vval.v_string);
5716}
5717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005718/*
5719 * If "tv" is a string give an error and return FAIL.
5720 */
5721 int
5722check_not_string(typval_T *tv)
5723{
5724 if (tv->v_type == VAR_STRING)
5725 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005726 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005727 clear_tv(tv);
5728 return FAIL;
5729 }
5730 return OK;
5731}
5732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005733
5734#endif // FEAT_EVAL