blob: afa0dcf198263f7090a1e28630e4d22f9c4dd7aa [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;
LemonBoy77142312022-04-15 20:50:46 +01002659
Bram Moolenaar89445512022-04-14 12:58:23 +01002660 SOURCING_LNUM = iptr->isn_lnum;
2661 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002662 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002663 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002664 }
2665 break;
2666
Bram Moolenaar4c137212021-04-19 16:48:48 +02002667 // execute :substitute with an expression
2668 case ISN_SUBSTITUTE:
2669 {
2670 subs_T *subs = &iptr->isn_arg.subs;
2671 source_cookie_T cookie;
2672 struct subs_expr_S *save_instr = substitute_instr;
2673 struct subs_expr_S subs_instr;
2674 int res;
2675
2676 subs_instr.subs_ectx = ectx;
2677 subs_instr.subs_instr = subs->subs_instr;
2678 subs_instr.subs_status = OK;
2679 substitute_instr = &subs_instr;
2680
2681 SOURCING_LNUM = iptr->isn_lnum;
2682 // This is very much like ISN_EXEC
2683 CLEAR_FIELD(cookie);
2684 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2685 res = do_cmdline(subs->subs_cmd,
2686 getsourceline, &cookie,
2687 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2688 substitute_instr = save_instr;
2689
2690 if (res == FAIL || did_emsg
2691 || subs_instr.subs_status == FAIL)
2692 goto on_error;
2693 }
2694 break;
2695
2696 case ISN_FINISH:
2697 goto done;
2698
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002699 case ISN_REDIRSTART:
2700 // create a dummy entry for var_redir_str()
2701 if (alloc_redir_lval() == FAIL)
2702 goto on_error;
2703
2704 // The output is stored in growarray "redir_ga" until
2705 // redirection ends.
2706 init_redir_ga();
2707 redir_vname = 1;
2708 break;
2709
2710 case ISN_REDIREND:
2711 {
2712 char_u *res = get_clear_redir_ga();
2713
2714 // End redirection, put redirected text on the stack.
2715 clear_redir_lval();
2716 redir_vname = 0;
2717
Bram Moolenaar35578162021-08-02 19:10:38 +02002718 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002719 {
2720 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002721 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002722 }
2723 tv = STACK_TV_BOT(0);
2724 tv->v_type = VAR_STRING;
2725 tv->vval.v_string = res;
2726 ++ectx->ec_stack.ga_len;
2727 }
2728 break;
2729
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002730 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002731#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002732 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002733 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2734 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002735 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002736#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002737 break;
2738
2739 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002740#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002741 {
2742 exarg_T ea;
2743 int res;
2744
2745 CLEAR_FIELD(ea);
2746 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2747 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2748 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2749 --ectx->ec_stack.ga_len;
2750 tv = STACK_TV_BOT(0);
2751 res = cexpr_core(&ea, tv);
2752 clear_tv(tv);
2753 if (res == FAIL)
2754 goto on_error;
2755 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002756#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002757 break;
2758
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002759 // execute Ex command from pieces on the stack
2760 case ISN_EXECCONCAT:
2761 {
2762 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002763 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002764 int pass;
2765 int i;
2766 char_u *cmd = NULL;
2767 char_u *str;
2768
2769 for (pass = 1; pass <= 2; ++pass)
2770 {
2771 for (i = 0; i < count; ++i)
2772 {
2773 tv = STACK_TV_BOT(i - count);
2774 str = tv->vval.v_string;
2775 if (str != NULL && *str != NUL)
2776 {
2777 if (pass == 2)
2778 STRCPY(cmd + len, str);
2779 len += STRLEN(str);
2780 }
2781 if (pass == 2)
2782 clear_tv(tv);
2783 }
2784 if (pass == 1)
2785 {
2786 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002787 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002788 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002789 len = 0;
2790 }
2791 }
2792
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002793 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002794 do_cmdline_cmd(cmd);
2795 vim_free(cmd);
2796 }
2797 break;
2798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 // execute :echo {string} ...
2800 case ISN_ECHO:
2801 {
2802 int count = iptr->isn_arg.echo.echo_count;
2803 int atstart = TRUE;
2804 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002805 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806
2807 for (idx = 0; idx < count; ++idx)
2808 {
2809 tv = STACK_TV_BOT(idx - count);
2810 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2811 &atstart, &needclr);
2812 clear_tv(tv);
2813 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002814 if (needclr)
2815 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002816 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 }
2818 break;
2819
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002820 // :execute {string} ...
2821 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002822 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002823 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002824 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002825 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002826 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002827 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002828 {
2829 int count = iptr->isn_arg.number;
2830 garray_T ga;
2831 char_u buf[NUMBUFLEN];
2832 char_u *p;
2833 int len;
2834 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002835 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002836
2837 ga_init2(&ga, 1, 80);
2838 for (idx = 0; idx < count; ++idx)
2839 {
2840 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002841 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002842 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002843 if (tv->v_type == VAR_CHANNEL
2844 || tv->v_type == VAR_JOB)
2845 {
2846 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002847 semsg(_(e_using_invalid_value_as_string_str),
2848 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002849 break;
2850 }
2851 else
2852 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002853 }
2854 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002855 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002856
2857 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002858 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002859 failed = TRUE;
2860 else
2861 {
2862 if (ga.ga_len > 0)
2863 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2864 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2865 ga.ga_len += len;
2866 }
2867 clear_tv(tv);
2868 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002869 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002870 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002871 {
2872 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002873 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002874 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002875
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002876 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002877 {
2878 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002879 {
2880 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002881 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002882 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002883 {
2884 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002885 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002886 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002887 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002888 else
2889 {
2890 msg_sb_eol();
2891 if (iptr->isn_type == ISN_ECHOMSG)
2892 {
2893 msg_attr(ga.ga_data, echo_attr);
2894 out_flush();
2895 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002896 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2897 {
2898 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2899 TRUE);
2900 ui_write((char_u *)"\r\n", 2, TRUE);
2901 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002902 else
2903 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002904 SOURCING_LNUM = iptr->isn_lnum;
2905 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002906 }
2907 }
2908 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002909 ga_clear(&ga);
2910 }
2911 break;
2912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 // load local variable or argument
2914 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002915 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002916 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002918 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 break;
2920
2921 // load v: variable
2922 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002923 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002924 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002926 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 break;
2928
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002929 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 case ISN_LOADSCRIPT:
2931 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002932 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 svar_T *sv;
2934
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002935 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002936 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002937 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002938 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002939 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002940 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002942 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 }
2944 break;
2945
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002946 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002948 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002950 int sid = iptr->isn_arg.loadstore.ls_sid;
2951 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002952 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 if (di == NULL)
2956 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002957 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002958 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002959 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 }
2961 else
2962 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002963 if (iptr->isn_type == ISN_LOADEXPORT)
2964 {
2965 int idx = get_script_item_idx(sid, name, 0,
2966 NULL, NULL);
2967 svar_T *sv;
2968
2969 if (idx >= 0)
2970 {
2971 sv = ((svar_T *)SCRIPT_ITEM(sid)
2972 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002973 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002974 {
2975 SOURCING_LNUM = iptr->isn_lnum;
2976 semsg(_(e_item_not_exported_in_script_str),
2977 name);
2978 goto on_error;
2979 }
2980 }
2981 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002982 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002983 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002985 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 }
2987 }
2988 break;
2989
Bram Moolenaard3aac292020-04-19 14:32:17 +02002990 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002992 case ISN_LOADB:
2993 case ISN_LOADW:
2994 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002996 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002997
Bram Moolenaar06b77222022-01-25 15:51:56 +00002998 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002999 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003000 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003001 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 break;
3005
Bram Moolenaar03290b82020-12-19 16:30:44 +01003006 // load autoload variable
3007 case ISN_LOADAUTO:
3008 {
3009 char_u *name = iptr->isn_arg.string;
3010
Bram Moolenaar35578162021-08-02 19:10:38 +02003011 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003012 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003013 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003014 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003015 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003016 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003017 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003018 }
3019 break;
3020
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003021 // load g:/b:/w:/t: namespace
3022 case ISN_LOADGDICT:
3023 case ISN_LOADBDICT:
3024 case ISN_LOADWDICT:
3025 case ISN_LOADTDICT:
3026 {
3027 dict_T *d = NULL;
3028
3029 switch (iptr->isn_type)
3030 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003031 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3032 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3033 case ISN_LOADWDICT: d = curwin->w_vars; break;
3034 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003035 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003036 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003037 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003038 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003039 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003040 tv = STACK_TV_BOT(0);
3041 tv->v_type = VAR_DICT;
3042 tv->v_lock = 0;
3043 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003044 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003045 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003046 }
3047 break;
3048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 // load &option
3050 case ISN_LOADOPT:
3051 {
3052 typval_T optval;
3053 char_u *name = iptr->isn_arg.string;
3054
Bram Moolenaara8c17702020-04-01 21:17:24 +02003055 // This is not expected to fail, name is checked during
3056 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003057 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003058 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003059 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003060 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003062 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 }
3064 break;
3065
3066 // load $ENV
3067 case ISN_LOADENV:
3068 {
3069 typval_T optval;
3070 char_u *name = iptr->isn_arg.string;
3071
Bram Moolenaar35578162021-08-02 19:10:38 +02003072 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003073 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003074 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003075 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003077 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 }
3079 break;
3080
3081 // load @register
3082 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003083 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003084 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 tv = STACK_TV_BOT(0);
3086 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003087 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003088 // This may result in NULL, which should be equivalent to an
3089 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 tv->vval.v_string = get_reg_contents(
3091 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003092 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 break;
3094
3095 // store local variable
3096 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003097 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 tv = STACK_TV_VAR(iptr->isn_arg.number);
3099 clear_tv(tv);
3100 *tv = *STACK_TV_BOT(0);
3101 break;
3102
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003103 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003104 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003105 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003106 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003107 int sid = iptr->isn_arg.loadstore.ls_sid;
3108 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003109 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003110 dictitem_T *di = find_var_in_ht(ht, 0,
3111 iptr->isn_type == ISN_STORES
3112 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003113
Bram Moolenaar4c137212021-04-19 16:48:48 +02003114 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003115 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003116 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003117 {
3118 if (iptr->isn_type == ISN_STOREEXPORT)
3119 {
3120 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003121 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003122 goto on_error;
3123 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003124 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003125 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003126 else
3127 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003128 if (iptr->isn_type == ISN_STOREEXPORT)
3129 {
3130 int idx = get_script_item_idx(sid, name, 0,
3131 NULL, NULL);
3132
3133 if (idx >= 0)
3134 {
3135 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3136 ->sn_var_vals.ga_data) + idx;
3137
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003138 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003139 {
3140 semsg(_(e_item_not_exported_in_script_str),
3141 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003142 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003143 goto on_error;
3144 }
3145 }
3146 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003147 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003148 {
3149 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003150 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003151 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003152 clear_tv(&di->di_tv);
3153 di->di_tv = *STACK_TV_BOT(0);
3154 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003155 }
3156 break;
3157
3158 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 case ISN_STORESCRIPT:
3160 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003161 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3162 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003164 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003165 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003166 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003167 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003168
3169 // "const" and "final" are checked at compile time, locking
3170 // the value needs to be checked here.
3171 SOURCING_LNUM = iptr->isn_lnum;
3172 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003173 {
3174 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003175 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003176 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 clear_tv(sv->sv_tv);
3179 *sv->sv_tv = *STACK_TV_BOT(0);
3180 }
3181 break;
3182
3183 // store option
3184 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003185 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003187 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3188 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 long n = 0;
3190 char_u *s = NULL;
3191 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003192 char_u numbuf[NUMBUFLEN];
3193 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194
Bram Moolenaar4c137212021-04-19 16:48:48 +02003195 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 tv = STACK_TV_BOT(0);
3197 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003198 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003200 if (s == NULL)
3201 s = (char_u *)"";
3202 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003203 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3204 {
3205 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003206 // If the option can be set to a function reference or
3207 // a lambda and the passed value is a function
3208 // reference, then convert it to the name (string) of
3209 // the function reference.
3210 s = tv2string(tv, &tofree, numbuf, 0);
3211 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003212 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003213 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003214 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003215 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003216 goto on_error;
3217 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003220 // must be VAR_NUMBER, CHECKTYPE makes sure
3221 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003222 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003223 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003224 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 if (msg != NULL)
3226 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003227 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003229 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 }
3232 break;
3233
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003234 // store $ENV
3235 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003236 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003237 tv = STACK_TV_BOT(0);
3238 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3239 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003240 break;
3241
3242 // store @r
3243 case ISN_STOREREG:
3244 {
3245 int reg = iptr->isn_arg.number;
3246
Bram Moolenaar4c137212021-04-19 16:48:48 +02003247 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003248 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003249 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003250 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003251 }
3252 break;
3253
3254 // store v: variable
3255 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003256 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003257 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3258 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003259 // should not happen, type is checked when compiling
3260 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003261 break;
3262
Bram Moolenaard3aac292020-04-19 14:32:17 +02003263 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003265 case ISN_STOREB:
3266 case ISN_STOREW:
3267 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003269 dictitem_T *di;
3270 hashtab_T *ht;
3271 char_u *name = iptr->isn_arg.string + 2;
3272
Bram Moolenaard3aac292020-04-19 14:32:17 +02003273 switch (iptr->isn_type)
3274 {
3275 case ISN_STOREG:
3276 ht = get_globvar_ht();
3277 break;
3278 case ISN_STOREB:
3279 ht = &curbuf->b_vars->dv_hashtab;
3280 break;
3281 case ISN_STOREW:
3282 ht = &curwin->w_vars->dv_hashtab;
3283 break;
3284 case ISN_STORET:
3285 ht = &curtab->tp_vars->dv_hashtab;
3286 break;
3287 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003288 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290
Bram Moolenaar4c137212021-04-19 16:48:48 +02003291 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003292 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003294 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 else
3296 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003297 SOURCING_LNUM = iptr->isn_lnum;
3298 if (var_check_permission(di, name) == FAIL)
3299 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 clear_tv(&di->di_tv);
3301 di->di_tv = *STACK_TV_BOT(0);
3302 }
3303 }
3304 break;
3305
Bram Moolenaar03290b82020-12-19 16:30:44 +01003306 // store an autoload variable
3307 case ISN_STOREAUTO:
3308 SOURCING_LNUM = iptr->isn_lnum;
3309 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3310 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003311 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003312 break;
3313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 // store number in local variable
3315 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003316 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 clear_tv(tv);
3318 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003319 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 break;
3321
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003322 // store value in list or dict variable
3323 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003324 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003325 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003326
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003327 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003328 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003329 if (res == NOTDONE)
3330 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003331 }
3332 break;
3333
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003334 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003335 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003336 if (execute_storerange(iptr, ectx) == FAIL)
3337 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003338 break;
3339
Bram Moolenaar0186e582021-01-10 18:33:11 +01003340 // load or store variable or argument from outer scope
3341 case ISN_LOADOUTER:
3342 case ISN_STOREOUTER:
3343 {
3344 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003345 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3346 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003347
3348 while (depth > 1 && outer != NULL)
3349 {
3350 outer = outer->out_up;
3351 --depth;
3352 }
3353 if (outer == NULL)
3354 {
3355 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003356 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3357 || ectx->ec_outer_ref == NULL)
3358 // Possibly :def function called from legacy
3359 // context.
3360 emsg(_(e_closure_called_from_invalid_context));
3361 else
3362 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003363 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003364 }
3365 tv = ((typval_T *)outer->out_stack->ga_data)
3366 + outer->out_frame_idx + STACK_FRAME_SIZE
3367 + iptr->isn_arg.outer.outer_idx;
3368 if (iptr->isn_type == ISN_LOADOUTER)
3369 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003370 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003371 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003372 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003373 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003374 }
3375 else
3376 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003377 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003378 clear_tv(tv);
3379 *tv = *STACK_TV_BOT(0);
3380 }
3381 }
3382 break;
3383
Bram Moolenaar752fc692021-01-04 21:57:11 +01003384 // unlet item in list or dict variable
3385 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003386 if (execute_unletindex(iptr, ectx) == FAIL)
3387 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003388 break;
3389
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003390 // unlet range of items in list variable
3391 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003392 if (execute_unletrange(iptr, ectx) == FAIL)
3393 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003394 break;
3395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 // push constant
3397 case ISN_PUSHNR:
3398 case ISN_PUSHBOOL:
3399 case ISN_PUSHSPEC:
3400 case ISN_PUSHF:
3401 case ISN_PUSHS:
3402 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003403 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003404 case ISN_PUSHCHANNEL:
3405 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003406 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003407 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003409 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003410 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 switch (iptr->isn_type)
3412 {
3413 case ISN_PUSHNR:
3414 tv->v_type = VAR_NUMBER;
3415 tv->vval.v_number = iptr->isn_arg.number;
3416 break;
3417 case ISN_PUSHBOOL:
3418 tv->v_type = VAR_BOOL;
3419 tv->vval.v_number = iptr->isn_arg.number;
3420 break;
3421 case ISN_PUSHSPEC:
3422 tv->v_type = VAR_SPECIAL;
3423 tv->vval.v_number = iptr->isn_arg.number;
3424 break;
3425#ifdef FEAT_FLOAT
3426 case ISN_PUSHF:
3427 tv->v_type = VAR_FLOAT;
3428 tv->vval.v_float = iptr->isn_arg.fnumber;
3429 break;
3430#endif
3431 case ISN_PUSHBLOB:
3432 blob_copy(iptr->isn_arg.blob, tv);
3433 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003434 case ISN_PUSHFUNC:
3435 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003436 if (iptr->isn_arg.string == NULL)
3437 tv->vval.v_string = NULL;
3438 else
3439 tv->vval.v_string =
3440 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003441 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003442 case ISN_PUSHCHANNEL:
3443#ifdef FEAT_JOB_CHANNEL
3444 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003445 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003446#endif
3447 break;
3448 case ISN_PUSHJOB:
3449#ifdef FEAT_JOB_CHANNEL
3450 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003451 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003452#endif
3453 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 default:
3455 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003456 tv->vval.v_string = iptr->isn_arg.string == NULL
3457 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 }
3459 break;
3460
Bram Moolenaar06b77222022-01-25 15:51:56 +00003461 case ISN_AUTOLOAD:
3462 {
3463 char_u *name = iptr->isn_arg.string;
3464
3465 (void)script_autoload(name, FALSE);
3466 if (find_func(name, TRUE))
3467 {
3468 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3469 goto theend;
3470 tv = STACK_TV_BOT(0);
3471 tv->v_lock = 0;
3472 ++ectx->ec_stack.ga_len;
3473 tv->v_type = VAR_FUNC;
3474 tv->vval.v_string = vim_strsave(name);
3475 }
3476 else
3477 {
3478 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3479
3480 if (res == NOTDONE)
3481 goto theend;
3482 if (res == FAIL)
3483 goto on_error;
3484 }
3485 }
3486 break;
3487
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003488 case ISN_UNLET:
3489 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3490 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003491 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003492 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003493 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003494 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003495 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003496
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003497 case ISN_LOCKUNLOCK:
3498 {
3499 typval_T *lval_root_save = lval_root;
3500 int res;
3501
3502 // Stack has the local variable, argument the whole :lock
3503 // or :unlock command, like ISN_EXEC.
3504 --ectx->ec_stack.ga_len;
3505 lval_root = STACK_TV_BOT(0);
3506 res = exec_command(iptr);
3507 clear_tv(lval_root);
3508 lval_root = lval_root_save;
3509 if (res == FAIL)
3510 goto on_error;
3511 }
3512 break;
3513
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003514 case ISN_LOCKCONST:
3515 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3516 break;
3517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 // create a list from items on the stack; uses a single allocation
3519 // for the list header and the items
3520 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003521 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003522 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 break;
3524
3525 // create a dict from items on the stack
3526 case ISN_NEWDICT:
3527 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003528 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003530 SOURCING_LNUM = iptr->isn_lnum;
3531 res = exe_newdict(iptr->isn_arg.number, ectx);
3532 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003533 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003534 if (res == MAYBE)
3535 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 }
3537 break;
3538
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003539 // create a partial with NULL value
3540 case ISN_NEWPARTIAL:
3541 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3542 goto theend;
3543 ++ectx->ec_stack.ga_len;
3544 tv = STACK_TV_BOT(-1);
3545 tv->v_type = VAR_PARTIAL;
3546 tv->v_lock = 0;
3547 tv->vval.v_partial = NULL;
3548 break;
3549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 // call a :def function
3551 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003552 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003553 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3554 NULL,
3555 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003556 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003557 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 break;
3559
3560 // call a builtin function
3561 case ISN_BCALL:
3562 SOURCING_LNUM = iptr->isn_lnum;
3563 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3564 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003565 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003566 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 break;
3568
3569 // call a funcref or partial
3570 case ISN_PCALL:
3571 {
3572 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3573 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003574 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575
3576 SOURCING_LNUM = iptr->isn_lnum;
3577 if (pfunc->cpf_top)
3578 {
3579 // funcref is above the arguments
3580 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3581 }
3582 else
3583 {
3584 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003585 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003586 partial_tv = *STACK_TV_BOT(0);
3587 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003589 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003590 if (tv == &partial_tv)
3591 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003593 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 }
3595 break;
3596
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003597 case ISN_PCALL_END:
3598 // PCALL finished, arguments have been consumed and replaced by
3599 // the return value. Now clear the funcref from the stack,
3600 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003601 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003602 clear_tv(STACK_TV_BOT(-1));
3603 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3604 break;
3605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 // call a user defined function or funcref/partial
3607 case ISN_UCALL:
3608 {
3609 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3610
3611 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003612 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003613 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003614 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 }
3616 break;
3617
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003618 // return from a :def function call without a value
3619 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003620 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003621 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003622 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003623 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003624 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003625 tv->vval.v_number = 0;
3626 tv->v_lock = 0;
3627 // FALLTHROUGH
3628
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003629 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 case ISN_RETURN:
3631 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003632 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003633 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003634
3635 if (trystack->ga_len > 0)
3636 trycmd = ((trycmd_T *)trystack->ga_data)
3637 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003638 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003639 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003641 // jump to ":finally" or ":endtry"
3642 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003643 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003644 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003645 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 trycmd->tcd_return = TRUE;
3647 }
3648 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003649 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 }
3651 break;
3652
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003653 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 case ISN_FUNCREF:
3655 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003656 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003657 ufunc_T *ufunc;
3658 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003661 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003662 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003663 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003664 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003665 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003666 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003667 if (funcref->fr_func_name == NULL)
3668 {
3669 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3670 + funcref->fr_dfunc_idx;
3671
3672 ufunc = pt_dfunc->df_ufunc;
3673 }
3674 else
3675 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003676 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003677 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003678 if (ufunc == NULL)
3679 {
3680 SOURCING_LNUM = iptr->isn_lnum;
3681 iemsg("ufunc unexpectedly NULL for FUNCREF");
3682 goto theend;
3683 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003684 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003685 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003687 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 tv->vval.v_partial = pt;
3689 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003690 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 }
3692 break;
3693
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003694 // Create a global function from a lambda.
3695 case ISN_NEWFUNC:
3696 {
3697 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3698
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003699 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003700 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003701 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003702 }
3703 break;
3704
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003705 // List functions
3706 case ISN_DEF:
3707 if (iptr->isn_arg.string == NULL)
3708 list_functions(NULL);
3709 else
3710 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003711 exarg_T ea;
3712 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003713
3714 CLEAR_FIELD(ea);
3715 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003716 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3717 define_function(&ea, NULL, &lines_to_free);
3718 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003719 }
3720 break;
3721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 // jump if a condition is met
3723 case ISN_JUMP:
3724 {
3725 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003726 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 int jump = TRUE;
3728
3729 if (when != JUMP_ALWAYS)
3730 {
3731 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003732 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003733 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003734 || when == JUMP_IF_COND_TRUE)
3735 {
3736 SOURCING_LNUM = iptr->isn_lnum;
3737 jump = tv_get_bool_chk(tv, &error);
3738 if (error)
3739 goto on_error;
3740 }
3741 else
3742 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003744 || when == JUMP_AND_KEEP_IF_FALSE
3745 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003747 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 {
3749 // drop the value from the stack
3750 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003751 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 }
3753 }
3754 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003755 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 }
3757 break;
3758
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003759 // Jump if an argument with a default value was already set and not
3760 // v:none.
3761 case ISN_JUMP_IF_ARG_SET:
3762 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3763 if (tv->v_type != VAR_UNKNOWN
3764 && !(tv->v_type == VAR_SPECIAL
3765 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003766 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003767 break;
3768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 // top of a for loop
3770 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003771 if (execute_for(iptr, ectx) == FAIL)
3772 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 break;
3774
3775 // start of ":try" block
3776 case ISN_TRY:
3777 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003778 trycmd_T *trycmd = NULL;
3779
Bram Moolenaar35578162021-08-02 19:10:38 +02003780 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003781 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003782 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3783 + ectx->ec_trystack.ga_len;
3784 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003786 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003787 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3788 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003789 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003790 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003791 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003792 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003793 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003794 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 }
3796 break;
3797
3798 case ISN_PUSHEXC:
3799 if (current_exception == NULL)
3800 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003801 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003803 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003805 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003806 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003808 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003810 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 tv->vval.v_string = vim_strsave(
3812 (char_u *)current_exception->value);
3813 break;
3814
3815 case ISN_CATCH:
3816 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003817 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818
Bram Moolenaar4c137212021-04-19 16:48:48 +02003819 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 if (trystack->ga_len > 0)
3821 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003822 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 + trystack->ga_len - 1;
3824 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003825 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 }
3827 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003828 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 catch_exception(current_exception);
3830 }
3831 break;
3832
Bram Moolenaarc150c092021-02-13 15:02:46 +01003833 case ISN_TRYCONT:
3834 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003835 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003836 trycont_T *trycont = &iptr->isn_arg.trycont;
3837 int i;
3838 trycmd_T *trycmd;
3839 int iidx = trycont->tct_where;
3840
3841 if (trystack->ga_len < trycont->tct_levels)
3842 {
3843 siemsg("TRYCONT: expected %d levels, found %d",
3844 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003845 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003846 }
3847 // Make :endtry jump to any outer try block and the last
3848 // :endtry inside the loop to the loop start.
3849 for (i = trycont->tct_levels; i > 0; --i)
3850 {
3851 trycmd = ((trycmd_T *)trystack->ga_data)
3852 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003853 // Add one to tcd_cont to be able to jump to
3854 // instruction with index zero.
3855 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003856 iidx = trycmd->tcd_finally_idx == 0
3857 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003858 }
3859 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003860 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003861 }
3862 break;
3863
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003864 case ISN_FINALLY:
3865 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003866 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003867 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3868 + trystack->ga_len - 1;
3869
3870 // Reset the index to avoid a return statement jumps here
3871 // again.
3872 trycmd->tcd_finally_idx = 0;
3873 break;
3874 }
3875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 // end of ":try" block
3877 case ISN_ENDTRY:
3878 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003879 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880
3881 if (trystack->ga_len > 0)
3882 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003883 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 --trystack->ga_len;
3886 --trylevel;
3887 trycmd = ((trycmd_T *)trystack->ga_data)
3888 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003889 if (trycmd->tcd_did_throw)
3890 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003891 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 {
3893 // discard the exception
3894 if (caught_stack == current_exception)
3895 caught_stack = caught_stack->caught;
3896 discard_current_exception();
3897 }
3898
3899 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003900 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003901
Bram Moolenaar4c137212021-04-19 16:48:48 +02003902 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003903 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003904 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003905 clear_tv(STACK_TV_BOT(0));
3906 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003907 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003908 // handling :continue: jump to outer try block or
3909 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003910 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 }
3912 }
3913 break;
3914
3915 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003916 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003917 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003918
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003919 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3920 {
3921 // throwing an exception while using "silent!" causes
3922 // the function to abort but not display an error.
3923 tv = STACK_TV_BOT(-1);
3924 clear_tv(tv);
3925 tv->v_type = VAR_NUMBER;
3926 tv->vval.v_number = 0;
3927 goto done;
3928 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003929 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003930 tv = STACK_TV_BOT(0);
3931 if (tv->vval.v_string == NULL
3932 || *skipwhite(tv->vval.v_string) == NUL)
3933 {
3934 vim_free(tv->vval.v_string);
3935 SOURCING_LNUM = iptr->isn_lnum;
3936 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003937 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003938 }
3939
3940 // Inside a "catch" we need to first discard the caught
3941 // exception.
3942 if (trystack->ga_len > 0)
3943 {
3944 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3945 + trystack->ga_len - 1;
3946 if (trycmd->tcd_caught && current_exception != NULL)
3947 {
3948 // discard the exception
3949 if (caught_stack == current_exception)
3950 caught_stack = caught_stack->caught;
3951 discard_current_exception();
3952 trycmd->tcd_caught = FALSE;
3953 }
3954 }
3955
Bram Moolenaar90a57162022-02-12 14:23:17 +00003956 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003957 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3958 == FAIL)
3959 {
3960 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003961 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003962 }
3963 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 break;
3966
3967 // compare with special values
3968 case ISN_COMPAREBOOL:
3969 case ISN_COMPARESPECIAL:
3970 {
3971 typval_T *tv1 = STACK_TV_BOT(-2);
3972 typval_T *tv2 = STACK_TV_BOT(-1);
3973 varnumber_T arg1 = tv1->vval.v_number;
3974 varnumber_T arg2 = tv2->vval.v_number;
3975 int res;
3976
3977 switch (iptr->isn_arg.op.op_type)
3978 {
3979 case EXPR_EQUAL: res = arg1 == arg2; break;
3980 case EXPR_NEQUAL: res = arg1 != arg2; break;
3981 default: res = 0; break;
3982 }
3983
Bram Moolenaar4c137212021-04-19 16:48:48 +02003984 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 tv1->v_type = VAR_BOOL;
3986 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3987 }
3988 break;
3989
Bram Moolenaar7a222242022-03-01 19:23:24 +00003990 case ISN_COMPARENULL:
3991 {
3992 typval_T *tv1 = STACK_TV_BOT(-2);
3993 typval_T *tv2 = STACK_TV_BOT(-1);
3994 int res;
3995
3996 res = typval_compare_null(tv1, tv2);
3997 if (res == MAYBE)
3998 goto on_error;
3999 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4000 res = !res;
4001 clear_tv(tv1);
4002 clear_tv(tv2);
4003 --ectx->ec_stack.ga_len;
4004 tv1->v_type = VAR_BOOL;
4005 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4006 }
4007 break;
4008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 // Operation with two number arguments
4010 case ISN_OPNR:
4011 case ISN_COMPARENR:
4012 {
4013 typval_T *tv1 = STACK_TV_BOT(-2);
4014 typval_T *tv2 = STACK_TV_BOT(-1);
4015 varnumber_T arg1 = tv1->vval.v_number;
4016 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004017 varnumber_T res = 0;
4018 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019
4020 switch (iptr->isn_arg.op.op_type)
4021 {
4022 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004023 case EXPR_DIV: if (arg2 == 0)
4024 div_zero = TRUE;
4025 else
4026 res = arg1 / arg2;
4027 break;
4028 case EXPR_REM: if (arg2 == 0)
4029 div_zero = TRUE;
4030 else
4031 res = arg1 % arg2;
4032 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 case EXPR_SUB: res = arg1 - arg2; break;
4034 case EXPR_ADD: res = arg1 + arg2; break;
4035
4036 case EXPR_EQUAL: res = arg1 == arg2; break;
4037 case EXPR_NEQUAL: res = arg1 != arg2; break;
4038 case EXPR_GREATER: res = arg1 > arg2; break;
4039 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4040 case EXPR_SMALLER: res = arg1 < arg2; break;
4041 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004042 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 }
4044
Bram Moolenaar4c137212021-04-19 16:48:48 +02004045 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 if (iptr->isn_type == ISN_COMPARENR)
4047 {
4048 tv1->v_type = VAR_BOOL;
4049 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4050 }
4051 else
4052 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004053 if (div_zero)
4054 {
4055 SOURCING_LNUM = iptr->isn_lnum;
4056 emsg(_(e_divide_by_zero));
4057 goto on_error;
4058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 }
4060 break;
4061
4062 // Computation with two float arguments
4063 case ISN_OPFLOAT:
4064 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004065#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 {
4067 typval_T *tv1 = STACK_TV_BOT(-2);
4068 typval_T *tv2 = STACK_TV_BOT(-1);
4069 float_T arg1 = tv1->vval.v_float;
4070 float_T arg2 = tv2->vval.v_float;
4071 float_T res = 0;
4072 int cmp = FALSE;
4073
4074 switch (iptr->isn_arg.op.op_type)
4075 {
4076 case EXPR_MULT: res = arg1 * arg2; break;
4077 case EXPR_DIV: res = arg1 / arg2; break;
4078 case EXPR_SUB: res = arg1 - arg2; break;
4079 case EXPR_ADD: res = arg1 + arg2; break;
4080
4081 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4082 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4083 case EXPR_GREATER: cmp = arg1 > arg2; break;
4084 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4085 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4086 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4087 default: cmp = 0; break;
4088 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004089 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 if (iptr->isn_type == ISN_COMPAREFLOAT)
4091 {
4092 tv1->v_type = VAR_BOOL;
4093 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4094 }
4095 else
4096 tv1->vval.v_float = res;
4097 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004098#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 break;
4100
4101 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004102 case ISN_COMPAREDICT:
4103 case ISN_COMPAREFUNC:
4104 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 case ISN_COMPAREBLOB:
4106 {
4107 typval_T *tv1 = STACK_TV_BOT(-2);
4108 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004109 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4110 int ic = iptr->isn_arg.op.op_ic;
4111 int res = FALSE;
4112 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113
Bram Moolenaar265f8112021-12-19 12:33:05 +00004114 SOURCING_LNUM = iptr->isn_lnum;
4115 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004117 status = typval_compare_list(tv1, tv2,
4118 exprtype, ic, &res);
4119 }
4120 else if (iptr->isn_type == ISN_COMPAREDICT)
4121 {
4122 status = typval_compare_dict(tv1, tv2,
4123 exprtype, ic, &res);
4124 }
4125 else if (iptr->isn_type == ISN_COMPAREFUNC)
4126 {
4127 status = typval_compare_func(tv1, tv2,
4128 exprtype, ic, &res);
4129 }
4130 else if (iptr->isn_type == ISN_COMPARESTRING)
4131 {
4132 status = typval_compare_string(tv1, tv2,
4133 exprtype, ic, &res);
4134 }
4135 else
4136 {
4137 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004139 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 clear_tv(tv1);
4141 clear_tv(tv2);
4142 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004143 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4144 if (status == FAIL)
4145 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 }
4147 break;
4148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 case ISN_COMPAREANY:
4150 {
4151 typval_T *tv1 = STACK_TV_BOT(-2);
4152 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004153 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004155 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156
Bram Moolenaareb26f432020-09-14 16:50:05 +02004157 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004158 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004160 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004161 if (status == FAIL)
4162 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 }
4164 break;
4165
4166 case ISN_ADDLIST:
4167 case ISN_ADDBLOB:
4168 {
4169 typval_T *tv1 = STACK_TV_BOT(-2);
4170 typval_T *tv2 = STACK_TV_BOT(-1);
4171
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004172 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004174 {
4175 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4176 && tv1->vval.v_list != NULL)
4177 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4178 NULL);
4179 else
4180 eval_addlist(tv1, tv2);
4181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 else
4183 eval_addblob(tv1, tv2);
4184 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004185 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 }
4187 break;
4188
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004189 case ISN_LISTAPPEND:
4190 {
4191 typval_T *tv1 = STACK_TV_BOT(-2);
4192 typval_T *tv2 = STACK_TV_BOT(-1);
4193 list_T *l = tv1->vval.v_list;
4194
4195 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004196 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004197 if (l == NULL)
4198 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004199 emsg(_(e_cannot_add_to_null_list));
4200 goto on_error;
4201 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004202 if (value_check_lock(l->lv_lock, NULL, FALSE))
4203 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004204 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004205 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004206 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004207 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004208 }
4209 break;
4210
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004211 case ISN_BLOBAPPEND:
4212 {
4213 typval_T *tv1 = STACK_TV_BOT(-2);
4214 typval_T *tv2 = STACK_TV_BOT(-1);
4215 blob_T *b = tv1->vval.v_blob;
4216 int error = FALSE;
4217 varnumber_T n;
4218
4219 // add a number to a blob
4220 if (b == NULL)
4221 {
4222 SOURCING_LNUM = iptr->isn_lnum;
4223 emsg(_(e_cannot_add_to_null_blob));
4224 goto on_error;
4225 }
4226 n = tv_get_number_chk(tv2, &error);
4227 if (error)
4228 goto on_error;
4229 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004230 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004231 }
4232 break;
4233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 // Computation with two arguments of unknown type
4235 case ISN_OPANY:
4236 {
4237 typval_T *tv1 = STACK_TV_BOT(-2);
4238 typval_T *tv2 = STACK_TV_BOT(-1);
4239 varnumber_T n1, n2;
4240#ifdef FEAT_FLOAT
4241 float_T f1 = 0, f2 = 0;
4242#endif
4243 int error = FALSE;
4244
4245 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4246 {
4247 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4248 {
4249 eval_addlist(tv1, tv2);
4250 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004251 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 break;
4253 }
4254 else if (tv1->v_type == VAR_BLOB
4255 && tv2->v_type == VAR_BLOB)
4256 {
4257 eval_addblob(tv1, tv2);
4258 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004259 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 break;
4261 }
4262 }
4263#ifdef FEAT_FLOAT
4264 if (tv1->v_type == VAR_FLOAT)
4265 {
4266 f1 = tv1->vval.v_float;
4267 n1 = 0;
4268 }
4269 else
4270#endif
4271 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004272 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 n1 = tv_get_number_chk(tv1, &error);
4274 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004275 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276#ifdef FEAT_FLOAT
4277 if (tv2->v_type == VAR_FLOAT)
4278 f1 = n1;
4279#endif
4280 }
4281#ifdef FEAT_FLOAT
4282 if (tv2->v_type == VAR_FLOAT)
4283 {
4284 f2 = tv2->vval.v_float;
4285 n2 = 0;
4286 }
4287 else
4288#endif
4289 {
4290 n2 = tv_get_number_chk(tv2, &error);
4291 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004292 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293#ifdef FEAT_FLOAT
4294 if (tv1->v_type == VAR_FLOAT)
4295 f2 = n2;
4296#endif
4297 }
4298#ifdef FEAT_FLOAT
4299 // if there is a float on either side the result is a float
4300 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4301 {
4302 switch (iptr->isn_arg.op.op_type)
4303 {
4304 case EXPR_MULT: f1 = f1 * f2; break;
4305 case EXPR_DIV: f1 = f1 / f2; break;
4306 case EXPR_SUB: f1 = f1 - f2; break;
4307 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004308 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004309 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004310 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 }
4312 clear_tv(tv1);
4313 clear_tv(tv2);
4314 tv1->v_type = VAR_FLOAT;
4315 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004316 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 }
4318 else
4319#endif
4320 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004321 int failed = FALSE;
4322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 switch (iptr->isn_arg.op.op_type)
4324 {
4325 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004326 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4327 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004328 goto on_error;
4329 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 case EXPR_SUB: n1 = n1 - n2; break;
4331 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004332 default: n1 = num_modulus(n1, n2, &failed);
4333 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004334 goto on_error;
4335 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 }
4337 clear_tv(tv1);
4338 clear_tv(tv2);
4339 tv1->v_type = VAR_NUMBER;
4340 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004341 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 }
4343 }
4344 break;
4345
4346 case ISN_CONCAT:
4347 {
4348 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4349 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4350 char_u *res;
4351
4352 res = concat_str(str1, str2);
4353 clear_tv(STACK_TV_BOT(-2));
4354 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004355 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 STACK_TV_BOT(-1)->vval.v_string = res;
4357 }
4358 break;
4359
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004360 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004361 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004362 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004363 int is_slice = iptr->isn_type == ISN_STRSLICE;
4364 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004365 char_u *res;
4366
4367 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004368 // string slice: string is at stack-3, first index at
4369 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004370 if (is_slice)
4371 {
4372 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004373 n1 = tv->vval.v_number;
4374 }
4375
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004376 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004377 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004378
Bram Moolenaar4c137212021-04-19 16:48:48 +02004379 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004380 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004381 if (is_slice)
4382 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004383 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004384 else
4385 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004386 // single character (including composing characters).
4387 // If the index is too big or negative the result is
4388 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004389 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004390 vim_free(tv->vval.v_string);
4391 tv->vval.v_string = res;
4392 }
4393 break;
4394
4395 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004396 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004397 case ISN_BLOBINDEX:
4398 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004400 int is_slice = iptr->isn_type == ISN_LISTSLICE
4401 || iptr->isn_type == ISN_BLOBSLICE;
4402 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4403 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004404 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004405 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406
4407 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004408 // list slice: list is at stack-3, indexes at stack-2 and
4409 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004410 // Same for blob.
4411 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412
4413 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004414 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004416
4417 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 {
Bram Moolenaared591872020-08-15 22:14:53 +02004419 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004420 n1 = tv->vval.v_number;
4421 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 }
Bram Moolenaared591872020-08-15 22:14:53 +02004423
Bram Moolenaar4c137212021-04-19 16:48:48 +02004424 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004425 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004426 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004427 if (is_blob)
4428 {
4429 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4430 n1, n2, FALSE, tv) == FAIL)
4431 goto on_error;
4432 }
4433 else
4434 {
4435 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4436 n1, n2, FALSE, tv, TRUE) == FAIL)
4437 goto on_error;
4438 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 }
4440 break;
4441
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004442 case ISN_ANYINDEX:
4443 case ISN_ANYSLICE:
4444 {
4445 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4446 typval_T *var1, *var2;
4447 int res;
4448
4449 // index: composite is at stack-2, index at stack-1
4450 // slice: composite is at stack-3, indexes at stack-2 and
4451 // stack-1
4452 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004453 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004454 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4455 goto on_error;
4456 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4457 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004458 res = eval_index_inner(tv, is_slice, var1, var2,
4459 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004460 clear_tv(var1);
4461 if (is_slice)
4462 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004463 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004464 if (res == FAIL)
4465 goto on_error;
4466 }
4467 break;
4468
Bram Moolenaar9af78762020-06-16 11:34:42 +02004469 case ISN_SLICE:
4470 {
4471 list_T *list;
4472 int count = iptr->isn_arg.number;
4473
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004474 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004475 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004476 list = tv->vval.v_list;
4477
4478 // no error for short list, expect it to be checked earlier
4479 if (list != NULL && list->lv_len >= count)
4480 {
4481 list_T *newlist = list_slice(list,
4482 count, list->lv_len - 1);
4483
4484 if (newlist != NULL)
4485 {
4486 list_unref(list);
4487 tv->vval.v_list = newlist;
4488 ++newlist->lv_refcount;
4489 }
4490 }
4491 }
4492 break;
4493
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004494 case ISN_GETITEM:
4495 {
4496 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004497 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004498
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004499 // Get list item: list is at stack-1, push item.
4500 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004501 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4502 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004503
Bram Moolenaar35578162021-08-02 19:10:38 +02004504 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004505 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004506 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004507 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004508
4509 // Useful when used in unpack assignment. Reset at
4510 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004511 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004512 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004513 }
4514 break;
4515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 case ISN_MEMBER:
4517 {
4518 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004519 char_u *key;
4520 dictitem_T *di;
4521
4522 // dict member: dict is at stack-2, key at stack-1
4523 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004524 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004525 dict = tv->vval.v_dict;
4526
4527 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004528 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004529 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004530 if (key == NULL)
4531 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004532
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004533 if ((di = dict_find(dict, key, -1)) == NULL)
4534 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004535 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004536 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004537
4538 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004539 // stack contents makes sense and the dict stack is
4540 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004541 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004542 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004543 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004544 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004545 tv->v_type = VAR_NUMBER;
4546 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004547 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004548 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004549 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004550 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004551 // Put the dict used on the dict stack, it might be used by
4552 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004553 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004554 if (dict_stack_save(tv) == FAIL)
4555 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004556 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004557 }
4558 break;
4559
4560 // dict member with string key
4561 case ISN_STRINGMEMBER:
4562 {
4563 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 dictitem_T *di;
4565
4566 tv = STACK_TV_BOT(-1);
4567 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4568 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004569 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004570 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004571 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 }
4573 dict = tv->vval.v_dict;
4574
4575 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4576 == NULL)
4577 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004578 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004579 semsg(_(e_key_not_present_in_dictionary),
4580 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004581 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004583 // Put the dict used on the dict stack, it might be used by
4584 // a dict function later.
4585 if (dict_stack_save(tv) == FAIL)
4586 goto on_fatal_error;
4587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004589 }
4590 break;
4591
4592 case ISN_CLEARDICT:
4593 dict_stack_drop();
4594 break;
4595
4596 case ISN_USEDICT:
4597 {
4598 typval_T *dict_tv = dict_stack_get_tv();
4599
4600 // Turn "dict.Func" into a partial for "Func" bound to
4601 // "dict". Don't do this when "Func" is already a partial
4602 // that was bound explicitly (pt_auto is FALSE).
4603 tv = STACK_TV_BOT(-1);
4604 if (dict_tv != NULL
4605 && dict_tv->v_type == VAR_DICT
4606 && dict_tv->vval.v_dict != NULL
4607 && (tv->v_type == VAR_FUNC
4608 || (tv->v_type == VAR_PARTIAL
4609 && (tv->vval.v_partial->pt_auto
4610 || tv->vval.v_partial->pt_dict == NULL))))
4611 dict_tv->vval.v_dict =
4612 make_partial(dict_tv->vval.v_dict, tv);
4613 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 }
4615 break;
4616
4617 case ISN_NEGATENR:
4618 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004619 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004620#ifdef FEAT_FLOAT
4621 if (tv->v_type == VAR_FLOAT)
4622 tv->vval.v_float = -tv->vval.v_float;
4623 else
4624#endif
4625 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 break;
4627
4628 case ISN_CHECKNR:
4629 {
4630 int error = FALSE;
4631
4632 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004633 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004635 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 (void)tv_get_number_chk(tv, &error);
4637 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004638 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 }
4640 break;
4641
4642 case ISN_CHECKTYPE:
4643 {
4644 checktype_T *ct = &iptr->isn_arg.type;
4645
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004646 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004647 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004648 if (!ectx->ec_where.wt_variable)
4649 ectx->ec_where.wt_index = ct->ct_arg_idx;
4650 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4651 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004652 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004653 if (!ectx->ec_where.wt_variable)
4654 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004655
4656 // number 0 is FALSE, number 1 is TRUE
4657 if (tv->v_type == VAR_NUMBER
4658 && ct->ct_type->tt_type == VAR_BOOL
4659 && (tv->vval.v_number == 0
4660 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004662 tv->v_type = VAR_BOOL;
4663 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004664 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 }
4666 }
4667 break;
4668
Bram Moolenaar9af78762020-06-16 11:34:42 +02004669 case ISN_CHECKLEN:
4670 {
4671 int min_len = iptr->isn_arg.checklen.cl_min_len;
4672 list_T *list = NULL;
4673
4674 tv = STACK_TV_BOT(-1);
4675 if (tv->v_type == VAR_LIST)
4676 list = tv->vval.v_list;
4677 if (list == NULL || list->lv_len < min_len
4678 || (list->lv_len > min_len
4679 && !iptr->isn_arg.checklen.cl_more_OK))
4680 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004681 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004682 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004683 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004684 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004685 }
4686 }
4687 break;
4688
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004689 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004690 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004691 break;
4692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004694 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 {
4696 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004697 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004699 if (iptr->isn_type == ISN_2BOOL)
4700 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004701 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004702 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004703 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004704 n = !n;
4705 }
4706 else
4707 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004708 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004709 SOURCING_LNUM = iptr->isn_lnum;
4710 n = tv_get_bool_chk(tv, &error);
4711 if (error)
4712 goto on_error;
4713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 clear_tv(tv);
4715 tv->v_type = VAR_BOOL;
4716 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4717 }
4718 break;
4719
4720 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004721 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004722 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004723 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4724 iptr->isn_type == ISN_2STRING_ANY,
4725 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004726 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 break;
4728
Bram Moolenaar08597872020-12-10 19:43:40 +01004729 case ISN_RANGE:
4730 {
4731 exarg_T ea;
4732 char *errormsg;
4733
Bram Moolenaarece0b872021-01-08 20:40:45 +01004734 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004735 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004736 ea.addr_type = ADDR_LINES;
4737 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004738 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004739 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004740 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004741
Bram Moolenaar35578162021-08-02 19:10:38 +02004742 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004743 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004744 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004745 tv = STACK_TV_BOT(-1);
4746 tv->v_type = VAR_NUMBER;
4747 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004748 if (ea.addr_count == 0)
4749 tv->vval.v_number = curwin->w_cursor.lnum;
4750 else
4751 tv->vval.v_number = ea.line2;
4752 }
4753 break;
4754
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004755 case ISN_PUT:
4756 {
4757 int regname = iptr->isn_arg.put.put_regname;
4758 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4759 char_u *expr = NULL;
4760 int dir = FORWARD;
4761
Bram Moolenaar08597872020-12-10 19:43:40 +01004762 if (lnum < -2)
4763 {
4764 // line number was put on the stack by ISN_RANGE
4765 tv = STACK_TV_BOT(-1);
4766 curwin->w_cursor.lnum = tv->vval.v_number;
4767 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4768 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004769 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004770 }
4771 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004772 // :put! above cursor
4773 dir = BACKWARD;
4774 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004775 {
4776 curwin->w_cursor.lnum = lnum;
4777 if (lnum == 0)
4778 // check_cursor() below will move to line 1
4779 dir = BACKWARD;
4780 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004781
4782 if (regname == '=')
4783 {
4784 tv = STACK_TV_BOT(-1);
4785 if (tv->v_type == VAR_STRING)
4786 expr = tv->vval.v_string;
4787 else
4788 {
4789 expr = typval2string(tv, TRUE); // allocates value
4790 clear_tv(tv);
4791 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004792 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004793 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004794 check_cursor();
4795 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4796 vim_free(expr);
4797 }
4798 break;
4799
Bram Moolenaar02194d22020-10-24 23:08:38 +02004800 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004801 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4802 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4803 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4804 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004805 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4806 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004807 break;
4808
Bram Moolenaar02194d22020-10-24 23:08:38 +02004809 case ISN_CMDMOD_REV:
4810 // filter regprog is owned by the instruction, don't free it
4811 cmdmod.cmod_filter_regmatch.regprog = NULL;
4812 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004813 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4814 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004815 break;
4816
Bram Moolenaar792f7862020-11-23 08:31:18 +01004817 case ISN_UNPACK:
4818 {
4819 int count = iptr->isn_arg.unpack.unp_count;
4820 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4821 list_T *l;
4822 listitem_T *li;
4823 int i;
4824
4825 // Check there is a valid list to unpack.
4826 tv = STACK_TV_BOT(-1);
4827 if (tv->v_type != VAR_LIST)
4828 {
4829 SOURCING_LNUM = iptr->isn_lnum;
4830 emsg(_(e_for_argument_must_be_sequence_of_lists));
4831 goto on_error;
4832 }
4833 l = tv->vval.v_list;
4834 if (l == NULL
4835 || l->lv_len < (semicolon ? count - 1 : count))
4836 {
4837 SOURCING_LNUM = iptr->isn_lnum;
4838 emsg(_(e_list_value_does_not_have_enough_items));
4839 goto on_error;
4840 }
4841 else if (!semicolon && l->lv_len > count)
4842 {
4843 SOURCING_LNUM = iptr->isn_lnum;
4844 emsg(_(e_list_value_has_more_items_than_targets));
4845 goto on_error;
4846 }
4847
4848 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004849 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004850 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004851 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004852
4853 // Variable after semicolon gets a list with the remaining
4854 // items.
4855 if (semicolon)
4856 {
4857 list_T *rem_list =
4858 list_alloc_with_items(l->lv_len - count + 1);
4859
4860 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004861 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004862 tv = STACK_TV_BOT(-count);
4863 tv->vval.v_list = rem_list;
4864 ++rem_list->lv_refcount;
4865 tv->v_lock = 0;
4866 li = l->lv_first;
4867 for (i = 0; i < count - 1; ++i)
4868 li = li->li_next;
4869 for (i = 0; li != NULL; ++i)
4870 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004871 typval_T tvcopy;
4872
4873 copy_tv(&li->li_tv, &tvcopy);
4874 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004875 li = li->li_next;
4876 }
4877 --count;
4878 }
4879
4880 // Produce the values in reverse order, first item last.
4881 li = l->lv_first;
4882 for (i = 0; i < count; ++i)
4883 {
4884 tv = STACK_TV_BOT(-i - 1);
4885 copy_tv(&li->li_tv, tv);
4886 li = li->li_next;
4887 }
4888
4889 list_unref(l);
4890 }
4891 break;
4892
Bram Moolenaarb2049902021-01-24 12:53:53 +01004893 case ISN_PROF_START:
4894 case ISN_PROF_END:
4895 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004896#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004897 funccall_T cookie;
4898 ufunc_T *cur_ufunc =
4899 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004900 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004901
4902 cookie.func = cur_ufunc;
4903 if (iptr->isn_type == ISN_PROF_START)
4904 {
4905 func_line_start(&cookie, iptr->isn_lnum);
4906 // if we get here the instruction is executed
4907 func_line_exec(&cookie);
4908 }
4909 else
4910 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004911#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004912 }
4913 break;
4914
Bram Moolenaare99d4222021-06-13 14:01:26 +02004915 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004916 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004917 break;
4918
Bram Moolenaar389df252020-07-09 21:20:47 +02004919 case ISN_SHUFFLE:
4920 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004921 typval_T tmp_tv;
4922 int item = iptr->isn_arg.shuffle.shfl_item;
4923 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004924
4925 tmp_tv = *STACK_TV_BOT(-item);
4926 for ( ; up > 0 && item > 1; --up)
4927 {
4928 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4929 --item;
4930 }
4931 *STACK_TV_BOT(-item) = tmp_tv;
4932 }
4933 break;
4934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004936 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004938 ectx->ec_where.wt_index = 0;
4939 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 break;
4941 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004942 continue;
4943
Bram Moolenaard032f342020-07-18 18:13:02 +02004944func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004945 // Restore previous function. If the frame pointer is where we started
4946 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004947 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004948 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004949
Bram Moolenaar4c137212021-04-19 16:48:48 +02004950 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004951 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004952 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004953 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004954
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004955on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004956 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004957 // If "emsg_silent" is set then ignore the error, unless it was set
4958 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004959 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004960 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004961 {
4962 // If a sequence of instructions causes an error while ":silent!"
4963 // was used, restore the stack length and jump ahead to restoring
4964 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004965 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004966 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004967 while (ectx->ec_stack.ga_len
4968 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004969 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004970 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004971 clear_tv(STACK_TV_BOT(0));
4972 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004973 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4974 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004975 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004976 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004977 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004978on_fatal_error:
4979 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004980 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004981 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004982 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 }
4984
4985done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004986 ret = OK;
4987theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004988 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004989 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4990 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004991}
4992
4993/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004994 * Execute the instructions from a VAR_INSTR typeval and put the result in
4995 * "rettv".
4996 * Return OK or FAIL.
4997 */
4998 int
4999exe_typval_instr(typval_T *tv, typval_T *rettv)
5000{
5001 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5002 isn_T *save_instr = ectx->ec_instr;
5003 int save_iidx = ectx->ec_iidx;
5004 int res;
5005
5006 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5007 res = exec_instructions(ectx);
5008 if (res == OK)
5009 {
5010 *rettv = *STACK_TV_BOT(-1);
5011 --ectx->ec_stack.ga_len;
5012 }
5013
5014 ectx->ec_instr = save_instr;
5015 ectx->ec_iidx = save_iidx;
5016
5017 return res;
5018}
5019
5020/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005021 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5022 * "substitute_instr".
5023 */
5024 char_u *
5025exe_substitute_instr(void)
5026{
5027 ectx_T *ectx = substitute_instr->subs_ectx;
5028 isn_T *save_instr = ectx->ec_instr;
5029 int save_iidx = ectx->ec_iidx;
5030 char_u *res;
5031
5032 ectx->ec_instr = substitute_instr->subs_instr;
5033 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005034 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005035 typval_T *tv = STACK_TV_BOT(-1);
5036
Bram Moolenaar27523602021-06-05 21:36:19 +02005037 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005038 --ectx->ec_stack.ga_len;
5039 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005040 }
5041 else
5042 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005043 substitute_instr->subs_status = FAIL;
5044 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005045 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046
Bram Moolenaar4c137212021-04-19 16:48:48 +02005047 ectx->ec_instr = save_instr;
5048 ectx->ec_iidx = save_iidx;
5049
5050 return res;
5051}
5052
5053/*
5054 * Call a "def" function from old Vim script.
5055 * Return OK or FAIL.
5056 */
5057 int
5058call_def_function(
5059 ufunc_T *ufunc,
5060 int argc_arg, // nr of arguments
5061 typval_T *argv, // arguments
5062 partial_T *partial, // optional partial for context
5063 typval_T *rettv) // return value
5064{
5065 ectx_T ectx; // execution context
5066 int argc = argc_arg;
5067 typval_T *tv;
5068 int idx;
5069 int ret = FAIL;
5070 int defcount = ufunc->uf_args.ga_len - argc;
5071 sctx_T save_current_sctx = current_sctx;
5072 int did_emsg_before = did_emsg_cumul + did_emsg;
5073 int save_suppress_errthrow = suppress_errthrow;
5074 msglist_T **saved_msg_list = NULL;
5075 msglist_T *private_msg_list = NULL;
5076 int save_emsg_silent_def = emsg_silent_def;
5077 int save_did_emsg_def = did_emsg_def;
5078 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005079 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005080
5081// Get pointer to item in the stack.
5082#undef STACK_TV
5083#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5084
5085// Get pointer to item at the bottom of the stack, -1 is the bottom.
5086#undef STACK_TV_BOT
5087#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5088
5089// Get pointer to a local variable on the stack. Negative for arguments.
5090#undef STACK_TV_VAR
5091#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5092
5093 if (ufunc->uf_def_status == UF_NOT_COMPILED
5094 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005095 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5096 && compile_def_function(ufunc, FALSE,
5097 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005098 {
5099 if (did_emsg_cumul + did_emsg == did_emsg_before)
5100 semsg(_(e_function_is_not_compiled_str),
5101 printable_func_name(ufunc));
5102 return FAIL;
5103 }
5104
5105 {
5106 // Check the function was really compiled.
5107 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5108 + ufunc->uf_dfunc_idx;
5109 if (INSTRUCTIONS(dfunc) == NULL)
5110 {
5111 iemsg("using call_def_function() on not compiled function");
5112 return FAIL;
5113 }
5114 }
5115
5116 // If depth of calling is getting too high, don't execute the function.
5117 orig_funcdepth = funcdepth_get();
5118 if (funcdepth_increment() == FAIL)
5119 return FAIL;
5120
5121 CLEAR_FIELD(ectx);
5122 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5123 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005124 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005125 {
5126 funcdepth_decrement();
5127 return FAIL;
5128 }
5129 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5130 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5131 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005132 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005133
5134 idx = argc - ufunc->uf_args.ga_len;
5135 if (idx > 0 && ufunc->uf_va_name == NULL)
5136 {
5137 if (idx == 1)
5138 emsg(_(e_one_argument_too_many));
5139 else
5140 semsg(_(e_nr_arguments_too_many), idx);
5141 goto failed_early;
5142 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005143 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5144 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005145 {
5146 if (idx == -1)
5147 emsg(_(e_one_argument_too_few));
5148 else
5149 semsg(_(e_nr_arguments_too_few), -idx);
5150 goto failed_early;
5151 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005152
5153 // Put arguments on the stack, but no more than what the function expects.
5154 // A lambda can be called with more arguments than it uses.
5155 for (idx = 0; idx < argc
5156 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5157 ++idx)
5158 {
5159 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5160 && argv[idx].v_type == VAR_SPECIAL
5161 && argv[idx].vval.v_number == VVAL_NONE)
5162 {
5163 // Use the default value.
5164 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5165 }
5166 else
5167 {
5168 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5169 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005170 ufunc->uf_arg_types[idx], &argv[idx],
5171 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005172 goto failed_early;
5173 copy_tv(&argv[idx], STACK_TV_BOT(0));
5174 }
5175 ++ectx.ec_stack.ga_len;
5176 }
5177
5178 // Turn varargs into a list. Empty list if no args.
5179 if (ufunc->uf_va_name != NULL)
5180 {
5181 int vararg_count = argc - ufunc->uf_args.ga_len;
5182
5183 if (vararg_count < 0)
5184 vararg_count = 0;
5185 else
5186 argc -= vararg_count;
5187 if (exe_newlist(vararg_count, &ectx) == FAIL)
5188 goto failed_early;
5189
5190 // Check the type of the list items.
5191 tv = STACK_TV_BOT(-1);
5192 if (ufunc->uf_va_type != NULL
5193 && ufunc->uf_va_type != &t_list_any
5194 && ufunc->uf_va_type->tt_member != &t_any
5195 && tv->vval.v_list != NULL)
5196 {
5197 type_T *expected = ufunc->uf_va_type->tt_member;
5198 listitem_T *li = tv->vval.v_list->lv_first;
5199
5200 for (idx = 0; idx < vararg_count; ++idx)
5201 {
5202 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005203 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005204 goto failed_early;
5205 li = li->li_next;
5206 }
5207 }
5208
5209 if (defcount > 0)
5210 // Move varargs list to below missing default arguments.
5211 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5212 --ectx.ec_stack.ga_len;
5213 }
5214
5215 // Make space for omitted arguments, will store default value below.
5216 // Any varargs list goes after them.
5217 if (defcount > 0)
5218 for (idx = 0; idx < defcount; ++idx)
5219 {
5220 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5221 ++ectx.ec_stack.ga_len;
5222 }
5223 if (ufunc->uf_va_name != NULL)
5224 ++ectx.ec_stack.ga_len;
5225
5226 // Frame pointer points to just after arguments.
5227 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5228 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5229
5230 {
5231 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5232 + ufunc->uf_dfunc_idx;
5233 ufunc_T *base_ufunc = dfunc->df_ufunc;
5234
5235 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5236 // by copy_func().
5237 if (partial != NULL || base_ufunc->uf_partial != NULL)
5238 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005239 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5240 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005241 goto failed_early;
5242 if (partial != NULL)
5243 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005244 outer_T *outer = get_pt_outer(partial);
5245
5246 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005247 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005248 if (current_ectx != NULL)
5249 {
5250 if (current_ectx->ec_outer_ref != NULL
5251 && current_ectx->ec_outer_ref->or_outer != NULL)
5252 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005253 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005254 }
5255 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005256 }
5257 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005258 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005259 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005260 ++partial->pt_refcount;
5261 ectx.ec_outer_ref->or_partial = partial;
5262 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005263 }
5264 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005265 {
5266 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5267 ++base_ufunc->uf_partial->pt_refcount;
5268 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5269 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005270 }
5271 }
5272
5273 // dummy frame entries
5274 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5275 {
5276 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5277 ++ectx.ec_stack.ga_len;
5278 }
5279
5280 {
5281 // Reserve space for local variables and any closure reference count.
5282 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5283 + ufunc->uf_dfunc_idx;
5284
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005285 // Initialize variables to zero. That avoids having to generate
5286 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005287 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005288 {
5289 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5290 STACK_TV_VAR(idx)->vval.v_number = 0;
5291 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005292 ectx.ec_stack.ga_len += dfunc->df_varcount;
5293 if (dfunc->df_has_closure)
5294 {
5295 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5296 STACK_TV_VAR(idx)->vval.v_number = 0;
5297 ++ectx.ec_stack.ga_len;
5298 }
5299
5300 ectx.ec_instr = INSTRUCTIONS(dfunc);
5301 }
5302
5303 // Following errors are in the function, not the caller.
5304 // Commands behave like vim9script.
5305 estack_push_ufunc(ufunc, 1);
5306 current_sctx = ufunc->uf_script_ctx;
5307 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5308
5309 // Use a specific location for storing error messages to be converted to an
5310 // exception.
5311 saved_msg_list = msg_list;
5312 msg_list = &private_msg_list;
5313
5314 // Do turn errors into exceptions.
5315 suppress_errthrow = FALSE;
5316
5317 // Do not delete the function while executing it.
5318 ++ufunc->uf_calls;
5319
5320 // When ":silent!" was used before calling then we still abort the
5321 // function. If ":silent!" is used in the function then we don't.
5322 emsg_silent_def = emsg_silent;
5323 did_emsg_def = 0;
5324
5325 ectx.ec_where.wt_index = 0;
5326 ectx.ec_where.wt_variable = FALSE;
5327
5328 // Execute the instructions until done.
5329 ret = exec_instructions(&ectx);
5330 if (ret == OK)
5331 {
5332 // function finished, get result from the stack.
5333 if (ufunc->uf_ret_type == &t_void)
5334 {
5335 rettv->v_type = VAR_VOID;
5336 }
5337 else
5338 {
5339 tv = STACK_TV_BOT(-1);
5340 *rettv = *tv;
5341 tv->v_type = VAR_UNKNOWN;
5342 }
5343 }
5344
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005345 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005346 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5347 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005348
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005349 // Deal with any remaining closures, they may be in use somewhere.
5350 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005351 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005352 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005353 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005354 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005355
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005356 estack_pop();
5357 current_sctx = save_current_sctx;
5358
Bram Moolenaar2336c372021-12-06 15:06:54 +00005359 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5360 // Function was unreferenced while being used, free it now.
5361 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005362
Bram Moolenaar352134b2020-10-17 22:04:08 +02005363 if (*msg_list != NULL && saved_msg_list != NULL)
5364 {
5365 msglist_T **plist = saved_msg_list;
5366
5367 // Append entries from the current msg_list (uncaught exceptions) to
5368 // the saved msg_list.
5369 while (*plist != NULL)
5370 plist = &(*plist)->next;
5371
5372 *plist = *msg_list;
5373 }
5374 msg_list = saved_msg_list;
5375
Bram Moolenaar4c137212021-04-19 16:48:48 +02005376 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005377 {
5378 cmdmod.cmod_filter_regmatch.regprog = NULL;
5379 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005380 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005381 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005382 emsg_silent_def = save_emsg_silent_def;
5383 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005384
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005385failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005386 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5388 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005389 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005390
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005391 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005392 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005393 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005394 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005395 if (ectx.ec_outer_ref->or_outer_allocated)
5396 vim_free(ectx.ec_outer_ref->or_outer);
5397 partial_unref(ectx.ec_outer_ref->or_partial);
5398 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005399 }
5400
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005401 // Not sure if this is necessary.
5402 suppress_errthrow = save_suppress_errthrow;
5403
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005404 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5405 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005406 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005407 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005408 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409 return ret;
5410}
5411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005412/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005413 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5414 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5415 * "pfx" is prefixed to every line.
5416 */
5417 static void
5418list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5419{
5420 int line_idx = 0;
5421 int prev_current = 0;
5422 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005423 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005424
5425 for (current = 0; current < instr_count; ++current)
5426 {
5427 isn_T *iptr = &instr[current];
5428 char *line;
5429
5430 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005431 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005432 while (line_idx < iptr->isn_lnum
5433 && line_idx < ufunc->uf_lines.ga_len)
5434 {
5435 if (current > prev_current)
5436 {
5437 msg_puts("\n\n");
5438 prev_current = current;
5439 }
5440 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5441 if (line != NULL)
5442 msg(line);
5443 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005444 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5445 {
5446 int first_def_arg = ufunc->uf_args.ga_len
5447 - ufunc->uf_def_args.ga_len;
5448
5449 if (def_arg_idx > 0)
5450 msg_puts("\n\n");
5451 msg_start();
5452 msg_puts(" ");
5453 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5454 first_def_arg + def_arg_idx]);
5455 msg_puts(" = ");
5456 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5457 msg_clr_eos();
5458 msg_end();
5459 }
5460 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005461
5462 switch (iptr->isn_type)
5463 {
5464 case ISN_EXEC:
5465 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5466 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005467 case ISN_EXEC_SPLIT:
5468 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5469 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005470 case ISN_EXECRANGE:
5471 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5472 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005473 case ISN_LEGACY_EVAL:
5474 smsg("%s%4d EVAL legacy %s", pfx, current,
5475 iptr->isn_arg.string);
5476 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005477 case ISN_REDIRSTART:
5478 smsg("%s%4d REDIR", pfx, current);
5479 break;
5480 case ISN_REDIREND:
5481 smsg("%s%4d REDIR END%s", pfx, current,
5482 iptr->isn_arg.number ? " append" : "");
5483 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005484 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005485#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005486 smsg("%s%4d CEXPR pre %s", pfx, current,
5487 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005488#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005489 break;
5490 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005491#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005492 {
5493 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5494
5495 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5496 cexpr_get_auname(cer->cer_cmdidx),
5497 cer->cer_forceit ? "!" : "",
5498 cer->cer_cmdline);
5499 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005500#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005501 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005502 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005503 smsg("%s%4d INSTR", pfx, current);
5504 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5505 msg(" -------------");
5506 break;
5507 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005508 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005509 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5510
5511 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005512 }
5513 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005514 case ISN_SUBSTITUTE:
5515 {
5516 subs_T *subs = &iptr->isn_arg.subs;
5517
5518 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5519 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5520 msg(" -------------");
5521 }
5522 break;
5523 case ISN_EXECCONCAT:
5524 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5525 (varnumber_T)iptr->isn_arg.number);
5526 break;
5527 case ISN_ECHO:
5528 {
5529 echo_T *echo = &iptr->isn_arg.echo;
5530
5531 smsg("%s%4d %s %d", pfx, current,
5532 echo->echo_with_white ? "ECHO" : "ECHON",
5533 echo->echo_count);
5534 }
5535 break;
5536 case ISN_EXECUTE:
5537 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005538 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005539 break;
5540 case ISN_ECHOMSG:
5541 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005542 (varnumber_T)(iptr->isn_arg.number));
5543 break;
5544 case ISN_ECHOCONSOLE:
5545 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5546 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005547 break;
5548 case ISN_ECHOERR:
5549 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005550 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005551 break;
5552 case ISN_LOAD:
5553 {
5554 if (iptr->isn_arg.number < 0)
5555 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5556 (varnumber_T)(iptr->isn_arg.number
5557 + STACK_FRAME_SIZE));
5558 else
5559 smsg("%s%4d LOAD $%lld", pfx, current,
5560 (varnumber_T)(iptr->isn_arg.number));
5561 }
5562 break;
5563 case ISN_LOADOUTER:
5564 {
5565 if (iptr->isn_arg.number < 0)
5566 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5567 iptr->isn_arg.outer.outer_depth,
5568 iptr->isn_arg.outer.outer_idx
5569 + STACK_FRAME_SIZE);
5570 else
5571 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5572 iptr->isn_arg.outer.outer_depth,
5573 iptr->isn_arg.outer.outer_idx);
5574 }
5575 break;
5576 case ISN_LOADV:
5577 smsg("%s%4d LOADV v:%s", pfx, current,
5578 get_vim_var_name(iptr->isn_arg.number));
5579 break;
5580 case ISN_LOADSCRIPT:
5581 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005582 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5583 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5584 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005585
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005586 sv = get_script_svar(sref, -1);
5587 if (sv == NULL)
5588 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5589 pfx, current, si->sn_name);
5590 else
5591 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005592 sv->sv_name,
5593 sref->sref_idx,
5594 si->sn_name);
5595 }
5596 break;
5597 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005598 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005599 {
5600 scriptitem_T *si = SCRIPT_ITEM(
5601 iptr->isn_arg.loadstore.ls_sid);
5602
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005603 smsg("%s%4d %s s:%s from %s", pfx, current,
5604 iptr->isn_type == ISN_LOADS ? "LOADS"
5605 : "LOADEXPORT",
5606 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005607 }
5608 break;
5609 case ISN_LOADAUTO:
5610 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5611 break;
5612 case ISN_LOADG:
5613 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5614 break;
5615 case ISN_LOADB:
5616 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5617 break;
5618 case ISN_LOADW:
5619 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5620 break;
5621 case ISN_LOADT:
5622 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5623 break;
5624 case ISN_LOADGDICT:
5625 smsg("%s%4d LOAD g:", pfx, current);
5626 break;
5627 case ISN_LOADBDICT:
5628 smsg("%s%4d LOAD b:", pfx, current);
5629 break;
5630 case ISN_LOADWDICT:
5631 smsg("%s%4d LOAD w:", pfx, current);
5632 break;
5633 case ISN_LOADTDICT:
5634 smsg("%s%4d LOAD t:", pfx, current);
5635 break;
5636 case ISN_LOADOPT:
5637 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5638 break;
5639 case ISN_LOADENV:
5640 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5641 break;
5642 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005643 smsg("%s%4d LOADREG @%c", pfx, current,
5644 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005645 break;
5646
5647 case ISN_STORE:
5648 if (iptr->isn_arg.number < 0)
5649 smsg("%s%4d STORE arg[%lld]", pfx, current,
5650 iptr->isn_arg.number + STACK_FRAME_SIZE);
5651 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005652 smsg("%s%4d STORE $%lld", pfx, current,
5653 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005654 break;
5655 case ISN_STOREOUTER:
5656 {
5657 if (iptr->isn_arg.number < 0)
5658 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5659 iptr->isn_arg.outer.outer_depth,
5660 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5661 else
5662 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5663 iptr->isn_arg.outer.outer_depth,
5664 iptr->isn_arg.outer.outer_idx);
5665 }
5666 break;
5667 case ISN_STOREV:
5668 smsg("%s%4d STOREV v:%s", pfx, current,
5669 get_vim_var_name(iptr->isn_arg.number));
5670 break;
5671 case ISN_STOREAUTO:
5672 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5673 break;
5674 case ISN_STOREG:
5675 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5676 break;
5677 case ISN_STOREB:
5678 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5679 break;
5680 case ISN_STOREW:
5681 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5682 break;
5683 case ISN_STORET:
5684 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5685 break;
5686 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005687 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005688 {
5689 scriptitem_T *si = SCRIPT_ITEM(
5690 iptr->isn_arg.loadstore.ls_sid);
5691
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005692 smsg("%s%4d %s %s in %s", pfx, current,
5693 iptr->isn_type == ISN_STORES
5694 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005695 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5696 }
5697 break;
5698 case ISN_STORESCRIPT:
5699 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005700 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5701 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5702 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005703
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005704 sv = get_script_svar(sref, -1);
5705 if (sv == NULL)
5706 smsg("%s%4d STORESCRIPT [deleted] in %s",
5707 pfx, current, si->sn_name);
5708 else
5709 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005710 sv->sv_name,
5711 sref->sref_idx,
5712 si->sn_name);
5713 }
5714 break;
5715 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005716 case ISN_STOREFUNCOPT:
5717 smsg("%s%4d %s &%s", pfx, current,
5718 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005719 iptr->isn_arg.storeopt.so_name);
5720 break;
5721 case ISN_STOREENV:
5722 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5723 break;
5724 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005725 smsg("%s%4d STOREREG @%c", pfx, current,
5726 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005727 break;
5728 case ISN_STORENR:
5729 smsg("%s%4d STORE %lld in $%d", pfx, current,
5730 iptr->isn_arg.storenr.stnr_val,
5731 iptr->isn_arg.storenr.stnr_idx);
5732 break;
5733
5734 case ISN_STOREINDEX:
5735 smsg("%s%4d STOREINDEX %s", pfx, current,
5736 vartype_name(iptr->isn_arg.vartype));
5737 break;
5738
5739 case ISN_STORERANGE:
5740 smsg("%s%4d STORERANGE", pfx, current);
5741 break;
5742
5743 // constants
5744 case ISN_PUSHNR:
5745 smsg("%s%4d PUSHNR %lld", pfx, current,
5746 (varnumber_T)(iptr->isn_arg.number));
5747 break;
5748 case ISN_PUSHBOOL:
5749 case ISN_PUSHSPEC:
5750 smsg("%s%4d PUSH %s", pfx, current,
5751 get_var_special_name(iptr->isn_arg.number));
5752 break;
5753 case ISN_PUSHF:
5754#ifdef FEAT_FLOAT
5755 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5756#endif
5757 break;
5758 case ISN_PUSHS:
5759 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5760 break;
5761 case ISN_PUSHBLOB:
5762 {
5763 char_u *r;
5764 char_u numbuf[NUMBUFLEN];
5765 char_u *tofree;
5766
5767 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5768 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5769 vim_free(tofree);
5770 }
5771 break;
5772 case ISN_PUSHFUNC:
5773 {
5774 char *name = (char *)iptr->isn_arg.string;
5775
5776 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5777 name == NULL ? "[none]" : name);
5778 }
5779 break;
5780 case ISN_PUSHCHANNEL:
5781#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005782 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005783#endif
5784 break;
5785 case ISN_PUSHJOB:
5786#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005787 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005788#endif
5789 break;
5790 case ISN_PUSHEXC:
5791 smsg("%s%4d PUSH v:exception", pfx, current);
5792 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005793 case ISN_AUTOLOAD:
5794 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5795 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005796 case ISN_UNLET:
5797 smsg("%s%4d UNLET%s %s", pfx, current,
5798 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5799 iptr->isn_arg.unlet.ul_name);
5800 break;
5801 case ISN_UNLETENV:
5802 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5803 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5804 iptr->isn_arg.unlet.ul_name);
5805 break;
5806 case ISN_UNLETINDEX:
5807 smsg("%s%4d UNLETINDEX", pfx, current);
5808 break;
5809 case ISN_UNLETRANGE:
5810 smsg("%s%4d UNLETRANGE", pfx, current);
5811 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005812 case ISN_LOCKUNLOCK:
5813 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5814 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005815 case ISN_LOCKCONST:
5816 smsg("%s%4d LOCKCONST", pfx, current);
5817 break;
5818 case ISN_NEWLIST:
5819 smsg("%s%4d NEWLIST size %lld", pfx, current,
5820 (varnumber_T)(iptr->isn_arg.number));
5821 break;
5822 case ISN_NEWDICT:
5823 smsg("%s%4d NEWDICT size %lld", pfx, current,
5824 (varnumber_T)(iptr->isn_arg.number));
5825 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005826 case ISN_NEWPARTIAL:
5827 smsg("%s%4d NEWPARTIAL", pfx, current);
5828 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005829
5830 // function call
5831 case ISN_BCALL:
5832 {
5833 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5834
5835 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5836 internal_func_name(cbfunc->cbf_idx),
5837 cbfunc->cbf_argcount);
5838 }
5839 break;
5840 case ISN_DCALL:
5841 {
5842 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5843 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5844 + cdfunc->cdf_idx;
5845
5846 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005847 printable_func_name(df->df_ufunc),
5848 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005849 }
5850 break;
5851 case ISN_UCALL:
5852 {
5853 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5854
5855 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5856 cufunc->cuf_name, cufunc->cuf_argcount);
5857 }
5858 break;
5859 case ISN_PCALL:
5860 {
5861 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5862
5863 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5864 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5865 }
5866 break;
5867 case ISN_PCALL_END:
5868 smsg("%s%4d PCALL end", pfx, current);
5869 break;
5870 case ISN_RETURN:
5871 smsg("%s%4d RETURN", pfx, current);
5872 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005873 case ISN_RETURN_VOID:
5874 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005875 break;
5876 case ISN_FUNCREF:
5877 {
5878 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005879 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005880
Bram Moolenaar38453522021-11-28 22:00:12 +00005881 if (funcref->fr_func_name == NULL)
5882 {
5883 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5884 + funcref->fr_dfunc_idx;
5885 name = df->df_ufunc->uf_name;
5886 }
5887 else
5888 name = funcref->fr_func_name;
5889 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005890 }
5891 break;
5892
5893 case ISN_NEWFUNC:
5894 {
5895 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5896
5897 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5898 newfunc->nf_lambda, newfunc->nf_global);
5899 }
5900 break;
5901
5902 case ISN_DEF:
5903 {
5904 char_u *name = iptr->isn_arg.string;
5905
5906 smsg("%s%4d DEF %s", pfx, current,
5907 name == NULL ? (char_u *)"" : name);
5908 }
5909 break;
5910
5911 case ISN_JUMP:
5912 {
5913 char *when = "?";
5914
5915 switch (iptr->isn_arg.jump.jump_when)
5916 {
5917 case JUMP_ALWAYS:
5918 when = "JUMP";
5919 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005920 case JUMP_NEVER:
5921 iemsg("JUMP_NEVER should not be used");
5922 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005923 case JUMP_AND_KEEP_IF_TRUE:
5924 when = "JUMP_AND_KEEP_IF_TRUE";
5925 break;
5926 case JUMP_IF_FALSE:
5927 when = "JUMP_IF_FALSE";
5928 break;
5929 case JUMP_AND_KEEP_IF_FALSE:
5930 when = "JUMP_AND_KEEP_IF_FALSE";
5931 break;
5932 case JUMP_IF_COND_FALSE:
5933 when = "JUMP_IF_COND_FALSE";
5934 break;
5935 case JUMP_IF_COND_TRUE:
5936 when = "JUMP_IF_COND_TRUE";
5937 break;
5938 }
5939 smsg("%s%4d %s -> %d", pfx, current, when,
5940 iptr->isn_arg.jump.jump_where);
5941 }
5942 break;
5943
5944 case ISN_JUMP_IF_ARG_SET:
5945 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5946 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5947 iptr->isn_arg.jump.jump_where);
5948 break;
5949
5950 case ISN_FOR:
5951 {
5952 forloop_T *forloop = &iptr->isn_arg.forloop;
5953
5954 smsg("%s%4d FOR $%d -> %d", pfx, current,
5955 forloop->for_idx, forloop->for_end);
5956 }
5957 break;
5958
5959 case ISN_TRY:
5960 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005961 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005962
5963 if (try->try_ref->try_finally == 0)
5964 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5965 pfx, current,
5966 try->try_ref->try_catch,
5967 try->try_ref->try_endtry);
5968 else
5969 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5970 pfx, current,
5971 try->try_ref->try_catch,
5972 try->try_ref->try_finally,
5973 try->try_ref->try_endtry);
5974 }
5975 break;
5976 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005977 smsg("%s%4d CATCH", pfx, current);
5978 break;
5979 case ISN_TRYCONT:
5980 {
5981 trycont_T *trycont = &iptr->isn_arg.trycont;
5982
5983 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5984 trycont->tct_levels,
5985 trycont->tct_levels == 1 ? "" : "s",
5986 trycont->tct_where);
5987 }
5988 break;
5989 case ISN_FINALLY:
5990 smsg("%s%4d FINALLY", pfx, current);
5991 break;
5992 case ISN_ENDTRY:
5993 smsg("%s%4d ENDTRY", pfx, current);
5994 break;
5995 case ISN_THROW:
5996 smsg("%s%4d THROW", pfx, current);
5997 break;
5998
5999 // expression operations on number
6000 case ISN_OPNR:
6001 case ISN_OPFLOAT:
6002 case ISN_OPANY:
6003 {
6004 char *what;
6005 char *ins;
6006
6007 switch (iptr->isn_arg.op.op_type)
6008 {
6009 case EXPR_MULT: what = "*"; break;
6010 case EXPR_DIV: what = "/"; break;
6011 case EXPR_REM: what = "%"; break;
6012 case EXPR_SUB: what = "-"; break;
6013 case EXPR_ADD: what = "+"; break;
6014 default: what = "???"; break;
6015 }
6016 switch (iptr->isn_type)
6017 {
6018 case ISN_OPNR: ins = "OPNR"; break;
6019 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6020 case ISN_OPANY: ins = "OPANY"; break;
6021 default: ins = "???"; break;
6022 }
6023 smsg("%s%4d %s %s", pfx, current, ins, what);
6024 }
6025 break;
6026
6027 case ISN_COMPAREBOOL:
6028 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006029 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006030 case ISN_COMPARENR:
6031 case ISN_COMPAREFLOAT:
6032 case ISN_COMPARESTRING:
6033 case ISN_COMPAREBLOB:
6034 case ISN_COMPARELIST:
6035 case ISN_COMPAREDICT:
6036 case ISN_COMPAREFUNC:
6037 case ISN_COMPAREANY:
6038 {
6039 char *p;
6040 char buf[10];
6041 char *type;
6042
6043 switch (iptr->isn_arg.op.op_type)
6044 {
6045 case EXPR_EQUAL: p = "=="; break;
6046 case EXPR_NEQUAL: p = "!="; break;
6047 case EXPR_GREATER: p = ">"; break;
6048 case EXPR_GEQUAL: p = ">="; break;
6049 case EXPR_SMALLER: p = "<"; break;
6050 case EXPR_SEQUAL: p = "<="; break;
6051 case EXPR_MATCH: p = "=~"; break;
6052 case EXPR_IS: p = "is"; break;
6053 case EXPR_ISNOT: p = "isnot"; break;
6054 case EXPR_NOMATCH: p = "!~"; break;
6055 default: p = "???"; break;
6056 }
6057 STRCPY(buf, p);
6058 if (iptr->isn_arg.op.op_ic == TRUE)
6059 strcat(buf, "?");
6060 switch(iptr->isn_type)
6061 {
6062 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6063 case ISN_COMPARESPECIAL:
6064 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006065 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006066 case ISN_COMPARENR: type = "COMPARENR"; break;
6067 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6068 case ISN_COMPARESTRING:
6069 type = "COMPARESTRING"; break;
6070 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6071 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6072 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6073 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6074 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6075 default: type = "???"; break;
6076 }
6077
6078 smsg("%s%4d %s %s", pfx, current, type, buf);
6079 }
6080 break;
6081
6082 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6083 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6084
6085 // expression operations
6086 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
6087 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6088 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6089 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6090 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6091 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6092 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6093 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6094 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6095 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6096 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6097 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006098 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006099 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6100 iptr->isn_arg.getitem.gi_index,
6101 iptr->isn_arg.getitem.gi_with_op ?
6102 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006103 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6104 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6105 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006106 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6107 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6108
Bram Moolenaar4c137212021-04-19 16:48:48 +02006109 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6110
6111 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6112 case ISN_CHECKTYPE:
6113 {
6114 checktype_T *ct = &iptr->isn_arg.type;
6115 char *tofree;
6116
6117 if (ct->ct_arg_idx == 0)
6118 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6119 type_name(ct->ct_type, &tofree),
6120 (int)ct->ct_off);
6121 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006122 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6123 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006124 type_name(ct->ct_type, &tofree),
6125 (int)ct->ct_off,
6126 (int)ct->ct_arg_idx);
6127 vim_free(tofree);
6128 break;
6129 }
6130 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6131 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6132 iptr->isn_arg.checklen.cl_min_len);
6133 break;
6134 case ISN_SETTYPE:
6135 {
6136 char *tofree;
6137
6138 smsg("%s%4d SETTYPE %s", pfx, current,
6139 type_name(iptr->isn_arg.type.ct_type, &tofree));
6140 vim_free(tofree);
6141 break;
6142 }
6143 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006144 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6145 smsg("%s%4d INVERT %d (!val)", pfx, current,
6146 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006147 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006148 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6149 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006150 break;
6151 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006152 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006153 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006154 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6155 pfx, current,
6156 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006157 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006158 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6159 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006160 break;
6161 case ISN_PUT:
6162 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6163 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006164 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006165 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6166 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006167 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006168 else
6169 smsg("%s%4d PUT %c %ld", pfx, current,
6170 iptr->isn_arg.put.put_regname,
6171 (long)iptr->isn_arg.put.put_lnum);
6172 break;
6173
Bram Moolenaar4c137212021-04-19 16:48:48 +02006174 case ISN_CMDMOD:
6175 {
6176 char_u *buf;
6177 size_t len = produce_cmdmods(
6178 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6179
6180 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006181 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006182 {
6183 (void)produce_cmdmods(
6184 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6185 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6186 vim_free(buf);
6187 }
6188 break;
6189 }
6190 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6191
6192 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006193 smsg("%s%4d PROFILE START line %d", pfx, current,
6194 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006195 break;
6196
6197 case ISN_PROF_END:
6198 smsg("%s%4d PROFILE END", pfx, current);
6199 break;
6200
Bram Moolenaare99d4222021-06-13 14:01:26 +02006201 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006202 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6203 iptr->isn_arg.debug.dbg_break_lnum + 1,
6204 iptr->isn_lnum,
6205 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006206 break;
6207
Bram Moolenaar4c137212021-04-19 16:48:48 +02006208 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6209 iptr->isn_arg.unpack.unp_count,
6210 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6211 break;
6212 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6213 iptr->isn_arg.shuffle.shfl_item,
6214 iptr->isn_arg.shuffle.shfl_up);
6215 break;
6216 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6217
6218 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6219 return;
6220 }
6221
6222 out_flush(); // output one line at a time
6223 ui_breakcheck();
6224 if (got_int)
6225 break;
6226 }
6227}
6228
6229/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006230 * Handle command line completion for the :disassemble command.
6231 */
6232 void
6233set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6234{
6235 char_u *p;
6236
6237 // Default: expand user functions, "debug" and "profile"
6238 xp->xp_context = EXPAND_DISASSEMBLE;
6239 xp->xp_pattern = arg;
6240
6241 // first argument already typed: only user function names
6242 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6243 {
6244 xp->xp_context = EXPAND_USER_FUNC;
6245 xp->xp_pattern = skipwhite(p);
6246 }
6247}
6248
6249/*
6250 * Function given to ExpandGeneric() to obtain the list of :disassemble
6251 * arguments.
6252 */
6253 char_u *
6254get_disassemble_argument(expand_T *xp, int idx)
6255{
6256 if (idx == 0)
6257 return (char_u *)"debug";
6258 if (idx == 1)
6259 return (char_u *)"profile";
6260 return get_user_func_name(xp, idx - 2);
6261}
6262
6263/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006264 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006265 * We don't really need this at runtime, but we do have tests that require it,
6266 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 */
6268 void
6269ex_disassemble(exarg_T *eap)
6270{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006271 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006272 char_u *fname;
6273 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274 dfunc_T *dfunc;
6275 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006276 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006277 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006278 compiletype_T compile_type = CT_NONE;
6279
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006280 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006281 {
6282 compile_type = CT_PROFILE;
6283 arg = skipwhite(arg + 7);
6284 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006285 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006286 {
6287 compile_type = CT_DEBUG;
6288 arg = skipwhite(arg + 5);
6289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006291 if (STRNCMP(arg, "<lambda>", 8) == 0)
6292 {
6293 arg += 8;
6294 (void)getdigits(&arg);
6295 fname = vim_strnsave(eap->arg, arg - eap->arg);
6296 }
6297 else
6298 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006299 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006300 if (fname == NULL)
6301 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006302 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006303 return;
6304 }
6305
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006306 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006307 if (ufunc == NULL)
6308 {
6309 char_u *p = untrans_function_name(fname);
6310
6311 if (p != NULL)
6312 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006313 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006314 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006315 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316 if (ufunc == NULL)
6317 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006318 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 return;
6320 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006321 if (func_needs_compiling(ufunc, compile_type)
6322 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006323 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006324 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006326 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327 return;
6328 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006329 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330
6331 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006332 switch (compile_type)
6333 {
6334 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006335#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006336 instr = dfunc->df_instr_prof;
6337 instr_count = dfunc->df_instr_prof_count;
6338 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006339#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006340 // FALLTHROUGH
6341 case CT_NONE:
6342 instr = dfunc->df_instr;
6343 instr_count = dfunc->df_instr_count;
6344 break;
6345 case CT_DEBUG:
6346 instr = dfunc->df_instr_debug;
6347 instr_count = dfunc->df_instr_debug_count;
6348 break;
6349 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350
Bram Moolenaar4c137212021-04-19 16:48:48 +02006351 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352}
6353
6354/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006355 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356 * list, etc. Mostly like what JavaScript does, except that empty list and
6357 * empty dictionary are FALSE.
6358 */
6359 int
6360tv2bool(typval_T *tv)
6361{
6362 switch (tv->v_type)
6363 {
6364 case VAR_NUMBER:
6365 return tv->vval.v_number != 0;
6366 case VAR_FLOAT:
6367#ifdef FEAT_FLOAT
6368 return tv->vval.v_float != 0.0;
6369#else
6370 break;
6371#endif
6372 case VAR_PARTIAL:
6373 return tv->vval.v_partial != NULL;
6374 case VAR_FUNC:
6375 case VAR_STRING:
6376 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6377 case VAR_LIST:
6378 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6379 case VAR_DICT:
6380 return tv->vval.v_dict != NULL
6381 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6382 case VAR_BOOL:
6383 case VAR_SPECIAL:
6384 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6385 case VAR_JOB:
6386#ifdef FEAT_JOB_CHANNEL
6387 return tv->vval.v_job != NULL;
6388#else
6389 break;
6390#endif
6391 case VAR_CHANNEL:
6392#ifdef FEAT_JOB_CHANNEL
6393 return tv->vval.v_channel != NULL;
6394#else
6395 break;
6396#endif
6397 case VAR_BLOB:
6398 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6399 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006400 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006402 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403 break;
6404 }
6405 return FALSE;
6406}
6407
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006408 void
6409emsg_using_string_as(typval_T *tv, int as_number)
6410{
6411 semsg(_(as_number ? e_using_string_as_number_str
6412 : e_using_string_as_bool_str),
6413 tv->vval.v_string == NULL
6414 ? (char_u *)"" : tv->vval.v_string);
6415}
6416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006417/*
6418 * If "tv" is a string give an error and return FAIL.
6419 */
6420 int
6421check_not_string(typval_T *tv)
6422{
6423 if (tv->v_type == VAR_STRING)
6424 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006425 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006426 clear_tv(tv);
6427 return FAIL;
6428 }
6429 return OK;
6430}
6431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006432
6433#endif // FEAT_EVAL