blob: 2448b6d7b1606b42f73969321bf7e75b3ffa26aa [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 Moolenaarb46c0832022-09-15 17:19:37 +0100507 if (GA_GROW_FAILS(&ectx->ec_stack,
508 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509 return FAIL;
510
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100511 // If depth of calling is getting too high, don't execute the function.
512 if (funcdepth_increment() == FAIL)
513 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200514 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100515
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100516 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200517 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100518 {
519 floc = ALLOC_ONE(funclocal_T);
520 if (floc == NULL)
521 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200522 *floc = ectx->ec_funclocal;
523 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100524 }
525
Bram Moolenaarfe270812020-04-11 22:31:27 +0200526 // Move the vararg-list to below the missing optional arguments.
527 if (vararg_count > 0 && arg_to_add > 0)
528 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100529
530 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200531 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200532 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200533 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534
535 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100536 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
537 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200538 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200539 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
540 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100541 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100542 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200543 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544
545 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200546 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000547 {
548 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
549
550 tv->v_type = VAR_NUMBER;
551 tv->vval.v_number = 0;
552 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200553 if (dfunc->df_has_closure)
554 {
555 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
556
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100557 // Initialize the variable that counts how many closures were created.
558 // This is used in handle_closure_in_use().
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200559 tv->v_type = VAR_NUMBER;
560 tv->vval.v_number = 0;
561 }
562 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100564 if (pt != NULL || ufunc->uf_partial != NULL
565 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100566 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200567 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100568
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200569 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100570 return FAIL;
571 if (pt != NULL)
572 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000573 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200574 ++pt->pt_refcount;
575 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100576 }
577 else if (ufunc->uf_partial != NULL)
578 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000579 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200580 ++ufunc->uf_partial->pt_refcount;
581 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100582 }
583 else
584 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200585 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200586 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200587 {
588 vim_free(ref);
589 return FAIL;
590 }
591 ref->or_outer_allocated = TRUE;
592 ref->or_outer->out_stack = &ectx->ec_stack;
593 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
594 if (ectx->ec_outer_ref != NULL)
595 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100596 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200597 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100598 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100599 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200600 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100601
Bram Moolenaarc970e422021-03-17 15:03:04 +0100602 ++ufunc->uf_calls;
603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604 // Set execution state to the start of the called function.
605 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100606 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100607 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200608 if (entry != NULL)
609 {
610 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200611 // Save the current context so it can be restored on return.
612 entry->es_save_sctx = current_sctx;
613 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200614 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100615
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200616 // Start execution at the first instruction.
617 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618
619 return OK;
620}
621
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000622// Double linked list of funcstack_T in use.
623static funcstack_T *first_funcstack = NULL;
624
625 static void
626add_funcstack_to_list(funcstack_T *funcstack)
627{
628 // Link in list of funcstacks.
629 if (first_funcstack != NULL)
630 first_funcstack->fs_prev = funcstack;
631 funcstack->fs_next = first_funcstack;
632 funcstack->fs_prev = NULL;
633 first_funcstack = funcstack;
634}
635
636 static void
637remove_funcstack_from_list(funcstack_T *funcstack)
638{
639 if (funcstack->fs_prev == NULL)
640 first_funcstack = funcstack->fs_next;
641 else
642 funcstack->fs_prev->fs_next = funcstack->fs_next;
643 if (funcstack->fs_next != NULL)
644 funcstack->fs_next->fs_prev = funcstack->fs_prev;
645}
646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200648 * Used when returning from a function: Check if any closure is still
649 * referenced. If so then move the arguments and variables to a separate piece
650 * of stack to be used when the closure is called.
651 * When "free_arguments" is TRUE the arguments are to be freed.
652 * Returns FAIL when out of memory.
653 */
654 static int
655handle_closure_in_use(ectx_T *ectx, int free_arguments)
656{
657 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
658 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200659 int argcount;
660 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200661 int idx;
662 typval_T *tv;
663 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200664 garray_T *gap = &ectx->ec_funcrefs;
665 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200666
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200667 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200668 return OK; // function was freed
669 if (dfunc->df_has_closure == 0)
670 return OK; // no closures
671 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
672 closure_count = tv->vval.v_number;
673 if (closure_count == 0)
674 return OK; // no funcrefs created
675
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100676 // Compute "top": the first entry in the stack used by the function.
677 // This is the first argument (after that comes the stack frame and then
678 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200679 argcount = ufunc_argcount(dfunc->df_ufunc);
680 top = ectx->ec_frame_idx - argcount;
681
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200682 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200683 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200684 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200685 partial_T *pt;
686 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200687
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200688 if (off < 0)
689 continue; // count is off or already done
690 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200691 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200692 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200693 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200694 int i;
695
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000696 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200697 // unreferenced on return.
698 for (i = 0; i < dfunc->df_varcount; ++i)
699 {
700 typval_T *stv = STACK_TV(ectx->ec_frame_idx
701 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200702 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200703 --refcount;
704 }
705 if (refcount > 1)
706 {
707 closure_in_use = TRUE;
708 break;
709 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200710 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200711 }
712
713 if (closure_in_use)
714 {
715 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
716 typval_T *stack;
717
718 // A closure is using the arguments and/or local variables.
719 // Move them to the called function.
720 if (funcstack == NULL)
721 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000722
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200723 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
724 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200725 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
726 funcstack->fs_ga.ga_data = stack;
727 if (stack == NULL)
728 {
729 vim_free(funcstack);
730 return FAIL;
731 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000732 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200733
734 // Move or copy the arguments.
735 for (idx = 0; idx < argcount; ++idx)
736 {
737 tv = STACK_TV(top + idx);
738 if (free_arguments)
739 {
740 *(stack + idx) = *tv;
741 tv->v_type = VAR_UNKNOWN;
742 }
743 else
744 copy_tv(tv, stack + idx);
745 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100746 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200747 // Move the local variables.
748 for (idx = 0; idx < dfunc->df_varcount; ++idx)
749 {
750 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200751
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200752 // A partial created for a local function, that is also used as a
753 // local variable, has a reference count for the variable, thus
754 // will never go down to zero. When all these refcounts are one
755 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000756 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200757 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
758 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200759 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200760
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200761 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200762 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
763 gap->ga_len - closure_count + i])
764 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200765 }
766
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200767 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200768 tv->v_type = VAR_UNKNOWN;
769 }
770
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200771 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200772 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200773 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
774 - closure_count + idx];
775 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200776 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100777 int prev_frame_idx = pt->pt_outer.out_frame_idx;
778
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200779 ++funcstack->fs_refcount;
780 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100781 pt->pt_outer.out_stack = &funcstack->fs_ga;
782 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100783
784 // TODO: drop this, should be done at ISN_ENDLOOP
785 pt->pt_outer.out_loop_stack = &funcstack->fs_ga;
786 pt->pt_outer.out_loop_var_idx -=
787 prev_frame_idx - pt->pt_outer.out_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200788 }
789 }
790 }
791
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200792 for (idx = 0; idx < closure_count; ++idx)
793 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
794 - closure_count + idx]);
795 gap->ga_len -= closure_count;
796 if (gap->ga_len == 0)
797 ga_clear(gap);
798
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200799 return OK;
800}
801
802/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200803 * Called when a partial is freed or its reference count goes down to one. The
804 * funcstack may be the only reference to the partials in the local variables.
805 * Go over all of them, the funcref and can be freed if all partials
806 * referencing the funcstack have a reference count of one.
807 */
808 void
809funcstack_check_refcount(funcstack_T *funcstack)
810{
811 int i;
812 garray_T *gap = &funcstack->fs_ga;
813 int done = 0;
814
815 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
816 return;
817 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
818 {
819 typval_T *tv = ((typval_T *)gap->ga_data) + i;
820
821 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
822 && tv->vval.v_partial->pt_funcstack == funcstack
823 && tv->vval.v_partial->pt_refcount == 1)
824 ++done;
825 }
826 if (done == funcstack->fs_min_refcount)
827 {
828 typval_T *stack = gap->ga_data;
829
830 // All partials referencing the funcstack have a reference count of
831 // one, thus the funcstack is no longer of use.
832 for (i = 0; i < gap->ga_len; ++i)
833 clear_tv(stack + i);
834 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000835 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200836 vim_free(funcstack);
837 }
838}
839
840/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000841 * For garbage collecting: set references in all variables referenced by
842 * all funcstacks.
843 */
844 int
845set_ref_in_funcstacks(int copyID)
846{
847 funcstack_T *funcstack;
848
849 for (funcstack = first_funcstack; funcstack != NULL;
850 funcstack = funcstack->fs_next)
851 {
852 typval_T *stack = funcstack->fs_ga.ga_data;
853 int i;
854
855 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
856 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
857 return TRUE; // abort
858 }
859 return FALSE;
860}
861
Bram Moolenaar806a2732022-09-04 15:40:36 +0100862// Ugly static to avoid passing the execution context around through many
863// layers.
864static ectx_T *current_ectx = NULL;
865
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000866/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100867 * Return TRUE if currently executing a :def function.
868 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100869 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100870 int
871in_def_function(void)
872{
873 return current_ectx != NULL;
874}
875
876/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100877 * Clear "current_ectx" and return the previous value. To be used when calling
878 * a user function.
879 */
880 ectx_T *
881clear_currrent_ectx(void)
882{
883 ectx_T *r = current_ectx;
884
885 current_ectx = NULL;
886 return r;
887}
888
889 void
890restore_current_ectx(ectx_T *ectx)
891{
892 if (current_ectx != NULL)
893 iemsg("Restoring current_ectx while it is not NULL");
894 current_ectx = ectx;
895}
896
897/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100898 * Add an entry for a deferred function call to the currently executing
899 * function.
900 * Return the list or NULL when failed.
901 */
902 static list_T *
903add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100904{
905 typval_T *defer_tv = STACK_TV_VAR(var_idx);
906 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100907 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100908 typval_T listval;
909
910 if (defer_tv->v_type != VAR_LIST)
911 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100912 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100913 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100914 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100915 }
916 defer_l = defer_tv->vval.v_list;
917
918 l = list_alloc_with_items(argcount + 1);
919 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100920 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100921 listval.v_type = VAR_LIST;
922 listval.vval.v_list = l;
923 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100924 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100925 {
926 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100927 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100928 }
929
Bram Moolenaar806a2732022-09-04 15:40:36 +0100930 return l;
931}
932
933/*
934 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
935 * The local variable that lists deferred functions is "var_idx".
936 * Returns OK or FAIL.
937 */
938 static int
939defer_command(int var_idx, int argcount, ectx_T *ectx)
940{
941 list_T *l = add_defer_item(var_idx, argcount, ectx);
942 int i;
943 typval_T *func_tv;
944
945 if (l == NULL)
946 return FAIL;
947
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100948 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100949 if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL)
950 {
951 semsg(_(e_expected_str_but_got_str),
952 "function or partial",
953 vartype_name(func_tv->v_type));
954 return FAIL;
955 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100956 list_set_item(l, 0, func_tv);
957
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100958 for (i = 0; i < argcount; ++i)
959 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100960 ectx->ec_stack.ga_len -= argcount + 1;
961 return OK;
962}
963
964/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100965 * Add a deferred function "name" with one argument "arg_tv".
966 * Consumes "name", also on failure.
967 * Only to be called when in_def_function() returns TRUE.
968 */
969 int
970add_defer_function(char_u *name, int argcount, typval_T *argvars)
971{
972 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
973 + current_ectx->ec_dfunc_idx;
974 list_T *l;
975 typval_T func_tv;
976 int i;
977
978 if (dfunc->df_defer_var_idx == 0)
979 {
980 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100981 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100982 return FAIL;
983 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100984
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100985 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100986 if (l == NULL)
987 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100988 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100989 return FAIL;
990 }
991
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100992 func_tv.v_type = VAR_FUNC;
993 func_tv.v_lock = 0;
994 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100995 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100996
Bram Moolenaar806a2732022-09-04 15:40:36 +0100997 for (i = 0; i < argcount; ++i)
998 list_set_item(l, i + 1, argvars + i);
999 return OK;
1000}
1001
1002/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001003 * Invoked when returning from a function: Invoke any deferred calls.
1004 */
1005 static void
1006invoke_defer_funcs(ectx_T *ectx)
1007{
1008 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1009 + ectx->ec_dfunc_idx;
1010 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1011 listitem_T *li;
1012
1013 if (defer_tv->v_type != VAR_LIST)
1014 return; // no function added
1015 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1016 {
1017 list_T *l = li->li_tv.vval.v_list;
1018 typval_T rettv;
1019 typval_T argvars[MAX_FUNC_ARGS];
1020 int i;
1021 listitem_T *arg_li = l->lv_first;
1022 funcexe_T funcexe;
1023
1024 for (i = 0; i < l->lv_len - 1; ++i)
1025 {
1026 arg_li = arg_li->li_next;
1027 argvars[i] = arg_li->li_tv;
1028 }
1029
1030 CLEAR_FIELD(funcexe);
1031 funcexe.fe_evaluate = TRUE;
1032 rettv.v_type = VAR_UNKNOWN;
1033 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1034 &rettv, l->lv_len - 1, argvars, &funcexe);
1035 clear_tv(&rettv);
1036 }
1037}
1038
1039/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040 * Return from the current function.
1041 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001042 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001043func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001046 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001047 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1048 + ectx->ec_dfunc_idx;
1049 int argcount = ufunc_argcount(dfunc->df_ufunc);
1050 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001051 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001052 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1053 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001054 funclocal_T *floc;
1055#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001056 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1057 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058
Bram Moolenaar12d26532021-02-19 19:13:21 +01001059 if (do_profiling == PROF_YES)
1060 {
1061 ufunc_T *caller = prev_dfunc->df_ufunc;
1062
1063 if (dfunc->df_ufunc->uf_profiling
1064 || (caller != NULL && caller->uf_profiling))
1065 {
1066 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1067 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1068 --profile_info_ga.ga_len;
1069 }
1070 }
1071#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001072
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001073 if (dfunc->df_defer_var_idx > 0)
1074 invoke_defer_funcs(ectx);
1075
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001076 // No check for uf_refcount being zero, cannot think of a way that would
1077 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001078 --dfunc->df_ufunc->uf_calls;
1079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001081 entry = estack_pop();
1082 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001083 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001085 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1086 return FAIL;
1087
1088 // Clear the arguments.
1089 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1090 clear_tv(STACK_TV(idx));
1091
1092 // Clear local variables and temp values, but not the return value.
1093 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001094 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001096
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001097 // The return value should be on top of the stack. However, when aborting
1098 // it may not be there and ec_frame_idx is the top of the stack.
1099 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001100 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001101 ret_idx = 0;
1102
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001103 if (ectx->ec_outer_ref != NULL)
1104 {
1105 if (ectx->ec_outer_ref->or_outer_allocated)
1106 vim_free(ectx->ec_outer_ref->or_outer);
1107 partial_unref(ectx->ec_outer_ref->or_partial);
1108 vim_free(ectx->ec_outer_ref);
1109 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001110
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001111 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001112 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001113 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1114 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001115 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1116 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001117 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001118 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001119 floc = (void *)STACK_TV(ectx->ec_frame_idx
1120 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001121 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001122 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1123 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001124
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001125 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001126 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001127 else
1128 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001129 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001130 vim_free(floc);
1131 }
1132
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001133 if (ret_idx > 0)
1134 {
1135 // Reset the stack to the position before the call, with a spot for the
1136 // return value, moved there from above the frame.
1137 ectx->ec_stack.ga_len = top + 1;
1138 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1139 }
1140 else
1141 // Reset the stack to the position before the call.
1142 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001143
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001144 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001145 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001146 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147}
1148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149/*
1150 * Prepare arguments and rettv for calling a builtin or user function.
1151 */
1152 static int
1153call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1154{
1155 int idx;
1156 typval_T *tv;
1157
1158 // Move arguments from bottom of the stack to argvars[] and add terminator.
1159 for (idx = 0; idx < argcount; ++idx)
1160 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1161 argvars[argcount].v_type = VAR_UNKNOWN;
1162
1163 // Result replaces the arguments on the stack.
1164 if (argcount > 0)
1165 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001166 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 return FAIL;
1168 else
1169 ++ectx->ec_stack.ga_len;
1170
1171 // Default return value is zero.
1172 tv = STACK_TV_BOT(-1);
1173 tv->v_type = VAR_NUMBER;
1174 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001175 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176
1177 return OK;
1178}
1179
1180/*
1181 * Call a builtin function by index.
1182 */
1183 static int
1184call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1185{
1186 typval_T argvars[MAX_FUNC_ARGS];
1187 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001188 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001189 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001190 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191
1192 if (call_prepare(argcount, argvars, ectx) == FAIL)
1193 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001194 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001196 // Call the builtin function. Set "current_ectx" so that when it
1197 // recursively invokes call_def_function() a closure context can be set.
1198 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001200 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001201 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202
1203 // Clear the arguments.
1204 for (idx = 0; idx < argcount; ++idx)
1205 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001206
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001207 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001208 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 return OK;
1210}
1211
1212/*
1213 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001214 * If the function is compiled this will add a stack frame and set the
1215 * instruction pointer at the start of the function.
1216 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001217 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001218 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 */
1220 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001221call_ufunc(
1222 ufunc_T *ufunc,
1223 partial_T *pt,
1224 int argcount,
1225 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001226 isn_T *iptr,
1227 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228{
1229 typval_T argvars[MAX_FUNC_ARGS];
1230 funcexe_T funcexe;
1231 int error;
1232 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001233 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001234 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235
Bram Moolenaare99d4222021-06-13 14:01:26 +02001236 if (func_needs_compiling(ufunc, compile_type)
1237 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1238 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001239 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001240 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001241 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001242 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001243 if (error != FCERR_UNKNOWN)
1244 {
1245 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001246 semsg(_(e_too_many_arguments_for_function_str),
1247 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001248 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001249 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001250 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001251 return FAIL;
1252 }
1253
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001254 // The function has been compiled, can call it quickly. For a function
1255 // that was defined later: we can call it directly next time.
1256 if (iptr != NULL)
1257 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001258 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001259 iptr->isn_type = ISN_DCALL;
1260 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1261 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1262 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001263 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001264 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265
1266 if (call_prepare(argcount, argvars, ectx) == FAIL)
1267 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001268 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001269 funcexe.fe_evaluate = TRUE;
1270 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271
1272 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001274 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275
1276 // Clear the arguments.
1277 for (idx = 0; idx < argcount; ++idx)
1278 clear_tv(&argvars[idx]);
1279
1280 if (error != FCERR_NONE)
1281 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001282 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 return FAIL;
1284 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001285 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001286 // Error other than from calling the function itself.
1287 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 return OK;
1289}
1290
1291/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001292 * If command modifiers were applied restore them.
1293 */
1294 static void
1295may_restore_cmdmod(funclocal_T *funclocal)
1296{
1297 if (funclocal->floc_restore_cmdmod)
1298 {
1299 cmdmod.cmod_filter_regmatch.regprog = NULL;
1300 undo_cmdmod(&cmdmod);
1301 cmdmod = funclocal->floc_save_cmdmod;
1302 funclocal->floc_restore_cmdmod = FALSE;
1303 }
1304}
1305
1306/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001307 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1308 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001309 */
1310 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001311vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001312{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001313 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001314}
1315
1316/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 * Execute a function by "name".
1318 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001319 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 * Returns FAIL if not found without an error message.
1321 */
1322 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001323call_by_name(
1324 char_u *name,
1325 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001326 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001327 isn_T *iptr,
1328 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329{
1330 ufunc_T *ufunc;
1331
1332 if (builtin_function(name, -1))
1333 {
1334 int func_idx = find_internal_func(name);
1335
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001336 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001338 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 return FAIL;
1340 return call_bfunc(func_idx, argcount, ectx);
1341 }
1342
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001343 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001344
1345 if (ufunc == NULL)
1346 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001347 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001348
1349 if (script_autoload(name, TRUE))
1350 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001351 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001352
1353 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001354 return FAIL; // bail out if loading the script caused an error
1355 }
1356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001358 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001359 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001360 {
1361 int i;
1362 typval_T *argv = STACK_TV_BOT(0) - argcount;
1363
1364 // The function can change at runtime, check that the argument
1365 // types are correct.
1366 for (i = 0; i < argcount; ++i)
1367 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001368 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001369
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001370 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001371 type = ufunc->uf_arg_types[i];
1372 else if (ufunc->uf_va_type != NULL)
1373 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001374 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001375 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001376 return FAIL;
1377 }
1378 }
1379
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001380 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001381 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382
1383 return FAIL;
1384}
1385
1386 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001387call_partial(
1388 typval_T *tv,
1389 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001390 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001392 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001393 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001395 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001396 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397
1398 if (tv->v_type == VAR_PARTIAL)
1399 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001400 partial_T *pt = tv->vval.v_partial;
1401 int i;
1402
1403 if (pt->pt_argc > 0)
1404 {
1405 // Make space for arguments from the partial, shift the "argcount"
1406 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001407 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001408 return FAIL;
1409 for (i = 1; i <= argcount; ++i)
1410 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1411 ectx->ec_stack.ga_len += pt->pt_argc;
1412 argcount += pt->pt_argc;
1413
1414 // copy the arguments from the partial onto the stack
1415 for (i = 0; i < pt->pt_argc; ++i)
1416 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1417 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001418 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419
1420 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001421 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 name = pt->pt_name;
1424 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001425 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001427 if (name != NULL)
1428 {
1429 char_u fname_buf[FLEN_FIXED + 1];
1430 char_u *tofree = NULL;
1431 int error = FCERR_NONE;
1432 char_u *fname;
1433
1434 // May need to translate <SNR>123_ to K_SNR.
1435 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1436 if (error != FCERR_NONE)
1437 res = FAIL;
1438 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001439 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001440 vim_free(tofree);
1441 }
1442
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001443 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 {
1445 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001446 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001447 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 return FAIL;
1449 }
1450 return OK;
1451}
1452
1453/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001454 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1455 * TRUE.
1456 */
1457 static int
1458error_if_locked(int lock, char *error)
1459{
1460 if (lock & (VAR_LOCKED | VAR_FIXED))
1461 {
1462 emsg(_(error));
1463 return TRUE;
1464 }
1465 return FALSE;
1466}
1467
1468/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001469 * Give an error if "tv" is not a number and return FAIL.
1470 */
1471 static int
1472check_for_number(typval_T *tv)
1473{
1474 if (tv->v_type != VAR_NUMBER)
1475 {
1476 semsg(_(e_expected_str_but_got_str),
1477 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1478 return FAIL;
1479 }
1480 return OK;
1481}
1482
1483/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001484 * Store "tv" in variable "name".
1485 * This is for s: and g: variables.
1486 */
1487 static void
1488store_var(char_u *name, typval_T *tv)
1489{
1490 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001491 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001492
Bram Moolenaard877a572021-04-01 19:42:48 +02001493 if (tv->v_lock)
1494 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001495 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001496 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001497 restore_funccal();
1498}
1499
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001500/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001501 * Convert "tv" to a string.
1502 * Return FAIL if not allowed.
1503 */
1504 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001505do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001506{
1507 if (tv->v_type != VAR_STRING)
1508 {
1509 char_u *str;
1510
1511 if (is_2string_any)
1512 {
1513 switch (tv->v_type)
1514 {
1515 case VAR_SPECIAL:
1516 case VAR_BOOL:
1517 case VAR_NUMBER:
1518 case VAR_FLOAT:
1519 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001520
1521 case VAR_LIST:
1522 if (tolerant)
1523 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001524 char_u *s, *e, *p;
1525 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001526
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001527 ga_init2(&ga, sizeof(char_u *), 1);
1528
1529 // Convert to NL separated items, then
1530 // escape the items and replace the NL with
1531 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001532 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001533 if (str == NULL)
1534 return FAIL;
1535 s = str;
1536 while ((e = vim_strchr(s, '\n')) != NULL)
1537 {
1538 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001539 p = vim_strsave_fnameescape(s,
1540 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001541 if (p != NULL)
1542 {
1543 ga_concat(&ga, p);
1544 ga_concat(&ga, (char_u *)" ");
1545 vim_free(p);
1546 }
1547 s = e + 1;
1548 }
1549 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001550 clear_tv(tv);
1551 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001552 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001553 return OK;
1554 }
1555 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001556 default: to_string_error(tv->v_type);
1557 return FAIL;
1558 }
1559 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001560 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001561 clear_tv(tv);
1562 tv->v_type = VAR_STRING;
1563 tv->vval.v_string = str;
1564 }
1565 return OK;
1566}
1567
1568/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001569 * When the value of "sv" is a null list of dict, allocate it.
1570 */
1571 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001572allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001573{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001574 typval_T *tv = sv->sv_tv;
1575
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001576 switch (tv->v_type)
1577 {
1578 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001579 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001580 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001581 break;
1582 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001583 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001584 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001585 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001586 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001587 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001588 (void)rettv_blob_alloc(tv);
1589 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001590 default:
1591 break;
1592 }
1593}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001594
Bram Moolenaare7525c52021-01-09 13:20:37 +01001595/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001596 * Return the character "str[index]" where "index" is the character index,
1597 * including composing characters.
1598 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001599 */
1600 char_u *
1601char_from_string(char_u *str, varnumber_T index)
1602{
1603 size_t nbyte = 0;
1604 varnumber_T nchar = index;
1605 size_t slen;
1606
1607 if (str == NULL)
1608 return NULL;
1609 slen = STRLEN(str);
1610
Bram Moolenaarff871402021-03-26 13:34:05 +01001611 // Do the same as for a list: a negative index counts from the end.
1612 // Optimization to check the first byte to be below 0x80 (and no composing
1613 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001614 if (index < 0)
1615 {
1616 int clen = 0;
1617
1618 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001619 {
1620 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1621 ++nbyte;
1622 else if (enc_utf8)
1623 nbyte += utfc_ptr2len(str + nbyte);
1624 else
1625 nbyte += mb_ptr2len(str + nbyte);
1626 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001627 nchar = clen + index;
1628 if (nchar < 0)
1629 // unlike list: index out of range results in empty string
1630 return NULL;
1631 }
1632
1633 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001634 {
1635 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1636 ++nbyte;
1637 else if (enc_utf8)
1638 nbyte += utfc_ptr2len(str + nbyte);
1639 else
1640 nbyte += mb_ptr2len(str + nbyte);
1641 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001642 if (nbyte >= slen)
1643 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001644 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001645}
1646
1647/*
1648 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001649 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001650 * If going over the end return "str_len".
1651 * If "idx" is negative count from the end, -1 is the last character.
1652 * When going over the start return -1.
1653 */
1654 static long
1655char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1656{
1657 varnumber_T nchar = idx;
1658 size_t nbyte = 0;
1659
1660 if (nchar >= 0)
1661 {
1662 while (nchar > 0 && nbyte < str_len)
1663 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001664 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001665 --nchar;
1666 }
1667 }
1668 else
1669 {
1670 nbyte = str_len;
1671 while (nchar < 0 && nbyte > 0)
1672 {
1673 --nbyte;
1674 nbyte -= mb_head_off(str, str + nbyte);
1675 ++nchar;
1676 }
1677 if (nchar < 0)
1678 return -1;
1679 }
1680 return (long)nbyte;
1681}
1682
1683/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001684 * Return the slice "str[first : last]" using character indexes. Composing
1685 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001686 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001687 * Return NULL when the result is empty.
1688 */
1689 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001690string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001691{
1692 long start_byte, end_byte;
1693 size_t slen;
1694
1695 if (str == NULL)
1696 return NULL;
1697 slen = STRLEN(str);
1698 start_byte = char_idx2byte(str, slen, first);
1699 if (start_byte < 0)
1700 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001701 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001702 end_byte = (long)slen;
1703 else
1704 {
1705 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001706 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001707 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001708 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001709 }
1710
1711 if (start_byte >= (long)slen || end_byte <= start_byte)
1712 return NULL;
1713 return vim_strnsave(str + start_byte, end_byte - start_byte);
1714}
1715
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001716/*
1717 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1718 * When "dfunc_idx" is negative don't give an error.
1719 * Returns NULL for an error.
1720 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001721 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001722get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001723{
1724 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001725 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1726 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001727 svar_T *sv;
1728
1729 if (sref->sref_seq != si->sn_script_seq)
1730 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001731 // The script was reloaded after the function was compiled, the
1732 // script_idx may not be valid.
1733 if (dfunc != NULL)
1734 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1735 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001736 return NULL;
1737 }
1738 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001739 if (sv->sv_name == NULL)
1740 {
1741 if (dfunc != NULL)
1742 emsg(_(e_script_variable_was_deleted));
1743 return NULL;
1744 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001745 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001746 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001747 if (dfunc != NULL)
1748 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001749 return NULL;
1750 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001751
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001752 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1753 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001754 {
1755 if (dfunc != NULL)
1756 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1757 return NULL;
1758 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001759 return sv;
1760}
1761
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001762/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001763 * Function passed to do_cmdline() for splitting a script joined by NL
1764 * characters.
1765 */
1766 static char_u *
1767get_split_sourceline(
1768 int c UNUSED,
1769 void *cookie,
1770 int indent UNUSED,
1771 getline_opt_T options UNUSED)
1772{
1773 source_cookie_T *sp = (source_cookie_T *)cookie;
1774 char_u *p;
1775 char_u *line;
1776
Bram Moolenaar20677332021-06-06 17:02:53 +02001777 p = vim_strchr(sp->nextline, '\n');
1778 if (p == NULL)
1779 {
1780 line = vim_strsave(sp->nextline);
1781 sp->nextline += STRLEN(sp->nextline);
1782 }
1783 else
1784 {
1785 line = vim_strnsave(sp->nextline, p - sp->nextline);
1786 sp->nextline = p + 1;
1787 }
1788 return line;
1789}
1790
1791/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 * Execute a function by "name".
1793 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001794 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 */
1796 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001797call_eval_func(
1798 char_u *name,
1799 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001800 ectx_T *ectx,
1801 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802{
Bram Moolenaared677f52020-08-12 16:38:10 +02001803 int called_emsg_before = called_emsg;
1804 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001806 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001807 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001809 dictitem_T *v;
1810
1811 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001812 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1813 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001814 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001815 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001816 return FAIL;
1817 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001818 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001820 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821}
1822
1823/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001824 * When a function reference is used, fill a partial with the information
1825 * needed, especially when it is used as a closure.
1826 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001827 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001828fill_partial_and_closure(
1829 partial_T *pt,
1830 ufunc_T *ufunc,
1831 short loop_var_idx,
1832 short loop_var_count,
1833 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001834{
1835 pt->pt_func = ufunc;
1836 pt->pt_refcount = 1;
1837
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001838 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001839 {
1840 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1841 + ectx->ec_dfunc_idx;
1842
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001843 // The closure may need to find arguments and local variables of the
1844 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001845 pt->pt_outer.out_stack = &ectx->ec_stack;
1846 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001847 if (ectx->ec_outer_ref != NULL)
1848 {
1849 // The current context already has a context, link to that one.
1850 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1851 if (ectx->ec_outer_ref->or_partial != NULL)
1852 {
1853 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1854 ++pt->pt_outer.out_up_partial->pt_refcount;
1855 }
1856 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001857
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001858 // The closure may need to find variables defined inside a loop. A
1859 // new reference is made every time, ISN_ENDLOOP will check if they
1860 // are actually used.
1861 pt->pt_outer.out_loop_stack = &ectx->ec_stack;
1862 pt->pt_outer.out_loop_var_idx = ectx->ec_frame_idx + STACK_FRAME_SIZE
1863 + loop_var_idx;
1864 pt->pt_outer.out_loop_var_count = loop_var_count;
1865
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001866 // If the function currently executing returns and the closure is still
1867 // being referenced, we need to make a copy of the context (arguments
1868 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001869 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001870 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001871 {
1872 vim_free(pt);
1873 return FAIL;
1874 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001875 // Extra variable keeps the count of closures created in the current
1876 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001877 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1878 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1879
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001880 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1881 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001882 ++pt->pt_refcount;
1883 ++ectx->ec_funcrefs.ga_len;
1884 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001885 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001886 return OK;
1887}
1888
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001889/*
1890 * Execute iptr->isn_arg.string as an Ex command.
1891 */
1892 static int
1893exec_command(isn_T *iptr)
1894{
1895 source_cookie_T cookie;
1896
1897 SOURCING_LNUM = iptr->isn_lnum;
1898 // Pass getsourceline to get an error for a missing ":end"
1899 // command.
1900 CLEAR_FIELD(cookie);
1901 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1902 if (do_cmdline(iptr->isn_arg.string,
1903 getsourceline, &cookie,
1904 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1905 || did_emsg)
1906 return FAIL;
1907 return OK;
1908}
1909
Bram Moolenaar89445512022-04-14 12:58:23 +01001910/*
1911 * If script "sid" is not loaded yet then load it now.
1912 * Caller must make sure "sid" is a valid script ID.
1913 * "loaded" is set to TRUE if the script had to be loaded.
1914 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1915 */
1916 int
1917may_load_script(int sid, int *loaded)
1918{
1919 scriptitem_T *si = SCRIPT_ITEM(sid);
1920
1921 if (si->sn_state == SN_STATE_NOT_LOADED)
1922 {
1923 *loaded = TRUE;
1924 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1925 {
1926 semsg(_(e_cant_open_file_str), si->sn_name);
1927 return FAIL;
1928 }
1929 }
1930 return OK;
1931}
1932
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001933// used for v_instr of typval of VAR_INSTR
1934struct instr_S {
1935 ectx_T *instr_ectx;
1936 isn_T *instr_instr;
1937};
1938
Bram Moolenaar4c137212021-04-19 16:48:48 +02001939// used for substitute_instr
1940typedef struct subs_expr_S {
1941 ectx_T *subs_ectx;
1942 isn_T *subs_instr;
1943 int subs_status;
1944} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001946// Set when calling do_debug().
1947static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001948static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001949
1950/*
1951 * When debugging lookup "name" and return the typeval.
1952 * When not found return NULL.
1953 */
1954 typval_T *
1955lookup_debug_var(char_u *name)
1956{
1957 int idx;
1958 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001959 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001960 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001961 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001962
1963 if (ectx == NULL)
1964 return NULL;
1965 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1966
1967 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001968 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001969 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001970 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1971
1972 // the variable name may be NULL when not available in this block
1973 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001974 return STACK_TV_VAR(idx);
1975 }
1976
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001977 // Go through argument names.
1978 ufunc = dfunc->df_ufunc;
1979 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1980 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1981 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1982 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1983 - varargs_off + idx);
1984 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1985 return STACK_TV(ectx->ec_frame_idx - 1);
1986
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001987 return NULL;
1988}
1989
Bram Moolenaar26a44842021-09-02 18:49:06 +02001990/*
1991 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1992 * breakpoint was set in that function or when there is any expression.
1993 */
1994 int
1995may_break_in_function(ufunc_T *ufunc)
1996{
1997 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1998}
1999
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002000 static void
2001handle_debug(isn_T *iptr, ectx_T *ectx)
2002{
2003 char_u *line;
2004 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2005 + ectx->ec_dfunc_idx)->df_ufunc;
2006 isn_T *ni;
2007 int end_lnum = iptr->isn_lnum;
2008 garray_T ga;
2009 int lnum;
2010
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002011 if (ex_nesting_level > debug_break_level)
2012 {
2013 linenr_T breakpoint;
2014
Bram Moolenaar26a44842021-09-02 18:49:06 +02002015 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002016 return;
2017
2018 // check for the next breakpoint if needed
2019 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002020 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002021 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2022 return;
2023 }
2024
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002025 SOURCING_LNUM = iptr->isn_lnum;
2026 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002027 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002028
2029 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2030 if (ni->isn_type == ISN_DEBUG
2031 || ni->isn_type == ISN_RETURN
2032 || ni->isn_type == ISN_RETURN_VOID)
2033 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002034 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002035 break;
2036 }
2037
2038 if (end_lnum > iptr->isn_lnum)
2039 {
2040 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002041 for (lnum = iptr->isn_lnum; lnum < end_lnum
2042 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002043 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002044 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002045
Bram Moolenaar303215d2021-07-07 20:10:43 +02002046 if (p == NULL)
2047 continue; // left over from continuation line
2048 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002049 if (*p == '#')
2050 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002051 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002052 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2053 if (STRNCMP(p, "def ", 4) == 0)
2054 break;
2055 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002056 line = ga_concat_strings(&ga, " ");
2057 vim_free(ga.ga_data);
2058 }
2059 else
2060 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002061
Dominique Pelle6e969552021-06-17 13:53:41 +02002062 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002063 debug_context = NULL;
2064
2065 if (end_lnum > iptr->isn_lnum)
2066 vim_free(line);
2067}
2068
Bram Moolenaar4c137212021-04-19 16:48:48 +02002069/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002070 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002071 * Returns OK, FAIL or NOTDONE (uncatchable error).
2072 */
2073 static int
2074execute_storeindex(isn_T *iptr, ectx_T *ectx)
2075{
2076 vartype_T dest_type = iptr->isn_arg.vartype;
2077 typval_T *tv;
2078 typval_T *tv_idx = STACK_TV_BOT(-2);
2079 typval_T *tv_dest = STACK_TV_BOT(-1);
2080 int status = OK;
2081
2082 // Stack contains:
2083 // -3 value to be stored
2084 // -2 index
2085 // -1 dict or list
2086 tv = STACK_TV_BOT(-3);
2087 SOURCING_LNUM = iptr->isn_lnum;
2088 if (dest_type == VAR_ANY)
2089 {
2090 dest_type = tv_dest->v_type;
2091 if (dest_type == VAR_DICT)
2092 status = do_2string(tv_idx, TRUE, FALSE);
2093 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2094 {
2095 emsg(_(e_number_expected));
2096 status = FAIL;
2097 }
2098 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002099
2100 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002101 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002102 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002103 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002104 long lidx = (long)tv_idx->vval.v_number;
2105 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002106
Bram Moolenaare08be092022-02-17 13:08:26 +00002107 if (list == NULL)
2108 {
2109 emsg(_(e_list_not_set));
2110 return FAIL;
2111 }
2112 if (lidx < 0 && list->lv_len + lidx >= 0)
2113 // negative index is relative to the end
2114 lidx = list->lv_len + lidx;
2115 if (lidx < 0 || lidx > list->lv_len)
2116 {
2117 semsg(_(e_list_index_out_of_range_nr), lidx);
2118 return FAIL;
2119 }
2120 if (lidx < list->lv_len)
2121 {
2122 listitem_T *li = list_find(list, lidx);
2123
2124 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002125 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002126 return FAIL;
2127 // overwrite existing list item
2128 clear_tv(&li->li_tv);
2129 li->li_tv = *tv;
2130 }
2131 else
2132 {
2133 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2134 return FAIL;
2135 // append to list, only fails when out of memory
2136 if (list_append_tv(list, tv) == FAIL)
2137 return NOTDONE;
2138 clear_tv(tv);
2139 }
2140 }
2141 else if (dest_type == VAR_DICT)
2142 {
2143 char_u *key = tv_idx->vval.v_string;
2144 dict_T *dict = tv_dest->vval.v_dict;
2145 dictitem_T *di;
2146
2147 SOURCING_LNUM = iptr->isn_lnum;
2148 if (dict == NULL)
2149 {
2150 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002151 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002152 }
2153 if (key == NULL)
2154 key = (char_u *)"";
2155 di = dict_find(dict, key, -1);
2156 if (di != NULL)
2157 {
2158 if (error_if_locked(di->di_tv.v_lock,
2159 e_cannot_change_dict_item))
2160 return FAIL;
2161 // overwrite existing value
2162 clear_tv(&di->di_tv);
2163 di->di_tv = *tv;
2164 }
2165 else
2166 {
2167 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2168 return FAIL;
2169 // add to dict, only fails when out of memory
2170 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2171 return NOTDONE;
2172 clear_tv(tv);
2173 }
2174 }
2175 else if (dest_type == VAR_BLOB)
2176 {
2177 long lidx = (long)tv_idx->vval.v_number;
2178 blob_T *blob = tv_dest->vval.v_blob;
2179 varnumber_T nr;
2180 int error = FALSE;
2181 int len;
2182
2183 if (blob == NULL)
2184 {
2185 emsg(_(e_blob_not_set));
2186 return FAIL;
2187 }
2188 len = blob_len(blob);
2189 if (lidx < 0 && len + lidx >= 0)
2190 // negative index is relative to the end
2191 lidx = len + lidx;
2192
2193 // Can add one byte at the end.
2194 if (lidx < 0 || lidx > len)
2195 {
2196 semsg(_(e_blob_index_out_of_range_nr), lidx);
2197 return FAIL;
2198 }
2199 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2200 return FAIL;
2201 nr = tv_get_number_chk(tv, &error);
2202 if (error)
2203 return FAIL;
2204 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002205 }
2206 else
2207 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002208 status = FAIL;
2209 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002210 }
2211 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002212
2213 clear_tv(tv_idx);
2214 clear_tv(tv_dest);
2215 ectx->ec_stack.ga_len -= 3;
2216 if (status == FAIL)
2217 {
2218 clear_tv(tv);
2219 return FAIL;
2220 }
2221 return OK;
2222}
2223
2224/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002225 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002226 */
2227 static int
2228execute_storerange(isn_T *iptr, ectx_T *ectx)
2229{
2230 typval_T *tv;
2231 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2232 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2233 typval_T *tv_dest = STACK_TV_BOT(-1);
2234 int status = OK;
2235
2236 // Stack contains:
2237 // -4 value to be stored
2238 // -3 first index or "none"
2239 // -2 second index or "none"
2240 // -1 destination list or blob
2241 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002242 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002243 if (tv_dest->v_type == VAR_LIST)
2244 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002245 long n1;
2246 long n2;
2247 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002248
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002249 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2250 if (tv_idx2->v_type == VAR_SPECIAL
2251 && tv_idx2->vval.v_number == VVAL_NONE)
2252 n2 = list_len(tv_dest->vval.v_list) - 1;
2253 else
2254 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2255
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002256 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002257 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002258 status = FAIL;
2259 else
2260 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002261 status = check_range_index_two(tv_dest->vval.v_list,
2262 &n1, li1, &n2, FALSE);
2263 if (status != FAIL)
2264 status = list_assign_range(
2265 tv_dest->vval.v_list,
2266 tv->vval.v_list,
2267 n1,
2268 n2,
2269 tv_idx2->v_type == VAR_SPECIAL,
2270 (char_u *)"=",
2271 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002272 }
2273 }
2274 else if (tv_dest->v_type == VAR_BLOB)
2275 {
2276 varnumber_T n1;
2277 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002278 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002279
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002280 n1 = tv_get_number_chk(tv_idx1, NULL);
2281 if (tv_idx2->v_type == VAR_SPECIAL
2282 && tv_idx2->vval.v_number == VVAL_NONE)
2283 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2284 else
2285 n2 = tv_get_number_chk(tv_idx2, NULL);
2286 bloblen = blob_len(tv_dest->vval.v_blob);
2287
2288 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2289 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002290 status = FAIL;
2291 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002292 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002293 }
2294 else
2295 {
2296 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002297 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002298 }
2299
2300 clear_tv(tv_idx1);
2301 clear_tv(tv_idx2);
2302 clear_tv(tv_dest);
2303 ectx->ec_stack.ga_len -= 4;
2304 clear_tv(tv);
2305
2306 return status;
2307}
2308
2309/*
2310 * Unlet item in list or dict variable.
2311 */
2312 static int
2313execute_unletindex(isn_T *iptr, ectx_T *ectx)
2314{
2315 typval_T *tv_idx = STACK_TV_BOT(-2);
2316 typval_T *tv_dest = STACK_TV_BOT(-1);
2317 int status = OK;
2318
2319 // Stack contains:
2320 // -2 index
2321 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002322 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002323 if (tv_dest->v_type == VAR_DICT)
2324 {
2325 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002326 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002327 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002328 semsg(_(e_expected_str_but_got_str),
2329 vartype_name(VAR_STRING),
2330 vartype_name(tv_idx->v_type));
2331 status = FAIL;
2332 }
2333 else
2334 {
2335 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002336 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002337 dictitem_T *di = NULL;
2338
2339 if (d != NULL && value_check_lock(
2340 d->dv_lock, NULL, FALSE))
2341 status = FAIL;
2342 else
2343 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002344 if (tv_idx->v_type == VAR_STRING)
2345 {
2346 key = tv_idx->vval.v_string;
2347 if (key == NULL)
2348 key = (char_u *)"";
2349 }
2350 else
2351 {
2352 key = tv_get_string(tv_idx);
2353 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002354 if (d != NULL)
2355 di = dict_find(d, key, (int)STRLEN(key));
2356 if (di == NULL)
2357 {
2358 // NULL dict is equivalent to empty dict
2359 semsg(_(e_key_not_present_in_dictionary),
2360 key);
2361 status = FAIL;
2362 }
2363 else if (var_check_fixed(di->di_flags,
2364 NULL, FALSE)
2365 || var_check_ro(di->di_flags,
2366 NULL, FALSE))
2367 status = FAIL;
2368 else
2369 dictitem_remove(d, di);
2370 }
2371 }
2372 }
2373 else if (tv_dest->v_type == VAR_LIST)
2374 {
2375 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002376 if (check_for_number(tv_idx) == FAIL)
2377 {
2378 status = FAIL;
2379 }
2380 else
2381 {
2382 list_T *l = tv_dest->vval.v_list;
2383 long n = (long)tv_idx->vval.v_number;
2384
2385 if (l != NULL && value_check_lock(
2386 l->lv_lock, NULL, FALSE))
2387 status = FAIL;
2388 else
2389 {
2390 listitem_T *li = list_find(l, n);
2391
2392 if (li == NULL)
2393 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002394 semsg(_(e_list_index_out_of_range_nr), n);
2395 status = FAIL;
2396 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002397 else
2398 listitem_remove(l, li);
2399 }
2400 }
2401 }
2402 else
2403 {
2404 status = FAIL;
2405 semsg(_(e_cannot_index_str),
2406 vartype_name(tv_dest->v_type));
2407 }
2408
2409 clear_tv(tv_idx);
2410 clear_tv(tv_dest);
2411 ectx->ec_stack.ga_len -= 2;
2412
2413 return status;
2414}
2415
2416/*
2417 * Unlet a range of items in a list variable.
2418 */
2419 static int
2420execute_unletrange(isn_T *iptr, ectx_T *ectx)
2421{
2422 // Stack contains:
2423 // -3 index1
2424 // -2 index2
2425 // -1 dict or list
2426 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2427 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2428 typval_T *tv_dest = STACK_TV_BOT(-1);
2429 int status = OK;
2430
2431 if (tv_dest->v_type == VAR_LIST)
2432 {
2433 // indexes must be a number
2434 SOURCING_LNUM = iptr->isn_lnum;
2435 if (check_for_number(tv_idx1) == FAIL
2436 || (tv_idx2->v_type != VAR_SPECIAL
2437 && check_for_number(tv_idx2) == FAIL))
2438 {
2439 status = FAIL;
2440 }
2441 else
2442 {
2443 list_T *l = tv_dest->vval.v_list;
2444 long n1 = (long)tv_idx1->vval.v_number;
2445 long n2 = tv_idx2->v_type == VAR_SPECIAL
2446 ? 0 : (long)tv_idx2->vval.v_number;
2447 listitem_T *li;
2448
2449 li = list_find_index(l, &n1);
2450 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002451 {
2452 semsg(_(e_list_index_out_of_range_nr),
2453 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002454 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002455 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002456 else
2457 {
2458 if (n1 < 0)
2459 n1 = list_idx_of_item(l, li);
2460 if (n2 < 0)
2461 {
2462 listitem_T *li2 = list_find(l, n2);
2463
2464 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002465 {
2466 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002467 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002468 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002469 else
2470 n2 = list_idx_of_item(l, li2);
2471 }
2472 if (status != FAIL
2473 && tv_idx2->v_type != VAR_SPECIAL
2474 && n2 < n1)
2475 {
2476 semsg(_(e_list_index_out_of_range_nr), n2);
2477 status = FAIL;
2478 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002479 if (status != FAIL)
2480 list_unlet_range(l, li, n1,
2481 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002482 }
2483 }
2484 }
2485 else
2486 {
2487 status = FAIL;
2488 SOURCING_LNUM = iptr->isn_lnum;
2489 semsg(_(e_cannot_index_str),
2490 vartype_name(tv_dest->v_type));
2491 }
2492
2493 clear_tv(tv_idx1);
2494 clear_tv(tv_idx2);
2495 clear_tv(tv_dest);
2496 ectx->ec_stack.ga_len -= 3;
2497
2498 return status;
2499}
2500
2501/*
2502 * Top of a for loop.
2503 */
2504 static int
2505execute_for(isn_T *iptr, ectx_T *ectx)
2506{
2507 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002508 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002509 typval_T *ltv = STACK_TV_BOT(-1);
2510 typval_T *idxtv =
2511 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2512
2513 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2514 return FAIL;
2515 if (ltv->v_type == VAR_LIST)
2516 {
2517 list_T *list = ltv->vval.v_list;
2518
2519 // push the next item from the list
2520 ++idxtv->vval.v_number;
2521 if (list == NULL
2522 || idxtv->vval.v_number >= list->lv_len)
2523 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002524 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002525 }
2526 else if (list->lv_first == &range_list_item)
2527 {
2528 // non-materialized range() list
2529 tv = STACK_TV_BOT(0);
2530 tv->v_type = VAR_NUMBER;
2531 tv->v_lock = 0;
2532 tv->vval.v_number = list_find_nr(
2533 list, idxtv->vval.v_number, NULL);
2534 ++ectx->ec_stack.ga_len;
2535 }
2536 else
2537 {
2538 listitem_T *li = list_find(list,
2539 idxtv->vval.v_number);
2540
2541 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2542 ++ectx->ec_stack.ga_len;
2543 }
2544 }
2545 else if (ltv->v_type == VAR_STRING)
2546 {
2547 char_u *str = ltv->vval.v_string;
2548
2549 // The index is for the last byte of the previous
2550 // character.
2551 ++idxtv->vval.v_number;
2552 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2553 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002554 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002555 }
2556 else
2557 {
2558 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2559
2560 // Push the next character from the string.
2561 tv = STACK_TV_BOT(0);
2562 tv->v_type = VAR_STRING;
2563 tv->vval.v_string = vim_strnsave(
2564 str + idxtv->vval.v_number, clen);
2565 ++ectx->ec_stack.ga_len;
2566 idxtv->vval.v_number += clen - 1;
2567 }
2568 }
2569 else if (ltv->v_type == VAR_BLOB)
2570 {
2571 blob_T *blob = ltv->vval.v_blob;
2572
2573 // When we get here the first time make a copy of the
2574 // blob, so that the iteration still works when it is
2575 // changed.
2576 if (idxtv->vval.v_number == -1 && blob != NULL)
2577 {
2578 blob_copy(blob, ltv);
2579 blob_unref(blob);
2580 blob = ltv->vval.v_blob;
2581 }
2582
2583 // The index is for the previous byte.
2584 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002585 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002586 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002587 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002588 }
2589 else
2590 {
2591 // Push the next byte from the blob.
2592 tv = STACK_TV_BOT(0);
2593 tv->v_type = VAR_NUMBER;
2594 tv->vval.v_number = blob_get(blob,
2595 idxtv->vval.v_number);
2596 ++ectx->ec_stack.ga_len;
2597 }
2598 }
2599 else
2600 {
2601 semsg(_(e_for_loop_on_str_not_supported),
2602 vartype_name(ltv->v_type));
2603 return FAIL;
2604 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002605
2606 if (jump)
2607 {
2608 // past the end of the list/string/blob, jump to "endfor"
2609 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2610 may_restore_cmdmod(&ectx->ec_funclocal);
2611 }
2612 else
2613 {
2614 // Store the current number of funcrefs, this may be used in
2615 // ISN_LOOPEND. The variable index is always one more than the loop
2616 // variable index.
2617 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_idx + 1);
2618 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2619 }
2620
2621 return OK;
2622}
2623
2624/*
2625 * End of a for or while loop: Handle any variables used by a closure.
2626 */
2627 static int
2628execute_endloop(isn_T *iptr UNUSED, ectx_T *ectx UNUSED)
2629{
2630 // TODO
2631
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002632 return OK;
2633}
2634
2635/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002636 * Load instruction for w:/b:/g:/t: variable.
2637 * "isn_type" is used instead of "iptr->isn_type".
2638 */
2639 static int
2640load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2641{
2642 dictitem_T *di = NULL;
2643 hashtab_T *ht = NULL;
2644 char namespace;
2645
2646 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2647 return NOTDONE;
2648 switch (isn_type)
2649 {
2650 case ISN_LOADG:
2651 ht = get_globvar_ht();
2652 namespace = 'g';
2653 break;
2654 case ISN_LOADB:
2655 ht = &curbuf->b_vars->dv_hashtab;
2656 namespace = 'b';
2657 break;
2658 case ISN_LOADW:
2659 ht = &curwin->w_vars->dv_hashtab;
2660 namespace = 'w';
2661 break;
2662 case ISN_LOADT:
2663 ht = &curtab->tp_vars->dv_hashtab;
2664 namespace = 't';
2665 break;
2666 default: // Cannot reach here
2667 return NOTDONE;
2668 }
2669 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2670
Bram Moolenaar06b77222022-01-25 15:51:56 +00002671 if (di == NULL)
2672 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002673 if (isn_type == ISN_LOADG)
2674 {
2675 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2676
2677 // g:Something could be a function
2678 if (ufunc != NULL)
2679 {
2680 typval_T *tv = STACK_TV_BOT(0);
2681
2682 ++ectx->ec_stack.ga_len;
2683 tv->v_type = VAR_FUNC;
2684 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2685 if (tv->vval.v_string == NULL)
2686 return FAIL;
2687 STRCPY(tv->vval.v_string, "g:");
2688 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2689 return OK;
2690 }
2691 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002692 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002693 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002694 // no check if the item exists in the script but
2695 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002696 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002697 else
2698 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002699 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002700 return FAIL;
2701 }
2702 else
2703 {
2704 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2705 ++ectx->ec_stack.ga_len;
2706 }
2707 return OK;
2708}
2709
2710/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002711 * Execute instructions in execution context "ectx".
2712 * Return OK or FAIL;
2713 */
2714 static int
2715exec_instructions(ectx_T *ectx)
2716{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002717 int ret = FAIL;
2718 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002719 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002720
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002721 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002722 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002723
Bram Moolenaarff652882021-05-16 15:24:49 +02002724 // Only catch exceptions in this instruction list.
2725 ectx->ec_trylevel_at_start = trylevel;
2726
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 for (;;)
2728 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002729 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002731 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002732
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002733 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002734 {
2735 line_breakcheck();
2736 breakcheck_count = 0;
2737 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002738 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002739 {
2740 // Turn CTRL-C into an exception.
2741 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002742 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002743 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002744 did_throw = TRUE;
2745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002747 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002748 {
2749 // Turn an error message into an exception.
2750 did_emsg = FALSE;
2751 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002752 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002753 did_throw = TRUE;
2754 *msg_list = NULL;
2755 }
2756
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002757 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002759 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002760 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002761 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762
2763 // An exception jumps to the first catch, finally, or returns from
2764 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002765 while (index > 0)
2766 {
2767 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002768 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002769 break;
2770 // In the catch and finally block of this try we have to go up
2771 // one level.
2772 --index;
2773 trycmd = NULL;
2774 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002775 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002777 if (trycmd->tcd_in_catch)
2778 {
2779 // exception inside ":catch", jump to ":finally" once
2780 ectx->ec_iidx = trycmd->tcd_finally_idx;
2781 trycmd->tcd_finally_idx = 0;
2782 }
2783 else
2784 // jump to first ":catch"
2785 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002786 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002787 did_throw = FALSE; // don't come back here until :endtry
2788 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 }
2790 else
2791 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002792 // Not inside try or need to return from current functions.
2793 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002794 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002795 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002796 tv = STACK_TV_BOT(0);
2797 tv->v_type = VAR_NUMBER;
2798 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002799 ++ectx->ec_stack.ga_len;
2800 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002802 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002803 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002804 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002805 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 goto done;
2807 }
2808
Bram Moolenaar4c137212021-04-19 16:48:48 +02002809 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002810 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 }
2812 continue;
2813 }
2814
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002815 /*
2816 * Big switch on the instruction. Most compilers will be turning this
2817 * into an efficient lookup table, since the "case" values are an enum
2818 * with sequential numbers. It may look ugly, but it should be the
2819 * most efficient way.
2820 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002821 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 switch (iptr->isn_type)
2823 {
2824 // execute Ex command line
2825 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002826 if (exec_command(iptr) == FAIL)
2827 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 break;
2829
Bram Moolenaar20677332021-06-06 17:02:53 +02002830 // execute Ex command line split at NL characters.
2831 case ISN_EXEC_SPLIT:
2832 {
2833 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002834 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002835
2836 SOURCING_LNUM = iptr->isn_lnum;
2837 CLEAR_FIELD(cookie);
2838 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2839 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002840 line = get_split_sourceline(0, &cookie, 0, 0);
2841 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002842 get_split_sourceline, &cookie,
2843 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2844 == FAIL
2845 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002846 {
2847 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002848 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002849 }
2850 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002851 }
2852 break;
2853
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002854 // execute Ex command line that is only a range
2855 case ISN_EXECRANGE:
2856 {
2857 exarg_T ea;
2858 char *error = NULL;
2859
2860 CLEAR_FIELD(ea);
2861 ea.cmdidx = CMD_SIZE;
2862 ea.addr_type = ADDR_LINES;
2863 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002864 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002865 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002866 if (ea.cmd == NULL)
2867 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002868 // error is always NULL when using ADDR_LINES
2869 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002870 if (error != NULL)
2871 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002872 emsg(error);
2873 goto on_error;
2874 }
2875 }
2876 break;
2877
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002878 // Evaluate an expression with legacy syntax, push it onto the
2879 // stack.
2880 case ISN_LEGACY_EVAL:
2881 {
2882 char_u *arg = iptr->isn_arg.string;
2883 int res;
2884 int save_flags = cmdmod.cmod_flags;
2885
Bram Moolenaar35578162021-08-02 19:10:38 +02002886 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002887 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002888 tv = STACK_TV_BOT(0);
2889 init_tv(tv);
2890 cmdmod.cmod_flags |= CMOD_LEGACY;
2891 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2892 cmdmod.cmod_flags = save_flags;
2893 if (res == FAIL)
2894 goto on_error;
2895 ++ectx->ec_stack.ga_len;
2896 }
2897 break;
2898
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002899 // push typeval VAR_INSTR with instructions to be executed
2900 case ISN_INSTR:
2901 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002902 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002903 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002904 tv = STACK_TV_BOT(0);
2905 tv->vval.v_instr = ALLOC_ONE(instr_T);
2906 if (tv->vval.v_instr == NULL)
2907 goto on_error;
2908 ++ectx->ec_stack.ga_len;
2909
2910 tv->v_type = VAR_INSTR;
2911 tv->vval.v_instr->instr_ectx = ectx;
2912 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2913 }
2914 break;
2915
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002916 case ISN_SOURCE:
2917 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002918 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002919
Bram Moolenaar89445512022-04-14 12:58:23 +01002920 SOURCING_LNUM = iptr->isn_lnum;
2921 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002922 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002923 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002924 }
2925 break;
2926
Bram Moolenaar4c137212021-04-19 16:48:48 +02002927 // execute :substitute with an expression
2928 case ISN_SUBSTITUTE:
2929 {
2930 subs_T *subs = &iptr->isn_arg.subs;
2931 source_cookie_T cookie;
2932 struct subs_expr_S *save_instr = substitute_instr;
2933 struct subs_expr_S subs_instr;
2934 int res;
2935
2936 subs_instr.subs_ectx = ectx;
2937 subs_instr.subs_instr = subs->subs_instr;
2938 subs_instr.subs_status = OK;
2939 substitute_instr = &subs_instr;
2940
2941 SOURCING_LNUM = iptr->isn_lnum;
2942 // This is very much like ISN_EXEC
2943 CLEAR_FIELD(cookie);
2944 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2945 res = do_cmdline(subs->subs_cmd,
2946 getsourceline, &cookie,
2947 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2948 substitute_instr = save_instr;
2949
2950 if (res == FAIL || did_emsg
2951 || subs_instr.subs_status == FAIL)
2952 goto on_error;
2953 }
2954 break;
2955
2956 case ISN_FINISH:
2957 goto done;
2958
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002959 case ISN_REDIRSTART:
2960 // create a dummy entry for var_redir_str()
2961 if (alloc_redir_lval() == FAIL)
2962 goto on_error;
2963
2964 // The output is stored in growarray "redir_ga" until
2965 // redirection ends.
2966 init_redir_ga();
2967 redir_vname = 1;
2968 break;
2969
2970 case ISN_REDIREND:
2971 {
2972 char_u *res = get_clear_redir_ga();
2973
2974 // End redirection, put redirected text on the stack.
2975 clear_redir_lval();
2976 redir_vname = 0;
2977
Bram Moolenaar35578162021-08-02 19:10:38 +02002978 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002979 {
2980 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002981 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002982 }
2983 tv = STACK_TV_BOT(0);
2984 tv->v_type = VAR_STRING;
2985 tv->vval.v_string = res;
2986 ++ectx->ec_stack.ga_len;
2987 }
2988 break;
2989
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002990 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002991#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002992 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002993 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2994 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002995 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002996#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002997 break;
2998
2999 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003000#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003001 {
3002 exarg_T ea;
3003 int res;
3004
3005 CLEAR_FIELD(ea);
3006 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3007 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3008 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3009 --ectx->ec_stack.ga_len;
3010 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003011 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003012 res = cexpr_core(&ea, tv);
3013 clear_tv(tv);
3014 if (res == FAIL)
3015 goto on_error;
3016 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003017#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003018 break;
3019
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003020 // execute Ex command from pieces on the stack
3021 case ISN_EXECCONCAT:
3022 {
3023 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003024 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003025 int pass;
3026 int i;
3027 char_u *cmd = NULL;
3028 char_u *str;
3029
3030 for (pass = 1; pass <= 2; ++pass)
3031 {
3032 for (i = 0; i < count; ++i)
3033 {
3034 tv = STACK_TV_BOT(i - count);
3035 str = tv->vval.v_string;
3036 if (str != NULL && *str != NUL)
3037 {
3038 if (pass == 2)
3039 STRCPY(cmd + len, str);
3040 len += STRLEN(str);
3041 }
3042 if (pass == 2)
3043 clear_tv(tv);
3044 }
3045 if (pass == 1)
3046 {
3047 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003048 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003049 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003050 len = 0;
3051 }
3052 }
3053
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003054 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003055 do_cmdline_cmd(cmd);
3056 vim_free(cmd);
3057 }
3058 break;
3059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 // execute :echo {string} ...
3061 case ISN_ECHO:
3062 {
3063 int count = iptr->isn_arg.echo.echo_count;
3064 int atstart = TRUE;
3065 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003066 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067
3068 for (idx = 0; idx < count; ++idx)
3069 {
3070 tv = STACK_TV_BOT(idx - count);
3071 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3072 &atstart, &needclr);
3073 clear_tv(tv);
3074 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003075 if (needclr)
3076 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003077 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 }
3079 break;
3080
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003081 // :execute {string} ...
3082 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003083 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003084 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003085 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003086 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003087 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003088 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003089 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003090 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003091 {
3092 int count = iptr->isn_arg.number;
3093 garray_T ga;
3094 char_u buf[NUMBUFLEN];
3095 char_u *p;
3096 int len;
3097 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003098 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003099
3100 ga_init2(&ga, 1, 80);
3101 for (idx = 0; idx < count; ++idx)
3102 {
3103 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003104 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003105 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003106 if (tv->v_type == VAR_CHANNEL
3107 || tv->v_type == VAR_JOB)
3108 {
3109 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003110 semsg(_(e_using_invalid_value_as_string_str),
3111 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003112 break;
3113 }
3114 else
3115 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003116 }
3117 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003118 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003119
3120 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003121 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003122 failed = TRUE;
3123 else
3124 {
3125 if (ga.ga_len > 0)
3126 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3127 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3128 ga.ga_len += len;
3129 }
3130 clear_tv(tv);
3131 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003132 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003133 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003134 {
3135 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003136 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003137 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003138
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003139 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003140 {
3141 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003142 {
3143 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003144 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003145 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003146 {
3147 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003148 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003149 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003150 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003151 else
3152 {
3153 msg_sb_eol();
3154 if (iptr->isn_type == ISN_ECHOMSG)
3155 {
3156 msg_attr(ga.ga_data, echo_attr);
3157 out_flush();
3158 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003159#ifdef HAS_MESSAGE_WINDOW
3160 else if (iptr->isn_type == ISN_ECHOWINDOW)
3161 {
3162 start_echowindow();
3163 msg_attr(ga.ga_data, echo_attr);
3164 end_echowindow();
3165 }
3166#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003167 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3168 {
3169 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3170 TRUE);
3171 ui_write((char_u *)"\r\n", 2, TRUE);
3172 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003173 else
3174 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003175 SOURCING_LNUM = iptr->isn_lnum;
3176 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003177 }
3178 }
3179 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003180 ga_clear(&ga);
3181 }
3182 break;
3183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 // load local variable or argument
3185 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003186 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003187 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003189 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 break;
3191
3192 // load v: variable
3193 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003194 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003195 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003197 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 break;
3199
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003200 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 case ISN_LOADSCRIPT:
3202 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003203 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 svar_T *sv;
3205
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003206 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003207 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003208 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003209 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003210 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003211 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003213 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 }
3215 break;
3216
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003217 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003219 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003221 int sid = iptr->isn_arg.loadstore.ls_sid;
3222 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003223 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 if (di == NULL)
3227 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003228 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003229 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003230 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 }
3232 else
3233 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003234 if (iptr->isn_type == ISN_LOADEXPORT)
3235 {
3236 int idx = get_script_item_idx(sid, name, 0,
3237 NULL, NULL);
3238 svar_T *sv;
3239
3240 if (idx >= 0)
3241 {
3242 sv = ((svar_T *)SCRIPT_ITEM(sid)
3243 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003244 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003245 {
3246 SOURCING_LNUM = iptr->isn_lnum;
3247 semsg(_(e_item_not_exported_in_script_str),
3248 name);
3249 goto on_error;
3250 }
3251 }
3252 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003253 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003254 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003256 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 }
3258 }
3259 break;
3260
Bram Moolenaard3aac292020-04-19 14:32:17 +02003261 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003263 case ISN_LOADB:
3264 case ISN_LOADW:
3265 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003267 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003268
Bram Moolenaar06b77222022-01-25 15:51:56 +00003269 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003270 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003271 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003272 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 break;
3276
Bram Moolenaar03290b82020-12-19 16:30:44 +01003277 // load autoload variable
3278 case ISN_LOADAUTO:
3279 {
3280 char_u *name = iptr->isn_arg.string;
3281
Bram Moolenaar35578162021-08-02 19:10:38 +02003282 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003283 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003284 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003285 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003286 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003287 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003288 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003289 }
3290 break;
3291
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003292 // load g:/b:/w:/t: namespace
3293 case ISN_LOADGDICT:
3294 case ISN_LOADBDICT:
3295 case ISN_LOADWDICT:
3296 case ISN_LOADTDICT:
3297 {
3298 dict_T *d = NULL;
3299
3300 switch (iptr->isn_type)
3301 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003302 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3303 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3304 case ISN_LOADWDICT: d = curwin->w_vars; break;
3305 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003306 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003307 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003308 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003309 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003310 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003311 tv = STACK_TV_BOT(0);
3312 tv->v_type = VAR_DICT;
3313 tv->v_lock = 0;
3314 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003315 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003316 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003317 }
3318 break;
3319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 // load &option
3321 case ISN_LOADOPT:
3322 {
3323 typval_T optval;
3324 char_u *name = iptr->isn_arg.string;
3325
Bram Moolenaara8c17702020-04-01 21:17:24 +02003326 // This is not expected to fail, name is checked during
3327 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003328 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003329 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003330 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003331 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003333 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 }
3335 break;
3336
3337 // load $ENV
3338 case ISN_LOADENV:
3339 {
3340 typval_T optval;
3341 char_u *name = iptr->isn_arg.string;
3342
Bram Moolenaar35578162021-08-02 19:10:38 +02003343 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003344 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003345 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003346 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003348 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 }
3350 break;
3351
3352 // load @register
3353 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003354 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003355 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 tv = STACK_TV_BOT(0);
3357 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003358 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003359 // This may result in NULL, which should be equivalent to an
3360 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 tv->vval.v_string = get_reg_contents(
3362 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003363 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 break;
3365
3366 // store local variable
3367 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003368 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 tv = STACK_TV_VAR(iptr->isn_arg.number);
3370 clear_tv(tv);
3371 *tv = *STACK_TV_BOT(0);
3372 break;
3373
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003374 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003375 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003376 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003377 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003378 int sid = iptr->isn_arg.loadstore.ls_sid;
3379 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003380 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003381 dictitem_T *di = find_var_in_ht(ht, 0,
3382 iptr->isn_type == ISN_STORES
3383 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003384
Bram Moolenaar4c137212021-04-19 16:48:48 +02003385 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003386 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003387 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003388 {
3389 if (iptr->isn_type == ISN_STOREEXPORT)
3390 {
3391 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003392 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003393 goto on_error;
3394 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003395 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003396 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003397 else
3398 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003399 if (iptr->isn_type == ISN_STOREEXPORT)
3400 {
3401 int idx = get_script_item_idx(sid, name, 0,
3402 NULL, NULL);
3403
Bram Moolenaar06651632022-04-27 17:54:25 +01003404 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003405 if (idx >= 0)
3406 {
3407 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3408 ->sn_var_vals.ga_data) + idx;
3409
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003410 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003411 {
3412 semsg(_(e_item_not_exported_in_script_str),
3413 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003414 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003415 goto on_error;
3416 }
3417 }
3418 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003419 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003420 {
3421 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003422 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003423 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003424 clear_tv(&di->di_tv);
3425 di->di_tv = *STACK_TV_BOT(0);
3426 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003427 }
3428 break;
3429
3430 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 case ISN_STORESCRIPT:
3432 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003433 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3434 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003436 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003437 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003438 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003439 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003440
3441 // "const" and "final" are checked at compile time, locking
3442 // the value needs to be checked here.
3443 SOURCING_LNUM = iptr->isn_lnum;
3444 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003445 {
3446 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003447 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003448 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 clear_tv(sv->sv_tv);
3451 *sv->sv_tv = *STACK_TV_BOT(0);
3452 }
3453 break;
3454
3455 // store option
3456 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003457 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003459 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3460 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 long n = 0;
3462 char_u *s = NULL;
3463 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003464 char_u numbuf[NUMBUFLEN];
3465 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466
Bram Moolenaar4c137212021-04-19 16:48:48 +02003467 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 tv = STACK_TV_BOT(0);
3469 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003470 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003472 if (s == NULL)
3473 s = (char_u *)"";
3474 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003475 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3476 {
3477 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003478 // If the option can be set to a function reference or
3479 // a lambda and the passed value is a function
3480 // reference, then convert it to the name (string) of
3481 // the function reference.
3482 s = tv2string(tv, &tofree, numbuf, 0);
3483 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003484 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003485 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003486 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003487 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003488 goto on_error;
3489 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003490 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003492 // must be VAR_NUMBER, CHECKTYPE makes sure
3493 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003494 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003495 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003496 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 if (msg != NULL)
3498 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003499 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003501 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 }
3504 break;
3505
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003506 // store $ENV
3507 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003508 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003509 tv = STACK_TV_BOT(0);
3510 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3511 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003512 break;
3513
3514 // store @r
3515 case ISN_STOREREG:
3516 {
3517 int reg = iptr->isn_arg.number;
3518
Bram Moolenaar4c137212021-04-19 16:48:48 +02003519 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003520 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003521 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003522 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003523 }
3524 break;
3525
3526 // store v: variable
3527 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003528 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003529 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3530 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003531 // should not happen, type is checked when compiling
3532 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003533 break;
3534
Bram Moolenaard3aac292020-04-19 14:32:17 +02003535 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003537 case ISN_STOREB:
3538 case ISN_STOREW:
3539 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003541 dictitem_T *di;
3542 hashtab_T *ht;
3543 char_u *name = iptr->isn_arg.string + 2;
3544
Bram Moolenaard3aac292020-04-19 14:32:17 +02003545 switch (iptr->isn_type)
3546 {
3547 case ISN_STOREG:
3548 ht = get_globvar_ht();
3549 break;
3550 case ISN_STOREB:
3551 ht = &curbuf->b_vars->dv_hashtab;
3552 break;
3553 case ISN_STOREW:
3554 ht = &curwin->w_vars->dv_hashtab;
3555 break;
3556 case ISN_STORET:
3557 ht = &curtab->tp_vars->dv_hashtab;
3558 break;
3559 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003560 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003561 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562
Bram Moolenaar4c137212021-04-19 16:48:48 +02003563 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003564 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003566 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 else
3568 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003569 SOURCING_LNUM = iptr->isn_lnum;
3570 if (var_check_permission(di, name) == FAIL)
3571 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 clear_tv(&di->di_tv);
3573 di->di_tv = *STACK_TV_BOT(0);
3574 }
3575 }
3576 break;
3577
Bram Moolenaar03290b82020-12-19 16:30:44 +01003578 // store an autoload variable
3579 case ISN_STOREAUTO:
3580 SOURCING_LNUM = iptr->isn_lnum;
3581 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3582 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003583 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003584 break;
3585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 // store number in local variable
3587 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003588 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 clear_tv(tv);
3590 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003591 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 break;
3593
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003594 // store value in list or dict variable
3595 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003596 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003597 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003598
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003599 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003600 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003601 if (res == NOTDONE)
3602 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003603 }
3604 break;
3605
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003606 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003607 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003608 if (execute_storerange(iptr, ectx) == FAIL)
3609 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003610 break;
3611
Bram Moolenaar0186e582021-01-10 18:33:11 +01003612 // load or store variable or argument from outer scope
3613 case ISN_LOADOUTER:
3614 case ISN_STOREOUTER:
3615 {
3616 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003617 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3618 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003619
3620 while (depth > 1 && outer != NULL)
3621 {
3622 outer = outer->out_up;
3623 --depth;
3624 }
3625 if (outer == NULL)
3626 {
3627 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003628 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3629 || ectx->ec_outer_ref == NULL)
3630 // Possibly :def function called from legacy
3631 // context.
3632 emsg(_(e_closure_called_from_invalid_context));
3633 else
3634 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003635 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003636 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003637 if (depth == OUTER_LOOP_DEPTH)
3638 // variable declared in loop
3639 tv = ((typval_T *)outer->out_loop_stack->ga_data)
3640 + outer->out_loop_var_idx
3641 + iptr->isn_arg.outer.outer_idx;
3642 else
3643 tv = ((typval_T *)outer->out_stack->ga_data)
3644 + outer->out_frame_idx + STACK_FRAME_SIZE
3645 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003646 if (iptr->isn_type == ISN_LOADOUTER)
3647 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003648 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003649 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003650 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003651 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003652 }
3653 else
3654 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003655 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003656 clear_tv(tv);
3657 *tv = *STACK_TV_BOT(0);
3658 }
3659 }
3660 break;
3661
Bram Moolenaar752fc692021-01-04 21:57:11 +01003662 // unlet item in list or dict variable
3663 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003664 if (execute_unletindex(iptr, ectx) == FAIL)
3665 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003666 break;
3667
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003668 // unlet range of items in list variable
3669 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003670 if (execute_unletrange(iptr, ectx) == FAIL)
3671 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003672 break;
3673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 // push constant
3675 case ISN_PUSHNR:
3676 case ISN_PUSHBOOL:
3677 case ISN_PUSHSPEC:
3678 case ISN_PUSHF:
3679 case ISN_PUSHS:
3680 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003681 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003682 case ISN_PUSHCHANNEL:
3683 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003684 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003685 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003687 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003688 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 switch (iptr->isn_type)
3690 {
3691 case ISN_PUSHNR:
3692 tv->v_type = VAR_NUMBER;
3693 tv->vval.v_number = iptr->isn_arg.number;
3694 break;
3695 case ISN_PUSHBOOL:
3696 tv->v_type = VAR_BOOL;
3697 tv->vval.v_number = iptr->isn_arg.number;
3698 break;
3699 case ISN_PUSHSPEC:
3700 tv->v_type = VAR_SPECIAL;
3701 tv->vval.v_number = iptr->isn_arg.number;
3702 break;
3703#ifdef FEAT_FLOAT
3704 case ISN_PUSHF:
3705 tv->v_type = VAR_FLOAT;
3706 tv->vval.v_float = iptr->isn_arg.fnumber;
3707 break;
3708#endif
3709 case ISN_PUSHBLOB:
3710 blob_copy(iptr->isn_arg.blob, tv);
3711 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003712 case ISN_PUSHFUNC:
3713 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003714 if (iptr->isn_arg.string == NULL)
3715 tv->vval.v_string = NULL;
3716 else
3717 tv->vval.v_string =
3718 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003719 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003720 case ISN_PUSHCHANNEL:
3721#ifdef FEAT_JOB_CHANNEL
3722 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003723 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003724#endif
3725 break;
3726 case ISN_PUSHJOB:
3727#ifdef FEAT_JOB_CHANNEL
3728 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003729 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003730#endif
3731 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 default:
3733 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003734 tv->vval.v_string = iptr->isn_arg.string == NULL
3735 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 }
3737 break;
3738
Bram Moolenaar06b77222022-01-25 15:51:56 +00003739 case ISN_AUTOLOAD:
3740 {
3741 char_u *name = iptr->isn_arg.string;
3742
3743 (void)script_autoload(name, FALSE);
3744 if (find_func(name, TRUE))
3745 {
3746 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3747 goto theend;
3748 tv = STACK_TV_BOT(0);
3749 tv->v_lock = 0;
3750 ++ectx->ec_stack.ga_len;
3751 tv->v_type = VAR_FUNC;
3752 tv->vval.v_string = vim_strsave(name);
3753 }
3754 else
3755 {
3756 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3757
3758 if (res == NOTDONE)
3759 goto theend;
3760 if (res == FAIL)
3761 goto on_error;
3762 }
3763 }
3764 break;
3765
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003766 case ISN_UNLET:
3767 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3768 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003769 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003770 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003771 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003772 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003773 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003774
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003775 case ISN_LOCKUNLOCK:
3776 {
3777 typval_T *lval_root_save = lval_root;
3778 int res;
3779
3780 // Stack has the local variable, argument the whole :lock
3781 // or :unlock command, like ISN_EXEC.
3782 --ectx->ec_stack.ga_len;
3783 lval_root = STACK_TV_BOT(0);
3784 res = exec_command(iptr);
3785 clear_tv(lval_root);
3786 lval_root = lval_root_save;
3787 if (res == FAIL)
3788 goto on_error;
3789 }
3790 break;
3791
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003792 case ISN_LOCKCONST:
3793 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3794 break;
3795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 // create a list from items on the stack; uses a single allocation
3797 // for the list header and the items
3798 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003799 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003800 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 break;
3802
3803 // create a dict from items on the stack
3804 case ISN_NEWDICT:
3805 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003806 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003808 SOURCING_LNUM = iptr->isn_lnum;
3809 res = exe_newdict(iptr->isn_arg.number, ectx);
3810 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003811 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003812 if (res == MAYBE)
3813 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 }
3815 break;
3816
LemonBoy372bcce2022-04-25 12:43:20 +01003817 case ISN_CONCAT:
3818 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3819 goto theend;
3820 break;
3821
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003822 // create a partial with NULL value
3823 case ISN_NEWPARTIAL:
3824 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3825 goto theend;
3826 ++ectx->ec_stack.ga_len;
3827 tv = STACK_TV_BOT(-1);
3828 tv->v_type = VAR_PARTIAL;
3829 tv->v_lock = 0;
3830 tv->vval.v_partial = NULL;
3831 break;
3832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 // call a :def function
3834 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003835 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003836 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3837 NULL,
3838 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003839 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003840 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 break;
3842
3843 // call a builtin function
3844 case ISN_BCALL:
3845 SOURCING_LNUM = iptr->isn_lnum;
3846 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3847 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003848 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003849 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 break;
3851
3852 // call a funcref or partial
3853 case ISN_PCALL:
3854 {
3855 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3856 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003857 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858
3859 SOURCING_LNUM = iptr->isn_lnum;
3860 if (pfunc->cpf_top)
3861 {
3862 // funcref is above the arguments
3863 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3864 }
3865 else
3866 {
3867 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003868 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003869 partial_tv = *STACK_TV_BOT(0);
3870 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003872 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003873 if (tv == &partial_tv)
3874 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003876 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 }
3878 break;
3879
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003880 case ISN_PCALL_END:
3881 // PCALL finished, arguments have been consumed and replaced by
3882 // the return value. Now clear the funcref from the stack,
3883 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003884 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003885 clear_tv(STACK_TV_BOT(-1));
3886 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3887 break;
3888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 // call a user defined function or funcref/partial
3890 case ISN_UCALL:
3891 {
3892 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3893
3894 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003895 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003896 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003897 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 }
3899 break;
3900
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003901 // :defer func(arg)
3902 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01003903 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003904 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
3905 goto on_error;
3906 break;
3907
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003908 // return from a :def function call without a value
3909 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003910 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003911 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003912 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003913 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003914 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003915 tv->vval.v_number = 0;
3916 tv->v_lock = 0;
3917 // FALLTHROUGH
3918
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003919 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 case ISN_RETURN:
3921 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003922 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003923 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003924
3925 if (trystack->ga_len > 0)
3926 trycmd = ((trycmd_T *)trystack->ga_data)
3927 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003928 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003929 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003931 // jump to ":finally" or ":endtry"
3932 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003933 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003934 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003935 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 trycmd->tcd_return = TRUE;
3937 }
3938 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003939 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 }
3941 break;
3942
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003943 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 case ISN_FUNCREF:
3945 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003946 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
3947 ufunc_T *ufunc;
3948 funcref_T *funcref = &iptr->isn_arg.funcref;
3949 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003952 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003953 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003954 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003955 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003956 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003957 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003958 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00003959 {
3960 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3961 + funcref->fr_dfunc_idx;
3962
3963 ufunc = pt_dfunc->df_ufunc;
3964 }
3965 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003966 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003967 if (ufunc == NULL)
3968 {
3969 SOURCING_LNUM = iptr->isn_lnum;
3970 iemsg("ufunc unexpectedly NULL for FUNCREF");
3971 goto theend;
3972 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003973 if (fill_partial_and_closure(pt, ufunc,
3974 extra == NULL ? 0 : extra->fre_loop_var_idx,
3975 extra == NULL ? 0 : extra->fre_loop_var_count,
3976 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003977 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003979 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 tv->vval.v_partial = pt;
3981 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003982 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 }
3984 break;
3985
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003986 // Create a global function from a lambda.
3987 case ISN_NEWFUNC:
3988 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003989 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003990
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003991 if (copy_lambda_to_global_func(arg->nfa_lambda,
3992 arg->nfa_global, arg->nfa_loop_var_idx,
3993 arg->nfa_loop_var_count, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003994 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003995 }
3996 break;
3997
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003998 // List functions
3999 case ISN_DEF:
4000 if (iptr->isn_arg.string == NULL)
4001 list_functions(NULL);
4002 else
4003 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004004 exarg_T ea;
4005 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004006
4007 CLEAR_FIELD(ea);
4008 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004009 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4010 define_function(&ea, NULL, &lines_to_free);
4011 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004012 }
4013 break;
4014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 // jump if a condition is met
4016 case ISN_JUMP:
4017 {
4018 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004019 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 int jump = TRUE;
4021
4022 if (when != JUMP_ALWAYS)
4023 {
4024 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004025 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004026 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004027 || when == JUMP_IF_COND_TRUE)
4028 {
4029 SOURCING_LNUM = iptr->isn_lnum;
4030 jump = tv_get_bool_chk(tv, &error);
4031 if (error)
4032 goto on_error;
4033 }
4034 else
4035 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004036 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004038 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 {
4040 // drop the value from the stack
4041 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004042 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 }
4044 }
4045 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004046 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 }
4048 break;
4049
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004050 // "while": jump to end if a condition is false
4051 case ISN_WHILE:
4052 {
4053 int error = FALSE;
4054 int jump = TRUE;
4055
4056 tv = STACK_TV_BOT(-1);
4057 SOURCING_LNUM = iptr->isn_lnum;
4058 jump = !tv_get_bool_chk(tv, &error);
4059 if (error)
4060 goto on_error;
4061 // drop the value from the stack
4062 clear_tv(tv);
4063 --ectx->ec_stack.ga_len;
4064 if (jump)
4065 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4066
4067 // Store the current funccal count, may be used by
4068 // ISN_LOOPEND later
4069 tv = STACK_TV_VAR(
4070 iptr->isn_arg.whileloop.while_funcref_idx);
4071 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4072 }
4073 break;
4074
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004075 // Jump if an argument with a default value was already set and not
4076 // v:none.
4077 case ISN_JUMP_IF_ARG_SET:
4078 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
4079 if (tv->v_type != VAR_UNKNOWN
4080 && !(tv->v_type == VAR_SPECIAL
4081 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004082 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004083 break;
4084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 // top of a for loop
4086 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004087 if (execute_for(iptr, ectx) == FAIL)
4088 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 break;
4090
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004091 // end of a for or while loop
4092 case ISN_ENDLOOP:
4093 if (execute_endloop(iptr, ectx) == FAIL)
4094 goto theend;
4095 break;
4096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 // start of ":try" block
4098 case ISN_TRY:
4099 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004100 trycmd_T *trycmd = NULL;
4101
Bram Moolenaar35578162021-08-02 19:10:38 +02004102 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004103 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004104 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4105 + ectx->ec_trystack.ga_len;
4106 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004108 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004109 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4110 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004111 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004112 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004113 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004114 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004115 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004116 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 }
4118 break;
4119
4120 case ISN_PUSHEXC:
4121 if (current_exception == NULL)
4122 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004123 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004125 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004127 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004128 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004130 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004132 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 tv->vval.v_string = vim_strsave(
4134 (char_u *)current_exception->value);
4135 break;
4136
4137 case ISN_CATCH:
4138 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004139 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004140 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141
Bram Moolenaar4c137212021-04-19 16:48:48 +02004142 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004143 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004145 trycmd->tcd_caught = TRUE;
4146 trycmd->tcd_did_throw = FALSE;
4147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004149 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 catch_exception(current_exception);
4151 }
4152 break;
4153
Bram Moolenaarc150c092021-02-13 15:02:46 +01004154 case ISN_TRYCONT:
4155 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004156 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004157 trycont_T *trycont = &iptr->isn_arg.trycont;
4158 int i;
4159 trycmd_T *trycmd;
4160 int iidx = trycont->tct_where;
4161
4162 if (trystack->ga_len < trycont->tct_levels)
4163 {
4164 siemsg("TRYCONT: expected %d levels, found %d",
4165 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004166 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004167 }
4168 // Make :endtry jump to any outer try block and the last
4169 // :endtry inside the loop to the loop start.
4170 for (i = trycont->tct_levels; i > 0; --i)
4171 {
4172 trycmd = ((trycmd_T *)trystack->ga_data)
4173 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004174 // Add one to tcd_cont to be able to jump to
4175 // instruction with index zero.
4176 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004177 iidx = trycmd->tcd_finally_idx == 0
4178 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004179 }
4180 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004181 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004182 }
4183 break;
4184
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004185 case ISN_FINALLY:
4186 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004187 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004188 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4189 + trystack->ga_len - 1;
4190
4191 // Reset the index to avoid a return statement jumps here
4192 // again.
4193 trycmd->tcd_finally_idx = 0;
4194 break;
4195 }
4196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 // end of ":try" block
4198 case ISN_ENDTRY:
4199 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004200 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004201 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202
Bram Moolenaar06651632022-04-27 17:54:25 +01004203 --trystack->ga_len;
4204 --trylevel;
4205 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4206 if (trycmd->tcd_did_throw)
4207 did_throw = TRUE;
4208 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004210 // discard the exception
4211 if (caught_stack == current_exception)
4212 caught_stack = caught_stack->caught;
4213 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004215
4216 if (trycmd->tcd_return)
4217 goto func_return;
4218
4219 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4220 {
4221 --ectx->ec_stack.ga_len;
4222 clear_tv(STACK_TV_BOT(0));
4223 }
4224 if (trycmd->tcd_cont != 0)
4225 // handling :continue: jump to outer try block or
4226 // start of the loop
4227 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 }
4229 break;
4230
4231 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004232 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004233 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004234
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004235 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4236 {
4237 // throwing an exception while using "silent!" causes
4238 // the function to abort but not display an error.
4239 tv = STACK_TV_BOT(-1);
4240 clear_tv(tv);
4241 tv->v_type = VAR_NUMBER;
4242 tv->vval.v_number = 0;
4243 goto done;
4244 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004245 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004246 tv = STACK_TV_BOT(0);
4247 if (tv->vval.v_string == NULL
4248 || *skipwhite(tv->vval.v_string) == NUL)
4249 {
4250 vim_free(tv->vval.v_string);
4251 SOURCING_LNUM = iptr->isn_lnum;
4252 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004253 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004254 }
4255
4256 // Inside a "catch" we need to first discard the caught
4257 // exception.
4258 if (trystack->ga_len > 0)
4259 {
4260 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4261 + trystack->ga_len - 1;
4262 if (trycmd->tcd_caught && current_exception != NULL)
4263 {
4264 // discard the exception
4265 if (caught_stack == current_exception)
4266 caught_stack = caught_stack->caught;
4267 discard_current_exception();
4268 trycmd->tcd_caught = FALSE;
4269 }
4270 }
4271
Bram Moolenaar90a57162022-02-12 14:23:17 +00004272 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004273 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4274 == FAIL)
4275 {
4276 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004277 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004278 }
4279 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 break;
4282
4283 // compare with special values
4284 case ISN_COMPAREBOOL:
4285 case ISN_COMPARESPECIAL:
4286 {
4287 typval_T *tv1 = STACK_TV_BOT(-2);
4288 typval_T *tv2 = STACK_TV_BOT(-1);
4289 varnumber_T arg1 = tv1->vval.v_number;
4290 varnumber_T arg2 = tv2->vval.v_number;
4291 int res;
4292
Bram Moolenaar06651632022-04-27 17:54:25 +01004293 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4294 res = arg1 == arg2;
4295 else
4296 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297
Bram Moolenaar4c137212021-04-19 16:48:48 +02004298 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 tv1->v_type = VAR_BOOL;
4300 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4301 }
4302 break;
4303
Bram Moolenaar7a222242022-03-01 19:23:24 +00004304 case ISN_COMPARENULL:
4305 {
4306 typval_T *tv1 = STACK_TV_BOT(-2);
4307 typval_T *tv2 = STACK_TV_BOT(-1);
4308 int res;
4309
4310 res = typval_compare_null(tv1, tv2);
4311 if (res == MAYBE)
4312 goto on_error;
4313 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4314 res = !res;
4315 clear_tv(tv1);
4316 clear_tv(tv2);
4317 --ectx->ec_stack.ga_len;
4318 tv1->v_type = VAR_BOOL;
4319 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4320 }
4321 break;
4322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 // Operation with two number arguments
4324 case ISN_OPNR:
4325 case ISN_COMPARENR:
4326 {
4327 typval_T *tv1 = STACK_TV_BOT(-2);
4328 typval_T *tv2 = STACK_TV_BOT(-1);
4329 varnumber_T arg1 = tv1->vval.v_number;
4330 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004331 varnumber_T res = 0;
4332 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004334 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4335 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4336 {
4337 if (arg2 < 0)
4338 {
4339 SOURCING_LNUM = iptr->isn_lnum;
4340 emsg(_(e_bitshift_ops_must_be_postive));
4341 goto on_error;
4342 }
4343 }
4344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 switch (iptr->isn_arg.op.op_type)
4346 {
4347 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004348 case EXPR_DIV: if (arg2 == 0)
4349 div_zero = TRUE;
4350 else
4351 res = arg1 / arg2;
4352 break;
4353 case EXPR_REM: if (arg2 == 0)
4354 div_zero = TRUE;
4355 else
4356 res = arg1 % arg2;
4357 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 case EXPR_SUB: res = arg1 - arg2; break;
4359 case EXPR_ADD: res = arg1 + arg2; break;
4360
4361 case EXPR_EQUAL: res = arg1 == arg2; break;
4362 case EXPR_NEQUAL: res = arg1 != arg2; break;
4363 case EXPR_GREATER: res = arg1 > arg2; break;
4364 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4365 case EXPR_SMALLER: res = arg1 < arg2; break;
4366 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004367 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4368 res = 0;
4369 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004370 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004371 break;
4372 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4373 res = 0;
4374 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004375 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004376 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004377 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 }
4379
Bram Moolenaar4c137212021-04-19 16:48:48 +02004380 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 if (iptr->isn_type == ISN_COMPARENR)
4382 {
4383 tv1->v_type = VAR_BOOL;
4384 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4385 }
4386 else
4387 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004388 if (div_zero)
4389 {
4390 SOURCING_LNUM = iptr->isn_lnum;
4391 emsg(_(e_divide_by_zero));
4392 goto on_error;
4393 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 }
4395 break;
4396
4397 // Computation with two float arguments
4398 case ISN_OPFLOAT:
4399 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004400#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 {
4402 typval_T *tv1 = STACK_TV_BOT(-2);
4403 typval_T *tv2 = STACK_TV_BOT(-1);
4404 float_T arg1 = tv1->vval.v_float;
4405 float_T arg2 = tv2->vval.v_float;
4406 float_T res = 0;
4407 int cmp = FALSE;
4408
4409 switch (iptr->isn_arg.op.op_type)
4410 {
4411 case EXPR_MULT: res = arg1 * arg2; break;
4412 case EXPR_DIV: res = arg1 / arg2; break;
4413 case EXPR_SUB: res = arg1 - arg2; break;
4414 case EXPR_ADD: res = arg1 + arg2; break;
4415
4416 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4417 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4418 case EXPR_GREATER: cmp = arg1 > arg2; break;
4419 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4420 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4421 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4422 default: cmp = 0; break;
4423 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004424 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 if (iptr->isn_type == ISN_COMPAREFLOAT)
4426 {
4427 tv1->v_type = VAR_BOOL;
4428 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4429 }
4430 else
4431 tv1->vval.v_float = res;
4432 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004433#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 break;
4435
4436 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004437 case ISN_COMPAREDICT:
4438 case ISN_COMPAREFUNC:
4439 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 case ISN_COMPAREBLOB:
4441 {
4442 typval_T *tv1 = STACK_TV_BOT(-2);
4443 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004444 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4445 int ic = iptr->isn_arg.op.op_ic;
4446 int res = FALSE;
4447 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448
Bram Moolenaar265f8112021-12-19 12:33:05 +00004449 SOURCING_LNUM = iptr->isn_lnum;
4450 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004452 status = typval_compare_list(tv1, tv2,
4453 exprtype, ic, &res);
4454 }
4455 else if (iptr->isn_type == ISN_COMPAREDICT)
4456 {
4457 status = typval_compare_dict(tv1, tv2,
4458 exprtype, ic, &res);
4459 }
4460 else if (iptr->isn_type == ISN_COMPAREFUNC)
4461 {
4462 status = typval_compare_func(tv1, tv2,
4463 exprtype, ic, &res);
4464 }
4465 else if (iptr->isn_type == ISN_COMPARESTRING)
4466 {
4467 status = typval_compare_string(tv1, tv2,
4468 exprtype, ic, &res);
4469 }
4470 else
4471 {
4472 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004474 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 clear_tv(tv1);
4476 clear_tv(tv2);
4477 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004478 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4479 if (status == FAIL)
4480 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 }
4482 break;
4483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 case ISN_COMPAREANY:
4485 {
4486 typval_T *tv1 = STACK_TV_BOT(-2);
4487 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004488 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004490 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491
Bram Moolenaareb26f432020-09-14 16:50:05 +02004492 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004493 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004495 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004496 if (status == FAIL)
4497 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 }
4499 break;
4500
4501 case ISN_ADDLIST:
4502 case ISN_ADDBLOB:
4503 {
4504 typval_T *tv1 = STACK_TV_BOT(-2);
4505 typval_T *tv2 = STACK_TV_BOT(-1);
4506
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004507 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004509 {
4510 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4511 && tv1->vval.v_list != NULL)
4512 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4513 NULL);
4514 else
4515 eval_addlist(tv1, tv2);
4516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 else
4518 eval_addblob(tv1, tv2);
4519 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004520 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 }
4522 break;
4523
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004524 case ISN_LISTAPPEND:
4525 {
4526 typval_T *tv1 = STACK_TV_BOT(-2);
4527 typval_T *tv2 = STACK_TV_BOT(-1);
4528 list_T *l = tv1->vval.v_list;
4529
4530 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004531 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004532 if (l == NULL)
4533 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004534 emsg(_(e_cannot_add_to_null_list));
4535 goto on_error;
4536 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004537 if (value_check_lock(l->lv_lock, NULL, FALSE))
4538 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004539 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004540 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004541 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004542 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004543 }
4544 break;
4545
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004546 case ISN_BLOBAPPEND:
4547 {
4548 typval_T *tv1 = STACK_TV_BOT(-2);
4549 typval_T *tv2 = STACK_TV_BOT(-1);
4550 blob_T *b = tv1->vval.v_blob;
4551 int error = FALSE;
4552 varnumber_T n;
4553
4554 // add a number to a blob
4555 if (b == NULL)
4556 {
4557 SOURCING_LNUM = iptr->isn_lnum;
4558 emsg(_(e_cannot_add_to_null_blob));
4559 goto on_error;
4560 }
4561 n = tv_get_number_chk(tv2, &error);
4562 if (error)
4563 goto on_error;
4564 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004565 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004566 }
4567 break;
4568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 // Computation with two arguments of unknown type
4570 case ISN_OPANY:
4571 {
4572 typval_T *tv1 = STACK_TV_BOT(-2);
4573 typval_T *tv2 = STACK_TV_BOT(-1);
4574 varnumber_T n1, n2;
4575#ifdef FEAT_FLOAT
4576 float_T f1 = 0, f2 = 0;
4577#endif
4578 int error = FALSE;
4579
4580 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4581 {
4582 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4583 {
4584 eval_addlist(tv1, tv2);
4585 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004586 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 break;
4588 }
4589 else if (tv1->v_type == VAR_BLOB
4590 && tv2->v_type == VAR_BLOB)
4591 {
4592 eval_addblob(tv1, tv2);
4593 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004594 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 break;
4596 }
4597 }
4598#ifdef FEAT_FLOAT
4599 if (tv1->v_type == VAR_FLOAT)
4600 {
4601 f1 = tv1->vval.v_float;
4602 n1 = 0;
4603 }
4604 else
4605#endif
4606 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004607 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 n1 = tv_get_number_chk(tv1, &error);
4609 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004610 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611#ifdef FEAT_FLOAT
4612 if (tv2->v_type == VAR_FLOAT)
4613 f1 = n1;
4614#endif
4615 }
4616#ifdef FEAT_FLOAT
4617 if (tv2->v_type == VAR_FLOAT)
4618 {
4619 f2 = tv2->vval.v_float;
4620 n2 = 0;
4621 }
4622 else
4623#endif
4624 {
4625 n2 = tv_get_number_chk(tv2, &error);
4626 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004627 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628#ifdef FEAT_FLOAT
4629 if (tv1->v_type == VAR_FLOAT)
4630 f2 = n2;
4631#endif
4632 }
4633#ifdef FEAT_FLOAT
4634 // if there is a float on either side the result is a float
4635 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4636 {
4637 switch (iptr->isn_arg.op.op_type)
4638 {
4639 case EXPR_MULT: f1 = f1 * f2; break;
4640 case EXPR_DIV: f1 = f1 / f2; break;
4641 case EXPR_SUB: f1 = f1 - f2; break;
4642 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004643 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004644 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004645 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 }
4647 clear_tv(tv1);
4648 clear_tv(tv2);
4649 tv1->v_type = VAR_FLOAT;
4650 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004651 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 }
4653 else
4654#endif
4655 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004656 int failed = FALSE;
4657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 switch (iptr->isn_arg.op.op_type)
4659 {
4660 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004661 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4662 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004663 goto on_error;
4664 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 case EXPR_SUB: n1 = n1 - n2; break;
4666 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004667 default: n1 = num_modulus(n1, n2, &failed);
4668 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004669 goto on_error;
4670 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 }
4672 clear_tv(tv1);
4673 clear_tv(tv2);
4674 tv1->v_type = VAR_NUMBER;
4675 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004676 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 }
4678 }
4679 break;
4680
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004681 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004682 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004683 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004684 int is_slice = iptr->isn_type == ISN_STRSLICE;
4685 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004686 char_u *res;
4687
4688 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004689 // string slice: string is at stack-3, first index at
4690 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004691 if (is_slice)
4692 {
4693 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004694 n1 = tv->vval.v_number;
4695 }
4696
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004697 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004698 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004699
Bram Moolenaar4c137212021-04-19 16:48:48 +02004700 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004701 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004702 if (is_slice)
4703 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004704 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004705 else
4706 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004707 // single character (including composing characters).
4708 // If the index is too big or negative the result is
4709 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004710 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004711 vim_free(tv->vval.v_string);
4712 tv->vval.v_string = res;
4713 }
4714 break;
4715
4716 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004717 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004718 case ISN_BLOBINDEX:
4719 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004721 int is_slice = iptr->isn_type == ISN_LISTSLICE
4722 || iptr->isn_type == ISN_BLOBSLICE;
4723 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4724 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004725 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004726 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727
4728 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004729 // list slice: list is at stack-3, indexes at stack-2 and
4730 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004731 // Same for blob.
4732 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733
4734 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004735 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004737
4738 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 {
Bram Moolenaared591872020-08-15 22:14:53 +02004740 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004741 n1 = tv->vval.v_number;
4742 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 }
Bram Moolenaared591872020-08-15 22:14:53 +02004744
Bram Moolenaar4c137212021-04-19 16:48:48 +02004745 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004746 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004747 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004748 if (is_blob)
4749 {
4750 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4751 n1, n2, FALSE, tv) == FAIL)
4752 goto on_error;
4753 }
4754 else
4755 {
4756 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4757 n1, n2, FALSE, tv, TRUE) == FAIL)
4758 goto on_error;
4759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 }
4761 break;
4762
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004763 case ISN_ANYINDEX:
4764 case ISN_ANYSLICE:
4765 {
4766 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4767 typval_T *var1, *var2;
4768 int res;
4769
4770 // index: composite is at stack-2, index at stack-1
4771 // slice: composite is at stack-3, indexes at stack-2 and
4772 // stack-1
4773 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004774 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004775 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4776 goto on_error;
4777 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4778 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004779 res = eval_index_inner(tv, is_slice, var1, var2,
4780 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004781 clear_tv(var1);
4782 if (is_slice)
4783 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004784 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004785 if (res == FAIL)
4786 goto on_error;
4787 }
4788 break;
4789
Bram Moolenaar9af78762020-06-16 11:34:42 +02004790 case ISN_SLICE:
4791 {
4792 list_T *list;
4793 int count = iptr->isn_arg.number;
4794
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004795 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004796 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004797 list = tv->vval.v_list;
4798
4799 // no error for short list, expect it to be checked earlier
4800 if (list != NULL && list->lv_len >= count)
4801 {
4802 list_T *newlist = list_slice(list,
4803 count, list->lv_len - 1);
4804
4805 if (newlist != NULL)
4806 {
4807 list_unref(list);
4808 tv->vval.v_list = newlist;
4809 ++newlist->lv_refcount;
4810 }
4811 }
4812 }
4813 break;
4814
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004815 case ISN_GETITEM:
4816 {
4817 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004818 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004819
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004820 // Get list item: list is at stack-1, push item.
4821 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004822 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4823 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004824
Bram Moolenaar35578162021-08-02 19:10:38 +02004825 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004826 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004827 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004828 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004829
4830 // Useful when used in unpack assignment. Reset at
4831 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004832 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004833 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004834 }
4835 break;
4836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 case ISN_MEMBER:
4838 {
4839 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004840 char_u *key;
4841 dictitem_T *di;
4842
4843 // dict member: dict is at stack-2, key at stack-1
4844 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004845 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004846 dict = tv->vval.v_dict;
4847
4848 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004849 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004850 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004851 if (key == NULL)
4852 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004853
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004854 if ((di = dict_find(dict, key, -1)) == NULL)
4855 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004856 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004857 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004858
4859 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004860 // stack contents makes sense and the dict stack is
4861 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004862 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004863 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004864 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004865 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004866 tv->v_type = VAR_NUMBER;
4867 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004868 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004869 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004870 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004871 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004872 // Put the dict used on the dict stack, it might be used by
4873 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004874 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004875 if (dict_stack_save(tv) == FAIL)
4876 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004877 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004878 }
4879 break;
4880
4881 // dict member with string key
4882 case ISN_STRINGMEMBER:
4883 {
4884 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 dictitem_T *di;
4886
4887 tv = STACK_TV_BOT(-1);
4888 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4889 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004890 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004891 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004892 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 }
4894 dict = tv->vval.v_dict;
4895
4896 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4897 == NULL)
4898 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004899 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004900 semsg(_(e_key_not_present_in_dictionary),
4901 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004902 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004904 // Put the dict used on the dict stack, it might be used by
4905 // a dict function later.
4906 if (dict_stack_save(tv) == FAIL)
4907 goto on_fatal_error;
4908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004910 }
4911 break;
4912
4913 case ISN_CLEARDICT:
4914 dict_stack_drop();
4915 break;
4916
4917 case ISN_USEDICT:
4918 {
4919 typval_T *dict_tv = dict_stack_get_tv();
4920
4921 // Turn "dict.Func" into a partial for "Func" bound to
4922 // "dict". Don't do this when "Func" is already a partial
4923 // that was bound explicitly (pt_auto is FALSE).
4924 tv = STACK_TV_BOT(-1);
4925 if (dict_tv != NULL
4926 && dict_tv->v_type == VAR_DICT
4927 && dict_tv->vval.v_dict != NULL
4928 && (tv->v_type == VAR_FUNC
4929 || (tv->v_type == VAR_PARTIAL
4930 && (tv->vval.v_partial->pt_auto
4931 || tv->vval.v_partial->pt_dict == NULL))))
4932 dict_tv->vval.v_dict =
4933 make_partial(dict_tv->vval.v_dict, tv);
4934 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 }
4936 break;
4937
4938 case ISN_NEGATENR:
4939 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004940 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004941#ifdef FEAT_FLOAT
4942 if (tv->v_type == VAR_FLOAT)
4943 tv->vval.v_float = -tv->vval.v_float;
4944 else
4945#endif
4946 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 break;
4948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 case ISN_CHECKTYPE:
4950 {
4951 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004952 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004953 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004955 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004956 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004957 if (!ectx->ec_where.wt_variable)
4958 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004959 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004960 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004961 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004962 if (r == FAIL)
4963 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004964 if (!ectx->ec_where.wt_variable)
4965 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004966
4967 // number 0 is FALSE, number 1 is TRUE
4968 if (tv->v_type == VAR_NUMBER
4969 && ct->ct_type->tt_type == VAR_BOOL
4970 && (tv->vval.v_number == 0
4971 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004973 tv->v_type = VAR_BOOL;
4974 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004975 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 }
4977 }
4978 break;
4979
Bram Moolenaar9af78762020-06-16 11:34:42 +02004980 case ISN_CHECKLEN:
4981 {
4982 int min_len = iptr->isn_arg.checklen.cl_min_len;
4983 list_T *list = NULL;
4984
4985 tv = STACK_TV_BOT(-1);
4986 if (tv->v_type == VAR_LIST)
4987 list = tv->vval.v_list;
4988 if (list == NULL || list->lv_len < min_len
4989 || (list->lv_len > min_len
4990 && !iptr->isn_arg.checklen.cl_more_OK))
4991 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004992 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004993 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004994 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004995 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004996 }
4997 }
4998 break;
4999
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005000 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005001 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005002 break;
5003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005005 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006 {
5007 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005008 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005010 if (iptr->isn_type == ISN_2BOOL)
5011 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005012 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005013 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005014 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005015 n = !n;
5016 }
5017 else
5018 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005019 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005020 SOURCING_LNUM = iptr->isn_lnum;
5021 n = tv_get_bool_chk(tv, &error);
5022 if (error)
5023 goto on_error;
5024 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 clear_tv(tv);
5026 tv->v_type = VAR_BOOL;
5027 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5028 }
5029 break;
5030
5031 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005032 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005033 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005034 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5035 iptr->isn_type == ISN_2STRING_ANY,
5036 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005037 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 break;
5039
Bram Moolenaar08597872020-12-10 19:43:40 +01005040 case ISN_RANGE:
5041 {
5042 exarg_T ea;
5043 char *errormsg;
5044
Bram Moolenaarece0b872021-01-08 20:40:45 +01005045 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005046 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005047 ea.addr_type = ADDR_LINES;
5048 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005049 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005050 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005051 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005052
Bram Moolenaar35578162021-08-02 19:10:38 +02005053 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005054 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005055 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005056 tv = STACK_TV_BOT(-1);
5057 tv->v_type = VAR_NUMBER;
5058 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005059 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005060 }
5061 break;
5062
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005063 case ISN_PUT:
5064 {
5065 int regname = iptr->isn_arg.put.put_regname;
5066 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5067 char_u *expr = NULL;
5068 int dir = FORWARD;
5069
Bram Moolenaar08597872020-12-10 19:43:40 +01005070 if (lnum < -2)
5071 {
5072 // line number was put on the stack by ISN_RANGE
5073 tv = STACK_TV_BOT(-1);
5074 curwin->w_cursor.lnum = tv->vval.v_number;
5075 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5076 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005077 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005078 }
5079 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005080 // :put! above cursor
5081 dir = BACKWARD;
5082 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005083 {
5084 curwin->w_cursor.lnum = lnum;
5085 if (lnum == 0)
5086 // check_cursor() below will move to line 1
5087 dir = BACKWARD;
5088 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005089
5090 if (regname == '=')
5091 {
5092 tv = STACK_TV_BOT(-1);
5093 if (tv->v_type == VAR_STRING)
5094 expr = tv->vval.v_string;
5095 else
5096 {
5097 expr = typval2string(tv, TRUE); // allocates value
5098 clear_tv(tv);
5099 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005100 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005101 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005102 check_cursor();
5103 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5104 vim_free(expr);
5105 }
5106 break;
5107
Bram Moolenaar02194d22020-10-24 23:08:38 +02005108 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005109 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5110 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5111 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5112 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005113 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5114 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005115 break;
5116
Bram Moolenaar02194d22020-10-24 23:08:38 +02005117 case ISN_CMDMOD_REV:
5118 // filter regprog is owned by the instruction, don't free it
5119 cmdmod.cmod_filter_regmatch.regprog = NULL;
5120 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005121 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5122 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005123 break;
5124
Bram Moolenaar792f7862020-11-23 08:31:18 +01005125 case ISN_UNPACK:
5126 {
5127 int count = iptr->isn_arg.unpack.unp_count;
5128 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5129 list_T *l;
5130 listitem_T *li;
5131 int i;
5132
5133 // Check there is a valid list to unpack.
5134 tv = STACK_TV_BOT(-1);
5135 if (tv->v_type != VAR_LIST)
5136 {
5137 SOURCING_LNUM = iptr->isn_lnum;
5138 emsg(_(e_for_argument_must_be_sequence_of_lists));
5139 goto on_error;
5140 }
5141 l = tv->vval.v_list;
5142 if (l == NULL
5143 || l->lv_len < (semicolon ? count - 1 : count))
5144 {
5145 SOURCING_LNUM = iptr->isn_lnum;
5146 emsg(_(e_list_value_does_not_have_enough_items));
5147 goto on_error;
5148 }
5149 else if (!semicolon && l->lv_len > count)
5150 {
5151 SOURCING_LNUM = iptr->isn_lnum;
5152 emsg(_(e_list_value_has_more_items_than_targets));
5153 goto on_error;
5154 }
5155
5156 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005157 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005158 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005159 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005160
5161 // Variable after semicolon gets a list with the remaining
5162 // items.
5163 if (semicolon)
5164 {
5165 list_T *rem_list =
5166 list_alloc_with_items(l->lv_len - count + 1);
5167
5168 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005169 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005170 tv = STACK_TV_BOT(-count);
5171 tv->vval.v_list = rem_list;
5172 ++rem_list->lv_refcount;
5173 tv->v_lock = 0;
5174 li = l->lv_first;
5175 for (i = 0; i < count - 1; ++i)
5176 li = li->li_next;
5177 for (i = 0; li != NULL; ++i)
5178 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005179 typval_T tvcopy;
5180
5181 copy_tv(&li->li_tv, &tvcopy);
5182 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005183 li = li->li_next;
5184 }
5185 --count;
5186 }
5187
5188 // Produce the values in reverse order, first item last.
5189 li = l->lv_first;
5190 for (i = 0; i < count; ++i)
5191 {
5192 tv = STACK_TV_BOT(-i - 1);
5193 copy_tv(&li->li_tv, tv);
5194 li = li->li_next;
5195 }
5196
5197 list_unref(l);
5198 }
5199 break;
5200
Bram Moolenaarb2049902021-01-24 12:53:53 +01005201 case ISN_PROF_START:
5202 case ISN_PROF_END:
5203 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005204#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005205 funccall_T cookie;
5206 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005207 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005208 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005209
Bram Moolenaarca16c602022-09-06 18:57:08 +01005210 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005211 if (iptr->isn_type == ISN_PROF_START)
5212 {
5213 func_line_start(&cookie, iptr->isn_lnum);
5214 // if we get here the instruction is executed
5215 func_line_exec(&cookie);
5216 }
5217 else
5218 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005219#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005220 }
5221 break;
5222
Bram Moolenaare99d4222021-06-13 14:01:26 +02005223 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005224 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005225 break;
5226
Bram Moolenaar389df252020-07-09 21:20:47 +02005227 case ISN_SHUFFLE:
5228 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005229 typval_T tmp_tv;
5230 int item = iptr->isn_arg.shuffle.shfl_item;
5231 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005232
5233 tmp_tv = *STACK_TV_BOT(-item);
5234 for ( ; up > 0 && item > 1; --up)
5235 {
5236 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5237 --item;
5238 }
5239 *STACK_TV_BOT(-item) = tmp_tv;
5240 }
5241 break;
5242
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005244 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005246 ectx->ec_where.wt_index = 0;
5247 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248 break;
5249 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005250 continue;
5251
Bram Moolenaard032f342020-07-18 18:13:02 +02005252func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005253 // Restore previous function. If the frame pointer is where we started
5254 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005255 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005256 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005257
Bram Moolenaar4c137212021-04-19 16:48:48 +02005258 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005259 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005260 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005261 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005262
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005263on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005264 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005265 // If "emsg_silent" is set then ignore the error, unless it was set
5266 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005267 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005268 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005269 {
5270 // If a sequence of instructions causes an error while ":silent!"
5271 // was used, restore the stack length and jump ahead to restoring
5272 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005273 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005274 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005275 while (ectx->ec_stack.ga_len
5276 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005277 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005278 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005279 clear_tv(STACK_TV_BOT(0));
5280 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005281 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5282 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005283 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005284 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005285 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005286on_fatal_error:
5287 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005288 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005289 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005290 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291 }
5292
5293done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005294 ret = OK;
5295theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005296 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005297
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005298 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005299 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5300 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005301}
5302
5303/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005304 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005305 * "rettv".
5306 * Return OK or FAIL.
5307 */
5308 int
5309exe_typval_instr(typval_T *tv, typval_T *rettv)
5310{
5311 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5312 isn_T *save_instr = ectx->ec_instr;
5313 int save_iidx = ectx->ec_iidx;
5314 int res;
5315
LemonBoyf3b48952022-05-05 13:53:03 +01005316 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5317 // even when the compilation fails.
5318 rettv->v_type = VAR_UNKNOWN;
5319
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005320 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5321 res = exec_instructions(ectx);
5322 if (res == OK)
5323 {
5324 *rettv = *STACK_TV_BOT(-1);
5325 --ectx->ec_stack.ga_len;
5326 }
5327
5328 ectx->ec_instr = save_instr;
5329 ectx->ec_iidx = save_iidx;
5330
5331 return res;
5332}
5333
5334/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005335 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5336 * "substitute_instr".
5337 */
5338 char_u *
5339exe_substitute_instr(void)
5340{
5341 ectx_T *ectx = substitute_instr->subs_ectx;
5342 isn_T *save_instr = ectx->ec_instr;
5343 int save_iidx = ectx->ec_iidx;
5344 char_u *res;
5345
5346 ectx->ec_instr = substitute_instr->subs_instr;
5347 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005348 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005349 typval_T *tv = STACK_TV_BOT(-1);
5350
Bram Moolenaar27523602021-06-05 21:36:19 +02005351 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005352 --ectx->ec_stack.ga_len;
5353 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005354 }
5355 else
5356 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005357 substitute_instr->subs_status = FAIL;
5358 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360
Bram Moolenaar4c137212021-04-19 16:48:48 +02005361 ectx->ec_instr = save_instr;
5362 ectx->ec_iidx = save_iidx;
5363
5364 return res;
5365}
5366
5367/*
5368 * Call a "def" function from old Vim script.
5369 * Return OK or FAIL.
5370 */
5371 int
5372call_def_function(
5373 ufunc_T *ufunc,
5374 int argc_arg, // nr of arguments
5375 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005376 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005377 partial_T *partial, // optional partial for context
Bram Moolenaar58779852022-09-06 18:31:14 +01005378 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005379 typval_T *rettv) // return value
5380{
5381 ectx_T ectx; // execution context
5382 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005383 int partial_argc = partial == NULL
5384 || (flags & DEF_USE_PT_ARGV) == 0
5385 ? 0 : partial->pt_argc;
5386 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005387 typval_T *tv;
5388 int idx;
5389 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005390 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005391 sctx_T save_current_sctx = current_sctx;
5392 int did_emsg_before = did_emsg_cumul + did_emsg;
5393 int save_suppress_errthrow = suppress_errthrow;
5394 msglist_T **saved_msg_list = NULL;
5395 msglist_T *private_msg_list = NULL;
5396 int save_emsg_silent_def = emsg_silent_def;
5397 int save_did_emsg_def = did_emsg_def;
5398 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005399 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005400
5401// Get pointer to item in the stack.
5402#undef STACK_TV
5403#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5404
5405// Get pointer to item at the bottom of the stack, -1 is the bottom.
5406#undef STACK_TV_BOT
5407#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5408
5409// Get pointer to a local variable on the stack. Negative for arguments.
5410#undef STACK_TV_VAR
5411#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5412
5413 if (ufunc->uf_def_status == UF_NOT_COMPILED
5414 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005415 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5416 && compile_def_function(ufunc, FALSE,
5417 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005418 {
5419 if (did_emsg_cumul + did_emsg == did_emsg_before)
5420 semsg(_(e_function_is_not_compiled_str),
5421 printable_func_name(ufunc));
5422 return FAIL;
5423 }
5424
5425 {
5426 // Check the function was really compiled.
5427 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5428 + ufunc->uf_dfunc_idx;
5429 if (INSTRUCTIONS(dfunc) == NULL)
5430 {
5431 iemsg("using call_def_function() on not compiled function");
5432 return FAIL;
5433 }
5434 }
5435
5436 // If depth of calling is getting too high, don't execute the function.
5437 orig_funcdepth = funcdepth_get();
5438 if (funcdepth_increment() == FAIL)
5439 return FAIL;
5440
5441 CLEAR_FIELD(ectx);
5442 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5443 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005444 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005445 {
5446 funcdepth_decrement();
5447 return FAIL;
5448 }
5449 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5450 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5451 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005452 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005453
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005454 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005455 if (idx > 0 && ufunc->uf_va_name == NULL)
5456 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005457 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005458 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005459 goto failed_early;
5460 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005461 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005462 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005463 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005464 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005465 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005466 goto failed_early;
5467 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005468
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005469 // Put values from the partial and arguments on the stack, but no more than
5470 // what the function expects. A lambda can be called with more arguments
5471 // than it uses.
5472 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005473 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5474 ++idx)
5475 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005476 int argv_idx = idx - partial_argc;
5477
5478 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005479 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005480 && tv->v_type == VAR_SPECIAL
5481 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005482 {
5483 // Use the default value.
5484 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5485 }
5486 else
5487 {
5488 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5489 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005490 ufunc->uf_arg_types[idx], tv,
5491 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005492 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005493 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005494 }
5495 ++ectx.ec_stack.ga_len;
5496 }
5497
5498 // Turn varargs into a list. Empty list if no args.
5499 if (ufunc->uf_va_name != NULL)
5500 {
5501 int vararg_count = argc - ufunc->uf_args.ga_len;
5502
5503 if (vararg_count < 0)
5504 vararg_count = 0;
5505 else
5506 argc -= vararg_count;
5507 if (exe_newlist(vararg_count, &ectx) == FAIL)
5508 goto failed_early;
5509
5510 // Check the type of the list items.
5511 tv = STACK_TV_BOT(-1);
5512 if (ufunc->uf_va_type != NULL
5513 && ufunc->uf_va_type != &t_list_any
5514 && ufunc->uf_va_type->tt_member != &t_any
5515 && tv->vval.v_list != NULL)
5516 {
5517 type_T *expected = ufunc->uf_va_type->tt_member;
5518 listitem_T *li = tv->vval.v_list->lv_first;
5519
5520 for (idx = 0; idx < vararg_count; ++idx)
5521 {
5522 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005523 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005524 goto failed_early;
5525 li = li->li_next;
5526 }
5527 }
5528
5529 if (defcount > 0)
5530 // Move varargs list to below missing default arguments.
5531 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5532 --ectx.ec_stack.ga_len;
5533 }
5534
5535 // Make space for omitted arguments, will store default value below.
5536 // Any varargs list goes after them.
5537 if (defcount > 0)
5538 for (idx = 0; idx < defcount; ++idx)
5539 {
5540 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5541 ++ectx.ec_stack.ga_len;
5542 }
5543 if (ufunc->uf_va_name != NULL)
5544 ++ectx.ec_stack.ga_len;
5545
5546 // Frame pointer points to just after arguments.
5547 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5548 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5549
5550 {
5551 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5552 + ufunc->uf_dfunc_idx;
5553 ufunc_T *base_ufunc = dfunc->df_ufunc;
5554
5555 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005556 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005557 if (partial != NULL || base_ufunc->uf_partial != NULL)
5558 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005559 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5560 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005561 goto failed_early;
5562 if (partial != NULL)
5563 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005564 outer_T *outer = get_pt_outer(partial);
5565
5566 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005567 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005568 if (current_ectx != NULL)
5569 {
5570 if (current_ectx->ec_outer_ref != NULL
5571 && current_ectx->ec_outer_ref->or_outer != NULL)
5572 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005573 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005574 }
5575 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005576 }
5577 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005578 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005579 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005580 ++partial->pt_refcount;
5581 ectx.ec_outer_ref->or_partial = partial;
5582 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005583 }
5584 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005585 {
5586 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5587 ++base_ufunc->uf_partial->pt_refcount;
5588 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5589 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005590 }
5591 }
5592
5593 // dummy frame entries
5594 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5595 {
5596 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5597 ++ectx.ec_stack.ga_len;
5598 }
5599
5600 {
5601 // Reserve space for local variables and any closure reference count.
5602 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5603 + ufunc->uf_dfunc_idx;
5604
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005605 // Initialize variables to zero. That avoids having to generate
5606 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005607 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005608 {
5609 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5610 STACK_TV_VAR(idx)->vval.v_number = 0;
5611 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005612 ectx.ec_stack.ga_len += dfunc->df_varcount;
5613 if (dfunc->df_has_closure)
5614 {
5615 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5616 STACK_TV_VAR(idx)->vval.v_number = 0;
5617 ++ectx.ec_stack.ga_len;
5618 }
5619
5620 ectx.ec_instr = INSTRUCTIONS(dfunc);
5621 }
5622
Bram Moolenaar58779852022-09-06 18:31:14 +01005623 // Store the execution context in funccal, used by invoke_all_defer().
5624 if (funccal != NULL)
5625 funccal->fc_ectx = &ectx;
5626
Bram Moolenaar4c137212021-04-19 16:48:48 +02005627 // Following errors are in the function, not the caller.
5628 // Commands behave like vim9script.
5629 estack_push_ufunc(ufunc, 1);
5630 current_sctx = ufunc->uf_script_ctx;
5631 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5632
5633 // Use a specific location for storing error messages to be converted to an
5634 // exception.
5635 saved_msg_list = msg_list;
5636 msg_list = &private_msg_list;
5637
5638 // Do turn errors into exceptions.
5639 suppress_errthrow = FALSE;
5640
5641 // Do not delete the function while executing it.
5642 ++ufunc->uf_calls;
5643
5644 // When ":silent!" was used before calling then we still abort the
5645 // function. If ":silent!" is used in the function then we don't.
5646 emsg_silent_def = emsg_silent;
5647 did_emsg_def = 0;
5648
5649 ectx.ec_where.wt_index = 0;
5650 ectx.ec_where.wt_variable = FALSE;
5651
5652 // Execute the instructions until done.
5653 ret = exec_instructions(&ectx);
5654 if (ret == OK)
5655 {
5656 // function finished, get result from the stack.
5657 if (ufunc->uf_ret_type == &t_void)
5658 {
5659 rettv->v_type = VAR_VOID;
5660 }
5661 else
5662 {
5663 tv = STACK_TV_BOT(-1);
5664 *rettv = *tv;
5665 tv->v_type = VAR_UNKNOWN;
5666 }
5667 }
5668
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005669 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005670 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005671
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005672 // Deal with any remaining closures, they may be in use somewhere.
5673 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005674 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005675 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005676 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005677 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005678
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005679 estack_pop();
5680 current_sctx = save_current_sctx;
5681
Bram Moolenaar2336c372021-12-06 15:06:54 +00005682 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5683 // Function was unreferenced while being used, free it now.
5684 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005685
Bram Moolenaar352134b2020-10-17 22:04:08 +02005686 if (*msg_list != NULL && saved_msg_list != NULL)
5687 {
5688 msglist_T **plist = saved_msg_list;
5689
5690 // Append entries from the current msg_list (uncaught exceptions) to
5691 // the saved msg_list.
5692 while (*plist != NULL)
5693 plist = &(*plist)->next;
5694
5695 *plist = *msg_list;
5696 }
5697 msg_list = saved_msg_list;
5698
Bram Moolenaar4c137212021-04-19 16:48:48 +02005699 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005700 {
5701 cmdmod.cmod_filter_regmatch.regprog = NULL;
5702 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005703 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005704 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005705 emsg_silent_def = save_emsg_silent_def;
5706 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005707
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005708failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005709 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005710 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5711 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005712 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005714 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005715 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005716 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005717 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005718 if (ectx.ec_outer_ref->or_outer_allocated)
5719 vim_free(ectx.ec_outer_ref->or_outer);
5720 partial_unref(ectx.ec_outer_ref->or_partial);
5721 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005722 }
5723
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005724 // Not sure if this is necessary.
5725 suppress_errthrow = save_suppress_errthrow;
5726
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005727 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5728 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005729 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005730 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005731 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005732 return ret;
5733}
5734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005735/*
Bram Moolenaar58779852022-09-06 18:31:14 +01005736 * Called when a def function has finished (possibly failed).
5737 * Invoke all the function returns to clean up and invoke deferred functions,
5738 * except the toplevel one.
5739 */
5740 void
5741unwind_def_callstack(ectx_T *ectx)
5742{
5743 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
5744 func_return(ectx);
5745}
5746
5747/*
5748 * Invoke any deffered functions for the top function in "ectx".
5749 */
5750 void
5751may_invoke_defer_funcs(ectx_T *ectx)
5752{
5753 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
5754
5755 if (dfunc->df_defer_var_idx > 0)
5756 invoke_defer_funcs(ectx);
5757}
5758
5759/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005760 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5761 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5762 * "pfx" is prefixed to every line.
5763 */
5764 static void
5765list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5766{
5767 int line_idx = 0;
5768 int prev_current = 0;
5769 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005770 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005771
5772 for (current = 0; current < instr_count; ++current)
5773 {
5774 isn_T *iptr = &instr[current];
5775 char *line;
5776
5777 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005778 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005779 while (line_idx < iptr->isn_lnum
5780 && line_idx < ufunc->uf_lines.ga_len)
5781 {
5782 if (current > prev_current)
5783 {
5784 msg_puts("\n\n");
5785 prev_current = current;
5786 }
5787 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5788 if (line != NULL)
5789 msg(line);
5790 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005791 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5792 {
5793 int first_def_arg = ufunc->uf_args.ga_len
5794 - ufunc->uf_def_args.ga_len;
5795
5796 if (def_arg_idx > 0)
5797 msg_puts("\n\n");
5798 msg_start();
5799 msg_puts(" ");
5800 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5801 first_def_arg + def_arg_idx]);
5802 msg_puts(" = ");
5803 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5804 msg_clr_eos();
5805 msg_end();
5806 }
5807 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005808
5809 switch (iptr->isn_type)
5810 {
5811 case ISN_EXEC:
5812 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5813 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005814 case ISN_EXEC_SPLIT:
5815 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5816 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005817 case ISN_EXECRANGE:
5818 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5819 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005820 case ISN_LEGACY_EVAL:
5821 smsg("%s%4d EVAL legacy %s", pfx, current,
5822 iptr->isn_arg.string);
5823 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005824 case ISN_REDIRSTART:
5825 smsg("%s%4d REDIR", pfx, current);
5826 break;
5827 case ISN_REDIREND:
5828 smsg("%s%4d REDIR END%s", pfx, current,
5829 iptr->isn_arg.number ? " append" : "");
5830 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005831 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005832#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005833 smsg("%s%4d CEXPR pre %s", pfx, current,
5834 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005835#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005836 break;
5837 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005838#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005839 {
5840 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5841
5842 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5843 cexpr_get_auname(cer->cer_cmdidx),
5844 cer->cer_forceit ? "!" : "",
5845 cer->cer_cmdline);
5846 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005847#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005848 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005849 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005850 smsg("%s%4d INSTR", pfx, current);
5851 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5852 msg(" -------------");
5853 break;
5854 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005855 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005856 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5857
5858 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005859 }
5860 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005861 case ISN_SUBSTITUTE:
5862 {
5863 subs_T *subs = &iptr->isn_arg.subs;
5864
5865 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5866 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5867 msg(" -------------");
5868 }
5869 break;
5870 case ISN_EXECCONCAT:
5871 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5872 (varnumber_T)iptr->isn_arg.number);
5873 break;
5874 case ISN_ECHO:
5875 {
5876 echo_T *echo = &iptr->isn_arg.echo;
5877
5878 smsg("%s%4d %s %d", pfx, current,
5879 echo->echo_with_white ? "ECHO" : "ECHON",
5880 echo->echo_count);
5881 }
5882 break;
5883 case ISN_EXECUTE:
5884 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005885 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005886 break;
5887 case ISN_ECHOMSG:
5888 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005889 (varnumber_T)(iptr->isn_arg.number));
5890 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01005891 case ISN_ECHOWINDOW:
5892 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
5893 (varnumber_T)(iptr->isn_arg.number));
5894 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02005895 case ISN_ECHOCONSOLE:
5896 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5897 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005898 break;
5899 case ISN_ECHOERR:
5900 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005901 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005902 break;
5903 case ISN_LOAD:
5904 {
5905 if (iptr->isn_arg.number < 0)
5906 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5907 (varnumber_T)(iptr->isn_arg.number
5908 + STACK_FRAME_SIZE));
5909 else
5910 smsg("%s%4d LOAD $%lld", pfx, current,
5911 (varnumber_T)(iptr->isn_arg.number));
5912 }
5913 break;
5914 case ISN_LOADOUTER:
5915 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005916 isn_outer_T *outer = &iptr->isn_arg.outer;
5917
5918 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005919 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005920 outer->outer_depth,
5921 outer->outer_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +02005922 + STACK_FRAME_SIZE);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005923 else if (outer->outer_depth == OUTER_LOOP_DEPTH)
5924 smsg("%s%4d LOADOUTER level 1 $%d in loop",
5925 pfx, current, outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005926 else
5927 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005928 outer->outer_depth,
5929 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005930 }
5931 break;
5932 case ISN_LOADV:
5933 smsg("%s%4d LOADV v:%s", pfx, current,
5934 get_vim_var_name(iptr->isn_arg.number));
5935 break;
5936 case ISN_LOADSCRIPT:
5937 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005938 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5939 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5940 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005941
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005942 sv = get_script_svar(sref, -1);
5943 if (sv == NULL)
5944 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5945 pfx, current, si->sn_name);
5946 else
5947 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005948 sv->sv_name,
5949 sref->sref_idx,
5950 si->sn_name);
5951 }
5952 break;
5953 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005954 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005955 {
5956 scriptitem_T *si = SCRIPT_ITEM(
5957 iptr->isn_arg.loadstore.ls_sid);
5958
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005959 smsg("%s%4d %s s:%s from %s", pfx, current,
5960 iptr->isn_type == ISN_LOADS ? "LOADS"
5961 : "LOADEXPORT",
5962 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005963 }
5964 break;
5965 case ISN_LOADAUTO:
5966 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5967 break;
5968 case ISN_LOADG:
5969 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5970 break;
5971 case ISN_LOADB:
5972 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5973 break;
5974 case ISN_LOADW:
5975 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5976 break;
5977 case ISN_LOADT:
5978 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5979 break;
5980 case ISN_LOADGDICT:
5981 smsg("%s%4d LOAD g:", pfx, current);
5982 break;
5983 case ISN_LOADBDICT:
5984 smsg("%s%4d LOAD b:", pfx, current);
5985 break;
5986 case ISN_LOADWDICT:
5987 smsg("%s%4d LOAD w:", pfx, current);
5988 break;
5989 case ISN_LOADTDICT:
5990 smsg("%s%4d LOAD t:", pfx, current);
5991 break;
5992 case ISN_LOADOPT:
5993 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5994 break;
5995 case ISN_LOADENV:
5996 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5997 break;
5998 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005999 smsg("%s%4d LOADREG @%c", pfx, current,
6000 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006001 break;
6002
6003 case ISN_STORE:
6004 if (iptr->isn_arg.number < 0)
6005 smsg("%s%4d STORE arg[%lld]", pfx, current,
6006 iptr->isn_arg.number + STACK_FRAME_SIZE);
6007 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006008 smsg("%s%4d STORE $%lld", pfx, current,
6009 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006010 break;
6011 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006012 {
6013 isn_outer_T *outer = &iptr->isn_arg.outer;
6014
6015 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6016 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6017 pfx, current, outer->outer_idx);
6018 else
6019 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6020 outer->outer_depth, outer->outer_idx);
6021 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006022 break;
6023 case ISN_STOREV:
6024 smsg("%s%4d STOREV v:%s", pfx, current,
6025 get_vim_var_name(iptr->isn_arg.number));
6026 break;
6027 case ISN_STOREAUTO:
6028 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6029 break;
6030 case ISN_STOREG:
6031 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6032 break;
6033 case ISN_STOREB:
6034 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6035 break;
6036 case ISN_STOREW:
6037 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6038 break;
6039 case ISN_STORET:
6040 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6041 break;
6042 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006043 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006044 {
6045 scriptitem_T *si = SCRIPT_ITEM(
6046 iptr->isn_arg.loadstore.ls_sid);
6047
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006048 smsg("%s%4d %s %s in %s", pfx, current,
6049 iptr->isn_type == ISN_STORES
6050 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006051 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6052 }
6053 break;
6054 case ISN_STORESCRIPT:
6055 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006056 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6057 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6058 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006059
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006060 sv = get_script_svar(sref, -1);
6061 if (sv == NULL)
6062 smsg("%s%4d STORESCRIPT [deleted] in %s",
6063 pfx, current, si->sn_name);
6064 else
6065 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006066 sv->sv_name,
6067 sref->sref_idx,
6068 si->sn_name);
6069 }
6070 break;
6071 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006072 case ISN_STOREFUNCOPT:
6073 smsg("%s%4d %s &%s", pfx, current,
6074 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006075 iptr->isn_arg.storeopt.so_name);
6076 break;
6077 case ISN_STOREENV:
6078 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6079 break;
6080 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006081 smsg("%s%4d STOREREG @%c", pfx, current,
6082 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006083 break;
6084 case ISN_STORENR:
6085 smsg("%s%4d STORE %lld in $%d", pfx, current,
6086 iptr->isn_arg.storenr.stnr_val,
6087 iptr->isn_arg.storenr.stnr_idx);
6088 break;
6089
6090 case ISN_STOREINDEX:
6091 smsg("%s%4d STOREINDEX %s", pfx, current,
6092 vartype_name(iptr->isn_arg.vartype));
6093 break;
6094
6095 case ISN_STORERANGE:
6096 smsg("%s%4d STORERANGE", pfx, current);
6097 break;
6098
6099 // constants
6100 case ISN_PUSHNR:
6101 smsg("%s%4d PUSHNR %lld", pfx, current,
6102 (varnumber_T)(iptr->isn_arg.number));
6103 break;
6104 case ISN_PUSHBOOL:
6105 case ISN_PUSHSPEC:
6106 smsg("%s%4d PUSH %s", pfx, current,
6107 get_var_special_name(iptr->isn_arg.number));
6108 break;
6109 case ISN_PUSHF:
6110#ifdef FEAT_FLOAT
6111 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
6112#endif
6113 break;
6114 case ISN_PUSHS:
6115 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6116 break;
6117 case ISN_PUSHBLOB:
6118 {
6119 char_u *r;
6120 char_u numbuf[NUMBUFLEN];
6121 char_u *tofree;
6122
6123 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6124 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6125 vim_free(tofree);
6126 }
6127 break;
6128 case ISN_PUSHFUNC:
6129 {
6130 char *name = (char *)iptr->isn_arg.string;
6131
6132 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6133 name == NULL ? "[none]" : name);
6134 }
6135 break;
6136 case ISN_PUSHCHANNEL:
6137#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006138 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006139#endif
6140 break;
6141 case ISN_PUSHJOB:
6142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006143 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006144#endif
6145 break;
6146 case ISN_PUSHEXC:
6147 smsg("%s%4d PUSH v:exception", pfx, current);
6148 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006149 case ISN_AUTOLOAD:
6150 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6151 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006152 case ISN_UNLET:
6153 smsg("%s%4d UNLET%s %s", pfx, current,
6154 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6155 iptr->isn_arg.unlet.ul_name);
6156 break;
6157 case ISN_UNLETENV:
6158 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6159 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6160 iptr->isn_arg.unlet.ul_name);
6161 break;
6162 case ISN_UNLETINDEX:
6163 smsg("%s%4d UNLETINDEX", pfx, current);
6164 break;
6165 case ISN_UNLETRANGE:
6166 smsg("%s%4d UNLETRANGE", pfx, current);
6167 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006168 case ISN_LOCKUNLOCK:
6169 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6170 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006171 case ISN_LOCKCONST:
6172 smsg("%s%4d LOCKCONST", pfx, current);
6173 break;
6174 case ISN_NEWLIST:
6175 smsg("%s%4d NEWLIST size %lld", pfx, current,
6176 (varnumber_T)(iptr->isn_arg.number));
6177 break;
6178 case ISN_NEWDICT:
6179 smsg("%s%4d NEWDICT size %lld", pfx, current,
6180 (varnumber_T)(iptr->isn_arg.number));
6181 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006182 case ISN_NEWPARTIAL:
6183 smsg("%s%4d NEWPARTIAL", pfx, current);
6184 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006185
6186 // function call
6187 case ISN_BCALL:
6188 {
6189 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6190
6191 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6192 internal_func_name(cbfunc->cbf_idx),
6193 cbfunc->cbf_argcount);
6194 }
6195 break;
6196 case ISN_DCALL:
6197 {
6198 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6199 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6200 + cdfunc->cdf_idx;
6201
6202 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006203 printable_func_name(df->df_ufunc),
6204 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006205 }
6206 break;
6207 case ISN_UCALL:
6208 {
6209 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6210
6211 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6212 cufunc->cuf_name, cufunc->cuf_argcount);
6213 }
6214 break;
6215 case ISN_PCALL:
6216 {
6217 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6218
6219 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6220 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6221 }
6222 break;
6223 case ISN_PCALL_END:
6224 smsg("%s%4d PCALL end", pfx, current);
6225 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006226 case ISN_DEFER:
6227 smsg("%s%4d DEFER %d args", pfx, current,
6228 (int)iptr->isn_arg.defer.defer_argcount);
6229 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006230 case ISN_RETURN:
6231 smsg("%s%4d RETURN", pfx, current);
6232 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006233 case ISN_RETURN_VOID:
6234 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006235 break;
6236 case ISN_FUNCREF:
6237 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006238 funcref_T *funcref = &iptr->isn_arg.funcref;
6239 funcref_extra_T *extra = funcref->fr_extra;
6240 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006241
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006242 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006243 {
6244 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6245 + funcref->fr_dfunc_idx;
6246 name = df->df_ufunc->uf_name;
6247 }
6248 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006249 name = extra->fre_func_name;
6250 if (extra == NULL || extra->fre_loop_var_count == 0)
6251 smsg("%s%4d FUNCREF %s", pfx, current, name);
6252 else
6253 smsg("%s%4d FUNCREF %s var $%d - $%d", pfx, current,
6254 name,
6255 extra->fre_loop_var_idx,
6256 extra->fre_loop_var_idx
6257 + extra->fre_loop_var_count - 1);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006258 }
6259 break;
6260
6261 case ISN_NEWFUNC:
6262 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006263 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006264
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006265 if (arg->nfa_loop_var_count == 0)
6266 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6267 arg->nfa_lambda, arg->nfa_global);
6268 else
6269 smsg("%s%4d NEWFUNC %s %s var $%d - $%d", pfx, current,
6270 arg->nfa_lambda, arg->nfa_global,
6271 arg->nfa_loop_var_idx,
6272 arg->nfa_loop_var_idx + arg->nfa_loop_var_count - 1);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006273 }
6274 break;
6275
6276 case ISN_DEF:
6277 {
6278 char_u *name = iptr->isn_arg.string;
6279
6280 smsg("%s%4d DEF %s", pfx, current,
6281 name == NULL ? (char_u *)"" : name);
6282 }
6283 break;
6284
6285 case ISN_JUMP:
6286 {
6287 char *when = "?";
6288
6289 switch (iptr->isn_arg.jump.jump_when)
6290 {
6291 case JUMP_ALWAYS:
6292 when = "JUMP";
6293 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006294 case JUMP_NEVER:
6295 iemsg("JUMP_NEVER should not be used");
6296 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006297 case JUMP_AND_KEEP_IF_TRUE:
6298 when = "JUMP_AND_KEEP_IF_TRUE";
6299 break;
6300 case JUMP_IF_FALSE:
6301 when = "JUMP_IF_FALSE";
6302 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006303 case JUMP_WHILE_FALSE:
6304 when = "JUMP_WHILE_FALSE"; // unused
6305 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006306 case JUMP_IF_COND_FALSE:
6307 when = "JUMP_IF_COND_FALSE";
6308 break;
6309 case JUMP_IF_COND_TRUE:
6310 when = "JUMP_IF_COND_TRUE";
6311 break;
6312 }
6313 smsg("%s%4d %s -> %d", pfx, current, when,
6314 iptr->isn_arg.jump.jump_where);
6315 }
6316 break;
6317
6318 case ISN_JUMP_IF_ARG_SET:
6319 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6320 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6321 iptr->isn_arg.jump.jump_where);
6322 break;
6323
6324 case ISN_FOR:
6325 {
6326 forloop_T *forloop = &iptr->isn_arg.forloop;
6327
6328 smsg("%s%4d FOR $%d -> %d", pfx, current,
6329 forloop->for_idx, forloop->for_end);
6330 }
6331 break;
6332
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006333 case ISN_ENDLOOP:
6334 {
6335 endloop_T *endloop = &iptr->isn_arg.endloop;
6336
6337 smsg("%s%4d ENDLOOP $%d save $%d - $%d", pfx, current,
6338 endloop->end_funcref_idx,
6339 endloop->end_var_idx,
6340 endloop->end_var_idx + endloop->end_var_count - 1);
6341 }
6342 break;
6343
6344 case ISN_WHILE:
6345 {
6346 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6347
6348 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6349 whileloop->while_funcref_idx,
6350 whileloop->while_end);
6351 }
6352 break;
6353
Bram Moolenaar4c137212021-04-19 16:48:48 +02006354 case ISN_TRY:
6355 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006356 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006357
6358 if (try->try_ref->try_finally == 0)
6359 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6360 pfx, current,
6361 try->try_ref->try_catch,
6362 try->try_ref->try_endtry);
6363 else
6364 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6365 pfx, current,
6366 try->try_ref->try_catch,
6367 try->try_ref->try_finally,
6368 try->try_ref->try_endtry);
6369 }
6370 break;
6371 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006372 smsg("%s%4d CATCH", pfx, current);
6373 break;
6374 case ISN_TRYCONT:
6375 {
6376 trycont_T *trycont = &iptr->isn_arg.trycont;
6377
6378 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6379 trycont->tct_levels,
6380 trycont->tct_levels == 1 ? "" : "s",
6381 trycont->tct_where);
6382 }
6383 break;
6384 case ISN_FINALLY:
6385 smsg("%s%4d FINALLY", pfx, current);
6386 break;
6387 case ISN_ENDTRY:
6388 smsg("%s%4d ENDTRY", pfx, current);
6389 break;
6390 case ISN_THROW:
6391 smsg("%s%4d THROW", pfx, current);
6392 break;
6393
6394 // expression operations on number
6395 case ISN_OPNR:
6396 case ISN_OPFLOAT:
6397 case ISN_OPANY:
6398 {
6399 char *what;
6400 char *ins;
6401
6402 switch (iptr->isn_arg.op.op_type)
6403 {
6404 case EXPR_MULT: what = "*"; break;
6405 case EXPR_DIV: what = "/"; break;
6406 case EXPR_REM: what = "%"; break;
6407 case EXPR_SUB: what = "-"; break;
6408 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006409 case EXPR_LSHIFT: what = "<<"; break;
6410 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006411 default: what = "???"; break;
6412 }
6413 switch (iptr->isn_type)
6414 {
6415 case ISN_OPNR: ins = "OPNR"; break;
6416 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6417 case ISN_OPANY: ins = "OPANY"; break;
6418 default: ins = "???"; break;
6419 }
6420 smsg("%s%4d %s %s", pfx, current, ins, what);
6421 }
6422 break;
6423
6424 case ISN_COMPAREBOOL:
6425 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006426 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006427 case ISN_COMPARENR:
6428 case ISN_COMPAREFLOAT:
6429 case ISN_COMPARESTRING:
6430 case ISN_COMPAREBLOB:
6431 case ISN_COMPARELIST:
6432 case ISN_COMPAREDICT:
6433 case ISN_COMPAREFUNC:
6434 case ISN_COMPAREANY:
6435 {
6436 char *p;
6437 char buf[10];
6438 char *type;
6439
6440 switch (iptr->isn_arg.op.op_type)
6441 {
6442 case EXPR_EQUAL: p = "=="; break;
6443 case EXPR_NEQUAL: p = "!="; break;
6444 case EXPR_GREATER: p = ">"; break;
6445 case EXPR_GEQUAL: p = ">="; break;
6446 case EXPR_SMALLER: p = "<"; break;
6447 case EXPR_SEQUAL: p = "<="; break;
6448 case EXPR_MATCH: p = "=~"; break;
6449 case EXPR_IS: p = "is"; break;
6450 case EXPR_ISNOT: p = "isnot"; break;
6451 case EXPR_NOMATCH: p = "!~"; break;
6452 default: p = "???"; break;
6453 }
6454 STRCPY(buf, p);
6455 if (iptr->isn_arg.op.op_ic == TRUE)
6456 strcat(buf, "?");
6457 switch(iptr->isn_type)
6458 {
6459 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6460 case ISN_COMPARESPECIAL:
6461 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006462 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006463 case ISN_COMPARENR: type = "COMPARENR"; break;
6464 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6465 case ISN_COMPARESTRING:
6466 type = "COMPARESTRING"; break;
6467 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6468 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6469 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6470 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6471 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6472 default: type = "???"; break;
6473 }
6474
6475 smsg("%s%4d %s %s", pfx, current, type, buf);
6476 }
6477 break;
6478
6479 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6480 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6481
6482 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006483 case ISN_CONCAT:
6484 smsg("%s%4d CONCAT size %lld", pfx, current,
6485 (varnumber_T)(iptr->isn_arg.number));
6486 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006487 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6488 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6489 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6490 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6491 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6492 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6493 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6494 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6495 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6496 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6497 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006498 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006499 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6500 iptr->isn_arg.getitem.gi_index,
6501 iptr->isn_arg.getitem.gi_with_op ?
6502 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006503 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6504 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6505 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006506 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6507 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6508
Bram Moolenaar4c137212021-04-19 16:48:48 +02006509 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6510
Bram Moolenaar4c137212021-04-19 16:48:48 +02006511 case ISN_CHECKTYPE:
6512 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006513 checktype_T *ct = &iptr->isn_arg.type;
6514 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006515
6516 if (ct->ct_arg_idx == 0)
6517 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6518 type_name(ct->ct_type, &tofree),
6519 (int)ct->ct_off);
6520 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006521 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006522 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006523 type_name(ct->ct_type, &tofree),
6524 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006525 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006526 (int)ct->ct_arg_idx);
6527 vim_free(tofree);
6528 break;
6529 }
6530 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6531 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6532 iptr->isn_arg.checklen.cl_min_len);
6533 break;
6534 case ISN_SETTYPE:
6535 {
6536 char *tofree;
6537
6538 smsg("%s%4d SETTYPE %s", pfx, current,
6539 type_name(iptr->isn_arg.type.ct_type, &tofree));
6540 vim_free(tofree);
6541 break;
6542 }
6543 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006544 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6545 smsg("%s%4d INVERT %d (!val)", pfx, current,
6546 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006547 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006548 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6549 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006550 break;
6551 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006552 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006553 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006554 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6555 pfx, current,
6556 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006557 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006558 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6559 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006560 break;
6561 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006562 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006563 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006564 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006565 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6566 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006567 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006568 else
6569 smsg("%s%4d PUT %c %ld", pfx, current,
6570 iptr->isn_arg.put.put_regname,
6571 (long)iptr->isn_arg.put.put_lnum);
6572 break;
6573
Bram Moolenaar4c137212021-04-19 16:48:48 +02006574 case ISN_CMDMOD:
6575 {
6576 char_u *buf;
6577 size_t len = produce_cmdmods(
6578 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6579
6580 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006581 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006582 {
6583 (void)produce_cmdmods(
6584 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6585 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6586 vim_free(buf);
6587 }
6588 break;
6589 }
6590 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6591
6592 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006593 smsg("%s%4d PROFILE START line %d", pfx, current,
6594 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006595 break;
6596
6597 case ISN_PROF_END:
6598 smsg("%s%4d PROFILE END", pfx, current);
6599 break;
6600
Bram Moolenaare99d4222021-06-13 14:01:26 +02006601 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006602 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6603 iptr->isn_arg.debug.dbg_break_lnum + 1,
6604 iptr->isn_lnum,
6605 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006606 break;
6607
Bram Moolenaar4c137212021-04-19 16:48:48 +02006608 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6609 iptr->isn_arg.unpack.unp_count,
6610 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6611 break;
6612 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6613 iptr->isn_arg.shuffle.shfl_item,
6614 iptr->isn_arg.shuffle.shfl_up);
6615 break;
6616 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6617
6618 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6619 return;
6620 }
6621
6622 out_flush(); // output one line at a time
6623 ui_breakcheck();
6624 if (got_int)
6625 break;
6626 }
6627}
6628
6629/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006630 * Handle command line completion for the :disassemble command.
6631 */
6632 void
6633set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6634{
6635 char_u *p;
6636
6637 // Default: expand user functions, "debug" and "profile"
6638 xp->xp_context = EXPAND_DISASSEMBLE;
6639 xp->xp_pattern = arg;
6640
6641 // first argument already typed: only user function names
6642 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6643 {
6644 xp->xp_context = EXPAND_USER_FUNC;
6645 xp->xp_pattern = skipwhite(p);
6646 }
6647}
6648
6649/*
6650 * Function given to ExpandGeneric() to obtain the list of :disassemble
6651 * arguments.
6652 */
6653 char_u *
6654get_disassemble_argument(expand_T *xp, int idx)
6655{
6656 if (idx == 0)
6657 return (char_u *)"debug";
6658 if (idx == 1)
6659 return (char_u *)"profile";
6660 return get_user_func_name(xp, idx - 2);
6661}
6662
6663/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006664 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006665 * We don't really need this at runtime, but we do have tests that require it,
6666 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006667 */
6668 void
6669ex_disassemble(exarg_T *eap)
6670{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006671 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006672 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006673 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006674 isn_T *instr = NULL; // init to shut up gcc warning
6675 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006676 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006677
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006678 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006679 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006680 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006681 if (func_needs_compiling(ufunc, compile_type)
6682 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006683 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006684 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006686 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687 return;
6688 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006689 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690
6691 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006692 switch (compile_type)
6693 {
6694 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006695#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006696 instr = dfunc->df_instr_prof;
6697 instr_count = dfunc->df_instr_prof_count;
6698 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006699#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006700 // FALLTHROUGH
6701 case CT_NONE:
6702 instr = dfunc->df_instr;
6703 instr_count = dfunc->df_instr_count;
6704 break;
6705 case CT_DEBUG:
6706 instr = dfunc->df_instr_debug;
6707 instr_count = dfunc->df_instr_debug_count;
6708 break;
6709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710
Bram Moolenaar4c137212021-04-19 16:48:48 +02006711 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712}
6713
6714/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006715 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006716 * list, etc. Mostly like what JavaScript does, except that empty list and
6717 * empty dictionary are FALSE.
6718 */
6719 int
6720tv2bool(typval_T *tv)
6721{
6722 switch (tv->v_type)
6723 {
6724 case VAR_NUMBER:
6725 return tv->vval.v_number != 0;
6726 case VAR_FLOAT:
6727#ifdef FEAT_FLOAT
6728 return tv->vval.v_float != 0.0;
6729#else
6730 break;
6731#endif
6732 case VAR_PARTIAL:
6733 return tv->vval.v_partial != NULL;
6734 case VAR_FUNC:
6735 case VAR_STRING:
6736 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6737 case VAR_LIST:
6738 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6739 case VAR_DICT:
6740 return tv->vval.v_dict != NULL
6741 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6742 case VAR_BOOL:
6743 case VAR_SPECIAL:
6744 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6745 case VAR_JOB:
6746#ifdef FEAT_JOB_CHANNEL
6747 return tv->vval.v_job != NULL;
6748#else
6749 break;
6750#endif
6751 case VAR_CHANNEL:
6752#ifdef FEAT_JOB_CHANNEL
6753 return tv->vval.v_channel != NULL;
6754#else
6755 break;
6756#endif
6757 case VAR_BLOB:
6758 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6759 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006760 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006761 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006762 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006763 break;
6764 }
6765 return FALSE;
6766}
6767
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006768 void
6769emsg_using_string_as(typval_T *tv, int as_number)
6770{
6771 semsg(_(as_number ? e_using_string_as_number_str
6772 : e_using_string_as_bool_str),
6773 tv->vval.v_string == NULL
6774 ? (char_u *)"" : tv->vval.v_string);
6775}
6776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777/*
6778 * If "tv" is a string give an error and return FAIL.
6779 */
6780 int
6781check_not_string(typval_T *tv)
6782{
6783 if (tv->v_type == VAR_STRING)
6784 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006785 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 clear_tv(tv);
6787 return FAIL;
6788 }
6789 return OK;
6790}
6791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792
6793#endif // FEAT_EVAL