blob: 08774fee323cdb35f7f1c5e91eeaed8625901324 [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
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010067// 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)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
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 Moolenaar1d84f762022-09-03 21:35:53 +0100104// Get pointer to item in the stack.
105#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200108#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 +0100109
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100110// Get pointer to a local variable on the stack. Negative for arguments.
111#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
112
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200113 void
114to_string_error(vartype_T vartype)
115{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200116 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200117}
118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100120 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 */
122 static int
123ufunc_argcount(ufunc_T *ufunc)
124{
125 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
126}
127
128/*
LemonBoy372bcce2022-04-25 12:43:20 +0100129 * Create a new string from "count" items at the bottom of the stack.
130 * A trailing NUL is appended.
131 * When "count" is zero an empty string is added to the stack.
132 */
133 static int
134exe_concat(int count, ectx_T *ectx)
135{
136 int idx;
137 int len = 0;
138 typval_T *tv;
139 garray_T ga;
140
141 ga_init2(&ga, sizeof(char), 1);
142 // Preallocate enough space for the whole string to avoid having to grow
143 // and copy.
144 for (idx = 0; idx < count; ++idx)
145 {
146 tv = STACK_TV_BOT(idx - count);
147 if (tv->vval.v_string != NULL)
148 len += (int)STRLEN(tv->vval.v_string);
149 }
150
151 if (ga_grow(&ga, len + 1) == FAIL)
152 return FAIL;
153
154 for (idx = 0; idx < count; ++idx)
155 {
156 tv = STACK_TV_BOT(idx - count);
157 ga_concat(&ga, tv->vval.v_string);
158 clear_tv(tv);
159 }
160
161 // add a terminating NUL
162 ga_append(&ga, NUL);
163
164 ectx->ec_stack.ga_len -= count - 1;
165 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
166
167 return OK;
168}
169
170/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200171 * Create a new list from "count" items at the bottom of the stack.
172 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100173 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200174 */
175 static int
176exe_newlist(int count, ectx_T *ectx)
177{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 int idx;
180 typval_T *tv;
181
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100182 if (count >= 0)
183 {
184 list = list_alloc_with_items(count);
185 if (list == NULL)
186 return FAIL;
187 for (idx = 0; idx < count; ++idx)
188 list_set_item(list, idx, STACK_TV_BOT(idx - count));
189 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190
191 if (count > 0)
192 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200193 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100194 {
195 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 else
199 ++ectx->ec_stack.ga_len;
200 tv = STACK_TV_BOT(-1);
201 tv->v_type = VAR_LIST;
202 tv->vval.v_list = list;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100203 if (list != NULL)
204 ++list->lv_refcount;
205 return OK;
206}
207
208/*
209 * Implementation of ISN_NEWDICT.
210 * Returns FAIL on total failure, MAYBE on error.
211 */
212 static int
213exe_newdict(int count, ectx_T *ectx)
214{
215 dict_T *dict = NULL;
216 dictitem_T *item;
217 char_u *key;
218 int idx;
219 typval_T *tv;
220
221 if (count >= 0)
222 {
223 dict = dict_alloc();
224 if (unlikely(dict == NULL))
225 return FAIL;
226 for (idx = 0; idx < count; ++idx)
227 {
228 // have already checked key type is VAR_STRING
229 tv = STACK_TV_BOT(2 * (idx - count));
230 // check key is unique
231 key = tv->vval.v_string == NULL
232 ? (char_u *)"" : tv->vval.v_string;
233 item = dict_find(dict, key, -1);
234 if (item != NULL)
235 {
236 semsg(_(e_duplicate_key_in_dicitonary), key);
237 dict_unref(dict);
238 return MAYBE;
239 }
240 item = dictitem_alloc(key);
241 clear_tv(tv);
242 if (unlikely(item == NULL))
243 {
244 dict_unref(dict);
245 return FAIL;
246 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100247 tv = STACK_TV_BOT(2 * (idx - count) + 1);
248 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100249 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100250 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100251 if (dict_add(dict, item) == FAIL)
252 {
253 // can this ever happen?
254 dict_unref(dict);
255 return FAIL;
256 }
257 }
258 }
259
260 if (count > 0)
261 ectx->ec_stack.ga_len -= 2 * count - 1;
262 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
263 return FAIL;
264 else
265 ++ectx->ec_stack.ga_len;
266 tv = STACK_TV_BOT(-1);
267 tv->v_type = VAR_DICT;
268 tv->v_lock = 0;
269 tv->vval.v_dict = dict;
270 if (dict != NULL)
271 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200272 return OK;
273}
274
275/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200276 * If debug_tick changed check if "ufunc" has a breakpoint and update
277 * "uf_has_breakpoint".
278 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000279 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200280update_has_breakpoint(ufunc_T *ufunc)
281{
282 if (ufunc->uf_debug_tick != debug_tick)
283 {
284 linenr_T breakpoint;
285
286 ufunc->uf_debug_tick = debug_tick;
287 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
288 ufunc->uf_has_breakpoint = breakpoint > 0;
289 }
290}
291
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200292static garray_T dict_stack = GA_EMPTY;
293
294/*
295 * Put a value on the dict stack. This consumes "tv".
296 */
297 static int
298dict_stack_save(typval_T *tv)
299{
300 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000301 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200302 if (ga_grow(&dict_stack, 1) == FAIL)
303 return FAIL;
304 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
305 ++dict_stack.ga_len;
306 return OK;
307}
308
309/*
310 * Get the typval at top of the dict stack.
311 */
312 static typval_T *
313dict_stack_get_tv(void)
314{
315 if (dict_stack.ga_len == 0)
316 return NULL;
317 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
318}
319
320/*
321 * Get the dict at top of the dict stack.
322 */
323 static dict_T *
324dict_stack_get_dict(void)
325{
326 typval_T *tv;
327
328 if (dict_stack.ga_len == 0)
329 return NULL;
330 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
331 if (tv->v_type == VAR_DICT)
332 return tv->vval.v_dict;
333 return NULL;
334}
335
336/*
337 * Drop an item from the dict stack.
338 */
339 static void
340dict_stack_drop(void)
341{
342 if (dict_stack.ga_len == 0)
343 {
344 iemsg("Dict stack underflow");
345 return;
346 }
347 --dict_stack.ga_len;
348 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
349}
350
351/*
352 * Drop items from the dict stack until the length is equal to "len".
353 */
354 static void
355dict_stack_clear(int len)
356{
357 while (dict_stack.ga_len > len)
358 dict_stack_drop();
359}
360
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200361/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000362 * Get a pointer to useful "pt_outer" of "pt".
363 */
364 static outer_T *
365get_pt_outer(partial_T *pt)
366{
367 partial_T *ptref = pt->pt_outer_partial;
368
369 if (ptref == NULL)
370 return &pt->pt_outer;
371
372 // partial using partial (recursively)
373 while (ptref->pt_outer_partial != NULL)
374 ptref = ptref->pt_outer_partial;
375 return &ptref->pt_outer;
376}
377
378/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100380 * This adds a stack frame and sets the instruction pointer to the start of the
381 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200382 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 *
384 * Stack has:
385 * - current arguments (already there)
386 * - omitted optional argument (default values) added here
387 * - stack frame:
388 * - pointer to calling function
389 * - Index of next instruction in calling function
390 * - previous frame pointer
391 * - reserved space for local variables
392 */
393 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394call_dfunc(
395 int cdf_idx,
396 partial_T *pt,
397 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100400 int argcount = argcount_arg;
401 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
402 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200403 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100404 int arg_to_add;
405 int vararg_count = 0;
406 int varcount;
407 int idx;
408 estack_T *entry;
409 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200410 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000411 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412
413 if (dfunc->df_deleted)
414 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100415 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000416 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100417 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 return FAIL;
419 }
420
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100421#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100422 if (do_profiling == PROF_YES)
423 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200424 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100425 {
426 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
427 + profile_info_ga.ga_len;
428 ++profile_info_ga.ga_len;
429 CLEAR_POINTER(info);
430 profile_may_start_func(info, ufunc,
431 (((dfunc_T *)def_functions.ga_data)
432 + ectx->ec_dfunc_idx)->df_ufunc);
433 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100434 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100435#endif
436
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200437 // When debugging and using "cont" switches to the not-debugged
438 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000439 compile_type = get_compile_type(ufunc);
440 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200441 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000442 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200443
444 // compile_def_function() may cause def_functions.ga_data to change
445 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
446 }
447 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200448 {
449 if (did_emsg_cumul + did_emsg == did_emsg_before)
450 semsg(_(e_function_is_not_compiled_str),
451 printable_func_name(ufunc));
452 return FAIL;
453 }
454
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200455 if (ufunc->uf_va_name != NULL)
456 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200457 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200458 // Stack at time of call with 2 varargs:
459 // normal_arg
460 // optional_arg
461 // vararg_1
462 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200463 // After creating the list:
464 // normal_arg
465 // optional_arg
466 // vararg-list
467 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200468 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200469 // After creating the list
470 // normal_arg
471 // (space for optional_arg)
472 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200473 vararg_count = argcount - ufunc->uf_args.ga_len;
474 if (vararg_count < 0)
475 vararg_count = 0;
476 else
477 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200478 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200479 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200480
481 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200482 }
483
Bram Moolenaarfe270812020-04-11 22:31:27 +0200484 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200485 if (arg_to_add < 0)
486 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100487 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
488 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200489 return FAIL;
490 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000491 else if (arg_to_add > ufunc->uf_def_args.ga_len)
492 {
493 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
494
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100495 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
496 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000497 return FAIL;
498 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200499
500 // Reserve space for:
501 // - missing arguments
502 // - stack frame
503 // - local variables
504 // - if needed: a counter for number of closures created in
505 // ectx->ec_funcrefs.
506 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200507 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 return FAIL;
509
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100510 // If depth of calling is getting too high, don't execute the function.
511 if (funcdepth_increment() == FAIL)
512 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200513 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100514
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100515 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200516 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100517 {
518 floc = ALLOC_ONE(funclocal_T);
519 if (floc == NULL)
520 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200521 *floc = ectx->ec_funclocal;
522 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100523 }
524
Bram Moolenaarfe270812020-04-11 22:31:27 +0200525 // Move the vararg-list to below the missing optional arguments.
526 if (vararg_count > 0 && arg_to_add > 0)
527 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100528
529 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200530 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200531 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200532 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533
534 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100535 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
536 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200537 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200538 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
539 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100540 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100541 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200542 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543
544 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200545 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000546 {
547 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
548
549 tv->v_type = VAR_NUMBER;
550 tv->vval.v_number = 0;
551 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200552 if (dfunc->df_has_closure)
553 {
554 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
555
556 tv->v_type = VAR_NUMBER;
557 tv->vval.v_number = 0;
558 }
559 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100561 if (pt != NULL || ufunc->uf_partial != NULL
562 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100563 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200564 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100565
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200566 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100567 return FAIL;
568 if (pt != NULL)
569 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000570 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200571 ++pt->pt_refcount;
572 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100573 }
574 else if (ufunc->uf_partial != NULL)
575 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000576 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200577 ++ufunc->uf_partial->pt_refcount;
578 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100579 }
580 else
581 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200582 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200583 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200584 {
585 vim_free(ref);
586 return FAIL;
587 }
588 ref->or_outer_allocated = TRUE;
589 ref->or_outer->out_stack = &ectx->ec_stack;
590 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
591 if (ectx->ec_outer_ref != NULL)
592 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100593 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200594 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100595 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100596 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200597 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100598
Bram Moolenaarc970e422021-03-17 15:03:04 +0100599 ++ufunc->uf_calls;
600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 // Set execution state to the start of the called function.
602 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100603 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100604 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200605 if (entry != NULL)
606 {
607 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200608 // Save the current context so it can be restored on return.
609 entry->es_save_sctx = current_sctx;
610 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200611 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100612
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200613 // Start execution at the first instruction.
614 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615
616 return OK;
617}
618
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000619// Double linked list of funcstack_T in use.
620static funcstack_T *first_funcstack = NULL;
621
622 static void
623add_funcstack_to_list(funcstack_T *funcstack)
624{
625 // Link in list of funcstacks.
626 if (first_funcstack != NULL)
627 first_funcstack->fs_prev = funcstack;
628 funcstack->fs_next = first_funcstack;
629 funcstack->fs_prev = NULL;
630 first_funcstack = funcstack;
631}
632
633 static void
634remove_funcstack_from_list(funcstack_T *funcstack)
635{
636 if (funcstack->fs_prev == NULL)
637 first_funcstack = funcstack->fs_next;
638 else
639 funcstack->fs_prev->fs_next = funcstack->fs_next;
640 if (funcstack->fs_next != NULL)
641 funcstack->fs_next->fs_prev = funcstack->fs_prev;
642}
643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200645 * Used when returning from a function: Check if any closure is still
646 * referenced. If so then move the arguments and variables to a separate piece
647 * of stack to be used when the closure is called.
648 * When "free_arguments" is TRUE the arguments are to be freed.
649 * Returns FAIL when out of memory.
650 */
651 static int
652handle_closure_in_use(ectx_T *ectx, int free_arguments)
653{
654 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
655 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200656 int argcount;
657 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200658 int idx;
659 typval_T *tv;
660 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200661 garray_T *gap = &ectx->ec_funcrefs;
662 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200663
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200664 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200665 return OK; // function was freed
666 if (dfunc->df_has_closure == 0)
667 return OK; // no closures
668 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
669 closure_count = tv->vval.v_number;
670 if (closure_count == 0)
671 return OK; // no funcrefs created
672
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200673 argcount = ufunc_argcount(dfunc->df_ufunc);
674 top = ectx->ec_frame_idx - argcount;
675
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200676 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200677 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200678 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200679 partial_T *pt;
680 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200681
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200682 if (off < 0)
683 continue; // count is off or already done
684 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200685 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200686 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200687 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200688 int i;
689
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000690 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200691 // unreferenced on return.
692 for (i = 0; i < dfunc->df_varcount; ++i)
693 {
694 typval_T *stv = STACK_TV(ectx->ec_frame_idx
695 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200696 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200697 --refcount;
698 }
699 if (refcount > 1)
700 {
701 closure_in_use = TRUE;
702 break;
703 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200704 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200705 }
706
707 if (closure_in_use)
708 {
709 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
710 typval_T *stack;
711
712 // A closure is using the arguments and/or local variables.
713 // Move them to the called function.
714 if (funcstack == NULL)
715 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000716
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200717 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
718 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200719 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
720 funcstack->fs_ga.ga_data = stack;
721 if (stack == NULL)
722 {
723 vim_free(funcstack);
724 return FAIL;
725 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000726 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200727
728 // Move or copy the arguments.
729 for (idx = 0; idx < argcount; ++idx)
730 {
731 tv = STACK_TV(top + idx);
732 if (free_arguments)
733 {
734 *(stack + idx) = *tv;
735 tv->v_type = VAR_UNKNOWN;
736 }
737 else
738 copy_tv(tv, stack + idx);
739 }
740 // Move the local variables.
741 for (idx = 0; idx < dfunc->df_varcount; ++idx)
742 {
743 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200744
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200745 // A partial created for a local function, that is also used as a
746 // local variable, has a reference count for the variable, thus
747 // will never go down to zero. When all these refcounts are one
748 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000749 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200750 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
751 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200752 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200753
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200754 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200755 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
756 gap->ga_len - closure_count + i])
757 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200758 }
759
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200760 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200761 tv->v_type = VAR_UNKNOWN;
762 }
763
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200764 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200765 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200766 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
767 - closure_count + idx];
768 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200769 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200770 ++funcstack->fs_refcount;
771 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100772 pt->pt_outer.out_stack = &funcstack->fs_ga;
773 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200774 }
775 }
776 }
777
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200778 for (idx = 0; idx < closure_count; ++idx)
779 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
780 - closure_count + idx]);
781 gap->ga_len -= closure_count;
782 if (gap->ga_len == 0)
783 ga_clear(gap);
784
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200785 return OK;
786}
787
788/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200789 * Called when a partial is freed or its reference count goes down to one. The
790 * funcstack may be the only reference to the partials in the local variables.
791 * Go over all of them, the funcref and can be freed if all partials
792 * referencing the funcstack have a reference count of one.
793 */
794 void
795funcstack_check_refcount(funcstack_T *funcstack)
796{
797 int i;
798 garray_T *gap = &funcstack->fs_ga;
799 int done = 0;
800
801 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
802 return;
803 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
804 {
805 typval_T *tv = ((typval_T *)gap->ga_data) + i;
806
807 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
808 && tv->vval.v_partial->pt_funcstack == funcstack
809 && tv->vval.v_partial->pt_refcount == 1)
810 ++done;
811 }
812 if (done == funcstack->fs_min_refcount)
813 {
814 typval_T *stack = gap->ga_data;
815
816 // All partials referencing the funcstack have a reference count of
817 // one, thus the funcstack is no longer of use.
818 for (i = 0; i < gap->ga_len; ++i)
819 clear_tv(stack + i);
820 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000821 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200822 vim_free(funcstack);
823 }
824}
825
826/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000827 * For garbage collecting: set references in all variables referenced by
828 * all funcstacks.
829 */
830 int
831set_ref_in_funcstacks(int copyID)
832{
833 funcstack_T *funcstack;
834
835 for (funcstack = first_funcstack; funcstack != NULL;
836 funcstack = funcstack->fs_next)
837 {
838 typval_T *stack = funcstack->fs_ga.ga_data;
839 int i;
840
841 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
842 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
843 return TRUE; // abort
844 }
845 return FALSE;
846}
847
Bram Moolenaar806a2732022-09-04 15:40:36 +0100848// Ugly static to avoid passing the execution context around through many
849// layers.
850static ectx_T *current_ectx = NULL;
851
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000852/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100853 * Return TRUE if currently executing a :def function.
854 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100855 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100856 int
857in_def_function(void)
858{
859 return current_ectx != NULL;
860}
861
862/*
863 * Add an entry for a deferred function call to the currently executing
864 * function.
865 * Return the list or NULL when failed.
866 */
867 static list_T *
868add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100869{
870 typval_T *defer_tv = STACK_TV_VAR(var_idx);
871 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100872 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100873 typval_T listval;
874
875 if (defer_tv->v_type != VAR_LIST)
876 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100877 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100878 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100879 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100880 }
881 defer_l = defer_tv->vval.v_list;
882
883 l = list_alloc_with_items(argcount + 1);
884 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100885 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100886 listval.v_type = VAR_LIST;
887 listval.vval.v_list = l;
888 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100889 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100890 {
891 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100892 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100893 }
894
Bram Moolenaar806a2732022-09-04 15:40:36 +0100895 return l;
896}
897
898/*
899 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
900 * The local variable that lists deferred functions is "var_idx".
901 * Returns OK or FAIL.
902 */
903 static int
904defer_command(int var_idx, int argcount, ectx_T *ectx)
905{
906 list_T *l = add_defer_item(var_idx, argcount, ectx);
907 int i;
908 typval_T *func_tv;
909
910 if (l == NULL)
911 return FAIL;
912
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100913 func_tv = STACK_TV_BOT(-argcount - 1);
914 // TODO: check type is a funcref
915 list_set_item(l, 0, func_tv);
916
917 for (i = 1; i <= argcount; ++i)
918 list_set_item(l, i, STACK_TV_BOT(-argcount + i - 1));
919 ectx->ec_stack.ga_len -= argcount + 1;
920 return OK;
921}
922
923/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100924 * Add a deferred function "name" with one argument "arg_tv".
925 * Consumes "name", also on failure.
926 * Only to be called when in_def_function() returns TRUE.
927 */
928 int
929add_defer_function(char_u *name, int argcount, typval_T *argvars)
930{
931 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
932 + current_ectx->ec_dfunc_idx;
933 list_T *l;
934 typval_T func_tv;
935 int i;
936
937 if (dfunc->df_defer_var_idx == 0)
938 {
939 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100940 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100941 return FAIL;
942 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100943
944 l = add_defer_item(dfunc->df_defer_var_idx - 1, 1, current_ectx);
945 if (l == NULL)
946 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100947 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100948 return FAIL;
949 }
950
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100951 func_tv.v_type = VAR_FUNC;
952 func_tv.v_lock = 0;
953 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100954 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100955
Bram Moolenaar806a2732022-09-04 15:40:36 +0100956 for (i = 0; i < argcount; ++i)
957 list_set_item(l, i + 1, argvars + i);
958 return OK;
959}
960
961/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100962 * Invoked when returning from a function: Invoke any deferred calls.
963 */
964 static void
965invoke_defer_funcs(ectx_T *ectx)
966{
967 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
968 + ectx->ec_dfunc_idx;
969 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
970 listitem_T *li;
971
972 if (defer_tv->v_type != VAR_LIST)
973 return; // no function added
974 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
975 {
976 list_T *l = li->li_tv.vval.v_list;
977 typval_T rettv;
978 typval_T argvars[MAX_FUNC_ARGS];
979 int i;
980 listitem_T *arg_li = l->lv_first;
981 funcexe_T funcexe;
982
983 for (i = 0; i < l->lv_len - 1; ++i)
984 {
985 arg_li = arg_li->li_next;
986 argvars[i] = arg_li->li_tv;
987 }
988
989 CLEAR_FIELD(funcexe);
990 funcexe.fe_evaluate = TRUE;
991 rettv.v_type = VAR_UNKNOWN;
992 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
993 &rettv, l->lv_len - 1, argvars, &funcexe);
994 clear_tv(&rettv);
995 }
996}
997
998/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 * Return from the current function.
1000 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001001 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001002func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001005 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001006 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1007 + ectx->ec_dfunc_idx;
1008 int argcount = ufunc_argcount(dfunc->df_ufunc);
1009 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001010 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001011 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1012 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001013 funclocal_T *floc;
1014#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001015 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1016 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017
Bram Moolenaar12d26532021-02-19 19:13:21 +01001018 if (do_profiling == PROF_YES)
1019 {
1020 ufunc_T *caller = prev_dfunc->df_ufunc;
1021
1022 if (dfunc->df_ufunc->uf_profiling
1023 || (caller != NULL && caller->uf_profiling))
1024 {
1025 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1026 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1027 --profile_info_ga.ga_len;
1028 }
1029 }
1030#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001031
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001032 if (dfunc->df_defer_var_idx > 0)
1033 invoke_defer_funcs(ectx);
1034
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001035 // No check for uf_refcount being zero, cannot think of a way that would
1036 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001037 --dfunc->df_ufunc->uf_calls;
1038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001040 entry = estack_pop();
1041 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001042 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001044 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1045 return FAIL;
1046
1047 // Clear the arguments.
1048 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1049 clear_tv(STACK_TV(idx));
1050
1051 // Clear local variables and temp values, but not the return value.
1052 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001053 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001055
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001056 // The return value should be on top of the stack. However, when aborting
1057 // it may not be there and ec_frame_idx is the top of the stack.
1058 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001059 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001060 ret_idx = 0;
1061
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001062 if (ectx->ec_outer_ref != NULL)
1063 {
1064 if (ectx->ec_outer_ref->or_outer_allocated)
1065 vim_free(ectx->ec_outer_ref->or_outer);
1066 partial_unref(ectx->ec_outer_ref->or_partial);
1067 vim_free(ectx->ec_outer_ref);
1068 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001069
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001070 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001071 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001072 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1073 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001074 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1075 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001076 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001077 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001078 floc = (void *)STACK_TV(ectx->ec_frame_idx
1079 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001080 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001081 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1082 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001083
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001084 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001085 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001086 else
1087 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001088 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001089 vim_free(floc);
1090 }
1091
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001092 if (ret_idx > 0)
1093 {
1094 // Reset the stack to the position before the call, with a spot for the
1095 // return value, moved there from above the frame.
1096 ectx->ec_stack.ga_len = top + 1;
1097 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1098 }
1099 else
1100 // Reset the stack to the position before the call.
1101 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001102
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001103 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001104 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001105 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106}
1107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108/*
1109 * Prepare arguments and rettv for calling a builtin or user function.
1110 */
1111 static int
1112call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1113{
1114 int idx;
1115 typval_T *tv;
1116
1117 // Move arguments from bottom of the stack to argvars[] and add terminator.
1118 for (idx = 0; idx < argcount; ++idx)
1119 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1120 argvars[argcount].v_type = VAR_UNKNOWN;
1121
1122 // Result replaces the arguments on the stack.
1123 if (argcount > 0)
1124 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001125 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 return FAIL;
1127 else
1128 ++ectx->ec_stack.ga_len;
1129
1130 // Default return value is zero.
1131 tv = STACK_TV_BOT(-1);
1132 tv->v_type = VAR_NUMBER;
1133 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001134 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135
1136 return OK;
1137}
1138
1139/*
1140 * Call a builtin function by index.
1141 */
1142 static int
1143call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1144{
1145 typval_T argvars[MAX_FUNC_ARGS];
1146 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001147 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001148 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001149 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150
1151 if (call_prepare(argcount, argvars, ectx) == FAIL)
1152 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001153 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001155 // Call the builtin function. Set "current_ectx" so that when it
1156 // recursively invokes call_def_function() a closure context can be set.
1157 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001159 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001160 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161
1162 // Clear the arguments.
1163 for (idx = 0; idx < argcount; ++idx)
1164 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001165
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001166 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001167 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 return OK;
1169}
1170
1171/*
1172 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001173 * If the function is compiled this will add a stack frame and set the
1174 * instruction pointer at the start of the function.
1175 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001176 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001177 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 */
1179 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001180call_ufunc(
1181 ufunc_T *ufunc,
1182 partial_T *pt,
1183 int argcount,
1184 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001185 isn_T *iptr,
1186 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187{
1188 typval_T argvars[MAX_FUNC_ARGS];
1189 funcexe_T funcexe;
1190 int error;
1191 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001192 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001193 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194
Bram Moolenaare99d4222021-06-13 14:01:26 +02001195 if (func_needs_compiling(ufunc, compile_type)
1196 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1197 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001198 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001199 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001200 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001201 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001202 if (error != FCERR_UNKNOWN)
1203 {
1204 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001205 semsg(_(e_too_many_arguments_for_function_str),
1206 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001207 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001208 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001209 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001210 return FAIL;
1211 }
1212
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001213 // The function has been compiled, can call it quickly. For a function
1214 // that was defined later: we can call it directly next time.
1215 if (iptr != NULL)
1216 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001217 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001218 iptr->isn_type = ISN_DCALL;
1219 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1220 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1221 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001222 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224
1225 if (call_prepare(argcount, argvars, ectx) == FAIL)
1226 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001227 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001228 funcexe.fe_evaluate = TRUE;
1229 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230
1231 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001233 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234
1235 // Clear the arguments.
1236 for (idx = 0; idx < argcount; ++idx)
1237 clear_tv(&argvars[idx]);
1238
1239 if (error != FCERR_NONE)
1240 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001241 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 return FAIL;
1243 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001244 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001245 // Error other than from calling the function itself.
1246 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 return OK;
1248}
1249
1250/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001251 * If command modifiers were applied restore them.
1252 */
1253 static void
1254may_restore_cmdmod(funclocal_T *funclocal)
1255{
1256 if (funclocal->floc_restore_cmdmod)
1257 {
1258 cmdmod.cmod_filter_regmatch.regprog = NULL;
1259 undo_cmdmod(&cmdmod);
1260 cmdmod = funclocal->floc_save_cmdmod;
1261 funclocal->floc_restore_cmdmod = FALSE;
1262 }
1263}
1264
1265/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001266 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1267 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001268 */
1269 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001270vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001271{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001272 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001273}
1274
1275/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 * Execute a function by "name".
1277 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001278 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 * Returns FAIL if not found without an error message.
1280 */
1281 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001282call_by_name(
1283 char_u *name,
1284 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001285 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001286 isn_T *iptr,
1287 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288{
1289 ufunc_T *ufunc;
1290
1291 if (builtin_function(name, -1))
1292 {
1293 int func_idx = find_internal_func(name);
1294
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001295 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001297 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 return FAIL;
1299 return call_bfunc(func_idx, argcount, ectx);
1300 }
1301
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001302 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001303
1304 if (ufunc == NULL)
1305 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001306 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001307
1308 if (script_autoload(name, TRUE))
1309 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001310 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001311
1312 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001313 return FAIL; // bail out if loading the script caused an error
1314 }
1315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001317 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001318 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001319 {
1320 int i;
1321 typval_T *argv = STACK_TV_BOT(0) - argcount;
1322
1323 // The function can change at runtime, check that the argument
1324 // types are correct.
1325 for (i = 0; i < argcount; ++i)
1326 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001327 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001328
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001329 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001330 type = ufunc->uf_arg_types[i];
1331 else if (ufunc->uf_va_type != NULL)
1332 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001333 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001334 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001335 return FAIL;
1336 }
1337 }
1338
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001339 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341
1342 return FAIL;
1343}
1344
1345 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001346call_partial(
1347 typval_T *tv,
1348 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001349 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001351 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001352 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001354 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001355 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356
1357 if (tv->v_type == VAR_PARTIAL)
1358 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001359 partial_T *pt = tv->vval.v_partial;
1360 int i;
1361
1362 if (pt->pt_argc > 0)
1363 {
1364 // Make space for arguments from the partial, shift the "argcount"
1365 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001366 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001367 return FAIL;
1368 for (i = 1; i <= argcount; ++i)
1369 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1370 ectx->ec_stack.ga_len += pt->pt_argc;
1371 argcount += pt->pt_argc;
1372
1373 // copy the arguments from the partial onto the stack
1374 for (i = 0; i < pt->pt_argc; ++i)
1375 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1376 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001377 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378
1379 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001380 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 name = pt->pt_name;
1383 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001384 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001386 if (name != NULL)
1387 {
1388 char_u fname_buf[FLEN_FIXED + 1];
1389 char_u *tofree = NULL;
1390 int error = FCERR_NONE;
1391 char_u *fname;
1392
1393 // May need to translate <SNR>123_ to K_SNR.
1394 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1395 if (error != FCERR_NONE)
1396 res = FAIL;
1397 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001398 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001399 vim_free(tofree);
1400 }
1401
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001402 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 {
1404 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001405 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001406 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 return FAIL;
1408 }
1409 return OK;
1410}
1411
1412/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001413 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1414 * TRUE.
1415 */
1416 static int
1417error_if_locked(int lock, char *error)
1418{
1419 if (lock & (VAR_LOCKED | VAR_FIXED))
1420 {
1421 emsg(_(error));
1422 return TRUE;
1423 }
1424 return FALSE;
1425}
1426
1427/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001428 * Give an error if "tv" is not a number and return FAIL.
1429 */
1430 static int
1431check_for_number(typval_T *tv)
1432{
1433 if (tv->v_type != VAR_NUMBER)
1434 {
1435 semsg(_(e_expected_str_but_got_str),
1436 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1437 return FAIL;
1438 }
1439 return OK;
1440}
1441
1442/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001443 * Store "tv" in variable "name".
1444 * This is for s: and g: variables.
1445 */
1446 static void
1447store_var(char_u *name, typval_T *tv)
1448{
1449 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001450 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001451
Bram Moolenaard877a572021-04-01 19:42:48 +02001452 if (tv->v_lock)
1453 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001454 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001455 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001456 restore_funccal();
1457}
1458
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001459/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001460 * Convert "tv" to a string.
1461 * Return FAIL if not allowed.
1462 */
1463 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001464do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001465{
1466 if (tv->v_type != VAR_STRING)
1467 {
1468 char_u *str;
1469
1470 if (is_2string_any)
1471 {
1472 switch (tv->v_type)
1473 {
1474 case VAR_SPECIAL:
1475 case VAR_BOOL:
1476 case VAR_NUMBER:
1477 case VAR_FLOAT:
1478 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001479
1480 case VAR_LIST:
1481 if (tolerant)
1482 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001483 char_u *s, *e, *p;
1484 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001485
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001486 ga_init2(&ga, sizeof(char_u *), 1);
1487
1488 // Convert to NL separated items, then
1489 // escape the items and replace the NL with
1490 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001491 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001492 if (str == NULL)
1493 return FAIL;
1494 s = str;
1495 while ((e = vim_strchr(s, '\n')) != NULL)
1496 {
1497 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001498 p = vim_strsave_fnameescape(s,
1499 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001500 if (p != NULL)
1501 {
1502 ga_concat(&ga, p);
1503 ga_concat(&ga, (char_u *)" ");
1504 vim_free(p);
1505 }
1506 s = e + 1;
1507 }
1508 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001509 clear_tv(tv);
1510 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001511 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001512 return OK;
1513 }
1514 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001515 default: to_string_error(tv->v_type);
1516 return FAIL;
1517 }
1518 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001519 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001520 clear_tv(tv);
1521 tv->v_type = VAR_STRING;
1522 tv->vval.v_string = str;
1523 }
1524 return OK;
1525}
1526
1527/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001528 * When the value of "sv" is a null list of dict, allocate it.
1529 */
1530 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001531allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001532{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001533 typval_T *tv = sv->sv_tv;
1534
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001535 switch (tv->v_type)
1536 {
1537 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001538 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001539 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001540 break;
1541 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001542 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001543 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001544 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001545 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001546 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001547 (void)rettv_blob_alloc(tv);
1548 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001549 default:
1550 break;
1551 }
1552}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001553
Bram Moolenaare7525c52021-01-09 13:20:37 +01001554/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001555 * Return the character "str[index]" where "index" is the character index,
1556 * including composing characters.
1557 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001558 */
1559 char_u *
1560char_from_string(char_u *str, varnumber_T index)
1561{
1562 size_t nbyte = 0;
1563 varnumber_T nchar = index;
1564 size_t slen;
1565
1566 if (str == NULL)
1567 return NULL;
1568 slen = STRLEN(str);
1569
Bram Moolenaarff871402021-03-26 13:34:05 +01001570 // Do the same as for a list: a negative index counts from the end.
1571 // Optimization to check the first byte to be below 0x80 (and no composing
1572 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001573 if (index < 0)
1574 {
1575 int clen = 0;
1576
1577 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001578 {
1579 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1580 ++nbyte;
1581 else if (enc_utf8)
1582 nbyte += utfc_ptr2len(str + nbyte);
1583 else
1584 nbyte += mb_ptr2len(str + nbyte);
1585 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001586 nchar = clen + index;
1587 if (nchar < 0)
1588 // unlike list: index out of range results in empty string
1589 return NULL;
1590 }
1591
1592 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001593 {
1594 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1595 ++nbyte;
1596 else if (enc_utf8)
1597 nbyte += utfc_ptr2len(str + nbyte);
1598 else
1599 nbyte += mb_ptr2len(str + nbyte);
1600 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001601 if (nbyte >= slen)
1602 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001603 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001604}
1605
1606/*
1607 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001608 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001609 * If going over the end return "str_len".
1610 * If "idx" is negative count from the end, -1 is the last character.
1611 * When going over the start return -1.
1612 */
1613 static long
1614char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1615{
1616 varnumber_T nchar = idx;
1617 size_t nbyte = 0;
1618
1619 if (nchar >= 0)
1620 {
1621 while (nchar > 0 && nbyte < str_len)
1622 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001623 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001624 --nchar;
1625 }
1626 }
1627 else
1628 {
1629 nbyte = str_len;
1630 while (nchar < 0 && nbyte > 0)
1631 {
1632 --nbyte;
1633 nbyte -= mb_head_off(str, str + nbyte);
1634 ++nchar;
1635 }
1636 if (nchar < 0)
1637 return -1;
1638 }
1639 return (long)nbyte;
1640}
1641
1642/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001643 * Return the slice "str[first : last]" using character indexes. Composing
1644 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001645 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001646 * Return NULL when the result is empty.
1647 */
1648 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001649string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001650{
1651 long start_byte, end_byte;
1652 size_t slen;
1653
1654 if (str == NULL)
1655 return NULL;
1656 slen = STRLEN(str);
1657 start_byte = char_idx2byte(str, slen, first);
1658 if (start_byte < 0)
1659 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001660 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001661 end_byte = (long)slen;
1662 else
1663 {
1664 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001665 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001666 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001667 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001668 }
1669
1670 if (start_byte >= (long)slen || end_byte <= start_byte)
1671 return NULL;
1672 return vim_strnsave(str + start_byte, end_byte - start_byte);
1673}
1674
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001675/*
1676 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1677 * When "dfunc_idx" is negative don't give an error.
1678 * Returns NULL for an error.
1679 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001680 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001681get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001682{
1683 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001684 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1685 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001686 svar_T *sv;
1687
1688 if (sref->sref_seq != si->sn_script_seq)
1689 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001690 // The script was reloaded after the function was compiled, the
1691 // script_idx may not be valid.
1692 if (dfunc != NULL)
1693 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1694 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001695 return NULL;
1696 }
1697 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001698 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001699 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001700 if (dfunc != NULL)
1701 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001702 return NULL;
1703 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001704
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001705 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1706 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001707 {
1708 if (dfunc != NULL)
1709 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1710 return NULL;
1711 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001712 return sv;
1713}
1714
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001715/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001716 * Function passed to do_cmdline() for splitting a script joined by NL
1717 * characters.
1718 */
1719 static char_u *
1720get_split_sourceline(
1721 int c UNUSED,
1722 void *cookie,
1723 int indent UNUSED,
1724 getline_opt_T options UNUSED)
1725{
1726 source_cookie_T *sp = (source_cookie_T *)cookie;
1727 char_u *p;
1728 char_u *line;
1729
Bram Moolenaar20677332021-06-06 17:02:53 +02001730 p = vim_strchr(sp->nextline, '\n');
1731 if (p == NULL)
1732 {
1733 line = vim_strsave(sp->nextline);
1734 sp->nextline += STRLEN(sp->nextline);
1735 }
1736 else
1737 {
1738 line = vim_strnsave(sp->nextline, p - sp->nextline);
1739 sp->nextline = p + 1;
1740 }
1741 return line;
1742}
1743
1744/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 * Execute a function by "name".
1746 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001747 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 */
1749 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001750call_eval_func(
1751 char_u *name,
1752 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001753 ectx_T *ectx,
1754 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755{
Bram Moolenaared677f52020-08-12 16:38:10 +02001756 int called_emsg_before = called_emsg;
1757 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001759 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001760 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001762 dictitem_T *v;
1763
1764 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001765 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1766 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001767 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001768 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001769 return FAIL;
1770 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001771 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001773 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774}
1775
1776/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001777 * When a function reference is used, fill a partial with the information
1778 * needed, especially when it is used as a closure.
1779 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001780 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001781fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1782{
1783 pt->pt_func = ufunc;
1784 pt->pt_refcount = 1;
1785
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001786 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001787 {
1788 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1789 + ectx->ec_dfunc_idx;
1790
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001791 // The closure may need to find arguments and local variables in the
1792 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001793 pt->pt_outer.out_stack = &ectx->ec_stack;
1794 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001795 if (ectx->ec_outer_ref != NULL)
1796 {
1797 // The current context already has a context, link to that one.
1798 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1799 if (ectx->ec_outer_ref->or_partial != NULL)
1800 {
1801 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1802 ++pt->pt_outer.out_up_partial->pt_refcount;
1803 }
1804 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001805
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001806 // If this function returns and the closure is still being used, we
1807 // need to make a copy of the context (arguments and local variables).
1808 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001809 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001810 {
1811 vim_free(pt);
1812 return FAIL;
1813 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001814 // Extra variable keeps the count of closures created in the current
1815 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001816 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1817 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1818
1819 ((partial_T **)ectx->ec_funcrefs.ga_data)
1820 [ectx->ec_funcrefs.ga_len] = pt;
1821 ++pt->pt_refcount;
1822 ++ectx->ec_funcrefs.ga_len;
1823 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001824 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001825 return OK;
1826}
1827
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001828/*
1829 * Execute iptr->isn_arg.string as an Ex command.
1830 */
1831 static int
1832exec_command(isn_T *iptr)
1833{
1834 source_cookie_T cookie;
1835
1836 SOURCING_LNUM = iptr->isn_lnum;
1837 // Pass getsourceline to get an error for a missing ":end"
1838 // command.
1839 CLEAR_FIELD(cookie);
1840 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1841 if (do_cmdline(iptr->isn_arg.string,
1842 getsourceline, &cookie,
1843 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1844 || did_emsg)
1845 return FAIL;
1846 return OK;
1847}
1848
Bram Moolenaar89445512022-04-14 12:58:23 +01001849/*
1850 * If script "sid" is not loaded yet then load it now.
1851 * Caller must make sure "sid" is a valid script ID.
1852 * "loaded" is set to TRUE if the script had to be loaded.
1853 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1854 */
1855 int
1856may_load_script(int sid, int *loaded)
1857{
1858 scriptitem_T *si = SCRIPT_ITEM(sid);
1859
1860 if (si->sn_state == SN_STATE_NOT_LOADED)
1861 {
1862 *loaded = TRUE;
1863 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1864 {
1865 semsg(_(e_cant_open_file_str), si->sn_name);
1866 return FAIL;
1867 }
1868 }
1869 return OK;
1870}
1871
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001872// used for v_instr of typval of VAR_INSTR
1873struct instr_S {
1874 ectx_T *instr_ectx;
1875 isn_T *instr_instr;
1876};
1877
Bram Moolenaar4c137212021-04-19 16:48:48 +02001878// used for substitute_instr
1879typedef struct subs_expr_S {
1880 ectx_T *subs_ectx;
1881 isn_T *subs_instr;
1882 int subs_status;
1883} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001885// Set when calling do_debug().
1886static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001887static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001888
1889/*
1890 * When debugging lookup "name" and return the typeval.
1891 * When not found return NULL.
1892 */
1893 typval_T *
1894lookup_debug_var(char_u *name)
1895{
1896 int idx;
1897 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001898 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001899 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001900 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001901
1902 if (ectx == NULL)
1903 return NULL;
1904 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1905
1906 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001907 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001908 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001909 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1910
1911 // the variable name may be NULL when not available in this block
1912 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001913 return STACK_TV_VAR(idx);
1914 }
1915
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001916 // Go through argument names.
1917 ufunc = dfunc->df_ufunc;
1918 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1919 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1920 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1921 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1922 - varargs_off + idx);
1923 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1924 return STACK_TV(ectx->ec_frame_idx - 1);
1925
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001926 return NULL;
1927}
1928
Bram Moolenaar26a44842021-09-02 18:49:06 +02001929/*
1930 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1931 * breakpoint was set in that function or when there is any expression.
1932 */
1933 int
1934may_break_in_function(ufunc_T *ufunc)
1935{
1936 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1937}
1938
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001939 static void
1940handle_debug(isn_T *iptr, ectx_T *ectx)
1941{
1942 char_u *line;
1943 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1944 + ectx->ec_dfunc_idx)->df_ufunc;
1945 isn_T *ni;
1946 int end_lnum = iptr->isn_lnum;
1947 garray_T ga;
1948 int lnum;
1949
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001950 if (ex_nesting_level > debug_break_level)
1951 {
1952 linenr_T breakpoint;
1953
Bram Moolenaar26a44842021-09-02 18:49:06 +02001954 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001955 return;
1956
1957 // check for the next breakpoint if needed
1958 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001959 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001960 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1961 return;
1962 }
1963
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001964 SOURCING_LNUM = iptr->isn_lnum;
1965 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001966 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001967
1968 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1969 if (ni->isn_type == ISN_DEBUG
1970 || ni->isn_type == ISN_RETURN
1971 || ni->isn_type == ISN_RETURN_VOID)
1972 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001973 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001974 break;
1975 }
1976
1977 if (end_lnum > iptr->isn_lnum)
1978 {
1979 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001980 for (lnum = iptr->isn_lnum; lnum < end_lnum
1981 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001982 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001983 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001984
Bram Moolenaar303215d2021-07-07 20:10:43 +02001985 if (p == NULL)
1986 continue; // left over from continuation line
1987 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001988 if (*p == '#')
1989 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001990 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001991 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1992 if (STRNCMP(p, "def ", 4) == 0)
1993 break;
1994 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001995 line = ga_concat_strings(&ga, " ");
1996 vim_free(ga.ga_data);
1997 }
1998 else
1999 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002000
Dominique Pelle6e969552021-06-17 13:53:41 +02002001 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002002 debug_context = NULL;
2003
2004 if (end_lnum > iptr->isn_lnum)
2005 vim_free(line);
2006}
2007
Bram Moolenaar4c137212021-04-19 16:48:48 +02002008/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002009 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002010 * Returns OK, FAIL or NOTDONE (uncatchable error).
2011 */
2012 static int
2013execute_storeindex(isn_T *iptr, ectx_T *ectx)
2014{
2015 vartype_T dest_type = iptr->isn_arg.vartype;
2016 typval_T *tv;
2017 typval_T *tv_idx = STACK_TV_BOT(-2);
2018 typval_T *tv_dest = STACK_TV_BOT(-1);
2019 int status = OK;
2020
2021 // Stack contains:
2022 // -3 value to be stored
2023 // -2 index
2024 // -1 dict or list
2025 tv = STACK_TV_BOT(-3);
2026 SOURCING_LNUM = iptr->isn_lnum;
2027 if (dest_type == VAR_ANY)
2028 {
2029 dest_type = tv_dest->v_type;
2030 if (dest_type == VAR_DICT)
2031 status = do_2string(tv_idx, TRUE, FALSE);
2032 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2033 {
2034 emsg(_(e_number_expected));
2035 status = FAIL;
2036 }
2037 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002038
2039 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002040 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002041 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002042 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002043 long lidx = (long)tv_idx->vval.v_number;
2044 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002045
Bram Moolenaare08be092022-02-17 13:08:26 +00002046 if (list == NULL)
2047 {
2048 emsg(_(e_list_not_set));
2049 return FAIL;
2050 }
2051 if (lidx < 0 && list->lv_len + lidx >= 0)
2052 // negative index is relative to the end
2053 lidx = list->lv_len + lidx;
2054 if (lidx < 0 || lidx > list->lv_len)
2055 {
2056 semsg(_(e_list_index_out_of_range_nr), lidx);
2057 return FAIL;
2058 }
2059 if (lidx < list->lv_len)
2060 {
2061 listitem_T *li = list_find(list, lidx);
2062
2063 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002064 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002065 return FAIL;
2066 // overwrite existing list item
2067 clear_tv(&li->li_tv);
2068 li->li_tv = *tv;
2069 }
2070 else
2071 {
2072 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2073 return FAIL;
2074 // append to list, only fails when out of memory
2075 if (list_append_tv(list, tv) == FAIL)
2076 return NOTDONE;
2077 clear_tv(tv);
2078 }
2079 }
2080 else if (dest_type == VAR_DICT)
2081 {
2082 char_u *key = tv_idx->vval.v_string;
2083 dict_T *dict = tv_dest->vval.v_dict;
2084 dictitem_T *di;
2085
2086 SOURCING_LNUM = iptr->isn_lnum;
2087 if (dict == NULL)
2088 {
2089 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002090 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002091 }
2092 if (key == NULL)
2093 key = (char_u *)"";
2094 di = dict_find(dict, key, -1);
2095 if (di != NULL)
2096 {
2097 if (error_if_locked(di->di_tv.v_lock,
2098 e_cannot_change_dict_item))
2099 return FAIL;
2100 // overwrite existing value
2101 clear_tv(&di->di_tv);
2102 di->di_tv = *tv;
2103 }
2104 else
2105 {
2106 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2107 return FAIL;
2108 // add to dict, only fails when out of memory
2109 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2110 return NOTDONE;
2111 clear_tv(tv);
2112 }
2113 }
2114 else if (dest_type == VAR_BLOB)
2115 {
2116 long lidx = (long)tv_idx->vval.v_number;
2117 blob_T *blob = tv_dest->vval.v_blob;
2118 varnumber_T nr;
2119 int error = FALSE;
2120 int len;
2121
2122 if (blob == NULL)
2123 {
2124 emsg(_(e_blob_not_set));
2125 return FAIL;
2126 }
2127 len = blob_len(blob);
2128 if (lidx < 0 && len + lidx >= 0)
2129 // negative index is relative to the end
2130 lidx = len + lidx;
2131
2132 // Can add one byte at the end.
2133 if (lidx < 0 || lidx > len)
2134 {
2135 semsg(_(e_blob_index_out_of_range_nr), lidx);
2136 return FAIL;
2137 }
2138 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2139 return FAIL;
2140 nr = tv_get_number_chk(tv, &error);
2141 if (error)
2142 return FAIL;
2143 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002144 }
2145 else
2146 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002147 status = FAIL;
2148 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002149 }
2150 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002151
2152 clear_tv(tv_idx);
2153 clear_tv(tv_dest);
2154 ectx->ec_stack.ga_len -= 3;
2155 if (status == FAIL)
2156 {
2157 clear_tv(tv);
2158 return FAIL;
2159 }
2160 return OK;
2161}
2162
2163/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002164 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002165 */
2166 static int
2167execute_storerange(isn_T *iptr, ectx_T *ectx)
2168{
2169 typval_T *tv;
2170 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2171 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2172 typval_T *tv_dest = STACK_TV_BOT(-1);
2173 int status = OK;
2174
2175 // Stack contains:
2176 // -4 value to be stored
2177 // -3 first index or "none"
2178 // -2 second index or "none"
2179 // -1 destination list or blob
2180 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002181 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002182 if (tv_dest->v_type == VAR_LIST)
2183 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002184 long n1;
2185 long n2;
2186 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002187
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002188 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2189 if (tv_idx2->v_type == VAR_SPECIAL
2190 && tv_idx2->vval.v_number == VVAL_NONE)
2191 n2 = list_len(tv_dest->vval.v_list) - 1;
2192 else
2193 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2194
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002195 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002196 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002197 status = FAIL;
2198 else
2199 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002200 status = check_range_index_two(tv_dest->vval.v_list,
2201 &n1, li1, &n2, FALSE);
2202 if (status != FAIL)
2203 status = list_assign_range(
2204 tv_dest->vval.v_list,
2205 tv->vval.v_list,
2206 n1,
2207 n2,
2208 tv_idx2->v_type == VAR_SPECIAL,
2209 (char_u *)"=",
2210 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002211 }
2212 }
2213 else if (tv_dest->v_type == VAR_BLOB)
2214 {
2215 varnumber_T n1;
2216 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002217 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002218
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002219 n1 = tv_get_number_chk(tv_idx1, NULL);
2220 if (tv_idx2->v_type == VAR_SPECIAL
2221 && tv_idx2->vval.v_number == VVAL_NONE)
2222 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2223 else
2224 n2 = tv_get_number_chk(tv_idx2, NULL);
2225 bloblen = blob_len(tv_dest->vval.v_blob);
2226
2227 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2228 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002229 status = FAIL;
2230 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002231 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002232 }
2233 else
2234 {
2235 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002236 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002237 }
2238
2239 clear_tv(tv_idx1);
2240 clear_tv(tv_idx2);
2241 clear_tv(tv_dest);
2242 ectx->ec_stack.ga_len -= 4;
2243 clear_tv(tv);
2244
2245 return status;
2246}
2247
2248/*
2249 * Unlet item in list or dict variable.
2250 */
2251 static int
2252execute_unletindex(isn_T *iptr, ectx_T *ectx)
2253{
2254 typval_T *tv_idx = STACK_TV_BOT(-2);
2255 typval_T *tv_dest = STACK_TV_BOT(-1);
2256 int status = OK;
2257
2258 // Stack contains:
2259 // -2 index
2260 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002261 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002262 if (tv_dest->v_type == VAR_DICT)
2263 {
2264 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002265 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002266 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002267 semsg(_(e_expected_str_but_got_str),
2268 vartype_name(VAR_STRING),
2269 vartype_name(tv_idx->v_type));
2270 status = FAIL;
2271 }
2272 else
2273 {
2274 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002275 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002276 dictitem_T *di = NULL;
2277
2278 if (d != NULL && value_check_lock(
2279 d->dv_lock, NULL, FALSE))
2280 status = FAIL;
2281 else
2282 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002283 if (tv_idx->v_type == VAR_STRING)
2284 {
2285 key = tv_idx->vval.v_string;
2286 if (key == NULL)
2287 key = (char_u *)"";
2288 }
2289 else
2290 {
2291 key = tv_get_string(tv_idx);
2292 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002293 if (d != NULL)
2294 di = dict_find(d, key, (int)STRLEN(key));
2295 if (di == NULL)
2296 {
2297 // NULL dict is equivalent to empty dict
2298 semsg(_(e_key_not_present_in_dictionary),
2299 key);
2300 status = FAIL;
2301 }
2302 else if (var_check_fixed(di->di_flags,
2303 NULL, FALSE)
2304 || var_check_ro(di->di_flags,
2305 NULL, FALSE))
2306 status = FAIL;
2307 else
2308 dictitem_remove(d, di);
2309 }
2310 }
2311 }
2312 else if (tv_dest->v_type == VAR_LIST)
2313 {
2314 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002315 if (check_for_number(tv_idx) == FAIL)
2316 {
2317 status = FAIL;
2318 }
2319 else
2320 {
2321 list_T *l = tv_dest->vval.v_list;
2322 long n = (long)tv_idx->vval.v_number;
2323
2324 if (l != NULL && value_check_lock(
2325 l->lv_lock, NULL, FALSE))
2326 status = FAIL;
2327 else
2328 {
2329 listitem_T *li = list_find(l, n);
2330
2331 if (li == NULL)
2332 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002333 semsg(_(e_list_index_out_of_range_nr), n);
2334 status = FAIL;
2335 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002336 else
2337 listitem_remove(l, li);
2338 }
2339 }
2340 }
2341 else
2342 {
2343 status = FAIL;
2344 semsg(_(e_cannot_index_str),
2345 vartype_name(tv_dest->v_type));
2346 }
2347
2348 clear_tv(tv_idx);
2349 clear_tv(tv_dest);
2350 ectx->ec_stack.ga_len -= 2;
2351
2352 return status;
2353}
2354
2355/*
2356 * Unlet a range of items in a list variable.
2357 */
2358 static int
2359execute_unletrange(isn_T *iptr, ectx_T *ectx)
2360{
2361 // Stack contains:
2362 // -3 index1
2363 // -2 index2
2364 // -1 dict or list
2365 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2366 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2367 typval_T *tv_dest = STACK_TV_BOT(-1);
2368 int status = OK;
2369
2370 if (tv_dest->v_type == VAR_LIST)
2371 {
2372 // indexes must be a number
2373 SOURCING_LNUM = iptr->isn_lnum;
2374 if (check_for_number(tv_idx1) == FAIL
2375 || (tv_idx2->v_type != VAR_SPECIAL
2376 && check_for_number(tv_idx2) == FAIL))
2377 {
2378 status = FAIL;
2379 }
2380 else
2381 {
2382 list_T *l = tv_dest->vval.v_list;
2383 long n1 = (long)tv_idx1->vval.v_number;
2384 long n2 = tv_idx2->v_type == VAR_SPECIAL
2385 ? 0 : (long)tv_idx2->vval.v_number;
2386 listitem_T *li;
2387
2388 li = list_find_index(l, &n1);
2389 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002390 {
2391 semsg(_(e_list_index_out_of_range_nr),
2392 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002393 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002394 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002395 else
2396 {
2397 if (n1 < 0)
2398 n1 = list_idx_of_item(l, li);
2399 if (n2 < 0)
2400 {
2401 listitem_T *li2 = list_find(l, n2);
2402
2403 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002404 {
2405 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002406 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002407 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002408 else
2409 n2 = list_idx_of_item(l, li2);
2410 }
2411 if (status != FAIL
2412 && tv_idx2->v_type != VAR_SPECIAL
2413 && n2 < n1)
2414 {
2415 semsg(_(e_list_index_out_of_range_nr), n2);
2416 status = FAIL;
2417 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002418 if (status != FAIL)
2419 list_unlet_range(l, li, n1,
2420 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002421 }
2422 }
2423 }
2424 else
2425 {
2426 status = FAIL;
2427 SOURCING_LNUM = iptr->isn_lnum;
2428 semsg(_(e_cannot_index_str),
2429 vartype_name(tv_dest->v_type));
2430 }
2431
2432 clear_tv(tv_idx1);
2433 clear_tv(tv_idx2);
2434 clear_tv(tv_dest);
2435 ectx->ec_stack.ga_len -= 3;
2436
2437 return status;
2438}
2439
2440/*
2441 * Top of a for loop.
2442 */
2443 static int
2444execute_for(isn_T *iptr, ectx_T *ectx)
2445{
2446 typval_T *tv;
2447 typval_T *ltv = STACK_TV_BOT(-1);
2448 typval_T *idxtv =
2449 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2450
2451 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2452 return FAIL;
2453 if (ltv->v_type == VAR_LIST)
2454 {
2455 list_T *list = ltv->vval.v_list;
2456
2457 // push the next item from the list
2458 ++idxtv->vval.v_number;
2459 if (list == NULL
2460 || idxtv->vval.v_number >= list->lv_len)
2461 {
2462 // past the end of the list, jump to "endfor"
2463 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2464 may_restore_cmdmod(&ectx->ec_funclocal);
2465 }
2466 else if (list->lv_first == &range_list_item)
2467 {
2468 // non-materialized range() list
2469 tv = STACK_TV_BOT(0);
2470 tv->v_type = VAR_NUMBER;
2471 tv->v_lock = 0;
2472 tv->vval.v_number = list_find_nr(
2473 list, idxtv->vval.v_number, NULL);
2474 ++ectx->ec_stack.ga_len;
2475 }
2476 else
2477 {
2478 listitem_T *li = list_find(list,
2479 idxtv->vval.v_number);
2480
2481 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2482 ++ectx->ec_stack.ga_len;
2483 }
2484 }
2485 else if (ltv->v_type == VAR_STRING)
2486 {
2487 char_u *str = ltv->vval.v_string;
2488
2489 // The index is for the last byte of the previous
2490 // character.
2491 ++idxtv->vval.v_number;
2492 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2493 {
2494 // past the end of the string, jump to "endfor"
2495 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2496 may_restore_cmdmod(&ectx->ec_funclocal);
2497 }
2498 else
2499 {
2500 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2501
2502 // Push the next character from the string.
2503 tv = STACK_TV_BOT(0);
2504 tv->v_type = VAR_STRING;
2505 tv->vval.v_string = vim_strnsave(
2506 str + idxtv->vval.v_number, clen);
2507 ++ectx->ec_stack.ga_len;
2508 idxtv->vval.v_number += clen - 1;
2509 }
2510 }
2511 else if (ltv->v_type == VAR_BLOB)
2512 {
2513 blob_T *blob = ltv->vval.v_blob;
2514
2515 // When we get here the first time make a copy of the
2516 // blob, so that the iteration still works when it is
2517 // changed.
2518 if (idxtv->vval.v_number == -1 && blob != NULL)
2519 {
2520 blob_copy(blob, ltv);
2521 blob_unref(blob);
2522 blob = ltv->vval.v_blob;
2523 }
2524
2525 // The index is for the previous byte.
2526 ++idxtv->vval.v_number;
2527 if (blob == NULL
2528 || idxtv->vval.v_number >= blob_len(blob))
2529 {
2530 // past the end of the blob, jump to "endfor"
2531 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2532 may_restore_cmdmod(&ectx->ec_funclocal);
2533 }
2534 else
2535 {
2536 // Push the next byte from the blob.
2537 tv = STACK_TV_BOT(0);
2538 tv->v_type = VAR_NUMBER;
2539 tv->vval.v_number = blob_get(blob,
2540 idxtv->vval.v_number);
2541 ++ectx->ec_stack.ga_len;
2542 }
2543 }
2544 else
2545 {
2546 semsg(_(e_for_loop_on_str_not_supported),
2547 vartype_name(ltv->v_type));
2548 return FAIL;
2549 }
2550 return OK;
2551}
2552
2553/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002554 * Load instruction for w:/b:/g:/t: variable.
2555 * "isn_type" is used instead of "iptr->isn_type".
2556 */
2557 static int
2558load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2559{
2560 dictitem_T *di = NULL;
2561 hashtab_T *ht = NULL;
2562 char namespace;
2563
2564 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2565 return NOTDONE;
2566 switch (isn_type)
2567 {
2568 case ISN_LOADG:
2569 ht = get_globvar_ht();
2570 namespace = 'g';
2571 break;
2572 case ISN_LOADB:
2573 ht = &curbuf->b_vars->dv_hashtab;
2574 namespace = 'b';
2575 break;
2576 case ISN_LOADW:
2577 ht = &curwin->w_vars->dv_hashtab;
2578 namespace = 'w';
2579 break;
2580 case ISN_LOADT:
2581 ht = &curtab->tp_vars->dv_hashtab;
2582 namespace = 't';
2583 break;
2584 default: // Cannot reach here
2585 return NOTDONE;
2586 }
2587 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2588
Bram Moolenaar06b77222022-01-25 15:51:56 +00002589 if (di == NULL)
2590 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002591 if (isn_type == ISN_LOADG)
2592 {
2593 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2594
2595 // g:Something could be a function
2596 if (ufunc != NULL)
2597 {
2598 typval_T *tv = STACK_TV_BOT(0);
2599
2600 ++ectx->ec_stack.ga_len;
2601 tv->v_type = VAR_FUNC;
2602 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2603 if (tv->vval.v_string == NULL)
2604 return FAIL;
2605 STRCPY(tv->vval.v_string, "g:");
2606 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2607 return OK;
2608 }
2609 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002610 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002611 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002612 // no check if the item exists in the script but
2613 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002614 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002615 else
2616 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002617 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002618 return FAIL;
2619 }
2620 else
2621 {
2622 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2623 ++ectx->ec_stack.ga_len;
2624 }
2625 return OK;
2626}
2627
2628/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002629 * Execute instructions in execution context "ectx".
2630 * Return OK or FAIL;
2631 */
2632 static int
2633exec_instructions(ectx_T *ectx)
2634{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002635 int ret = FAIL;
2636 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002637 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002638
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002639 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002640 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002641
Bram Moolenaarff652882021-05-16 15:24:49 +02002642 // Only catch exceptions in this instruction list.
2643 ectx->ec_trylevel_at_start = trylevel;
2644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 for (;;)
2646 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002647 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002649 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002650
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002651 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002652 {
2653 line_breakcheck();
2654 breakcheck_count = 0;
2655 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002656 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002657 {
2658 // Turn CTRL-C into an exception.
2659 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002660 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002661 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002662 did_throw = TRUE;
2663 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002665 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002666 {
2667 // Turn an error message into an exception.
2668 did_emsg = FALSE;
2669 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002670 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002671 did_throw = TRUE;
2672 *msg_list = NULL;
2673 }
2674
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002675 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002677 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002678 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002679 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680
2681 // An exception jumps to the first catch, finally, or returns from
2682 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002683 while (index > 0)
2684 {
2685 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002686 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002687 break;
2688 // In the catch and finally block of this try we have to go up
2689 // one level.
2690 --index;
2691 trycmd = NULL;
2692 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002693 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002695 if (trycmd->tcd_in_catch)
2696 {
2697 // exception inside ":catch", jump to ":finally" once
2698 ectx->ec_iidx = trycmd->tcd_finally_idx;
2699 trycmd->tcd_finally_idx = 0;
2700 }
2701 else
2702 // jump to first ":catch"
2703 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002704 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002705 did_throw = FALSE; // don't come back here until :endtry
2706 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 }
2708 else
2709 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002710 // Not inside try or need to return from current functions.
2711 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002712 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002713 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002714 tv = STACK_TV_BOT(0);
2715 tv->v_type = VAR_NUMBER;
2716 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002717 ++ectx->ec_stack.ga_len;
2718 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002720 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002721 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002722 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002723 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 goto done;
2725 }
2726
Bram Moolenaar4c137212021-04-19 16:48:48 +02002727 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002728 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 }
2730 continue;
2731 }
2732
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002733 /*
2734 * Big switch on the instruction. Most compilers will be turning this
2735 * into an efficient lookup table, since the "case" values are an enum
2736 * with sequential numbers. It may look ugly, but it should be the
2737 * most efficient way.
2738 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002739 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 switch (iptr->isn_type)
2741 {
2742 // execute Ex command line
2743 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002744 if (exec_command(iptr) == FAIL)
2745 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 break;
2747
Bram Moolenaar20677332021-06-06 17:02:53 +02002748 // execute Ex command line split at NL characters.
2749 case ISN_EXEC_SPLIT:
2750 {
2751 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002752 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002753
2754 SOURCING_LNUM = iptr->isn_lnum;
2755 CLEAR_FIELD(cookie);
2756 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2757 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002758 line = get_split_sourceline(0, &cookie, 0, 0);
2759 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002760 get_split_sourceline, &cookie,
2761 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2762 == FAIL
2763 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002764 {
2765 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002766 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002767 }
2768 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002769 }
2770 break;
2771
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002772 // execute Ex command line that is only a range
2773 case ISN_EXECRANGE:
2774 {
2775 exarg_T ea;
2776 char *error = NULL;
2777
2778 CLEAR_FIELD(ea);
2779 ea.cmdidx = CMD_SIZE;
2780 ea.addr_type = ADDR_LINES;
2781 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002782 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002783 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002784 if (ea.cmd == NULL)
2785 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002786 // error is always NULL when using ADDR_LINES
2787 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002788 if (error != NULL)
2789 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002790 emsg(error);
2791 goto on_error;
2792 }
2793 }
2794 break;
2795
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002796 // Evaluate an expression with legacy syntax, push it onto the
2797 // stack.
2798 case ISN_LEGACY_EVAL:
2799 {
2800 char_u *arg = iptr->isn_arg.string;
2801 int res;
2802 int save_flags = cmdmod.cmod_flags;
2803
Bram Moolenaar35578162021-08-02 19:10:38 +02002804 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002805 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002806 tv = STACK_TV_BOT(0);
2807 init_tv(tv);
2808 cmdmod.cmod_flags |= CMOD_LEGACY;
2809 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2810 cmdmod.cmod_flags = save_flags;
2811 if (res == FAIL)
2812 goto on_error;
2813 ++ectx->ec_stack.ga_len;
2814 }
2815 break;
2816
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002817 // push typeval VAR_INSTR with instructions to be executed
2818 case ISN_INSTR:
2819 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002820 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002821 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002822 tv = STACK_TV_BOT(0);
2823 tv->vval.v_instr = ALLOC_ONE(instr_T);
2824 if (tv->vval.v_instr == NULL)
2825 goto on_error;
2826 ++ectx->ec_stack.ga_len;
2827
2828 tv->v_type = VAR_INSTR;
2829 tv->vval.v_instr->instr_ectx = ectx;
2830 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2831 }
2832 break;
2833
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002834 case ISN_SOURCE:
2835 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002836 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002837
Bram Moolenaar89445512022-04-14 12:58:23 +01002838 SOURCING_LNUM = iptr->isn_lnum;
2839 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002840 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002841 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002842 }
2843 break;
2844
Bram Moolenaar4c137212021-04-19 16:48:48 +02002845 // execute :substitute with an expression
2846 case ISN_SUBSTITUTE:
2847 {
2848 subs_T *subs = &iptr->isn_arg.subs;
2849 source_cookie_T cookie;
2850 struct subs_expr_S *save_instr = substitute_instr;
2851 struct subs_expr_S subs_instr;
2852 int res;
2853
2854 subs_instr.subs_ectx = ectx;
2855 subs_instr.subs_instr = subs->subs_instr;
2856 subs_instr.subs_status = OK;
2857 substitute_instr = &subs_instr;
2858
2859 SOURCING_LNUM = iptr->isn_lnum;
2860 // This is very much like ISN_EXEC
2861 CLEAR_FIELD(cookie);
2862 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2863 res = do_cmdline(subs->subs_cmd,
2864 getsourceline, &cookie,
2865 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2866 substitute_instr = save_instr;
2867
2868 if (res == FAIL || did_emsg
2869 || subs_instr.subs_status == FAIL)
2870 goto on_error;
2871 }
2872 break;
2873
2874 case ISN_FINISH:
2875 goto done;
2876
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002877 case ISN_REDIRSTART:
2878 // create a dummy entry for var_redir_str()
2879 if (alloc_redir_lval() == FAIL)
2880 goto on_error;
2881
2882 // The output is stored in growarray "redir_ga" until
2883 // redirection ends.
2884 init_redir_ga();
2885 redir_vname = 1;
2886 break;
2887
2888 case ISN_REDIREND:
2889 {
2890 char_u *res = get_clear_redir_ga();
2891
2892 // End redirection, put redirected text on the stack.
2893 clear_redir_lval();
2894 redir_vname = 0;
2895
Bram Moolenaar35578162021-08-02 19:10:38 +02002896 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002897 {
2898 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002899 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002900 }
2901 tv = STACK_TV_BOT(0);
2902 tv->v_type = VAR_STRING;
2903 tv->vval.v_string = res;
2904 ++ectx->ec_stack.ga_len;
2905 }
2906 break;
2907
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002908 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002909#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002910 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002911 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2912 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002913 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002914#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002915 break;
2916
2917 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002918#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002919 {
2920 exarg_T ea;
2921 int res;
2922
2923 CLEAR_FIELD(ea);
2924 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2925 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2926 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2927 --ectx->ec_stack.ga_len;
2928 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01002929 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002930 res = cexpr_core(&ea, tv);
2931 clear_tv(tv);
2932 if (res == FAIL)
2933 goto on_error;
2934 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002935#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002936 break;
2937
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002938 // execute Ex command from pieces on the stack
2939 case ISN_EXECCONCAT:
2940 {
2941 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002942 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002943 int pass;
2944 int i;
2945 char_u *cmd = NULL;
2946 char_u *str;
2947
2948 for (pass = 1; pass <= 2; ++pass)
2949 {
2950 for (i = 0; i < count; ++i)
2951 {
2952 tv = STACK_TV_BOT(i - count);
2953 str = tv->vval.v_string;
2954 if (str != NULL && *str != NUL)
2955 {
2956 if (pass == 2)
2957 STRCPY(cmd + len, str);
2958 len += STRLEN(str);
2959 }
2960 if (pass == 2)
2961 clear_tv(tv);
2962 }
2963 if (pass == 1)
2964 {
2965 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002966 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002967 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002968 len = 0;
2969 }
2970 }
2971
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002972 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002973 do_cmdline_cmd(cmd);
2974 vim_free(cmd);
2975 }
2976 break;
2977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 // execute :echo {string} ...
2979 case ISN_ECHO:
2980 {
2981 int count = iptr->isn_arg.echo.echo_count;
2982 int atstart = TRUE;
2983 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002984 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985
2986 for (idx = 0; idx < count; ++idx)
2987 {
2988 tv = STACK_TV_BOT(idx - count);
2989 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2990 &atstart, &needclr);
2991 clear_tv(tv);
2992 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002993 if (needclr)
2994 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002995 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 }
2997 break;
2998
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002999 // :execute {string} ...
3000 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003001 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003002 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003003 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003004 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003005 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003006 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003007 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003008 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003009 {
3010 int count = iptr->isn_arg.number;
3011 garray_T ga;
3012 char_u buf[NUMBUFLEN];
3013 char_u *p;
3014 int len;
3015 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003016 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003017
3018 ga_init2(&ga, 1, 80);
3019 for (idx = 0; idx < count; ++idx)
3020 {
3021 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003022 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003023 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003024 if (tv->v_type == VAR_CHANNEL
3025 || tv->v_type == VAR_JOB)
3026 {
3027 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003028 semsg(_(e_using_invalid_value_as_string_str),
3029 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003030 break;
3031 }
3032 else
3033 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003034 }
3035 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003036 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003037
3038 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003039 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003040 failed = TRUE;
3041 else
3042 {
3043 if (ga.ga_len > 0)
3044 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3045 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3046 ga.ga_len += len;
3047 }
3048 clear_tv(tv);
3049 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003050 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003051 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003052 {
3053 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003054 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003055 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003056
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003057 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003058 {
3059 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003060 {
3061 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003062 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003063 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003064 {
3065 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003066 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003067 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003068 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003069 else
3070 {
3071 msg_sb_eol();
3072 if (iptr->isn_type == ISN_ECHOMSG)
3073 {
3074 msg_attr(ga.ga_data, echo_attr);
3075 out_flush();
3076 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003077#ifdef HAS_MESSAGE_WINDOW
3078 else if (iptr->isn_type == ISN_ECHOWINDOW)
3079 {
3080 start_echowindow();
3081 msg_attr(ga.ga_data, echo_attr);
3082 end_echowindow();
3083 }
3084#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003085 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3086 {
3087 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3088 TRUE);
3089 ui_write((char_u *)"\r\n", 2, TRUE);
3090 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003091 else
3092 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003093 SOURCING_LNUM = iptr->isn_lnum;
3094 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003095 }
3096 }
3097 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003098 ga_clear(&ga);
3099 }
3100 break;
3101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 // load local variable or argument
3103 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003104 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003105 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003107 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 break;
3109
3110 // load v: variable
3111 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003112 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003113 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003115 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 break;
3117
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003118 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 case ISN_LOADSCRIPT:
3120 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003121 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 svar_T *sv;
3123
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003124 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003125 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003126 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003127 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003128 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003129 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003131 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 }
3133 break;
3134
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003135 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003137 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003139 int sid = iptr->isn_arg.loadstore.ls_sid;
3140 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003141 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 if (di == NULL)
3145 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003146 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003147 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003148 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 }
3150 else
3151 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003152 if (iptr->isn_type == ISN_LOADEXPORT)
3153 {
3154 int idx = get_script_item_idx(sid, name, 0,
3155 NULL, NULL);
3156 svar_T *sv;
3157
3158 if (idx >= 0)
3159 {
3160 sv = ((svar_T *)SCRIPT_ITEM(sid)
3161 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003162 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003163 {
3164 SOURCING_LNUM = iptr->isn_lnum;
3165 semsg(_(e_item_not_exported_in_script_str),
3166 name);
3167 goto on_error;
3168 }
3169 }
3170 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003171 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003172 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003174 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 }
3176 }
3177 break;
3178
Bram Moolenaard3aac292020-04-19 14:32:17 +02003179 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003181 case ISN_LOADB:
3182 case ISN_LOADW:
3183 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003185 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003186
Bram Moolenaar06b77222022-01-25 15:51:56 +00003187 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003188 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003189 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003190 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003192
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 break;
3194
Bram Moolenaar03290b82020-12-19 16:30:44 +01003195 // load autoload variable
3196 case ISN_LOADAUTO:
3197 {
3198 char_u *name = iptr->isn_arg.string;
3199
Bram Moolenaar35578162021-08-02 19:10:38 +02003200 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003201 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003202 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003203 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003204 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003205 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003206 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003207 }
3208 break;
3209
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003210 // load g:/b:/w:/t: namespace
3211 case ISN_LOADGDICT:
3212 case ISN_LOADBDICT:
3213 case ISN_LOADWDICT:
3214 case ISN_LOADTDICT:
3215 {
3216 dict_T *d = NULL;
3217
3218 switch (iptr->isn_type)
3219 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003220 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3221 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3222 case ISN_LOADWDICT: d = curwin->w_vars; break;
3223 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003224 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003225 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003226 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003227 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003228 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003229 tv = STACK_TV_BOT(0);
3230 tv->v_type = VAR_DICT;
3231 tv->v_lock = 0;
3232 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003233 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003234 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003235 }
3236 break;
3237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 // load &option
3239 case ISN_LOADOPT:
3240 {
3241 typval_T optval;
3242 char_u *name = iptr->isn_arg.string;
3243
Bram Moolenaara8c17702020-04-01 21:17:24 +02003244 // This is not expected to fail, name is checked during
3245 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003246 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003247 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003248 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003249 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003251 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 }
3253 break;
3254
3255 // load $ENV
3256 case ISN_LOADENV:
3257 {
3258 typval_T optval;
3259 char_u *name = iptr->isn_arg.string;
3260
Bram Moolenaar35578162021-08-02 19:10:38 +02003261 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003262 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003263 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003264 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003266 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 }
3268 break;
3269
3270 // load @register
3271 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003272 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003273 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 tv = STACK_TV_BOT(0);
3275 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003276 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003277 // This may result in NULL, which should be equivalent to an
3278 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 tv->vval.v_string = get_reg_contents(
3280 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003281 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 break;
3283
3284 // store local variable
3285 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003286 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 tv = STACK_TV_VAR(iptr->isn_arg.number);
3288 clear_tv(tv);
3289 *tv = *STACK_TV_BOT(0);
3290 break;
3291
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003292 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003293 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003294 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003295 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003296 int sid = iptr->isn_arg.loadstore.ls_sid;
3297 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003298 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003299 dictitem_T *di = find_var_in_ht(ht, 0,
3300 iptr->isn_type == ISN_STORES
3301 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003302
Bram Moolenaar4c137212021-04-19 16:48:48 +02003303 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003304 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003305 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003306 {
3307 if (iptr->isn_type == ISN_STOREEXPORT)
3308 {
3309 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003310 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003311 goto on_error;
3312 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003313 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003314 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003315 else
3316 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003317 if (iptr->isn_type == ISN_STOREEXPORT)
3318 {
3319 int idx = get_script_item_idx(sid, name, 0,
3320 NULL, NULL);
3321
Bram Moolenaar06651632022-04-27 17:54:25 +01003322 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003323 if (idx >= 0)
3324 {
3325 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3326 ->sn_var_vals.ga_data) + idx;
3327
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003328 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003329 {
3330 semsg(_(e_item_not_exported_in_script_str),
3331 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003332 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003333 goto on_error;
3334 }
3335 }
3336 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003337 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003338 {
3339 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003340 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003341 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003342 clear_tv(&di->di_tv);
3343 di->di_tv = *STACK_TV_BOT(0);
3344 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003345 }
3346 break;
3347
3348 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 case ISN_STORESCRIPT:
3350 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003351 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3352 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003354 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003355 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003356 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003357 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003358
3359 // "const" and "final" are checked at compile time, locking
3360 // the value needs to be checked here.
3361 SOURCING_LNUM = iptr->isn_lnum;
3362 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003363 {
3364 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003365 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003366 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 clear_tv(sv->sv_tv);
3369 *sv->sv_tv = *STACK_TV_BOT(0);
3370 }
3371 break;
3372
3373 // store option
3374 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003375 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003377 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3378 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 long n = 0;
3380 char_u *s = NULL;
3381 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003382 char_u numbuf[NUMBUFLEN];
3383 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384
Bram Moolenaar4c137212021-04-19 16:48:48 +02003385 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 tv = STACK_TV_BOT(0);
3387 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003388 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003390 if (s == NULL)
3391 s = (char_u *)"";
3392 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003393 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3394 {
3395 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003396 // If the option can be set to a function reference or
3397 // a lambda and the passed value is a function
3398 // reference, then convert it to the name (string) of
3399 // the function reference.
3400 s = tv2string(tv, &tofree, numbuf, 0);
3401 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003402 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003403 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003404 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003405 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003406 goto on_error;
3407 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003408 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003410 // must be VAR_NUMBER, CHECKTYPE makes sure
3411 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003412 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003413 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003414 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 if (msg != NULL)
3416 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003417 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003419 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 }
3422 break;
3423
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003424 // store $ENV
3425 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003426 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003427 tv = STACK_TV_BOT(0);
3428 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3429 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003430 break;
3431
3432 // store @r
3433 case ISN_STOREREG:
3434 {
3435 int reg = iptr->isn_arg.number;
3436
Bram Moolenaar4c137212021-04-19 16:48:48 +02003437 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003438 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003439 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003440 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003441 }
3442 break;
3443
3444 // store v: variable
3445 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003446 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003447 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3448 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003449 // should not happen, type is checked when compiling
3450 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003451 break;
3452
Bram Moolenaard3aac292020-04-19 14:32:17 +02003453 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003455 case ISN_STOREB:
3456 case ISN_STOREW:
3457 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003459 dictitem_T *di;
3460 hashtab_T *ht;
3461 char_u *name = iptr->isn_arg.string + 2;
3462
Bram Moolenaard3aac292020-04-19 14:32:17 +02003463 switch (iptr->isn_type)
3464 {
3465 case ISN_STOREG:
3466 ht = get_globvar_ht();
3467 break;
3468 case ISN_STOREB:
3469 ht = &curbuf->b_vars->dv_hashtab;
3470 break;
3471 case ISN_STOREW:
3472 ht = &curwin->w_vars->dv_hashtab;
3473 break;
3474 case ISN_STORET:
3475 ht = &curtab->tp_vars->dv_hashtab;
3476 break;
3477 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003478 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003479 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480
Bram Moolenaar4c137212021-04-19 16:48:48 +02003481 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003482 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003484 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 else
3486 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003487 SOURCING_LNUM = iptr->isn_lnum;
3488 if (var_check_permission(di, name) == FAIL)
3489 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490 clear_tv(&di->di_tv);
3491 di->di_tv = *STACK_TV_BOT(0);
3492 }
3493 }
3494 break;
3495
Bram Moolenaar03290b82020-12-19 16:30:44 +01003496 // store an autoload variable
3497 case ISN_STOREAUTO:
3498 SOURCING_LNUM = iptr->isn_lnum;
3499 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3500 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003501 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003502 break;
3503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 // store number in local variable
3505 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003506 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 clear_tv(tv);
3508 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003509 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 break;
3511
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003512 // store value in list or dict variable
3513 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003514 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003515 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003516
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003517 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003518 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003519 if (res == NOTDONE)
3520 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003521 }
3522 break;
3523
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003524 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003525 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003526 if (execute_storerange(iptr, ectx) == FAIL)
3527 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003528 break;
3529
Bram Moolenaar0186e582021-01-10 18:33:11 +01003530 // load or store variable or argument from outer scope
3531 case ISN_LOADOUTER:
3532 case ISN_STOREOUTER:
3533 {
3534 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003535 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3536 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003537
3538 while (depth > 1 && outer != NULL)
3539 {
3540 outer = outer->out_up;
3541 --depth;
3542 }
3543 if (outer == NULL)
3544 {
3545 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003546 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3547 || ectx->ec_outer_ref == NULL)
3548 // Possibly :def function called from legacy
3549 // context.
3550 emsg(_(e_closure_called_from_invalid_context));
3551 else
3552 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003553 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003554 }
3555 tv = ((typval_T *)outer->out_stack->ga_data)
3556 + outer->out_frame_idx + STACK_FRAME_SIZE
3557 + iptr->isn_arg.outer.outer_idx;
3558 if (iptr->isn_type == ISN_LOADOUTER)
3559 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003560 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003561 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003562 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003563 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003564 }
3565 else
3566 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003567 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003568 clear_tv(tv);
3569 *tv = *STACK_TV_BOT(0);
3570 }
3571 }
3572 break;
3573
Bram Moolenaar752fc692021-01-04 21:57:11 +01003574 // unlet item in list or dict variable
3575 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003576 if (execute_unletindex(iptr, ectx) == FAIL)
3577 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003578 break;
3579
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003580 // unlet range of items in list variable
3581 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003582 if (execute_unletrange(iptr, ectx) == FAIL)
3583 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003584 break;
3585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 // push constant
3587 case ISN_PUSHNR:
3588 case ISN_PUSHBOOL:
3589 case ISN_PUSHSPEC:
3590 case ISN_PUSHF:
3591 case ISN_PUSHS:
3592 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003593 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003594 case ISN_PUSHCHANNEL:
3595 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003596 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003597 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003599 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003600 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 switch (iptr->isn_type)
3602 {
3603 case ISN_PUSHNR:
3604 tv->v_type = VAR_NUMBER;
3605 tv->vval.v_number = iptr->isn_arg.number;
3606 break;
3607 case ISN_PUSHBOOL:
3608 tv->v_type = VAR_BOOL;
3609 tv->vval.v_number = iptr->isn_arg.number;
3610 break;
3611 case ISN_PUSHSPEC:
3612 tv->v_type = VAR_SPECIAL;
3613 tv->vval.v_number = iptr->isn_arg.number;
3614 break;
3615#ifdef FEAT_FLOAT
3616 case ISN_PUSHF:
3617 tv->v_type = VAR_FLOAT;
3618 tv->vval.v_float = iptr->isn_arg.fnumber;
3619 break;
3620#endif
3621 case ISN_PUSHBLOB:
3622 blob_copy(iptr->isn_arg.blob, tv);
3623 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003624 case ISN_PUSHFUNC:
3625 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003626 if (iptr->isn_arg.string == NULL)
3627 tv->vval.v_string = NULL;
3628 else
3629 tv->vval.v_string =
3630 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003631 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003632 case ISN_PUSHCHANNEL:
3633#ifdef FEAT_JOB_CHANNEL
3634 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003635 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003636#endif
3637 break;
3638 case ISN_PUSHJOB:
3639#ifdef FEAT_JOB_CHANNEL
3640 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003641 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003642#endif
3643 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 default:
3645 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003646 tv->vval.v_string = iptr->isn_arg.string == NULL
3647 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 }
3649 break;
3650
Bram Moolenaar06b77222022-01-25 15:51:56 +00003651 case ISN_AUTOLOAD:
3652 {
3653 char_u *name = iptr->isn_arg.string;
3654
3655 (void)script_autoload(name, FALSE);
3656 if (find_func(name, TRUE))
3657 {
3658 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3659 goto theend;
3660 tv = STACK_TV_BOT(0);
3661 tv->v_lock = 0;
3662 ++ectx->ec_stack.ga_len;
3663 tv->v_type = VAR_FUNC;
3664 tv->vval.v_string = vim_strsave(name);
3665 }
3666 else
3667 {
3668 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3669
3670 if (res == NOTDONE)
3671 goto theend;
3672 if (res == FAIL)
3673 goto on_error;
3674 }
3675 }
3676 break;
3677
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003678 case ISN_UNLET:
3679 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3680 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003681 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003682 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003683 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003684 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003685 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003686
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003687 case ISN_LOCKUNLOCK:
3688 {
3689 typval_T *lval_root_save = lval_root;
3690 int res;
3691
3692 // Stack has the local variable, argument the whole :lock
3693 // or :unlock command, like ISN_EXEC.
3694 --ectx->ec_stack.ga_len;
3695 lval_root = STACK_TV_BOT(0);
3696 res = exec_command(iptr);
3697 clear_tv(lval_root);
3698 lval_root = lval_root_save;
3699 if (res == FAIL)
3700 goto on_error;
3701 }
3702 break;
3703
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003704 case ISN_LOCKCONST:
3705 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3706 break;
3707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 // create a list from items on the stack; uses a single allocation
3709 // for the list header and the items
3710 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003711 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003712 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 break;
3714
3715 // create a dict from items on the stack
3716 case ISN_NEWDICT:
3717 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003718 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003720 SOURCING_LNUM = iptr->isn_lnum;
3721 res = exe_newdict(iptr->isn_arg.number, ectx);
3722 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003723 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003724 if (res == MAYBE)
3725 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 }
3727 break;
3728
LemonBoy372bcce2022-04-25 12:43:20 +01003729 case ISN_CONCAT:
3730 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3731 goto theend;
3732 break;
3733
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003734 // create a partial with NULL value
3735 case ISN_NEWPARTIAL:
3736 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3737 goto theend;
3738 ++ectx->ec_stack.ga_len;
3739 tv = STACK_TV_BOT(-1);
3740 tv->v_type = VAR_PARTIAL;
3741 tv->v_lock = 0;
3742 tv->vval.v_partial = NULL;
3743 break;
3744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 // call a :def function
3746 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003747 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003748 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3749 NULL,
3750 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003751 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003752 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 break;
3754
3755 // call a builtin function
3756 case ISN_BCALL:
3757 SOURCING_LNUM = iptr->isn_lnum;
3758 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3759 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003760 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003761 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 break;
3763
3764 // call a funcref or partial
3765 case ISN_PCALL:
3766 {
3767 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3768 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003769 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770
3771 SOURCING_LNUM = iptr->isn_lnum;
3772 if (pfunc->cpf_top)
3773 {
3774 // funcref is above the arguments
3775 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3776 }
3777 else
3778 {
3779 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003780 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003781 partial_tv = *STACK_TV_BOT(0);
3782 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003784 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003785 if (tv == &partial_tv)
3786 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003788 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 }
3790 break;
3791
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003792 case ISN_PCALL_END:
3793 // PCALL finished, arguments have been consumed and replaced by
3794 // the return value. Now clear the funcref from the stack,
3795 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003796 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003797 clear_tv(STACK_TV_BOT(-1));
3798 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3799 break;
3800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 // call a user defined function or funcref/partial
3802 case ISN_UCALL:
3803 {
3804 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3805
3806 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003807 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003808 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003809 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 }
3811 break;
3812
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003813 // :defer func(arg)
3814 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01003815 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003816 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
3817 goto on_error;
3818 break;
3819
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003820 // return from a :def function call without a value
3821 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003822 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003823 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003824 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003825 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003826 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003827 tv->vval.v_number = 0;
3828 tv->v_lock = 0;
3829 // FALLTHROUGH
3830
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003831 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 case ISN_RETURN:
3833 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003834 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003835 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003836
3837 if (trystack->ga_len > 0)
3838 trycmd = ((trycmd_T *)trystack->ga_data)
3839 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003840 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003841 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003843 // jump to ":finally" or ":endtry"
3844 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003845 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003846 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003847 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 trycmd->tcd_return = TRUE;
3849 }
3850 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003851 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 }
3853 break;
3854
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003855 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 case ISN_FUNCREF:
3857 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003858 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003859 ufunc_T *ufunc;
3860 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003863 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003864 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003865 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003866 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003867 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003868 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003869 if (funcref->fr_func_name == NULL)
3870 {
3871 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3872 + funcref->fr_dfunc_idx;
3873
3874 ufunc = pt_dfunc->df_ufunc;
3875 }
3876 else
3877 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003878 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003879 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003880 if (ufunc == NULL)
3881 {
3882 SOURCING_LNUM = iptr->isn_lnum;
3883 iemsg("ufunc unexpectedly NULL for FUNCREF");
3884 goto theend;
3885 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003886 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003887 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003889 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 tv->vval.v_partial = pt;
3891 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003892 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 }
3894 break;
3895
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003896 // Create a global function from a lambda.
3897 case ISN_NEWFUNC:
3898 {
3899 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3900
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003901 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003902 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003903 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003904 }
3905 break;
3906
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003907 // List functions
3908 case ISN_DEF:
3909 if (iptr->isn_arg.string == NULL)
3910 list_functions(NULL);
3911 else
3912 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003913 exarg_T ea;
3914 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003915
3916 CLEAR_FIELD(ea);
3917 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003918 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3919 define_function(&ea, NULL, &lines_to_free);
3920 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003921 }
3922 break;
3923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 // jump if a condition is met
3925 case ISN_JUMP:
3926 {
3927 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003928 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 int jump = TRUE;
3930
3931 if (when != JUMP_ALWAYS)
3932 {
3933 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003934 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003935 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003936 || when == JUMP_IF_COND_TRUE)
3937 {
3938 SOURCING_LNUM = iptr->isn_lnum;
3939 jump = tv_get_bool_chk(tv, &error);
3940 if (error)
3941 goto on_error;
3942 }
3943 else
3944 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01003945 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003947 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 {
3949 // drop the value from the stack
3950 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003951 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 }
3953 }
3954 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003955 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 }
3957 break;
3958
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003959 // Jump if an argument with a default value was already set and not
3960 // v:none.
3961 case ISN_JUMP_IF_ARG_SET:
3962 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3963 if (tv->v_type != VAR_UNKNOWN
3964 && !(tv->v_type == VAR_SPECIAL
3965 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003966 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003967 break;
3968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 // top of a for loop
3970 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003971 if (execute_for(iptr, ectx) == FAIL)
3972 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 break;
3974
3975 // start of ":try" block
3976 case ISN_TRY:
3977 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003978 trycmd_T *trycmd = NULL;
3979
Bram Moolenaar35578162021-08-02 19:10:38 +02003980 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003981 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003982 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3983 + ectx->ec_trystack.ga_len;
3984 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003986 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003987 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3988 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003989 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003990 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003991 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003992 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003993 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003994 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 }
3996 break;
3997
3998 case ISN_PUSHEXC:
3999 if (current_exception == NULL)
4000 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004001 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004003 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004005 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004006 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004008 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004010 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 tv->vval.v_string = vim_strsave(
4012 (char_u *)current_exception->value);
4013 break;
4014
4015 case ISN_CATCH:
4016 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004017 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004018 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019
Bram Moolenaar4c137212021-04-19 16:48:48 +02004020 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004021 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004023 trycmd->tcd_caught = TRUE;
4024 trycmd->tcd_did_throw = FALSE;
4025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004027 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 catch_exception(current_exception);
4029 }
4030 break;
4031
Bram Moolenaarc150c092021-02-13 15:02:46 +01004032 case ISN_TRYCONT:
4033 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004034 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004035 trycont_T *trycont = &iptr->isn_arg.trycont;
4036 int i;
4037 trycmd_T *trycmd;
4038 int iidx = trycont->tct_where;
4039
4040 if (trystack->ga_len < trycont->tct_levels)
4041 {
4042 siemsg("TRYCONT: expected %d levels, found %d",
4043 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004044 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004045 }
4046 // Make :endtry jump to any outer try block and the last
4047 // :endtry inside the loop to the loop start.
4048 for (i = trycont->tct_levels; i > 0; --i)
4049 {
4050 trycmd = ((trycmd_T *)trystack->ga_data)
4051 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004052 // Add one to tcd_cont to be able to jump to
4053 // instruction with index zero.
4054 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004055 iidx = trycmd->tcd_finally_idx == 0
4056 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004057 }
4058 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004059 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004060 }
4061 break;
4062
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004063 case ISN_FINALLY:
4064 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004065 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004066 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4067 + trystack->ga_len - 1;
4068
4069 // Reset the index to avoid a return statement jumps here
4070 // again.
4071 trycmd->tcd_finally_idx = 0;
4072 break;
4073 }
4074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 // end of ":try" block
4076 case ISN_ENDTRY:
4077 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004078 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004079 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080
Bram Moolenaar06651632022-04-27 17:54:25 +01004081 --trystack->ga_len;
4082 --trylevel;
4083 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4084 if (trycmd->tcd_did_throw)
4085 did_throw = TRUE;
4086 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004088 // discard the exception
4089 if (caught_stack == current_exception)
4090 caught_stack = caught_stack->caught;
4091 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004093
4094 if (trycmd->tcd_return)
4095 goto func_return;
4096
4097 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4098 {
4099 --ectx->ec_stack.ga_len;
4100 clear_tv(STACK_TV_BOT(0));
4101 }
4102 if (trycmd->tcd_cont != 0)
4103 // handling :continue: jump to outer try block or
4104 // start of the loop
4105 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 }
4107 break;
4108
4109 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004110 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004111 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004112
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004113 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4114 {
4115 // throwing an exception while using "silent!" causes
4116 // the function to abort but not display an error.
4117 tv = STACK_TV_BOT(-1);
4118 clear_tv(tv);
4119 tv->v_type = VAR_NUMBER;
4120 tv->vval.v_number = 0;
4121 goto done;
4122 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004123 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004124 tv = STACK_TV_BOT(0);
4125 if (tv->vval.v_string == NULL
4126 || *skipwhite(tv->vval.v_string) == NUL)
4127 {
4128 vim_free(tv->vval.v_string);
4129 SOURCING_LNUM = iptr->isn_lnum;
4130 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004131 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004132 }
4133
4134 // Inside a "catch" we need to first discard the caught
4135 // exception.
4136 if (trystack->ga_len > 0)
4137 {
4138 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4139 + trystack->ga_len - 1;
4140 if (trycmd->tcd_caught && current_exception != NULL)
4141 {
4142 // discard the exception
4143 if (caught_stack == current_exception)
4144 caught_stack = caught_stack->caught;
4145 discard_current_exception();
4146 trycmd->tcd_caught = FALSE;
4147 }
4148 }
4149
Bram Moolenaar90a57162022-02-12 14:23:17 +00004150 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004151 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4152 == FAIL)
4153 {
4154 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004155 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004156 }
4157 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 break;
4160
4161 // compare with special values
4162 case ISN_COMPAREBOOL:
4163 case ISN_COMPARESPECIAL:
4164 {
4165 typval_T *tv1 = STACK_TV_BOT(-2);
4166 typval_T *tv2 = STACK_TV_BOT(-1);
4167 varnumber_T arg1 = tv1->vval.v_number;
4168 varnumber_T arg2 = tv2->vval.v_number;
4169 int res;
4170
Bram Moolenaar06651632022-04-27 17:54:25 +01004171 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4172 res = arg1 == arg2;
4173 else
4174 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175
Bram Moolenaar4c137212021-04-19 16:48:48 +02004176 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 tv1->v_type = VAR_BOOL;
4178 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4179 }
4180 break;
4181
Bram Moolenaar7a222242022-03-01 19:23:24 +00004182 case ISN_COMPARENULL:
4183 {
4184 typval_T *tv1 = STACK_TV_BOT(-2);
4185 typval_T *tv2 = STACK_TV_BOT(-1);
4186 int res;
4187
4188 res = typval_compare_null(tv1, tv2);
4189 if (res == MAYBE)
4190 goto on_error;
4191 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4192 res = !res;
4193 clear_tv(tv1);
4194 clear_tv(tv2);
4195 --ectx->ec_stack.ga_len;
4196 tv1->v_type = VAR_BOOL;
4197 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4198 }
4199 break;
4200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 // Operation with two number arguments
4202 case ISN_OPNR:
4203 case ISN_COMPARENR:
4204 {
4205 typval_T *tv1 = STACK_TV_BOT(-2);
4206 typval_T *tv2 = STACK_TV_BOT(-1);
4207 varnumber_T arg1 = tv1->vval.v_number;
4208 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004209 varnumber_T res = 0;
4210 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004212 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4213 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4214 {
4215 if (arg2 < 0)
4216 {
4217 SOURCING_LNUM = iptr->isn_lnum;
4218 emsg(_(e_bitshift_ops_must_be_postive));
4219 goto on_error;
4220 }
4221 }
4222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 switch (iptr->isn_arg.op.op_type)
4224 {
4225 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004226 case EXPR_DIV: if (arg2 == 0)
4227 div_zero = TRUE;
4228 else
4229 res = arg1 / arg2;
4230 break;
4231 case EXPR_REM: if (arg2 == 0)
4232 div_zero = TRUE;
4233 else
4234 res = arg1 % arg2;
4235 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 case EXPR_SUB: res = arg1 - arg2; break;
4237 case EXPR_ADD: res = arg1 + arg2; break;
4238
4239 case EXPR_EQUAL: res = arg1 == arg2; break;
4240 case EXPR_NEQUAL: res = arg1 != arg2; break;
4241 case EXPR_GREATER: res = arg1 > arg2; break;
4242 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4243 case EXPR_SMALLER: res = arg1 < arg2; break;
4244 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004245 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4246 res = 0;
4247 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004248 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004249 break;
4250 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4251 res = 0;
4252 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004253 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004254 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004255 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 }
4257
Bram Moolenaar4c137212021-04-19 16:48:48 +02004258 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 if (iptr->isn_type == ISN_COMPARENR)
4260 {
4261 tv1->v_type = VAR_BOOL;
4262 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4263 }
4264 else
4265 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004266 if (div_zero)
4267 {
4268 SOURCING_LNUM = iptr->isn_lnum;
4269 emsg(_(e_divide_by_zero));
4270 goto on_error;
4271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 }
4273 break;
4274
4275 // Computation with two float arguments
4276 case ISN_OPFLOAT:
4277 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004278#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 {
4280 typval_T *tv1 = STACK_TV_BOT(-2);
4281 typval_T *tv2 = STACK_TV_BOT(-1);
4282 float_T arg1 = tv1->vval.v_float;
4283 float_T arg2 = tv2->vval.v_float;
4284 float_T res = 0;
4285 int cmp = FALSE;
4286
4287 switch (iptr->isn_arg.op.op_type)
4288 {
4289 case EXPR_MULT: res = arg1 * arg2; break;
4290 case EXPR_DIV: res = arg1 / arg2; break;
4291 case EXPR_SUB: res = arg1 - arg2; break;
4292 case EXPR_ADD: res = arg1 + arg2; break;
4293
4294 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4295 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4296 case EXPR_GREATER: cmp = arg1 > arg2; break;
4297 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4298 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4299 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4300 default: cmp = 0; break;
4301 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004302 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 if (iptr->isn_type == ISN_COMPAREFLOAT)
4304 {
4305 tv1->v_type = VAR_BOOL;
4306 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4307 }
4308 else
4309 tv1->vval.v_float = res;
4310 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004311#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 break;
4313
4314 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004315 case ISN_COMPAREDICT:
4316 case ISN_COMPAREFUNC:
4317 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 case ISN_COMPAREBLOB:
4319 {
4320 typval_T *tv1 = STACK_TV_BOT(-2);
4321 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004322 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4323 int ic = iptr->isn_arg.op.op_ic;
4324 int res = FALSE;
4325 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326
Bram Moolenaar265f8112021-12-19 12:33:05 +00004327 SOURCING_LNUM = iptr->isn_lnum;
4328 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004330 status = typval_compare_list(tv1, tv2,
4331 exprtype, ic, &res);
4332 }
4333 else if (iptr->isn_type == ISN_COMPAREDICT)
4334 {
4335 status = typval_compare_dict(tv1, tv2,
4336 exprtype, ic, &res);
4337 }
4338 else if (iptr->isn_type == ISN_COMPAREFUNC)
4339 {
4340 status = typval_compare_func(tv1, tv2,
4341 exprtype, ic, &res);
4342 }
4343 else if (iptr->isn_type == ISN_COMPARESTRING)
4344 {
4345 status = typval_compare_string(tv1, tv2,
4346 exprtype, ic, &res);
4347 }
4348 else
4349 {
4350 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004352 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 clear_tv(tv1);
4354 clear_tv(tv2);
4355 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004356 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4357 if (status == FAIL)
4358 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 }
4360 break;
4361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 case ISN_COMPAREANY:
4363 {
4364 typval_T *tv1 = STACK_TV_BOT(-2);
4365 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004366 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004368 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369
Bram Moolenaareb26f432020-09-14 16:50:05 +02004370 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004371 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004373 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004374 if (status == FAIL)
4375 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 }
4377 break;
4378
4379 case ISN_ADDLIST:
4380 case ISN_ADDBLOB:
4381 {
4382 typval_T *tv1 = STACK_TV_BOT(-2);
4383 typval_T *tv2 = STACK_TV_BOT(-1);
4384
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004385 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004387 {
4388 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4389 && tv1->vval.v_list != NULL)
4390 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4391 NULL);
4392 else
4393 eval_addlist(tv1, tv2);
4394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 else
4396 eval_addblob(tv1, tv2);
4397 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004398 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 }
4400 break;
4401
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004402 case ISN_LISTAPPEND:
4403 {
4404 typval_T *tv1 = STACK_TV_BOT(-2);
4405 typval_T *tv2 = STACK_TV_BOT(-1);
4406 list_T *l = tv1->vval.v_list;
4407
4408 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004409 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004410 if (l == NULL)
4411 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004412 emsg(_(e_cannot_add_to_null_list));
4413 goto on_error;
4414 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004415 if (value_check_lock(l->lv_lock, NULL, FALSE))
4416 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004417 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004418 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004419 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004420 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004421 }
4422 break;
4423
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004424 case ISN_BLOBAPPEND:
4425 {
4426 typval_T *tv1 = STACK_TV_BOT(-2);
4427 typval_T *tv2 = STACK_TV_BOT(-1);
4428 blob_T *b = tv1->vval.v_blob;
4429 int error = FALSE;
4430 varnumber_T n;
4431
4432 // add a number to a blob
4433 if (b == NULL)
4434 {
4435 SOURCING_LNUM = iptr->isn_lnum;
4436 emsg(_(e_cannot_add_to_null_blob));
4437 goto on_error;
4438 }
4439 n = tv_get_number_chk(tv2, &error);
4440 if (error)
4441 goto on_error;
4442 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004443 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004444 }
4445 break;
4446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 // Computation with two arguments of unknown type
4448 case ISN_OPANY:
4449 {
4450 typval_T *tv1 = STACK_TV_BOT(-2);
4451 typval_T *tv2 = STACK_TV_BOT(-1);
4452 varnumber_T n1, n2;
4453#ifdef FEAT_FLOAT
4454 float_T f1 = 0, f2 = 0;
4455#endif
4456 int error = FALSE;
4457
4458 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4459 {
4460 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4461 {
4462 eval_addlist(tv1, tv2);
4463 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004464 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 break;
4466 }
4467 else if (tv1->v_type == VAR_BLOB
4468 && tv2->v_type == VAR_BLOB)
4469 {
4470 eval_addblob(tv1, tv2);
4471 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004472 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 break;
4474 }
4475 }
4476#ifdef FEAT_FLOAT
4477 if (tv1->v_type == VAR_FLOAT)
4478 {
4479 f1 = tv1->vval.v_float;
4480 n1 = 0;
4481 }
4482 else
4483#endif
4484 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004485 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 n1 = tv_get_number_chk(tv1, &error);
4487 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004488 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489#ifdef FEAT_FLOAT
4490 if (tv2->v_type == VAR_FLOAT)
4491 f1 = n1;
4492#endif
4493 }
4494#ifdef FEAT_FLOAT
4495 if (tv2->v_type == VAR_FLOAT)
4496 {
4497 f2 = tv2->vval.v_float;
4498 n2 = 0;
4499 }
4500 else
4501#endif
4502 {
4503 n2 = tv_get_number_chk(tv2, &error);
4504 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004505 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506#ifdef FEAT_FLOAT
4507 if (tv1->v_type == VAR_FLOAT)
4508 f2 = n2;
4509#endif
4510 }
4511#ifdef FEAT_FLOAT
4512 // if there is a float on either side the result is a float
4513 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4514 {
4515 switch (iptr->isn_arg.op.op_type)
4516 {
4517 case EXPR_MULT: f1 = f1 * f2; break;
4518 case EXPR_DIV: f1 = f1 / f2; break;
4519 case EXPR_SUB: f1 = f1 - f2; break;
4520 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004521 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004522 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004523 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 }
4525 clear_tv(tv1);
4526 clear_tv(tv2);
4527 tv1->v_type = VAR_FLOAT;
4528 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004529 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 }
4531 else
4532#endif
4533 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004534 int failed = FALSE;
4535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 switch (iptr->isn_arg.op.op_type)
4537 {
4538 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004539 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4540 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004541 goto on_error;
4542 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 case EXPR_SUB: n1 = n1 - n2; break;
4544 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004545 default: n1 = num_modulus(n1, n2, &failed);
4546 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004547 goto on_error;
4548 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 }
4550 clear_tv(tv1);
4551 clear_tv(tv2);
4552 tv1->v_type = VAR_NUMBER;
4553 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004554 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 }
4556 }
4557 break;
4558
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004559 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004560 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004561 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004562 int is_slice = iptr->isn_type == ISN_STRSLICE;
4563 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004564 char_u *res;
4565
4566 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004567 // string slice: string is at stack-3, first index at
4568 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004569 if (is_slice)
4570 {
4571 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004572 n1 = tv->vval.v_number;
4573 }
4574
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004575 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004576 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004577
Bram Moolenaar4c137212021-04-19 16:48:48 +02004578 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004579 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004580 if (is_slice)
4581 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004582 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004583 else
4584 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004585 // single character (including composing characters).
4586 // If the index is too big or negative the result is
4587 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004588 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004589 vim_free(tv->vval.v_string);
4590 tv->vval.v_string = res;
4591 }
4592 break;
4593
4594 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004595 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004596 case ISN_BLOBINDEX:
4597 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004599 int is_slice = iptr->isn_type == ISN_LISTSLICE
4600 || iptr->isn_type == ISN_BLOBSLICE;
4601 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4602 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004603 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004604 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605
4606 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004607 // list slice: list is at stack-3, indexes at stack-2 and
4608 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004609 // Same for blob.
4610 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611
4612 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004613 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004615
4616 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 {
Bram Moolenaared591872020-08-15 22:14:53 +02004618 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004619 n1 = tv->vval.v_number;
4620 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 }
Bram Moolenaared591872020-08-15 22:14:53 +02004622
Bram Moolenaar4c137212021-04-19 16:48:48 +02004623 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004624 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004625 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004626 if (is_blob)
4627 {
4628 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4629 n1, n2, FALSE, tv) == FAIL)
4630 goto on_error;
4631 }
4632 else
4633 {
4634 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4635 n1, n2, FALSE, tv, TRUE) == FAIL)
4636 goto on_error;
4637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 }
4639 break;
4640
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004641 case ISN_ANYINDEX:
4642 case ISN_ANYSLICE:
4643 {
4644 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4645 typval_T *var1, *var2;
4646 int res;
4647
4648 // index: composite is at stack-2, index at stack-1
4649 // slice: composite is at stack-3, indexes at stack-2 and
4650 // stack-1
4651 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004652 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004653 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4654 goto on_error;
4655 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4656 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004657 res = eval_index_inner(tv, is_slice, var1, var2,
4658 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004659 clear_tv(var1);
4660 if (is_slice)
4661 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004662 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004663 if (res == FAIL)
4664 goto on_error;
4665 }
4666 break;
4667
Bram Moolenaar9af78762020-06-16 11:34:42 +02004668 case ISN_SLICE:
4669 {
4670 list_T *list;
4671 int count = iptr->isn_arg.number;
4672
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004673 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004674 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004675 list = tv->vval.v_list;
4676
4677 // no error for short list, expect it to be checked earlier
4678 if (list != NULL && list->lv_len >= count)
4679 {
4680 list_T *newlist = list_slice(list,
4681 count, list->lv_len - 1);
4682
4683 if (newlist != NULL)
4684 {
4685 list_unref(list);
4686 tv->vval.v_list = newlist;
4687 ++newlist->lv_refcount;
4688 }
4689 }
4690 }
4691 break;
4692
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004693 case ISN_GETITEM:
4694 {
4695 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004696 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004697
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004698 // Get list item: list is at stack-1, push item.
4699 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004700 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4701 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004702
Bram Moolenaar35578162021-08-02 19:10:38 +02004703 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004704 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004705 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004706 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004707
4708 // Useful when used in unpack assignment. Reset at
4709 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004710 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004711 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004712 }
4713 break;
4714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 case ISN_MEMBER:
4716 {
4717 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004718 char_u *key;
4719 dictitem_T *di;
4720
4721 // dict member: dict is at stack-2, key at stack-1
4722 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004723 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004724 dict = tv->vval.v_dict;
4725
4726 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004727 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004728 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004729 if (key == NULL)
4730 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004731
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004732 if ((di = dict_find(dict, key, -1)) == NULL)
4733 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004734 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004735 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004736
4737 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004738 // stack contents makes sense and the dict stack is
4739 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004740 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004741 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004742 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004743 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004744 tv->v_type = VAR_NUMBER;
4745 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004746 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004747 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004748 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004749 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004750 // Put the dict used on the dict stack, it might be used by
4751 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004752 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004753 if (dict_stack_save(tv) == FAIL)
4754 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004755 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004756 }
4757 break;
4758
4759 // dict member with string key
4760 case ISN_STRINGMEMBER:
4761 {
4762 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 dictitem_T *di;
4764
4765 tv = STACK_TV_BOT(-1);
4766 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4767 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004768 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004769 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004770 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 }
4772 dict = tv->vval.v_dict;
4773
4774 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4775 == NULL)
4776 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004777 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004778 semsg(_(e_key_not_present_in_dictionary),
4779 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004780 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004782 // Put the dict used on the dict stack, it might be used by
4783 // a dict function later.
4784 if (dict_stack_save(tv) == FAIL)
4785 goto on_fatal_error;
4786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004788 }
4789 break;
4790
4791 case ISN_CLEARDICT:
4792 dict_stack_drop();
4793 break;
4794
4795 case ISN_USEDICT:
4796 {
4797 typval_T *dict_tv = dict_stack_get_tv();
4798
4799 // Turn "dict.Func" into a partial for "Func" bound to
4800 // "dict". Don't do this when "Func" is already a partial
4801 // that was bound explicitly (pt_auto is FALSE).
4802 tv = STACK_TV_BOT(-1);
4803 if (dict_tv != NULL
4804 && dict_tv->v_type == VAR_DICT
4805 && dict_tv->vval.v_dict != NULL
4806 && (tv->v_type == VAR_FUNC
4807 || (tv->v_type == VAR_PARTIAL
4808 && (tv->vval.v_partial->pt_auto
4809 || tv->vval.v_partial->pt_dict == NULL))))
4810 dict_tv->vval.v_dict =
4811 make_partial(dict_tv->vval.v_dict, tv);
4812 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004813 }
4814 break;
4815
4816 case ISN_NEGATENR:
4817 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004818 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004819#ifdef FEAT_FLOAT
4820 if (tv->v_type == VAR_FLOAT)
4821 tv->vval.v_float = -tv->vval.v_float;
4822 else
4823#endif
4824 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 break;
4826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827 case ISN_CHECKTYPE:
4828 {
4829 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004830 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004831 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004833 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004834 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004835 if (!ectx->ec_where.wt_variable)
4836 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004837 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004838 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004839 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004840 if (r == FAIL)
4841 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004842 if (!ectx->ec_where.wt_variable)
4843 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004844
4845 // number 0 is FALSE, number 1 is TRUE
4846 if (tv->v_type == VAR_NUMBER
4847 && ct->ct_type->tt_type == VAR_BOOL
4848 && (tv->vval.v_number == 0
4849 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004851 tv->v_type = VAR_BOOL;
4852 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004853 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854 }
4855 }
4856 break;
4857
Bram Moolenaar9af78762020-06-16 11:34:42 +02004858 case ISN_CHECKLEN:
4859 {
4860 int min_len = iptr->isn_arg.checklen.cl_min_len;
4861 list_T *list = NULL;
4862
4863 tv = STACK_TV_BOT(-1);
4864 if (tv->v_type == VAR_LIST)
4865 list = tv->vval.v_list;
4866 if (list == NULL || list->lv_len < min_len
4867 || (list->lv_len > min_len
4868 && !iptr->isn_arg.checklen.cl_more_OK))
4869 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004870 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004871 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004872 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004873 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004874 }
4875 }
4876 break;
4877
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004878 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004879 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004880 break;
4881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004883 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 {
4885 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004886 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004888 if (iptr->isn_type == ISN_2BOOL)
4889 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004890 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004891 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004892 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004893 n = !n;
4894 }
4895 else
4896 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004897 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004898 SOURCING_LNUM = iptr->isn_lnum;
4899 n = tv_get_bool_chk(tv, &error);
4900 if (error)
4901 goto on_error;
4902 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 clear_tv(tv);
4904 tv->v_type = VAR_BOOL;
4905 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4906 }
4907 break;
4908
4909 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004910 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004911 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004912 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4913 iptr->isn_type == ISN_2STRING_ANY,
4914 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004915 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 break;
4917
Bram Moolenaar08597872020-12-10 19:43:40 +01004918 case ISN_RANGE:
4919 {
4920 exarg_T ea;
4921 char *errormsg;
4922
Bram Moolenaarece0b872021-01-08 20:40:45 +01004923 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004924 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004925 ea.addr_type = ADDR_LINES;
4926 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004927 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004928 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004929 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004930
Bram Moolenaar35578162021-08-02 19:10:38 +02004931 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004932 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004933 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004934 tv = STACK_TV_BOT(-1);
4935 tv->v_type = VAR_NUMBER;
4936 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004937 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004938 }
4939 break;
4940
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004941 case ISN_PUT:
4942 {
4943 int regname = iptr->isn_arg.put.put_regname;
4944 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4945 char_u *expr = NULL;
4946 int dir = FORWARD;
4947
Bram Moolenaar08597872020-12-10 19:43:40 +01004948 if (lnum < -2)
4949 {
4950 // line number was put on the stack by ISN_RANGE
4951 tv = STACK_TV_BOT(-1);
4952 curwin->w_cursor.lnum = tv->vval.v_number;
4953 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4954 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004955 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004956 }
4957 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004958 // :put! above cursor
4959 dir = BACKWARD;
4960 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004961 {
4962 curwin->w_cursor.lnum = lnum;
4963 if (lnum == 0)
4964 // check_cursor() below will move to line 1
4965 dir = BACKWARD;
4966 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004967
4968 if (regname == '=')
4969 {
4970 tv = STACK_TV_BOT(-1);
4971 if (tv->v_type == VAR_STRING)
4972 expr = tv->vval.v_string;
4973 else
4974 {
4975 expr = typval2string(tv, TRUE); // allocates value
4976 clear_tv(tv);
4977 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004978 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004979 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004980 check_cursor();
4981 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4982 vim_free(expr);
4983 }
4984 break;
4985
Bram Moolenaar02194d22020-10-24 23:08:38 +02004986 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004987 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4988 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4989 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4990 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004991 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4992 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004993 break;
4994
Bram Moolenaar02194d22020-10-24 23:08:38 +02004995 case ISN_CMDMOD_REV:
4996 // filter regprog is owned by the instruction, don't free it
4997 cmdmod.cmod_filter_regmatch.regprog = NULL;
4998 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004999 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5000 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005001 break;
5002
Bram Moolenaar792f7862020-11-23 08:31:18 +01005003 case ISN_UNPACK:
5004 {
5005 int count = iptr->isn_arg.unpack.unp_count;
5006 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5007 list_T *l;
5008 listitem_T *li;
5009 int i;
5010
5011 // Check there is a valid list to unpack.
5012 tv = STACK_TV_BOT(-1);
5013 if (tv->v_type != VAR_LIST)
5014 {
5015 SOURCING_LNUM = iptr->isn_lnum;
5016 emsg(_(e_for_argument_must_be_sequence_of_lists));
5017 goto on_error;
5018 }
5019 l = tv->vval.v_list;
5020 if (l == NULL
5021 || l->lv_len < (semicolon ? count - 1 : count))
5022 {
5023 SOURCING_LNUM = iptr->isn_lnum;
5024 emsg(_(e_list_value_does_not_have_enough_items));
5025 goto on_error;
5026 }
5027 else if (!semicolon && l->lv_len > count)
5028 {
5029 SOURCING_LNUM = iptr->isn_lnum;
5030 emsg(_(e_list_value_has_more_items_than_targets));
5031 goto on_error;
5032 }
5033
5034 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005035 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005036 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005037 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005038
5039 // Variable after semicolon gets a list with the remaining
5040 // items.
5041 if (semicolon)
5042 {
5043 list_T *rem_list =
5044 list_alloc_with_items(l->lv_len - count + 1);
5045
5046 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005047 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005048 tv = STACK_TV_BOT(-count);
5049 tv->vval.v_list = rem_list;
5050 ++rem_list->lv_refcount;
5051 tv->v_lock = 0;
5052 li = l->lv_first;
5053 for (i = 0; i < count - 1; ++i)
5054 li = li->li_next;
5055 for (i = 0; li != NULL; ++i)
5056 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005057 typval_T tvcopy;
5058
5059 copy_tv(&li->li_tv, &tvcopy);
5060 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005061 li = li->li_next;
5062 }
5063 --count;
5064 }
5065
5066 // Produce the values in reverse order, first item last.
5067 li = l->lv_first;
5068 for (i = 0; i < count; ++i)
5069 {
5070 tv = STACK_TV_BOT(-i - 1);
5071 copy_tv(&li->li_tv, tv);
5072 li = li->li_next;
5073 }
5074
5075 list_unref(l);
5076 }
5077 break;
5078
Bram Moolenaarb2049902021-01-24 12:53:53 +01005079 case ISN_PROF_START:
5080 case ISN_PROF_END:
5081 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005082#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005083 funccall_T cookie;
5084 ufunc_T *cur_ufunc =
5085 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005086 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005087
5088 cookie.func = cur_ufunc;
5089 if (iptr->isn_type == ISN_PROF_START)
5090 {
5091 func_line_start(&cookie, iptr->isn_lnum);
5092 // if we get here the instruction is executed
5093 func_line_exec(&cookie);
5094 }
5095 else
5096 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005097#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005098 }
5099 break;
5100
Bram Moolenaare99d4222021-06-13 14:01:26 +02005101 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005102 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005103 break;
5104
Bram Moolenaar389df252020-07-09 21:20:47 +02005105 case ISN_SHUFFLE:
5106 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005107 typval_T tmp_tv;
5108 int item = iptr->isn_arg.shuffle.shfl_item;
5109 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005110
5111 tmp_tv = *STACK_TV_BOT(-item);
5112 for ( ; up > 0 && item > 1; --up)
5113 {
5114 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5115 --item;
5116 }
5117 *STACK_TV_BOT(-item) = tmp_tv;
5118 }
5119 break;
5120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005122 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005124 ectx->ec_where.wt_index = 0;
5125 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126 break;
5127 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005128 continue;
5129
Bram Moolenaard032f342020-07-18 18:13:02 +02005130func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005131 // Restore previous function. If the frame pointer is where we started
5132 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005133 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005134 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005135
Bram Moolenaar4c137212021-04-19 16:48:48 +02005136 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005137 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005138 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005139 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005140
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005141on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005142 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005143 // If "emsg_silent" is set then ignore the error, unless it was set
5144 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005145 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005146 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005147 {
5148 // If a sequence of instructions causes an error while ":silent!"
5149 // was used, restore the stack length and jump ahead to restoring
5150 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005151 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005152 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005153 while (ectx->ec_stack.ga_len
5154 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005155 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005156 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005157 clear_tv(STACK_TV_BOT(0));
5158 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005159 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5160 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005161 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005162 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005163 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005164on_fatal_error:
5165 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005166 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005167 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005168 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005169 }
5170
5171done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005172 ret = OK;
5173theend:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005174 {
5175 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5176 + ectx->ec_dfunc_idx;
5177
5178 if (dfunc->df_defer_var_idx > 0)
5179 invoke_defer_funcs(ectx);
5180 }
5181
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005182 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005183 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5184 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005185}
5186
5187/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005188 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005189 * "rettv".
5190 * Return OK or FAIL.
5191 */
5192 int
5193exe_typval_instr(typval_T *tv, typval_T *rettv)
5194{
5195 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5196 isn_T *save_instr = ectx->ec_instr;
5197 int save_iidx = ectx->ec_iidx;
5198 int res;
5199
LemonBoyf3b48952022-05-05 13:53:03 +01005200 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5201 // even when the compilation fails.
5202 rettv->v_type = VAR_UNKNOWN;
5203
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005204 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5205 res = exec_instructions(ectx);
5206 if (res == OK)
5207 {
5208 *rettv = *STACK_TV_BOT(-1);
5209 --ectx->ec_stack.ga_len;
5210 }
5211
5212 ectx->ec_instr = save_instr;
5213 ectx->ec_iidx = save_iidx;
5214
5215 return res;
5216}
5217
5218/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005219 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5220 * "substitute_instr".
5221 */
5222 char_u *
5223exe_substitute_instr(void)
5224{
5225 ectx_T *ectx = substitute_instr->subs_ectx;
5226 isn_T *save_instr = ectx->ec_instr;
5227 int save_iidx = ectx->ec_iidx;
5228 char_u *res;
5229
5230 ectx->ec_instr = substitute_instr->subs_instr;
5231 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005232 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005233 typval_T *tv = STACK_TV_BOT(-1);
5234
Bram Moolenaar27523602021-06-05 21:36:19 +02005235 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005236 --ectx->ec_stack.ga_len;
5237 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005238 }
5239 else
5240 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005241 substitute_instr->subs_status = FAIL;
5242 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005244
Bram Moolenaar4c137212021-04-19 16:48:48 +02005245 ectx->ec_instr = save_instr;
5246 ectx->ec_iidx = save_iidx;
5247
5248 return res;
5249}
5250
5251/*
5252 * Call a "def" function from old Vim script.
5253 * Return OK or FAIL.
5254 */
5255 int
5256call_def_function(
5257 ufunc_T *ufunc,
5258 int argc_arg, // nr of arguments
5259 typval_T *argv, // arguments
5260 partial_T *partial, // optional partial for context
5261 typval_T *rettv) // return value
5262{
5263 ectx_T ectx; // execution context
5264 int argc = argc_arg;
5265 typval_T *tv;
5266 int idx;
5267 int ret = FAIL;
5268 int defcount = ufunc->uf_args.ga_len - argc;
5269 sctx_T save_current_sctx = current_sctx;
5270 int did_emsg_before = did_emsg_cumul + did_emsg;
5271 int save_suppress_errthrow = suppress_errthrow;
5272 msglist_T **saved_msg_list = NULL;
5273 msglist_T *private_msg_list = NULL;
5274 int save_emsg_silent_def = emsg_silent_def;
5275 int save_did_emsg_def = did_emsg_def;
5276 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005277 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005278
5279// Get pointer to item in the stack.
5280#undef STACK_TV
5281#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5282
5283// Get pointer to item at the bottom of the stack, -1 is the bottom.
5284#undef STACK_TV_BOT
5285#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5286
5287// Get pointer to a local variable on the stack. Negative for arguments.
5288#undef STACK_TV_VAR
5289#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5290
5291 if (ufunc->uf_def_status == UF_NOT_COMPILED
5292 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005293 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5294 && compile_def_function(ufunc, FALSE,
5295 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005296 {
5297 if (did_emsg_cumul + did_emsg == did_emsg_before)
5298 semsg(_(e_function_is_not_compiled_str),
5299 printable_func_name(ufunc));
5300 return FAIL;
5301 }
5302
5303 {
5304 // Check the function was really compiled.
5305 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5306 + ufunc->uf_dfunc_idx;
5307 if (INSTRUCTIONS(dfunc) == NULL)
5308 {
5309 iemsg("using call_def_function() on not compiled function");
5310 return FAIL;
5311 }
5312 }
5313
5314 // If depth of calling is getting too high, don't execute the function.
5315 orig_funcdepth = funcdepth_get();
5316 if (funcdepth_increment() == FAIL)
5317 return FAIL;
5318
5319 CLEAR_FIELD(ectx);
5320 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5321 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005322 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005323 {
5324 funcdepth_decrement();
5325 return FAIL;
5326 }
5327 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5328 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5329 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005330 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005331
5332 idx = argc - ufunc->uf_args.ga_len;
5333 if (idx > 0 && ufunc->uf_va_name == NULL)
5334 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005335 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
5336 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005337 goto failed_early;
5338 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005339 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5340 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005341 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005342 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
5343 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005344 goto failed_early;
5345 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005346
5347 // Put arguments on the stack, but no more than what the function expects.
5348 // A lambda can be called with more arguments than it uses.
5349 for (idx = 0; idx < argc
5350 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5351 ++idx)
5352 {
5353 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5354 && argv[idx].v_type == VAR_SPECIAL
5355 && argv[idx].vval.v_number == VVAL_NONE)
5356 {
5357 // Use the default value.
5358 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5359 }
5360 else
5361 {
5362 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5363 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005364 ufunc->uf_arg_types[idx], &argv[idx],
5365 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005366 goto failed_early;
5367 copy_tv(&argv[idx], STACK_TV_BOT(0));
5368 }
5369 ++ectx.ec_stack.ga_len;
5370 }
5371
5372 // Turn varargs into a list. Empty list if no args.
5373 if (ufunc->uf_va_name != NULL)
5374 {
5375 int vararg_count = argc - ufunc->uf_args.ga_len;
5376
5377 if (vararg_count < 0)
5378 vararg_count = 0;
5379 else
5380 argc -= vararg_count;
5381 if (exe_newlist(vararg_count, &ectx) == FAIL)
5382 goto failed_early;
5383
5384 // Check the type of the list items.
5385 tv = STACK_TV_BOT(-1);
5386 if (ufunc->uf_va_type != NULL
5387 && ufunc->uf_va_type != &t_list_any
5388 && ufunc->uf_va_type->tt_member != &t_any
5389 && tv->vval.v_list != NULL)
5390 {
5391 type_T *expected = ufunc->uf_va_type->tt_member;
5392 listitem_T *li = tv->vval.v_list->lv_first;
5393
5394 for (idx = 0; idx < vararg_count; ++idx)
5395 {
5396 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005397 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005398 goto failed_early;
5399 li = li->li_next;
5400 }
5401 }
5402
5403 if (defcount > 0)
5404 // Move varargs list to below missing default arguments.
5405 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5406 --ectx.ec_stack.ga_len;
5407 }
5408
5409 // Make space for omitted arguments, will store default value below.
5410 // Any varargs list goes after them.
5411 if (defcount > 0)
5412 for (idx = 0; idx < defcount; ++idx)
5413 {
5414 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5415 ++ectx.ec_stack.ga_len;
5416 }
5417 if (ufunc->uf_va_name != NULL)
5418 ++ectx.ec_stack.ga_len;
5419
5420 // Frame pointer points to just after arguments.
5421 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5422 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5423
5424 {
5425 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5426 + ufunc->uf_dfunc_idx;
5427 ufunc_T *base_ufunc = dfunc->df_ufunc;
5428
5429 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5430 // by copy_func().
5431 if (partial != NULL || base_ufunc->uf_partial != NULL)
5432 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005433 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5434 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005435 goto failed_early;
5436 if (partial != NULL)
5437 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005438 outer_T *outer = get_pt_outer(partial);
5439
5440 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005441 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005442 if (current_ectx != NULL)
5443 {
5444 if (current_ectx->ec_outer_ref != NULL
5445 && current_ectx->ec_outer_ref->or_outer != NULL)
5446 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005447 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005448 }
5449 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005450 }
5451 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005452 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005453 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005454 ++partial->pt_refcount;
5455 ectx.ec_outer_ref->or_partial = partial;
5456 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005457 }
5458 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005459 {
5460 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5461 ++base_ufunc->uf_partial->pt_refcount;
5462 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5463 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005464 }
5465 }
5466
5467 // dummy frame entries
5468 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5469 {
5470 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5471 ++ectx.ec_stack.ga_len;
5472 }
5473
5474 {
5475 // Reserve space for local variables and any closure reference count.
5476 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5477 + ufunc->uf_dfunc_idx;
5478
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005479 // Initialize variables to zero. That avoids having to generate
5480 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005481 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005482 {
5483 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5484 STACK_TV_VAR(idx)->vval.v_number = 0;
5485 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005486 ectx.ec_stack.ga_len += dfunc->df_varcount;
5487 if (dfunc->df_has_closure)
5488 {
5489 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5490 STACK_TV_VAR(idx)->vval.v_number = 0;
5491 ++ectx.ec_stack.ga_len;
5492 }
5493
5494 ectx.ec_instr = INSTRUCTIONS(dfunc);
5495 }
5496
5497 // Following errors are in the function, not the caller.
5498 // Commands behave like vim9script.
5499 estack_push_ufunc(ufunc, 1);
5500 current_sctx = ufunc->uf_script_ctx;
5501 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5502
5503 // Use a specific location for storing error messages to be converted to an
5504 // exception.
5505 saved_msg_list = msg_list;
5506 msg_list = &private_msg_list;
5507
5508 // Do turn errors into exceptions.
5509 suppress_errthrow = FALSE;
5510
5511 // Do not delete the function while executing it.
5512 ++ufunc->uf_calls;
5513
5514 // When ":silent!" was used before calling then we still abort the
5515 // function. If ":silent!" is used in the function then we don't.
5516 emsg_silent_def = emsg_silent;
5517 did_emsg_def = 0;
5518
5519 ectx.ec_where.wt_index = 0;
5520 ectx.ec_where.wt_variable = FALSE;
5521
5522 // Execute the instructions until done.
5523 ret = exec_instructions(&ectx);
5524 if (ret == OK)
5525 {
5526 // function finished, get result from the stack.
5527 if (ufunc->uf_ret_type == &t_void)
5528 {
5529 rettv->v_type = VAR_VOID;
5530 }
5531 else
5532 {
5533 tv = STACK_TV_BOT(-1);
5534 *rettv = *tv;
5535 tv->v_type = VAR_UNKNOWN;
5536 }
5537 }
5538
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005539 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005540 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5541 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005542
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005543 // Deal with any remaining closures, they may be in use somewhere.
5544 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005545 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005546 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005547 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005548 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005549
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005550 estack_pop();
5551 current_sctx = save_current_sctx;
5552
Bram Moolenaar2336c372021-12-06 15:06:54 +00005553 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5554 // Function was unreferenced while being used, free it now.
5555 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005556
Bram Moolenaar352134b2020-10-17 22:04:08 +02005557 if (*msg_list != NULL && saved_msg_list != NULL)
5558 {
5559 msglist_T **plist = saved_msg_list;
5560
5561 // Append entries from the current msg_list (uncaught exceptions) to
5562 // the saved msg_list.
5563 while (*plist != NULL)
5564 plist = &(*plist)->next;
5565
5566 *plist = *msg_list;
5567 }
5568 msg_list = saved_msg_list;
5569
Bram Moolenaar4c137212021-04-19 16:48:48 +02005570 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005571 {
5572 cmdmod.cmod_filter_regmatch.regprog = NULL;
5573 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005574 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005575 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005576 emsg_silent_def = save_emsg_silent_def;
5577 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005578
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005579failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005580 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5582 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005583 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005586 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005587 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005588 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005589 if (ectx.ec_outer_ref->or_outer_allocated)
5590 vim_free(ectx.ec_outer_ref->or_outer);
5591 partial_unref(ectx.ec_outer_ref->or_partial);
5592 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005593 }
5594
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005595 // Not sure if this is necessary.
5596 suppress_errthrow = save_suppress_errthrow;
5597
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005598 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5599 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005600 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005601 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005602 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005603 return ret;
5604}
5605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005606/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005607 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5608 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5609 * "pfx" is prefixed to every line.
5610 */
5611 static void
5612list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5613{
5614 int line_idx = 0;
5615 int prev_current = 0;
5616 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005617 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005618
5619 for (current = 0; current < instr_count; ++current)
5620 {
5621 isn_T *iptr = &instr[current];
5622 char *line;
5623
5624 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005625 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005626 while (line_idx < iptr->isn_lnum
5627 && line_idx < ufunc->uf_lines.ga_len)
5628 {
5629 if (current > prev_current)
5630 {
5631 msg_puts("\n\n");
5632 prev_current = current;
5633 }
5634 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5635 if (line != NULL)
5636 msg(line);
5637 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005638 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5639 {
5640 int first_def_arg = ufunc->uf_args.ga_len
5641 - ufunc->uf_def_args.ga_len;
5642
5643 if (def_arg_idx > 0)
5644 msg_puts("\n\n");
5645 msg_start();
5646 msg_puts(" ");
5647 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5648 first_def_arg + def_arg_idx]);
5649 msg_puts(" = ");
5650 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5651 msg_clr_eos();
5652 msg_end();
5653 }
5654 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005655
5656 switch (iptr->isn_type)
5657 {
5658 case ISN_EXEC:
5659 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5660 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005661 case ISN_EXEC_SPLIT:
5662 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5663 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005664 case ISN_EXECRANGE:
5665 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5666 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005667 case ISN_LEGACY_EVAL:
5668 smsg("%s%4d EVAL legacy %s", pfx, current,
5669 iptr->isn_arg.string);
5670 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005671 case ISN_REDIRSTART:
5672 smsg("%s%4d REDIR", pfx, current);
5673 break;
5674 case ISN_REDIREND:
5675 smsg("%s%4d REDIR END%s", pfx, current,
5676 iptr->isn_arg.number ? " append" : "");
5677 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005678 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005679#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005680 smsg("%s%4d CEXPR pre %s", pfx, current,
5681 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005682#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005683 break;
5684 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005685#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005686 {
5687 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5688
5689 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5690 cexpr_get_auname(cer->cer_cmdidx),
5691 cer->cer_forceit ? "!" : "",
5692 cer->cer_cmdline);
5693 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005694#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005695 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005696 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005697 smsg("%s%4d INSTR", pfx, current);
5698 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5699 msg(" -------------");
5700 break;
5701 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005702 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005703 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5704
5705 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005706 }
5707 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005708 case ISN_SUBSTITUTE:
5709 {
5710 subs_T *subs = &iptr->isn_arg.subs;
5711
5712 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5713 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5714 msg(" -------------");
5715 }
5716 break;
5717 case ISN_EXECCONCAT:
5718 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5719 (varnumber_T)iptr->isn_arg.number);
5720 break;
5721 case ISN_ECHO:
5722 {
5723 echo_T *echo = &iptr->isn_arg.echo;
5724
5725 smsg("%s%4d %s %d", pfx, current,
5726 echo->echo_with_white ? "ECHO" : "ECHON",
5727 echo->echo_count);
5728 }
5729 break;
5730 case ISN_EXECUTE:
5731 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005732 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005733 break;
5734 case ISN_ECHOMSG:
5735 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005736 (varnumber_T)(iptr->isn_arg.number));
5737 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01005738 case ISN_ECHOWINDOW:
5739 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
5740 (varnumber_T)(iptr->isn_arg.number));
5741 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02005742 case ISN_ECHOCONSOLE:
5743 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5744 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005745 break;
5746 case ISN_ECHOERR:
5747 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005748 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005749 break;
5750 case ISN_LOAD:
5751 {
5752 if (iptr->isn_arg.number < 0)
5753 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5754 (varnumber_T)(iptr->isn_arg.number
5755 + STACK_FRAME_SIZE));
5756 else
5757 smsg("%s%4d LOAD $%lld", pfx, current,
5758 (varnumber_T)(iptr->isn_arg.number));
5759 }
5760 break;
5761 case ISN_LOADOUTER:
5762 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005763 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005764 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5765 iptr->isn_arg.outer.outer_depth,
5766 iptr->isn_arg.outer.outer_idx
5767 + STACK_FRAME_SIZE);
5768 else
5769 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5770 iptr->isn_arg.outer.outer_depth,
5771 iptr->isn_arg.outer.outer_idx);
5772 }
5773 break;
5774 case ISN_LOADV:
5775 smsg("%s%4d LOADV v:%s", pfx, current,
5776 get_vim_var_name(iptr->isn_arg.number));
5777 break;
5778 case ISN_LOADSCRIPT:
5779 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005780 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5781 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5782 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005783
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005784 sv = get_script_svar(sref, -1);
5785 if (sv == NULL)
5786 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5787 pfx, current, si->sn_name);
5788 else
5789 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005790 sv->sv_name,
5791 sref->sref_idx,
5792 si->sn_name);
5793 }
5794 break;
5795 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005796 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005797 {
5798 scriptitem_T *si = SCRIPT_ITEM(
5799 iptr->isn_arg.loadstore.ls_sid);
5800
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005801 smsg("%s%4d %s s:%s from %s", pfx, current,
5802 iptr->isn_type == ISN_LOADS ? "LOADS"
5803 : "LOADEXPORT",
5804 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005805 }
5806 break;
5807 case ISN_LOADAUTO:
5808 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5809 break;
5810 case ISN_LOADG:
5811 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5812 break;
5813 case ISN_LOADB:
5814 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5815 break;
5816 case ISN_LOADW:
5817 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5818 break;
5819 case ISN_LOADT:
5820 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5821 break;
5822 case ISN_LOADGDICT:
5823 smsg("%s%4d LOAD g:", pfx, current);
5824 break;
5825 case ISN_LOADBDICT:
5826 smsg("%s%4d LOAD b:", pfx, current);
5827 break;
5828 case ISN_LOADWDICT:
5829 smsg("%s%4d LOAD w:", pfx, current);
5830 break;
5831 case ISN_LOADTDICT:
5832 smsg("%s%4d LOAD t:", pfx, current);
5833 break;
5834 case ISN_LOADOPT:
5835 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5836 break;
5837 case ISN_LOADENV:
5838 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5839 break;
5840 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005841 smsg("%s%4d LOADREG @%c", pfx, current,
5842 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005843 break;
5844
5845 case ISN_STORE:
5846 if (iptr->isn_arg.number < 0)
5847 smsg("%s%4d STORE arg[%lld]", pfx, current,
5848 iptr->isn_arg.number + STACK_FRAME_SIZE);
5849 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005850 smsg("%s%4d STORE $%lld", pfx, current,
5851 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005852 break;
5853 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005854 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5855 iptr->isn_arg.outer.outer_depth,
5856 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005857 break;
5858 case ISN_STOREV:
5859 smsg("%s%4d STOREV v:%s", pfx, current,
5860 get_vim_var_name(iptr->isn_arg.number));
5861 break;
5862 case ISN_STOREAUTO:
5863 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5864 break;
5865 case ISN_STOREG:
5866 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5867 break;
5868 case ISN_STOREB:
5869 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5870 break;
5871 case ISN_STOREW:
5872 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5873 break;
5874 case ISN_STORET:
5875 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5876 break;
5877 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005878 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005879 {
5880 scriptitem_T *si = SCRIPT_ITEM(
5881 iptr->isn_arg.loadstore.ls_sid);
5882
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005883 smsg("%s%4d %s %s in %s", pfx, current,
5884 iptr->isn_type == ISN_STORES
5885 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005886 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5887 }
5888 break;
5889 case ISN_STORESCRIPT:
5890 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005891 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5892 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5893 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005894
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005895 sv = get_script_svar(sref, -1);
5896 if (sv == NULL)
5897 smsg("%s%4d STORESCRIPT [deleted] in %s",
5898 pfx, current, si->sn_name);
5899 else
5900 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005901 sv->sv_name,
5902 sref->sref_idx,
5903 si->sn_name);
5904 }
5905 break;
5906 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005907 case ISN_STOREFUNCOPT:
5908 smsg("%s%4d %s &%s", pfx, current,
5909 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005910 iptr->isn_arg.storeopt.so_name);
5911 break;
5912 case ISN_STOREENV:
5913 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5914 break;
5915 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005916 smsg("%s%4d STOREREG @%c", pfx, current,
5917 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005918 break;
5919 case ISN_STORENR:
5920 smsg("%s%4d STORE %lld in $%d", pfx, current,
5921 iptr->isn_arg.storenr.stnr_val,
5922 iptr->isn_arg.storenr.stnr_idx);
5923 break;
5924
5925 case ISN_STOREINDEX:
5926 smsg("%s%4d STOREINDEX %s", pfx, current,
5927 vartype_name(iptr->isn_arg.vartype));
5928 break;
5929
5930 case ISN_STORERANGE:
5931 smsg("%s%4d STORERANGE", pfx, current);
5932 break;
5933
5934 // constants
5935 case ISN_PUSHNR:
5936 smsg("%s%4d PUSHNR %lld", pfx, current,
5937 (varnumber_T)(iptr->isn_arg.number));
5938 break;
5939 case ISN_PUSHBOOL:
5940 case ISN_PUSHSPEC:
5941 smsg("%s%4d PUSH %s", pfx, current,
5942 get_var_special_name(iptr->isn_arg.number));
5943 break;
5944 case ISN_PUSHF:
5945#ifdef FEAT_FLOAT
5946 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5947#endif
5948 break;
5949 case ISN_PUSHS:
5950 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5951 break;
5952 case ISN_PUSHBLOB:
5953 {
5954 char_u *r;
5955 char_u numbuf[NUMBUFLEN];
5956 char_u *tofree;
5957
5958 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5959 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5960 vim_free(tofree);
5961 }
5962 break;
5963 case ISN_PUSHFUNC:
5964 {
5965 char *name = (char *)iptr->isn_arg.string;
5966
5967 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5968 name == NULL ? "[none]" : name);
5969 }
5970 break;
5971 case ISN_PUSHCHANNEL:
5972#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005973 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005974#endif
5975 break;
5976 case ISN_PUSHJOB:
5977#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005978 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005979#endif
5980 break;
5981 case ISN_PUSHEXC:
5982 smsg("%s%4d PUSH v:exception", pfx, current);
5983 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005984 case ISN_AUTOLOAD:
5985 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5986 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005987 case ISN_UNLET:
5988 smsg("%s%4d UNLET%s %s", pfx, current,
5989 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5990 iptr->isn_arg.unlet.ul_name);
5991 break;
5992 case ISN_UNLETENV:
5993 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5994 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5995 iptr->isn_arg.unlet.ul_name);
5996 break;
5997 case ISN_UNLETINDEX:
5998 smsg("%s%4d UNLETINDEX", pfx, current);
5999 break;
6000 case ISN_UNLETRANGE:
6001 smsg("%s%4d UNLETRANGE", pfx, current);
6002 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006003 case ISN_LOCKUNLOCK:
6004 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6005 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006006 case ISN_LOCKCONST:
6007 smsg("%s%4d LOCKCONST", pfx, current);
6008 break;
6009 case ISN_NEWLIST:
6010 smsg("%s%4d NEWLIST size %lld", pfx, current,
6011 (varnumber_T)(iptr->isn_arg.number));
6012 break;
6013 case ISN_NEWDICT:
6014 smsg("%s%4d NEWDICT size %lld", pfx, current,
6015 (varnumber_T)(iptr->isn_arg.number));
6016 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006017 case ISN_NEWPARTIAL:
6018 smsg("%s%4d NEWPARTIAL", pfx, current);
6019 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006020
6021 // function call
6022 case ISN_BCALL:
6023 {
6024 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6025
6026 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6027 internal_func_name(cbfunc->cbf_idx),
6028 cbfunc->cbf_argcount);
6029 }
6030 break;
6031 case ISN_DCALL:
6032 {
6033 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6034 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6035 + cdfunc->cdf_idx;
6036
6037 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006038 printable_func_name(df->df_ufunc),
6039 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006040 }
6041 break;
6042 case ISN_UCALL:
6043 {
6044 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6045
6046 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6047 cufunc->cuf_name, cufunc->cuf_argcount);
6048 }
6049 break;
6050 case ISN_PCALL:
6051 {
6052 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6053
6054 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6055 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6056 }
6057 break;
6058 case ISN_PCALL_END:
6059 smsg("%s%4d PCALL end", pfx, current);
6060 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006061 case ISN_DEFER:
6062 smsg("%s%4d DEFER %d args", pfx, current,
6063 (int)iptr->isn_arg.defer.defer_argcount);
6064 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006065 case ISN_RETURN:
6066 smsg("%s%4d RETURN", pfx, current);
6067 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006068 case ISN_RETURN_VOID:
6069 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006070 break;
6071 case ISN_FUNCREF:
6072 {
6073 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00006074 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006075
Bram Moolenaar38453522021-11-28 22:00:12 +00006076 if (funcref->fr_func_name == NULL)
6077 {
6078 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6079 + funcref->fr_dfunc_idx;
6080 name = df->df_ufunc->uf_name;
6081 }
6082 else
6083 name = funcref->fr_func_name;
6084 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006085 }
6086 break;
6087
6088 case ISN_NEWFUNC:
6089 {
6090 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
6091
6092 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6093 newfunc->nf_lambda, newfunc->nf_global);
6094 }
6095 break;
6096
6097 case ISN_DEF:
6098 {
6099 char_u *name = iptr->isn_arg.string;
6100
6101 smsg("%s%4d DEF %s", pfx, current,
6102 name == NULL ? (char_u *)"" : name);
6103 }
6104 break;
6105
6106 case ISN_JUMP:
6107 {
6108 char *when = "?";
6109
6110 switch (iptr->isn_arg.jump.jump_when)
6111 {
6112 case JUMP_ALWAYS:
6113 when = "JUMP";
6114 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006115 case JUMP_NEVER:
6116 iemsg("JUMP_NEVER should not be used");
6117 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006118 case JUMP_AND_KEEP_IF_TRUE:
6119 when = "JUMP_AND_KEEP_IF_TRUE";
6120 break;
6121 case JUMP_IF_FALSE:
6122 when = "JUMP_IF_FALSE";
6123 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006124 case JUMP_IF_COND_FALSE:
6125 when = "JUMP_IF_COND_FALSE";
6126 break;
6127 case JUMP_IF_COND_TRUE:
6128 when = "JUMP_IF_COND_TRUE";
6129 break;
6130 }
6131 smsg("%s%4d %s -> %d", pfx, current, when,
6132 iptr->isn_arg.jump.jump_where);
6133 }
6134 break;
6135
6136 case ISN_JUMP_IF_ARG_SET:
6137 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6138 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6139 iptr->isn_arg.jump.jump_where);
6140 break;
6141
6142 case ISN_FOR:
6143 {
6144 forloop_T *forloop = &iptr->isn_arg.forloop;
6145
6146 smsg("%s%4d FOR $%d -> %d", pfx, current,
6147 forloop->for_idx, forloop->for_end);
6148 }
6149 break;
6150
6151 case ISN_TRY:
6152 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006153 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006154
6155 if (try->try_ref->try_finally == 0)
6156 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6157 pfx, current,
6158 try->try_ref->try_catch,
6159 try->try_ref->try_endtry);
6160 else
6161 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6162 pfx, current,
6163 try->try_ref->try_catch,
6164 try->try_ref->try_finally,
6165 try->try_ref->try_endtry);
6166 }
6167 break;
6168 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006169 smsg("%s%4d CATCH", pfx, current);
6170 break;
6171 case ISN_TRYCONT:
6172 {
6173 trycont_T *trycont = &iptr->isn_arg.trycont;
6174
6175 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6176 trycont->tct_levels,
6177 trycont->tct_levels == 1 ? "" : "s",
6178 trycont->tct_where);
6179 }
6180 break;
6181 case ISN_FINALLY:
6182 smsg("%s%4d FINALLY", pfx, current);
6183 break;
6184 case ISN_ENDTRY:
6185 smsg("%s%4d ENDTRY", pfx, current);
6186 break;
6187 case ISN_THROW:
6188 smsg("%s%4d THROW", pfx, current);
6189 break;
6190
6191 // expression operations on number
6192 case ISN_OPNR:
6193 case ISN_OPFLOAT:
6194 case ISN_OPANY:
6195 {
6196 char *what;
6197 char *ins;
6198
6199 switch (iptr->isn_arg.op.op_type)
6200 {
6201 case EXPR_MULT: what = "*"; break;
6202 case EXPR_DIV: what = "/"; break;
6203 case EXPR_REM: what = "%"; break;
6204 case EXPR_SUB: what = "-"; break;
6205 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006206 case EXPR_LSHIFT: what = "<<"; break;
6207 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006208 default: what = "???"; break;
6209 }
6210 switch (iptr->isn_type)
6211 {
6212 case ISN_OPNR: ins = "OPNR"; break;
6213 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6214 case ISN_OPANY: ins = "OPANY"; break;
6215 default: ins = "???"; break;
6216 }
6217 smsg("%s%4d %s %s", pfx, current, ins, what);
6218 }
6219 break;
6220
6221 case ISN_COMPAREBOOL:
6222 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006223 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006224 case ISN_COMPARENR:
6225 case ISN_COMPAREFLOAT:
6226 case ISN_COMPARESTRING:
6227 case ISN_COMPAREBLOB:
6228 case ISN_COMPARELIST:
6229 case ISN_COMPAREDICT:
6230 case ISN_COMPAREFUNC:
6231 case ISN_COMPAREANY:
6232 {
6233 char *p;
6234 char buf[10];
6235 char *type;
6236
6237 switch (iptr->isn_arg.op.op_type)
6238 {
6239 case EXPR_EQUAL: p = "=="; break;
6240 case EXPR_NEQUAL: p = "!="; break;
6241 case EXPR_GREATER: p = ">"; break;
6242 case EXPR_GEQUAL: p = ">="; break;
6243 case EXPR_SMALLER: p = "<"; break;
6244 case EXPR_SEQUAL: p = "<="; break;
6245 case EXPR_MATCH: p = "=~"; break;
6246 case EXPR_IS: p = "is"; break;
6247 case EXPR_ISNOT: p = "isnot"; break;
6248 case EXPR_NOMATCH: p = "!~"; break;
6249 default: p = "???"; break;
6250 }
6251 STRCPY(buf, p);
6252 if (iptr->isn_arg.op.op_ic == TRUE)
6253 strcat(buf, "?");
6254 switch(iptr->isn_type)
6255 {
6256 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6257 case ISN_COMPARESPECIAL:
6258 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006259 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006260 case ISN_COMPARENR: type = "COMPARENR"; break;
6261 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6262 case ISN_COMPARESTRING:
6263 type = "COMPARESTRING"; break;
6264 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6265 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6266 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6267 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6268 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6269 default: type = "???"; break;
6270 }
6271
6272 smsg("%s%4d %s %s", pfx, current, type, buf);
6273 }
6274 break;
6275
6276 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6277 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6278
6279 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006280 case ISN_CONCAT:
6281 smsg("%s%4d CONCAT size %lld", pfx, current,
6282 (varnumber_T)(iptr->isn_arg.number));
6283 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006284 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6285 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6286 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6287 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6288 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6289 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6290 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6291 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6292 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6293 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6294 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006295 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006296 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6297 iptr->isn_arg.getitem.gi_index,
6298 iptr->isn_arg.getitem.gi_with_op ?
6299 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006300 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6301 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6302 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006303 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6304 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6305
Bram Moolenaar4c137212021-04-19 16:48:48 +02006306 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6307
Bram Moolenaar4c137212021-04-19 16:48:48 +02006308 case ISN_CHECKTYPE:
6309 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006310 checktype_T *ct = &iptr->isn_arg.type;
6311 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006312
6313 if (ct->ct_arg_idx == 0)
6314 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6315 type_name(ct->ct_type, &tofree),
6316 (int)ct->ct_off);
6317 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006318 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006319 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006320 type_name(ct->ct_type, &tofree),
6321 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006322 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006323 (int)ct->ct_arg_idx);
6324 vim_free(tofree);
6325 break;
6326 }
6327 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6328 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6329 iptr->isn_arg.checklen.cl_min_len);
6330 break;
6331 case ISN_SETTYPE:
6332 {
6333 char *tofree;
6334
6335 smsg("%s%4d SETTYPE %s", pfx, current,
6336 type_name(iptr->isn_arg.type.ct_type, &tofree));
6337 vim_free(tofree);
6338 break;
6339 }
6340 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006341 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6342 smsg("%s%4d INVERT %d (!val)", pfx, current,
6343 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006344 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006345 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6346 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006347 break;
6348 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006349 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006350 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006351 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6352 pfx, current,
6353 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006354 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006355 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6356 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006357 break;
6358 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006359 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006360 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006361 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006362 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6363 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006364 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006365 else
6366 smsg("%s%4d PUT %c %ld", pfx, current,
6367 iptr->isn_arg.put.put_regname,
6368 (long)iptr->isn_arg.put.put_lnum);
6369 break;
6370
Bram Moolenaar4c137212021-04-19 16:48:48 +02006371 case ISN_CMDMOD:
6372 {
6373 char_u *buf;
6374 size_t len = produce_cmdmods(
6375 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6376
6377 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006378 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006379 {
6380 (void)produce_cmdmods(
6381 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6382 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6383 vim_free(buf);
6384 }
6385 break;
6386 }
6387 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6388
6389 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006390 smsg("%s%4d PROFILE START line %d", pfx, current,
6391 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006392 break;
6393
6394 case ISN_PROF_END:
6395 smsg("%s%4d PROFILE END", pfx, current);
6396 break;
6397
Bram Moolenaare99d4222021-06-13 14:01:26 +02006398 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006399 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6400 iptr->isn_arg.debug.dbg_break_lnum + 1,
6401 iptr->isn_lnum,
6402 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006403 break;
6404
Bram Moolenaar4c137212021-04-19 16:48:48 +02006405 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6406 iptr->isn_arg.unpack.unp_count,
6407 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6408 break;
6409 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6410 iptr->isn_arg.shuffle.shfl_item,
6411 iptr->isn_arg.shuffle.shfl_up);
6412 break;
6413 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6414
6415 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6416 return;
6417 }
6418
6419 out_flush(); // output one line at a time
6420 ui_breakcheck();
6421 if (got_int)
6422 break;
6423 }
6424}
6425
6426/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006427 * Handle command line completion for the :disassemble command.
6428 */
6429 void
6430set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6431{
6432 char_u *p;
6433
6434 // Default: expand user functions, "debug" and "profile"
6435 xp->xp_context = EXPAND_DISASSEMBLE;
6436 xp->xp_pattern = arg;
6437
6438 // first argument already typed: only user function names
6439 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6440 {
6441 xp->xp_context = EXPAND_USER_FUNC;
6442 xp->xp_pattern = skipwhite(p);
6443 }
6444}
6445
6446/*
6447 * Function given to ExpandGeneric() to obtain the list of :disassemble
6448 * arguments.
6449 */
6450 char_u *
6451get_disassemble_argument(expand_T *xp, int idx)
6452{
6453 if (idx == 0)
6454 return (char_u *)"debug";
6455 if (idx == 1)
6456 return (char_u *)"profile";
6457 return get_user_func_name(xp, idx - 2);
6458}
6459
6460/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006461 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006462 * We don't really need this at runtime, but we do have tests that require it,
6463 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464 */
6465 void
6466ex_disassemble(exarg_T *eap)
6467{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006468 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006469 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006470 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006471 isn_T *instr = NULL; // init to shut up gcc warning
6472 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006473 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006474
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006475 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006476 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006478 if (func_needs_compiling(ufunc, compile_type)
6479 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006480 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006481 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006483 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006484 return;
6485 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006486 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006487
6488 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006489 switch (compile_type)
6490 {
6491 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006492#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006493 instr = dfunc->df_instr_prof;
6494 instr_count = dfunc->df_instr_prof_count;
6495 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006496#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006497 // FALLTHROUGH
6498 case CT_NONE:
6499 instr = dfunc->df_instr;
6500 instr_count = dfunc->df_instr_count;
6501 break;
6502 case CT_DEBUG:
6503 instr = dfunc->df_instr_debug;
6504 instr_count = dfunc->df_instr_debug_count;
6505 break;
6506 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006507
Bram Moolenaar4c137212021-04-19 16:48:48 +02006508 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509}
6510
6511/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006512 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 * list, etc. Mostly like what JavaScript does, except that empty list and
6514 * empty dictionary are FALSE.
6515 */
6516 int
6517tv2bool(typval_T *tv)
6518{
6519 switch (tv->v_type)
6520 {
6521 case VAR_NUMBER:
6522 return tv->vval.v_number != 0;
6523 case VAR_FLOAT:
6524#ifdef FEAT_FLOAT
6525 return tv->vval.v_float != 0.0;
6526#else
6527 break;
6528#endif
6529 case VAR_PARTIAL:
6530 return tv->vval.v_partial != NULL;
6531 case VAR_FUNC:
6532 case VAR_STRING:
6533 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6534 case VAR_LIST:
6535 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6536 case VAR_DICT:
6537 return tv->vval.v_dict != NULL
6538 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6539 case VAR_BOOL:
6540 case VAR_SPECIAL:
6541 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6542 case VAR_JOB:
6543#ifdef FEAT_JOB_CHANNEL
6544 return tv->vval.v_job != NULL;
6545#else
6546 break;
6547#endif
6548 case VAR_CHANNEL:
6549#ifdef FEAT_JOB_CHANNEL
6550 return tv->vval.v_channel != NULL;
6551#else
6552 break;
6553#endif
6554 case VAR_BLOB:
6555 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6556 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006557 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006559 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560 break;
6561 }
6562 return FALSE;
6563}
6564
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006565 void
6566emsg_using_string_as(typval_T *tv, int as_number)
6567{
6568 semsg(_(as_number ? e_using_string_as_number_str
6569 : e_using_string_as_bool_str),
6570 tv->vval.v_string == NULL
6571 ? (char_u *)"" : tv->vval.v_string);
6572}
6573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006574/*
6575 * If "tv" is a string give an error and return FAIL.
6576 */
6577 int
6578check_not_string(typval_T *tv)
6579{
6580 if (tv->v_type == VAR_STRING)
6581 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006582 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583 clear_tv(tv);
6584 return FAIL;
6585 }
6586 return OK;
6587}
6588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589
6590#endif // FEAT_EVAL