blob: f453af7947224f2e9d3f7cbf4250a60d2d05fcd4 [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
Dominique Pelle5a9e5842021-07-24 19:32:12 +020025#if defined(__GNUC__) || defined(__clang__)
26# define likely(x) __builtin_expect((x), 1)
27# define unlikely(x) __builtin_expect((x), 0)
28#else
29# define unlikely(x) (x)
30# define likely(x) (x)
31#endif
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033// Structure put on ec_trystack when ISN_TRY is encountered.
34typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010035 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
36 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020037 int tcd_in_catch; // in catch or finally block
38 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010039 int tcd_catch_idx; // instruction of the first :catch or :finally
40 int tcd_finally_idx; // instruction of the :finally block or zero
41 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010043 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010044 int tcd_return; // when TRUE return from end of :finally
45} trycmd_T;
46
Bram Moolenaar4c137212021-04-19 16:48:48 +020047// Data local to a function.
48// On a function call, if not empty, is saved on the stack and restored when
49// returning.
50typedef struct {
51 int floc_restore_cmdmod;
52 cmdmod_T floc_save_cmdmod;
53 int floc_restore_cmdmod_stacklen;
54} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010055
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020056// Structure to hold a reference to an outer_T, with information of whether it
57// was allocated.
58typedef struct {
59 outer_T *or_outer;
60 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
61 int or_outer_allocated; // free "or_outer" later
62} outer_ref_T;
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064// A stack is used to store:
65// - arguments passed to a :def function
66// - info about the calling function, to use when returning
67// - local variables
68// - temporary values
69//
70// In detail (FP == Frame Pointer):
71// arg1 first argument from caller (if present)
72// arg2 second argument from caller (if present)
73// extra_arg1 any missing optional argument default value
74// FP -> cur_func calling function
75// current previous instruction pointer
76// frame_ptr previous Frame Pointer
77// var1 space for local variable
78// var2 space for local variable
79// .... fixed space for max. number of local variables
80// temp temporary values
81// .... flexible space for temporary values (can grow big)
82
83/*
84 * Execution context.
85 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010086struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020088 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020089 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020091 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020092 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010094 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095
96 int ec_dfunc_idx; // current function index
97 isn_T *ec_instr; // array with instructions
98 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020099
100 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +0200101
102 int ec_did_emsg_before;
103 int ec_trylevel_at_start;
104 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100105};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
Bram Moolenaar12d26532021-02-19 19:13:21 +0100107#ifdef FEAT_PROFILE
108// stack of profinfo_T used when profiling.
109static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
110#endif
111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200113#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 +0100114
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200115 void
116to_string_error(vartype_T vartype)
117{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200118 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200119}
120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100122 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100123 */
124 static int
125ufunc_argcount(ufunc_T *ufunc)
126{
127 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
128}
129
130/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200131 * Create a new list from "count" items at the bottom of the stack.
132 * When "count" is zero an empty list is added to the stack.
133 */
134 static int
135exe_newlist(int count, ectx_T *ectx)
136{
137 list_T *list = list_alloc_with_items(count);
138 int idx;
139 typval_T *tv;
140
141 if (list == NULL)
142 return FAIL;
143 for (idx = 0; idx < count; ++idx)
144 list_set_item(list, idx, STACK_TV_BOT(idx - count));
145
146 if (count > 0)
147 ectx->ec_stack.ga_len -= count - 1;
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200148 else if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarfe270812020-04-11 22:31:27 +0200149 return FAIL;
150 else
151 ++ectx->ec_stack.ga_len;
152 tv = STACK_TV_BOT(-1);
153 tv->v_type = VAR_LIST;
154 tv->vval.v_list = list;
155 ++list->lv_refcount;
156 return OK;
157}
158
159/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200160 * If debug_tick changed check if "ufunc" has a breakpoint and update
161 * "uf_has_breakpoint".
162 */
163 static void
164update_has_breakpoint(ufunc_T *ufunc)
165{
166 if (ufunc->uf_debug_tick != debug_tick)
167 {
168 linenr_T breakpoint;
169
170 ufunc->uf_debug_tick = debug_tick;
171 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
172 ufunc->uf_has_breakpoint = breakpoint > 0;
173 }
174}
175
176/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100178 * This adds a stack frame and sets the instruction pointer to the start of the
179 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200180 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181 *
182 * Stack has:
183 * - current arguments (already there)
184 * - omitted optional argument (default values) added here
185 * - stack frame:
186 * - pointer to calling function
187 * - Index of next instruction in calling function
188 * - previous frame pointer
189 * - reserved space for local variables
190 */
191 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100192call_dfunc(
193 int cdf_idx,
194 partial_T *pt,
195 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100196 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100198 int argcount = argcount_arg;
199 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
200 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200201 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100202 int arg_to_add;
203 int vararg_count = 0;
204 int varcount;
205 int idx;
206 estack_T *entry;
207 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200208 int res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100209
210 if (dfunc->df_deleted)
211 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100212 // don't use ufunc->uf_name, it may have been freed
213 emsg_funcname(e_func_deleted,
214 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215 return FAIL;
216 }
217
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100218#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100219 if (do_profiling == PROF_YES)
220 {
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200221 if (likely(ga_grow(&profile_info_ga, 1) == OK))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100222 {
223 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
224 + profile_info_ga.ga_len;
225 ++profile_info_ga.ga_len;
226 CLEAR_POINTER(info);
227 profile_may_start_func(info, ufunc,
228 (((dfunc_T *)def_functions.ga_data)
229 + ectx->ec_dfunc_idx)->df_ufunc);
230 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100231 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100232#endif
233
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200234 // Update uf_has_breakpoint if needed.
235 update_has_breakpoint(ufunc);
236
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200237 // When debugging and using "cont" switches to the not-debugged
238 // instructions, may need to still compile them.
Bram Moolenaar648594e2021-07-11 17:55:01 +0200239 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)))
240 {
241 res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL);
242
243 // compile_def_function() may cause def_functions.ga_data to change
244 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
245 }
246 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200247 {
248 if (did_emsg_cumul + did_emsg == did_emsg_before)
249 semsg(_(e_function_is_not_compiled_str),
250 printable_func_name(ufunc));
251 return FAIL;
252 }
253
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200254 if (ufunc->uf_va_name != NULL)
255 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200256 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200257 // Stack at time of call with 2 varargs:
258 // normal_arg
259 // optional_arg
260 // vararg_1
261 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200262 // After creating the list:
263 // normal_arg
264 // optional_arg
265 // vararg-list
266 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200267 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200268 // After creating the list
269 // normal_arg
270 // (space for optional_arg)
271 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200272 vararg_count = argcount - ufunc->uf_args.ga_len;
273 if (vararg_count < 0)
274 vararg_count = 0;
275 else
276 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200277 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200278 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200279
280 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200281 }
282
Bram Moolenaarfe270812020-04-11 22:31:27 +0200283 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200284 if (arg_to_add < 0)
285 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200286 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200287 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200288 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200289 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200290 return FAIL;
291 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200292
293 // Reserve space for:
294 // - missing arguments
295 // - stack frame
296 // - local variables
297 // - if needed: a counter for number of closures created in
298 // ectx->ec_funcrefs.
299 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200300 if (unlikely(ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE
301 + varcount) == FAIL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100302 return FAIL;
303
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100304 // If depth of calling is getting too high, don't execute the function.
305 if (funcdepth_increment() == FAIL)
306 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200307 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100308
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100309 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200310 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100311 {
312 floc = ALLOC_ONE(funclocal_T);
313 if (floc == NULL)
314 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200315 *floc = ectx->ec_funclocal;
316 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100317 }
318
Bram Moolenaarfe270812020-04-11 22:31:27 +0200319 // Move the vararg-list to below the missing optional arguments.
320 if (vararg_count > 0 && arg_to_add > 0)
321 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100322
323 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200324 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200325 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200326 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100327
328 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100329 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
330 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200331 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200332 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
333 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100334 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100335 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200336 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100337
338 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200339 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200341 if (dfunc->df_has_closure)
342 {
343 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
344
345 tv->v_type = VAR_NUMBER;
346 tv->vval.v_number = 0;
347 }
348 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100349
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100350 if (pt != NULL || ufunc->uf_partial != NULL
351 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100352 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200353 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100354
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200355 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100356 return FAIL;
357 if (pt != NULL)
358 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200359 ref->or_outer = &pt->pt_outer;
360 ++pt->pt_refcount;
361 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100362 }
363 else if (ufunc->uf_partial != NULL)
364 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200365 ref->or_outer = &ufunc->uf_partial->pt_outer;
366 ++ufunc->uf_partial->pt_refcount;
367 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100368 }
369 else
370 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200371 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200372 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200373 {
374 vim_free(ref);
375 return FAIL;
376 }
377 ref->or_outer_allocated = TRUE;
378 ref->or_outer->out_stack = &ectx->ec_stack;
379 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
380 if (ectx->ec_outer_ref != NULL)
381 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100382 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200383 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100384 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100385 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200386 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100387
Bram Moolenaarc970e422021-03-17 15:03:04 +0100388 ++ufunc->uf_calls;
389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390 // Set execution state to the start of the called function.
391 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100392 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100393 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200394 if (entry != NULL)
395 {
396 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200397 // Save the current context so it can be restored on return.
398 entry->es_save_sctx = current_sctx;
399 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200400 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100401
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200402 // Start execution at the first instruction.
403 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100404
405 return OK;
406}
407
408// Get pointer to item in the stack.
409#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
410
411/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200412 * Used when returning from a function: Check if any closure is still
413 * referenced. If so then move the arguments and variables to a separate piece
414 * of stack to be used when the closure is called.
415 * When "free_arguments" is TRUE the arguments are to be freed.
416 * Returns FAIL when out of memory.
417 */
418 static int
419handle_closure_in_use(ectx_T *ectx, int free_arguments)
420{
421 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
422 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200423 int argcount;
424 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200425 int idx;
426 typval_T *tv;
427 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200428 garray_T *gap = &ectx->ec_funcrefs;
429 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200430
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200431 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200432 return OK; // function was freed
433 if (dfunc->df_has_closure == 0)
434 return OK; // no closures
435 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
436 closure_count = tv->vval.v_number;
437 if (closure_count == 0)
438 return OK; // no funcrefs created
439
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200440 argcount = ufunc_argcount(dfunc->df_ufunc);
441 top = ectx->ec_frame_idx - argcount;
442
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200443 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200444 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200445 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200446 partial_T *pt;
447 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200448
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200449 if (off < 0)
450 continue; // count is off or already done
451 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200452 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200453 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200454 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200455 int i;
456
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200457 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200458 // unreferenced on return.
459 for (i = 0; i < dfunc->df_varcount; ++i)
460 {
461 typval_T *stv = STACK_TV(ectx->ec_frame_idx
462 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200463 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200464 --refcount;
465 }
466 if (refcount > 1)
467 {
468 closure_in_use = TRUE;
469 break;
470 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200471 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200472 }
473
474 if (closure_in_use)
475 {
476 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
477 typval_T *stack;
478
479 // A closure is using the arguments and/or local variables.
480 // Move them to the called function.
481 if (funcstack == NULL)
482 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200483 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
484 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200485 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
486 funcstack->fs_ga.ga_data = stack;
487 if (stack == NULL)
488 {
489 vim_free(funcstack);
490 return FAIL;
491 }
492
493 // Move or copy the arguments.
494 for (idx = 0; idx < argcount; ++idx)
495 {
496 tv = STACK_TV(top + idx);
497 if (free_arguments)
498 {
499 *(stack + idx) = *tv;
500 tv->v_type = VAR_UNKNOWN;
501 }
502 else
503 copy_tv(tv, stack + idx);
504 }
505 // Move the local variables.
506 for (idx = 0; idx < dfunc->df_varcount; ++idx)
507 {
508 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200509
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200510 // A partial created for a local function, that is also used as a
511 // local variable, has a reference count for the variable, thus
512 // will never go down to zero. When all these refcounts are one
513 // then the funcstack is unused. We need to count how many we have
514 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200515 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
516 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200517 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200518
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200519 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200520 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
521 gap->ga_len - closure_count + i])
522 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200523 }
524
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200525 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200526 tv->v_type = VAR_UNKNOWN;
527 }
528
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200529 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200530 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200531 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
532 - closure_count + idx];
533 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200534 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200535 ++funcstack->fs_refcount;
536 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100537 pt->pt_outer.out_stack = &funcstack->fs_ga;
538 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200539 }
540 }
541 }
542
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200543 for (idx = 0; idx < closure_count; ++idx)
544 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
545 - closure_count + idx]);
546 gap->ga_len -= closure_count;
547 if (gap->ga_len == 0)
548 ga_clear(gap);
549
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200550 return OK;
551}
552
553/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200554 * Called when a partial is freed or its reference count goes down to one. The
555 * funcstack may be the only reference to the partials in the local variables.
556 * Go over all of them, the funcref and can be freed if all partials
557 * referencing the funcstack have a reference count of one.
558 */
559 void
560funcstack_check_refcount(funcstack_T *funcstack)
561{
562 int i;
563 garray_T *gap = &funcstack->fs_ga;
564 int done = 0;
565
566 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
567 return;
568 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
569 {
570 typval_T *tv = ((typval_T *)gap->ga_data) + i;
571
572 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
573 && tv->vval.v_partial->pt_funcstack == funcstack
574 && tv->vval.v_partial->pt_refcount == 1)
575 ++done;
576 }
577 if (done == funcstack->fs_min_refcount)
578 {
579 typval_T *stack = gap->ga_data;
580
581 // All partials referencing the funcstack have a reference count of
582 // one, thus the funcstack is no longer of use.
583 for (i = 0; i < gap->ga_len; ++i)
584 clear_tv(stack + i);
585 vim_free(stack);
586 vim_free(funcstack);
587 }
588}
589
590/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 * Return from the current function.
592 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200593 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200594func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100597 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200598 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
599 + ectx->ec_dfunc_idx;
600 int argcount = ufunc_argcount(dfunc->df_ufunc);
601 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200602 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100603 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
604 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200605 funclocal_T *floc;
606#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100607 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
608 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
Bram Moolenaar12d26532021-02-19 19:13:21 +0100610 if (do_profiling == PROF_YES)
611 {
612 ufunc_T *caller = prev_dfunc->df_ufunc;
613
614 if (dfunc->df_ufunc->uf_profiling
615 || (caller != NULL && caller->uf_profiling))
616 {
617 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
618 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
619 --profile_info_ga.ga_len;
620 }
621 }
622#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100623 // TODO: when is it safe to delete the function when it is no longer used?
624 --dfunc->df_ufunc->uf_calls;
625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200627 entry = estack_pop();
628 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200629 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100630
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200631 if (handle_closure_in_use(ectx, TRUE) == FAIL)
632 return FAIL;
633
634 // Clear the arguments.
635 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
636 clear_tv(STACK_TV(idx));
637
638 // Clear local variables and temp values, but not the return value.
639 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100640 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100642
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100643 // The return value should be on top of the stack. However, when aborting
644 // it may not be there and ec_frame_idx is the top of the stack.
645 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100646 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100647 ret_idx = 0;
648
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200649 if (ectx->ec_outer_ref != NULL)
650 {
651 if (ectx->ec_outer_ref->or_outer_allocated)
652 vim_free(ectx->ec_outer_ref->or_outer);
653 partial_unref(ectx->ec_outer_ref->or_partial);
654 vim_free(ectx->ec_outer_ref);
655 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100656
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100657 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100658 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100659 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
660 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200661 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
662 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200663 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100664 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100665 floc = (void *)STACK_TV(ectx->ec_frame_idx
666 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200667 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100668 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
669 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200670
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100671 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200672 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100673 else
674 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200675 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100676 vim_free(floc);
677 }
678
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100679 if (ret_idx > 0)
680 {
681 // Reset the stack to the position before the call, with a spot for the
682 // return value, moved there from above the frame.
683 ectx->ec_stack.ga_len = top + 1;
684 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
685 }
686 else
687 // Reset the stack to the position before the call.
688 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200689
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100690 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200691 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200692 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693}
694
695#undef STACK_TV
696
697/*
698 * Prepare arguments and rettv for calling a builtin or user function.
699 */
700 static int
701call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
702{
703 int idx;
704 typval_T *tv;
705
706 // Move arguments from bottom of the stack to argvars[] and add terminator.
707 for (idx = 0; idx < argcount; ++idx)
708 argvars[idx] = *STACK_TV_BOT(idx - argcount);
709 argvars[argcount].v_type = VAR_UNKNOWN;
710
711 // Result replaces the arguments on the stack.
712 if (argcount > 0)
713 ectx->ec_stack.ga_len -= argcount - 1;
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200714 else if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 return FAIL;
716 else
717 ++ectx->ec_stack.ga_len;
718
719 // Default return value is zero.
720 tv = STACK_TV_BOT(-1);
721 tv->v_type = VAR_NUMBER;
722 tv->vval.v_number = 0;
723
724 return OK;
725}
726
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200727// Ugly global to avoid passing the execution context around through many
728// layers.
729static ectx_T *current_ectx = NULL;
730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731/*
732 * Call a builtin function by index.
733 */
734 static int
735call_bfunc(int func_idx, int argcount, ectx_T *ectx)
736{
737 typval_T argvars[MAX_FUNC_ARGS];
738 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100739 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200740 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200741 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742
743 if (call_prepare(argcount, argvars, ectx) == FAIL)
744 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200745 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200747 // Call the builtin function. Set "current_ectx" so that when it
748 // recursively invokes call_def_function() a closure context can be set.
749 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200751 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200752 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753
754 // Clear the arguments.
755 for (idx = 0; idx < argcount; ++idx)
756 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200757
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100758 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200759 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 return OK;
761}
762
763/*
764 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100765 * If the function is compiled this will add a stack frame and set the
766 * instruction pointer at the start of the function.
767 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200768 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100769 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 */
771 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100772call_ufunc(
773 ufunc_T *ufunc,
774 partial_T *pt,
775 int argcount,
776 ectx_T *ectx,
777 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778{
779 typval_T argvars[MAX_FUNC_ARGS];
780 funcexe_T funcexe;
781 int error;
782 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100783 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200784 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785
Bram Moolenaare99d4222021-06-13 14:01:26 +0200786 if (func_needs_compiling(ufunc, compile_type)
787 && compile_def_function(ufunc, FALSE, compile_type, NULL)
788 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200789 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200790 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100791 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100792 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100793 if (error != FCERR_UNKNOWN)
794 {
795 if (error == FCERR_TOOMANY)
796 semsg(_(e_toomanyarg), ufunc->uf_name);
797 else
798 semsg(_(e_toofewarg), ufunc->uf_name);
799 return FAIL;
800 }
801
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100802 // The function has been compiled, can call it quickly. For a function
803 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100804 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100805 if (iptr != NULL)
806 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100807 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100808 iptr->isn_type = ISN_DCALL;
809 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
810 iptr->isn_arg.dfunc.cdf_argcount = argcount;
811 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200812 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100813 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814
815 if (call_prepare(argcount, argvars, ectx) == FAIL)
816 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200817 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818 funcexe.evaluate = TRUE;
819
820 // Call the user function. Result goes in last position on the stack.
821 // TODO: add selfdict if there is one
822 error = call_user_func_check(ufunc, argcount, argvars,
823 STACK_TV_BOT(-1), &funcexe, NULL);
824
825 // Clear the arguments.
826 for (idx = 0; idx < argcount; ++idx)
827 clear_tv(&argvars[idx]);
828
829 if (error != FCERR_NONE)
830 {
831 user_func_error(error, ufunc->uf_name);
832 return FAIL;
833 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100834 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200835 // Error other than from calling the function itself.
836 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 return OK;
838}
839
840/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100841 * If command modifiers were applied restore them.
842 */
843 static void
844may_restore_cmdmod(funclocal_T *funclocal)
845{
846 if (funclocal->floc_restore_cmdmod)
847 {
848 cmdmod.cmod_filter_regmatch.regprog = NULL;
849 undo_cmdmod(&cmdmod);
850 cmdmod = funclocal->floc_save_cmdmod;
851 funclocal->floc_restore_cmdmod = FALSE;
852 }
853}
854
855/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200856 * Return TRUE if an error was given or CTRL-C was pressed.
857 */
858 static int
859vim9_aborting(int prev_called_emsg)
860{
861 return called_emsg > prev_called_emsg || got_int || did_throw;
862}
863
864/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 * Execute a function by "name".
866 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100867 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 * Returns FAIL if not found without an error message.
869 */
870 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100871call_by_name(
872 char_u *name,
873 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100874 ectx_T *ectx,
875 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876{
877 ufunc_T *ufunc;
878
879 if (builtin_function(name, -1))
880 {
881 int func_idx = find_internal_func(name);
882
883 if (func_idx < 0)
884 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200885 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886 return FAIL;
887 return call_bfunc(func_idx, argcount, ectx);
888 }
889
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200890 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200891
892 if (ufunc == NULL)
893 {
894 int called_emsg_before = called_emsg;
895
896 if (script_autoload(name, TRUE))
897 // loaded a package, search for the function again
898 ufunc = find_func(name, FALSE, NULL);
899 if (vim9_aborting(called_emsg_before))
900 return FAIL; // bail out if loading the script caused an error
901 }
902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100904 {
905 if (ufunc->uf_arg_types != NULL)
906 {
907 int i;
908 typval_T *argv = STACK_TV_BOT(0) - argcount;
909
910 // The function can change at runtime, check that the argument
911 // types are correct.
912 for (i = 0; i < argcount; ++i)
913 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100914 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100915
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100916 if (i < ufunc->uf_args.ga_len)
917 type = ufunc->uf_arg_types[i];
918 else if (ufunc->uf_va_type != NULL)
919 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100920 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200921 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100922 return FAIL;
923 }
924 }
925
Bram Moolenaar4c137212021-04-19 16:48:48 +0200926 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928
929 return FAIL;
930}
931
932 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100933call_partial(
934 typval_T *tv,
935 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100936 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200938 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200939 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100941 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942
943 if (tv->v_type == VAR_PARTIAL)
944 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200945 partial_T *pt = tv->vval.v_partial;
946 int i;
947
948 if (pt->pt_argc > 0)
949 {
950 // Make space for arguments from the partial, shift the "argcount"
951 // arguments up.
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200952 if (unlikely(ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL))
Bram Moolenaara90afb92020-07-15 22:38:56 +0200953 return FAIL;
954 for (i = 1; i <= argcount; ++i)
955 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
956 ectx->ec_stack.ga_len += pt->pt_argc;
957 argcount += pt->pt_argc;
958
959 // copy the arguments from the partial onto the stack
960 for (i = 0; i < pt->pt_argc; ++i)
961 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
962 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963
964 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200965 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 name = pt->pt_name;
968 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200969 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200971 if (name != NULL)
972 {
973 char_u fname_buf[FLEN_FIXED + 1];
974 char_u *tofree = NULL;
975 int error = FCERR_NONE;
976 char_u *fname;
977
978 // May need to translate <SNR>123_ to K_SNR.
979 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
980 if (error != FCERR_NONE)
981 res = FAIL;
982 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200983 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200984 vim_free(tofree);
985 }
986
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100987 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100988 {
989 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200990 semsg(_(e_unknownfunc),
991 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992 return FAIL;
993 }
994 return OK;
995}
996
997/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200998 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
999 * TRUE.
1000 */
1001 static int
1002error_if_locked(int lock, char *error)
1003{
1004 if (lock & (VAR_LOCKED | VAR_FIXED))
1005 {
1006 emsg(_(error));
1007 return TRUE;
1008 }
1009 return FALSE;
1010}
1011
1012/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001013 * Give an error if "tv" is not a number and return FAIL.
1014 */
1015 static int
1016check_for_number(typval_T *tv)
1017{
1018 if (tv->v_type != VAR_NUMBER)
1019 {
1020 semsg(_(e_expected_str_but_got_str),
1021 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1022 return FAIL;
1023 }
1024 return OK;
1025}
1026
1027/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001028 * Store "tv" in variable "name".
1029 * This is for s: and g: variables.
1030 */
1031 static void
1032store_var(char_u *name, typval_T *tv)
1033{
1034 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001035 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001036
Bram Moolenaard877a572021-04-01 19:42:48 +02001037 if (tv->v_lock)
1038 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001039 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001040 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001041 restore_funccal();
1042}
1043
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001044/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001045 * Convert "tv" to a string.
1046 * Return FAIL if not allowed.
1047 */
1048 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001049do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001050{
1051 if (tv->v_type != VAR_STRING)
1052 {
1053 char_u *str;
1054
1055 if (is_2string_any)
1056 {
1057 switch (tv->v_type)
1058 {
1059 case VAR_SPECIAL:
1060 case VAR_BOOL:
1061 case VAR_NUMBER:
1062 case VAR_FLOAT:
1063 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001064
1065 case VAR_LIST:
1066 if (tolerant)
1067 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001068 char_u *s, *e, *p;
1069 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001070
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001071 ga_init2(&ga, sizeof(char_u *), 1);
1072
1073 // Convert to NL separated items, then
1074 // escape the items and replace the NL with
1075 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001076 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001077 if (str == NULL)
1078 return FAIL;
1079 s = str;
1080 while ((e = vim_strchr(s, '\n')) != NULL)
1081 {
1082 *e = NUL;
1083 p = vim_strsave_fnameescape(s, FALSE);
1084 if (p != NULL)
1085 {
1086 ga_concat(&ga, p);
1087 ga_concat(&ga, (char_u *)" ");
1088 vim_free(p);
1089 }
1090 s = e + 1;
1091 }
1092 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001093 clear_tv(tv);
1094 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001095 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001096 return OK;
1097 }
1098 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001099 default: to_string_error(tv->v_type);
1100 return FAIL;
1101 }
1102 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001103 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001104 clear_tv(tv);
1105 tv->v_type = VAR_STRING;
1106 tv->vval.v_string = str;
1107 }
1108 return OK;
1109}
1110
1111/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001112 * When the value of "sv" is a null list of dict, allocate it.
1113 */
1114 static void
1115allocate_if_null(typval_T *tv)
1116{
1117 switch (tv->v_type)
1118 {
1119 case VAR_LIST:
1120 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001121 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001122 break;
1123 case VAR_DICT:
1124 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001125 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001126 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001127 case VAR_BLOB:
1128 if (tv->vval.v_blob == NULL)
1129 (void)rettv_blob_alloc(tv);
1130 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001131 default:
1132 break;
1133 }
1134}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001135
Bram Moolenaare7525c52021-01-09 13:20:37 +01001136/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001137 * Return the character "str[index]" where "index" is the character index,
1138 * including composing characters.
1139 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001140 */
1141 char_u *
1142char_from_string(char_u *str, varnumber_T index)
1143{
1144 size_t nbyte = 0;
1145 varnumber_T nchar = index;
1146 size_t slen;
1147
1148 if (str == NULL)
1149 return NULL;
1150 slen = STRLEN(str);
1151
Bram Moolenaarff871402021-03-26 13:34:05 +01001152 // Do the same as for a list: a negative index counts from the end.
1153 // Optimization to check the first byte to be below 0x80 (and no composing
1154 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001155 if (index < 0)
1156 {
1157 int clen = 0;
1158
1159 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001160 {
1161 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1162 ++nbyte;
1163 else if (enc_utf8)
1164 nbyte += utfc_ptr2len(str + nbyte);
1165 else
1166 nbyte += mb_ptr2len(str + nbyte);
1167 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001168 nchar = clen + index;
1169 if (nchar < 0)
1170 // unlike list: index out of range results in empty string
1171 return NULL;
1172 }
1173
1174 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001175 {
1176 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1177 ++nbyte;
1178 else if (enc_utf8)
1179 nbyte += utfc_ptr2len(str + nbyte);
1180 else
1181 nbyte += mb_ptr2len(str + nbyte);
1182 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001183 if (nbyte >= slen)
1184 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001185 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001186}
1187
1188/*
1189 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001190 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001191 * If going over the end return "str_len".
1192 * If "idx" is negative count from the end, -1 is the last character.
1193 * When going over the start return -1.
1194 */
1195 static long
1196char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1197{
1198 varnumber_T nchar = idx;
1199 size_t nbyte = 0;
1200
1201 if (nchar >= 0)
1202 {
1203 while (nchar > 0 && nbyte < str_len)
1204 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001205 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001206 --nchar;
1207 }
1208 }
1209 else
1210 {
1211 nbyte = str_len;
1212 while (nchar < 0 && nbyte > 0)
1213 {
1214 --nbyte;
1215 nbyte -= mb_head_off(str, str + nbyte);
1216 ++nchar;
1217 }
1218 if (nchar < 0)
1219 return -1;
1220 }
1221 return (long)nbyte;
1222}
1223
1224/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001225 * Return the slice "str[first : last]" using character indexes. Composing
1226 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001227 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001228 * Return NULL when the result is empty.
1229 */
1230 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001231string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001232{
1233 long start_byte, end_byte;
1234 size_t slen;
1235
1236 if (str == NULL)
1237 return NULL;
1238 slen = STRLEN(str);
1239 start_byte = char_idx2byte(str, slen, first);
1240 if (start_byte < 0)
1241 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001242 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001243 end_byte = (long)slen;
1244 else
1245 {
1246 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001247 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001248 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001249 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001250 }
1251
1252 if (start_byte >= (long)slen || end_byte <= start_byte)
1253 return NULL;
1254 return vim_strnsave(str + start_byte, end_byte - start_byte);
1255}
1256
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001257 static svar_T *
1258get_script_svar(scriptref_T *sref, ectx_T *ectx)
1259{
1260 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1261 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1262 + ectx->ec_dfunc_idx;
1263 svar_T *sv;
1264
1265 if (sref->sref_seq != si->sn_script_seq)
1266 {
1267 // The script was reloaded after the function was
1268 // compiled, the script_idx may not be valid.
1269 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1270 dfunc->df_ufunc->uf_name_exp);
1271 return NULL;
1272 }
1273 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001274 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001275 {
1276 emsg(_(e_script_variable_type_changed));
1277 return NULL;
1278 }
1279 return sv;
1280}
1281
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001282/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001283 * Function passed to do_cmdline() for splitting a script joined by NL
1284 * characters.
1285 */
1286 static char_u *
1287get_split_sourceline(
1288 int c UNUSED,
1289 void *cookie,
1290 int indent UNUSED,
1291 getline_opt_T options UNUSED)
1292{
1293 source_cookie_T *sp = (source_cookie_T *)cookie;
1294 char_u *p;
1295 char_u *line;
1296
1297 if (*sp->nextline == NUL)
1298 return NULL;
1299 p = vim_strchr(sp->nextline, '\n');
1300 if (p == NULL)
1301 {
1302 line = vim_strsave(sp->nextline);
1303 sp->nextline += STRLEN(sp->nextline);
1304 }
1305 else
1306 {
1307 line = vim_strnsave(sp->nextline, p - sp->nextline);
1308 sp->nextline = p + 1;
1309 }
1310 return line;
1311}
1312
1313/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 * Execute a function by "name".
1315 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001316 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 */
1318 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001319call_eval_func(
1320 char_u *name,
1321 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001322 ectx_T *ectx,
1323 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324{
Bram Moolenaared677f52020-08-12 16:38:10 +02001325 int called_emsg_before = called_emsg;
1326 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
Bram Moolenaar4c137212021-04-19 16:48:48 +02001328 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001329 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001331 dictitem_T *v;
1332
1333 v = find_var(name, NULL, FALSE);
1334 if (v == NULL)
1335 {
1336 semsg(_(e_unknownfunc), name);
1337 return FAIL;
1338 }
1339 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1340 {
1341 semsg(_(e_unknownfunc), name);
1342 return FAIL;
1343 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001344 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001346 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347}
1348
1349/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001350 * When a function reference is used, fill a partial with the information
1351 * needed, especially when it is used as a closure.
1352 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001353 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001354fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1355{
1356 pt->pt_func = ufunc;
1357 pt->pt_refcount = 1;
1358
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001359 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001360 {
1361 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1362 + ectx->ec_dfunc_idx;
1363
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001364 // The closure may need to find arguments and local variables in the
1365 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001366 pt->pt_outer.out_stack = &ectx->ec_stack;
1367 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001368 if (ectx->ec_outer_ref != NULL)
1369 {
1370 // The current context already has a context, link to that one.
1371 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1372 if (ectx->ec_outer_ref->or_partial != NULL)
1373 {
1374 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1375 ++pt->pt_outer.out_up_partial->pt_refcount;
1376 }
1377 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001378
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001379 // If this function returns and the closure is still being used, we
1380 // need to make a copy of the context (arguments and local variables).
1381 // Store a reference to the partial so we can handle that.
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001382 if (unlikely(ga_grow(&ectx->ec_funcrefs, 1) == FAIL))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001383 {
1384 vim_free(pt);
1385 return FAIL;
1386 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001387 // Extra variable keeps the count of closures created in the current
1388 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001389 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1390 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1391
1392 ((partial_T **)ectx->ec_funcrefs.ga_data)
1393 [ectx->ec_funcrefs.ga_len] = pt;
1394 ++pt->pt_refcount;
1395 ++ectx->ec_funcrefs.ga_len;
1396 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001397 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001398 return OK;
1399}
1400
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001401// used for v_instr of typval of VAR_INSTR
1402struct instr_S {
1403 ectx_T *instr_ectx;
1404 isn_T *instr_instr;
1405};
1406
Bram Moolenaar4c137212021-04-19 16:48:48 +02001407// used for substitute_instr
1408typedef struct subs_expr_S {
1409 ectx_T *subs_ectx;
1410 isn_T *subs_instr;
1411 int subs_status;
1412} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413
1414// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001415#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416
1417// Get pointer to item at the bottom of the stack, -1 is the bottom.
1418#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001419#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 +01001420
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001421// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001422#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 +01001423
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001424// Set when calling do_debug().
1425static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001426static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001427
1428/*
1429 * When debugging lookup "name" and return the typeval.
1430 * When not found return NULL.
1431 */
1432 typval_T *
1433lookup_debug_var(char_u *name)
1434{
1435 int idx;
1436 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001437 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001438 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001439 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001440
1441 if (ectx == NULL)
1442 return NULL;
1443 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1444
1445 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001446 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001447 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001448 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001449 return STACK_TV_VAR(idx);
1450 }
1451
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001452 // Go through argument names.
1453 ufunc = dfunc->df_ufunc;
1454 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1455 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1456 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1457 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1458 - varargs_off + idx);
1459 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1460 return STACK_TV(ectx->ec_frame_idx - 1);
1461
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001462 return NULL;
1463}
1464
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001465 static void
1466handle_debug(isn_T *iptr, ectx_T *ectx)
1467{
1468 char_u *line;
1469 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1470 + ectx->ec_dfunc_idx)->df_ufunc;
1471 isn_T *ni;
1472 int end_lnum = iptr->isn_lnum;
1473 garray_T ga;
1474 int lnum;
1475
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001476 if (ex_nesting_level > debug_break_level)
1477 {
1478 linenr_T breakpoint;
1479
1480 if (!ufunc->uf_has_breakpoint)
1481 return;
1482
1483 // check for the next breakpoint if needed
1484 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001485 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001486 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1487 return;
1488 }
1489
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001490 SOURCING_LNUM = iptr->isn_lnum;
1491 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001492 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001493
1494 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1495 if (ni->isn_type == ISN_DEBUG
1496 || ni->isn_type == ISN_RETURN
1497 || ni->isn_type == ISN_RETURN_VOID)
1498 {
1499 end_lnum = ni->isn_lnum;
1500 break;
1501 }
1502
1503 if (end_lnum > iptr->isn_lnum)
1504 {
1505 ga_init2(&ga, sizeof(char_u *), 10);
1506 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001507 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001508 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001509
Bram Moolenaar303215d2021-07-07 20:10:43 +02001510 if (p == NULL)
1511 continue; // left over from continuation line
1512 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001513 if (*p == '#')
1514 break;
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001515 if (likely(ga_grow(&ga, 1) == OK))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001516 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1517 if (STRNCMP(p, "def ", 4) == 0)
1518 break;
1519 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001520 line = ga_concat_strings(&ga, " ");
1521 vim_free(ga.ga_data);
1522 }
1523 else
1524 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001525
Dominique Pelle6e969552021-06-17 13:53:41 +02001526 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001527 debug_context = NULL;
1528
1529 if (end_lnum > iptr->isn_lnum)
1530 vim_free(line);
1531}
1532
Bram Moolenaar4c137212021-04-19 16:48:48 +02001533/*
1534 * Execute instructions in execution context "ectx".
1535 * Return OK or FAIL;
1536 */
1537 static int
1538exec_instructions(ectx_T *ectx)
1539{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001540 int ret = FAIL;
1541 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001542
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001543 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001544 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001545
Bram Moolenaarff652882021-05-16 15:24:49 +02001546 // Only catch exceptions in this instruction list.
1547 ectx->ec_trylevel_at_start = trylevel;
1548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 for (;;)
1550 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001551 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001553 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001554
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001555 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001556 {
1557 line_breakcheck();
1558 breakcheck_count = 0;
1559 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001560 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001561 {
1562 // Turn CTRL-C into an exception.
1563 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001564 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001565 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001566 did_throw = TRUE;
1567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001569 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001570 {
1571 // Turn an error message into an exception.
1572 did_emsg = FALSE;
1573 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001574 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001575 did_throw = TRUE;
1576 *msg_list = NULL;
1577 }
1578
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001579 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001581 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001582 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001583 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584
1585 // An exception jumps to the first catch, finally, or returns from
1586 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001587 while (index > 0)
1588 {
1589 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001590 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001591 break;
1592 // In the catch and finally block of this try we have to go up
1593 // one level.
1594 --index;
1595 trycmd = NULL;
1596 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001597 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001599 if (trycmd->tcd_in_catch)
1600 {
1601 // exception inside ":catch", jump to ":finally" once
1602 ectx->ec_iidx = trycmd->tcd_finally_idx;
1603 trycmd->tcd_finally_idx = 0;
1604 }
1605 else
1606 // jump to first ":catch"
1607 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001608 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001609 did_throw = FALSE; // don't come back here until :endtry
1610 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 }
1612 else
1613 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001614 // Not inside try or need to return from current functions.
1615 // Push a dummy return value.
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001616 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001617 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001618 tv = STACK_TV_BOT(0);
1619 tv->v_type = VAR_NUMBER;
1620 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001621 ++ectx->ec_stack.ga_len;
1622 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001624 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001625 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001626 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001627 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 goto done;
1629 }
1630
Bram Moolenaar4c137212021-04-19 16:48:48 +02001631 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001632 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 }
1634 continue;
1635 }
1636
Bram Moolenaar4c137212021-04-19 16:48:48 +02001637 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 switch (iptr->isn_type)
1639 {
1640 // execute Ex command line
1641 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001642 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001643 source_cookie_T cookie;
1644
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001645 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001646 // Pass getsourceline to get an error for a missing ":end"
1647 // command.
1648 CLEAR_FIELD(cookie);
1649 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1650 if (do_cmdline(iptr->isn_arg.string,
1651 getsourceline, &cookie,
1652 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1653 == FAIL
1654 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001655 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 break;
1658
Bram Moolenaar20677332021-06-06 17:02:53 +02001659 // execute Ex command line split at NL characters.
1660 case ISN_EXEC_SPLIT:
1661 {
1662 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001663 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001664
1665 SOURCING_LNUM = iptr->isn_lnum;
1666 CLEAR_FIELD(cookie);
1667 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1668 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001669 line = get_split_sourceline(0, &cookie, 0, 0);
1670 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001671 get_split_sourceline, &cookie,
1672 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1673 == FAIL
1674 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001675 {
1676 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001677 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001678 }
1679 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001680 }
1681 break;
1682
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001683 // Evaluate an expression with legacy syntax, push it onto the
1684 // stack.
1685 case ISN_LEGACY_EVAL:
1686 {
1687 char_u *arg = iptr->isn_arg.string;
1688 int res;
1689 int save_flags = cmdmod.cmod_flags;
1690
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001691 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001692 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001693 tv = STACK_TV_BOT(0);
1694 init_tv(tv);
1695 cmdmod.cmod_flags |= CMOD_LEGACY;
1696 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1697 cmdmod.cmod_flags = save_flags;
1698 if (res == FAIL)
1699 goto on_error;
1700 ++ectx->ec_stack.ga_len;
1701 }
1702 break;
1703
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001704 // push typeval VAR_INSTR with instructions to be executed
1705 case ISN_INSTR:
1706 {
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001707 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001708 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001709 tv = STACK_TV_BOT(0);
1710 tv->vval.v_instr = ALLOC_ONE(instr_T);
1711 if (tv->vval.v_instr == NULL)
1712 goto on_error;
1713 ++ectx->ec_stack.ga_len;
1714
1715 tv->v_type = VAR_INSTR;
1716 tv->vval.v_instr->instr_ectx = ectx;
1717 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1718 }
1719 break;
1720
Bram Moolenaar4c137212021-04-19 16:48:48 +02001721 // execute :substitute with an expression
1722 case ISN_SUBSTITUTE:
1723 {
1724 subs_T *subs = &iptr->isn_arg.subs;
1725 source_cookie_T cookie;
1726 struct subs_expr_S *save_instr = substitute_instr;
1727 struct subs_expr_S subs_instr;
1728 int res;
1729
1730 subs_instr.subs_ectx = ectx;
1731 subs_instr.subs_instr = subs->subs_instr;
1732 subs_instr.subs_status = OK;
1733 substitute_instr = &subs_instr;
1734
1735 SOURCING_LNUM = iptr->isn_lnum;
1736 // This is very much like ISN_EXEC
1737 CLEAR_FIELD(cookie);
1738 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1739 res = do_cmdline(subs->subs_cmd,
1740 getsourceline, &cookie,
1741 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1742 substitute_instr = save_instr;
1743
1744 if (res == FAIL || did_emsg
1745 || subs_instr.subs_status == FAIL)
1746 goto on_error;
1747 }
1748 break;
1749
1750 case ISN_FINISH:
1751 goto done;
1752
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001753 case ISN_REDIRSTART:
1754 // create a dummy entry for var_redir_str()
1755 if (alloc_redir_lval() == FAIL)
1756 goto on_error;
1757
1758 // The output is stored in growarray "redir_ga" until
1759 // redirection ends.
1760 init_redir_ga();
1761 redir_vname = 1;
1762 break;
1763
1764 case ISN_REDIREND:
1765 {
1766 char_u *res = get_clear_redir_ga();
1767
1768 // End redirection, put redirected text on the stack.
1769 clear_redir_lval();
1770 redir_vname = 0;
1771
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001772 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001773 {
1774 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001775 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001776 }
1777 tv = STACK_TV_BOT(0);
1778 tv->v_type = VAR_STRING;
1779 tv->vval.v_string = res;
1780 ++ectx->ec_stack.ga_len;
1781 }
1782 break;
1783
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001784 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001785#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001786 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1787 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001788#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001789 break;
1790
1791 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001792#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001793 {
1794 exarg_T ea;
1795 int res;
1796
1797 CLEAR_FIELD(ea);
1798 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1799 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1800 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1801 --ectx->ec_stack.ga_len;
1802 tv = STACK_TV_BOT(0);
1803 res = cexpr_core(&ea, tv);
1804 clear_tv(tv);
1805 if (res == FAIL)
1806 goto on_error;
1807 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001808#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001809 break;
1810
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001811 // execute Ex command from pieces on the stack
1812 case ISN_EXECCONCAT:
1813 {
1814 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001815 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001816 int pass;
1817 int i;
1818 char_u *cmd = NULL;
1819 char_u *str;
1820
1821 for (pass = 1; pass <= 2; ++pass)
1822 {
1823 for (i = 0; i < count; ++i)
1824 {
1825 tv = STACK_TV_BOT(i - count);
1826 str = tv->vval.v_string;
1827 if (str != NULL && *str != NUL)
1828 {
1829 if (pass == 2)
1830 STRCPY(cmd + len, str);
1831 len += STRLEN(str);
1832 }
1833 if (pass == 2)
1834 clear_tv(tv);
1835 }
1836 if (pass == 1)
1837 {
1838 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001839 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001840 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001841 len = 0;
1842 }
1843 }
1844
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001845 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001846 do_cmdline_cmd(cmd);
1847 vim_free(cmd);
1848 }
1849 break;
1850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 // execute :echo {string} ...
1852 case ISN_ECHO:
1853 {
1854 int count = iptr->isn_arg.echo.echo_count;
1855 int atstart = TRUE;
1856 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001857 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858
1859 for (idx = 0; idx < count; ++idx)
1860 {
1861 tv = STACK_TV_BOT(idx - count);
1862 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1863 &atstart, &needclr);
1864 clear_tv(tv);
1865 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001866 if (needclr)
1867 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001868 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 }
1870 break;
1871
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001872 // :execute {string} ...
1873 // :echomsg {string} ...
1874 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001875 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001876 case ISN_ECHOMSG:
1877 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001878 {
1879 int count = iptr->isn_arg.number;
1880 garray_T ga;
1881 char_u buf[NUMBUFLEN];
1882 char_u *p;
1883 int len;
1884 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001885 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001886
1887 ga_init2(&ga, 1, 80);
1888 for (idx = 0; idx < count; ++idx)
1889 {
1890 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001891 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001892 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001893 if (tv->v_type == VAR_CHANNEL
1894 || tv->v_type == VAR_JOB)
1895 {
1896 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001897 semsg(_(e_using_invalid_value_as_string_str),
1898 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001899 break;
1900 }
1901 else
1902 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001903 }
1904 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001905 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001906
1907 len = (int)STRLEN(p);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001908 if (unlikely(ga_grow(&ga, len + 2) == FAIL))
Bram Moolenaarad39c092020-02-26 18:23:43 +01001909 failed = TRUE;
1910 else
1911 {
1912 if (ga.ga_len > 0)
1913 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1914 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1915 ga.ga_len += len;
1916 }
1917 clear_tv(tv);
1918 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001919 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001920 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001921 {
1922 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001923 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001924 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001925
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001926 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001927 {
1928 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001929 {
1930 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001931 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001932 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001933 {
1934 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001935 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001936 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001937 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001938 else
1939 {
1940 msg_sb_eol();
1941 if (iptr->isn_type == ISN_ECHOMSG)
1942 {
1943 msg_attr(ga.ga_data, echo_attr);
1944 out_flush();
1945 }
1946 else
1947 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001948 SOURCING_LNUM = iptr->isn_lnum;
1949 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001950 }
1951 }
1952 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001953 ga_clear(&ga);
1954 }
1955 break;
1956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 // load local variable or argument
1958 case ISN_LOAD:
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001959 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001960 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001962 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 break;
1964
1965 // load v: variable
1966 case ISN_LOADV:
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001967 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001968 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001970 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 break;
1972
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001973 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 case ISN_LOADSCRIPT:
1975 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001976 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 svar_T *sv;
1978
Bram Moolenaar4c137212021-04-19 16:48:48 +02001979 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001980 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001981 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001982 allocate_if_null(sv->sv_tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001983 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001984 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001986 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 }
1988 break;
1989
1990 // load s: variable in old script
1991 case ISN_LOADS:
1992 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001993 hashtab_T *ht = &SCRIPT_VARS(
1994 iptr->isn_arg.loadstore.ls_sid);
1995 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 if (di == NULL)
1999 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002000 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002001 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002002 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 }
2004 else
2005 {
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002006 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002007 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002009 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 }
2011 }
2012 break;
2013
Bram Moolenaard3aac292020-04-19 14:32:17 +02002014 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002016 case ISN_LOADB:
2017 case ISN_LOADW:
2018 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002020 dictitem_T *di = NULL;
2021 hashtab_T *ht = NULL;
2022 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002023
Bram Moolenaard3aac292020-04-19 14:32:17 +02002024 switch (iptr->isn_type)
2025 {
2026 case ISN_LOADG:
2027 ht = get_globvar_ht();
2028 namespace = 'g';
2029 break;
2030 case ISN_LOADB:
2031 ht = &curbuf->b_vars->dv_hashtab;
2032 namespace = 'b';
2033 break;
2034 case ISN_LOADW:
2035 ht = &curwin->w_vars->dv_hashtab;
2036 namespace = 'w';
2037 break;
2038 case ISN_LOADT:
2039 ht = &curtab->tp_vars->dv_hashtab;
2040 namespace = 't';
2041 break;
2042 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002043 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002044 }
2045 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 if (di == NULL)
2048 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002049 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002050 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002051 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002052 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 }
2054 else
2055 {
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002056 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002057 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002059 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 }
2061 }
2062 break;
2063
Bram Moolenaar03290b82020-12-19 16:30:44 +01002064 // load autoload variable
2065 case ISN_LOADAUTO:
2066 {
2067 char_u *name = iptr->isn_arg.string;
2068
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002069 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002070 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002071 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002072 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002073 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002074 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002075 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002076 }
2077 break;
2078
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002079 // load g:/b:/w:/t: namespace
2080 case ISN_LOADGDICT:
2081 case ISN_LOADBDICT:
2082 case ISN_LOADWDICT:
2083 case ISN_LOADTDICT:
2084 {
2085 dict_T *d = NULL;
2086
2087 switch (iptr->isn_type)
2088 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002089 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2090 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2091 case ISN_LOADWDICT: d = curwin->w_vars; break;
2092 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002093 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002094 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002095 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002096 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002097 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002098 tv = STACK_TV_BOT(0);
2099 tv->v_type = VAR_DICT;
2100 tv->v_lock = 0;
2101 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002102 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002103 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002104 }
2105 break;
2106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 // load &option
2108 case ISN_LOADOPT:
2109 {
2110 typval_T optval;
2111 char_u *name = iptr->isn_arg.string;
2112
Bram Moolenaara8c17702020-04-01 21:17:24 +02002113 // This is not expected to fail, name is checked during
2114 // compilation: don't set SOURCING_LNUM.
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002115 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002116 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002117 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002118 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002120 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 }
2122 break;
2123
2124 // load $ENV
2125 case ISN_LOADENV:
2126 {
2127 typval_T optval;
2128 char_u *name = iptr->isn_arg.string;
2129
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002130 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002131 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002132 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002133 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002135 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 }
2137 break;
2138
2139 // load @register
2140 case ISN_LOADREG:
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002141 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002142 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 tv = STACK_TV_BOT(0);
2144 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002145 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002146 // This may result in NULL, which should be equivalent to an
2147 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 tv->vval.v_string = get_reg_contents(
2149 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002150 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 break;
2152
2153 // store local variable
2154 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002155 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 tv = STACK_TV_VAR(iptr->isn_arg.number);
2157 clear_tv(tv);
2158 *tv = *STACK_TV_BOT(0);
2159 break;
2160
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002161 // store s: variable in old script
2162 case ISN_STORES:
2163 {
2164 hashtab_T *ht = &SCRIPT_VARS(
2165 iptr->isn_arg.loadstore.ls_sid);
2166 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002167 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002168
Bram Moolenaar4c137212021-04-19 16:48:48 +02002169 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002170 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002171 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002172 else
2173 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002174 SOURCING_LNUM = iptr->isn_lnum;
2175 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002176 {
2177 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002178 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002179 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002180 clear_tv(&di->di_tv);
2181 di->di_tv = *STACK_TV_BOT(0);
2182 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002183 }
2184 break;
2185
2186 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 case ISN_STORESCRIPT:
2188 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002189 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2190 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191
Bram Moolenaar4c137212021-04-19 16:48:48 +02002192 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002193 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002194 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002195 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002196
2197 // "const" and "final" are checked at compile time, locking
2198 // the value needs to be checked here.
2199 SOURCING_LNUM = iptr->isn_lnum;
2200 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002201 {
2202 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002203 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002204 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 clear_tv(sv->sv_tv);
2207 *sv->sv_tv = *STACK_TV_BOT(0);
2208 }
2209 break;
2210
2211 // store option
2212 case ISN_STOREOPT:
2213 {
2214 long n = 0;
2215 char_u *s = NULL;
2216 char *msg;
2217
Bram Moolenaar4c137212021-04-19 16:48:48 +02002218 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 tv = STACK_TV_BOT(0);
2220 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002221 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002223 if (s == NULL)
2224 s = (char_u *)"";
2225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002227 // must be VAR_NUMBER, CHECKTYPE makes sure
2228 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2230 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002231 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 if (msg != NULL)
2233 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002234 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002236 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 }
2239 break;
2240
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002241 // store $ENV
2242 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002243 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002244 tv = STACK_TV_BOT(0);
2245 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2246 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002247 break;
2248
2249 // store @r
2250 case ISN_STOREREG:
2251 {
2252 int reg = iptr->isn_arg.number;
2253
Bram Moolenaar4c137212021-04-19 16:48:48 +02002254 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002255 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002256 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002257 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002258 }
2259 break;
2260
2261 // store v: variable
2262 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002263 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002264 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2265 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002266 // should not happen, type is checked when compiling
2267 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002268 break;
2269
Bram Moolenaard3aac292020-04-19 14:32:17 +02002270 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002272 case ISN_STOREB:
2273 case ISN_STOREW:
2274 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002276 dictitem_T *di;
2277 hashtab_T *ht;
2278 char_u *name = iptr->isn_arg.string + 2;
2279
Bram Moolenaard3aac292020-04-19 14:32:17 +02002280 switch (iptr->isn_type)
2281 {
2282 case ISN_STOREG:
2283 ht = get_globvar_ht();
2284 break;
2285 case ISN_STOREB:
2286 ht = &curbuf->b_vars->dv_hashtab;
2287 break;
2288 case ISN_STOREW:
2289 ht = &curwin->w_vars->dv_hashtab;
2290 break;
2291 case ISN_STORET:
2292 ht = &curtab->tp_vars->dv_hashtab;
2293 break;
2294 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002295 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297
Bram Moolenaar4c137212021-04-19 16:48:48 +02002298 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002299 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002301 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 else
2303 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002304 SOURCING_LNUM = iptr->isn_lnum;
2305 if (var_check_permission(di, name) == FAIL)
2306 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 clear_tv(&di->di_tv);
2308 di->di_tv = *STACK_TV_BOT(0);
2309 }
2310 }
2311 break;
2312
Bram Moolenaar03290b82020-12-19 16:30:44 +01002313 // store an autoload variable
2314 case ISN_STOREAUTO:
2315 SOURCING_LNUM = iptr->isn_lnum;
2316 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2317 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002318 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002319 break;
2320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 // store number in local variable
2322 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002323 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 clear_tv(tv);
2325 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002326 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 break;
2328
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002329 // store value in list or dict variable
2330 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002331 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002332 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002333 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002334 typval_T *tv_dest = STACK_TV_BOT(-1);
2335 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002336
Bram Moolenaar752fc692021-01-04 21:57:11 +01002337 // Stack contains:
2338 // -3 value to be stored
2339 // -2 index
2340 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002341 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002342 SOURCING_LNUM = iptr->isn_lnum;
2343 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002344 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002345 dest_type = tv_dest->v_type;
2346 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002347 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002348 else if (dest_type == VAR_LIST
2349 && tv_idx->v_type != VAR_NUMBER)
2350 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002351 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002352 status = FAIL;
2353 }
2354 }
2355 else if (dest_type != tv_dest->v_type)
2356 {
2357 // just in case, should be OK
2358 semsg(_(e_expected_str_but_got_str),
2359 vartype_name(dest_type),
2360 vartype_name(tv_dest->v_type));
2361 status = FAIL;
2362 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002363
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002364 if (status == OK && dest_type == VAR_LIST)
2365 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002366 long lidx = (long)tv_idx->vval.v_number;
2367 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002368
2369 if (list == NULL)
2370 {
2371 emsg(_(e_list_not_set));
2372 goto on_error;
2373 }
2374 if (lidx < 0 && list->lv_len + lidx >= 0)
2375 // negative index is relative to the end
2376 lidx = list->lv_len + lidx;
2377 if (lidx < 0 || lidx > list->lv_len)
2378 {
2379 semsg(_(e_listidx), lidx);
2380 goto on_error;
2381 }
2382 if (lidx < list->lv_len)
2383 {
2384 listitem_T *li = list_find(list, lidx);
2385
2386 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002387 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002388 goto on_error;
2389 // overwrite existing list item
2390 clear_tv(&li->li_tv);
2391 li->li_tv = *tv;
2392 }
2393 else
2394 {
2395 if (error_if_locked(list->lv_lock,
2396 e_cannot_change_list))
2397 goto on_error;
2398 // append to list, only fails when out of memory
2399 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002400 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002401 clear_tv(tv);
2402 }
2403 }
2404 else if (status == OK && dest_type == VAR_DICT)
2405 {
2406 char_u *key = tv_idx->vval.v_string;
2407 dict_T *dict = tv_dest->vval.v_dict;
2408 dictitem_T *di;
2409
2410 SOURCING_LNUM = iptr->isn_lnum;
2411 if (dict == NULL)
2412 {
2413 emsg(_(e_dictionary_not_set));
2414 goto on_error;
2415 }
2416 if (key == NULL)
2417 key = (char_u *)"";
2418 di = dict_find(dict, key, -1);
2419 if (di != NULL)
2420 {
2421 if (error_if_locked(di->di_tv.v_lock,
2422 e_cannot_change_dict_item))
2423 goto on_error;
2424 // overwrite existing value
2425 clear_tv(&di->di_tv);
2426 di->di_tv = *tv;
2427 }
2428 else
2429 {
2430 if (error_if_locked(dict->dv_lock,
2431 e_cannot_change_dict))
2432 goto on_error;
2433 // add to dict, only fails when out of memory
2434 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002435 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002436 clear_tv(tv);
2437 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002438 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002439 else if (status == OK && dest_type == VAR_BLOB)
2440 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002441 long lidx = (long)tv_idx->vval.v_number;
2442 blob_T *blob = tv_dest->vval.v_blob;
2443 varnumber_T nr;
2444 int error = FALSE;
2445 int len;
2446
2447 if (blob == NULL)
2448 {
2449 emsg(_(e_blob_not_set));
2450 goto on_error;
2451 }
2452 len = blob_len(blob);
2453 if (lidx < 0 && len + lidx >= 0)
2454 // negative index is relative to the end
2455 lidx = len + lidx;
2456
2457 // Can add one byte at the end.
2458 if (lidx < 0 || lidx > len)
2459 {
2460 semsg(_(e_blobidx), lidx);
2461 goto on_error;
2462 }
2463 if (value_check_lock(blob->bv_lock,
2464 (char_u *)"blob", FALSE))
2465 goto on_error;
2466 nr = tv_get_number_chk(tv, &error);
2467 if (error)
2468 goto on_error;
2469 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002470 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002471 else
2472 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002473 status = FAIL;
2474 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002475 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002476
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002477 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002478 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002479 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002480 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002481 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002482 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002483 goto on_error;
2484 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002485 }
2486 break;
2487
Bram Moolenaar68452172021-04-12 21:21:02 +02002488 // store value in blob range
2489 case ISN_STORERANGE:
2490 {
2491 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2492 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2493 typval_T *tv_dest = STACK_TV_BOT(-1);
2494 int status = OK;
2495
2496 // Stack contains:
2497 // -4 value to be stored
2498 // -3 first index or "none"
2499 // -2 second index or "none"
2500 // -1 destination blob
2501 tv = STACK_TV_BOT(-4);
2502 if (tv_dest->v_type != VAR_BLOB)
2503 {
2504 status = FAIL;
2505 emsg(_(e_blob_required));
2506 }
2507 else
2508 {
2509 varnumber_T n1;
2510 varnumber_T n2;
2511 int error = FALSE;
2512
2513 n1 = tv_get_number_chk(tv_idx1, &error);
2514 if (error)
2515 status = FAIL;
2516 else
2517 {
2518 if (tv_idx2->v_type == VAR_SPECIAL
2519 && tv_idx2->vval.v_number == VVAL_NONE)
2520 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2521 else
2522 n2 = tv_get_number_chk(tv_idx2, &error);
2523 if (error)
2524 status = FAIL;
2525 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002526 {
2527 long bloblen = blob_len(tv_dest->vval.v_blob);
2528
2529 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002530 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002531 || check_blob_range(bloblen,
2532 n1, n2, FALSE) == FAIL)
2533 status = FAIL;
2534 else
2535 status = blob_set_range(
2536 tv_dest->vval.v_blob, n1, n2, tv);
2537 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002538 }
2539 }
2540
2541 clear_tv(tv_idx1);
2542 clear_tv(tv_idx2);
2543 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002544 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002545 clear_tv(tv);
2546
2547 if (status == FAIL)
2548 goto on_error;
2549 }
2550 break;
2551
Bram Moolenaar0186e582021-01-10 18:33:11 +01002552 // load or store variable or argument from outer scope
2553 case ISN_LOADOUTER:
2554 case ISN_STOREOUTER:
2555 {
2556 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002557 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2558 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002559
2560 while (depth > 1 && outer != NULL)
2561 {
2562 outer = outer->out_up;
2563 --depth;
2564 }
2565 if (outer == NULL)
2566 {
2567 SOURCING_LNUM = iptr->isn_lnum;
2568 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002569 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002570 }
2571 tv = ((typval_T *)outer->out_stack->ga_data)
2572 + outer->out_frame_idx + STACK_FRAME_SIZE
2573 + iptr->isn_arg.outer.outer_idx;
2574 if (iptr->isn_type == ISN_LOADOUTER)
2575 {
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002576 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002577 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002578 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002579 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002580 }
2581 else
2582 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002583 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002584 clear_tv(tv);
2585 *tv = *STACK_TV_BOT(0);
2586 }
2587 }
2588 break;
2589
Bram Moolenaar752fc692021-01-04 21:57:11 +01002590 // unlet item in list or dict variable
2591 case ISN_UNLETINDEX:
2592 {
2593 typval_T *tv_idx = STACK_TV_BOT(-2);
2594 typval_T *tv_dest = STACK_TV_BOT(-1);
2595 int status = OK;
2596
2597 // Stack contains:
2598 // -2 index
2599 // -1 dict or list
2600 if (tv_dest->v_type == VAR_DICT)
2601 {
2602 // unlet a dict item, index must be a string
2603 if (tv_idx->v_type != VAR_STRING)
2604 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002605 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002606 semsg(_(e_expected_str_but_got_str),
2607 vartype_name(VAR_STRING),
2608 vartype_name(tv_idx->v_type));
2609 status = FAIL;
2610 }
2611 else
2612 {
2613 dict_T *d = tv_dest->vval.v_dict;
2614 char_u *key = tv_idx->vval.v_string;
2615 dictitem_T *di = NULL;
2616
2617 if (key == NULL)
2618 key = (char_u *)"";
2619 if (d != NULL)
2620 di = dict_find(d, key, (int)STRLEN(key));
2621 if (di == NULL)
2622 {
2623 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002624 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002625 semsg(_(e_dictkey), key);
2626 status = FAIL;
2627 }
2628 else
2629 {
2630 // TODO: check for dict or item locked
2631 dictitem_remove(d, di);
2632 }
2633 }
2634 }
2635 else if (tv_dest->v_type == VAR_LIST)
2636 {
2637 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002638 SOURCING_LNUM = iptr->isn_lnum;
2639 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002640 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002641 status = FAIL;
2642 }
2643 else
2644 {
2645 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002646 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002647 listitem_T *li = NULL;
2648
2649 li = list_find(l, n);
2650 if (li == NULL)
2651 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002652 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002653 semsg(_(e_listidx), n);
2654 status = FAIL;
2655 }
2656 else
2657 // TODO: check for list or item locked
2658 listitem_remove(l, li);
2659 }
2660 }
2661 else
2662 {
2663 status = FAIL;
2664 semsg(_(e_cannot_index_str),
2665 vartype_name(tv_dest->v_type));
2666 }
2667
2668 clear_tv(tv_idx);
2669 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002670 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002671 if (status == FAIL)
2672 goto on_error;
2673 }
2674 break;
2675
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002676 // unlet range of items in list variable
2677 case ISN_UNLETRANGE:
2678 {
2679 // Stack contains:
2680 // -3 index1
2681 // -2 index2
2682 // -1 dict or list
2683 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2684 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2685 typval_T *tv_dest = STACK_TV_BOT(-1);
2686 int status = OK;
2687
2688 if (tv_dest->v_type == VAR_LIST)
2689 {
2690 // indexes must be a number
2691 SOURCING_LNUM = iptr->isn_lnum;
2692 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002693 || (tv_idx2->v_type != VAR_SPECIAL
2694 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002695 {
2696 status = FAIL;
2697 }
2698 else
2699 {
2700 list_T *l = tv_dest->vval.v_list;
2701 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002702 long n2 = tv_idx2->v_type == VAR_SPECIAL
2703 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002704 listitem_T *li;
2705
2706 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002707 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002708 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002709 else
2710 {
2711 if (n1 < 0)
2712 n1 = list_idx_of_item(l, li);
2713 if (n2 < 0)
2714 {
2715 listitem_T *li2 = list_find(l, n2);
2716
2717 if (li2 == NULL)
2718 status = FAIL;
2719 else
2720 n2 = list_idx_of_item(l, li2);
2721 }
2722 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002723 && tv_idx2->v_type != VAR_SPECIAL
2724 && n2 < n1)
2725 {
2726 semsg(_(e_listidx), n2);
2727 status = FAIL;
2728 }
2729 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002730 && list_unlet_range(l, li, NULL, n1,
2731 tv_idx2->v_type != VAR_SPECIAL, n2)
2732 == FAIL)
2733 status = FAIL;
2734 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002735 }
2736 }
2737 else
2738 {
2739 status = FAIL;
2740 SOURCING_LNUM = iptr->isn_lnum;
2741 semsg(_(e_cannot_index_str),
2742 vartype_name(tv_dest->v_type));
2743 }
2744
2745 clear_tv(tv_idx1);
2746 clear_tv(tv_idx2);
2747 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002748 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002749 if (status == FAIL)
2750 goto on_error;
2751 }
2752 break;
2753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 // push constant
2755 case ISN_PUSHNR:
2756 case ISN_PUSHBOOL:
2757 case ISN_PUSHSPEC:
2758 case ISN_PUSHF:
2759 case ISN_PUSHS:
2760 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002761 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002762 case ISN_PUSHCHANNEL:
2763 case ISN_PUSHJOB:
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002764 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002765 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002767 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002768 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769 switch (iptr->isn_type)
2770 {
2771 case ISN_PUSHNR:
2772 tv->v_type = VAR_NUMBER;
2773 tv->vval.v_number = iptr->isn_arg.number;
2774 break;
2775 case ISN_PUSHBOOL:
2776 tv->v_type = VAR_BOOL;
2777 tv->vval.v_number = iptr->isn_arg.number;
2778 break;
2779 case ISN_PUSHSPEC:
2780 tv->v_type = VAR_SPECIAL;
2781 tv->vval.v_number = iptr->isn_arg.number;
2782 break;
2783#ifdef FEAT_FLOAT
2784 case ISN_PUSHF:
2785 tv->v_type = VAR_FLOAT;
2786 tv->vval.v_float = iptr->isn_arg.fnumber;
2787 break;
2788#endif
2789 case ISN_PUSHBLOB:
2790 blob_copy(iptr->isn_arg.blob, tv);
2791 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002792 case ISN_PUSHFUNC:
2793 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002794 if (iptr->isn_arg.string == NULL)
2795 tv->vval.v_string = NULL;
2796 else
2797 tv->vval.v_string =
2798 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002799 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002800 case ISN_PUSHCHANNEL:
2801#ifdef FEAT_JOB_CHANNEL
2802 tv->v_type = VAR_CHANNEL;
2803 tv->vval.v_channel = iptr->isn_arg.channel;
2804 if (tv->vval.v_channel != NULL)
2805 ++tv->vval.v_channel->ch_refcount;
2806#endif
2807 break;
2808 case ISN_PUSHJOB:
2809#ifdef FEAT_JOB_CHANNEL
2810 tv->v_type = VAR_JOB;
2811 tv->vval.v_job = iptr->isn_arg.job;
2812 if (tv->vval.v_job != NULL)
2813 ++tv->vval.v_job->jv_refcount;
2814#endif
2815 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 default:
2817 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002818 tv->vval.v_string = vim_strsave(
2819 iptr->isn_arg.string == NULL
2820 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 }
2822 break;
2823
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002824 case ISN_UNLET:
2825 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2826 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002827 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002828 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002829 case ISN_UNLETENV:
2830 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2831 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002832
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002833 case ISN_LOCKCONST:
2834 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2835 break;
2836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 // create a list from items on the stack; uses a single allocation
2838 // for the list header and the items
2839 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002840 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002841 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 break;
2843
2844 // create a dict from items on the stack
2845 case ISN_NEWDICT:
2846 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002847 int count = iptr->isn_arg.number;
2848 dict_T *dict = dict_alloc();
2849 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002850 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002851 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002853 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002854 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 for (idx = 0; idx < count; ++idx)
2856 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002857 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002859 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002860 key = tv->vval.v_string == NULL
2861 ? (char_u *)"" : tv->vval.v_string;
2862 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002863 if (item != NULL)
2864 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002865 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002866 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002867 dict_unref(dict);
2868 goto on_error;
2869 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002870 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002872 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02002873 {
2874 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002875 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2878 item->di_tv.v_lock = 0;
2879 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002880 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002881 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002882 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002883 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 }
2886
2887 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002888 ectx->ec_stack.ga_len -= 2 * count - 1;
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002889 else if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002890 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002892 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 tv = STACK_TV_BOT(-1);
2894 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002895 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 tv->vval.v_dict = dict;
2897 ++dict->dv_refcount;
2898 }
2899 break;
2900
2901 // call a :def function
2902 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002903 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002904 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2905 NULL,
2906 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002907 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002908 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 break;
2910
2911 // call a builtin function
2912 case ISN_BCALL:
2913 SOURCING_LNUM = iptr->isn_lnum;
2914 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2915 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002916 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002917 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 break;
2919
2920 // call a funcref or partial
2921 case ISN_PCALL:
2922 {
2923 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2924 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002925 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926
2927 SOURCING_LNUM = iptr->isn_lnum;
2928 if (pfunc->cpf_top)
2929 {
2930 // funcref is above the arguments
2931 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2932 }
2933 else
2934 {
2935 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002936 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002937 partial_tv = *STACK_TV_BOT(0);
2938 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002940 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002941 if (tv == &partial_tv)
2942 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002944 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 }
2946 break;
2947
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002948 case ISN_PCALL_END:
2949 // PCALL finished, arguments have been consumed and replaced by
2950 // the return value. Now clear the funcref from the stack,
2951 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002952 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002953 clear_tv(STACK_TV_BOT(-1));
2954 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2955 break;
2956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 // call a user defined function or funcref/partial
2958 case ISN_UCALL:
2959 {
2960 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2961
2962 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002963 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002964 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002965 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 }
2967 break;
2968
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002969 // return from a :def function call without a value
2970 case ISN_RETURN_VOID:
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002971 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002972 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002973 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002974 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002975 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002976 tv->vval.v_number = 0;
2977 tv->v_lock = 0;
2978 // FALLTHROUGH
2979
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002980 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 case ISN_RETURN:
2982 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002983 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002984 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002985
2986 if (trystack->ga_len > 0)
2987 trycmd = ((trycmd_T *)trystack->ga_data)
2988 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002989 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002990 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002992 // jump to ":finally" or ":endtry"
2993 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002994 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002995 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002996 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 trycmd->tcd_return = TRUE;
2998 }
2999 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003000 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 }
3002 break;
3003
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003004 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 case ISN_FUNCREF:
3006 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003007 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
3008 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3009 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003012 goto theend;
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003013 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003014 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003015 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003016 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003017 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01003018 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003019 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003020 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003022 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 tv->vval.v_partial = pt;
3024 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003025 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 }
3027 break;
3028
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003029 // Create a global function from a lambda.
3030 case ISN_NEWFUNC:
3031 {
3032 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3033
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003034 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003035 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003036 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003037 }
3038 break;
3039
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003040 // List functions
3041 case ISN_DEF:
3042 if (iptr->isn_arg.string == NULL)
3043 list_functions(NULL);
3044 else
3045 {
3046 exarg_T ea;
3047
3048 CLEAR_FIELD(ea);
3049 ea.cmd = ea.arg = iptr->isn_arg.string;
3050 define_function(&ea, NULL);
3051 }
3052 break;
3053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 // jump if a condition is met
3055 case ISN_JUMP:
3056 {
3057 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003058 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 int jump = TRUE;
3060
3061 if (when != JUMP_ALWAYS)
3062 {
3063 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003064 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003065 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003066 || when == JUMP_IF_COND_TRUE)
3067 {
3068 SOURCING_LNUM = iptr->isn_lnum;
3069 jump = tv_get_bool_chk(tv, &error);
3070 if (error)
3071 goto on_error;
3072 }
3073 else
3074 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003076 || when == JUMP_AND_KEEP_IF_FALSE
3077 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003079 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 {
3081 // drop the value from the stack
3082 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003083 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 }
3085 }
3086 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003087 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 }
3089 break;
3090
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003091 // Jump if an argument with a default value was already set and not
3092 // v:none.
3093 case ISN_JUMP_IF_ARG_SET:
3094 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3095 if (tv->v_type != VAR_UNKNOWN
3096 && !(tv->v_type == VAR_SPECIAL
3097 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003098 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003099 break;
3100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 // top of a for loop
3102 case ISN_FOR:
3103 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003104 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 typval_T *idxtv =
3106 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3107
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003108 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003109 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003110 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003111 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003112 list_T *list = ltv->vval.v_list;
3113
3114 // push the next item from the list
3115 ++idxtv->vval.v_number;
3116 if (list == NULL
3117 || idxtv->vval.v_number >= list->lv_len)
3118 {
3119 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003120 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3121 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003122 }
3123 else if (list->lv_first == &range_list_item)
3124 {
3125 // non-materialized range() list
3126 tv = STACK_TV_BOT(0);
3127 tv->v_type = VAR_NUMBER;
3128 tv->v_lock = 0;
3129 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003131 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003132 }
3133 else
3134 {
3135 listitem_T *li = list_find(list,
3136 idxtv->vval.v_number);
3137
3138 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003139 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003140 }
3141 }
3142 else if (ltv->v_type == VAR_STRING)
3143 {
3144 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003145
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003146 // The index is for the last byte of the previous
3147 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003148 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003149 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003150 {
3151 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003152 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3153 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003154 }
3155 else
3156 {
3157 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3158
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003159 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003160 tv = STACK_TV_BOT(0);
3161 tv->v_type = VAR_STRING;
3162 tv->vval.v_string = vim_strnsave(
3163 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003164 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003165 idxtv->vval.v_number += clen - 1;
3166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003168 else if (ltv->v_type == VAR_BLOB)
3169 {
3170 blob_T *blob = ltv->vval.v_blob;
3171
3172 // When we get here the first time make a copy of the
3173 // blob, so that the iteration still works when it is
3174 // changed.
3175 if (idxtv->vval.v_number == -1 && blob != NULL)
3176 {
3177 blob_copy(blob, ltv);
3178 blob_unref(blob);
3179 blob = ltv->vval.v_blob;
3180 }
3181
3182 // The index is for the previous byte.
3183 ++idxtv->vval.v_number;
3184 if (blob == NULL
3185 || idxtv->vval.v_number >= blob_len(blob))
3186 {
3187 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003188 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3189 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003190 }
3191 else
3192 {
3193 // Push the next byte from the blob.
3194 tv = STACK_TV_BOT(0);
3195 tv->v_type = VAR_NUMBER;
3196 tv->vval.v_number = blob_get(blob,
3197 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003198 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003199 }
3200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 else
3202 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003203 semsg(_(e_for_loop_on_str_not_supported),
3204 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003205 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 }
3207 }
3208 break;
3209
3210 // start of ":try" block
3211 case ISN_TRY:
3212 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003213 trycmd_T *trycmd = NULL;
3214
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003215 if (unlikely(GA_GROW(&ectx->ec_trystack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003216 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003217 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3218 + ectx->ec_trystack.ga_len;
3219 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003221 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003222 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3223 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003224 trycmd->tcd_catch_idx =
3225 iptr->isn_arg.try.try_ref->try_catch;
3226 trycmd->tcd_finally_idx =
3227 iptr->isn_arg.try.try_ref->try_finally;
3228 trycmd->tcd_endtry_idx =
3229 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 }
3231 break;
3232
3233 case ISN_PUSHEXC:
3234 if (current_exception == NULL)
3235 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003236 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003238 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003240 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003241 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003243 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003245 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 tv->vval.v_string = vim_strsave(
3247 (char_u *)current_exception->value);
3248 break;
3249
3250 case ISN_CATCH:
3251 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003252 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253
Bram Moolenaar4c137212021-04-19 16:48:48 +02003254 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 if (trystack->ga_len > 0)
3256 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003257 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 + trystack->ga_len - 1;
3259 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003260 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 }
3262 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003263 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 catch_exception(current_exception);
3265 }
3266 break;
3267
Bram Moolenaarc150c092021-02-13 15:02:46 +01003268 case ISN_TRYCONT:
3269 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003270 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003271 trycont_T *trycont = &iptr->isn_arg.trycont;
3272 int i;
3273 trycmd_T *trycmd;
3274 int iidx = trycont->tct_where;
3275
3276 if (trystack->ga_len < trycont->tct_levels)
3277 {
3278 siemsg("TRYCONT: expected %d levels, found %d",
3279 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003280 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003281 }
3282 // Make :endtry jump to any outer try block and the last
3283 // :endtry inside the loop to the loop start.
3284 for (i = trycont->tct_levels; i > 0; --i)
3285 {
3286 trycmd = ((trycmd_T *)trystack->ga_data)
3287 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003288 // Add one to tcd_cont to be able to jump to
3289 // instruction with index zero.
3290 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003291 iidx = trycmd->tcd_finally_idx == 0
3292 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003293 }
3294 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003295 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003296 }
3297 break;
3298
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003299 case ISN_FINALLY:
3300 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003301 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003302 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3303 + trystack->ga_len - 1;
3304
3305 // Reset the index to avoid a return statement jumps here
3306 // again.
3307 trycmd->tcd_finally_idx = 0;
3308 break;
3309 }
3310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 // end of ":try" block
3312 case ISN_ENDTRY:
3313 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003314 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315
3316 if (trystack->ga_len > 0)
3317 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003318 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 --trystack->ga_len;
3321 --trylevel;
3322 trycmd = ((trycmd_T *)trystack->ga_data)
3323 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003324 if (trycmd->tcd_did_throw)
3325 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003326 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 {
3328 // discard the exception
3329 if (caught_stack == current_exception)
3330 caught_stack = caught_stack->caught;
3331 discard_current_exception();
3332 }
3333
3334 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003335 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003336
Bram Moolenaar4c137212021-04-19 16:48:48 +02003337 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003338 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003339 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003340 clear_tv(STACK_TV_BOT(0));
3341 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003342 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003343 // handling :continue: jump to outer try block or
3344 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003345 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 }
3347 }
3348 break;
3349
3350 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003351 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003352 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003353
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003354 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3355 {
3356 // throwing an exception while using "silent!" causes
3357 // the function to abort but not display an error.
3358 tv = STACK_TV_BOT(-1);
3359 clear_tv(tv);
3360 tv->v_type = VAR_NUMBER;
3361 tv->vval.v_number = 0;
3362 goto done;
3363 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003364 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003365 tv = STACK_TV_BOT(0);
3366 if (tv->vval.v_string == NULL
3367 || *skipwhite(tv->vval.v_string) == NUL)
3368 {
3369 vim_free(tv->vval.v_string);
3370 SOURCING_LNUM = iptr->isn_lnum;
3371 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003372 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003373 }
3374
3375 // Inside a "catch" we need to first discard the caught
3376 // exception.
3377 if (trystack->ga_len > 0)
3378 {
3379 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3380 + trystack->ga_len - 1;
3381 if (trycmd->tcd_caught && current_exception != NULL)
3382 {
3383 // discard the exception
3384 if (caught_stack == current_exception)
3385 caught_stack = caught_stack->caught;
3386 discard_current_exception();
3387 trycmd->tcd_caught = FALSE;
3388 }
3389 }
3390
3391 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3392 == FAIL)
3393 {
3394 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003395 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003396 }
3397 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 break;
3400
3401 // compare with special values
3402 case ISN_COMPAREBOOL:
3403 case ISN_COMPARESPECIAL:
3404 {
3405 typval_T *tv1 = STACK_TV_BOT(-2);
3406 typval_T *tv2 = STACK_TV_BOT(-1);
3407 varnumber_T arg1 = tv1->vval.v_number;
3408 varnumber_T arg2 = tv2->vval.v_number;
3409 int res;
3410
3411 switch (iptr->isn_arg.op.op_type)
3412 {
3413 case EXPR_EQUAL: res = arg1 == arg2; break;
3414 case EXPR_NEQUAL: res = arg1 != arg2; break;
3415 default: res = 0; break;
3416 }
3417
Bram Moolenaar4c137212021-04-19 16:48:48 +02003418 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 tv1->v_type = VAR_BOOL;
3420 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3421 }
3422 break;
3423
3424 // Operation with two number arguments
3425 case ISN_OPNR:
3426 case ISN_COMPARENR:
3427 {
3428 typval_T *tv1 = STACK_TV_BOT(-2);
3429 typval_T *tv2 = STACK_TV_BOT(-1);
3430 varnumber_T arg1 = tv1->vval.v_number;
3431 varnumber_T arg2 = tv2->vval.v_number;
3432 varnumber_T res;
3433
3434 switch (iptr->isn_arg.op.op_type)
3435 {
3436 case EXPR_MULT: res = arg1 * arg2; break;
3437 case EXPR_DIV: res = arg1 / arg2; break;
3438 case EXPR_REM: res = arg1 % arg2; break;
3439 case EXPR_SUB: res = arg1 - arg2; break;
3440 case EXPR_ADD: res = arg1 + arg2; break;
3441
3442 case EXPR_EQUAL: res = arg1 == arg2; break;
3443 case EXPR_NEQUAL: res = arg1 != arg2; break;
3444 case EXPR_GREATER: res = arg1 > arg2; break;
3445 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3446 case EXPR_SMALLER: res = arg1 < arg2; break;
3447 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3448 default: res = 0; break;
3449 }
3450
Bram Moolenaar4c137212021-04-19 16:48:48 +02003451 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 if (iptr->isn_type == ISN_COMPARENR)
3453 {
3454 tv1->v_type = VAR_BOOL;
3455 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3456 }
3457 else
3458 tv1->vval.v_number = res;
3459 }
3460 break;
3461
3462 // Computation with two float arguments
3463 case ISN_OPFLOAT:
3464 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003465#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 {
3467 typval_T *tv1 = STACK_TV_BOT(-2);
3468 typval_T *tv2 = STACK_TV_BOT(-1);
3469 float_T arg1 = tv1->vval.v_float;
3470 float_T arg2 = tv2->vval.v_float;
3471 float_T res = 0;
3472 int cmp = FALSE;
3473
3474 switch (iptr->isn_arg.op.op_type)
3475 {
3476 case EXPR_MULT: res = arg1 * arg2; break;
3477 case EXPR_DIV: res = arg1 / arg2; break;
3478 case EXPR_SUB: res = arg1 - arg2; break;
3479 case EXPR_ADD: res = arg1 + arg2; break;
3480
3481 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3482 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3483 case EXPR_GREATER: cmp = arg1 > arg2; break;
3484 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3485 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3486 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3487 default: cmp = 0; break;
3488 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003489 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490 if (iptr->isn_type == ISN_COMPAREFLOAT)
3491 {
3492 tv1->v_type = VAR_BOOL;
3493 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3494 }
3495 else
3496 tv1->vval.v_float = res;
3497 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003498#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 break;
3500
3501 case ISN_COMPARELIST:
3502 {
3503 typval_T *tv1 = STACK_TV_BOT(-2);
3504 typval_T *tv2 = STACK_TV_BOT(-1);
3505 list_T *arg1 = tv1->vval.v_list;
3506 list_T *arg2 = tv2->vval.v_list;
3507 int cmp = FALSE;
3508 int ic = iptr->isn_arg.op.op_ic;
3509
3510 switch (iptr->isn_arg.op.op_type)
3511 {
3512 case EXPR_EQUAL: cmp =
3513 list_equal(arg1, arg2, ic, FALSE); break;
3514 case EXPR_NEQUAL: cmp =
3515 !list_equal(arg1, arg2, ic, FALSE); break;
3516 case EXPR_IS: cmp = arg1 == arg2; break;
3517 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3518 default: cmp = 0; break;
3519 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003520 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 clear_tv(tv1);
3522 clear_tv(tv2);
3523 tv1->v_type = VAR_BOOL;
3524 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3525 }
3526 break;
3527
3528 case ISN_COMPAREBLOB:
3529 {
3530 typval_T *tv1 = STACK_TV_BOT(-2);
3531 typval_T *tv2 = STACK_TV_BOT(-1);
3532 blob_T *arg1 = tv1->vval.v_blob;
3533 blob_T *arg2 = tv2->vval.v_blob;
3534 int cmp = FALSE;
3535
3536 switch (iptr->isn_arg.op.op_type)
3537 {
3538 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3539 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3540 case EXPR_IS: cmp = arg1 == arg2; break;
3541 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3542 default: cmp = 0; break;
3543 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003544 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 clear_tv(tv1);
3546 clear_tv(tv2);
3547 tv1->v_type = VAR_BOOL;
3548 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3549 }
3550 break;
3551
3552 // TODO: handle separately
3553 case ISN_COMPARESTRING:
3554 case ISN_COMPAREDICT:
3555 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 case ISN_COMPAREANY:
3557 {
3558 typval_T *tv1 = STACK_TV_BOT(-2);
3559 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003560 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 int ic = iptr->isn_arg.op.op_ic;
3562
Bram Moolenaareb26f432020-09-14 16:50:05 +02003563 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003564 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003566 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 }
3568 break;
3569
3570 case ISN_ADDLIST:
3571 case ISN_ADDBLOB:
3572 {
3573 typval_T *tv1 = STACK_TV_BOT(-2);
3574 typval_T *tv2 = STACK_TV_BOT(-1);
3575
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003576 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 if (iptr->isn_type == ISN_ADDLIST)
3578 eval_addlist(tv1, tv2);
3579 else
3580 eval_addblob(tv1, tv2);
3581 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003582 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 }
3584 break;
3585
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003586 case ISN_LISTAPPEND:
3587 {
3588 typval_T *tv1 = STACK_TV_BOT(-2);
3589 typval_T *tv2 = STACK_TV_BOT(-1);
3590 list_T *l = tv1->vval.v_list;
3591
3592 // add an item to a list
3593 if (l == NULL)
3594 {
3595 SOURCING_LNUM = iptr->isn_lnum;
3596 emsg(_(e_cannot_add_to_null_list));
3597 goto on_error;
3598 }
3599 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003600 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003601 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003602 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003603 }
3604 break;
3605
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003606 case ISN_BLOBAPPEND:
3607 {
3608 typval_T *tv1 = STACK_TV_BOT(-2);
3609 typval_T *tv2 = STACK_TV_BOT(-1);
3610 blob_T *b = tv1->vval.v_blob;
3611 int error = FALSE;
3612 varnumber_T n;
3613
3614 // add a number to a blob
3615 if (b == NULL)
3616 {
3617 SOURCING_LNUM = iptr->isn_lnum;
3618 emsg(_(e_cannot_add_to_null_blob));
3619 goto on_error;
3620 }
3621 n = tv_get_number_chk(tv2, &error);
3622 if (error)
3623 goto on_error;
3624 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003625 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003626 }
3627 break;
3628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 // Computation with two arguments of unknown type
3630 case ISN_OPANY:
3631 {
3632 typval_T *tv1 = STACK_TV_BOT(-2);
3633 typval_T *tv2 = STACK_TV_BOT(-1);
3634 varnumber_T n1, n2;
3635#ifdef FEAT_FLOAT
3636 float_T f1 = 0, f2 = 0;
3637#endif
3638 int error = FALSE;
3639
3640 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3641 {
3642 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3643 {
3644 eval_addlist(tv1, tv2);
3645 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003646 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 break;
3648 }
3649 else if (tv1->v_type == VAR_BLOB
3650 && tv2->v_type == VAR_BLOB)
3651 {
3652 eval_addblob(tv1, tv2);
3653 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003654 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 break;
3656 }
3657 }
3658#ifdef FEAT_FLOAT
3659 if (tv1->v_type == VAR_FLOAT)
3660 {
3661 f1 = tv1->vval.v_float;
3662 n1 = 0;
3663 }
3664 else
3665#endif
3666 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003667 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 n1 = tv_get_number_chk(tv1, &error);
3669 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003670 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671#ifdef FEAT_FLOAT
3672 if (tv2->v_type == VAR_FLOAT)
3673 f1 = n1;
3674#endif
3675 }
3676#ifdef FEAT_FLOAT
3677 if (tv2->v_type == VAR_FLOAT)
3678 {
3679 f2 = tv2->vval.v_float;
3680 n2 = 0;
3681 }
3682 else
3683#endif
3684 {
3685 n2 = tv_get_number_chk(tv2, &error);
3686 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003687 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688#ifdef FEAT_FLOAT
3689 if (tv1->v_type == VAR_FLOAT)
3690 f2 = n2;
3691#endif
3692 }
3693#ifdef FEAT_FLOAT
3694 // if there is a float on either side the result is a float
3695 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3696 {
3697 switch (iptr->isn_arg.op.op_type)
3698 {
3699 case EXPR_MULT: f1 = f1 * f2; break;
3700 case EXPR_DIV: f1 = f1 / f2; break;
3701 case EXPR_SUB: f1 = f1 - f2; break;
3702 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003703 default: SOURCING_LNUM = iptr->isn_lnum;
3704 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003705 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 }
3707 clear_tv(tv1);
3708 clear_tv(tv2);
3709 tv1->v_type = VAR_FLOAT;
3710 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003711 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 }
3713 else
3714#endif
3715 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003716 int failed = FALSE;
3717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 switch (iptr->isn_arg.op.op_type)
3719 {
3720 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003721 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3722 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003723 goto on_error;
3724 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 case EXPR_SUB: n1 = n1 - n2; break;
3726 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003727 default: n1 = num_modulus(n1, n2, &failed);
3728 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003729 goto on_error;
3730 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 }
3732 clear_tv(tv1);
3733 clear_tv(tv2);
3734 tv1->v_type = VAR_NUMBER;
3735 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003736 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 }
3738 }
3739 break;
3740
3741 case ISN_CONCAT:
3742 {
3743 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3744 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3745 char_u *res;
3746
3747 res = concat_str(str1, str2);
3748 clear_tv(STACK_TV_BOT(-2));
3749 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003750 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 STACK_TV_BOT(-1)->vval.v_string = res;
3752 }
3753 break;
3754
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003755 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003756 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003757 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003758 int is_slice = iptr->isn_type == ISN_STRSLICE;
3759 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003760 char_u *res;
3761
3762 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003763 // string slice: string is at stack-3, first index at
3764 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003765 if (is_slice)
3766 {
3767 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003768 n1 = tv->vval.v_number;
3769 }
3770
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003771 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003772 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003773
Bram Moolenaar4c137212021-04-19 16:48:48 +02003774 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003775 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003776 if (is_slice)
3777 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003778 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003779 else
3780 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003781 // single character (including composing characters).
3782 // If the index is too big or negative the result is
3783 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003784 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003785 vim_free(tv->vval.v_string);
3786 tv->vval.v_string = res;
3787 }
3788 break;
3789
3790 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003791 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003792 case ISN_BLOBINDEX:
3793 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003795 int is_slice = iptr->isn_type == ISN_LISTSLICE
3796 || iptr->isn_type == ISN_BLOBSLICE;
3797 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3798 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003799 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003800 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801
3802 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003803 // list slice: list is at stack-3, indexes at stack-2 and
3804 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003805 // Same for blob.
3806 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
3808 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003809 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003811
3812 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 {
Bram Moolenaared591872020-08-15 22:14:53 +02003814 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003815 n1 = tv->vval.v_number;
3816 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 }
Bram Moolenaared591872020-08-15 22:14:53 +02003818
Bram Moolenaar4c137212021-04-19 16:48:48 +02003819 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003820 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003821 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003822 if (is_blob)
3823 {
3824 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3825 n1, n2, FALSE, tv) == FAIL)
3826 goto on_error;
3827 }
3828 else
3829 {
3830 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3831 n1, n2, FALSE, tv, TRUE) == FAIL)
3832 goto on_error;
3833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 }
3835 break;
3836
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003837 case ISN_ANYINDEX:
3838 case ISN_ANYSLICE:
3839 {
3840 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3841 typval_T *var1, *var2;
3842 int res;
3843
3844 // index: composite is at stack-2, index at stack-1
3845 // slice: composite is at stack-3, indexes at stack-2 and
3846 // stack-1
3847 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003848 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003849 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3850 goto on_error;
3851 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3852 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003853 res = eval_index_inner(tv, is_slice, var1, var2,
3854 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003855 clear_tv(var1);
3856 if (is_slice)
3857 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003858 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003859 if (res == FAIL)
3860 goto on_error;
3861 }
3862 break;
3863
Bram Moolenaar9af78762020-06-16 11:34:42 +02003864 case ISN_SLICE:
3865 {
3866 list_T *list;
3867 int count = iptr->isn_arg.number;
3868
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003869 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003870 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003871 list = tv->vval.v_list;
3872
3873 // no error for short list, expect it to be checked earlier
3874 if (list != NULL && list->lv_len >= count)
3875 {
3876 list_T *newlist = list_slice(list,
3877 count, list->lv_len - 1);
3878
3879 if (newlist != NULL)
3880 {
3881 list_unref(list);
3882 tv->vval.v_list = newlist;
3883 ++newlist->lv_refcount;
3884 }
3885 }
3886 }
3887 break;
3888
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003889 case ISN_GETITEM:
3890 {
3891 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003892 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003893
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003894 // Get list item: list is at stack-1, push item.
3895 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003896 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
3897 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003898
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003899 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003900 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003901 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003902 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003903
3904 // Useful when used in unpack assignment. Reset at
3905 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003906 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003907 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003908 }
3909 break;
3910
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 case ISN_MEMBER:
3912 {
3913 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003914 char_u *key;
3915 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003916 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003917
3918 // dict member: dict is at stack-2, key at stack-1
3919 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003920 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003921 dict = tv->vval.v_dict;
3922
3923 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003924 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003925 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003926 if (key == NULL)
3927 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003928
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003929 if ((di = dict_find(dict, key, -1)) == NULL)
3930 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003931 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003932 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003933
3934 // If :silent! is used we will continue, make sure the
3935 // stack contents makes sense.
3936 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003937 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003938 tv = STACK_TV_BOT(-1);
3939 clear_tv(tv);
3940 tv->v_type = VAR_NUMBER;
3941 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003942 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003943 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003944 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003945 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003946 // Clear the dict only after getting the item, to avoid
3947 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003948 tv = STACK_TV_BOT(-1);
3949 temp_tv = *tv;
3950 copy_tv(&di->di_tv, tv);
3951 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003952 }
3953 break;
3954
3955 // dict member with string key
3956 case ISN_STRINGMEMBER:
3957 {
3958 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003960 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961
3962 tv = STACK_TV_BOT(-1);
3963 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3964 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003965 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003967 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 }
3969 dict = tv->vval.v_dict;
3970
3971 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3972 == NULL)
3973 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003974 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003976 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003978 // Clear the dict after getting the item, to avoid that it
3979 // make the item invalid.
3980 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003982 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 }
3984 break;
3985
3986 case ISN_NEGATENR:
3987 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003988 if (tv->v_type != VAR_NUMBER
3989#ifdef FEAT_FLOAT
3990 && tv->v_type != VAR_FLOAT
3991#endif
3992 )
3993 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003994 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003995 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003996 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003997 }
3998#ifdef FEAT_FLOAT
3999 if (tv->v_type == VAR_FLOAT)
4000 tv->vval.v_float = -tv->vval.v_float;
4001 else
4002#endif
4003 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 break;
4005
4006 case ISN_CHECKNR:
4007 {
4008 int error = FALSE;
4009
4010 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004011 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004013 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 (void)tv_get_number_chk(tv, &error);
4015 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004016 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 }
4018 break;
4019
4020 case ISN_CHECKTYPE:
4021 {
4022 checktype_T *ct = &iptr->isn_arg.type;
4023
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004024 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004025 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004026 if (!ectx->ec_where.wt_variable)
4027 ectx->ec_where.wt_index = ct->ct_arg_idx;
4028 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4029 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004030 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004031 if (!ectx->ec_where.wt_variable)
4032 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004033
4034 // number 0 is FALSE, number 1 is TRUE
4035 if (tv->v_type == VAR_NUMBER
4036 && ct->ct_type->tt_type == VAR_BOOL
4037 && (tv->vval.v_number == 0
4038 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004040 tv->v_type = VAR_BOOL;
4041 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004042 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 }
4044 }
4045 break;
4046
Bram Moolenaar9af78762020-06-16 11:34:42 +02004047 case ISN_CHECKLEN:
4048 {
4049 int min_len = iptr->isn_arg.checklen.cl_min_len;
4050 list_T *list = NULL;
4051
4052 tv = STACK_TV_BOT(-1);
4053 if (tv->v_type == VAR_LIST)
4054 list = tv->vval.v_list;
4055 if (list == NULL || list->lv_len < min_len
4056 || (list->lv_len > min_len
4057 && !iptr->isn_arg.checklen.cl_more_OK))
4058 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004059 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004060 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004061 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004062 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004063 }
4064 }
4065 break;
4066
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004067 case ISN_SETTYPE:
4068 {
4069 checktype_T *ct = &iptr->isn_arg.type;
4070
4071 tv = STACK_TV_BOT(-1);
4072 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4073 {
4074 free_type(tv->vval.v_dict->dv_type);
4075 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4076 }
4077 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4078 {
4079 free_type(tv->vval.v_list->lv_type);
4080 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4081 }
4082 }
4083 break;
4084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004086 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 {
4088 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004089 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004091 if (iptr->isn_type == ISN_2BOOL)
4092 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004093 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004094 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004095 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004096 n = !n;
4097 }
4098 else
4099 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004100 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004101 SOURCING_LNUM = iptr->isn_lnum;
4102 n = tv_get_bool_chk(tv, &error);
4103 if (error)
4104 goto on_error;
4105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 clear_tv(tv);
4107 tv->v_type = VAR_BOOL;
4108 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4109 }
4110 break;
4111
4112 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004113 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004114 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004115 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4116 iptr->isn_type == ISN_2STRING_ANY,
4117 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004118 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 break;
4120
Bram Moolenaar08597872020-12-10 19:43:40 +01004121 case ISN_RANGE:
4122 {
4123 exarg_T ea;
4124 char *errormsg;
4125
Bram Moolenaarece0b872021-01-08 20:40:45 +01004126 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004127 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004128 ea.addr_type = ADDR_LINES;
4129 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004130 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004131 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004132 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004133
Dominique Pelle5a9e5842021-07-24 19:32:12 +02004134 if (unlikely(GA_GROW(&ectx->ec_stack, 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004135 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004136 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004137 tv = STACK_TV_BOT(-1);
4138 tv->v_type = VAR_NUMBER;
4139 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004140 if (ea.addr_count == 0)
4141 tv->vval.v_number = curwin->w_cursor.lnum;
4142 else
4143 tv->vval.v_number = ea.line2;
4144 }
4145 break;
4146
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004147 case ISN_PUT:
4148 {
4149 int regname = iptr->isn_arg.put.put_regname;
4150 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4151 char_u *expr = NULL;
4152 int dir = FORWARD;
4153
Bram Moolenaar08597872020-12-10 19:43:40 +01004154 if (lnum < -2)
4155 {
4156 // line number was put on the stack by ISN_RANGE
4157 tv = STACK_TV_BOT(-1);
4158 curwin->w_cursor.lnum = tv->vval.v_number;
4159 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4160 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004161 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004162 }
4163 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004164 // :put! above cursor
4165 dir = BACKWARD;
4166 else if (lnum >= 0)
4167 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004168
4169 if (regname == '=')
4170 {
4171 tv = STACK_TV_BOT(-1);
4172 if (tv->v_type == VAR_STRING)
4173 expr = tv->vval.v_string;
4174 else
4175 {
4176 expr = typval2string(tv, TRUE); // allocates value
4177 clear_tv(tv);
4178 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004179 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004180 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004181 check_cursor();
4182 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4183 vim_free(expr);
4184 }
4185 break;
4186
Bram Moolenaar02194d22020-10-24 23:08:38 +02004187 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004188 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4189 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4190 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4191 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004192 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4193 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004194 break;
4195
Bram Moolenaar02194d22020-10-24 23:08:38 +02004196 case ISN_CMDMOD_REV:
4197 // filter regprog is owned by the instruction, don't free it
4198 cmdmod.cmod_filter_regmatch.regprog = NULL;
4199 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004200 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4201 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004202 break;
4203
Bram Moolenaar792f7862020-11-23 08:31:18 +01004204 case ISN_UNPACK:
4205 {
4206 int count = iptr->isn_arg.unpack.unp_count;
4207 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4208 list_T *l;
4209 listitem_T *li;
4210 int i;
4211
4212 // Check there is a valid list to unpack.
4213 tv = STACK_TV_BOT(-1);
4214 if (tv->v_type != VAR_LIST)
4215 {
4216 SOURCING_LNUM = iptr->isn_lnum;
4217 emsg(_(e_for_argument_must_be_sequence_of_lists));
4218 goto on_error;
4219 }
4220 l = tv->vval.v_list;
4221 if (l == NULL
4222 || l->lv_len < (semicolon ? count - 1 : count))
4223 {
4224 SOURCING_LNUM = iptr->isn_lnum;
4225 emsg(_(e_list_value_does_not_have_enough_items));
4226 goto on_error;
4227 }
4228 else if (!semicolon && l->lv_len > count)
4229 {
4230 SOURCING_LNUM = iptr->isn_lnum;
4231 emsg(_(e_list_value_has_more_items_than_targets));
4232 goto on_error;
4233 }
4234
4235 CHECK_LIST_MATERIALIZE(l);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02004236 if (unlikely(GA_GROW(&ectx->ec_stack, count - 1) == FAIL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004237 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004238 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004239
4240 // Variable after semicolon gets a list with the remaining
4241 // items.
4242 if (semicolon)
4243 {
4244 list_T *rem_list =
4245 list_alloc_with_items(l->lv_len - count + 1);
4246
4247 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004248 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004249 tv = STACK_TV_BOT(-count);
4250 tv->vval.v_list = rem_list;
4251 ++rem_list->lv_refcount;
4252 tv->v_lock = 0;
4253 li = l->lv_first;
4254 for (i = 0; i < count - 1; ++i)
4255 li = li->li_next;
4256 for (i = 0; li != NULL; ++i)
4257 {
4258 list_set_item(rem_list, i, &li->li_tv);
4259 li = li->li_next;
4260 }
4261 --count;
4262 }
4263
4264 // Produce the values in reverse order, first item last.
4265 li = l->lv_first;
4266 for (i = 0; i < count; ++i)
4267 {
4268 tv = STACK_TV_BOT(-i - 1);
4269 copy_tv(&li->li_tv, tv);
4270 li = li->li_next;
4271 }
4272
4273 list_unref(l);
4274 }
4275 break;
4276
Bram Moolenaarb2049902021-01-24 12:53:53 +01004277 case ISN_PROF_START:
4278 case ISN_PROF_END:
4279 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004280#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004281 funccall_T cookie;
4282 ufunc_T *cur_ufunc =
4283 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004284 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004285
4286 cookie.func = cur_ufunc;
4287 if (iptr->isn_type == ISN_PROF_START)
4288 {
4289 func_line_start(&cookie, iptr->isn_lnum);
4290 // if we get here the instruction is executed
4291 func_line_exec(&cookie);
4292 }
4293 else
4294 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004295#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004296 }
4297 break;
4298
Bram Moolenaare99d4222021-06-13 14:01:26 +02004299 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004300 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004301 break;
4302
Bram Moolenaar389df252020-07-09 21:20:47 +02004303 case ISN_SHUFFLE:
4304 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004305 typval_T tmp_tv;
4306 int item = iptr->isn_arg.shuffle.shfl_item;
4307 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004308
4309 tmp_tv = *STACK_TV_BOT(-item);
4310 for ( ; up > 0 && item > 1; --up)
4311 {
4312 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4313 --item;
4314 }
4315 *STACK_TV_BOT(-item) = tmp_tv;
4316 }
4317 break;
4318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004320 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004322 ectx->ec_where.wt_index = 0;
4323 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 break;
4325 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004326 continue;
4327
Bram Moolenaard032f342020-07-18 18:13:02 +02004328func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004329 // Restore previous function. If the frame pointer is where we started
4330 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004331 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004332 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004333
Bram Moolenaar4c137212021-04-19 16:48:48 +02004334 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004335 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004336 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004337 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004338
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004339on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004340 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004341 // If "emsg_silent" is set then ignore the error, unless it was set
4342 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004343 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004344 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004345 {
4346 // If a sequence of instructions causes an error while ":silent!"
4347 // was used, restore the stack length and jump ahead to restoring
4348 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004349 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004350 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004351 while (ectx->ec_stack.ga_len
4352 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004353 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004354 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004355 clear_tv(STACK_TV_BOT(0));
4356 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004357 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4358 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004359 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004360 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004361 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004362on_fatal_error:
4363 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004364 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004365 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004366 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 }
4368
4369done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004370 ret = OK;
4371theend:
4372 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4373 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004374}
4375
4376/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004377 * Execute the instructions from a VAR_INSTR typeval and put the result in
4378 * "rettv".
4379 * Return OK or FAIL.
4380 */
4381 int
4382exe_typval_instr(typval_T *tv, typval_T *rettv)
4383{
4384 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4385 isn_T *save_instr = ectx->ec_instr;
4386 int save_iidx = ectx->ec_iidx;
4387 int res;
4388
4389 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4390 res = exec_instructions(ectx);
4391 if (res == OK)
4392 {
4393 *rettv = *STACK_TV_BOT(-1);
4394 --ectx->ec_stack.ga_len;
4395 }
4396
4397 ectx->ec_instr = save_instr;
4398 ectx->ec_iidx = save_iidx;
4399
4400 return res;
4401}
4402
4403/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004404 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4405 * "substitute_instr".
4406 */
4407 char_u *
4408exe_substitute_instr(void)
4409{
4410 ectx_T *ectx = substitute_instr->subs_ectx;
4411 isn_T *save_instr = ectx->ec_instr;
4412 int save_iidx = ectx->ec_iidx;
4413 char_u *res;
4414
4415 ectx->ec_instr = substitute_instr->subs_instr;
4416 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004417 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004418 typval_T *tv = STACK_TV_BOT(-1);
4419
Bram Moolenaar27523602021-06-05 21:36:19 +02004420 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004421 --ectx->ec_stack.ga_len;
4422 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004423 }
4424 else
4425 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004426 substitute_instr->subs_status = FAIL;
4427 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429
Bram Moolenaar4c137212021-04-19 16:48:48 +02004430 ectx->ec_instr = save_instr;
4431 ectx->ec_iidx = save_iidx;
4432
4433 return res;
4434}
4435
4436/*
4437 * Call a "def" function from old Vim script.
4438 * Return OK or FAIL.
4439 */
4440 int
4441call_def_function(
4442 ufunc_T *ufunc,
4443 int argc_arg, // nr of arguments
4444 typval_T *argv, // arguments
4445 partial_T *partial, // optional partial for context
4446 typval_T *rettv) // return value
4447{
4448 ectx_T ectx; // execution context
4449 int argc = argc_arg;
4450 typval_T *tv;
4451 int idx;
4452 int ret = FAIL;
4453 int defcount = ufunc->uf_args.ga_len - argc;
4454 sctx_T save_current_sctx = current_sctx;
4455 int did_emsg_before = did_emsg_cumul + did_emsg;
4456 int save_suppress_errthrow = suppress_errthrow;
4457 msglist_T **saved_msg_list = NULL;
4458 msglist_T *private_msg_list = NULL;
4459 int save_emsg_silent_def = emsg_silent_def;
4460 int save_did_emsg_def = did_emsg_def;
4461 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004462 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004463
4464// Get pointer to item in the stack.
4465#undef STACK_TV
4466#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4467
4468// Get pointer to item at the bottom of the stack, -1 is the bottom.
4469#undef STACK_TV_BOT
4470#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4471
4472// Get pointer to a local variable on the stack. Negative for arguments.
4473#undef STACK_TV_VAR
4474#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4475
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004476 // Update uf_has_breakpoint if needed.
4477 update_has_breakpoint(ufunc);
4478
Bram Moolenaar4c137212021-04-19 16:48:48 +02004479 if (ufunc->uf_def_status == UF_NOT_COMPILED
4480 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004481 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4482 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004483 == FAIL))
4484 {
4485 if (did_emsg_cumul + did_emsg == did_emsg_before)
4486 semsg(_(e_function_is_not_compiled_str),
4487 printable_func_name(ufunc));
4488 return FAIL;
4489 }
4490
4491 {
4492 // Check the function was really compiled.
4493 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4494 + ufunc->uf_dfunc_idx;
4495 if (INSTRUCTIONS(dfunc) == NULL)
4496 {
4497 iemsg("using call_def_function() on not compiled function");
4498 return FAIL;
4499 }
4500 }
4501
4502 // If depth of calling is getting too high, don't execute the function.
4503 orig_funcdepth = funcdepth_get();
4504 if (funcdepth_increment() == FAIL)
4505 return FAIL;
4506
4507 CLEAR_FIELD(ectx);
4508 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4509 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02004510 if (unlikely(ga_grow(&ectx.ec_stack, 20) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004511 {
4512 funcdepth_decrement();
4513 return FAIL;
4514 }
4515 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4516 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4517 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004518 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004519
4520 idx = argc - ufunc->uf_args.ga_len;
4521 if (idx > 0 && ufunc->uf_va_name == NULL)
4522 {
4523 if (idx == 1)
4524 emsg(_(e_one_argument_too_many));
4525 else
4526 semsg(_(e_nr_arguments_too_many), idx);
4527 goto failed_early;
4528 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004529 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4530 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004531 {
4532 if (idx == -1)
4533 emsg(_(e_one_argument_too_few));
4534 else
4535 semsg(_(e_nr_arguments_too_few), -idx);
4536 goto failed_early;
4537 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004538
4539 // Put arguments on the stack, but no more than what the function expects.
4540 // A lambda can be called with more arguments than it uses.
4541 for (idx = 0; idx < argc
4542 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4543 ++idx)
4544 {
4545 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4546 && argv[idx].v_type == VAR_SPECIAL
4547 && argv[idx].vval.v_number == VVAL_NONE)
4548 {
4549 // Use the default value.
4550 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4551 }
4552 else
4553 {
4554 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4555 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004556 ufunc->uf_arg_types[idx], &argv[idx],
4557 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004558 goto failed_early;
4559 copy_tv(&argv[idx], STACK_TV_BOT(0));
4560 }
4561 ++ectx.ec_stack.ga_len;
4562 }
4563
4564 // Turn varargs into a list. Empty list if no args.
4565 if (ufunc->uf_va_name != NULL)
4566 {
4567 int vararg_count = argc - ufunc->uf_args.ga_len;
4568
4569 if (vararg_count < 0)
4570 vararg_count = 0;
4571 else
4572 argc -= vararg_count;
4573 if (exe_newlist(vararg_count, &ectx) == FAIL)
4574 goto failed_early;
4575
4576 // Check the type of the list items.
4577 tv = STACK_TV_BOT(-1);
4578 if (ufunc->uf_va_type != NULL
4579 && ufunc->uf_va_type != &t_list_any
4580 && ufunc->uf_va_type->tt_member != &t_any
4581 && tv->vval.v_list != NULL)
4582 {
4583 type_T *expected = ufunc->uf_va_type->tt_member;
4584 listitem_T *li = tv->vval.v_list->lv_first;
4585
4586 for (idx = 0; idx < vararg_count; ++idx)
4587 {
4588 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004589 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004590 goto failed_early;
4591 li = li->li_next;
4592 }
4593 }
4594
4595 if (defcount > 0)
4596 // Move varargs list to below missing default arguments.
4597 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4598 --ectx.ec_stack.ga_len;
4599 }
4600
4601 // Make space for omitted arguments, will store default value below.
4602 // Any varargs list goes after them.
4603 if (defcount > 0)
4604 for (idx = 0; idx < defcount; ++idx)
4605 {
4606 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4607 ++ectx.ec_stack.ga_len;
4608 }
4609 if (ufunc->uf_va_name != NULL)
4610 ++ectx.ec_stack.ga_len;
4611
4612 // Frame pointer points to just after arguments.
4613 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4614 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4615
4616 {
4617 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4618 + ufunc->uf_dfunc_idx;
4619 ufunc_T *base_ufunc = dfunc->df_ufunc;
4620
4621 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4622 // by copy_func().
4623 if (partial != NULL || base_ufunc->uf_partial != NULL)
4624 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004625 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4626 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004627 goto failed_early;
4628 if (partial != NULL)
4629 {
4630 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4631 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004632 if (current_ectx->ec_outer_ref != NULL
4633 && current_ectx->ec_outer_ref->or_outer != NULL)
4634 ectx.ec_outer_ref->or_outer =
4635 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004636 }
4637 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004638 {
4639 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4640 ++partial->pt_refcount;
4641 ectx.ec_outer_ref->or_partial = partial;
4642 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004643 }
4644 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004645 {
4646 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4647 ++base_ufunc->uf_partial->pt_refcount;
4648 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4649 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004650 }
4651 }
4652
4653 // dummy frame entries
4654 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4655 {
4656 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4657 ++ectx.ec_stack.ga_len;
4658 }
4659
4660 {
4661 // Reserve space for local variables and any closure reference count.
4662 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4663 + ufunc->uf_dfunc_idx;
4664
4665 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4666 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4667 ectx.ec_stack.ga_len += dfunc->df_varcount;
4668 if (dfunc->df_has_closure)
4669 {
4670 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4671 STACK_TV_VAR(idx)->vval.v_number = 0;
4672 ++ectx.ec_stack.ga_len;
4673 }
4674
4675 ectx.ec_instr = INSTRUCTIONS(dfunc);
4676 }
4677
4678 // Following errors are in the function, not the caller.
4679 // Commands behave like vim9script.
4680 estack_push_ufunc(ufunc, 1);
4681 current_sctx = ufunc->uf_script_ctx;
4682 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4683
4684 // Use a specific location for storing error messages to be converted to an
4685 // exception.
4686 saved_msg_list = msg_list;
4687 msg_list = &private_msg_list;
4688
4689 // Do turn errors into exceptions.
4690 suppress_errthrow = FALSE;
4691
4692 // Do not delete the function while executing it.
4693 ++ufunc->uf_calls;
4694
4695 // When ":silent!" was used before calling then we still abort the
4696 // function. If ":silent!" is used in the function then we don't.
4697 emsg_silent_def = emsg_silent;
4698 did_emsg_def = 0;
4699
4700 ectx.ec_where.wt_index = 0;
4701 ectx.ec_where.wt_variable = FALSE;
4702
4703 // Execute the instructions until done.
4704 ret = exec_instructions(&ectx);
4705 if (ret == OK)
4706 {
4707 // function finished, get result from the stack.
4708 if (ufunc->uf_ret_type == &t_void)
4709 {
4710 rettv->v_type = VAR_VOID;
4711 }
4712 else
4713 {
4714 tv = STACK_TV_BOT(-1);
4715 *rettv = *tv;
4716 tv->v_type = VAR_UNKNOWN;
4717 }
4718 }
4719
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004720 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004721 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4722 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004723
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004724 // Deal with any remaining closures, they may be in use somewhere.
4725 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004726 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004727 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004728 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4729 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004730
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004731 estack_pop();
4732 current_sctx = save_current_sctx;
4733
Bram Moolenaarc970e422021-03-17 15:03:04 +01004734 // TODO: when is it safe to delete the function if it is no longer used?
4735 --ufunc->uf_calls;
4736
Bram Moolenaar352134b2020-10-17 22:04:08 +02004737 if (*msg_list != NULL && saved_msg_list != NULL)
4738 {
4739 msglist_T **plist = saved_msg_list;
4740
4741 // Append entries from the current msg_list (uncaught exceptions) to
4742 // the saved msg_list.
4743 while (*plist != NULL)
4744 plist = &(*plist)->next;
4745
4746 *plist = *msg_list;
4747 }
4748 msg_list = saved_msg_list;
4749
Bram Moolenaar4c137212021-04-19 16:48:48 +02004750 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004751 {
4752 cmdmod.cmod_filter_regmatch.regprog = NULL;
4753 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004754 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004755 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004756 emsg_silent_def = save_emsg_silent_def;
4757 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004758
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004759failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004760 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4762 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004763 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004766 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004767 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004768 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004769 if (ectx.ec_outer_ref->or_outer_allocated)
4770 vim_free(ectx.ec_outer_ref->or_outer);
4771 partial_unref(ectx.ec_outer_ref->or_partial);
4772 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004773 }
4774
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004775 // Not sure if this is necessary.
4776 suppress_errthrow = save_suppress_errthrow;
4777
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004778 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
4779 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004780 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004781 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004782 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783 return ret;
4784}
4785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004787 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4788 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4789 * "pfx" is prefixed to every line.
4790 */
4791 static void
4792list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4793{
4794 int line_idx = 0;
4795 int prev_current = 0;
4796 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004797 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004798
4799 for (current = 0; current < instr_count; ++current)
4800 {
4801 isn_T *iptr = &instr[current];
4802 char *line;
4803
4804 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004805 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004806 while (line_idx < iptr->isn_lnum
4807 && line_idx < ufunc->uf_lines.ga_len)
4808 {
4809 if (current > prev_current)
4810 {
4811 msg_puts("\n\n");
4812 prev_current = current;
4813 }
4814 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4815 if (line != NULL)
4816 msg(line);
4817 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004818 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4819 {
4820 int first_def_arg = ufunc->uf_args.ga_len
4821 - ufunc->uf_def_args.ga_len;
4822
4823 if (def_arg_idx > 0)
4824 msg_puts("\n\n");
4825 msg_start();
4826 msg_puts(" ");
4827 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4828 first_def_arg + def_arg_idx]);
4829 msg_puts(" = ");
4830 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4831 msg_clr_eos();
4832 msg_end();
4833 }
4834 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004835
4836 switch (iptr->isn_type)
4837 {
4838 case ISN_EXEC:
4839 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4840 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004841 case ISN_EXEC_SPLIT:
4842 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4843 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004844 case ISN_LEGACY_EVAL:
4845 smsg("%s%4d EVAL legacy %s", pfx, current,
4846 iptr->isn_arg.string);
4847 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004848 case ISN_REDIRSTART:
4849 smsg("%s%4d REDIR", pfx, current);
4850 break;
4851 case ISN_REDIREND:
4852 smsg("%s%4d REDIR END%s", pfx, current,
4853 iptr->isn_arg.number ? " append" : "");
4854 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004855 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004856#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004857 smsg("%s%4d CEXPR pre %s", pfx, current,
4858 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004859#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004860 break;
4861 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004862#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004863 {
4864 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4865
4866 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4867 cexpr_get_auname(cer->cer_cmdidx),
4868 cer->cer_forceit ? "!" : "",
4869 cer->cer_cmdline);
4870 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004871#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004872 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004873 case ISN_INSTR:
4874 {
4875 smsg("%s%4d INSTR", pfx, current);
4876 list_instructions(" ", iptr->isn_arg.instr,
4877 INT_MAX, NULL);
4878 msg(" -------------");
4879 }
4880 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004881 case ISN_SUBSTITUTE:
4882 {
4883 subs_T *subs = &iptr->isn_arg.subs;
4884
4885 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4886 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4887 msg(" -------------");
4888 }
4889 break;
4890 case ISN_EXECCONCAT:
4891 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4892 (varnumber_T)iptr->isn_arg.number);
4893 break;
4894 case ISN_ECHO:
4895 {
4896 echo_T *echo = &iptr->isn_arg.echo;
4897
4898 smsg("%s%4d %s %d", pfx, current,
4899 echo->echo_with_white ? "ECHO" : "ECHON",
4900 echo->echo_count);
4901 }
4902 break;
4903 case ISN_EXECUTE:
4904 smsg("%s%4d EXECUTE %lld", pfx, current,
4905 (varnumber_T)(iptr->isn_arg.number));
4906 break;
4907 case ISN_ECHOMSG:
4908 smsg("%s%4d ECHOMSG %lld", pfx, current,
4909 (varnumber_T)(iptr->isn_arg.number));
4910 break;
4911 case ISN_ECHOERR:
4912 smsg("%s%4d ECHOERR %lld", pfx, current,
4913 (varnumber_T)(iptr->isn_arg.number));
4914 break;
4915 case ISN_LOAD:
4916 {
4917 if (iptr->isn_arg.number < 0)
4918 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4919 (varnumber_T)(iptr->isn_arg.number
4920 + STACK_FRAME_SIZE));
4921 else
4922 smsg("%s%4d LOAD $%lld", pfx, current,
4923 (varnumber_T)(iptr->isn_arg.number));
4924 }
4925 break;
4926 case ISN_LOADOUTER:
4927 {
4928 if (iptr->isn_arg.number < 0)
4929 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4930 iptr->isn_arg.outer.outer_depth,
4931 iptr->isn_arg.outer.outer_idx
4932 + STACK_FRAME_SIZE);
4933 else
4934 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4935 iptr->isn_arg.outer.outer_depth,
4936 iptr->isn_arg.outer.outer_idx);
4937 }
4938 break;
4939 case ISN_LOADV:
4940 smsg("%s%4d LOADV v:%s", pfx, current,
4941 get_vim_var_name(iptr->isn_arg.number));
4942 break;
4943 case ISN_LOADSCRIPT:
4944 {
4945 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4946 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4947 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4948 + sref->sref_idx;
4949
4950 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4951 sv->sv_name,
4952 sref->sref_idx,
4953 si->sn_name);
4954 }
4955 break;
4956 case ISN_LOADS:
4957 {
4958 scriptitem_T *si = SCRIPT_ITEM(
4959 iptr->isn_arg.loadstore.ls_sid);
4960
4961 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4962 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4963 }
4964 break;
4965 case ISN_LOADAUTO:
4966 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4967 break;
4968 case ISN_LOADG:
4969 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4970 break;
4971 case ISN_LOADB:
4972 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4973 break;
4974 case ISN_LOADW:
4975 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4976 break;
4977 case ISN_LOADT:
4978 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4979 break;
4980 case ISN_LOADGDICT:
4981 smsg("%s%4d LOAD g:", pfx, current);
4982 break;
4983 case ISN_LOADBDICT:
4984 smsg("%s%4d LOAD b:", pfx, current);
4985 break;
4986 case ISN_LOADWDICT:
4987 smsg("%s%4d LOAD w:", pfx, current);
4988 break;
4989 case ISN_LOADTDICT:
4990 smsg("%s%4d LOAD t:", pfx, current);
4991 break;
4992 case ISN_LOADOPT:
4993 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4994 break;
4995 case ISN_LOADENV:
4996 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4997 break;
4998 case ISN_LOADREG:
4999 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
5000 break;
5001
5002 case ISN_STORE:
5003 if (iptr->isn_arg.number < 0)
5004 smsg("%s%4d STORE arg[%lld]", pfx, current,
5005 iptr->isn_arg.number + STACK_FRAME_SIZE);
5006 else
5007 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
5008 break;
5009 case ISN_STOREOUTER:
5010 {
5011 if (iptr->isn_arg.number < 0)
5012 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5013 iptr->isn_arg.outer.outer_depth,
5014 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5015 else
5016 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5017 iptr->isn_arg.outer.outer_depth,
5018 iptr->isn_arg.outer.outer_idx);
5019 }
5020 break;
5021 case ISN_STOREV:
5022 smsg("%s%4d STOREV v:%s", pfx, current,
5023 get_vim_var_name(iptr->isn_arg.number));
5024 break;
5025 case ISN_STOREAUTO:
5026 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5027 break;
5028 case ISN_STOREG:
5029 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5030 break;
5031 case ISN_STOREB:
5032 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5033 break;
5034 case ISN_STOREW:
5035 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5036 break;
5037 case ISN_STORET:
5038 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5039 break;
5040 case ISN_STORES:
5041 {
5042 scriptitem_T *si = SCRIPT_ITEM(
5043 iptr->isn_arg.loadstore.ls_sid);
5044
5045 smsg("%s%4d STORES %s in %s", pfx, current,
5046 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5047 }
5048 break;
5049 case ISN_STORESCRIPT:
5050 {
5051 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5052 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5053 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
5054 + sref->sref_idx;
5055
5056 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
5057 sv->sv_name,
5058 sref->sref_idx,
5059 si->sn_name);
5060 }
5061 break;
5062 case ISN_STOREOPT:
5063 smsg("%s%4d STOREOPT &%s", pfx, current,
5064 iptr->isn_arg.storeopt.so_name);
5065 break;
5066 case ISN_STOREENV:
5067 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5068 break;
5069 case ISN_STOREREG:
5070 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
5071 break;
5072 case ISN_STORENR:
5073 smsg("%s%4d STORE %lld in $%d", pfx, current,
5074 iptr->isn_arg.storenr.stnr_val,
5075 iptr->isn_arg.storenr.stnr_idx);
5076 break;
5077
5078 case ISN_STOREINDEX:
5079 smsg("%s%4d STOREINDEX %s", pfx, current,
5080 vartype_name(iptr->isn_arg.vartype));
5081 break;
5082
5083 case ISN_STORERANGE:
5084 smsg("%s%4d STORERANGE", pfx, current);
5085 break;
5086
5087 // constants
5088 case ISN_PUSHNR:
5089 smsg("%s%4d PUSHNR %lld", pfx, current,
5090 (varnumber_T)(iptr->isn_arg.number));
5091 break;
5092 case ISN_PUSHBOOL:
5093 case ISN_PUSHSPEC:
5094 smsg("%s%4d PUSH %s", pfx, current,
5095 get_var_special_name(iptr->isn_arg.number));
5096 break;
5097 case ISN_PUSHF:
5098#ifdef FEAT_FLOAT
5099 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5100#endif
5101 break;
5102 case ISN_PUSHS:
5103 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5104 break;
5105 case ISN_PUSHBLOB:
5106 {
5107 char_u *r;
5108 char_u numbuf[NUMBUFLEN];
5109 char_u *tofree;
5110
5111 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5112 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5113 vim_free(tofree);
5114 }
5115 break;
5116 case ISN_PUSHFUNC:
5117 {
5118 char *name = (char *)iptr->isn_arg.string;
5119
5120 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5121 name == NULL ? "[none]" : name);
5122 }
5123 break;
5124 case ISN_PUSHCHANNEL:
5125#ifdef FEAT_JOB_CHANNEL
5126 {
5127 channel_T *channel = iptr->isn_arg.channel;
5128
5129 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5130 channel == NULL ? 0 : channel->ch_id);
5131 }
5132#endif
5133 break;
5134 case ISN_PUSHJOB:
5135#ifdef FEAT_JOB_CHANNEL
5136 {
5137 typval_T tv;
5138 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005139 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005140
5141 tv.v_type = VAR_JOB;
5142 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005143 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005144 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5145 }
5146#endif
5147 break;
5148 case ISN_PUSHEXC:
5149 smsg("%s%4d PUSH v:exception", pfx, current);
5150 break;
5151 case ISN_UNLET:
5152 smsg("%s%4d UNLET%s %s", pfx, current,
5153 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5154 iptr->isn_arg.unlet.ul_name);
5155 break;
5156 case ISN_UNLETENV:
5157 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5158 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5159 iptr->isn_arg.unlet.ul_name);
5160 break;
5161 case ISN_UNLETINDEX:
5162 smsg("%s%4d UNLETINDEX", pfx, current);
5163 break;
5164 case ISN_UNLETRANGE:
5165 smsg("%s%4d UNLETRANGE", pfx, current);
5166 break;
5167 case ISN_LOCKCONST:
5168 smsg("%s%4d LOCKCONST", pfx, current);
5169 break;
5170 case ISN_NEWLIST:
5171 smsg("%s%4d NEWLIST size %lld", pfx, current,
5172 (varnumber_T)(iptr->isn_arg.number));
5173 break;
5174 case ISN_NEWDICT:
5175 smsg("%s%4d NEWDICT size %lld", pfx, current,
5176 (varnumber_T)(iptr->isn_arg.number));
5177 break;
5178
5179 // function call
5180 case ISN_BCALL:
5181 {
5182 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5183
5184 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5185 internal_func_name(cbfunc->cbf_idx),
5186 cbfunc->cbf_argcount);
5187 }
5188 break;
5189 case ISN_DCALL:
5190 {
5191 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5192 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5193 + cdfunc->cdf_idx;
5194
5195 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5196 df->df_ufunc->uf_name_exp != NULL
5197 ? df->df_ufunc->uf_name_exp
5198 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5199 }
5200 break;
5201 case ISN_UCALL:
5202 {
5203 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5204
5205 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5206 cufunc->cuf_name, cufunc->cuf_argcount);
5207 }
5208 break;
5209 case ISN_PCALL:
5210 {
5211 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5212
5213 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5214 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5215 }
5216 break;
5217 case ISN_PCALL_END:
5218 smsg("%s%4d PCALL end", pfx, current);
5219 break;
5220 case ISN_RETURN:
5221 smsg("%s%4d RETURN", pfx, current);
5222 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005223 case ISN_RETURN_VOID:
5224 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005225 break;
5226 case ISN_FUNCREF:
5227 {
5228 funcref_T *funcref = &iptr->isn_arg.funcref;
5229 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5230 + funcref->fr_func;
5231
5232 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5233 }
5234 break;
5235
5236 case ISN_NEWFUNC:
5237 {
5238 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5239
5240 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5241 newfunc->nf_lambda, newfunc->nf_global);
5242 }
5243 break;
5244
5245 case ISN_DEF:
5246 {
5247 char_u *name = iptr->isn_arg.string;
5248
5249 smsg("%s%4d DEF %s", pfx, current,
5250 name == NULL ? (char_u *)"" : name);
5251 }
5252 break;
5253
5254 case ISN_JUMP:
5255 {
5256 char *when = "?";
5257
5258 switch (iptr->isn_arg.jump.jump_when)
5259 {
5260 case JUMP_ALWAYS:
5261 when = "JUMP";
5262 break;
5263 case JUMP_AND_KEEP_IF_TRUE:
5264 when = "JUMP_AND_KEEP_IF_TRUE";
5265 break;
5266 case JUMP_IF_FALSE:
5267 when = "JUMP_IF_FALSE";
5268 break;
5269 case JUMP_AND_KEEP_IF_FALSE:
5270 when = "JUMP_AND_KEEP_IF_FALSE";
5271 break;
5272 case JUMP_IF_COND_FALSE:
5273 when = "JUMP_IF_COND_FALSE";
5274 break;
5275 case JUMP_IF_COND_TRUE:
5276 when = "JUMP_IF_COND_TRUE";
5277 break;
5278 }
5279 smsg("%s%4d %s -> %d", pfx, current, when,
5280 iptr->isn_arg.jump.jump_where);
5281 }
5282 break;
5283
5284 case ISN_JUMP_IF_ARG_SET:
5285 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5286 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5287 iptr->isn_arg.jump.jump_where);
5288 break;
5289
5290 case ISN_FOR:
5291 {
5292 forloop_T *forloop = &iptr->isn_arg.forloop;
5293
5294 smsg("%s%4d FOR $%d -> %d", pfx, current,
5295 forloop->for_idx, forloop->for_end);
5296 }
5297 break;
5298
5299 case ISN_TRY:
5300 {
5301 try_T *try = &iptr->isn_arg.try;
5302
5303 if (try->try_ref->try_finally == 0)
5304 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5305 pfx, current,
5306 try->try_ref->try_catch,
5307 try->try_ref->try_endtry);
5308 else
5309 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5310 pfx, current,
5311 try->try_ref->try_catch,
5312 try->try_ref->try_finally,
5313 try->try_ref->try_endtry);
5314 }
5315 break;
5316 case ISN_CATCH:
5317 // TODO
5318 smsg("%s%4d CATCH", pfx, current);
5319 break;
5320 case ISN_TRYCONT:
5321 {
5322 trycont_T *trycont = &iptr->isn_arg.trycont;
5323
5324 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5325 trycont->tct_levels,
5326 trycont->tct_levels == 1 ? "" : "s",
5327 trycont->tct_where);
5328 }
5329 break;
5330 case ISN_FINALLY:
5331 smsg("%s%4d FINALLY", pfx, current);
5332 break;
5333 case ISN_ENDTRY:
5334 smsg("%s%4d ENDTRY", pfx, current);
5335 break;
5336 case ISN_THROW:
5337 smsg("%s%4d THROW", pfx, current);
5338 break;
5339
5340 // expression operations on number
5341 case ISN_OPNR:
5342 case ISN_OPFLOAT:
5343 case ISN_OPANY:
5344 {
5345 char *what;
5346 char *ins;
5347
5348 switch (iptr->isn_arg.op.op_type)
5349 {
5350 case EXPR_MULT: what = "*"; break;
5351 case EXPR_DIV: what = "/"; break;
5352 case EXPR_REM: what = "%"; break;
5353 case EXPR_SUB: what = "-"; break;
5354 case EXPR_ADD: what = "+"; break;
5355 default: what = "???"; break;
5356 }
5357 switch (iptr->isn_type)
5358 {
5359 case ISN_OPNR: ins = "OPNR"; break;
5360 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5361 case ISN_OPANY: ins = "OPANY"; break;
5362 default: ins = "???"; break;
5363 }
5364 smsg("%s%4d %s %s", pfx, current, ins, what);
5365 }
5366 break;
5367
5368 case ISN_COMPAREBOOL:
5369 case ISN_COMPARESPECIAL:
5370 case ISN_COMPARENR:
5371 case ISN_COMPAREFLOAT:
5372 case ISN_COMPARESTRING:
5373 case ISN_COMPAREBLOB:
5374 case ISN_COMPARELIST:
5375 case ISN_COMPAREDICT:
5376 case ISN_COMPAREFUNC:
5377 case ISN_COMPAREANY:
5378 {
5379 char *p;
5380 char buf[10];
5381 char *type;
5382
5383 switch (iptr->isn_arg.op.op_type)
5384 {
5385 case EXPR_EQUAL: p = "=="; break;
5386 case EXPR_NEQUAL: p = "!="; break;
5387 case EXPR_GREATER: p = ">"; break;
5388 case EXPR_GEQUAL: p = ">="; break;
5389 case EXPR_SMALLER: p = "<"; break;
5390 case EXPR_SEQUAL: p = "<="; break;
5391 case EXPR_MATCH: p = "=~"; break;
5392 case EXPR_IS: p = "is"; break;
5393 case EXPR_ISNOT: p = "isnot"; break;
5394 case EXPR_NOMATCH: p = "!~"; break;
5395 default: p = "???"; break;
5396 }
5397 STRCPY(buf, p);
5398 if (iptr->isn_arg.op.op_ic == TRUE)
5399 strcat(buf, "?");
5400 switch(iptr->isn_type)
5401 {
5402 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5403 case ISN_COMPARESPECIAL:
5404 type = "COMPARESPECIAL"; break;
5405 case ISN_COMPARENR: type = "COMPARENR"; break;
5406 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5407 case ISN_COMPARESTRING:
5408 type = "COMPARESTRING"; break;
5409 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5410 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5411 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5412 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5413 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5414 default: type = "???"; break;
5415 }
5416
5417 smsg("%s%4d %s %s", pfx, current, type, buf);
5418 }
5419 break;
5420
5421 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5422 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5423
5424 // expression operations
5425 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5426 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5427 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5428 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5429 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5430 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5431 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5432 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5433 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5434 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5435 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5436 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5437 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005438 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5439 iptr->isn_arg.getitem.gi_index,
5440 iptr->isn_arg.getitem.gi_with_op ?
5441 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005442 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5443 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5444 iptr->isn_arg.string); break;
5445 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5446
5447 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5448 case ISN_CHECKTYPE:
5449 {
5450 checktype_T *ct = &iptr->isn_arg.type;
5451 char *tofree;
5452
5453 if (ct->ct_arg_idx == 0)
5454 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5455 type_name(ct->ct_type, &tofree),
5456 (int)ct->ct_off);
5457 else
5458 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5459 type_name(ct->ct_type, &tofree),
5460 (int)ct->ct_off,
5461 (int)ct->ct_arg_idx);
5462 vim_free(tofree);
5463 break;
5464 }
5465 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5466 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5467 iptr->isn_arg.checklen.cl_min_len);
5468 break;
5469 case ISN_SETTYPE:
5470 {
5471 char *tofree;
5472
5473 smsg("%s%4d SETTYPE %s", pfx, current,
5474 type_name(iptr->isn_arg.type.ct_type, &tofree));
5475 vim_free(tofree);
5476 break;
5477 }
5478 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005479 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5480 smsg("%s%4d INVERT %d (!val)", pfx, current,
5481 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005482 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005483 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5484 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005485 break;
5486 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005487 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005488 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005489 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5490 pfx, current,
5491 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005492 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005493 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5494 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005495 break;
5496 case ISN_PUT:
5497 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5498 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005499 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005500 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5501 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005502 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005503 else
5504 smsg("%s%4d PUT %c %ld", pfx, current,
5505 iptr->isn_arg.put.put_regname,
5506 (long)iptr->isn_arg.put.put_lnum);
5507 break;
5508
5509 // TODO: summarize modifiers
5510 case ISN_CMDMOD:
5511 {
5512 char_u *buf;
5513 size_t len = produce_cmdmods(
5514 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5515
5516 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005517 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005518 {
5519 (void)produce_cmdmods(
5520 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5521 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5522 vim_free(buf);
5523 }
5524 break;
5525 }
5526 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5527
5528 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005529 smsg("%s%4d PROFILE START line %d", pfx, current,
5530 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005531 break;
5532
5533 case ISN_PROF_END:
5534 smsg("%s%4d PROFILE END", pfx, current);
5535 break;
5536
Bram Moolenaare99d4222021-06-13 14:01:26 +02005537 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005538 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5539 iptr->isn_arg.debug.dbg_break_lnum + 1,
5540 iptr->isn_lnum,
5541 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005542 break;
5543
Bram Moolenaar4c137212021-04-19 16:48:48 +02005544 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5545 iptr->isn_arg.unpack.unp_count,
5546 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5547 break;
5548 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5549 iptr->isn_arg.shuffle.shfl_item,
5550 iptr->isn_arg.shuffle.shfl_up);
5551 break;
5552 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5553
5554 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5555 return;
5556 }
5557
5558 out_flush(); // output one line at a time
5559 ui_breakcheck();
5560 if (got_int)
5561 break;
5562 }
5563}
5564
5565/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005566 * Handle command line completion for the :disassemble command.
5567 */
5568 void
5569set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5570{
5571 char_u *p;
5572
5573 // Default: expand user functions, "debug" and "profile"
5574 xp->xp_context = EXPAND_DISASSEMBLE;
5575 xp->xp_pattern = arg;
5576
5577 // first argument already typed: only user function names
5578 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5579 {
5580 xp->xp_context = EXPAND_USER_FUNC;
5581 xp->xp_pattern = skipwhite(p);
5582 }
5583}
5584
5585/*
5586 * Function given to ExpandGeneric() to obtain the list of :disassemble
5587 * arguments.
5588 */
5589 char_u *
5590get_disassemble_argument(expand_T *xp, int idx)
5591{
5592 if (idx == 0)
5593 return (char_u *)"debug";
5594 if (idx == 1)
5595 return (char_u *)"profile";
5596 return get_user_func_name(xp, idx - 2);
5597}
5598
5599/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005600 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005601 * We don't really need this at runtime, but we do have tests that require it,
5602 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005603 */
5604 void
5605ex_disassemble(exarg_T *eap)
5606{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005607 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005608 char_u *fname;
5609 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005610 dfunc_T *dfunc;
5611 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005612 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005613 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005614 compiletype_T compile_type = CT_NONE;
5615
5616 if (STRNCMP(arg, "profile", 7) == 0)
5617 {
5618 compile_type = CT_PROFILE;
5619 arg = skipwhite(arg + 7);
5620 }
5621 else if (STRNCMP(arg, "debug", 5) == 0)
5622 {
5623 compile_type = CT_DEBUG;
5624 arg = skipwhite(arg + 5);
5625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005626
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005627 if (STRNCMP(arg, "<lambda>", 8) == 0)
5628 {
5629 arg += 8;
5630 (void)getdigits(&arg);
5631 fname = vim_strnsave(eap->arg, arg - eap->arg);
5632 }
5633 else
5634 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005635 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005636 if (fname == NULL)
5637 {
5638 semsg(_(e_invarg2), eap->arg);
5639 return;
5640 }
5641
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005642 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005643 if (ufunc == NULL)
5644 {
5645 char_u *p = untrans_function_name(fname);
5646
5647 if (p != NULL)
5648 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005649 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005650 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005651 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652 if (ufunc == NULL)
5653 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005654 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005655 return;
5656 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005657 if (func_needs_compiling(ufunc, compile_type)
5658 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005659 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005660 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005661 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005662 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005663 return;
5664 }
5665 if (ufunc->uf_name_exp != NULL)
5666 msg((char *)ufunc->uf_name_exp);
5667 else
5668 msg((char *)ufunc->uf_name);
5669
5670 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005671 switch (compile_type)
5672 {
5673 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005674#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005675 instr = dfunc->df_instr_prof;
5676 instr_count = dfunc->df_instr_prof_count;
5677 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005678#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005679 // FALLTHROUGH
5680 case CT_NONE:
5681 instr = dfunc->df_instr;
5682 instr_count = dfunc->df_instr_count;
5683 break;
5684 case CT_DEBUG:
5685 instr = dfunc->df_instr_debug;
5686 instr_count = dfunc->df_instr_debug_count;
5687 break;
5688 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005689
Bram Moolenaar4c137212021-04-19 16:48:48 +02005690 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005691}
5692
5693/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005694 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005695 * list, etc. Mostly like what JavaScript does, except that empty list and
5696 * empty dictionary are FALSE.
5697 */
5698 int
5699tv2bool(typval_T *tv)
5700{
5701 switch (tv->v_type)
5702 {
5703 case VAR_NUMBER:
5704 return tv->vval.v_number != 0;
5705 case VAR_FLOAT:
5706#ifdef FEAT_FLOAT
5707 return tv->vval.v_float != 0.0;
5708#else
5709 break;
5710#endif
5711 case VAR_PARTIAL:
5712 return tv->vval.v_partial != NULL;
5713 case VAR_FUNC:
5714 case VAR_STRING:
5715 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5716 case VAR_LIST:
5717 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5718 case VAR_DICT:
5719 return tv->vval.v_dict != NULL
5720 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5721 case VAR_BOOL:
5722 case VAR_SPECIAL:
5723 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5724 case VAR_JOB:
5725#ifdef FEAT_JOB_CHANNEL
5726 return tv->vval.v_job != NULL;
5727#else
5728 break;
5729#endif
5730 case VAR_CHANNEL:
5731#ifdef FEAT_JOB_CHANNEL
5732 return tv->vval.v_channel != NULL;
5733#else
5734 break;
5735#endif
5736 case VAR_BLOB:
5737 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5738 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005739 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005740 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005741 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005742 break;
5743 }
5744 return FALSE;
5745}
5746
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005747 void
5748emsg_using_string_as(typval_T *tv, int as_number)
5749{
5750 semsg(_(as_number ? e_using_string_as_number_str
5751 : e_using_string_as_bool_str),
5752 tv->vval.v_string == NULL
5753 ? (char_u *)"" : tv->vval.v_string);
5754}
5755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005756/*
5757 * If "tv" is a string give an error and return FAIL.
5758 */
5759 int
5760check_not_string(typval_T *tv)
5761{
5762 if (tv->v_type == VAR_STRING)
5763 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005764 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005765 clear_tv(tv);
5766 return FAIL;
5767 }
5768 return OK;
5769}
5770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771
5772#endif // FEAT_EVAL