blob: f6456d64b23e0dad3d1c5df58e5278f792b02a22 [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
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
67// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
74
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
125 */
126 static int
127exe_newlist(int count, ectx_T *ectx)
128{
129 list_T *list = list_alloc_with_items(count);
130 int idx;
131 typval_T *tv;
132
133 if (list == NULL)
134 return FAIL;
135 for (idx = 0; idx < count; ++idx)
136 list_set_item(list, idx, STACK_TV_BOT(idx - count));
137
138 if (count > 0)
139 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200140 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarfe270812020-04-11 22:31:27 +0200141 return FAIL;
142 else
143 ++ectx->ec_stack.ga_len;
144 tv = STACK_TV_BOT(-1);
145 tv->v_type = VAR_LIST;
146 tv->vval.v_list = list;
147 ++list->lv_refcount;
148 return OK;
149}
150
151/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200152 * If debug_tick changed check if "ufunc" has a breakpoint and update
153 * "uf_has_breakpoint".
154 */
155 static void
156update_has_breakpoint(ufunc_T *ufunc)
157{
158 if (ufunc->uf_debug_tick != debug_tick)
159 {
160 linenr_T breakpoint;
161
162 ufunc->uf_debug_tick = debug_tick;
163 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
164 ufunc->uf_has_breakpoint = breakpoint > 0;
165 }
166}
167
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200168static garray_T dict_stack = GA_EMPTY;
169
170/*
171 * Put a value on the dict stack. This consumes "tv".
172 */
173 static int
174dict_stack_save(typval_T *tv)
175{
176 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000177 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200178 if (ga_grow(&dict_stack, 1) == FAIL)
179 return FAIL;
180 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
181 ++dict_stack.ga_len;
182 return OK;
183}
184
185/*
186 * Get the typval at top of the dict stack.
187 */
188 static typval_T *
189dict_stack_get_tv(void)
190{
191 if (dict_stack.ga_len == 0)
192 return NULL;
193 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
194}
195
196/*
197 * Get the dict at top of the dict stack.
198 */
199 static dict_T *
200dict_stack_get_dict(void)
201{
202 typval_T *tv;
203
204 if (dict_stack.ga_len == 0)
205 return NULL;
206 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
207 if (tv->v_type == VAR_DICT)
208 return tv->vval.v_dict;
209 return NULL;
210}
211
212/*
213 * Drop an item from the dict stack.
214 */
215 static void
216dict_stack_drop(void)
217{
218 if (dict_stack.ga_len == 0)
219 {
220 iemsg("Dict stack underflow");
221 return;
222 }
223 --dict_stack.ga_len;
224 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
225}
226
227/*
228 * Drop items from the dict stack until the length is equal to "len".
229 */
230 static void
231dict_stack_clear(int len)
232{
233 while (dict_stack.ga_len > len)
234 dict_stack_drop();
235}
236
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200237/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000238 * Get a pointer to useful "pt_outer" of "pt".
239 */
240 static outer_T *
241get_pt_outer(partial_T *pt)
242{
243 partial_T *ptref = pt->pt_outer_partial;
244
245 if (ptref == NULL)
246 return &pt->pt_outer;
247
248 // partial using partial (recursively)
249 while (ptref->pt_outer_partial != NULL)
250 ptref = ptref->pt_outer_partial;
251 return &ptref->pt_outer;
252}
253
254/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100256 * This adds a stack frame and sets the instruction pointer to the start of the
257 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200258 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259 *
260 * Stack has:
261 * - current arguments (already there)
262 * - omitted optional argument (default values) added here
263 * - stack frame:
264 * - pointer to calling function
265 * - Index of next instruction in calling function
266 * - previous frame pointer
267 * - reserved space for local variables
268 */
269 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100270call_dfunc(
271 int cdf_idx,
272 partial_T *pt,
273 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100274 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100276 int argcount = argcount_arg;
277 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
278 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200279 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100280 int arg_to_add;
281 int vararg_count = 0;
282 int varcount;
283 int idx;
284 estack_T *entry;
285 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200286 int res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287
288 if (dfunc->df_deleted)
289 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100290 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000291 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100292 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293 return FAIL;
294 }
295
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100296#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100297 if (do_profiling == PROF_YES)
298 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200299 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100300 {
301 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
302 + profile_info_ga.ga_len;
303 ++profile_info_ga.ga_len;
304 CLEAR_POINTER(info);
305 profile_may_start_func(info, ufunc,
306 (((dfunc_T *)def_functions.ga_data)
307 + ectx->ec_dfunc_idx)->df_ufunc);
308 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100309 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100310#endif
311
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200312 // Update uf_has_breakpoint if needed.
313 update_has_breakpoint(ufunc);
314
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200315 // When debugging and using "cont" switches to the not-debugged
316 // instructions, may need to still compile them.
Bram Moolenaar648594e2021-07-11 17:55:01 +0200317 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)))
318 {
319 res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL);
320
321 // compile_def_function() may cause def_functions.ga_data to change
322 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
323 }
324 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200325 {
326 if (did_emsg_cumul + did_emsg == did_emsg_before)
327 semsg(_(e_function_is_not_compiled_str),
328 printable_func_name(ufunc));
329 return FAIL;
330 }
331
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200332 if (ufunc->uf_va_name != NULL)
333 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200334 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200335 // Stack at time of call with 2 varargs:
336 // normal_arg
337 // optional_arg
338 // vararg_1
339 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200340 // After creating the list:
341 // normal_arg
342 // optional_arg
343 // vararg-list
344 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200345 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200346 // After creating the list
347 // normal_arg
348 // (space for optional_arg)
349 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200350 vararg_count = argcount - ufunc->uf_args.ga_len;
351 if (vararg_count < 0)
352 vararg_count = 0;
353 else
354 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200355 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200356 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200357
358 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200359 }
360
Bram Moolenaarfe270812020-04-11 22:31:27 +0200361 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200362 if (arg_to_add < 0)
363 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200364 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200365 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200366 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200367 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200368 return FAIL;
369 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200370
371 // Reserve space for:
372 // - missing arguments
373 // - stack frame
374 // - local variables
375 // - if needed: a counter for number of closures created in
376 // ectx->ec_funcrefs.
377 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200378 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379 return FAIL;
380
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100381 // If depth of calling is getting too high, don't execute the function.
382 if (funcdepth_increment() == FAIL)
383 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200384 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100385
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100386 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200387 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100388 {
389 floc = ALLOC_ONE(funclocal_T);
390 if (floc == NULL)
391 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200392 *floc = ectx->ec_funclocal;
393 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394 }
395
Bram Moolenaarfe270812020-04-11 22:31:27 +0200396 // Move the vararg-list to below the missing optional arguments.
397 if (vararg_count > 0 && arg_to_add > 0)
398 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100399
400 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200401 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200402 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200403 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100404
405 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100406 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
407 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200408 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200409 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
410 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100411 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100412 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200413 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414
415 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200416 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000417 {
418 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
419
420 tv->v_type = VAR_NUMBER;
421 tv->vval.v_number = 0;
422 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200423 if (dfunc->df_has_closure)
424 {
425 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
426
427 tv->v_type = VAR_NUMBER;
428 tv->vval.v_number = 0;
429 }
430 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100432 if (pt != NULL || ufunc->uf_partial != NULL
433 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100434 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200435 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100436
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200437 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100438 return FAIL;
439 if (pt != NULL)
440 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000441 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200442 ++pt->pt_refcount;
443 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100444 }
445 else if (ufunc->uf_partial != NULL)
446 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000447 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200448 ++ufunc->uf_partial->pt_refcount;
449 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100450 }
451 else
452 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200453 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200454 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200455 {
456 vim_free(ref);
457 return FAIL;
458 }
459 ref->or_outer_allocated = TRUE;
460 ref->or_outer->out_stack = &ectx->ec_stack;
461 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
462 if (ectx->ec_outer_ref != NULL)
463 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100464 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200465 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100466 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100467 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200468 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100469
Bram Moolenaarc970e422021-03-17 15:03:04 +0100470 ++ufunc->uf_calls;
471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100472 // Set execution state to the start of the called function.
473 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100474 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100475 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200476 if (entry != NULL)
477 {
478 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200479 // Save the current context so it can be restored on return.
480 entry->es_save_sctx = current_sctx;
481 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200482 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100483
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200484 // Start execution at the first instruction.
485 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486
487 return OK;
488}
489
490// Get pointer to item in the stack.
491#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
492
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000493// Double linked list of funcstack_T in use.
494static funcstack_T *first_funcstack = NULL;
495
496 static void
497add_funcstack_to_list(funcstack_T *funcstack)
498{
499 // Link in list of funcstacks.
500 if (first_funcstack != NULL)
501 first_funcstack->fs_prev = funcstack;
502 funcstack->fs_next = first_funcstack;
503 funcstack->fs_prev = NULL;
504 first_funcstack = funcstack;
505}
506
507 static void
508remove_funcstack_from_list(funcstack_T *funcstack)
509{
510 if (funcstack->fs_prev == NULL)
511 first_funcstack = funcstack->fs_next;
512 else
513 funcstack->fs_prev->fs_next = funcstack->fs_next;
514 if (funcstack->fs_next != NULL)
515 funcstack->fs_next->fs_prev = funcstack->fs_prev;
516}
517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200519 * Used when returning from a function: Check if any closure is still
520 * referenced. If so then move the arguments and variables to a separate piece
521 * of stack to be used when the closure is called.
522 * When "free_arguments" is TRUE the arguments are to be freed.
523 * Returns FAIL when out of memory.
524 */
525 static int
526handle_closure_in_use(ectx_T *ectx, int free_arguments)
527{
528 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
529 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200530 int argcount;
531 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200532 int idx;
533 typval_T *tv;
534 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200535 garray_T *gap = &ectx->ec_funcrefs;
536 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200537
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200538 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200539 return OK; // function was freed
540 if (dfunc->df_has_closure == 0)
541 return OK; // no closures
542 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
543 closure_count = tv->vval.v_number;
544 if (closure_count == 0)
545 return OK; // no funcrefs created
546
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200547 argcount = ufunc_argcount(dfunc->df_ufunc);
548 top = ectx->ec_frame_idx - argcount;
549
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200550 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200551 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200552 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200553 partial_T *pt;
554 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200555
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200556 if (off < 0)
557 continue; // count is off or already done
558 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200559 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200560 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200561 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200562 int i;
563
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000564 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200565 // unreferenced on return.
566 for (i = 0; i < dfunc->df_varcount; ++i)
567 {
568 typval_T *stv = STACK_TV(ectx->ec_frame_idx
569 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200570 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200571 --refcount;
572 }
573 if (refcount > 1)
574 {
575 closure_in_use = TRUE;
576 break;
577 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200578 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200579 }
580
581 if (closure_in_use)
582 {
583 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
584 typval_T *stack;
585
586 // A closure is using the arguments and/or local variables.
587 // Move them to the called function.
588 if (funcstack == NULL)
589 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000590
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200591 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
592 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200593 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
594 funcstack->fs_ga.ga_data = stack;
595 if (stack == NULL)
596 {
597 vim_free(funcstack);
598 return FAIL;
599 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000600 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200601
602 // Move or copy the arguments.
603 for (idx = 0; idx < argcount; ++idx)
604 {
605 tv = STACK_TV(top + idx);
606 if (free_arguments)
607 {
608 *(stack + idx) = *tv;
609 tv->v_type = VAR_UNKNOWN;
610 }
611 else
612 copy_tv(tv, stack + idx);
613 }
614 // Move the local variables.
615 for (idx = 0; idx < dfunc->df_varcount; ++idx)
616 {
617 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200618
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200619 // A partial created for a local function, that is also used as a
620 // local variable, has a reference count for the variable, thus
621 // will never go down to zero. When all these refcounts are one
622 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000623 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200624 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
625 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200626 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200627
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200628 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200629 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
630 gap->ga_len - closure_count + i])
631 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200632 }
633
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200634 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200635 tv->v_type = VAR_UNKNOWN;
636 }
637
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200638 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200639 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200640 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
641 - closure_count + idx];
642 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200643 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200644 ++funcstack->fs_refcount;
645 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100646 pt->pt_outer.out_stack = &funcstack->fs_ga;
647 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200648 }
649 }
650 }
651
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200652 for (idx = 0; idx < closure_count; ++idx)
653 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
654 - closure_count + idx]);
655 gap->ga_len -= closure_count;
656 if (gap->ga_len == 0)
657 ga_clear(gap);
658
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200659 return OK;
660}
661
662/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200663 * Called when a partial is freed or its reference count goes down to one. The
664 * funcstack may be the only reference to the partials in the local variables.
665 * Go over all of them, the funcref and can be freed if all partials
666 * referencing the funcstack have a reference count of one.
667 */
668 void
669funcstack_check_refcount(funcstack_T *funcstack)
670{
671 int i;
672 garray_T *gap = &funcstack->fs_ga;
673 int done = 0;
674
675 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
676 return;
677 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
678 {
679 typval_T *tv = ((typval_T *)gap->ga_data) + i;
680
681 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
682 && tv->vval.v_partial->pt_funcstack == funcstack
683 && tv->vval.v_partial->pt_refcount == 1)
684 ++done;
685 }
686 if (done == funcstack->fs_min_refcount)
687 {
688 typval_T *stack = gap->ga_data;
689
690 // All partials referencing the funcstack have a reference count of
691 // one, thus the funcstack is no longer of use.
692 for (i = 0; i < gap->ga_len; ++i)
693 clear_tv(stack + i);
694 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000695 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200696 vim_free(funcstack);
697 }
698}
699
700/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000701 * For garbage collecting: set references in all variables referenced by
702 * all funcstacks.
703 */
704 int
705set_ref_in_funcstacks(int copyID)
706{
707 funcstack_T *funcstack;
708
709 for (funcstack = first_funcstack; funcstack != NULL;
710 funcstack = funcstack->fs_next)
711 {
712 typval_T *stack = funcstack->fs_ga.ga_data;
713 int i;
714
715 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
716 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
717 return TRUE; // abort
718 }
719 return FALSE;
720}
721
722/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723 * Return from the current function.
724 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200725 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200726func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100729 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200730 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
731 + ectx->ec_dfunc_idx;
732 int argcount = ufunc_argcount(dfunc->df_ufunc);
733 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200734 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100735 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
736 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200737 funclocal_T *floc;
738#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100739 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
740 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741
Bram Moolenaar12d26532021-02-19 19:13:21 +0100742 if (do_profiling == PROF_YES)
743 {
744 ufunc_T *caller = prev_dfunc->df_ufunc;
745
746 if (dfunc->df_ufunc->uf_profiling
747 || (caller != NULL && caller->uf_profiling))
748 {
749 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
750 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
751 --profile_info_ga.ga_len;
752 }
753 }
754#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000755
756 // No check for uf_refcount being zero, cannot think of a way that would
757 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100758 --dfunc->df_ufunc->uf_calls;
759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200761 entry = estack_pop();
762 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200763 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200765 if (handle_closure_in_use(ectx, TRUE) == FAIL)
766 return FAIL;
767
768 // Clear the arguments.
769 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
770 clear_tv(STACK_TV(idx));
771
772 // Clear local variables and temp values, but not the return value.
773 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100774 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100776
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100777 // The return value should be on top of the stack. However, when aborting
778 // it may not be there and ec_frame_idx is the top of the stack.
779 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100780 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100781 ret_idx = 0;
782
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200783 if (ectx->ec_outer_ref != NULL)
784 {
785 if (ectx->ec_outer_ref->or_outer_allocated)
786 vim_free(ectx->ec_outer_ref->or_outer);
787 partial_unref(ectx->ec_outer_ref->or_partial);
788 vim_free(ectx->ec_outer_ref);
789 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100790
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100791 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100792 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100793 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
794 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200795 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
796 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200797 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100798 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100799 floc = (void *)STACK_TV(ectx->ec_frame_idx
800 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200801 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100802 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
803 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200804
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100805 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200806 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100807 else
808 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200809 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100810 vim_free(floc);
811 }
812
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100813 if (ret_idx > 0)
814 {
815 // Reset the stack to the position before the call, with a spot for the
816 // return value, moved there from above the frame.
817 ectx->ec_stack.ga_len = top + 1;
818 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
819 }
820 else
821 // Reset the stack to the position before the call.
822 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200823
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100824 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200825 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200826 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827}
828
829#undef STACK_TV
830
831/*
832 * Prepare arguments and rettv for calling a builtin or user function.
833 */
834 static int
835call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
836{
837 int idx;
838 typval_T *tv;
839
840 // Move arguments from bottom of the stack to argvars[] and add terminator.
841 for (idx = 0; idx < argcount; ++idx)
842 argvars[idx] = *STACK_TV_BOT(idx - argcount);
843 argvars[argcount].v_type = VAR_UNKNOWN;
844
845 // Result replaces the arguments on the stack.
846 if (argcount > 0)
847 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200848 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849 return FAIL;
850 else
851 ++ectx->ec_stack.ga_len;
852
853 // Default return value is zero.
854 tv = STACK_TV_BOT(-1);
855 tv->v_type = VAR_NUMBER;
856 tv->vval.v_number = 0;
857
858 return OK;
859}
860
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200861// Ugly global to avoid passing the execution context around through many
862// layers.
863static ectx_T *current_ectx = NULL;
864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865/*
866 * Call a builtin function by index.
867 */
868 static int
869call_bfunc(int func_idx, int argcount, ectx_T *ectx)
870{
871 typval_T argvars[MAX_FUNC_ARGS];
872 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100873 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200874 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200875 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876
877 if (call_prepare(argcount, argvars, ectx) == FAIL)
878 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200879 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200881 // Call the builtin function. Set "current_ectx" so that when it
882 // recursively invokes call_def_function() a closure context can be set.
883 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200885 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200886 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887
888 // Clear the arguments.
889 for (idx = 0; idx < argcount; ++idx)
890 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200891
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100892 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200893 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 return OK;
895}
896
897/*
898 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100899 * If the function is compiled this will add a stack frame and set the
900 * instruction pointer at the start of the function.
901 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200902 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100903 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 */
905 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100906call_ufunc(
907 ufunc_T *ufunc,
908 partial_T *pt,
909 int argcount,
910 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200911 isn_T *iptr,
912 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913{
914 typval_T argvars[MAX_FUNC_ARGS];
915 funcexe_T funcexe;
916 int error;
917 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100918 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200919 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920
Bram Moolenaare99d4222021-06-13 14:01:26 +0200921 if (func_needs_compiling(ufunc, compile_type)
922 && compile_def_function(ufunc, FALSE, compile_type, NULL)
923 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200924 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200925 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100926 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100927 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100928 if (error != FCERR_UNKNOWN)
929 {
930 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +0000931 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100932 else
Bram Moolenaare1242042021-12-16 20:56:57 +0000933 semsg(_(e_not_enough_arguments_for_function_str),
934 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100935 return FAIL;
936 }
937
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100938 // The function has been compiled, can call it quickly. For a function
939 // that was defined later: we can call it directly next time.
940 if (iptr != NULL)
941 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100942 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100943 iptr->isn_type = ISN_DCALL;
944 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
945 iptr->isn_arg.dfunc.cdf_argcount = argcount;
946 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200947 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949
950 if (call_prepare(argcount, argvars, ectx) == FAIL)
951 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200952 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000953 funcexe.fe_evaluate = TRUE;
954 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955
956 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000958 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959
960 // Clear the arguments.
961 for (idx = 0; idx < argcount; ++idx)
962 clear_tv(&argvars[idx]);
963
964 if (error != FCERR_NONE)
965 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000966 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 return FAIL;
968 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100969 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200970 // Error other than from calling the function itself.
971 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 return OK;
973}
974
975/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100976 * If command modifiers were applied restore them.
977 */
978 static void
979may_restore_cmdmod(funclocal_T *funclocal)
980{
981 if (funclocal->floc_restore_cmdmod)
982 {
983 cmdmod.cmod_filter_regmatch.regprog = NULL;
984 undo_cmdmod(&cmdmod);
985 cmdmod = funclocal->floc_save_cmdmod;
986 funclocal->floc_restore_cmdmod = FALSE;
987 }
988}
989
990/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200991 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
992 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200993 */
994 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200995vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200996{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200997 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200998}
999
1000/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 * Execute a function by "name".
1002 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001003 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004 * Returns FAIL if not found without an error message.
1005 */
1006 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001007call_by_name(
1008 char_u *name,
1009 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001010 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001011 isn_T *iptr,
1012 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013{
1014 ufunc_T *ufunc;
1015
1016 if (builtin_function(name, -1))
1017 {
1018 int func_idx = find_internal_func(name);
1019
1020 if (func_idx < 0)
1021 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001022 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 return FAIL;
1024 return call_bfunc(func_idx, argcount, ectx);
1025 }
1026
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001027 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001028
1029 if (ufunc == NULL)
1030 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001031 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001032
1033 if (script_autoload(name, TRUE))
1034 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001035 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001036
1037 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001038 return FAIL; // bail out if loading the script caused an error
1039 }
1040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001042 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001043 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001044 {
1045 int i;
1046 typval_T *argv = STACK_TV_BOT(0) - argcount;
1047
1048 // The function can change at runtime, check that the argument
1049 // types are correct.
1050 for (i = 0; i < argcount; ++i)
1051 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001052 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001053
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001054 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001055 type = ufunc->uf_arg_types[i];
1056 else if (ufunc->uf_va_type != NULL)
1057 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001058 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001059 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001060 return FAIL;
1061 }
1062 }
1063
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001064 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066
1067 return FAIL;
1068}
1069
1070 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001071call_partial(
1072 typval_T *tv,
1073 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001074 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001076 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001077 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001079 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001080 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081
1082 if (tv->v_type == VAR_PARTIAL)
1083 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001084 partial_T *pt = tv->vval.v_partial;
1085 int i;
1086
1087 if (pt->pt_argc > 0)
1088 {
1089 // Make space for arguments from the partial, shift the "argcount"
1090 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001091 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001092 return FAIL;
1093 for (i = 1; i <= argcount; ++i)
1094 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1095 ectx->ec_stack.ga_len += pt->pt_argc;
1096 argcount += pt->pt_argc;
1097
1098 // copy the arguments from the partial onto the stack
1099 for (i = 0; i < pt->pt_argc; ++i)
1100 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1101 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001102 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103
1104 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001105 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 name = pt->pt_name;
1108 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001109 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001111 if (name != NULL)
1112 {
1113 char_u fname_buf[FLEN_FIXED + 1];
1114 char_u *tofree = NULL;
1115 int error = FCERR_NONE;
1116 char_u *fname;
1117
1118 // May need to translate <SNR>123_ to K_SNR.
1119 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1120 if (error != FCERR_NONE)
1121 res = FAIL;
1122 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001123 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001124 vim_free(tofree);
1125 }
1126
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001127 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 {
1129 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001130 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001131 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 return FAIL;
1133 }
1134 return OK;
1135}
1136
1137/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001138 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1139 * TRUE.
1140 */
1141 static int
1142error_if_locked(int lock, char *error)
1143{
1144 if (lock & (VAR_LOCKED | VAR_FIXED))
1145 {
1146 emsg(_(error));
1147 return TRUE;
1148 }
1149 return FALSE;
1150}
1151
1152/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001153 * Give an error if "tv" is not a number and return FAIL.
1154 */
1155 static int
1156check_for_number(typval_T *tv)
1157{
1158 if (tv->v_type != VAR_NUMBER)
1159 {
1160 semsg(_(e_expected_str_but_got_str),
1161 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1162 return FAIL;
1163 }
1164 return OK;
1165}
1166
1167/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001168 * Store "tv" in variable "name".
1169 * This is for s: and g: variables.
1170 */
1171 static void
1172store_var(char_u *name, typval_T *tv)
1173{
1174 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001175 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001176
Bram Moolenaard877a572021-04-01 19:42:48 +02001177 if (tv->v_lock)
1178 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001179 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001180 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001181 restore_funccal();
1182}
1183
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001184/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001185 * Convert "tv" to a string.
1186 * Return FAIL if not allowed.
1187 */
1188 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001189do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001190{
1191 if (tv->v_type != VAR_STRING)
1192 {
1193 char_u *str;
1194
1195 if (is_2string_any)
1196 {
1197 switch (tv->v_type)
1198 {
1199 case VAR_SPECIAL:
1200 case VAR_BOOL:
1201 case VAR_NUMBER:
1202 case VAR_FLOAT:
1203 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001204
1205 case VAR_LIST:
1206 if (tolerant)
1207 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001208 char_u *s, *e, *p;
1209 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001210
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001211 ga_init2(&ga, sizeof(char_u *), 1);
1212
1213 // Convert to NL separated items, then
1214 // escape the items and replace the NL with
1215 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001216 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001217 if (str == NULL)
1218 return FAIL;
1219 s = str;
1220 while ((e = vim_strchr(s, '\n')) != NULL)
1221 {
1222 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001223 p = vim_strsave_fnameescape(s,
1224 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001225 if (p != NULL)
1226 {
1227 ga_concat(&ga, p);
1228 ga_concat(&ga, (char_u *)" ");
1229 vim_free(p);
1230 }
1231 s = e + 1;
1232 }
1233 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001234 clear_tv(tv);
1235 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001236 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001237 return OK;
1238 }
1239 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001240 default: to_string_error(tv->v_type);
1241 return FAIL;
1242 }
1243 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001244 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001245 clear_tv(tv);
1246 tv->v_type = VAR_STRING;
1247 tv->vval.v_string = str;
1248 }
1249 return OK;
1250}
1251
1252/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001253 * When the value of "sv" is a null list of dict, allocate it.
1254 */
1255 static void
1256allocate_if_null(typval_T *tv)
1257{
1258 switch (tv->v_type)
1259 {
1260 case VAR_LIST:
1261 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001262 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001263 break;
1264 case VAR_DICT:
1265 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001266 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001267 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001268 case VAR_BLOB:
1269 if (tv->vval.v_blob == NULL)
1270 (void)rettv_blob_alloc(tv);
1271 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001272 default:
1273 break;
1274 }
1275}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001276
Bram Moolenaare7525c52021-01-09 13:20:37 +01001277/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001278 * Return the character "str[index]" where "index" is the character index,
1279 * including composing characters.
1280 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001281 */
1282 char_u *
1283char_from_string(char_u *str, varnumber_T index)
1284{
1285 size_t nbyte = 0;
1286 varnumber_T nchar = index;
1287 size_t slen;
1288
1289 if (str == NULL)
1290 return NULL;
1291 slen = STRLEN(str);
1292
Bram Moolenaarff871402021-03-26 13:34:05 +01001293 // Do the same as for a list: a negative index counts from the end.
1294 // Optimization to check the first byte to be below 0x80 (and no composing
1295 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001296 if (index < 0)
1297 {
1298 int clen = 0;
1299
1300 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001301 {
1302 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1303 ++nbyte;
1304 else if (enc_utf8)
1305 nbyte += utfc_ptr2len(str + nbyte);
1306 else
1307 nbyte += mb_ptr2len(str + nbyte);
1308 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001309 nchar = clen + index;
1310 if (nchar < 0)
1311 // unlike list: index out of range results in empty string
1312 return NULL;
1313 }
1314
1315 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001316 {
1317 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1318 ++nbyte;
1319 else if (enc_utf8)
1320 nbyte += utfc_ptr2len(str + nbyte);
1321 else
1322 nbyte += mb_ptr2len(str + nbyte);
1323 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001324 if (nbyte >= slen)
1325 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001326 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001327}
1328
1329/*
1330 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001331 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001332 * If going over the end return "str_len".
1333 * If "idx" is negative count from the end, -1 is the last character.
1334 * When going over the start return -1.
1335 */
1336 static long
1337char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1338{
1339 varnumber_T nchar = idx;
1340 size_t nbyte = 0;
1341
1342 if (nchar >= 0)
1343 {
1344 while (nchar > 0 && nbyte < str_len)
1345 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001346 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001347 --nchar;
1348 }
1349 }
1350 else
1351 {
1352 nbyte = str_len;
1353 while (nchar < 0 && nbyte > 0)
1354 {
1355 --nbyte;
1356 nbyte -= mb_head_off(str, str + nbyte);
1357 ++nchar;
1358 }
1359 if (nchar < 0)
1360 return -1;
1361 }
1362 return (long)nbyte;
1363}
1364
1365/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001366 * Return the slice "str[first : last]" using character indexes. Composing
1367 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001368 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001369 * Return NULL when the result is empty.
1370 */
1371 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001372string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001373{
1374 long start_byte, end_byte;
1375 size_t slen;
1376
1377 if (str == NULL)
1378 return NULL;
1379 slen = STRLEN(str);
1380 start_byte = char_idx2byte(str, slen, first);
1381 if (start_byte < 0)
1382 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001383 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001384 end_byte = (long)slen;
1385 else
1386 {
1387 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001388 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001389 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001390 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001391 }
1392
1393 if (start_byte >= (long)slen || end_byte <= start_byte)
1394 return NULL;
1395 return vim_strnsave(str + start_byte, end_byte - start_byte);
1396}
1397
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001398/*
1399 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1400 * When "dfunc_idx" is negative don't give an error.
1401 * Returns NULL for an error.
1402 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001403 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001404get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001405{
1406 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001407 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1408 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001409 svar_T *sv;
1410
1411 if (sref->sref_seq != si->sn_script_seq)
1412 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001413 // The script was reloaded after the function was compiled, the
1414 // script_idx may not be valid.
1415 if (dfunc != NULL)
1416 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1417 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001418 return NULL;
1419 }
1420 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001421 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001422 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001423 if (dfunc != NULL)
1424 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001425 return NULL;
1426 }
1427 return sv;
1428}
1429
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001430/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001431 * Function passed to do_cmdline() for splitting a script joined by NL
1432 * characters.
1433 */
1434 static char_u *
1435get_split_sourceline(
1436 int c UNUSED,
1437 void *cookie,
1438 int indent UNUSED,
1439 getline_opt_T options UNUSED)
1440{
1441 source_cookie_T *sp = (source_cookie_T *)cookie;
1442 char_u *p;
1443 char_u *line;
1444
1445 if (*sp->nextline == NUL)
1446 return NULL;
1447 p = vim_strchr(sp->nextline, '\n');
1448 if (p == NULL)
1449 {
1450 line = vim_strsave(sp->nextline);
1451 sp->nextline += STRLEN(sp->nextline);
1452 }
1453 else
1454 {
1455 line = vim_strnsave(sp->nextline, p - sp->nextline);
1456 sp->nextline = p + 1;
1457 }
1458 return line;
1459}
1460
1461/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 * Execute a function by "name".
1463 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001464 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 */
1466 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001467call_eval_func(
1468 char_u *name,
1469 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001470 ectx_T *ectx,
1471 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472{
Bram Moolenaared677f52020-08-12 16:38:10 +02001473 int called_emsg_before = called_emsg;
1474 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001476 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001477 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001479 dictitem_T *v;
1480
1481 v = find_var(name, NULL, FALSE);
1482 if (v == NULL)
1483 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001484 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001485 return FAIL;
1486 }
1487 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1488 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001489 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001490 return FAIL;
1491 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001492 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001494 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495}
1496
1497/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001498 * When a function reference is used, fill a partial with the information
1499 * needed, especially when it is used as a closure.
1500 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001501 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001502fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1503{
1504 pt->pt_func = ufunc;
1505 pt->pt_refcount = 1;
1506
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001507 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001508 {
1509 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1510 + ectx->ec_dfunc_idx;
1511
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001512 // The closure may need to find arguments and local variables in the
1513 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001514 pt->pt_outer.out_stack = &ectx->ec_stack;
1515 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001516 if (ectx->ec_outer_ref != NULL)
1517 {
1518 // The current context already has a context, link to that one.
1519 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1520 if (ectx->ec_outer_ref->or_partial != NULL)
1521 {
1522 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1523 ++pt->pt_outer.out_up_partial->pt_refcount;
1524 }
1525 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001526
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001527 // If this function returns and the closure is still being used, we
1528 // need to make a copy of the context (arguments and local variables).
1529 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001530 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001531 {
1532 vim_free(pt);
1533 return FAIL;
1534 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001535 // Extra variable keeps the count of closures created in the current
1536 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001537 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1538 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1539
1540 ((partial_T **)ectx->ec_funcrefs.ga_data)
1541 [ectx->ec_funcrefs.ga_len] = pt;
1542 ++pt->pt_refcount;
1543 ++ectx->ec_funcrefs.ga_len;
1544 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001545 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001546 return OK;
1547}
1548
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001549/*
1550 * Execute iptr->isn_arg.string as an Ex command.
1551 */
1552 static int
1553exec_command(isn_T *iptr)
1554{
1555 source_cookie_T cookie;
1556
1557 SOURCING_LNUM = iptr->isn_lnum;
1558 // Pass getsourceline to get an error for a missing ":end"
1559 // command.
1560 CLEAR_FIELD(cookie);
1561 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1562 if (do_cmdline(iptr->isn_arg.string,
1563 getsourceline, &cookie,
1564 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1565 || did_emsg)
1566 return FAIL;
1567 return OK;
1568}
1569
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001570// used for v_instr of typval of VAR_INSTR
1571struct instr_S {
1572 ectx_T *instr_ectx;
1573 isn_T *instr_instr;
1574};
1575
Bram Moolenaar4c137212021-04-19 16:48:48 +02001576// used for substitute_instr
1577typedef struct subs_expr_S {
1578 ectx_T *subs_ectx;
1579 isn_T *subs_instr;
1580 int subs_status;
1581} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582
1583// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001584#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585
1586// Get pointer to item at the bottom of the stack, -1 is the bottom.
1587#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001588#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 +01001589
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001590// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001591#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 +01001592
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001593// Set when calling do_debug().
1594static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001595static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001596
1597/*
1598 * When debugging lookup "name" and return the typeval.
1599 * When not found return NULL.
1600 */
1601 typval_T *
1602lookup_debug_var(char_u *name)
1603{
1604 int idx;
1605 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001606 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001607 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001608 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001609
1610 if (ectx == NULL)
1611 return NULL;
1612 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1613
1614 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001615 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001616 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001617 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001618 return STACK_TV_VAR(idx);
1619 }
1620
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001621 // Go through argument names.
1622 ufunc = dfunc->df_ufunc;
1623 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1624 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1625 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1626 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1627 - varargs_off + idx);
1628 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1629 return STACK_TV(ectx->ec_frame_idx - 1);
1630
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001631 return NULL;
1632}
1633
Bram Moolenaar26a44842021-09-02 18:49:06 +02001634/*
1635 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1636 * breakpoint was set in that function or when there is any expression.
1637 */
1638 int
1639may_break_in_function(ufunc_T *ufunc)
1640{
1641 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1642}
1643
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001644 static void
1645handle_debug(isn_T *iptr, ectx_T *ectx)
1646{
1647 char_u *line;
1648 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1649 + ectx->ec_dfunc_idx)->df_ufunc;
1650 isn_T *ni;
1651 int end_lnum = iptr->isn_lnum;
1652 garray_T ga;
1653 int lnum;
1654
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001655 if (ex_nesting_level > debug_break_level)
1656 {
1657 linenr_T breakpoint;
1658
Bram Moolenaar26a44842021-09-02 18:49:06 +02001659 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001660 return;
1661
1662 // check for the next breakpoint if needed
1663 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001664 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001665 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1666 return;
1667 }
1668
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001669 SOURCING_LNUM = iptr->isn_lnum;
1670 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001671 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001672
1673 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1674 if (ni->isn_type == ISN_DEBUG
1675 || ni->isn_type == ISN_RETURN
1676 || ni->isn_type == ISN_RETURN_VOID)
1677 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001678 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001679 break;
1680 }
1681
1682 if (end_lnum > iptr->isn_lnum)
1683 {
1684 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001685 for (lnum = iptr->isn_lnum; lnum < end_lnum
1686 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001687 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001688 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001689
Bram Moolenaar303215d2021-07-07 20:10:43 +02001690 if (p == NULL)
1691 continue; // left over from continuation line
1692 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001693 if (*p == '#')
1694 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001695 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001696 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1697 if (STRNCMP(p, "def ", 4) == 0)
1698 break;
1699 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001700 line = ga_concat_strings(&ga, " ");
1701 vim_free(ga.ga_data);
1702 }
1703 else
1704 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001705
Dominique Pelle6e969552021-06-17 13:53:41 +02001706 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001707 debug_context = NULL;
1708
1709 if (end_lnum > iptr->isn_lnum)
1710 vim_free(line);
1711}
1712
Bram Moolenaar4c137212021-04-19 16:48:48 +02001713/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001714 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001715 * Returns OK, FAIL or NOTDONE (uncatchable error).
1716 */
1717 static int
1718execute_storeindex(isn_T *iptr, ectx_T *ectx)
1719{
1720 vartype_T dest_type = iptr->isn_arg.vartype;
1721 typval_T *tv;
1722 typval_T *tv_idx = STACK_TV_BOT(-2);
1723 typval_T *tv_dest = STACK_TV_BOT(-1);
1724 int status = OK;
1725
1726 // Stack contains:
1727 // -3 value to be stored
1728 // -2 index
1729 // -1 dict or list
1730 tv = STACK_TV_BOT(-3);
1731 SOURCING_LNUM = iptr->isn_lnum;
1732 if (dest_type == VAR_ANY)
1733 {
1734 dest_type = tv_dest->v_type;
1735 if (dest_type == VAR_DICT)
1736 status = do_2string(tv_idx, TRUE, FALSE);
1737 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1738 {
1739 emsg(_(e_number_expected));
1740 status = FAIL;
1741 }
1742 }
1743 else if (dest_type != tv_dest->v_type)
1744 {
1745 // just in case, should be OK
1746 semsg(_(e_expected_str_but_got_str),
1747 vartype_name(dest_type),
1748 vartype_name(tv_dest->v_type));
1749 status = FAIL;
1750 }
1751
1752 if (status == OK && dest_type == VAR_LIST)
1753 {
1754 long lidx = (long)tv_idx->vval.v_number;
1755 list_T *list = tv_dest->vval.v_list;
1756
1757 if (list == NULL)
1758 {
1759 emsg(_(e_list_not_set));
1760 return FAIL;
1761 }
1762 if (lidx < 0 && list->lv_len + lidx >= 0)
1763 // negative index is relative to the end
1764 lidx = list->lv_len + lidx;
1765 if (lidx < 0 || lidx > list->lv_len)
1766 {
1767 semsg(_(e_list_index_out_of_range_nr), lidx);
1768 return FAIL;
1769 }
1770 if (lidx < list->lv_len)
1771 {
1772 listitem_T *li = list_find(list, lidx);
1773
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001774 if (error_if_locked(li->li_tv.v_lock,
1775 e_cannot_change_locked_list_item))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001776 return FAIL;
1777 // overwrite existing list item
1778 clear_tv(&li->li_tv);
1779 li->li_tv = *tv;
1780 }
1781 else
1782 {
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001783 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001784 return FAIL;
1785 // append to list, only fails when out of memory
1786 if (list_append_tv(list, tv) == FAIL)
1787 return NOTDONE;
1788 clear_tv(tv);
1789 }
1790 }
1791 else if (status == OK && dest_type == VAR_DICT)
1792 {
1793 char_u *key = tv_idx->vval.v_string;
1794 dict_T *dict = tv_dest->vval.v_dict;
1795 dictitem_T *di;
1796
1797 SOURCING_LNUM = iptr->isn_lnum;
1798 if (dict == NULL)
1799 {
1800 emsg(_(e_dictionary_not_set));
1801 return FAIL;
1802 }
1803 if (key == NULL)
1804 key = (char_u *)"";
1805 di = dict_find(dict, key, -1);
1806 if (di != NULL)
1807 {
1808 if (error_if_locked(di->di_tv.v_lock, e_cannot_change_dict_item))
1809 return FAIL;
1810 // overwrite existing value
1811 clear_tv(&di->di_tv);
1812 di->di_tv = *tv;
1813 }
1814 else
1815 {
1816 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1817 return FAIL;
1818 // add to dict, only fails when out of memory
1819 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1820 return NOTDONE;
1821 clear_tv(tv);
1822 }
1823 }
1824 else if (status == OK && dest_type == VAR_BLOB)
1825 {
1826 long lidx = (long)tv_idx->vval.v_number;
1827 blob_T *blob = tv_dest->vval.v_blob;
1828 varnumber_T nr;
1829 int error = FALSE;
1830 int len;
1831
1832 if (blob == NULL)
1833 {
1834 emsg(_(e_blob_not_set));
1835 return FAIL;
1836 }
1837 len = blob_len(blob);
1838 if (lidx < 0 && len + lidx >= 0)
1839 // negative index is relative to the end
1840 lidx = len + lidx;
1841
1842 // Can add one byte at the end.
1843 if (lidx < 0 || lidx > len)
1844 {
1845 semsg(_(e_blob_index_out_of_range_nr), lidx);
1846 return FAIL;
1847 }
1848 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1849 return FAIL;
1850 nr = tv_get_number_chk(tv, &error);
1851 if (error)
1852 return FAIL;
1853 blob_set_append(blob, lidx, nr);
1854 }
1855 else
1856 {
1857 status = FAIL;
1858 semsg(_(e_cannot_index_str), vartype_name(dest_type));
1859 }
1860
1861 clear_tv(tv_idx);
1862 clear_tv(tv_dest);
1863 ectx->ec_stack.ga_len -= 3;
1864 if (status == FAIL)
1865 {
1866 clear_tv(tv);
1867 return FAIL;
1868 }
1869 return OK;
1870}
1871
1872/*
1873 * Store a value in a blob range.
1874 */
1875 static int
1876execute_storerange(isn_T *iptr, ectx_T *ectx)
1877{
1878 typval_T *tv;
1879 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1880 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1881 typval_T *tv_dest = STACK_TV_BOT(-1);
1882 int status = OK;
1883
1884 // Stack contains:
1885 // -4 value to be stored
1886 // -3 first index or "none"
1887 // -2 second index or "none"
1888 // -1 destination list or blob
1889 tv = STACK_TV_BOT(-4);
1890 if (tv_dest->v_type == VAR_LIST)
1891 {
1892 long n1;
1893 long n2;
1894 int error = FALSE;
1895
1896 SOURCING_LNUM = iptr->isn_lnum;
1897 n1 = (long)tv_get_number_chk(tv_idx1, &error);
1898 if (error)
1899 status = FAIL;
1900 else
1901 {
1902 if (tv_idx2->v_type == VAR_SPECIAL
1903 && tv_idx2->vval.v_number == VVAL_NONE)
1904 n2 = list_len(tv_dest->vval.v_list) - 1;
1905 else
1906 n2 = (long)tv_get_number_chk(tv_idx2, &error);
1907 if (error)
1908 status = FAIL;
1909 else
1910 {
1911 listitem_T *li1 = check_range_index_one(
1912 tv_dest->vval.v_list, &n1, FALSE);
1913
1914 if (li1 == NULL)
1915 status = FAIL;
1916 else
1917 {
1918 status = check_range_index_two(
1919 tv_dest->vval.v_list,
1920 &n1, li1, &n2, FALSE);
1921 if (status != FAIL)
1922 status = list_assign_range(
1923 tv_dest->vval.v_list,
1924 tv->vval.v_list,
1925 n1,
1926 n2,
1927 tv_idx2->v_type == VAR_SPECIAL,
1928 (char_u *)"=",
1929 (char_u *)"[unknown]");
1930 }
1931 }
1932 }
1933 }
1934 else if (tv_dest->v_type == VAR_BLOB)
1935 {
1936 varnumber_T n1;
1937 varnumber_T n2;
1938 int error = FALSE;
1939
1940 n1 = tv_get_number_chk(tv_idx1, &error);
1941 if (error)
1942 status = FAIL;
1943 else
1944 {
1945 if (tv_idx2->v_type == VAR_SPECIAL
1946 && tv_idx2->vval.v_number == VVAL_NONE)
1947 n2 = blob_len(tv_dest->vval.v_blob) - 1;
1948 else
1949 n2 = tv_get_number_chk(tv_idx2, &error);
1950 if (error)
1951 status = FAIL;
1952 else
1953 {
1954 long bloblen = blob_len(tv_dest->vval.v_blob);
1955
1956 if (check_blob_index(bloblen,
1957 n1, FALSE) == FAIL
1958 || check_blob_range(bloblen,
1959 n1, n2, FALSE) == FAIL)
1960 status = FAIL;
1961 else
1962 status = blob_set_range(
1963 tv_dest->vval.v_blob, n1, n2, tv);
1964 }
1965 }
1966 }
1967 else
1968 {
1969 status = FAIL;
1970 emsg(_(e_blob_required));
1971 }
1972
1973 clear_tv(tv_idx1);
1974 clear_tv(tv_idx2);
1975 clear_tv(tv_dest);
1976 ectx->ec_stack.ga_len -= 4;
1977 clear_tv(tv);
1978
1979 return status;
1980}
1981
1982/*
1983 * Unlet item in list or dict variable.
1984 */
1985 static int
1986execute_unletindex(isn_T *iptr, ectx_T *ectx)
1987{
1988 typval_T *tv_idx = STACK_TV_BOT(-2);
1989 typval_T *tv_dest = STACK_TV_BOT(-1);
1990 int status = OK;
1991
1992 // Stack contains:
1993 // -2 index
1994 // -1 dict or list
1995 if (tv_dest->v_type == VAR_DICT)
1996 {
1997 // unlet a dict item, index must be a string
1998 if (tv_idx->v_type != VAR_STRING)
1999 {
2000 SOURCING_LNUM = iptr->isn_lnum;
2001 semsg(_(e_expected_str_but_got_str),
2002 vartype_name(VAR_STRING),
2003 vartype_name(tv_idx->v_type));
2004 status = FAIL;
2005 }
2006 else
2007 {
2008 dict_T *d = tv_dest->vval.v_dict;
2009 char_u *key = tv_idx->vval.v_string;
2010 dictitem_T *di = NULL;
2011
2012 if (d != NULL && value_check_lock(
2013 d->dv_lock, NULL, FALSE))
2014 status = FAIL;
2015 else
2016 {
2017 SOURCING_LNUM = iptr->isn_lnum;
2018 if (key == NULL)
2019 key = (char_u *)"";
2020 if (d != NULL)
2021 di = dict_find(d, key, (int)STRLEN(key));
2022 if (di == NULL)
2023 {
2024 // NULL dict is equivalent to empty dict
2025 semsg(_(e_key_not_present_in_dictionary),
2026 key);
2027 status = FAIL;
2028 }
2029 else if (var_check_fixed(di->di_flags,
2030 NULL, FALSE)
2031 || var_check_ro(di->di_flags,
2032 NULL, FALSE))
2033 status = FAIL;
2034 else
2035 dictitem_remove(d, di);
2036 }
2037 }
2038 }
2039 else if (tv_dest->v_type == VAR_LIST)
2040 {
2041 // unlet a List item, index must be a number
2042 SOURCING_LNUM = iptr->isn_lnum;
2043 if (check_for_number(tv_idx) == FAIL)
2044 {
2045 status = FAIL;
2046 }
2047 else
2048 {
2049 list_T *l = tv_dest->vval.v_list;
2050 long n = (long)tv_idx->vval.v_number;
2051
2052 if (l != NULL && value_check_lock(
2053 l->lv_lock, NULL, FALSE))
2054 status = FAIL;
2055 else
2056 {
2057 listitem_T *li = list_find(l, n);
2058
2059 if (li == NULL)
2060 {
2061 SOURCING_LNUM = iptr->isn_lnum;
2062 semsg(_(e_list_index_out_of_range_nr), n);
2063 status = FAIL;
2064 }
2065 else if (value_check_lock(li->li_tv.v_lock,
2066 NULL, FALSE))
2067 status = FAIL;
2068 else
2069 listitem_remove(l, li);
2070 }
2071 }
2072 }
2073 else
2074 {
2075 status = FAIL;
2076 semsg(_(e_cannot_index_str),
2077 vartype_name(tv_dest->v_type));
2078 }
2079
2080 clear_tv(tv_idx);
2081 clear_tv(tv_dest);
2082 ectx->ec_stack.ga_len -= 2;
2083
2084 return status;
2085}
2086
2087/*
2088 * Unlet a range of items in a list variable.
2089 */
2090 static int
2091execute_unletrange(isn_T *iptr, ectx_T *ectx)
2092{
2093 // Stack contains:
2094 // -3 index1
2095 // -2 index2
2096 // -1 dict or list
2097 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2098 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2099 typval_T *tv_dest = STACK_TV_BOT(-1);
2100 int status = OK;
2101
2102 if (tv_dest->v_type == VAR_LIST)
2103 {
2104 // indexes must be a number
2105 SOURCING_LNUM = iptr->isn_lnum;
2106 if (check_for_number(tv_idx1) == FAIL
2107 || (tv_idx2->v_type != VAR_SPECIAL
2108 && check_for_number(tv_idx2) == FAIL))
2109 {
2110 status = FAIL;
2111 }
2112 else
2113 {
2114 list_T *l = tv_dest->vval.v_list;
2115 long n1 = (long)tv_idx1->vval.v_number;
2116 long n2 = tv_idx2->v_type == VAR_SPECIAL
2117 ? 0 : (long)tv_idx2->vval.v_number;
2118 listitem_T *li;
2119
2120 li = list_find_index(l, &n1);
2121 if (li == NULL)
2122 status = FAIL;
2123 else
2124 {
2125 if (n1 < 0)
2126 n1 = list_idx_of_item(l, li);
2127 if (n2 < 0)
2128 {
2129 listitem_T *li2 = list_find(l, n2);
2130
2131 if (li2 == NULL)
2132 status = FAIL;
2133 else
2134 n2 = list_idx_of_item(l, li2);
2135 }
2136 if (status != FAIL
2137 && tv_idx2->v_type != VAR_SPECIAL
2138 && n2 < n1)
2139 {
2140 semsg(_(e_list_index_out_of_range_nr), n2);
2141 status = FAIL;
2142 }
2143 if (status != FAIL
2144 && list_unlet_range(l, li, NULL, n1,
2145 tv_idx2->v_type != VAR_SPECIAL, n2)
2146 == FAIL)
2147 status = FAIL;
2148 }
2149 }
2150 }
2151 else
2152 {
2153 status = FAIL;
2154 SOURCING_LNUM = iptr->isn_lnum;
2155 semsg(_(e_cannot_index_str),
2156 vartype_name(tv_dest->v_type));
2157 }
2158
2159 clear_tv(tv_idx1);
2160 clear_tv(tv_idx2);
2161 clear_tv(tv_dest);
2162 ectx->ec_stack.ga_len -= 3;
2163
2164 return status;
2165}
2166
2167/*
2168 * Top of a for loop.
2169 */
2170 static int
2171execute_for(isn_T *iptr, ectx_T *ectx)
2172{
2173 typval_T *tv;
2174 typval_T *ltv = STACK_TV_BOT(-1);
2175 typval_T *idxtv =
2176 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2177
2178 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2179 return FAIL;
2180 if (ltv->v_type == VAR_LIST)
2181 {
2182 list_T *list = ltv->vval.v_list;
2183
2184 // push the next item from the list
2185 ++idxtv->vval.v_number;
2186 if (list == NULL
2187 || idxtv->vval.v_number >= list->lv_len)
2188 {
2189 // past the end of the list, jump to "endfor"
2190 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2191 may_restore_cmdmod(&ectx->ec_funclocal);
2192 }
2193 else if (list->lv_first == &range_list_item)
2194 {
2195 // non-materialized range() list
2196 tv = STACK_TV_BOT(0);
2197 tv->v_type = VAR_NUMBER;
2198 tv->v_lock = 0;
2199 tv->vval.v_number = list_find_nr(
2200 list, idxtv->vval.v_number, NULL);
2201 ++ectx->ec_stack.ga_len;
2202 }
2203 else
2204 {
2205 listitem_T *li = list_find(list,
2206 idxtv->vval.v_number);
2207
2208 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2209 ++ectx->ec_stack.ga_len;
2210 }
2211 }
2212 else if (ltv->v_type == VAR_STRING)
2213 {
2214 char_u *str = ltv->vval.v_string;
2215
2216 // The index is for the last byte of the previous
2217 // character.
2218 ++idxtv->vval.v_number;
2219 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2220 {
2221 // past the end of the string, jump to "endfor"
2222 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2223 may_restore_cmdmod(&ectx->ec_funclocal);
2224 }
2225 else
2226 {
2227 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2228
2229 // Push the next character from the string.
2230 tv = STACK_TV_BOT(0);
2231 tv->v_type = VAR_STRING;
2232 tv->vval.v_string = vim_strnsave(
2233 str + idxtv->vval.v_number, clen);
2234 ++ectx->ec_stack.ga_len;
2235 idxtv->vval.v_number += clen - 1;
2236 }
2237 }
2238 else if (ltv->v_type == VAR_BLOB)
2239 {
2240 blob_T *blob = ltv->vval.v_blob;
2241
2242 // When we get here the first time make a copy of the
2243 // blob, so that the iteration still works when it is
2244 // changed.
2245 if (idxtv->vval.v_number == -1 && blob != NULL)
2246 {
2247 blob_copy(blob, ltv);
2248 blob_unref(blob);
2249 blob = ltv->vval.v_blob;
2250 }
2251
2252 // The index is for the previous byte.
2253 ++idxtv->vval.v_number;
2254 if (blob == NULL
2255 || idxtv->vval.v_number >= blob_len(blob))
2256 {
2257 // past the end of the blob, jump to "endfor"
2258 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2259 may_restore_cmdmod(&ectx->ec_funclocal);
2260 }
2261 else
2262 {
2263 // Push the next byte from the blob.
2264 tv = STACK_TV_BOT(0);
2265 tv->v_type = VAR_NUMBER;
2266 tv->vval.v_number = blob_get(blob,
2267 idxtv->vval.v_number);
2268 ++ectx->ec_stack.ga_len;
2269 }
2270 }
2271 else
2272 {
2273 semsg(_(e_for_loop_on_str_not_supported),
2274 vartype_name(ltv->v_type));
2275 return FAIL;
2276 }
2277 return OK;
2278}
2279
2280/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002281 * Load instruction for w:/b:/g:/t: variable.
2282 * "isn_type" is used instead of "iptr->isn_type".
2283 */
2284 static int
2285load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2286{
2287 dictitem_T *di = NULL;
2288 hashtab_T *ht = NULL;
2289 char namespace;
2290
2291 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2292 return NOTDONE;
2293 switch (isn_type)
2294 {
2295 case ISN_LOADG:
2296 ht = get_globvar_ht();
2297 namespace = 'g';
2298 break;
2299 case ISN_LOADB:
2300 ht = &curbuf->b_vars->dv_hashtab;
2301 namespace = 'b';
2302 break;
2303 case ISN_LOADW:
2304 ht = &curwin->w_vars->dv_hashtab;
2305 namespace = 'w';
2306 break;
2307 case ISN_LOADT:
2308 ht = &curtab->tp_vars->dv_hashtab;
2309 namespace = 't';
2310 break;
2311 default: // Cannot reach here
2312 return NOTDONE;
2313 }
2314 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2315
2316 if (di == NULL && ht == get_globvar_ht()
2317 && vim_strchr(iptr->isn_arg.string,
2318 AUTOLOAD_CHAR) != NULL)
2319 {
2320 // Global variable has an autoload name, may still need
2321 // to load the script.
2322 if (script_autoload(iptr->isn_arg.string, FALSE))
2323 di = find_var_in_ht(ht, 0,
2324 iptr->isn_arg.string, TRUE);
2325 if (did_emsg)
2326 return FAIL;
2327 }
2328
2329 if (di == NULL)
2330 {
2331 SOURCING_LNUM = iptr->isn_lnum;
2332 if (vim_strchr(iptr->isn_arg.string,
2333 AUTOLOAD_CHAR) != NULL)
2334 // no check if the item exists in the script but
2335 // isn't exported, it is too complicated
2336 semsg(_(e_item_not_found_in_script_str),
2337 iptr->isn_arg.string);
2338 else
2339 semsg(_(e_undefined_variable_char_str),
2340 namespace, iptr->isn_arg.string);
2341 return FAIL;
2342 }
2343 else
2344 {
2345 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2346 ++ectx->ec_stack.ga_len;
2347 }
2348 return OK;
2349}
2350
2351/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002352 * Execute instructions in execution context "ectx".
2353 * Return OK or FAIL;
2354 */
2355 static int
2356exec_instructions(ectx_T *ectx)
2357{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002358 int ret = FAIL;
2359 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002360 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002361
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002362 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002363 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002364
Bram Moolenaarff652882021-05-16 15:24:49 +02002365 // Only catch exceptions in this instruction list.
2366 ectx->ec_trylevel_at_start = trylevel;
2367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 for (;;)
2369 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002370 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002372 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002373
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002374 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002375 {
2376 line_breakcheck();
2377 breakcheck_count = 0;
2378 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002379 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002380 {
2381 // Turn CTRL-C into an exception.
2382 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002383 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002384 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002385 did_throw = TRUE;
2386 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002388 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002389 {
2390 // Turn an error message into an exception.
2391 did_emsg = FALSE;
2392 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002393 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002394 did_throw = TRUE;
2395 *msg_list = NULL;
2396 }
2397
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002398 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002400 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002401 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002402 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403
2404 // An exception jumps to the first catch, finally, or returns from
2405 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002406 while (index > 0)
2407 {
2408 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002409 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002410 break;
2411 // In the catch and finally block of this try we have to go up
2412 // one level.
2413 --index;
2414 trycmd = NULL;
2415 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002416 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002418 if (trycmd->tcd_in_catch)
2419 {
2420 // exception inside ":catch", jump to ":finally" once
2421 ectx->ec_iidx = trycmd->tcd_finally_idx;
2422 trycmd->tcd_finally_idx = 0;
2423 }
2424 else
2425 // jump to first ":catch"
2426 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002427 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002428 did_throw = FALSE; // don't come back here until :endtry
2429 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 }
2431 else
2432 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002433 // Not inside try or need to return from current functions.
2434 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002435 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002436 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002437 tv = STACK_TV_BOT(0);
2438 tv->v_type = VAR_NUMBER;
2439 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002440 ++ectx->ec_stack.ga_len;
2441 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002443 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002444 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002445 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002446 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 goto done;
2448 }
2449
Bram Moolenaar4c137212021-04-19 16:48:48 +02002450 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002451 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452 }
2453 continue;
2454 }
2455
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002456 /*
2457 * Big switch on the instruction. Most compilers will be turning this
2458 * into an efficient lookup table, since the "case" values are an enum
2459 * with sequential numbers. It may look ugly, but it should be the
2460 * most efficient way.
2461 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002462 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 switch (iptr->isn_type)
2464 {
2465 // execute Ex command line
2466 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002467 if (exec_command(iptr) == FAIL)
2468 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 break;
2470
Bram Moolenaar20677332021-06-06 17:02:53 +02002471 // execute Ex command line split at NL characters.
2472 case ISN_EXEC_SPLIT:
2473 {
2474 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002475 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002476
2477 SOURCING_LNUM = iptr->isn_lnum;
2478 CLEAR_FIELD(cookie);
2479 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2480 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002481 line = get_split_sourceline(0, &cookie, 0, 0);
2482 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002483 get_split_sourceline, &cookie,
2484 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2485 == FAIL
2486 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002487 {
2488 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002489 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002490 }
2491 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002492 }
2493 break;
2494
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002495 // execute Ex command line that is only a range
2496 case ISN_EXECRANGE:
2497 {
2498 exarg_T ea;
2499 char *error = NULL;
2500
2501 CLEAR_FIELD(ea);
2502 ea.cmdidx = CMD_SIZE;
2503 ea.addr_type = ADDR_LINES;
2504 ea.cmd = iptr->isn_arg.string;
2505 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002506 if (ea.cmd == NULL)
2507 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002508 if (error == NULL)
2509 error = ex_range_without_command(&ea);
2510 if (error != NULL)
2511 {
2512 SOURCING_LNUM = iptr->isn_lnum;
2513 emsg(error);
2514 goto on_error;
2515 }
2516 }
2517 break;
2518
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002519 // Evaluate an expression with legacy syntax, push it onto the
2520 // stack.
2521 case ISN_LEGACY_EVAL:
2522 {
2523 char_u *arg = iptr->isn_arg.string;
2524 int res;
2525 int save_flags = cmdmod.cmod_flags;
2526
Bram Moolenaar35578162021-08-02 19:10:38 +02002527 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002528 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002529 tv = STACK_TV_BOT(0);
2530 init_tv(tv);
2531 cmdmod.cmod_flags |= CMOD_LEGACY;
2532 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2533 cmdmod.cmod_flags = save_flags;
2534 if (res == FAIL)
2535 goto on_error;
2536 ++ectx->ec_stack.ga_len;
2537 }
2538 break;
2539
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002540 // push typeval VAR_INSTR with instructions to be executed
2541 case ISN_INSTR:
2542 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002543 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002544 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002545 tv = STACK_TV_BOT(0);
2546 tv->vval.v_instr = ALLOC_ONE(instr_T);
2547 if (tv->vval.v_instr == NULL)
2548 goto on_error;
2549 ++ectx->ec_stack.ga_len;
2550
2551 tv->v_type = VAR_INSTR;
2552 tv->vval.v_instr->instr_ectx = ectx;
2553 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2554 }
2555 break;
2556
Bram Moolenaar4c137212021-04-19 16:48:48 +02002557 // execute :substitute with an expression
2558 case ISN_SUBSTITUTE:
2559 {
2560 subs_T *subs = &iptr->isn_arg.subs;
2561 source_cookie_T cookie;
2562 struct subs_expr_S *save_instr = substitute_instr;
2563 struct subs_expr_S subs_instr;
2564 int res;
2565
2566 subs_instr.subs_ectx = ectx;
2567 subs_instr.subs_instr = subs->subs_instr;
2568 subs_instr.subs_status = OK;
2569 substitute_instr = &subs_instr;
2570
2571 SOURCING_LNUM = iptr->isn_lnum;
2572 // This is very much like ISN_EXEC
2573 CLEAR_FIELD(cookie);
2574 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2575 res = do_cmdline(subs->subs_cmd,
2576 getsourceline, &cookie,
2577 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2578 substitute_instr = save_instr;
2579
2580 if (res == FAIL || did_emsg
2581 || subs_instr.subs_status == FAIL)
2582 goto on_error;
2583 }
2584 break;
2585
2586 case ISN_FINISH:
2587 goto done;
2588
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002589 case ISN_REDIRSTART:
2590 // create a dummy entry for var_redir_str()
2591 if (alloc_redir_lval() == FAIL)
2592 goto on_error;
2593
2594 // The output is stored in growarray "redir_ga" until
2595 // redirection ends.
2596 init_redir_ga();
2597 redir_vname = 1;
2598 break;
2599
2600 case ISN_REDIREND:
2601 {
2602 char_u *res = get_clear_redir_ga();
2603
2604 // End redirection, put redirected text on the stack.
2605 clear_redir_lval();
2606 redir_vname = 0;
2607
Bram Moolenaar35578162021-08-02 19:10:38 +02002608 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002609 {
2610 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002611 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002612 }
2613 tv = STACK_TV_BOT(0);
2614 tv->v_type = VAR_STRING;
2615 tv->vval.v_string = res;
2616 ++ectx->ec_stack.ga_len;
2617 }
2618 break;
2619
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002620 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002621#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002622 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2623 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002624#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002625 break;
2626
2627 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002628#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002629 {
2630 exarg_T ea;
2631 int res;
2632
2633 CLEAR_FIELD(ea);
2634 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2635 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2636 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2637 --ectx->ec_stack.ga_len;
2638 tv = STACK_TV_BOT(0);
2639 res = cexpr_core(&ea, tv);
2640 clear_tv(tv);
2641 if (res == FAIL)
2642 goto on_error;
2643 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002644#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002645 break;
2646
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002647 // execute Ex command from pieces on the stack
2648 case ISN_EXECCONCAT:
2649 {
2650 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002651 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002652 int pass;
2653 int i;
2654 char_u *cmd = NULL;
2655 char_u *str;
2656
2657 for (pass = 1; pass <= 2; ++pass)
2658 {
2659 for (i = 0; i < count; ++i)
2660 {
2661 tv = STACK_TV_BOT(i - count);
2662 str = tv->vval.v_string;
2663 if (str != NULL && *str != NUL)
2664 {
2665 if (pass == 2)
2666 STRCPY(cmd + len, str);
2667 len += STRLEN(str);
2668 }
2669 if (pass == 2)
2670 clear_tv(tv);
2671 }
2672 if (pass == 1)
2673 {
2674 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002675 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002676 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002677 len = 0;
2678 }
2679 }
2680
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002681 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002682 do_cmdline_cmd(cmd);
2683 vim_free(cmd);
2684 }
2685 break;
2686
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 // execute :echo {string} ...
2688 case ISN_ECHO:
2689 {
2690 int count = iptr->isn_arg.echo.echo_count;
2691 int atstart = TRUE;
2692 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002693 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694
2695 for (idx = 0; idx < count; ++idx)
2696 {
2697 tv = STACK_TV_BOT(idx - count);
2698 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2699 &atstart, &needclr);
2700 clear_tv(tv);
2701 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002702 if (needclr)
2703 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002704 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 }
2706 break;
2707
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002708 // :execute {string} ...
2709 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002710 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002711 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002712 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002713 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002714 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002715 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002716 {
2717 int count = iptr->isn_arg.number;
2718 garray_T ga;
2719 char_u buf[NUMBUFLEN];
2720 char_u *p;
2721 int len;
2722 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002723 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002724
2725 ga_init2(&ga, 1, 80);
2726 for (idx = 0; idx < count; ++idx)
2727 {
2728 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002729 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002730 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002731 if (tv->v_type == VAR_CHANNEL
2732 || tv->v_type == VAR_JOB)
2733 {
2734 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002735 semsg(_(e_using_invalid_value_as_string_str),
2736 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002737 break;
2738 }
2739 else
2740 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002741 }
2742 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002743 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002744
2745 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002746 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002747 failed = TRUE;
2748 else
2749 {
2750 if (ga.ga_len > 0)
2751 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2752 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2753 ga.ga_len += len;
2754 }
2755 clear_tv(tv);
2756 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002757 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002758 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002759 {
2760 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002761 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002762 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002763
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002764 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002765 {
2766 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002767 {
2768 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002769 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002770 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002771 {
2772 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002773 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002774 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002775 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002776 else
2777 {
2778 msg_sb_eol();
2779 if (iptr->isn_type == ISN_ECHOMSG)
2780 {
2781 msg_attr(ga.ga_data, echo_attr);
2782 out_flush();
2783 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002784 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2785 {
2786 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2787 TRUE);
2788 ui_write((char_u *)"\r\n", 2, TRUE);
2789 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002790 else
2791 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002792 SOURCING_LNUM = iptr->isn_lnum;
2793 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002794 }
2795 }
2796 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002797 ga_clear(&ga);
2798 }
2799 break;
2800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 // load local variable or argument
2802 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002803 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002804 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002806 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 break;
2808
2809 // load v: variable
2810 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002811 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002812 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002814 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 break;
2816
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002817 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 case ISN_LOADSCRIPT:
2819 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002820 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 svar_T *sv;
2822
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002823 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002824 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002825 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002826 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002827 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002828 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002830 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 }
2832 break;
2833
2834 // load s: variable in old script
2835 case ISN_LOADS:
2836 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002837 hashtab_T *ht = &SCRIPT_VARS(
2838 iptr->isn_arg.loadstore.ls_sid);
2839 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002841
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 if (di == NULL)
2843 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002844 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002845 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002846 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 }
2848 else
2849 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002850 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002851 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002853 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 }
2855 }
2856 break;
2857
Bram Moolenaard3aac292020-04-19 14:32:17 +02002858 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002860 case ISN_LOADB:
2861 case ISN_LOADW:
2862 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002864 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002865
Bram Moolenaar06b77222022-01-25 15:51:56 +00002866 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002867 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002868 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002869 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 break;
2873
Bram Moolenaar03290b82020-12-19 16:30:44 +01002874 // load autoload variable
2875 case ISN_LOADAUTO:
2876 {
2877 char_u *name = iptr->isn_arg.string;
2878
Bram Moolenaar35578162021-08-02 19:10:38 +02002879 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002880 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002881 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002882 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002883 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002884 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002885 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002886 }
2887 break;
2888
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002889 // load g:/b:/w:/t: namespace
2890 case ISN_LOADGDICT:
2891 case ISN_LOADBDICT:
2892 case ISN_LOADWDICT:
2893 case ISN_LOADTDICT:
2894 {
2895 dict_T *d = NULL;
2896
2897 switch (iptr->isn_type)
2898 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002899 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2900 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2901 case ISN_LOADWDICT: d = curwin->w_vars; break;
2902 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002903 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002904 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002905 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002906 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002907 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002908 tv = STACK_TV_BOT(0);
2909 tv->v_type = VAR_DICT;
2910 tv->v_lock = 0;
2911 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002912 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002913 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002914 }
2915 break;
2916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 // load &option
2918 case ISN_LOADOPT:
2919 {
2920 typval_T optval;
2921 char_u *name = iptr->isn_arg.string;
2922
Bram Moolenaara8c17702020-04-01 21:17:24 +02002923 // This is not expected to fail, name is checked during
2924 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002925 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002926 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002927 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002928 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002930 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 }
2932 break;
2933
2934 // load $ENV
2935 case ISN_LOADENV:
2936 {
2937 typval_T optval;
2938 char_u *name = iptr->isn_arg.string;
2939
Bram Moolenaar35578162021-08-02 19:10:38 +02002940 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002941 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002942 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002943 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002945 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 }
2947 break;
2948
2949 // load @register
2950 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002951 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002952 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 tv = STACK_TV_BOT(0);
2954 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002955 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002956 // This may result in NULL, which should be equivalent to an
2957 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 tv->vval.v_string = get_reg_contents(
2959 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002960 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 break;
2962
2963 // store local variable
2964 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002965 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 tv = STACK_TV_VAR(iptr->isn_arg.number);
2967 clear_tv(tv);
2968 *tv = *STACK_TV_BOT(0);
2969 break;
2970
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002971 // store s: variable in old script
2972 case ISN_STORES:
2973 {
2974 hashtab_T *ht = &SCRIPT_VARS(
2975 iptr->isn_arg.loadstore.ls_sid);
2976 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002977 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002978
Bram Moolenaar4c137212021-04-19 16:48:48 +02002979 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002980 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002981 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002982 else
2983 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002984 SOURCING_LNUM = iptr->isn_lnum;
2985 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002986 {
2987 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002988 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002989 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002990 clear_tv(&di->di_tv);
2991 di->di_tv = *STACK_TV_BOT(0);
2992 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002993 }
2994 break;
2995
2996 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 case ISN_STORESCRIPT:
2998 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002999 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3000 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003002 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003003 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003004 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003005 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003006
3007 // "const" and "final" are checked at compile time, locking
3008 // the value needs to be checked here.
3009 SOURCING_LNUM = iptr->isn_lnum;
3010 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003011 {
3012 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003013 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003014 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 clear_tv(sv->sv_tv);
3017 *sv->sv_tv = *STACK_TV_BOT(0);
3018 }
3019 break;
3020
3021 // store option
3022 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003023 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003025 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3026 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 long n = 0;
3028 char_u *s = NULL;
3029 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003030 char_u numbuf[NUMBUFLEN];
3031 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032
Bram Moolenaar4c137212021-04-19 16:48:48 +02003033 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 tv = STACK_TV_BOT(0);
3035 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003036 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003038 if (s == NULL)
3039 s = (char_u *)"";
3040 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003041 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3042 {
3043 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003044 // If the option can be set to a function reference or
3045 // a lambda and the passed value is a function
3046 // reference, then convert it to the name (string) of
3047 // the function reference.
3048 s = tv2string(tv, &tofree, numbuf, 0);
3049 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003050 {
3051 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003052 goto on_error;
3053 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003056 // must be VAR_NUMBER, CHECKTYPE makes sure
3057 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003058 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003059 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003060 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 if (msg != NULL)
3062 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003063 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003065 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 }
3068 break;
3069
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003070 // store $ENV
3071 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003072 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003073 tv = STACK_TV_BOT(0);
3074 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3075 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003076 break;
3077
3078 // store @r
3079 case ISN_STOREREG:
3080 {
3081 int reg = iptr->isn_arg.number;
3082
Bram Moolenaar4c137212021-04-19 16:48:48 +02003083 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003084 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003085 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003086 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003087 }
3088 break;
3089
3090 // store v: variable
3091 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003092 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003093 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3094 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003095 // should not happen, type is checked when compiling
3096 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003097 break;
3098
Bram Moolenaard3aac292020-04-19 14:32:17 +02003099 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003101 case ISN_STOREB:
3102 case ISN_STOREW:
3103 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003105 dictitem_T *di;
3106 hashtab_T *ht;
3107 char_u *name = iptr->isn_arg.string + 2;
3108
Bram Moolenaard3aac292020-04-19 14:32:17 +02003109 switch (iptr->isn_type)
3110 {
3111 case ISN_STOREG:
3112 ht = get_globvar_ht();
3113 break;
3114 case ISN_STOREB:
3115 ht = &curbuf->b_vars->dv_hashtab;
3116 break;
3117 case ISN_STOREW:
3118 ht = &curwin->w_vars->dv_hashtab;
3119 break;
3120 case ISN_STORET:
3121 ht = &curtab->tp_vars->dv_hashtab;
3122 break;
3123 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003124 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126
Bram Moolenaar4c137212021-04-19 16:48:48 +02003127 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003128 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003130 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 else
3132 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003133 SOURCING_LNUM = iptr->isn_lnum;
3134 if (var_check_permission(di, name) == FAIL)
3135 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 clear_tv(&di->di_tv);
3137 di->di_tv = *STACK_TV_BOT(0);
3138 }
3139 }
3140 break;
3141
Bram Moolenaar03290b82020-12-19 16:30:44 +01003142 // store an autoload variable
3143 case ISN_STOREAUTO:
3144 SOURCING_LNUM = iptr->isn_lnum;
3145 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3146 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003147 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003148 break;
3149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 // store number in local variable
3151 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003152 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 clear_tv(tv);
3154 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003155 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 break;
3157
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003158 // store value in list or dict variable
3159 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003160 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003161 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003162
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003163 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003164 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003165 if (res == NOTDONE)
3166 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003167 }
3168 break;
3169
Bram Moolenaar68452172021-04-12 21:21:02 +02003170 // store value in blob range
3171 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003172 if (execute_storerange(iptr, ectx) == FAIL)
3173 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003174 break;
3175
Bram Moolenaar0186e582021-01-10 18:33:11 +01003176 // load or store variable or argument from outer scope
3177 case ISN_LOADOUTER:
3178 case ISN_STOREOUTER:
3179 {
3180 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003181 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3182 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003183
3184 while (depth > 1 && outer != NULL)
3185 {
3186 outer = outer->out_up;
3187 --depth;
3188 }
3189 if (outer == NULL)
3190 {
3191 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003192 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3193 || ectx->ec_outer_ref == NULL)
3194 // Possibly :def function called from legacy
3195 // context.
3196 emsg(_(e_closure_called_from_invalid_context));
3197 else
3198 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003199 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003200 }
3201 tv = ((typval_T *)outer->out_stack->ga_data)
3202 + outer->out_frame_idx + STACK_FRAME_SIZE
3203 + iptr->isn_arg.outer.outer_idx;
3204 if (iptr->isn_type == ISN_LOADOUTER)
3205 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003206 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003207 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003208 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003209 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003210 }
3211 else
3212 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003213 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003214 clear_tv(tv);
3215 *tv = *STACK_TV_BOT(0);
3216 }
3217 }
3218 break;
3219
Bram Moolenaar752fc692021-01-04 21:57:11 +01003220 // unlet item in list or dict variable
3221 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003222 if (execute_unletindex(iptr, ectx) == FAIL)
3223 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003224 break;
3225
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003226 // unlet range of items in list variable
3227 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003228 if (execute_unletrange(iptr, ectx) == FAIL)
3229 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003230 break;
3231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 // push constant
3233 case ISN_PUSHNR:
3234 case ISN_PUSHBOOL:
3235 case ISN_PUSHSPEC:
3236 case ISN_PUSHF:
3237 case ISN_PUSHS:
3238 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003239 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003240 case ISN_PUSHCHANNEL:
3241 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003242 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003243 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003245 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003246 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 switch (iptr->isn_type)
3248 {
3249 case ISN_PUSHNR:
3250 tv->v_type = VAR_NUMBER;
3251 tv->vval.v_number = iptr->isn_arg.number;
3252 break;
3253 case ISN_PUSHBOOL:
3254 tv->v_type = VAR_BOOL;
3255 tv->vval.v_number = iptr->isn_arg.number;
3256 break;
3257 case ISN_PUSHSPEC:
3258 tv->v_type = VAR_SPECIAL;
3259 tv->vval.v_number = iptr->isn_arg.number;
3260 break;
3261#ifdef FEAT_FLOAT
3262 case ISN_PUSHF:
3263 tv->v_type = VAR_FLOAT;
3264 tv->vval.v_float = iptr->isn_arg.fnumber;
3265 break;
3266#endif
3267 case ISN_PUSHBLOB:
3268 blob_copy(iptr->isn_arg.blob, tv);
3269 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003270 case ISN_PUSHFUNC:
3271 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003272 if (iptr->isn_arg.string == NULL)
3273 tv->vval.v_string = NULL;
3274 else
3275 tv->vval.v_string =
3276 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003277 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003278 case ISN_PUSHCHANNEL:
3279#ifdef FEAT_JOB_CHANNEL
3280 tv->v_type = VAR_CHANNEL;
3281 tv->vval.v_channel = iptr->isn_arg.channel;
3282 if (tv->vval.v_channel != NULL)
3283 ++tv->vval.v_channel->ch_refcount;
3284#endif
3285 break;
3286 case ISN_PUSHJOB:
3287#ifdef FEAT_JOB_CHANNEL
3288 tv->v_type = VAR_JOB;
3289 tv->vval.v_job = iptr->isn_arg.job;
3290 if (tv->vval.v_job != NULL)
3291 ++tv->vval.v_job->jv_refcount;
3292#endif
3293 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 default:
3295 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003296 tv->vval.v_string = vim_strsave(
3297 iptr->isn_arg.string == NULL
3298 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 }
3300 break;
3301
Bram Moolenaar06b77222022-01-25 15:51:56 +00003302 case ISN_AUTOLOAD:
3303 {
3304 char_u *name = iptr->isn_arg.string;
3305
3306 (void)script_autoload(name, FALSE);
3307 if (find_func(name, TRUE))
3308 {
3309 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3310 goto theend;
3311 tv = STACK_TV_BOT(0);
3312 tv->v_lock = 0;
3313 ++ectx->ec_stack.ga_len;
3314 tv->v_type = VAR_FUNC;
3315 tv->vval.v_string = vim_strsave(name);
3316 }
3317 else
3318 {
3319 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3320
3321 if (res == NOTDONE)
3322 goto theend;
3323 if (res == FAIL)
3324 goto on_error;
3325 }
3326 }
3327 break;
3328
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003329 case ISN_UNLET:
3330 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3331 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003332 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003333 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003334 case ISN_UNLETENV:
3335 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3336 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003337
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003338 case ISN_LOCKUNLOCK:
3339 {
3340 typval_T *lval_root_save = lval_root;
3341 int res;
3342
3343 // Stack has the local variable, argument the whole :lock
3344 // or :unlock command, like ISN_EXEC.
3345 --ectx->ec_stack.ga_len;
3346 lval_root = STACK_TV_BOT(0);
3347 res = exec_command(iptr);
3348 clear_tv(lval_root);
3349 lval_root = lval_root_save;
3350 if (res == FAIL)
3351 goto on_error;
3352 }
3353 break;
3354
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003355 case ISN_LOCKCONST:
3356 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3357 break;
3358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 // create a list from items on the stack; uses a single allocation
3360 // for the list header and the items
3361 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003362 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003363 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 break;
3365
3366 // create a dict from items on the stack
3367 case ISN_NEWDICT:
3368 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003369 int count = iptr->isn_arg.number;
3370 dict_T *dict = dict_alloc();
3371 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003372 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003373 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003375 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003376 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 for (idx = 0; idx < count; ++idx)
3378 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003379 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003381 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003382 key = tv->vval.v_string == NULL
3383 ? (char_u *)"" : tv->vval.v_string;
3384 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003385 if (item != NULL)
3386 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003387 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar74409f62022-01-01 15:58:22 +00003388 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003389 dict_unref(dict);
3390 goto on_error;
3391 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003392 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003394 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003395 {
3396 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003397 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3400 item->di_tv.v_lock = 0;
3401 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003402 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003403 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003404 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003405 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 }
3408
3409 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003410 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003411 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003412 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003414 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 tv = STACK_TV_BOT(-1);
3416 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003417 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 tv->vval.v_dict = dict;
3419 ++dict->dv_refcount;
3420 }
3421 break;
3422
3423 // call a :def function
3424 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003425 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003426 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3427 NULL,
3428 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003429 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003430 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 break;
3432
3433 // call a builtin function
3434 case ISN_BCALL:
3435 SOURCING_LNUM = iptr->isn_lnum;
3436 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3437 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003438 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003439 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 break;
3441
3442 // call a funcref or partial
3443 case ISN_PCALL:
3444 {
3445 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3446 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003447 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448
3449 SOURCING_LNUM = iptr->isn_lnum;
3450 if (pfunc->cpf_top)
3451 {
3452 // funcref is above the arguments
3453 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3454 }
3455 else
3456 {
3457 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003458 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003459 partial_tv = *STACK_TV_BOT(0);
3460 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003462 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003463 if (tv == &partial_tv)
3464 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003466 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 }
3468 break;
3469
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003470 case ISN_PCALL_END:
3471 // PCALL finished, arguments have been consumed and replaced by
3472 // the return value. Now clear the funcref from the stack,
3473 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003474 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003475 clear_tv(STACK_TV_BOT(-1));
3476 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3477 break;
3478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 // call a user defined function or funcref/partial
3480 case ISN_UCALL:
3481 {
3482 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3483
3484 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003485 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003486 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003487 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 }
3489 break;
3490
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003491 // return from a :def function call without a value
3492 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003493 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003494 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003495 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003496 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003497 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003498 tv->vval.v_number = 0;
3499 tv->v_lock = 0;
3500 // FALLTHROUGH
3501
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003502 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 case ISN_RETURN:
3504 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003505 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003506 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003507
3508 if (trystack->ga_len > 0)
3509 trycmd = ((trycmd_T *)trystack->ga_data)
3510 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003511 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003512 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003514 // jump to ":finally" or ":endtry"
3515 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003516 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003517 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003518 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 trycmd->tcd_return = TRUE;
3520 }
3521 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003522 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 }
3524 break;
3525
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003526 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 case ISN_FUNCREF:
3528 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003529 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003530 ufunc_T *ufunc;
3531 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003534 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003535 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003536 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003537 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003538 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003539 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003540 if (funcref->fr_func_name == NULL)
3541 {
3542 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3543 + funcref->fr_dfunc_idx;
3544
3545 ufunc = pt_dfunc->df_ufunc;
3546 }
3547 else
3548 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003549 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003550 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003551 if (ufunc == NULL)
3552 {
3553 SOURCING_LNUM = iptr->isn_lnum;
3554 emsg(_(e_function_reference_invalid));
3555 goto theend;
3556 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003557 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003558 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003560 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 tv->vval.v_partial = pt;
3562 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003563 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 }
3565 break;
3566
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003567 // Create a global function from a lambda.
3568 case ISN_NEWFUNC:
3569 {
3570 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3571
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003572 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003573 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003574 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003575 }
3576 break;
3577
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003578 // List functions
3579 case ISN_DEF:
3580 if (iptr->isn_arg.string == NULL)
3581 list_functions(NULL);
3582 else
3583 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003584 exarg_T ea;
3585 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003586
3587 CLEAR_FIELD(ea);
3588 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003589 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3590 define_function(&ea, NULL, &lines_to_free);
3591 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003592 }
3593 break;
3594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 // jump if a condition is met
3596 case ISN_JUMP:
3597 {
3598 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003599 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 int jump = TRUE;
3601
3602 if (when != JUMP_ALWAYS)
3603 {
3604 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003605 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003606 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003607 || when == JUMP_IF_COND_TRUE)
3608 {
3609 SOURCING_LNUM = iptr->isn_lnum;
3610 jump = tv_get_bool_chk(tv, &error);
3611 if (error)
3612 goto on_error;
3613 }
3614 else
3615 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003617 || when == JUMP_AND_KEEP_IF_FALSE
3618 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003620 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 {
3622 // drop the value from the stack
3623 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003624 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 }
3626 }
3627 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003628 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 }
3630 break;
3631
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003632 // Jump if an argument with a default value was already set and not
3633 // v:none.
3634 case ISN_JUMP_IF_ARG_SET:
3635 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3636 if (tv->v_type != VAR_UNKNOWN
3637 && !(tv->v_type == VAR_SPECIAL
3638 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003639 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003640 break;
3641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 // top of a for loop
3643 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003644 if (execute_for(iptr, ectx) == FAIL)
3645 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 break;
3647
3648 // start of ":try" block
3649 case ISN_TRY:
3650 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003651 trycmd_T *trycmd = NULL;
3652
Bram Moolenaar35578162021-08-02 19:10:38 +02003653 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003654 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003655 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3656 + ectx->ec_trystack.ga_len;
3657 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003659 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003660 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3661 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003662 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003663 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003664 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003665 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003666 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003667 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 }
3669 break;
3670
3671 case ISN_PUSHEXC:
3672 if (current_exception == NULL)
3673 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003674 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003676 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003678 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003679 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003681 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003683 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 tv->vval.v_string = vim_strsave(
3685 (char_u *)current_exception->value);
3686 break;
3687
3688 case ISN_CATCH:
3689 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003690 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691
Bram Moolenaar4c137212021-04-19 16:48:48 +02003692 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 if (trystack->ga_len > 0)
3694 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003695 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 + trystack->ga_len - 1;
3697 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003698 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 }
3700 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003701 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 catch_exception(current_exception);
3703 }
3704 break;
3705
Bram Moolenaarc150c092021-02-13 15:02:46 +01003706 case ISN_TRYCONT:
3707 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003708 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003709 trycont_T *trycont = &iptr->isn_arg.trycont;
3710 int i;
3711 trycmd_T *trycmd;
3712 int iidx = trycont->tct_where;
3713
3714 if (trystack->ga_len < trycont->tct_levels)
3715 {
3716 siemsg("TRYCONT: expected %d levels, found %d",
3717 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003718 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003719 }
3720 // Make :endtry jump to any outer try block and the last
3721 // :endtry inside the loop to the loop start.
3722 for (i = trycont->tct_levels; i > 0; --i)
3723 {
3724 trycmd = ((trycmd_T *)trystack->ga_data)
3725 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003726 // Add one to tcd_cont to be able to jump to
3727 // instruction with index zero.
3728 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003729 iidx = trycmd->tcd_finally_idx == 0
3730 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003731 }
3732 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003733 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003734 }
3735 break;
3736
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003737 case ISN_FINALLY:
3738 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003739 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003740 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3741 + trystack->ga_len - 1;
3742
3743 // Reset the index to avoid a return statement jumps here
3744 // again.
3745 trycmd->tcd_finally_idx = 0;
3746 break;
3747 }
3748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 // end of ":try" block
3750 case ISN_ENDTRY:
3751 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003752 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753
3754 if (trystack->ga_len > 0)
3755 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003756 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 --trystack->ga_len;
3759 --trylevel;
3760 trycmd = ((trycmd_T *)trystack->ga_data)
3761 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003762 if (trycmd->tcd_did_throw)
3763 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003764 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 {
3766 // discard the exception
3767 if (caught_stack == current_exception)
3768 caught_stack = caught_stack->caught;
3769 discard_current_exception();
3770 }
3771
3772 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003773 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003774
Bram Moolenaar4c137212021-04-19 16:48:48 +02003775 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003776 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003777 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003778 clear_tv(STACK_TV_BOT(0));
3779 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003780 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003781 // handling :continue: jump to outer try block or
3782 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003783 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 }
3785 }
3786 break;
3787
3788 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003789 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003790 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003791
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003792 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3793 {
3794 // throwing an exception while using "silent!" causes
3795 // the function to abort but not display an error.
3796 tv = STACK_TV_BOT(-1);
3797 clear_tv(tv);
3798 tv->v_type = VAR_NUMBER;
3799 tv->vval.v_number = 0;
3800 goto done;
3801 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003802 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003803 tv = STACK_TV_BOT(0);
3804 if (tv->vval.v_string == NULL
3805 || *skipwhite(tv->vval.v_string) == NUL)
3806 {
3807 vim_free(tv->vval.v_string);
3808 SOURCING_LNUM = iptr->isn_lnum;
3809 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003810 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003811 }
3812
3813 // Inside a "catch" we need to first discard the caught
3814 // exception.
3815 if (trystack->ga_len > 0)
3816 {
3817 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3818 + trystack->ga_len - 1;
3819 if (trycmd->tcd_caught && current_exception != NULL)
3820 {
3821 // discard the exception
3822 if (caught_stack == current_exception)
3823 caught_stack = caught_stack->caught;
3824 discard_current_exception();
3825 trycmd->tcd_caught = FALSE;
3826 }
3827 }
3828
Bram Moolenaar90a57162022-02-12 14:23:17 +00003829 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003830 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3831 == FAIL)
3832 {
3833 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003834 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003835 }
3836 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 break;
3839
3840 // compare with special values
3841 case ISN_COMPAREBOOL:
3842 case ISN_COMPARESPECIAL:
3843 {
3844 typval_T *tv1 = STACK_TV_BOT(-2);
3845 typval_T *tv2 = STACK_TV_BOT(-1);
3846 varnumber_T arg1 = tv1->vval.v_number;
3847 varnumber_T arg2 = tv2->vval.v_number;
3848 int res;
3849
3850 switch (iptr->isn_arg.op.op_type)
3851 {
3852 case EXPR_EQUAL: res = arg1 == arg2; break;
3853 case EXPR_NEQUAL: res = arg1 != arg2; break;
3854 default: res = 0; break;
3855 }
3856
Bram Moolenaar4c137212021-04-19 16:48:48 +02003857 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 tv1->v_type = VAR_BOOL;
3859 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3860 }
3861 break;
3862
3863 // Operation with two number arguments
3864 case ISN_OPNR:
3865 case ISN_COMPARENR:
3866 {
3867 typval_T *tv1 = STACK_TV_BOT(-2);
3868 typval_T *tv2 = STACK_TV_BOT(-1);
3869 varnumber_T arg1 = tv1->vval.v_number;
3870 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003871 varnumber_T res = 0;
3872 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873
3874 switch (iptr->isn_arg.op.op_type)
3875 {
3876 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003877 case EXPR_DIV: if (arg2 == 0)
3878 div_zero = TRUE;
3879 else
3880 res = arg1 / arg2;
3881 break;
3882 case EXPR_REM: if (arg2 == 0)
3883 div_zero = TRUE;
3884 else
3885 res = arg1 % arg2;
3886 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 case EXPR_SUB: res = arg1 - arg2; break;
3888 case EXPR_ADD: res = arg1 + arg2; break;
3889
3890 case EXPR_EQUAL: res = arg1 == arg2; break;
3891 case EXPR_NEQUAL: res = arg1 != arg2; break;
3892 case EXPR_GREATER: res = arg1 > arg2; break;
3893 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3894 case EXPR_SMALLER: res = arg1 < arg2; break;
3895 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003896 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 }
3898
Bram Moolenaar4c137212021-04-19 16:48:48 +02003899 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 if (iptr->isn_type == ISN_COMPARENR)
3901 {
3902 tv1->v_type = VAR_BOOL;
3903 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3904 }
3905 else
3906 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003907 if (div_zero)
3908 {
3909 SOURCING_LNUM = iptr->isn_lnum;
3910 emsg(_(e_divide_by_zero));
3911 goto on_error;
3912 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 }
3914 break;
3915
3916 // Computation with two float arguments
3917 case ISN_OPFLOAT:
3918 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003919#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 {
3921 typval_T *tv1 = STACK_TV_BOT(-2);
3922 typval_T *tv2 = STACK_TV_BOT(-1);
3923 float_T arg1 = tv1->vval.v_float;
3924 float_T arg2 = tv2->vval.v_float;
3925 float_T res = 0;
3926 int cmp = FALSE;
3927
3928 switch (iptr->isn_arg.op.op_type)
3929 {
3930 case EXPR_MULT: res = arg1 * arg2; break;
3931 case EXPR_DIV: res = arg1 / arg2; break;
3932 case EXPR_SUB: res = arg1 - arg2; break;
3933 case EXPR_ADD: res = arg1 + arg2; break;
3934
3935 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3936 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3937 case EXPR_GREATER: cmp = arg1 > arg2; break;
3938 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3939 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3940 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3941 default: cmp = 0; break;
3942 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003943 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 if (iptr->isn_type == ISN_COMPAREFLOAT)
3945 {
3946 tv1->v_type = VAR_BOOL;
3947 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3948 }
3949 else
3950 tv1->vval.v_float = res;
3951 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003952#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 break;
3954
3955 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00003956 case ISN_COMPAREDICT:
3957 case ISN_COMPAREFUNC:
3958 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 case ISN_COMPAREBLOB:
3960 {
3961 typval_T *tv1 = STACK_TV_BOT(-2);
3962 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00003963 exprtype_T exprtype = iptr->isn_arg.op.op_type;
3964 int ic = iptr->isn_arg.op.op_ic;
3965 int res = FALSE;
3966 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967
Bram Moolenaar265f8112021-12-19 12:33:05 +00003968 SOURCING_LNUM = iptr->isn_lnum;
3969 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00003971 status = typval_compare_list(tv1, tv2,
3972 exprtype, ic, &res);
3973 }
3974 else if (iptr->isn_type == ISN_COMPAREDICT)
3975 {
3976 status = typval_compare_dict(tv1, tv2,
3977 exprtype, ic, &res);
3978 }
3979 else if (iptr->isn_type == ISN_COMPAREFUNC)
3980 {
3981 status = typval_compare_func(tv1, tv2,
3982 exprtype, ic, &res);
3983 }
3984 else if (iptr->isn_type == ISN_COMPARESTRING)
3985 {
3986 status = typval_compare_string(tv1, tv2,
3987 exprtype, ic, &res);
3988 }
3989 else
3990 {
3991 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003993 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 clear_tv(tv1);
3995 clear_tv(tv2);
3996 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003997 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3998 if (status == FAIL)
3999 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 }
4001 break;
4002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 case ISN_COMPAREANY:
4004 {
4005 typval_T *tv1 = STACK_TV_BOT(-2);
4006 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004007 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004009 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010
Bram Moolenaareb26f432020-09-14 16:50:05 +02004011 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004012 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004014 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004015 if (status == FAIL)
4016 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 }
4018 break;
4019
4020 case ISN_ADDLIST:
4021 case ISN_ADDBLOB:
4022 {
4023 typval_T *tv1 = STACK_TV_BOT(-2);
4024 typval_T *tv2 = STACK_TV_BOT(-1);
4025
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004026 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004028 {
4029 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4030 && tv1->vval.v_list != NULL)
4031 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4032 NULL);
4033 else
4034 eval_addlist(tv1, tv2);
4035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 else
4037 eval_addblob(tv1, tv2);
4038 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004039 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 }
4041 break;
4042
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004043 case ISN_LISTAPPEND:
4044 {
4045 typval_T *tv1 = STACK_TV_BOT(-2);
4046 typval_T *tv2 = STACK_TV_BOT(-1);
4047 list_T *l = tv1->vval.v_list;
4048
4049 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004050 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004051 if (l == NULL)
4052 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004053 emsg(_(e_cannot_add_to_null_list));
4054 goto on_error;
4055 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004056 if (value_check_lock(l->lv_lock, NULL, FALSE))
4057 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004058 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004059 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004060 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004061 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004062 }
4063 break;
4064
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004065 case ISN_BLOBAPPEND:
4066 {
4067 typval_T *tv1 = STACK_TV_BOT(-2);
4068 typval_T *tv2 = STACK_TV_BOT(-1);
4069 blob_T *b = tv1->vval.v_blob;
4070 int error = FALSE;
4071 varnumber_T n;
4072
4073 // add a number to a blob
4074 if (b == NULL)
4075 {
4076 SOURCING_LNUM = iptr->isn_lnum;
4077 emsg(_(e_cannot_add_to_null_blob));
4078 goto on_error;
4079 }
4080 n = tv_get_number_chk(tv2, &error);
4081 if (error)
4082 goto on_error;
4083 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004084 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004085 }
4086 break;
4087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 // Computation with two arguments of unknown type
4089 case ISN_OPANY:
4090 {
4091 typval_T *tv1 = STACK_TV_BOT(-2);
4092 typval_T *tv2 = STACK_TV_BOT(-1);
4093 varnumber_T n1, n2;
4094#ifdef FEAT_FLOAT
4095 float_T f1 = 0, f2 = 0;
4096#endif
4097 int error = FALSE;
4098
4099 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4100 {
4101 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4102 {
4103 eval_addlist(tv1, tv2);
4104 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004105 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 break;
4107 }
4108 else if (tv1->v_type == VAR_BLOB
4109 && tv2->v_type == VAR_BLOB)
4110 {
4111 eval_addblob(tv1, tv2);
4112 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004113 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 break;
4115 }
4116 }
4117#ifdef FEAT_FLOAT
4118 if (tv1->v_type == VAR_FLOAT)
4119 {
4120 f1 = tv1->vval.v_float;
4121 n1 = 0;
4122 }
4123 else
4124#endif
4125 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004126 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 n1 = tv_get_number_chk(tv1, &error);
4128 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004129 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130#ifdef FEAT_FLOAT
4131 if (tv2->v_type == VAR_FLOAT)
4132 f1 = n1;
4133#endif
4134 }
4135#ifdef FEAT_FLOAT
4136 if (tv2->v_type == VAR_FLOAT)
4137 {
4138 f2 = tv2->vval.v_float;
4139 n2 = 0;
4140 }
4141 else
4142#endif
4143 {
4144 n2 = tv_get_number_chk(tv2, &error);
4145 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004146 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147#ifdef FEAT_FLOAT
4148 if (tv1->v_type == VAR_FLOAT)
4149 f2 = n2;
4150#endif
4151 }
4152#ifdef FEAT_FLOAT
4153 // if there is a float on either side the result is a float
4154 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4155 {
4156 switch (iptr->isn_arg.op.op_type)
4157 {
4158 case EXPR_MULT: f1 = f1 * f2; break;
4159 case EXPR_DIV: f1 = f1 / f2; break;
4160 case EXPR_SUB: f1 = f1 - f2; break;
4161 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004162 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004163 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004164 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 }
4166 clear_tv(tv1);
4167 clear_tv(tv2);
4168 tv1->v_type = VAR_FLOAT;
4169 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004170 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 }
4172 else
4173#endif
4174 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004175 int failed = FALSE;
4176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 switch (iptr->isn_arg.op.op_type)
4178 {
4179 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004180 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4181 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004182 goto on_error;
4183 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 case EXPR_SUB: n1 = n1 - n2; break;
4185 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004186 default: n1 = num_modulus(n1, n2, &failed);
4187 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004188 goto on_error;
4189 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 }
4191 clear_tv(tv1);
4192 clear_tv(tv2);
4193 tv1->v_type = VAR_NUMBER;
4194 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004195 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 }
4197 }
4198 break;
4199
4200 case ISN_CONCAT:
4201 {
4202 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4203 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4204 char_u *res;
4205
4206 res = concat_str(str1, str2);
4207 clear_tv(STACK_TV_BOT(-2));
4208 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004209 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 STACK_TV_BOT(-1)->vval.v_string = res;
4211 }
4212 break;
4213
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004214 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004215 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004216 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004217 int is_slice = iptr->isn_type == ISN_STRSLICE;
4218 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004219 char_u *res;
4220
4221 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004222 // string slice: string is at stack-3, first index at
4223 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004224 if (is_slice)
4225 {
4226 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004227 n1 = tv->vval.v_number;
4228 }
4229
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004230 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004231 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004232
Bram Moolenaar4c137212021-04-19 16:48:48 +02004233 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004234 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004235 if (is_slice)
4236 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004237 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004238 else
4239 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004240 // single character (including composing characters).
4241 // If the index is too big or negative the result is
4242 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004243 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004244 vim_free(tv->vval.v_string);
4245 tv->vval.v_string = res;
4246 }
4247 break;
4248
4249 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004250 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004251 case ISN_BLOBINDEX:
4252 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004254 int is_slice = iptr->isn_type == ISN_LISTSLICE
4255 || iptr->isn_type == ISN_BLOBSLICE;
4256 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4257 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004258 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004259 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260
4261 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004262 // list slice: list is at stack-3, indexes at stack-2 and
4263 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004264 // Same for blob.
4265 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266
4267 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004268 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004270
4271 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 {
Bram Moolenaared591872020-08-15 22:14:53 +02004273 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004274 n1 = tv->vval.v_number;
4275 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 }
Bram Moolenaared591872020-08-15 22:14:53 +02004277
Bram Moolenaar4c137212021-04-19 16:48:48 +02004278 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004279 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004280 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004281 if (is_blob)
4282 {
4283 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4284 n1, n2, FALSE, tv) == FAIL)
4285 goto on_error;
4286 }
4287 else
4288 {
4289 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4290 n1, n2, FALSE, tv, TRUE) == FAIL)
4291 goto on_error;
4292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 }
4294 break;
4295
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004296 case ISN_ANYINDEX:
4297 case ISN_ANYSLICE:
4298 {
4299 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4300 typval_T *var1, *var2;
4301 int res;
4302
4303 // index: composite is at stack-2, index at stack-1
4304 // slice: composite is at stack-3, indexes at stack-2 and
4305 // stack-1
4306 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004307 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004308 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4309 goto on_error;
4310 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4311 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004312 res = eval_index_inner(tv, is_slice, var1, var2,
4313 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004314 clear_tv(var1);
4315 if (is_slice)
4316 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004317 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004318 if (res == FAIL)
4319 goto on_error;
4320 }
4321 break;
4322
Bram Moolenaar9af78762020-06-16 11:34:42 +02004323 case ISN_SLICE:
4324 {
4325 list_T *list;
4326 int count = iptr->isn_arg.number;
4327
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004328 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004329 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004330 list = tv->vval.v_list;
4331
4332 // no error for short list, expect it to be checked earlier
4333 if (list != NULL && list->lv_len >= count)
4334 {
4335 list_T *newlist = list_slice(list,
4336 count, list->lv_len - 1);
4337
4338 if (newlist != NULL)
4339 {
4340 list_unref(list);
4341 tv->vval.v_list = newlist;
4342 ++newlist->lv_refcount;
4343 }
4344 }
4345 }
4346 break;
4347
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004348 case ISN_GETITEM:
4349 {
4350 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004351 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004352
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004353 // Get list item: list is at stack-1, push item.
4354 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004355 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4356 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004357
Bram Moolenaar35578162021-08-02 19:10:38 +02004358 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004359 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004360 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004361 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004362
4363 // Useful when used in unpack assignment. Reset at
4364 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004365 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004366 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004367 }
4368 break;
4369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 case ISN_MEMBER:
4371 {
4372 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004373 char_u *key;
4374 dictitem_T *di;
4375
4376 // dict member: dict is at stack-2, key at stack-1
4377 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004378 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004379 dict = tv->vval.v_dict;
4380
4381 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004382 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004383 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004384 if (key == NULL)
4385 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004386
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004387 if ((di = dict_find(dict, key, -1)) == NULL)
4388 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004389 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004390 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004391
4392 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004393 // stack contents makes sense and the dict stack is
4394 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004395 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004396 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004397 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004398 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004399 tv->v_type = VAR_NUMBER;
4400 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004401 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004402 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004403 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004404 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004405 // Put the dict used on the dict stack, it might be used by
4406 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004407 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004408 if (dict_stack_save(tv) == FAIL)
4409 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004410 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004411 }
4412 break;
4413
4414 // dict member with string key
4415 case ISN_STRINGMEMBER:
4416 {
4417 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 dictitem_T *di;
4419
4420 tv = STACK_TV_BOT(-1);
4421 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4422 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004423 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004424 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004425 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 }
4427 dict = tv->vval.v_dict;
4428
4429 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4430 == NULL)
4431 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004432 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004433 semsg(_(e_key_not_present_in_dictionary),
4434 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004435 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004437 // Put the dict used on the dict stack, it might be used by
4438 // a dict function later.
4439 if (dict_stack_save(tv) == FAIL)
4440 goto on_fatal_error;
4441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004443 }
4444 break;
4445
4446 case ISN_CLEARDICT:
4447 dict_stack_drop();
4448 break;
4449
4450 case ISN_USEDICT:
4451 {
4452 typval_T *dict_tv = dict_stack_get_tv();
4453
4454 // Turn "dict.Func" into a partial for "Func" bound to
4455 // "dict". Don't do this when "Func" is already a partial
4456 // that was bound explicitly (pt_auto is FALSE).
4457 tv = STACK_TV_BOT(-1);
4458 if (dict_tv != NULL
4459 && dict_tv->v_type == VAR_DICT
4460 && dict_tv->vval.v_dict != NULL
4461 && (tv->v_type == VAR_FUNC
4462 || (tv->v_type == VAR_PARTIAL
4463 && (tv->vval.v_partial->pt_auto
4464 || tv->vval.v_partial->pt_dict == NULL))))
4465 dict_tv->vval.v_dict =
4466 make_partial(dict_tv->vval.v_dict, tv);
4467 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 }
4469 break;
4470
4471 case ISN_NEGATENR:
4472 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004473 if (tv->v_type != VAR_NUMBER
4474#ifdef FEAT_FLOAT
4475 && tv->v_type != VAR_FLOAT
4476#endif
4477 )
4478 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004479 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004480 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004481 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004482 }
4483#ifdef FEAT_FLOAT
4484 if (tv->v_type == VAR_FLOAT)
4485 tv->vval.v_float = -tv->vval.v_float;
4486 else
4487#endif
4488 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 break;
4490
4491 case ISN_CHECKNR:
4492 {
4493 int error = FALSE;
4494
4495 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004496 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004498 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 (void)tv_get_number_chk(tv, &error);
4500 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004501 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 }
4503 break;
4504
4505 case ISN_CHECKTYPE:
4506 {
4507 checktype_T *ct = &iptr->isn_arg.type;
4508
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004509 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004510 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004511 if (!ectx->ec_where.wt_variable)
4512 ectx->ec_where.wt_index = ct->ct_arg_idx;
4513 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4514 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004515 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004516 if (!ectx->ec_where.wt_variable)
4517 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004518
4519 // number 0 is FALSE, number 1 is TRUE
4520 if (tv->v_type == VAR_NUMBER
4521 && ct->ct_type->tt_type == VAR_BOOL
4522 && (tv->vval.v_number == 0
4523 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004525 tv->v_type = VAR_BOOL;
4526 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004527 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 }
4529 }
4530 break;
4531
Bram Moolenaar9af78762020-06-16 11:34:42 +02004532 case ISN_CHECKLEN:
4533 {
4534 int min_len = iptr->isn_arg.checklen.cl_min_len;
4535 list_T *list = NULL;
4536
4537 tv = STACK_TV_BOT(-1);
4538 if (tv->v_type == VAR_LIST)
4539 list = tv->vval.v_list;
4540 if (list == NULL || list->lv_len < min_len
4541 || (list->lv_len > min_len
4542 && !iptr->isn_arg.checklen.cl_more_OK))
4543 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004544 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004545 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004546 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004547 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004548 }
4549 }
4550 break;
4551
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004552 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004553 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004554 break;
4555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004557 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 {
4559 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004560 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004562 if (iptr->isn_type == ISN_2BOOL)
4563 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004564 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004565 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004566 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004567 n = !n;
4568 }
4569 else
4570 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004571 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004572 SOURCING_LNUM = iptr->isn_lnum;
4573 n = tv_get_bool_chk(tv, &error);
4574 if (error)
4575 goto on_error;
4576 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 clear_tv(tv);
4578 tv->v_type = VAR_BOOL;
4579 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4580 }
4581 break;
4582
4583 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004584 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004585 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004586 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4587 iptr->isn_type == ISN_2STRING_ANY,
4588 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004589 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 break;
4591
Bram Moolenaar08597872020-12-10 19:43:40 +01004592 case ISN_RANGE:
4593 {
4594 exarg_T ea;
4595 char *errormsg;
4596
Bram Moolenaarece0b872021-01-08 20:40:45 +01004597 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004598 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004599 ea.addr_type = ADDR_LINES;
4600 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004601 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004602 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004603 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004604
Bram Moolenaar35578162021-08-02 19:10:38 +02004605 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004606 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004607 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004608 tv = STACK_TV_BOT(-1);
4609 tv->v_type = VAR_NUMBER;
4610 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004611 if (ea.addr_count == 0)
4612 tv->vval.v_number = curwin->w_cursor.lnum;
4613 else
4614 tv->vval.v_number = ea.line2;
4615 }
4616 break;
4617
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004618 case ISN_PUT:
4619 {
4620 int regname = iptr->isn_arg.put.put_regname;
4621 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4622 char_u *expr = NULL;
4623 int dir = FORWARD;
4624
Bram Moolenaar08597872020-12-10 19:43:40 +01004625 if (lnum < -2)
4626 {
4627 // line number was put on the stack by ISN_RANGE
4628 tv = STACK_TV_BOT(-1);
4629 curwin->w_cursor.lnum = tv->vval.v_number;
4630 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4631 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004632 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004633 }
4634 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004635 // :put! above cursor
4636 dir = BACKWARD;
4637 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004638 {
4639 curwin->w_cursor.lnum = lnum;
4640 if (lnum == 0)
4641 // check_cursor() below will move to line 1
4642 dir = BACKWARD;
4643 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004644
4645 if (regname == '=')
4646 {
4647 tv = STACK_TV_BOT(-1);
4648 if (tv->v_type == VAR_STRING)
4649 expr = tv->vval.v_string;
4650 else
4651 {
4652 expr = typval2string(tv, TRUE); // allocates value
4653 clear_tv(tv);
4654 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004655 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004656 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004657 check_cursor();
4658 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4659 vim_free(expr);
4660 }
4661 break;
4662
Bram Moolenaar02194d22020-10-24 23:08:38 +02004663 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004664 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4665 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4666 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4667 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004668 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4669 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004670 break;
4671
Bram Moolenaar02194d22020-10-24 23:08:38 +02004672 case ISN_CMDMOD_REV:
4673 // filter regprog is owned by the instruction, don't free it
4674 cmdmod.cmod_filter_regmatch.regprog = NULL;
4675 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004676 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4677 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004678 break;
4679
Bram Moolenaar792f7862020-11-23 08:31:18 +01004680 case ISN_UNPACK:
4681 {
4682 int count = iptr->isn_arg.unpack.unp_count;
4683 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4684 list_T *l;
4685 listitem_T *li;
4686 int i;
4687
4688 // Check there is a valid list to unpack.
4689 tv = STACK_TV_BOT(-1);
4690 if (tv->v_type != VAR_LIST)
4691 {
4692 SOURCING_LNUM = iptr->isn_lnum;
4693 emsg(_(e_for_argument_must_be_sequence_of_lists));
4694 goto on_error;
4695 }
4696 l = tv->vval.v_list;
4697 if (l == NULL
4698 || l->lv_len < (semicolon ? count - 1 : count))
4699 {
4700 SOURCING_LNUM = iptr->isn_lnum;
4701 emsg(_(e_list_value_does_not_have_enough_items));
4702 goto on_error;
4703 }
4704 else if (!semicolon && l->lv_len > count)
4705 {
4706 SOURCING_LNUM = iptr->isn_lnum;
4707 emsg(_(e_list_value_has_more_items_than_targets));
4708 goto on_error;
4709 }
4710
4711 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004712 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004713 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004714 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004715
4716 // Variable after semicolon gets a list with the remaining
4717 // items.
4718 if (semicolon)
4719 {
4720 list_T *rem_list =
4721 list_alloc_with_items(l->lv_len - count + 1);
4722
4723 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004724 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004725 tv = STACK_TV_BOT(-count);
4726 tv->vval.v_list = rem_list;
4727 ++rem_list->lv_refcount;
4728 tv->v_lock = 0;
4729 li = l->lv_first;
4730 for (i = 0; i < count - 1; ++i)
4731 li = li->li_next;
4732 for (i = 0; li != NULL; ++i)
4733 {
4734 list_set_item(rem_list, i, &li->li_tv);
4735 li = li->li_next;
4736 }
4737 --count;
4738 }
4739
4740 // Produce the values in reverse order, first item last.
4741 li = l->lv_first;
4742 for (i = 0; i < count; ++i)
4743 {
4744 tv = STACK_TV_BOT(-i - 1);
4745 copy_tv(&li->li_tv, tv);
4746 li = li->li_next;
4747 }
4748
4749 list_unref(l);
4750 }
4751 break;
4752
Bram Moolenaarb2049902021-01-24 12:53:53 +01004753 case ISN_PROF_START:
4754 case ISN_PROF_END:
4755 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004756#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004757 funccall_T cookie;
4758 ufunc_T *cur_ufunc =
4759 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004760 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004761
4762 cookie.func = cur_ufunc;
4763 if (iptr->isn_type == ISN_PROF_START)
4764 {
4765 func_line_start(&cookie, iptr->isn_lnum);
4766 // if we get here the instruction is executed
4767 func_line_exec(&cookie);
4768 }
4769 else
4770 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004771#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004772 }
4773 break;
4774
Bram Moolenaare99d4222021-06-13 14:01:26 +02004775 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004776 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004777 break;
4778
Bram Moolenaar389df252020-07-09 21:20:47 +02004779 case ISN_SHUFFLE:
4780 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004781 typval_T tmp_tv;
4782 int item = iptr->isn_arg.shuffle.shfl_item;
4783 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004784
4785 tmp_tv = *STACK_TV_BOT(-item);
4786 for ( ; up > 0 && item > 1; --up)
4787 {
4788 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4789 --item;
4790 }
4791 *STACK_TV_BOT(-item) = tmp_tv;
4792 }
4793 break;
4794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004796 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004798 ectx->ec_where.wt_index = 0;
4799 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 break;
4801 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004802 continue;
4803
Bram Moolenaard032f342020-07-18 18:13:02 +02004804func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004805 // Restore previous function. If the frame pointer is where we started
4806 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004807 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004808 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004809
Bram Moolenaar4c137212021-04-19 16:48:48 +02004810 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004811 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004812 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004813 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004814
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004815on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004816 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004817 // If "emsg_silent" is set then ignore the error, unless it was set
4818 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004819 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004820 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004821 {
4822 // If a sequence of instructions causes an error while ":silent!"
4823 // was used, restore the stack length and jump ahead to restoring
4824 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004825 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004826 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004827 while (ectx->ec_stack.ga_len
4828 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004829 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004830 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004831 clear_tv(STACK_TV_BOT(0));
4832 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004833 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4834 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004835 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004836 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004837 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004838on_fatal_error:
4839 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004840 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004841 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004842 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 }
4844
4845done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004846 ret = OK;
4847theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004848 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004849 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4850 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004851}
4852
4853/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004854 * Execute the instructions from a VAR_INSTR typeval and put the result in
4855 * "rettv".
4856 * Return OK or FAIL.
4857 */
4858 int
4859exe_typval_instr(typval_T *tv, typval_T *rettv)
4860{
4861 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4862 isn_T *save_instr = ectx->ec_instr;
4863 int save_iidx = ectx->ec_iidx;
4864 int res;
4865
4866 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4867 res = exec_instructions(ectx);
4868 if (res == OK)
4869 {
4870 *rettv = *STACK_TV_BOT(-1);
4871 --ectx->ec_stack.ga_len;
4872 }
4873
4874 ectx->ec_instr = save_instr;
4875 ectx->ec_iidx = save_iidx;
4876
4877 return res;
4878}
4879
4880/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004881 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4882 * "substitute_instr".
4883 */
4884 char_u *
4885exe_substitute_instr(void)
4886{
4887 ectx_T *ectx = substitute_instr->subs_ectx;
4888 isn_T *save_instr = ectx->ec_instr;
4889 int save_iidx = ectx->ec_iidx;
4890 char_u *res;
4891
4892 ectx->ec_instr = substitute_instr->subs_instr;
4893 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004894 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004895 typval_T *tv = STACK_TV_BOT(-1);
4896
Bram Moolenaar27523602021-06-05 21:36:19 +02004897 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004898 --ectx->ec_stack.ga_len;
4899 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004900 }
4901 else
4902 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004903 substitute_instr->subs_status = FAIL;
4904 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906
Bram Moolenaar4c137212021-04-19 16:48:48 +02004907 ectx->ec_instr = save_instr;
4908 ectx->ec_iidx = save_iidx;
4909
4910 return res;
4911}
4912
4913/*
4914 * Call a "def" function from old Vim script.
4915 * Return OK or FAIL.
4916 */
4917 int
4918call_def_function(
4919 ufunc_T *ufunc,
4920 int argc_arg, // nr of arguments
4921 typval_T *argv, // arguments
4922 partial_T *partial, // optional partial for context
4923 typval_T *rettv) // return value
4924{
4925 ectx_T ectx; // execution context
4926 int argc = argc_arg;
4927 typval_T *tv;
4928 int idx;
4929 int ret = FAIL;
4930 int defcount = ufunc->uf_args.ga_len - argc;
4931 sctx_T save_current_sctx = current_sctx;
4932 int did_emsg_before = did_emsg_cumul + did_emsg;
4933 int save_suppress_errthrow = suppress_errthrow;
4934 msglist_T **saved_msg_list = NULL;
4935 msglist_T *private_msg_list = NULL;
4936 int save_emsg_silent_def = emsg_silent_def;
4937 int save_did_emsg_def = did_emsg_def;
4938 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004939 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004940
4941// Get pointer to item in the stack.
4942#undef STACK_TV
4943#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4944
4945// Get pointer to item at the bottom of the stack, -1 is the bottom.
4946#undef STACK_TV_BOT
4947#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4948
4949// Get pointer to a local variable on the stack. Negative for arguments.
4950#undef STACK_TV_VAR
4951#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4952
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004953 // Update uf_has_breakpoint if needed.
4954 update_has_breakpoint(ufunc);
4955
Bram Moolenaar4c137212021-04-19 16:48:48 +02004956 if (ufunc->uf_def_status == UF_NOT_COMPILED
4957 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004958 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4959 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004960 == FAIL))
4961 {
4962 if (did_emsg_cumul + did_emsg == did_emsg_before)
4963 semsg(_(e_function_is_not_compiled_str),
4964 printable_func_name(ufunc));
4965 return FAIL;
4966 }
4967
4968 {
4969 // Check the function was really compiled.
4970 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4971 + ufunc->uf_dfunc_idx;
4972 if (INSTRUCTIONS(dfunc) == NULL)
4973 {
4974 iemsg("using call_def_function() on not compiled function");
4975 return FAIL;
4976 }
4977 }
4978
4979 // If depth of calling is getting too high, don't execute the function.
4980 orig_funcdepth = funcdepth_get();
4981 if (funcdepth_increment() == FAIL)
4982 return FAIL;
4983
4984 CLEAR_FIELD(ectx);
4985 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4986 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004987 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004988 {
4989 funcdepth_decrement();
4990 return FAIL;
4991 }
4992 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4993 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4994 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004995 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004996
4997 idx = argc - ufunc->uf_args.ga_len;
4998 if (idx > 0 && ufunc->uf_va_name == NULL)
4999 {
5000 if (idx == 1)
5001 emsg(_(e_one_argument_too_many));
5002 else
5003 semsg(_(e_nr_arguments_too_many), idx);
5004 goto failed_early;
5005 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005006 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5007 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005008 {
5009 if (idx == -1)
5010 emsg(_(e_one_argument_too_few));
5011 else
5012 semsg(_(e_nr_arguments_too_few), -idx);
5013 goto failed_early;
5014 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005015
5016 // Put arguments on the stack, but no more than what the function expects.
5017 // A lambda can be called with more arguments than it uses.
5018 for (idx = 0; idx < argc
5019 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5020 ++idx)
5021 {
5022 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5023 && argv[idx].v_type == VAR_SPECIAL
5024 && argv[idx].vval.v_number == VVAL_NONE)
5025 {
5026 // Use the default value.
5027 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5028 }
5029 else
5030 {
5031 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5032 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005033 ufunc->uf_arg_types[idx], &argv[idx],
5034 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005035 goto failed_early;
5036 copy_tv(&argv[idx], STACK_TV_BOT(0));
5037 }
5038 ++ectx.ec_stack.ga_len;
5039 }
5040
5041 // Turn varargs into a list. Empty list if no args.
5042 if (ufunc->uf_va_name != NULL)
5043 {
5044 int vararg_count = argc - ufunc->uf_args.ga_len;
5045
5046 if (vararg_count < 0)
5047 vararg_count = 0;
5048 else
5049 argc -= vararg_count;
5050 if (exe_newlist(vararg_count, &ectx) == FAIL)
5051 goto failed_early;
5052
5053 // Check the type of the list items.
5054 tv = STACK_TV_BOT(-1);
5055 if (ufunc->uf_va_type != NULL
5056 && ufunc->uf_va_type != &t_list_any
5057 && ufunc->uf_va_type->tt_member != &t_any
5058 && tv->vval.v_list != NULL)
5059 {
5060 type_T *expected = ufunc->uf_va_type->tt_member;
5061 listitem_T *li = tv->vval.v_list->lv_first;
5062
5063 for (idx = 0; idx < vararg_count; ++idx)
5064 {
5065 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005066 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005067 goto failed_early;
5068 li = li->li_next;
5069 }
5070 }
5071
5072 if (defcount > 0)
5073 // Move varargs list to below missing default arguments.
5074 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5075 --ectx.ec_stack.ga_len;
5076 }
5077
5078 // Make space for omitted arguments, will store default value below.
5079 // Any varargs list goes after them.
5080 if (defcount > 0)
5081 for (idx = 0; idx < defcount; ++idx)
5082 {
5083 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5084 ++ectx.ec_stack.ga_len;
5085 }
5086 if (ufunc->uf_va_name != NULL)
5087 ++ectx.ec_stack.ga_len;
5088
5089 // Frame pointer points to just after arguments.
5090 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5091 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5092
5093 {
5094 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5095 + ufunc->uf_dfunc_idx;
5096 ufunc_T *base_ufunc = dfunc->df_ufunc;
5097
5098 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5099 // by copy_func().
5100 if (partial != NULL || base_ufunc->uf_partial != NULL)
5101 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005102 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5103 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005104 goto failed_early;
5105 if (partial != NULL)
5106 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005107 outer_T *outer = get_pt_outer(partial);
5108
5109 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005110 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005111 if (current_ectx != NULL)
5112 {
5113 if (current_ectx->ec_outer_ref != NULL
5114 && current_ectx->ec_outer_ref->or_outer != NULL)
5115 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005116 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005117 }
5118 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005119 }
5120 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005121 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005122 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005123 ++partial->pt_refcount;
5124 ectx.ec_outer_ref->or_partial = partial;
5125 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005126 }
5127 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005128 {
5129 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5130 ++base_ufunc->uf_partial->pt_refcount;
5131 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5132 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005133 }
5134 }
5135
5136 // dummy frame entries
5137 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5138 {
5139 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5140 ++ectx.ec_stack.ga_len;
5141 }
5142
5143 {
5144 // Reserve space for local variables and any closure reference count.
5145 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5146 + ufunc->uf_dfunc_idx;
5147
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005148 // Initialize variables to zero. That avoids having to generate
5149 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005150 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005151 {
5152 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5153 STACK_TV_VAR(idx)->vval.v_number = 0;
5154 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005155 ectx.ec_stack.ga_len += dfunc->df_varcount;
5156 if (dfunc->df_has_closure)
5157 {
5158 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5159 STACK_TV_VAR(idx)->vval.v_number = 0;
5160 ++ectx.ec_stack.ga_len;
5161 }
5162
5163 ectx.ec_instr = INSTRUCTIONS(dfunc);
5164 }
5165
5166 // Following errors are in the function, not the caller.
5167 // Commands behave like vim9script.
5168 estack_push_ufunc(ufunc, 1);
5169 current_sctx = ufunc->uf_script_ctx;
5170 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5171
5172 // Use a specific location for storing error messages to be converted to an
5173 // exception.
5174 saved_msg_list = msg_list;
5175 msg_list = &private_msg_list;
5176
5177 // Do turn errors into exceptions.
5178 suppress_errthrow = FALSE;
5179
5180 // Do not delete the function while executing it.
5181 ++ufunc->uf_calls;
5182
5183 // When ":silent!" was used before calling then we still abort the
5184 // function. If ":silent!" is used in the function then we don't.
5185 emsg_silent_def = emsg_silent;
5186 did_emsg_def = 0;
5187
5188 ectx.ec_where.wt_index = 0;
5189 ectx.ec_where.wt_variable = FALSE;
5190
5191 // Execute the instructions until done.
5192 ret = exec_instructions(&ectx);
5193 if (ret == OK)
5194 {
5195 // function finished, get result from the stack.
5196 if (ufunc->uf_ret_type == &t_void)
5197 {
5198 rettv->v_type = VAR_VOID;
5199 }
5200 else
5201 {
5202 tv = STACK_TV_BOT(-1);
5203 *rettv = *tv;
5204 tv->v_type = VAR_UNKNOWN;
5205 }
5206 }
5207
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005208 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005209 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5210 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005211
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005212 // Deal with any remaining closures, they may be in use somewhere.
5213 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005214 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005215 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005216 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005217 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005218
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005219 estack_pop();
5220 current_sctx = save_current_sctx;
5221
Bram Moolenaar2336c372021-12-06 15:06:54 +00005222 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5223 // Function was unreferenced while being used, free it now.
5224 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005225
Bram Moolenaar352134b2020-10-17 22:04:08 +02005226 if (*msg_list != NULL && saved_msg_list != NULL)
5227 {
5228 msglist_T **plist = saved_msg_list;
5229
5230 // Append entries from the current msg_list (uncaught exceptions) to
5231 // the saved msg_list.
5232 while (*plist != NULL)
5233 plist = &(*plist)->next;
5234
5235 *plist = *msg_list;
5236 }
5237 msg_list = saved_msg_list;
5238
Bram Moolenaar4c137212021-04-19 16:48:48 +02005239 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005240 {
5241 cmdmod.cmod_filter_regmatch.regprog = NULL;
5242 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005243 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005244 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005245 emsg_silent_def = save_emsg_silent_def;
5246 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005247
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005248failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005249 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005250 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5251 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005252 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005255 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005256 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005257 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005258 if (ectx.ec_outer_ref->or_outer_allocated)
5259 vim_free(ectx.ec_outer_ref->or_outer);
5260 partial_unref(ectx.ec_outer_ref->or_partial);
5261 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005262 }
5263
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005264 // Not sure if this is necessary.
5265 suppress_errthrow = save_suppress_errthrow;
5266
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005267 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5268 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005269 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005270 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005271 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005272 return ret;
5273}
5274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005275/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005276 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5277 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5278 * "pfx" is prefixed to every line.
5279 */
5280 static void
5281list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5282{
5283 int line_idx = 0;
5284 int prev_current = 0;
5285 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005286 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005287
5288 for (current = 0; current < instr_count; ++current)
5289 {
5290 isn_T *iptr = &instr[current];
5291 char *line;
5292
5293 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005294 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005295 while (line_idx < iptr->isn_lnum
5296 && line_idx < ufunc->uf_lines.ga_len)
5297 {
5298 if (current > prev_current)
5299 {
5300 msg_puts("\n\n");
5301 prev_current = current;
5302 }
5303 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5304 if (line != NULL)
5305 msg(line);
5306 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005307 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5308 {
5309 int first_def_arg = ufunc->uf_args.ga_len
5310 - ufunc->uf_def_args.ga_len;
5311
5312 if (def_arg_idx > 0)
5313 msg_puts("\n\n");
5314 msg_start();
5315 msg_puts(" ");
5316 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5317 first_def_arg + def_arg_idx]);
5318 msg_puts(" = ");
5319 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5320 msg_clr_eos();
5321 msg_end();
5322 }
5323 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005324
5325 switch (iptr->isn_type)
5326 {
5327 case ISN_EXEC:
5328 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5329 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005330 case ISN_EXEC_SPLIT:
5331 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5332 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005333 case ISN_EXECRANGE:
5334 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5335 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005336 case ISN_LEGACY_EVAL:
5337 smsg("%s%4d EVAL legacy %s", pfx, current,
5338 iptr->isn_arg.string);
5339 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005340 case ISN_REDIRSTART:
5341 smsg("%s%4d REDIR", pfx, current);
5342 break;
5343 case ISN_REDIREND:
5344 smsg("%s%4d REDIR END%s", pfx, current,
5345 iptr->isn_arg.number ? " append" : "");
5346 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005347 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005348#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005349 smsg("%s%4d CEXPR pre %s", pfx, current,
5350 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005351#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005352 break;
5353 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005354#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005355 {
5356 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5357
5358 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5359 cexpr_get_auname(cer->cer_cmdidx),
5360 cer->cer_forceit ? "!" : "",
5361 cer->cer_cmdline);
5362 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005363#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005364 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005365 case ISN_INSTR:
5366 {
5367 smsg("%s%4d INSTR", pfx, current);
5368 list_instructions(" ", iptr->isn_arg.instr,
5369 INT_MAX, NULL);
5370 msg(" -------------");
5371 }
5372 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005373 case ISN_SUBSTITUTE:
5374 {
5375 subs_T *subs = &iptr->isn_arg.subs;
5376
5377 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5378 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5379 msg(" -------------");
5380 }
5381 break;
5382 case ISN_EXECCONCAT:
5383 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5384 (varnumber_T)iptr->isn_arg.number);
5385 break;
5386 case ISN_ECHO:
5387 {
5388 echo_T *echo = &iptr->isn_arg.echo;
5389
5390 smsg("%s%4d %s %d", pfx, current,
5391 echo->echo_with_white ? "ECHO" : "ECHON",
5392 echo->echo_count);
5393 }
5394 break;
5395 case ISN_EXECUTE:
5396 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005397 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005398 break;
5399 case ISN_ECHOMSG:
5400 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005401 (varnumber_T)(iptr->isn_arg.number));
5402 break;
5403 case ISN_ECHOCONSOLE:
5404 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5405 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005406 break;
5407 case ISN_ECHOERR:
5408 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005409 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005410 break;
5411 case ISN_LOAD:
5412 {
5413 if (iptr->isn_arg.number < 0)
5414 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5415 (varnumber_T)(iptr->isn_arg.number
5416 + STACK_FRAME_SIZE));
5417 else
5418 smsg("%s%4d LOAD $%lld", pfx, current,
5419 (varnumber_T)(iptr->isn_arg.number));
5420 }
5421 break;
5422 case ISN_LOADOUTER:
5423 {
5424 if (iptr->isn_arg.number < 0)
5425 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5426 iptr->isn_arg.outer.outer_depth,
5427 iptr->isn_arg.outer.outer_idx
5428 + STACK_FRAME_SIZE);
5429 else
5430 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5431 iptr->isn_arg.outer.outer_depth,
5432 iptr->isn_arg.outer.outer_idx);
5433 }
5434 break;
5435 case ISN_LOADV:
5436 smsg("%s%4d LOADV v:%s", pfx, current,
5437 get_vim_var_name(iptr->isn_arg.number));
5438 break;
5439 case ISN_LOADSCRIPT:
5440 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005441 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5442 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5443 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005444
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005445 sv = get_script_svar(sref, -1);
5446 if (sv == NULL)
5447 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5448 pfx, current, si->sn_name);
5449 else
5450 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005451 sv->sv_name,
5452 sref->sref_idx,
5453 si->sn_name);
5454 }
5455 break;
5456 case ISN_LOADS:
5457 {
5458 scriptitem_T *si = SCRIPT_ITEM(
5459 iptr->isn_arg.loadstore.ls_sid);
5460
5461 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5462 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5463 }
5464 break;
5465 case ISN_LOADAUTO:
5466 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5467 break;
5468 case ISN_LOADG:
5469 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5470 break;
5471 case ISN_LOADB:
5472 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5473 break;
5474 case ISN_LOADW:
5475 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5476 break;
5477 case ISN_LOADT:
5478 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5479 break;
5480 case ISN_LOADGDICT:
5481 smsg("%s%4d LOAD g:", pfx, current);
5482 break;
5483 case ISN_LOADBDICT:
5484 smsg("%s%4d LOAD b:", pfx, current);
5485 break;
5486 case ISN_LOADWDICT:
5487 smsg("%s%4d LOAD w:", pfx, current);
5488 break;
5489 case ISN_LOADTDICT:
5490 smsg("%s%4d LOAD t:", pfx, current);
5491 break;
5492 case ISN_LOADOPT:
5493 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5494 break;
5495 case ISN_LOADENV:
5496 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5497 break;
5498 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005499 smsg("%s%4d LOADREG @%c", pfx, current,
5500 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005501 break;
5502
5503 case ISN_STORE:
5504 if (iptr->isn_arg.number < 0)
5505 smsg("%s%4d STORE arg[%lld]", pfx, current,
5506 iptr->isn_arg.number + STACK_FRAME_SIZE);
5507 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005508 smsg("%s%4d STORE $%lld", pfx, current,
5509 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005510 break;
5511 case ISN_STOREOUTER:
5512 {
5513 if (iptr->isn_arg.number < 0)
5514 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5515 iptr->isn_arg.outer.outer_depth,
5516 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5517 else
5518 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5519 iptr->isn_arg.outer.outer_depth,
5520 iptr->isn_arg.outer.outer_idx);
5521 }
5522 break;
5523 case ISN_STOREV:
5524 smsg("%s%4d STOREV v:%s", pfx, current,
5525 get_vim_var_name(iptr->isn_arg.number));
5526 break;
5527 case ISN_STOREAUTO:
5528 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5529 break;
5530 case ISN_STOREG:
5531 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5532 break;
5533 case ISN_STOREB:
5534 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5535 break;
5536 case ISN_STOREW:
5537 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5538 break;
5539 case ISN_STORET:
5540 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5541 break;
5542 case ISN_STORES:
5543 {
5544 scriptitem_T *si = SCRIPT_ITEM(
5545 iptr->isn_arg.loadstore.ls_sid);
5546
5547 smsg("%s%4d STORES %s in %s", pfx, current,
5548 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5549 }
5550 break;
5551 case ISN_STORESCRIPT:
5552 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005553 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5554 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5555 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005556
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005557 sv = get_script_svar(sref, -1);
5558 if (sv == NULL)
5559 smsg("%s%4d STORESCRIPT [deleted] in %s",
5560 pfx, current, si->sn_name);
5561 else
5562 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005563 sv->sv_name,
5564 sref->sref_idx,
5565 si->sn_name);
5566 }
5567 break;
5568 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005569 case ISN_STOREFUNCOPT:
5570 smsg("%s%4d %s &%s", pfx, current,
5571 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005572 iptr->isn_arg.storeopt.so_name);
5573 break;
5574 case ISN_STOREENV:
5575 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5576 break;
5577 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005578 smsg("%s%4d STOREREG @%c", pfx, current,
5579 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005580 break;
5581 case ISN_STORENR:
5582 smsg("%s%4d STORE %lld in $%d", pfx, current,
5583 iptr->isn_arg.storenr.stnr_val,
5584 iptr->isn_arg.storenr.stnr_idx);
5585 break;
5586
5587 case ISN_STOREINDEX:
5588 smsg("%s%4d STOREINDEX %s", pfx, current,
5589 vartype_name(iptr->isn_arg.vartype));
5590 break;
5591
5592 case ISN_STORERANGE:
5593 smsg("%s%4d STORERANGE", pfx, current);
5594 break;
5595
5596 // constants
5597 case ISN_PUSHNR:
5598 smsg("%s%4d PUSHNR %lld", pfx, current,
5599 (varnumber_T)(iptr->isn_arg.number));
5600 break;
5601 case ISN_PUSHBOOL:
5602 case ISN_PUSHSPEC:
5603 smsg("%s%4d PUSH %s", pfx, current,
5604 get_var_special_name(iptr->isn_arg.number));
5605 break;
5606 case ISN_PUSHF:
5607#ifdef FEAT_FLOAT
5608 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5609#endif
5610 break;
5611 case ISN_PUSHS:
5612 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5613 break;
5614 case ISN_PUSHBLOB:
5615 {
5616 char_u *r;
5617 char_u numbuf[NUMBUFLEN];
5618 char_u *tofree;
5619
5620 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5621 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5622 vim_free(tofree);
5623 }
5624 break;
5625 case ISN_PUSHFUNC:
5626 {
5627 char *name = (char *)iptr->isn_arg.string;
5628
5629 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5630 name == NULL ? "[none]" : name);
5631 }
5632 break;
5633 case ISN_PUSHCHANNEL:
5634#ifdef FEAT_JOB_CHANNEL
5635 {
5636 channel_T *channel = iptr->isn_arg.channel;
5637
5638 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5639 channel == NULL ? 0 : channel->ch_id);
5640 }
5641#endif
5642 break;
5643 case ISN_PUSHJOB:
5644#ifdef FEAT_JOB_CHANNEL
5645 {
5646 typval_T tv;
5647 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005648 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005649
5650 tv.v_type = VAR_JOB;
5651 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005652 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005653 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5654 }
5655#endif
5656 break;
5657 case ISN_PUSHEXC:
5658 smsg("%s%4d PUSH v:exception", pfx, current);
5659 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005660 case ISN_AUTOLOAD:
5661 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5662 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005663 case ISN_UNLET:
5664 smsg("%s%4d UNLET%s %s", pfx, current,
5665 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5666 iptr->isn_arg.unlet.ul_name);
5667 break;
5668 case ISN_UNLETENV:
5669 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5670 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5671 iptr->isn_arg.unlet.ul_name);
5672 break;
5673 case ISN_UNLETINDEX:
5674 smsg("%s%4d UNLETINDEX", pfx, current);
5675 break;
5676 case ISN_UNLETRANGE:
5677 smsg("%s%4d UNLETRANGE", pfx, current);
5678 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005679 case ISN_LOCKUNLOCK:
5680 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5681 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005682 case ISN_LOCKCONST:
5683 smsg("%s%4d LOCKCONST", pfx, current);
5684 break;
5685 case ISN_NEWLIST:
5686 smsg("%s%4d NEWLIST size %lld", pfx, current,
5687 (varnumber_T)(iptr->isn_arg.number));
5688 break;
5689 case ISN_NEWDICT:
5690 smsg("%s%4d NEWDICT size %lld", pfx, current,
5691 (varnumber_T)(iptr->isn_arg.number));
5692 break;
5693
5694 // function call
5695 case ISN_BCALL:
5696 {
5697 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5698
5699 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5700 internal_func_name(cbfunc->cbf_idx),
5701 cbfunc->cbf_argcount);
5702 }
5703 break;
5704 case ISN_DCALL:
5705 {
5706 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5707 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5708 + cdfunc->cdf_idx;
5709
5710 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005711 printable_func_name(df->df_ufunc),
5712 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005713 }
5714 break;
5715 case ISN_UCALL:
5716 {
5717 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5718
5719 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5720 cufunc->cuf_name, cufunc->cuf_argcount);
5721 }
5722 break;
5723 case ISN_PCALL:
5724 {
5725 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5726
5727 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5728 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5729 }
5730 break;
5731 case ISN_PCALL_END:
5732 smsg("%s%4d PCALL end", pfx, current);
5733 break;
5734 case ISN_RETURN:
5735 smsg("%s%4d RETURN", pfx, current);
5736 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005737 case ISN_RETURN_VOID:
5738 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005739 break;
5740 case ISN_FUNCREF:
5741 {
5742 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005743 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005744
Bram Moolenaar38453522021-11-28 22:00:12 +00005745 if (funcref->fr_func_name == NULL)
5746 {
5747 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5748 + funcref->fr_dfunc_idx;
5749 name = df->df_ufunc->uf_name;
5750 }
5751 else
5752 name = funcref->fr_func_name;
5753 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005754 }
5755 break;
5756
5757 case ISN_NEWFUNC:
5758 {
5759 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5760
5761 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5762 newfunc->nf_lambda, newfunc->nf_global);
5763 }
5764 break;
5765
5766 case ISN_DEF:
5767 {
5768 char_u *name = iptr->isn_arg.string;
5769
5770 smsg("%s%4d DEF %s", pfx, current,
5771 name == NULL ? (char_u *)"" : name);
5772 }
5773 break;
5774
5775 case ISN_JUMP:
5776 {
5777 char *when = "?";
5778
5779 switch (iptr->isn_arg.jump.jump_when)
5780 {
5781 case JUMP_ALWAYS:
5782 when = "JUMP";
5783 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005784 case JUMP_NEVER:
5785 iemsg("JUMP_NEVER should not be used");
5786 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005787 case JUMP_AND_KEEP_IF_TRUE:
5788 when = "JUMP_AND_KEEP_IF_TRUE";
5789 break;
5790 case JUMP_IF_FALSE:
5791 when = "JUMP_IF_FALSE";
5792 break;
5793 case JUMP_AND_KEEP_IF_FALSE:
5794 when = "JUMP_AND_KEEP_IF_FALSE";
5795 break;
5796 case JUMP_IF_COND_FALSE:
5797 when = "JUMP_IF_COND_FALSE";
5798 break;
5799 case JUMP_IF_COND_TRUE:
5800 when = "JUMP_IF_COND_TRUE";
5801 break;
5802 }
5803 smsg("%s%4d %s -> %d", pfx, current, when,
5804 iptr->isn_arg.jump.jump_where);
5805 }
5806 break;
5807
5808 case ISN_JUMP_IF_ARG_SET:
5809 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5810 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5811 iptr->isn_arg.jump.jump_where);
5812 break;
5813
5814 case ISN_FOR:
5815 {
5816 forloop_T *forloop = &iptr->isn_arg.forloop;
5817
5818 smsg("%s%4d FOR $%d -> %d", pfx, current,
5819 forloop->for_idx, forloop->for_end);
5820 }
5821 break;
5822
5823 case ISN_TRY:
5824 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005825 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005826
5827 if (try->try_ref->try_finally == 0)
5828 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5829 pfx, current,
5830 try->try_ref->try_catch,
5831 try->try_ref->try_endtry);
5832 else
5833 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5834 pfx, current,
5835 try->try_ref->try_catch,
5836 try->try_ref->try_finally,
5837 try->try_ref->try_endtry);
5838 }
5839 break;
5840 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005841 smsg("%s%4d CATCH", pfx, current);
5842 break;
5843 case ISN_TRYCONT:
5844 {
5845 trycont_T *trycont = &iptr->isn_arg.trycont;
5846
5847 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5848 trycont->tct_levels,
5849 trycont->tct_levels == 1 ? "" : "s",
5850 trycont->tct_where);
5851 }
5852 break;
5853 case ISN_FINALLY:
5854 smsg("%s%4d FINALLY", pfx, current);
5855 break;
5856 case ISN_ENDTRY:
5857 smsg("%s%4d ENDTRY", pfx, current);
5858 break;
5859 case ISN_THROW:
5860 smsg("%s%4d THROW", pfx, current);
5861 break;
5862
5863 // expression operations on number
5864 case ISN_OPNR:
5865 case ISN_OPFLOAT:
5866 case ISN_OPANY:
5867 {
5868 char *what;
5869 char *ins;
5870
5871 switch (iptr->isn_arg.op.op_type)
5872 {
5873 case EXPR_MULT: what = "*"; break;
5874 case EXPR_DIV: what = "/"; break;
5875 case EXPR_REM: what = "%"; break;
5876 case EXPR_SUB: what = "-"; break;
5877 case EXPR_ADD: what = "+"; break;
5878 default: what = "???"; break;
5879 }
5880 switch (iptr->isn_type)
5881 {
5882 case ISN_OPNR: ins = "OPNR"; break;
5883 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5884 case ISN_OPANY: ins = "OPANY"; break;
5885 default: ins = "???"; break;
5886 }
5887 smsg("%s%4d %s %s", pfx, current, ins, what);
5888 }
5889 break;
5890
5891 case ISN_COMPAREBOOL:
5892 case ISN_COMPARESPECIAL:
5893 case ISN_COMPARENR:
5894 case ISN_COMPAREFLOAT:
5895 case ISN_COMPARESTRING:
5896 case ISN_COMPAREBLOB:
5897 case ISN_COMPARELIST:
5898 case ISN_COMPAREDICT:
5899 case ISN_COMPAREFUNC:
5900 case ISN_COMPAREANY:
5901 {
5902 char *p;
5903 char buf[10];
5904 char *type;
5905
5906 switch (iptr->isn_arg.op.op_type)
5907 {
5908 case EXPR_EQUAL: p = "=="; break;
5909 case EXPR_NEQUAL: p = "!="; break;
5910 case EXPR_GREATER: p = ">"; break;
5911 case EXPR_GEQUAL: p = ">="; break;
5912 case EXPR_SMALLER: p = "<"; break;
5913 case EXPR_SEQUAL: p = "<="; break;
5914 case EXPR_MATCH: p = "=~"; break;
5915 case EXPR_IS: p = "is"; break;
5916 case EXPR_ISNOT: p = "isnot"; break;
5917 case EXPR_NOMATCH: p = "!~"; break;
5918 default: p = "???"; break;
5919 }
5920 STRCPY(buf, p);
5921 if (iptr->isn_arg.op.op_ic == TRUE)
5922 strcat(buf, "?");
5923 switch(iptr->isn_type)
5924 {
5925 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5926 case ISN_COMPARESPECIAL:
5927 type = "COMPARESPECIAL"; break;
5928 case ISN_COMPARENR: type = "COMPARENR"; break;
5929 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5930 case ISN_COMPARESTRING:
5931 type = "COMPARESTRING"; break;
5932 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5933 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5934 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5935 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5936 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5937 default: type = "???"; break;
5938 }
5939
5940 smsg("%s%4d %s %s", pfx, current, type, buf);
5941 }
5942 break;
5943
5944 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5945 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5946
5947 // expression operations
5948 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5949 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5950 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5951 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5952 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5953 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5954 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5955 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5956 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5957 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5958 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5959 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005960 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005961 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5962 iptr->isn_arg.getitem.gi_index,
5963 iptr->isn_arg.getitem.gi_with_op ?
5964 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005965 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5966 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5967 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005968 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5969 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5970
Bram Moolenaar4c137212021-04-19 16:48:48 +02005971 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5972
5973 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5974 case ISN_CHECKTYPE:
5975 {
5976 checktype_T *ct = &iptr->isn_arg.type;
5977 char *tofree;
5978
5979 if (ct->ct_arg_idx == 0)
5980 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5981 type_name(ct->ct_type, &tofree),
5982 (int)ct->ct_off);
5983 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005984 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5985 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005986 type_name(ct->ct_type, &tofree),
5987 (int)ct->ct_off,
5988 (int)ct->ct_arg_idx);
5989 vim_free(tofree);
5990 break;
5991 }
5992 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5993 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5994 iptr->isn_arg.checklen.cl_min_len);
5995 break;
5996 case ISN_SETTYPE:
5997 {
5998 char *tofree;
5999
6000 smsg("%s%4d SETTYPE %s", pfx, current,
6001 type_name(iptr->isn_arg.type.ct_type, &tofree));
6002 vim_free(tofree);
6003 break;
6004 }
6005 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006006 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6007 smsg("%s%4d INVERT %d (!val)", pfx, current,
6008 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006009 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006010 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6011 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006012 break;
6013 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006014 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006015 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006016 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6017 pfx, current,
6018 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006019 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006020 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6021 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006022 break;
6023 case ISN_PUT:
6024 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6025 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006026 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006027 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6028 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006029 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006030 else
6031 smsg("%s%4d PUT %c %ld", pfx, current,
6032 iptr->isn_arg.put.put_regname,
6033 (long)iptr->isn_arg.put.put_lnum);
6034 break;
6035
Bram Moolenaar4c137212021-04-19 16:48:48 +02006036 case ISN_CMDMOD:
6037 {
6038 char_u *buf;
6039 size_t len = produce_cmdmods(
6040 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6041
6042 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006043 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006044 {
6045 (void)produce_cmdmods(
6046 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6047 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6048 vim_free(buf);
6049 }
6050 break;
6051 }
6052 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6053
6054 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006055 smsg("%s%4d PROFILE START line %d", pfx, current,
6056 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006057 break;
6058
6059 case ISN_PROF_END:
6060 smsg("%s%4d PROFILE END", pfx, current);
6061 break;
6062
Bram Moolenaare99d4222021-06-13 14:01:26 +02006063 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006064 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6065 iptr->isn_arg.debug.dbg_break_lnum + 1,
6066 iptr->isn_lnum,
6067 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006068 break;
6069
Bram Moolenaar4c137212021-04-19 16:48:48 +02006070 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6071 iptr->isn_arg.unpack.unp_count,
6072 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6073 break;
6074 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6075 iptr->isn_arg.shuffle.shfl_item,
6076 iptr->isn_arg.shuffle.shfl_up);
6077 break;
6078 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6079
6080 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6081 return;
6082 }
6083
6084 out_flush(); // output one line at a time
6085 ui_breakcheck();
6086 if (got_int)
6087 break;
6088 }
6089}
6090
6091/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006092 * Handle command line completion for the :disassemble command.
6093 */
6094 void
6095set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6096{
6097 char_u *p;
6098
6099 // Default: expand user functions, "debug" and "profile"
6100 xp->xp_context = EXPAND_DISASSEMBLE;
6101 xp->xp_pattern = arg;
6102
6103 // first argument already typed: only user function names
6104 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6105 {
6106 xp->xp_context = EXPAND_USER_FUNC;
6107 xp->xp_pattern = skipwhite(p);
6108 }
6109}
6110
6111/*
6112 * Function given to ExpandGeneric() to obtain the list of :disassemble
6113 * arguments.
6114 */
6115 char_u *
6116get_disassemble_argument(expand_T *xp, int idx)
6117{
6118 if (idx == 0)
6119 return (char_u *)"debug";
6120 if (idx == 1)
6121 return (char_u *)"profile";
6122 return get_user_func_name(xp, idx - 2);
6123}
6124
6125/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006126 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006127 * We don't really need this at runtime, but we do have tests that require it,
6128 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129 */
6130 void
6131ex_disassemble(exarg_T *eap)
6132{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006133 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006134 char_u *fname;
6135 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006136 dfunc_T *dfunc;
6137 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006138 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006139 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006140 compiletype_T compile_type = CT_NONE;
6141
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006142 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006143 {
6144 compile_type = CT_PROFILE;
6145 arg = skipwhite(arg + 7);
6146 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006147 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006148 {
6149 compile_type = CT_DEBUG;
6150 arg = skipwhite(arg + 5);
6151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006152
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006153 if (STRNCMP(arg, "<lambda>", 8) == 0)
6154 {
6155 arg += 8;
6156 (void)getdigits(&arg);
6157 fname = vim_strnsave(eap->arg, arg - eap->arg);
6158 }
6159 else
6160 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006161 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006162 if (fname == NULL)
6163 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006164 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006165 return;
6166 }
6167
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006168 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006169 if (ufunc == NULL)
6170 {
6171 char_u *p = untrans_function_name(fname);
6172
6173 if (p != NULL)
6174 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006175 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006176 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006177 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006178 if (ufunc == NULL)
6179 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006180 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006181 return;
6182 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006183 if (func_needs_compiling(ufunc, compile_type)
6184 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006185 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006186 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006188 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006189 return;
6190 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006191 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006192
6193 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006194 switch (compile_type)
6195 {
6196 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006197#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006198 instr = dfunc->df_instr_prof;
6199 instr_count = dfunc->df_instr_prof_count;
6200 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006201#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006202 // FALLTHROUGH
6203 case CT_NONE:
6204 instr = dfunc->df_instr;
6205 instr_count = dfunc->df_instr_count;
6206 break;
6207 case CT_DEBUG:
6208 instr = dfunc->df_instr_debug;
6209 instr_count = dfunc->df_instr_debug_count;
6210 break;
6211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006212
Bram Moolenaar4c137212021-04-19 16:48:48 +02006213 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214}
6215
6216/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006217 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218 * list, etc. Mostly like what JavaScript does, except that empty list and
6219 * empty dictionary are FALSE.
6220 */
6221 int
6222tv2bool(typval_T *tv)
6223{
6224 switch (tv->v_type)
6225 {
6226 case VAR_NUMBER:
6227 return tv->vval.v_number != 0;
6228 case VAR_FLOAT:
6229#ifdef FEAT_FLOAT
6230 return tv->vval.v_float != 0.0;
6231#else
6232 break;
6233#endif
6234 case VAR_PARTIAL:
6235 return tv->vval.v_partial != NULL;
6236 case VAR_FUNC:
6237 case VAR_STRING:
6238 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6239 case VAR_LIST:
6240 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6241 case VAR_DICT:
6242 return tv->vval.v_dict != NULL
6243 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6244 case VAR_BOOL:
6245 case VAR_SPECIAL:
6246 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6247 case VAR_JOB:
6248#ifdef FEAT_JOB_CHANNEL
6249 return tv->vval.v_job != NULL;
6250#else
6251 break;
6252#endif
6253 case VAR_CHANNEL:
6254#ifdef FEAT_JOB_CHANNEL
6255 return tv->vval.v_channel != NULL;
6256#else
6257 break;
6258#endif
6259 case VAR_BLOB:
6260 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6261 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006262 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006264 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265 break;
6266 }
6267 return FALSE;
6268}
6269
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006270 void
6271emsg_using_string_as(typval_T *tv, int as_number)
6272{
6273 semsg(_(as_number ? e_using_string_as_number_str
6274 : e_using_string_as_bool_str),
6275 tv->vval.v_string == NULL
6276 ? (char_u *)"" : tv->vval.v_string);
6277}
6278
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279/*
6280 * If "tv" is a string give an error and return FAIL.
6281 */
6282 int
6283check_not_string(typval_T *tv)
6284{
6285 if (tv->v_type == VAR_STRING)
6286 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006287 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006288 clear_tv(tv);
6289 return FAIL;
6290 }
6291 return OK;
6292}
6293
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294
6295#endif // FEAT_EVAL