blob: 99135eadd25aff858933ecc6dcf6f54cc43abed5 [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
1517 if (!sv->sv_export && sref->sref_sid != current_sctx.sc_sid)
1518 {
1519 if (dfunc != NULL)
1520 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1521 return NULL;
1522 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001523 return sv;
1524}
1525
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001526/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001527 * Function passed to do_cmdline() for splitting a script joined by NL
1528 * characters.
1529 */
1530 static char_u *
1531get_split_sourceline(
1532 int c UNUSED,
1533 void *cookie,
1534 int indent UNUSED,
1535 getline_opt_T options UNUSED)
1536{
1537 source_cookie_T *sp = (source_cookie_T *)cookie;
1538 char_u *p;
1539 char_u *line;
1540
Bram Moolenaar20677332021-06-06 17:02:53 +02001541 p = vim_strchr(sp->nextline, '\n');
1542 if (p == NULL)
1543 {
1544 line = vim_strsave(sp->nextline);
1545 sp->nextline += STRLEN(sp->nextline);
1546 }
1547 else
1548 {
1549 line = vim_strnsave(sp->nextline, p - sp->nextline);
1550 sp->nextline = p + 1;
1551 }
1552 return line;
1553}
1554
1555/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 * Execute a function by "name".
1557 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001558 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 */
1560 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001561call_eval_func(
1562 char_u *name,
1563 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001564 ectx_T *ectx,
1565 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566{
Bram Moolenaared677f52020-08-12 16:38:10 +02001567 int called_emsg_before = called_emsg;
1568 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001570 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001571 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001573 dictitem_T *v;
1574
1575 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001576 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1577 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001578 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001579 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001580 return FAIL;
1581 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001582 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001584 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585}
1586
1587/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001588 * When a function reference is used, fill a partial with the information
1589 * needed, especially when it is used as a closure.
1590 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001591 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001592fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1593{
1594 pt->pt_func = ufunc;
1595 pt->pt_refcount = 1;
1596
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001597 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001598 {
1599 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1600 + ectx->ec_dfunc_idx;
1601
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001602 // The closure may need to find arguments and local variables in the
1603 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001604 pt->pt_outer.out_stack = &ectx->ec_stack;
1605 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001606 if (ectx->ec_outer_ref != NULL)
1607 {
1608 // The current context already has a context, link to that one.
1609 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1610 if (ectx->ec_outer_ref->or_partial != NULL)
1611 {
1612 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1613 ++pt->pt_outer.out_up_partial->pt_refcount;
1614 }
1615 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001616
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001617 // If this function returns and the closure is still being used, we
1618 // need to make a copy of the context (arguments and local variables).
1619 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001620 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001621 {
1622 vim_free(pt);
1623 return FAIL;
1624 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001625 // Extra variable keeps the count of closures created in the current
1626 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001627 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1628 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1629
1630 ((partial_T **)ectx->ec_funcrefs.ga_data)
1631 [ectx->ec_funcrefs.ga_len] = pt;
1632 ++pt->pt_refcount;
1633 ++ectx->ec_funcrefs.ga_len;
1634 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001635 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001636 return OK;
1637}
1638
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001639/*
1640 * Execute iptr->isn_arg.string as an Ex command.
1641 */
1642 static int
1643exec_command(isn_T *iptr)
1644{
1645 source_cookie_T cookie;
1646
1647 SOURCING_LNUM = iptr->isn_lnum;
1648 // Pass getsourceline to get an error for a missing ":end"
1649 // command.
1650 CLEAR_FIELD(cookie);
1651 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1652 if (do_cmdline(iptr->isn_arg.string,
1653 getsourceline, &cookie,
1654 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1655 || did_emsg)
1656 return FAIL;
1657 return OK;
1658}
1659
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001660// used for v_instr of typval of VAR_INSTR
1661struct instr_S {
1662 ectx_T *instr_ectx;
1663 isn_T *instr_instr;
1664};
1665
Bram Moolenaar4c137212021-04-19 16:48:48 +02001666// used for substitute_instr
1667typedef struct subs_expr_S {
1668 ectx_T *subs_ectx;
1669 isn_T *subs_instr;
1670 int subs_status;
1671} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672
1673// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001674#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675
1676// Get pointer to item at the bottom of the stack, -1 is the bottom.
1677#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001678#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 +01001679
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001680// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001681#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 +01001682
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001683// Set when calling do_debug().
1684static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001685static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001686
1687/*
1688 * When debugging lookup "name" and return the typeval.
1689 * When not found return NULL.
1690 */
1691 typval_T *
1692lookup_debug_var(char_u *name)
1693{
1694 int idx;
1695 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001696 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001697 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001698 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001699
1700 if (ectx == NULL)
1701 return NULL;
1702 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1703
1704 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001705 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001706 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001707 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1708
1709 // the variable name may be NULL when not available in this block
1710 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001711 return STACK_TV_VAR(idx);
1712 }
1713
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001714 // Go through argument names.
1715 ufunc = dfunc->df_ufunc;
1716 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1717 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1718 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1719 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1720 - varargs_off + idx);
1721 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1722 return STACK_TV(ectx->ec_frame_idx - 1);
1723
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001724 return NULL;
1725}
1726
Bram Moolenaar26a44842021-09-02 18:49:06 +02001727/*
1728 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1729 * breakpoint was set in that function or when there is any expression.
1730 */
1731 int
1732may_break_in_function(ufunc_T *ufunc)
1733{
1734 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1735}
1736
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001737 static void
1738handle_debug(isn_T *iptr, ectx_T *ectx)
1739{
1740 char_u *line;
1741 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1742 + ectx->ec_dfunc_idx)->df_ufunc;
1743 isn_T *ni;
1744 int end_lnum = iptr->isn_lnum;
1745 garray_T ga;
1746 int lnum;
1747
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001748 if (ex_nesting_level > debug_break_level)
1749 {
1750 linenr_T breakpoint;
1751
Bram Moolenaar26a44842021-09-02 18:49:06 +02001752 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001753 return;
1754
1755 // check for the next breakpoint if needed
1756 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001757 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001758 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1759 return;
1760 }
1761
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001762 SOURCING_LNUM = iptr->isn_lnum;
1763 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001764 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001765
1766 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1767 if (ni->isn_type == ISN_DEBUG
1768 || ni->isn_type == ISN_RETURN
1769 || ni->isn_type == ISN_RETURN_VOID)
1770 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001771 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001772 break;
1773 }
1774
1775 if (end_lnum > iptr->isn_lnum)
1776 {
1777 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001778 for (lnum = iptr->isn_lnum; lnum < end_lnum
1779 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001780 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001781 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001782
Bram Moolenaar303215d2021-07-07 20:10:43 +02001783 if (p == NULL)
1784 continue; // left over from continuation line
1785 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001786 if (*p == '#')
1787 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001788 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001789 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1790 if (STRNCMP(p, "def ", 4) == 0)
1791 break;
1792 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001793 line = ga_concat_strings(&ga, " ");
1794 vim_free(ga.ga_data);
1795 }
1796 else
1797 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001798
Dominique Pelle6e969552021-06-17 13:53:41 +02001799 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001800 debug_context = NULL;
1801
1802 if (end_lnum > iptr->isn_lnum)
1803 vim_free(line);
1804}
1805
Bram Moolenaar4c137212021-04-19 16:48:48 +02001806/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001807 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001808 * Returns OK, FAIL or NOTDONE (uncatchable error).
1809 */
1810 static int
1811execute_storeindex(isn_T *iptr, ectx_T *ectx)
1812{
1813 vartype_T dest_type = iptr->isn_arg.vartype;
1814 typval_T *tv;
1815 typval_T *tv_idx = STACK_TV_BOT(-2);
1816 typval_T *tv_dest = STACK_TV_BOT(-1);
1817 int status = OK;
1818
1819 // Stack contains:
1820 // -3 value to be stored
1821 // -2 index
1822 // -1 dict or list
1823 tv = STACK_TV_BOT(-3);
1824 SOURCING_LNUM = iptr->isn_lnum;
1825 if (dest_type == VAR_ANY)
1826 {
1827 dest_type = tv_dest->v_type;
1828 if (dest_type == VAR_DICT)
1829 status = do_2string(tv_idx, TRUE, FALSE);
1830 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1831 {
1832 emsg(_(e_number_expected));
1833 status = FAIL;
1834 }
1835 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001836
1837 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001838 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001839 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001840 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001841 long lidx = (long)tv_idx->vval.v_number;
1842 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001843
Bram Moolenaare08be092022-02-17 13:08:26 +00001844 if (list == NULL)
1845 {
1846 emsg(_(e_list_not_set));
1847 return FAIL;
1848 }
1849 if (lidx < 0 && list->lv_len + lidx >= 0)
1850 // negative index is relative to the end
1851 lidx = list->lv_len + lidx;
1852 if (lidx < 0 || lidx > list->lv_len)
1853 {
1854 semsg(_(e_list_index_out_of_range_nr), lidx);
1855 return FAIL;
1856 }
1857 if (lidx < list->lv_len)
1858 {
1859 listitem_T *li = list_find(list, lidx);
1860
1861 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001862 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001863 return FAIL;
1864 // overwrite existing list item
1865 clear_tv(&li->li_tv);
1866 li->li_tv = *tv;
1867 }
1868 else
1869 {
1870 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1871 return FAIL;
1872 // append to list, only fails when out of memory
1873 if (list_append_tv(list, tv) == FAIL)
1874 return NOTDONE;
1875 clear_tv(tv);
1876 }
1877 }
1878 else if (dest_type == VAR_DICT)
1879 {
1880 char_u *key = tv_idx->vval.v_string;
1881 dict_T *dict = tv_dest->vval.v_dict;
1882 dictitem_T *di;
1883
1884 SOURCING_LNUM = iptr->isn_lnum;
1885 if (dict == NULL)
1886 {
1887 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001888 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001889 }
1890 if (key == NULL)
1891 key = (char_u *)"";
1892 di = dict_find(dict, key, -1);
1893 if (di != NULL)
1894 {
1895 if (error_if_locked(di->di_tv.v_lock,
1896 e_cannot_change_dict_item))
1897 return FAIL;
1898 // overwrite existing value
1899 clear_tv(&di->di_tv);
1900 di->di_tv = *tv;
1901 }
1902 else
1903 {
1904 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1905 return FAIL;
1906 // add to dict, only fails when out of memory
1907 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1908 return NOTDONE;
1909 clear_tv(tv);
1910 }
1911 }
1912 else if (dest_type == VAR_BLOB)
1913 {
1914 long lidx = (long)tv_idx->vval.v_number;
1915 blob_T *blob = tv_dest->vval.v_blob;
1916 varnumber_T nr;
1917 int error = FALSE;
1918 int len;
1919
1920 if (blob == NULL)
1921 {
1922 emsg(_(e_blob_not_set));
1923 return FAIL;
1924 }
1925 len = blob_len(blob);
1926 if (lidx < 0 && len + lidx >= 0)
1927 // negative index is relative to the end
1928 lidx = len + lidx;
1929
1930 // Can add one byte at the end.
1931 if (lidx < 0 || lidx > len)
1932 {
1933 semsg(_(e_blob_index_out_of_range_nr), lidx);
1934 return FAIL;
1935 }
1936 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1937 return FAIL;
1938 nr = tv_get_number_chk(tv, &error);
1939 if (error)
1940 return FAIL;
1941 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001942 }
1943 else
1944 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001945 status = FAIL;
1946 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001947 }
1948 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001949
1950 clear_tv(tv_idx);
1951 clear_tv(tv_dest);
1952 ectx->ec_stack.ga_len -= 3;
1953 if (status == FAIL)
1954 {
1955 clear_tv(tv);
1956 return FAIL;
1957 }
1958 return OK;
1959}
1960
1961/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001962 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001963 */
1964 static int
1965execute_storerange(isn_T *iptr, ectx_T *ectx)
1966{
1967 typval_T *tv;
1968 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1969 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1970 typval_T *tv_dest = STACK_TV_BOT(-1);
1971 int status = OK;
1972
1973 // Stack contains:
1974 // -4 value to be stored
1975 // -3 first index or "none"
1976 // -2 second index or "none"
1977 // -1 destination list or blob
1978 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001979 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001980 if (tv_dest->v_type == VAR_LIST)
1981 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001982 long n1;
1983 long n2;
1984 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001985
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001986 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
1987 if (tv_idx2->v_type == VAR_SPECIAL
1988 && tv_idx2->vval.v_number == VVAL_NONE)
1989 n2 = list_len(tv_dest->vval.v_list) - 1;
1990 else
1991 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
1992
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001993 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001994 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001995 status = FAIL;
1996 else
1997 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001998 status = check_range_index_two(tv_dest->vval.v_list,
1999 &n1, li1, &n2, FALSE);
2000 if (status != FAIL)
2001 status = list_assign_range(
2002 tv_dest->vval.v_list,
2003 tv->vval.v_list,
2004 n1,
2005 n2,
2006 tv_idx2->v_type == VAR_SPECIAL,
2007 (char_u *)"=",
2008 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002009 }
2010 }
2011 else if (tv_dest->v_type == VAR_BLOB)
2012 {
2013 varnumber_T n1;
2014 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002015 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002016
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002017 n1 = tv_get_number_chk(tv_idx1, NULL);
2018 if (tv_idx2->v_type == VAR_SPECIAL
2019 && tv_idx2->vval.v_number == VVAL_NONE)
2020 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2021 else
2022 n2 = tv_get_number_chk(tv_idx2, NULL);
2023 bloblen = blob_len(tv_dest->vval.v_blob);
2024
2025 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2026 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002027 status = FAIL;
2028 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002029 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002030 }
2031 else
2032 {
2033 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002034 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002035 }
2036
2037 clear_tv(tv_idx1);
2038 clear_tv(tv_idx2);
2039 clear_tv(tv_dest);
2040 ectx->ec_stack.ga_len -= 4;
2041 clear_tv(tv);
2042
2043 return status;
2044}
2045
2046/*
2047 * Unlet item in list or dict variable.
2048 */
2049 static int
2050execute_unletindex(isn_T *iptr, ectx_T *ectx)
2051{
2052 typval_T *tv_idx = STACK_TV_BOT(-2);
2053 typval_T *tv_dest = STACK_TV_BOT(-1);
2054 int status = OK;
2055
2056 // Stack contains:
2057 // -2 index
2058 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002059 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002060 if (tv_dest->v_type == VAR_DICT)
2061 {
2062 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002063 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002064 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002065 semsg(_(e_expected_str_but_got_str),
2066 vartype_name(VAR_STRING),
2067 vartype_name(tv_idx->v_type));
2068 status = FAIL;
2069 }
2070 else
2071 {
2072 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002073 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002074 dictitem_T *di = NULL;
2075
2076 if (d != NULL && value_check_lock(
2077 d->dv_lock, NULL, FALSE))
2078 status = FAIL;
2079 else
2080 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002081 if (tv_idx->v_type == VAR_STRING)
2082 {
2083 key = tv_idx->vval.v_string;
2084 if (key == NULL)
2085 key = (char_u *)"";
2086 }
2087 else
2088 {
2089 key = tv_get_string(tv_idx);
2090 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002091 if (d != NULL)
2092 di = dict_find(d, key, (int)STRLEN(key));
2093 if (di == NULL)
2094 {
2095 // NULL dict is equivalent to empty dict
2096 semsg(_(e_key_not_present_in_dictionary),
2097 key);
2098 status = FAIL;
2099 }
2100 else if (var_check_fixed(di->di_flags,
2101 NULL, FALSE)
2102 || var_check_ro(di->di_flags,
2103 NULL, FALSE))
2104 status = FAIL;
2105 else
2106 dictitem_remove(d, di);
2107 }
2108 }
2109 }
2110 else if (tv_dest->v_type == VAR_LIST)
2111 {
2112 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002113 if (check_for_number(tv_idx) == FAIL)
2114 {
2115 status = FAIL;
2116 }
2117 else
2118 {
2119 list_T *l = tv_dest->vval.v_list;
2120 long n = (long)tv_idx->vval.v_number;
2121
2122 if (l != NULL && value_check_lock(
2123 l->lv_lock, NULL, FALSE))
2124 status = FAIL;
2125 else
2126 {
2127 listitem_T *li = list_find(l, n);
2128
2129 if (li == NULL)
2130 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002131 semsg(_(e_list_index_out_of_range_nr), n);
2132 status = FAIL;
2133 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002134 else
2135 listitem_remove(l, li);
2136 }
2137 }
2138 }
2139 else
2140 {
2141 status = FAIL;
2142 semsg(_(e_cannot_index_str),
2143 vartype_name(tv_dest->v_type));
2144 }
2145
2146 clear_tv(tv_idx);
2147 clear_tv(tv_dest);
2148 ectx->ec_stack.ga_len -= 2;
2149
2150 return status;
2151}
2152
2153/*
2154 * Unlet a range of items in a list variable.
2155 */
2156 static int
2157execute_unletrange(isn_T *iptr, ectx_T *ectx)
2158{
2159 // Stack contains:
2160 // -3 index1
2161 // -2 index2
2162 // -1 dict or list
2163 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2164 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2165 typval_T *tv_dest = STACK_TV_BOT(-1);
2166 int status = OK;
2167
2168 if (tv_dest->v_type == VAR_LIST)
2169 {
2170 // indexes must be a number
2171 SOURCING_LNUM = iptr->isn_lnum;
2172 if (check_for_number(tv_idx1) == FAIL
2173 || (tv_idx2->v_type != VAR_SPECIAL
2174 && check_for_number(tv_idx2) == FAIL))
2175 {
2176 status = FAIL;
2177 }
2178 else
2179 {
2180 list_T *l = tv_dest->vval.v_list;
2181 long n1 = (long)tv_idx1->vval.v_number;
2182 long n2 = tv_idx2->v_type == VAR_SPECIAL
2183 ? 0 : (long)tv_idx2->vval.v_number;
2184 listitem_T *li;
2185
2186 li = list_find_index(l, &n1);
2187 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002188 {
2189 semsg(_(e_list_index_out_of_range_nr),
2190 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002191 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002192 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002193 else
2194 {
2195 if (n1 < 0)
2196 n1 = list_idx_of_item(l, li);
2197 if (n2 < 0)
2198 {
2199 listitem_T *li2 = list_find(l, n2);
2200
2201 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002202 {
2203 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002204 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002205 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002206 else
2207 n2 = list_idx_of_item(l, li2);
2208 }
2209 if (status != FAIL
2210 && tv_idx2->v_type != VAR_SPECIAL
2211 && n2 < n1)
2212 {
2213 semsg(_(e_list_index_out_of_range_nr), n2);
2214 status = FAIL;
2215 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002216 if (status != FAIL)
2217 list_unlet_range(l, li, n1,
2218 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002219 }
2220 }
2221 }
2222 else
2223 {
2224 status = FAIL;
2225 SOURCING_LNUM = iptr->isn_lnum;
2226 semsg(_(e_cannot_index_str),
2227 vartype_name(tv_dest->v_type));
2228 }
2229
2230 clear_tv(tv_idx1);
2231 clear_tv(tv_idx2);
2232 clear_tv(tv_dest);
2233 ectx->ec_stack.ga_len -= 3;
2234
2235 return status;
2236}
2237
2238/*
2239 * Top of a for loop.
2240 */
2241 static int
2242execute_for(isn_T *iptr, ectx_T *ectx)
2243{
2244 typval_T *tv;
2245 typval_T *ltv = STACK_TV_BOT(-1);
2246 typval_T *idxtv =
2247 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2248
2249 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2250 return FAIL;
2251 if (ltv->v_type == VAR_LIST)
2252 {
2253 list_T *list = ltv->vval.v_list;
2254
2255 // push the next item from the list
2256 ++idxtv->vval.v_number;
2257 if (list == NULL
2258 || idxtv->vval.v_number >= list->lv_len)
2259 {
2260 // past the end of the list, jump to "endfor"
2261 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2262 may_restore_cmdmod(&ectx->ec_funclocal);
2263 }
2264 else if (list->lv_first == &range_list_item)
2265 {
2266 // non-materialized range() list
2267 tv = STACK_TV_BOT(0);
2268 tv->v_type = VAR_NUMBER;
2269 tv->v_lock = 0;
2270 tv->vval.v_number = list_find_nr(
2271 list, idxtv->vval.v_number, NULL);
2272 ++ectx->ec_stack.ga_len;
2273 }
2274 else
2275 {
2276 listitem_T *li = list_find(list,
2277 idxtv->vval.v_number);
2278
2279 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2280 ++ectx->ec_stack.ga_len;
2281 }
2282 }
2283 else if (ltv->v_type == VAR_STRING)
2284 {
2285 char_u *str = ltv->vval.v_string;
2286
2287 // The index is for the last byte of the previous
2288 // character.
2289 ++idxtv->vval.v_number;
2290 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2291 {
2292 // past the end of the string, jump to "endfor"
2293 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2294 may_restore_cmdmod(&ectx->ec_funclocal);
2295 }
2296 else
2297 {
2298 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2299
2300 // Push the next character from the string.
2301 tv = STACK_TV_BOT(0);
2302 tv->v_type = VAR_STRING;
2303 tv->vval.v_string = vim_strnsave(
2304 str + idxtv->vval.v_number, clen);
2305 ++ectx->ec_stack.ga_len;
2306 idxtv->vval.v_number += clen - 1;
2307 }
2308 }
2309 else if (ltv->v_type == VAR_BLOB)
2310 {
2311 blob_T *blob = ltv->vval.v_blob;
2312
2313 // When we get here the first time make a copy of the
2314 // blob, so that the iteration still works when it is
2315 // changed.
2316 if (idxtv->vval.v_number == -1 && blob != NULL)
2317 {
2318 blob_copy(blob, ltv);
2319 blob_unref(blob);
2320 blob = ltv->vval.v_blob;
2321 }
2322
2323 // The index is for the previous byte.
2324 ++idxtv->vval.v_number;
2325 if (blob == NULL
2326 || idxtv->vval.v_number >= blob_len(blob))
2327 {
2328 // past the end of the blob, jump to "endfor"
2329 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2330 may_restore_cmdmod(&ectx->ec_funclocal);
2331 }
2332 else
2333 {
2334 // Push the next byte from the blob.
2335 tv = STACK_TV_BOT(0);
2336 tv->v_type = VAR_NUMBER;
2337 tv->vval.v_number = blob_get(blob,
2338 idxtv->vval.v_number);
2339 ++ectx->ec_stack.ga_len;
2340 }
2341 }
2342 else
2343 {
2344 semsg(_(e_for_loop_on_str_not_supported),
2345 vartype_name(ltv->v_type));
2346 return FAIL;
2347 }
2348 return OK;
2349}
2350
2351/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002352 * Load instruction for w:/b:/g:/t: variable.
2353 * "isn_type" is used instead of "iptr->isn_type".
2354 */
2355 static int
2356load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2357{
2358 dictitem_T *di = NULL;
2359 hashtab_T *ht = NULL;
2360 char namespace;
2361
2362 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2363 return NOTDONE;
2364 switch (isn_type)
2365 {
2366 case ISN_LOADG:
2367 ht = get_globvar_ht();
2368 namespace = 'g';
2369 break;
2370 case ISN_LOADB:
2371 ht = &curbuf->b_vars->dv_hashtab;
2372 namespace = 'b';
2373 break;
2374 case ISN_LOADW:
2375 ht = &curwin->w_vars->dv_hashtab;
2376 namespace = 'w';
2377 break;
2378 case ISN_LOADT:
2379 ht = &curtab->tp_vars->dv_hashtab;
2380 namespace = 't';
2381 break;
2382 default: // Cannot reach here
2383 return NOTDONE;
2384 }
2385 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2386
Bram Moolenaar06b77222022-01-25 15:51:56 +00002387 if (di == NULL)
2388 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002389 if (isn_type == ISN_LOADG)
2390 {
2391 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2392
2393 // g:Something could be a function
2394 if (ufunc != NULL)
2395 {
2396 typval_T *tv = STACK_TV_BOT(0);
2397
2398 ++ectx->ec_stack.ga_len;
2399 tv->v_type = VAR_FUNC;
2400 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2401 if (tv->vval.v_string == NULL)
2402 return FAIL;
2403 STRCPY(tv->vval.v_string, "g:");
2404 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2405 return OK;
2406 }
2407 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002408 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002409 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002410 // no check if the item exists in the script but
2411 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002412 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002413 else
2414 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002415 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002416 return FAIL;
2417 }
2418 else
2419 {
2420 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2421 ++ectx->ec_stack.ga_len;
2422 }
2423 return OK;
2424}
2425
2426/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002427 * Execute instructions in execution context "ectx".
2428 * Return OK or FAIL;
2429 */
2430 static int
2431exec_instructions(ectx_T *ectx)
2432{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002433 int ret = FAIL;
2434 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002435 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002436
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002437 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002438 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002439
Bram Moolenaarff652882021-05-16 15:24:49 +02002440 // Only catch exceptions in this instruction list.
2441 ectx->ec_trylevel_at_start = trylevel;
2442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 for (;;)
2444 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002445 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002447 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002448
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002449 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002450 {
2451 line_breakcheck();
2452 breakcheck_count = 0;
2453 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002454 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002455 {
2456 // Turn CTRL-C into an exception.
2457 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002458 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002459 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002460 did_throw = TRUE;
2461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002463 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002464 {
2465 // Turn an error message into an exception.
2466 did_emsg = FALSE;
2467 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002468 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002469 did_throw = TRUE;
2470 *msg_list = NULL;
2471 }
2472
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002473 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002475 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002476 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002477 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478
2479 // An exception jumps to the first catch, finally, or returns from
2480 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002481 while (index > 0)
2482 {
2483 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002484 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002485 break;
2486 // In the catch and finally block of this try we have to go up
2487 // one level.
2488 --index;
2489 trycmd = NULL;
2490 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002491 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002493 if (trycmd->tcd_in_catch)
2494 {
2495 // exception inside ":catch", jump to ":finally" once
2496 ectx->ec_iidx = trycmd->tcd_finally_idx;
2497 trycmd->tcd_finally_idx = 0;
2498 }
2499 else
2500 // jump to first ":catch"
2501 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002502 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002503 did_throw = FALSE; // don't come back here until :endtry
2504 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 }
2506 else
2507 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002508 // Not inside try or need to return from current functions.
2509 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002510 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002511 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002512 tv = STACK_TV_BOT(0);
2513 tv->v_type = VAR_NUMBER;
2514 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002515 ++ectx->ec_stack.ga_len;
2516 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002518 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002519 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002520 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002521 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 goto done;
2523 }
2524
Bram Moolenaar4c137212021-04-19 16:48:48 +02002525 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002526 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 }
2528 continue;
2529 }
2530
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002531 /*
2532 * Big switch on the instruction. Most compilers will be turning this
2533 * into an efficient lookup table, since the "case" values are an enum
2534 * with sequential numbers. It may look ugly, but it should be the
2535 * most efficient way.
2536 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002537 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 switch (iptr->isn_type)
2539 {
2540 // execute Ex command line
2541 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002542 if (exec_command(iptr) == FAIL)
2543 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 break;
2545
Bram Moolenaar20677332021-06-06 17:02:53 +02002546 // execute Ex command line split at NL characters.
2547 case ISN_EXEC_SPLIT:
2548 {
2549 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002550 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002551
2552 SOURCING_LNUM = iptr->isn_lnum;
2553 CLEAR_FIELD(cookie);
2554 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2555 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002556 line = get_split_sourceline(0, &cookie, 0, 0);
2557 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002558 get_split_sourceline, &cookie,
2559 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2560 == FAIL
2561 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002562 {
2563 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002564 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002565 }
2566 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002567 }
2568 break;
2569
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002570 // execute Ex command line that is only a range
2571 case ISN_EXECRANGE:
2572 {
2573 exarg_T ea;
2574 char *error = NULL;
2575
2576 CLEAR_FIELD(ea);
2577 ea.cmdidx = CMD_SIZE;
2578 ea.addr_type = ADDR_LINES;
2579 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002580 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002581 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002582 if (ea.cmd == NULL)
2583 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002584 // error is always NULL when using ADDR_LINES
2585 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002586 if (error != NULL)
2587 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002588 emsg(error);
2589 goto on_error;
2590 }
2591 }
2592 break;
2593
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002594 // Evaluate an expression with legacy syntax, push it onto the
2595 // stack.
2596 case ISN_LEGACY_EVAL:
2597 {
2598 char_u *arg = iptr->isn_arg.string;
2599 int res;
2600 int save_flags = cmdmod.cmod_flags;
2601
Bram Moolenaar35578162021-08-02 19:10:38 +02002602 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002603 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002604 tv = STACK_TV_BOT(0);
2605 init_tv(tv);
2606 cmdmod.cmod_flags |= CMOD_LEGACY;
2607 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2608 cmdmod.cmod_flags = save_flags;
2609 if (res == FAIL)
2610 goto on_error;
2611 ++ectx->ec_stack.ga_len;
2612 }
2613 break;
2614
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002615 // push typeval VAR_INSTR with instructions to be executed
2616 case ISN_INSTR:
2617 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002618 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002619 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002620 tv = STACK_TV_BOT(0);
2621 tv->vval.v_instr = ALLOC_ONE(instr_T);
2622 if (tv->vval.v_instr == NULL)
2623 goto on_error;
2624 ++ectx->ec_stack.ga_len;
2625
2626 tv->v_type = VAR_INSTR;
2627 tv->vval.v_instr->instr_ectx = ectx;
2628 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2629 }
2630 break;
2631
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002632 case ISN_SOURCE:
2633 {
2634 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
2635
2636 if (si->sn_state == SN_STATE_NOT_LOADED)
2637 {
2638 SOURCING_LNUM = iptr->isn_lnum;
2639 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL)
2640 == FAIL)
Bram Moolenaar10611952022-04-03 21:11:34 +01002641 {
Bram Moolenaar242c1522022-04-03 21:52:51 +01002642 semsg(_(e_cant_open_file_str), si->sn_name);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002643 goto on_error;
Bram Moolenaar10611952022-04-03 21:11:34 +01002644 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002645 }
2646 }
2647 break;
2648
Bram Moolenaar4c137212021-04-19 16:48:48 +02002649 // execute :substitute with an expression
2650 case ISN_SUBSTITUTE:
2651 {
2652 subs_T *subs = &iptr->isn_arg.subs;
2653 source_cookie_T cookie;
2654 struct subs_expr_S *save_instr = substitute_instr;
2655 struct subs_expr_S subs_instr;
2656 int res;
2657
2658 subs_instr.subs_ectx = ectx;
2659 subs_instr.subs_instr = subs->subs_instr;
2660 subs_instr.subs_status = OK;
2661 substitute_instr = &subs_instr;
2662
2663 SOURCING_LNUM = iptr->isn_lnum;
2664 // This is very much like ISN_EXEC
2665 CLEAR_FIELD(cookie);
2666 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2667 res = do_cmdline(subs->subs_cmd,
2668 getsourceline, &cookie,
2669 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2670 substitute_instr = save_instr;
2671
2672 if (res == FAIL || did_emsg
2673 || subs_instr.subs_status == FAIL)
2674 goto on_error;
2675 }
2676 break;
2677
2678 case ISN_FINISH:
2679 goto done;
2680
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002681 case ISN_REDIRSTART:
2682 // create a dummy entry for var_redir_str()
2683 if (alloc_redir_lval() == FAIL)
2684 goto on_error;
2685
2686 // The output is stored in growarray "redir_ga" until
2687 // redirection ends.
2688 init_redir_ga();
2689 redir_vname = 1;
2690 break;
2691
2692 case ISN_REDIREND:
2693 {
2694 char_u *res = get_clear_redir_ga();
2695
2696 // End redirection, put redirected text on the stack.
2697 clear_redir_lval();
2698 redir_vname = 0;
2699
Bram Moolenaar35578162021-08-02 19:10:38 +02002700 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002701 {
2702 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002703 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002704 }
2705 tv = STACK_TV_BOT(0);
2706 tv->v_type = VAR_STRING;
2707 tv->vval.v_string = res;
2708 ++ectx->ec_stack.ga_len;
2709 }
2710 break;
2711
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002712 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002713#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002714 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002715 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2716 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002717 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002718#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002719 break;
2720
2721 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002722#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002723 {
2724 exarg_T ea;
2725 int res;
2726
2727 CLEAR_FIELD(ea);
2728 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2729 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2730 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2731 --ectx->ec_stack.ga_len;
2732 tv = STACK_TV_BOT(0);
2733 res = cexpr_core(&ea, tv);
2734 clear_tv(tv);
2735 if (res == FAIL)
2736 goto on_error;
2737 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002738#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002739 break;
2740
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002741 // execute Ex command from pieces on the stack
2742 case ISN_EXECCONCAT:
2743 {
2744 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002745 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002746 int pass;
2747 int i;
2748 char_u *cmd = NULL;
2749 char_u *str;
2750
2751 for (pass = 1; pass <= 2; ++pass)
2752 {
2753 for (i = 0; i < count; ++i)
2754 {
2755 tv = STACK_TV_BOT(i - count);
2756 str = tv->vval.v_string;
2757 if (str != NULL && *str != NUL)
2758 {
2759 if (pass == 2)
2760 STRCPY(cmd + len, str);
2761 len += STRLEN(str);
2762 }
2763 if (pass == 2)
2764 clear_tv(tv);
2765 }
2766 if (pass == 1)
2767 {
2768 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002769 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002770 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002771 len = 0;
2772 }
2773 }
2774
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002775 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002776 do_cmdline_cmd(cmd);
2777 vim_free(cmd);
2778 }
2779 break;
2780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 // execute :echo {string} ...
2782 case ISN_ECHO:
2783 {
2784 int count = iptr->isn_arg.echo.echo_count;
2785 int atstart = TRUE;
2786 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002787 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788
2789 for (idx = 0; idx < count; ++idx)
2790 {
2791 tv = STACK_TV_BOT(idx - count);
2792 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2793 &atstart, &needclr);
2794 clear_tv(tv);
2795 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002796 if (needclr)
2797 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002798 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 }
2800 break;
2801
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002802 // :execute {string} ...
2803 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002804 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002805 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002806 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002807 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002808 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002809 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002810 {
2811 int count = iptr->isn_arg.number;
2812 garray_T ga;
2813 char_u buf[NUMBUFLEN];
2814 char_u *p;
2815 int len;
2816 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002817 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002818
2819 ga_init2(&ga, 1, 80);
2820 for (idx = 0; idx < count; ++idx)
2821 {
2822 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002823 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002824 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002825 if (tv->v_type == VAR_CHANNEL
2826 || tv->v_type == VAR_JOB)
2827 {
2828 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002829 semsg(_(e_using_invalid_value_as_string_str),
2830 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002831 break;
2832 }
2833 else
2834 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002835 }
2836 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002837 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002838
2839 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002840 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002841 failed = TRUE;
2842 else
2843 {
2844 if (ga.ga_len > 0)
2845 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2846 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2847 ga.ga_len += len;
2848 }
2849 clear_tv(tv);
2850 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002851 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002852 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002853 {
2854 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002855 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002856 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002857
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002858 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002859 {
2860 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002861 {
2862 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002863 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002864 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002865 {
2866 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002867 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002868 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002869 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002870 else
2871 {
2872 msg_sb_eol();
2873 if (iptr->isn_type == ISN_ECHOMSG)
2874 {
2875 msg_attr(ga.ga_data, echo_attr);
2876 out_flush();
2877 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002878 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2879 {
2880 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2881 TRUE);
2882 ui_write((char_u *)"\r\n", 2, TRUE);
2883 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002884 else
2885 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002886 SOURCING_LNUM = iptr->isn_lnum;
2887 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002888 }
2889 }
2890 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002891 ga_clear(&ga);
2892 }
2893 break;
2894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 // load local variable or argument
2896 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002897 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002898 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002900 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 break;
2902
2903 // load v: variable
2904 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002905 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002906 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002908 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 break;
2910
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002911 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 case ISN_LOADSCRIPT:
2913 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002914 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 svar_T *sv;
2916
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002917 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002918 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002919 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002920 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002921 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002922 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002924 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 }
2926 break;
2927
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002928 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002930 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002932 int sid = iptr->isn_arg.loadstore.ls_sid;
2933 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002934 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002936
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 if (di == NULL)
2938 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002939 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002940 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002941 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 }
2943 else
2944 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002945 if (iptr->isn_type == ISN_LOADEXPORT)
2946 {
2947 int idx = get_script_item_idx(sid, name, 0,
2948 NULL, NULL);
2949 svar_T *sv;
2950
2951 if (idx >= 0)
2952 {
2953 sv = ((svar_T *)SCRIPT_ITEM(sid)
2954 ->sn_var_vals.ga_data) + idx;
2955 if (!sv->sv_export)
2956 {
2957 SOURCING_LNUM = iptr->isn_lnum;
2958 semsg(_(e_item_not_exported_in_script_str),
2959 name);
2960 goto on_error;
2961 }
2962 }
2963 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002964 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002965 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002967 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 }
2969 }
2970 break;
2971
Bram Moolenaard3aac292020-04-19 14:32:17 +02002972 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002974 case ISN_LOADB:
2975 case ISN_LOADW:
2976 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002978 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002979
Bram Moolenaar06b77222022-01-25 15:51:56 +00002980 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002981 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002982 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002983 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 break;
2987
Bram Moolenaar03290b82020-12-19 16:30:44 +01002988 // load autoload variable
2989 case ISN_LOADAUTO:
2990 {
2991 char_u *name = iptr->isn_arg.string;
2992
Bram Moolenaar35578162021-08-02 19:10:38 +02002993 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002994 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002995 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002996 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002997 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002998 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002999 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003000 }
3001 break;
3002
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003003 // load g:/b:/w:/t: namespace
3004 case ISN_LOADGDICT:
3005 case ISN_LOADBDICT:
3006 case ISN_LOADWDICT:
3007 case ISN_LOADTDICT:
3008 {
3009 dict_T *d = NULL;
3010
3011 switch (iptr->isn_type)
3012 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003013 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3014 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3015 case ISN_LOADWDICT: d = curwin->w_vars; break;
3016 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003017 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003018 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003019 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003020 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003021 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003022 tv = STACK_TV_BOT(0);
3023 tv->v_type = VAR_DICT;
3024 tv->v_lock = 0;
3025 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003026 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003027 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003028 }
3029 break;
3030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 // load &option
3032 case ISN_LOADOPT:
3033 {
3034 typval_T optval;
3035 char_u *name = iptr->isn_arg.string;
3036
Bram Moolenaara8c17702020-04-01 21:17:24 +02003037 // This is not expected to fail, name is checked during
3038 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003039 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003040 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003041 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003042 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003044 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 }
3046 break;
3047
3048 // load $ENV
3049 case ISN_LOADENV:
3050 {
3051 typval_T optval;
3052 char_u *name = iptr->isn_arg.string;
3053
Bram Moolenaar35578162021-08-02 19:10:38 +02003054 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003055 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003056 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003057 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003059 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 }
3061 break;
3062
3063 // load @register
3064 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003065 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003066 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 tv = STACK_TV_BOT(0);
3068 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003069 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003070 // This may result in NULL, which should be equivalent to an
3071 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 tv->vval.v_string = get_reg_contents(
3073 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003074 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 break;
3076
3077 // store local variable
3078 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003079 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 tv = STACK_TV_VAR(iptr->isn_arg.number);
3081 clear_tv(tv);
3082 *tv = *STACK_TV_BOT(0);
3083 break;
3084
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003085 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003086 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003087 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003088 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003089 int sid = iptr->isn_arg.loadstore.ls_sid;
3090 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003091 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003092 dictitem_T *di = find_var_in_ht(ht, 0,
3093 iptr->isn_type == ISN_STORES
3094 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003095
Bram Moolenaar4c137212021-04-19 16:48:48 +02003096 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003097 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003098 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003099 {
3100 if (iptr->isn_type == ISN_STOREEXPORT)
3101 {
3102 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003103 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003104 goto on_error;
3105 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003106 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003107 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003108 else
3109 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003110 if (iptr->isn_type == ISN_STOREEXPORT)
3111 {
3112 int idx = get_script_item_idx(sid, name, 0,
3113 NULL, NULL);
3114
3115 if (idx >= 0)
3116 {
3117 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3118 ->sn_var_vals.ga_data) + idx;
3119
3120 if (!sv->sv_export)
3121 {
3122 semsg(_(e_item_not_exported_in_script_str),
3123 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003124 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003125 goto on_error;
3126 }
3127 }
3128 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003129 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003130 {
3131 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003132 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003133 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003134 clear_tv(&di->di_tv);
3135 di->di_tv = *STACK_TV_BOT(0);
3136 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003137 }
3138 break;
3139
3140 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 case ISN_STORESCRIPT:
3142 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003143 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3144 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003146 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003147 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003148 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003149 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003150
3151 // "const" and "final" are checked at compile time, locking
3152 // the value needs to be checked here.
3153 SOURCING_LNUM = iptr->isn_lnum;
3154 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003155 {
3156 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003157 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003158 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003159
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 clear_tv(sv->sv_tv);
3161 *sv->sv_tv = *STACK_TV_BOT(0);
3162 }
3163 break;
3164
3165 // store option
3166 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003167 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003169 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3170 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 long n = 0;
3172 char_u *s = NULL;
3173 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003174 char_u numbuf[NUMBUFLEN];
3175 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176
Bram Moolenaar4c137212021-04-19 16:48:48 +02003177 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 tv = STACK_TV_BOT(0);
3179 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003180 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003182 if (s == NULL)
3183 s = (char_u *)"";
3184 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003185 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3186 {
3187 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003188 // If the option can be set to a function reference or
3189 // a lambda and the passed value is a function
3190 // reference, then convert it to the name (string) of
3191 // the function reference.
3192 s = tv2string(tv, &tofree, numbuf, 0);
3193 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003194 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003195 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003196 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003197 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003198 goto on_error;
3199 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003202 // must be VAR_NUMBER, CHECKTYPE makes sure
3203 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003204 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003205 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003206 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 if (msg != NULL)
3208 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003209 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003211 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 }
3214 break;
3215
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003216 // store $ENV
3217 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003218 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003219 tv = STACK_TV_BOT(0);
3220 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3221 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003222 break;
3223
3224 // store @r
3225 case ISN_STOREREG:
3226 {
3227 int reg = iptr->isn_arg.number;
3228
Bram Moolenaar4c137212021-04-19 16:48:48 +02003229 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003230 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003231 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003232 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003233 }
3234 break;
3235
3236 // store v: variable
3237 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003238 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003239 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3240 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003241 // should not happen, type is checked when compiling
3242 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003243 break;
3244
Bram Moolenaard3aac292020-04-19 14:32:17 +02003245 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003247 case ISN_STOREB:
3248 case ISN_STOREW:
3249 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003251 dictitem_T *di;
3252 hashtab_T *ht;
3253 char_u *name = iptr->isn_arg.string + 2;
3254
Bram Moolenaard3aac292020-04-19 14:32:17 +02003255 switch (iptr->isn_type)
3256 {
3257 case ISN_STOREG:
3258 ht = get_globvar_ht();
3259 break;
3260 case ISN_STOREB:
3261 ht = &curbuf->b_vars->dv_hashtab;
3262 break;
3263 case ISN_STOREW:
3264 ht = &curwin->w_vars->dv_hashtab;
3265 break;
3266 case ISN_STORET:
3267 ht = &curtab->tp_vars->dv_hashtab;
3268 break;
3269 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003270 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272
Bram Moolenaar4c137212021-04-19 16:48:48 +02003273 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003274 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003276 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 else
3278 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003279 SOURCING_LNUM = iptr->isn_lnum;
3280 if (var_check_permission(di, name) == FAIL)
3281 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 clear_tv(&di->di_tv);
3283 di->di_tv = *STACK_TV_BOT(0);
3284 }
3285 }
3286 break;
3287
Bram Moolenaar03290b82020-12-19 16:30:44 +01003288 // store an autoload variable
3289 case ISN_STOREAUTO:
3290 SOURCING_LNUM = iptr->isn_lnum;
3291 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3292 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003293 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003294 break;
3295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 // store number in local variable
3297 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003298 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 clear_tv(tv);
3300 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003301 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 break;
3303
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003304 // store value in list or dict variable
3305 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003306 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003307 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003308
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003309 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003310 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003311 if (res == NOTDONE)
3312 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003313 }
3314 break;
3315
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003316 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003317 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003318 if (execute_storerange(iptr, ectx) == FAIL)
3319 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003320 break;
3321
Bram Moolenaar0186e582021-01-10 18:33:11 +01003322 // load or store variable or argument from outer scope
3323 case ISN_LOADOUTER:
3324 case ISN_STOREOUTER:
3325 {
3326 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003327 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3328 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003329
3330 while (depth > 1 && outer != NULL)
3331 {
3332 outer = outer->out_up;
3333 --depth;
3334 }
3335 if (outer == NULL)
3336 {
3337 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003338 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3339 || ectx->ec_outer_ref == NULL)
3340 // Possibly :def function called from legacy
3341 // context.
3342 emsg(_(e_closure_called_from_invalid_context));
3343 else
3344 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003345 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003346 }
3347 tv = ((typval_T *)outer->out_stack->ga_data)
3348 + outer->out_frame_idx + STACK_FRAME_SIZE
3349 + iptr->isn_arg.outer.outer_idx;
3350 if (iptr->isn_type == ISN_LOADOUTER)
3351 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003352 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003353 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003354 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003355 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003356 }
3357 else
3358 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003359 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003360 clear_tv(tv);
3361 *tv = *STACK_TV_BOT(0);
3362 }
3363 }
3364 break;
3365
Bram Moolenaar752fc692021-01-04 21:57:11 +01003366 // unlet item in list or dict variable
3367 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003368 if (execute_unletindex(iptr, ectx) == FAIL)
3369 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003370 break;
3371
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003372 // unlet range of items in list variable
3373 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003374 if (execute_unletrange(iptr, ectx) == FAIL)
3375 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003376 break;
3377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 // push constant
3379 case ISN_PUSHNR:
3380 case ISN_PUSHBOOL:
3381 case ISN_PUSHSPEC:
3382 case ISN_PUSHF:
3383 case ISN_PUSHS:
3384 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003385 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003386 case ISN_PUSHCHANNEL:
3387 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003388 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003389 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003391 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003392 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 switch (iptr->isn_type)
3394 {
3395 case ISN_PUSHNR:
3396 tv->v_type = VAR_NUMBER;
3397 tv->vval.v_number = iptr->isn_arg.number;
3398 break;
3399 case ISN_PUSHBOOL:
3400 tv->v_type = VAR_BOOL;
3401 tv->vval.v_number = iptr->isn_arg.number;
3402 break;
3403 case ISN_PUSHSPEC:
3404 tv->v_type = VAR_SPECIAL;
3405 tv->vval.v_number = iptr->isn_arg.number;
3406 break;
3407#ifdef FEAT_FLOAT
3408 case ISN_PUSHF:
3409 tv->v_type = VAR_FLOAT;
3410 tv->vval.v_float = iptr->isn_arg.fnumber;
3411 break;
3412#endif
3413 case ISN_PUSHBLOB:
3414 blob_copy(iptr->isn_arg.blob, tv);
3415 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003416 case ISN_PUSHFUNC:
3417 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003418 if (iptr->isn_arg.string == NULL)
3419 tv->vval.v_string = NULL;
3420 else
3421 tv->vval.v_string =
3422 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003423 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003424 case ISN_PUSHCHANNEL:
3425#ifdef FEAT_JOB_CHANNEL
3426 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003427 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003428#endif
3429 break;
3430 case ISN_PUSHJOB:
3431#ifdef FEAT_JOB_CHANNEL
3432 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003433 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003434#endif
3435 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 default:
3437 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003438 tv->vval.v_string = iptr->isn_arg.string == NULL
3439 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 }
3441 break;
3442
Bram Moolenaar06b77222022-01-25 15:51:56 +00003443 case ISN_AUTOLOAD:
3444 {
3445 char_u *name = iptr->isn_arg.string;
3446
3447 (void)script_autoload(name, FALSE);
3448 if (find_func(name, TRUE))
3449 {
3450 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3451 goto theend;
3452 tv = STACK_TV_BOT(0);
3453 tv->v_lock = 0;
3454 ++ectx->ec_stack.ga_len;
3455 tv->v_type = VAR_FUNC;
3456 tv->vval.v_string = vim_strsave(name);
3457 }
3458 else
3459 {
3460 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3461
3462 if (res == NOTDONE)
3463 goto theend;
3464 if (res == FAIL)
3465 goto on_error;
3466 }
3467 }
3468 break;
3469
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003470 case ISN_UNLET:
3471 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3472 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003473 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003474 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003475 case ISN_UNLETENV:
3476 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3477 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003478
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003479 case ISN_LOCKUNLOCK:
3480 {
3481 typval_T *lval_root_save = lval_root;
3482 int res;
3483
3484 // Stack has the local variable, argument the whole :lock
3485 // or :unlock command, like ISN_EXEC.
3486 --ectx->ec_stack.ga_len;
3487 lval_root = STACK_TV_BOT(0);
3488 res = exec_command(iptr);
3489 clear_tv(lval_root);
3490 lval_root = lval_root_save;
3491 if (res == FAIL)
3492 goto on_error;
3493 }
3494 break;
3495
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003496 case ISN_LOCKCONST:
3497 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3498 break;
3499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 // create a list from items on the stack; uses a single allocation
3501 // for the list header and the items
3502 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003503 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003504 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 break;
3506
3507 // create a dict from items on the stack
3508 case ISN_NEWDICT:
3509 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003510 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003512 SOURCING_LNUM = iptr->isn_lnum;
3513 res = exe_newdict(iptr->isn_arg.number, ectx);
3514 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003515 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003516 if (res == MAYBE)
3517 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 }
3519 break;
3520
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003521 // create a partial with NULL value
3522 case ISN_NEWPARTIAL:
3523 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3524 goto theend;
3525 ++ectx->ec_stack.ga_len;
3526 tv = STACK_TV_BOT(-1);
3527 tv->v_type = VAR_PARTIAL;
3528 tv->v_lock = 0;
3529 tv->vval.v_partial = NULL;
3530 break;
3531
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 // call a :def function
3533 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003534 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003535 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3536 NULL,
3537 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003538 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003539 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 break;
3541
3542 // call a builtin function
3543 case ISN_BCALL:
3544 SOURCING_LNUM = iptr->isn_lnum;
3545 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3546 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003547 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003548 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 break;
3550
3551 // call a funcref or partial
3552 case ISN_PCALL:
3553 {
3554 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3555 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003556 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557
3558 SOURCING_LNUM = iptr->isn_lnum;
3559 if (pfunc->cpf_top)
3560 {
3561 // funcref is above the arguments
3562 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3563 }
3564 else
3565 {
3566 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003567 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003568 partial_tv = *STACK_TV_BOT(0);
3569 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003571 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003572 if (tv == &partial_tv)
3573 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003575 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 }
3577 break;
3578
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003579 case ISN_PCALL_END:
3580 // PCALL finished, arguments have been consumed and replaced by
3581 // the return value. Now clear the funcref from the stack,
3582 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003583 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003584 clear_tv(STACK_TV_BOT(-1));
3585 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3586 break;
3587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 // call a user defined function or funcref/partial
3589 case ISN_UCALL:
3590 {
3591 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3592
3593 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003594 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003595 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003596 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 }
3598 break;
3599
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003600 // return from a :def function call without a value
3601 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003602 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003603 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003604 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003605 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003606 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003607 tv->vval.v_number = 0;
3608 tv->v_lock = 0;
3609 // FALLTHROUGH
3610
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003611 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 case ISN_RETURN:
3613 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003614 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003615 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003616
3617 if (trystack->ga_len > 0)
3618 trycmd = ((trycmd_T *)trystack->ga_data)
3619 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003620 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003621 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003623 // jump to ":finally" or ":endtry"
3624 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003625 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003626 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003627 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 trycmd->tcd_return = TRUE;
3629 }
3630 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003631 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 }
3633 break;
3634
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003635 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 case ISN_FUNCREF:
3637 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003638 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003639 ufunc_T *ufunc;
3640 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003643 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003644 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003645 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003646 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003647 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003648 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003649 if (funcref->fr_func_name == NULL)
3650 {
3651 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3652 + funcref->fr_dfunc_idx;
3653
3654 ufunc = pt_dfunc->df_ufunc;
3655 }
3656 else
3657 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003658 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003659 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003660 if (ufunc == NULL)
3661 {
3662 SOURCING_LNUM = iptr->isn_lnum;
3663 iemsg("ufunc unexpectedly NULL for FUNCREF");
3664 goto theend;
3665 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003666 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003667 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003669 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 tv->vval.v_partial = pt;
3671 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003672 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 }
3674 break;
3675
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003676 // Create a global function from a lambda.
3677 case ISN_NEWFUNC:
3678 {
3679 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3680
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003681 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003682 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003683 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003684 }
3685 break;
3686
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003687 // List functions
3688 case ISN_DEF:
3689 if (iptr->isn_arg.string == NULL)
3690 list_functions(NULL);
3691 else
3692 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003693 exarg_T ea;
3694 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003695
3696 CLEAR_FIELD(ea);
3697 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003698 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3699 define_function(&ea, NULL, &lines_to_free);
3700 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003701 }
3702 break;
3703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 // jump if a condition is met
3705 case ISN_JUMP:
3706 {
3707 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003708 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 int jump = TRUE;
3710
3711 if (when != JUMP_ALWAYS)
3712 {
3713 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003714 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003715 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003716 || when == JUMP_IF_COND_TRUE)
3717 {
3718 SOURCING_LNUM = iptr->isn_lnum;
3719 jump = tv_get_bool_chk(tv, &error);
3720 if (error)
3721 goto on_error;
3722 }
3723 else
3724 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003726 || when == JUMP_AND_KEEP_IF_FALSE
3727 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003729 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 {
3731 // drop the value from the stack
3732 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003733 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 }
3735 }
3736 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003737 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 }
3739 break;
3740
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003741 // Jump if an argument with a default value was already set and not
3742 // v:none.
3743 case ISN_JUMP_IF_ARG_SET:
3744 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3745 if (tv->v_type != VAR_UNKNOWN
3746 && !(tv->v_type == VAR_SPECIAL
3747 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003748 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003749 break;
3750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 // top of a for loop
3752 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003753 if (execute_for(iptr, ectx) == FAIL)
3754 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 break;
3756
3757 // start of ":try" block
3758 case ISN_TRY:
3759 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003760 trycmd_T *trycmd = NULL;
3761
Bram Moolenaar35578162021-08-02 19:10:38 +02003762 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003763 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003764 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3765 + ectx->ec_trystack.ga_len;
3766 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003768 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003769 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3770 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003771 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003772 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003773 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003774 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003775 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003776 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 }
3778 break;
3779
3780 case ISN_PUSHEXC:
3781 if (current_exception == NULL)
3782 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003783 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003785 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003787 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003788 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003790 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003792 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 tv->vval.v_string = vim_strsave(
3794 (char_u *)current_exception->value);
3795 break;
3796
3797 case ISN_CATCH:
3798 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003799 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800
Bram Moolenaar4c137212021-04-19 16:48:48 +02003801 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 if (trystack->ga_len > 0)
3803 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003804 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 + trystack->ga_len - 1;
3806 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003807 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 }
3809 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003810 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 catch_exception(current_exception);
3812 }
3813 break;
3814
Bram Moolenaarc150c092021-02-13 15:02:46 +01003815 case ISN_TRYCONT:
3816 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003817 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003818 trycont_T *trycont = &iptr->isn_arg.trycont;
3819 int i;
3820 trycmd_T *trycmd;
3821 int iidx = trycont->tct_where;
3822
3823 if (trystack->ga_len < trycont->tct_levels)
3824 {
3825 siemsg("TRYCONT: expected %d levels, found %d",
3826 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003827 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003828 }
3829 // Make :endtry jump to any outer try block and the last
3830 // :endtry inside the loop to the loop start.
3831 for (i = trycont->tct_levels; i > 0; --i)
3832 {
3833 trycmd = ((trycmd_T *)trystack->ga_data)
3834 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003835 // Add one to tcd_cont to be able to jump to
3836 // instruction with index zero.
3837 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003838 iidx = trycmd->tcd_finally_idx == 0
3839 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003840 }
3841 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003842 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003843 }
3844 break;
3845
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003846 case ISN_FINALLY:
3847 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003848 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003849 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3850 + trystack->ga_len - 1;
3851
3852 // Reset the index to avoid a return statement jumps here
3853 // again.
3854 trycmd->tcd_finally_idx = 0;
3855 break;
3856 }
3857
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 // end of ":try" block
3859 case ISN_ENDTRY:
3860 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003861 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862
3863 if (trystack->ga_len > 0)
3864 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003865 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003866
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 --trystack->ga_len;
3868 --trylevel;
3869 trycmd = ((trycmd_T *)trystack->ga_data)
3870 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003871 if (trycmd->tcd_did_throw)
3872 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003873 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 {
3875 // discard the exception
3876 if (caught_stack == current_exception)
3877 caught_stack = caught_stack->caught;
3878 discard_current_exception();
3879 }
3880
3881 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003882 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003883
Bram Moolenaar4c137212021-04-19 16:48:48 +02003884 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003885 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003886 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003887 clear_tv(STACK_TV_BOT(0));
3888 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003889 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003890 // handling :continue: jump to outer try block or
3891 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003892 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 }
3894 }
3895 break;
3896
3897 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003898 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003899 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003900
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003901 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3902 {
3903 // throwing an exception while using "silent!" causes
3904 // the function to abort but not display an error.
3905 tv = STACK_TV_BOT(-1);
3906 clear_tv(tv);
3907 tv->v_type = VAR_NUMBER;
3908 tv->vval.v_number = 0;
3909 goto done;
3910 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003911 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003912 tv = STACK_TV_BOT(0);
3913 if (tv->vval.v_string == NULL
3914 || *skipwhite(tv->vval.v_string) == NUL)
3915 {
3916 vim_free(tv->vval.v_string);
3917 SOURCING_LNUM = iptr->isn_lnum;
3918 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003919 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003920 }
3921
3922 // Inside a "catch" we need to first discard the caught
3923 // exception.
3924 if (trystack->ga_len > 0)
3925 {
3926 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3927 + trystack->ga_len - 1;
3928 if (trycmd->tcd_caught && current_exception != NULL)
3929 {
3930 // discard the exception
3931 if (caught_stack == current_exception)
3932 caught_stack = caught_stack->caught;
3933 discard_current_exception();
3934 trycmd->tcd_caught = FALSE;
3935 }
3936 }
3937
Bram Moolenaar90a57162022-02-12 14:23:17 +00003938 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003939 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3940 == FAIL)
3941 {
3942 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003943 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003944 }
3945 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 break;
3948
3949 // compare with special values
3950 case ISN_COMPAREBOOL:
3951 case ISN_COMPARESPECIAL:
3952 {
3953 typval_T *tv1 = STACK_TV_BOT(-2);
3954 typval_T *tv2 = STACK_TV_BOT(-1);
3955 varnumber_T arg1 = tv1->vval.v_number;
3956 varnumber_T arg2 = tv2->vval.v_number;
3957 int res;
3958
3959 switch (iptr->isn_arg.op.op_type)
3960 {
3961 case EXPR_EQUAL: res = arg1 == arg2; break;
3962 case EXPR_NEQUAL: res = arg1 != arg2; break;
3963 default: res = 0; break;
3964 }
3965
Bram Moolenaar4c137212021-04-19 16:48:48 +02003966 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 tv1->v_type = VAR_BOOL;
3968 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3969 }
3970 break;
3971
Bram Moolenaar7a222242022-03-01 19:23:24 +00003972 case ISN_COMPARENULL:
3973 {
3974 typval_T *tv1 = STACK_TV_BOT(-2);
3975 typval_T *tv2 = STACK_TV_BOT(-1);
3976 int res;
3977
3978 res = typval_compare_null(tv1, tv2);
3979 if (res == MAYBE)
3980 goto on_error;
3981 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
3982 res = !res;
3983 clear_tv(tv1);
3984 clear_tv(tv2);
3985 --ectx->ec_stack.ga_len;
3986 tv1->v_type = VAR_BOOL;
3987 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3988 }
3989 break;
3990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 // Operation with two number arguments
3992 case ISN_OPNR:
3993 case ISN_COMPARENR:
3994 {
3995 typval_T *tv1 = STACK_TV_BOT(-2);
3996 typval_T *tv2 = STACK_TV_BOT(-1);
3997 varnumber_T arg1 = tv1->vval.v_number;
3998 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003999 varnumber_T res = 0;
4000 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001
4002 switch (iptr->isn_arg.op.op_type)
4003 {
4004 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004005 case EXPR_DIV: if (arg2 == 0)
4006 div_zero = TRUE;
4007 else
4008 res = arg1 / arg2;
4009 break;
4010 case EXPR_REM: if (arg2 == 0)
4011 div_zero = TRUE;
4012 else
4013 res = arg1 % arg2;
4014 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 case EXPR_SUB: res = arg1 - arg2; break;
4016 case EXPR_ADD: res = arg1 + arg2; break;
4017
4018 case EXPR_EQUAL: res = arg1 == arg2; break;
4019 case EXPR_NEQUAL: res = arg1 != arg2; break;
4020 case EXPR_GREATER: res = arg1 > arg2; break;
4021 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4022 case EXPR_SMALLER: res = arg1 < arg2; break;
4023 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004024 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 }
4026
Bram Moolenaar4c137212021-04-19 16:48:48 +02004027 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 if (iptr->isn_type == ISN_COMPARENR)
4029 {
4030 tv1->v_type = VAR_BOOL;
4031 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4032 }
4033 else
4034 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004035 if (div_zero)
4036 {
4037 SOURCING_LNUM = iptr->isn_lnum;
4038 emsg(_(e_divide_by_zero));
4039 goto on_error;
4040 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 }
4042 break;
4043
4044 // Computation with two float arguments
4045 case ISN_OPFLOAT:
4046 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004047#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 {
4049 typval_T *tv1 = STACK_TV_BOT(-2);
4050 typval_T *tv2 = STACK_TV_BOT(-1);
4051 float_T arg1 = tv1->vval.v_float;
4052 float_T arg2 = tv2->vval.v_float;
4053 float_T res = 0;
4054 int cmp = FALSE;
4055
4056 switch (iptr->isn_arg.op.op_type)
4057 {
4058 case EXPR_MULT: res = arg1 * arg2; break;
4059 case EXPR_DIV: res = arg1 / arg2; break;
4060 case EXPR_SUB: res = arg1 - arg2; break;
4061 case EXPR_ADD: res = arg1 + arg2; break;
4062
4063 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4064 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4065 case EXPR_GREATER: cmp = arg1 > arg2; break;
4066 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4067 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4068 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4069 default: cmp = 0; break;
4070 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004071 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 if (iptr->isn_type == ISN_COMPAREFLOAT)
4073 {
4074 tv1->v_type = VAR_BOOL;
4075 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4076 }
4077 else
4078 tv1->vval.v_float = res;
4079 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004080#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 break;
4082
4083 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004084 case ISN_COMPAREDICT:
4085 case ISN_COMPAREFUNC:
4086 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 case ISN_COMPAREBLOB:
4088 {
4089 typval_T *tv1 = STACK_TV_BOT(-2);
4090 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004091 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4092 int ic = iptr->isn_arg.op.op_ic;
4093 int res = FALSE;
4094 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095
Bram Moolenaar265f8112021-12-19 12:33:05 +00004096 SOURCING_LNUM = iptr->isn_lnum;
4097 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004099 status = typval_compare_list(tv1, tv2,
4100 exprtype, ic, &res);
4101 }
4102 else if (iptr->isn_type == ISN_COMPAREDICT)
4103 {
4104 status = typval_compare_dict(tv1, tv2,
4105 exprtype, ic, &res);
4106 }
4107 else if (iptr->isn_type == ISN_COMPAREFUNC)
4108 {
4109 status = typval_compare_func(tv1, tv2,
4110 exprtype, ic, &res);
4111 }
4112 else if (iptr->isn_type == ISN_COMPARESTRING)
4113 {
4114 status = typval_compare_string(tv1, tv2,
4115 exprtype, ic, &res);
4116 }
4117 else
4118 {
4119 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004121 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 clear_tv(tv1);
4123 clear_tv(tv2);
4124 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004125 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4126 if (status == FAIL)
4127 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 }
4129 break;
4130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 case ISN_COMPAREANY:
4132 {
4133 typval_T *tv1 = STACK_TV_BOT(-2);
4134 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004135 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004137 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138
Bram Moolenaareb26f432020-09-14 16:50:05 +02004139 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004140 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004142 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004143 if (status == FAIL)
4144 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 }
4146 break;
4147
4148 case ISN_ADDLIST:
4149 case ISN_ADDBLOB:
4150 {
4151 typval_T *tv1 = STACK_TV_BOT(-2);
4152 typval_T *tv2 = STACK_TV_BOT(-1);
4153
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004154 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004156 {
4157 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4158 && tv1->vval.v_list != NULL)
4159 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4160 NULL);
4161 else
4162 eval_addlist(tv1, tv2);
4163 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 else
4165 eval_addblob(tv1, tv2);
4166 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004167 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 }
4169 break;
4170
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004171 case ISN_LISTAPPEND:
4172 {
4173 typval_T *tv1 = STACK_TV_BOT(-2);
4174 typval_T *tv2 = STACK_TV_BOT(-1);
4175 list_T *l = tv1->vval.v_list;
4176
4177 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004178 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004179 if (l == NULL)
4180 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004181 emsg(_(e_cannot_add_to_null_list));
4182 goto on_error;
4183 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004184 if (value_check_lock(l->lv_lock, NULL, FALSE))
4185 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004186 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004187 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004188 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004189 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004190 }
4191 break;
4192
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004193 case ISN_BLOBAPPEND:
4194 {
4195 typval_T *tv1 = STACK_TV_BOT(-2);
4196 typval_T *tv2 = STACK_TV_BOT(-1);
4197 blob_T *b = tv1->vval.v_blob;
4198 int error = FALSE;
4199 varnumber_T n;
4200
4201 // add a number to a blob
4202 if (b == NULL)
4203 {
4204 SOURCING_LNUM = iptr->isn_lnum;
4205 emsg(_(e_cannot_add_to_null_blob));
4206 goto on_error;
4207 }
4208 n = tv_get_number_chk(tv2, &error);
4209 if (error)
4210 goto on_error;
4211 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004212 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004213 }
4214 break;
4215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 // Computation with two arguments of unknown type
4217 case ISN_OPANY:
4218 {
4219 typval_T *tv1 = STACK_TV_BOT(-2);
4220 typval_T *tv2 = STACK_TV_BOT(-1);
4221 varnumber_T n1, n2;
4222#ifdef FEAT_FLOAT
4223 float_T f1 = 0, f2 = 0;
4224#endif
4225 int error = FALSE;
4226
4227 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4228 {
4229 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4230 {
4231 eval_addlist(tv1, tv2);
4232 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004233 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 break;
4235 }
4236 else if (tv1->v_type == VAR_BLOB
4237 && tv2->v_type == VAR_BLOB)
4238 {
4239 eval_addblob(tv1, tv2);
4240 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004241 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 break;
4243 }
4244 }
4245#ifdef FEAT_FLOAT
4246 if (tv1->v_type == VAR_FLOAT)
4247 {
4248 f1 = tv1->vval.v_float;
4249 n1 = 0;
4250 }
4251 else
4252#endif
4253 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004254 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 n1 = tv_get_number_chk(tv1, &error);
4256 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004257 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258#ifdef FEAT_FLOAT
4259 if (tv2->v_type == VAR_FLOAT)
4260 f1 = n1;
4261#endif
4262 }
4263#ifdef FEAT_FLOAT
4264 if (tv2->v_type == VAR_FLOAT)
4265 {
4266 f2 = tv2->vval.v_float;
4267 n2 = 0;
4268 }
4269 else
4270#endif
4271 {
4272 n2 = tv_get_number_chk(tv2, &error);
4273 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004274 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275#ifdef FEAT_FLOAT
4276 if (tv1->v_type == VAR_FLOAT)
4277 f2 = n2;
4278#endif
4279 }
4280#ifdef FEAT_FLOAT
4281 // if there is a float on either side the result is a float
4282 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4283 {
4284 switch (iptr->isn_arg.op.op_type)
4285 {
4286 case EXPR_MULT: f1 = f1 * f2; break;
4287 case EXPR_DIV: f1 = f1 / f2; break;
4288 case EXPR_SUB: f1 = f1 - f2; break;
4289 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004290 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004291 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004292 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 }
4294 clear_tv(tv1);
4295 clear_tv(tv2);
4296 tv1->v_type = VAR_FLOAT;
4297 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004298 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 }
4300 else
4301#endif
4302 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004303 int failed = FALSE;
4304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 switch (iptr->isn_arg.op.op_type)
4306 {
4307 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004308 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4309 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004310 goto on_error;
4311 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 case EXPR_SUB: n1 = n1 - n2; break;
4313 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004314 default: n1 = num_modulus(n1, n2, &failed);
4315 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004316 goto on_error;
4317 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 }
4319 clear_tv(tv1);
4320 clear_tv(tv2);
4321 tv1->v_type = VAR_NUMBER;
4322 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004323 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 }
4325 }
4326 break;
4327
4328 case ISN_CONCAT:
4329 {
4330 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4331 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4332 char_u *res;
4333
4334 res = concat_str(str1, str2);
4335 clear_tv(STACK_TV_BOT(-2));
4336 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004337 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 STACK_TV_BOT(-1)->vval.v_string = res;
4339 }
4340 break;
4341
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004342 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004343 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004344 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004345 int is_slice = iptr->isn_type == ISN_STRSLICE;
4346 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004347 char_u *res;
4348
4349 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004350 // string slice: string is at stack-3, first index at
4351 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004352 if (is_slice)
4353 {
4354 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004355 n1 = tv->vval.v_number;
4356 }
4357
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004358 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004359 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004360
Bram Moolenaar4c137212021-04-19 16:48:48 +02004361 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004362 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004363 if (is_slice)
4364 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004365 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004366 else
4367 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004368 // single character (including composing characters).
4369 // If the index is too big or negative the result is
4370 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004371 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004372 vim_free(tv->vval.v_string);
4373 tv->vval.v_string = res;
4374 }
4375 break;
4376
4377 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004378 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004379 case ISN_BLOBINDEX:
4380 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004382 int is_slice = iptr->isn_type == ISN_LISTSLICE
4383 || iptr->isn_type == ISN_BLOBSLICE;
4384 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4385 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004386 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004387 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388
4389 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004390 // list slice: list is at stack-3, indexes at stack-2 and
4391 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004392 // Same for blob.
4393 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394
4395 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004396 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004398
4399 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 {
Bram Moolenaared591872020-08-15 22:14:53 +02004401 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004402 n1 = tv->vval.v_number;
4403 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 }
Bram Moolenaared591872020-08-15 22:14:53 +02004405
Bram Moolenaar4c137212021-04-19 16:48:48 +02004406 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004407 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004408 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004409 if (is_blob)
4410 {
4411 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4412 n1, n2, FALSE, tv) == FAIL)
4413 goto on_error;
4414 }
4415 else
4416 {
4417 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4418 n1, n2, FALSE, tv, TRUE) == FAIL)
4419 goto on_error;
4420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 }
4422 break;
4423
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004424 case ISN_ANYINDEX:
4425 case ISN_ANYSLICE:
4426 {
4427 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4428 typval_T *var1, *var2;
4429 int res;
4430
4431 // index: composite is at stack-2, index at stack-1
4432 // slice: composite is at stack-3, indexes at stack-2 and
4433 // stack-1
4434 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004435 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004436 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4437 goto on_error;
4438 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4439 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004440 res = eval_index_inner(tv, is_slice, var1, var2,
4441 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004442 clear_tv(var1);
4443 if (is_slice)
4444 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004445 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004446 if (res == FAIL)
4447 goto on_error;
4448 }
4449 break;
4450
Bram Moolenaar9af78762020-06-16 11:34:42 +02004451 case ISN_SLICE:
4452 {
4453 list_T *list;
4454 int count = iptr->isn_arg.number;
4455
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004456 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004457 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004458 list = tv->vval.v_list;
4459
4460 // no error for short list, expect it to be checked earlier
4461 if (list != NULL && list->lv_len >= count)
4462 {
4463 list_T *newlist = list_slice(list,
4464 count, list->lv_len - 1);
4465
4466 if (newlist != NULL)
4467 {
4468 list_unref(list);
4469 tv->vval.v_list = newlist;
4470 ++newlist->lv_refcount;
4471 }
4472 }
4473 }
4474 break;
4475
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004476 case ISN_GETITEM:
4477 {
4478 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004479 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004480
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004481 // Get list item: list is at stack-1, push item.
4482 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004483 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4484 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004485
Bram Moolenaar35578162021-08-02 19:10:38 +02004486 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004487 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004488 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004489 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004490
4491 // Useful when used in unpack assignment. Reset at
4492 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004493 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004494 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004495 }
4496 break;
4497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 case ISN_MEMBER:
4499 {
4500 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004501 char_u *key;
4502 dictitem_T *di;
4503
4504 // dict member: dict is at stack-2, key at stack-1
4505 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004506 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004507 dict = tv->vval.v_dict;
4508
4509 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004510 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004511 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004512 if (key == NULL)
4513 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004514
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004515 if ((di = dict_find(dict, key, -1)) == NULL)
4516 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004517 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004518 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004519
4520 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004521 // stack contents makes sense and the dict stack is
4522 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004523 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004524 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004525 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004526 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004527 tv->v_type = VAR_NUMBER;
4528 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004529 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004530 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004531 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004532 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004533 // Put the dict used on the dict stack, it might be used by
4534 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004535 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004536 if (dict_stack_save(tv) == FAIL)
4537 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004538 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004539 }
4540 break;
4541
4542 // dict member with string key
4543 case ISN_STRINGMEMBER:
4544 {
4545 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 dictitem_T *di;
4547
4548 tv = STACK_TV_BOT(-1);
4549 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4550 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004551 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004552 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004553 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 }
4555 dict = tv->vval.v_dict;
4556
4557 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4558 == NULL)
4559 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004560 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004561 semsg(_(e_key_not_present_in_dictionary),
4562 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004563 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004565 // Put the dict used on the dict stack, it might be used by
4566 // a dict function later.
4567 if (dict_stack_save(tv) == FAIL)
4568 goto on_fatal_error;
4569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004571 }
4572 break;
4573
4574 case ISN_CLEARDICT:
4575 dict_stack_drop();
4576 break;
4577
4578 case ISN_USEDICT:
4579 {
4580 typval_T *dict_tv = dict_stack_get_tv();
4581
4582 // Turn "dict.Func" into a partial for "Func" bound to
4583 // "dict". Don't do this when "Func" is already a partial
4584 // that was bound explicitly (pt_auto is FALSE).
4585 tv = STACK_TV_BOT(-1);
4586 if (dict_tv != NULL
4587 && dict_tv->v_type == VAR_DICT
4588 && dict_tv->vval.v_dict != NULL
4589 && (tv->v_type == VAR_FUNC
4590 || (tv->v_type == VAR_PARTIAL
4591 && (tv->vval.v_partial->pt_auto
4592 || tv->vval.v_partial->pt_dict == NULL))))
4593 dict_tv->vval.v_dict =
4594 make_partial(dict_tv->vval.v_dict, tv);
4595 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 }
4597 break;
4598
4599 case ISN_NEGATENR:
4600 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004601 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004602#ifdef FEAT_FLOAT
4603 if (tv->v_type == VAR_FLOAT)
4604 tv->vval.v_float = -tv->vval.v_float;
4605 else
4606#endif
4607 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 break;
4609
4610 case ISN_CHECKNR:
4611 {
4612 int error = FALSE;
4613
4614 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004615 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004617 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 (void)tv_get_number_chk(tv, &error);
4619 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004620 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 }
4622 break;
4623
4624 case ISN_CHECKTYPE:
4625 {
4626 checktype_T *ct = &iptr->isn_arg.type;
4627
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004628 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004629 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004630 if (!ectx->ec_where.wt_variable)
4631 ectx->ec_where.wt_index = ct->ct_arg_idx;
4632 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4633 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004634 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004635 if (!ectx->ec_where.wt_variable)
4636 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004637
4638 // number 0 is FALSE, number 1 is TRUE
4639 if (tv->v_type == VAR_NUMBER
4640 && ct->ct_type->tt_type == VAR_BOOL
4641 && (tv->vval.v_number == 0
4642 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004644 tv->v_type = VAR_BOOL;
4645 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004646 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 }
4648 }
4649 break;
4650
Bram Moolenaar9af78762020-06-16 11:34:42 +02004651 case ISN_CHECKLEN:
4652 {
4653 int min_len = iptr->isn_arg.checklen.cl_min_len;
4654 list_T *list = NULL;
4655
4656 tv = STACK_TV_BOT(-1);
4657 if (tv->v_type == VAR_LIST)
4658 list = tv->vval.v_list;
4659 if (list == NULL || list->lv_len < min_len
4660 || (list->lv_len > min_len
4661 && !iptr->isn_arg.checklen.cl_more_OK))
4662 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004663 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004664 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004665 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004666 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004667 }
4668 }
4669 break;
4670
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004671 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004672 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004673 break;
4674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004676 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 {
4678 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004679 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004681 if (iptr->isn_type == ISN_2BOOL)
4682 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004683 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004684 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004685 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004686 n = !n;
4687 }
4688 else
4689 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004690 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004691 SOURCING_LNUM = iptr->isn_lnum;
4692 n = tv_get_bool_chk(tv, &error);
4693 if (error)
4694 goto on_error;
4695 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 clear_tv(tv);
4697 tv->v_type = VAR_BOOL;
4698 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4699 }
4700 break;
4701
4702 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004703 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004704 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004705 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4706 iptr->isn_type == ISN_2STRING_ANY,
4707 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004708 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 break;
4710
Bram Moolenaar08597872020-12-10 19:43:40 +01004711 case ISN_RANGE:
4712 {
4713 exarg_T ea;
4714 char *errormsg;
4715
Bram Moolenaarece0b872021-01-08 20:40:45 +01004716 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004717 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004718 ea.addr_type = ADDR_LINES;
4719 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004720 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004721 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004722 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004723
Bram Moolenaar35578162021-08-02 19:10:38 +02004724 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004725 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004726 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004727 tv = STACK_TV_BOT(-1);
4728 tv->v_type = VAR_NUMBER;
4729 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004730 if (ea.addr_count == 0)
4731 tv->vval.v_number = curwin->w_cursor.lnum;
4732 else
4733 tv->vval.v_number = ea.line2;
4734 }
4735 break;
4736
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004737 case ISN_PUT:
4738 {
4739 int regname = iptr->isn_arg.put.put_regname;
4740 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4741 char_u *expr = NULL;
4742 int dir = FORWARD;
4743
Bram Moolenaar08597872020-12-10 19:43:40 +01004744 if (lnum < -2)
4745 {
4746 // line number was put on the stack by ISN_RANGE
4747 tv = STACK_TV_BOT(-1);
4748 curwin->w_cursor.lnum = tv->vval.v_number;
4749 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4750 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004751 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004752 }
4753 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004754 // :put! above cursor
4755 dir = BACKWARD;
4756 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004757 {
4758 curwin->w_cursor.lnum = lnum;
4759 if (lnum == 0)
4760 // check_cursor() below will move to line 1
4761 dir = BACKWARD;
4762 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004763
4764 if (regname == '=')
4765 {
4766 tv = STACK_TV_BOT(-1);
4767 if (tv->v_type == VAR_STRING)
4768 expr = tv->vval.v_string;
4769 else
4770 {
4771 expr = typval2string(tv, TRUE); // allocates value
4772 clear_tv(tv);
4773 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004774 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004775 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004776 check_cursor();
4777 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4778 vim_free(expr);
4779 }
4780 break;
4781
Bram Moolenaar02194d22020-10-24 23:08:38 +02004782 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004783 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4784 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4785 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4786 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004787 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4788 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004789 break;
4790
Bram Moolenaar02194d22020-10-24 23:08:38 +02004791 case ISN_CMDMOD_REV:
4792 // filter regprog is owned by the instruction, don't free it
4793 cmdmod.cmod_filter_regmatch.regprog = NULL;
4794 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004795 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4796 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004797 break;
4798
Bram Moolenaar792f7862020-11-23 08:31:18 +01004799 case ISN_UNPACK:
4800 {
4801 int count = iptr->isn_arg.unpack.unp_count;
4802 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4803 list_T *l;
4804 listitem_T *li;
4805 int i;
4806
4807 // Check there is a valid list to unpack.
4808 tv = STACK_TV_BOT(-1);
4809 if (tv->v_type != VAR_LIST)
4810 {
4811 SOURCING_LNUM = iptr->isn_lnum;
4812 emsg(_(e_for_argument_must_be_sequence_of_lists));
4813 goto on_error;
4814 }
4815 l = tv->vval.v_list;
4816 if (l == NULL
4817 || l->lv_len < (semicolon ? count - 1 : count))
4818 {
4819 SOURCING_LNUM = iptr->isn_lnum;
4820 emsg(_(e_list_value_does_not_have_enough_items));
4821 goto on_error;
4822 }
4823 else if (!semicolon && l->lv_len > count)
4824 {
4825 SOURCING_LNUM = iptr->isn_lnum;
4826 emsg(_(e_list_value_has_more_items_than_targets));
4827 goto on_error;
4828 }
4829
4830 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004831 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004832 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004833 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004834
4835 // Variable after semicolon gets a list with the remaining
4836 // items.
4837 if (semicolon)
4838 {
4839 list_T *rem_list =
4840 list_alloc_with_items(l->lv_len - count + 1);
4841
4842 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004843 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004844 tv = STACK_TV_BOT(-count);
4845 tv->vval.v_list = rem_list;
4846 ++rem_list->lv_refcount;
4847 tv->v_lock = 0;
4848 li = l->lv_first;
4849 for (i = 0; i < count - 1; ++i)
4850 li = li->li_next;
4851 for (i = 0; li != NULL; ++i)
4852 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004853 typval_T tvcopy;
4854
4855 copy_tv(&li->li_tv, &tvcopy);
4856 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004857 li = li->li_next;
4858 }
4859 --count;
4860 }
4861
4862 // Produce the values in reverse order, first item last.
4863 li = l->lv_first;
4864 for (i = 0; i < count; ++i)
4865 {
4866 tv = STACK_TV_BOT(-i - 1);
4867 copy_tv(&li->li_tv, tv);
4868 li = li->li_next;
4869 }
4870
4871 list_unref(l);
4872 }
4873 break;
4874
Bram Moolenaarb2049902021-01-24 12:53:53 +01004875 case ISN_PROF_START:
4876 case ISN_PROF_END:
4877 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004878#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004879 funccall_T cookie;
4880 ufunc_T *cur_ufunc =
4881 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004882 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004883
4884 cookie.func = cur_ufunc;
4885 if (iptr->isn_type == ISN_PROF_START)
4886 {
4887 func_line_start(&cookie, iptr->isn_lnum);
4888 // if we get here the instruction is executed
4889 func_line_exec(&cookie);
4890 }
4891 else
4892 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004893#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004894 }
4895 break;
4896
Bram Moolenaare99d4222021-06-13 14:01:26 +02004897 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004898 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004899 break;
4900
Bram Moolenaar389df252020-07-09 21:20:47 +02004901 case ISN_SHUFFLE:
4902 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004903 typval_T tmp_tv;
4904 int item = iptr->isn_arg.shuffle.shfl_item;
4905 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004906
4907 tmp_tv = *STACK_TV_BOT(-item);
4908 for ( ; up > 0 && item > 1; --up)
4909 {
4910 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4911 --item;
4912 }
4913 *STACK_TV_BOT(-item) = tmp_tv;
4914 }
4915 break;
4916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004918 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004920 ectx->ec_where.wt_index = 0;
4921 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922 break;
4923 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004924 continue;
4925
Bram Moolenaard032f342020-07-18 18:13:02 +02004926func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004927 // Restore previous function. If the frame pointer is where we started
4928 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004929 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004930 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004931
Bram Moolenaar4c137212021-04-19 16:48:48 +02004932 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004933 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004934 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004935 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004936
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004937on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004938 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004939 // If "emsg_silent" is set then ignore the error, unless it was set
4940 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004941 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004942 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004943 {
4944 // If a sequence of instructions causes an error while ":silent!"
4945 // was used, restore the stack length and jump ahead to restoring
4946 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004947 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004948 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004949 while (ectx->ec_stack.ga_len
4950 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004951 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004952 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004953 clear_tv(STACK_TV_BOT(0));
4954 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004955 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4956 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004957 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004958 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004959 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004960on_fatal_error:
4961 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004962 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004963 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004964 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 }
4966
4967done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004968 ret = OK;
4969theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004970 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004971 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4972 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004973}
4974
4975/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004976 * Execute the instructions from a VAR_INSTR typeval and put the result in
4977 * "rettv".
4978 * Return OK or FAIL.
4979 */
4980 int
4981exe_typval_instr(typval_T *tv, typval_T *rettv)
4982{
4983 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4984 isn_T *save_instr = ectx->ec_instr;
4985 int save_iidx = ectx->ec_iidx;
4986 int res;
4987
4988 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4989 res = exec_instructions(ectx);
4990 if (res == OK)
4991 {
4992 *rettv = *STACK_TV_BOT(-1);
4993 --ectx->ec_stack.ga_len;
4994 }
4995
4996 ectx->ec_instr = save_instr;
4997 ectx->ec_iidx = save_iidx;
4998
4999 return res;
5000}
5001
5002/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005003 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5004 * "substitute_instr".
5005 */
5006 char_u *
5007exe_substitute_instr(void)
5008{
5009 ectx_T *ectx = substitute_instr->subs_ectx;
5010 isn_T *save_instr = ectx->ec_instr;
5011 int save_iidx = ectx->ec_iidx;
5012 char_u *res;
5013
5014 ectx->ec_instr = substitute_instr->subs_instr;
5015 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005016 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005017 typval_T *tv = STACK_TV_BOT(-1);
5018
Bram Moolenaar27523602021-06-05 21:36:19 +02005019 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005020 --ectx->ec_stack.ga_len;
5021 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005022 }
5023 else
5024 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005025 substitute_instr->subs_status = FAIL;
5026 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028
Bram Moolenaar4c137212021-04-19 16:48:48 +02005029 ectx->ec_instr = save_instr;
5030 ectx->ec_iidx = save_iidx;
5031
5032 return res;
5033}
5034
5035/*
5036 * Call a "def" function from old Vim script.
5037 * Return OK or FAIL.
5038 */
5039 int
5040call_def_function(
5041 ufunc_T *ufunc,
5042 int argc_arg, // nr of arguments
5043 typval_T *argv, // arguments
5044 partial_T *partial, // optional partial for context
5045 typval_T *rettv) // return value
5046{
5047 ectx_T ectx; // execution context
5048 int argc = argc_arg;
5049 typval_T *tv;
5050 int idx;
5051 int ret = FAIL;
5052 int defcount = ufunc->uf_args.ga_len - argc;
5053 sctx_T save_current_sctx = current_sctx;
5054 int did_emsg_before = did_emsg_cumul + did_emsg;
5055 int save_suppress_errthrow = suppress_errthrow;
5056 msglist_T **saved_msg_list = NULL;
5057 msglist_T *private_msg_list = NULL;
5058 int save_emsg_silent_def = emsg_silent_def;
5059 int save_did_emsg_def = did_emsg_def;
5060 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005061 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005062
5063// Get pointer to item in the stack.
5064#undef STACK_TV
5065#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5066
5067// Get pointer to item at the bottom of the stack, -1 is the bottom.
5068#undef STACK_TV_BOT
5069#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5070
5071// Get pointer to a local variable on the stack. Negative for arguments.
5072#undef STACK_TV_VAR
5073#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5074
5075 if (ufunc->uf_def_status == UF_NOT_COMPILED
5076 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005077 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5078 && compile_def_function(ufunc, FALSE,
5079 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005080 {
5081 if (did_emsg_cumul + did_emsg == did_emsg_before)
5082 semsg(_(e_function_is_not_compiled_str),
5083 printable_func_name(ufunc));
5084 return FAIL;
5085 }
5086
5087 {
5088 // Check the function was really compiled.
5089 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5090 + ufunc->uf_dfunc_idx;
5091 if (INSTRUCTIONS(dfunc) == NULL)
5092 {
5093 iemsg("using call_def_function() on not compiled function");
5094 return FAIL;
5095 }
5096 }
5097
5098 // If depth of calling is getting too high, don't execute the function.
5099 orig_funcdepth = funcdepth_get();
5100 if (funcdepth_increment() == FAIL)
5101 return FAIL;
5102
5103 CLEAR_FIELD(ectx);
5104 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5105 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005106 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005107 {
5108 funcdepth_decrement();
5109 return FAIL;
5110 }
5111 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5112 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5113 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005114 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005115
5116 idx = argc - ufunc->uf_args.ga_len;
5117 if (idx > 0 && ufunc->uf_va_name == NULL)
5118 {
5119 if (idx == 1)
5120 emsg(_(e_one_argument_too_many));
5121 else
5122 semsg(_(e_nr_arguments_too_many), idx);
5123 goto failed_early;
5124 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005125 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5126 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005127 {
5128 if (idx == -1)
5129 emsg(_(e_one_argument_too_few));
5130 else
5131 semsg(_(e_nr_arguments_too_few), -idx);
5132 goto failed_early;
5133 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005134
5135 // Put arguments on the stack, but no more than what the function expects.
5136 // A lambda can be called with more arguments than it uses.
5137 for (idx = 0; idx < argc
5138 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5139 ++idx)
5140 {
5141 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5142 && argv[idx].v_type == VAR_SPECIAL
5143 && argv[idx].vval.v_number == VVAL_NONE)
5144 {
5145 // Use the default value.
5146 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5147 }
5148 else
5149 {
5150 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5151 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005152 ufunc->uf_arg_types[idx], &argv[idx],
5153 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005154 goto failed_early;
5155 copy_tv(&argv[idx], STACK_TV_BOT(0));
5156 }
5157 ++ectx.ec_stack.ga_len;
5158 }
5159
5160 // Turn varargs into a list. Empty list if no args.
5161 if (ufunc->uf_va_name != NULL)
5162 {
5163 int vararg_count = argc - ufunc->uf_args.ga_len;
5164
5165 if (vararg_count < 0)
5166 vararg_count = 0;
5167 else
5168 argc -= vararg_count;
5169 if (exe_newlist(vararg_count, &ectx) == FAIL)
5170 goto failed_early;
5171
5172 // Check the type of the list items.
5173 tv = STACK_TV_BOT(-1);
5174 if (ufunc->uf_va_type != NULL
5175 && ufunc->uf_va_type != &t_list_any
5176 && ufunc->uf_va_type->tt_member != &t_any
5177 && tv->vval.v_list != NULL)
5178 {
5179 type_T *expected = ufunc->uf_va_type->tt_member;
5180 listitem_T *li = tv->vval.v_list->lv_first;
5181
5182 for (idx = 0; idx < vararg_count; ++idx)
5183 {
5184 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005185 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005186 goto failed_early;
5187 li = li->li_next;
5188 }
5189 }
5190
5191 if (defcount > 0)
5192 // Move varargs list to below missing default arguments.
5193 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5194 --ectx.ec_stack.ga_len;
5195 }
5196
5197 // Make space for omitted arguments, will store default value below.
5198 // Any varargs list goes after them.
5199 if (defcount > 0)
5200 for (idx = 0; idx < defcount; ++idx)
5201 {
5202 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5203 ++ectx.ec_stack.ga_len;
5204 }
5205 if (ufunc->uf_va_name != NULL)
5206 ++ectx.ec_stack.ga_len;
5207
5208 // Frame pointer points to just after arguments.
5209 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5210 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5211
5212 {
5213 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5214 + ufunc->uf_dfunc_idx;
5215 ufunc_T *base_ufunc = dfunc->df_ufunc;
5216
5217 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5218 // by copy_func().
5219 if (partial != NULL || base_ufunc->uf_partial != NULL)
5220 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005221 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5222 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005223 goto failed_early;
5224 if (partial != NULL)
5225 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005226 outer_T *outer = get_pt_outer(partial);
5227
5228 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005229 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005230 if (current_ectx != NULL)
5231 {
5232 if (current_ectx->ec_outer_ref != NULL
5233 && current_ectx->ec_outer_ref->or_outer != NULL)
5234 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005235 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005236 }
5237 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005238 }
5239 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005240 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005241 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005242 ++partial->pt_refcount;
5243 ectx.ec_outer_ref->or_partial = partial;
5244 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005245 }
5246 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005247 {
5248 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5249 ++base_ufunc->uf_partial->pt_refcount;
5250 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5251 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005252 }
5253 }
5254
5255 // dummy frame entries
5256 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5257 {
5258 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5259 ++ectx.ec_stack.ga_len;
5260 }
5261
5262 {
5263 // Reserve space for local variables and any closure reference count.
5264 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5265 + ufunc->uf_dfunc_idx;
5266
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005267 // Initialize variables to zero. That avoids having to generate
5268 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005269 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005270 {
5271 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5272 STACK_TV_VAR(idx)->vval.v_number = 0;
5273 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005274 ectx.ec_stack.ga_len += dfunc->df_varcount;
5275 if (dfunc->df_has_closure)
5276 {
5277 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5278 STACK_TV_VAR(idx)->vval.v_number = 0;
5279 ++ectx.ec_stack.ga_len;
5280 }
5281
5282 ectx.ec_instr = INSTRUCTIONS(dfunc);
5283 }
5284
5285 // Following errors are in the function, not the caller.
5286 // Commands behave like vim9script.
5287 estack_push_ufunc(ufunc, 1);
5288 current_sctx = ufunc->uf_script_ctx;
5289 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5290
5291 // Use a specific location for storing error messages to be converted to an
5292 // exception.
5293 saved_msg_list = msg_list;
5294 msg_list = &private_msg_list;
5295
5296 // Do turn errors into exceptions.
5297 suppress_errthrow = FALSE;
5298
5299 // Do not delete the function while executing it.
5300 ++ufunc->uf_calls;
5301
5302 // When ":silent!" was used before calling then we still abort the
5303 // function. If ":silent!" is used in the function then we don't.
5304 emsg_silent_def = emsg_silent;
5305 did_emsg_def = 0;
5306
5307 ectx.ec_where.wt_index = 0;
5308 ectx.ec_where.wt_variable = FALSE;
5309
5310 // Execute the instructions until done.
5311 ret = exec_instructions(&ectx);
5312 if (ret == OK)
5313 {
5314 // function finished, get result from the stack.
5315 if (ufunc->uf_ret_type == &t_void)
5316 {
5317 rettv->v_type = VAR_VOID;
5318 }
5319 else
5320 {
5321 tv = STACK_TV_BOT(-1);
5322 *rettv = *tv;
5323 tv->v_type = VAR_UNKNOWN;
5324 }
5325 }
5326
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005327 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005328 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5329 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005330
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005331 // Deal with any remaining closures, they may be in use somewhere.
5332 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005333 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005334 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005335 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005336 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005337
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005338 estack_pop();
5339 current_sctx = save_current_sctx;
5340
Bram Moolenaar2336c372021-12-06 15:06:54 +00005341 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5342 // Function was unreferenced while being used, free it now.
5343 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005344
Bram Moolenaar352134b2020-10-17 22:04:08 +02005345 if (*msg_list != NULL && saved_msg_list != NULL)
5346 {
5347 msglist_T **plist = saved_msg_list;
5348
5349 // Append entries from the current msg_list (uncaught exceptions) to
5350 // the saved msg_list.
5351 while (*plist != NULL)
5352 plist = &(*plist)->next;
5353
5354 *plist = *msg_list;
5355 }
5356 msg_list = saved_msg_list;
5357
Bram Moolenaar4c137212021-04-19 16:48:48 +02005358 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005359 {
5360 cmdmod.cmod_filter_regmatch.regprog = NULL;
5361 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005362 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005363 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005364 emsg_silent_def = save_emsg_silent_def;
5365 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005366
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005367failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005368 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5370 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005371 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005374 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005375 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005376 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005377 if (ectx.ec_outer_ref->or_outer_allocated)
5378 vim_free(ectx.ec_outer_ref->or_outer);
5379 partial_unref(ectx.ec_outer_ref->or_partial);
5380 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005381 }
5382
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005383 // Not sure if this is necessary.
5384 suppress_errthrow = save_suppress_errthrow;
5385
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005386 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5387 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005388 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005389 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005390 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005391 return ret;
5392}
5393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005395 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5396 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5397 * "pfx" is prefixed to every line.
5398 */
5399 static void
5400list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5401{
5402 int line_idx = 0;
5403 int prev_current = 0;
5404 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005405 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005406
5407 for (current = 0; current < instr_count; ++current)
5408 {
5409 isn_T *iptr = &instr[current];
5410 char *line;
5411
5412 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005413 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005414 while (line_idx < iptr->isn_lnum
5415 && line_idx < ufunc->uf_lines.ga_len)
5416 {
5417 if (current > prev_current)
5418 {
5419 msg_puts("\n\n");
5420 prev_current = current;
5421 }
5422 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5423 if (line != NULL)
5424 msg(line);
5425 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005426 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5427 {
5428 int first_def_arg = ufunc->uf_args.ga_len
5429 - ufunc->uf_def_args.ga_len;
5430
5431 if (def_arg_idx > 0)
5432 msg_puts("\n\n");
5433 msg_start();
5434 msg_puts(" ");
5435 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5436 first_def_arg + def_arg_idx]);
5437 msg_puts(" = ");
5438 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5439 msg_clr_eos();
5440 msg_end();
5441 }
5442 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005443
5444 switch (iptr->isn_type)
5445 {
5446 case ISN_EXEC:
5447 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5448 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005449 case ISN_EXEC_SPLIT:
5450 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5451 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005452 case ISN_EXECRANGE:
5453 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5454 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005455 case ISN_LEGACY_EVAL:
5456 smsg("%s%4d EVAL legacy %s", pfx, current,
5457 iptr->isn_arg.string);
5458 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005459 case ISN_REDIRSTART:
5460 smsg("%s%4d REDIR", pfx, current);
5461 break;
5462 case ISN_REDIREND:
5463 smsg("%s%4d REDIR END%s", pfx, current,
5464 iptr->isn_arg.number ? " append" : "");
5465 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005466 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005467#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005468 smsg("%s%4d CEXPR pre %s", pfx, current,
5469 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005470#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005471 break;
5472 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005473#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005474 {
5475 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5476
5477 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5478 cexpr_get_auname(cer->cer_cmdidx),
5479 cer->cer_forceit ? "!" : "",
5480 cer->cer_cmdline);
5481 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005482#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005483 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005484 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005485 smsg("%s%4d INSTR", pfx, current);
5486 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5487 msg(" -------------");
5488 break;
5489 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005490 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005491 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5492
5493 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005494 }
5495 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005496 case ISN_SUBSTITUTE:
5497 {
5498 subs_T *subs = &iptr->isn_arg.subs;
5499
5500 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5501 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5502 msg(" -------------");
5503 }
5504 break;
5505 case ISN_EXECCONCAT:
5506 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5507 (varnumber_T)iptr->isn_arg.number);
5508 break;
5509 case ISN_ECHO:
5510 {
5511 echo_T *echo = &iptr->isn_arg.echo;
5512
5513 smsg("%s%4d %s %d", pfx, current,
5514 echo->echo_with_white ? "ECHO" : "ECHON",
5515 echo->echo_count);
5516 }
5517 break;
5518 case ISN_EXECUTE:
5519 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005520 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005521 break;
5522 case ISN_ECHOMSG:
5523 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005524 (varnumber_T)(iptr->isn_arg.number));
5525 break;
5526 case ISN_ECHOCONSOLE:
5527 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5528 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005529 break;
5530 case ISN_ECHOERR:
5531 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005532 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005533 break;
5534 case ISN_LOAD:
5535 {
5536 if (iptr->isn_arg.number < 0)
5537 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5538 (varnumber_T)(iptr->isn_arg.number
5539 + STACK_FRAME_SIZE));
5540 else
5541 smsg("%s%4d LOAD $%lld", pfx, current,
5542 (varnumber_T)(iptr->isn_arg.number));
5543 }
5544 break;
5545 case ISN_LOADOUTER:
5546 {
5547 if (iptr->isn_arg.number < 0)
5548 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5549 iptr->isn_arg.outer.outer_depth,
5550 iptr->isn_arg.outer.outer_idx
5551 + STACK_FRAME_SIZE);
5552 else
5553 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5554 iptr->isn_arg.outer.outer_depth,
5555 iptr->isn_arg.outer.outer_idx);
5556 }
5557 break;
5558 case ISN_LOADV:
5559 smsg("%s%4d LOADV v:%s", pfx, current,
5560 get_vim_var_name(iptr->isn_arg.number));
5561 break;
5562 case ISN_LOADSCRIPT:
5563 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005564 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5565 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5566 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005567
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005568 sv = get_script_svar(sref, -1);
5569 if (sv == NULL)
5570 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5571 pfx, current, si->sn_name);
5572 else
5573 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005574 sv->sv_name,
5575 sref->sref_idx,
5576 si->sn_name);
5577 }
5578 break;
5579 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005580 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005581 {
5582 scriptitem_T *si = SCRIPT_ITEM(
5583 iptr->isn_arg.loadstore.ls_sid);
5584
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005585 smsg("%s%4d %s s:%s from %s", pfx, current,
5586 iptr->isn_type == ISN_LOADS ? "LOADS"
5587 : "LOADEXPORT",
5588 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005589 }
5590 break;
5591 case ISN_LOADAUTO:
5592 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5593 break;
5594 case ISN_LOADG:
5595 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5596 break;
5597 case ISN_LOADB:
5598 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5599 break;
5600 case ISN_LOADW:
5601 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5602 break;
5603 case ISN_LOADT:
5604 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5605 break;
5606 case ISN_LOADGDICT:
5607 smsg("%s%4d LOAD g:", pfx, current);
5608 break;
5609 case ISN_LOADBDICT:
5610 smsg("%s%4d LOAD b:", pfx, current);
5611 break;
5612 case ISN_LOADWDICT:
5613 smsg("%s%4d LOAD w:", pfx, current);
5614 break;
5615 case ISN_LOADTDICT:
5616 smsg("%s%4d LOAD t:", pfx, current);
5617 break;
5618 case ISN_LOADOPT:
5619 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5620 break;
5621 case ISN_LOADENV:
5622 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5623 break;
5624 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005625 smsg("%s%4d LOADREG @%c", pfx, current,
5626 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005627 break;
5628
5629 case ISN_STORE:
5630 if (iptr->isn_arg.number < 0)
5631 smsg("%s%4d STORE arg[%lld]", pfx, current,
5632 iptr->isn_arg.number + STACK_FRAME_SIZE);
5633 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005634 smsg("%s%4d STORE $%lld", pfx, current,
5635 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005636 break;
5637 case ISN_STOREOUTER:
5638 {
5639 if (iptr->isn_arg.number < 0)
5640 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5641 iptr->isn_arg.outer.outer_depth,
5642 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5643 else
5644 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5645 iptr->isn_arg.outer.outer_depth,
5646 iptr->isn_arg.outer.outer_idx);
5647 }
5648 break;
5649 case ISN_STOREV:
5650 smsg("%s%4d STOREV v:%s", pfx, current,
5651 get_vim_var_name(iptr->isn_arg.number));
5652 break;
5653 case ISN_STOREAUTO:
5654 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5655 break;
5656 case ISN_STOREG:
5657 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5658 break;
5659 case ISN_STOREB:
5660 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5661 break;
5662 case ISN_STOREW:
5663 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5664 break;
5665 case ISN_STORET:
5666 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5667 break;
5668 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005669 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005670 {
5671 scriptitem_T *si = SCRIPT_ITEM(
5672 iptr->isn_arg.loadstore.ls_sid);
5673
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005674 smsg("%s%4d %s %s in %s", pfx, current,
5675 iptr->isn_type == ISN_STORES
5676 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005677 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5678 }
5679 break;
5680 case ISN_STORESCRIPT:
5681 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005682 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5683 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5684 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005685
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005686 sv = get_script_svar(sref, -1);
5687 if (sv == NULL)
5688 smsg("%s%4d STORESCRIPT [deleted] in %s",
5689 pfx, current, si->sn_name);
5690 else
5691 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005692 sv->sv_name,
5693 sref->sref_idx,
5694 si->sn_name);
5695 }
5696 break;
5697 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005698 case ISN_STOREFUNCOPT:
5699 smsg("%s%4d %s &%s", pfx, current,
5700 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005701 iptr->isn_arg.storeopt.so_name);
5702 break;
5703 case ISN_STOREENV:
5704 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5705 break;
5706 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005707 smsg("%s%4d STOREREG @%c", pfx, current,
5708 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005709 break;
5710 case ISN_STORENR:
5711 smsg("%s%4d STORE %lld in $%d", pfx, current,
5712 iptr->isn_arg.storenr.stnr_val,
5713 iptr->isn_arg.storenr.stnr_idx);
5714 break;
5715
5716 case ISN_STOREINDEX:
5717 smsg("%s%4d STOREINDEX %s", pfx, current,
5718 vartype_name(iptr->isn_arg.vartype));
5719 break;
5720
5721 case ISN_STORERANGE:
5722 smsg("%s%4d STORERANGE", pfx, current);
5723 break;
5724
5725 // constants
5726 case ISN_PUSHNR:
5727 smsg("%s%4d PUSHNR %lld", pfx, current,
5728 (varnumber_T)(iptr->isn_arg.number));
5729 break;
5730 case ISN_PUSHBOOL:
5731 case ISN_PUSHSPEC:
5732 smsg("%s%4d PUSH %s", pfx, current,
5733 get_var_special_name(iptr->isn_arg.number));
5734 break;
5735 case ISN_PUSHF:
5736#ifdef FEAT_FLOAT
5737 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5738#endif
5739 break;
5740 case ISN_PUSHS:
5741 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5742 break;
5743 case ISN_PUSHBLOB:
5744 {
5745 char_u *r;
5746 char_u numbuf[NUMBUFLEN];
5747 char_u *tofree;
5748
5749 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5750 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5751 vim_free(tofree);
5752 }
5753 break;
5754 case ISN_PUSHFUNC:
5755 {
5756 char *name = (char *)iptr->isn_arg.string;
5757
5758 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5759 name == NULL ? "[none]" : name);
5760 }
5761 break;
5762 case ISN_PUSHCHANNEL:
5763#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005764 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005765#endif
5766 break;
5767 case ISN_PUSHJOB:
5768#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005769 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005770#endif
5771 break;
5772 case ISN_PUSHEXC:
5773 smsg("%s%4d PUSH v:exception", pfx, current);
5774 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005775 case ISN_AUTOLOAD:
5776 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5777 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005778 case ISN_UNLET:
5779 smsg("%s%4d UNLET%s %s", pfx, current,
5780 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5781 iptr->isn_arg.unlet.ul_name);
5782 break;
5783 case ISN_UNLETENV:
5784 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5785 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5786 iptr->isn_arg.unlet.ul_name);
5787 break;
5788 case ISN_UNLETINDEX:
5789 smsg("%s%4d UNLETINDEX", pfx, current);
5790 break;
5791 case ISN_UNLETRANGE:
5792 smsg("%s%4d UNLETRANGE", pfx, current);
5793 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005794 case ISN_LOCKUNLOCK:
5795 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5796 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005797 case ISN_LOCKCONST:
5798 smsg("%s%4d LOCKCONST", pfx, current);
5799 break;
5800 case ISN_NEWLIST:
5801 smsg("%s%4d NEWLIST size %lld", pfx, current,
5802 (varnumber_T)(iptr->isn_arg.number));
5803 break;
5804 case ISN_NEWDICT:
5805 smsg("%s%4d NEWDICT size %lld", pfx, current,
5806 (varnumber_T)(iptr->isn_arg.number));
5807 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005808 case ISN_NEWPARTIAL:
5809 smsg("%s%4d NEWPARTIAL", pfx, current);
5810 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005811
5812 // function call
5813 case ISN_BCALL:
5814 {
5815 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5816
5817 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5818 internal_func_name(cbfunc->cbf_idx),
5819 cbfunc->cbf_argcount);
5820 }
5821 break;
5822 case ISN_DCALL:
5823 {
5824 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5825 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5826 + cdfunc->cdf_idx;
5827
5828 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005829 printable_func_name(df->df_ufunc),
5830 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005831 }
5832 break;
5833 case ISN_UCALL:
5834 {
5835 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5836
5837 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5838 cufunc->cuf_name, cufunc->cuf_argcount);
5839 }
5840 break;
5841 case ISN_PCALL:
5842 {
5843 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5844
5845 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5846 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5847 }
5848 break;
5849 case ISN_PCALL_END:
5850 smsg("%s%4d PCALL end", pfx, current);
5851 break;
5852 case ISN_RETURN:
5853 smsg("%s%4d RETURN", pfx, current);
5854 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005855 case ISN_RETURN_VOID:
5856 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005857 break;
5858 case ISN_FUNCREF:
5859 {
5860 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005861 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005862
Bram Moolenaar38453522021-11-28 22:00:12 +00005863 if (funcref->fr_func_name == NULL)
5864 {
5865 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5866 + funcref->fr_dfunc_idx;
5867 name = df->df_ufunc->uf_name;
5868 }
5869 else
5870 name = funcref->fr_func_name;
5871 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005872 }
5873 break;
5874
5875 case ISN_NEWFUNC:
5876 {
5877 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5878
5879 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5880 newfunc->nf_lambda, newfunc->nf_global);
5881 }
5882 break;
5883
5884 case ISN_DEF:
5885 {
5886 char_u *name = iptr->isn_arg.string;
5887
5888 smsg("%s%4d DEF %s", pfx, current,
5889 name == NULL ? (char_u *)"" : name);
5890 }
5891 break;
5892
5893 case ISN_JUMP:
5894 {
5895 char *when = "?";
5896
5897 switch (iptr->isn_arg.jump.jump_when)
5898 {
5899 case JUMP_ALWAYS:
5900 when = "JUMP";
5901 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005902 case JUMP_NEVER:
5903 iemsg("JUMP_NEVER should not be used");
5904 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005905 case JUMP_AND_KEEP_IF_TRUE:
5906 when = "JUMP_AND_KEEP_IF_TRUE";
5907 break;
5908 case JUMP_IF_FALSE:
5909 when = "JUMP_IF_FALSE";
5910 break;
5911 case JUMP_AND_KEEP_IF_FALSE:
5912 when = "JUMP_AND_KEEP_IF_FALSE";
5913 break;
5914 case JUMP_IF_COND_FALSE:
5915 when = "JUMP_IF_COND_FALSE";
5916 break;
5917 case JUMP_IF_COND_TRUE:
5918 when = "JUMP_IF_COND_TRUE";
5919 break;
5920 }
5921 smsg("%s%4d %s -> %d", pfx, current, when,
5922 iptr->isn_arg.jump.jump_where);
5923 }
5924 break;
5925
5926 case ISN_JUMP_IF_ARG_SET:
5927 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5928 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5929 iptr->isn_arg.jump.jump_where);
5930 break;
5931
5932 case ISN_FOR:
5933 {
5934 forloop_T *forloop = &iptr->isn_arg.forloop;
5935
5936 smsg("%s%4d FOR $%d -> %d", pfx, current,
5937 forloop->for_idx, forloop->for_end);
5938 }
5939 break;
5940
5941 case ISN_TRY:
5942 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005943 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005944
5945 if (try->try_ref->try_finally == 0)
5946 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5947 pfx, current,
5948 try->try_ref->try_catch,
5949 try->try_ref->try_endtry);
5950 else
5951 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5952 pfx, current,
5953 try->try_ref->try_catch,
5954 try->try_ref->try_finally,
5955 try->try_ref->try_endtry);
5956 }
5957 break;
5958 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005959 smsg("%s%4d CATCH", pfx, current);
5960 break;
5961 case ISN_TRYCONT:
5962 {
5963 trycont_T *trycont = &iptr->isn_arg.trycont;
5964
5965 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5966 trycont->tct_levels,
5967 trycont->tct_levels == 1 ? "" : "s",
5968 trycont->tct_where);
5969 }
5970 break;
5971 case ISN_FINALLY:
5972 smsg("%s%4d FINALLY", pfx, current);
5973 break;
5974 case ISN_ENDTRY:
5975 smsg("%s%4d ENDTRY", pfx, current);
5976 break;
5977 case ISN_THROW:
5978 smsg("%s%4d THROW", pfx, current);
5979 break;
5980
5981 // expression operations on number
5982 case ISN_OPNR:
5983 case ISN_OPFLOAT:
5984 case ISN_OPANY:
5985 {
5986 char *what;
5987 char *ins;
5988
5989 switch (iptr->isn_arg.op.op_type)
5990 {
5991 case EXPR_MULT: what = "*"; break;
5992 case EXPR_DIV: what = "/"; break;
5993 case EXPR_REM: what = "%"; break;
5994 case EXPR_SUB: what = "-"; break;
5995 case EXPR_ADD: what = "+"; break;
5996 default: what = "???"; break;
5997 }
5998 switch (iptr->isn_type)
5999 {
6000 case ISN_OPNR: ins = "OPNR"; break;
6001 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6002 case ISN_OPANY: ins = "OPANY"; break;
6003 default: ins = "???"; break;
6004 }
6005 smsg("%s%4d %s %s", pfx, current, ins, what);
6006 }
6007 break;
6008
6009 case ISN_COMPAREBOOL:
6010 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006011 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006012 case ISN_COMPARENR:
6013 case ISN_COMPAREFLOAT:
6014 case ISN_COMPARESTRING:
6015 case ISN_COMPAREBLOB:
6016 case ISN_COMPARELIST:
6017 case ISN_COMPAREDICT:
6018 case ISN_COMPAREFUNC:
6019 case ISN_COMPAREANY:
6020 {
6021 char *p;
6022 char buf[10];
6023 char *type;
6024
6025 switch (iptr->isn_arg.op.op_type)
6026 {
6027 case EXPR_EQUAL: p = "=="; break;
6028 case EXPR_NEQUAL: p = "!="; break;
6029 case EXPR_GREATER: p = ">"; break;
6030 case EXPR_GEQUAL: p = ">="; break;
6031 case EXPR_SMALLER: p = "<"; break;
6032 case EXPR_SEQUAL: p = "<="; break;
6033 case EXPR_MATCH: p = "=~"; break;
6034 case EXPR_IS: p = "is"; break;
6035 case EXPR_ISNOT: p = "isnot"; break;
6036 case EXPR_NOMATCH: p = "!~"; break;
6037 default: p = "???"; break;
6038 }
6039 STRCPY(buf, p);
6040 if (iptr->isn_arg.op.op_ic == TRUE)
6041 strcat(buf, "?");
6042 switch(iptr->isn_type)
6043 {
6044 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6045 case ISN_COMPARESPECIAL:
6046 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006047 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006048 case ISN_COMPARENR: type = "COMPARENR"; break;
6049 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6050 case ISN_COMPARESTRING:
6051 type = "COMPARESTRING"; break;
6052 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6053 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6054 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6055 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6056 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6057 default: type = "???"; break;
6058 }
6059
6060 smsg("%s%4d %s %s", pfx, current, type, buf);
6061 }
6062 break;
6063
6064 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6065 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6066
6067 // expression operations
6068 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
6069 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6070 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6071 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6072 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6073 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6074 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6075 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6076 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6077 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6078 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6079 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006080 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006081 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6082 iptr->isn_arg.getitem.gi_index,
6083 iptr->isn_arg.getitem.gi_with_op ?
6084 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006085 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6086 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6087 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006088 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6089 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6090
Bram Moolenaar4c137212021-04-19 16:48:48 +02006091 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6092
6093 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6094 case ISN_CHECKTYPE:
6095 {
6096 checktype_T *ct = &iptr->isn_arg.type;
6097 char *tofree;
6098
6099 if (ct->ct_arg_idx == 0)
6100 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6101 type_name(ct->ct_type, &tofree),
6102 (int)ct->ct_off);
6103 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006104 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6105 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006106 type_name(ct->ct_type, &tofree),
6107 (int)ct->ct_off,
6108 (int)ct->ct_arg_idx);
6109 vim_free(tofree);
6110 break;
6111 }
6112 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6113 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6114 iptr->isn_arg.checklen.cl_min_len);
6115 break;
6116 case ISN_SETTYPE:
6117 {
6118 char *tofree;
6119
6120 smsg("%s%4d SETTYPE %s", pfx, current,
6121 type_name(iptr->isn_arg.type.ct_type, &tofree));
6122 vim_free(tofree);
6123 break;
6124 }
6125 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006126 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6127 smsg("%s%4d INVERT %d (!val)", pfx, current,
6128 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006129 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006130 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6131 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006132 break;
6133 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006134 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006135 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006136 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6137 pfx, current,
6138 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006139 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006140 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6141 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006142 break;
6143 case ISN_PUT:
6144 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6145 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006146 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006147 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6148 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006149 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006150 else
6151 smsg("%s%4d PUT %c %ld", pfx, current,
6152 iptr->isn_arg.put.put_regname,
6153 (long)iptr->isn_arg.put.put_lnum);
6154 break;
6155
Bram Moolenaar4c137212021-04-19 16:48:48 +02006156 case ISN_CMDMOD:
6157 {
6158 char_u *buf;
6159 size_t len = produce_cmdmods(
6160 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6161
6162 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006163 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006164 {
6165 (void)produce_cmdmods(
6166 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6167 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6168 vim_free(buf);
6169 }
6170 break;
6171 }
6172 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6173
6174 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006175 smsg("%s%4d PROFILE START line %d", pfx, current,
6176 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006177 break;
6178
6179 case ISN_PROF_END:
6180 smsg("%s%4d PROFILE END", pfx, current);
6181 break;
6182
Bram Moolenaare99d4222021-06-13 14:01:26 +02006183 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006184 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6185 iptr->isn_arg.debug.dbg_break_lnum + 1,
6186 iptr->isn_lnum,
6187 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006188 break;
6189
Bram Moolenaar4c137212021-04-19 16:48:48 +02006190 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6191 iptr->isn_arg.unpack.unp_count,
6192 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6193 break;
6194 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6195 iptr->isn_arg.shuffle.shfl_item,
6196 iptr->isn_arg.shuffle.shfl_up);
6197 break;
6198 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6199
6200 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6201 return;
6202 }
6203
6204 out_flush(); // output one line at a time
6205 ui_breakcheck();
6206 if (got_int)
6207 break;
6208 }
6209}
6210
6211/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006212 * Handle command line completion for the :disassemble command.
6213 */
6214 void
6215set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6216{
6217 char_u *p;
6218
6219 // Default: expand user functions, "debug" and "profile"
6220 xp->xp_context = EXPAND_DISASSEMBLE;
6221 xp->xp_pattern = arg;
6222
6223 // first argument already typed: only user function names
6224 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6225 {
6226 xp->xp_context = EXPAND_USER_FUNC;
6227 xp->xp_pattern = skipwhite(p);
6228 }
6229}
6230
6231/*
6232 * Function given to ExpandGeneric() to obtain the list of :disassemble
6233 * arguments.
6234 */
6235 char_u *
6236get_disassemble_argument(expand_T *xp, int idx)
6237{
6238 if (idx == 0)
6239 return (char_u *)"debug";
6240 if (idx == 1)
6241 return (char_u *)"profile";
6242 return get_user_func_name(xp, idx - 2);
6243}
6244
6245/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006246 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006247 * We don't really need this at runtime, but we do have tests that require it,
6248 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006249 */
6250 void
6251ex_disassemble(exarg_T *eap)
6252{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006253 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006254 char_u *fname;
6255 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256 dfunc_T *dfunc;
6257 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006258 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006259 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006260 compiletype_T compile_type = CT_NONE;
6261
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006262 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006263 {
6264 compile_type = CT_PROFILE;
6265 arg = skipwhite(arg + 7);
6266 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006267 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006268 {
6269 compile_type = CT_DEBUG;
6270 arg = skipwhite(arg + 5);
6271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006273 if (STRNCMP(arg, "<lambda>", 8) == 0)
6274 {
6275 arg += 8;
6276 (void)getdigits(&arg);
6277 fname = vim_strnsave(eap->arg, arg - eap->arg);
6278 }
6279 else
6280 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006281 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006282 if (fname == NULL)
6283 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006284 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006285 return;
6286 }
6287
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006288 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006289 if (ufunc == NULL)
6290 {
6291 char_u *p = untrans_function_name(fname);
6292
6293 if (p != NULL)
6294 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006295 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006296 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006297 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298 if (ufunc == NULL)
6299 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006300 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 return;
6302 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006303 if (func_needs_compiling(ufunc, compile_type)
6304 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006305 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006306 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006308 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 return;
6310 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006311 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312
6313 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006314 switch (compile_type)
6315 {
6316 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006317#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006318 instr = dfunc->df_instr_prof;
6319 instr_count = dfunc->df_instr_prof_count;
6320 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006321#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006322 // FALLTHROUGH
6323 case CT_NONE:
6324 instr = dfunc->df_instr;
6325 instr_count = dfunc->df_instr_count;
6326 break;
6327 case CT_DEBUG:
6328 instr = dfunc->df_instr_debug;
6329 instr_count = dfunc->df_instr_debug_count;
6330 break;
6331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332
Bram Moolenaar4c137212021-04-19 16:48:48 +02006333 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334}
6335
6336/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006337 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006338 * list, etc. Mostly like what JavaScript does, except that empty list and
6339 * empty dictionary are FALSE.
6340 */
6341 int
6342tv2bool(typval_T *tv)
6343{
6344 switch (tv->v_type)
6345 {
6346 case VAR_NUMBER:
6347 return tv->vval.v_number != 0;
6348 case VAR_FLOAT:
6349#ifdef FEAT_FLOAT
6350 return tv->vval.v_float != 0.0;
6351#else
6352 break;
6353#endif
6354 case VAR_PARTIAL:
6355 return tv->vval.v_partial != NULL;
6356 case VAR_FUNC:
6357 case VAR_STRING:
6358 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6359 case VAR_LIST:
6360 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6361 case VAR_DICT:
6362 return tv->vval.v_dict != NULL
6363 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6364 case VAR_BOOL:
6365 case VAR_SPECIAL:
6366 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6367 case VAR_JOB:
6368#ifdef FEAT_JOB_CHANNEL
6369 return tv->vval.v_job != NULL;
6370#else
6371 break;
6372#endif
6373 case VAR_CHANNEL:
6374#ifdef FEAT_JOB_CHANNEL
6375 return tv->vval.v_channel != NULL;
6376#else
6377 break;
6378#endif
6379 case VAR_BLOB:
6380 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6381 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006382 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006384 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006385 break;
6386 }
6387 return FALSE;
6388}
6389
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006390 void
6391emsg_using_string_as(typval_T *tv, int as_number)
6392{
6393 semsg(_(as_number ? e_using_string_as_number_str
6394 : e_using_string_as_bool_str),
6395 tv->vval.v_string == NULL
6396 ? (char_u *)"" : tv->vval.v_string);
6397}
6398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399/*
6400 * If "tv" is a string give an error and return FAIL.
6401 */
6402 int
6403check_not_string(typval_T *tv)
6404{
6405 if (tv->v_type == VAR_STRING)
6406 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006407 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 clear_tv(tv);
6409 return FAIL;
6410 }
6411 return OK;
6412}
6413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006414
6415#endif // FEAT_EVAL