blob: 6cc1fc7e9f7913bcd129a935d1fdecb2e7e53688 [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 Moolenaar648594e2021-07-11 17:55:01 +0200200 int res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100201
202 if (dfunc->df_deleted)
203 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100204 // don't use ufunc->uf_name, it may have been freed
205 emsg_funcname(e_func_deleted,
206 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 return FAIL;
208 }
209
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100210#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100211 if (do_profiling == PROF_YES)
212 {
213 if (ga_grow(&profile_info_ga, 1) == OK)
214 {
215 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
216 + profile_info_ga.ga_len;
217 ++profile_info_ga.ga_len;
218 CLEAR_POINTER(info);
219 profile_may_start_func(info, ufunc,
220 (((dfunc_T *)def_functions.ga_data)
221 + ectx->ec_dfunc_idx)->df_ufunc);
222 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100223 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100224#endif
225
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200226 // Update uf_has_breakpoint if needed.
227 update_has_breakpoint(ufunc);
228
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200229 // When debugging and using "cont" switches to the not-debugged
230 // instructions, may need to still compile them.
Bram Moolenaar648594e2021-07-11 17:55:01 +0200231 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)))
232 {
233 res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL);
234
235 // compile_def_function() may cause def_functions.ga_data to change
236 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
237 }
238 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200239 {
240 if (did_emsg_cumul + did_emsg == did_emsg_before)
241 semsg(_(e_function_is_not_compiled_str),
242 printable_func_name(ufunc));
243 return FAIL;
244 }
245
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200246 if (ufunc->uf_va_name != NULL)
247 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200248 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200249 // Stack at time of call with 2 varargs:
250 // normal_arg
251 // optional_arg
252 // vararg_1
253 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200254 // After creating the list:
255 // normal_arg
256 // optional_arg
257 // vararg-list
258 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200259 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200260 // After creating the list
261 // normal_arg
262 // (space for optional_arg)
263 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200264 vararg_count = argcount - ufunc->uf_args.ga_len;
265 if (vararg_count < 0)
266 vararg_count = 0;
267 else
268 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200269 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200270 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200271
272 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200273 }
274
Bram Moolenaarfe270812020-04-11 22:31:27 +0200275 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200276 if (arg_to_add < 0)
277 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200278 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200279 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200280 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200281 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200282 return FAIL;
283 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200284
285 // Reserve space for:
286 // - missing arguments
287 // - stack frame
288 // - local variables
289 // - if needed: a counter for number of closures created in
290 // ectx->ec_funcrefs.
291 varcount = dfunc->df_varcount + dfunc->df_has_closure;
292 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
293 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294 return FAIL;
295
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100296 // If depth of calling is getting too high, don't execute the function.
297 if (funcdepth_increment() == FAIL)
298 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200299 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100300
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100301 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200302 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100303 {
304 floc = ALLOC_ONE(funclocal_T);
305 if (floc == NULL)
306 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200307 *floc = ectx->ec_funclocal;
308 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100309 }
310
Bram Moolenaarfe270812020-04-11 22:31:27 +0200311 // Move the vararg-list to below the missing optional arguments.
312 if (vararg_count > 0 && arg_to_add > 0)
313 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100314
315 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200316 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200317 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200318 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319
320 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100321 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
322 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200323 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200324 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
325 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100326 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100327 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200328 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329
330 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200331 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200333 if (dfunc->df_has_closure)
334 {
335 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
336
337 tv->v_type = VAR_NUMBER;
338 tv->vval.v_number = 0;
339 }
340 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100341
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100342 if (pt != NULL || ufunc->uf_partial != NULL
343 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100344 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200345 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100346
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200347 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100348 return FAIL;
349 if (pt != NULL)
350 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200351 ref->or_outer = &pt->pt_outer;
352 ++pt->pt_refcount;
353 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100354 }
355 else if (ufunc->uf_partial != NULL)
356 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200357 ref->or_outer = &ufunc->uf_partial->pt_outer;
358 ++ufunc->uf_partial->pt_refcount;
359 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100360 }
361 else
362 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200363 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
364 if (ref->or_outer == NULL)
365 {
366 vim_free(ref);
367 return FAIL;
368 }
369 ref->or_outer_allocated = TRUE;
370 ref->or_outer->out_stack = &ectx->ec_stack;
371 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
372 if (ectx->ec_outer_ref != NULL)
373 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100374 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200375 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100376 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100377 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200378 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100379
Bram Moolenaarc970e422021-03-17 15:03:04 +0100380 ++ufunc->uf_calls;
381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382 // Set execution state to the start of the called function.
383 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100384 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100385 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200386 if (entry != NULL)
387 {
388 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200389 // Save the current context so it can be restored on return.
390 entry->es_save_sctx = current_sctx;
391 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200392 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100393
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200394 // Start execution at the first instruction.
395 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396
397 return OK;
398}
399
400// Get pointer to item in the stack.
401#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
402
403/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200404 * Used when returning from a function: Check if any closure is still
405 * referenced. If so then move the arguments and variables to a separate piece
406 * of stack to be used when the closure is called.
407 * When "free_arguments" is TRUE the arguments are to be freed.
408 * Returns FAIL when out of memory.
409 */
410 static int
411handle_closure_in_use(ectx_T *ectx, int free_arguments)
412{
413 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
414 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200415 int argcount;
416 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200417 int idx;
418 typval_T *tv;
419 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200420 garray_T *gap = &ectx->ec_funcrefs;
421 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200422
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200423 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200424 return OK; // function was freed
425 if (dfunc->df_has_closure == 0)
426 return OK; // no closures
427 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
428 closure_count = tv->vval.v_number;
429 if (closure_count == 0)
430 return OK; // no funcrefs created
431
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200432 argcount = ufunc_argcount(dfunc->df_ufunc);
433 top = ectx->ec_frame_idx - argcount;
434
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200435 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200436 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200437 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200438 partial_T *pt;
439 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200440
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200441 if (off < 0)
442 continue; // count is off or already done
443 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200444 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200445 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200446 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200447 int i;
448
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200449 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200450 // unreferenced on return.
451 for (i = 0; i < dfunc->df_varcount; ++i)
452 {
453 typval_T *stv = STACK_TV(ectx->ec_frame_idx
454 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200455 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200456 --refcount;
457 }
458 if (refcount > 1)
459 {
460 closure_in_use = TRUE;
461 break;
462 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200463 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200464 }
465
466 if (closure_in_use)
467 {
468 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
469 typval_T *stack;
470
471 // A closure is using the arguments and/or local variables.
472 // Move them to the called function.
473 if (funcstack == NULL)
474 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200475 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
476 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200477 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
478 funcstack->fs_ga.ga_data = stack;
479 if (stack == NULL)
480 {
481 vim_free(funcstack);
482 return FAIL;
483 }
484
485 // Move or copy the arguments.
486 for (idx = 0; idx < argcount; ++idx)
487 {
488 tv = STACK_TV(top + idx);
489 if (free_arguments)
490 {
491 *(stack + idx) = *tv;
492 tv->v_type = VAR_UNKNOWN;
493 }
494 else
495 copy_tv(tv, stack + idx);
496 }
497 // Move the local variables.
498 for (idx = 0; idx < dfunc->df_varcount; ++idx)
499 {
500 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200501
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200502 // A partial created for a local function, that is also used as a
503 // local variable, has a reference count for the variable, thus
504 // will never go down to zero. When all these refcounts are one
505 // then the funcstack is unused. We need to count how many we have
506 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200507 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
508 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200509 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200510
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200511 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200512 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
513 gap->ga_len - closure_count + i])
514 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200515 }
516
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200517 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200518 tv->v_type = VAR_UNKNOWN;
519 }
520
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200521 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200522 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200523 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
524 - closure_count + idx];
525 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200526 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200527 ++funcstack->fs_refcount;
528 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100529 pt->pt_outer.out_stack = &funcstack->fs_ga;
530 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200531 }
532 }
533 }
534
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200535 for (idx = 0; idx < closure_count; ++idx)
536 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
537 - closure_count + idx]);
538 gap->ga_len -= closure_count;
539 if (gap->ga_len == 0)
540 ga_clear(gap);
541
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200542 return OK;
543}
544
545/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200546 * Called when a partial is freed or its reference count goes down to one. The
547 * funcstack may be the only reference to the partials in the local variables.
548 * Go over all of them, the funcref and can be freed if all partials
549 * referencing the funcstack have a reference count of one.
550 */
551 void
552funcstack_check_refcount(funcstack_T *funcstack)
553{
554 int i;
555 garray_T *gap = &funcstack->fs_ga;
556 int done = 0;
557
558 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
559 return;
560 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
561 {
562 typval_T *tv = ((typval_T *)gap->ga_data) + i;
563
564 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
565 && tv->vval.v_partial->pt_funcstack == funcstack
566 && tv->vval.v_partial->pt_refcount == 1)
567 ++done;
568 }
569 if (done == funcstack->fs_min_refcount)
570 {
571 typval_T *stack = gap->ga_data;
572
573 // All partials referencing the funcstack have a reference count of
574 // one, thus the funcstack is no longer of use.
575 for (i = 0; i < gap->ga_len; ++i)
576 clear_tv(stack + i);
577 vim_free(stack);
578 vim_free(funcstack);
579 }
580}
581
582/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 * Return from the current function.
584 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200585 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200586func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100589 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200590 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
591 + ectx->ec_dfunc_idx;
592 int argcount = ufunc_argcount(dfunc->df_ufunc);
593 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200594 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100595 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
596 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200597 funclocal_T *floc;
598#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100599 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
600 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601
Bram Moolenaar12d26532021-02-19 19:13:21 +0100602 if (do_profiling == PROF_YES)
603 {
604 ufunc_T *caller = prev_dfunc->df_ufunc;
605
606 if (dfunc->df_ufunc->uf_profiling
607 || (caller != NULL && caller->uf_profiling))
608 {
609 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
610 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
611 --profile_info_ga.ga_len;
612 }
613 }
614#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100615 // TODO: when is it safe to delete the function when it is no longer used?
616 --dfunc->df_ufunc->uf_calls;
617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200619 entry = estack_pop();
620 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200621 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200623 if (handle_closure_in_use(ectx, TRUE) == FAIL)
624 return FAIL;
625
626 // Clear the arguments.
627 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
628 clear_tv(STACK_TV(idx));
629
630 // Clear local variables and temp values, but not the return value.
631 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100632 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100634
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100635 // The return value should be on top of the stack. However, when aborting
636 // it may not be there and ec_frame_idx is the top of the stack.
637 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100638 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100639 ret_idx = 0;
640
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200641 if (ectx->ec_outer_ref != NULL)
642 {
643 if (ectx->ec_outer_ref->or_outer_allocated)
644 vim_free(ectx->ec_outer_ref->or_outer);
645 partial_unref(ectx->ec_outer_ref->or_partial);
646 vim_free(ectx->ec_outer_ref);
647 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100648
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100649 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100650 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100651 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
652 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200653 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
654 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200655 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100656 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100657 floc = (void *)STACK_TV(ectx->ec_frame_idx
658 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200659 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100660 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
661 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200662
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100663 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200664 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100665 else
666 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200667 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100668 vim_free(floc);
669 }
670
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100671 if (ret_idx > 0)
672 {
673 // Reset the stack to the position before the call, with a spot for the
674 // return value, moved there from above the frame.
675 ectx->ec_stack.ga_len = top + 1;
676 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
677 }
678 else
679 // Reset the stack to the position before the call.
680 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200681
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100682 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200683 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200684 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685}
686
687#undef STACK_TV
688
689/*
690 * Prepare arguments and rettv for calling a builtin or user function.
691 */
692 static int
693call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
694{
695 int idx;
696 typval_T *tv;
697
698 // Move arguments from bottom of the stack to argvars[] and add terminator.
699 for (idx = 0; idx < argcount; ++idx)
700 argvars[idx] = *STACK_TV_BOT(idx - argcount);
701 argvars[argcount].v_type = VAR_UNKNOWN;
702
703 // Result replaces the arguments on the stack.
704 if (argcount > 0)
705 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200706 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 return FAIL;
708 else
709 ++ectx->ec_stack.ga_len;
710
711 // Default return value is zero.
712 tv = STACK_TV_BOT(-1);
713 tv->v_type = VAR_NUMBER;
714 tv->vval.v_number = 0;
715
716 return OK;
717}
718
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200719// Ugly global to avoid passing the execution context around through many
720// layers.
721static ectx_T *current_ectx = NULL;
722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723/*
724 * Call a builtin function by index.
725 */
726 static int
727call_bfunc(int func_idx, int argcount, ectx_T *ectx)
728{
729 typval_T argvars[MAX_FUNC_ARGS];
730 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100731 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200732 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733
734 if (call_prepare(argcount, argvars, ectx) == FAIL)
735 return FAIL;
736
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200737 // Call the builtin function. Set "current_ectx" so that when it
738 // recursively invokes call_def_function() a closure context can be set.
739 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100740 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200741 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742
743 // Clear the arguments.
744 for (idx = 0; idx < argcount; ++idx)
745 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200746
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100747 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200748 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 return OK;
750}
751
752/*
753 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100754 * If the function is compiled this will add a stack frame and set the
755 * instruction pointer at the start of the function.
756 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200757 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100758 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 */
760 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100761call_ufunc(
762 ufunc_T *ufunc,
763 partial_T *pt,
764 int argcount,
765 ectx_T *ectx,
766 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767{
768 typval_T argvars[MAX_FUNC_ARGS];
769 funcexe_T funcexe;
770 int error;
771 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100772 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200773 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774
Bram Moolenaare99d4222021-06-13 14:01:26 +0200775 if (func_needs_compiling(ufunc, compile_type)
776 && compile_def_function(ufunc, FALSE, compile_type, NULL)
777 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200778 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200779 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100780 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100781 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100782 if (error != FCERR_UNKNOWN)
783 {
784 if (error == FCERR_TOOMANY)
785 semsg(_(e_toomanyarg), ufunc->uf_name);
786 else
787 semsg(_(e_toofewarg), ufunc->uf_name);
788 return FAIL;
789 }
790
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100791 // The function has been compiled, can call it quickly. For a function
792 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100793 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100794 if (iptr != NULL)
795 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100796 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100797 iptr->isn_type = ISN_DCALL;
798 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
799 iptr->isn_arg.dfunc.cdf_argcount = argcount;
800 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200801 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803
804 if (call_prepare(argcount, argvars, ectx) == FAIL)
805 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200806 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807 funcexe.evaluate = TRUE;
808
809 // Call the user function. Result goes in last position on the stack.
810 // TODO: add selfdict if there is one
811 error = call_user_func_check(ufunc, argcount, argvars,
812 STACK_TV_BOT(-1), &funcexe, NULL);
813
814 // Clear the arguments.
815 for (idx = 0; idx < argcount; ++idx)
816 clear_tv(&argvars[idx]);
817
818 if (error != FCERR_NONE)
819 {
820 user_func_error(error, ufunc->uf_name);
821 return FAIL;
822 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100823 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200824 // Error other than from calling the function itself.
825 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 return OK;
827}
828
829/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100830 * If command modifiers were applied restore them.
831 */
832 static void
833may_restore_cmdmod(funclocal_T *funclocal)
834{
835 if (funclocal->floc_restore_cmdmod)
836 {
837 cmdmod.cmod_filter_regmatch.regprog = NULL;
838 undo_cmdmod(&cmdmod);
839 cmdmod = funclocal->floc_save_cmdmod;
840 funclocal->floc_restore_cmdmod = FALSE;
841 }
842}
843
844/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200845 * Return TRUE if an error was given or CTRL-C was pressed.
846 */
847 static int
848vim9_aborting(int prev_called_emsg)
849{
850 return called_emsg > prev_called_emsg || got_int || did_throw;
851}
852
853/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854 * Execute a function by "name".
855 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100856 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 * Returns FAIL if not found without an error message.
858 */
859 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100860call_by_name(
861 char_u *name,
862 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100863 ectx_T *ectx,
864 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865{
866 ufunc_T *ufunc;
867
868 if (builtin_function(name, -1))
869 {
870 int func_idx = find_internal_func(name);
871
872 if (func_idx < 0)
873 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200874 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875 return FAIL;
876 return call_bfunc(func_idx, argcount, ectx);
877 }
878
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200879 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200880
881 if (ufunc == NULL)
882 {
883 int called_emsg_before = called_emsg;
884
885 if (script_autoload(name, TRUE))
886 // loaded a package, search for the function again
887 ufunc = find_func(name, FALSE, NULL);
888 if (vim9_aborting(called_emsg_before))
889 return FAIL; // bail out if loading the script caused an error
890 }
891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100893 {
894 if (ufunc->uf_arg_types != NULL)
895 {
896 int i;
897 typval_T *argv = STACK_TV_BOT(0) - argcount;
898
899 // The function can change at runtime, check that the argument
900 // types are correct.
901 for (i = 0; i < argcount; ++i)
902 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100903 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100904
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100905 if (i < ufunc->uf_args.ga_len)
906 type = ufunc->uf_arg_types[i];
907 else if (ufunc->uf_va_type != NULL)
908 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100909 if (type != NULL && check_typval_arg_type(type,
910 &argv[i], i + 1) == FAIL)
911 return FAIL;
912 }
913 }
914
Bram Moolenaar4c137212021-04-19 16:48:48 +0200915 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917
918 return FAIL;
919}
920
921 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100922call_partial(
923 typval_T *tv,
924 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100925 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200927 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200928 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100930 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931
932 if (tv->v_type == VAR_PARTIAL)
933 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200934 partial_T *pt = tv->vval.v_partial;
935 int i;
936
937 if (pt->pt_argc > 0)
938 {
939 // Make space for arguments from the partial, shift the "argcount"
940 // arguments up.
941 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
942 return FAIL;
943 for (i = 1; i <= argcount; ++i)
944 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
945 ectx->ec_stack.ga_len += pt->pt_argc;
946 argcount += pt->pt_argc;
947
948 // copy the arguments from the partial onto the stack
949 for (i = 0; i < pt->pt_argc; ++i)
950 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952
953 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200954 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 name = pt->pt_name;
957 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200958 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200960 if (name != NULL)
961 {
962 char_u fname_buf[FLEN_FIXED + 1];
963 char_u *tofree = NULL;
964 int error = FCERR_NONE;
965 char_u *fname;
966
967 // May need to translate <SNR>123_ to K_SNR.
968 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
969 if (error != FCERR_NONE)
970 res = FAIL;
971 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200972 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200973 vim_free(tofree);
974 }
975
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100976 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 {
978 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200979 semsg(_(e_unknownfunc),
980 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 return FAIL;
982 }
983 return OK;
984}
985
986/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200987 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
988 * TRUE.
989 */
990 static int
991error_if_locked(int lock, char *error)
992{
993 if (lock & (VAR_LOCKED | VAR_FIXED))
994 {
995 emsg(_(error));
996 return TRUE;
997 }
998 return FALSE;
999}
1000
1001/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001002 * Give an error if "tv" is not a number and return FAIL.
1003 */
1004 static int
1005check_for_number(typval_T *tv)
1006{
1007 if (tv->v_type != VAR_NUMBER)
1008 {
1009 semsg(_(e_expected_str_but_got_str),
1010 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1011 return FAIL;
1012 }
1013 return OK;
1014}
1015
1016/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001017 * Store "tv" in variable "name".
1018 * This is for s: and g: variables.
1019 */
1020 static void
1021store_var(char_u *name, typval_T *tv)
1022{
1023 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001024 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001025
Bram Moolenaard877a572021-04-01 19:42:48 +02001026 if (tv->v_lock)
1027 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001028 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001029 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001030 restore_funccal();
1031}
1032
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001033/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001034 * Convert "tv" to a string.
1035 * Return FAIL if not allowed.
1036 */
1037 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001038do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001039{
1040 if (tv->v_type != VAR_STRING)
1041 {
1042 char_u *str;
1043
1044 if (is_2string_any)
1045 {
1046 switch (tv->v_type)
1047 {
1048 case VAR_SPECIAL:
1049 case VAR_BOOL:
1050 case VAR_NUMBER:
1051 case VAR_FLOAT:
1052 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001053
1054 case VAR_LIST:
1055 if (tolerant)
1056 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001057 char_u *s, *e, *p;
1058 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001059
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001060 ga_init2(&ga, sizeof(char_u *), 1);
1061
1062 // Convert to NL separated items, then
1063 // escape the items and replace the NL with
1064 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001065 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001066 if (str == NULL)
1067 return FAIL;
1068 s = str;
1069 while ((e = vim_strchr(s, '\n')) != NULL)
1070 {
1071 *e = NUL;
1072 p = vim_strsave_fnameescape(s, FALSE);
1073 if (p != NULL)
1074 {
1075 ga_concat(&ga, p);
1076 ga_concat(&ga, (char_u *)" ");
1077 vim_free(p);
1078 }
1079 s = e + 1;
1080 }
1081 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001082 clear_tv(tv);
1083 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001084 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001085 return OK;
1086 }
1087 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001088 default: to_string_error(tv->v_type);
1089 return FAIL;
1090 }
1091 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001092 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001093 clear_tv(tv);
1094 tv->v_type = VAR_STRING;
1095 tv->vval.v_string = str;
1096 }
1097 return OK;
1098}
1099
1100/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001101 * When the value of "sv" is a null list of dict, allocate it.
1102 */
1103 static void
1104allocate_if_null(typval_T *tv)
1105{
1106 switch (tv->v_type)
1107 {
1108 case VAR_LIST:
1109 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001110 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001111 break;
1112 case VAR_DICT:
1113 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001114 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001115 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001116 case VAR_BLOB:
1117 if (tv->vval.v_blob == NULL)
1118 (void)rettv_blob_alloc(tv);
1119 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001120 default:
1121 break;
1122 }
1123}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001124
Bram Moolenaare7525c52021-01-09 13:20:37 +01001125/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001126 * Return the character "str[index]" where "index" is the character index,
1127 * including composing characters.
1128 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001129 */
1130 char_u *
1131char_from_string(char_u *str, varnumber_T index)
1132{
1133 size_t nbyte = 0;
1134 varnumber_T nchar = index;
1135 size_t slen;
1136
1137 if (str == NULL)
1138 return NULL;
1139 slen = STRLEN(str);
1140
Bram Moolenaarff871402021-03-26 13:34:05 +01001141 // Do the same as for a list: a negative index counts from the end.
1142 // Optimization to check the first byte to be below 0x80 (and no composing
1143 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001144 if (index < 0)
1145 {
1146 int clen = 0;
1147
1148 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001149 {
1150 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1151 ++nbyte;
1152 else if (enc_utf8)
1153 nbyte += utfc_ptr2len(str + nbyte);
1154 else
1155 nbyte += mb_ptr2len(str + nbyte);
1156 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001157 nchar = clen + index;
1158 if (nchar < 0)
1159 // unlike list: index out of range results in empty string
1160 return NULL;
1161 }
1162
1163 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001164 {
1165 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1166 ++nbyte;
1167 else if (enc_utf8)
1168 nbyte += utfc_ptr2len(str + nbyte);
1169 else
1170 nbyte += mb_ptr2len(str + nbyte);
1171 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001172 if (nbyte >= slen)
1173 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001174 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001175}
1176
1177/*
1178 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001179 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001180 * If going over the end return "str_len".
1181 * If "idx" is negative count from the end, -1 is the last character.
1182 * When going over the start return -1.
1183 */
1184 static long
1185char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1186{
1187 varnumber_T nchar = idx;
1188 size_t nbyte = 0;
1189
1190 if (nchar >= 0)
1191 {
1192 while (nchar > 0 && nbyte < str_len)
1193 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001194 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001195 --nchar;
1196 }
1197 }
1198 else
1199 {
1200 nbyte = str_len;
1201 while (nchar < 0 && nbyte > 0)
1202 {
1203 --nbyte;
1204 nbyte -= mb_head_off(str, str + nbyte);
1205 ++nchar;
1206 }
1207 if (nchar < 0)
1208 return -1;
1209 }
1210 return (long)nbyte;
1211}
1212
1213/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001214 * Return the slice "str[first : last]" using character indexes. Composing
1215 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001216 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001217 * Return NULL when the result is empty.
1218 */
1219 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001220string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001221{
1222 long start_byte, end_byte;
1223 size_t slen;
1224
1225 if (str == NULL)
1226 return NULL;
1227 slen = STRLEN(str);
1228 start_byte = char_idx2byte(str, slen, first);
1229 if (start_byte < 0)
1230 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001231 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001232 end_byte = (long)slen;
1233 else
1234 {
1235 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001236 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001237 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001238 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001239 }
1240
1241 if (start_byte >= (long)slen || end_byte <= start_byte)
1242 return NULL;
1243 return vim_strnsave(str + start_byte, end_byte - start_byte);
1244}
1245
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001246 static svar_T *
1247get_script_svar(scriptref_T *sref, ectx_T *ectx)
1248{
1249 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1250 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1251 + ectx->ec_dfunc_idx;
1252 svar_T *sv;
1253
1254 if (sref->sref_seq != si->sn_script_seq)
1255 {
1256 // The script was reloaded after the function was
1257 // compiled, the script_idx may not be valid.
1258 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1259 dfunc->df_ufunc->uf_name_exp);
1260 return NULL;
1261 }
1262 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1263 if (!equal_type(sv->sv_type, sref->sref_type))
1264 {
1265 emsg(_(e_script_variable_type_changed));
1266 return NULL;
1267 }
1268 return sv;
1269}
1270
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001271/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001272 * Function passed to do_cmdline() for splitting a script joined by NL
1273 * characters.
1274 */
1275 static char_u *
1276get_split_sourceline(
1277 int c UNUSED,
1278 void *cookie,
1279 int indent UNUSED,
1280 getline_opt_T options UNUSED)
1281{
1282 source_cookie_T *sp = (source_cookie_T *)cookie;
1283 char_u *p;
1284 char_u *line;
1285
1286 if (*sp->nextline == NUL)
1287 return NULL;
1288 p = vim_strchr(sp->nextline, '\n');
1289 if (p == NULL)
1290 {
1291 line = vim_strsave(sp->nextline);
1292 sp->nextline += STRLEN(sp->nextline);
1293 }
1294 else
1295 {
1296 line = vim_strnsave(sp->nextline, p - sp->nextline);
1297 sp->nextline = p + 1;
1298 }
1299 return line;
1300}
1301
1302/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 * Execute a function by "name".
1304 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001305 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 */
1307 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001308call_eval_func(
1309 char_u *name,
1310 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001311 ectx_T *ectx,
1312 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313{
Bram Moolenaared677f52020-08-12 16:38:10 +02001314 int called_emsg_before = called_emsg;
1315 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316
Bram Moolenaar4c137212021-04-19 16:48:48 +02001317 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001318 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001320 dictitem_T *v;
1321
1322 v = find_var(name, NULL, FALSE);
1323 if (v == NULL)
1324 {
1325 semsg(_(e_unknownfunc), name);
1326 return FAIL;
1327 }
1328 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1329 {
1330 semsg(_(e_unknownfunc), name);
1331 return FAIL;
1332 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001333 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001335 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336}
1337
1338/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001339 * When a function reference is used, fill a partial with the information
1340 * needed, especially when it is used as a closure.
1341 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001342 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001343fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1344{
1345 pt->pt_func = ufunc;
1346 pt->pt_refcount = 1;
1347
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001348 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001349 {
1350 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1351 + ectx->ec_dfunc_idx;
1352
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001353 // The closure may need to find arguments and local variables in the
1354 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001355 pt->pt_outer.out_stack = &ectx->ec_stack;
1356 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001357 if (ectx->ec_outer_ref != NULL)
1358 {
1359 // The current context already has a context, link to that one.
1360 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1361 if (ectx->ec_outer_ref->or_partial != NULL)
1362 {
1363 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1364 ++pt->pt_outer.out_up_partial->pt_refcount;
1365 }
1366 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001367
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001368 // If this function returns and the closure is still being used, we
1369 // need to make a copy of the context (arguments and local variables).
1370 // Store a reference to the partial so we can handle that.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001371 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1372 {
1373 vim_free(pt);
1374 return FAIL;
1375 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001376 // Extra variable keeps the count of closures created in the current
1377 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001378 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1379 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1380
1381 ((partial_T **)ectx->ec_funcrefs.ga_data)
1382 [ectx->ec_funcrefs.ga_len] = pt;
1383 ++pt->pt_refcount;
1384 ++ectx->ec_funcrefs.ga_len;
1385 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001386 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001387 return OK;
1388}
1389
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001390// used for v_instr of typval of VAR_INSTR
1391struct instr_S {
1392 ectx_T *instr_ectx;
1393 isn_T *instr_instr;
1394};
1395
Bram Moolenaar4c137212021-04-19 16:48:48 +02001396// used for substitute_instr
1397typedef struct subs_expr_S {
1398 ectx_T *subs_ectx;
1399 isn_T *subs_instr;
1400 int subs_status;
1401} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402
1403// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001404#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405
1406// Get pointer to item at the bottom of the stack, -1 is the bottom.
1407#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001408#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 +01001409
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001410// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001411#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 +01001412
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001413// Set when calling do_debug().
1414static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001415static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001416
1417/*
1418 * When debugging lookup "name" and return the typeval.
1419 * When not found return NULL.
1420 */
1421 typval_T *
1422lookup_debug_var(char_u *name)
1423{
1424 int idx;
1425 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001426 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001427 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001428 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001429
1430 if (ectx == NULL)
1431 return NULL;
1432 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1433
1434 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001435 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001436 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001437 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001438 return STACK_TV_VAR(idx);
1439 }
1440
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001441 // Go through argument names.
1442 ufunc = dfunc->df_ufunc;
1443 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1444 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1445 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1446 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1447 - varargs_off + idx);
1448 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1449 return STACK_TV(ectx->ec_frame_idx - 1);
1450
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001451 return NULL;
1452}
1453
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001454 static void
1455handle_debug(isn_T *iptr, ectx_T *ectx)
1456{
1457 char_u *line;
1458 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1459 + ectx->ec_dfunc_idx)->df_ufunc;
1460 isn_T *ni;
1461 int end_lnum = iptr->isn_lnum;
1462 garray_T ga;
1463 int lnum;
1464
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001465 if (ex_nesting_level > debug_break_level)
1466 {
1467 linenr_T breakpoint;
1468
1469 if (!ufunc->uf_has_breakpoint)
1470 return;
1471
1472 // check for the next breakpoint if needed
1473 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001474 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001475 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1476 return;
1477 }
1478
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001479 SOURCING_LNUM = iptr->isn_lnum;
1480 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001481 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001482
1483 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1484 if (ni->isn_type == ISN_DEBUG
1485 || ni->isn_type == ISN_RETURN
1486 || ni->isn_type == ISN_RETURN_VOID)
1487 {
1488 end_lnum = ni->isn_lnum;
1489 break;
1490 }
1491
1492 if (end_lnum > iptr->isn_lnum)
1493 {
1494 ga_init2(&ga, sizeof(char_u *), 10);
1495 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001496 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001497 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001498
Bram Moolenaar303215d2021-07-07 20:10:43 +02001499 if (p == NULL)
1500 continue; // left over from continuation line
1501 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001502 if (*p == '#')
1503 break;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001504 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001505 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1506 if (STRNCMP(p, "def ", 4) == 0)
1507 break;
1508 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001509 line = ga_concat_strings(&ga, " ");
1510 vim_free(ga.ga_data);
1511 }
1512 else
1513 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001514
Dominique Pelle6e969552021-06-17 13:53:41 +02001515 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001516 debug_context = NULL;
1517
1518 if (end_lnum > iptr->isn_lnum)
1519 vim_free(line);
1520}
1521
Bram Moolenaar4c137212021-04-19 16:48:48 +02001522/*
1523 * Execute instructions in execution context "ectx".
1524 * Return OK or FAIL;
1525 */
1526 static int
1527exec_instructions(ectx_T *ectx)
1528{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001529 int ret = FAIL;
1530 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001531
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001532 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001533 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001534
Bram Moolenaarff652882021-05-16 15:24:49 +02001535 // Only catch exceptions in this instruction list.
1536 ectx->ec_trylevel_at_start = trylevel;
1537
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 for (;;)
1539 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001540 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001542 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001543
Bram Moolenaar270d0382020-05-15 21:42:53 +02001544 if (++breakcheck_count >= 100)
1545 {
1546 line_breakcheck();
1547 breakcheck_count = 0;
1548 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001549 if (got_int)
1550 {
1551 // Turn CTRL-C into an exception.
1552 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001553 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001554 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001555 did_throw = TRUE;
1556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557
Bram Moolenaara26b9702020-04-18 19:53:28 +02001558 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1559 {
1560 // Turn an error message into an exception.
1561 did_emsg = FALSE;
1562 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001563 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001564 did_throw = TRUE;
1565 *msg_list = NULL;
1566 }
1567
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001568 if (did_throw)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001570 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001571 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001572 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573
1574 // An exception jumps to the first catch, finally, or returns from
1575 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001576 while (index > 0)
1577 {
1578 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001579 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001580 break;
1581 // In the catch and finally block of this try we have to go up
1582 // one level.
1583 --index;
1584 trycmd = NULL;
1585 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001586 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001588 if (trycmd->tcd_in_catch)
1589 {
1590 // exception inside ":catch", jump to ":finally" once
1591 ectx->ec_iidx = trycmd->tcd_finally_idx;
1592 trycmd->tcd_finally_idx = 0;
1593 }
1594 else
1595 // jump to first ":catch"
1596 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001597 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001598 did_throw = FALSE; // don't come back here until :endtry
1599 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 }
1601 else
1602 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001603 // Not inside try or need to return from current functions.
1604 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001605 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001606 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001607 tv = STACK_TV_BOT(0);
1608 tv->v_type = VAR_NUMBER;
1609 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001610 ++ectx->ec_stack.ga_len;
1611 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001613 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001614 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001615 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001616 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 goto done;
1618 }
1619
Bram Moolenaar4c137212021-04-19 16:48:48 +02001620 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001621 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 }
1623 continue;
1624 }
1625
Bram Moolenaar4c137212021-04-19 16:48:48 +02001626 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 switch (iptr->isn_type)
1628 {
1629 // execute Ex command line
1630 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001631 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001632 source_cookie_T cookie;
1633
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001634 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001635 // Pass getsourceline to get an error for a missing ":end"
1636 // command.
1637 CLEAR_FIELD(cookie);
1638 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1639 if (do_cmdline(iptr->isn_arg.string,
1640 getsourceline, &cookie,
1641 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1642 == FAIL
1643 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001644 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001645 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 break;
1647
Bram Moolenaar20677332021-06-06 17:02:53 +02001648 // execute Ex command line split at NL characters.
1649 case ISN_EXEC_SPLIT:
1650 {
1651 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001652 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001653
1654 SOURCING_LNUM = iptr->isn_lnum;
1655 CLEAR_FIELD(cookie);
1656 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1657 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001658 line = get_split_sourceline(0, &cookie, 0, 0);
1659 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001660 get_split_sourceline, &cookie,
1661 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1662 == FAIL
1663 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001664 {
1665 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001666 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001667 }
1668 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001669 }
1670 break;
1671
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001672 // Evaluate an expression with legacy syntax, push it onto the
1673 // stack.
1674 case ISN_LEGACY_EVAL:
1675 {
1676 char_u *arg = iptr->isn_arg.string;
1677 int res;
1678 int save_flags = cmdmod.cmod_flags;
1679
1680 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001681 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001682 tv = STACK_TV_BOT(0);
1683 init_tv(tv);
1684 cmdmod.cmod_flags |= CMOD_LEGACY;
1685 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1686 cmdmod.cmod_flags = save_flags;
1687 if (res == FAIL)
1688 goto on_error;
1689 ++ectx->ec_stack.ga_len;
1690 }
1691 break;
1692
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001693 // push typeval VAR_INSTR with instructions to be executed
1694 case ISN_INSTR:
1695 {
1696 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001697 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001698 tv = STACK_TV_BOT(0);
1699 tv->vval.v_instr = ALLOC_ONE(instr_T);
1700 if (tv->vval.v_instr == NULL)
1701 goto on_error;
1702 ++ectx->ec_stack.ga_len;
1703
1704 tv->v_type = VAR_INSTR;
1705 tv->vval.v_instr->instr_ectx = ectx;
1706 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1707 }
1708 break;
1709
Bram Moolenaar4c137212021-04-19 16:48:48 +02001710 // execute :substitute with an expression
1711 case ISN_SUBSTITUTE:
1712 {
1713 subs_T *subs = &iptr->isn_arg.subs;
1714 source_cookie_T cookie;
1715 struct subs_expr_S *save_instr = substitute_instr;
1716 struct subs_expr_S subs_instr;
1717 int res;
1718
1719 subs_instr.subs_ectx = ectx;
1720 subs_instr.subs_instr = subs->subs_instr;
1721 subs_instr.subs_status = OK;
1722 substitute_instr = &subs_instr;
1723
1724 SOURCING_LNUM = iptr->isn_lnum;
1725 // This is very much like ISN_EXEC
1726 CLEAR_FIELD(cookie);
1727 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1728 res = do_cmdline(subs->subs_cmd,
1729 getsourceline, &cookie,
1730 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1731 substitute_instr = save_instr;
1732
1733 if (res == FAIL || did_emsg
1734 || subs_instr.subs_status == FAIL)
1735 goto on_error;
1736 }
1737 break;
1738
1739 case ISN_FINISH:
1740 goto done;
1741
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001742 case ISN_REDIRSTART:
1743 // create a dummy entry for var_redir_str()
1744 if (alloc_redir_lval() == FAIL)
1745 goto on_error;
1746
1747 // The output is stored in growarray "redir_ga" until
1748 // redirection ends.
1749 init_redir_ga();
1750 redir_vname = 1;
1751 break;
1752
1753 case ISN_REDIREND:
1754 {
1755 char_u *res = get_clear_redir_ga();
1756
1757 // End redirection, put redirected text on the stack.
1758 clear_redir_lval();
1759 redir_vname = 0;
1760
1761 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1762 {
1763 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001764 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001765 }
1766 tv = STACK_TV_BOT(0);
1767 tv->v_type = VAR_STRING;
1768 tv->vval.v_string = res;
1769 ++ectx->ec_stack.ga_len;
1770 }
1771 break;
1772
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001773 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001774#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001775 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1776 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001777#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001778 break;
1779
1780 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001781#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001782 {
1783 exarg_T ea;
1784 int res;
1785
1786 CLEAR_FIELD(ea);
1787 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1788 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1789 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1790 --ectx->ec_stack.ga_len;
1791 tv = STACK_TV_BOT(0);
1792 res = cexpr_core(&ea, tv);
1793 clear_tv(tv);
1794 if (res == FAIL)
1795 goto on_error;
1796 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001797#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001798 break;
1799
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001800 // execute Ex command from pieces on the stack
1801 case ISN_EXECCONCAT:
1802 {
1803 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001804 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001805 int pass;
1806 int i;
1807 char_u *cmd = NULL;
1808 char_u *str;
1809
1810 for (pass = 1; pass <= 2; ++pass)
1811 {
1812 for (i = 0; i < count; ++i)
1813 {
1814 tv = STACK_TV_BOT(i - count);
1815 str = tv->vval.v_string;
1816 if (str != NULL && *str != NUL)
1817 {
1818 if (pass == 2)
1819 STRCPY(cmd + len, str);
1820 len += STRLEN(str);
1821 }
1822 if (pass == 2)
1823 clear_tv(tv);
1824 }
1825 if (pass == 1)
1826 {
1827 cmd = alloc(len + 1);
1828 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001829 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001830 len = 0;
1831 }
1832 }
1833
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001834 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001835 do_cmdline_cmd(cmd);
1836 vim_free(cmd);
1837 }
1838 break;
1839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 // execute :echo {string} ...
1841 case ISN_ECHO:
1842 {
1843 int count = iptr->isn_arg.echo.echo_count;
1844 int atstart = TRUE;
1845 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001846 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847
1848 for (idx = 0; idx < count; ++idx)
1849 {
1850 tv = STACK_TV_BOT(idx - count);
1851 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1852 &atstart, &needclr);
1853 clear_tv(tv);
1854 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001855 if (needclr)
1856 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001857 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 }
1859 break;
1860
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001861 // :execute {string} ...
1862 // :echomsg {string} ...
1863 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001864 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001865 case ISN_ECHOMSG:
1866 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001867 {
1868 int count = iptr->isn_arg.number;
1869 garray_T ga;
1870 char_u buf[NUMBUFLEN];
1871 char_u *p;
1872 int len;
1873 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001874 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001875
1876 ga_init2(&ga, 1, 80);
1877 for (idx = 0; idx < count; ++idx)
1878 {
1879 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001880 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001881 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001882 if (tv->v_type == VAR_CHANNEL
1883 || tv->v_type == VAR_JOB)
1884 {
1885 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001886 semsg(_(e_using_invalid_value_as_string_str),
1887 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001888 break;
1889 }
1890 else
1891 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001892 }
1893 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001894 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001895
1896 len = (int)STRLEN(p);
1897 if (ga_grow(&ga, len + 2) == FAIL)
1898 failed = TRUE;
1899 else
1900 {
1901 if (ga.ga_len > 0)
1902 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1903 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1904 ga.ga_len += len;
1905 }
1906 clear_tv(tv);
1907 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001908 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001909 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001910 {
1911 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001912 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001913 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001914
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001915 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001916 {
1917 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001918 {
1919 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001920 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001921 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001922 {
1923 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001924 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001925 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001926 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001927 else
1928 {
1929 msg_sb_eol();
1930 if (iptr->isn_type == ISN_ECHOMSG)
1931 {
1932 msg_attr(ga.ga_data, echo_attr);
1933 out_flush();
1934 }
1935 else
1936 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001937 SOURCING_LNUM = iptr->isn_lnum;
1938 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001939 }
1940 }
1941 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001942 ga_clear(&ga);
1943 }
1944 break;
1945
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 // load local variable or argument
1947 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001948 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001949 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001951 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 break;
1953
1954 // load v: variable
1955 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001956 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001957 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001959 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 break;
1961
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001962 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 case ISN_LOADSCRIPT:
1964 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001965 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 svar_T *sv;
1967
Bram Moolenaar4c137212021-04-19 16:48:48 +02001968 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001969 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001970 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001971 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001972 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001973 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001975 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 }
1977 break;
1978
1979 // load s: variable in old script
1980 case ISN_LOADS:
1981 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001982 hashtab_T *ht = &SCRIPT_VARS(
1983 iptr->isn_arg.loadstore.ls_sid);
1984 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 if (di == NULL)
1988 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001989 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001990 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001991 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 }
1993 else
1994 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001995 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001996 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001998 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 }
2000 }
2001 break;
2002
Bram Moolenaard3aac292020-04-19 14:32:17 +02002003 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002005 case ISN_LOADB:
2006 case ISN_LOADW:
2007 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002009 dictitem_T *di = NULL;
2010 hashtab_T *ht = NULL;
2011 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002012
Bram Moolenaard3aac292020-04-19 14:32:17 +02002013 switch (iptr->isn_type)
2014 {
2015 case ISN_LOADG:
2016 ht = get_globvar_ht();
2017 namespace = 'g';
2018 break;
2019 case ISN_LOADB:
2020 ht = &curbuf->b_vars->dv_hashtab;
2021 namespace = 'b';
2022 break;
2023 case ISN_LOADW:
2024 ht = &curwin->w_vars->dv_hashtab;
2025 namespace = 'w';
2026 break;
2027 case ISN_LOADT:
2028 ht = &curtab->tp_vars->dv_hashtab;
2029 namespace = 't';
2030 break;
2031 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002032 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002033 }
2034 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 if (di == NULL)
2037 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002038 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002039 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002040 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002041 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 }
2043 else
2044 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002045 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002046 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002048 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049 }
2050 }
2051 break;
2052
Bram Moolenaar03290b82020-12-19 16:30:44 +01002053 // load autoload variable
2054 case ISN_LOADAUTO:
2055 {
2056 char_u *name = iptr->isn_arg.string;
2057
Bram Moolenaar4c137212021-04-19 16:48:48 +02002058 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002059 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002060 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002061 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002062 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002063 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002064 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002065 }
2066 break;
2067
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002068 // load g:/b:/w:/t: namespace
2069 case ISN_LOADGDICT:
2070 case ISN_LOADBDICT:
2071 case ISN_LOADWDICT:
2072 case ISN_LOADTDICT:
2073 {
2074 dict_T *d = NULL;
2075
2076 switch (iptr->isn_type)
2077 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002078 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2079 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2080 case ISN_LOADWDICT: d = curwin->w_vars; break;
2081 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002082 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002083 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002084 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002085 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002086 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002087 tv = STACK_TV_BOT(0);
2088 tv->v_type = VAR_DICT;
2089 tv->v_lock = 0;
2090 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002091 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002092 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002093 }
2094 break;
2095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 // load &option
2097 case ISN_LOADOPT:
2098 {
2099 typval_T optval;
2100 char_u *name = iptr->isn_arg.string;
2101
Bram Moolenaara8c17702020-04-01 21:17:24 +02002102 // This is not expected to fail, name is checked during
2103 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002104 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002105 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002106 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002107 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002109 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 }
2111 break;
2112
2113 // load $ENV
2114 case ISN_LOADENV:
2115 {
2116 typval_T optval;
2117 char_u *name = iptr->isn_arg.string;
2118
Bram Moolenaar4c137212021-04-19 16:48:48 +02002119 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002120 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002121 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002122 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002124 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 }
2126 break;
2127
2128 // load @register
2129 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002130 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002131 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 tv = STACK_TV_BOT(0);
2133 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002134 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002135 // This may result in NULL, which should be equivalent to an
2136 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 tv->vval.v_string = get_reg_contents(
2138 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002139 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 break;
2141
2142 // store local variable
2143 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002144 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145 tv = STACK_TV_VAR(iptr->isn_arg.number);
2146 clear_tv(tv);
2147 *tv = *STACK_TV_BOT(0);
2148 break;
2149
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002150 // store s: variable in old script
2151 case ISN_STORES:
2152 {
2153 hashtab_T *ht = &SCRIPT_VARS(
2154 iptr->isn_arg.loadstore.ls_sid);
2155 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002156 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002157
Bram Moolenaar4c137212021-04-19 16:48:48 +02002158 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002159 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002160 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002161 else
2162 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002163 SOURCING_LNUM = iptr->isn_lnum;
2164 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002165 {
2166 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002167 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002168 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002169 clear_tv(&di->di_tv);
2170 di->di_tv = *STACK_TV_BOT(0);
2171 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002172 }
2173 break;
2174
2175 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 case ISN_STORESCRIPT:
2177 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002178 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2179 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180
Bram Moolenaar4c137212021-04-19 16:48:48 +02002181 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002182 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002183 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002184 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002185
2186 // "const" and "final" are checked at compile time, locking
2187 // the value needs to be checked here.
2188 SOURCING_LNUM = iptr->isn_lnum;
2189 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002190 {
2191 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002192 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002193 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 clear_tv(sv->sv_tv);
2196 *sv->sv_tv = *STACK_TV_BOT(0);
2197 }
2198 break;
2199
2200 // store option
2201 case ISN_STOREOPT:
2202 {
2203 long n = 0;
2204 char_u *s = NULL;
2205 char *msg;
2206
Bram Moolenaar4c137212021-04-19 16:48:48 +02002207 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 tv = STACK_TV_BOT(0);
2209 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002210 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002212 if (s == NULL)
2213 s = (char_u *)"";
2214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002216 // must be VAR_NUMBER, CHECKTYPE makes sure
2217 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2219 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002220 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 if (msg != NULL)
2222 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002223 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002225 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 }
2228 break;
2229
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002230 // store $ENV
2231 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002232 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002233 tv = STACK_TV_BOT(0);
2234 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2235 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002236 break;
2237
2238 // store @r
2239 case ISN_STOREREG:
2240 {
2241 int reg = iptr->isn_arg.number;
2242
Bram Moolenaar4c137212021-04-19 16:48:48 +02002243 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002244 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002245 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002246 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002247 }
2248 break;
2249
2250 // store v: variable
2251 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002252 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002253 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2254 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002255 // should not happen, type is checked when compiling
2256 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002257 break;
2258
Bram Moolenaard3aac292020-04-19 14:32:17 +02002259 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002261 case ISN_STOREB:
2262 case ISN_STOREW:
2263 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002265 dictitem_T *di;
2266 hashtab_T *ht;
2267 char_u *name = iptr->isn_arg.string + 2;
2268
Bram Moolenaard3aac292020-04-19 14:32:17 +02002269 switch (iptr->isn_type)
2270 {
2271 case ISN_STOREG:
2272 ht = get_globvar_ht();
2273 break;
2274 case ISN_STOREB:
2275 ht = &curbuf->b_vars->dv_hashtab;
2276 break;
2277 case ISN_STOREW:
2278 ht = &curwin->w_vars->dv_hashtab;
2279 break;
2280 case ISN_STORET:
2281 ht = &curtab->tp_vars->dv_hashtab;
2282 break;
2283 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002284 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002285 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286
Bram Moolenaar4c137212021-04-19 16:48:48 +02002287 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002288 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002290 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291 else
2292 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002293 SOURCING_LNUM = iptr->isn_lnum;
2294 if (var_check_permission(di, name) == FAIL)
2295 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 clear_tv(&di->di_tv);
2297 di->di_tv = *STACK_TV_BOT(0);
2298 }
2299 }
2300 break;
2301
Bram Moolenaar03290b82020-12-19 16:30:44 +01002302 // store an autoload variable
2303 case ISN_STOREAUTO:
2304 SOURCING_LNUM = iptr->isn_lnum;
2305 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2306 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002307 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002308 break;
2309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 // store number in local variable
2311 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002312 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 clear_tv(tv);
2314 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002315 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 break;
2317
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002318 // store value in list or dict variable
2319 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002320 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002321 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002322 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002323 typval_T *tv_dest = STACK_TV_BOT(-1);
2324 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002325
Bram Moolenaar752fc692021-01-04 21:57:11 +01002326 // Stack contains:
2327 // -3 value to be stored
2328 // -2 index
2329 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002330 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002331 SOURCING_LNUM = iptr->isn_lnum;
2332 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002333 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002334 dest_type = tv_dest->v_type;
2335 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002336 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002337 else if (dest_type == VAR_LIST
2338 && tv_idx->v_type != VAR_NUMBER)
2339 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002340 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002341 status = FAIL;
2342 }
2343 }
2344 else if (dest_type != tv_dest->v_type)
2345 {
2346 // just in case, should be OK
2347 semsg(_(e_expected_str_but_got_str),
2348 vartype_name(dest_type),
2349 vartype_name(tv_dest->v_type));
2350 status = FAIL;
2351 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002352
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002353 if (status == OK && dest_type == VAR_LIST)
2354 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002355 long lidx = (long)tv_idx->vval.v_number;
2356 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002357
2358 if (list == NULL)
2359 {
2360 emsg(_(e_list_not_set));
2361 goto on_error;
2362 }
2363 if (lidx < 0 && list->lv_len + lidx >= 0)
2364 // negative index is relative to the end
2365 lidx = list->lv_len + lidx;
2366 if (lidx < 0 || lidx > list->lv_len)
2367 {
2368 semsg(_(e_listidx), lidx);
2369 goto on_error;
2370 }
2371 if (lidx < list->lv_len)
2372 {
2373 listitem_T *li = list_find(list, lidx);
2374
2375 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002376 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002377 goto on_error;
2378 // overwrite existing list item
2379 clear_tv(&li->li_tv);
2380 li->li_tv = *tv;
2381 }
2382 else
2383 {
2384 if (error_if_locked(list->lv_lock,
2385 e_cannot_change_list))
2386 goto on_error;
2387 // append to list, only fails when out of memory
2388 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002389 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002390 clear_tv(tv);
2391 }
2392 }
2393 else if (status == OK && dest_type == VAR_DICT)
2394 {
2395 char_u *key = tv_idx->vval.v_string;
2396 dict_T *dict = tv_dest->vval.v_dict;
2397 dictitem_T *di;
2398
2399 SOURCING_LNUM = iptr->isn_lnum;
2400 if (dict == NULL)
2401 {
2402 emsg(_(e_dictionary_not_set));
2403 goto on_error;
2404 }
2405 if (key == NULL)
2406 key = (char_u *)"";
2407 di = dict_find(dict, key, -1);
2408 if (di != NULL)
2409 {
2410 if (error_if_locked(di->di_tv.v_lock,
2411 e_cannot_change_dict_item))
2412 goto on_error;
2413 // overwrite existing value
2414 clear_tv(&di->di_tv);
2415 di->di_tv = *tv;
2416 }
2417 else
2418 {
2419 if (error_if_locked(dict->dv_lock,
2420 e_cannot_change_dict))
2421 goto on_error;
2422 // add to dict, only fails when out of memory
2423 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002424 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002425 clear_tv(tv);
2426 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002427 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002428 else if (status == OK && dest_type == VAR_BLOB)
2429 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002430 long lidx = (long)tv_idx->vval.v_number;
2431 blob_T *blob = tv_dest->vval.v_blob;
2432 varnumber_T nr;
2433 int error = FALSE;
2434 int len;
2435
2436 if (blob == NULL)
2437 {
2438 emsg(_(e_blob_not_set));
2439 goto on_error;
2440 }
2441 len = blob_len(blob);
2442 if (lidx < 0 && len + lidx >= 0)
2443 // negative index is relative to the end
2444 lidx = len + lidx;
2445
2446 // Can add one byte at the end.
2447 if (lidx < 0 || lidx > len)
2448 {
2449 semsg(_(e_blobidx), lidx);
2450 goto on_error;
2451 }
2452 if (value_check_lock(blob->bv_lock,
2453 (char_u *)"blob", FALSE))
2454 goto on_error;
2455 nr = tv_get_number_chk(tv, &error);
2456 if (error)
2457 goto on_error;
2458 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002459 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002460 else
2461 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002462 status = FAIL;
2463 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002464 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002465
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002466 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002467 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002468 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002469 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002470 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002471 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002472 goto on_error;
2473 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002474 }
2475 break;
2476
Bram Moolenaar68452172021-04-12 21:21:02 +02002477 // store value in blob range
2478 case ISN_STORERANGE:
2479 {
2480 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2481 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2482 typval_T *tv_dest = STACK_TV_BOT(-1);
2483 int status = OK;
2484
2485 // Stack contains:
2486 // -4 value to be stored
2487 // -3 first index or "none"
2488 // -2 second index or "none"
2489 // -1 destination blob
2490 tv = STACK_TV_BOT(-4);
2491 if (tv_dest->v_type != VAR_BLOB)
2492 {
2493 status = FAIL;
2494 emsg(_(e_blob_required));
2495 }
2496 else
2497 {
2498 varnumber_T n1;
2499 varnumber_T n2;
2500 int error = FALSE;
2501
2502 n1 = tv_get_number_chk(tv_idx1, &error);
2503 if (error)
2504 status = FAIL;
2505 else
2506 {
2507 if (tv_idx2->v_type == VAR_SPECIAL
2508 && tv_idx2->vval.v_number == VVAL_NONE)
2509 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2510 else
2511 n2 = tv_get_number_chk(tv_idx2, &error);
2512 if (error)
2513 status = FAIL;
2514 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002515 {
2516 long bloblen = blob_len(tv_dest->vval.v_blob);
2517
2518 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002519 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002520 || check_blob_range(bloblen,
2521 n1, n2, FALSE) == FAIL)
2522 status = FAIL;
2523 else
2524 status = blob_set_range(
2525 tv_dest->vval.v_blob, n1, n2, tv);
2526 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002527 }
2528 }
2529
2530 clear_tv(tv_idx1);
2531 clear_tv(tv_idx2);
2532 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002533 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002534 clear_tv(tv);
2535
2536 if (status == FAIL)
2537 goto on_error;
2538 }
2539 break;
2540
Bram Moolenaar0186e582021-01-10 18:33:11 +01002541 // load or store variable or argument from outer scope
2542 case ISN_LOADOUTER:
2543 case ISN_STOREOUTER:
2544 {
2545 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002546 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2547 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002548
2549 while (depth > 1 && outer != NULL)
2550 {
2551 outer = outer->out_up;
2552 --depth;
2553 }
2554 if (outer == NULL)
2555 {
2556 SOURCING_LNUM = iptr->isn_lnum;
2557 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002558 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002559 }
2560 tv = ((typval_T *)outer->out_stack->ga_data)
2561 + outer->out_frame_idx + STACK_FRAME_SIZE
2562 + iptr->isn_arg.outer.outer_idx;
2563 if (iptr->isn_type == ISN_LOADOUTER)
2564 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002565 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002566 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002567 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002568 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002569 }
2570 else
2571 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002572 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002573 clear_tv(tv);
2574 *tv = *STACK_TV_BOT(0);
2575 }
2576 }
2577 break;
2578
Bram Moolenaar752fc692021-01-04 21:57:11 +01002579 // unlet item in list or dict variable
2580 case ISN_UNLETINDEX:
2581 {
2582 typval_T *tv_idx = STACK_TV_BOT(-2);
2583 typval_T *tv_dest = STACK_TV_BOT(-1);
2584 int status = OK;
2585
2586 // Stack contains:
2587 // -2 index
2588 // -1 dict or list
2589 if (tv_dest->v_type == VAR_DICT)
2590 {
2591 // unlet a dict item, index must be a string
2592 if (tv_idx->v_type != VAR_STRING)
2593 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002594 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002595 semsg(_(e_expected_str_but_got_str),
2596 vartype_name(VAR_STRING),
2597 vartype_name(tv_idx->v_type));
2598 status = FAIL;
2599 }
2600 else
2601 {
2602 dict_T *d = tv_dest->vval.v_dict;
2603 char_u *key = tv_idx->vval.v_string;
2604 dictitem_T *di = NULL;
2605
2606 if (key == NULL)
2607 key = (char_u *)"";
2608 if (d != NULL)
2609 di = dict_find(d, key, (int)STRLEN(key));
2610 if (di == NULL)
2611 {
2612 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002613 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002614 semsg(_(e_dictkey), key);
2615 status = FAIL;
2616 }
2617 else
2618 {
2619 // TODO: check for dict or item locked
2620 dictitem_remove(d, di);
2621 }
2622 }
2623 }
2624 else if (tv_dest->v_type == VAR_LIST)
2625 {
2626 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002627 SOURCING_LNUM = iptr->isn_lnum;
2628 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002629 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002630 status = FAIL;
2631 }
2632 else
2633 {
2634 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002635 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002636 listitem_T *li = NULL;
2637
2638 li = list_find(l, n);
2639 if (li == NULL)
2640 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002641 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002642 semsg(_(e_listidx), n);
2643 status = FAIL;
2644 }
2645 else
2646 // TODO: check for list or item locked
2647 listitem_remove(l, li);
2648 }
2649 }
2650 else
2651 {
2652 status = FAIL;
2653 semsg(_(e_cannot_index_str),
2654 vartype_name(tv_dest->v_type));
2655 }
2656
2657 clear_tv(tv_idx);
2658 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002659 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002660 if (status == FAIL)
2661 goto on_error;
2662 }
2663 break;
2664
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002665 // unlet range of items in list variable
2666 case ISN_UNLETRANGE:
2667 {
2668 // Stack contains:
2669 // -3 index1
2670 // -2 index2
2671 // -1 dict or list
2672 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2673 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2674 typval_T *tv_dest = STACK_TV_BOT(-1);
2675 int status = OK;
2676
2677 if (tv_dest->v_type == VAR_LIST)
2678 {
2679 // indexes must be a number
2680 SOURCING_LNUM = iptr->isn_lnum;
2681 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002682 || (tv_idx2->v_type != VAR_SPECIAL
2683 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002684 {
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;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002691 long n2 = tv_idx2->v_type == VAR_SPECIAL
2692 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002693 listitem_T *li;
2694
2695 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002696 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002697 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002698 else
2699 {
2700 if (n1 < 0)
2701 n1 = list_idx_of_item(l, li);
2702 if (n2 < 0)
2703 {
2704 listitem_T *li2 = list_find(l, n2);
2705
2706 if (li2 == NULL)
2707 status = FAIL;
2708 else
2709 n2 = list_idx_of_item(l, li2);
2710 }
2711 if (status != FAIL
2712 && list_unlet_range(l, li, NULL, n1,
2713 tv_idx2->v_type != VAR_SPECIAL, n2)
2714 == FAIL)
2715 status = FAIL;
2716 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002717 }
2718 }
2719 else
2720 {
2721 status = FAIL;
2722 SOURCING_LNUM = iptr->isn_lnum;
2723 semsg(_(e_cannot_index_str),
2724 vartype_name(tv_dest->v_type));
2725 }
2726
2727 clear_tv(tv_idx1);
2728 clear_tv(tv_idx2);
2729 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002730 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002731 if (status == FAIL)
2732 goto on_error;
2733 }
2734 break;
2735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 // push constant
2737 case ISN_PUSHNR:
2738 case ISN_PUSHBOOL:
2739 case ISN_PUSHSPEC:
2740 case ISN_PUSHF:
2741 case ISN_PUSHS:
2742 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002743 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002744 case ISN_PUSHCHANNEL:
2745 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002746 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002747 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002749 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002750 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 switch (iptr->isn_type)
2752 {
2753 case ISN_PUSHNR:
2754 tv->v_type = VAR_NUMBER;
2755 tv->vval.v_number = iptr->isn_arg.number;
2756 break;
2757 case ISN_PUSHBOOL:
2758 tv->v_type = VAR_BOOL;
2759 tv->vval.v_number = iptr->isn_arg.number;
2760 break;
2761 case ISN_PUSHSPEC:
2762 tv->v_type = VAR_SPECIAL;
2763 tv->vval.v_number = iptr->isn_arg.number;
2764 break;
2765#ifdef FEAT_FLOAT
2766 case ISN_PUSHF:
2767 tv->v_type = VAR_FLOAT;
2768 tv->vval.v_float = iptr->isn_arg.fnumber;
2769 break;
2770#endif
2771 case ISN_PUSHBLOB:
2772 blob_copy(iptr->isn_arg.blob, tv);
2773 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002774 case ISN_PUSHFUNC:
2775 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002776 if (iptr->isn_arg.string == NULL)
2777 tv->vval.v_string = NULL;
2778 else
2779 tv->vval.v_string =
2780 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002781 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002782 case ISN_PUSHCHANNEL:
2783#ifdef FEAT_JOB_CHANNEL
2784 tv->v_type = VAR_CHANNEL;
2785 tv->vval.v_channel = iptr->isn_arg.channel;
2786 if (tv->vval.v_channel != NULL)
2787 ++tv->vval.v_channel->ch_refcount;
2788#endif
2789 break;
2790 case ISN_PUSHJOB:
2791#ifdef FEAT_JOB_CHANNEL
2792 tv->v_type = VAR_JOB;
2793 tv->vval.v_job = iptr->isn_arg.job;
2794 if (tv->vval.v_job != NULL)
2795 ++tv->vval.v_job->jv_refcount;
2796#endif
2797 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 default:
2799 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002800 tv->vval.v_string = vim_strsave(
2801 iptr->isn_arg.string == NULL
2802 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 }
2804 break;
2805
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002806 case ISN_UNLET:
2807 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2808 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002809 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002810 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002811 case ISN_UNLETENV:
2812 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2813 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002814
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002815 case ISN_LOCKCONST:
2816 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2817 break;
2818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 // create a list from items on the stack; uses a single allocation
2820 // for the list header and the items
2821 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002822 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002823 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 break;
2825
2826 // create a dict from items on the stack
2827 case ISN_NEWDICT:
2828 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002829 int count = iptr->isn_arg.number;
2830 dict_T *dict = dict_alloc();
2831 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002832 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002833 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834
2835 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002836 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 for (idx = 0; idx < count; ++idx)
2838 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002839 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002841 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002842 key = tv->vval.v_string == NULL
2843 ? (char_u *)"" : tv->vval.v_string;
2844 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002845 if (item != NULL)
2846 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002847 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002848 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002849 dict_unref(dict);
2850 goto on_error;
2851 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002852 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 clear_tv(tv);
2854 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002855 {
2856 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002857 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002858 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2860 item->di_tv.v_lock = 0;
2861 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002862 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002863 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002864 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002865 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002866 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 }
2868
2869 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002870 ectx->ec_stack.ga_len -= 2 * count - 1;
2871 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002872 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002874 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 tv = STACK_TV_BOT(-1);
2876 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002877 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 tv->vval.v_dict = dict;
2879 ++dict->dv_refcount;
2880 }
2881 break;
2882
2883 // call a :def function
2884 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002885 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002886 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2887 NULL,
2888 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002889 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002890 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 break;
2892
2893 // call a builtin function
2894 case ISN_BCALL:
2895 SOURCING_LNUM = iptr->isn_lnum;
2896 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2897 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002898 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002899 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 break;
2901
2902 // call a funcref or partial
2903 case ISN_PCALL:
2904 {
2905 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2906 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002907 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908
2909 SOURCING_LNUM = iptr->isn_lnum;
2910 if (pfunc->cpf_top)
2911 {
2912 // funcref is above the arguments
2913 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2914 }
2915 else
2916 {
2917 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002918 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002919 partial_tv = *STACK_TV_BOT(0);
2920 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002922 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002923 if (tv == &partial_tv)
2924 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002926 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 }
2928 break;
2929
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002930 case ISN_PCALL_END:
2931 // PCALL finished, arguments have been consumed and replaced by
2932 // the return value. Now clear the funcref from the stack,
2933 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002934 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002935 clear_tv(STACK_TV_BOT(-1));
2936 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2937 break;
2938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 // call a user defined function or funcref/partial
2940 case ISN_UCALL:
2941 {
2942 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2943
2944 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002945 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002946 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002947 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 }
2949 break;
2950
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002951 // return from a :def function call without a value
2952 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002953 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002954 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002955 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002956 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002957 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002958 tv->vval.v_number = 0;
2959 tv->v_lock = 0;
2960 // FALLTHROUGH
2961
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002962 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 case ISN_RETURN:
2964 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002965 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002966 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002967
2968 if (trystack->ga_len > 0)
2969 trycmd = ((trycmd_T *)trystack->ga_data)
2970 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002971 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002972 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002974 // jump to ":finally" or ":endtry"
2975 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002976 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002977 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002978 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 trycmd->tcd_return = TRUE;
2980 }
2981 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002982 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 }
2984 break;
2985
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002986 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 case ISN_FUNCREF:
2988 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002989 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2990 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2991 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002994 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002995 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002996 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002997 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002998 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002999 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01003000 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003001 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003002 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003004 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 tv->vval.v_partial = pt;
3006 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003007 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 }
3009 break;
3010
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003011 // Create a global function from a lambda.
3012 case ISN_NEWFUNC:
3013 {
3014 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3015
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003016 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003017 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003018 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003019 }
3020 break;
3021
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003022 // List functions
3023 case ISN_DEF:
3024 if (iptr->isn_arg.string == NULL)
3025 list_functions(NULL);
3026 else
3027 {
3028 exarg_T ea;
3029
3030 CLEAR_FIELD(ea);
3031 ea.cmd = ea.arg = iptr->isn_arg.string;
3032 define_function(&ea, NULL);
3033 }
3034 break;
3035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 // jump if a condition is met
3037 case ISN_JUMP:
3038 {
3039 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003040 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 int jump = TRUE;
3042
3043 if (when != JUMP_ALWAYS)
3044 {
3045 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003046 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003047 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003048 || when == JUMP_IF_COND_TRUE)
3049 {
3050 SOURCING_LNUM = iptr->isn_lnum;
3051 jump = tv_get_bool_chk(tv, &error);
3052 if (error)
3053 goto on_error;
3054 }
3055 else
3056 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003058 || when == JUMP_AND_KEEP_IF_FALSE
3059 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003061 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 {
3063 // drop the value from the stack
3064 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003065 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 }
3067 }
3068 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003069 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 }
3071 break;
3072
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003073 // Jump if an argument with a default value was already set and not
3074 // v:none.
3075 case ISN_JUMP_IF_ARG_SET:
3076 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3077 if (tv->v_type != VAR_UNKNOWN
3078 && !(tv->v_type == VAR_SPECIAL
3079 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003080 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003081 break;
3082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 // top of a for loop
3084 case ISN_FOR:
3085 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003086 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 typval_T *idxtv =
3088 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3089
Bram Moolenaar4c137212021-04-19 16:48:48 +02003090 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003091 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003092 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003093 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003094 list_T *list = ltv->vval.v_list;
3095
3096 // push the next item from the list
3097 ++idxtv->vval.v_number;
3098 if (list == NULL
3099 || idxtv->vval.v_number >= list->lv_len)
3100 {
3101 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003102 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3103 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003104 }
3105 else if (list->lv_first == &range_list_item)
3106 {
3107 // non-materialized range() list
3108 tv = STACK_TV_BOT(0);
3109 tv->v_type = VAR_NUMBER;
3110 tv->v_lock = 0;
3111 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003113 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003114 }
3115 else
3116 {
3117 listitem_T *li = list_find(list,
3118 idxtv->vval.v_number);
3119
3120 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003121 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003122 }
3123 }
3124 else if (ltv->v_type == VAR_STRING)
3125 {
3126 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003127
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003128 // The index is for the last byte of the previous
3129 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003130 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003131 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003132 {
3133 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003134 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3135 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003136 }
3137 else
3138 {
3139 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3140
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003141 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003142 tv = STACK_TV_BOT(0);
3143 tv->v_type = VAR_STRING;
3144 tv->vval.v_string = vim_strnsave(
3145 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003146 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003147 idxtv->vval.v_number += clen - 1;
3148 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003150 else if (ltv->v_type == VAR_BLOB)
3151 {
3152 blob_T *blob = ltv->vval.v_blob;
3153
3154 // When we get here the first time make a copy of the
3155 // blob, so that the iteration still works when it is
3156 // changed.
3157 if (idxtv->vval.v_number == -1 && blob != NULL)
3158 {
3159 blob_copy(blob, ltv);
3160 blob_unref(blob);
3161 blob = ltv->vval.v_blob;
3162 }
3163
3164 // The index is for the previous byte.
3165 ++idxtv->vval.v_number;
3166 if (blob == NULL
3167 || idxtv->vval.v_number >= blob_len(blob))
3168 {
3169 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003170 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3171 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003172 }
3173 else
3174 {
3175 // Push the next byte from the blob.
3176 tv = STACK_TV_BOT(0);
3177 tv->v_type = VAR_NUMBER;
3178 tv->vval.v_number = blob_get(blob,
3179 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003180 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003181 }
3182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 else
3184 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003185 semsg(_(e_for_loop_on_str_not_supported),
3186 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003187 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 }
3189 }
3190 break;
3191
3192 // start of ":try" block
3193 case ISN_TRY:
3194 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003195 trycmd_T *trycmd = NULL;
3196
Bram Moolenaar4c137212021-04-19 16:48:48 +02003197 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003198 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003199 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3200 + ectx->ec_trystack.ga_len;
3201 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003203 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003204 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3205 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003206 trycmd->tcd_catch_idx =
3207 iptr->isn_arg.try.try_ref->try_catch;
3208 trycmd->tcd_finally_idx =
3209 iptr->isn_arg.try.try_ref->try_finally;
3210 trycmd->tcd_endtry_idx =
3211 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 }
3213 break;
3214
3215 case ISN_PUSHEXC:
3216 if (current_exception == NULL)
3217 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003218 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003220 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003222 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003223 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003225 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003227 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 tv->vval.v_string = vim_strsave(
3229 (char_u *)current_exception->value);
3230 break;
3231
3232 case ISN_CATCH:
3233 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003234 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235
Bram Moolenaar4c137212021-04-19 16:48:48 +02003236 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 if (trystack->ga_len > 0)
3238 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003239 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 + trystack->ga_len - 1;
3241 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003242 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 }
3244 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003245 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 catch_exception(current_exception);
3247 }
3248 break;
3249
Bram Moolenaarc150c092021-02-13 15:02:46 +01003250 case ISN_TRYCONT:
3251 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003252 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003253 trycont_T *trycont = &iptr->isn_arg.trycont;
3254 int i;
3255 trycmd_T *trycmd;
3256 int iidx = trycont->tct_where;
3257
3258 if (trystack->ga_len < trycont->tct_levels)
3259 {
3260 siemsg("TRYCONT: expected %d levels, found %d",
3261 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003262 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003263 }
3264 // Make :endtry jump to any outer try block and the last
3265 // :endtry inside the loop to the loop start.
3266 for (i = trycont->tct_levels; i > 0; --i)
3267 {
3268 trycmd = ((trycmd_T *)trystack->ga_data)
3269 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003270 // Add one to tcd_cont to be able to jump to
3271 // instruction with index zero.
3272 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003273 iidx = trycmd->tcd_finally_idx == 0
3274 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003275 }
3276 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003277 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003278 }
3279 break;
3280
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003281 case ISN_FINALLY:
3282 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003283 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003284 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3285 + trystack->ga_len - 1;
3286
3287 // Reset the index to avoid a return statement jumps here
3288 // again.
3289 trycmd->tcd_finally_idx = 0;
3290 break;
3291 }
3292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 // end of ":try" block
3294 case ISN_ENDTRY:
3295 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003296 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297
3298 if (trystack->ga_len > 0)
3299 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003300 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 --trystack->ga_len;
3303 --trylevel;
3304 trycmd = ((trycmd_T *)trystack->ga_data)
3305 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003306 if (trycmd->tcd_did_throw)
3307 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003308 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 {
3310 // discard the exception
3311 if (caught_stack == current_exception)
3312 caught_stack = caught_stack->caught;
3313 discard_current_exception();
3314 }
3315
3316 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003317 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003318
Bram Moolenaar4c137212021-04-19 16:48:48 +02003319 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003320 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003321 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003322 clear_tv(STACK_TV_BOT(0));
3323 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003324 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003325 // handling :continue: jump to outer try block or
3326 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003327 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 }
3329 }
3330 break;
3331
3332 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003333 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003334 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003335
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003336 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3337 {
3338 // throwing an exception while using "silent!" causes
3339 // the function to abort but not display an error.
3340 tv = STACK_TV_BOT(-1);
3341 clear_tv(tv);
3342 tv->v_type = VAR_NUMBER;
3343 tv->vval.v_number = 0;
3344 goto done;
3345 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003346 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003347 tv = STACK_TV_BOT(0);
3348 if (tv->vval.v_string == NULL
3349 || *skipwhite(tv->vval.v_string) == NUL)
3350 {
3351 vim_free(tv->vval.v_string);
3352 SOURCING_LNUM = iptr->isn_lnum;
3353 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003354 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003355 }
3356
3357 // Inside a "catch" we need to first discard the caught
3358 // exception.
3359 if (trystack->ga_len > 0)
3360 {
3361 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3362 + trystack->ga_len - 1;
3363 if (trycmd->tcd_caught && current_exception != NULL)
3364 {
3365 // discard the exception
3366 if (caught_stack == current_exception)
3367 caught_stack = caught_stack->caught;
3368 discard_current_exception();
3369 trycmd->tcd_caught = FALSE;
3370 }
3371 }
3372
3373 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3374 == FAIL)
3375 {
3376 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003377 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003378 }
3379 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 break;
3382
3383 // compare with special values
3384 case ISN_COMPAREBOOL:
3385 case ISN_COMPARESPECIAL:
3386 {
3387 typval_T *tv1 = STACK_TV_BOT(-2);
3388 typval_T *tv2 = STACK_TV_BOT(-1);
3389 varnumber_T arg1 = tv1->vval.v_number;
3390 varnumber_T arg2 = tv2->vval.v_number;
3391 int res;
3392
3393 switch (iptr->isn_arg.op.op_type)
3394 {
3395 case EXPR_EQUAL: res = arg1 == arg2; break;
3396 case EXPR_NEQUAL: res = arg1 != arg2; break;
3397 default: res = 0; break;
3398 }
3399
Bram Moolenaar4c137212021-04-19 16:48:48 +02003400 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 tv1->v_type = VAR_BOOL;
3402 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3403 }
3404 break;
3405
3406 // Operation with two number arguments
3407 case ISN_OPNR:
3408 case ISN_COMPARENR:
3409 {
3410 typval_T *tv1 = STACK_TV_BOT(-2);
3411 typval_T *tv2 = STACK_TV_BOT(-1);
3412 varnumber_T arg1 = tv1->vval.v_number;
3413 varnumber_T arg2 = tv2->vval.v_number;
3414 varnumber_T res;
3415
3416 switch (iptr->isn_arg.op.op_type)
3417 {
3418 case EXPR_MULT: res = arg1 * arg2; break;
3419 case EXPR_DIV: res = arg1 / arg2; break;
3420 case EXPR_REM: res = arg1 % arg2; break;
3421 case EXPR_SUB: res = arg1 - arg2; break;
3422 case EXPR_ADD: res = arg1 + arg2; break;
3423
3424 case EXPR_EQUAL: res = arg1 == arg2; break;
3425 case EXPR_NEQUAL: res = arg1 != arg2; break;
3426 case EXPR_GREATER: res = arg1 > arg2; break;
3427 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3428 case EXPR_SMALLER: res = arg1 < arg2; break;
3429 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3430 default: res = 0; break;
3431 }
3432
Bram Moolenaar4c137212021-04-19 16:48:48 +02003433 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 if (iptr->isn_type == ISN_COMPARENR)
3435 {
3436 tv1->v_type = VAR_BOOL;
3437 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3438 }
3439 else
3440 tv1->vval.v_number = res;
3441 }
3442 break;
3443
3444 // Computation with two float arguments
3445 case ISN_OPFLOAT:
3446 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003447#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 {
3449 typval_T *tv1 = STACK_TV_BOT(-2);
3450 typval_T *tv2 = STACK_TV_BOT(-1);
3451 float_T arg1 = tv1->vval.v_float;
3452 float_T arg2 = tv2->vval.v_float;
3453 float_T res = 0;
3454 int cmp = FALSE;
3455
3456 switch (iptr->isn_arg.op.op_type)
3457 {
3458 case EXPR_MULT: res = arg1 * arg2; break;
3459 case EXPR_DIV: res = arg1 / arg2; break;
3460 case EXPR_SUB: res = arg1 - arg2; break;
3461 case EXPR_ADD: res = arg1 + arg2; break;
3462
3463 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3464 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3465 case EXPR_GREATER: cmp = arg1 > arg2; break;
3466 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3467 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3468 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3469 default: cmp = 0; break;
3470 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003471 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 if (iptr->isn_type == ISN_COMPAREFLOAT)
3473 {
3474 tv1->v_type = VAR_BOOL;
3475 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3476 }
3477 else
3478 tv1->vval.v_float = res;
3479 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003480#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 break;
3482
3483 case ISN_COMPARELIST:
3484 {
3485 typval_T *tv1 = STACK_TV_BOT(-2);
3486 typval_T *tv2 = STACK_TV_BOT(-1);
3487 list_T *arg1 = tv1->vval.v_list;
3488 list_T *arg2 = tv2->vval.v_list;
3489 int cmp = FALSE;
3490 int ic = iptr->isn_arg.op.op_ic;
3491
3492 switch (iptr->isn_arg.op.op_type)
3493 {
3494 case EXPR_EQUAL: cmp =
3495 list_equal(arg1, arg2, ic, FALSE); break;
3496 case EXPR_NEQUAL: cmp =
3497 !list_equal(arg1, arg2, ic, FALSE); break;
3498 case EXPR_IS: cmp = arg1 == arg2; break;
3499 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3500 default: cmp = 0; break;
3501 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003502 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 clear_tv(tv1);
3504 clear_tv(tv2);
3505 tv1->v_type = VAR_BOOL;
3506 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3507 }
3508 break;
3509
3510 case ISN_COMPAREBLOB:
3511 {
3512 typval_T *tv1 = STACK_TV_BOT(-2);
3513 typval_T *tv2 = STACK_TV_BOT(-1);
3514 blob_T *arg1 = tv1->vval.v_blob;
3515 blob_T *arg2 = tv2->vval.v_blob;
3516 int cmp = FALSE;
3517
3518 switch (iptr->isn_arg.op.op_type)
3519 {
3520 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3521 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3522 case EXPR_IS: cmp = arg1 == arg2; break;
3523 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3524 default: cmp = 0; break;
3525 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003526 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 clear_tv(tv1);
3528 clear_tv(tv2);
3529 tv1->v_type = VAR_BOOL;
3530 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3531 }
3532 break;
3533
3534 // TODO: handle separately
3535 case ISN_COMPARESTRING:
3536 case ISN_COMPAREDICT:
3537 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 case ISN_COMPAREANY:
3539 {
3540 typval_T *tv1 = STACK_TV_BOT(-2);
3541 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003542 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 int ic = iptr->isn_arg.op.op_ic;
3544
Bram Moolenaareb26f432020-09-14 16:50:05 +02003545 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003546 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003548 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 }
3550 break;
3551
3552 case ISN_ADDLIST:
3553 case ISN_ADDBLOB:
3554 {
3555 typval_T *tv1 = STACK_TV_BOT(-2);
3556 typval_T *tv2 = STACK_TV_BOT(-1);
3557
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003558 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 if (iptr->isn_type == ISN_ADDLIST)
3560 eval_addlist(tv1, tv2);
3561 else
3562 eval_addblob(tv1, tv2);
3563 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003564 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 }
3566 break;
3567
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003568 case ISN_LISTAPPEND:
3569 {
3570 typval_T *tv1 = STACK_TV_BOT(-2);
3571 typval_T *tv2 = STACK_TV_BOT(-1);
3572 list_T *l = tv1->vval.v_list;
3573
3574 // add an item to a list
3575 if (l == NULL)
3576 {
3577 SOURCING_LNUM = iptr->isn_lnum;
3578 emsg(_(e_cannot_add_to_null_list));
3579 goto on_error;
3580 }
3581 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003582 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003583 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003584 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003585 }
3586 break;
3587
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003588 case ISN_BLOBAPPEND:
3589 {
3590 typval_T *tv1 = STACK_TV_BOT(-2);
3591 typval_T *tv2 = STACK_TV_BOT(-1);
3592 blob_T *b = tv1->vval.v_blob;
3593 int error = FALSE;
3594 varnumber_T n;
3595
3596 // add a number to a blob
3597 if (b == NULL)
3598 {
3599 SOURCING_LNUM = iptr->isn_lnum;
3600 emsg(_(e_cannot_add_to_null_blob));
3601 goto on_error;
3602 }
3603 n = tv_get_number_chk(tv2, &error);
3604 if (error)
3605 goto on_error;
3606 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003607 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003608 }
3609 break;
3610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 // Computation with two arguments of unknown type
3612 case ISN_OPANY:
3613 {
3614 typval_T *tv1 = STACK_TV_BOT(-2);
3615 typval_T *tv2 = STACK_TV_BOT(-1);
3616 varnumber_T n1, n2;
3617#ifdef FEAT_FLOAT
3618 float_T f1 = 0, f2 = 0;
3619#endif
3620 int error = FALSE;
3621
3622 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3623 {
3624 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3625 {
3626 eval_addlist(tv1, tv2);
3627 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003628 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 break;
3630 }
3631 else if (tv1->v_type == VAR_BLOB
3632 && tv2->v_type == VAR_BLOB)
3633 {
3634 eval_addblob(tv1, tv2);
3635 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003636 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 break;
3638 }
3639 }
3640#ifdef FEAT_FLOAT
3641 if (tv1->v_type == VAR_FLOAT)
3642 {
3643 f1 = tv1->vval.v_float;
3644 n1 = 0;
3645 }
3646 else
3647#endif
3648 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003649 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 n1 = tv_get_number_chk(tv1, &error);
3651 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003652 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653#ifdef FEAT_FLOAT
3654 if (tv2->v_type == VAR_FLOAT)
3655 f1 = n1;
3656#endif
3657 }
3658#ifdef FEAT_FLOAT
3659 if (tv2->v_type == VAR_FLOAT)
3660 {
3661 f2 = tv2->vval.v_float;
3662 n2 = 0;
3663 }
3664 else
3665#endif
3666 {
3667 n2 = tv_get_number_chk(tv2, &error);
3668 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003669 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670#ifdef FEAT_FLOAT
3671 if (tv1->v_type == VAR_FLOAT)
3672 f2 = n2;
3673#endif
3674 }
3675#ifdef FEAT_FLOAT
3676 // if there is a float on either side the result is a float
3677 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3678 {
3679 switch (iptr->isn_arg.op.op_type)
3680 {
3681 case EXPR_MULT: f1 = f1 * f2; break;
3682 case EXPR_DIV: f1 = f1 / f2; break;
3683 case EXPR_SUB: f1 = f1 - f2; break;
3684 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003685 default: SOURCING_LNUM = iptr->isn_lnum;
3686 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003687 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 }
3689 clear_tv(tv1);
3690 clear_tv(tv2);
3691 tv1->v_type = VAR_FLOAT;
3692 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003693 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 }
3695 else
3696#endif
3697 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003698 int failed = FALSE;
3699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 switch (iptr->isn_arg.op.op_type)
3701 {
3702 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003703 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3704 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003705 goto on_error;
3706 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 case EXPR_SUB: n1 = n1 - n2; break;
3708 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003709 default: n1 = num_modulus(n1, n2, &failed);
3710 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003711 goto on_error;
3712 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 }
3714 clear_tv(tv1);
3715 clear_tv(tv2);
3716 tv1->v_type = VAR_NUMBER;
3717 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003718 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 }
3720 }
3721 break;
3722
3723 case ISN_CONCAT:
3724 {
3725 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3726 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3727 char_u *res;
3728
3729 res = concat_str(str1, str2);
3730 clear_tv(STACK_TV_BOT(-2));
3731 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003732 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 STACK_TV_BOT(-1)->vval.v_string = res;
3734 }
3735 break;
3736
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003737 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003738 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003739 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003740 int is_slice = iptr->isn_type == ISN_STRSLICE;
3741 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003742 char_u *res;
3743
3744 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003745 // string slice: string is at stack-3, first index at
3746 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003747 if (is_slice)
3748 {
3749 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003750 n1 = tv->vval.v_number;
3751 }
3752
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003753 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003754 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003755
Bram Moolenaar4c137212021-04-19 16:48:48 +02003756 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003757 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003758 if (is_slice)
3759 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003760 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003761 else
3762 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003763 // single character (including composing characters).
3764 // If the index is too big or negative the result is
3765 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003766 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003767 vim_free(tv->vval.v_string);
3768 tv->vval.v_string = res;
3769 }
3770 break;
3771
3772 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003773 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003774 case ISN_BLOBINDEX:
3775 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003777 int is_slice = iptr->isn_type == ISN_LISTSLICE
3778 || iptr->isn_type == ISN_BLOBSLICE;
3779 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3780 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003781 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003782 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783
3784 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003785 // list slice: list is at stack-3, indexes at stack-2 and
3786 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003787 // Same for blob.
3788 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789
3790 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003791 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003793
3794 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 {
Bram Moolenaared591872020-08-15 22:14:53 +02003796 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003797 n1 = tv->vval.v_number;
3798 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 }
Bram Moolenaared591872020-08-15 22:14:53 +02003800
Bram Moolenaar4c137212021-04-19 16:48:48 +02003801 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003802 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003803 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003804 if (is_blob)
3805 {
3806 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3807 n1, n2, FALSE, tv) == FAIL)
3808 goto on_error;
3809 }
3810 else
3811 {
3812 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3813 n1, n2, FALSE, tv, TRUE) == FAIL)
3814 goto on_error;
3815 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 }
3817 break;
3818
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003819 case ISN_ANYINDEX:
3820 case ISN_ANYSLICE:
3821 {
3822 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3823 typval_T *var1, *var2;
3824 int res;
3825
3826 // index: composite is at stack-2, index at stack-1
3827 // slice: composite is at stack-3, indexes at stack-2 and
3828 // stack-1
3829 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003830 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003831 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3832 goto on_error;
3833 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3834 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003835 res = eval_index_inner(tv, is_slice, var1, var2,
3836 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003837 clear_tv(var1);
3838 if (is_slice)
3839 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003840 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003841 if (res == FAIL)
3842 goto on_error;
3843 }
3844 break;
3845
Bram Moolenaar9af78762020-06-16 11:34:42 +02003846 case ISN_SLICE:
3847 {
3848 list_T *list;
3849 int count = iptr->isn_arg.number;
3850
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003851 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003852 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003853 list = tv->vval.v_list;
3854
3855 // no error for short list, expect it to be checked earlier
3856 if (list != NULL && list->lv_len >= count)
3857 {
3858 list_T *newlist = list_slice(list,
3859 count, list->lv_len - 1);
3860
3861 if (newlist != NULL)
3862 {
3863 list_unref(list);
3864 tv->vval.v_list = newlist;
3865 ++newlist->lv_refcount;
3866 }
3867 }
3868 }
3869 break;
3870
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003871 case ISN_GETITEM:
3872 {
3873 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003874 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003875
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003876 // Get list item: list is at stack-1, push item.
3877 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003878 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
3879 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003880
Bram Moolenaar4c137212021-04-19 16:48:48 +02003881 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003882 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003883 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003884 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003885
3886 // Useful when used in unpack assignment. Reset at
3887 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003888 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003889 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003890 }
3891 break;
3892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 case ISN_MEMBER:
3894 {
3895 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003896 char_u *key;
3897 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003898 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003899
3900 // dict member: dict is at stack-2, key at stack-1
3901 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003902 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003903 dict = tv->vval.v_dict;
3904
3905 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003906 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003907 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003908 if (key == NULL)
3909 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003910
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003911 if ((di = dict_find(dict, key, -1)) == NULL)
3912 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003913 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003914 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003915
3916 // If :silent! is used we will continue, make sure the
3917 // stack contents makes sense.
3918 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003919 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003920 tv = STACK_TV_BOT(-1);
3921 clear_tv(tv);
3922 tv->v_type = VAR_NUMBER;
3923 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003924 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003925 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003926 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003927 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003928 // Clear the dict only after getting the item, to avoid
3929 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003930 tv = STACK_TV_BOT(-1);
3931 temp_tv = *tv;
3932 copy_tv(&di->di_tv, tv);
3933 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003934 }
3935 break;
3936
3937 // dict member with string key
3938 case ISN_STRINGMEMBER:
3939 {
3940 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003942 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943
3944 tv = STACK_TV_BOT(-1);
3945 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3946 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003947 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003949 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 }
3951 dict = tv->vval.v_dict;
3952
3953 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3954 == NULL)
3955 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003956 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003958 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003960 // Clear the dict after getting the item, to avoid that it
3961 // make the item invalid.
3962 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003964 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 }
3966 break;
3967
3968 case ISN_NEGATENR:
3969 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003970 if (tv->v_type != VAR_NUMBER
3971#ifdef FEAT_FLOAT
3972 && tv->v_type != VAR_FLOAT
3973#endif
3974 )
3975 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003976 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003977 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003978 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003979 }
3980#ifdef FEAT_FLOAT
3981 if (tv->v_type == VAR_FLOAT)
3982 tv->vval.v_float = -tv->vval.v_float;
3983 else
3984#endif
3985 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 break;
3987
3988 case ISN_CHECKNR:
3989 {
3990 int error = FALSE;
3991
3992 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003993 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003995 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 (void)tv_get_number_chk(tv, &error);
3997 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003998 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 }
4000 break;
4001
4002 case ISN_CHECKTYPE:
4003 {
4004 checktype_T *ct = &iptr->isn_arg.type;
4005
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004006 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004007 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004008 if (!ectx->ec_where.wt_variable)
4009 ectx->ec_where.wt_index = ct->ct_arg_idx;
4010 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4011 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004012 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004013 if (!ectx->ec_where.wt_variable)
4014 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004015
4016 // number 0 is FALSE, number 1 is TRUE
4017 if (tv->v_type == VAR_NUMBER
4018 && ct->ct_type->tt_type == VAR_BOOL
4019 && (tv->vval.v_number == 0
4020 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004022 tv->v_type = VAR_BOOL;
4023 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004024 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 }
4026 }
4027 break;
4028
Bram Moolenaar9af78762020-06-16 11:34:42 +02004029 case ISN_CHECKLEN:
4030 {
4031 int min_len = iptr->isn_arg.checklen.cl_min_len;
4032 list_T *list = NULL;
4033
4034 tv = STACK_TV_BOT(-1);
4035 if (tv->v_type == VAR_LIST)
4036 list = tv->vval.v_list;
4037 if (list == NULL || list->lv_len < min_len
4038 || (list->lv_len > min_len
4039 && !iptr->isn_arg.checklen.cl_more_OK))
4040 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004041 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004042 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004043 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004044 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004045 }
4046 }
4047 break;
4048
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004049 case ISN_SETTYPE:
4050 {
4051 checktype_T *ct = &iptr->isn_arg.type;
4052
4053 tv = STACK_TV_BOT(-1);
4054 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4055 {
4056 free_type(tv->vval.v_dict->dv_type);
4057 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4058 }
4059 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4060 {
4061 free_type(tv->vval.v_list->lv_type);
4062 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4063 }
4064 }
4065 break;
4066
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004068 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 {
4070 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004071 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004073 if (iptr->isn_type == ISN_2BOOL)
4074 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004075 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004076 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004077 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004078 n = !n;
4079 }
4080 else
4081 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004082 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004083 SOURCING_LNUM = iptr->isn_lnum;
4084 n = tv_get_bool_chk(tv, &error);
4085 if (error)
4086 goto on_error;
4087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 clear_tv(tv);
4089 tv->v_type = VAR_BOOL;
4090 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4091 }
4092 break;
4093
4094 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004095 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004096 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004097 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4098 iptr->isn_type == ISN_2STRING_ANY,
4099 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004100 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 break;
4102
Bram Moolenaar08597872020-12-10 19:43:40 +01004103 case ISN_RANGE:
4104 {
4105 exarg_T ea;
4106 char *errormsg;
4107
Bram Moolenaarece0b872021-01-08 20:40:45 +01004108 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004109 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004110 ea.addr_type = ADDR_LINES;
4111 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004112 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004113 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004114 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004115
Bram Moolenaar4c137212021-04-19 16:48:48 +02004116 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004117 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004118 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004119 tv = STACK_TV_BOT(-1);
4120 tv->v_type = VAR_NUMBER;
4121 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004122 if (ea.addr_count == 0)
4123 tv->vval.v_number = curwin->w_cursor.lnum;
4124 else
4125 tv->vval.v_number = ea.line2;
4126 }
4127 break;
4128
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004129 case ISN_PUT:
4130 {
4131 int regname = iptr->isn_arg.put.put_regname;
4132 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4133 char_u *expr = NULL;
4134 int dir = FORWARD;
4135
Bram Moolenaar08597872020-12-10 19:43:40 +01004136 if (lnum < -2)
4137 {
4138 // line number was put on the stack by ISN_RANGE
4139 tv = STACK_TV_BOT(-1);
4140 curwin->w_cursor.lnum = tv->vval.v_number;
4141 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4142 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004143 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004144 }
4145 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004146 // :put! above cursor
4147 dir = BACKWARD;
4148 else if (lnum >= 0)
4149 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004150
4151 if (regname == '=')
4152 {
4153 tv = STACK_TV_BOT(-1);
4154 if (tv->v_type == VAR_STRING)
4155 expr = tv->vval.v_string;
4156 else
4157 {
4158 expr = typval2string(tv, TRUE); // allocates value
4159 clear_tv(tv);
4160 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004161 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004162 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004163 check_cursor();
4164 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4165 vim_free(expr);
4166 }
4167 break;
4168
Bram Moolenaar02194d22020-10-24 23:08:38 +02004169 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004170 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4171 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4172 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4173 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004174 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4175 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004176 break;
4177
Bram Moolenaar02194d22020-10-24 23:08:38 +02004178 case ISN_CMDMOD_REV:
4179 // filter regprog is owned by the instruction, don't free it
4180 cmdmod.cmod_filter_regmatch.regprog = NULL;
4181 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004182 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4183 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004184 break;
4185
Bram Moolenaar792f7862020-11-23 08:31:18 +01004186 case ISN_UNPACK:
4187 {
4188 int count = iptr->isn_arg.unpack.unp_count;
4189 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4190 list_T *l;
4191 listitem_T *li;
4192 int i;
4193
4194 // Check there is a valid list to unpack.
4195 tv = STACK_TV_BOT(-1);
4196 if (tv->v_type != VAR_LIST)
4197 {
4198 SOURCING_LNUM = iptr->isn_lnum;
4199 emsg(_(e_for_argument_must_be_sequence_of_lists));
4200 goto on_error;
4201 }
4202 l = tv->vval.v_list;
4203 if (l == NULL
4204 || l->lv_len < (semicolon ? count - 1 : count))
4205 {
4206 SOURCING_LNUM = iptr->isn_lnum;
4207 emsg(_(e_list_value_does_not_have_enough_items));
4208 goto on_error;
4209 }
4210 else if (!semicolon && l->lv_len > count)
4211 {
4212 SOURCING_LNUM = iptr->isn_lnum;
4213 emsg(_(e_list_value_has_more_items_than_targets));
4214 goto on_error;
4215 }
4216
4217 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004218 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004219 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004220 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004221
4222 // Variable after semicolon gets a list with the remaining
4223 // items.
4224 if (semicolon)
4225 {
4226 list_T *rem_list =
4227 list_alloc_with_items(l->lv_len - count + 1);
4228
4229 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004230 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004231 tv = STACK_TV_BOT(-count);
4232 tv->vval.v_list = rem_list;
4233 ++rem_list->lv_refcount;
4234 tv->v_lock = 0;
4235 li = l->lv_first;
4236 for (i = 0; i < count - 1; ++i)
4237 li = li->li_next;
4238 for (i = 0; li != NULL; ++i)
4239 {
4240 list_set_item(rem_list, i, &li->li_tv);
4241 li = li->li_next;
4242 }
4243 --count;
4244 }
4245
4246 // Produce the values in reverse order, first item last.
4247 li = l->lv_first;
4248 for (i = 0; i < count; ++i)
4249 {
4250 tv = STACK_TV_BOT(-i - 1);
4251 copy_tv(&li->li_tv, tv);
4252 li = li->li_next;
4253 }
4254
4255 list_unref(l);
4256 }
4257 break;
4258
Bram Moolenaarb2049902021-01-24 12:53:53 +01004259 case ISN_PROF_START:
4260 case ISN_PROF_END:
4261 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004262#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004263 funccall_T cookie;
4264 ufunc_T *cur_ufunc =
4265 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004266 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004267
4268 cookie.func = cur_ufunc;
4269 if (iptr->isn_type == ISN_PROF_START)
4270 {
4271 func_line_start(&cookie, iptr->isn_lnum);
4272 // if we get here the instruction is executed
4273 func_line_exec(&cookie);
4274 }
4275 else
4276 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004277#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004278 }
4279 break;
4280
Bram Moolenaare99d4222021-06-13 14:01:26 +02004281 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004282 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004283 break;
4284
Bram Moolenaar389df252020-07-09 21:20:47 +02004285 case ISN_SHUFFLE:
4286 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004287 typval_T tmp_tv;
4288 int item = iptr->isn_arg.shuffle.shfl_item;
4289 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004290
4291 tmp_tv = *STACK_TV_BOT(-item);
4292 for ( ; up > 0 && item > 1; --up)
4293 {
4294 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4295 --item;
4296 }
4297 *STACK_TV_BOT(-item) = tmp_tv;
4298 }
4299 break;
4300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004302 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004304 ectx->ec_where.wt_index = 0;
4305 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 break;
4307 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004308 continue;
4309
Bram Moolenaard032f342020-07-18 18:13:02 +02004310func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004311 // Restore previous function. If the frame pointer is where we started
4312 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004313 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004314 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004315
Bram Moolenaar4c137212021-04-19 16:48:48 +02004316 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004317 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004318 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004319 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004320
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004321on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004322 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004323 // If "emsg_silent" is set then ignore the error, unless it was set
4324 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004325 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004326 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004327 {
4328 // If a sequence of instructions causes an error while ":silent!"
4329 // was used, restore the stack length and jump ahead to restoring
4330 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004331 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004332 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004333 while (ectx->ec_stack.ga_len
4334 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004335 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004336 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004337 clear_tv(STACK_TV_BOT(0));
4338 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004339 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4340 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004341 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004342 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004343 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004344on_fatal_error:
4345 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004346 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004347 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004348 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 }
4350
4351done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004352 ret = OK;
4353theend:
4354 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4355 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004356}
4357
4358/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004359 * Execute the instructions from a VAR_INSTR typeval and put the result in
4360 * "rettv".
4361 * Return OK or FAIL.
4362 */
4363 int
4364exe_typval_instr(typval_T *tv, typval_T *rettv)
4365{
4366 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4367 isn_T *save_instr = ectx->ec_instr;
4368 int save_iidx = ectx->ec_iidx;
4369 int res;
4370
4371 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4372 res = exec_instructions(ectx);
4373 if (res == OK)
4374 {
4375 *rettv = *STACK_TV_BOT(-1);
4376 --ectx->ec_stack.ga_len;
4377 }
4378
4379 ectx->ec_instr = save_instr;
4380 ectx->ec_iidx = save_iidx;
4381
4382 return res;
4383}
4384
4385/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004386 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4387 * "substitute_instr".
4388 */
4389 char_u *
4390exe_substitute_instr(void)
4391{
4392 ectx_T *ectx = substitute_instr->subs_ectx;
4393 isn_T *save_instr = ectx->ec_instr;
4394 int save_iidx = ectx->ec_iidx;
4395 char_u *res;
4396
4397 ectx->ec_instr = substitute_instr->subs_instr;
4398 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004399 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004400 typval_T *tv = STACK_TV_BOT(-1);
4401
Bram Moolenaar27523602021-06-05 21:36:19 +02004402 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004403 --ectx->ec_stack.ga_len;
4404 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004405 }
4406 else
4407 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004408 substitute_instr->subs_status = FAIL;
4409 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004410 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411
Bram Moolenaar4c137212021-04-19 16:48:48 +02004412 ectx->ec_instr = save_instr;
4413 ectx->ec_iidx = save_iidx;
4414
4415 return res;
4416}
4417
4418/*
4419 * Call a "def" function from old Vim script.
4420 * Return OK or FAIL.
4421 */
4422 int
4423call_def_function(
4424 ufunc_T *ufunc,
4425 int argc_arg, // nr of arguments
4426 typval_T *argv, // arguments
4427 partial_T *partial, // optional partial for context
4428 typval_T *rettv) // return value
4429{
4430 ectx_T ectx; // execution context
4431 int argc = argc_arg;
4432 typval_T *tv;
4433 int idx;
4434 int ret = FAIL;
4435 int defcount = ufunc->uf_args.ga_len - argc;
4436 sctx_T save_current_sctx = current_sctx;
4437 int did_emsg_before = did_emsg_cumul + did_emsg;
4438 int save_suppress_errthrow = suppress_errthrow;
4439 msglist_T **saved_msg_list = NULL;
4440 msglist_T *private_msg_list = NULL;
4441 int save_emsg_silent_def = emsg_silent_def;
4442 int save_did_emsg_def = did_emsg_def;
4443 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004444 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004445
4446// Get pointer to item in the stack.
4447#undef STACK_TV
4448#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4449
4450// Get pointer to item at the bottom of the stack, -1 is the bottom.
4451#undef STACK_TV_BOT
4452#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4453
4454// Get pointer to a local variable on the stack. Negative for arguments.
4455#undef STACK_TV_VAR
4456#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4457
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004458 // Update uf_has_breakpoint if needed.
4459 update_has_breakpoint(ufunc);
4460
Bram Moolenaar4c137212021-04-19 16:48:48 +02004461 if (ufunc->uf_def_status == UF_NOT_COMPILED
4462 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004463 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4464 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004465 == FAIL))
4466 {
4467 if (did_emsg_cumul + did_emsg == did_emsg_before)
4468 semsg(_(e_function_is_not_compiled_str),
4469 printable_func_name(ufunc));
4470 return FAIL;
4471 }
4472
4473 {
4474 // Check the function was really compiled.
4475 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4476 + ufunc->uf_dfunc_idx;
4477 if (INSTRUCTIONS(dfunc) == NULL)
4478 {
4479 iemsg("using call_def_function() on not compiled function");
4480 return FAIL;
4481 }
4482 }
4483
4484 // If depth of calling is getting too high, don't execute the function.
4485 orig_funcdepth = funcdepth_get();
4486 if (funcdepth_increment() == FAIL)
4487 return FAIL;
4488
4489 CLEAR_FIELD(ectx);
4490 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4491 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4492 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4493 {
4494 funcdepth_decrement();
4495 return FAIL;
4496 }
4497 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4498 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4499 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004500 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004501
4502 idx = argc - ufunc->uf_args.ga_len;
4503 if (idx > 0 && ufunc->uf_va_name == NULL)
4504 {
4505 if (idx == 1)
4506 emsg(_(e_one_argument_too_many));
4507 else
4508 semsg(_(e_nr_arguments_too_many), idx);
4509 goto failed_early;
4510 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004511 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4512 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004513 {
4514 if (idx == -1)
4515 emsg(_(e_one_argument_too_few));
4516 else
4517 semsg(_(e_nr_arguments_too_few), -idx);
4518 goto failed_early;
4519 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004520
4521 // Put arguments on the stack, but no more than what the function expects.
4522 // A lambda can be called with more arguments than it uses.
4523 for (idx = 0; idx < argc
4524 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4525 ++idx)
4526 {
4527 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4528 && argv[idx].v_type == VAR_SPECIAL
4529 && argv[idx].vval.v_number == VVAL_NONE)
4530 {
4531 // Use the default value.
4532 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4533 }
4534 else
4535 {
4536 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4537 && check_typval_arg_type(
4538 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4539 goto failed_early;
4540 copy_tv(&argv[idx], STACK_TV_BOT(0));
4541 }
4542 ++ectx.ec_stack.ga_len;
4543 }
4544
4545 // Turn varargs into a list. Empty list if no args.
4546 if (ufunc->uf_va_name != NULL)
4547 {
4548 int vararg_count = argc - ufunc->uf_args.ga_len;
4549
4550 if (vararg_count < 0)
4551 vararg_count = 0;
4552 else
4553 argc -= vararg_count;
4554 if (exe_newlist(vararg_count, &ectx) == FAIL)
4555 goto failed_early;
4556
4557 // Check the type of the list items.
4558 tv = STACK_TV_BOT(-1);
4559 if (ufunc->uf_va_type != NULL
4560 && ufunc->uf_va_type != &t_list_any
4561 && ufunc->uf_va_type->tt_member != &t_any
4562 && tv->vval.v_list != NULL)
4563 {
4564 type_T *expected = ufunc->uf_va_type->tt_member;
4565 listitem_T *li = tv->vval.v_list->lv_first;
4566
4567 for (idx = 0; idx < vararg_count; ++idx)
4568 {
4569 if (check_typval_arg_type(expected, &li->li_tv,
4570 argc + idx + 1) == FAIL)
4571 goto failed_early;
4572 li = li->li_next;
4573 }
4574 }
4575
4576 if (defcount > 0)
4577 // Move varargs list to below missing default arguments.
4578 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4579 --ectx.ec_stack.ga_len;
4580 }
4581
4582 // Make space for omitted arguments, will store default value below.
4583 // Any varargs list goes after them.
4584 if (defcount > 0)
4585 for (idx = 0; idx < defcount; ++idx)
4586 {
4587 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4588 ++ectx.ec_stack.ga_len;
4589 }
4590 if (ufunc->uf_va_name != NULL)
4591 ++ectx.ec_stack.ga_len;
4592
4593 // Frame pointer points to just after arguments.
4594 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4595 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4596
4597 {
4598 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4599 + ufunc->uf_dfunc_idx;
4600 ufunc_T *base_ufunc = dfunc->df_ufunc;
4601
4602 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4603 // by copy_func().
4604 if (partial != NULL || base_ufunc->uf_partial != NULL)
4605 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004606 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4607 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004608 goto failed_early;
4609 if (partial != NULL)
4610 {
4611 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4612 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004613 if (current_ectx->ec_outer_ref != NULL
4614 && current_ectx->ec_outer_ref->or_outer != NULL)
4615 ectx.ec_outer_ref->or_outer =
4616 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004617 }
4618 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004619 {
4620 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4621 ++partial->pt_refcount;
4622 ectx.ec_outer_ref->or_partial = partial;
4623 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004624 }
4625 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004626 {
4627 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4628 ++base_ufunc->uf_partial->pt_refcount;
4629 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4630 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004631 }
4632 }
4633
4634 // dummy frame entries
4635 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4636 {
4637 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4638 ++ectx.ec_stack.ga_len;
4639 }
4640
4641 {
4642 // Reserve space for local variables and any closure reference count.
4643 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4644 + ufunc->uf_dfunc_idx;
4645
4646 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4647 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4648 ectx.ec_stack.ga_len += dfunc->df_varcount;
4649 if (dfunc->df_has_closure)
4650 {
4651 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4652 STACK_TV_VAR(idx)->vval.v_number = 0;
4653 ++ectx.ec_stack.ga_len;
4654 }
4655
4656 ectx.ec_instr = INSTRUCTIONS(dfunc);
4657 }
4658
4659 // Following errors are in the function, not the caller.
4660 // Commands behave like vim9script.
4661 estack_push_ufunc(ufunc, 1);
4662 current_sctx = ufunc->uf_script_ctx;
4663 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4664
4665 // Use a specific location for storing error messages to be converted to an
4666 // exception.
4667 saved_msg_list = msg_list;
4668 msg_list = &private_msg_list;
4669
4670 // Do turn errors into exceptions.
4671 suppress_errthrow = FALSE;
4672
4673 // Do not delete the function while executing it.
4674 ++ufunc->uf_calls;
4675
4676 // When ":silent!" was used before calling then we still abort the
4677 // function. If ":silent!" is used in the function then we don't.
4678 emsg_silent_def = emsg_silent;
4679 did_emsg_def = 0;
4680
4681 ectx.ec_where.wt_index = 0;
4682 ectx.ec_where.wt_variable = FALSE;
4683
4684 // Execute the instructions until done.
4685 ret = exec_instructions(&ectx);
4686 if (ret == OK)
4687 {
4688 // function finished, get result from the stack.
4689 if (ufunc->uf_ret_type == &t_void)
4690 {
4691 rettv->v_type = VAR_VOID;
4692 }
4693 else
4694 {
4695 tv = STACK_TV_BOT(-1);
4696 *rettv = *tv;
4697 tv->v_type = VAR_UNKNOWN;
4698 }
4699 }
4700
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004701 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004702 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4703 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004704
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004705 // Deal with any remaining closures, they may be in use somewhere.
4706 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004707 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004708 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004709 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4710 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004711
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004712 estack_pop();
4713 current_sctx = save_current_sctx;
4714
Bram Moolenaarc970e422021-03-17 15:03:04 +01004715 // TODO: when is it safe to delete the function if it is no longer used?
4716 --ufunc->uf_calls;
4717
Bram Moolenaar352134b2020-10-17 22:04:08 +02004718 if (*msg_list != NULL && saved_msg_list != NULL)
4719 {
4720 msglist_T **plist = saved_msg_list;
4721
4722 // Append entries from the current msg_list (uncaught exceptions) to
4723 // the saved msg_list.
4724 while (*plist != NULL)
4725 plist = &(*plist)->next;
4726
4727 *plist = *msg_list;
4728 }
4729 msg_list = saved_msg_list;
4730
Bram Moolenaar4c137212021-04-19 16:48:48 +02004731 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004732 {
4733 cmdmod.cmod_filter_regmatch.regprog = NULL;
4734 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004735 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004736 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004737 emsg_silent_def = save_emsg_silent_def;
4738 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004739
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004740failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004741 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4743 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004744 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004747 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004748 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004749 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004750 if (ectx.ec_outer_ref->or_outer_allocated)
4751 vim_free(ectx.ec_outer_ref->or_outer);
4752 partial_unref(ectx.ec_outer_ref->or_partial);
4753 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004754 }
4755
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004756 // Not sure if this is necessary.
4757 suppress_errthrow = save_suppress_errthrow;
4758
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004759 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
4760 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004761 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004762 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004763 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 return ret;
4765}
4766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004768 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4769 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4770 * "pfx" is prefixed to every line.
4771 */
4772 static void
4773list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4774{
4775 int line_idx = 0;
4776 int prev_current = 0;
4777 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004778 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004779
4780 for (current = 0; current < instr_count; ++current)
4781 {
4782 isn_T *iptr = &instr[current];
4783 char *line;
4784
4785 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004786 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004787 while (line_idx < iptr->isn_lnum
4788 && line_idx < ufunc->uf_lines.ga_len)
4789 {
4790 if (current > prev_current)
4791 {
4792 msg_puts("\n\n");
4793 prev_current = current;
4794 }
4795 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4796 if (line != NULL)
4797 msg(line);
4798 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004799 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4800 {
4801 int first_def_arg = ufunc->uf_args.ga_len
4802 - ufunc->uf_def_args.ga_len;
4803
4804 if (def_arg_idx > 0)
4805 msg_puts("\n\n");
4806 msg_start();
4807 msg_puts(" ");
4808 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4809 first_def_arg + def_arg_idx]);
4810 msg_puts(" = ");
4811 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4812 msg_clr_eos();
4813 msg_end();
4814 }
4815 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004816
4817 switch (iptr->isn_type)
4818 {
4819 case ISN_EXEC:
4820 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4821 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004822 case ISN_EXEC_SPLIT:
4823 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4824 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004825 case ISN_LEGACY_EVAL:
4826 smsg("%s%4d EVAL legacy %s", pfx, current,
4827 iptr->isn_arg.string);
4828 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004829 case ISN_REDIRSTART:
4830 smsg("%s%4d REDIR", pfx, current);
4831 break;
4832 case ISN_REDIREND:
4833 smsg("%s%4d REDIR END%s", pfx, current,
4834 iptr->isn_arg.number ? " append" : "");
4835 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004836 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004837#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004838 smsg("%s%4d CEXPR pre %s", pfx, current,
4839 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004840#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004841 break;
4842 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004843#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004844 {
4845 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4846
4847 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4848 cexpr_get_auname(cer->cer_cmdidx),
4849 cer->cer_forceit ? "!" : "",
4850 cer->cer_cmdline);
4851 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004852#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004853 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004854 case ISN_INSTR:
4855 {
4856 smsg("%s%4d INSTR", pfx, current);
4857 list_instructions(" ", iptr->isn_arg.instr,
4858 INT_MAX, NULL);
4859 msg(" -------------");
4860 }
4861 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004862 case ISN_SUBSTITUTE:
4863 {
4864 subs_T *subs = &iptr->isn_arg.subs;
4865
4866 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4867 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4868 msg(" -------------");
4869 }
4870 break;
4871 case ISN_EXECCONCAT:
4872 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4873 (varnumber_T)iptr->isn_arg.number);
4874 break;
4875 case ISN_ECHO:
4876 {
4877 echo_T *echo = &iptr->isn_arg.echo;
4878
4879 smsg("%s%4d %s %d", pfx, current,
4880 echo->echo_with_white ? "ECHO" : "ECHON",
4881 echo->echo_count);
4882 }
4883 break;
4884 case ISN_EXECUTE:
4885 smsg("%s%4d EXECUTE %lld", pfx, current,
4886 (varnumber_T)(iptr->isn_arg.number));
4887 break;
4888 case ISN_ECHOMSG:
4889 smsg("%s%4d ECHOMSG %lld", pfx, current,
4890 (varnumber_T)(iptr->isn_arg.number));
4891 break;
4892 case ISN_ECHOERR:
4893 smsg("%s%4d ECHOERR %lld", pfx, current,
4894 (varnumber_T)(iptr->isn_arg.number));
4895 break;
4896 case ISN_LOAD:
4897 {
4898 if (iptr->isn_arg.number < 0)
4899 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4900 (varnumber_T)(iptr->isn_arg.number
4901 + STACK_FRAME_SIZE));
4902 else
4903 smsg("%s%4d LOAD $%lld", pfx, current,
4904 (varnumber_T)(iptr->isn_arg.number));
4905 }
4906 break;
4907 case ISN_LOADOUTER:
4908 {
4909 if (iptr->isn_arg.number < 0)
4910 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4911 iptr->isn_arg.outer.outer_depth,
4912 iptr->isn_arg.outer.outer_idx
4913 + STACK_FRAME_SIZE);
4914 else
4915 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4916 iptr->isn_arg.outer.outer_depth,
4917 iptr->isn_arg.outer.outer_idx);
4918 }
4919 break;
4920 case ISN_LOADV:
4921 smsg("%s%4d LOADV v:%s", pfx, current,
4922 get_vim_var_name(iptr->isn_arg.number));
4923 break;
4924 case ISN_LOADSCRIPT:
4925 {
4926 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4927 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4928 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4929 + sref->sref_idx;
4930
4931 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4932 sv->sv_name,
4933 sref->sref_idx,
4934 si->sn_name);
4935 }
4936 break;
4937 case ISN_LOADS:
4938 {
4939 scriptitem_T *si = SCRIPT_ITEM(
4940 iptr->isn_arg.loadstore.ls_sid);
4941
4942 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4943 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4944 }
4945 break;
4946 case ISN_LOADAUTO:
4947 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4948 break;
4949 case ISN_LOADG:
4950 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4951 break;
4952 case ISN_LOADB:
4953 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4954 break;
4955 case ISN_LOADW:
4956 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4957 break;
4958 case ISN_LOADT:
4959 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4960 break;
4961 case ISN_LOADGDICT:
4962 smsg("%s%4d LOAD g:", pfx, current);
4963 break;
4964 case ISN_LOADBDICT:
4965 smsg("%s%4d LOAD b:", pfx, current);
4966 break;
4967 case ISN_LOADWDICT:
4968 smsg("%s%4d LOAD w:", pfx, current);
4969 break;
4970 case ISN_LOADTDICT:
4971 smsg("%s%4d LOAD t:", pfx, current);
4972 break;
4973 case ISN_LOADOPT:
4974 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4975 break;
4976 case ISN_LOADENV:
4977 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4978 break;
4979 case ISN_LOADREG:
4980 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4981 break;
4982
4983 case ISN_STORE:
4984 if (iptr->isn_arg.number < 0)
4985 smsg("%s%4d STORE arg[%lld]", pfx, current,
4986 iptr->isn_arg.number + STACK_FRAME_SIZE);
4987 else
4988 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4989 break;
4990 case ISN_STOREOUTER:
4991 {
4992 if (iptr->isn_arg.number < 0)
4993 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4994 iptr->isn_arg.outer.outer_depth,
4995 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4996 else
4997 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4998 iptr->isn_arg.outer.outer_depth,
4999 iptr->isn_arg.outer.outer_idx);
5000 }
5001 break;
5002 case ISN_STOREV:
5003 smsg("%s%4d STOREV v:%s", pfx, current,
5004 get_vim_var_name(iptr->isn_arg.number));
5005 break;
5006 case ISN_STOREAUTO:
5007 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5008 break;
5009 case ISN_STOREG:
5010 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5011 break;
5012 case ISN_STOREB:
5013 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5014 break;
5015 case ISN_STOREW:
5016 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5017 break;
5018 case ISN_STORET:
5019 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5020 break;
5021 case ISN_STORES:
5022 {
5023 scriptitem_T *si = SCRIPT_ITEM(
5024 iptr->isn_arg.loadstore.ls_sid);
5025
5026 smsg("%s%4d STORES %s in %s", pfx, current,
5027 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5028 }
5029 break;
5030 case ISN_STORESCRIPT:
5031 {
5032 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5033 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5034 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
5035 + sref->sref_idx;
5036
5037 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
5038 sv->sv_name,
5039 sref->sref_idx,
5040 si->sn_name);
5041 }
5042 break;
5043 case ISN_STOREOPT:
5044 smsg("%s%4d STOREOPT &%s", pfx, current,
5045 iptr->isn_arg.storeopt.so_name);
5046 break;
5047 case ISN_STOREENV:
5048 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5049 break;
5050 case ISN_STOREREG:
5051 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
5052 break;
5053 case ISN_STORENR:
5054 smsg("%s%4d STORE %lld in $%d", pfx, current,
5055 iptr->isn_arg.storenr.stnr_val,
5056 iptr->isn_arg.storenr.stnr_idx);
5057 break;
5058
5059 case ISN_STOREINDEX:
5060 smsg("%s%4d STOREINDEX %s", pfx, current,
5061 vartype_name(iptr->isn_arg.vartype));
5062 break;
5063
5064 case ISN_STORERANGE:
5065 smsg("%s%4d STORERANGE", pfx, current);
5066 break;
5067
5068 // constants
5069 case ISN_PUSHNR:
5070 smsg("%s%4d PUSHNR %lld", pfx, current,
5071 (varnumber_T)(iptr->isn_arg.number));
5072 break;
5073 case ISN_PUSHBOOL:
5074 case ISN_PUSHSPEC:
5075 smsg("%s%4d PUSH %s", pfx, current,
5076 get_var_special_name(iptr->isn_arg.number));
5077 break;
5078 case ISN_PUSHF:
5079#ifdef FEAT_FLOAT
5080 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5081#endif
5082 break;
5083 case ISN_PUSHS:
5084 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5085 break;
5086 case ISN_PUSHBLOB:
5087 {
5088 char_u *r;
5089 char_u numbuf[NUMBUFLEN];
5090 char_u *tofree;
5091
5092 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5093 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5094 vim_free(tofree);
5095 }
5096 break;
5097 case ISN_PUSHFUNC:
5098 {
5099 char *name = (char *)iptr->isn_arg.string;
5100
5101 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5102 name == NULL ? "[none]" : name);
5103 }
5104 break;
5105 case ISN_PUSHCHANNEL:
5106#ifdef FEAT_JOB_CHANNEL
5107 {
5108 channel_T *channel = iptr->isn_arg.channel;
5109
5110 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5111 channel == NULL ? 0 : channel->ch_id);
5112 }
5113#endif
5114 break;
5115 case ISN_PUSHJOB:
5116#ifdef FEAT_JOB_CHANNEL
5117 {
5118 typval_T tv;
5119 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005120 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005121
5122 tv.v_type = VAR_JOB;
5123 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005124 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005125 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5126 }
5127#endif
5128 break;
5129 case ISN_PUSHEXC:
5130 smsg("%s%4d PUSH v:exception", pfx, current);
5131 break;
5132 case ISN_UNLET:
5133 smsg("%s%4d UNLET%s %s", pfx, current,
5134 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5135 iptr->isn_arg.unlet.ul_name);
5136 break;
5137 case ISN_UNLETENV:
5138 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5139 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5140 iptr->isn_arg.unlet.ul_name);
5141 break;
5142 case ISN_UNLETINDEX:
5143 smsg("%s%4d UNLETINDEX", pfx, current);
5144 break;
5145 case ISN_UNLETRANGE:
5146 smsg("%s%4d UNLETRANGE", pfx, current);
5147 break;
5148 case ISN_LOCKCONST:
5149 smsg("%s%4d LOCKCONST", pfx, current);
5150 break;
5151 case ISN_NEWLIST:
5152 smsg("%s%4d NEWLIST size %lld", pfx, current,
5153 (varnumber_T)(iptr->isn_arg.number));
5154 break;
5155 case ISN_NEWDICT:
5156 smsg("%s%4d NEWDICT size %lld", pfx, current,
5157 (varnumber_T)(iptr->isn_arg.number));
5158 break;
5159
5160 // function call
5161 case ISN_BCALL:
5162 {
5163 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5164
5165 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5166 internal_func_name(cbfunc->cbf_idx),
5167 cbfunc->cbf_argcount);
5168 }
5169 break;
5170 case ISN_DCALL:
5171 {
5172 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5173 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5174 + cdfunc->cdf_idx;
5175
5176 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5177 df->df_ufunc->uf_name_exp != NULL
5178 ? df->df_ufunc->uf_name_exp
5179 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5180 }
5181 break;
5182 case ISN_UCALL:
5183 {
5184 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5185
5186 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5187 cufunc->cuf_name, cufunc->cuf_argcount);
5188 }
5189 break;
5190 case ISN_PCALL:
5191 {
5192 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5193
5194 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5195 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5196 }
5197 break;
5198 case ISN_PCALL_END:
5199 smsg("%s%4d PCALL end", pfx, current);
5200 break;
5201 case ISN_RETURN:
5202 smsg("%s%4d RETURN", pfx, current);
5203 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005204 case ISN_RETURN_VOID:
5205 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005206 break;
5207 case ISN_FUNCREF:
5208 {
5209 funcref_T *funcref = &iptr->isn_arg.funcref;
5210 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5211 + funcref->fr_func;
5212
5213 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5214 }
5215 break;
5216
5217 case ISN_NEWFUNC:
5218 {
5219 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5220
5221 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5222 newfunc->nf_lambda, newfunc->nf_global);
5223 }
5224 break;
5225
5226 case ISN_DEF:
5227 {
5228 char_u *name = iptr->isn_arg.string;
5229
5230 smsg("%s%4d DEF %s", pfx, current,
5231 name == NULL ? (char_u *)"" : name);
5232 }
5233 break;
5234
5235 case ISN_JUMP:
5236 {
5237 char *when = "?";
5238
5239 switch (iptr->isn_arg.jump.jump_when)
5240 {
5241 case JUMP_ALWAYS:
5242 when = "JUMP";
5243 break;
5244 case JUMP_AND_KEEP_IF_TRUE:
5245 when = "JUMP_AND_KEEP_IF_TRUE";
5246 break;
5247 case JUMP_IF_FALSE:
5248 when = "JUMP_IF_FALSE";
5249 break;
5250 case JUMP_AND_KEEP_IF_FALSE:
5251 when = "JUMP_AND_KEEP_IF_FALSE";
5252 break;
5253 case JUMP_IF_COND_FALSE:
5254 when = "JUMP_IF_COND_FALSE";
5255 break;
5256 case JUMP_IF_COND_TRUE:
5257 when = "JUMP_IF_COND_TRUE";
5258 break;
5259 }
5260 smsg("%s%4d %s -> %d", pfx, current, when,
5261 iptr->isn_arg.jump.jump_where);
5262 }
5263 break;
5264
5265 case ISN_JUMP_IF_ARG_SET:
5266 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5267 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5268 iptr->isn_arg.jump.jump_where);
5269 break;
5270
5271 case ISN_FOR:
5272 {
5273 forloop_T *forloop = &iptr->isn_arg.forloop;
5274
5275 smsg("%s%4d FOR $%d -> %d", pfx, current,
5276 forloop->for_idx, forloop->for_end);
5277 }
5278 break;
5279
5280 case ISN_TRY:
5281 {
5282 try_T *try = &iptr->isn_arg.try;
5283
5284 if (try->try_ref->try_finally == 0)
5285 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5286 pfx, current,
5287 try->try_ref->try_catch,
5288 try->try_ref->try_endtry);
5289 else
5290 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5291 pfx, current,
5292 try->try_ref->try_catch,
5293 try->try_ref->try_finally,
5294 try->try_ref->try_endtry);
5295 }
5296 break;
5297 case ISN_CATCH:
5298 // TODO
5299 smsg("%s%4d CATCH", pfx, current);
5300 break;
5301 case ISN_TRYCONT:
5302 {
5303 trycont_T *trycont = &iptr->isn_arg.trycont;
5304
5305 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5306 trycont->tct_levels,
5307 trycont->tct_levels == 1 ? "" : "s",
5308 trycont->tct_where);
5309 }
5310 break;
5311 case ISN_FINALLY:
5312 smsg("%s%4d FINALLY", pfx, current);
5313 break;
5314 case ISN_ENDTRY:
5315 smsg("%s%4d ENDTRY", pfx, current);
5316 break;
5317 case ISN_THROW:
5318 smsg("%s%4d THROW", pfx, current);
5319 break;
5320
5321 // expression operations on number
5322 case ISN_OPNR:
5323 case ISN_OPFLOAT:
5324 case ISN_OPANY:
5325 {
5326 char *what;
5327 char *ins;
5328
5329 switch (iptr->isn_arg.op.op_type)
5330 {
5331 case EXPR_MULT: what = "*"; break;
5332 case EXPR_DIV: what = "/"; break;
5333 case EXPR_REM: what = "%"; break;
5334 case EXPR_SUB: what = "-"; break;
5335 case EXPR_ADD: what = "+"; break;
5336 default: what = "???"; break;
5337 }
5338 switch (iptr->isn_type)
5339 {
5340 case ISN_OPNR: ins = "OPNR"; break;
5341 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5342 case ISN_OPANY: ins = "OPANY"; break;
5343 default: ins = "???"; break;
5344 }
5345 smsg("%s%4d %s %s", pfx, current, ins, what);
5346 }
5347 break;
5348
5349 case ISN_COMPAREBOOL:
5350 case ISN_COMPARESPECIAL:
5351 case ISN_COMPARENR:
5352 case ISN_COMPAREFLOAT:
5353 case ISN_COMPARESTRING:
5354 case ISN_COMPAREBLOB:
5355 case ISN_COMPARELIST:
5356 case ISN_COMPAREDICT:
5357 case ISN_COMPAREFUNC:
5358 case ISN_COMPAREANY:
5359 {
5360 char *p;
5361 char buf[10];
5362 char *type;
5363
5364 switch (iptr->isn_arg.op.op_type)
5365 {
5366 case EXPR_EQUAL: p = "=="; break;
5367 case EXPR_NEQUAL: p = "!="; break;
5368 case EXPR_GREATER: p = ">"; break;
5369 case EXPR_GEQUAL: p = ">="; break;
5370 case EXPR_SMALLER: p = "<"; break;
5371 case EXPR_SEQUAL: p = "<="; break;
5372 case EXPR_MATCH: p = "=~"; break;
5373 case EXPR_IS: p = "is"; break;
5374 case EXPR_ISNOT: p = "isnot"; break;
5375 case EXPR_NOMATCH: p = "!~"; break;
5376 default: p = "???"; break;
5377 }
5378 STRCPY(buf, p);
5379 if (iptr->isn_arg.op.op_ic == TRUE)
5380 strcat(buf, "?");
5381 switch(iptr->isn_type)
5382 {
5383 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5384 case ISN_COMPARESPECIAL:
5385 type = "COMPARESPECIAL"; break;
5386 case ISN_COMPARENR: type = "COMPARENR"; break;
5387 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5388 case ISN_COMPARESTRING:
5389 type = "COMPARESTRING"; break;
5390 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5391 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5392 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5393 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5394 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5395 default: type = "???"; break;
5396 }
5397
5398 smsg("%s%4d %s %s", pfx, current, type, buf);
5399 }
5400 break;
5401
5402 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5403 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5404
5405 // expression operations
5406 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5407 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5408 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5409 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5410 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5411 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5412 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5413 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5414 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5415 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5416 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5417 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5418 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005419 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5420 iptr->isn_arg.getitem.gi_index,
5421 iptr->isn_arg.getitem.gi_with_op ?
5422 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005423 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5424 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5425 iptr->isn_arg.string); break;
5426 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5427
5428 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5429 case ISN_CHECKTYPE:
5430 {
5431 checktype_T *ct = &iptr->isn_arg.type;
5432 char *tofree;
5433
5434 if (ct->ct_arg_idx == 0)
5435 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5436 type_name(ct->ct_type, &tofree),
5437 (int)ct->ct_off);
5438 else
5439 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5440 type_name(ct->ct_type, &tofree),
5441 (int)ct->ct_off,
5442 (int)ct->ct_arg_idx);
5443 vim_free(tofree);
5444 break;
5445 }
5446 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5447 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5448 iptr->isn_arg.checklen.cl_min_len);
5449 break;
5450 case ISN_SETTYPE:
5451 {
5452 char *tofree;
5453
5454 smsg("%s%4d SETTYPE %s", pfx, current,
5455 type_name(iptr->isn_arg.type.ct_type, &tofree));
5456 vim_free(tofree);
5457 break;
5458 }
5459 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005460 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5461 smsg("%s%4d INVERT %d (!val)", pfx, current,
5462 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005463 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005464 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5465 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005466 break;
5467 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005468 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005469 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005470 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5471 pfx, current,
5472 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005473 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005474 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5475 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005476 break;
5477 case ISN_PUT:
5478 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5479 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005480 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005481 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5482 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005483 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005484 else
5485 smsg("%s%4d PUT %c %ld", pfx, current,
5486 iptr->isn_arg.put.put_regname,
5487 (long)iptr->isn_arg.put.put_lnum);
5488 break;
5489
5490 // TODO: summarize modifiers
5491 case ISN_CMDMOD:
5492 {
5493 char_u *buf;
5494 size_t len = produce_cmdmods(
5495 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5496
5497 buf = alloc(len + 1);
5498 if (buf != NULL)
5499 {
5500 (void)produce_cmdmods(
5501 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5502 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5503 vim_free(buf);
5504 }
5505 break;
5506 }
5507 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5508
5509 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005510 smsg("%s%4d PROFILE START line %d", pfx, current,
5511 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005512 break;
5513
5514 case ISN_PROF_END:
5515 smsg("%s%4d PROFILE END", pfx, current);
5516 break;
5517
Bram Moolenaare99d4222021-06-13 14:01:26 +02005518 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005519 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5520 iptr->isn_arg.debug.dbg_break_lnum + 1,
5521 iptr->isn_lnum,
5522 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005523 break;
5524
Bram Moolenaar4c137212021-04-19 16:48:48 +02005525 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5526 iptr->isn_arg.unpack.unp_count,
5527 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5528 break;
5529 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5530 iptr->isn_arg.shuffle.shfl_item,
5531 iptr->isn_arg.shuffle.shfl_up);
5532 break;
5533 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5534
5535 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5536 return;
5537 }
5538
5539 out_flush(); // output one line at a time
5540 ui_breakcheck();
5541 if (got_int)
5542 break;
5543 }
5544}
5545
5546/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005547 * Handle command line completion for the :disassemble command.
5548 */
5549 void
5550set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5551{
5552 char_u *p;
5553
5554 // Default: expand user functions, "debug" and "profile"
5555 xp->xp_context = EXPAND_DISASSEMBLE;
5556 xp->xp_pattern = arg;
5557
5558 // first argument already typed: only user function names
5559 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5560 {
5561 xp->xp_context = EXPAND_USER_FUNC;
5562 xp->xp_pattern = skipwhite(p);
5563 }
5564}
5565
5566/*
5567 * Function given to ExpandGeneric() to obtain the list of :disassemble
5568 * arguments.
5569 */
5570 char_u *
5571get_disassemble_argument(expand_T *xp, int idx)
5572{
5573 if (idx == 0)
5574 return (char_u *)"debug";
5575 if (idx == 1)
5576 return (char_u *)"profile";
5577 return get_user_func_name(xp, idx - 2);
5578}
5579
5580/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005581 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005582 * We don't really need this at runtime, but we do have tests that require it,
5583 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 */
5585 void
5586ex_disassemble(exarg_T *eap)
5587{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005588 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005589 char_u *fname;
5590 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005591 dfunc_T *dfunc;
5592 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005593 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005594 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005595 compiletype_T compile_type = CT_NONE;
5596
5597 if (STRNCMP(arg, "profile", 7) == 0)
5598 {
5599 compile_type = CT_PROFILE;
5600 arg = skipwhite(arg + 7);
5601 }
5602 else if (STRNCMP(arg, "debug", 5) == 0)
5603 {
5604 compile_type = CT_DEBUG;
5605 arg = skipwhite(arg + 5);
5606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005608 if (STRNCMP(arg, "<lambda>", 8) == 0)
5609 {
5610 arg += 8;
5611 (void)getdigits(&arg);
5612 fname = vim_strnsave(eap->arg, arg - eap->arg);
5613 }
5614 else
5615 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005616 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005617 if (fname == NULL)
5618 {
5619 semsg(_(e_invarg2), eap->arg);
5620 return;
5621 }
5622
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005623 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005624 if (ufunc == NULL)
5625 {
5626 char_u *p = untrans_function_name(fname);
5627
5628 if (p != NULL)
5629 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005630 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005631 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005632 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633 if (ufunc == NULL)
5634 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005635 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005636 return;
5637 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005638 if (func_needs_compiling(ufunc, compile_type)
5639 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005640 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005641 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005643 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644 return;
5645 }
5646 if (ufunc->uf_name_exp != NULL)
5647 msg((char *)ufunc->uf_name_exp);
5648 else
5649 msg((char *)ufunc->uf_name);
5650
5651 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005652 switch (compile_type)
5653 {
5654 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005655#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005656 instr = dfunc->df_instr_prof;
5657 instr_count = dfunc->df_instr_prof_count;
5658 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005659#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005660 // FALLTHROUGH
5661 case CT_NONE:
5662 instr = dfunc->df_instr;
5663 instr_count = dfunc->df_instr_count;
5664 break;
5665 case CT_DEBUG:
5666 instr = dfunc->df_instr_debug;
5667 instr_count = dfunc->df_instr_debug_count;
5668 break;
5669 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005670
Bram Moolenaar4c137212021-04-19 16:48:48 +02005671 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672}
5673
5674/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005675 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005676 * list, etc. Mostly like what JavaScript does, except that empty list and
5677 * empty dictionary are FALSE.
5678 */
5679 int
5680tv2bool(typval_T *tv)
5681{
5682 switch (tv->v_type)
5683 {
5684 case VAR_NUMBER:
5685 return tv->vval.v_number != 0;
5686 case VAR_FLOAT:
5687#ifdef FEAT_FLOAT
5688 return tv->vval.v_float != 0.0;
5689#else
5690 break;
5691#endif
5692 case VAR_PARTIAL:
5693 return tv->vval.v_partial != NULL;
5694 case VAR_FUNC:
5695 case VAR_STRING:
5696 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5697 case VAR_LIST:
5698 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5699 case VAR_DICT:
5700 return tv->vval.v_dict != NULL
5701 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5702 case VAR_BOOL:
5703 case VAR_SPECIAL:
5704 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5705 case VAR_JOB:
5706#ifdef FEAT_JOB_CHANNEL
5707 return tv->vval.v_job != NULL;
5708#else
5709 break;
5710#endif
5711 case VAR_CHANNEL:
5712#ifdef FEAT_JOB_CHANNEL
5713 return tv->vval.v_channel != NULL;
5714#else
5715 break;
5716#endif
5717 case VAR_BLOB:
5718 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5719 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005720 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005721 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005722 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005723 break;
5724 }
5725 return FALSE;
5726}
5727
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005728 void
5729emsg_using_string_as(typval_T *tv, int as_number)
5730{
5731 semsg(_(as_number ? e_using_string_as_number_str
5732 : e_using_string_as_bool_str),
5733 tv->vval.v_string == NULL
5734 ? (char_u *)"" : tv->vval.v_string);
5735}
5736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737/*
5738 * If "tv" is a string give an error and return FAIL.
5739 */
5740 int
5741check_not_string(typval_T *tv)
5742{
5743 if (tv->v_type == VAR_STRING)
5744 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005745 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746 clear_tv(tv);
5747 return FAIL;
5748 }
5749 return OK;
5750}
5751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005752
5753#endif // FEAT_EVAL