blob: 3194f872f4c758d1fb6ade24759727d2b53f119a [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");
940 vim_free(func_tv.vval.v_string);
941 return FAIL;
942 }
943 func_tv.v_type = VAR_FUNC;
944 func_tv.v_lock = 0;
945 func_tv.vval.v_string = name;
946
947 l = add_defer_item(dfunc->df_defer_var_idx - 1, 1, current_ectx);
948 if (l == NULL)
949 {
950 vim_free(func_tv.vval.v_string);
951 return FAIL;
952 }
953
954 list_set_item(l, 0, &func_tv);
955 for (i = 0; i < argcount; ++i)
956 list_set_item(l, i + 1, argvars + i);
957 return OK;
958}
959
960/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100961 * Invoked when returning from a function: Invoke any deferred calls.
962 */
963 static void
964invoke_defer_funcs(ectx_T *ectx)
965{
966 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
967 + ectx->ec_dfunc_idx;
968 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
969 listitem_T *li;
970
971 if (defer_tv->v_type != VAR_LIST)
972 return; // no function added
973 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
974 {
975 list_T *l = li->li_tv.vval.v_list;
976 typval_T rettv;
977 typval_T argvars[MAX_FUNC_ARGS];
978 int i;
979 listitem_T *arg_li = l->lv_first;
980 funcexe_T funcexe;
981
982 for (i = 0; i < l->lv_len - 1; ++i)
983 {
984 arg_li = arg_li->li_next;
985 argvars[i] = arg_li->li_tv;
986 }
987
988 CLEAR_FIELD(funcexe);
989 funcexe.fe_evaluate = TRUE;
990 rettv.v_type = VAR_UNKNOWN;
991 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
992 &rettv, l->lv_len - 1, argvars, &funcexe);
993 clear_tv(&rettv);
994 }
995}
996
997/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 * Return from the current function.
999 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001000 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001001func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001004 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001005 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1006 + ectx->ec_dfunc_idx;
1007 int argcount = ufunc_argcount(dfunc->df_ufunc);
1008 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001009 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001010 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1011 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001012 funclocal_T *floc;
1013#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001014 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1015 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001016
Bram Moolenaar12d26532021-02-19 19:13:21 +01001017 if (do_profiling == PROF_YES)
1018 {
1019 ufunc_T *caller = prev_dfunc->df_ufunc;
1020
1021 if (dfunc->df_ufunc->uf_profiling
1022 || (caller != NULL && caller->uf_profiling))
1023 {
1024 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1025 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1026 --profile_info_ga.ga_len;
1027 }
1028 }
1029#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001030
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001031 if (dfunc->df_defer_var_idx > 0)
1032 invoke_defer_funcs(ectx);
1033
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001034 // No check for uf_refcount being zero, cannot think of a way that would
1035 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001036 --dfunc->df_ufunc->uf_calls;
1037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001039 entry = estack_pop();
1040 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001041 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001043 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1044 return FAIL;
1045
1046 // Clear the arguments.
1047 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1048 clear_tv(STACK_TV(idx));
1049
1050 // Clear local variables and temp values, but not the return value.
1051 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001052 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001054
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001055 // The return value should be on top of the stack. However, when aborting
1056 // it may not be there and ec_frame_idx is the top of the stack.
1057 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001058 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001059 ret_idx = 0;
1060
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001061 if (ectx->ec_outer_ref != NULL)
1062 {
1063 if (ectx->ec_outer_ref->or_outer_allocated)
1064 vim_free(ectx->ec_outer_ref->or_outer);
1065 partial_unref(ectx->ec_outer_ref->or_partial);
1066 vim_free(ectx->ec_outer_ref);
1067 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001068
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001069 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001070 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001071 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1072 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001073 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1074 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001075 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001076 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001077 floc = (void *)STACK_TV(ectx->ec_frame_idx
1078 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001079 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001080 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1081 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001082
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001083 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001084 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001085 else
1086 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001087 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001088 vim_free(floc);
1089 }
1090
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001091 if (ret_idx > 0)
1092 {
1093 // Reset the stack to the position before the call, with a spot for the
1094 // return value, moved there from above the frame.
1095 ectx->ec_stack.ga_len = top + 1;
1096 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1097 }
1098 else
1099 // Reset the stack to the position before the call.
1100 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001101
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001102 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001103 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001104 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105}
1106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107/*
1108 * Prepare arguments and rettv for calling a builtin or user function.
1109 */
1110 static int
1111call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1112{
1113 int idx;
1114 typval_T *tv;
1115
1116 // Move arguments from bottom of the stack to argvars[] and add terminator.
1117 for (idx = 0; idx < argcount; ++idx)
1118 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1119 argvars[argcount].v_type = VAR_UNKNOWN;
1120
1121 // Result replaces the arguments on the stack.
1122 if (argcount > 0)
1123 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001124 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 return FAIL;
1126 else
1127 ++ectx->ec_stack.ga_len;
1128
1129 // Default return value is zero.
1130 tv = STACK_TV_BOT(-1);
1131 tv->v_type = VAR_NUMBER;
1132 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001133 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134
1135 return OK;
1136}
1137
1138/*
1139 * Call a builtin function by index.
1140 */
1141 static int
1142call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1143{
1144 typval_T argvars[MAX_FUNC_ARGS];
1145 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001146 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001147 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001148 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149
1150 if (call_prepare(argcount, argvars, ectx) == FAIL)
1151 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001152 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001154 // Call the builtin function. Set "current_ectx" so that when it
1155 // recursively invokes call_def_function() a closure context can be set.
1156 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001158 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001159 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160
1161 // Clear the arguments.
1162 for (idx = 0; idx < argcount; ++idx)
1163 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001164
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001165 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001166 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 return OK;
1168}
1169
1170/*
1171 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001172 * If the function is compiled this will add a stack frame and set the
1173 * instruction pointer at the start of the function.
1174 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001175 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001176 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177 */
1178 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001179call_ufunc(
1180 ufunc_T *ufunc,
1181 partial_T *pt,
1182 int argcount,
1183 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001184 isn_T *iptr,
1185 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186{
1187 typval_T argvars[MAX_FUNC_ARGS];
1188 funcexe_T funcexe;
1189 int error;
1190 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001191 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001192 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193
Bram Moolenaare99d4222021-06-13 14:01:26 +02001194 if (func_needs_compiling(ufunc, compile_type)
1195 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1196 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001197 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001198 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001199 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001200 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001201 if (error != FCERR_UNKNOWN)
1202 {
1203 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001204 semsg(_(e_too_many_arguments_for_function_str),
1205 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001206 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001207 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001208 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001209 return FAIL;
1210 }
1211
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001212 // The function has been compiled, can call it quickly. For a function
1213 // that was defined later: we can call it directly next time.
1214 if (iptr != NULL)
1215 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001216 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001217 iptr->isn_type = ISN_DCALL;
1218 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1219 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1220 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001221 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001222 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223
1224 if (call_prepare(argcount, argvars, ectx) == FAIL)
1225 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001226 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001227 funcexe.fe_evaluate = TRUE;
1228 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229
1230 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001232 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233
1234 // Clear the arguments.
1235 for (idx = 0; idx < argcount; ++idx)
1236 clear_tv(&argvars[idx]);
1237
1238 if (error != FCERR_NONE)
1239 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001240 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001241 return FAIL;
1242 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001243 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001244 // Error other than from calling the function itself.
1245 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 return OK;
1247}
1248
1249/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001250 * If command modifiers were applied restore them.
1251 */
1252 static void
1253may_restore_cmdmod(funclocal_T *funclocal)
1254{
1255 if (funclocal->floc_restore_cmdmod)
1256 {
1257 cmdmod.cmod_filter_regmatch.regprog = NULL;
1258 undo_cmdmod(&cmdmod);
1259 cmdmod = funclocal->floc_save_cmdmod;
1260 funclocal->floc_restore_cmdmod = FALSE;
1261 }
1262}
1263
1264/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001265 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1266 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001267 */
1268 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001269vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001270{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001271 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001272}
1273
1274/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 * Execute a function by "name".
1276 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001277 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 * Returns FAIL if not found without an error message.
1279 */
1280 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001281call_by_name(
1282 char_u *name,
1283 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001284 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001285 isn_T *iptr,
1286 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287{
1288 ufunc_T *ufunc;
1289
1290 if (builtin_function(name, -1))
1291 {
1292 int func_idx = find_internal_func(name);
1293
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001294 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001296 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 return FAIL;
1298 return call_bfunc(func_idx, argcount, ectx);
1299 }
1300
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001301 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001302
1303 if (ufunc == NULL)
1304 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001305 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001306
1307 if (script_autoload(name, TRUE))
1308 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001309 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001310
1311 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001312 return FAIL; // bail out if loading the script caused an error
1313 }
1314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001316 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001317 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001318 {
1319 int i;
1320 typval_T *argv = STACK_TV_BOT(0) - argcount;
1321
1322 // The function can change at runtime, check that the argument
1323 // types are correct.
1324 for (i = 0; i < argcount; ++i)
1325 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001326 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001327
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001328 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001329 type = ufunc->uf_arg_types[i];
1330 else if (ufunc->uf_va_type != NULL)
1331 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001332 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001333 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001334 return FAIL;
1335 }
1336 }
1337
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001338 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001339 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340
1341 return FAIL;
1342}
1343
1344 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001345call_partial(
1346 typval_T *tv,
1347 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001348 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001350 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001351 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001353 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001354 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355
1356 if (tv->v_type == VAR_PARTIAL)
1357 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001358 partial_T *pt = tv->vval.v_partial;
1359 int i;
1360
1361 if (pt->pt_argc > 0)
1362 {
1363 // Make space for arguments from the partial, shift the "argcount"
1364 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001365 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001366 return FAIL;
1367 for (i = 1; i <= argcount; ++i)
1368 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1369 ectx->ec_stack.ga_len += pt->pt_argc;
1370 argcount += pt->pt_argc;
1371
1372 // copy the arguments from the partial onto the stack
1373 for (i = 0; i < pt->pt_argc; ++i)
1374 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1375 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001376 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377
1378 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001379 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 name = pt->pt_name;
1382 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001383 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001385 if (name != NULL)
1386 {
1387 char_u fname_buf[FLEN_FIXED + 1];
1388 char_u *tofree = NULL;
1389 int error = FCERR_NONE;
1390 char_u *fname;
1391
1392 // May need to translate <SNR>123_ to K_SNR.
1393 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1394 if (error != FCERR_NONE)
1395 res = FAIL;
1396 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001397 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001398 vim_free(tofree);
1399 }
1400
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001401 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 {
1403 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001404 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001405 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 return FAIL;
1407 }
1408 return OK;
1409}
1410
1411/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001412 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1413 * TRUE.
1414 */
1415 static int
1416error_if_locked(int lock, char *error)
1417{
1418 if (lock & (VAR_LOCKED | VAR_FIXED))
1419 {
1420 emsg(_(error));
1421 return TRUE;
1422 }
1423 return FALSE;
1424}
1425
1426/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001427 * Give an error if "tv" is not a number and return FAIL.
1428 */
1429 static int
1430check_for_number(typval_T *tv)
1431{
1432 if (tv->v_type != VAR_NUMBER)
1433 {
1434 semsg(_(e_expected_str_but_got_str),
1435 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1436 return FAIL;
1437 }
1438 return OK;
1439}
1440
1441/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001442 * Store "tv" in variable "name".
1443 * This is for s: and g: variables.
1444 */
1445 static void
1446store_var(char_u *name, typval_T *tv)
1447{
1448 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001449 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001450
Bram Moolenaard877a572021-04-01 19:42:48 +02001451 if (tv->v_lock)
1452 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001453 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001454 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001455 restore_funccal();
1456}
1457
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001458/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001459 * Convert "tv" to a string.
1460 * Return FAIL if not allowed.
1461 */
1462 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001463do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001464{
1465 if (tv->v_type != VAR_STRING)
1466 {
1467 char_u *str;
1468
1469 if (is_2string_any)
1470 {
1471 switch (tv->v_type)
1472 {
1473 case VAR_SPECIAL:
1474 case VAR_BOOL:
1475 case VAR_NUMBER:
1476 case VAR_FLOAT:
1477 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001478
1479 case VAR_LIST:
1480 if (tolerant)
1481 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001482 char_u *s, *e, *p;
1483 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001484
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001485 ga_init2(&ga, sizeof(char_u *), 1);
1486
1487 // Convert to NL separated items, then
1488 // escape the items and replace the NL with
1489 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001490 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001491 if (str == NULL)
1492 return FAIL;
1493 s = str;
1494 while ((e = vim_strchr(s, '\n')) != NULL)
1495 {
1496 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001497 p = vim_strsave_fnameescape(s,
1498 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001499 if (p != NULL)
1500 {
1501 ga_concat(&ga, p);
1502 ga_concat(&ga, (char_u *)" ");
1503 vim_free(p);
1504 }
1505 s = e + 1;
1506 }
1507 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001508 clear_tv(tv);
1509 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001510 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001511 return OK;
1512 }
1513 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001514 default: to_string_error(tv->v_type);
1515 return FAIL;
1516 }
1517 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001518 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001519 clear_tv(tv);
1520 tv->v_type = VAR_STRING;
1521 tv->vval.v_string = str;
1522 }
1523 return OK;
1524}
1525
1526/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001527 * When the value of "sv" is a null list of dict, allocate it.
1528 */
1529 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001530allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001531{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001532 typval_T *tv = sv->sv_tv;
1533
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001534 switch (tv->v_type)
1535 {
1536 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001537 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001538 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001539 break;
1540 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001541 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001542 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001543 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001544 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001545 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001546 (void)rettv_blob_alloc(tv);
1547 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001548 default:
1549 break;
1550 }
1551}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001552
Bram Moolenaare7525c52021-01-09 13:20:37 +01001553/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001554 * Return the character "str[index]" where "index" is the character index,
1555 * including composing characters.
1556 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001557 */
1558 char_u *
1559char_from_string(char_u *str, varnumber_T index)
1560{
1561 size_t nbyte = 0;
1562 varnumber_T nchar = index;
1563 size_t slen;
1564
1565 if (str == NULL)
1566 return NULL;
1567 slen = STRLEN(str);
1568
Bram Moolenaarff871402021-03-26 13:34:05 +01001569 // Do the same as for a list: a negative index counts from the end.
1570 // Optimization to check the first byte to be below 0x80 (and no composing
1571 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001572 if (index < 0)
1573 {
1574 int clen = 0;
1575
1576 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001577 {
1578 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1579 ++nbyte;
1580 else if (enc_utf8)
1581 nbyte += utfc_ptr2len(str + nbyte);
1582 else
1583 nbyte += mb_ptr2len(str + nbyte);
1584 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001585 nchar = clen + index;
1586 if (nchar < 0)
1587 // unlike list: index out of range results in empty string
1588 return NULL;
1589 }
1590
1591 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001592 {
1593 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1594 ++nbyte;
1595 else if (enc_utf8)
1596 nbyte += utfc_ptr2len(str + nbyte);
1597 else
1598 nbyte += mb_ptr2len(str + nbyte);
1599 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001600 if (nbyte >= slen)
1601 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001602 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001603}
1604
1605/*
1606 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001607 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001608 * If going over the end return "str_len".
1609 * If "idx" is negative count from the end, -1 is the last character.
1610 * When going over the start return -1.
1611 */
1612 static long
1613char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1614{
1615 varnumber_T nchar = idx;
1616 size_t nbyte = 0;
1617
1618 if (nchar >= 0)
1619 {
1620 while (nchar > 0 && nbyte < str_len)
1621 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001622 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001623 --nchar;
1624 }
1625 }
1626 else
1627 {
1628 nbyte = str_len;
1629 while (nchar < 0 && nbyte > 0)
1630 {
1631 --nbyte;
1632 nbyte -= mb_head_off(str, str + nbyte);
1633 ++nchar;
1634 }
1635 if (nchar < 0)
1636 return -1;
1637 }
1638 return (long)nbyte;
1639}
1640
1641/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001642 * Return the slice "str[first : last]" using character indexes. Composing
1643 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001644 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001645 * Return NULL when the result is empty.
1646 */
1647 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001648string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001649{
1650 long start_byte, end_byte;
1651 size_t slen;
1652
1653 if (str == NULL)
1654 return NULL;
1655 slen = STRLEN(str);
1656 start_byte = char_idx2byte(str, slen, first);
1657 if (start_byte < 0)
1658 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001659 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001660 end_byte = (long)slen;
1661 else
1662 {
1663 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001664 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001665 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001666 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001667 }
1668
1669 if (start_byte >= (long)slen || end_byte <= start_byte)
1670 return NULL;
1671 return vim_strnsave(str + start_byte, end_byte - start_byte);
1672}
1673
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001674/*
1675 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1676 * When "dfunc_idx" is negative don't give an error.
1677 * Returns NULL for an error.
1678 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001679 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001680get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001681{
1682 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001683 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1684 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001685 svar_T *sv;
1686
1687 if (sref->sref_seq != si->sn_script_seq)
1688 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001689 // The script was reloaded after the function was compiled, the
1690 // script_idx may not be valid.
1691 if (dfunc != NULL)
1692 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1693 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001694 return NULL;
1695 }
1696 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001697 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001698 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001699 if (dfunc != NULL)
1700 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001701 return NULL;
1702 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001703
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001704 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1705 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001706 {
1707 if (dfunc != NULL)
1708 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1709 return NULL;
1710 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001711 return sv;
1712}
1713
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001714/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001715 * Function passed to do_cmdline() for splitting a script joined by NL
1716 * characters.
1717 */
1718 static char_u *
1719get_split_sourceline(
1720 int c UNUSED,
1721 void *cookie,
1722 int indent UNUSED,
1723 getline_opt_T options UNUSED)
1724{
1725 source_cookie_T *sp = (source_cookie_T *)cookie;
1726 char_u *p;
1727 char_u *line;
1728
Bram Moolenaar20677332021-06-06 17:02:53 +02001729 p = vim_strchr(sp->nextline, '\n');
1730 if (p == NULL)
1731 {
1732 line = vim_strsave(sp->nextline);
1733 sp->nextline += STRLEN(sp->nextline);
1734 }
1735 else
1736 {
1737 line = vim_strnsave(sp->nextline, p - sp->nextline);
1738 sp->nextline = p + 1;
1739 }
1740 return line;
1741}
1742
1743/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 * Execute a function by "name".
1745 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001746 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 */
1748 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001749call_eval_func(
1750 char_u *name,
1751 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001752 ectx_T *ectx,
1753 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754{
Bram Moolenaared677f52020-08-12 16:38:10 +02001755 int called_emsg_before = called_emsg;
1756 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001758 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001759 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001761 dictitem_T *v;
1762
1763 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001764 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1765 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001766 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001767 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001768 return FAIL;
1769 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001770 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001772 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773}
1774
1775/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001776 * When a function reference is used, fill a partial with the information
1777 * needed, especially when it is used as a closure.
1778 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001779 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001780fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1781{
1782 pt->pt_func = ufunc;
1783 pt->pt_refcount = 1;
1784
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001785 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001786 {
1787 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1788 + ectx->ec_dfunc_idx;
1789
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001790 // The closure may need to find arguments and local variables in the
1791 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001792 pt->pt_outer.out_stack = &ectx->ec_stack;
1793 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001794 if (ectx->ec_outer_ref != NULL)
1795 {
1796 // The current context already has a context, link to that one.
1797 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1798 if (ectx->ec_outer_ref->or_partial != NULL)
1799 {
1800 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1801 ++pt->pt_outer.out_up_partial->pt_refcount;
1802 }
1803 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001804
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001805 // If this function returns and the closure is still being used, we
1806 // need to make a copy of the context (arguments and local variables).
1807 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001808 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001809 {
1810 vim_free(pt);
1811 return FAIL;
1812 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001813 // Extra variable keeps the count of closures created in the current
1814 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001815 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1816 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1817
1818 ((partial_T **)ectx->ec_funcrefs.ga_data)
1819 [ectx->ec_funcrefs.ga_len] = pt;
1820 ++pt->pt_refcount;
1821 ++ectx->ec_funcrefs.ga_len;
1822 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001823 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001824 return OK;
1825}
1826
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001827/*
1828 * Execute iptr->isn_arg.string as an Ex command.
1829 */
1830 static int
1831exec_command(isn_T *iptr)
1832{
1833 source_cookie_T cookie;
1834
1835 SOURCING_LNUM = iptr->isn_lnum;
1836 // Pass getsourceline to get an error for a missing ":end"
1837 // command.
1838 CLEAR_FIELD(cookie);
1839 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1840 if (do_cmdline(iptr->isn_arg.string,
1841 getsourceline, &cookie,
1842 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1843 || did_emsg)
1844 return FAIL;
1845 return OK;
1846}
1847
Bram Moolenaar89445512022-04-14 12:58:23 +01001848/*
1849 * If script "sid" is not loaded yet then load it now.
1850 * Caller must make sure "sid" is a valid script ID.
1851 * "loaded" is set to TRUE if the script had to be loaded.
1852 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1853 */
1854 int
1855may_load_script(int sid, int *loaded)
1856{
1857 scriptitem_T *si = SCRIPT_ITEM(sid);
1858
1859 if (si->sn_state == SN_STATE_NOT_LOADED)
1860 {
1861 *loaded = TRUE;
1862 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1863 {
1864 semsg(_(e_cant_open_file_str), si->sn_name);
1865 return FAIL;
1866 }
1867 }
1868 return OK;
1869}
1870
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001871// used for v_instr of typval of VAR_INSTR
1872struct instr_S {
1873 ectx_T *instr_ectx;
1874 isn_T *instr_instr;
1875};
1876
Bram Moolenaar4c137212021-04-19 16:48:48 +02001877// used for substitute_instr
1878typedef struct subs_expr_S {
1879 ectx_T *subs_ectx;
1880 isn_T *subs_instr;
1881 int subs_status;
1882} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001884// Set when calling do_debug().
1885static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001886static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001887
1888/*
1889 * When debugging lookup "name" and return the typeval.
1890 * When not found return NULL.
1891 */
1892 typval_T *
1893lookup_debug_var(char_u *name)
1894{
1895 int idx;
1896 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001897 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001898 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001899 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001900
1901 if (ectx == NULL)
1902 return NULL;
1903 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1904
1905 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001906 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001907 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001908 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1909
1910 // the variable name may be NULL when not available in this block
1911 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001912 return STACK_TV_VAR(idx);
1913 }
1914
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001915 // Go through argument names.
1916 ufunc = dfunc->df_ufunc;
1917 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1918 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1919 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1920 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1921 - varargs_off + idx);
1922 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1923 return STACK_TV(ectx->ec_frame_idx - 1);
1924
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001925 return NULL;
1926}
1927
Bram Moolenaar26a44842021-09-02 18:49:06 +02001928/*
1929 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1930 * breakpoint was set in that function or when there is any expression.
1931 */
1932 int
1933may_break_in_function(ufunc_T *ufunc)
1934{
1935 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1936}
1937
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001938 static void
1939handle_debug(isn_T *iptr, ectx_T *ectx)
1940{
1941 char_u *line;
1942 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1943 + ectx->ec_dfunc_idx)->df_ufunc;
1944 isn_T *ni;
1945 int end_lnum = iptr->isn_lnum;
1946 garray_T ga;
1947 int lnum;
1948
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001949 if (ex_nesting_level > debug_break_level)
1950 {
1951 linenr_T breakpoint;
1952
Bram Moolenaar26a44842021-09-02 18:49:06 +02001953 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001954 return;
1955
1956 // check for the next breakpoint if needed
1957 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001958 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001959 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1960 return;
1961 }
1962
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001963 SOURCING_LNUM = iptr->isn_lnum;
1964 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001965 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001966
1967 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1968 if (ni->isn_type == ISN_DEBUG
1969 || ni->isn_type == ISN_RETURN
1970 || ni->isn_type == ISN_RETURN_VOID)
1971 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001972 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001973 break;
1974 }
1975
1976 if (end_lnum > iptr->isn_lnum)
1977 {
1978 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001979 for (lnum = iptr->isn_lnum; lnum < end_lnum
1980 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001981 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001982 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001983
Bram Moolenaar303215d2021-07-07 20:10:43 +02001984 if (p == NULL)
1985 continue; // left over from continuation line
1986 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001987 if (*p == '#')
1988 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001989 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001990 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1991 if (STRNCMP(p, "def ", 4) == 0)
1992 break;
1993 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001994 line = ga_concat_strings(&ga, " ");
1995 vim_free(ga.ga_data);
1996 }
1997 else
1998 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001999
Dominique Pelle6e969552021-06-17 13:53:41 +02002000 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002001 debug_context = NULL;
2002
2003 if (end_lnum > iptr->isn_lnum)
2004 vim_free(line);
2005}
2006
Bram Moolenaar4c137212021-04-19 16:48:48 +02002007/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002008 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002009 * Returns OK, FAIL or NOTDONE (uncatchable error).
2010 */
2011 static int
2012execute_storeindex(isn_T *iptr, ectx_T *ectx)
2013{
2014 vartype_T dest_type = iptr->isn_arg.vartype;
2015 typval_T *tv;
2016 typval_T *tv_idx = STACK_TV_BOT(-2);
2017 typval_T *tv_dest = STACK_TV_BOT(-1);
2018 int status = OK;
2019
2020 // Stack contains:
2021 // -3 value to be stored
2022 // -2 index
2023 // -1 dict or list
2024 tv = STACK_TV_BOT(-3);
2025 SOURCING_LNUM = iptr->isn_lnum;
2026 if (dest_type == VAR_ANY)
2027 {
2028 dest_type = tv_dest->v_type;
2029 if (dest_type == VAR_DICT)
2030 status = do_2string(tv_idx, TRUE, FALSE);
2031 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2032 {
2033 emsg(_(e_number_expected));
2034 status = FAIL;
2035 }
2036 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002037
2038 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002039 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002040 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002041 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002042 long lidx = (long)tv_idx->vval.v_number;
2043 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002044
Bram Moolenaare08be092022-02-17 13:08:26 +00002045 if (list == NULL)
2046 {
2047 emsg(_(e_list_not_set));
2048 return FAIL;
2049 }
2050 if (lidx < 0 && list->lv_len + lidx >= 0)
2051 // negative index is relative to the end
2052 lidx = list->lv_len + lidx;
2053 if (lidx < 0 || lidx > list->lv_len)
2054 {
2055 semsg(_(e_list_index_out_of_range_nr), lidx);
2056 return FAIL;
2057 }
2058 if (lidx < list->lv_len)
2059 {
2060 listitem_T *li = list_find(list, lidx);
2061
2062 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002063 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002064 return FAIL;
2065 // overwrite existing list item
2066 clear_tv(&li->li_tv);
2067 li->li_tv = *tv;
2068 }
2069 else
2070 {
2071 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2072 return FAIL;
2073 // append to list, only fails when out of memory
2074 if (list_append_tv(list, tv) == FAIL)
2075 return NOTDONE;
2076 clear_tv(tv);
2077 }
2078 }
2079 else if (dest_type == VAR_DICT)
2080 {
2081 char_u *key = tv_idx->vval.v_string;
2082 dict_T *dict = tv_dest->vval.v_dict;
2083 dictitem_T *di;
2084
2085 SOURCING_LNUM = iptr->isn_lnum;
2086 if (dict == NULL)
2087 {
2088 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002089 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002090 }
2091 if (key == NULL)
2092 key = (char_u *)"";
2093 di = dict_find(dict, key, -1);
2094 if (di != NULL)
2095 {
2096 if (error_if_locked(di->di_tv.v_lock,
2097 e_cannot_change_dict_item))
2098 return FAIL;
2099 // overwrite existing value
2100 clear_tv(&di->di_tv);
2101 di->di_tv = *tv;
2102 }
2103 else
2104 {
2105 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2106 return FAIL;
2107 // add to dict, only fails when out of memory
2108 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2109 return NOTDONE;
2110 clear_tv(tv);
2111 }
2112 }
2113 else if (dest_type == VAR_BLOB)
2114 {
2115 long lidx = (long)tv_idx->vval.v_number;
2116 blob_T *blob = tv_dest->vval.v_blob;
2117 varnumber_T nr;
2118 int error = FALSE;
2119 int len;
2120
2121 if (blob == NULL)
2122 {
2123 emsg(_(e_blob_not_set));
2124 return FAIL;
2125 }
2126 len = blob_len(blob);
2127 if (lidx < 0 && len + lidx >= 0)
2128 // negative index is relative to the end
2129 lidx = len + lidx;
2130
2131 // Can add one byte at the end.
2132 if (lidx < 0 || lidx > len)
2133 {
2134 semsg(_(e_blob_index_out_of_range_nr), lidx);
2135 return FAIL;
2136 }
2137 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2138 return FAIL;
2139 nr = tv_get_number_chk(tv, &error);
2140 if (error)
2141 return FAIL;
2142 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002143 }
2144 else
2145 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002146 status = FAIL;
2147 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002148 }
2149 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002150
2151 clear_tv(tv_idx);
2152 clear_tv(tv_dest);
2153 ectx->ec_stack.ga_len -= 3;
2154 if (status == FAIL)
2155 {
2156 clear_tv(tv);
2157 return FAIL;
2158 }
2159 return OK;
2160}
2161
2162/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002163 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002164 */
2165 static int
2166execute_storerange(isn_T *iptr, ectx_T *ectx)
2167{
2168 typval_T *tv;
2169 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2170 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2171 typval_T *tv_dest = STACK_TV_BOT(-1);
2172 int status = OK;
2173
2174 // Stack contains:
2175 // -4 value to be stored
2176 // -3 first index or "none"
2177 // -2 second index or "none"
2178 // -1 destination list or blob
2179 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002180 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002181 if (tv_dest->v_type == VAR_LIST)
2182 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002183 long n1;
2184 long n2;
2185 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002186
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002187 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2188 if (tv_idx2->v_type == VAR_SPECIAL
2189 && tv_idx2->vval.v_number == VVAL_NONE)
2190 n2 = list_len(tv_dest->vval.v_list) - 1;
2191 else
2192 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2193
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002194 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002195 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002196 status = FAIL;
2197 else
2198 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002199 status = check_range_index_two(tv_dest->vval.v_list,
2200 &n1, li1, &n2, FALSE);
2201 if (status != FAIL)
2202 status = list_assign_range(
2203 tv_dest->vval.v_list,
2204 tv->vval.v_list,
2205 n1,
2206 n2,
2207 tv_idx2->v_type == VAR_SPECIAL,
2208 (char_u *)"=",
2209 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002210 }
2211 }
2212 else if (tv_dest->v_type == VAR_BLOB)
2213 {
2214 varnumber_T n1;
2215 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002216 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002217
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002218 n1 = tv_get_number_chk(tv_idx1, NULL);
2219 if (tv_idx2->v_type == VAR_SPECIAL
2220 && tv_idx2->vval.v_number == VVAL_NONE)
2221 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2222 else
2223 n2 = tv_get_number_chk(tv_idx2, NULL);
2224 bloblen = blob_len(tv_dest->vval.v_blob);
2225
2226 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2227 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002228 status = FAIL;
2229 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002230 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002231 }
2232 else
2233 {
2234 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002235 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002236 }
2237
2238 clear_tv(tv_idx1);
2239 clear_tv(tv_idx2);
2240 clear_tv(tv_dest);
2241 ectx->ec_stack.ga_len -= 4;
2242 clear_tv(tv);
2243
2244 return status;
2245}
2246
2247/*
2248 * Unlet item in list or dict variable.
2249 */
2250 static int
2251execute_unletindex(isn_T *iptr, ectx_T *ectx)
2252{
2253 typval_T *tv_idx = STACK_TV_BOT(-2);
2254 typval_T *tv_dest = STACK_TV_BOT(-1);
2255 int status = OK;
2256
2257 // Stack contains:
2258 // -2 index
2259 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002260 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002261 if (tv_dest->v_type == VAR_DICT)
2262 {
2263 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002264 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002265 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002266 semsg(_(e_expected_str_but_got_str),
2267 vartype_name(VAR_STRING),
2268 vartype_name(tv_idx->v_type));
2269 status = FAIL;
2270 }
2271 else
2272 {
2273 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002274 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002275 dictitem_T *di = NULL;
2276
2277 if (d != NULL && value_check_lock(
2278 d->dv_lock, NULL, FALSE))
2279 status = FAIL;
2280 else
2281 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002282 if (tv_idx->v_type == VAR_STRING)
2283 {
2284 key = tv_idx->vval.v_string;
2285 if (key == NULL)
2286 key = (char_u *)"";
2287 }
2288 else
2289 {
2290 key = tv_get_string(tv_idx);
2291 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002292 if (d != NULL)
2293 di = dict_find(d, key, (int)STRLEN(key));
2294 if (di == NULL)
2295 {
2296 // NULL dict is equivalent to empty dict
2297 semsg(_(e_key_not_present_in_dictionary),
2298 key);
2299 status = FAIL;
2300 }
2301 else if (var_check_fixed(di->di_flags,
2302 NULL, FALSE)
2303 || var_check_ro(di->di_flags,
2304 NULL, FALSE))
2305 status = FAIL;
2306 else
2307 dictitem_remove(d, di);
2308 }
2309 }
2310 }
2311 else if (tv_dest->v_type == VAR_LIST)
2312 {
2313 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002314 if (check_for_number(tv_idx) == FAIL)
2315 {
2316 status = FAIL;
2317 }
2318 else
2319 {
2320 list_T *l = tv_dest->vval.v_list;
2321 long n = (long)tv_idx->vval.v_number;
2322
2323 if (l != NULL && value_check_lock(
2324 l->lv_lock, NULL, FALSE))
2325 status = FAIL;
2326 else
2327 {
2328 listitem_T *li = list_find(l, n);
2329
2330 if (li == NULL)
2331 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002332 semsg(_(e_list_index_out_of_range_nr), n);
2333 status = FAIL;
2334 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002335 else
2336 listitem_remove(l, li);
2337 }
2338 }
2339 }
2340 else
2341 {
2342 status = FAIL;
2343 semsg(_(e_cannot_index_str),
2344 vartype_name(tv_dest->v_type));
2345 }
2346
2347 clear_tv(tv_idx);
2348 clear_tv(tv_dest);
2349 ectx->ec_stack.ga_len -= 2;
2350
2351 return status;
2352}
2353
2354/*
2355 * Unlet a range of items in a list variable.
2356 */
2357 static int
2358execute_unletrange(isn_T *iptr, ectx_T *ectx)
2359{
2360 // Stack contains:
2361 // -3 index1
2362 // -2 index2
2363 // -1 dict or list
2364 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2365 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2366 typval_T *tv_dest = STACK_TV_BOT(-1);
2367 int status = OK;
2368
2369 if (tv_dest->v_type == VAR_LIST)
2370 {
2371 // indexes must be a number
2372 SOURCING_LNUM = iptr->isn_lnum;
2373 if (check_for_number(tv_idx1) == FAIL
2374 || (tv_idx2->v_type != VAR_SPECIAL
2375 && check_for_number(tv_idx2) == FAIL))
2376 {
2377 status = FAIL;
2378 }
2379 else
2380 {
2381 list_T *l = tv_dest->vval.v_list;
2382 long n1 = (long)tv_idx1->vval.v_number;
2383 long n2 = tv_idx2->v_type == VAR_SPECIAL
2384 ? 0 : (long)tv_idx2->vval.v_number;
2385 listitem_T *li;
2386
2387 li = list_find_index(l, &n1);
2388 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002389 {
2390 semsg(_(e_list_index_out_of_range_nr),
2391 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002392 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002393 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002394 else
2395 {
2396 if (n1 < 0)
2397 n1 = list_idx_of_item(l, li);
2398 if (n2 < 0)
2399 {
2400 listitem_T *li2 = list_find(l, n2);
2401
2402 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002403 {
2404 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002405 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002406 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002407 else
2408 n2 = list_idx_of_item(l, li2);
2409 }
2410 if (status != FAIL
2411 && tv_idx2->v_type != VAR_SPECIAL
2412 && n2 < n1)
2413 {
2414 semsg(_(e_list_index_out_of_range_nr), n2);
2415 status = FAIL;
2416 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002417 if (status != FAIL)
2418 list_unlet_range(l, li, n1,
2419 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002420 }
2421 }
2422 }
2423 else
2424 {
2425 status = FAIL;
2426 SOURCING_LNUM = iptr->isn_lnum;
2427 semsg(_(e_cannot_index_str),
2428 vartype_name(tv_dest->v_type));
2429 }
2430
2431 clear_tv(tv_idx1);
2432 clear_tv(tv_idx2);
2433 clear_tv(tv_dest);
2434 ectx->ec_stack.ga_len -= 3;
2435
2436 return status;
2437}
2438
2439/*
2440 * Top of a for loop.
2441 */
2442 static int
2443execute_for(isn_T *iptr, ectx_T *ectx)
2444{
2445 typval_T *tv;
2446 typval_T *ltv = STACK_TV_BOT(-1);
2447 typval_T *idxtv =
2448 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2449
2450 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2451 return FAIL;
2452 if (ltv->v_type == VAR_LIST)
2453 {
2454 list_T *list = ltv->vval.v_list;
2455
2456 // push the next item from the list
2457 ++idxtv->vval.v_number;
2458 if (list == NULL
2459 || idxtv->vval.v_number >= list->lv_len)
2460 {
2461 // past the end of the list, jump to "endfor"
2462 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2463 may_restore_cmdmod(&ectx->ec_funclocal);
2464 }
2465 else if (list->lv_first == &range_list_item)
2466 {
2467 // non-materialized range() list
2468 tv = STACK_TV_BOT(0);
2469 tv->v_type = VAR_NUMBER;
2470 tv->v_lock = 0;
2471 tv->vval.v_number = list_find_nr(
2472 list, idxtv->vval.v_number, NULL);
2473 ++ectx->ec_stack.ga_len;
2474 }
2475 else
2476 {
2477 listitem_T *li = list_find(list,
2478 idxtv->vval.v_number);
2479
2480 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2481 ++ectx->ec_stack.ga_len;
2482 }
2483 }
2484 else if (ltv->v_type == VAR_STRING)
2485 {
2486 char_u *str = ltv->vval.v_string;
2487
2488 // The index is for the last byte of the previous
2489 // character.
2490 ++idxtv->vval.v_number;
2491 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2492 {
2493 // past the end of the string, jump to "endfor"
2494 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2495 may_restore_cmdmod(&ectx->ec_funclocal);
2496 }
2497 else
2498 {
2499 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2500
2501 // Push the next character from the string.
2502 tv = STACK_TV_BOT(0);
2503 tv->v_type = VAR_STRING;
2504 tv->vval.v_string = vim_strnsave(
2505 str + idxtv->vval.v_number, clen);
2506 ++ectx->ec_stack.ga_len;
2507 idxtv->vval.v_number += clen - 1;
2508 }
2509 }
2510 else if (ltv->v_type == VAR_BLOB)
2511 {
2512 blob_T *blob = ltv->vval.v_blob;
2513
2514 // When we get here the first time make a copy of the
2515 // blob, so that the iteration still works when it is
2516 // changed.
2517 if (idxtv->vval.v_number == -1 && blob != NULL)
2518 {
2519 blob_copy(blob, ltv);
2520 blob_unref(blob);
2521 blob = ltv->vval.v_blob;
2522 }
2523
2524 // The index is for the previous byte.
2525 ++idxtv->vval.v_number;
2526 if (blob == NULL
2527 || idxtv->vval.v_number >= blob_len(blob))
2528 {
2529 // past the end of the blob, jump to "endfor"
2530 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2531 may_restore_cmdmod(&ectx->ec_funclocal);
2532 }
2533 else
2534 {
2535 // Push the next byte from the blob.
2536 tv = STACK_TV_BOT(0);
2537 tv->v_type = VAR_NUMBER;
2538 tv->vval.v_number = blob_get(blob,
2539 idxtv->vval.v_number);
2540 ++ectx->ec_stack.ga_len;
2541 }
2542 }
2543 else
2544 {
2545 semsg(_(e_for_loop_on_str_not_supported),
2546 vartype_name(ltv->v_type));
2547 return FAIL;
2548 }
2549 return OK;
2550}
2551
2552/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002553 * Load instruction for w:/b:/g:/t: variable.
2554 * "isn_type" is used instead of "iptr->isn_type".
2555 */
2556 static int
2557load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2558{
2559 dictitem_T *di = NULL;
2560 hashtab_T *ht = NULL;
2561 char namespace;
2562
2563 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2564 return NOTDONE;
2565 switch (isn_type)
2566 {
2567 case ISN_LOADG:
2568 ht = get_globvar_ht();
2569 namespace = 'g';
2570 break;
2571 case ISN_LOADB:
2572 ht = &curbuf->b_vars->dv_hashtab;
2573 namespace = 'b';
2574 break;
2575 case ISN_LOADW:
2576 ht = &curwin->w_vars->dv_hashtab;
2577 namespace = 'w';
2578 break;
2579 case ISN_LOADT:
2580 ht = &curtab->tp_vars->dv_hashtab;
2581 namespace = 't';
2582 break;
2583 default: // Cannot reach here
2584 return NOTDONE;
2585 }
2586 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2587
Bram Moolenaar06b77222022-01-25 15:51:56 +00002588 if (di == NULL)
2589 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002590 if (isn_type == ISN_LOADG)
2591 {
2592 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2593
2594 // g:Something could be a function
2595 if (ufunc != NULL)
2596 {
2597 typval_T *tv = STACK_TV_BOT(0);
2598
2599 ++ectx->ec_stack.ga_len;
2600 tv->v_type = VAR_FUNC;
2601 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2602 if (tv->vval.v_string == NULL)
2603 return FAIL;
2604 STRCPY(tv->vval.v_string, "g:");
2605 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2606 return OK;
2607 }
2608 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002609 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002610 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002611 // no check if the item exists in the script but
2612 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002613 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002614 else
2615 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002616 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002617 return FAIL;
2618 }
2619 else
2620 {
2621 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2622 ++ectx->ec_stack.ga_len;
2623 }
2624 return OK;
2625}
2626
2627/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002628 * Execute instructions in execution context "ectx".
2629 * Return OK or FAIL;
2630 */
2631 static int
2632exec_instructions(ectx_T *ectx)
2633{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002634 int ret = FAIL;
2635 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002636 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002637
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002638 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002639 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002640
Bram Moolenaarff652882021-05-16 15:24:49 +02002641 // Only catch exceptions in this instruction list.
2642 ectx->ec_trylevel_at_start = trylevel;
2643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 for (;;)
2645 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002646 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002648 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002649
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002650 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002651 {
2652 line_breakcheck();
2653 breakcheck_count = 0;
2654 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002655 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002656 {
2657 // Turn CTRL-C into an exception.
2658 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002659 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002660 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002661 did_throw = TRUE;
2662 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002664 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002665 {
2666 // Turn an error message into an exception.
2667 did_emsg = FALSE;
2668 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002669 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002670 did_throw = TRUE;
2671 *msg_list = NULL;
2672 }
2673
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002674 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002676 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002677 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002678 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679
2680 // An exception jumps to the first catch, finally, or returns from
2681 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002682 while (index > 0)
2683 {
2684 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002685 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002686 break;
2687 // In the catch and finally block of this try we have to go up
2688 // one level.
2689 --index;
2690 trycmd = NULL;
2691 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002692 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002694 if (trycmd->tcd_in_catch)
2695 {
2696 // exception inside ":catch", jump to ":finally" once
2697 ectx->ec_iidx = trycmd->tcd_finally_idx;
2698 trycmd->tcd_finally_idx = 0;
2699 }
2700 else
2701 // jump to first ":catch"
2702 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002703 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002704 did_throw = FALSE; // don't come back here until :endtry
2705 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 }
2707 else
2708 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002709 // Not inside try or need to return from current functions.
2710 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002711 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002712 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002713 tv = STACK_TV_BOT(0);
2714 tv->v_type = VAR_NUMBER;
2715 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002716 ++ectx->ec_stack.ga_len;
2717 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002719 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002720 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002721 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002722 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 goto done;
2724 }
2725
Bram Moolenaar4c137212021-04-19 16:48:48 +02002726 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002727 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 }
2729 continue;
2730 }
2731
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002732 /*
2733 * Big switch on the instruction. Most compilers will be turning this
2734 * into an efficient lookup table, since the "case" values are an enum
2735 * with sequential numbers. It may look ugly, but it should be the
2736 * most efficient way.
2737 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002738 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 switch (iptr->isn_type)
2740 {
2741 // execute Ex command line
2742 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002743 if (exec_command(iptr) == FAIL)
2744 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 break;
2746
Bram Moolenaar20677332021-06-06 17:02:53 +02002747 // execute Ex command line split at NL characters.
2748 case ISN_EXEC_SPLIT:
2749 {
2750 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002751 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002752
2753 SOURCING_LNUM = iptr->isn_lnum;
2754 CLEAR_FIELD(cookie);
2755 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2756 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002757 line = get_split_sourceline(0, &cookie, 0, 0);
2758 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002759 get_split_sourceline, &cookie,
2760 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2761 == FAIL
2762 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002763 {
2764 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002765 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002766 }
2767 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002768 }
2769 break;
2770
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002771 // execute Ex command line that is only a range
2772 case ISN_EXECRANGE:
2773 {
2774 exarg_T ea;
2775 char *error = NULL;
2776
2777 CLEAR_FIELD(ea);
2778 ea.cmdidx = CMD_SIZE;
2779 ea.addr_type = ADDR_LINES;
2780 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002781 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002782 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002783 if (ea.cmd == NULL)
2784 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002785 // error is always NULL when using ADDR_LINES
2786 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002787 if (error != NULL)
2788 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002789 emsg(error);
2790 goto on_error;
2791 }
2792 }
2793 break;
2794
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002795 // Evaluate an expression with legacy syntax, push it onto the
2796 // stack.
2797 case ISN_LEGACY_EVAL:
2798 {
2799 char_u *arg = iptr->isn_arg.string;
2800 int res;
2801 int save_flags = cmdmod.cmod_flags;
2802
Bram Moolenaar35578162021-08-02 19:10:38 +02002803 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002804 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002805 tv = STACK_TV_BOT(0);
2806 init_tv(tv);
2807 cmdmod.cmod_flags |= CMOD_LEGACY;
2808 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2809 cmdmod.cmod_flags = save_flags;
2810 if (res == FAIL)
2811 goto on_error;
2812 ++ectx->ec_stack.ga_len;
2813 }
2814 break;
2815
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002816 // push typeval VAR_INSTR with instructions to be executed
2817 case ISN_INSTR:
2818 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002819 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002820 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002821 tv = STACK_TV_BOT(0);
2822 tv->vval.v_instr = ALLOC_ONE(instr_T);
2823 if (tv->vval.v_instr == NULL)
2824 goto on_error;
2825 ++ectx->ec_stack.ga_len;
2826
2827 tv->v_type = VAR_INSTR;
2828 tv->vval.v_instr->instr_ectx = ectx;
2829 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2830 }
2831 break;
2832
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002833 case ISN_SOURCE:
2834 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002835 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002836
Bram Moolenaar89445512022-04-14 12:58:23 +01002837 SOURCING_LNUM = iptr->isn_lnum;
2838 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002839 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002840 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002841 }
2842 break;
2843
Bram Moolenaar4c137212021-04-19 16:48:48 +02002844 // execute :substitute with an expression
2845 case ISN_SUBSTITUTE:
2846 {
2847 subs_T *subs = &iptr->isn_arg.subs;
2848 source_cookie_T cookie;
2849 struct subs_expr_S *save_instr = substitute_instr;
2850 struct subs_expr_S subs_instr;
2851 int res;
2852
2853 subs_instr.subs_ectx = ectx;
2854 subs_instr.subs_instr = subs->subs_instr;
2855 subs_instr.subs_status = OK;
2856 substitute_instr = &subs_instr;
2857
2858 SOURCING_LNUM = iptr->isn_lnum;
2859 // This is very much like ISN_EXEC
2860 CLEAR_FIELD(cookie);
2861 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2862 res = do_cmdline(subs->subs_cmd,
2863 getsourceline, &cookie,
2864 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2865 substitute_instr = save_instr;
2866
2867 if (res == FAIL || did_emsg
2868 || subs_instr.subs_status == FAIL)
2869 goto on_error;
2870 }
2871 break;
2872
2873 case ISN_FINISH:
2874 goto done;
2875
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002876 case ISN_REDIRSTART:
2877 // create a dummy entry for var_redir_str()
2878 if (alloc_redir_lval() == FAIL)
2879 goto on_error;
2880
2881 // The output is stored in growarray "redir_ga" until
2882 // redirection ends.
2883 init_redir_ga();
2884 redir_vname = 1;
2885 break;
2886
2887 case ISN_REDIREND:
2888 {
2889 char_u *res = get_clear_redir_ga();
2890
2891 // End redirection, put redirected text on the stack.
2892 clear_redir_lval();
2893 redir_vname = 0;
2894
Bram Moolenaar35578162021-08-02 19:10:38 +02002895 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002896 {
2897 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002898 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002899 }
2900 tv = STACK_TV_BOT(0);
2901 tv->v_type = VAR_STRING;
2902 tv->vval.v_string = res;
2903 ++ectx->ec_stack.ga_len;
2904 }
2905 break;
2906
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002907 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002908#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002909 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002910 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2911 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002912 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002913#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002914 break;
2915
2916 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002917#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002918 {
2919 exarg_T ea;
2920 int res;
2921
2922 CLEAR_FIELD(ea);
2923 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2924 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2925 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2926 --ectx->ec_stack.ga_len;
2927 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01002928 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002929 res = cexpr_core(&ea, tv);
2930 clear_tv(tv);
2931 if (res == FAIL)
2932 goto on_error;
2933 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002934#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002935 break;
2936
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002937 // execute Ex command from pieces on the stack
2938 case ISN_EXECCONCAT:
2939 {
2940 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002941 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002942 int pass;
2943 int i;
2944 char_u *cmd = NULL;
2945 char_u *str;
2946
2947 for (pass = 1; pass <= 2; ++pass)
2948 {
2949 for (i = 0; i < count; ++i)
2950 {
2951 tv = STACK_TV_BOT(i - count);
2952 str = tv->vval.v_string;
2953 if (str != NULL && *str != NUL)
2954 {
2955 if (pass == 2)
2956 STRCPY(cmd + len, str);
2957 len += STRLEN(str);
2958 }
2959 if (pass == 2)
2960 clear_tv(tv);
2961 }
2962 if (pass == 1)
2963 {
2964 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002965 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002966 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002967 len = 0;
2968 }
2969 }
2970
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002971 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002972 do_cmdline_cmd(cmd);
2973 vim_free(cmd);
2974 }
2975 break;
2976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 // execute :echo {string} ...
2978 case ISN_ECHO:
2979 {
2980 int count = iptr->isn_arg.echo.echo_count;
2981 int atstart = TRUE;
2982 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002983 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984
2985 for (idx = 0; idx < count; ++idx)
2986 {
2987 tv = STACK_TV_BOT(idx - count);
2988 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2989 &atstart, &needclr);
2990 clear_tv(tv);
2991 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002992 if (needclr)
2993 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002994 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 }
2996 break;
2997
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002998 // :execute {string} ...
2999 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003000 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003001 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003002 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003003 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003004 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003005 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003006 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003007 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003008 {
3009 int count = iptr->isn_arg.number;
3010 garray_T ga;
3011 char_u buf[NUMBUFLEN];
3012 char_u *p;
3013 int len;
3014 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003015 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003016
3017 ga_init2(&ga, 1, 80);
3018 for (idx = 0; idx < count; ++idx)
3019 {
3020 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003021 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003022 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003023 if (tv->v_type == VAR_CHANNEL
3024 || tv->v_type == VAR_JOB)
3025 {
3026 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003027 semsg(_(e_using_invalid_value_as_string_str),
3028 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003029 break;
3030 }
3031 else
3032 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003033 }
3034 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003035 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003036
3037 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003038 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003039 failed = TRUE;
3040 else
3041 {
3042 if (ga.ga_len > 0)
3043 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3044 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3045 ga.ga_len += len;
3046 }
3047 clear_tv(tv);
3048 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003049 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003050 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003051 {
3052 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003053 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003054 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003055
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003056 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003057 {
3058 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003059 {
3060 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003061 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003062 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003063 {
3064 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003065 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003066 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003067 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003068 else
3069 {
3070 msg_sb_eol();
3071 if (iptr->isn_type == ISN_ECHOMSG)
3072 {
3073 msg_attr(ga.ga_data, echo_attr);
3074 out_flush();
3075 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003076#ifdef HAS_MESSAGE_WINDOW
3077 else if (iptr->isn_type == ISN_ECHOWINDOW)
3078 {
3079 start_echowindow();
3080 msg_attr(ga.ga_data, echo_attr);
3081 end_echowindow();
3082 }
3083#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003084 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3085 {
3086 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3087 TRUE);
3088 ui_write((char_u *)"\r\n", 2, TRUE);
3089 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003090 else
3091 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003092 SOURCING_LNUM = iptr->isn_lnum;
3093 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003094 }
3095 }
3096 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003097 ga_clear(&ga);
3098 }
3099 break;
3100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 // load local variable or argument
3102 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003103 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003104 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003106 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 break;
3108
3109 // load v: variable
3110 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003111 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003112 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003114 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 break;
3116
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003117 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 case ISN_LOADSCRIPT:
3119 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003120 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 svar_T *sv;
3122
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003123 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003124 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003125 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003126 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003127 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003128 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003130 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 }
3132 break;
3133
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003134 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003136 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003138 int sid = iptr->isn_arg.loadstore.ls_sid;
3139 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003140 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 if (di == NULL)
3144 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003145 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003146 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003147 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 }
3149 else
3150 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003151 if (iptr->isn_type == ISN_LOADEXPORT)
3152 {
3153 int idx = get_script_item_idx(sid, name, 0,
3154 NULL, NULL);
3155 svar_T *sv;
3156
3157 if (idx >= 0)
3158 {
3159 sv = ((svar_T *)SCRIPT_ITEM(sid)
3160 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003161 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003162 {
3163 SOURCING_LNUM = iptr->isn_lnum;
3164 semsg(_(e_item_not_exported_in_script_str),
3165 name);
3166 goto on_error;
3167 }
3168 }
3169 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003170 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003171 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003173 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 }
3175 }
3176 break;
3177
Bram Moolenaard3aac292020-04-19 14:32:17 +02003178 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003180 case ISN_LOADB:
3181 case ISN_LOADW:
3182 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003184 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003185
Bram Moolenaar06b77222022-01-25 15:51:56 +00003186 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003187 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003188 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003189 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 break;
3193
Bram Moolenaar03290b82020-12-19 16:30:44 +01003194 // load autoload variable
3195 case ISN_LOADAUTO:
3196 {
3197 char_u *name = iptr->isn_arg.string;
3198
Bram Moolenaar35578162021-08-02 19:10:38 +02003199 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003200 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003201 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003202 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003203 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003204 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003205 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003206 }
3207 break;
3208
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003209 // load g:/b:/w:/t: namespace
3210 case ISN_LOADGDICT:
3211 case ISN_LOADBDICT:
3212 case ISN_LOADWDICT:
3213 case ISN_LOADTDICT:
3214 {
3215 dict_T *d = NULL;
3216
3217 switch (iptr->isn_type)
3218 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003219 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3220 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3221 case ISN_LOADWDICT: d = curwin->w_vars; break;
3222 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003223 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003224 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003225 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003226 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003227 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003228 tv = STACK_TV_BOT(0);
3229 tv->v_type = VAR_DICT;
3230 tv->v_lock = 0;
3231 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003232 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003233 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003234 }
3235 break;
3236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 // load &option
3238 case ISN_LOADOPT:
3239 {
3240 typval_T optval;
3241 char_u *name = iptr->isn_arg.string;
3242
Bram Moolenaara8c17702020-04-01 21:17:24 +02003243 // This is not expected to fail, name is checked during
3244 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003245 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003246 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003247 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003248 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003250 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 }
3252 break;
3253
3254 // load $ENV
3255 case ISN_LOADENV:
3256 {
3257 typval_T optval;
3258 char_u *name = iptr->isn_arg.string;
3259
Bram Moolenaar35578162021-08-02 19:10:38 +02003260 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003261 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003262 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003263 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003265 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 }
3267 break;
3268
3269 // load @register
3270 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003271 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003272 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 tv = STACK_TV_BOT(0);
3274 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003275 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003276 // This may result in NULL, which should be equivalent to an
3277 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 tv->vval.v_string = get_reg_contents(
3279 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003280 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 break;
3282
3283 // store local variable
3284 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003285 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 tv = STACK_TV_VAR(iptr->isn_arg.number);
3287 clear_tv(tv);
3288 *tv = *STACK_TV_BOT(0);
3289 break;
3290
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003291 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003292 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003293 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003294 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003295 int sid = iptr->isn_arg.loadstore.ls_sid;
3296 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003297 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003298 dictitem_T *di = find_var_in_ht(ht, 0,
3299 iptr->isn_type == ISN_STORES
3300 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003301
Bram Moolenaar4c137212021-04-19 16:48:48 +02003302 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003303 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003304 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003305 {
3306 if (iptr->isn_type == ISN_STOREEXPORT)
3307 {
3308 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003309 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003310 goto on_error;
3311 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003312 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003313 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003314 else
3315 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003316 if (iptr->isn_type == ISN_STOREEXPORT)
3317 {
3318 int idx = get_script_item_idx(sid, name, 0,
3319 NULL, NULL);
3320
Bram Moolenaar06651632022-04-27 17:54:25 +01003321 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003322 if (idx >= 0)
3323 {
3324 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3325 ->sn_var_vals.ga_data) + idx;
3326
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003327 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003328 {
3329 semsg(_(e_item_not_exported_in_script_str),
3330 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003331 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003332 goto on_error;
3333 }
3334 }
3335 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003336 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003337 {
3338 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003339 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003340 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003341 clear_tv(&di->di_tv);
3342 di->di_tv = *STACK_TV_BOT(0);
3343 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003344 }
3345 break;
3346
3347 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 case ISN_STORESCRIPT:
3349 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003350 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3351 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003353 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003354 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003355 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003356 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003357
3358 // "const" and "final" are checked at compile time, locking
3359 // the value needs to be checked here.
3360 SOURCING_LNUM = iptr->isn_lnum;
3361 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003362 {
3363 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003364 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003365 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 clear_tv(sv->sv_tv);
3368 *sv->sv_tv = *STACK_TV_BOT(0);
3369 }
3370 break;
3371
3372 // store option
3373 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003374 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003376 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3377 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 long n = 0;
3379 char_u *s = NULL;
3380 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003381 char_u numbuf[NUMBUFLEN];
3382 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383
Bram Moolenaar4c137212021-04-19 16:48:48 +02003384 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 tv = STACK_TV_BOT(0);
3386 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003387 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003389 if (s == NULL)
3390 s = (char_u *)"";
3391 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003392 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3393 {
3394 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003395 // If the option can be set to a function reference or
3396 // a lambda and the passed value is a function
3397 // reference, then convert it to the name (string) of
3398 // the function reference.
3399 s = tv2string(tv, &tofree, numbuf, 0);
3400 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003401 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003402 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003403 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003404 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003405 goto on_error;
3406 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003407 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003409 // must be VAR_NUMBER, CHECKTYPE makes sure
3410 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003411 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003412 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003413 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 if (msg != NULL)
3415 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003416 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003418 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 }
3421 break;
3422
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003423 // store $ENV
3424 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003425 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003426 tv = STACK_TV_BOT(0);
3427 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3428 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003429 break;
3430
3431 // store @r
3432 case ISN_STOREREG:
3433 {
3434 int reg = iptr->isn_arg.number;
3435
Bram Moolenaar4c137212021-04-19 16:48:48 +02003436 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003437 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003438 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003439 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003440 }
3441 break;
3442
3443 // store v: variable
3444 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003445 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003446 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3447 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003448 // should not happen, type is checked when compiling
3449 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003450 break;
3451
Bram Moolenaard3aac292020-04-19 14:32:17 +02003452 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003454 case ISN_STOREB:
3455 case ISN_STOREW:
3456 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003458 dictitem_T *di;
3459 hashtab_T *ht;
3460 char_u *name = iptr->isn_arg.string + 2;
3461
Bram Moolenaard3aac292020-04-19 14:32:17 +02003462 switch (iptr->isn_type)
3463 {
3464 case ISN_STOREG:
3465 ht = get_globvar_ht();
3466 break;
3467 case ISN_STOREB:
3468 ht = &curbuf->b_vars->dv_hashtab;
3469 break;
3470 case ISN_STOREW:
3471 ht = &curwin->w_vars->dv_hashtab;
3472 break;
3473 case ISN_STORET:
3474 ht = &curtab->tp_vars->dv_hashtab;
3475 break;
3476 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003477 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479
Bram Moolenaar4c137212021-04-19 16:48:48 +02003480 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003481 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003483 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 else
3485 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003486 SOURCING_LNUM = iptr->isn_lnum;
3487 if (var_check_permission(di, name) == FAIL)
3488 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 clear_tv(&di->di_tv);
3490 di->di_tv = *STACK_TV_BOT(0);
3491 }
3492 }
3493 break;
3494
Bram Moolenaar03290b82020-12-19 16:30:44 +01003495 // store an autoload variable
3496 case ISN_STOREAUTO:
3497 SOURCING_LNUM = iptr->isn_lnum;
3498 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3499 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003500 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003501 break;
3502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 // store number in local variable
3504 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003505 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 clear_tv(tv);
3507 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003508 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 break;
3510
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003511 // store value in list or dict variable
3512 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003513 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003514 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003515
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003516 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003517 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003518 if (res == NOTDONE)
3519 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003520 }
3521 break;
3522
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003523 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003524 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003525 if (execute_storerange(iptr, ectx) == FAIL)
3526 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003527 break;
3528
Bram Moolenaar0186e582021-01-10 18:33:11 +01003529 // load or store variable or argument from outer scope
3530 case ISN_LOADOUTER:
3531 case ISN_STOREOUTER:
3532 {
3533 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003534 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3535 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003536
3537 while (depth > 1 && outer != NULL)
3538 {
3539 outer = outer->out_up;
3540 --depth;
3541 }
3542 if (outer == NULL)
3543 {
3544 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003545 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3546 || ectx->ec_outer_ref == NULL)
3547 // Possibly :def function called from legacy
3548 // context.
3549 emsg(_(e_closure_called_from_invalid_context));
3550 else
3551 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003552 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003553 }
3554 tv = ((typval_T *)outer->out_stack->ga_data)
3555 + outer->out_frame_idx + STACK_FRAME_SIZE
3556 + iptr->isn_arg.outer.outer_idx;
3557 if (iptr->isn_type == ISN_LOADOUTER)
3558 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003559 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003560 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003561 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003562 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003563 }
3564 else
3565 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003566 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003567 clear_tv(tv);
3568 *tv = *STACK_TV_BOT(0);
3569 }
3570 }
3571 break;
3572
Bram Moolenaar752fc692021-01-04 21:57:11 +01003573 // unlet item in list or dict variable
3574 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003575 if (execute_unletindex(iptr, ectx) == FAIL)
3576 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003577 break;
3578
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003579 // unlet range of items in list variable
3580 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003581 if (execute_unletrange(iptr, ectx) == FAIL)
3582 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003583 break;
3584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 // push constant
3586 case ISN_PUSHNR:
3587 case ISN_PUSHBOOL:
3588 case ISN_PUSHSPEC:
3589 case ISN_PUSHF:
3590 case ISN_PUSHS:
3591 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003592 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003593 case ISN_PUSHCHANNEL:
3594 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003595 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003596 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003598 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003599 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 switch (iptr->isn_type)
3601 {
3602 case ISN_PUSHNR:
3603 tv->v_type = VAR_NUMBER;
3604 tv->vval.v_number = iptr->isn_arg.number;
3605 break;
3606 case ISN_PUSHBOOL:
3607 tv->v_type = VAR_BOOL;
3608 tv->vval.v_number = iptr->isn_arg.number;
3609 break;
3610 case ISN_PUSHSPEC:
3611 tv->v_type = VAR_SPECIAL;
3612 tv->vval.v_number = iptr->isn_arg.number;
3613 break;
3614#ifdef FEAT_FLOAT
3615 case ISN_PUSHF:
3616 tv->v_type = VAR_FLOAT;
3617 tv->vval.v_float = iptr->isn_arg.fnumber;
3618 break;
3619#endif
3620 case ISN_PUSHBLOB:
3621 blob_copy(iptr->isn_arg.blob, tv);
3622 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003623 case ISN_PUSHFUNC:
3624 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003625 if (iptr->isn_arg.string == NULL)
3626 tv->vval.v_string = NULL;
3627 else
3628 tv->vval.v_string =
3629 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003630 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003631 case ISN_PUSHCHANNEL:
3632#ifdef FEAT_JOB_CHANNEL
3633 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003634 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003635#endif
3636 break;
3637 case ISN_PUSHJOB:
3638#ifdef FEAT_JOB_CHANNEL
3639 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003640 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003641#endif
3642 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 default:
3644 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003645 tv->vval.v_string = iptr->isn_arg.string == NULL
3646 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 }
3648 break;
3649
Bram Moolenaar06b77222022-01-25 15:51:56 +00003650 case ISN_AUTOLOAD:
3651 {
3652 char_u *name = iptr->isn_arg.string;
3653
3654 (void)script_autoload(name, FALSE);
3655 if (find_func(name, TRUE))
3656 {
3657 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3658 goto theend;
3659 tv = STACK_TV_BOT(0);
3660 tv->v_lock = 0;
3661 ++ectx->ec_stack.ga_len;
3662 tv->v_type = VAR_FUNC;
3663 tv->vval.v_string = vim_strsave(name);
3664 }
3665 else
3666 {
3667 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3668
3669 if (res == NOTDONE)
3670 goto theend;
3671 if (res == FAIL)
3672 goto on_error;
3673 }
3674 }
3675 break;
3676
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003677 case ISN_UNLET:
3678 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3679 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003680 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003681 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003682 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003683 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003684 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003685
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003686 case ISN_LOCKUNLOCK:
3687 {
3688 typval_T *lval_root_save = lval_root;
3689 int res;
3690
3691 // Stack has the local variable, argument the whole :lock
3692 // or :unlock command, like ISN_EXEC.
3693 --ectx->ec_stack.ga_len;
3694 lval_root = STACK_TV_BOT(0);
3695 res = exec_command(iptr);
3696 clear_tv(lval_root);
3697 lval_root = lval_root_save;
3698 if (res == FAIL)
3699 goto on_error;
3700 }
3701 break;
3702
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003703 case ISN_LOCKCONST:
3704 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3705 break;
3706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 // create a list from items on the stack; uses a single allocation
3708 // for the list header and the items
3709 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003710 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003711 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 break;
3713
3714 // create a dict from items on the stack
3715 case ISN_NEWDICT:
3716 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003717 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003719 SOURCING_LNUM = iptr->isn_lnum;
3720 res = exe_newdict(iptr->isn_arg.number, ectx);
3721 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003722 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003723 if (res == MAYBE)
3724 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 }
3726 break;
3727
LemonBoy372bcce2022-04-25 12:43:20 +01003728 case ISN_CONCAT:
3729 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3730 goto theend;
3731 break;
3732
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003733 // create a partial with NULL value
3734 case ISN_NEWPARTIAL:
3735 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3736 goto theend;
3737 ++ectx->ec_stack.ga_len;
3738 tv = STACK_TV_BOT(-1);
3739 tv->v_type = VAR_PARTIAL;
3740 tv->v_lock = 0;
3741 tv->vval.v_partial = NULL;
3742 break;
3743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 // call a :def function
3745 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003746 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003747 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3748 NULL,
3749 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003750 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003751 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 break;
3753
3754 // call a builtin function
3755 case ISN_BCALL:
3756 SOURCING_LNUM = iptr->isn_lnum;
3757 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3758 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003759 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003760 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 break;
3762
3763 // call a funcref or partial
3764 case ISN_PCALL:
3765 {
3766 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3767 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003768 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769
3770 SOURCING_LNUM = iptr->isn_lnum;
3771 if (pfunc->cpf_top)
3772 {
3773 // funcref is above the arguments
3774 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3775 }
3776 else
3777 {
3778 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003779 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003780 partial_tv = *STACK_TV_BOT(0);
3781 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003783 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003784 if (tv == &partial_tv)
3785 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003787 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 }
3789 break;
3790
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003791 case ISN_PCALL_END:
3792 // PCALL finished, arguments have been consumed and replaced by
3793 // the return value. Now clear the funcref from the stack,
3794 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003795 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003796 clear_tv(STACK_TV_BOT(-1));
3797 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3798 break;
3799
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 // call a user defined function or funcref/partial
3801 case ISN_UCALL:
3802 {
3803 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3804
3805 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003806 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003807 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003808 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 }
3810 break;
3811
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003812 // :defer func(arg)
3813 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01003814 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003815 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
3816 goto on_error;
3817 break;
3818
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003819 // return from a :def function call without a value
3820 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003821 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003822 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003823 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003824 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003825 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003826 tv->vval.v_number = 0;
3827 tv->v_lock = 0;
3828 // FALLTHROUGH
3829
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003830 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 case ISN_RETURN:
3832 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003833 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003834 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003835
3836 if (trystack->ga_len > 0)
3837 trycmd = ((trycmd_T *)trystack->ga_data)
3838 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003839 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003840 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003842 // jump to ":finally" or ":endtry"
3843 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003844 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003845 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003846 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 trycmd->tcd_return = TRUE;
3848 }
3849 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003850 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 }
3852 break;
3853
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003854 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 case ISN_FUNCREF:
3856 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003857 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003858 ufunc_T *ufunc;
3859 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003862 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003863 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003864 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003865 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003866 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003867 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003868 if (funcref->fr_func_name == NULL)
3869 {
3870 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3871 + funcref->fr_dfunc_idx;
3872
3873 ufunc = pt_dfunc->df_ufunc;
3874 }
3875 else
3876 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003877 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003878 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003879 if (ufunc == NULL)
3880 {
3881 SOURCING_LNUM = iptr->isn_lnum;
3882 iemsg("ufunc unexpectedly NULL for FUNCREF");
3883 goto theend;
3884 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003885 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003886 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003888 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 tv->vval.v_partial = pt;
3890 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003891 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 }
3893 break;
3894
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003895 // Create a global function from a lambda.
3896 case ISN_NEWFUNC:
3897 {
3898 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3899
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003900 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003901 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003902 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003903 }
3904 break;
3905
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003906 // List functions
3907 case ISN_DEF:
3908 if (iptr->isn_arg.string == NULL)
3909 list_functions(NULL);
3910 else
3911 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003912 exarg_T ea;
3913 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003914
3915 CLEAR_FIELD(ea);
3916 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003917 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3918 define_function(&ea, NULL, &lines_to_free);
3919 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003920 }
3921 break;
3922
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 // jump if a condition is met
3924 case ISN_JUMP:
3925 {
3926 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003927 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 int jump = TRUE;
3929
3930 if (when != JUMP_ALWAYS)
3931 {
3932 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003933 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003934 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003935 || when == JUMP_IF_COND_TRUE)
3936 {
3937 SOURCING_LNUM = iptr->isn_lnum;
3938 jump = tv_get_bool_chk(tv, &error);
3939 if (error)
3940 goto on_error;
3941 }
3942 else
3943 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01003944 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003946 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 {
3948 // drop the value from the stack
3949 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003950 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 }
3952 }
3953 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003954 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 }
3956 break;
3957
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003958 // Jump if an argument with a default value was already set and not
3959 // v:none.
3960 case ISN_JUMP_IF_ARG_SET:
3961 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3962 if (tv->v_type != VAR_UNKNOWN
3963 && !(tv->v_type == VAR_SPECIAL
3964 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003965 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003966 break;
3967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 // top of a for loop
3969 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003970 if (execute_for(iptr, ectx) == FAIL)
3971 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 break;
3973
3974 // start of ":try" block
3975 case ISN_TRY:
3976 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003977 trycmd_T *trycmd = NULL;
3978
Bram Moolenaar35578162021-08-02 19:10:38 +02003979 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003980 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003981 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3982 + ectx->ec_trystack.ga_len;
3983 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003985 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003986 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3987 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003988 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003989 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003990 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003991 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003992 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003993 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 }
3995 break;
3996
3997 case ISN_PUSHEXC:
3998 if (current_exception == NULL)
3999 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004000 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004002 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004004 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004005 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004007 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004009 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 tv->vval.v_string = vim_strsave(
4011 (char_u *)current_exception->value);
4012 break;
4013
4014 case ISN_CATCH:
4015 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004016 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004017 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018
Bram Moolenaar4c137212021-04-19 16:48:48 +02004019 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004020 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004022 trycmd->tcd_caught = TRUE;
4023 trycmd->tcd_did_throw = FALSE;
4024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004026 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 catch_exception(current_exception);
4028 }
4029 break;
4030
Bram Moolenaarc150c092021-02-13 15:02:46 +01004031 case ISN_TRYCONT:
4032 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004033 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004034 trycont_T *trycont = &iptr->isn_arg.trycont;
4035 int i;
4036 trycmd_T *trycmd;
4037 int iidx = trycont->tct_where;
4038
4039 if (trystack->ga_len < trycont->tct_levels)
4040 {
4041 siemsg("TRYCONT: expected %d levels, found %d",
4042 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004043 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004044 }
4045 // Make :endtry jump to any outer try block and the last
4046 // :endtry inside the loop to the loop start.
4047 for (i = trycont->tct_levels; i > 0; --i)
4048 {
4049 trycmd = ((trycmd_T *)trystack->ga_data)
4050 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004051 // Add one to tcd_cont to be able to jump to
4052 // instruction with index zero.
4053 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004054 iidx = trycmd->tcd_finally_idx == 0
4055 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004056 }
4057 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004058 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004059 }
4060 break;
4061
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004062 case ISN_FINALLY:
4063 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004064 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004065 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4066 + trystack->ga_len - 1;
4067
4068 // Reset the index to avoid a return statement jumps here
4069 // again.
4070 trycmd->tcd_finally_idx = 0;
4071 break;
4072 }
4073
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 // end of ":try" block
4075 case ISN_ENDTRY:
4076 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004077 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004078 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079
Bram Moolenaar06651632022-04-27 17:54:25 +01004080 --trystack->ga_len;
4081 --trylevel;
4082 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4083 if (trycmd->tcd_did_throw)
4084 did_throw = TRUE;
4085 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004087 // discard the exception
4088 if (caught_stack == current_exception)
4089 caught_stack = caught_stack->caught;
4090 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004092
4093 if (trycmd->tcd_return)
4094 goto func_return;
4095
4096 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4097 {
4098 --ectx->ec_stack.ga_len;
4099 clear_tv(STACK_TV_BOT(0));
4100 }
4101 if (trycmd->tcd_cont != 0)
4102 // handling :continue: jump to outer try block or
4103 // start of the loop
4104 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 }
4106 break;
4107
4108 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004109 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004110 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004111
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004112 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4113 {
4114 // throwing an exception while using "silent!" causes
4115 // the function to abort but not display an error.
4116 tv = STACK_TV_BOT(-1);
4117 clear_tv(tv);
4118 tv->v_type = VAR_NUMBER;
4119 tv->vval.v_number = 0;
4120 goto done;
4121 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004122 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004123 tv = STACK_TV_BOT(0);
4124 if (tv->vval.v_string == NULL
4125 || *skipwhite(tv->vval.v_string) == NUL)
4126 {
4127 vim_free(tv->vval.v_string);
4128 SOURCING_LNUM = iptr->isn_lnum;
4129 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004130 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004131 }
4132
4133 // Inside a "catch" we need to first discard the caught
4134 // exception.
4135 if (trystack->ga_len > 0)
4136 {
4137 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4138 + trystack->ga_len - 1;
4139 if (trycmd->tcd_caught && current_exception != NULL)
4140 {
4141 // discard the exception
4142 if (caught_stack == current_exception)
4143 caught_stack = caught_stack->caught;
4144 discard_current_exception();
4145 trycmd->tcd_caught = FALSE;
4146 }
4147 }
4148
Bram Moolenaar90a57162022-02-12 14:23:17 +00004149 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004150 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4151 == FAIL)
4152 {
4153 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004154 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004155 }
4156 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 break;
4159
4160 // compare with special values
4161 case ISN_COMPAREBOOL:
4162 case ISN_COMPARESPECIAL:
4163 {
4164 typval_T *tv1 = STACK_TV_BOT(-2);
4165 typval_T *tv2 = STACK_TV_BOT(-1);
4166 varnumber_T arg1 = tv1->vval.v_number;
4167 varnumber_T arg2 = tv2->vval.v_number;
4168 int res;
4169
Bram Moolenaar06651632022-04-27 17:54:25 +01004170 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4171 res = arg1 == arg2;
4172 else
4173 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174
Bram Moolenaar4c137212021-04-19 16:48:48 +02004175 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 tv1->v_type = VAR_BOOL;
4177 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4178 }
4179 break;
4180
Bram Moolenaar7a222242022-03-01 19:23:24 +00004181 case ISN_COMPARENULL:
4182 {
4183 typval_T *tv1 = STACK_TV_BOT(-2);
4184 typval_T *tv2 = STACK_TV_BOT(-1);
4185 int res;
4186
4187 res = typval_compare_null(tv1, tv2);
4188 if (res == MAYBE)
4189 goto on_error;
4190 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4191 res = !res;
4192 clear_tv(tv1);
4193 clear_tv(tv2);
4194 --ectx->ec_stack.ga_len;
4195 tv1->v_type = VAR_BOOL;
4196 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4197 }
4198 break;
4199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 // Operation with two number arguments
4201 case ISN_OPNR:
4202 case ISN_COMPARENR:
4203 {
4204 typval_T *tv1 = STACK_TV_BOT(-2);
4205 typval_T *tv2 = STACK_TV_BOT(-1);
4206 varnumber_T arg1 = tv1->vval.v_number;
4207 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004208 varnumber_T res = 0;
4209 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004211 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4212 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4213 {
4214 if (arg2 < 0)
4215 {
4216 SOURCING_LNUM = iptr->isn_lnum;
4217 emsg(_(e_bitshift_ops_must_be_postive));
4218 goto on_error;
4219 }
4220 }
4221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 switch (iptr->isn_arg.op.op_type)
4223 {
4224 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004225 case EXPR_DIV: if (arg2 == 0)
4226 div_zero = TRUE;
4227 else
4228 res = arg1 / arg2;
4229 break;
4230 case EXPR_REM: if (arg2 == 0)
4231 div_zero = TRUE;
4232 else
4233 res = arg1 % arg2;
4234 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 case EXPR_SUB: res = arg1 - arg2; break;
4236 case EXPR_ADD: res = arg1 + arg2; break;
4237
4238 case EXPR_EQUAL: res = arg1 == arg2; break;
4239 case EXPR_NEQUAL: res = arg1 != arg2; break;
4240 case EXPR_GREATER: res = arg1 > arg2; break;
4241 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4242 case EXPR_SMALLER: res = arg1 < arg2; break;
4243 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004244 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4245 res = 0;
4246 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004247 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004248 break;
4249 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4250 res = 0;
4251 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004252 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004253 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004254 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 }
4256
Bram Moolenaar4c137212021-04-19 16:48:48 +02004257 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 if (iptr->isn_type == ISN_COMPARENR)
4259 {
4260 tv1->v_type = VAR_BOOL;
4261 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4262 }
4263 else
4264 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004265 if (div_zero)
4266 {
4267 SOURCING_LNUM = iptr->isn_lnum;
4268 emsg(_(e_divide_by_zero));
4269 goto on_error;
4270 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 }
4272 break;
4273
4274 // Computation with two float arguments
4275 case ISN_OPFLOAT:
4276 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004277#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 {
4279 typval_T *tv1 = STACK_TV_BOT(-2);
4280 typval_T *tv2 = STACK_TV_BOT(-1);
4281 float_T arg1 = tv1->vval.v_float;
4282 float_T arg2 = tv2->vval.v_float;
4283 float_T res = 0;
4284 int cmp = FALSE;
4285
4286 switch (iptr->isn_arg.op.op_type)
4287 {
4288 case EXPR_MULT: res = arg1 * arg2; break;
4289 case EXPR_DIV: res = arg1 / arg2; break;
4290 case EXPR_SUB: res = arg1 - arg2; break;
4291 case EXPR_ADD: res = arg1 + arg2; break;
4292
4293 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4294 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4295 case EXPR_GREATER: cmp = arg1 > arg2; break;
4296 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4297 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4298 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4299 default: cmp = 0; break;
4300 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004301 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 if (iptr->isn_type == ISN_COMPAREFLOAT)
4303 {
4304 tv1->v_type = VAR_BOOL;
4305 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4306 }
4307 else
4308 tv1->vval.v_float = res;
4309 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004310#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 break;
4312
4313 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004314 case ISN_COMPAREDICT:
4315 case ISN_COMPAREFUNC:
4316 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 case ISN_COMPAREBLOB:
4318 {
4319 typval_T *tv1 = STACK_TV_BOT(-2);
4320 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004321 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4322 int ic = iptr->isn_arg.op.op_ic;
4323 int res = FALSE;
4324 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325
Bram Moolenaar265f8112021-12-19 12:33:05 +00004326 SOURCING_LNUM = iptr->isn_lnum;
4327 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004329 status = typval_compare_list(tv1, tv2,
4330 exprtype, ic, &res);
4331 }
4332 else if (iptr->isn_type == ISN_COMPAREDICT)
4333 {
4334 status = typval_compare_dict(tv1, tv2,
4335 exprtype, ic, &res);
4336 }
4337 else if (iptr->isn_type == ISN_COMPAREFUNC)
4338 {
4339 status = typval_compare_func(tv1, tv2,
4340 exprtype, ic, &res);
4341 }
4342 else if (iptr->isn_type == ISN_COMPARESTRING)
4343 {
4344 status = typval_compare_string(tv1, tv2,
4345 exprtype, ic, &res);
4346 }
4347 else
4348 {
4349 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004351 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352 clear_tv(tv1);
4353 clear_tv(tv2);
4354 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004355 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4356 if (status == FAIL)
4357 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 }
4359 break;
4360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 case ISN_COMPAREANY:
4362 {
4363 typval_T *tv1 = STACK_TV_BOT(-2);
4364 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004365 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004367 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368
Bram Moolenaareb26f432020-09-14 16:50:05 +02004369 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004370 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004372 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004373 if (status == FAIL)
4374 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 }
4376 break;
4377
4378 case ISN_ADDLIST:
4379 case ISN_ADDBLOB:
4380 {
4381 typval_T *tv1 = STACK_TV_BOT(-2);
4382 typval_T *tv2 = STACK_TV_BOT(-1);
4383
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004384 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004386 {
4387 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4388 && tv1->vval.v_list != NULL)
4389 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4390 NULL);
4391 else
4392 eval_addlist(tv1, tv2);
4393 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 else
4395 eval_addblob(tv1, tv2);
4396 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004397 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 }
4399 break;
4400
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004401 case ISN_LISTAPPEND:
4402 {
4403 typval_T *tv1 = STACK_TV_BOT(-2);
4404 typval_T *tv2 = STACK_TV_BOT(-1);
4405 list_T *l = tv1->vval.v_list;
4406
4407 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004408 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004409 if (l == NULL)
4410 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004411 emsg(_(e_cannot_add_to_null_list));
4412 goto on_error;
4413 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004414 if (value_check_lock(l->lv_lock, NULL, FALSE))
4415 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004416 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004417 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004418 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004419 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004420 }
4421 break;
4422
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004423 case ISN_BLOBAPPEND:
4424 {
4425 typval_T *tv1 = STACK_TV_BOT(-2);
4426 typval_T *tv2 = STACK_TV_BOT(-1);
4427 blob_T *b = tv1->vval.v_blob;
4428 int error = FALSE;
4429 varnumber_T n;
4430
4431 // add a number to a blob
4432 if (b == NULL)
4433 {
4434 SOURCING_LNUM = iptr->isn_lnum;
4435 emsg(_(e_cannot_add_to_null_blob));
4436 goto on_error;
4437 }
4438 n = tv_get_number_chk(tv2, &error);
4439 if (error)
4440 goto on_error;
4441 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004442 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004443 }
4444 break;
4445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 // Computation with two arguments of unknown type
4447 case ISN_OPANY:
4448 {
4449 typval_T *tv1 = STACK_TV_BOT(-2);
4450 typval_T *tv2 = STACK_TV_BOT(-1);
4451 varnumber_T n1, n2;
4452#ifdef FEAT_FLOAT
4453 float_T f1 = 0, f2 = 0;
4454#endif
4455 int error = FALSE;
4456
4457 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4458 {
4459 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4460 {
4461 eval_addlist(tv1, tv2);
4462 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004463 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 break;
4465 }
4466 else if (tv1->v_type == VAR_BLOB
4467 && tv2->v_type == VAR_BLOB)
4468 {
4469 eval_addblob(tv1, tv2);
4470 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004471 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 break;
4473 }
4474 }
4475#ifdef FEAT_FLOAT
4476 if (tv1->v_type == VAR_FLOAT)
4477 {
4478 f1 = tv1->vval.v_float;
4479 n1 = 0;
4480 }
4481 else
4482#endif
4483 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004484 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 n1 = tv_get_number_chk(tv1, &error);
4486 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004487 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488#ifdef FEAT_FLOAT
4489 if (tv2->v_type == VAR_FLOAT)
4490 f1 = n1;
4491#endif
4492 }
4493#ifdef FEAT_FLOAT
4494 if (tv2->v_type == VAR_FLOAT)
4495 {
4496 f2 = tv2->vval.v_float;
4497 n2 = 0;
4498 }
4499 else
4500#endif
4501 {
4502 n2 = tv_get_number_chk(tv2, &error);
4503 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004504 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505#ifdef FEAT_FLOAT
4506 if (tv1->v_type == VAR_FLOAT)
4507 f2 = n2;
4508#endif
4509 }
4510#ifdef FEAT_FLOAT
4511 // if there is a float on either side the result is a float
4512 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4513 {
4514 switch (iptr->isn_arg.op.op_type)
4515 {
4516 case EXPR_MULT: f1 = f1 * f2; break;
4517 case EXPR_DIV: f1 = f1 / f2; break;
4518 case EXPR_SUB: f1 = f1 - f2; break;
4519 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004520 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004521 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004522 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 }
4524 clear_tv(tv1);
4525 clear_tv(tv2);
4526 tv1->v_type = VAR_FLOAT;
4527 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004528 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 }
4530 else
4531#endif
4532 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004533 int failed = FALSE;
4534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 switch (iptr->isn_arg.op.op_type)
4536 {
4537 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004538 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4539 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004540 goto on_error;
4541 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 case EXPR_SUB: n1 = n1 - n2; break;
4543 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004544 default: n1 = num_modulus(n1, n2, &failed);
4545 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004546 goto on_error;
4547 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 }
4549 clear_tv(tv1);
4550 clear_tv(tv2);
4551 tv1->v_type = VAR_NUMBER;
4552 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004553 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 }
4555 }
4556 break;
4557
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004558 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004559 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004560 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004561 int is_slice = iptr->isn_type == ISN_STRSLICE;
4562 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004563 char_u *res;
4564
4565 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004566 // string slice: string is at stack-3, first index at
4567 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004568 if (is_slice)
4569 {
4570 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004571 n1 = tv->vval.v_number;
4572 }
4573
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004574 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004575 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004576
Bram Moolenaar4c137212021-04-19 16:48:48 +02004577 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004578 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004579 if (is_slice)
4580 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004581 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004582 else
4583 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004584 // single character (including composing characters).
4585 // If the index is too big or negative the result is
4586 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004587 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004588 vim_free(tv->vval.v_string);
4589 tv->vval.v_string = res;
4590 }
4591 break;
4592
4593 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004594 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004595 case ISN_BLOBINDEX:
4596 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004598 int is_slice = iptr->isn_type == ISN_LISTSLICE
4599 || iptr->isn_type == ISN_BLOBSLICE;
4600 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4601 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004602 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004603 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604
4605 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004606 // list slice: list is at stack-3, indexes at stack-2 and
4607 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004608 // Same for blob.
4609 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610
4611 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004612 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004614
4615 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 {
Bram Moolenaared591872020-08-15 22:14:53 +02004617 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004618 n1 = tv->vval.v_number;
4619 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 }
Bram Moolenaared591872020-08-15 22:14:53 +02004621
Bram Moolenaar4c137212021-04-19 16:48:48 +02004622 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004623 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004624 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004625 if (is_blob)
4626 {
4627 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4628 n1, n2, FALSE, tv) == FAIL)
4629 goto on_error;
4630 }
4631 else
4632 {
4633 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4634 n1, n2, FALSE, tv, TRUE) == FAIL)
4635 goto on_error;
4636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 }
4638 break;
4639
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004640 case ISN_ANYINDEX:
4641 case ISN_ANYSLICE:
4642 {
4643 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4644 typval_T *var1, *var2;
4645 int res;
4646
4647 // index: composite is at stack-2, index at stack-1
4648 // slice: composite is at stack-3, indexes at stack-2 and
4649 // stack-1
4650 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004651 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004652 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4653 goto on_error;
4654 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4655 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004656 res = eval_index_inner(tv, is_slice, var1, var2,
4657 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004658 clear_tv(var1);
4659 if (is_slice)
4660 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004661 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004662 if (res == FAIL)
4663 goto on_error;
4664 }
4665 break;
4666
Bram Moolenaar9af78762020-06-16 11:34:42 +02004667 case ISN_SLICE:
4668 {
4669 list_T *list;
4670 int count = iptr->isn_arg.number;
4671
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004672 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004673 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004674 list = tv->vval.v_list;
4675
4676 // no error for short list, expect it to be checked earlier
4677 if (list != NULL && list->lv_len >= count)
4678 {
4679 list_T *newlist = list_slice(list,
4680 count, list->lv_len - 1);
4681
4682 if (newlist != NULL)
4683 {
4684 list_unref(list);
4685 tv->vval.v_list = newlist;
4686 ++newlist->lv_refcount;
4687 }
4688 }
4689 }
4690 break;
4691
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004692 case ISN_GETITEM:
4693 {
4694 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004695 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004696
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004697 // Get list item: list is at stack-1, push item.
4698 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004699 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4700 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004701
Bram Moolenaar35578162021-08-02 19:10:38 +02004702 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004703 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004704 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004705 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004706
4707 // Useful when used in unpack assignment. Reset at
4708 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004709 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004710 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004711 }
4712 break;
4713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 case ISN_MEMBER:
4715 {
4716 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004717 char_u *key;
4718 dictitem_T *di;
4719
4720 // dict member: dict is at stack-2, key at stack-1
4721 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004722 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004723 dict = tv->vval.v_dict;
4724
4725 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004726 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004727 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004728 if (key == NULL)
4729 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004730
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004731 if ((di = dict_find(dict, key, -1)) == NULL)
4732 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004733 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004734 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004735
4736 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004737 // stack contents makes sense and the dict stack is
4738 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004739 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004740 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004741 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004742 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004743 tv->v_type = VAR_NUMBER;
4744 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004745 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004746 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004747 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004748 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004749 // Put the dict used on the dict stack, it might be used by
4750 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004751 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004752 if (dict_stack_save(tv) == FAIL)
4753 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004754 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004755 }
4756 break;
4757
4758 // dict member with string key
4759 case ISN_STRINGMEMBER:
4760 {
4761 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 dictitem_T *di;
4763
4764 tv = STACK_TV_BOT(-1);
4765 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4766 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004767 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004768 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004769 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 }
4771 dict = tv->vval.v_dict;
4772
4773 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4774 == NULL)
4775 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004776 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004777 semsg(_(e_key_not_present_in_dictionary),
4778 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004779 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004781 // Put the dict used on the dict stack, it might be used by
4782 // a dict function later.
4783 if (dict_stack_save(tv) == FAIL)
4784 goto on_fatal_error;
4785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004787 }
4788 break;
4789
4790 case ISN_CLEARDICT:
4791 dict_stack_drop();
4792 break;
4793
4794 case ISN_USEDICT:
4795 {
4796 typval_T *dict_tv = dict_stack_get_tv();
4797
4798 // Turn "dict.Func" into a partial for "Func" bound to
4799 // "dict". Don't do this when "Func" is already a partial
4800 // that was bound explicitly (pt_auto is FALSE).
4801 tv = STACK_TV_BOT(-1);
4802 if (dict_tv != NULL
4803 && dict_tv->v_type == VAR_DICT
4804 && dict_tv->vval.v_dict != NULL
4805 && (tv->v_type == VAR_FUNC
4806 || (tv->v_type == VAR_PARTIAL
4807 && (tv->vval.v_partial->pt_auto
4808 || tv->vval.v_partial->pt_dict == NULL))))
4809 dict_tv->vval.v_dict =
4810 make_partial(dict_tv->vval.v_dict, tv);
4811 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 }
4813 break;
4814
4815 case ISN_NEGATENR:
4816 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004817 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004818#ifdef FEAT_FLOAT
4819 if (tv->v_type == VAR_FLOAT)
4820 tv->vval.v_float = -tv->vval.v_float;
4821 else
4822#endif
4823 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 break;
4825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826 case ISN_CHECKTYPE:
4827 {
4828 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004829 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004830 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004832 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004833 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004834 if (!ectx->ec_where.wt_variable)
4835 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004836 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004837 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004838 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004839 if (r == FAIL)
4840 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004841 if (!ectx->ec_where.wt_variable)
4842 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004843
4844 // number 0 is FALSE, number 1 is TRUE
4845 if (tv->v_type == VAR_NUMBER
4846 && ct->ct_type->tt_type == VAR_BOOL
4847 && (tv->vval.v_number == 0
4848 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004850 tv->v_type = VAR_BOOL;
4851 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004852 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 }
4854 }
4855 break;
4856
Bram Moolenaar9af78762020-06-16 11:34:42 +02004857 case ISN_CHECKLEN:
4858 {
4859 int min_len = iptr->isn_arg.checklen.cl_min_len;
4860 list_T *list = NULL;
4861
4862 tv = STACK_TV_BOT(-1);
4863 if (tv->v_type == VAR_LIST)
4864 list = tv->vval.v_list;
4865 if (list == NULL || list->lv_len < min_len
4866 || (list->lv_len > min_len
4867 && !iptr->isn_arg.checklen.cl_more_OK))
4868 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004869 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004870 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004871 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004872 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004873 }
4874 }
4875 break;
4876
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004877 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004878 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004879 break;
4880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004882 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 {
4884 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004885 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004887 if (iptr->isn_type == ISN_2BOOL)
4888 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004889 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004890 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004891 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004892 n = !n;
4893 }
4894 else
4895 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004896 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004897 SOURCING_LNUM = iptr->isn_lnum;
4898 n = tv_get_bool_chk(tv, &error);
4899 if (error)
4900 goto on_error;
4901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 clear_tv(tv);
4903 tv->v_type = VAR_BOOL;
4904 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4905 }
4906 break;
4907
4908 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004909 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004910 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004911 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4912 iptr->isn_type == ISN_2STRING_ANY,
4913 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004914 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 break;
4916
Bram Moolenaar08597872020-12-10 19:43:40 +01004917 case ISN_RANGE:
4918 {
4919 exarg_T ea;
4920 char *errormsg;
4921
Bram Moolenaarece0b872021-01-08 20:40:45 +01004922 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004923 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004924 ea.addr_type = ADDR_LINES;
4925 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004926 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004927 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004928 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004929
Bram Moolenaar35578162021-08-02 19:10:38 +02004930 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004931 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004932 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004933 tv = STACK_TV_BOT(-1);
4934 tv->v_type = VAR_NUMBER;
4935 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004936 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004937 }
4938 break;
4939
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004940 case ISN_PUT:
4941 {
4942 int regname = iptr->isn_arg.put.put_regname;
4943 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4944 char_u *expr = NULL;
4945 int dir = FORWARD;
4946
Bram Moolenaar08597872020-12-10 19:43:40 +01004947 if (lnum < -2)
4948 {
4949 // line number was put on the stack by ISN_RANGE
4950 tv = STACK_TV_BOT(-1);
4951 curwin->w_cursor.lnum = tv->vval.v_number;
4952 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4953 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004954 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004955 }
4956 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004957 // :put! above cursor
4958 dir = BACKWARD;
4959 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004960 {
4961 curwin->w_cursor.lnum = lnum;
4962 if (lnum == 0)
4963 // check_cursor() below will move to line 1
4964 dir = BACKWARD;
4965 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004966
4967 if (regname == '=')
4968 {
4969 tv = STACK_TV_BOT(-1);
4970 if (tv->v_type == VAR_STRING)
4971 expr = tv->vval.v_string;
4972 else
4973 {
4974 expr = typval2string(tv, TRUE); // allocates value
4975 clear_tv(tv);
4976 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004977 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004978 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004979 check_cursor();
4980 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4981 vim_free(expr);
4982 }
4983 break;
4984
Bram Moolenaar02194d22020-10-24 23:08:38 +02004985 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004986 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4987 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4988 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4989 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004990 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4991 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004992 break;
4993
Bram Moolenaar02194d22020-10-24 23:08:38 +02004994 case ISN_CMDMOD_REV:
4995 // filter regprog is owned by the instruction, don't free it
4996 cmdmod.cmod_filter_regmatch.regprog = NULL;
4997 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004998 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4999 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005000 break;
5001
Bram Moolenaar792f7862020-11-23 08:31:18 +01005002 case ISN_UNPACK:
5003 {
5004 int count = iptr->isn_arg.unpack.unp_count;
5005 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5006 list_T *l;
5007 listitem_T *li;
5008 int i;
5009
5010 // Check there is a valid list to unpack.
5011 tv = STACK_TV_BOT(-1);
5012 if (tv->v_type != VAR_LIST)
5013 {
5014 SOURCING_LNUM = iptr->isn_lnum;
5015 emsg(_(e_for_argument_must_be_sequence_of_lists));
5016 goto on_error;
5017 }
5018 l = tv->vval.v_list;
5019 if (l == NULL
5020 || l->lv_len < (semicolon ? count - 1 : count))
5021 {
5022 SOURCING_LNUM = iptr->isn_lnum;
5023 emsg(_(e_list_value_does_not_have_enough_items));
5024 goto on_error;
5025 }
5026 else if (!semicolon && l->lv_len > count)
5027 {
5028 SOURCING_LNUM = iptr->isn_lnum;
5029 emsg(_(e_list_value_has_more_items_than_targets));
5030 goto on_error;
5031 }
5032
5033 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005034 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005035 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005036 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005037
5038 // Variable after semicolon gets a list with the remaining
5039 // items.
5040 if (semicolon)
5041 {
5042 list_T *rem_list =
5043 list_alloc_with_items(l->lv_len - count + 1);
5044
5045 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005046 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005047 tv = STACK_TV_BOT(-count);
5048 tv->vval.v_list = rem_list;
5049 ++rem_list->lv_refcount;
5050 tv->v_lock = 0;
5051 li = l->lv_first;
5052 for (i = 0; i < count - 1; ++i)
5053 li = li->li_next;
5054 for (i = 0; li != NULL; ++i)
5055 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005056 typval_T tvcopy;
5057
5058 copy_tv(&li->li_tv, &tvcopy);
5059 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005060 li = li->li_next;
5061 }
5062 --count;
5063 }
5064
5065 // Produce the values in reverse order, first item last.
5066 li = l->lv_first;
5067 for (i = 0; i < count; ++i)
5068 {
5069 tv = STACK_TV_BOT(-i - 1);
5070 copy_tv(&li->li_tv, tv);
5071 li = li->li_next;
5072 }
5073
5074 list_unref(l);
5075 }
5076 break;
5077
Bram Moolenaarb2049902021-01-24 12:53:53 +01005078 case ISN_PROF_START:
5079 case ISN_PROF_END:
5080 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005081#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005082 funccall_T cookie;
5083 ufunc_T *cur_ufunc =
5084 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005085 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005086
5087 cookie.func = cur_ufunc;
5088 if (iptr->isn_type == ISN_PROF_START)
5089 {
5090 func_line_start(&cookie, iptr->isn_lnum);
5091 // if we get here the instruction is executed
5092 func_line_exec(&cookie);
5093 }
5094 else
5095 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005096#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005097 }
5098 break;
5099
Bram Moolenaare99d4222021-06-13 14:01:26 +02005100 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005101 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005102 break;
5103
Bram Moolenaar389df252020-07-09 21:20:47 +02005104 case ISN_SHUFFLE:
5105 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005106 typval_T tmp_tv;
5107 int item = iptr->isn_arg.shuffle.shfl_item;
5108 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005109
5110 tmp_tv = *STACK_TV_BOT(-item);
5111 for ( ; up > 0 && item > 1; --up)
5112 {
5113 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5114 --item;
5115 }
5116 *STACK_TV_BOT(-item) = tmp_tv;
5117 }
5118 break;
5119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005121 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005123 ectx->ec_where.wt_index = 0;
5124 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 break;
5126 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005127 continue;
5128
Bram Moolenaard032f342020-07-18 18:13:02 +02005129func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005130 // Restore previous function. If the frame pointer is where we started
5131 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005132 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005133 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005134
Bram Moolenaar4c137212021-04-19 16:48:48 +02005135 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005136 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005137 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005138 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005139
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005140on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005141 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005142 // If "emsg_silent" is set then ignore the error, unless it was set
5143 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005144 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005145 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005146 {
5147 // If a sequence of instructions causes an error while ":silent!"
5148 // was used, restore the stack length and jump ahead to restoring
5149 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005150 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005151 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005152 while (ectx->ec_stack.ga_len
5153 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005154 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005155 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005156 clear_tv(STACK_TV_BOT(0));
5157 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005158 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5159 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005160 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005161 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005162 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005163on_fatal_error:
5164 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005165 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005166 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005167 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168 }
5169
5170done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005171 ret = OK;
5172theend:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005173 {
5174 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5175 + ectx->ec_dfunc_idx;
5176
5177 if (dfunc->df_defer_var_idx > 0)
5178 invoke_defer_funcs(ectx);
5179 }
5180
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005181 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005182 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5183 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005184}
5185
5186/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005187 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005188 * "rettv".
5189 * Return OK or FAIL.
5190 */
5191 int
5192exe_typval_instr(typval_T *tv, typval_T *rettv)
5193{
5194 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5195 isn_T *save_instr = ectx->ec_instr;
5196 int save_iidx = ectx->ec_iidx;
5197 int res;
5198
LemonBoyf3b48952022-05-05 13:53:03 +01005199 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5200 // even when the compilation fails.
5201 rettv->v_type = VAR_UNKNOWN;
5202
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005203 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5204 res = exec_instructions(ectx);
5205 if (res == OK)
5206 {
5207 *rettv = *STACK_TV_BOT(-1);
5208 --ectx->ec_stack.ga_len;
5209 }
5210
5211 ectx->ec_instr = save_instr;
5212 ectx->ec_iidx = save_iidx;
5213
5214 return res;
5215}
5216
5217/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005218 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5219 * "substitute_instr".
5220 */
5221 char_u *
5222exe_substitute_instr(void)
5223{
5224 ectx_T *ectx = substitute_instr->subs_ectx;
5225 isn_T *save_instr = ectx->ec_instr;
5226 int save_iidx = ectx->ec_iidx;
5227 char_u *res;
5228
5229 ectx->ec_instr = substitute_instr->subs_instr;
5230 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005231 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005232 typval_T *tv = STACK_TV_BOT(-1);
5233
Bram Moolenaar27523602021-06-05 21:36:19 +02005234 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005235 --ectx->ec_stack.ga_len;
5236 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005237 }
5238 else
5239 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005240 substitute_instr->subs_status = FAIL;
5241 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243
Bram Moolenaar4c137212021-04-19 16:48:48 +02005244 ectx->ec_instr = save_instr;
5245 ectx->ec_iidx = save_iidx;
5246
5247 return res;
5248}
5249
5250/*
5251 * Call a "def" function from old Vim script.
5252 * Return OK or FAIL.
5253 */
5254 int
5255call_def_function(
5256 ufunc_T *ufunc,
5257 int argc_arg, // nr of arguments
5258 typval_T *argv, // arguments
5259 partial_T *partial, // optional partial for context
5260 typval_T *rettv) // return value
5261{
5262 ectx_T ectx; // execution context
5263 int argc = argc_arg;
5264 typval_T *tv;
5265 int idx;
5266 int ret = FAIL;
5267 int defcount = ufunc->uf_args.ga_len - argc;
5268 sctx_T save_current_sctx = current_sctx;
5269 int did_emsg_before = did_emsg_cumul + did_emsg;
5270 int save_suppress_errthrow = suppress_errthrow;
5271 msglist_T **saved_msg_list = NULL;
5272 msglist_T *private_msg_list = NULL;
5273 int save_emsg_silent_def = emsg_silent_def;
5274 int save_did_emsg_def = did_emsg_def;
5275 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005276 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005277
5278// Get pointer to item in the stack.
5279#undef STACK_TV
5280#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5281
5282// Get pointer to item at the bottom of the stack, -1 is the bottom.
5283#undef STACK_TV_BOT
5284#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5285
5286// Get pointer to a local variable on the stack. Negative for arguments.
5287#undef STACK_TV_VAR
5288#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5289
5290 if (ufunc->uf_def_status == UF_NOT_COMPILED
5291 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005292 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5293 && compile_def_function(ufunc, FALSE,
5294 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005295 {
5296 if (did_emsg_cumul + did_emsg == did_emsg_before)
5297 semsg(_(e_function_is_not_compiled_str),
5298 printable_func_name(ufunc));
5299 return FAIL;
5300 }
5301
5302 {
5303 // Check the function was really compiled.
5304 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5305 + ufunc->uf_dfunc_idx;
5306 if (INSTRUCTIONS(dfunc) == NULL)
5307 {
5308 iemsg("using call_def_function() on not compiled function");
5309 return FAIL;
5310 }
5311 }
5312
5313 // If depth of calling is getting too high, don't execute the function.
5314 orig_funcdepth = funcdepth_get();
5315 if (funcdepth_increment() == FAIL)
5316 return FAIL;
5317
5318 CLEAR_FIELD(ectx);
5319 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5320 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005321 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005322 {
5323 funcdepth_decrement();
5324 return FAIL;
5325 }
5326 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5327 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5328 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005329 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005330
5331 idx = argc - ufunc->uf_args.ga_len;
5332 if (idx > 0 && ufunc->uf_va_name == NULL)
5333 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005334 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
5335 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005336 goto failed_early;
5337 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005338 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5339 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005340 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005341 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
5342 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005343 goto failed_early;
5344 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005345
5346 // Put arguments on the stack, but no more than what the function expects.
5347 // A lambda can be called with more arguments than it uses.
5348 for (idx = 0; idx < argc
5349 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5350 ++idx)
5351 {
5352 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5353 && argv[idx].v_type == VAR_SPECIAL
5354 && argv[idx].vval.v_number == VVAL_NONE)
5355 {
5356 // Use the default value.
5357 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5358 }
5359 else
5360 {
5361 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5362 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005363 ufunc->uf_arg_types[idx], &argv[idx],
5364 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005365 goto failed_early;
5366 copy_tv(&argv[idx], STACK_TV_BOT(0));
5367 }
5368 ++ectx.ec_stack.ga_len;
5369 }
5370
5371 // Turn varargs into a list. Empty list if no args.
5372 if (ufunc->uf_va_name != NULL)
5373 {
5374 int vararg_count = argc - ufunc->uf_args.ga_len;
5375
5376 if (vararg_count < 0)
5377 vararg_count = 0;
5378 else
5379 argc -= vararg_count;
5380 if (exe_newlist(vararg_count, &ectx) == FAIL)
5381 goto failed_early;
5382
5383 // Check the type of the list items.
5384 tv = STACK_TV_BOT(-1);
5385 if (ufunc->uf_va_type != NULL
5386 && ufunc->uf_va_type != &t_list_any
5387 && ufunc->uf_va_type->tt_member != &t_any
5388 && tv->vval.v_list != NULL)
5389 {
5390 type_T *expected = ufunc->uf_va_type->tt_member;
5391 listitem_T *li = tv->vval.v_list->lv_first;
5392
5393 for (idx = 0; idx < vararg_count; ++idx)
5394 {
5395 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005396 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005397 goto failed_early;
5398 li = li->li_next;
5399 }
5400 }
5401
5402 if (defcount > 0)
5403 // Move varargs list to below missing default arguments.
5404 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5405 --ectx.ec_stack.ga_len;
5406 }
5407
5408 // Make space for omitted arguments, will store default value below.
5409 // Any varargs list goes after them.
5410 if (defcount > 0)
5411 for (idx = 0; idx < defcount; ++idx)
5412 {
5413 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5414 ++ectx.ec_stack.ga_len;
5415 }
5416 if (ufunc->uf_va_name != NULL)
5417 ++ectx.ec_stack.ga_len;
5418
5419 // Frame pointer points to just after arguments.
5420 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5421 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5422
5423 {
5424 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5425 + ufunc->uf_dfunc_idx;
5426 ufunc_T *base_ufunc = dfunc->df_ufunc;
5427
5428 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5429 // by copy_func().
5430 if (partial != NULL || base_ufunc->uf_partial != NULL)
5431 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005432 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5433 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005434 goto failed_early;
5435 if (partial != NULL)
5436 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005437 outer_T *outer = get_pt_outer(partial);
5438
5439 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005440 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005441 if (current_ectx != NULL)
5442 {
5443 if (current_ectx->ec_outer_ref != NULL
5444 && current_ectx->ec_outer_ref->or_outer != NULL)
5445 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005446 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005447 }
5448 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005449 }
5450 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005451 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005452 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005453 ++partial->pt_refcount;
5454 ectx.ec_outer_ref->or_partial = partial;
5455 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005456 }
5457 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005458 {
5459 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5460 ++base_ufunc->uf_partial->pt_refcount;
5461 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5462 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005463 }
5464 }
5465
5466 // dummy frame entries
5467 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5468 {
5469 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5470 ++ectx.ec_stack.ga_len;
5471 }
5472
5473 {
5474 // Reserve space for local variables and any closure reference count.
5475 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5476 + ufunc->uf_dfunc_idx;
5477
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005478 // Initialize variables to zero. That avoids having to generate
5479 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005480 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005481 {
5482 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5483 STACK_TV_VAR(idx)->vval.v_number = 0;
5484 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005485 ectx.ec_stack.ga_len += dfunc->df_varcount;
5486 if (dfunc->df_has_closure)
5487 {
5488 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5489 STACK_TV_VAR(idx)->vval.v_number = 0;
5490 ++ectx.ec_stack.ga_len;
5491 }
5492
5493 ectx.ec_instr = INSTRUCTIONS(dfunc);
5494 }
5495
5496 // Following errors are in the function, not the caller.
5497 // Commands behave like vim9script.
5498 estack_push_ufunc(ufunc, 1);
5499 current_sctx = ufunc->uf_script_ctx;
5500 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5501
5502 // Use a specific location for storing error messages to be converted to an
5503 // exception.
5504 saved_msg_list = msg_list;
5505 msg_list = &private_msg_list;
5506
5507 // Do turn errors into exceptions.
5508 suppress_errthrow = FALSE;
5509
5510 // Do not delete the function while executing it.
5511 ++ufunc->uf_calls;
5512
5513 // When ":silent!" was used before calling then we still abort the
5514 // function. If ":silent!" is used in the function then we don't.
5515 emsg_silent_def = emsg_silent;
5516 did_emsg_def = 0;
5517
5518 ectx.ec_where.wt_index = 0;
5519 ectx.ec_where.wt_variable = FALSE;
5520
5521 // Execute the instructions until done.
5522 ret = exec_instructions(&ectx);
5523 if (ret == OK)
5524 {
5525 // function finished, get result from the stack.
5526 if (ufunc->uf_ret_type == &t_void)
5527 {
5528 rettv->v_type = VAR_VOID;
5529 }
5530 else
5531 {
5532 tv = STACK_TV_BOT(-1);
5533 *rettv = *tv;
5534 tv->v_type = VAR_UNKNOWN;
5535 }
5536 }
5537
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005538 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005539 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5540 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005541
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005542 // Deal with any remaining closures, they may be in use somewhere.
5543 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005544 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005545 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005546 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005547 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005548
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005549 estack_pop();
5550 current_sctx = save_current_sctx;
5551
Bram Moolenaar2336c372021-12-06 15:06:54 +00005552 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5553 // Function was unreferenced while being used, free it now.
5554 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005555
Bram Moolenaar352134b2020-10-17 22:04:08 +02005556 if (*msg_list != NULL && saved_msg_list != NULL)
5557 {
5558 msglist_T **plist = saved_msg_list;
5559
5560 // Append entries from the current msg_list (uncaught exceptions) to
5561 // the saved msg_list.
5562 while (*plist != NULL)
5563 plist = &(*plist)->next;
5564
5565 *plist = *msg_list;
5566 }
5567 msg_list = saved_msg_list;
5568
Bram Moolenaar4c137212021-04-19 16:48:48 +02005569 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005570 {
5571 cmdmod.cmod_filter_regmatch.regprog = NULL;
5572 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005573 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005574 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005575 emsg_silent_def = save_emsg_silent_def;
5576 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005577
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005578failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005579 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005580 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5581 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005582 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005585 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005586 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005587 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005588 if (ectx.ec_outer_ref->or_outer_allocated)
5589 vim_free(ectx.ec_outer_ref->or_outer);
5590 partial_unref(ectx.ec_outer_ref->or_partial);
5591 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005592 }
5593
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005594 // Not sure if this is necessary.
5595 suppress_errthrow = save_suppress_errthrow;
5596
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005597 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5598 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005599 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005600 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005601 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005602 return ret;
5603}
5604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005606 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5607 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5608 * "pfx" is prefixed to every line.
5609 */
5610 static void
5611list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5612{
5613 int line_idx = 0;
5614 int prev_current = 0;
5615 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005616 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005617
5618 for (current = 0; current < instr_count; ++current)
5619 {
5620 isn_T *iptr = &instr[current];
5621 char *line;
5622
5623 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005624 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005625 while (line_idx < iptr->isn_lnum
5626 && line_idx < ufunc->uf_lines.ga_len)
5627 {
5628 if (current > prev_current)
5629 {
5630 msg_puts("\n\n");
5631 prev_current = current;
5632 }
5633 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5634 if (line != NULL)
5635 msg(line);
5636 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005637 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5638 {
5639 int first_def_arg = ufunc->uf_args.ga_len
5640 - ufunc->uf_def_args.ga_len;
5641
5642 if (def_arg_idx > 0)
5643 msg_puts("\n\n");
5644 msg_start();
5645 msg_puts(" ");
5646 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5647 first_def_arg + def_arg_idx]);
5648 msg_puts(" = ");
5649 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5650 msg_clr_eos();
5651 msg_end();
5652 }
5653 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005654
5655 switch (iptr->isn_type)
5656 {
5657 case ISN_EXEC:
5658 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5659 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005660 case ISN_EXEC_SPLIT:
5661 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5662 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005663 case ISN_EXECRANGE:
5664 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5665 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005666 case ISN_LEGACY_EVAL:
5667 smsg("%s%4d EVAL legacy %s", pfx, current,
5668 iptr->isn_arg.string);
5669 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005670 case ISN_REDIRSTART:
5671 smsg("%s%4d REDIR", pfx, current);
5672 break;
5673 case ISN_REDIREND:
5674 smsg("%s%4d REDIR END%s", pfx, current,
5675 iptr->isn_arg.number ? " append" : "");
5676 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005677 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005678#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005679 smsg("%s%4d CEXPR pre %s", pfx, current,
5680 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005681#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005682 break;
5683 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005684#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005685 {
5686 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5687
5688 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5689 cexpr_get_auname(cer->cer_cmdidx),
5690 cer->cer_forceit ? "!" : "",
5691 cer->cer_cmdline);
5692 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005693#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005694 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005695 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005696 smsg("%s%4d INSTR", pfx, current);
5697 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5698 msg(" -------------");
5699 break;
5700 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005701 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005702 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5703
5704 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005705 }
5706 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005707 case ISN_SUBSTITUTE:
5708 {
5709 subs_T *subs = &iptr->isn_arg.subs;
5710
5711 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5712 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5713 msg(" -------------");
5714 }
5715 break;
5716 case ISN_EXECCONCAT:
5717 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5718 (varnumber_T)iptr->isn_arg.number);
5719 break;
5720 case ISN_ECHO:
5721 {
5722 echo_T *echo = &iptr->isn_arg.echo;
5723
5724 smsg("%s%4d %s %d", pfx, current,
5725 echo->echo_with_white ? "ECHO" : "ECHON",
5726 echo->echo_count);
5727 }
5728 break;
5729 case ISN_EXECUTE:
5730 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005731 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005732 break;
5733 case ISN_ECHOMSG:
5734 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005735 (varnumber_T)(iptr->isn_arg.number));
5736 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01005737 case ISN_ECHOWINDOW:
5738 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
5739 (varnumber_T)(iptr->isn_arg.number));
5740 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02005741 case ISN_ECHOCONSOLE:
5742 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5743 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005744 break;
5745 case ISN_ECHOERR:
5746 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005747 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005748 break;
5749 case ISN_LOAD:
5750 {
5751 if (iptr->isn_arg.number < 0)
5752 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5753 (varnumber_T)(iptr->isn_arg.number
5754 + STACK_FRAME_SIZE));
5755 else
5756 smsg("%s%4d LOAD $%lld", pfx, current,
5757 (varnumber_T)(iptr->isn_arg.number));
5758 }
5759 break;
5760 case ISN_LOADOUTER:
5761 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005762 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005763 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5764 iptr->isn_arg.outer.outer_depth,
5765 iptr->isn_arg.outer.outer_idx
5766 + STACK_FRAME_SIZE);
5767 else
5768 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5769 iptr->isn_arg.outer.outer_depth,
5770 iptr->isn_arg.outer.outer_idx);
5771 }
5772 break;
5773 case ISN_LOADV:
5774 smsg("%s%4d LOADV v:%s", pfx, current,
5775 get_vim_var_name(iptr->isn_arg.number));
5776 break;
5777 case ISN_LOADSCRIPT:
5778 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005779 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5780 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5781 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005782
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005783 sv = get_script_svar(sref, -1);
5784 if (sv == NULL)
5785 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5786 pfx, current, si->sn_name);
5787 else
5788 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005789 sv->sv_name,
5790 sref->sref_idx,
5791 si->sn_name);
5792 }
5793 break;
5794 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005795 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005796 {
5797 scriptitem_T *si = SCRIPT_ITEM(
5798 iptr->isn_arg.loadstore.ls_sid);
5799
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005800 smsg("%s%4d %s s:%s from %s", pfx, current,
5801 iptr->isn_type == ISN_LOADS ? "LOADS"
5802 : "LOADEXPORT",
5803 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005804 }
5805 break;
5806 case ISN_LOADAUTO:
5807 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5808 break;
5809 case ISN_LOADG:
5810 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5811 break;
5812 case ISN_LOADB:
5813 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5814 break;
5815 case ISN_LOADW:
5816 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5817 break;
5818 case ISN_LOADT:
5819 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5820 break;
5821 case ISN_LOADGDICT:
5822 smsg("%s%4d LOAD g:", pfx, current);
5823 break;
5824 case ISN_LOADBDICT:
5825 smsg("%s%4d LOAD b:", pfx, current);
5826 break;
5827 case ISN_LOADWDICT:
5828 smsg("%s%4d LOAD w:", pfx, current);
5829 break;
5830 case ISN_LOADTDICT:
5831 smsg("%s%4d LOAD t:", pfx, current);
5832 break;
5833 case ISN_LOADOPT:
5834 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5835 break;
5836 case ISN_LOADENV:
5837 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5838 break;
5839 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005840 smsg("%s%4d LOADREG @%c", pfx, current,
5841 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005842 break;
5843
5844 case ISN_STORE:
5845 if (iptr->isn_arg.number < 0)
5846 smsg("%s%4d STORE arg[%lld]", pfx, current,
5847 iptr->isn_arg.number + STACK_FRAME_SIZE);
5848 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005849 smsg("%s%4d STORE $%lld", pfx, current,
5850 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005851 break;
5852 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005853 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5854 iptr->isn_arg.outer.outer_depth,
5855 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005856 break;
5857 case ISN_STOREV:
5858 smsg("%s%4d STOREV v:%s", pfx, current,
5859 get_vim_var_name(iptr->isn_arg.number));
5860 break;
5861 case ISN_STOREAUTO:
5862 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5863 break;
5864 case ISN_STOREG:
5865 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5866 break;
5867 case ISN_STOREB:
5868 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5869 break;
5870 case ISN_STOREW:
5871 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5872 break;
5873 case ISN_STORET:
5874 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5875 break;
5876 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005877 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005878 {
5879 scriptitem_T *si = SCRIPT_ITEM(
5880 iptr->isn_arg.loadstore.ls_sid);
5881
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005882 smsg("%s%4d %s %s in %s", pfx, current,
5883 iptr->isn_type == ISN_STORES
5884 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005885 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5886 }
5887 break;
5888 case ISN_STORESCRIPT:
5889 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005890 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5891 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5892 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005893
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005894 sv = get_script_svar(sref, -1);
5895 if (sv == NULL)
5896 smsg("%s%4d STORESCRIPT [deleted] in %s",
5897 pfx, current, si->sn_name);
5898 else
5899 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005900 sv->sv_name,
5901 sref->sref_idx,
5902 si->sn_name);
5903 }
5904 break;
5905 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005906 case ISN_STOREFUNCOPT:
5907 smsg("%s%4d %s &%s", pfx, current,
5908 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005909 iptr->isn_arg.storeopt.so_name);
5910 break;
5911 case ISN_STOREENV:
5912 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5913 break;
5914 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005915 smsg("%s%4d STOREREG @%c", pfx, current,
5916 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005917 break;
5918 case ISN_STORENR:
5919 smsg("%s%4d STORE %lld in $%d", pfx, current,
5920 iptr->isn_arg.storenr.stnr_val,
5921 iptr->isn_arg.storenr.stnr_idx);
5922 break;
5923
5924 case ISN_STOREINDEX:
5925 smsg("%s%4d STOREINDEX %s", pfx, current,
5926 vartype_name(iptr->isn_arg.vartype));
5927 break;
5928
5929 case ISN_STORERANGE:
5930 smsg("%s%4d STORERANGE", pfx, current);
5931 break;
5932
5933 // constants
5934 case ISN_PUSHNR:
5935 smsg("%s%4d PUSHNR %lld", pfx, current,
5936 (varnumber_T)(iptr->isn_arg.number));
5937 break;
5938 case ISN_PUSHBOOL:
5939 case ISN_PUSHSPEC:
5940 smsg("%s%4d PUSH %s", pfx, current,
5941 get_var_special_name(iptr->isn_arg.number));
5942 break;
5943 case ISN_PUSHF:
5944#ifdef FEAT_FLOAT
5945 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5946#endif
5947 break;
5948 case ISN_PUSHS:
5949 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5950 break;
5951 case ISN_PUSHBLOB:
5952 {
5953 char_u *r;
5954 char_u numbuf[NUMBUFLEN];
5955 char_u *tofree;
5956
5957 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5958 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5959 vim_free(tofree);
5960 }
5961 break;
5962 case ISN_PUSHFUNC:
5963 {
5964 char *name = (char *)iptr->isn_arg.string;
5965
5966 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5967 name == NULL ? "[none]" : name);
5968 }
5969 break;
5970 case ISN_PUSHCHANNEL:
5971#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005972 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005973#endif
5974 break;
5975 case ISN_PUSHJOB:
5976#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005977 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005978#endif
5979 break;
5980 case ISN_PUSHEXC:
5981 smsg("%s%4d PUSH v:exception", pfx, current);
5982 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005983 case ISN_AUTOLOAD:
5984 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5985 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005986 case ISN_UNLET:
5987 smsg("%s%4d UNLET%s %s", pfx, current,
5988 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5989 iptr->isn_arg.unlet.ul_name);
5990 break;
5991 case ISN_UNLETENV:
5992 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5993 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5994 iptr->isn_arg.unlet.ul_name);
5995 break;
5996 case ISN_UNLETINDEX:
5997 smsg("%s%4d UNLETINDEX", pfx, current);
5998 break;
5999 case ISN_UNLETRANGE:
6000 smsg("%s%4d UNLETRANGE", pfx, current);
6001 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006002 case ISN_LOCKUNLOCK:
6003 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6004 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006005 case ISN_LOCKCONST:
6006 smsg("%s%4d LOCKCONST", pfx, current);
6007 break;
6008 case ISN_NEWLIST:
6009 smsg("%s%4d NEWLIST size %lld", pfx, current,
6010 (varnumber_T)(iptr->isn_arg.number));
6011 break;
6012 case ISN_NEWDICT:
6013 smsg("%s%4d NEWDICT size %lld", pfx, current,
6014 (varnumber_T)(iptr->isn_arg.number));
6015 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006016 case ISN_NEWPARTIAL:
6017 smsg("%s%4d NEWPARTIAL", pfx, current);
6018 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006019
6020 // function call
6021 case ISN_BCALL:
6022 {
6023 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6024
6025 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6026 internal_func_name(cbfunc->cbf_idx),
6027 cbfunc->cbf_argcount);
6028 }
6029 break;
6030 case ISN_DCALL:
6031 {
6032 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6033 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6034 + cdfunc->cdf_idx;
6035
6036 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006037 printable_func_name(df->df_ufunc),
6038 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006039 }
6040 break;
6041 case ISN_UCALL:
6042 {
6043 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6044
6045 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6046 cufunc->cuf_name, cufunc->cuf_argcount);
6047 }
6048 break;
6049 case ISN_PCALL:
6050 {
6051 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6052
6053 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6054 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6055 }
6056 break;
6057 case ISN_PCALL_END:
6058 smsg("%s%4d PCALL end", pfx, current);
6059 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006060 case ISN_DEFER:
6061 smsg("%s%4d DEFER %d args", pfx, current,
6062 (int)iptr->isn_arg.defer.defer_argcount);
6063 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006064 case ISN_RETURN:
6065 smsg("%s%4d RETURN", pfx, current);
6066 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006067 case ISN_RETURN_VOID:
6068 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006069 break;
6070 case ISN_FUNCREF:
6071 {
6072 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00006073 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006074
Bram Moolenaar38453522021-11-28 22:00:12 +00006075 if (funcref->fr_func_name == NULL)
6076 {
6077 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6078 + funcref->fr_dfunc_idx;
6079 name = df->df_ufunc->uf_name;
6080 }
6081 else
6082 name = funcref->fr_func_name;
6083 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006084 }
6085 break;
6086
6087 case ISN_NEWFUNC:
6088 {
6089 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
6090
6091 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6092 newfunc->nf_lambda, newfunc->nf_global);
6093 }
6094 break;
6095
6096 case ISN_DEF:
6097 {
6098 char_u *name = iptr->isn_arg.string;
6099
6100 smsg("%s%4d DEF %s", pfx, current,
6101 name == NULL ? (char_u *)"" : name);
6102 }
6103 break;
6104
6105 case ISN_JUMP:
6106 {
6107 char *when = "?";
6108
6109 switch (iptr->isn_arg.jump.jump_when)
6110 {
6111 case JUMP_ALWAYS:
6112 when = "JUMP";
6113 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006114 case JUMP_NEVER:
6115 iemsg("JUMP_NEVER should not be used");
6116 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006117 case JUMP_AND_KEEP_IF_TRUE:
6118 when = "JUMP_AND_KEEP_IF_TRUE";
6119 break;
6120 case JUMP_IF_FALSE:
6121 when = "JUMP_IF_FALSE";
6122 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006123 case JUMP_IF_COND_FALSE:
6124 when = "JUMP_IF_COND_FALSE";
6125 break;
6126 case JUMP_IF_COND_TRUE:
6127 when = "JUMP_IF_COND_TRUE";
6128 break;
6129 }
6130 smsg("%s%4d %s -> %d", pfx, current, when,
6131 iptr->isn_arg.jump.jump_where);
6132 }
6133 break;
6134
6135 case ISN_JUMP_IF_ARG_SET:
6136 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6137 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6138 iptr->isn_arg.jump.jump_where);
6139 break;
6140
6141 case ISN_FOR:
6142 {
6143 forloop_T *forloop = &iptr->isn_arg.forloop;
6144
6145 smsg("%s%4d FOR $%d -> %d", pfx, current,
6146 forloop->for_idx, forloop->for_end);
6147 }
6148 break;
6149
6150 case ISN_TRY:
6151 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006152 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006153
6154 if (try->try_ref->try_finally == 0)
6155 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6156 pfx, current,
6157 try->try_ref->try_catch,
6158 try->try_ref->try_endtry);
6159 else
6160 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6161 pfx, current,
6162 try->try_ref->try_catch,
6163 try->try_ref->try_finally,
6164 try->try_ref->try_endtry);
6165 }
6166 break;
6167 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006168 smsg("%s%4d CATCH", pfx, current);
6169 break;
6170 case ISN_TRYCONT:
6171 {
6172 trycont_T *trycont = &iptr->isn_arg.trycont;
6173
6174 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6175 trycont->tct_levels,
6176 trycont->tct_levels == 1 ? "" : "s",
6177 trycont->tct_where);
6178 }
6179 break;
6180 case ISN_FINALLY:
6181 smsg("%s%4d FINALLY", pfx, current);
6182 break;
6183 case ISN_ENDTRY:
6184 smsg("%s%4d ENDTRY", pfx, current);
6185 break;
6186 case ISN_THROW:
6187 smsg("%s%4d THROW", pfx, current);
6188 break;
6189
6190 // expression operations on number
6191 case ISN_OPNR:
6192 case ISN_OPFLOAT:
6193 case ISN_OPANY:
6194 {
6195 char *what;
6196 char *ins;
6197
6198 switch (iptr->isn_arg.op.op_type)
6199 {
6200 case EXPR_MULT: what = "*"; break;
6201 case EXPR_DIV: what = "/"; break;
6202 case EXPR_REM: what = "%"; break;
6203 case EXPR_SUB: what = "-"; break;
6204 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006205 case EXPR_LSHIFT: what = "<<"; break;
6206 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006207 default: what = "???"; break;
6208 }
6209 switch (iptr->isn_type)
6210 {
6211 case ISN_OPNR: ins = "OPNR"; break;
6212 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6213 case ISN_OPANY: ins = "OPANY"; break;
6214 default: ins = "???"; break;
6215 }
6216 smsg("%s%4d %s %s", pfx, current, ins, what);
6217 }
6218 break;
6219
6220 case ISN_COMPAREBOOL:
6221 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006222 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006223 case ISN_COMPARENR:
6224 case ISN_COMPAREFLOAT:
6225 case ISN_COMPARESTRING:
6226 case ISN_COMPAREBLOB:
6227 case ISN_COMPARELIST:
6228 case ISN_COMPAREDICT:
6229 case ISN_COMPAREFUNC:
6230 case ISN_COMPAREANY:
6231 {
6232 char *p;
6233 char buf[10];
6234 char *type;
6235
6236 switch (iptr->isn_arg.op.op_type)
6237 {
6238 case EXPR_EQUAL: p = "=="; break;
6239 case EXPR_NEQUAL: p = "!="; break;
6240 case EXPR_GREATER: p = ">"; break;
6241 case EXPR_GEQUAL: p = ">="; break;
6242 case EXPR_SMALLER: p = "<"; break;
6243 case EXPR_SEQUAL: p = "<="; break;
6244 case EXPR_MATCH: p = "=~"; break;
6245 case EXPR_IS: p = "is"; break;
6246 case EXPR_ISNOT: p = "isnot"; break;
6247 case EXPR_NOMATCH: p = "!~"; break;
6248 default: p = "???"; break;
6249 }
6250 STRCPY(buf, p);
6251 if (iptr->isn_arg.op.op_ic == TRUE)
6252 strcat(buf, "?");
6253 switch(iptr->isn_type)
6254 {
6255 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6256 case ISN_COMPARESPECIAL:
6257 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006258 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006259 case ISN_COMPARENR: type = "COMPARENR"; break;
6260 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6261 case ISN_COMPARESTRING:
6262 type = "COMPARESTRING"; break;
6263 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6264 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6265 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6266 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6267 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6268 default: type = "???"; break;
6269 }
6270
6271 smsg("%s%4d %s %s", pfx, current, type, buf);
6272 }
6273 break;
6274
6275 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6276 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6277
6278 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006279 case ISN_CONCAT:
6280 smsg("%s%4d CONCAT size %lld", pfx, current,
6281 (varnumber_T)(iptr->isn_arg.number));
6282 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006283 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6284 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6285 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6286 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6287 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6288 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6289 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6290 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6291 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6292 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6293 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006294 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006295 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6296 iptr->isn_arg.getitem.gi_index,
6297 iptr->isn_arg.getitem.gi_with_op ?
6298 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006299 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6300 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6301 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006302 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6303 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6304
Bram Moolenaar4c137212021-04-19 16:48:48 +02006305 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6306
Bram Moolenaar4c137212021-04-19 16:48:48 +02006307 case ISN_CHECKTYPE:
6308 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006309 checktype_T *ct = &iptr->isn_arg.type;
6310 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006311
6312 if (ct->ct_arg_idx == 0)
6313 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6314 type_name(ct->ct_type, &tofree),
6315 (int)ct->ct_off);
6316 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006317 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006318 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006319 type_name(ct->ct_type, &tofree),
6320 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006321 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006322 (int)ct->ct_arg_idx);
6323 vim_free(tofree);
6324 break;
6325 }
6326 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6327 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6328 iptr->isn_arg.checklen.cl_min_len);
6329 break;
6330 case ISN_SETTYPE:
6331 {
6332 char *tofree;
6333
6334 smsg("%s%4d SETTYPE %s", pfx, current,
6335 type_name(iptr->isn_arg.type.ct_type, &tofree));
6336 vim_free(tofree);
6337 break;
6338 }
6339 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006340 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6341 smsg("%s%4d INVERT %d (!val)", pfx, current,
6342 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006343 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006344 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6345 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006346 break;
6347 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006348 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006349 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006350 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6351 pfx, current,
6352 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006353 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006354 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6355 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006356 break;
6357 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006358 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006359 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006360 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006361 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6362 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006363 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006364 else
6365 smsg("%s%4d PUT %c %ld", pfx, current,
6366 iptr->isn_arg.put.put_regname,
6367 (long)iptr->isn_arg.put.put_lnum);
6368 break;
6369
Bram Moolenaar4c137212021-04-19 16:48:48 +02006370 case ISN_CMDMOD:
6371 {
6372 char_u *buf;
6373 size_t len = produce_cmdmods(
6374 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6375
6376 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006377 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006378 {
6379 (void)produce_cmdmods(
6380 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6381 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6382 vim_free(buf);
6383 }
6384 break;
6385 }
6386 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6387
6388 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006389 smsg("%s%4d PROFILE START line %d", pfx, current,
6390 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006391 break;
6392
6393 case ISN_PROF_END:
6394 smsg("%s%4d PROFILE END", pfx, current);
6395 break;
6396
Bram Moolenaare99d4222021-06-13 14:01:26 +02006397 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006398 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6399 iptr->isn_arg.debug.dbg_break_lnum + 1,
6400 iptr->isn_lnum,
6401 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006402 break;
6403
Bram Moolenaar4c137212021-04-19 16:48:48 +02006404 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6405 iptr->isn_arg.unpack.unp_count,
6406 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6407 break;
6408 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6409 iptr->isn_arg.shuffle.shfl_item,
6410 iptr->isn_arg.shuffle.shfl_up);
6411 break;
6412 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6413
6414 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6415 return;
6416 }
6417
6418 out_flush(); // output one line at a time
6419 ui_breakcheck();
6420 if (got_int)
6421 break;
6422 }
6423}
6424
6425/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006426 * Handle command line completion for the :disassemble command.
6427 */
6428 void
6429set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6430{
6431 char_u *p;
6432
6433 // Default: expand user functions, "debug" and "profile"
6434 xp->xp_context = EXPAND_DISASSEMBLE;
6435 xp->xp_pattern = arg;
6436
6437 // first argument already typed: only user function names
6438 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6439 {
6440 xp->xp_context = EXPAND_USER_FUNC;
6441 xp->xp_pattern = skipwhite(p);
6442 }
6443}
6444
6445/*
6446 * Function given to ExpandGeneric() to obtain the list of :disassemble
6447 * arguments.
6448 */
6449 char_u *
6450get_disassemble_argument(expand_T *xp, int idx)
6451{
6452 if (idx == 0)
6453 return (char_u *)"debug";
6454 if (idx == 1)
6455 return (char_u *)"profile";
6456 return get_user_func_name(xp, idx - 2);
6457}
6458
6459/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006460 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006461 * We don't really need this at runtime, but we do have tests that require it,
6462 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006463 */
6464 void
6465ex_disassemble(exarg_T *eap)
6466{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006467 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006468 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006469 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006470 isn_T *instr = NULL; // init to shut up gcc warning
6471 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006472 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006473
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006474 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006475 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006476 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006477 if (func_needs_compiling(ufunc, compile_type)
6478 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006479 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006480 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006481 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006482 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483 return;
6484 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006485 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486
6487 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006488 switch (compile_type)
6489 {
6490 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006491#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006492 instr = dfunc->df_instr_prof;
6493 instr_count = dfunc->df_instr_prof_count;
6494 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006495#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006496 // FALLTHROUGH
6497 case CT_NONE:
6498 instr = dfunc->df_instr;
6499 instr_count = dfunc->df_instr_count;
6500 break;
6501 case CT_DEBUG:
6502 instr = dfunc->df_instr_debug;
6503 instr_count = dfunc->df_instr_debug_count;
6504 break;
6505 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006506
Bram Moolenaar4c137212021-04-19 16:48:48 +02006507 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006508}
6509
6510/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006511 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006512 * list, etc. Mostly like what JavaScript does, except that empty list and
6513 * empty dictionary are FALSE.
6514 */
6515 int
6516tv2bool(typval_T *tv)
6517{
6518 switch (tv->v_type)
6519 {
6520 case VAR_NUMBER:
6521 return tv->vval.v_number != 0;
6522 case VAR_FLOAT:
6523#ifdef FEAT_FLOAT
6524 return tv->vval.v_float != 0.0;
6525#else
6526 break;
6527#endif
6528 case VAR_PARTIAL:
6529 return tv->vval.v_partial != NULL;
6530 case VAR_FUNC:
6531 case VAR_STRING:
6532 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6533 case VAR_LIST:
6534 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6535 case VAR_DICT:
6536 return tv->vval.v_dict != NULL
6537 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6538 case VAR_BOOL:
6539 case VAR_SPECIAL:
6540 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6541 case VAR_JOB:
6542#ifdef FEAT_JOB_CHANNEL
6543 return tv->vval.v_job != NULL;
6544#else
6545 break;
6546#endif
6547 case VAR_CHANNEL:
6548#ifdef FEAT_JOB_CHANNEL
6549 return tv->vval.v_channel != NULL;
6550#else
6551 break;
6552#endif
6553 case VAR_BLOB:
6554 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6555 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006556 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006557 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006558 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006559 break;
6560 }
6561 return FALSE;
6562}
6563
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006564 void
6565emsg_using_string_as(typval_T *tv, int as_number)
6566{
6567 semsg(_(as_number ? e_using_string_as_number_str
6568 : e_using_string_as_bool_str),
6569 tv->vval.v_string == NULL
6570 ? (char_u *)"" : tv->vval.v_string);
6571}
6572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573/*
6574 * If "tv" is a string give an error and return FAIL.
6575 */
6576 int
6577check_not_string(typval_T *tv)
6578{
6579 if (tv->v_type == VAR_STRING)
6580 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006581 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582 clear_tv(tv);
6583 return FAIL;
6584 }
6585 return OK;
6586}
6587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588
6589#endif // FEAT_EVAL