blob: 3b6a084d7b377346673d256d95a1c8f8d3475e1e [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 Moolenaar148ce7a2020-09-23 21:57:23 +0200777 ++funcstack->fs_refcount;
778 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100779 pt->pt_outer.out_stack = &funcstack->fs_ga;
780 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200781 }
782 }
783 }
784
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200785 for (idx = 0; idx < closure_count; ++idx)
786 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
787 - closure_count + idx]);
788 gap->ga_len -= closure_count;
789 if (gap->ga_len == 0)
790 ga_clear(gap);
791
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200792 return OK;
793}
794
795/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200796 * Called when a partial is freed or its reference count goes down to one. The
797 * funcstack may be the only reference to the partials in the local variables.
798 * Go over all of them, the funcref and can be freed if all partials
799 * referencing the funcstack have a reference count of one.
800 */
801 void
802funcstack_check_refcount(funcstack_T *funcstack)
803{
804 int i;
805 garray_T *gap = &funcstack->fs_ga;
806 int done = 0;
807
808 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
809 return;
810 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
811 {
812 typval_T *tv = ((typval_T *)gap->ga_data) + i;
813
814 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
815 && tv->vval.v_partial->pt_funcstack == funcstack
816 && tv->vval.v_partial->pt_refcount == 1)
817 ++done;
818 }
819 if (done == funcstack->fs_min_refcount)
820 {
821 typval_T *stack = gap->ga_data;
822
823 // All partials referencing the funcstack have a reference count of
824 // one, thus the funcstack is no longer of use.
825 for (i = 0; i < gap->ga_len; ++i)
826 clear_tv(stack + i);
827 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000828 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200829 vim_free(funcstack);
830 }
831}
832
833/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000834 * For garbage collecting: set references in all variables referenced by
835 * all funcstacks.
836 */
837 int
838set_ref_in_funcstacks(int copyID)
839{
840 funcstack_T *funcstack;
841
842 for (funcstack = first_funcstack; funcstack != NULL;
843 funcstack = funcstack->fs_next)
844 {
845 typval_T *stack = funcstack->fs_ga.ga_data;
846 int i;
847
848 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
849 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
850 return TRUE; // abort
851 }
852 return FALSE;
853}
854
Bram Moolenaar806a2732022-09-04 15:40:36 +0100855// Ugly static to avoid passing the execution context around through many
856// layers.
857static ectx_T *current_ectx = NULL;
858
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000859/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100860 * Return TRUE if currently executing a :def function.
861 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100862 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100863 int
864in_def_function(void)
865{
866 return current_ectx != NULL;
867}
868
869/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100870 * Clear "current_ectx" and return the previous value. To be used when calling
871 * a user function.
872 */
873 ectx_T *
874clear_currrent_ectx(void)
875{
876 ectx_T *r = current_ectx;
877
878 current_ectx = NULL;
879 return r;
880}
881
882 void
883restore_current_ectx(ectx_T *ectx)
884{
885 if (current_ectx != NULL)
886 iemsg("Restoring current_ectx while it is not NULL");
887 current_ectx = ectx;
888}
889
890/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100891 * Add an entry for a deferred function call to the currently executing
892 * function.
893 * Return the list or NULL when failed.
894 */
895 static list_T *
896add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100897{
898 typval_T *defer_tv = STACK_TV_VAR(var_idx);
899 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100900 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100901 typval_T listval;
902
903 if (defer_tv->v_type != VAR_LIST)
904 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100905 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100906 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100907 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100908 }
909 defer_l = defer_tv->vval.v_list;
910
911 l = list_alloc_with_items(argcount + 1);
912 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100913 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100914 listval.v_type = VAR_LIST;
915 listval.vval.v_list = l;
916 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100917 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100918 {
919 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100920 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100921 }
922
Bram Moolenaar806a2732022-09-04 15:40:36 +0100923 return l;
924}
925
926/*
927 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
928 * The local variable that lists deferred functions is "var_idx".
929 * Returns OK or FAIL.
930 */
931 static int
932defer_command(int var_idx, int argcount, ectx_T *ectx)
933{
934 list_T *l = add_defer_item(var_idx, argcount, ectx);
935 int i;
936 typval_T *func_tv;
937
938 if (l == NULL)
939 return FAIL;
940
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100941 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100942 if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL)
943 {
944 semsg(_(e_expected_str_but_got_str),
945 "function or partial",
946 vartype_name(func_tv->v_type));
947 return FAIL;
948 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100949 list_set_item(l, 0, func_tv);
950
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100951 for (i = 0; i < argcount; ++i)
952 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100953 ectx->ec_stack.ga_len -= argcount + 1;
954 return OK;
955}
956
957/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100958 * Add a deferred function "name" with one argument "arg_tv".
959 * Consumes "name", also on failure.
960 * Only to be called when in_def_function() returns TRUE.
961 */
962 int
963add_defer_function(char_u *name, int argcount, typval_T *argvars)
964{
965 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
966 + current_ectx->ec_dfunc_idx;
967 list_T *l;
968 typval_T func_tv;
969 int i;
970
971 if (dfunc->df_defer_var_idx == 0)
972 {
973 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100974 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100975 return FAIL;
976 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100977
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100978 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100979 if (l == NULL)
980 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100981 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100982 return FAIL;
983 }
984
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100985 func_tv.v_type = VAR_FUNC;
986 func_tv.v_lock = 0;
987 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100988 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100989
Bram Moolenaar806a2732022-09-04 15:40:36 +0100990 for (i = 0; i < argcount; ++i)
991 list_set_item(l, i + 1, argvars + i);
992 return OK;
993}
994
995/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100996 * Invoked when returning from a function: Invoke any deferred calls.
997 */
998 static void
999invoke_defer_funcs(ectx_T *ectx)
1000{
1001 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1002 + ectx->ec_dfunc_idx;
1003 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1004 listitem_T *li;
1005
1006 if (defer_tv->v_type != VAR_LIST)
1007 return; // no function added
1008 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1009 {
1010 list_T *l = li->li_tv.vval.v_list;
1011 typval_T rettv;
1012 typval_T argvars[MAX_FUNC_ARGS];
1013 int i;
1014 listitem_T *arg_li = l->lv_first;
1015 funcexe_T funcexe;
1016
1017 for (i = 0; i < l->lv_len - 1; ++i)
1018 {
1019 arg_li = arg_li->li_next;
1020 argvars[i] = arg_li->li_tv;
1021 }
1022
1023 CLEAR_FIELD(funcexe);
1024 funcexe.fe_evaluate = TRUE;
1025 rettv.v_type = VAR_UNKNOWN;
1026 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1027 &rettv, l->lv_len - 1, argvars, &funcexe);
1028 clear_tv(&rettv);
1029 }
1030}
1031
1032/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 * Return from the current function.
1034 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001035 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001036func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001039 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001040 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1041 + ectx->ec_dfunc_idx;
1042 int argcount = ufunc_argcount(dfunc->df_ufunc);
1043 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001044 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001045 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1046 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001047 funclocal_T *floc;
1048#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001049 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1050 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051
Bram Moolenaar12d26532021-02-19 19:13:21 +01001052 if (do_profiling == PROF_YES)
1053 {
1054 ufunc_T *caller = prev_dfunc->df_ufunc;
1055
1056 if (dfunc->df_ufunc->uf_profiling
1057 || (caller != NULL && caller->uf_profiling))
1058 {
1059 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1060 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1061 --profile_info_ga.ga_len;
1062 }
1063 }
1064#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001065
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001066 if (dfunc->df_defer_var_idx > 0)
1067 invoke_defer_funcs(ectx);
1068
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001069 // No check for uf_refcount being zero, cannot think of a way that would
1070 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001071 --dfunc->df_ufunc->uf_calls;
1072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001074 entry = estack_pop();
1075 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001076 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001078 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1079 return FAIL;
1080
1081 // Clear the arguments.
1082 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1083 clear_tv(STACK_TV(idx));
1084
1085 // Clear local variables and temp values, but not the return value.
1086 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001087 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001089
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001090 // The return value should be on top of the stack. However, when aborting
1091 // it may not be there and ec_frame_idx is the top of the stack.
1092 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001093 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001094 ret_idx = 0;
1095
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001096 if (ectx->ec_outer_ref != NULL)
1097 {
1098 if (ectx->ec_outer_ref->or_outer_allocated)
1099 vim_free(ectx->ec_outer_ref->or_outer);
1100 partial_unref(ectx->ec_outer_ref->or_partial);
1101 vim_free(ectx->ec_outer_ref);
1102 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001103
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001104 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001105 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001106 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1107 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001108 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1109 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001110 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001111 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001112 floc = (void *)STACK_TV(ectx->ec_frame_idx
1113 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001114 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001115 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1116 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001117
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001118 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001119 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001120 else
1121 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001122 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001123 vim_free(floc);
1124 }
1125
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001126 if (ret_idx > 0)
1127 {
1128 // Reset the stack to the position before the call, with a spot for the
1129 // return value, moved there from above the frame.
1130 ectx->ec_stack.ga_len = top + 1;
1131 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1132 }
1133 else
1134 // Reset the stack to the position before the call.
1135 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001136
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001137 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001138 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001139 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140}
1141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142/*
1143 * Prepare arguments and rettv for calling a builtin or user function.
1144 */
1145 static int
1146call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1147{
1148 int idx;
1149 typval_T *tv;
1150
1151 // Move arguments from bottom of the stack to argvars[] and add terminator.
1152 for (idx = 0; idx < argcount; ++idx)
1153 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1154 argvars[argcount].v_type = VAR_UNKNOWN;
1155
1156 // Result replaces the arguments on the stack.
1157 if (argcount > 0)
1158 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001159 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160 return FAIL;
1161 else
1162 ++ectx->ec_stack.ga_len;
1163
1164 // Default return value is zero.
1165 tv = STACK_TV_BOT(-1);
1166 tv->v_type = VAR_NUMBER;
1167 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001168 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169
1170 return OK;
1171}
1172
1173/*
1174 * Call a builtin function by index.
1175 */
1176 static int
1177call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1178{
1179 typval_T argvars[MAX_FUNC_ARGS];
1180 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001181 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001182 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001183 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184
1185 if (call_prepare(argcount, argvars, ectx) == FAIL)
1186 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001187 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001189 // Call the builtin function. Set "current_ectx" so that when it
1190 // recursively invokes call_def_function() a closure context can be set.
1191 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001193 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001194 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195
1196 // Clear the arguments.
1197 for (idx = 0; idx < argcount; ++idx)
1198 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001199
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001200 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001201 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 return OK;
1203}
1204
1205/*
1206 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001207 * If the function is compiled this will add a stack frame and set the
1208 * instruction pointer at the start of the function.
1209 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001210 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001211 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 */
1213 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001214call_ufunc(
1215 ufunc_T *ufunc,
1216 partial_T *pt,
1217 int argcount,
1218 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001219 isn_T *iptr,
1220 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221{
1222 typval_T argvars[MAX_FUNC_ARGS];
1223 funcexe_T funcexe;
1224 int error;
1225 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001226 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001227 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228
Bram Moolenaare99d4222021-06-13 14:01:26 +02001229 if (func_needs_compiling(ufunc, compile_type)
1230 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1231 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001232 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001233 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001234 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001235 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001236 if (error != FCERR_UNKNOWN)
1237 {
1238 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001239 semsg(_(e_too_many_arguments_for_function_str),
1240 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001241 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001242 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001243 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001244 return FAIL;
1245 }
1246
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001247 // The function has been compiled, can call it quickly. For a function
1248 // that was defined later: we can call it directly next time.
1249 if (iptr != NULL)
1250 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001251 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001252 iptr->isn_type = ISN_DCALL;
1253 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1254 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1255 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001256 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258
1259 if (call_prepare(argcount, argvars, ectx) == FAIL)
1260 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001261 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001262 funcexe.fe_evaluate = TRUE;
1263 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264
1265 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001267 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268
1269 // Clear the arguments.
1270 for (idx = 0; idx < argcount; ++idx)
1271 clear_tv(&argvars[idx]);
1272
1273 if (error != FCERR_NONE)
1274 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001275 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 return FAIL;
1277 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001278 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001279 // Error other than from calling the function itself.
1280 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 return OK;
1282}
1283
1284/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001285 * If command modifiers were applied restore them.
1286 */
1287 static void
1288may_restore_cmdmod(funclocal_T *funclocal)
1289{
1290 if (funclocal->floc_restore_cmdmod)
1291 {
1292 cmdmod.cmod_filter_regmatch.regprog = NULL;
1293 undo_cmdmod(&cmdmod);
1294 cmdmod = funclocal->floc_save_cmdmod;
1295 funclocal->floc_restore_cmdmod = FALSE;
1296 }
1297}
1298
1299/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001300 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1301 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001302 */
1303 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001304vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001305{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001306 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001307}
1308
1309/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 * Execute a function by "name".
1311 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001312 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 * Returns FAIL if not found without an error message.
1314 */
1315 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001316call_by_name(
1317 char_u *name,
1318 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001319 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001320 isn_T *iptr,
1321 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322{
1323 ufunc_T *ufunc;
1324
1325 if (builtin_function(name, -1))
1326 {
1327 int func_idx = find_internal_func(name);
1328
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001329 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001331 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 return FAIL;
1333 return call_bfunc(func_idx, argcount, ectx);
1334 }
1335
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001336 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001337
1338 if (ufunc == NULL)
1339 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001340 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001341
1342 if (script_autoload(name, TRUE))
1343 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001344 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001345
1346 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001347 return FAIL; // bail out if loading the script caused an error
1348 }
1349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001351 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001352 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001353 {
1354 int i;
1355 typval_T *argv = STACK_TV_BOT(0) - argcount;
1356
1357 // The function can change at runtime, check that the argument
1358 // types are correct.
1359 for (i = 0; i < argcount; ++i)
1360 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001361 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001362
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001363 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001364 type = ufunc->uf_arg_types[i];
1365 else if (ufunc->uf_va_type != NULL)
1366 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001367 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001368 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001369 return FAIL;
1370 }
1371 }
1372
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001373 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001374 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375
1376 return FAIL;
1377}
1378
1379 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001380call_partial(
1381 typval_T *tv,
1382 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001383 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001385 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001386 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001388 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001389 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390
1391 if (tv->v_type == VAR_PARTIAL)
1392 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001393 partial_T *pt = tv->vval.v_partial;
1394 int i;
1395
1396 if (pt->pt_argc > 0)
1397 {
1398 // Make space for arguments from the partial, shift the "argcount"
1399 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001400 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001401 return FAIL;
1402 for (i = 1; i <= argcount; ++i)
1403 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1404 ectx->ec_stack.ga_len += pt->pt_argc;
1405 argcount += pt->pt_argc;
1406
1407 // copy the arguments from the partial onto the stack
1408 for (i = 0; i < pt->pt_argc; ++i)
1409 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1410 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001411 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412
1413 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001414 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 name = pt->pt_name;
1417 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001418 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001420 if (name != NULL)
1421 {
1422 char_u fname_buf[FLEN_FIXED + 1];
1423 char_u *tofree = NULL;
1424 int error = FCERR_NONE;
1425 char_u *fname;
1426
1427 // May need to translate <SNR>123_ to K_SNR.
1428 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1429 if (error != FCERR_NONE)
1430 res = FAIL;
1431 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001432 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001433 vim_free(tofree);
1434 }
1435
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001436 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 {
1438 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001439 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001440 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 return FAIL;
1442 }
1443 return OK;
1444}
1445
1446/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001447 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1448 * TRUE.
1449 */
1450 static int
1451error_if_locked(int lock, char *error)
1452{
1453 if (lock & (VAR_LOCKED | VAR_FIXED))
1454 {
1455 emsg(_(error));
1456 return TRUE;
1457 }
1458 return FALSE;
1459}
1460
1461/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001462 * Give an error if "tv" is not a number and return FAIL.
1463 */
1464 static int
1465check_for_number(typval_T *tv)
1466{
1467 if (tv->v_type != VAR_NUMBER)
1468 {
1469 semsg(_(e_expected_str_but_got_str),
1470 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1471 return FAIL;
1472 }
1473 return OK;
1474}
1475
1476/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001477 * Store "tv" in variable "name".
1478 * This is for s: and g: variables.
1479 */
1480 static void
1481store_var(char_u *name, typval_T *tv)
1482{
1483 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001484 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001485
Bram Moolenaard877a572021-04-01 19:42:48 +02001486 if (tv->v_lock)
1487 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001488 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001489 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001490 restore_funccal();
1491}
1492
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001493/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001494 * Convert "tv" to a string.
1495 * Return FAIL if not allowed.
1496 */
1497 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001498do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001499{
1500 if (tv->v_type != VAR_STRING)
1501 {
1502 char_u *str;
1503
1504 if (is_2string_any)
1505 {
1506 switch (tv->v_type)
1507 {
1508 case VAR_SPECIAL:
1509 case VAR_BOOL:
1510 case VAR_NUMBER:
1511 case VAR_FLOAT:
1512 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001513
1514 case VAR_LIST:
1515 if (tolerant)
1516 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001517 char_u *s, *e, *p;
1518 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001519
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001520 ga_init2(&ga, sizeof(char_u *), 1);
1521
1522 // Convert to NL separated items, then
1523 // escape the items and replace the NL with
1524 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001525 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001526 if (str == NULL)
1527 return FAIL;
1528 s = str;
1529 while ((e = vim_strchr(s, '\n')) != NULL)
1530 {
1531 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001532 p = vim_strsave_fnameescape(s,
1533 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001534 if (p != NULL)
1535 {
1536 ga_concat(&ga, p);
1537 ga_concat(&ga, (char_u *)" ");
1538 vim_free(p);
1539 }
1540 s = e + 1;
1541 }
1542 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001543 clear_tv(tv);
1544 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001545 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001546 return OK;
1547 }
1548 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001549 default: to_string_error(tv->v_type);
1550 return FAIL;
1551 }
1552 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001553 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001554 clear_tv(tv);
1555 tv->v_type = VAR_STRING;
1556 tv->vval.v_string = str;
1557 }
1558 return OK;
1559}
1560
1561/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001562 * When the value of "sv" is a null list of dict, allocate it.
1563 */
1564 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001565allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001566{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001567 typval_T *tv = sv->sv_tv;
1568
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001569 switch (tv->v_type)
1570 {
1571 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001572 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001573 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001574 break;
1575 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001576 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001577 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001578 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001579 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001580 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001581 (void)rettv_blob_alloc(tv);
1582 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001583 default:
1584 break;
1585 }
1586}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001587
Bram Moolenaare7525c52021-01-09 13:20:37 +01001588/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001589 * Return the character "str[index]" where "index" is the character index,
1590 * including composing characters.
1591 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001592 */
1593 char_u *
1594char_from_string(char_u *str, varnumber_T index)
1595{
1596 size_t nbyte = 0;
1597 varnumber_T nchar = index;
1598 size_t slen;
1599
1600 if (str == NULL)
1601 return NULL;
1602 slen = STRLEN(str);
1603
Bram Moolenaarff871402021-03-26 13:34:05 +01001604 // Do the same as for a list: a negative index counts from the end.
1605 // Optimization to check the first byte to be below 0x80 (and no composing
1606 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001607 if (index < 0)
1608 {
1609 int clen = 0;
1610
1611 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001612 {
1613 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1614 ++nbyte;
1615 else if (enc_utf8)
1616 nbyte += utfc_ptr2len(str + nbyte);
1617 else
1618 nbyte += mb_ptr2len(str + nbyte);
1619 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001620 nchar = clen + index;
1621 if (nchar < 0)
1622 // unlike list: index out of range results in empty string
1623 return NULL;
1624 }
1625
1626 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001627 {
1628 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1629 ++nbyte;
1630 else if (enc_utf8)
1631 nbyte += utfc_ptr2len(str + nbyte);
1632 else
1633 nbyte += mb_ptr2len(str + nbyte);
1634 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001635 if (nbyte >= slen)
1636 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001637 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001638}
1639
1640/*
1641 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001642 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001643 * If going over the end return "str_len".
1644 * If "idx" is negative count from the end, -1 is the last character.
1645 * When going over the start return -1.
1646 */
1647 static long
1648char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1649{
1650 varnumber_T nchar = idx;
1651 size_t nbyte = 0;
1652
1653 if (nchar >= 0)
1654 {
1655 while (nchar > 0 && nbyte < str_len)
1656 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001657 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001658 --nchar;
1659 }
1660 }
1661 else
1662 {
1663 nbyte = str_len;
1664 while (nchar < 0 && nbyte > 0)
1665 {
1666 --nbyte;
1667 nbyte -= mb_head_off(str, str + nbyte);
1668 ++nchar;
1669 }
1670 if (nchar < 0)
1671 return -1;
1672 }
1673 return (long)nbyte;
1674}
1675
1676/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001677 * Return the slice "str[first : last]" using character indexes. Composing
1678 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001679 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001680 * Return NULL when the result is empty.
1681 */
1682 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001683string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001684{
1685 long start_byte, end_byte;
1686 size_t slen;
1687
1688 if (str == NULL)
1689 return NULL;
1690 slen = STRLEN(str);
1691 start_byte = char_idx2byte(str, slen, first);
1692 if (start_byte < 0)
1693 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001694 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001695 end_byte = (long)slen;
1696 else
1697 {
1698 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001699 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001700 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001701 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001702 }
1703
1704 if (start_byte >= (long)slen || end_byte <= start_byte)
1705 return NULL;
1706 return vim_strnsave(str + start_byte, end_byte - start_byte);
1707}
1708
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001709/*
1710 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1711 * When "dfunc_idx" is negative don't give an error.
1712 * Returns NULL for an error.
1713 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001714 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001715get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001716{
1717 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001718 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1719 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001720 svar_T *sv;
1721
1722 if (sref->sref_seq != si->sn_script_seq)
1723 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001724 // The script was reloaded after the function was compiled, the
1725 // script_idx may not be valid.
1726 if (dfunc != NULL)
1727 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1728 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001729 return NULL;
1730 }
1731 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001732 if (sv->sv_name == NULL)
1733 {
1734 if (dfunc != NULL)
1735 emsg(_(e_script_variable_was_deleted));
1736 return NULL;
1737 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001738 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001739 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001740 if (dfunc != NULL)
1741 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001742 return NULL;
1743 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001744
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001745 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1746 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001747 {
1748 if (dfunc != NULL)
1749 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1750 return NULL;
1751 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001752 return sv;
1753}
1754
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001755/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001756 * Function passed to do_cmdline() for splitting a script joined by NL
1757 * characters.
1758 */
1759 static char_u *
1760get_split_sourceline(
1761 int c UNUSED,
1762 void *cookie,
1763 int indent UNUSED,
1764 getline_opt_T options UNUSED)
1765{
1766 source_cookie_T *sp = (source_cookie_T *)cookie;
1767 char_u *p;
1768 char_u *line;
1769
Bram Moolenaar20677332021-06-06 17:02:53 +02001770 p = vim_strchr(sp->nextline, '\n');
1771 if (p == NULL)
1772 {
1773 line = vim_strsave(sp->nextline);
1774 sp->nextline += STRLEN(sp->nextline);
1775 }
1776 else
1777 {
1778 line = vim_strnsave(sp->nextline, p - sp->nextline);
1779 sp->nextline = p + 1;
1780 }
1781 return line;
1782}
1783
1784/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 * Execute a function by "name".
1786 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001787 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 */
1789 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001790call_eval_func(
1791 char_u *name,
1792 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001793 ectx_T *ectx,
1794 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795{
Bram Moolenaared677f52020-08-12 16:38:10 +02001796 int called_emsg_before = called_emsg;
1797 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001799 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001800 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001802 dictitem_T *v;
1803
1804 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001805 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1806 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001807 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001808 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001809 return FAIL;
1810 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001811 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001813 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814}
1815
1816/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001817 * When a function reference is used, fill a partial with the information
1818 * needed, especially when it is used as a closure.
1819 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001820 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001821fill_partial_and_closure(
1822 partial_T *pt,
1823 ufunc_T *ufunc,
1824 short loop_var_idx,
1825 short loop_var_count,
1826 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001827{
1828 pt->pt_func = ufunc;
1829 pt->pt_refcount = 1;
1830
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001831 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001832 {
1833 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1834 + ectx->ec_dfunc_idx;
1835
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001836 // The closure may need to find arguments and local variables of the
1837 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001838 pt->pt_outer.out_stack = &ectx->ec_stack;
1839 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001840 if (ectx->ec_outer_ref != NULL)
1841 {
1842 // The current context already has a context, link to that one.
1843 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1844 if (ectx->ec_outer_ref->or_partial != NULL)
1845 {
1846 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1847 ++pt->pt_outer.out_up_partial->pt_refcount;
1848 }
1849 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001850
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001851 // The closure may need to find variables defined inside a loop. A
1852 // new reference is made every time, ISN_ENDLOOP will check if they
1853 // are actually used.
1854 pt->pt_outer.out_loop_stack = &ectx->ec_stack;
1855 pt->pt_outer.out_loop_var_idx = ectx->ec_frame_idx + STACK_FRAME_SIZE
1856 + loop_var_idx;
1857 pt->pt_outer.out_loop_var_count = loop_var_count;
1858
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001859 // If the function currently executing returns and the closure is still
1860 // being referenced, we need to make a copy of the context (arguments
1861 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001862 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001863 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001864 {
1865 vim_free(pt);
1866 return FAIL;
1867 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001868 // Extra variable keeps the count of closures created in the current
1869 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001870 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1871 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1872
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001873 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1874 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001875 ++pt->pt_refcount;
1876 ++ectx->ec_funcrefs.ga_len;
1877 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001878 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001879 return OK;
1880}
1881
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001882/*
1883 * Execute iptr->isn_arg.string as an Ex command.
1884 */
1885 static int
1886exec_command(isn_T *iptr)
1887{
1888 source_cookie_T cookie;
1889
1890 SOURCING_LNUM = iptr->isn_lnum;
1891 // Pass getsourceline to get an error for a missing ":end"
1892 // command.
1893 CLEAR_FIELD(cookie);
1894 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1895 if (do_cmdline(iptr->isn_arg.string,
1896 getsourceline, &cookie,
1897 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1898 || did_emsg)
1899 return FAIL;
1900 return OK;
1901}
1902
Bram Moolenaar89445512022-04-14 12:58:23 +01001903/*
1904 * If script "sid" is not loaded yet then load it now.
1905 * Caller must make sure "sid" is a valid script ID.
1906 * "loaded" is set to TRUE if the script had to be loaded.
1907 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1908 */
1909 int
1910may_load_script(int sid, int *loaded)
1911{
1912 scriptitem_T *si = SCRIPT_ITEM(sid);
1913
1914 if (si->sn_state == SN_STATE_NOT_LOADED)
1915 {
1916 *loaded = TRUE;
1917 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1918 {
1919 semsg(_(e_cant_open_file_str), si->sn_name);
1920 return FAIL;
1921 }
1922 }
1923 return OK;
1924}
1925
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001926// used for v_instr of typval of VAR_INSTR
1927struct instr_S {
1928 ectx_T *instr_ectx;
1929 isn_T *instr_instr;
1930};
1931
Bram Moolenaar4c137212021-04-19 16:48:48 +02001932// used for substitute_instr
1933typedef struct subs_expr_S {
1934 ectx_T *subs_ectx;
1935 isn_T *subs_instr;
1936 int subs_status;
1937} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001939// Set when calling do_debug().
1940static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001941static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001942
1943/*
1944 * When debugging lookup "name" and return the typeval.
1945 * When not found return NULL.
1946 */
1947 typval_T *
1948lookup_debug_var(char_u *name)
1949{
1950 int idx;
1951 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001952 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001953 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001954 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001955
1956 if (ectx == NULL)
1957 return NULL;
1958 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1959
1960 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001961 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001962 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001963 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1964
1965 // the variable name may be NULL when not available in this block
1966 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001967 return STACK_TV_VAR(idx);
1968 }
1969
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001970 // Go through argument names.
1971 ufunc = dfunc->df_ufunc;
1972 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1973 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1974 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1975 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1976 - varargs_off + idx);
1977 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1978 return STACK_TV(ectx->ec_frame_idx - 1);
1979
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001980 return NULL;
1981}
1982
Bram Moolenaar26a44842021-09-02 18:49:06 +02001983/*
1984 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1985 * breakpoint was set in that function or when there is any expression.
1986 */
1987 int
1988may_break_in_function(ufunc_T *ufunc)
1989{
1990 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1991}
1992
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001993 static void
1994handle_debug(isn_T *iptr, ectx_T *ectx)
1995{
1996 char_u *line;
1997 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1998 + ectx->ec_dfunc_idx)->df_ufunc;
1999 isn_T *ni;
2000 int end_lnum = iptr->isn_lnum;
2001 garray_T ga;
2002 int lnum;
2003
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002004 if (ex_nesting_level > debug_break_level)
2005 {
2006 linenr_T breakpoint;
2007
Bram Moolenaar26a44842021-09-02 18:49:06 +02002008 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002009 return;
2010
2011 // check for the next breakpoint if needed
2012 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002013 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002014 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2015 return;
2016 }
2017
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002018 SOURCING_LNUM = iptr->isn_lnum;
2019 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002020 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002021
2022 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2023 if (ni->isn_type == ISN_DEBUG
2024 || ni->isn_type == ISN_RETURN
2025 || ni->isn_type == ISN_RETURN_VOID)
2026 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002027 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002028 break;
2029 }
2030
2031 if (end_lnum > iptr->isn_lnum)
2032 {
2033 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002034 for (lnum = iptr->isn_lnum; lnum < end_lnum
2035 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002036 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002037 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002038
Bram Moolenaar303215d2021-07-07 20:10:43 +02002039 if (p == NULL)
2040 continue; // left over from continuation line
2041 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002042 if (*p == '#')
2043 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002044 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002045 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2046 if (STRNCMP(p, "def ", 4) == 0)
2047 break;
2048 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002049 line = ga_concat_strings(&ga, " ");
2050 vim_free(ga.ga_data);
2051 }
2052 else
2053 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002054
Dominique Pelle6e969552021-06-17 13:53:41 +02002055 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002056 debug_context = NULL;
2057
2058 if (end_lnum > iptr->isn_lnum)
2059 vim_free(line);
2060}
2061
Bram Moolenaar4c137212021-04-19 16:48:48 +02002062/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002063 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002064 * Returns OK, FAIL or NOTDONE (uncatchable error).
2065 */
2066 static int
2067execute_storeindex(isn_T *iptr, ectx_T *ectx)
2068{
2069 vartype_T dest_type = iptr->isn_arg.vartype;
2070 typval_T *tv;
2071 typval_T *tv_idx = STACK_TV_BOT(-2);
2072 typval_T *tv_dest = STACK_TV_BOT(-1);
2073 int status = OK;
2074
2075 // Stack contains:
2076 // -3 value to be stored
2077 // -2 index
2078 // -1 dict or list
2079 tv = STACK_TV_BOT(-3);
2080 SOURCING_LNUM = iptr->isn_lnum;
2081 if (dest_type == VAR_ANY)
2082 {
2083 dest_type = tv_dest->v_type;
2084 if (dest_type == VAR_DICT)
2085 status = do_2string(tv_idx, TRUE, FALSE);
2086 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2087 {
2088 emsg(_(e_number_expected));
2089 status = FAIL;
2090 }
2091 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002092
2093 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002094 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002095 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002096 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002097 long lidx = (long)tv_idx->vval.v_number;
2098 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002099
Bram Moolenaare08be092022-02-17 13:08:26 +00002100 if (list == NULL)
2101 {
2102 emsg(_(e_list_not_set));
2103 return FAIL;
2104 }
2105 if (lidx < 0 && list->lv_len + lidx >= 0)
2106 // negative index is relative to the end
2107 lidx = list->lv_len + lidx;
2108 if (lidx < 0 || lidx > list->lv_len)
2109 {
2110 semsg(_(e_list_index_out_of_range_nr), lidx);
2111 return FAIL;
2112 }
2113 if (lidx < list->lv_len)
2114 {
2115 listitem_T *li = list_find(list, lidx);
2116
2117 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002118 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002119 return FAIL;
2120 // overwrite existing list item
2121 clear_tv(&li->li_tv);
2122 li->li_tv = *tv;
2123 }
2124 else
2125 {
2126 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2127 return FAIL;
2128 // append to list, only fails when out of memory
2129 if (list_append_tv(list, tv) == FAIL)
2130 return NOTDONE;
2131 clear_tv(tv);
2132 }
2133 }
2134 else if (dest_type == VAR_DICT)
2135 {
2136 char_u *key = tv_idx->vval.v_string;
2137 dict_T *dict = tv_dest->vval.v_dict;
2138 dictitem_T *di;
2139
2140 SOURCING_LNUM = iptr->isn_lnum;
2141 if (dict == NULL)
2142 {
2143 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002144 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002145 }
2146 if (key == NULL)
2147 key = (char_u *)"";
2148 di = dict_find(dict, key, -1);
2149 if (di != NULL)
2150 {
2151 if (error_if_locked(di->di_tv.v_lock,
2152 e_cannot_change_dict_item))
2153 return FAIL;
2154 // overwrite existing value
2155 clear_tv(&di->di_tv);
2156 di->di_tv = *tv;
2157 }
2158 else
2159 {
2160 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2161 return FAIL;
2162 // add to dict, only fails when out of memory
2163 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2164 return NOTDONE;
2165 clear_tv(tv);
2166 }
2167 }
2168 else if (dest_type == VAR_BLOB)
2169 {
2170 long lidx = (long)tv_idx->vval.v_number;
2171 blob_T *blob = tv_dest->vval.v_blob;
2172 varnumber_T nr;
2173 int error = FALSE;
2174 int len;
2175
2176 if (blob == NULL)
2177 {
2178 emsg(_(e_blob_not_set));
2179 return FAIL;
2180 }
2181 len = blob_len(blob);
2182 if (lidx < 0 && len + lidx >= 0)
2183 // negative index is relative to the end
2184 lidx = len + lidx;
2185
2186 // Can add one byte at the end.
2187 if (lidx < 0 || lidx > len)
2188 {
2189 semsg(_(e_blob_index_out_of_range_nr), lidx);
2190 return FAIL;
2191 }
2192 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2193 return FAIL;
2194 nr = tv_get_number_chk(tv, &error);
2195 if (error)
2196 return FAIL;
2197 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002198 }
2199 else
2200 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002201 status = FAIL;
2202 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002203 }
2204 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002205
2206 clear_tv(tv_idx);
2207 clear_tv(tv_dest);
2208 ectx->ec_stack.ga_len -= 3;
2209 if (status == FAIL)
2210 {
2211 clear_tv(tv);
2212 return FAIL;
2213 }
2214 return OK;
2215}
2216
2217/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002218 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002219 */
2220 static int
2221execute_storerange(isn_T *iptr, ectx_T *ectx)
2222{
2223 typval_T *tv;
2224 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2225 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2226 typval_T *tv_dest = STACK_TV_BOT(-1);
2227 int status = OK;
2228
2229 // Stack contains:
2230 // -4 value to be stored
2231 // -3 first index or "none"
2232 // -2 second index or "none"
2233 // -1 destination list or blob
2234 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002235 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002236 if (tv_dest->v_type == VAR_LIST)
2237 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002238 long n1;
2239 long n2;
2240 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002241
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002242 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2243 if (tv_idx2->v_type == VAR_SPECIAL
2244 && tv_idx2->vval.v_number == VVAL_NONE)
2245 n2 = list_len(tv_dest->vval.v_list) - 1;
2246 else
2247 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2248
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002249 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002250 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002251 status = FAIL;
2252 else
2253 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002254 status = check_range_index_two(tv_dest->vval.v_list,
2255 &n1, li1, &n2, FALSE);
2256 if (status != FAIL)
2257 status = list_assign_range(
2258 tv_dest->vval.v_list,
2259 tv->vval.v_list,
2260 n1,
2261 n2,
2262 tv_idx2->v_type == VAR_SPECIAL,
2263 (char_u *)"=",
2264 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002265 }
2266 }
2267 else if (tv_dest->v_type == VAR_BLOB)
2268 {
2269 varnumber_T n1;
2270 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002271 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002272
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002273 n1 = tv_get_number_chk(tv_idx1, NULL);
2274 if (tv_idx2->v_type == VAR_SPECIAL
2275 && tv_idx2->vval.v_number == VVAL_NONE)
2276 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2277 else
2278 n2 = tv_get_number_chk(tv_idx2, NULL);
2279 bloblen = blob_len(tv_dest->vval.v_blob);
2280
2281 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2282 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002283 status = FAIL;
2284 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002285 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002286 }
2287 else
2288 {
2289 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002290 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002291 }
2292
2293 clear_tv(tv_idx1);
2294 clear_tv(tv_idx2);
2295 clear_tv(tv_dest);
2296 ectx->ec_stack.ga_len -= 4;
2297 clear_tv(tv);
2298
2299 return status;
2300}
2301
2302/*
2303 * Unlet item in list or dict variable.
2304 */
2305 static int
2306execute_unletindex(isn_T *iptr, ectx_T *ectx)
2307{
2308 typval_T *tv_idx = STACK_TV_BOT(-2);
2309 typval_T *tv_dest = STACK_TV_BOT(-1);
2310 int status = OK;
2311
2312 // Stack contains:
2313 // -2 index
2314 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002315 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002316 if (tv_dest->v_type == VAR_DICT)
2317 {
2318 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002319 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002320 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002321 semsg(_(e_expected_str_but_got_str),
2322 vartype_name(VAR_STRING),
2323 vartype_name(tv_idx->v_type));
2324 status = FAIL;
2325 }
2326 else
2327 {
2328 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002329 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002330 dictitem_T *di = NULL;
2331
2332 if (d != NULL && value_check_lock(
2333 d->dv_lock, NULL, FALSE))
2334 status = FAIL;
2335 else
2336 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002337 if (tv_idx->v_type == VAR_STRING)
2338 {
2339 key = tv_idx->vval.v_string;
2340 if (key == NULL)
2341 key = (char_u *)"";
2342 }
2343 else
2344 {
2345 key = tv_get_string(tv_idx);
2346 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002347 if (d != NULL)
2348 di = dict_find(d, key, (int)STRLEN(key));
2349 if (di == NULL)
2350 {
2351 // NULL dict is equivalent to empty dict
2352 semsg(_(e_key_not_present_in_dictionary),
2353 key);
2354 status = FAIL;
2355 }
2356 else if (var_check_fixed(di->di_flags,
2357 NULL, FALSE)
2358 || var_check_ro(di->di_flags,
2359 NULL, FALSE))
2360 status = FAIL;
2361 else
2362 dictitem_remove(d, di);
2363 }
2364 }
2365 }
2366 else if (tv_dest->v_type == VAR_LIST)
2367 {
2368 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002369 if (check_for_number(tv_idx) == FAIL)
2370 {
2371 status = FAIL;
2372 }
2373 else
2374 {
2375 list_T *l = tv_dest->vval.v_list;
2376 long n = (long)tv_idx->vval.v_number;
2377
2378 if (l != NULL && value_check_lock(
2379 l->lv_lock, NULL, FALSE))
2380 status = FAIL;
2381 else
2382 {
2383 listitem_T *li = list_find(l, n);
2384
2385 if (li == NULL)
2386 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002387 semsg(_(e_list_index_out_of_range_nr), n);
2388 status = FAIL;
2389 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002390 else
2391 listitem_remove(l, li);
2392 }
2393 }
2394 }
2395 else
2396 {
2397 status = FAIL;
2398 semsg(_(e_cannot_index_str),
2399 vartype_name(tv_dest->v_type));
2400 }
2401
2402 clear_tv(tv_idx);
2403 clear_tv(tv_dest);
2404 ectx->ec_stack.ga_len -= 2;
2405
2406 return status;
2407}
2408
2409/*
2410 * Unlet a range of items in a list variable.
2411 */
2412 static int
2413execute_unletrange(isn_T *iptr, ectx_T *ectx)
2414{
2415 // Stack contains:
2416 // -3 index1
2417 // -2 index2
2418 // -1 dict or list
2419 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2420 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2421 typval_T *tv_dest = STACK_TV_BOT(-1);
2422 int status = OK;
2423
2424 if (tv_dest->v_type == VAR_LIST)
2425 {
2426 // indexes must be a number
2427 SOURCING_LNUM = iptr->isn_lnum;
2428 if (check_for_number(tv_idx1) == FAIL
2429 || (tv_idx2->v_type != VAR_SPECIAL
2430 && check_for_number(tv_idx2) == FAIL))
2431 {
2432 status = FAIL;
2433 }
2434 else
2435 {
2436 list_T *l = tv_dest->vval.v_list;
2437 long n1 = (long)tv_idx1->vval.v_number;
2438 long n2 = tv_idx2->v_type == VAR_SPECIAL
2439 ? 0 : (long)tv_idx2->vval.v_number;
2440 listitem_T *li;
2441
2442 li = list_find_index(l, &n1);
2443 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002444 {
2445 semsg(_(e_list_index_out_of_range_nr),
2446 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002447 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002448 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002449 else
2450 {
2451 if (n1 < 0)
2452 n1 = list_idx_of_item(l, li);
2453 if (n2 < 0)
2454 {
2455 listitem_T *li2 = list_find(l, n2);
2456
2457 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002458 {
2459 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002460 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002461 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002462 else
2463 n2 = list_idx_of_item(l, li2);
2464 }
2465 if (status != FAIL
2466 && tv_idx2->v_type != VAR_SPECIAL
2467 && n2 < n1)
2468 {
2469 semsg(_(e_list_index_out_of_range_nr), n2);
2470 status = FAIL;
2471 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002472 if (status != FAIL)
2473 list_unlet_range(l, li, n1,
2474 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002475 }
2476 }
2477 }
2478 else
2479 {
2480 status = FAIL;
2481 SOURCING_LNUM = iptr->isn_lnum;
2482 semsg(_(e_cannot_index_str),
2483 vartype_name(tv_dest->v_type));
2484 }
2485
2486 clear_tv(tv_idx1);
2487 clear_tv(tv_idx2);
2488 clear_tv(tv_dest);
2489 ectx->ec_stack.ga_len -= 3;
2490
2491 return status;
2492}
2493
2494/*
2495 * Top of a for loop.
2496 */
2497 static int
2498execute_for(isn_T *iptr, ectx_T *ectx)
2499{
2500 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002501 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002502 typval_T *ltv = STACK_TV_BOT(-1);
2503 typval_T *idxtv =
2504 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2505
2506 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2507 return FAIL;
2508 if (ltv->v_type == VAR_LIST)
2509 {
2510 list_T *list = ltv->vval.v_list;
2511
2512 // push the next item from the list
2513 ++idxtv->vval.v_number;
2514 if (list == NULL
2515 || idxtv->vval.v_number >= list->lv_len)
2516 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002517 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002518 }
2519 else if (list->lv_first == &range_list_item)
2520 {
2521 // non-materialized range() list
2522 tv = STACK_TV_BOT(0);
2523 tv->v_type = VAR_NUMBER;
2524 tv->v_lock = 0;
2525 tv->vval.v_number = list_find_nr(
2526 list, idxtv->vval.v_number, NULL);
2527 ++ectx->ec_stack.ga_len;
2528 }
2529 else
2530 {
2531 listitem_T *li = list_find(list,
2532 idxtv->vval.v_number);
2533
2534 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2535 ++ectx->ec_stack.ga_len;
2536 }
2537 }
2538 else if (ltv->v_type == VAR_STRING)
2539 {
2540 char_u *str = ltv->vval.v_string;
2541
2542 // The index is for the last byte of the previous
2543 // character.
2544 ++idxtv->vval.v_number;
2545 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2546 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002547 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002548 }
2549 else
2550 {
2551 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2552
2553 // Push the next character from the string.
2554 tv = STACK_TV_BOT(0);
2555 tv->v_type = VAR_STRING;
2556 tv->vval.v_string = vim_strnsave(
2557 str + idxtv->vval.v_number, clen);
2558 ++ectx->ec_stack.ga_len;
2559 idxtv->vval.v_number += clen - 1;
2560 }
2561 }
2562 else if (ltv->v_type == VAR_BLOB)
2563 {
2564 blob_T *blob = ltv->vval.v_blob;
2565
2566 // When we get here the first time make a copy of the
2567 // blob, so that the iteration still works when it is
2568 // changed.
2569 if (idxtv->vval.v_number == -1 && blob != NULL)
2570 {
2571 blob_copy(blob, ltv);
2572 blob_unref(blob);
2573 blob = ltv->vval.v_blob;
2574 }
2575
2576 // The index is for the previous byte.
2577 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002578 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002579 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002580 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002581 }
2582 else
2583 {
2584 // Push the next byte from the blob.
2585 tv = STACK_TV_BOT(0);
2586 tv->v_type = VAR_NUMBER;
2587 tv->vval.v_number = blob_get(blob,
2588 idxtv->vval.v_number);
2589 ++ectx->ec_stack.ga_len;
2590 }
2591 }
2592 else
2593 {
2594 semsg(_(e_for_loop_on_str_not_supported),
2595 vartype_name(ltv->v_type));
2596 return FAIL;
2597 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002598
2599 if (jump)
2600 {
2601 // past the end of the list/string/blob, jump to "endfor"
2602 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2603 may_restore_cmdmod(&ectx->ec_funclocal);
2604 }
2605 else
2606 {
2607 // Store the current number of funcrefs, this may be used in
2608 // ISN_LOOPEND. The variable index is always one more than the loop
2609 // variable index.
2610 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_idx + 1);
2611 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2612 }
2613
2614 return OK;
2615}
2616
2617/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002618 * Code for handling variables declared inside a loop and used in a closure.
2619 * This is very similar to what is done with funcstack_T. The difference is
2620 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2621 * scope of the block inside a loop and each loop may have its own.
2622 */
2623
2624// Double linked list of loopvars_T in use.
2625static loopvars_T *first_loopvars = NULL;
2626
2627 static void
2628add_loopvars_to_list(loopvars_T *loopvars)
2629{
2630 // Link in list of loopvarss.
2631 if (first_loopvars != NULL)
2632 first_loopvars->lvs_prev = loopvars;
2633 loopvars->lvs_next = first_loopvars;
2634 loopvars->lvs_prev = NULL;
2635 first_loopvars = loopvars;
2636}
2637
2638 static void
2639remove_loopvars_from_list(loopvars_T *loopvars)
2640{
2641 if (loopvars->lvs_prev == NULL)
2642 first_loopvars = loopvars->lvs_next;
2643 else
2644 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2645 if (loopvars->lvs_next != NULL)
2646 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2647}
2648
2649/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002650 * End of a for or while loop: Handle any variables used by a closure.
2651 */
2652 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002653execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002654{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002655 endloop_T *endloop = &iptr->isn_arg.endloop;
2656 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2657 int prev_closure_count = tv_refcount->vval.v_number;
2658 garray_T *gap = &ectx->ec_funcrefs;
2659 int closure_in_use = FALSE;
2660 loopvars_T *loopvars;
2661 typval_T *stack;
2662 int idx;
2663
2664 // Check if any created closure is still being referenced.
2665 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2666 {
2667 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2668
2669 if (pt->pt_refcount > 1)
2670 {
2671 int refcount = pt->pt_refcount;
2672 int i;
2673
2674 // A Reference in a variable inside the loop doesn't count, it gets
2675 // unreferenced at the end of the loop.
2676 for (i = 0; i < endloop->end_var_count; ++i)
2677 {
2678 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2679
2680 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2681 --refcount;
2682 }
2683 if (refcount > 1)
2684 {
2685 closure_in_use = TRUE;
2686 break;
2687 }
2688 }
2689 }
2690
2691 // If no function reference were created since the start of the loop block
2692 // or it is no longer referenced there is nothing to do.
2693 if (!closure_in_use)
2694 return OK;
2695
2696 // A closure is using variables declared inside the loop.
2697 // Move them to the called function.
2698 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2699 if (loopvars == NULL)
2700 return FAIL;
2701
2702 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2703 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2704 loopvars->lvs_ga.ga_data = stack;
2705 if (stack == NULL)
2706 {
2707 vim_free(loopvars);
2708 return FAIL;
2709 }
2710 add_loopvars_to_list(loopvars);
2711
2712 // Move the variable values.
2713 for (idx = 0; idx < endloop->end_var_count; ++idx)
2714 {
2715 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2716
2717 *(stack + idx) = *tv;
2718 tv->v_type = VAR_UNKNOWN;
2719 }
2720
2721 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2722 {
2723 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2724
2725 if (pt->pt_refcount > 1)
2726 {
2727 ++loopvars->lvs_refcount;
2728 pt->pt_loopvars = loopvars;
2729
2730 pt->pt_outer.out_loop_stack = &loopvars->lvs_ga;
2731 pt->pt_outer.out_loop_var_idx -= ectx->ec_frame_idx
2732 + STACK_FRAME_SIZE + endloop->end_var_idx;
2733 }
2734 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002735
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002736 return OK;
2737}
2738
2739/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002740 * Called when a partial is freed or its reference count goes down to one. The
2741 * loopvars may be the only reference to the partials in the local variables.
2742 * Go over all of them, the funcref and can be freed if all partials
2743 * referencing the loopvars have a reference count of one.
2744 */
2745 void
2746loopvars_check_refcount(loopvars_T *loopvars)
2747{
2748 int i;
2749 garray_T *gap = &loopvars->lvs_ga;
2750 int done = 0;
2751
2752 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2753 return;
2754 for (i = 0; i < gap->ga_len; ++i)
2755 {
2756 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2757
2758 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2759 && tv->vval.v_partial->pt_loopvars == loopvars
2760 && tv->vval.v_partial->pt_refcount == 1)
2761 ++done;
2762 }
2763 if (done == loopvars->lvs_min_refcount)
2764 {
2765 typval_T *stack = gap->ga_data;
2766
2767 // All partials referencing the loopvars have a reference count of
2768 // one, thus the loopvars is no longer of use.
2769 for (i = 0; i < gap->ga_len; ++i)
2770 clear_tv(stack + i);
2771 vim_free(stack);
2772 remove_loopvars_from_list(loopvars);
2773 vim_free(loopvars);
2774 }
2775}
2776
2777/*
2778 * For garbage collecting: set references in all variables referenced by
2779 * all loopvars.
2780 */
2781 int
2782set_ref_in_loopvars(int copyID)
2783{
2784 loopvars_T *loopvars;
2785
2786 for (loopvars = first_loopvars; loopvars != NULL;
2787 loopvars = loopvars->lvs_next)
2788 {
2789 typval_T *stack = loopvars->lvs_ga.ga_data;
2790 int i;
2791
2792 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2793 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2794 return TRUE; // abort
2795 }
2796 return FALSE;
2797}
2798
2799/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002800 * Load instruction for w:/b:/g:/t: variable.
2801 * "isn_type" is used instead of "iptr->isn_type".
2802 */
2803 static int
2804load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2805{
2806 dictitem_T *di = NULL;
2807 hashtab_T *ht = NULL;
2808 char namespace;
2809
2810 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2811 return NOTDONE;
2812 switch (isn_type)
2813 {
2814 case ISN_LOADG:
2815 ht = get_globvar_ht();
2816 namespace = 'g';
2817 break;
2818 case ISN_LOADB:
2819 ht = &curbuf->b_vars->dv_hashtab;
2820 namespace = 'b';
2821 break;
2822 case ISN_LOADW:
2823 ht = &curwin->w_vars->dv_hashtab;
2824 namespace = 'w';
2825 break;
2826 case ISN_LOADT:
2827 ht = &curtab->tp_vars->dv_hashtab;
2828 namespace = 't';
2829 break;
2830 default: // Cannot reach here
2831 return NOTDONE;
2832 }
2833 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2834
Bram Moolenaar06b77222022-01-25 15:51:56 +00002835 if (di == NULL)
2836 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002837 if (isn_type == ISN_LOADG)
2838 {
2839 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2840
2841 // g:Something could be a function
2842 if (ufunc != NULL)
2843 {
2844 typval_T *tv = STACK_TV_BOT(0);
2845
2846 ++ectx->ec_stack.ga_len;
2847 tv->v_type = VAR_FUNC;
2848 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2849 if (tv->vval.v_string == NULL)
2850 return FAIL;
2851 STRCPY(tv->vval.v_string, "g:");
2852 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2853 return OK;
2854 }
2855 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002856 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002857 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002858 // no check if the item exists in the script but
2859 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002860 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002861 else
2862 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002863 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002864 return FAIL;
2865 }
2866 else
2867 {
2868 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2869 ++ectx->ec_stack.ga_len;
2870 }
2871 return OK;
2872}
2873
2874/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002875 * Execute instructions in execution context "ectx".
2876 * Return OK or FAIL;
2877 */
2878 static int
2879exec_instructions(ectx_T *ectx)
2880{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002881 int ret = FAIL;
2882 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002883 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002884
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002885 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002886 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002887
Bram Moolenaarff652882021-05-16 15:24:49 +02002888 // Only catch exceptions in this instruction list.
2889 ectx->ec_trylevel_at_start = trylevel;
2890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 for (;;)
2892 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002893 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002895 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002896
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002897 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002898 {
2899 line_breakcheck();
2900 breakcheck_count = 0;
2901 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002902 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002903 {
2904 // Turn CTRL-C into an exception.
2905 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002906 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002907 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002908 did_throw = TRUE;
2909 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002911 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002912 {
2913 // Turn an error message into an exception.
2914 did_emsg = FALSE;
2915 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002916 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002917 did_throw = TRUE;
2918 *msg_list = NULL;
2919 }
2920
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002921 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002923 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002924 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002925 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926
2927 // An exception jumps to the first catch, finally, or returns from
2928 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002929 while (index > 0)
2930 {
2931 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002932 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002933 break;
2934 // In the catch and finally block of this try we have to go up
2935 // one level.
2936 --index;
2937 trycmd = NULL;
2938 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002939 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002941 if (trycmd->tcd_in_catch)
2942 {
2943 // exception inside ":catch", jump to ":finally" once
2944 ectx->ec_iidx = trycmd->tcd_finally_idx;
2945 trycmd->tcd_finally_idx = 0;
2946 }
2947 else
2948 // jump to first ":catch"
2949 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002950 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002951 did_throw = FALSE; // don't come back here until :endtry
2952 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 }
2954 else
2955 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002956 // Not inside try or need to return from current functions.
2957 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002958 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002959 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002960 tv = STACK_TV_BOT(0);
2961 tv->v_type = VAR_NUMBER;
2962 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002963 ++ectx->ec_stack.ga_len;
2964 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002966 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002967 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002968 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002969 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 goto done;
2971 }
2972
Bram Moolenaar4c137212021-04-19 16:48:48 +02002973 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002974 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 }
2976 continue;
2977 }
2978
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002979 /*
2980 * Big switch on the instruction. Most compilers will be turning this
2981 * into an efficient lookup table, since the "case" values are an enum
2982 * with sequential numbers. It may look ugly, but it should be the
2983 * most efficient way.
2984 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002985 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 switch (iptr->isn_type)
2987 {
2988 // execute Ex command line
2989 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002990 if (exec_command(iptr) == FAIL)
2991 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 break;
2993
Bram Moolenaar20677332021-06-06 17:02:53 +02002994 // execute Ex command line split at NL characters.
2995 case ISN_EXEC_SPLIT:
2996 {
2997 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002998 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002999
3000 SOURCING_LNUM = iptr->isn_lnum;
3001 CLEAR_FIELD(cookie);
3002 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3003 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003004 line = get_split_sourceline(0, &cookie, 0, 0);
3005 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003006 get_split_sourceline, &cookie,
3007 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3008 == FAIL
3009 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003010 {
3011 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003012 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003013 }
3014 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003015 }
3016 break;
3017
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003018 // execute Ex command line that is only a range
3019 case ISN_EXECRANGE:
3020 {
3021 exarg_T ea;
3022 char *error = NULL;
3023
3024 CLEAR_FIELD(ea);
3025 ea.cmdidx = CMD_SIZE;
3026 ea.addr_type = ADDR_LINES;
3027 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003028 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003029 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003030 if (ea.cmd == NULL)
3031 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003032 // error is always NULL when using ADDR_LINES
3033 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003034 if (error != NULL)
3035 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003036 emsg(error);
3037 goto on_error;
3038 }
3039 }
3040 break;
3041
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003042 // Evaluate an expression with legacy syntax, push it onto the
3043 // stack.
3044 case ISN_LEGACY_EVAL:
3045 {
3046 char_u *arg = iptr->isn_arg.string;
3047 int res;
3048 int save_flags = cmdmod.cmod_flags;
3049
Bram Moolenaar35578162021-08-02 19:10:38 +02003050 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003051 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003052 tv = STACK_TV_BOT(0);
3053 init_tv(tv);
3054 cmdmod.cmod_flags |= CMOD_LEGACY;
3055 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3056 cmdmod.cmod_flags = save_flags;
3057 if (res == FAIL)
3058 goto on_error;
3059 ++ectx->ec_stack.ga_len;
3060 }
3061 break;
3062
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003063 // push typeval VAR_INSTR with instructions to be executed
3064 case ISN_INSTR:
3065 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003066 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003067 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003068 tv = STACK_TV_BOT(0);
3069 tv->vval.v_instr = ALLOC_ONE(instr_T);
3070 if (tv->vval.v_instr == NULL)
3071 goto on_error;
3072 ++ectx->ec_stack.ga_len;
3073
3074 tv->v_type = VAR_INSTR;
3075 tv->vval.v_instr->instr_ectx = ectx;
3076 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3077 }
3078 break;
3079
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003080 case ISN_SOURCE:
3081 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003082 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003083
Bram Moolenaar89445512022-04-14 12:58:23 +01003084 SOURCING_LNUM = iptr->isn_lnum;
3085 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003086 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003087 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003088 }
3089 break;
3090
Bram Moolenaar4c137212021-04-19 16:48:48 +02003091 // execute :substitute with an expression
3092 case ISN_SUBSTITUTE:
3093 {
3094 subs_T *subs = &iptr->isn_arg.subs;
3095 source_cookie_T cookie;
3096 struct subs_expr_S *save_instr = substitute_instr;
3097 struct subs_expr_S subs_instr;
3098 int res;
3099
3100 subs_instr.subs_ectx = ectx;
3101 subs_instr.subs_instr = subs->subs_instr;
3102 subs_instr.subs_status = OK;
3103 substitute_instr = &subs_instr;
3104
3105 SOURCING_LNUM = iptr->isn_lnum;
3106 // This is very much like ISN_EXEC
3107 CLEAR_FIELD(cookie);
3108 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3109 res = do_cmdline(subs->subs_cmd,
3110 getsourceline, &cookie,
3111 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3112 substitute_instr = save_instr;
3113
3114 if (res == FAIL || did_emsg
3115 || subs_instr.subs_status == FAIL)
3116 goto on_error;
3117 }
3118 break;
3119
3120 case ISN_FINISH:
3121 goto done;
3122
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003123 case ISN_REDIRSTART:
3124 // create a dummy entry for var_redir_str()
3125 if (alloc_redir_lval() == FAIL)
3126 goto on_error;
3127
3128 // The output is stored in growarray "redir_ga" until
3129 // redirection ends.
3130 init_redir_ga();
3131 redir_vname = 1;
3132 break;
3133
3134 case ISN_REDIREND:
3135 {
3136 char_u *res = get_clear_redir_ga();
3137
3138 // End redirection, put redirected text on the stack.
3139 clear_redir_lval();
3140 redir_vname = 0;
3141
Bram Moolenaar35578162021-08-02 19:10:38 +02003142 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003143 {
3144 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003145 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003146 }
3147 tv = STACK_TV_BOT(0);
3148 tv->v_type = VAR_STRING;
3149 tv->vval.v_string = res;
3150 ++ectx->ec_stack.ga_len;
3151 }
3152 break;
3153
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003154 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003155#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003156 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003157 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3158 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003159 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003160#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003161 break;
3162
3163 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003164#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003165 {
3166 exarg_T ea;
3167 int res;
3168
3169 CLEAR_FIELD(ea);
3170 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3171 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3172 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3173 --ectx->ec_stack.ga_len;
3174 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003175 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003176 res = cexpr_core(&ea, tv);
3177 clear_tv(tv);
3178 if (res == FAIL)
3179 goto on_error;
3180 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003181#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003182 break;
3183
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003184 // execute Ex command from pieces on the stack
3185 case ISN_EXECCONCAT:
3186 {
3187 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003188 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003189 int pass;
3190 int i;
3191 char_u *cmd = NULL;
3192 char_u *str;
3193
3194 for (pass = 1; pass <= 2; ++pass)
3195 {
3196 for (i = 0; i < count; ++i)
3197 {
3198 tv = STACK_TV_BOT(i - count);
3199 str = tv->vval.v_string;
3200 if (str != NULL && *str != NUL)
3201 {
3202 if (pass == 2)
3203 STRCPY(cmd + len, str);
3204 len += STRLEN(str);
3205 }
3206 if (pass == 2)
3207 clear_tv(tv);
3208 }
3209 if (pass == 1)
3210 {
3211 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003212 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003213 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003214 len = 0;
3215 }
3216 }
3217
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003218 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003219 do_cmdline_cmd(cmd);
3220 vim_free(cmd);
3221 }
3222 break;
3223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 // execute :echo {string} ...
3225 case ISN_ECHO:
3226 {
3227 int count = iptr->isn_arg.echo.echo_count;
3228 int atstart = TRUE;
3229 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003230 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231
3232 for (idx = 0; idx < count; ++idx)
3233 {
3234 tv = STACK_TV_BOT(idx - count);
3235 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3236 &atstart, &needclr);
3237 clear_tv(tv);
3238 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003239 if (needclr)
3240 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003241 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 }
3243 break;
3244
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003245 // :execute {string} ...
3246 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003247 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003248 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003249 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003250 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003251 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003252 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003253 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003254 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003255 {
3256 int count = iptr->isn_arg.number;
3257 garray_T ga;
3258 char_u buf[NUMBUFLEN];
3259 char_u *p;
3260 int len;
3261 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003262 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003263
3264 ga_init2(&ga, 1, 80);
3265 for (idx = 0; idx < count; ++idx)
3266 {
3267 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003268 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003269 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003270 if (tv->v_type == VAR_CHANNEL
3271 || tv->v_type == VAR_JOB)
3272 {
3273 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003274 semsg(_(e_using_invalid_value_as_string_str),
3275 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003276 break;
3277 }
3278 else
3279 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003280 }
3281 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003282 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003283
3284 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003285 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003286 failed = TRUE;
3287 else
3288 {
3289 if (ga.ga_len > 0)
3290 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3291 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3292 ga.ga_len += len;
3293 }
3294 clear_tv(tv);
3295 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003296 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003297 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003298 {
3299 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003300 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003301 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003302
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003303 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003304 {
3305 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003306 {
3307 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003308 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003309 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003310 {
3311 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003312 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003313 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003314 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003315 else
3316 {
3317 msg_sb_eol();
3318 if (iptr->isn_type == ISN_ECHOMSG)
3319 {
3320 msg_attr(ga.ga_data, echo_attr);
3321 out_flush();
3322 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003323#ifdef HAS_MESSAGE_WINDOW
3324 else if (iptr->isn_type == ISN_ECHOWINDOW)
3325 {
3326 start_echowindow();
3327 msg_attr(ga.ga_data, echo_attr);
3328 end_echowindow();
3329 }
3330#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003331 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3332 {
3333 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3334 TRUE);
3335 ui_write((char_u *)"\r\n", 2, TRUE);
3336 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003337 else
3338 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003339 SOURCING_LNUM = iptr->isn_lnum;
3340 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003341 }
3342 }
3343 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003344 ga_clear(&ga);
3345 }
3346 break;
3347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 // load local variable or argument
3349 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003350 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003351 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003353 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 break;
3355
3356 // load v: variable
3357 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003358 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003359 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003361 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 break;
3363
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003364 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 case ISN_LOADSCRIPT:
3366 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003367 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 svar_T *sv;
3369
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003370 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003371 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003372 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003373 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003374 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003375 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003377 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 }
3379 break;
3380
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003381 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003383 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003385 int sid = iptr->isn_arg.loadstore.ls_sid;
3386 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003387 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 if (di == NULL)
3391 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003392 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003393 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003394 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 }
3396 else
3397 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003398 if (iptr->isn_type == ISN_LOADEXPORT)
3399 {
3400 int idx = get_script_item_idx(sid, name, 0,
3401 NULL, NULL);
3402 svar_T *sv;
3403
3404 if (idx >= 0)
3405 {
3406 sv = ((svar_T *)SCRIPT_ITEM(sid)
3407 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003408 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003409 {
3410 SOURCING_LNUM = iptr->isn_lnum;
3411 semsg(_(e_item_not_exported_in_script_str),
3412 name);
3413 goto on_error;
3414 }
3415 }
3416 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003417 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003418 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003420 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 }
3422 }
3423 break;
3424
Bram Moolenaard3aac292020-04-19 14:32:17 +02003425 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003427 case ISN_LOADB:
3428 case ISN_LOADW:
3429 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003431 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003432
Bram Moolenaar06b77222022-01-25 15:51:56 +00003433 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003434 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003435 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003436 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 break;
3440
Bram Moolenaar03290b82020-12-19 16:30:44 +01003441 // load autoload variable
3442 case ISN_LOADAUTO:
3443 {
3444 char_u *name = iptr->isn_arg.string;
3445
Bram Moolenaar35578162021-08-02 19:10:38 +02003446 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003447 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003448 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003449 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003450 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003451 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003452 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003453 }
3454 break;
3455
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003456 // load g:/b:/w:/t: namespace
3457 case ISN_LOADGDICT:
3458 case ISN_LOADBDICT:
3459 case ISN_LOADWDICT:
3460 case ISN_LOADTDICT:
3461 {
3462 dict_T *d = NULL;
3463
3464 switch (iptr->isn_type)
3465 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003466 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3467 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3468 case ISN_LOADWDICT: d = curwin->w_vars; break;
3469 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003470 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003471 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003472 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003473 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003474 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003475 tv = STACK_TV_BOT(0);
3476 tv->v_type = VAR_DICT;
3477 tv->v_lock = 0;
3478 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003479 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003480 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003481 }
3482 break;
3483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 // load &option
3485 case ISN_LOADOPT:
3486 {
3487 typval_T optval;
3488 char_u *name = iptr->isn_arg.string;
3489
Bram Moolenaara8c17702020-04-01 21:17:24 +02003490 // This is not expected to fail, name is checked during
3491 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003492 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003493 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003494 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003495 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003497 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 }
3499 break;
3500
3501 // load $ENV
3502 case ISN_LOADENV:
3503 {
3504 typval_T optval;
3505 char_u *name = iptr->isn_arg.string;
3506
Bram Moolenaar35578162021-08-02 19:10:38 +02003507 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003508 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003509 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003510 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003512 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 }
3514 break;
3515
3516 // load @register
3517 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003518 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003519 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 tv = STACK_TV_BOT(0);
3521 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003522 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003523 // This may result in NULL, which should be equivalent to an
3524 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 tv->vval.v_string = get_reg_contents(
3526 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003527 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 break;
3529
3530 // store local variable
3531 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003532 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 tv = STACK_TV_VAR(iptr->isn_arg.number);
3534 clear_tv(tv);
3535 *tv = *STACK_TV_BOT(0);
3536 break;
3537
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003538 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003539 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003540 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003541 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003542 int sid = iptr->isn_arg.loadstore.ls_sid;
3543 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003544 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003545 dictitem_T *di = find_var_in_ht(ht, 0,
3546 iptr->isn_type == ISN_STORES
3547 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003548
Bram Moolenaar4c137212021-04-19 16:48:48 +02003549 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003550 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003551 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003552 {
3553 if (iptr->isn_type == ISN_STOREEXPORT)
3554 {
3555 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003556 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003557 goto on_error;
3558 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003559 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003560 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003561 else
3562 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003563 if (iptr->isn_type == ISN_STOREEXPORT)
3564 {
3565 int idx = get_script_item_idx(sid, name, 0,
3566 NULL, NULL);
3567
Bram Moolenaar06651632022-04-27 17:54:25 +01003568 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003569 if (idx >= 0)
3570 {
3571 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3572 ->sn_var_vals.ga_data) + idx;
3573
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003574 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003575 {
3576 semsg(_(e_item_not_exported_in_script_str),
3577 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003578 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003579 goto on_error;
3580 }
3581 }
3582 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003583 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003584 {
3585 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003586 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003587 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003588 clear_tv(&di->di_tv);
3589 di->di_tv = *STACK_TV_BOT(0);
3590 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003591 }
3592 break;
3593
3594 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 case ISN_STORESCRIPT:
3596 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003597 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3598 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003600 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003601 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003602 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003603 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003604
3605 // "const" and "final" are checked at compile time, locking
3606 // the value needs to be checked here.
3607 SOURCING_LNUM = iptr->isn_lnum;
3608 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003609 {
3610 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003611 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003612 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 clear_tv(sv->sv_tv);
3615 *sv->sv_tv = *STACK_TV_BOT(0);
3616 }
3617 break;
3618
3619 // store option
3620 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003621 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003623 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3624 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 long n = 0;
3626 char_u *s = NULL;
3627 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003628 char_u numbuf[NUMBUFLEN];
3629 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630
Bram Moolenaar4c137212021-04-19 16:48:48 +02003631 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 tv = STACK_TV_BOT(0);
3633 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003634 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003636 if (s == NULL)
3637 s = (char_u *)"";
3638 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003639 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3640 {
3641 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003642 // If the option can be set to a function reference or
3643 // a lambda and the passed value is a function
3644 // reference, then convert it to the name (string) of
3645 // the function reference.
3646 s = tv2string(tv, &tofree, numbuf, 0);
3647 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003648 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003649 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003650 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003651 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003652 goto on_error;
3653 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003656 // must be VAR_NUMBER, CHECKTYPE makes sure
3657 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003658 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003659 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003660 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 if (msg != NULL)
3662 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003663 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003665 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 }
3668 break;
3669
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003670 // store $ENV
3671 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003672 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003673 tv = STACK_TV_BOT(0);
3674 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3675 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003676 break;
3677
3678 // store @r
3679 case ISN_STOREREG:
3680 {
3681 int reg = iptr->isn_arg.number;
3682
Bram Moolenaar4c137212021-04-19 16:48:48 +02003683 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003684 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003685 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003686 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003687 }
3688 break;
3689
3690 // store v: variable
3691 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003692 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003693 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3694 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003695 // should not happen, type is checked when compiling
3696 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003697 break;
3698
Bram Moolenaard3aac292020-04-19 14:32:17 +02003699 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003701 case ISN_STOREB:
3702 case ISN_STOREW:
3703 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003705 dictitem_T *di;
3706 hashtab_T *ht;
3707 char_u *name = iptr->isn_arg.string + 2;
3708
Bram Moolenaard3aac292020-04-19 14:32:17 +02003709 switch (iptr->isn_type)
3710 {
3711 case ISN_STOREG:
3712 ht = get_globvar_ht();
3713 break;
3714 case ISN_STOREB:
3715 ht = &curbuf->b_vars->dv_hashtab;
3716 break;
3717 case ISN_STOREW:
3718 ht = &curwin->w_vars->dv_hashtab;
3719 break;
3720 case ISN_STORET:
3721 ht = &curtab->tp_vars->dv_hashtab;
3722 break;
3723 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003724 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726
Bram Moolenaar4c137212021-04-19 16:48:48 +02003727 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003728 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003730 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 else
3732 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003733 SOURCING_LNUM = iptr->isn_lnum;
3734 if (var_check_permission(di, name) == FAIL)
3735 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 clear_tv(&di->di_tv);
3737 di->di_tv = *STACK_TV_BOT(0);
3738 }
3739 }
3740 break;
3741
Bram Moolenaar03290b82020-12-19 16:30:44 +01003742 // store an autoload variable
3743 case ISN_STOREAUTO:
3744 SOURCING_LNUM = iptr->isn_lnum;
3745 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3746 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003747 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003748 break;
3749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 // store number in local variable
3751 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003752 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 clear_tv(tv);
3754 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003755 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 break;
3757
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003758 // store value in list or dict variable
3759 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003760 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003761 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003762
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003763 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003764 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003765 if (res == NOTDONE)
3766 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003767 }
3768 break;
3769
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003770 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003771 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003772 if (execute_storerange(iptr, ectx) == FAIL)
3773 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003774 break;
3775
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003776 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003777 case ISN_LOADOUTER:
3778 case ISN_STOREOUTER:
3779 {
3780 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003781 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3782 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003783
3784 while (depth > 1 && outer != NULL)
3785 {
3786 outer = outer->out_up;
3787 --depth;
3788 }
3789 if (outer == NULL)
3790 {
3791 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003792 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3793 || ectx->ec_outer_ref == NULL)
3794 // Possibly :def function called from legacy
3795 // context.
3796 emsg(_(e_closure_called_from_invalid_context));
3797 else
3798 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003799 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003800 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003801 if (depth == OUTER_LOOP_DEPTH)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003802 // Variable declared in loop. May be copied if the
3803 // loop block has already ended.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003804 tv = ((typval_T *)outer->out_loop_stack->ga_data)
3805 + outer->out_loop_var_idx
3806 + iptr->isn_arg.outer.outer_idx;
3807 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003808 // Variable declared in a function. May be copied if
3809 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003810 tv = ((typval_T *)outer->out_stack->ga_data)
3811 + outer->out_frame_idx + STACK_FRAME_SIZE
3812 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003813 if (iptr->isn_type == ISN_LOADOUTER)
3814 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003815 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003816 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003817 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003818 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003819 }
3820 else
3821 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003822 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003823 clear_tv(tv);
3824 *tv = *STACK_TV_BOT(0);
3825 }
3826 }
3827 break;
3828
Bram Moolenaar752fc692021-01-04 21:57:11 +01003829 // unlet item in list or dict variable
3830 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003831 if (execute_unletindex(iptr, ectx) == FAIL)
3832 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003833 break;
3834
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003835 // unlet range of items in list variable
3836 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003837 if (execute_unletrange(iptr, ectx) == FAIL)
3838 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003839 break;
3840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 // push constant
3842 case ISN_PUSHNR:
3843 case ISN_PUSHBOOL:
3844 case ISN_PUSHSPEC:
3845 case ISN_PUSHF:
3846 case ISN_PUSHS:
3847 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003848 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003849 case ISN_PUSHCHANNEL:
3850 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003851 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003852 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003854 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003855 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 switch (iptr->isn_type)
3857 {
3858 case ISN_PUSHNR:
3859 tv->v_type = VAR_NUMBER;
3860 tv->vval.v_number = iptr->isn_arg.number;
3861 break;
3862 case ISN_PUSHBOOL:
3863 tv->v_type = VAR_BOOL;
3864 tv->vval.v_number = iptr->isn_arg.number;
3865 break;
3866 case ISN_PUSHSPEC:
3867 tv->v_type = VAR_SPECIAL;
3868 tv->vval.v_number = iptr->isn_arg.number;
3869 break;
3870#ifdef FEAT_FLOAT
3871 case ISN_PUSHF:
3872 tv->v_type = VAR_FLOAT;
3873 tv->vval.v_float = iptr->isn_arg.fnumber;
3874 break;
3875#endif
3876 case ISN_PUSHBLOB:
3877 blob_copy(iptr->isn_arg.blob, tv);
3878 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003879 case ISN_PUSHFUNC:
3880 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003881 if (iptr->isn_arg.string == NULL)
3882 tv->vval.v_string = NULL;
3883 else
3884 tv->vval.v_string =
3885 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003886 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003887 case ISN_PUSHCHANNEL:
3888#ifdef FEAT_JOB_CHANNEL
3889 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003890 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003891#endif
3892 break;
3893 case ISN_PUSHJOB:
3894#ifdef FEAT_JOB_CHANNEL
3895 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003896 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003897#endif
3898 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 default:
3900 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003901 tv->vval.v_string = iptr->isn_arg.string == NULL
3902 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 }
3904 break;
3905
Bram Moolenaar06b77222022-01-25 15:51:56 +00003906 case ISN_AUTOLOAD:
3907 {
3908 char_u *name = iptr->isn_arg.string;
3909
3910 (void)script_autoload(name, FALSE);
3911 if (find_func(name, TRUE))
3912 {
3913 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3914 goto theend;
3915 tv = STACK_TV_BOT(0);
3916 tv->v_lock = 0;
3917 ++ectx->ec_stack.ga_len;
3918 tv->v_type = VAR_FUNC;
3919 tv->vval.v_string = vim_strsave(name);
3920 }
3921 else
3922 {
3923 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3924
3925 if (res == NOTDONE)
3926 goto theend;
3927 if (res == FAIL)
3928 goto on_error;
3929 }
3930 }
3931 break;
3932
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003933 case ISN_UNLET:
3934 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3935 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003936 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003937 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003938 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003939 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003940 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003941
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003942 case ISN_LOCKUNLOCK:
3943 {
3944 typval_T *lval_root_save = lval_root;
3945 int res;
3946
3947 // Stack has the local variable, argument the whole :lock
3948 // or :unlock command, like ISN_EXEC.
3949 --ectx->ec_stack.ga_len;
3950 lval_root = STACK_TV_BOT(0);
3951 res = exec_command(iptr);
3952 clear_tv(lval_root);
3953 lval_root = lval_root_save;
3954 if (res == FAIL)
3955 goto on_error;
3956 }
3957 break;
3958
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003959 case ISN_LOCKCONST:
3960 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3961 break;
3962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 // create a list from items on the stack; uses a single allocation
3964 // for the list header and the items
3965 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003966 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003967 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 break;
3969
3970 // create a dict from items on the stack
3971 case ISN_NEWDICT:
3972 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003973 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003975 SOURCING_LNUM = iptr->isn_lnum;
3976 res = exe_newdict(iptr->isn_arg.number, ectx);
3977 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003978 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003979 if (res == MAYBE)
3980 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 }
3982 break;
3983
LemonBoy372bcce2022-04-25 12:43:20 +01003984 case ISN_CONCAT:
3985 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3986 goto theend;
3987 break;
3988
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003989 // create a partial with NULL value
3990 case ISN_NEWPARTIAL:
3991 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3992 goto theend;
3993 ++ectx->ec_stack.ga_len;
3994 tv = STACK_TV_BOT(-1);
3995 tv->v_type = VAR_PARTIAL;
3996 tv->v_lock = 0;
3997 tv->vval.v_partial = NULL;
3998 break;
3999
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 // call a :def function
4001 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004002 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004003 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4004 NULL,
4005 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004006 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004007 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 break;
4009
4010 // call a builtin function
4011 case ISN_BCALL:
4012 SOURCING_LNUM = iptr->isn_lnum;
4013 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4014 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004015 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004016 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 break;
4018
4019 // call a funcref or partial
4020 case ISN_PCALL:
4021 {
4022 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4023 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004024 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025
4026 SOURCING_LNUM = iptr->isn_lnum;
4027 if (pfunc->cpf_top)
4028 {
4029 // funcref is above the arguments
4030 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4031 }
4032 else
4033 {
4034 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004035 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004036 partial_tv = *STACK_TV_BOT(0);
4037 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004039 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004040 if (tv == &partial_tv)
4041 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004043 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 }
4045 break;
4046
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004047 case ISN_PCALL_END:
4048 // PCALL finished, arguments have been consumed and replaced by
4049 // the return value. Now clear the funcref from the stack,
4050 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004051 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004052 clear_tv(STACK_TV_BOT(-1));
4053 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4054 break;
4055
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 // call a user defined function or funcref/partial
4057 case ISN_UCALL:
4058 {
4059 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4060
4061 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004062 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004063 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004064 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 }
4066 break;
4067
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004068 // :defer func(arg)
4069 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004070 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004071 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4072 goto on_error;
4073 break;
4074
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004075 // return from a :def function call without a value
4076 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02004077 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004078 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004079 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004080 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004081 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004082 tv->vval.v_number = 0;
4083 tv->v_lock = 0;
4084 // FALLTHROUGH
4085
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004086 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 case ISN_RETURN:
4088 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004089 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004090 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004091
4092 if (trystack->ga_len > 0)
4093 trycmd = ((trycmd_T *)trystack->ga_data)
4094 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004095 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004096 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004098 // jump to ":finally" or ":endtry"
4099 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004100 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004101 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004102 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 trycmd->tcd_return = TRUE;
4104 }
4105 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004106 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 }
4108 break;
4109
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004110 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 case ISN_FUNCREF:
4112 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004113 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4114 ufunc_T *ufunc;
4115 funcref_T *funcref = &iptr->isn_arg.funcref;
4116 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004119 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004120 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004121 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004122 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004123 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004124 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004125 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004126 {
4127 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4128 + funcref->fr_dfunc_idx;
4129
4130 ufunc = pt_dfunc->df_ufunc;
4131 }
4132 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004133 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004134 if (ufunc == NULL)
4135 {
4136 SOURCING_LNUM = iptr->isn_lnum;
4137 iemsg("ufunc unexpectedly NULL for FUNCREF");
4138 goto theend;
4139 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004140 if (fill_partial_and_closure(pt, ufunc,
4141 extra == NULL ? 0 : extra->fre_loop_var_idx,
4142 extra == NULL ? 0 : extra->fre_loop_var_count,
4143 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004144 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004146 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 tv->vval.v_partial = pt;
4148 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004149 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 }
4151 break;
4152
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004153 // Create a global function from a lambda.
4154 case ISN_NEWFUNC:
4155 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004156 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004157
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004158 if (copy_lambda_to_global_func(arg->nfa_lambda,
4159 arg->nfa_global, arg->nfa_loop_var_idx,
4160 arg->nfa_loop_var_count, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004161 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004162 }
4163 break;
4164
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004165 // List functions
4166 case ISN_DEF:
4167 if (iptr->isn_arg.string == NULL)
4168 list_functions(NULL);
4169 else
4170 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004171 exarg_T ea;
4172 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004173
4174 CLEAR_FIELD(ea);
4175 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004176 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4177 define_function(&ea, NULL, &lines_to_free);
4178 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004179 }
4180 break;
4181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 // jump if a condition is met
4183 case ISN_JUMP:
4184 {
4185 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004186 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 int jump = TRUE;
4188
4189 if (when != JUMP_ALWAYS)
4190 {
4191 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004192 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004193 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004194 || when == JUMP_IF_COND_TRUE)
4195 {
4196 SOURCING_LNUM = iptr->isn_lnum;
4197 jump = tv_get_bool_chk(tv, &error);
4198 if (error)
4199 goto on_error;
4200 }
4201 else
4202 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004203 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004205 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 {
4207 // drop the value from the stack
4208 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004209 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 }
4211 }
4212 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004213 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 }
4215 break;
4216
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004217 // "while": jump to end if a condition is false
4218 case ISN_WHILE:
4219 {
4220 int error = FALSE;
4221 int jump = TRUE;
4222
4223 tv = STACK_TV_BOT(-1);
4224 SOURCING_LNUM = iptr->isn_lnum;
4225 jump = !tv_get_bool_chk(tv, &error);
4226 if (error)
4227 goto on_error;
4228 // drop the value from the stack
4229 clear_tv(tv);
4230 --ectx->ec_stack.ga_len;
4231 if (jump)
4232 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4233
4234 // Store the current funccal count, may be used by
4235 // ISN_LOOPEND later
4236 tv = STACK_TV_VAR(
4237 iptr->isn_arg.whileloop.while_funcref_idx);
4238 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4239 }
4240 break;
4241
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004242 // Jump if an argument with a default value was already set and not
4243 // v:none.
4244 case ISN_JUMP_IF_ARG_SET:
4245 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
4246 if (tv->v_type != VAR_UNKNOWN
4247 && !(tv->v_type == VAR_SPECIAL
4248 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004249 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004250 break;
4251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 // top of a for loop
4253 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004254 if (execute_for(iptr, ectx) == FAIL)
4255 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 break;
4257
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004258 // end of a for or while loop
4259 case ISN_ENDLOOP:
4260 if (execute_endloop(iptr, ectx) == FAIL)
4261 goto theend;
4262 break;
4263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 // start of ":try" block
4265 case ISN_TRY:
4266 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004267 trycmd_T *trycmd = NULL;
4268
Bram Moolenaar35578162021-08-02 19:10:38 +02004269 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004270 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004271 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4272 + ectx->ec_trystack.ga_len;
4273 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004275 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004276 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4277 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004278 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004279 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004280 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004281 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004282 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004283 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 }
4285 break;
4286
4287 case ISN_PUSHEXC:
4288 if (current_exception == NULL)
4289 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004290 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004292 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004294 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004295 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004297 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004299 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 tv->vval.v_string = vim_strsave(
4301 (char_u *)current_exception->value);
4302 break;
4303
4304 case ISN_CATCH:
4305 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004306 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004307 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308
Bram Moolenaar4c137212021-04-19 16:48:48 +02004309 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004310 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004312 trycmd->tcd_caught = TRUE;
4313 trycmd->tcd_did_throw = FALSE;
4314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004316 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 catch_exception(current_exception);
4318 }
4319 break;
4320
Bram Moolenaarc150c092021-02-13 15:02:46 +01004321 case ISN_TRYCONT:
4322 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004323 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004324 trycont_T *trycont = &iptr->isn_arg.trycont;
4325 int i;
4326 trycmd_T *trycmd;
4327 int iidx = trycont->tct_where;
4328
4329 if (trystack->ga_len < trycont->tct_levels)
4330 {
4331 siemsg("TRYCONT: expected %d levels, found %d",
4332 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004333 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004334 }
4335 // Make :endtry jump to any outer try block and the last
4336 // :endtry inside the loop to the loop start.
4337 for (i = trycont->tct_levels; i > 0; --i)
4338 {
4339 trycmd = ((trycmd_T *)trystack->ga_data)
4340 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004341 // Add one to tcd_cont to be able to jump to
4342 // instruction with index zero.
4343 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004344 iidx = trycmd->tcd_finally_idx == 0
4345 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004346 }
4347 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004348 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004349 }
4350 break;
4351
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004352 case ISN_FINALLY:
4353 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004354 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004355 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4356 + trystack->ga_len - 1;
4357
4358 // Reset the index to avoid a return statement jumps here
4359 // again.
4360 trycmd->tcd_finally_idx = 0;
4361 break;
4362 }
4363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 // end of ":try" block
4365 case ISN_ENDTRY:
4366 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004367 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004368 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369
Bram Moolenaar06651632022-04-27 17:54:25 +01004370 --trystack->ga_len;
4371 --trylevel;
4372 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4373 if (trycmd->tcd_did_throw)
4374 did_throw = TRUE;
4375 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004377 // discard the exception
4378 if (caught_stack == current_exception)
4379 caught_stack = caught_stack->caught;
4380 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004382
4383 if (trycmd->tcd_return)
4384 goto func_return;
4385
4386 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4387 {
4388 --ectx->ec_stack.ga_len;
4389 clear_tv(STACK_TV_BOT(0));
4390 }
4391 if (trycmd->tcd_cont != 0)
4392 // handling :continue: jump to outer try block or
4393 // start of the loop
4394 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 }
4396 break;
4397
4398 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004399 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004400 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004401
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004402 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4403 {
4404 // throwing an exception while using "silent!" causes
4405 // the function to abort but not display an error.
4406 tv = STACK_TV_BOT(-1);
4407 clear_tv(tv);
4408 tv->v_type = VAR_NUMBER;
4409 tv->vval.v_number = 0;
4410 goto done;
4411 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004412 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004413 tv = STACK_TV_BOT(0);
4414 if (tv->vval.v_string == NULL
4415 || *skipwhite(tv->vval.v_string) == NUL)
4416 {
4417 vim_free(tv->vval.v_string);
4418 SOURCING_LNUM = iptr->isn_lnum;
4419 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004420 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004421 }
4422
4423 // Inside a "catch" we need to first discard the caught
4424 // exception.
4425 if (trystack->ga_len > 0)
4426 {
4427 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4428 + trystack->ga_len - 1;
4429 if (trycmd->tcd_caught && current_exception != NULL)
4430 {
4431 // discard the exception
4432 if (caught_stack == current_exception)
4433 caught_stack = caught_stack->caught;
4434 discard_current_exception();
4435 trycmd->tcd_caught = FALSE;
4436 }
4437 }
4438
Bram Moolenaar90a57162022-02-12 14:23:17 +00004439 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004440 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4441 == FAIL)
4442 {
4443 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004444 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004445 }
4446 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 break;
4449
4450 // compare with special values
4451 case ISN_COMPAREBOOL:
4452 case ISN_COMPARESPECIAL:
4453 {
4454 typval_T *tv1 = STACK_TV_BOT(-2);
4455 typval_T *tv2 = STACK_TV_BOT(-1);
4456 varnumber_T arg1 = tv1->vval.v_number;
4457 varnumber_T arg2 = tv2->vval.v_number;
4458 int res;
4459
Bram Moolenaar06651632022-04-27 17:54:25 +01004460 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4461 res = arg1 == arg2;
4462 else
4463 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464
Bram Moolenaar4c137212021-04-19 16:48:48 +02004465 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 tv1->v_type = VAR_BOOL;
4467 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4468 }
4469 break;
4470
Bram Moolenaar7a222242022-03-01 19:23:24 +00004471 case ISN_COMPARENULL:
4472 {
4473 typval_T *tv1 = STACK_TV_BOT(-2);
4474 typval_T *tv2 = STACK_TV_BOT(-1);
4475 int res;
4476
4477 res = typval_compare_null(tv1, tv2);
4478 if (res == MAYBE)
4479 goto on_error;
4480 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4481 res = !res;
4482 clear_tv(tv1);
4483 clear_tv(tv2);
4484 --ectx->ec_stack.ga_len;
4485 tv1->v_type = VAR_BOOL;
4486 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4487 }
4488 break;
4489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 // Operation with two number arguments
4491 case ISN_OPNR:
4492 case ISN_COMPARENR:
4493 {
4494 typval_T *tv1 = STACK_TV_BOT(-2);
4495 typval_T *tv2 = STACK_TV_BOT(-1);
4496 varnumber_T arg1 = tv1->vval.v_number;
4497 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004498 varnumber_T res = 0;
4499 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004501 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4502 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4503 {
4504 if (arg2 < 0)
4505 {
4506 SOURCING_LNUM = iptr->isn_lnum;
4507 emsg(_(e_bitshift_ops_must_be_postive));
4508 goto on_error;
4509 }
4510 }
4511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 switch (iptr->isn_arg.op.op_type)
4513 {
4514 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004515 case EXPR_DIV: if (arg2 == 0)
4516 div_zero = TRUE;
4517 else
4518 res = arg1 / arg2;
4519 break;
4520 case EXPR_REM: if (arg2 == 0)
4521 div_zero = TRUE;
4522 else
4523 res = arg1 % arg2;
4524 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 case EXPR_SUB: res = arg1 - arg2; break;
4526 case EXPR_ADD: res = arg1 + arg2; break;
4527
4528 case EXPR_EQUAL: res = arg1 == arg2; break;
4529 case EXPR_NEQUAL: res = arg1 != arg2; break;
4530 case EXPR_GREATER: res = arg1 > arg2; break;
4531 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4532 case EXPR_SMALLER: res = arg1 < arg2; break;
4533 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004534 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4535 res = 0;
4536 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004537 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004538 break;
4539 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4540 res = 0;
4541 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004542 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004543 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004544 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 }
4546
Bram Moolenaar4c137212021-04-19 16:48:48 +02004547 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 if (iptr->isn_type == ISN_COMPARENR)
4549 {
4550 tv1->v_type = VAR_BOOL;
4551 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4552 }
4553 else
4554 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004555 if (div_zero)
4556 {
4557 SOURCING_LNUM = iptr->isn_lnum;
4558 emsg(_(e_divide_by_zero));
4559 goto on_error;
4560 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 }
4562 break;
4563
4564 // Computation with two float arguments
4565 case ISN_OPFLOAT:
4566 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004567#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 {
4569 typval_T *tv1 = STACK_TV_BOT(-2);
4570 typval_T *tv2 = STACK_TV_BOT(-1);
4571 float_T arg1 = tv1->vval.v_float;
4572 float_T arg2 = tv2->vval.v_float;
4573 float_T res = 0;
4574 int cmp = FALSE;
4575
4576 switch (iptr->isn_arg.op.op_type)
4577 {
4578 case EXPR_MULT: res = arg1 * arg2; break;
4579 case EXPR_DIV: res = arg1 / arg2; break;
4580 case EXPR_SUB: res = arg1 - arg2; break;
4581 case EXPR_ADD: res = arg1 + arg2; break;
4582
4583 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4584 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4585 case EXPR_GREATER: cmp = arg1 > arg2; break;
4586 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4587 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4588 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4589 default: cmp = 0; break;
4590 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004591 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 if (iptr->isn_type == ISN_COMPAREFLOAT)
4593 {
4594 tv1->v_type = VAR_BOOL;
4595 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4596 }
4597 else
4598 tv1->vval.v_float = res;
4599 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004600#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 break;
4602
4603 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004604 case ISN_COMPAREDICT:
4605 case ISN_COMPAREFUNC:
4606 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 case ISN_COMPAREBLOB:
4608 {
4609 typval_T *tv1 = STACK_TV_BOT(-2);
4610 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004611 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4612 int ic = iptr->isn_arg.op.op_ic;
4613 int res = FALSE;
4614 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615
Bram Moolenaar265f8112021-12-19 12:33:05 +00004616 SOURCING_LNUM = iptr->isn_lnum;
4617 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004619 status = typval_compare_list(tv1, tv2,
4620 exprtype, ic, &res);
4621 }
4622 else if (iptr->isn_type == ISN_COMPAREDICT)
4623 {
4624 status = typval_compare_dict(tv1, tv2,
4625 exprtype, ic, &res);
4626 }
4627 else if (iptr->isn_type == ISN_COMPAREFUNC)
4628 {
4629 status = typval_compare_func(tv1, tv2,
4630 exprtype, ic, &res);
4631 }
4632 else if (iptr->isn_type == ISN_COMPARESTRING)
4633 {
4634 status = typval_compare_string(tv1, tv2,
4635 exprtype, ic, &res);
4636 }
4637 else
4638 {
4639 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004641 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 clear_tv(tv1);
4643 clear_tv(tv2);
4644 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004645 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4646 if (status == FAIL)
4647 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 }
4649 break;
4650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 case ISN_COMPAREANY:
4652 {
4653 typval_T *tv1 = STACK_TV_BOT(-2);
4654 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004655 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004657 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658
Bram Moolenaareb26f432020-09-14 16:50:05 +02004659 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004660 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004662 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004663 if (status == FAIL)
4664 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 }
4666 break;
4667
4668 case ISN_ADDLIST:
4669 case ISN_ADDBLOB:
4670 {
4671 typval_T *tv1 = STACK_TV_BOT(-2);
4672 typval_T *tv2 = STACK_TV_BOT(-1);
4673
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004674 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004676 {
4677 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4678 && tv1->vval.v_list != NULL)
4679 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4680 NULL);
4681 else
4682 eval_addlist(tv1, tv2);
4683 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 else
4685 eval_addblob(tv1, tv2);
4686 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004687 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 }
4689 break;
4690
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004691 case ISN_LISTAPPEND:
4692 {
4693 typval_T *tv1 = STACK_TV_BOT(-2);
4694 typval_T *tv2 = STACK_TV_BOT(-1);
4695 list_T *l = tv1->vval.v_list;
4696
4697 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004698 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004699 if (l == NULL)
4700 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004701 emsg(_(e_cannot_add_to_null_list));
4702 goto on_error;
4703 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004704 if (value_check_lock(l->lv_lock, NULL, FALSE))
4705 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004706 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004707 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004708 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004709 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004710 }
4711 break;
4712
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004713 case ISN_BLOBAPPEND:
4714 {
4715 typval_T *tv1 = STACK_TV_BOT(-2);
4716 typval_T *tv2 = STACK_TV_BOT(-1);
4717 blob_T *b = tv1->vval.v_blob;
4718 int error = FALSE;
4719 varnumber_T n;
4720
4721 // add a number to a blob
4722 if (b == NULL)
4723 {
4724 SOURCING_LNUM = iptr->isn_lnum;
4725 emsg(_(e_cannot_add_to_null_blob));
4726 goto on_error;
4727 }
4728 n = tv_get_number_chk(tv2, &error);
4729 if (error)
4730 goto on_error;
4731 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004732 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004733 }
4734 break;
4735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 // Computation with two arguments of unknown type
4737 case ISN_OPANY:
4738 {
4739 typval_T *tv1 = STACK_TV_BOT(-2);
4740 typval_T *tv2 = STACK_TV_BOT(-1);
4741 varnumber_T n1, n2;
4742#ifdef FEAT_FLOAT
4743 float_T f1 = 0, f2 = 0;
4744#endif
4745 int error = FALSE;
4746
4747 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4748 {
4749 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4750 {
4751 eval_addlist(tv1, tv2);
4752 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004753 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754 break;
4755 }
4756 else if (tv1->v_type == VAR_BLOB
4757 && tv2->v_type == VAR_BLOB)
4758 {
4759 eval_addblob(tv1, tv2);
4760 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004761 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 break;
4763 }
4764 }
4765#ifdef FEAT_FLOAT
4766 if (tv1->v_type == VAR_FLOAT)
4767 {
4768 f1 = tv1->vval.v_float;
4769 n1 = 0;
4770 }
4771 else
4772#endif
4773 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004774 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 n1 = tv_get_number_chk(tv1, &error);
4776 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004777 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778#ifdef FEAT_FLOAT
4779 if (tv2->v_type == VAR_FLOAT)
4780 f1 = n1;
4781#endif
4782 }
4783#ifdef FEAT_FLOAT
4784 if (tv2->v_type == VAR_FLOAT)
4785 {
4786 f2 = tv2->vval.v_float;
4787 n2 = 0;
4788 }
4789 else
4790#endif
4791 {
4792 n2 = tv_get_number_chk(tv2, &error);
4793 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004794 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795#ifdef FEAT_FLOAT
4796 if (tv1->v_type == VAR_FLOAT)
4797 f2 = n2;
4798#endif
4799 }
4800#ifdef FEAT_FLOAT
4801 // if there is a float on either side the result is a float
4802 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4803 {
4804 switch (iptr->isn_arg.op.op_type)
4805 {
4806 case EXPR_MULT: f1 = f1 * f2; break;
4807 case EXPR_DIV: f1 = f1 / f2; break;
4808 case EXPR_SUB: f1 = f1 - f2; break;
4809 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004810 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004811 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004812 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004813 }
4814 clear_tv(tv1);
4815 clear_tv(tv2);
4816 tv1->v_type = VAR_FLOAT;
4817 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004818 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819 }
4820 else
4821#endif
4822 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004823 int failed = FALSE;
4824
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 switch (iptr->isn_arg.op.op_type)
4826 {
4827 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004828 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4829 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004830 goto on_error;
4831 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 case EXPR_SUB: n1 = n1 - n2; break;
4833 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004834 default: n1 = num_modulus(n1, n2, &failed);
4835 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004836 goto on_error;
4837 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 }
4839 clear_tv(tv1);
4840 clear_tv(tv2);
4841 tv1->v_type = VAR_NUMBER;
4842 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004843 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 }
4845 }
4846 break;
4847
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004848 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004849 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004850 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004851 int is_slice = iptr->isn_type == ISN_STRSLICE;
4852 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004853 char_u *res;
4854
4855 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004856 // string slice: string is at stack-3, first index at
4857 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004858 if (is_slice)
4859 {
4860 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004861 n1 = tv->vval.v_number;
4862 }
4863
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004864 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004865 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004866
Bram Moolenaar4c137212021-04-19 16:48:48 +02004867 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004868 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004869 if (is_slice)
4870 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004871 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004872 else
4873 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004874 // single character (including composing characters).
4875 // If the index is too big or negative the result is
4876 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004877 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004878 vim_free(tv->vval.v_string);
4879 tv->vval.v_string = res;
4880 }
4881 break;
4882
4883 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004884 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004885 case ISN_BLOBINDEX:
4886 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004888 int is_slice = iptr->isn_type == ISN_LISTSLICE
4889 || iptr->isn_type == ISN_BLOBSLICE;
4890 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4891 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004892 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004893 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894
4895 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004896 // list slice: list is at stack-3, indexes at stack-2 and
4897 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004898 // Same for blob.
4899 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900
4901 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004902 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004904
4905 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906 {
Bram Moolenaared591872020-08-15 22:14:53 +02004907 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004908 n1 = tv->vval.v_number;
4909 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 }
Bram Moolenaared591872020-08-15 22:14:53 +02004911
Bram Moolenaar4c137212021-04-19 16:48:48 +02004912 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004913 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004914 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004915 if (is_blob)
4916 {
4917 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4918 n1, n2, FALSE, tv) == FAIL)
4919 goto on_error;
4920 }
4921 else
4922 {
4923 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4924 n1, n2, FALSE, tv, TRUE) == FAIL)
4925 goto on_error;
4926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004927 }
4928 break;
4929
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004930 case ISN_ANYINDEX:
4931 case ISN_ANYSLICE:
4932 {
4933 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4934 typval_T *var1, *var2;
4935 int res;
4936
4937 // index: composite is at stack-2, index at stack-1
4938 // slice: composite is at stack-3, indexes at stack-2 and
4939 // stack-1
4940 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004941 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004942 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4943 goto on_error;
4944 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4945 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004946 res = eval_index_inner(tv, is_slice, var1, var2,
4947 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004948 clear_tv(var1);
4949 if (is_slice)
4950 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004951 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004952 if (res == FAIL)
4953 goto on_error;
4954 }
4955 break;
4956
Bram Moolenaar9af78762020-06-16 11:34:42 +02004957 case ISN_SLICE:
4958 {
4959 list_T *list;
4960 int count = iptr->isn_arg.number;
4961
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004962 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004963 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004964 list = tv->vval.v_list;
4965
4966 // no error for short list, expect it to be checked earlier
4967 if (list != NULL && list->lv_len >= count)
4968 {
4969 list_T *newlist = list_slice(list,
4970 count, list->lv_len - 1);
4971
4972 if (newlist != NULL)
4973 {
4974 list_unref(list);
4975 tv->vval.v_list = newlist;
4976 ++newlist->lv_refcount;
4977 }
4978 }
4979 }
4980 break;
4981
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004982 case ISN_GETITEM:
4983 {
4984 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004985 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004986
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004987 // Get list item: list is at stack-1, push item.
4988 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004989 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4990 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004991
Bram Moolenaar35578162021-08-02 19:10:38 +02004992 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004993 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004994 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004995 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004996
4997 // Useful when used in unpack assignment. Reset at
4998 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004999 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005000 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005001 }
5002 break;
5003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 case ISN_MEMBER:
5005 {
5006 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005007 char_u *key;
5008 dictitem_T *di;
5009
5010 // dict member: dict is at stack-2, key at stack-1
5011 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005012 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005013 dict = tv->vval.v_dict;
5014
5015 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005016 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005017 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005018 if (key == NULL)
5019 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005020
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005021 if ((di = dict_find(dict, key, -1)) == NULL)
5022 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005023 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005024 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005025
5026 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005027 // stack contents makes sense and the dict stack is
5028 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005029 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005030 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005031 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005032 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005033 tv->v_type = VAR_NUMBER;
5034 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005035 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005036 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005037 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005038 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005039 // Put the dict used on the dict stack, it might be used by
5040 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005041 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005042 if (dict_stack_save(tv) == FAIL)
5043 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005044 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005045 }
5046 break;
5047
5048 // dict member with string key
5049 case ISN_STRINGMEMBER:
5050 {
5051 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052 dictitem_T *di;
5053
5054 tv = STACK_TV_BOT(-1);
5055 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5056 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005057 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005058 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005059 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 }
5061 dict = tv->vval.v_dict;
5062
5063 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5064 == NULL)
5065 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005066 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00005067 semsg(_(e_key_not_present_in_dictionary),
5068 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005069 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005071 // Put the dict used on the dict stack, it might be used by
5072 // a dict function later.
5073 if (dict_stack_save(tv) == FAIL)
5074 goto on_fatal_error;
5075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005077 }
5078 break;
5079
5080 case ISN_CLEARDICT:
5081 dict_stack_drop();
5082 break;
5083
5084 case ISN_USEDICT:
5085 {
5086 typval_T *dict_tv = dict_stack_get_tv();
5087
5088 // Turn "dict.Func" into a partial for "Func" bound to
5089 // "dict". Don't do this when "Func" is already a partial
5090 // that was bound explicitly (pt_auto is FALSE).
5091 tv = STACK_TV_BOT(-1);
5092 if (dict_tv != NULL
5093 && dict_tv->v_type == VAR_DICT
5094 && dict_tv->vval.v_dict != NULL
5095 && (tv->v_type == VAR_FUNC
5096 || (tv->v_type == VAR_PARTIAL
5097 && (tv->vval.v_partial->pt_auto
5098 || tv->vval.v_partial->pt_dict == NULL))))
5099 dict_tv->vval.v_dict =
5100 make_partial(dict_tv->vval.v_dict, tv);
5101 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 }
5103 break;
5104
5105 case ISN_NEGATENR:
5106 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005107 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005108#ifdef FEAT_FLOAT
5109 if (tv->v_type == VAR_FLOAT)
5110 tv->vval.v_float = -tv->vval.v_float;
5111 else
5112#endif
5113 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 break;
5115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 case ISN_CHECKTYPE:
5117 {
5118 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005119 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005120 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005122 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005123 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005124 if (!ectx->ec_where.wt_variable)
5125 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005126 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005127 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005128 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005129 if (r == FAIL)
5130 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005131 if (!ectx->ec_where.wt_variable)
5132 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005133
5134 // number 0 is FALSE, number 1 is TRUE
5135 if (tv->v_type == VAR_NUMBER
5136 && ct->ct_type->tt_type == VAR_BOOL
5137 && (tv->vval.v_number == 0
5138 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005140 tv->v_type = VAR_BOOL;
5141 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005142 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005143 }
5144 }
5145 break;
5146
Bram Moolenaar9af78762020-06-16 11:34:42 +02005147 case ISN_CHECKLEN:
5148 {
5149 int min_len = iptr->isn_arg.checklen.cl_min_len;
5150 list_T *list = NULL;
5151
5152 tv = STACK_TV_BOT(-1);
5153 if (tv->v_type == VAR_LIST)
5154 list = tv->vval.v_list;
5155 if (list == NULL || list->lv_len < min_len
5156 || (list->lv_len > min_len
5157 && !iptr->isn_arg.checklen.cl_more_OK))
5158 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005159 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005160 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005161 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005162 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005163 }
5164 }
5165 break;
5166
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005167 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005168 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005169 break;
5170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005172 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 {
5174 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005175 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005177 if (iptr->isn_type == ISN_2BOOL)
5178 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005179 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005180 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005181 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005182 n = !n;
5183 }
5184 else
5185 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005186 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005187 SOURCING_LNUM = iptr->isn_lnum;
5188 n = tv_get_bool_chk(tv, &error);
5189 if (error)
5190 goto on_error;
5191 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 clear_tv(tv);
5193 tv->v_type = VAR_BOOL;
5194 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5195 }
5196 break;
5197
5198 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005199 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005200 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005201 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5202 iptr->isn_type == ISN_2STRING_ANY,
5203 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005204 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 break;
5206
Bram Moolenaar08597872020-12-10 19:43:40 +01005207 case ISN_RANGE:
5208 {
5209 exarg_T ea;
5210 char *errormsg;
5211
Bram Moolenaarece0b872021-01-08 20:40:45 +01005212 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005213 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005214 ea.addr_type = ADDR_LINES;
5215 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005216 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005217 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005218 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005219
Bram Moolenaar35578162021-08-02 19:10:38 +02005220 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005221 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005222 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005223 tv = STACK_TV_BOT(-1);
5224 tv->v_type = VAR_NUMBER;
5225 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005226 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005227 }
5228 break;
5229
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005230 case ISN_PUT:
5231 {
5232 int regname = iptr->isn_arg.put.put_regname;
5233 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5234 char_u *expr = NULL;
5235 int dir = FORWARD;
5236
Bram Moolenaar08597872020-12-10 19:43:40 +01005237 if (lnum < -2)
5238 {
5239 // line number was put on the stack by ISN_RANGE
5240 tv = STACK_TV_BOT(-1);
5241 curwin->w_cursor.lnum = tv->vval.v_number;
5242 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5243 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005244 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005245 }
5246 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005247 // :put! above cursor
5248 dir = BACKWARD;
5249 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005250 {
5251 curwin->w_cursor.lnum = lnum;
5252 if (lnum == 0)
5253 // check_cursor() below will move to line 1
5254 dir = BACKWARD;
5255 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005256
5257 if (regname == '=')
5258 {
5259 tv = STACK_TV_BOT(-1);
5260 if (tv->v_type == VAR_STRING)
5261 expr = tv->vval.v_string;
5262 else
5263 {
5264 expr = typval2string(tv, TRUE); // allocates value
5265 clear_tv(tv);
5266 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005267 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005268 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005269 check_cursor();
5270 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5271 vim_free(expr);
5272 }
5273 break;
5274
Bram Moolenaar02194d22020-10-24 23:08:38 +02005275 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005276 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5277 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5278 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5279 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005280 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5281 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005282 break;
5283
Bram Moolenaar02194d22020-10-24 23:08:38 +02005284 case ISN_CMDMOD_REV:
5285 // filter regprog is owned by the instruction, don't free it
5286 cmdmod.cmod_filter_regmatch.regprog = NULL;
5287 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005288 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5289 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005290 break;
5291
Bram Moolenaar792f7862020-11-23 08:31:18 +01005292 case ISN_UNPACK:
5293 {
5294 int count = iptr->isn_arg.unpack.unp_count;
5295 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5296 list_T *l;
5297 listitem_T *li;
5298 int i;
5299
5300 // Check there is a valid list to unpack.
5301 tv = STACK_TV_BOT(-1);
5302 if (tv->v_type != VAR_LIST)
5303 {
5304 SOURCING_LNUM = iptr->isn_lnum;
5305 emsg(_(e_for_argument_must_be_sequence_of_lists));
5306 goto on_error;
5307 }
5308 l = tv->vval.v_list;
5309 if (l == NULL
5310 || l->lv_len < (semicolon ? count - 1 : count))
5311 {
5312 SOURCING_LNUM = iptr->isn_lnum;
5313 emsg(_(e_list_value_does_not_have_enough_items));
5314 goto on_error;
5315 }
5316 else if (!semicolon && l->lv_len > count)
5317 {
5318 SOURCING_LNUM = iptr->isn_lnum;
5319 emsg(_(e_list_value_has_more_items_than_targets));
5320 goto on_error;
5321 }
5322
5323 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005324 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005325 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005326 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005327
5328 // Variable after semicolon gets a list with the remaining
5329 // items.
5330 if (semicolon)
5331 {
5332 list_T *rem_list =
5333 list_alloc_with_items(l->lv_len - count + 1);
5334
5335 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005336 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005337 tv = STACK_TV_BOT(-count);
5338 tv->vval.v_list = rem_list;
5339 ++rem_list->lv_refcount;
5340 tv->v_lock = 0;
5341 li = l->lv_first;
5342 for (i = 0; i < count - 1; ++i)
5343 li = li->li_next;
5344 for (i = 0; li != NULL; ++i)
5345 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005346 typval_T tvcopy;
5347
5348 copy_tv(&li->li_tv, &tvcopy);
5349 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005350 li = li->li_next;
5351 }
5352 --count;
5353 }
5354
5355 // Produce the values in reverse order, first item last.
5356 li = l->lv_first;
5357 for (i = 0; i < count; ++i)
5358 {
5359 tv = STACK_TV_BOT(-i - 1);
5360 copy_tv(&li->li_tv, tv);
5361 li = li->li_next;
5362 }
5363
5364 list_unref(l);
5365 }
5366 break;
5367
Bram Moolenaarb2049902021-01-24 12:53:53 +01005368 case ISN_PROF_START:
5369 case ISN_PROF_END:
5370 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005371#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005372 funccall_T cookie;
5373 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005374 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005375 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005376
Bram Moolenaarca16c602022-09-06 18:57:08 +01005377 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005378 if (iptr->isn_type == ISN_PROF_START)
5379 {
5380 func_line_start(&cookie, iptr->isn_lnum);
5381 // if we get here the instruction is executed
5382 func_line_exec(&cookie);
5383 }
5384 else
5385 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005386#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005387 }
5388 break;
5389
Bram Moolenaare99d4222021-06-13 14:01:26 +02005390 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005391 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005392 break;
5393
Bram Moolenaar389df252020-07-09 21:20:47 +02005394 case ISN_SHUFFLE:
5395 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005396 typval_T tmp_tv;
5397 int item = iptr->isn_arg.shuffle.shfl_item;
5398 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005399
5400 tmp_tv = *STACK_TV_BOT(-item);
5401 for ( ; up > 0 && item > 1; --up)
5402 {
5403 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5404 --item;
5405 }
5406 *STACK_TV_BOT(-item) = tmp_tv;
5407 }
5408 break;
5409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005410 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005411 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005412 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005413 ectx->ec_where.wt_index = 0;
5414 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005415 break;
5416 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005417 continue;
5418
Bram Moolenaard032f342020-07-18 18:13:02 +02005419func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005420 // Restore previous function. If the frame pointer is where we started
5421 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005422 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005423 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005424
Bram Moolenaar4c137212021-04-19 16:48:48 +02005425 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005426 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005427 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005428 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005429
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005430on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005431 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005432 // If "emsg_silent" is set then ignore the error, unless it was set
5433 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005434 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005435 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005436 {
5437 // If a sequence of instructions causes an error while ":silent!"
5438 // was used, restore the stack length and jump ahead to restoring
5439 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005440 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005441 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005442 while (ectx->ec_stack.ga_len
5443 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005444 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005445 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005446 clear_tv(STACK_TV_BOT(0));
5447 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005448 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5449 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005450 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005451 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005452 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005453on_fatal_error:
5454 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005455 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005456 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005457 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458 }
5459
5460done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005461 ret = OK;
5462theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005463 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005464
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005465 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005466 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5467 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005468}
5469
5470/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005471 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005472 * "rettv".
5473 * Return OK or FAIL.
5474 */
5475 int
5476exe_typval_instr(typval_T *tv, typval_T *rettv)
5477{
5478 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5479 isn_T *save_instr = ectx->ec_instr;
5480 int save_iidx = ectx->ec_iidx;
5481 int res;
5482
LemonBoyf3b48952022-05-05 13:53:03 +01005483 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5484 // even when the compilation fails.
5485 rettv->v_type = VAR_UNKNOWN;
5486
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005487 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5488 res = exec_instructions(ectx);
5489 if (res == OK)
5490 {
5491 *rettv = *STACK_TV_BOT(-1);
5492 --ectx->ec_stack.ga_len;
5493 }
5494
5495 ectx->ec_instr = save_instr;
5496 ectx->ec_iidx = save_iidx;
5497
5498 return res;
5499}
5500
5501/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005502 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5503 * "substitute_instr".
5504 */
5505 char_u *
5506exe_substitute_instr(void)
5507{
5508 ectx_T *ectx = substitute_instr->subs_ectx;
5509 isn_T *save_instr = ectx->ec_instr;
5510 int save_iidx = ectx->ec_iidx;
5511 char_u *res;
5512
5513 ectx->ec_instr = substitute_instr->subs_instr;
5514 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005515 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005516 typval_T *tv = STACK_TV_BOT(-1);
5517
Bram Moolenaar27523602021-06-05 21:36:19 +02005518 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005519 --ectx->ec_stack.ga_len;
5520 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005521 }
5522 else
5523 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005524 substitute_instr->subs_status = FAIL;
5525 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005527
Bram Moolenaar4c137212021-04-19 16:48:48 +02005528 ectx->ec_instr = save_instr;
5529 ectx->ec_iidx = save_iidx;
5530
5531 return res;
5532}
5533
5534/*
5535 * Call a "def" function from old Vim script.
5536 * Return OK or FAIL.
5537 */
5538 int
5539call_def_function(
5540 ufunc_T *ufunc,
5541 int argc_arg, // nr of arguments
5542 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005543 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005544 partial_T *partial, // optional partial for context
Bram Moolenaar58779852022-09-06 18:31:14 +01005545 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005546 typval_T *rettv) // return value
5547{
5548 ectx_T ectx; // execution context
5549 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005550 int partial_argc = partial == NULL
5551 || (flags & DEF_USE_PT_ARGV) == 0
5552 ? 0 : partial->pt_argc;
5553 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005554 typval_T *tv;
5555 int idx;
5556 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005557 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005558 sctx_T save_current_sctx = current_sctx;
5559 int did_emsg_before = did_emsg_cumul + did_emsg;
5560 int save_suppress_errthrow = suppress_errthrow;
5561 msglist_T **saved_msg_list = NULL;
5562 msglist_T *private_msg_list = NULL;
5563 int save_emsg_silent_def = emsg_silent_def;
5564 int save_did_emsg_def = did_emsg_def;
5565 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005566 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005567
5568// Get pointer to item in the stack.
5569#undef STACK_TV
5570#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5571
5572// Get pointer to item at the bottom of the stack, -1 is the bottom.
5573#undef STACK_TV_BOT
5574#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5575
5576// Get pointer to a local variable on the stack. Negative for arguments.
5577#undef STACK_TV_VAR
5578#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5579
5580 if (ufunc->uf_def_status == UF_NOT_COMPILED
5581 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005582 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5583 && compile_def_function(ufunc, FALSE,
5584 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005585 {
5586 if (did_emsg_cumul + did_emsg == did_emsg_before)
5587 semsg(_(e_function_is_not_compiled_str),
5588 printable_func_name(ufunc));
5589 return FAIL;
5590 }
5591
5592 {
5593 // Check the function was really compiled.
5594 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5595 + ufunc->uf_dfunc_idx;
5596 if (INSTRUCTIONS(dfunc) == NULL)
5597 {
5598 iemsg("using call_def_function() on not compiled function");
5599 return FAIL;
5600 }
5601 }
5602
5603 // If depth of calling is getting too high, don't execute the function.
5604 orig_funcdepth = funcdepth_get();
5605 if (funcdepth_increment() == FAIL)
5606 return FAIL;
5607
5608 CLEAR_FIELD(ectx);
5609 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5610 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005611 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005612 {
5613 funcdepth_decrement();
5614 return FAIL;
5615 }
5616 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5617 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5618 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005619 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005620
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005621 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005622 if (idx > 0 && ufunc->uf_va_name == NULL)
5623 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005624 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005625 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005626 goto failed_early;
5627 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005628 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005629 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005630 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005631 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005632 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005633 goto failed_early;
5634 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005635
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005636 // Put values from the partial and arguments on the stack, but no more than
5637 // what the function expects. A lambda can be called with more arguments
5638 // than it uses.
5639 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005640 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5641 ++idx)
5642 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005643 int argv_idx = idx - partial_argc;
5644
5645 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005646 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005647 && tv->v_type == VAR_SPECIAL
5648 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005649 {
5650 // Use the default value.
5651 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5652 }
5653 else
5654 {
5655 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5656 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005657 ufunc->uf_arg_types[idx], tv,
5658 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005659 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005660 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005661 }
5662 ++ectx.ec_stack.ga_len;
5663 }
5664
5665 // Turn varargs into a list. Empty list if no args.
5666 if (ufunc->uf_va_name != NULL)
5667 {
5668 int vararg_count = argc - ufunc->uf_args.ga_len;
5669
5670 if (vararg_count < 0)
5671 vararg_count = 0;
5672 else
5673 argc -= vararg_count;
5674 if (exe_newlist(vararg_count, &ectx) == FAIL)
5675 goto failed_early;
5676
5677 // Check the type of the list items.
5678 tv = STACK_TV_BOT(-1);
5679 if (ufunc->uf_va_type != NULL
5680 && ufunc->uf_va_type != &t_list_any
5681 && ufunc->uf_va_type->tt_member != &t_any
5682 && tv->vval.v_list != NULL)
5683 {
5684 type_T *expected = ufunc->uf_va_type->tt_member;
5685 listitem_T *li = tv->vval.v_list->lv_first;
5686
5687 for (idx = 0; idx < vararg_count; ++idx)
5688 {
5689 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005690 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005691 goto failed_early;
5692 li = li->li_next;
5693 }
5694 }
5695
5696 if (defcount > 0)
5697 // Move varargs list to below missing default arguments.
5698 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5699 --ectx.ec_stack.ga_len;
5700 }
5701
5702 // Make space for omitted arguments, will store default value below.
5703 // Any varargs list goes after them.
5704 if (defcount > 0)
5705 for (idx = 0; idx < defcount; ++idx)
5706 {
5707 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5708 ++ectx.ec_stack.ga_len;
5709 }
5710 if (ufunc->uf_va_name != NULL)
5711 ++ectx.ec_stack.ga_len;
5712
5713 // Frame pointer points to just after arguments.
5714 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5715 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5716
5717 {
5718 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5719 + ufunc->uf_dfunc_idx;
5720 ufunc_T *base_ufunc = dfunc->df_ufunc;
5721
5722 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005723 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005724 if (partial != NULL || base_ufunc->uf_partial != NULL)
5725 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005726 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5727 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005728 goto failed_early;
5729 if (partial != NULL)
5730 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005731 outer_T *outer = get_pt_outer(partial);
5732
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005733 if (outer->out_stack == NULL && outer->out_loop_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005734 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005735 if (current_ectx != NULL)
5736 {
5737 if (current_ectx->ec_outer_ref != NULL
5738 && current_ectx->ec_outer_ref->or_outer != NULL)
5739 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005740 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005741 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005742 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005743 }
5744 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005745 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005746 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005747 ++partial->pt_refcount;
5748 ectx.ec_outer_ref->or_partial = partial;
5749 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005750 }
5751 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005752 {
5753 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5754 ++base_ufunc->uf_partial->pt_refcount;
5755 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5756 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005757 }
5758 }
5759
5760 // dummy frame entries
5761 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5762 {
5763 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5764 ++ectx.ec_stack.ga_len;
5765 }
5766
5767 {
5768 // Reserve space for local variables and any closure reference count.
5769 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5770 + ufunc->uf_dfunc_idx;
5771
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005772 // Initialize variables to zero. That avoids having to generate
5773 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005774 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005775 {
5776 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5777 STACK_TV_VAR(idx)->vval.v_number = 0;
5778 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005779 ectx.ec_stack.ga_len += dfunc->df_varcount;
5780 if (dfunc->df_has_closure)
5781 {
5782 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5783 STACK_TV_VAR(idx)->vval.v_number = 0;
5784 ++ectx.ec_stack.ga_len;
5785 }
5786
5787 ectx.ec_instr = INSTRUCTIONS(dfunc);
5788 }
5789
Bram Moolenaar58779852022-09-06 18:31:14 +01005790 // Store the execution context in funccal, used by invoke_all_defer().
5791 if (funccal != NULL)
5792 funccal->fc_ectx = &ectx;
5793
Bram Moolenaar4c137212021-04-19 16:48:48 +02005794 // Following errors are in the function, not the caller.
5795 // Commands behave like vim9script.
5796 estack_push_ufunc(ufunc, 1);
5797 current_sctx = ufunc->uf_script_ctx;
5798 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5799
5800 // Use a specific location for storing error messages to be converted to an
5801 // exception.
5802 saved_msg_list = msg_list;
5803 msg_list = &private_msg_list;
5804
5805 // Do turn errors into exceptions.
5806 suppress_errthrow = FALSE;
5807
5808 // Do not delete the function while executing it.
5809 ++ufunc->uf_calls;
5810
5811 // When ":silent!" was used before calling then we still abort the
5812 // function. If ":silent!" is used in the function then we don't.
5813 emsg_silent_def = emsg_silent;
5814 did_emsg_def = 0;
5815
5816 ectx.ec_where.wt_index = 0;
5817 ectx.ec_where.wt_variable = FALSE;
5818
5819 // Execute the instructions until done.
5820 ret = exec_instructions(&ectx);
5821 if (ret == OK)
5822 {
5823 // function finished, get result from the stack.
5824 if (ufunc->uf_ret_type == &t_void)
5825 {
5826 rettv->v_type = VAR_VOID;
5827 }
5828 else
5829 {
5830 tv = STACK_TV_BOT(-1);
5831 *rettv = *tv;
5832 tv->v_type = VAR_UNKNOWN;
5833 }
5834 }
5835
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005836 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005837 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005838
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005839 // Deal with any remaining closures, they may be in use somewhere.
5840 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005841 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005842 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005843 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005844 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005845
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005846 estack_pop();
5847 current_sctx = save_current_sctx;
5848
Bram Moolenaar2336c372021-12-06 15:06:54 +00005849 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5850 // Function was unreferenced while being used, free it now.
5851 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005852
Bram Moolenaar352134b2020-10-17 22:04:08 +02005853 if (*msg_list != NULL && saved_msg_list != NULL)
5854 {
5855 msglist_T **plist = saved_msg_list;
5856
5857 // Append entries from the current msg_list (uncaught exceptions) to
5858 // the saved msg_list.
5859 while (*plist != NULL)
5860 plist = &(*plist)->next;
5861
5862 *plist = *msg_list;
5863 }
5864 msg_list = saved_msg_list;
5865
Bram Moolenaar4c137212021-04-19 16:48:48 +02005866 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005867 {
5868 cmdmod.cmod_filter_regmatch.regprog = NULL;
5869 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005870 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005871 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005872 emsg_silent_def = save_emsg_silent_def;
5873 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005874
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005875failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005876 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005877 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5878 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005879 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005881 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005882 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005883 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005884 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005885 if (ectx.ec_outer_ref->or_outer_allocated)
5886 vim_free(ectx.ec_outer_ref->or_outer);
5887 partial_unref(ectx.ec_outer_ref->or_partial);
5888 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005889 }
5890
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005891 // Not sure if this is necessary.
5892 suppress_errthrow = save_suppress_errthrow;
5893
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005894 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5895 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005896 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005897 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005898 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005899 return ret;
5900}
5901
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902/*
Bram Moolenaar58779852022-09-06 18:31:14 +01005903 * Called when a def function has finished (possibly failed).
5904 * Invoke all the function returns to clean up and invoke deferred functions,
5905 * except the toplevel one.
5906 */
5907 void
5908unwind_def_callstack(ectx_T *ectx)
5909{
5910 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
5911 func_return(ectx);
5912}
5913
5914/*
5915 * Invoke any deffered functions for the top function in "ectx".
5916 */
5917 void
5918may_invoke_defer_funcs(ectx_T *ectx)
5919{
5920 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
5921
5922 if (dfunc->df_defer_var_idx > 0)
5923 invoke_defer_funcs(ectx);
5924}
5925
5926/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005927 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5928 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5929 * "pfx" is prefixed to every line.
5930 */
5931 static void
5932list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5933{
5934 int line_idx = 0;
5935 int prev_current = 0;
5936 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005937 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005938
5939 for (current = 0; current < instr_count; ++current)
5940 {
5941 isn_T *iptr = &instr[current];
5942 char *line;
5943
5944 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005945 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005946 while (line_idx < iptr->isn_lnum
5947 && line_idx < ufunc->uf_lines.ga_len)
5948 {
5949 if (current > prev_current)
5950 {
5951 msg_puts("\n\n");
5952 prev_current = current;
5953 }
5954 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5955 if (line != NULL)
5956 msg(line);
5957 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005958 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5959 {
5960 int first_def_arg = ufunc->uf_args.ga_len
5961 - ufunc->uf_def_args.ga_len;
5962
5963 if (def_arg_idx > 0)
5964 msg_puts("\n\n");
5965 msg_start();
5966 msg_puts(" ");
5967 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5968 first_def_arg + def_arg_idx]);
5969 msg_puts(" = ");
5970 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5971 msg_clr_eos();
5972 msg_end();
5973 }
5974 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005975
5976 switch (iptr->isn_type)
5977 {
5978 case ISN_EXEC:
5979 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5980 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005981 case ISN_EXEC_SPLIT:
5982 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5983 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005984 case ISN_EXECRANGE:
5985 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5986 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005987 case ISN_LEGACY_EVAL:
5988 smsg("%s%4d EVAL legacy %s", pfx, current,
5989 iptr->isn_arg.string);
5990 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005991 case ISN_REDIRSTART:
5992 smsg("%s%4d REDIR", pfx, current);
5993 break;
5994 case ISN_REDIREND:
5995 smsg("%s%4d REDIR END%s", pfx, current,
5996 iptr->isn_arg.number ? " append" : "");
5997 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005998 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005999#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006000 smsg("%s%4d CEXPR pre %s", pfx, current,
6001 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006002#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006003 break;
6004 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006005#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006006 {
6007 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6008
6009 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6010 cexpr_get_auname(cer->cer_cmdidx),
6011 cer->cer_forceit ? "!" : "",
6012 cer->cer_cmdline);
6013 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006014#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006015 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006016 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006017 smsg("%s%4d INSTR", pfx, current);
6018 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6019 msg(" -------------");
6020 break;
6021 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006022 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006023 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6024
6025 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006026 }
6027 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006028 case ISN_SUBSTITUTE:
6029 {
6030 subs_T *subs = &iptr->isn_arg.subs;
6031
6032 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6033 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6034 msg(" -------------");
6035 }
6036 break;
6037 case ISN_EXECCONCAT:
6038 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6039 (varnumber_T)iptr->isn_arg.number);
6040 break;
6041 case ISN_ECHO:
6042 {
6043 echo_T *echo = &iptr->isn_arg.echo;
6044
6045 smsg("%s%4d %s %d", pfx, current,
6046 echo->echo_with_white ? "ECHO" : "ECHON",
6047 echo->echo_count);
6048 }
6049 break;
6050 case ISN_EXECUTE:
6051 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006052 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006053 break;
6054 case ISN_ECHOMSG:
6055 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006056 (varnumber_T)(iptr->isn_arg.number));
6057 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006058 case ISN_ECHOWINDOW:
6059 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
6060 (varnumber_T)(iptr->isn_arg.number));
6061 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006062 case ISN_ECHOCONSOLE:
6063 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6064 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006065 break;
6066 case ISN_ECHOERR:
6067 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006068 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006069 break;
6070 case ISN_LOAD:
6071 {
6072 if (iptr->isn_arg.number < 0)
6073 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6074 (varnumber_T)(iptr->isn_arg.number
6075 + STACK_FRAME_SIZE));
6076 else
6077 smsg("%s%4d LOAD $%lld", pfx, current,
6078 (varnumber_T)(iptr->isn_arg.number));
6079 }
6080 break;
6081 case ISN_LOADOUTER:
6082 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006083 isn_outer_T *outer = &iptr->isn_arg.outer;
6084
6085 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006086 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006087 outer->outer_depth,
6088 outer->outer_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +02006089 + STACK_FRAME_SIZE);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006090 else if (outer->outer_depth == OUTER_LOOP_DEPTH)
6091 smsg("%s%4d LOADOUTER level 1 $%d in loop",
6092 pfx, current, outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006093 else
6094 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006095 outer->outer_depth,
6096 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006097 }
6098 break;
6099 case ISN_LOADV:
6100 smsg("%s%4d LOADV v:%s", pfx, current,
6101 get_vim_var_name(iptr->isn_arg.number));
6102 break;
6103 case ISN_LOADSCRIPT:
6104 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006105 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6106 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6107 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006108
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006109 sv = get_script_svar(sref, -1);
6110 if (sv == NULL)
6111 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6112 pfx, current, si->sn_name);
6113 else
6114 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006115 sv->sv_name,
6116 sref->sref_idx,
6117 si->sn_name);
6118 }
6119 break;
6120 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006121 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006122 {
6123 scriptitem_T *si = SCRIPT_ITEM(
6124 iptr->isn_arg.loadstore.ls_sid);
6125
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006126 smsg("%s%4d %s s:%s from %s", pfx, current,
6127 iptr->isn_type == ISN_LOADS ? "LOADS"
6128 : "LOADEXPORT",
6129 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006130 }
6131 break;
6132 case ISN_LOADAUTO:
6133 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6134 break;
6135 case ISN_LOADG:
6136 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6137 break;
6138 case ISN_LOADB:
6139 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6140 break;
6141 case ISN_LOADW:
6142 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6143 break;
6144 case ISN_LOADT:
6145 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6146 break;
6147 case ISN_LOADGDICT:
6148 smsg("%s%4d LOAD g:", pfx, current);
6149 break;
6150 case ISN_LOADBDICT:
6151 smsg("%s%4d LOAD b:", pfx, current);
6152 break;
6153 case ISN_LOADWDICT:
6154 smsg("%s%4d LOAD w:", pfx, current);
6155 break;
6156 case ISN_LOADTDICT:
6157 smsg("%s%4d LOAD t:", pfx, current);
6158 break;
6159 case ISN_LOADOPT:
6160 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6161 break;
6162 case ISN_LOADENV:
6163 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6164 break;
6165 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006166 smsg("%s%4d LOADREG @%c", pfx, current,
6167 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006168 break;
6169
6170 case ISN_STORE:
6171 if (iptr->isn_arg.number < 0)
6172 smsg("%s%4d STORE arg[%lld]", pfx, current,
6173 iptr->isn_arg.number + STACK_FRAME_SIZE);
6174 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006175 smsg("%s%4d STORE $%lld", pfx, current,
6176 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006177 break;
6178 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006179 {
6180 isn_outer_T *outer = &iptr->isn_arg.outer;
6181
6182 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6183 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6184 pfx, current, outer->outer_idx);
6185 else
6186 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6187 outer->outer_depth, outer->outer_idx);
6188 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006189 break;
6190 case ISN_STOREV:
6191 smsg("%s%4d STOREV v:%s", pfx, current,
6192 get_vim_var_name(iptr->isn_arg.number));
6193 break;
6194 case ISN_STOREAUTO:
6195 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6196 break;
6197 case ISN_STOREG:
6198 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6199 break;
6200 case ISN_STOREB:
6201 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6202 break;
6203 case ISN_STOREW:
6204 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6205 break;
6206 case ISN_STORET:
6207 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6208 break;
6209 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006210 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006211 {
6212 scriptitem_T *si = SCRIPT_ITEM(
6213 iptr->isn_arg.loadstore.ls_sid);
6214
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006215 smsg("%s%4d %s %s in %s", pfx, current,
6216 iptr->isn_type == ISN_STORES
6217 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006218 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6219 }
6220 break;
6221 case ISN_STORESCRIPT:
6222 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006223 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6224 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6225 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006226
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006227 sv = get_script_svar(sref, -1);
6228 if (sv == NULL)
6229 smsg("%s%4d STORESCRIPT [deleted] in %s",
6230 pfx, current, si->sn_name);
6231 else
6232 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006233 sv->sv_name,
6234 sref->sref_idx,
6235 si->sn_name);
6236 }
6237 break;
6238 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006239 case ISN_STOREFUNCOPT:
6240 smsg("%s%4d %s &%s", pfx, current,
6241 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006242 iptr->isn_arg.storeopt.so_name);
6243 break;
6244 case ISN_STOREENV:
6245 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6246 break;
6247 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006248 smsg("%s%4d STOREREG @%c", pfx, current,
6249 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006250 break;
6251 case ISN_STORENR:
6252 smsg("%s%4d STORE %lld in $%d", pfx, current,
6253 iptr->isn_arg.storenr.stnr_val,
6254 iptr->isn_arg.storenr.stnr_idx);
6255 break;
6256
6257 case ISN_STOREINDEX:
6258 smsg("%s%4d STOREINDEX %s", pfx, current,
6259 vartype_name(iptr->isn_arg.vartype));
6260 break;
6261
6262 case ISN_STORERANGE:
6263 smsg("%s%4d STORERANGE", pfx, current);
6264 break;
6265
6266 // constants
6267 case ISN_PUSHNR:
6268 smsg("%s%4d PUSHNR %lld", pfx, current,
6269 (varnumber_T)(iptr->isn_arg.number));
6270 break;
6271 case ISN_PUSHBOOL:
6272 case ISN_PUSHSPEC:
6273 smsg("%s%4d PUSH %s", pfx, current,
6274 get_var_special_name(iptr->isn_arg.number));
6275 break;
6276 case ISN_PUSHF:
6277#ifdef FEAT_FLOAT
6278 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
6279#endif
6280 break;
6281 case ISN_PUSHS:
6282 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6283 break;
6284 case ISN_PUSHBLOB:
6285 {
6286 char_u *r;
6287 char_u numbuf[NUMBUFLEN];
6288 char_u *tofree;
6289
6290 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6291 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6292 vim_free(tofree);
6293 }
6294 break;
6295 case ISN_PUSHFUNC:
6296 {
6297 char *name = (char *)iptr->isn_arg.string;
6298
6299 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6300 name == NULL ? "[none]" : name);
6301 }
6302 break;
6303 case ISN_PUSHCHANNEL:
6304#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006305 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006306#endif
6307 break;
6308 case ISN_PUSHJOB:
6309#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006310 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006311#endif
6312 break;
6313 case ISN_PUSHEXC:
6314 smsg("%s%4d PUSH v:exception", pfx, current);
6315 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006316 case ISN_AUTOLOAD:
6317 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6318 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006319 case ISN_UNLET:
6320 smsg("%s%4d UNLET%s %s", pfx, current,
6321 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6322 iptr->isn_arg.unlet.ul_name);
6323 break;
6324 case ISN_UNLETENV:
6325 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6326 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6327 iptr->isn_arg.unlet.ul_name);
6328 break;
6329 case ISN_UNLETINDEX:
6330 smsg("%s%4d UNLETINDEX", pfx, current);
6331 break;
6332 case ISN_UNLETRANGE:
6333 smsg("%s%4d UNLETRANGE", pfx, current);
6334 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006335 case ISN_LOCKUNLOCK:
6336 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6337 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006338 case ISN_LOCKCONST:
6339 smsg("%s%4d LOCKCONST", pfx, current);
6340 break;
6341 case ISN_NEWLIST:
6342 smsg("%s%4d NEWLIST size %lld", pfx, current,
6343 (varnumber_T)(iptr->isn_arg.number));
6344 break;
6345 case ISN_NEWDICT:
6346 smsg("%s%4d NEWDICT size %lld", pfx, current,
6347 (varnumber_T)(iptr->isn_arg.number));
6348 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006349 case ISN_NEWPARTIAL:
6350 smsg("%s%4d NEWPARTIAL", pfx, current);
6351 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006352
6353 // function call
6354 case ISN_BCALL:
6355 {
6356 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6357
6358 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6359 internal_func_name(cbfunc->cbf_idx),
6360 cbfunc->cbf_argcount);
6361 }
6362 break;
6363 case ISN_DCALL:
6364 {
6365 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6366 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6367 + cdfunc->cdf_idx;
6368
6369 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006370 printable_func_name(df->df_ufunc),
6371 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006372 }
6373 break;
6374 case ISN_UCALL:
6375 {
6376 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6377
6378 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6379 cufunc->cuf_name, cufunc->cuf_argcount);
6380 }
6381 break;
6382 case ISN_PCALL:
6383 {
6384 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6385
6386 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6387 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6388 }
6389 break;
6390 case ISN_PCALL_END:
6391 smsg("%s%4d PCALL end", pfx, current);
6392 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006393 case ISN_DEFER:
6394 smsg("%s%4d DEFER %d args", pfx, current,
6395 (int)iptr->isn_arg.defer.defer_argcount);
6396 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006397 case ISN_RETURN:
6398 smsg("%s%4d RETURN", pfx, current);
6399 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006400 case ISN_RETURN_VOID:
6401 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006402 break;
6403 case ISN_FUNCREF:
6404 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006405 funcref_T *funcref = &iptr->isn_arg.funcref;
6406 funcref_extra_T *extra = funcref->fr_extra;
6407 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006408
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006409 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006410 {
6411 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6412 + funcref->fr_dfunc_idx;
6413 name = df->df_ufunc->uf_name;
6414 }
6415 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006416 name = extra->fre_func_name;
6417 if (extra == NULL || extra->fre_loop_var_count == 0)
6418 smsg("%s%4d FUNCREF %s", pfx, current, name);
6419 else
6420 smsg("%s%4d FUNCREF %s var $%d - $%d", pfx, current,
6421 name,
6422 extra->fre_loop_var_idx,
6423 extra->fre_loop_var_idx
6424 + extra->fre_loop_var_count - 1);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006425 }
6426 break;
6427
6428 case ISN_NEWFUNC:
6429 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006430 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006431
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006432 if (arg->nfa_loop_var_count == 0)
6433 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6434 arg->nfa_lambda, arg->nfa_global);
6435 else
6436 smsg("%s%4d NEWFUNC %s %s var $%d - $%d", pfx, current,
6437 arg->nfa_lambda, arg->nfa_global,
6438 arg->nfa_loop_var_idx,
6439 arg->nfa_loop_var_idx + arg->nfa_loop_var_count - 1);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006440 }
6441 break;
6442
6443 case ISN_DEF:
6444 {
6445 char_u *name = iptr->isn_arg.string;
6446
6447 smsg("%s%4d DEF %s", pfx, current,
6448 name == NULL ? (char_u *)"" : name);
6449 }
6450 break;
6451
6452 case ISN_JUMP:
6453 {
6454 char *when = "?";
6455
6456 switch (iptr->isn_arg.jump.jump_when)
6457 {
6458 case JUMP_ALWAYS:
6459 when = "JUMP";
6460 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006461 case JUMP_NEVER:
6462 iemsg("JUMP_NEVER should not be used");
6463 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006464 case JUMP_AND_KEEP_IF_TRUE:
6465 when = "JUMP_AND_KEEP_IF_TRUE";
6466 break;
6467 case JUMP_IF_FALSE:
6468 when = "JUMP_IF_FALSE";
6469 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006470 case JUMP_WHILE_FALSE:
6471 when = "JUMP_WHILE_FALSE"; // unused
6472 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006473 case JUMP_IF_COND_FALSE:
6474 when = "JUMP_IF_COND_FALSE";
6475 break;
6476 case JUMP_IF_COND_TRUE:
6477 when = "JUMP_IF_COND_TRUE";
6478 break;
6479 }
6480 smsg("%s%4d %s -> %d", pfx, current, when,
6481 iptr->isn_arg.jump.jump_where);
6482 }
6483 break;
6484
6485 case ISN_JUMP_IF_ARG_SET:
6486 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6487 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6488 iptr->isn_arg.jump.jump_where);
6489 break;
6490
6491 case ISN_FOR:
6492 {
6493 forloop_T *forloop = &iptr->isn_arg.forloop;
6494
6495 smsg("%s%4d FOR $%d -> %d", pfx, current,
6496 forloop->for_idx, forloop->for_end);
6497 }
6498 break;
6499
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006500 case ISN_ENDLOOP:
6501 {
6502 endloop_T *endloop = &iptr->isn_arg.endloop;
6503
6504 smsg("%s%4d ENDLOOP $%d save $%d - $%d", pfx, current,
6505 endloop->end_funcref_idx,
6506 endloop->end_var_idx,
6507 endloop->end_var_idx + endloop->end_var_count - 1);
6508 }
6509 break;
6510
6511 case ISN_WHILE:
6512 {
6513 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6514
6515 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6516 whileloop->while_funcref_idx,
6517 whileloop->while_end);
6518 }
6519 break;
6520
Bram Moolenaar4c137212021-04-19 16:48:48 +02006521 case ISN_TRY:
6522 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006523 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006524
6525 if (try->try_ref->try_finally == 0)
6526 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6527 pfx, current,
6528 try->try_ref->try_catch,
6529 try->try_ref->try_endtry);
6530 else
6531 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6532 pfx, current,
6533 try->try_ref->try_catch,
6534 try->try_ref->try_finally,
6535 try->try_ref->try_endtry);
6536 }
6537 break;
6538 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006539 smsg("%s%4d CATCH", pfx, current);
6540 break;
6541 case ISN_TRYCONT:
6542 {
6543 trycont_T *trycont = &iptr->isn_arg.trycont;
6544
6545 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6546 trycont->tct_levels,
6547 trycont->tct_levels == 1 ? "" : "s",
6548 trycont->tct_where);
6549 }
6550 break;
6551 case ISN_FINALLY:
6552 smsg("%s%4d FINALLY", pfx, current);
6553 break;
6554 case ISN_ENDTRY:
6555 smsg("%s%4d ENDTRY", pfx, current);
6556 break;
6557 case ISN_THROW:
6558 smsg("%s%4d THROW", pfx, current);
6559 break;
6560
6561 // expression operations on number
6562 case ISN_OPNR:
6563 case ISN_OPFLOAT:
6564 case ISN_OPANY:
6565 {
6566 char *what;
6567 char *ins;
6568
6569 switch (iptr->isn_arg.op.op_type)
6570 {
6571 case EXPR_MULT: what = "*"; break;
6572 case EXPR_DIV: what = "/"; break;
6573 case EXPR_REM: what = "%"; break;
6574 case EXPR_SUB: what = "-"; break;
6575 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006576 case EXPR_LSHIFT: what = "<<"; break;
6577 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006578 default: what = "???"; break;
6579 }
6580 switch (iptr->isn_type)
6581 {
6582 case ISN_OPNR: ins = "OPNR"; break;
6583 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6584 case ISN_OPANY: ins = "OPANY"; break;
6585 default: ins = "???"; break;
6586 }
6587 smsg("%s%4d %s %s", pfx, current, ins, what);
6588 }
6589 break;
6590
6591 case ISN_COMPAREBOOL:
6592 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006593 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006594 case ISN_COMPARENR:
6595 case ISN_COMPAREFLOAT:
6596 case ISN_COMPARESTRING:
6597 case ISN_COMPAREBLOB:
6598 case ISN_COMPARELIST:
6599 case ISN_COMPAREDICT:
6600 case ISN_COMPAREFUNC:
6601 case ISN_COMPAREANY:
6602 {
6603 char *p;
6604 char buf[10];
6605 char *type;
6606
6607 switch (iptr->isn_arg.op.op_type)
6608 {
6609 case EXPR_EQUAL: p = "=="; break;
6610 case EXPR_NEQUAL: p = "!="; break;
6611 case EXPR_GREATER: p = ">"; break;
6612 case EXPR_GEQUAL: p = ">="; break;
6613 case EXPR_SMALLER: p = "<"; break;
6614 case EXPR_SEQUAL: p = "<="; break;
6615 case EXPR_MATCH: p = "=~"; break;
6616 case EXPR_IS: p = "is"; break;
6617 case EXPR_ISNOT: p = "isnot"; break;
6618 case EXPR_NOMATCH: p = "!~"; break;
6619 default: p = "???"; break;
6620 }
6621 STRCPY(buf, p);
6622 if (iptr->isn_arg.op.op_ic == TRUE)
6623 strcat(buf, "?");
6624 switch(iptr->isn_type)
6625 {
6626 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6627 case ISN_COMPARESPECIAL:
6628 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006629 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006630 case ISN_COMPARENR: type = "COMPARENR"; break;
6631 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6632 case ISN_COMPARESTRING:
6633 type = "COMPARESTRING"; break;
6634 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6635 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6636 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6637 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6638 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6639 default: type = "???"; break;
6640 }
6641
6642 smsg("%s%4d %s %s", pfx, current, type, buf);
6643 }
6644 break;
6645
6646 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6647 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6648
6649 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006650 case ISN_CONCAT:
6651 smsg("%s%4d CONCAT size %lld", pfx, current,
6652 (varnumber_T)(iptr->isn_arg.number));
6653 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006654 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6655 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6656 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6657 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6658 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6659 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6660 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6661 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6662 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6663 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6664 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006665 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006666 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6667 iptr->isn_arg.getitem.gi_index,
6668 iptr->isn_arg.getitem.gi_with_op ?
6669 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006670 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6671 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6672 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006673 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6674 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6675
Bram Moolenaar4c137212021-04-19 16:48:48 +02006676 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6677
Bram Moolenaar4c137212021-04-19 16:48:48 +02006678 case ISN_CHECKTYPE:
6679 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006680 checktype_T *ct = &iptr->isn_arg.type;
6681 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006682
6683 if (ct->ct_arg_idx == 0)
6684 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6685 type_name(ct->ct_type, &tofree),
6686 (int)ct->ct_off);
6687 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006688 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006689 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006690 type_name(ct->ct_type, &tofree),
6691 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006692 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006693 (int)ct->ct_arg_idx);
6694 vim_free(tofree);
6695 break;
6696 }
6697 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6698 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6699 iptr->isn_arg.checklen.cl_min_len);
6700 break;
6701 case ISN_SETTYPE:
6702 {
6703 char *tofree;
6704
6705 smsg("%s%4d SETTYPE %s", pfx, current,
6706 type_name(iptr->isn_arg.type.ct_type, &tofree));
6707 vim_free(tofree);
6708 break;
6709 }
6710 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006711 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6712 smsg("%s%4d INVERT %d (!val)", pfx, current,
6713 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006714 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006715 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6716 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006717 break;
6718 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006719 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006720 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006721 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6722 pfx, current,
6723 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006724 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006725 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6726 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006727 break;
6728 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006729 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006730 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006731 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006732 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6733 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006734 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006735 else
6736 smsg("%s%4d PUT %c %ld", pfx, current,
6737 iptr->isn_arg.put.put_regname,
6738 (long)iptr->isn_arg.put.put_lnum);
6739 break;
6740
Bram Moolenaar4c137212021-04-19 16:48:48 +02006741 case ISN_CMDMOD:
6742 {
6743 char_u *buf;
6744 size_t len = produce_cmdmods(
6745 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6746
6747 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006748 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006749 {
6750 (void)produce_cmdmods(
6751 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6752 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6753 vim_free(buf);
6754 }
6755 break;
6756 }
6757 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6758
6759 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006760 smsg("%s%4d PROFILE START line %d", pfx, current,
6761 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006762 break;
6763
6764 case ISN_PROF_END:
6765 smsg("%s%4d PROFILE END", pfx, current);
6766 break;
6767
Bram Moolenaare99d4222021-06-13 14:01:26 +02006768 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006769 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6770 iptr->isn_arg.debug.dbg_break_lnum + 1,
6771 iptr->isn_lnum,
6772 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006773 break;
6774
Bram Moolenaar4c137212021-04-19 16:48:48 +02006775 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6776 iptr->isn_arg.unpack.unp_count,
6777 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6778 break;
6779 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6780 iptr->isn_arg.shuffle.shfl_item,
6781 iptr->isn_arg.shuffle.shfl_up);
6782 break;
6783 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6784
6785 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6786 return;
6787 }
6788
6789 out_flush(); // output one line at a time
6790 ui_breakcheck();
6791 if (got_int)
6792 break;
6793 }
6794}
6795
6796/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006797 * Handle command line completion for the :disassemble command.
6798 */
6799 void
6800set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6801{
6802 char_u *p;
6803
6804 // Default: expand user functions, "debug" and "profile"
6805 xp->xp_context = EXPAND_DISASSEMBLE;
6806 xp->xp_pattern = arg;
6807
6808 // first argument already typed: only user function names
6809 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6810 {
6811 xp->xp_context = EXPAND_USER_FUNC;
6812 xp->xp_pattern = skipwhite(p);
6813 }
6814}
6815
6816/*
6817 * Function given to ExpandGeneric() to obtain the list of :disassemble
6818 * arguments.
6819 */
6820 char_u *
6821get_disassemble_argument(expand_T *xp, int idx)
6822{
6823 if (idx == 0)
6824 return (char_u *)"debug";
6825 if (idx == 1)
6826 return (char_u *)"profile";
6827 return get_user_func_name(xp, idx - 2);
6828}
6829
6830/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006831 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006832 * We don't really need this at runtime, but we do have tests that require it,
6833 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006834 */
6835 void
6836ex_disassemble(exarg_T *eap)
6837{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006838 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006839 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006841 isn_T *instr = NULL; // init to shut up gcc warning
6842 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006843 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006844
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006845 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006846 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006848 if (func_needs_compiling(ufunc, compile_type)
6849 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006850 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006851 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006852 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006853 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006854 return;
6855 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006856 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857
6858 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006859 switch (compile_type)
6860 {
6861 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006862#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006863 instr = dfunc->df_instr_prof;
6864 instr_count = dfunc->df_instr_prof_count;
6865 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006866#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006867 // FALLTHROUGH
6868 case CT_NONE:
6869 instr = dfunc->df_instr;
6870 instr_count = dfunc->df_instr_count;
6871 break;
6872 case CT_DEBUG:
6873 instr = dfunc->df_instr_debug;
6874 instr_count = dfunc->df_instr_debug_count;
6875 break;
6876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006877
Bram Moolenaar4c137212021-04-19 16:48:48 +02006878 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879}
6880
6881/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006882 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006883 * list, etc. Mostly like what JavaScript does, except that empty list and
6884 * empty dictionary are FALSE.
6885 */
6886 int
6887tv2bool(typval_T *tv)
6888{
6889 switch (tv->v_type)
6890 {
6891 case VAR_NUMBER:
6892 return tv->vval.v_number != 0;
6893 case VAR_FLOAT:
6894#ifdef FEAT_FLOAT
6895 return tv->vval.v_float != 0.0;
6896#else
6897 break;
6898#endif
6899 case VAR_PARTIAL:
6900 return tv->vval.v_partial != NULL;
6901 case VAR_FUNC:
6902 case VAR_STRING:
6903 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6904 case VAR_LIST:
6905 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6906 case VAR_DICT:
6907 return tv->vval.v_dict != NULL
6908 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6909 case VAR_BOOL:
6910 case VAR_SPECIAL:
6911 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6912 case VAR_JOB:
6913#ifdef FEAT_JOB_CHANNEL
6914 return tv->vval.v_job != NULL;
6915#else
6916 break;
6917#endif
6918 case VAR_CHANNEL:
6919#ifdef FEAT_JOB_CHANNEL
6920 return tv->vval.v_channel != NULL;
6921#else
6922 break;
6923#endif
6924 case VAR_BLOB:
6925 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6926 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006927 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006928 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006929 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 break;
6931 }
6932 return FALSE;
6933}
6934
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006935 void
6936emsg_using_string_as(typval_T *tv, int as_number)
6937{
6938 semsg(_(as_number ? e_using_string_as_number_str
6939 : e_using_string_as_bool_str),
6940 tv->vval.v_string == NULL
6941 ? (char_u *)"" : tv->vval.v_string);
6942}
6943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944/*
6945 * If "tv" is a string give an error and return FAIL.
6946 */
6947 int
6948check_not_string(typval_T *tv)
6949{
6950 if (tv->v_type == VAR_STRING)
6951 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006952 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 clear_tv(tv);
6954 return FAIL;
6955 }
6956 return OK;
6957}
6958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959
6960#endif // FEAT_EVAL