blob: da2822c1b5de47a7cdd2da667992729ca71e9a31 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
67// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
74
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100125 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200126 */
127 static int
128exe_newlist(int count, ectx_T *ectx)
129{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100130 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200131 int idx;
132 typval_T *tv;
133
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100134 if (count >= 0)
135 {
136 list = list_alloc_with_items(count);
137 if (list == NULL)
138 return FAIL;
139 for (idx = 0; idx < count; ++idx)
140 list_set_item(list, idx, STACK_TV_BOT(idx - count));
141 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200142
143 if (count > 0)
144 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200145 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100146 {
147 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200148 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100149 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200150 else
151 ++ectx->ec_stack.ga_len;
152 tv = STACK_TV_BOT(-1);
153 tv->v_type = VAR_LIST;
154 tv->vval.v_list = list;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100155 if (list != NULL)
156 ++list->lv_refcount;
157 return OK;
158}
159
160/*
161 * Implementation of ISN_NEWDICT.
162 * Returns FAIL on total failure, MAYBE on error.
163 */
164 static int
165exe_newdict(int count, ectx_T *ectx)
166{
167 dict_T *dict = NULL;
168 dictitem_T *item;
169 char_u *key;
170 int idx;
171 typval_T *tv;
172
173 if (count >= 0)
174 {
175 dict = dict_alloc();
176 if (unlikely(dict == NULL))
177 return FAIL;
178 for (idx = 0; idx < count; ++idx)
179 {
180 // have already checked key type is VAR_STRING
181 tv = STACK_TV_BOT(2 * (idx - count));
182 // check key is unique
183 key = tv->vval.v_string == NULL
184 ? (char_u *)"" : tv->vval.v_string;
185 item = dict_find(dict, key, -1);
186 if (item != NULL)
187 {
188 semsg(_(e_duplicate_key_in_dicitonary), key);
189 dict_unref(dict);
190 return MAYBE;
191 }
192 item = dictitem_alloc(key);
193 clear_tv(tv);
194 if (unlikely(item == NULL))
195 {
196 dict_unref(dict);
197 return FAIL;
198 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100199 tv = STACK_TV_BOT(2 * (idx - count) + 1);
200 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100201 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100202 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100203 if (dict_add(dict, item) == FAIL)
204 {
205 // can this ever happen?
206 dict_unref(dict);
207 return FAIL;
208 }
209 }
210 }
211
212 if (count > 0)
213 ectx->ec_stack.ga_len -= 2 * count - 1;
214 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
215 return FAIL;
216 else
217 ++ectx->ec_stack.ga_len;
218 tv = STACK_TV_BOT(-1);
219 tv->v_type = VAR_DICT;
220 tv->v_lock = 0;
221 tv->vval.v_dict = dict;
222 if (dict != NULL)
223 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200224 return OK;
225}
226
227/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200228 * If debug_tick changed check if "ufunc" has a breakpoint and update
229 * "uf_has_breakpoint".
230 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000231 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200232update_has_breakpoint(ufunc_T *ufunc)
233{
234 if (ufunc->uf_debug_tick != debug_tick)
235 {
236 linenr_T breakpoint;
237
238 ufunc->uf_debug_tick = debug_tick;
239 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
240 ufunc->uf_has_breakpoint = breakpoint > 0;
241 }
242}
243
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200244static garray_T dict_stack = GA_EMPTY;
245
246/*
247 * Put a value on the dict stack. This consumes "tv".
248 */
249 static int
250dict_stack_save(typval_T *tv)
251{
252 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000253 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200254 if (ga_grow(&dict_stack, 1) == FAIL)
255 return FAIL;
256 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
257 ++dict_stack.ga_len;
258 return OK;
259}
260
261/*
262 * Get the typval at top of the dict stack.
263 */
264 static typval_T *
265dict_stack_get_tv(void)
266{
267 if (dict_stack.ga_len == 0)
268 return NULL;
269 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
270}
271
272/*
273 * Get the dict at top of the dict stack.
274 */
275 static dict_T *
276dict_stack_get_dict(void)
277{
278 typval_T *tv;
279
280 if (dict_stack.ga_len == 0)
281 return NULL;
282 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
283 if (tv->v_type == VAR_DICT)
284 return tv->vval.v_dict;
285 return NULL;
286}
287
288/*
289 * Drop an item from the dict stack.
290 */
291 static void
292dict_stack_drop(void)
293{
294 if (dict_stack.ga_len == 0)
295 {
296 iemsg("Dict stack underflow");
297 return;
298 }
299 --dict_stack.ga_len;
300 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
301}
302
303/*
304 * Drop items from the dict stack until the length is equal to "len".
305 */
306 static void
307dict_stack_clear(int len)
308{
309 while (dict_stack.ga_len > len)
310 dict_stack_drop();
311}
312
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200313/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000314 * Get a pointer to useful "pt_outer" of "pt".
315 */
316 static outer_T *
317get_pt_outer(partial_T *pt)
318{
319 partial_T *ptref = pt->pt_outer_partial;
320
321 if (ptref == NULL)
322 return &pt->pt_outer;
323
324 // partial using partial (recursively)
325 while (ptref->pt_outer_partial != NULL)
326 ptref = ptref->pt_outer_partial;
327 return &ptref->pt_outer;
328}
329
330/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100331 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100332 * This adds a stack frame and sets the instruction pointer to the start of the
333 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200334 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335 *
336 * Stack has:
337 * - current arguments (already there)
338 * - omitted optional argument (default values) added here
339 * - stack frame:
340 * - pointer to calling function
341 * - Index of next instruction in calling function
342 * - previous frame pointer
343 * - reserved space for local variables
344 */
345 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100346call_dfunc(
347 int cdf_idx,
348 partial_T *pt,
349 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100350 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100351{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100352 int argcount = argcount_arg;
353 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
354 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200355 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100356 int arg_to_add;
357 int vararg_count = 0;
358 int varcount;
359 int idx;
360 estack_T *entry;
361 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200362 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000363 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364
365 if (dfunc->df_deleted)
366 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100367 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000368 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100369 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 return FAIL;
371 }
372
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100373#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100374 if (do_profiling == PROF_YES)
375 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200376 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100377 {
378 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
379 + profile_info_ga.ga_len;
380 ++profile_info_ga.ga_len;
381 CLEAR_POINTER(info);
382 profile_may_start_func(info, ufunc,
383 (((dfunc_T *)def_functions.ga_data)
384 + ectx->ec_dfunc_idx)->df_ufunc);
385 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100386 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100387#endif
388
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200389 // When debugging and using "cont" switches to the not-debugged
390 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000391 compile_type = get_compile_type(ufunc);
392 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200393 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000394 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200395
396 // compile_def_function() may cause def_functions.ga_data to change
397 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
398 }
399 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200400 {
401 if (did_emsg_cumul + did_emsg == did_emsg_before)
402 semsg(_(e_function_is_not_compiled_str),
403 printable_func_name(ufunc));
404 return FAIL;
405 }
406
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200407 if (ufunc->uf_va_name != NULL)
408 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200409 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200410 // Stack at time of call with 2 varargs:
411 // normal_arg
412 // optional_arg
413 // vararg_1
414 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200415 // After creating the list:
416 // normal_arg
417 // optional_arg
418 // vararg-list
419 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200420 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200421 // After creating the list
422 // normal_arg
423 // (space for optional_arg)
424 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200425 vararg_count = argcount - ufunc->uf_args.ga_len;
426 if (vararg_count < 0)
427 vararg_count = 0;
428 else
429 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200430 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200431 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200432
433 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200434 }
435
Bram Moolenaarfe270812020-04-11 22:31:27 +0200436 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200437 if (arg_to_add < 0)
438 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200439 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200440 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200441 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200442 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200443 return FAIL;
444 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000445 else if (arg_to_add > ufunc->uf_def_args.ga_len)
446 {
447 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
448
449 if (missing == 1)
450 emsg(_(e_one_argument_too_few));
451 else
452 semsg(_(e_nr_arguments_too_few), missing);
453 return FAIL;
454 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200455
456 // Reserve space for:
457 // - missing arguments
458 // - stack frame
459 // - local variables
460 // - if needed: a counter for number of closures created in
461 // ectx->ec_funcrefs.
462 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200463 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 return FAIL;
465
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100466 // If depth of calling is getting too high, don't execute the function.
467 if (funcdepth_increment() == FAIL)
468 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200469 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100470
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100471 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200472 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100473 {
474 floc = ALLOC_ONE(funclocal_T);
475 if (floc == NULL)
476 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200477 *floc = ectx->ec_funclocal;
478 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100479 }
480
Bram Moolenaarfe270812020-04-11 22:31:27 +0200481 // Move the vararg-list to below the missing optional arguments.
482 if (vararg_count > 0 && arg_to_add > 0)
483 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100484
485 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200486 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200487 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200488 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489
490 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100491 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
492 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200493 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200494 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
495 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100496 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100497 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200498 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100499
500 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200501 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000502 {
503 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
504
505 tv->v_type = VAR_NUMBER;
506 tv->vval.v_number = 0;
507 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200508 if (dfunc->df_has_closure)
509 {
510 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
511
512 tv->v_type = VAR_NUMBER;
513 tv->vval.v_number = 0;
514 }
515 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100517 if (pt != NULL || ufunc->uf_partial != NULL
518 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100519 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200520 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100521
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200522 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100523 return FAIL;
524 if (pt != NULL)
525 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000526 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200527 ++pt->pt_refcount;
528 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100529 }
530 else if (ufunc->uf_partial != NULL)
531 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000532 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200533 ++ufunc->uf_partial->pt_refcount;
534 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100535 }
536 else
537 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200538 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200539 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200540 {
541 vim_free(ref);
542 return FAIL;
543 }
544 ref->or_outer_allocated = TRUE;
545 ref->or_outer->out_stack = &ectx->ec_stack;
546 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
547 if (ectx->ec_outer_ref != NULL)
548 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100549 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200550 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100551 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100552 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200553 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100554
Bram Moolenaarc970e422021-03-17 15:03:04 +0100555 ++ufunc->uf_calls;
556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557 // Set execution state to the start of the called function.
558 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100559 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100560 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200561 if (entry != NULL)
562 {
563 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200564 // Save the current context so it can be restored on return.
565 entry->es_save_sctx = current_sctx;
566 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200567 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100568
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200569 // Start execution at the first instruction.
570 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571
572 return OK;
573}
574
575// Get pointer to item in the stack.
576#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
577
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000578// Double linked list of funcstack_T in use.
579static funcstack_T *first_funcstack = NULL;
580
581 static void
582add_funcstack_to_list(funcstack_T *funcstack)
583{
584 // Link in list of funcstacks.
585 if (first_funcstack != NULL)
586 first_funcstack->fs_prev = funcstack;
587 funcstack->fs_next = first_funcstack;
588 funcstack->fs_prev = NULL;
589 first_funcstack = funcstack;
590}
591
592 static void
593remove_funcstack_from_list(funcstack_T *funcstack)
594{
595 if (funcstack->fs_prev == NULL)
596 first_funcstack = funcstack->fs_next;
597 else
598 funcstack->fs_prev->fs_next = funcstack->fs_next;
599 if (funcstack->fs_next != NULL)
600 funcstack->fs_next->fs_prev = funcstack->fs_prev;
601}
602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200604 * Used when returning from a function: Check if any closure is still
605 * referenced. If so then move the arguments and variables to a separate piece
606 * of stack to be used when the closure is called.
607 * When "free_arguments" is TRUE the arguments are to be freed.
608 * Returns FAIL when out of memory.
609 */
610 static int
611handle_closure_in_use(ectx_T *ectx, int free_arguments)
612{
613 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
614 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200615 int argcount;
616 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200617 int idx;
618 typval_T *tv;
619 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200620 garray_T *gap = &ectx->ec_funcrefs;
621 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200622
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200623 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200624 return OK; // function was freed
625 if (dfunc->df_has_closure == 0)
626 return OK; // no closures
627 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
628 closure_count = tv->vval.v_number;
629 if (closure_count == 0)
630 return OK; // no funcrefs created
631
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200632 argcount = ufunc_argcount(dfunc->df_ufunc);
633 top = ectx->ec_frame_idx - argcount;
634
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200635 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200636 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200637 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200638 partial_T *pt;
639 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200640
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200641 if (off < 0)
642 continue; // count is off or already done
643 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200644 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200645 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200646 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200647 int i;
648
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000649 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200650 // unreferenced on return.
651 for (i = 0; i < dfunc->df_varcount; ++i)
652 {
653 typval_T *stv = STACK_TV(ectx->ec_frame_idx
654 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200655 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200656 --refcount;
657 }
658 if (refcount > 1)
659 {
660 closure_in_use = TRUE;
661 break;
662 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200663 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200664 }
665
666 if (closure_in_use)
667 {
668 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
669 typval_T *stack;
670
671 // A closure is using the arguments and/or local variables.
672 // Move them to the called function.
673 if (funcstack == NULL)
674 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000675
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200676 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
677 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200678 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
679 funcstack->fs_ga.ga_data = stack;
680 if (stack == NULL)
681 {
682 vim_free(funcstack);
683 return FAIL;
684 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000685 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200686
687 // Move or copy the arguments.
688 for (idx = 0; idx < argcount; ++idx)
689 {
690 tv = STACK_TV(top + idx);
691 if (free_arguments)
692 {
693 *(stack + idx) = *tv;
694 tv->v_type = VAR_UNKNOWN;
695 }
696 else
697 copy_tv(tv, stack + idx);
698 }
699 // Move the local variables.
700 for (idx = 0; idx < dfunc->df_varcount; ++idx)
701 {
702 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200703
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200704 // A partial created for a local function, that is also used as a
705 // local variable, has a reference count for the variable, thus
706 // will never go down to zero. When all these refcounts are one
707 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000708 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200709 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
710 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200711 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200712
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200713 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200714 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
715 gap->ga_len - closure_count + i])
716 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200717 }
718
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200719 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 tv->v_type = VAR_UNKNOWN;
721 }
722
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200723 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200724 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200725 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
726 - closure_count + idx];
727 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200728 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200729 ++funcstack->fs_refcount;
730 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100731 pt->pt_outer.out_stack = &funcstack->fs_ga;
732 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200733 }
734 }
735 }
736
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200737 for (idx = 0; idx < closure_count; ++idx)
738 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
739 - closure_count + idx]);
740 gap->ga_len -= closure_count;
741 if (gap->ga_len == 0)
742 ga_clear(gap);
743
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200744 return OK;
745}
746
747/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200748 * Called when a partial is freed or its reference count goes down to one. The
749 * funcstack may be the only reference to the partials in the local variables.
750 * Go over all of them, the funcref and can be freed if all partials
751 * referencing the funcstack have a reference count of one.
752 */
753 void
754funcstack_check_refcount(funcstack_T *funcstack)
755{
756 int i;
757 garray_T *gap = &funcstack->fs_ga;
758 int done = 0;
759
760 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
761 return;
762 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
763 {
764 typval_T *tv = ((typval_T *)gap->ga_data) + i;
765
766 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
767 && tv->vval.v_partial->pt_funcstack == funcstack
768 && tv->vval.v_partial->pt_refcount == 1)
769 ++done;
770 }
771 if (done == funcstack->fs_min_refcount)
772 {
773 typval_T *stack = gap->ga_data;
774
775 // All partials referencing the funcstack have a reference count of
776 // one, thus the funcstack is no longer of use.
777 for (i = 0; i < gap->ga_len; ++i)
778 clear_tv(stack + i);
779 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000780 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200781 vim_free(funcstack);
782 }
783}
784
785/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000786 * For garbage collecting: set references in all variables referenced by
787 * all funcstacks.
788 */
789 int
790set_ref_in_funcstacks(int copyID)
791{
792 funcstack_T *funcstack;
793
794 for (funcstack = first_funcstack; funcstack != NULL;
795 funcstack = funcstack->fs_next)
796 {
797 typval_T *stack = funcstack->fs_ga.ga_data;
798 int i;
799
800 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
801 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
802 return TRUE; // abort
803 }
804 return FALSE;
805}
806
807/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 * Return from the current function.
809 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200810 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200811func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100814 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200815 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
816 + ectx->ec_dfunc_idx;
817 int argcount = ufunc_argcount(dfunc->df_ufunc);
818 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200819 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100820 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
821 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200822 funclocal_T *floc;
823#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100824 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
825 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826
Bram Moolenaar12d26532021-02-19 19:13:21 +0100827 if (do_profiling == PROF_YES)
828 {
829 ufunc_T *caller = prev_dfunc->df_ufunc;
830
831 if (dfunc->df_ufunc->uf_profiling
832 || (caller != NULL && caller->uf_profiling))
833 {
834 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
835 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
836 --profile_info_ga.ga_len;
837 }
838 }
839#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000840
841 // No check for uf_refcount being zero, cannot think of a way that would
842 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100843 --dfunc->df_ufunc->uf_calls;
844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200846 entry = estack_pop();
847 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200848 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200850 if (handle_closure_in_use(ectx, TRUE) == FAIL)
851 return FAIL;
852
853 // Clear the arguments.
854 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
855 clear_tv(STACK_TV(idx));
856
857 // Clear local variables and temp values, but not the return value.
858 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100859 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100861
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100862 // The return value should be on top of the stack. However, when aborting
863 // it may not be there and ec_frame_idx is the top of the stack.
864 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100865 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100866 ret_idx = 0;
867
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200868 if (ectx->ec_outer_ref != NULL)
869 {
870 if (ectx->ec_outer_ref->or_outer_allocated)
871 vim_free(ectx->ec_outer_ref->or_outer);
872 partial_unref(ectx->ec_outer_ref->or_partial);
873 vim_free(ectx->ec_outer_ref);
874 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100875
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100876 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100877 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100878 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
879 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200880 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
881 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200882 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100883 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100884 floc = (void *)STACK_TV(ectx->ec_frame_idx
885 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200886 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100887 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
888 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200889
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100890 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200891 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100892 else
893 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200894 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100895 vim_free(floc);
896 }
897
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100898 if (ret_idx > 0)
899 {
900 // Reset the stack to the position before the call, with a spot for the
901 // return value, moved there from above the frame.
902 ectx->ec_stack.ga_len = top + 1;
903 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
904 }
905 else
906 // Reset the stack to the position before the call.
907 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200908
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100909 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200910 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200911 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912}
913
914#undef STACK_TV
915
916/*
917 * Prepare arguments and rettv for calling a builtin or user function.
918 */
919 static int
920call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
921{
922 int idx;
923 typval_T *tv;
924
925 // Move arguments from bottom of the stack to argvars[] and add terminator.
926 for (idx = 0; idx < argcount; ++idx)
927 argvars[idx] = *STACK_TV_BOT(idx - argcount);
928 argvars[argcount].v_type = VAR_UNKNOWN;
929
930 // Result replaces the arguments on the stack.
931 if (argcount > 0)
932 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200933 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 return FAIL;
935 else
936 ++ectx->ec_stack.ga_len;
937
938 // Default return value is zero.
939 tv = STACK_TV_BOT(-1);
940 tv->v_type = VAR_NUMBER;
941 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +0100942 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943
944 return OK;
945}
946
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200947// Ugly global to avoid passing the execution context around through many
948// layers.
949static ectx_T *current_ectx = NULL;
950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951/*
952 * Call a builtin function by index.
953 */
954 static int
955call_bfunc(int func_idx, int argcount, ectx_T *ectx)
956{
957 typval_T argvars[MAX_FUNC_ARGS];
958 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100959 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200960 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200961 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962
963 if (call_prepare(argcount, argvars, ectx) == FAIL)
964 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200965 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200967 // Call the builtin function. Set "current_ectx" so that when it
968 // recursively invokes call_def_function() a closure context can be set.
969 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200971 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200972 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973
974 // Clear the arguments.
975 for (idx = 0; idx < argcount; ++idx)
976 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200977
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100978 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200979 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 return OK;
981}
982
983/*
984 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100985 * If the function is compiled this will add a stack frame and set the
986 * instruction pointer at the start of the function.
987 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200988 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100989 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 */
991 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100992call_ufunc(
993 ufunc_T *ufunc,
994 partial_T *pt,
995 int argcount,
996 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200997 isn_T *iptr,
998 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999{
1000 typval_T argvars[MAX_FUNC_ARGS];
1001 funcexe_T funcexe;
1002 int error;
1003 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001004 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001005 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006
Bram Moolenaare99d4222021-06-13 14:01:26 +02001007 if (func_needs_compiling(ufunc, compile_type)
1008 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1009 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001010 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001011 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001012 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001013 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001014 if (error != FCERR_UNKNOWN)
1015 {
1016 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001017 semsg(_(e_too_many_arguments_for_function_str),
1018 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001019 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001020 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001021 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001022 return FAIL;
1023 }
1024
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001025 // The function has been compiled, can call it quickly. For a function
1026 // that was defined later: we can call it directly next time.
1027 if (iptr != NULL)
1028 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001029 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001030 iptr->isn_type = ISN_DCALL;
1031 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1032 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1033 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001034 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036
1037 if (call_prepare(argcount, argvars, ectx) == FAIL)
1038 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001039 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001040 funcexe.fe_evaluate = TRUE;
1041 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042
1043 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001045 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046
1047 // Clear the arguments.
1048 for (idx = 0; idx < argcount; ++idx)
1049 clear_tv(&argvars[idx]);
1050
1051 if (error != FCERR_NONE)
1052 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001053 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 return FAIL;
1055 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001056 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001057 // Error other than from calling the function itself.
1058 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 return OK;
1060}
1061
1062/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001063 * If command modifiers were applied restore them.
1064 */
1065 static void
1066may_restore_cmdmod(funclocal_T *funclocal)
1067{
1068 if (funclocal->floc_restore_cmdmod)
1069 {
1070 cmdmod.cmod_filter_regmatch.regprog = NULL;
1071 undo_cmdmod(&cmdmod);
1072 cmdmod = funclocal->floc_save_cmdmod;
1073 funclocal->floc_restore_cmdmod = FALSE;
1074 }
1075}
1076
1077/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001078 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1079 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001080 */
1081 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001082vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001083{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001084 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001085}
1086
1087/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 * Execute a function by "name".
1089 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001090 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 * Returns FAIL if not found without an error message.
1092 */
1093 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001094call_by_name(
1095 char_u *name,
1096 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001097 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001098 isn_T *iptr,
1099 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100{
1101 ufunc_T *ufunc;
1102
1103 if (builtin_function(name, -1))
1104 {
1105 int func_idx = find_internal_func(name);
1106
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001107 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001109 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 return FAIL;
1111 return call_bfunc(func_idx, argcount, ectx);
1112 }
1113
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001114 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001115
1116 if (ufunc == NULL)
1117 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001118 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001119
1120 if (script_autoload(name, TRUE))
1121 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001122 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001123
1124 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001125 return FAIL; // bail out if loading the script caused an error
1126 }
1127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001129 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001130 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001131 {
1132 int i;
1133 typval_T *argv = STACK_TV_BOT(0) - argcount;
1134
1135 // The function can change at runtime, check that the argument
1136 // types are correct.
1137 for (i = 0; i < argcount; ++i)
1138 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001139 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001140
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001141 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001142 type = ufunc->uf_arg_types[i];
1143 else if (ufunc->uf_va_type != NULL)
1144 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001145 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001146 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001147 return FAIL;
1148 }
1149 }
1150
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001151 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001152 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153
1154 return FAIL;
1155}
1156
1157 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001158call_partial(
1159 typval_T *tv,
1160 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001161 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001163 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001164 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001166 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001167 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168
1169 if (tv->v_type == VAR_PARTIAL)
1170 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001171 partial_T *pt = tv->vval.v_partial;
1172 int i;
1173
1174 if (pt->pt_argc > 0)
1175 {
1176 // Make space for arguments from the partial, shift the "argcount"
1177 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001178 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001179 return FAIL;
1180 for (i = 1; i <= argcount; ++i)
1181 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1182 ectx->ec_stack.ga_len += pt->pt_argc;
1183 argcount += pt->pt_argc;
1184
1185 // copy the arguments from the partial onto the stack
1186 for (i = 0; i < pt->pt_argc; ++i)
1187 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1188 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001189 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190
1191 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001192 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 name = pt->pt_name;
1195 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001196 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001198 if (name != NULL)
1199 {
1200 char_u fname_buf[FLEN_FIXED + 1];
1201 char_u *tofree = NULL;
1202 int error = FCERR_NONE;
1203 char_u *fname;
1204
1205 // May need to translate <SNR>123_ to K_SNR.
1206 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1207 if (error != FCERR_NONE)
1208 res = FAIL;
1209 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001210 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001211 vim_free(tofree);
1212 }
1213
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001214 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 {
1216 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001217 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001218 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 return FAIL;
1220 }
1221 return OK;
1222}
1223
1224/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001225 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1226 * TRUE.
1227 */
1228 static int
1229error_if_locked(int lock, char *error)
1230{
1231 if (lock & (VAR_LOCKED | VAR_FIXED))
1232 {
1233 emsg(_(error));
1234 return TRUE;
1235 }
1236 return FALSE;
1237}
1238
1239/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001240 * Give an error if "tv" is not a number and return FAIL.
1241 */
1242 static int
1243check_for_number(typval_T *tv)
1244{
1245 if (tv->v_type != VAR_NUMBER)
1246 {
1247 semsg(_(e_expected_str_but_got_str),
1248 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1249 return FAIL;
1250 }
1251 return OK;
1252}
1253
1254/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001255 * Store "tv" in variable "name".
1256 * This is for s: and g: variables.
1257 */
1258 static void
1259store_var(char_u *name, typval_T *tv)
1260{
1261 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001262 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001263
Bram Moolenaard877a572021-04-01 19:42:48 +02001264 if (tv->v_lock)
1265 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001266 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001267 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001268 restore_funccal();
1269}
1270
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001271/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001272 * Convert "tv" to a string.
1273 * Return FAIL if not allowed.
1274 */
1275 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001276do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001277{
1278 if (tv->v_type != VAR_STRING)
1279 {
1280 char_u *str;
1281
1282 if (is_2string_any)
1283 {
1284 switch (tv->v_type)
1285 {
1286 case VAR_SPECIAL:
1287 case VAR_BOOL:
1288 case VAR_NUMBER:
1289 case VAR_FLOAT:
1290 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001291
1292 case VAR_LIST:
1293 if (tolerant)
1294 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001295 char_u *s, *e, *p;
1296 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001297
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001298 ga_init2(&ga, sizeof(char_u *), 1);
1299
1300 // Convert to NL separated items, then
1301 // escape the items and replace the NL with
1302 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001303 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001304 if (str == NULL)
1305 return FAIL;
1306 s = str;
1307 while ((e = vim_strchr(s, '\n')) != NULL)
1308 {
1309 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001310 p = vim_strsave_fnameescape(s,
1311 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001312 if (p != NULL)
1313 {
1314 ga_concat(&ga, p);
1315 ga_concat(&ga, (char_u *)" ");
1316 vim_free(p);
1317 }
1318 s = e + 1;
1319 }
1320 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001321 clear_tv(tv);
1322 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001323 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001324 return OK;
1325 }
1326 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001327 default: to_string_error(tv->v_type);
1328 return FAIL;
1329 }
1330 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001331 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001332 clear_tv(tv);
1333 tv->v_type = VAR_STRING;
1334 tv->vval.v_string = str;
1335 }
1336 return OK;
1337}
1338
1339/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001340 * When the value of "sv" is a null list of dict, allocate it.
1341 */
1342 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001343allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001344{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001345 typval_T *tv = sv->sv_tv;
1346
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001347 switch (tv->v_type)
1348 {
1349 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001350 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001351 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001352 break;
1353 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001354 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001355 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001356 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001357 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001358 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001359 (void)rettv_blob_alloc(tv);
1360 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001361 default:
1362 break;
1363 }
1364}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001365
Bram Moolenaare7525c52021-01-09 13:20:37 +01001366/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001367 * Return the character "str[index]" where "index" is the character index,
1368 * including composing characters.
1369 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001370 */
1371 char_u *
1372char_from_string(char_u *str, varnumber_T index)
1373{
1374 size_t nbyte = 0;
1375 varnumber_T nchar = index;
1376 size_t slen;
1377
1378 if (str == NULL)
1379 return NULL;
1380 slen = STRLEN(str);
1381
Bram Moolenaarff871402021-03-26 13:34:05 +01001382 // Do the same as for a list: a negative index counts from the end.
1383 // Optimization to check the first byte to be below 0x80 (and no composing
1384 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001385 if (index < 0)
1386 {
1387 int clen = 0;
1388
1389 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001390 {
1391 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1392 ++nbyte;
1393 else if (enc_utf8)
1394 nbyte += utfc_ptr2len(str + nbyte);
1395 else
1396 nbyte += mb_ptr2len(str + nbyte);
1397 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001398 nchar = clen + index;
1399 if (nchar < 0)
1400 // unlike list: index out of range results in empty string
1401 return NULL;
1402 }
1403
1404 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001405 {
1406 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1407 ++nbyte;
1408 else if (enc_utf8)
1409 nbyte += utfc_ptr2len(str + nbyte);
1410 else
1411 nbyte += mb_ptr2len(str + nbyte);
1412 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001413 if (nbyte >= slen)
1414 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001415 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001416}
1417
1418/*
1419 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001420 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001421 * If going over the end return "str_len".
1422 * If "idx" is negative count from the end, -1 is the last character.
1423 * When going over the start return -1.
1424 */
1425 static long
1426char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1427{
1428 varnumber_T nchar = idx;
1429 size_t nbyte = 0;
1430
1431 if (nchar >= 0)
1432 {
1433 while (nchar > 0 && nbyte < str_len)
1434 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001435 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001436 --nchar;
1437 }
1438 }
1439 else
1440 {
1441 nbyte = str_len;
1442 while (nchar < 0 && nbyte > 0)
1443 {
1444 --nbyte;
1445 nbyte -= mb_head_off(str, str + nbyte);
1446 ++nchar;
1447 }
1448 if (nchar < 0)
1449 return -1;
1450 }
1451 return (long)nbyte;
1452}
1453
1454/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001455 * Return the slice "str[first : last]" using character indexes. Composing
1456 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001457 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001458 * Return NULL when the result is empty.
1459 */
1460 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001461string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001462{
1463 long start_byte, end_byte;
1464 size_t slen;
1465
1466 if (str == NULL)
1467 return NULL;
1468 slen = STRLEN(str);
1469 start_byte = char_idx2byte(str, slen, first);
1470 if (start_byte < 0)
1471 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001472 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001473 end_byte = (long)slen;
1474 else
1475 {
1476 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001477 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001478 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001479 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001480 }
1481
1482 if (start_byte >= (long)slen || end_byte <= start_byte)
1483 return NULL;
1484 return vim_strnsave(str + start_byte, end_byte - start_byte);
1485}
1486
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001487/*
1488 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1489 * When "dfunc_idx" is negative don't give an error.
1490 * Returns NULL for an error.
1491 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001492 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001493get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001494{
1495 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001496 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1497 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001498 svar_T *sv;
1499
1500 if (sref->sref_seq != si->sn_script_seq)
1501 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001502 // The script was reloaded after the function was compiled, the
1503 // script_idx may not be valid.
1504 if (dfunc != NULL)
1505 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1506 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001507 return NULL;
1508 }
1509 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001510 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001511 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001512 if (dfunc != NULL)
1513 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001514 return NULL;
1515 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001516
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001517 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1518 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001519 {
1520 if (dfunc != NULL)
1521 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1522 return NULL;
1523 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001524 return sv;
1525}
1526
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001527/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001528 * Function passed to do_cmdline() for splitting a script joined by NL
1529 * characters.
1530 */
1531 static char_u *
1532get_split_sourceline(
1533 int c UNUSED,
1534 void *cookie,
1535 int indent UNUSED,
1536 getline_opt_T options UNUSED)
1537{
1538 source_cookie_T *sp = (source_cookie_T *)cookie;
1539 char_u *p;
1540 char_u *line;
1541
Bram Moolenaar20677332021-06-06 17:02:53 +02001542 p = vim_strchr(sp->nextline, '\n');
1543 if (p == NULL)
1544 {
1545 line = vim_strsave(sp->nextline);
1546 sp->nextline += STRLEN(sp->nextline);
1547 }
1548 else
1549 {
1550 line = vim_strnsave(sp->nextline, p - sp->nextline);
1551 sp->nextline = p + 1;
1552 }
1553 return line;
1554}
1555
1556/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 * Execute a function by "name".
1558 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001559 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 */
1561 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001562call_eval_func(
1563 char_u *name,
1564 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001565 ectx_T *ectx,
1566 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567{
Bram Moolenaared677f52020-08-12 16:38:10 +02001568 int called_emsg_before = called_emsg;
1569 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001571 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001572 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001574 dictitem_T *v;
1575
1576 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001577 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1578 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001579 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001580 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001581 return FAIL;
1582 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001583 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001585 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586}
1587
1588/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001589 * When a function reference is used, fill a partial with the information
1590 * needed, especially when it is used as a closure.
1591 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001592 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001593fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1594{
1595 pt->pt_func = ufunc;
1596 pt->pt_refcount = 1;
1597
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001598 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001599 {
1600 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1601 + ectx->ec_dfunc_idx;
1602
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001603 // The closure may need to find arguments and local variables in the
1604 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001605 pt->pt_outer.out_stack = &ectx->ec_stack;
1606 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001607 if (ectx->ec_outer_ref != NULL)
1608 {
1609 // The current context already has a context, link to that one.
1610 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1611 if (ectx->ec_outer_ref->or_partial != NULL)
1612 {
1613 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1614 ++pt->pt_outer.out_up_partial->pt_refcount;
1615 }
1616 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001617
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001618 // If this function returns and the closure is still being used, we
1619 // need to make a copy of the context (arguments and local variables).
1620 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001621 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001622 {
1623 vim_free(pt);
1624 return FAIL;
1625 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001626 // Extra variable keeps the count of closures created in the current
1627 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001628 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1629 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1630
1631 ((partial_T **)ectx->ec_funcrefs.ga_data)
1632 [ectx->ec_funcrefs.ga_len] = pt;
1633 ++pt->pt_refcount;
1634 ++ectx->ec_funcrefs.ga_len;
1635 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001636 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001637 return OK;
1638}
1639
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001640/*
1641 * Execute iptr->isn_arg.string as an Ex command.
1642 */
1643 static int
1644exec_command(isn_T *iptr)
1645{
1646 source_cookie_T cookie;
1647
1648 SOURCING_LNUM = iptr->isn_lnum;
1649 // Pass getsourceline to get an error for a missing ":end"
1650 // command.
1651 CLEAR_FIELD(cookie);
1652 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1653 if (do_cmdline(iptr->isn_arg.string,
1654 getsourceline, &cookie,
1655 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1656 || did_emsg)
1657 return FAIL;
1658 return OK;
1659}
1660
Bram Moolenaar89445512022-04-14 12:58:23 +01001661/*
1662 * If script "sid" is not loaded yet then load it now.
1663 * Caller must make sure "sid" is a valid script ID.
1664 * "loaded" is set to TRUE if the script had to be loaded.
1665 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1666 */
1667 int
1668may_load_script(int sid, int *loaded)
1669{
1670 scriptitem_T *si = SCRIPT_ITEM(sid);
1671
1672 if (si->sn_state == SN_STATE_NOT_LOADED)
1673 {
1674 *loaded = TRUE;
1675 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1676 {
1677 semsg(_(e_cant_open_file_str), si->sn_name);
1678 return FAIL;
1679 }
1680 }
1681 return OK;
1682}
1683
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001684// used for v_instr of typval of VAR_INSTR
1685struct instr_S {
1686 ectx_T *instr_ectx;
1687 isn_T *instr_instr;
1688};
1689
Bram Moolenaar4c137212021-04-19 16:48:48 +02001690// used for substitute_instr
1691typedef struct subs_expr_S {
1692 ectx_T *subs_ectx;
1693 isn_T *subs_instr;
1694 int subs_status;
1695} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696
1697// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001698#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699
1700// Get pointer to item at the bottom of the stack, -1 is the bottom.
1701#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001702#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 +01001703
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001704// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001705#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001707// Set when calling do_debug().
1708static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001709static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001710
1711/*
1712 * When debugging lookup "name" and return the typeval.
1713 * When not found return NULL.
1714 */
1715 typval_T *
1716lookup_debug_var(char_u *name)
1717{
1718 int idx;
1719 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001720 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001721 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001722 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001723
1724 if (ectx == NULL)
1725 return NULL;
1726 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1727
1728 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001729 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001730 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001731 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1732
1733 // the variable name may be NULL when not available in this block
1734 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001735 return STACK_TV_VAR(idx);
1736 }
1737
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001738 // Go through argument names.
1739 ufunc = dfunc->df_ufunc;
1740 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1741 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1742 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1743 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1744 - varargs_off + idx);
1745 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1746 return STACK_TV(ectx->ec_frame_idx - 1);
1747
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001748 return NULL;
1749}
1750
Bram Moolenaar26a44842021-09-02 18:49:06 +02001751/*
1752 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1753 * breakpoint was set in that function or when there is any expression.
1754 */
1755 int
1756may_break_in_function(ufunc_T *ufunc)
1757{
1758 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1759}
1760
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001761 static void
1762handle_debug(isn_T *iptr, ectx_T *ectx)
1763{
1764 char_u *line;
1765 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1766 + ectx->ec_dfunc_idx)->df_ufunc;
1767 isn_T *ni;
1768 int end_lnum = iptr->isn_lnum;
1769 garray_T ga;
1770 int lnum;
1771
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001772 if (ex_nesting_level > debug_break_level)
1773 {
1774 linenr_T breakpoint;
1775
Bram Moolenaar26a44842021-09-02 18:49:06 +02001776 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001777 return;
1778
1779 // check for the next breakpoint if needed
1780 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001781 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001782 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1783 return;
1784 }
1785
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001786 SOURCING_LNUM = iptr->isn_lnum;
1787 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001788 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001789
1790 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1791 if (ni->isn_type == ISN_DEBUG
1792 || ni->isn_type == ISN_RETURN
1793 || ni->isn_type == ISN_RETURN_VOID)
1794 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001795 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001796 break;
1797 }
1798
1799 if (end_lnum > iptr->isn_lnum)
1800 {
1801 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001802 for (lnum = iptr->isn_lnum; lnum < end_lnum
1803 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001804 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001805 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001806
Bram Moolenaar303215d2021-07-07 20:10:43 +02001807 if (p == NULL)
1808 continue; // left over from continuation line
1809 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001810 if (*p == '#')
1811 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001812 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001813 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1814 if (STRNCMP(p, "def ", 4) == 0)
1815 break;
1816 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001817 line = ga_concat_strings(&ga, " ");
1818 vim_free(ga.ga_data);
1819 }
1820 else
1821 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001822
Dominique Pelle6e969552021-06-17 13:53:41 +02001823 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001824 debug_context = NULL;
1825
1826 if (end_lnum > iptr->isn_lnum)
1827 vim_free(line);
1828}
1829
Bram Moolenaar4c137212021-04-19 16:48:48 +02001830/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001831 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001832 * Returns OK, FAIL or NOTDONE (uncatchable error).
1833 */
1834 static int
1835execute_storeindex(isn_T *iptr, ectx_T *ectx)
1836{
1837 vartype_T dest_type = iptr->isn_arg.vartype;
1838 typval_T *tv;
1839 typval_T *tv_idx = STACK_TV_BOT(-2);
1840 typval_T *tv_dest = STACK_TV_BOT(-1);
1841 int status = OK;
1842
1843 // Stack contains:
1844 // -3 value to be stored
1845 // -2 index
1846 // -1 dict or list
1847 tv = STACK_TV_BOT(-3);
1848 SOURCING_LNUM = iptr->isn_lnum;
1849 if (dest_type == VAR_ANY)
1850 {
1851 dest_type = tv_dest->v_type;
1852 if (dest_type == VAR_DICT)
1853 status = do_2string(tv_idx, TRUE, FALSE);
1854 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1855 {
1856 emsg(_(e_number_expected));
1857 status = FAIL;
1858 }
1859 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001860
1861 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001862 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001863 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001864 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001865 long lidx = (long)tv_idx->vval.v_number;
1866 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001867
Bram Moolenaare08be092022-02-17 13:08:26 +00001868 if (list == NULL)
1869 {
1870 emsg(_(e_list_not_set));
1871 return FAIL;
1872 }
1873 if (lidx < 0 && list->lv_len + lidx >= 0)
1874 // negative index is relative to the end
1875 lidx = list->lv_len + lidx;
1876 if (lidx < 0 || lidx > list->lv_len)
1877 {
1878 semsg(_(e_list_index_out_of_range_nr), lidx);
1879 return FAIL;
1880 }
1881 if (lidx < list->lv_len)
1882 {
1883 listitem_T *li = list_find(list, lidx);
1884
1885 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001886 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001887 return FAIL;
1888 // overwrite existing list item
1889 clear_tv(&li->li_tv);
1890 li->li_tv = *tv;
1891 }
1892 else
1893 {
1894 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1895 return FAIL;
1896 // append to list, only fails when out of memory
1897 if (list_append_tv(list, tv) == FAIL)
1898 return NOTDONE;
1899 clear_tv(tv);
1900 }
1901 }
1902 else if (dest_type == VAR_DICT)
1903 {
1904 char_u *key = tv_idx->vval.v_string;
1905 dict_T *dict = tv_dest->vval.v_dict;
1906 dictitem_T *di;
1907
1908 SOURCING_LNUM = iptr->isn_lnum;
1909 if (dict == NULL)
1910 {
1911 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001912 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001913 }
1914 if (key == NULL)
1915 key = (char_u *)"";
1916 di = dict_find(dict, key, -1);
1917 if (di != NULL)
1918 {
1919 if (error_if_locked(di->di_tv.v_lock,
1920 e_cannot_change_dict_item))
1921 return FAIL;
1922 // overwrite existing value
1923 clear_tv(&di->di_tv);
1924 di->di_tv = *tv;
1925 }
1926 else
1927 {
1928 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1929 return FAIL;
1930 // add to dict, only fails when out of memory
1931 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1932 return NOTDONE;
1933 clear_tv(tv);
1934 }
1935 }
1936 else if (dest_type == VAR_BLOB)
1937 {
1938 long lidx = (long)tv_idx->vval.v_number;
1939 blob_T *blob = tv_dest->vval.v_blob;
1940 varnumber_T nr;
1941 int error = FALSE;
1942 int len;
1943
1944 if (blob == NULL)
1945 {
1946 emsg(_(e_blob_not_set));
1947 return FAIL;
1948 }
1949 len = blob_len(blob);
1950 if (lidx < 0 && len + lidx >= 0)
1951 // negative index is relative to the end
1952 lidx = len + lidx;
1953
1954 // Can add one byte at the end.
1955 if (lidx < 0 || lidx > len)
1956 {
1957 semsg(_(e_blob_index_out_of_range_nr), lidx);
1958 return FAIL;
1959 }
1960 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1961 return FAIL;
1962 nr = tv_get_number_chk(tv, &error);
1963 if (error)
1964 return FAIL;
1965 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001966 }
1967 else
1968 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001969 status = FAIL;
1970 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001971 }
1972 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001973
1974 clear_tv(tv_idx);
1975 clear_tv(tv_dest);
1976 ectx->ec_stack.ga_len -= 3;
1977 if (status == FAIL)
1978 {
1979 clear_tv(tv);
1980 return FAIL;
1981 }
1982 return OK;
1983}
1984
1985/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001986 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001987 */
1988 static int
1989execute_storerange(isn_T *iptr, ectx_T *ectx)
1990{
1991 typval_T *tv;
1992 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1993 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1994 typval_T *tv_dest = STACK_TV_BOT(-1);
1995 int status = OK;
1996
1997 // Stack contains:
1998 // -4 value to be stored
1999 // -3 first index or "none"
2000 // -2 second index or "none"
2001 // -1 destination list or blob
2002 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002003 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002004 if (tv_dest->v_type == VAR_LIST)
2005 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002006 long n1;
2007 long n2;
2008 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002009
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002010 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2011 if (tv_idx2->v_type == VAR_SPECIAL
2012 && tv_idx2->vval.v_number == VVAL_NONE)
2013 n2 = list_len(tv_dest->vval.v_list) - 1;
2014 else
2015 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2016
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002017 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002018 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002019 status = FAIL;
2020 else
2021 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002022 status = check_range_index_two(tv_dest->vval.v_list,
2023 &n1, li1, &n2, FALSE);
2024 if (status != FAIL)
2025 status = list_assign_range(
2026 tv_dest->vval.v_list,
2027 tv->vval.v_list,
2028 n1,
2029 n2,
2030 tv_idx2->v_type == VAR_SPECIAL,
2031 (char_u *)"=",
2032 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002033 }
2034 }
2035 else if (tv_dest->v_type == VAR_BLOB)
2036 {
2037 varnumber_T n1;
2038 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002039 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002040
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002041 n1 = tv_get_number_chk(tv_idx1, NULL);
2042 if (tv_idx2->v_type == VAR_SPECIAL
2043 && tv_idx2->vval.v_number == VVAL_NONE)
2044 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2045 else
2046 n2 = tv_get_number_chk(tv_idx2, NULL);
2047 bloblen = blob_len(tv_dest->vval.v_blob);
2048
2049 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2050 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002051 status = FAIL;
2052 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002053 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002054 }
2055 else
2056 {
2057 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002058 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002059 }
2060
2061 clear_tv(tv_idx1);
2062 clear_tv(tv_idx2);
2063 clear_tv(tv_dest);
2064 ectx->ec_stack.ga_len -= 4;
2065 clear_tv(tv);
2066
2067 return status;
2068}
2069
2070/*
2071 * Unlet item in list or dict variable.
2072 */
2073 static int
2074execute_unletindex(isn_T *iptr, ectx_T *ectx)
2075{
2076 typval_T *tv_idx = STACK_TV_BOT(-2);
2077 typval_T *tv_dest = STACK_TV_BOT(-1);
2078 int status = OK;
2079
2080 // Stack contains:
2081 // -2 index
2082 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002083 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002084 if (tv_dest->v_type == VAR_DICT)
2085 {
2086 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002087 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002088 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002089 semsg(_(e_expected_str_but_got_str),
2090 vartype_name(VAR_STRING),
2091 vartype_name(tv_idx->v_type));
2092 status = FAIL;
2093 }
2094 else
2095 {
2096 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002097 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002098 dictitem_T *di = NULL;
2099
2100 if (d != NULL && value_check_lock(
2101 d->dv_lock, NULL, FALSE))
2102 status = FAIL;
2103 else
2104 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002105 if (tv_idx->v_type == VAR_STRING)
2106 {
2107 key = tv_idx->vval.v_string;
2108 if (key == NULL)
2109 key = (char_u *)"";
2110 }
2111 else
2112 {
2113 key = tv_get_string(tv_idx);
2114 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002115 if (d != NULL)
2116 di = dict_find(d, key, (int)STRLEN(key));
2117 if (di == NULL)
2118 {
2119 // NULL dict is equivalent to empty dict
2120 semsg(_(e_key_not_present_in_dictionary),
2121 key);
2122 status = FAIL;
2123 }
2124 else if (var_check_fixed(di->di_flags,
2125 NULL, FALSE)
2126 || var_check_ro(di->di_flags,
2127 NULL, FALSE))
2128 status = FAIL;
2129 else
2130 dictitem_remove(d, di);
2131 }
2132 }
2133 }
2134 else if (tv_dest->v_type == VAR_LIST)
2135 {
2136 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002137 if (check_for_number(tv_idx) == FAIL)
2138 {
2139 status = FAIL;
2140 }
2141 else
2142 {
2143 list_T *l = tv_dest->vval.v_list;
2144 long n = (long)tv_idx->vval.v_number;
2145
2146 if (l != NULL && value_check_lock(
2147 l->lv_lock, NULL, FALSE))
2148 status = FAIL;
2149 else
2150 {
2151 listitem_T *li = list_find(l, n);
2152
2153 if (li == NULL)
2154 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002155 semsg(_(e_list_index_out_of_range_nr), n);
2156 status = FAIL;
2157 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002158 else
2159 listitem_remove(l, li);
2160 }
2161 }
2162 }
2163 else
2164 {
2165 status = FAIL;
2166 semsg(_(e_cannot_index_str),
2167 vartype_name(tv_dest->v_type));
2168 }
2169
2170 clear_tv(tv_idx);
2171 clear_tv(tv_dest);
2172 ectx->ec_stack.ga_len -= 2;
2173
2174 return status;
2175}
2176
2177/*
2178 * Unlet a range of items in a list variable.
2179 */
2180 static int
2181execute_unletrange(isn_T *iptr, ectx_T *ectx)
2182{
2183 // Stack contains:
2184 // -3 index1
2185 // -2 index2
2186 // -1 dict or list
2187 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2188 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2189 typval_T *tv_dest = STACK_TV_BOT(-1);
2190 int status = OK;
2191
2192 if (tv_dest->v_type == VAR_LIST)
2193 {
2194 // indexes must be a number
2195 SOURCING_LNUM = iptr->isn_lnum;
2196 if (check_for_number(tv_idx1) == FAIL
2197 || (tv_idx2->v_type != VAR_SPECIAL
2198 && check_for_number(tv_idx2) == FAIL))
2199 {
2200 status = FAIL;
2201 }
2202 else
2203 {
2204 list_T *l = tv_dest->vval.v_list;
2205 long n1 = (long)tv_idx1->vval.v_number;
2206 long n2 = tv_idx2->v_type == VAR_SPECIAL
2207 ? 0 : (long)tv_idx2->vval.v_number;
2208 listitem_T *li;
2209
2210 li = list_find_index(l, &n1);
2211 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002212 {
2213 semsg(_(e_list_index_out_of_range_nr),
2214 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002215 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002216 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002217 else
2218 {
2219 if (n1 < 0)
2220 n1 = list_idx_of_item(l, li);
2221 if (n2 < 0)
2222 {
2223 listitem_T *li2 = list_find(l, n2);
2224
2225 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002226 {
2227 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002228 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002229 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002230 else
2231 n2 = list_idx_of_item(l, li2);
2232 }
2233 if (status != FAIL
2234 && tv_idx2->v_type != VAR_SPECIAL
2235 && n2 < n1)
2236 {
2237 semsg(_(e_list_index_out_of_range_nr), n2);
2238 status = FAIL;
2239 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002240 if (status != FAIL)
2241 list_unlet_range(l, li, n1,
2242 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002243 }
2244 }
2245 }
2246 else
2247 {
2248 status = FAIL;
2249 SOURCING_LNUM = iptr->isn_lnum;
2250 semsg(_(e_cannot_index_str),
2251 vartype_name(tv_dest->v_type));
2252 }
2253
2254 clear_tv(tv_idx1);
2255 clear_tv(tv_idx2);
2256 clear_tv(tv_dest);
2257 ectx->ec_stack.ga_len -= 3;
2258
2259 return status;
2260}
2261
2262/*
2263 * Top of a for loop.
2264 */
2265 static int
2266execute_for(isn_T *iptr, ectx_T *ectx)
2267{
2268 typval_T *tv;
2269 typval_T *ltv = STACK_TV_BOT(-1);
2270 typval_T *idxtv =
2271 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2272
2273 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2274 return FAIL;
2275 if (ltv->v_type == VAR_LIST)
2276 {
2277 list_T *list = ltv->vval.v_list;
2278
2279 // push the next item from the list
2280 ++idxtv->vval.v_number;
2281 if (list == NULL
2282 || idxtv->vval.v_number >= list->lv_len)
2283 {
2284 // past the end of the list, jump to "endfor"
2285 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2286 may_restore_cmdmod(&ectx->ec_funclocal);
2287 }
2288 else if (list->lv_first == &range_list_item)
2289 {
2290 // non-materialized range() list
2291 tv = STACK_TV_BOT(0);
2292 tv->v_type = VAR_NUMBER;
2293 tv->v_lock = 0;
2294 tv->vval.v_number = list_find_nr(
2295 list, idxtv->vval.v_number, NULL);
2296 ++ectx->ec_stack.ga_len;
2297 }
2298 else
2299 {
2300 listitem_T *li = list_find(list,
2301 idxtv->vval.v_number);
2302
2303 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2304 ++ectx->ec_stack.ga_len;
2305 }
2306 }
2307 else if (ltv->v_type == VAR_STRING)
2308 {
2309 char_u *str = ltv->vval.v_string;
2310
2311 // The index is for the last byte of the previous
2312 // character.
2313 ++idxtv->vval.v_number;
2314 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2315 {
2316 // past the end of the string, jump to "endfor"
2317 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2318 may_restore_cmdmod(&ectx->ec_funclocal);
2319 }
2320 else
2321 {
2322 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2323
2324 // Push the next character from the string.
2325 tv = STACK_TV_BOT(0);
2326 tv->v_type = VAR_STRING;
2327 tv->vval.v_string = vim_strnsave(
2328 str + idxtv->vval.v_number, clen);
2329 ++ectx->ec_stack.ga_len;
2330 idxtv->vval.v_number += clen - 1;
2331 }
2332 }
2333 else if (ltv->v_type == VAR_BLOB)
2334 {
2335 blob_T *blob = ltv->vval.v_blob;
2336
2337 // When we get here the first time make a copy of the
2338 // blob, so that the iteration still works when it is
2339 // changed.
2340 if (idxtv->vval.v_number == -1 && blob != NULL)
2341 {
2342 blob_copy(blob, ltv);
2343 blob_unref(blob);
2344 blob = ltv->vval.v_blob;
2345 }
2346
2347 // The index is for the previous byte.
2348 ++idxtv->vval.v_number;
2349 if (blob == NULL
2350 || idxtv->vval.v_number >= blob_len(blob))
2351 {
2352 // past the end of the blob, jump to "endfor"
2353 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2354 may_restore_cmdmod(&ectx->ec_funclocal);
2355 }
2356 else
2357 {
2358 // Push the next byte from the blob.
2359 tv = STACK_TV_BOT(0);
2360 tv->v_type = VAR_NUMBER;
2361 tv->vval.v_number = blob_get(blob,
2362 idxtv->vval.v_number);
2363 ++ectx->ec_stack.ga_len;
2364 }
2365 }
2366 else
2367 {
2368 semsg(_(e_for_loop_on_str_not_supported),
2369 vartype_name(ltv->v_type));
2370 return FAIL;
2371 }
2372 return OK;
2373}
2374
2375/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002376 * Load instruction for w:/b:/g:/t: variable.
2377 * "isn_type" is used instead of "iptr->isn_type".
2378 */
2379 static int
2380load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2381{
2382 dictitem_T *di = NULL;
2383 hashtab_T *ht = NULL;
2384 char namespace;
2385
2386 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2387 return NOTDONE;
2388 switch (isn_type)
2389 {
2390 case ISN_LOADG:
2391 ht = get_globvar_ht();
2392 namespace = 'g';
2393 break;
2394 case ISN_LOADB:
2395 ht = &curbuf->b_vars->dv_hashtab;
2396 namespace = 'b';
2397 break;
2398 case ISN_LOADW:
2399 ht = &curwin->w_vars->dv_hashtab;
2400 namespace = 'w';
2401 break;
2402 case ISN_LOADT:
2403 ht = &curtab->tp_vars->dv_hashtab;
2404 namespace = 't';
2405 break;
2406 default: // Cannot reach here
2407 return NOTDONE;
2408 }
2409 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2410
Bram Moolenaar06b77222022-01-25 15:51:56 +00002411 if (di == NULL)
2412 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002413 if (isn_type == ISN_LOADG)
2414 {
2415 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2416
2417 // g:Something could be a function
2418 if (ufunc != NULL)
2419 {
2420 typval_T *tv = STACK_TV_BOT(0);
2421
2422 ++ectx->ec_stack.ga_len;
2423 tv->v_type = VAR_FUNC;
2424 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2425 if (tv->vval.v_string == NULL)
2426 return FAIL;
2427 STRCPY(tv->vval.v_string, "g:");
2428 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2429 return OK;
2430 }
2431 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002432 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002433 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002434 // no check if the item exists in the script but
2435 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002436 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002437 else
2438 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002439 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002440 return FAIL;
2441 }
2442 else
2443 {
2444 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2445 ++ectx->ec_stack.ga_len;
2446 }
2447 return OK;
2448}
2449
2450/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002451 * Execute instructions in execution context "ectx".
2452 * Return OK or FAIL;
2453 */
2454 static int
2455exec_instructions(ectx_T *ectx)
2456{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002457 int ret = FAIL;
2458 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002459 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002460
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002461 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002462 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002463
Bram Moolenaarff652882021-05-16 15:24:49 +02002464 // Only catch exceptions in this instruction list.
2465 ectx->ec_trylevel_at_start = trylevel;
2466
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 for (;;)
2468 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002469 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002471 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002472
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002473 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002474 {
2475 line_breakcheck();
2476 breakcheck_count = 0;
2477 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002478 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002479 {
2480 // Turn CTRL-C into an exception.
2481 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002482 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002483 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002484 did_throw = TRUE;
2485 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002487 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002488 {
2489 // Turn an error message into an exception.
2490 did_emsg = FALSE;
2491 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002492 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002493 did_throw = TRUE;
2494 *msg_list = NULL;
2495 }
2496
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002497 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002499 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002500 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002501 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502
2503 // An exception jumps to the first catch, finally, or returns from
2504 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002505 while (index > 0)
2506 {
2507 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002508 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002509 break;
2510 // In the catch and finally block of this try we have to go up
2511 // one level.
2512 --index;
2513 trycmd = NULL;
2514 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002515 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002517 if (trycmd->tcd_in_catch)
2518 {
2519 // exception inside ":catch", jump to ":finally" once
2520 ectx->ec_iidx = trycmd->tcd_finally_idx;
2521 trycmd->tcd_finally_idx = 0;
2522 }
2523 else
2524 // jump to first ":catch"
2525 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002526 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002527 did_throw = FALSE; // don't come back here until :endtry
2528 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 }
2530 else
2531 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002532 // Not inside try or need to return from current functions.
2533 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002534 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002535 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002536 tv = STACK_TV_BOT(0);
2537 tv->v_type = VAR_NUMBER;
2538 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002539 ++ectx->ec_stack.ga_len;
2540 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002542 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002543 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002544 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002545 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 goto done;
2547 }
2548
Bram Moolenaar4c137212021-04-19 16:48:48 +02002549 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002550 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 }
2552 continue;
2553 }
2554
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002555 /*
2556 * Big switch on the instruction. Most compilers will be turning this
2557 * into an efficient lookup table, since the "case" values are an enum
2558 * with sequential numbers. It may look ugly, but it should be the
2559 * most efficient way.
2560 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002561 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 switch (iptr->isn_type)
2563 {
2564 // execute Ex command line
2565 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002566 if (exec_command(iptr) == FAIL)
2567 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 break;
2569
Bram Moolenaar20677332021-06-06 17:02:53 +02002570 // execute Ex command line split at NL characters.
2571 case ISN_EXEC_SPLIT:
2572 {
2573 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002574 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002575
2576 SOURCING_LNUM = iptr->isn_lnum;
2577 CLEAR_FIELD(cookie);
2578 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2579 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002580 line = get_split_sourceline(0, &cookie, 0, 0);
2581 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002582 get_split_sourceline, &cookie,
2583 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2584 == FAIL
2585 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002586 {
2587 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002588 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002589 }
2590 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002591 }
2592 break;
2593
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002594 // execute Ex command line that is only a range
2595 case ISN_EXECRANGE:
2596 {
2597 exarg_T ea;
2598 char *error = NULL;
2599
2600 CLEAR_FIELD(ea);
2601 ea.cmdidx = CMD_SIZE;
2602 ea.addr_type = ADDR_LINES;
2603 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002604 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002605 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002606 if (ea.cmd == NULL)
2607 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002608 // error is always NULL when using ADDR_LINES
2609 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002610 if (error != NULL)
2611 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002612 emsg(error);
2613 goto on_error;
2614 }
2615 }
2616 break;
2617
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002618 // Evaluate an expression with legacy syntax, push it onto the
2619 // stack.
2620 case ISN_LEGACY_EVAL:
2621 {
2622 char_u *arg = iptr->isn_arg.string;
2623 int res;
2624 int save_flags = cmdmod.cmod_flags;
2625
Bram Moolenaar35578162021-08-02 19:10:38 +02002626 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002627 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002628 tv = STACK_TV_BOT(0);
2629 init_tv(tv);
2630 cmdmod.cmod_flags |= CMOD_LEGACY;
2631 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2632 cmdmod.cmod_flags = save_flags;
2633 if (res == FAIL)
2634 goto on_error;
2635 ++ectx->ec_stack.ga_len;
2636 }
2637 break;
2638
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002639 // push typeval VAR_INSTR with instructions to be executed
2640 case ISN_INSTR:
2641 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002642 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002643 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002644 tv = STACK_TV_BOT(0);
2645 tv->vval.v_instr = ALLOC_ONE(instr_T);
2646 if (tv->vval.v_instr == NULL)
2647 goto on_error;
2648 ++ectx->ec_stack.ga_len;
2649
2650 tv->v_type = VAR_INSTR;
2651 tv->vval.v_instr->instr_ectx = ectx;
2652 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2653 }
2654 break;
2655
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002656 case ISN_SOURCE:
2657 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002658 int notused;
2659 SOURCING_LNUM = iptr->isn_lnum;
2660 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002661 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002662 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002663 }
2664 break;
2665
Bram Moolenaar4c137212021-04-19 16:48:48 +02002666 // execute :substitute with an expression
2667 case ISN_SUBSTITUTE:
2668 {
2669 subs_T *subs = &iptr->isn_arg.subs;
2670 source_cookie_T cookie;
2671 struct subs_expr_S *save_instr = substitute_instr;
2672 struct subs_expr_S subs_instr;
2673 int res;
2674
2675 subs_instr.subs_ectx = ectx;
2676 subs_instr.subs_instr = subs->subs_instr;
2677 subs_instr.subs_status = OK;
2678 substitute_instr = &subs_instr;
2679
2680 SOURCING_LNUM = iptr->isn_lnum;
2681 // This is very much like ISN_EXEC
2682 CLEAR_FIELD(cookie);
2683 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2684 res = do_cmdline(subs->subs_cmd,
2685 getsourceline, &cookie,
2686 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2687 substitute_instr = save_instr;
2688
2689 if (res == FAIL || did_emsg
2690 || subs_instr.subs_status == FAIL)
2691 goto on_error;
2692 }
2693 break;
2694
2695 case ISN_FINISH:
2696 goto done;
2697
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002698 case ISN_REDIRSTART:
2699 // create a dummy entry for var_redir_str()
2700 if (alloc_redir_lval() == FAIL)
2701 goto on_error;
2702
2703 // The output is stored in growarray "redir_ga" until
2704 // redirection ends.
2705 init_redir_ga();
2706 redir_vname = 1;
2707 break;
2708
2709 case ISN_REDIREND:
2710 {
2711 char_u *res = get_clear_redir_ga();
2712
2713 // End redirection, put redirected text on the stack.
2714 clear_redir_lval();
2715 redir_vname = 0;
2716
Bram Moolenaar35578162021-08-02 19:10:38 +02002717 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002718 {
2719 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002720 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002721 }
2722 tv = STACK_TV_BOT(0);
2723 tv->v_type = VAR_STRING;
2724 tv->vval.v_string = res;
2725 ++ectx->ec_stack.ga_len;
2726 }
2727 break;
2728
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002729 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002730#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002731 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002732 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2733 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002734 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002735#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002736 break;
2737
2738 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002739#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002740 {
2741 exarg_T ea;
2742 int res;
2743
2744 CLEAR_FIELD(ea);
2745 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2746 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2747 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2748 --ectx->ec_stack.ga_len;
2749 tv = STACK_TV_BOT(0);
2750 res = cexpr_core(&ea, tv);
2751 clear_tv(tv);
2752 if (res == FAIL)
2753 goto on_error;
2754 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002755#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002756 break;
2757
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002758 // execute Ex command from pieces on the stack
2759 case ISN_EXECCONCAT:
2760 {
2761 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002762 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002763 int pass;
2764 int i;
2765 char_u *cmd = NULL;
2766 char_u *str;
2767
2768 for (pass = 1; pass <= 2; ++pass)
2769 {
2770 for (i = 0; i < count; ++i)
2771 {
2772 tv = STACK_TV_BOT(i - count);
2773 str = tv->vval.v_string;
2774 if (str != NULL && *str != NUL)
2775 {
2776 if (pass == 2)
2777 STRCPY(cmd + len, str);
2778 len += STRLEN(str);
2779 }
2780 if (pass == 2)
2781 clear_tv(tv);
2782 }
2783 if (pass == 1)
2784 {
2785 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002786 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002787 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002788 len = 0;
2789 }
2790 }
2791
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002792 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002793 do_cmdline_cmd(cmd);
2794 vim_free(cmd);
2795 }
2796 break;
2797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 // execute :echo {string} ...
2799 case ISN_ECHO:
2800 {
2801 int count = iptr->isn_arg.echo.echo_count;
2802 int atstart = TRUE;
2803 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002804 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805
2806 for (idx = 0; idx < count; ++idx)
2807 {
2808 tv = STACK_TV_BOT(idx - count);
2809 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2810 &atstart, &needclr);
2811 clear_tv(tv);
2812 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002813 if (needclr)
2814 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002815 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 }
2817 break;
2818
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002819 // :execute {string} ...
2820 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002821 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002822 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002823 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002824 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002825 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002826 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002827 {
2828 int count = iptr->isn_arg.number;
2829 garray_T ga;
2830 char_u buf[NUMBUFLEN];
2831 char_u *p;
2832 int len;
2833 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002834 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002835
2836 ga_init2(&ga, 1, 80);
2837 for (idx = 0; idx < count; ++idx)
2838 {
2839 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002840 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002841 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002842 if (tv->v_type == VAR_CHANNEL
2843 || tv->v_type == VAR_JOB)
2844 {
2845 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002846 semsg(_(e_using_invalid_value_as_string_str),
2847 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002848 break;
2849 }
2850 else
2851 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002852 }
2853 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002854 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002855
2856 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002857 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002858 failed = TRUE;
2859 else
2860 {
2861 if (ga.ga_len > 0)
2862 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2863 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2864 ga.ga_len += len;
2865 }
2866 clear_tv(tv);
2867 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002868 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002869 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002870 {
2871 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002872 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002873 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002874
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002875 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002876 {
2877 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002878 {
2879 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002880 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002881 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002882 {
2883 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002884 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002885 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002886 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002887 else
2888 {
2889 msg_sb_eol();
2890 if (iptr->isn_type == ISN_ECHOMSG)
2891 {
2892 msg_attr(ga.ga_data, echo_attr);
2893 out_flush();
2894 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002895 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2896 {
2897 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2898 TRUE);
2899 ui_write((char_u *)"\r\n", 2, TRUE);
2900 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002901 else
2902 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002903 SOURCING_LNUM = iptr->isn_lnum;
2904 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002905 }
2906 }
2907 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002908 ga_clear(&ga);
2909 }
2910 break;
2911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 // load local variable or argument
2913 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002914 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002915 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002917 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 break;
2919
2920 // load v: variable
2921 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002922 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002923 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002925 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 break;
2927
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002928 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 case ISN_LOADSCRIPT:
2930 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002931 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 svar_T *sv;
2933
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002934 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002935 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002936 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002937 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002938 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002939 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002941 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 }
2943 break;
2944
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002945 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002947 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002949 int sid = iptr->isn_arg.loadstore.ls_sid;
2950 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002951 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 if (di == NULL)
2955 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002956 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002957 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002958 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 }
2960 else
2961 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002962 if (iptr->isn_type == ISN_LOADEXPORT)
2963 {
2964 int idx = get_script_item_idx(sid, name, 0,
2965 NULL, NULL);
2966 svar_T *sv;
2967
2968 if (idx >= 0)
2969 {
2970 sv = ((svar_T *)SCRIPT_ITEM(sid)
2971 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002972 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002973 {
2974 SOURCING_LNUM = iptr->isn_lnum;
2975 semsg(_(e_item_not_exported_in_script_str),
2976 name);
2977 goto on_error;
2978 }
2979 }
2980 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002981 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002982 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002984 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 }
2986 }
2987 break;
2988
Bram Moolenaard3aac292020-04-19 14:32:17 +02002989 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002991 case ISN_LOADB:
2992 case ISN_LOADW:
2993 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002995 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002996
Bram Moolenaar06b77222022-01-25 15:51:56 +00002997 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002998 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002999 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003000 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 break;
3004
Bram Moolenaar03290b82020-12-19 16:30:44 +01003005 // load autoload variable
3006 case ISN_LOADAUTO:
3007 {
3008 char_u *name = iptr->isn_arg.string;
3009
Bram Moolenaar35578162021-08-02 19:10:38 +02003010 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003011 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003012 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003013 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003014 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003015 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003016 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003017 }
3018 break;
3019
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003020 // load g:/b:/w:/t: namespace
3021 case ISN_LOADGDICT:
3022 case ISN_LOADBDICT:
3023 case ISN_LOADWDICT:
3024 case ISN_LOADTDICT:
3025 {
3026 dict_T *d = NULL;
3027
3028 switch (iptr->isn_type)
3029 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003030 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3031 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3032 case ISN_LOADWDICT: d = curwin->w_vars; break;
3033 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003034 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003035 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003036 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003037 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003038 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003039 tv = STACK_TV_BOT(0);
3040 tv->v_type = VAR_DICT;
3041 tv->v_lock = 0;
3042 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003043 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003044 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003045 }
3046 break;
3047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 // load &option
3049 case ISN_LOADOPT:
3050 {
3051 typval_T optval;
3052 char_u *name = iptr->isn_arg.string;
3053
Bram Moolenaara8c17702020-04-01 21:17:24 +02003054 // This is not expected to fail, name is checked during
3055 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003056 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003057 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003058 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003059 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003061 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 }
3063 break;
3064
3065 // load $ENV
3066 case ISN_LOADENV:
3067 {
3068 typval_T optval;
3069 char_u *name = iptr->isn_arg.string;
3070
Bram Moolenaar35578162021-08-02 19:10:38 +02003071 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003072 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003073 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003074 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003076 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 }
3078 break;
3079
3080 // load @register
3081 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003082 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003083 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 tv = STACK_TV_BOT(0);
3085 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003086 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003087 // This may result in NULL, which should be equivalent to an
3088 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 tv->vval.v_string = get_reg_contents(
3090 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003091 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 break;
3093
3094 // store local variable
3095 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003096 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 tv = STACK_TV_VAR(iptr->isn_arg.number);
3098 clear_tv(tv);
3099 *tv = *STACK_TV_BOT(0);
3100 break;
3101
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003102 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003103 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003104 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003105 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003106 int sid = iptr->isn_arg.loadstore.ls_sid;
3107 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003108 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003109 dictitem_T *di = find_var_in_ht(ht, 0,
3110 iptr->isn_type == ISN_STORES
3111 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003112
Bram Moolenaar4c137212021-04-19 16:48:48 +02003113 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003114 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003115 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003116 {
3117 if (iptr->isn_type == ISN_STOREEXPORT)
3118 {
3119 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003120 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003121 goto on_error;
3122 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003123 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003124 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003125 else
3126 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003127 if (iptr->isn_type == ISN_STOREEXPORT)
3128 {
3129 int idx = get_script_item_idx(sid, name, 0,
3130 NULL, NULL);
3131
3132 if (idx >= 0)
3133 {
3134 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3135 ->sn_var_vals.ga_data) + idx;
3136
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003137 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003138 {
3139 semsg(_(e_item_not_exported_in_script_str),
3140 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003141 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003142 goto on_error;
3143 }
3144 }
3145 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003146 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003147 {
3148 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003149 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003150 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003151 clear_tv(&di->di_tv);
3152 di->di_tv = *STACK_TV_BOT(0);
3153 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003154 }
3155 break;
3156
3157 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 case ISN_STORESCRIPT:
3159 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003160 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3161 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003163 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003164 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003165 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003166 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003167
3168 // "const" and "final" are checked at compile time, locking
3169 // the value needs to be checked here.
3170 SOURCING_LNUM = iptr->isn_lnum;
3171 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003172 {
3173 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003174 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003175 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 clear_tv(sv->sv_tv);
3178 *sv->sv_tv = *STACK_TV_BOT(0);
3179 }
3180 break;
3181
3182 // store option
3183 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003184 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003186 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3187 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 long n = 0;
3189 char_u *s = NULL;
3190 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003191 char_u numbuf[NUMBUFLEN];
3192 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193
Bram Moolenaar4c137212021-04-19 16:48:48 +02003194 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 tv = STACK_TV_BOT(0);
3196 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003197 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003199 if (s == NULL)
3200 s = (char_u *)"";
3201 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003202 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3203 {
3204 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003205 // If the option can be set to a function reference or
3206 // a lambda and the passed value is a function
3207 // reference, then convert it to the name (string) of
3208 // the function reference.
3209 s = tv2string(tv, &tofree, numbuf, 0);
3210 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003211 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003212 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003213 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003214 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003215 goto on_error;
3216 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003219 // must be VAR_NUMBER, CHECKTYPE makes sure
3220 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003221 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003222 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003223 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 if (msg != NULL)
3225 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003226 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003228 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 }
3231 break;
3232
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003233 // store $ENV
3234 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003235 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003236 tv = STACK_TV_BOT(0);
3237 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3238 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003239 break;
3240
3241 // store @r
3242 case ISN_STOREREG:
3243 {
3244 int reg = iptr->isn_arg.number;
3245
Bram Moolenaar4c137212021-04-19 16:48:48 +02003246 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003247 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003248 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003249 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003250 }
3251 break;
3252
3253 // store v: variable
3254 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003255 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003256 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3257 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003258 // should not happen, type is checked when compiling
3259 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003260 break;
3261
Bram Moolenaard3aac292020-04-19 14:32:17 +02003262 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003264 case ISN_STOREB:
3265 case ISN_STOREW:
3266 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003268 dictitem_T *di;
3269 hashtab_T *ht;
3270 char_u *name = iptr->isn_arg.string + 2;
3271
Bram Moolenaard3aac292020-04-19 14:32:17 +02003272 switch (iptr->isn_type)
3273 {
3274 case ISN_STOREG:
3275 ht = get_globvar_ht();
3276 break;
3277 case ISN_STOREB:
3278 ht = &curbuf->b_vars->dv_hashtab;
3279 break;
3280 case ISN_STOREW:
3281 ht = &curwin->w_vars->dv_hashtab;
3282 break;
3283 case ISN_STORET:
3284 ht = &curtab->tp_vars->dv_hashtab;
3285 break;
3286 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003287 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289
Bram Moolenaar4c137212021-04-19 16:48:48 +02003290 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003291 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003293 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 else
3295 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003296 SOURCING_LNUM = iptr->isn_lnum;
3297 if (var_check_permission(di, name) == FAIL)
3298 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 clear_tv(&di->di_tv);
3300 di->di_tv = *STACK_TV_BOT(0);
3301 }
3302 }
3303 break;
3304
Bram Moolenaar03290b82020-12-19 16:30:44 +01003305 // store an autoload variable
3306 case ISN_STOREAUTO:
3307 SOURCING_LNUM = iptr->isn_lnum;
3308 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3309 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003310 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003311 break;
3312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 // store number in local variable
3314 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003315 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 clear_tv(tv);
3317 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003318 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 break;
3320
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003321 // store value in list or dict variable
3322 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003323 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003324 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003325
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003326 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003327 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003328 if (res == NOTDONE)
3329 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003330 }
3331 break;
3332
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003333 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003334 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003335 if (execute_storerange(iptr, ectx) == FAIL)
3336 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003337 break;
3338
Bram Moolenaar0186e582021-01-10 18:33:11 +01003339 // load or store variable or argument from outer scope
3340 case ISN_LOADOUTER:
3341 case ISN_STOREOUTER:
3342 {
3343 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003344 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3345 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003346
3347 while (depth > 1 && outer != NULL)
3348 {
3349 outer = outer->out_up;
3350 --depth;
3351 }
3352 if (outer == NULL)
3353 {
3354 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003355 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3356 || ectx->ec_outer_ref == NULL)
3357 // Possibly :def function called from legacy
3358 // context.
3359 emsg(_(e_closure_called_from_invalid_context));
3360 else
3361 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003362 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003363 }
3364 tv = ((typval_T *)outer->out_stack->ga_data)
3365 + outer->out_frame_idx + STACK_FRAME_SIZE
3366 + iptr->isn_arg.outer.outer_idx;
3367 if (iptr->isn_type == ISN_LOADOUTER)
3368 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003369 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003370 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003371 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003372 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003373 }
3374 else
3375 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003376 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003377 clear_tv(tv);
3378 *tv = *STACK_TV_BOT(0);
3379 }
3380 }
3381 break;
3382
Bram Moolenaar752fc692021-01-04 21:57:11 +01003383 // unlet item in list or dict variable
3384 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003385 if (execute_unletindex(iptr, ectx) == FAIL)
3386 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003387 break;
3388
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003389 // unlet range of items in list variable
3390 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003391 if (execute_unletrange(iptr, ectx) == FAIL)
3392 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003393 break;
3394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 // push constant
3396 case ISN_PUSHNR:
3397 case ISN_PUSHBOOL:
3398 case ISN_PUSHSPEC:
3399 case ISN_PUSHF:
3400 case ISN_PUSHS:
3401 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003402 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003403 case ISN_PUSHCHANNEL:
3404 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003405 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003406 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003408 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003409 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 switch (iptr->isn_type)
3411 {
3412 case ISN_PUSHNR:
3413 tv->v_type = VAR_NUMBER;
3414 tv->vval.v_number = iptr->isn_arg.number;
3415 break;
3416 case ISN_PUSHBOOL:
3417 tv->v_type = VAR_BOOL;
3418 tv->vval.v_number = iptr->isn_arg.number;
3419 break;
3420 case ISN_PUSHSPEC:
3421 tv->v_type = VAR_SPECIAL;
3422 tv->vval.v_number = iptr->isn_arg.number;
3423 break;
3424#ifdef FEAT_FLOAT
3425 case ISN_PUSHF:
3426 tv->v_type = VAR_FLOAT;
3427 tv->vval.v_float = iptr->isn_arg.fnumber;
3428 break;
3429#endif
3430 case ISN_PUSHBLOB:
3431 blob_copy(iptr->isn_arg.blob, tv);
3432 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003433 case ISN_PUSHFUNC:
3434 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003435 if (iptr->isn_arg.string == NULL)
3436 tv->vval.v_string = NULL;
3437 else
3438 tv->vval.v_string =
3439 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003440 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003441 case ISN_PUSHCHANNEL:
3442#ifdef FEAT_JOB_CHANNEL
3443 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003444 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003445#endif
3446 break;
3447 case ISN_PUSHJOB:
3448#ifdef FEAT_JOB_CHANNEL
3449 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003450 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003451#endif
3452 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 default:
3454 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003455 tv->vval.v_string = iptr->isn_arg.string == NULL
3456 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 }
3458 break;
3459
Bram Moolenaar06b77222022-01-25 15:51:56 +00003460 case ISN_AUTOLOAD:
3461 {
3462 char_u *name = iptr->isn_arg.string;
3463
3464 (void)script_autoload(name, FALSE);
3465 if (find_func(name, TRUE))
3466 {
3467 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3468 goto theend;
3469 tv = STACK_TV_BOT(0);
3470 tv->v_lock = 0;
3471 ++ectx->ec_stack.ga_len;
3472 tv->v_type = VAR_FUNC;
3473 tv->vval.v_string = vim_strsave(name);
3474 }
3475 else
3476 {
3477 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3478
3479 if (res == NOTDONE)
3480 goto theend;
3481 if (res == FAIL)
3482 goto on_error;
3483 }
3484 }
3485 break;
3486
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003487 case ISN_UNLET:
3488 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3489 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003490 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003491 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003492 case ISN_UNLETENV:
3493 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3494 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003495
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003496 case ISN_LOCKUNLOCK:
3497 {
3498 typval_T *lval_root_save = lval_root;
3499 int res;
3500
3501 // Stack has the local variable, argument the whole :lock
3502 // or :unlock command, like ISN_EXEC.
3503 --ectx->ec_stack.ga_len;
3504 lval_root = STACK_TV_BOT(0);
3505 res = exec_command(iptr);
3506 clear_tv(lval_root);
3507 lval_root = lval_root_save;
3508 if (res == FAIL)
3509 goto on_error;
3510 }
3511 break;
3512
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003513 case ISN_LOCKCONST:
3514 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3515 break;
3516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 // create a list from items on the stack; uses a single allocation
3518 // for the list header and the items
3519 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003520 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003521 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 break;
3523
3524 // create a dict from items on the stack
3525 case ISN_NEWDICT:
3526 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003527 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003529 SOURCING_LNUM = iptr->isn_lnum;
3530 res = exe_newdict(iptr->isn_arg.number, ectx);
3531 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003532 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003533 if (res == MAYBE)
3534 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 }
3536 break;
3537
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003538 // create a partial with NULL value
3539 case ISN_NEWPARTIAL:
3540 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3541 goto theend;
3542 ++ectx->ec_stack.ga_len;
3543 tv = STACK_TV_BOT(-1);
3544 tv->v_type = VAR_PARTIAL;
3545 tv->v_lock = 0;
3546 tv->vval.v_partial = NULL;
3547 break;
3548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 // call a :def function
3550 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003551 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003552 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3553 NULL,
3554 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003555 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003556 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 break;
3558
3559 // call a builtin function
3560 case ISN_BCALL:
3561 SOURCING_LNUM = iptr->isn_lnum;
3562 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3563 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003564 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003565 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 break;
3567
3568 // call a funcref or partial
3569 case ISN_PCALL:
3570 {
3571 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3572 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003573 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574
3575 SOURCING_LNUM = iptr->isn_lnum;
3576 if (pfunc->cpf_top)
3577 {
3578 // funcref is above the arguments
3579 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3580 }
3581 else
3582 {
3583 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003584 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003585 partial_tv = *STACK_TV_BOT(0);
3586 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003588 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003589 if (tv == &partial_tv)
3590 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003592 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 }
3594 break;
3595
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003596 case ISN_PCALL_END:
3597 // PCALL finished, arguments have been consumed and replaced by
3598 // the return value. Now clear the funcref from the stack,
3599 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003600 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003601 clear_tv(STACK_TV_BOT(-1));
3602 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3603 break;
3604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 // call a user defined function or funcref/partial
3606 case ISN_UCALL:
3607 {
3608 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3609
3610 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003611 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003612 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003613 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 }
3615 break;
3616
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003617 // return from a :def function call without a value
3618 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003619 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003620 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003621 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003622 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003623 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003624 tv->vval.v_number = 0;
3625 tv->v_lock = 0;
3626 // FALLTHROUGH
3627
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003628 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 case ISN_RETURN:
3630 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003631 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003632 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003633
3634 if (trystack->ga_len > 0)
3635 trycmd = ((trycmd_T *)trystack->ga_data)
3636 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003637 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003638 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003640 // jump to ":finally" or ":endtry"
3641 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003642 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003643 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003644 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 trycmd->tcd_return = TRUE;
3646 }
3647 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003648 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 }
3650 break;
3651
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003652 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 case ISN_FUNCREF:
3654 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003655 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003656 ufunc_T *ufunc;
3657 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003660 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003661 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003662 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003663 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003664 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003665 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003666 if (funcref->fr_func_name == NULL)
3667 {
3668 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3669 + funcref->fr_dfunc_idx;
3670
3671 ufunc = pt_dfunc->df_ufunc;
3672 }
3673 else
3674 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003675 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003676 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003677 if (ufunc == NULL)
3678 {
3679 SOURCING_LNUM = iptr->isn_lnum;
3680 iemsg("ufunc unexpectedly NULL for FUNCREF");
3681 goto theend;
3682 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003683 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003684 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003686 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 tv->vval.v_partial = pt;
3688 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003689 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 }
3691 break;
3692
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003693 // Create a global function from a lambda.
3694 case ISN_NEWFUNC:
3695 {
3696 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3697
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003698 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003699 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003700 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003701 }
3702 break;
3703
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003704 // List functions
3705 case ISN_DEF:
3706 if (iptr->isn_arg.string == NULL)
3707 list_functions(NULL);
3708 else
3709 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003710 exarg_T ea;
3711 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003712
3713 CLEAR_FIELD(ea);
3714 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003715 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3716 define_function(&ea, NULL, &lines_to_free);
3717 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003718 }
3719 break;
3720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 // jump if a condition is met
3722 case ISN_JUMP:
3723 {
3724 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003725 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 int jump = TRUE;
3727
3728 if (when != JUMP_ALWAYS)
3729 {
3730 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003731 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003732 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003733 || when == JUMP_IF_COND_TRUE)
3734 {
3735 SOURCING_LNUM = iptr->isn_lnum;
3736 jump = tv_get_bool_chk(tv, &error);
3737 if (error)
3738 goto on_error;
3739 }
3740 else
3741 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003743 || when == JUMP_AND_KEEP_IF_FALSE
3744 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003746 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 {
3748 // drop the value from the stack
3749 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003750 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 }
3752 }
3753 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003754 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 }
3756 break;
3757
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003758 // Jump if an argument with a default value was already set and not
3759 // v:none.
3760 case ISN_JUMP_IF_ARG_SET:
3761 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3762 if (tv->v_type != VAR_UNKNOWN
3763 && !(tv->v_type == VAR_SPECIAL
3764 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003765 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003766 break;
3767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 // top of a for loop
3769 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003770 if (execute_for(iptr, ectx) == FAIL)
3771 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 break;
3773
3774 // start of ":try" block
3775 case ISN_TRY:
3776 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003777 trycmd_T *trycmd = NULL;
3778
Bram Moolenaar35578162021-08-02 19:10:38 +02003779 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003780 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003781 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3782 + ectx->ec_trystack.ga_len;
3783 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003785 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003786 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3787 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003788 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003789 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003790 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003791 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003792 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003793 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 }
3795 break;
3796
3797 case ISN_PUSHEXC:
3798 if (current_exception == NULL)
3799 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003800 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003802 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003804 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003805 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003807 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003809 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 tv->vval.v_string = vim_strsave(
3811 (char_u *)current_exception->value);
3812 break;
3813
3814 case ISN_CATCH:
3815 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003816 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817
Bram Moolenaar4c137212021-04-19 16:48:48 +02003818 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 if (trystack->ga_len > 0)
3820 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003821 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 + trystack->ga_len - 1;
3823 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003824 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 }
3826 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003827 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 catch_exception(current_exception);
3829 }
3830 break;
3831
Bram Moolenaarc150c092021-02-13 15:02:46 +01003832 case ISN_TRYCONT:
3833 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003834 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003835 trycont_T *trycont = &iptr->isn_arg.trycont;
3836 int i;
3837 trycmd_T *trycmd;
3838 int iidx = trycont->tct_where;
3839
3840 if (trystack->ga_len < trycont->tct_levels)
3841 {
3842 siemsg("TRYCONT: expected %d levels, found %d",
3843 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003844 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003845 }
3846 // Make :endtry jump to any outer try block and the last
3847 // :endtry inside the loop to the loop start.
3848 for (i = trycont->tct_levels; i > 0; --i)
3849 {
3850 trycmd = ((trycmd_T *)trystack->ga_data)
3851 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003852 // Add one to tcd_cont to be able to jump to
3853 // instruction with index zero.
3854 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003855 iidx = trycmd->tcd_finally_idx == 0
3856 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003857 }
3858 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003859 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003860 }
3861 break;
3862
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003863 case ISN_FINALLY:
3864 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003865 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003866 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3867 + trystack->ga_len - 1;
3868
3869 // Reset the index to avoid a return statement jumps here
3870 // again.
3871 trycmd->tcd_finally_idx = 0;
3872 break;
3873 }
3874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 // end of ":try" block
3876 case ISN_ENDTRY:
3877 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003878 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879
3880 if (trystack->ga_len > 0)
3881 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003882 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 --trystack->ga_len;
3885 --trylevel;
3886 trycmd = ((trycmd_T *)trystack->ga_data)
3887 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003888 if (trycmd->tcd_did_throw)
3889 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003890 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 {
3892 // discard the exception
3893 if (caught_stack == current_exception)
3894 caught_stack = caught_stack->caught;
3895 discard_current_exception();
3896 }
3897
3898 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003899 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003900
Bram Moolenaar4c137212021-04-19 16:48:48 +02003901 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003902 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003903 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003904 clear_tv(STACK_TV_BOT(0));
3905 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003906 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003907 // handling :continue: jump to outer try block or
3908 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003909 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 }
3911 }
3912 break;
3913
3914 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003915 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003916 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003917
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003918 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3919 {
3920 // throwing an exception while using "silent!" causes
3921 // the function to abort but not display an error.
3922 tv = STACK_TV_BOT(-1);
3923 clear_tv(tv);
3924 tv->v_type = VAR_NUMBER;
3925 tv->vval.v_number = 0;
3926 goto done;
3927 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003928 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003929 tv = STACK_TV_BOT(0);
3930 if (tv->vval.v_string == NULL
3931 || *skipwhite(tv->vval.v_string) == NUL)
3932 {
3933 vim_free(tv->vval.v_string);
3934 SOURCING_LNUM = iptr->isn_lnum;
3935 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003936 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003937 }
3938
3939 // Inside a "catch" we need to first discard the caught
3940 // exception.
3941 if (trystack->ga_len > 0)
3942 {
3943 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3944 + trystack->ga_len - 1;
3945 if (trycmd->tcd_caught && current_exception != NULL)
3946 {
3947 // discard the exception
3948 if (caught_stack == current_exception)
3949 caught_stack = caught_stack->caught;
3950 discard_current_exception();
3951 trycmd->tcd_caught = FALSE;
3952 }
3953 }
3954
Bram Moolenaar90a57162022-02-12 14:23:17 +00003955 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003956 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3957 == FAIL)
3958 {
3959 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003960 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003961 }
3962 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 break;
3965
3966 // compare with special values
3967 case ISN_COMPAREBOOL:
3968 case ISN_COMPARESPECIAL:
3969 {
3970 typval_T *tv1 = STACK_TV_BOT(-2);
3971 typval_T *tv2 = STACK_TV_BOT(-1);
3972 varnumber_T arg1 = tv1->vval.v_number;
3973 varnumber_T arg2 = tv2->vval.v_number;
3974 int res;
3975
3976 switch (iptr->isn_arg.op.op_type)
3977 {
3978 case EXPR_EQUAL: res = arg1 == arg2; break;
3979 case EXPR_NEQUAL: res = arg1 != arg2; break;
3980 default: res = 0; break;
3981 }
3982
Bram Moolenaar4c137212021-04-19 16:48:48 +02003983 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 tv1->v_type = VAR_BOOL;
3985 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3986 }
3987 break;
3988
Bram Moolenaar7a222242022-03-01 19:23:24 +00003989 case ISN_COMPARENULL:
3990 {
3991 typval_T *tv1 = STACK_TV_BOT(-2);
3992 typval_T *tv2 = STACK_TV_BOT(-1);
3993 int res;
3994
3995 res = typval_compare_null(tv1, tv2);
3996 if (res == MAYBE)
3997 goto on_error;
3998 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
3999 res = !res;
4000 clear_tv(tv1);
4001 clear_tv(tv2);
4002 --ectx->ec_stack.ga_len;
4003 tv1->v_type = VAR_BOOL;
4004 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4005 }
4006 break;
4007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 // Operation with two number arguments
4009 case ISN_OPNR:
4010 case ISN_COMPARENR:
4011 {
4012 typval_T *tv1 = STACK_TV_BOT(-2);
4013 typval_T *tv2 = STACK_TV_BOT(-1);
4014 varnumber_T arg1 = tv1->vval.v_number;
4015 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004016 varnumber_T res = 0;
4017 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018
4019 switch (iptr->isn_arg.op.op_type)
4020 {
4021 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004022 case EXPR_DIV: if (arg2 == 0)
4023 div_zero = TRUE;
4024 else
4025 res = arg1 / arg2;
4026 break;
4027 case EXPR_REM: if (arg2 == 0)
4028 div_zero = TRUE;
4029 else
4030 res = arg1 % arg2;
4031 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 case EXPR_SUB: res = arg1 - arg2; break;
4033 case EXPR_ADD: res = arg1 + arg2; break;
4034
4035 case EXPR_EQUAL: res = arg1 == arg2; break;
4036 case EXPR_NEQUAL: res = arg1 != arg2; break;
4037 case EXPR_GREATER: res = arg1 > arg2; break;
4038 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4039 case EXPR_SMALLER: res = arg1 < arg2; break;
4040 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004041 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 }
4043
Bram Moolenaar4c137212021-04-19 16:48:48 +02004044 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 if (iptr->isn_type == ISN_COMPARENR)
4046 {
4047 tv1->v_type = VAR_BOOL;
4048 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4049 }
4050 else
4051 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004052 if (div_zero)
4053 {
4054 SOURCING_LNUM = iptr->isn_lnum;
4055 emsg(_(e_divide_by_zero));
4056 goto on_error;
4057 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 }
4059 break;
4060
4061 // Computation with two float arguments
4062 case ISN_OPFLOAT:
4063 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004064#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 {
4066 typval_T *tv1 = STACK_TV_BOT(-2);
4067 typval_T *tv2 = STACK_TV_BOT(-1);
4068 float_T arg1 = tv1->vval.v_float;
4069 float_T arg2 = tv2->vval.v_float;
4070 float_T res = 0;
4071 int cmp = FALSE;
4072
4073 switch (iptr->isn_arg.op.op_type)
4074 {
4075 case EXPR_MULT: res = arg1 * arg2; break;
4076 case EXPR_DIV: res = arg1 / arg2; break;
4077 case EXPR_SUB: res = arg1 - arg2; break;
4078 case EXPR_ADD: res = arg1 + arg2; break;
4079
4080 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4081 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4082 case EXPR_GREATER: cmp = arg1 > arg2; break;
4083 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4084 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4085 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4086 default: cmp = 0; break;
4087 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004088 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 if (iptr->isn_type == ISN_COMPAREFLOAT)
4090 {
4091 tv1->v_type = VAR_BOOL;
4092 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4093 }
4094 else
4095 tv1->vval.v_float = res;
4096 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004097#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 break;
4099
4100 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004101 case ISN_COMPAREDICT:
4102 case ISN_COMPAREFUNC:
4103 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 case ISN_COMPAREBLOB:
4105 {
4106 typval_T *tv1 = STACK_TV_BOT(-2);
4107 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004108 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4109 int ic = iptr->isn_arg.op.op_ic;
4110 int res = FALSE;
4111 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112
Bram Moolenaar265f8112021-12-19 12:33:05 +00004113 SOURCING_LNUM = iptr->isn_lnum;
4114 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004116 status = typval_compare_list(tv1, tv2,
4117 exprtype, ic, &res);
4118 }
4119 else if (iptr->isn_type == ISN_COMPAREDICT)
4120 {
4121 status = typval_compare_dict(tv1, tv2,
4122 exprtype, ic, &res);
4123 }
4124 else if (iptr->isn_type == ISN_COMPAREFUNC)
4125 {
4126 status = typval_compare_func(tv1, tv2,
4127 exprtype, ic, &res);
4128 }
4129 else if (iptr->isn_type == ISN_COMPARESTRING)
4130 {
4131 status = typval_compare_string(tv1, tv2,
4132 exprtype, ic, &res);
4133 }
4134 else
4135 {
4136 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004138 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 clear_tv(tv1);
4140 clear_tv(tv2);
4141 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004142 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4143 if (status == FAIL)
4144 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 }
4146 break;
4147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 case ISN_COMPAREANY:
4149 {
4150 typval_T *tv1 = STACK_TV_BOT(-2);
4151 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004152 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004154 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155
Bram Moolenaareb26f432020-09-14 16:50:05 +02004156 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004157 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004159 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004160 if (status == FAIL)
4161 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 }
4163 break;
4164
4165 case ISN_ADDLIST:
4166 case ISN_ADDBLOB:
4167 {
4168 typval_T *tv1 = STACK_TV_BOT(-2);
4169 typval_T *tv2 = STACK_TV_BOT(-1);
4170
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004171 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004173 {
4174 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4175 && tv1->vval.v_list != NULL)
4176 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4177 NULL);
4178 else
4179 eval_addlist(tv1, tv2);
4180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 else
4182 eval_addblob(tv1, tv2);
4183 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004184 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 }
4186 break;
4187
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004188 case ISN_LISTAPPEND:
4189 {
4190 typval_T *tv1 = STACK_TV_BOT(-2);
4191 typval_T *tv2 = STACK_TV_BOT(-1);
4192 list_T *l = tv1->vval.v_list;
4193
4194 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004195 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004196 if (l == NULL)
4197 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004198 emsg(_(e_cannot_add_to_null_list));
4199 goto on_error;
4200 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004201 if (value_check_lock(l->lv_lock, NULL, FALSE))
4202 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004203 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004204 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004205 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004206 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004207 }
4208 break;
4209
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004210 case ISN_BLOBAPPEND:
4211 {
4212 typval_T *tv1 = STACK_TV_BOT(-2);
4213 typval_T *tv2 = STACK_TV_BOT(-1);
4214 blob_T *b = tv1->vval.v_blob;
4215 int error = FALSE;
4216 varnumber_T n;
4217
4218 // add a number to a blob
4219 if (b == NULL)
4220 {
4221 SOURCING_LNUM = iptr->isn_lnum;
4222 emsg(_(e_cannot_add_to_null_blob));
4223 goto on_error;
4224 }
4225 n = tv_get_number_chk(tv2, &error);
4226 if (error)
4227 goto on_error;
4228 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004229 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004230 }
4231 break;
4232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 // Computation with two arguments of unknown type
4234 case ISN_OPANY:
4235 {
4236 typval_T *tv1 = STACK_TV_BOT(-2);
4237 typval_T *tv2 = STACK_TV_BOT(-1);
4238 varnumber_T n1, n2;
4239#ifdef FEAT_FLOAT
4240 float_T f1 = 0, f2 = 0;
4241#endif
4242 int error = FALSE;
4243
4244 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4245 {
4246 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4247 {
4248 eval_addlist(tv1, tv2);
4249 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004250 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 break;
4252 }
4253 else if (tv1->v_type == VAR_BLOB
4254 && tv2->v_type == VAR_BLOB)
4255 {
4256 eval_addblob(tv1, tv2);
4257 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004258 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 break;
4260 }
4261 }
4262#ifdef FEAT_FLOAT
4263 if (tv1->v_type == VAR_FLOAT)
4264 {
4265 f1 = tv1->vval.v_float;
4266 n1 = 0;
4267 }
4268 else
4269#endif
4270 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004271 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 n1 = tv_get_number_chk(tv1, &error);
4273 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004274 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275#ifdef FEAT_FLOAT
4276 if (tv2->v_type == VAR_FLOAT)
4277 f1 = n1;
4278#endif
4279 }
4280#ifdef FEAT_FLOAT
4281 if (tv2->v_type == VAR_FLOAT)
4282 {
4283 f2 = tv2->vval.v_float;
4284 n2 = 0;
4285 }
4286 else
4287#endif
4288 {
4289 n2 = tv_get_number_chk(tv2, &error);
4290 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004291 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292#ifdef FEAT_FLOAT
4293 if (tv1->v_type == VAR_FLOAT)
4294 f2 = n2;
4295#endif
4296 }
4297#ifdef FEAT_FLOAT
4298 // if there is a float on either side the result is a float
4299 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4300 {
4301 switch (iptr->isn_arg.op.op_type)
4302 {
4303 case EXPR_MULT: f1 = f1 * f2; break;
4304 case EXPR_DIV: f1 = f1 / f2; break;
4305 case EXPR_SUB: f1 = f1 - f2; break;
4306 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004307 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004308 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004309 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 }
4311 clear_tv(tv1);
4312 clear_tv(tv2);
4313 tv1->v_type = VAR_FLOAT;
4314 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004315 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 }
4317 else
4318#endif
4319 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004320 int failed = FALSE;
4321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 switch (iptr->isn_arg.op.op_type)
4323 {
4324 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004325 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4326 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004327 goto on_error;
4328 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 case EXPR_SUB: n1 = n1 - n2; break;
4330 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004331 default: n1 = num_modulus(n1, n2, &failed);
4332 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004333 goto on_error;
4334 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 }
4336 clear_tv(tv1);
4337 clear_tv(tv2);
4338 tv1->v_type = VAR_NUMBER;
4339 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004340 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 }
4342 }
4343 break;
4344
4345 case ISN_CONCAT:
4346 {
4347 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4348 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4349 char_u *res;
4350
4351 res = concat_str(str1, str2);
4352 clear_tv(STACK_TV_BOT(-2));
4353 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004354 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 STACK_TV_BOT(-1)->vval.v_string = res;
4356 }
4357 break;
4358
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004359 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004360 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004361 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004362 int is_slice = iptr->isn_type == ISN_STRSLICE;
4363 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004364 char_u *res;
4365
4366 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004367 // string slice: string is at stack-3, first index at
4368 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004369 if (is_slice)
4370 {
4371 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004372 n1 = tv->vval.v_number;
4373 }
4374
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004375 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004376 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004377
Bram Moolenaar4c137212021-04-19 16:48:48 +02004378 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004379 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004380 if (is_slice)
4381 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004382 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004383 else
4384 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004385 // single character (including composing characters).
4386 // If the index is too big or negative the result is
4387 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004388 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004389 vim_free(tv->vval.v_string);
4390 tv->vval.v_string = res;
4391 }
4392 break;
4393
4394 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004395 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004396 case ISN_BLOBINDEX:
4397 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004399 int is_slice = iptr->isn_type == ISN_LISTSLICE
4400 || iptr->isn_type == ISN_BLOBSLICE;
4401 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4402 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004403 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004404 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405
4406 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004407 // list slice: list is at stack-3, indexes at stack-2 and
4408 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004409 // Same for blob.
4410 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411
4412 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004413 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004415
4416 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 {
Bram Moolenaared591872020-08-15 22:14:53 +02004418 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004419 n1 = tv->vval.v_number;
4420 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 }
Bram Moolenaared591872020-08-15 22:14:53 +02004422
Bram Moolenaar4c137212021-04-19 16:48:48 +02004423 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004424 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004425 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004426 if (is_blob)
4427 {
4428 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4429 n1, n2, FALSE, tv) == FAIL)
4430 goto on_error;
4431 }
4432 else
4433 {
4434 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4435 n1, n2, FALSE, tv, TRUE) == FAIL)
4436 goto on_error;
4437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 }
4439 break;
4440
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004441 case ISN_ANYINDEX:
4442 case ISN_ANYSLICE:
4443 {
4444 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4445 typval_T *var1, *var2;
4446 int res;
4447
4448 // index: composite is at stack-2, index at stack-1
4449 // slice: composite is at stack-3, indexes at stack-2 and
4450 // stack-1
4451 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004452 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004453 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4454 goto on_error;
4455 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4456 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004457 res = eval_index_inner(tv, is_slice, var1, var2,
4458 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004459 clear_tv(var1);
4460 if (is_slice)
4461 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004462 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004463 if (res == FAIL)
4464 goto on_error;
4465 }
4466 break;
4467
Bram Moolenaar9af78762020-06-16 11:34:42 +02004468 case ISN_SLICE:
4469 {
4470 list_T *list;
4471 int count = iptr->isn_arg.number;
4472
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004473 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004474 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004475 list = tv->vval.v_list;
4476
4477 // no error for short list, expect it to be checked earlier
4478 if (list != NULL && list->lv_len >= count)
4479 {
4480 list_T *newlist = list_slice(list,
4481 count, list->lv_len - 1);
4482
4483 if (newlist != NULL)
4484 {
4485 list_unref(list);
4486 tv->vval.v_list = newlist;
4487 ++newlist->lv_refcount;
4488 }
4489 }
4490 }
4491 break;
4492
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004493 case ISN_GETITEM:
4494 {
4495 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004496 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004497
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004498 // Get list item: list is at stack-1, push item.
4499 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004500 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4501 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004502
Bram Moolenaar35578162021-08-02 19:10:38 +02004503 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004504 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004505 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004506 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004507
4508 // Useful when used in unpack assignment. Reset at
4509 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004510 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004511 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004512 }
4513 break;
4514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 case ISN_MEMBER:
4516 {
4517 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004518 char_u *key;
4519 dictitem_T *di;
4520
4521 // dict member: dict is at stack-2, key at stack-1
4522 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004523 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004524 dict = tv->vval.v_dict;
4525
4526 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004527 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004528 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004529 if (key == NULL)
4530 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004531
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004532 if ((di = dict_find(dict, key, -1)) == NULL)
4533 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004534 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004535 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004536
4537 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004538 // stack contents makes sense and the dict stack is
4539 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004540 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004541 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004542 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004543 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004544 tv->v_type = VAR_NUMBER;
4545 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004546 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004547 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004548 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004549 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004550 // Put the dict used on the dict stack, it might be used by
4551 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004552 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004553 if (dict_stack_save(tv) == FAIL)
4554 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004555 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004556 }
4557 break;
4558
4559 // dict member with string key
4560 case ISN_STRINGMEMBER:
4561 {
4562 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 dictitem_T *di;
4564
4565 tv = STACK_TV_BOT(-1);
4566 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4567 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004568 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004569 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004570 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 }
4572 dict = tv->vval.v_dict;
4573
4574 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4575 == NULL)
4576 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004577 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004578 semsg(_(e_key_not_present_in_dictionary),
4579 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004580 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004582 // Put the dict used on the dict stack, it might be used by
4583 // a dict function later.
4584 if (dict_stack_save(tv) == FAIL)
4585 goto on_fatal_error;
4586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004588 }
4589 break;
4590
4591 case ISN_CLEARDICT:
4592 dict_stack_drop();
4593 break;
4594
4595 case ISN_USEDICT:
4596 {
4597 typval_T *dict_tv = dict_stack_get_tv();
4598
4599 // Turn "dict.Func" into a partial for "Func" bound to
4600 // "dict". Don't do this when "Func" is already a partial
4601 // that was bound explicitly (pt_auto is FALSE).
4602 tv = STACK_TV_BOT(-1);
4603 if (dict_tv != NULL
4604 && dict_tv->v_type == VAR_DICT
4605 && dict_tv->vval.v_dict != NULL
4606 && (tv->v_type == VAR_FUNC
4607 || (tv->v_type == VAR_PARTIAL
4608 && (tv->vval.v_partial->pt_auto
4609 || tv->vval.v_partial->pt_dict == NULL))))
4610 dict_tv->vval.v_dict =
4611 make_partial(dict_tv->vval.v_dict, tv);
4612 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 }
4614 break;
4615
4616 case ISN_NEGATENR:
4617 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004618 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004619#ifdef FEAT_FLOAT
4620 if (tv->v_type == VAR_FLOAT)
4621 tv->vval.v_float = -tv->vval.v_float;
4622 else
4623#endif
4624 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 break;
4626
4627 case ISN_CHECKNR:
4628 {
4629 int error = FALSE;
4630
4631 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004632 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004634 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 (void)tv_get_number_chk(tv, &error);
4636 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004637 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 }
4639 break;
4640
4641 case ISN_CHECKTYPE:
4642 {
4643 checktype_T *ct = &iptr->isn_arg.type;
4644
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004645 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004646 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004647 if (!ectx->ec_where.wt_variable)
4648 ectx->ec_where.wt_index = ct->ct_arg_idx;
4649 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4650 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004651 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004652 if (!ectx->ec_where.wt_variable)
4653 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004654
4655 // number 0 is FALSE, number 1 is TRUE
4656 if (tv->v_type == VAR_NUMBER
4657 && ct->ct_type->tt_type == VAR_BOOL
4658 && (tv->vval.v_number == 0
4659 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004661 tv->v_type = VAR_BOOL;
4662 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004663 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 }
4665 }
4666 break;
4667
Bram Moolenaar9af78762020-06-16 11:34:42 +02004668 case ISN_CHECKLEN:
4669 {
4670 int min_len = iptr->isn_arg.checklen.cl_min_len;
4671 list_T *list = NULL;
4672
4673 tv = STACK_TV_BOT(-1);
4674 if (tv->v_type == VAR_LIST)
4675 list = tv->vval.v_list;
4676 if (list == NULL || list->lv_len < min_len
4677 || (list->lv_len > min_len
4678 && !iptr->isn_arg.checklen.cl_more_OK))
4679 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004680 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004681 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004682 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004683 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004684 }
4685 }
4686 break;
4687
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004688 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004689 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004690 break;
4691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004693 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 {
4695 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004696 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004698 if (iptr->isn_type == ISN_2BOOL)
4699 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004700 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004701 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004702 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004703 n = !n;
4704 }
4705 else
4706 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004707 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004708 SOURCING_LNUM = iptr->isn_lnum;
4709 n = tv_get_bool_chk(tv, &error);
4710 if (error)
4711 goto on_error;
4712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 clear_tv(tv);
4714 tv->v_type = VAR_BOOL;
4715 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4716 }
4717 break;
4718
4719 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004720 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004721 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004722 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4723 iptr->isn_type == ISN_2STRING_ANY,
4724 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004725 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 break;
4727
Bram Moolenaar08597872020-12-10 19:43:40 +01004728 case ISN_RANGE:
4729 {
4730 exarg_T ea;
4731 char *errormsg;
4732
Bram Moolenaarece0b872021-01-08 20:40:45 +01004733 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004734 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004735 ea.addr_type = ADDR_LINES;
4736 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004737 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004738 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004739 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004740
Bram Moolenaar35578162021-08-02 19:10:38 +02004741 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004742 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004743 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004744 tv = STACK_TV_BOT(-1);
4745 tv->v_type = VAR_NUMBER;
4746 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004747 if (ea.addr_count == 0)
4748 tv->vval.v_number = curwin->w_cursor.lnum;
4749 else
4750 tv->vval.v_number = ea.line2;
4751 }
4752 break;
4753
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004754 case ISN_PUT:
4755 {
4756 int regname = iptr->isn_arg.put.put_regname;
4757 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4758 char_u *expr = NULL;
4759 int dir = FORWARD;
4760
Bram Moolenaar08597872020-12-10 19:43:40 +01004761 if (lnum < -2)
4762 {
4763 // line number was put on the stack by ISN_RANGE
4764 tv = STACK_TV_BOT(-1);
4765 curwin->w_cursor.lnum = tv->vval.v_number;
4766 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4767 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004768 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004769 }
4770 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004771 // :put! above cursor
4772 dir = BACKWARD;
4773 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004774 {
4775 curwin->w_cursor.lnum = lnum;
4776 if (lnum == 0)
4777 // check_cursor() below will move to line 1
4778 dir = BACKWARD;
4779 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004780
4781 if (regname == '=')
4782 {
4783 tv = STACK_TV_BOT(-1);
4784 if (tv->v_type == VAR_STRING)
4785 expr = tv->vval.v_string;
4786 else
4787 {
4788 expr = typval2string(tv, TRUE); // allocates value
4789 clear_tv(tv);
4790 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004791 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004792 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004793 check_cursor();
4794 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4795 vim_free(expr);
4796 }
4797 break;
4798
Bram Moolenaar02194d22020-10-24 23:08:38 +02004799 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004800 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4801 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4802 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4803 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004804 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4805 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004806 break;
4807
Bram Moolenaar02194d22020-10-24 23:08:38 +02004808 case ISN_CMDMOD_REV:
4809 // filter regprog is owned by the instruction, don't free it
4810 cmdmod.cmod_filter_regmatch.regprog = NULL;
4811 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004812 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4813 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004814 break;
4815
Bram Moolenaar792f7862020-11-23 08:31:18 +01004816 case ISN_UNPACK:
4817 {
4818 int count = iptr->isn_arg.unpack.unp_count;
4819 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4820 list_T *l;
4821 listitem_T *li;
4822 int i;
4823
4824 // Check there is a valid list to unpack.
4825 tv = STACK_TV_BOT(-1);
4826 if (tv->v_type != VAR_LIST)
4827 {
4828 SOURCING_LNUM = iptr->isn_lnum;
4829 emsg(_(e_for_argument_must_be_sequence_of_lists));
4830 goto on_error;
4831 }
4832 l = tv->vval.v_list;
4833 if (l == NULL
4834 || l->lv_len < (semicolon ? count - 1 : count))
4835 {
4836 SOURCING_LNUM = iptr->isn_lnum;
4837 emsg(_(e_list_value_does_not_have_enough_items));
4838 goto on_error;
4839 }
4840 else if (!semicolon && l->lv_len > count)
4841 {
4842 SOURCING_LNUM = iptr->isn_lnum;
4843 emsg(_(e_list_value_has_more_items_than_targets));
4844 goto on_error;
4845 }
4846
4847 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004848 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004849 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004850 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004851
4852 // Variable after semicolon gets a list with the remaining
4853 // items.
4854 if (semicolon)
4855 {
4856 list_T *rem_list =
4857 list_alloc_with_items(l->lv_len - count + 1);
4858
4859 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004860 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004861 tv = STACK_TV_BOT(-count);
4862 tv->vval.v_list = rem_list;
4863 ++rem_list->lv_refcount;
4864 tv->v_lock = 0;
4865 li = l->lv_first;
4866 for (i = 0; i < count - 1; ++i)
4867 li = li->li_next;
4868 for (i = 0; li != NULL; ++i)
4869 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004870 typval_T tvcopy;
4871
4872 copy_tv(&li->li_tv, &tvcopy);
4873 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004874 li = li->li_next;
4875 }
4876 --count;
4877 }
4878
4879 // Produce the values in reverse order, first item last.
4880 li = l->lv_first;
4881 for (i = 0; i < count; ++i)
4882 {
4883 tv = STACK_TV_BOT(-i - 1);
4884 copy_tv(&li->li_tv, tv);
4885 li = li->li_next;
4886 }
4887
4888 list_unref(l);
4889 }
4890 break;
4891
Bram Moolenaarb2049902021-01-24 12:53:53 +01004892 case ISN_PROF_START:
4893 case ISN_PROF_END:
4894 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004895#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004896 funccall_T cookie;
4897 ufunc_T *cur_ufunc =
4898 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004899 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004900
4901 cookie.func = cur_ufunc;
4902 if (iptr->isn_type == ISN_PROF_START)
4903 {
4904 func_line_start(&cookie, iptr->isn_lnum);
4905 // if we get here the instruction is executed
4906 func_line_exec(&cookie);
4907 }
4908 else
4909 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004910#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004911 }
4912 break;
4913
Bram Moolenaare99d4222021-06-13 14:01:26 +02004914 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004915 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004916 break;
4917
Bram Moolenaar389df252020-07-09 21:20:47 +02004918 case ISN_SHUFFLE:
4919 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004920 typval_T tmp_tv;
4921 int item = iptr->isn_arg.shuffle.shfl_item;
4922 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004923
4924 tmp_tv = *STACK_TV_BOT(-item);
4925 for ( ; up > 0 && item > 1; --up)
4926 {
4927 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4928 --item;
4929 }
4930 *STACK_TV_BOT(-item) = tmp_tv;
4931 }
4932 break;
4933
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004935 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004937 ectx->ec_where.wt_index = 0;
4938 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 break;
4940 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004941 continue;
4942
Bram Moolenaard032f342020-07-18 18:13:02 +02004943func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004944 // Restore previous function. If the frame pointer is where we started
4945 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004946 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004947 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004948
Bram Moolenaar4c137212021-04-19 16:48:48 +02004949 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004950 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004951 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004952 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004953
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004954on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004955 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004956 // If "emsg_silent" is set then ignore the error, unless it was set
4957 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004958 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004959 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004960 {
4961 // If a sequence of instructions causes an error while ":silent!"
4962 // was used, restore the stack length and jump ahead to restoring
4963 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004964 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004965 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004966 while (ectx->ec_stack.ga_len
4967 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004968 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004969 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004970 clear_tv(STACK_TV_BOT(0));
4971 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004972 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4973 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004974 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004975 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004976 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004977on_fatal_error:
4978 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004979 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004980 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004981 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 }
4983
4984done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004985 ret = OK;
4986theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004987 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004988 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4989 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004990}
4991
4992/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004993 * Execute the instructions from a VAR_INSTR typeval and put the result in
4994 * "rettv".
4995 * Return OK or FAIL.
4996 */
4997 int
4998exe_typval_instr(typval_T *tv, typval_T *rettv)
4999{
5000 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5001 isn_T *save_instr = ectx->ec_instr;
5002 int save_iidx = ectx->ec_iidx;
5003 int res;
5004
5005 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5006 res = exec_instructions(ectx);
5007 if (res == OK)
5008 {
5009 *rettv = *STACK_TV_BOT(-1);
5010 --ectx->ec_stack.ga_len;
5011 }
5012
5013 ectx->ec_instr = save_instr;
5014 ectx->ec_iidx = save_iidx;
5015
5016 return res;
5017}
5018
5019/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005020 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5021 * "substitute_instr".
5022 */
5023 char_u *
5024exe_substitute_instr(void)
5025{
5026 ectx_T *ectx = substitute_instr->subs_ectx;
5027 isn_T *save_instr = ectx->ec_instr;
5028 int save_iidx = ectx->ec_iidx;
5029 char_u *res;
5030
5031 ectx->ec_instr = substitute_instr->subs_instr;
5032 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005033 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005034 typval_T *tv = STACK_TV_BOT(-1);
5035
Bram Moolenaar27523602021-06-05 21:36:19 +02005036 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005037 --ectx->ec_stack.ga_len;
5038 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005039 }
5040 else
5041 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005042 substitute_instr->subs_status = FAIL;
5043 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005044 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045
Bram Moolenaar4c137212021-04-19 16:48:48 +02005046 ectx->ec_instr = save_instr;
5047 ectx->ec_iidx = save_iidx;
5048
5049 return res;
5050}
5051
5052/*
5053 * Call a "def" function from old Vim script.
5054 * Return OK or FAIL.
5055 */
5056 int
5057call_def_function(
5058 ufunc_T *ufunc,
5059 int argc_arg, // nr of arguments
5060 typval_T *argv, // arguments
5061 partial_T *partial, // optional partial for context
5062 typval_T *rettv) // return value
5063{
5064 ectx_T ectx; // execution context
5065 int argc = argc_arg;
5066 typval_T *tv;
5067 int idx;
5068 int ret = FAIL;
5069 int defcount = ufunc->uf_args.ga_len - argc;
5070 sctx_T save_current_sctx = current_sctx;
5071 int did_emsg_before = did_emsg_cumul + did_emsg;
5072 int save_suppress_errthrow = suppress_errthrow;
5073 msglist_T **saved_msg_list = NULL;
5074 msglist_T *private_msg_list = NULL;
5075 int save_emsg_silent_def = emsg_silent_def;
5076 int save_did_emsg_def = did_emsg_def;
5077 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005078 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005079
5080// Get pointer to item in the stack.
5081#undef STACK_TV
5082#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5083
5084// Get pointer to item at the bottom of the stack, -1 is the bottom.
5085#undef STACK_TV_BOT
5086#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5087
5088// Get pointer to a local variable on the stack. Negative for arguments.
5089#undef STACK_TV_VAR
5090#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5091
5092 if (ufunc->uf_def_status == UF_NOT_COMPILED
5093 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005094 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5095 && compile_def_function(ufunc, FALSE,
5096 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005097 {
5098 if (did_emsg_cumul + did_emsg == did_emsg_before)
5099 semsg(_(e_function_is_not_compiled_str),
5100 printable_func_name(ufunc));
5101 return FAIL;
5102 }
5103
5104 {
5105 // Check the function was really compiled.
5106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5107 + ufunc->uf_dfunc_idx;
5108 if (INSTRUCTIONS(dfunc) == NULL)
5109 {
5110 iemsg("using call_def_function() on not compiled function");
5111 return FAIL;
5112 }
5113 }
5114
5115 // If depth of calling is getting too high, don't execute the function.
5116 orig_funcdepth = funcdepth_get();
5117 if (funcdepth_increment() == FAIL)
5118 return FAIL;
5119
5120 CLEAR_FIELD(ectx);
5121 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5122 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005123 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005124 {
5125 funcdepth_decrement();
5126 return FAIL;
5127 }
5128 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5129 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5130 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005131 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005132
5133 idx = argc - ufunc->uf_args.ga_len;
5134 if (idx > 0 && ufunc->uf_va_name == NULL)
5135 {
5136 if (idx == 1)
5137 emsg(_(e_one_argument_too_many));
5138 else
5139 semsg(_(e_nr_arguments_too_many), idx);
5140 goto failed_early;
5141 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005142 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5143 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005144 {
5145 if (idx == -1)
5146 emsg(_(e_one_argument_too_few));
5147 else
5148 semsg(_(e_nr_arguments_too_few), -idx);
5149 goto failed_early;
5150 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005151
5152 // Put arguments on the stack, but no more than what the function expects.
5153 // A lambda can be called with more arguments than it uses.
5154 for (idx = 0; idx < argc
5155 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5156 ++idx)
5157 {
5158 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5159 && argv[idx].v_type == VAR_SPECIAL
5160 && argv[idx].vval.v_number == VVAL_NONE)
5161 {
5162 // Use the default value.
5163 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5164 }
5165 else
5166 {
5167 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5168 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005169 ufunc->uf_arg_types[idx], &argv[idx],
5170 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005171 goto failed_early;
5172 copy_tv(&argv[idx], STACK_TV_BOT(0));
5173 }
5174 ++ectx.ec_stack.ga_len;
5175 }
5176
5177 // Turn varargs into a list. Empty list if no args.
5178 if (ufunc->uf_va_name != NULL)
5179 {
5180 int vararg_count = argc - ufunc->uf_args.ga_len;
5181
5182 if (vararg_count < 0)
5183 vararg_count = 0;
5184 else
5185 argc -= vararg_count;
5186 if (exe_newlist(vararg_count, &ectx) == FAIL)
5187 goto failed_early;
5188
5189 // Check the type of the list items.
5190 tv = STACK_TV_BOT(-1);
5191 if (ufunc->uf_va_type != NULL
5192 && ufunc->uf_va_type != &t_list_any
5193 && ufunc->uf_va_type->tt_member != &t_any
5194 && tv->vval.v_list != NULL)
5195 {
5196 type_T *expected = ufunc->uf_va_type->tt_member;
5197 listitem_T *li = tv->vval.v_list->lv_first;
5198
5199 for (idx = 0; idx < vararg_count; ++idx)
5200 {
5201 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005202 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005203 goto failed_early;
5204 li = li->li_next;
5205 }
5206 }
5207
5208 if (defcount > 0)
5209 // Move varargs list to below missing default arguments.
5210 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5211 --ectx.ec_stack.ga_len;
5212 }
5213
5214 // Make space for omitted arguments, will store default value below.
5215 // Any varargs list goes after them.
5216 if (defcount > 0)
5217 for (idx = 0; idx < defcount; ++idx)
5218 {
5219 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5220 ++ectx.ec_stack.ga_len;
5221 }
5222 if (ufunc->uf_va_name != NULL)
5223 ++ectx.ec_stack.ga_len;
5224
5225 // Frame pointer points to just after arguments.
5226 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5227 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5228
5229 {
5230 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5231 + ufunc->uf_dfunc_idx;
5232 ufunc_T *base_ufunc = dfunc->df_ufunc;
5233
5234 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5235 // by copy_func().
5236 if (partial != NULL || base_ufunc->uf_partial != NULL)
5237 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005238 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5239 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005240 goto failed_early;
5241 if (partial != NULL)
5242 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005243 outer_T *outer = get_pt_outer(partial);
5244
5245 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005246 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005247 if (current_ectx != NULL)
5248 {
5249 if (current_ectx->ec_outer_ref != NULL
5250 && current_ectx->ec_outer_ref->or_outer != NULL)
5251 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005252 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005253 }
5254 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005255 }
5256 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005257 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005258 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005259 ++partial->pt_refcount;
5260 ectx.ec_outer_ref->or_partial = partial;
5261 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005262 }
5263 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005264 {
5265 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5266 ++base_ufunc->uf_partial->pt_refcount;
5267 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5268 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005269 }
5270 }
5271
5272 // dummy frame entries
5273 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5274 {
5275 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5276 ++ectx.ec_stack.ga_len;
5277 }
5278
5279 {
5280 // Reserve space for local variables and any closure reference count.
5281 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5282 + ufunc->uf_dfunc_idx;
5283
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005284 // Initialize variables to zero. That avoids having to generate
5285 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005286 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005287 {
5288 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5289 STACK_TV_VAR(idx)->vval.v_number = 0;
5290 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005291 ectx.ec_stack.ga_len += dfunc->df_varcount;
5292 if (dfunc->df_has_closure)
5293 {
5294 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5295 STACK_TV_VAR(idx)->vval.v_number = 0;
5296 ++ectx.ec_stack.ga_len;
5297 }
5298
5299 ectx.ec_instr = INSTRUCTIONS(dfunc);
5300 }
5301
5302 // Following errors are in the function, not the caller.
5303 // Commands behave like vim9script.
5304 estack_push_ufunc(ufunc, 1);
5305 current_sctx = ufunc->uf_script_ctx;
5306 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5307
5308 // Use a specific location for storing error messages to be converted to an
5309 // exception.
5310 saved_msg_list = msg_list;
5311 msg_list = &private_msg_list;
5312
5313 // Do turn errors into exceptions.
5314 suppress_errthrow = FALSE;
5315
5316 // Do not delete the function while executing it.
5317 ++ufunc->uf_calls;
5318
5319 // When ":silent!" was used before calling then we still abort the
5320 // function. If ":silent!" is used in the function then we don't.
5321 emsg_silent_def = emsg_silent;
5322 did_emsg_def = 0;
5323
5324 ectx.ec_where.wt_index = 0;
5325 ectx.ec_where.wt_variable = FALSE;
5326
5327 // Execute the instructions until done.
5328 ret = exec_instructions(&ectx);
5329 if (ret == OK)
5330 {
5331 // function finished, get result from the stack.
5332 if (ufunc->uf_ret_type == &t_void)
5333 {
5334 rettv->v_type = VAR_VOID;
5335 }
5336 else
5337 {
5338 tv = STACK_TV_BOT(-1);
5339 *rettv = *tv;
5340 tv->v_type = VAR_UNKNOWN;
5341 }
5342 }
5343
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005344 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005345 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5346 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005347
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005348 // Deal with any remaining closures, they may be in use somewhere.
5349 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005350 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005351 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005352 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005353 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005354
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005355 estack_pop();
5356 current_sctx = save_current_sctx;
5357
Bram Moolenaar2336c372021-12-06 15:06:54 +00005358 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5359 // Function was unreferenced while being used, free it now.
5360 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005361
Bram Moolenaar352134b2020-10-17 22:04:08 +02005362 if (*msg_list != NULL && saved_msg_list != NULL)
5363 {
5364 msglist_T **plist = saved_msg_list;
5365
5366 // Append entries from the current msg_list (uncaught exceptions) to
5367 // the saved msg_list.
5368 while (*plist != NULL)
5369 plist = &(*plist)->next;
5370
5371 *plist = *msg_list;
5372 }
5373 msg_list = saved_msg_list;
5374
Bram Moolenaar4c137212021-04-19 16:48:48 +02005375 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005376 {
5377 cmdmod.cmod_filter_regmatch.regprog = NULL;
5378 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005379 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005380 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005381 emsg_silent_def = save_emsg_silent_def;
5382 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005383
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005384failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005385 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5387 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005388 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005391 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005392 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005393 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005394 if (ectx.ec_outer_ref->or_outer_allocated)
5395 vim_free(ectx.ec_outer_ref->or_outer);
5396 partial_unref(ectx.ec_outer_ref->or_partial);
5397 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005398 }
5399
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005400 // Not sure if this is necessary.
5401 suppress_errthrow = save_suppress_errthrow;
5402
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005403 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5404 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005405 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005406 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005407 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005408 return ret;
5409}
5410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005411/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005412 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5413 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5414 * "pfx" is prefixed to every line.
5415 */
5416 static void
5417list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5418{
5419 int line_idx = 0;
5420 int prev_current = 0;
5421 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005422 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005423
5424 for (current = 0; current < instr_count; ++current)
5425 {
5426 isn_T *iptr = &instr[current];
5427 char *line;
5428
5429 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005430 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005431 while (line_idx < iptr->isn_lnum
5432 && line_idx < ufunc->uf_lines.ga_len)
5433 {
5434 if (current > prev_current)
5435 {
5436 msg_puts("\n\n");
5437 prev_current = current;
5438 }
5439 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5440 if (line != NULL)
5441 msg(line);
5442 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005443 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5444 {
5445 int first_def_arg = ufunc->uf_args.ga_len
5446 - ufunc->uf_def_args.ga_len;
5447
5448 if (def_arg_idx > 0)
5449 msg_puts("\n\n");
5450 msg_start();
5451 msg_puts(" ");
5452 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5453 first_def_arg + def_arg_idx]);
5454 msg_puts(" = ");
5455 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5456 msg_clr_eos();
5457 msg_end();
5458 }
5459 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005460
5461 switch (iptr->isn_type)
5462 {
5463 case ISN_EXEC:
5464 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5465 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005466 case ISN_EXEC_SPLIT:
5467 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5468 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005469 case ISN_EXECRANGE:
5470 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5471 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005472 case ISN_LEGACY_EVAL:
5473 smsg("%s%4d EVAL legacy %s", pfx, current,
5474 iptr->isn_arg.string);
5475 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005476 case ISN_REDIRSTART:
5477 smsg("%s%4d REDIR", pfx, current);
5478 break;
5479 case ISN_REDIREND:
5480 smsg("%s%4d REDIR END%s", pfx, current,
5481 iptr->isn_arg.number ? " append" : "");
5482 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005483 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005484#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005485 smsg("%s%4d CEXPR pre %s", pfx, current,
5486 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005487#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005488 break;
5489 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005490#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005491 {
5492 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5493
5494 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5495 cexpr_get_auname(cer->cer_cmdidx),
5496 cer->cer_forceit ? "!" : "",
5497 cer->cer_cmdline);
5498 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005499#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005500 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005501 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005502 smsg("%s%4d INSTR", pfx, current);
5503 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5504 msg(" -------------");
5505 break;
5506 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005507 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005508 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5509
5510 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005511 }
5512 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005513 case ISN_SUBSTITUTE:
5514 {
5515 subs_T *subs = &iptr->isn_arg.subs;
5516
5517 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5518 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5519 msg(" -------------");
5520 }
5521 break;
5522 case ISN_EXECCONCAT:
5523 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5524 (varnumber_T)iptr->isn_arg.number);
5525 break;
5526 case ISN_ECHO:
5527 {
5528 echo_T *echo = &iptr->isn_arg.echo;
5529
5530 smsg("%s%4d %s %d", pfx, current,
5531 echo->echo_with_white ? "ECHO" : "ECHON",
5532 echo->echo_count);
5533 }
5534 break;
5535 case ISN_EXECUTE:
5536 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005537 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005538 break;
5539 case ISN_ECHOMSG:
5540 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005541 (varnumber_T)(iptr->isn_arg.number));
5542 break;
5543 case ISN_ECHOCONSOLE:
5544 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5545 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005546 break;
5547 case ISN_ECHOERR:
5548 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005549 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005550 break;
5551 case ISN_LOAD:
5552 {
5553 if (iptr->isn_arg.number < 0)
5554 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5555 (varnumber_T)(iptr->isn_arg.number
5556 + STACK_FRAME_SIZE));
5557 else
5558 smsg("%s%4d LOAD $%lld", pfx, current,
5559 (varnumber_T)(iptr->isn_arg.number));
5560 }
5561 break;
5562 case ISN_LOADOUTER:
5563 {
5564 if (iptr->isn_arg.number < 0)
5565 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5566 iptr->isn_arg.outer.outer_depth,
5567 iptr->isn_arg.outer.outer_idx
5568 + STACK_FRAME_SIZE);
5569 else
5570 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5571 iptr->isn_arg.outer.outer_depth,
5572 iptr->isn_arg.outer.outer_idx);
5573 }
5574 break;
5575 case ISN_LOADV:
5576 smsg("%s%4d LOADV v:%s", pfx, current,
5577 get_vim_var_name(iptr->isn_arg.number));
5578 break;
5579 case ISN_LOADSCRIPT:
5580 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005581 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5582 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5583 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005584
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005585 sv = get_script_svar(sref, -1);
5586 if (sv == NULL)
5587 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5588 pfx, current, si->sn_name);
5589 else
5590 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005591 sv->sv_name,
5592 sref->sref_idx,
5593 si->sn_name);
5594 }
5595 break;
5596 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005597 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005598 {
5599 scriptitem_T *si = SCRIPT_ITEM(
5600 iptr->isn_arg.loadstore.ls_sid);
5601
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005602 smsg("%s%4d %s s:%s from %s", pfx, current,
5603 iptr->isn_type == ISN_LOADS ? "LOADS"
5604 : "LOADEXPORT",
5605 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005606 }
5607 break;
5608 case ISN_LOADAUTO:
5609 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5610 break;
5611 case ISN_LOADG:
5612 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5613 break;
5614 case ISN_LOADB:
5615 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5616 break;
5617 case ISN_LOADW:
5618 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5619 break;
5620 case ISN_LOADT:
5621 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5622 break;
5623 case ISN_LOADGDICT:
5624 smsg("%s%4d LOAD g:", pfx, current);
5625 break;
5626 case ISN_LOADBDICT:
5627 smsg("%s%4d LOAD b:", pfx, current);
5628 break;
5629 case ISN_LOADWDICT:
5630 smsg("%s%4d LOAD w:", pfx, current);
5631 break;
5632 case ISN_LOADTDICT:
5633 smsg("%s%4d LOAD t:", pfx, current);
5634 break;
5635 case ISN_LOADOPT:
5636 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5637 break;
5638 case ISN_LOADENV:
5639 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5640 break;
5641 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005642 smsg("%s%4d LOADREG @%c", pfx, current,
5643 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005644 break;
5645
5646 case ISN_STORE:
5647 if (iptr->isn_arg.number < 0)
5648 smsg("%s%4d STORE arg[%lld]", pfx, current,
5649 iptr->isn_arg.number + STACK_FRAME_SIZE);
5650 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005651 smsg("%s%4d STORE $%lld", pfx, current,
5652 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005653 break;
5654 case ISN_STOREOUTER:
5655 {
5656 if (iptr->isn_arg.number < 0)
5657 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5658 iptr->isn_arg.outer.outer_depth,
5659 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5660 else
5661 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5662 iptr->isn_arg.outer.outer_depth,
5663 iptr->isn_arg.outer.outer_idx);
5664 }
5665 break;
5666 case ISN_STOREV:
5667 smsg("%s%4d STOREV v:%s", pfx, current,
5668 get_vim_var_name(iptr->isn_arg.number));
5669 break;
5670 case ISN_STOREAUTO:
5671 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5672 break;
5673 case ISN_STOREG:
5674 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5675 break;
5676 case ISN_STOREB:
5677 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5678 break;
5679 case ISN_STOREW:
5680 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5681 break;
5682 case ISN_STORET:
5683 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5684 break;
5685 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005686 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005687 {
5688 scriptitem_T *si = SCRIPT_ITEM(
5689 iptr->isn_arg.loadstore.ls_sid);
5690
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005691 smsg("%s%4d %s %s in %s", pfx, current,
5692 iptr->isn_type == ISN_STORES
5693 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005694 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5695 }
5696 break;
5697 case ISN_STORESCRIPT:
5698 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005699 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5700 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5701 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005702
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005703 sv = get_script_svar(sref, -1);
5704 if (sv == NULL)
5705 smsg("%s%4d STORESCRIPT [deleted] in %s",
5706 pfx, current, si->sn_name);
5707 else
5708 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005709 sv->sv_name,
5710 sref->sref_idx,
5711 si->sn_name);
5712 }
5713 break;
5714 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005715 case ISN_STOREFUNCOPT:
5716 smsg("%s%4d %s &%s", pfx, current,
5717 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005718 iptr->isn_arg.storeopt.so_name);
5719 break;
5720 case ISN_STOREENV:
5721 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5722 break;
5723 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005724 smsg("%s%4d STOREREG @%c", pfx, current,
5725 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005726 break;
5727 case ISN_STORENR:
5728 smsg("%s%4d STORE %lld in $%d", pfx, current,
5729 iptr->isn_arg.storenr.stnr_val,
5730 iptr->isn_arg.storenr.stnr_idx);
5731 break;
5732
5733 case ISN_STOREINDEX:
5734 smsg("%s%4d STOREINDEX %s", pfx, current,
5735 vartype_name(iptr->isn_arg.vartype));
5736 break;
5737
5738 case ISN_STORERANGE:
5739 smsg("%s%4d STORERANGE", pfx, current);
5740 break;
5741
5742 // constants
5743 case ISN_PUSHNR:
5744 smsg("%s%4d PUSHNR %lld", pfx, current,
5745 (varnumber_T)(iptr->isn_arg.number));
5746 break;
5747 case ISN_PUSHBOOL:
5748 case ISN_PUSHSPEC:
5749 smsg("%s%4d PUSH %s", pfx, current,
5750 get_var_special_name(iptr->isn_arg.number));
5751 break;
5752 case ISN_PUSHF:
5753#ifdef FEAT_FLOAT
5754 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5755#endif
5756 break;
5757 case ISN_PUSHS:
5758 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5759 break;
5760 case ISN_PUSHBLOB:
5761 {
5762 char_u *r;
5763 char_u numbuf[NUMBUFLEN];
5764 char_u *tofree;
5765
5766 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5767 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5768 vim_free(tofree);
5769 }
5770 break;
5771 case ISN_PUSHFUNC:
5772 {
5773 char *name = (char *)iptr->isn_arg.string;
5774
5775 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5776 name == NULL ? "[none]" : name);
5777 }
5778 break;
5779 case ISN_PUSHCHANNEL:
5780#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005781 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005782#endif
5783 break;
5784 case ISN_PUSHJOB:
5785#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005786 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005787#endif
5788 break;
5789 case ISN_PUSHEXC:
5790 smsg("%s%4d PUSH v:exception", pfx, current);
5791 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005792 case ISN_AUTOLOAD:
5793 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5794 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005795 case ISN_UNLET:
5796 smsg("%s%4d UNLET%s %s", pfx, current,
5797 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5798 iptr->isn_arg.unlet.ul_name);
5799 break;
5800 case ISN_UNLETENV:
5801 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5802 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5803 iptr->isn_arg.unlet.ul_name);
5804 break;
5805 case ISN_UNLETINDEX:
5806 smsg("%s%4d UNLETINDEX", pfx, current);
5807 break;
5808 case ISN_UNLETRANGE:
5809 smsg("%s%4d UNLETRANGE", pfx, current);
5810 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005811 case ISN_LOCKUNLOCK:
5812 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5813 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005814 case ISN_LOCKCONST:
5815 smsg("%s%4d LOCKCONST", pfx, current);
5816 break;
5817 case ISN_NEWLIST:
5818 smsg("%s%4d NEWLIST size %lld", pfx, current,
5819 (varnumber_T)(iptr->isn_arg.number));
5820 break;
5821 case ISN_NEWDICT:
5822 smsg("%s%4d NEWDICT size %lld", pfx, current,
5823 (varnumber_T)(iptr->isn_arg.number));
5824 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005825 case ISN_NEWPARTIAL:
5826 smsg("%s%4d NEWPARTIAL", pfx, current);
5827 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005828
5829 // function call
5830 case ISN_BCALL:
5831 {
5832 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5833
5834 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5835 internal_func_name(cbfunc->cbf_idx),
5836 cbfunc->cbf_argcount);
5837 }
5838 break;
5839 case ISN_DCALL:
5840 {
5841 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5842 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5843 + cdfunc->cdf_idx;
5844
5845 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005846 printable_func_name(df->df_ufunc),
5847 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005848 }
5849 break;
5850 case ISN_UCALL:
5851 {
5852 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5853
5854 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5855 cufunc->cuf_name, cufunc->cuf_argcount);
5856 }
5857 break;
5858 case ISN_PCALL:
5859 {
5860 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5861
5862 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5863 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5864 }
5865 break;
5866 case ISN_PCALL_END:
5867 smsg("%s%4d PCALL end", pfx, current);
5868 break;
5869 case ISN_RETURN:
5870 smsg("%s%4d RETURN", pfx, current);
5871 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005872 case ISN_RETURN_VOID:
5873 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005874 break;
5875 case ISN_FUNCREF:
5876 {
5877 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005878 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005879
Bram Moolenaar38453522021-11-28 22:00:12 +00005880 if (funcref->fr_func_name == NULL)
5881 {
5882 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5883 + funcref->fr_dfunc_idx;
5884 name = df->df_ufunc->uf_name;
5885 }
5886 else
5887 name = funcref->fr_func_name;
5888 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005889 }
5890 break;
5891
5892 case ISN_NEWFUNC:
5893 {
5894 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5895
5896 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5897 newfunc->nf_lambda, newfunc->nf_global);
5898 }
5899 break;
5900
5901 case ISN_DEF:
5902 {
5903 char_u *name = iptr->isn_arg.string;
5904
5905 smsg("%s%4d DEF %s", pfx, current,
5906 name == NULL ? (char_u *)"" : name);
5907 }
5908 break;
5909
5910 case ISN_JUMP:
5911 {
5912 char *when = "?";
5913
5914 switch (iptr->isn_arg.jump.jump_when)
5915 {
5916 case JUMP_ALWAYS:
5917 when = "JUMP";
5918 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005919 case JUMP_NEVER:
5920 iemsg("JUMP_NEVER should not be used");
5921 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005922 case JUMP_AND_KEEP_IF_TRUE:
5923 when = "JUMP_AND_KEEP_IF_TRUE";
5924 break;
5925 case JUMP_IF_FALSE:
5926 when = "JUMP_IF_FALSE";
5927 break;
5928 case JUMP_AND_KEEP_IF_FALSE:
5929 when = "JUMP_AND_KEEP_IF_FALSE";
5930 break;
5931 case JUMP_IF_COND_FALSE:
5932 when = "JUMP_IF_COND_FALSE";
5933 break;
5934 case JUMP_IF_COND_TRUE:
5935 when = "JUMP_IF_COND_TRUE";
5936 break;
5937 }
5938 smsg("%s%4d %s -> %d", pfx, current, when,
5939 iptr->isn_arg.jump.jump_where);
5940 }
5941 break;
5942
5943 case ISN_JUMP_IF_ARG_SET:
5944 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5945 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5946 iptr->isn_arg.jump.jump_where);
5947 break;
5948
5949 case ISN_FOR:
5950 {
5951 forloop_T *forloop = &iptr->isn_arg.forloop;
5952
5953 smsg("%s%4d FOR $%d -> %d", pfx, current,
5954 forloop->for_idx, forloop->for_end);
5955 }
5956 break;
5957
5958 case ISN_TRY:
5959 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005960 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005961
5962 if (try->try_ref->try_finally == 0)
5963 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5964 pfx, current,
5965 try->try_ref->try_catch,
5966 try->try_ref->try_endtry);
5967 else
5968 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5969 pfx, current,
5970 try->try_ref->try_catch,
5971 try->try_ref->try_finally,
5972 try->try_ref->try_endtry);
5973 }
5974 break;
5975 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005976 smsg("%s%4d CATCH", pfx, current);
5977 break;
5978 case ISN_TRYCONT:
5979 {
5980 trycont_T *trycont = &iptr->isn_arg.trycont;
5981
5982 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5983 trycont->tct_levels,
5984 trycont->tct_levels == 1 ? "" : "s",
5985 trycont->tct_where);
5986 }
5987 break;
5988 case ISN_FINALLY:
5989 smsg("%s%4d FINALLY", pfx, current);
5990 break;
5991 case ISN_ENDTRY:
5992 smsg("%s%4d ENDTRY", pfx, current);
5993 break;
5994 case ISN_THROW:
5995 smsg("%s%4d THROW", pfx, current);
5996 break;
5997
5998 // expression operations on number
5999 case ISN_OPNR:
6000 case ISN_OPFLOAT:
6001 case ISN_OPANY:
6002 {
6003 char *what;
6004 char *ins;
6005
6006 switch (iptr->isn_arg.op.op_type)
6007 {
6008 case EXPR_MULT: what = "*"; break;
6009 case EXPR_DIV: what = "/"; break;
6010 case EXPR_REM: what = "%"; break;
6011 case EXPR_SUB: what = "-"; break;
6012 case EXPR_ADD: what = "+"; break;
6013 default: what = "???"; break;
6014 }
6015 switch (iptr->isn_type)
6016 {
6017 case ISN_OPNR: ins = "OPNR"; break;
6018 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6019 case ISN_OPANY: ins = "OPANY"; break;
6020 default: ins = "???"; break;
6021 }
6022 smsg("%s%4d %s %s", pfx, current, ins, what);
6023 }
6024 break;
6025
6026 case ISN_COMPAREBOOL:
6027 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006028 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006029 case ISN_COMPARENR:
6030 case ISN_COMPAREFLOAT:
6031 case ISN_COMPARESTRING:
6032 case ISN_COMPAREBLOB:
6033 case ISN_COMPARELIST:
6034 case ISN_COMPAREDICT:
6035 case ISN_COMPAREFUNC:
6036 case ISN_COMPAREANY:
6037 {
6038 char *p;
6039 char buf[10];
6040 char *type;
6041
6042 switch (iptr->isn_arg.op.op_type)
6043 {
6044 case EXPR_EQUAL: p = "=="; break;
6045 case EXPR_NEQUAL: p = "!="; break;
6046 case EXPR_GREATER: p = ">"; break;
6047 case EXPR_GEQUAL: p = ">="; break;
6048 case EXPR_SMALLER: p = "<"; break;
6049 case EXPR_SEQUAL: p = "<="; break;
6050 case EXPR_MATCH: p = "=~"; break;
6051 case EXPR_IS: p = "is"; break;
6052 case EXPR_ISNOT: p = "isnot"; break;
6053 case EXPR_NOMATCH: p = "!~"; break;
6054 default: p = "???"; break;
6055 }
6056 STRCPY(buf, p);
6057 if (iptr->isn_arg.op.op_ic == TRUE)
6058 strcat(buf, "?");
6059 switch(iptr->isn_type)
6060 {
6061 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6062 case ISN_COMPARESPECIAL:
6063 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006064 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006065 case ISN_COMPARENR: type = "COMPARENR"; break;
6066 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6067 case ISN_COMPARESTRING:
6068 type = "COMPARESTRING"; break;
6069 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6070 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6071 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6072 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6073 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6074 default: type = "???"; break;
6075 }
6076
6077 smsg("%s%4d %s %s", pfx, current, type, buf);
6078 }
6079 break;
6080
6081 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6082 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6083
6084 // expression operations
6085 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
6086 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6087 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6088 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6089 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6090 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6091 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6092 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6093 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6094 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6095 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6096 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006097 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006098 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6099 iptr->isn_arg.getitem.gi_index,
6100 iptr->isn_arg.getitem.gi_with_op ?
6101 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006102 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6103 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6104 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006105 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6106 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6107
Bram Moolenaar4c137212021-04-19 16:48:48 +02006108 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6109
6110 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6111 case ISN_CHECKTYPE:
6112 {
6113 checktype_T *ct = &iptr->isn_arg.type;
6114 char *tofree;
6115
6116 if (ct->ct_arg_idx == 0)
6117 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6118 type_name(ct->ct_type, &tofree),
6119 (int)ct->ct_off);
6120 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006121 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6122 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006123 type_name(ct->ct_type, &tofree),
6124 (int)ct->ct_off,
6125 (int)ct->ct_arg_idx);
6126 vim_free(tofree);
6127 break;
6128 }
6129 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6130 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6131 iptr->isn_arg.checklen.cl_min_len);
6132 break;
6133 case ISN_SETTYPE:
6134 {
6135 char *tofree;
6136
6137 smsg("%s%4d SETTYPE %s", pfx, current,
6138 type_name(iptr->isn_arg.type.ct_type, &tofree));
6139 vim_free(tofree);
6140 break;
6141 }
6142 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006143 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6144 smsg("%s%4d INVERT %d (!val)", pfx, current,
6145 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006146 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006147 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6148 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006149 break;
6150 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006151 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006152 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006153 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6154 pfx, current,
6155 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006156 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006157 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6158 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006159 break;
6160 case ISN_PUT:
6161 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6162 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006163 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006164 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6165 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006166 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006167 else
6168 smsg("%s%4d PUT %c %ld", pfx, current,
6169 iptr->isn_arg.put.put_regname,
6170 (long)iptr->isn_arg.put.put_lnum);
6171 break;
6172
Bram Moolenaar4c137212021-04-19 16:48:48 +02006173 case ISN_CMDMOD:
6174 {
6175 char_u *buf;
6176 size_t len = produce_cmdmods(
6177 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6178
6179 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006180 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006181 {
6182 (void)produce_cmdmods(
6183 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6184 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6185 vim_free(buf);
6186 }
6187 break;
6188 }
6189 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6190
6191 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006192 smsg("%s%4d PROFILE START line %d", pfx, current,
6193 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006194 break;
6195
6196 case ISN_PROF_END:
6197 smsg("%s%4d PROFILE END", pfx, current);
6198 break;
6199
Bram Moolenaare99d4222021-06-13 14:01:26 +02006200 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006201 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6202 iptr->isn_arg.debug.dbg_break_lnum + 1,
6203 iptr->isn_lnum,
6204 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006205 break;
6206
Bram Moolenaar4c137212021-04-19 16:48:48 +02006207 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6208 iptr->isn_arg.unpack.unp_count,
6209 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6210 break;
6211 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6212 iptr->isn_arg.shuffle.shfl_item,
6213 iptr->isn_arg.shuffle.shfl_up);
6214 break;
6215 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6216
6217 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6218 return;
6219 }
6220
6221 out_flush(); // output one line at a time
6222 ui_breakcheck();
6223 if (got_int)
6224 break;
6225 }
6226}
6227
6228/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006229 * Handle command line completion for the :disassemble command.
6230 */
6231 void
6232set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6233{
6234 char_u *p;
6235
6236 // Default: expand user functions, "debug" and "profile"
6237 xp->xp_context = EXPAND_DISASSEMBLE;
6238 xp->xp_pattern = arg;
6239
6240 // first argument already typed: only user function names
6241 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6242 {
6243 xp->xp_context = EXPAND_USER_FUNC;
6244 xp->xp_pattern = skipwhite(p);
6245 }
6246}
6247
6248/*
6249 * Function given to ExpandGeneric() to obtain the list of :disassemble
6250 * arguments.
6251 */
6252 char_u *
6253get_disassemble_argument(expand_T *xp, int idx)
6254{
6255 if (idx == 0)
6256 return (char_u *)"debug";
6257 if (idx == 1)
6258 return (char_u *)"profile";
6259 return get_user_func_name(xp, idx - 2);
6260}
6261
6262/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006263 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006264 * We don't really need this at runtime, but we do have tests that require it,
6265 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006266 */
6267 void
6268ex_disassemble(exarg_T *eap)
6269{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006270 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006271 char_u *fname;
6272 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006273 dfunc_T *dfunc;
6274 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006275 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006276 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006277 compiletype_T compile_type = CT_NONE;
6278
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006279 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006280 {
6281 compile_type = CT_PROFILE;
6282 arg = skipwhite(arg + 7);
6283 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006284 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006285 {
6286 compile_type = CT_DEBUG;
6287 arg = skipwhite(arg + 5);
6288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006290 if (STRNCMP(arg, "<lambda>", 8) == 0)
6291 {
6292 arg += 8;
6293 (void)getdigits(&arg);
6294 fname = vim_strnsave(eap->arg, arg - eap->arg);
6295 }
6296 else
6297 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006298 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006299 if (fname == NULL)
6300 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006301 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006302 return;
6303 }
6304
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006305 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006306 if (ufunc == NULL)
6307 {
6308 char_u *p = untrans_function_name(fname);
6309
6310 if (p != NULL)
6311 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006312 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006313 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006314 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 if (ufunc == NULL)
6316 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006317 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 return;
6319 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006320 if (func_needs_compiling(ufunc, compile_type)
6321 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006322 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006323 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006325 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326 return;
6327 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006328 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329
6330 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006331 switch (compile_type)
6332 {
6333 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006334#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006335 instr = dfunc->df_instr_prof;
6336 instr_count = dfunc->df_instr_prof_count;
6337 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006338#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006339 // FALLTHROUGH
6340 case CT_NONE:
6341 instr = dfunc->df_instr;
6342 instr_count = dfunc->df_instr_count;
6343 break;
6344 case CT_DEBUG:
6345 instr = dfunc->df_instr_debug;
6346 instr_count = dfunc->df_instr_debug_count;
6347 break;
6348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006349
Bram Moolenaar4c137212021-04-19 16:48:48 +02006350 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006351}
6352
6353/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006354 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355 * list, etc. Mostly like what JavaScript does, except that empty list and
6356 * empty dictionary are FALSE.
6357 */
6358 int
6359tv2bool(typval_T *tv)
6360{
6361 switch (tv->v_type)
6362 {
6363 case VAR_NUMBER:
6364 return tv->vval.v_number != 0;
6365 case VAR_FLOAT:
6366#ifdef FEAT_FLOAT
6367 return tv->vval.v_float != 0.0;
6368#else
6369 break;
6370#endif
6371 case VAR_PARTIAL:
6372 return tv->vval.v_partial != NULL;
6373 case VAR_FUNC:
6374 case VAR_STRING:
6375 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6376 case VAR_LIST:
6377 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6378 case VAR_DICT:
6379 return tv->vval.v_dict != NULL
6380 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6381 case VAR_BOOL:
6382 case VAR_SPECIAL:
6383 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6384 case VAR_JOB:
6385#ifdef FEAT_JOB_CHANNEL
6386 return tv->vval.v_job != NULL;
6387#else
6388 break;
6389#endif
6390 case VAR_CHANNEL:
6391#ifdef FEAT_JOB_CHANNEL
6392 return tv->vval.v_channel != NULL;
6393#else
6394 break;
6395#endif
6396 case VAR_BLOB:
6397 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6398 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006399 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006401 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006402 break;
6403 }
6404 return FALSE;
6405}
6406
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006407 void
6408emsg_using_string_as(typval_T *tv, int as_number)
6409{
6410 semsg(_(as_number ? e_using_string_as_number_str
6411 : e_using_string_as_bool_str),
6412 tv->vval.v_string == NULL
6413 ? (char_u *)"" : tv->vval.v_string);
6414}
6415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006416/*
6417 * If "tv" is a string give an error and return FAIL.
6418 */
6419 int
6420check_not_string(typval_T *tv)
6421{
6422 if (tv->v_type == VAR_STRING)
6423 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006424 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006425 clear_tv(tv);
6426 return FAIL;
6427 }
6428 return OK;
6429}
6430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431
6432#endif // FEAT_EVAL