blob: 2d128e05b4f12493d1a78246f4630a61a1ece0a1 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010067// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100104// Get pointer to item in the stack.
105#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200108#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100110// Get pointer to a local variable on the stack. Negative for arguments.
111#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
112
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200113 void
114to_string_error(vartype_T vartype)
115{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200116 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200117}
118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100120 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 */
122 static int
123ufunc_argcount(ufunc_T *ufunc)
124{
125 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
126}
127
128/*
LemonBoy372bcce2022-04-25 12:43:20 +0100129 * Create a new string from "count" items at the bottom of the stack.
130 * A trailing NUL is appended.
131 * When "count" is zero an empty string is added to the stack.
132 */
133 static int
134exe_concat(int count, ectx_T *ectx)
135{
136 int idx;
137 int len = 0;
138 typval_T *tv;
139 garray_T ga;
140
141 ga_init2(&ga, sizeof(char), 1);
142 // Preallocate enough space for the whole string to avoid having to grow
143 // and copy.
144 for (idx = 0; idx < count; ++idx)
145 {
146 tv = STACK_TV_BOT(idx - count);
147 if (tv->vval.v_string != NULL)
148 len += (int)STRLEN(tv->vval.v_string);
149 }
150
151 if (ga_grow(&ga, len + 1) == FAIL)
152 return FAIL;
153
154 for (idx = 0; idx < count; ++idx)
155 {
156 tv = STACK_TV_BOT(idx - count);
157 ga_concat(&ga, tv->vval.v_string);
158 clear_tv(tv);
159 }
160
161 // add a terminating NUL
162 ga_append(&ga, NUL);
163
164 ectx->ec_stack.ga_len -= count - 1;
165 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
166
167 return OK;
168}
169
170/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200171 * Create a new list from "count" items at the bottom of the stack.
172 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100173 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200174 */
175 static int
176exe_newlist(int count, ectx_T *ectx)
177{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 int idx;
180 typval_T *tv;
181
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100182 if (count >= 0)
183 {
184 list = list_alloc_with_items(count);
185 if (list == NULL)
186 return FAIL;
187 for (idx = 0; idx < count; ++idx)
188 list_set_item(list, idx, STACK_TV_BOT(idx - count));
189 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190
191 if (count > 0)
192 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200193 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100194 {
195 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 else
199 ++ectx->ec_stack.ga_len;
200 tv = STACK_TV_BOT(-1);
201 tv->v_type = VAR_LIST;
202 tv->vval.v_list = list;
Bram Moolenaar566badc2022-09-18 13:46:08 +0100203 tv->v_lock = 0;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100204 if (list != NULL)
205 ++list->lv_refcount;
206 return OK;
207}
208
209/*
210 * Implementation of ISN_NEWDICT.
211 * Returns FAIL on total failure, MAYBE on error.
212 */
213 static int
214exe_newdict(int count, ectx_T *ectx)
215{
216 dict_T *dict = NULL;
217 dictitem_T *item;
218 char_u *key;
219 int idx;
220 typval_T *tv;
221
222 if (count >= 0)
223 {
224 dict = dict_alloc();
225 if (unlikely(dict == NULL))
226 return FAIL;
227 for (idx = 0; idx < count; ++idx)
228 {
229 // have already checked key type is VAR_STRING
230 tv = STACK_TV_BOT(2 * (idx - count));
231 // check key is unique
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000232 key = tv->vval.v_string == NULL ? (char_u *)"" : tv->vval.v_string;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100233 item = dict_find(dict, key, -1);
234 if (item != NULL)
235 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000236 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100237 dict_unref(dict);
238 return MAYBE;
239 }
240 item = dictitem_alloc(key);
241 clear_tv(tv);
242 if (unlikely(item == NULL))
243 {
244 dict_unref(dict);
245 return FAIL;
246 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100247 tv = STACK_TV_BOT(2 * (idx - count) + 1);
248 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100249 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100250 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100251 if (dict_add(dict, item) == FAIL)
252 {
253 // can this ever happen?
254 dict_unref(dict);
255 return FAIL;
256 }
257 }
258 }
259
260 if (count > 0)
261 ectx->ec_stack.ga_len -= 2 * count - 1;
262 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Christian Brabandt86eddce2024-03-26 18:42:52 +0100263 {
264 dict_unref(dict);
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100265 return FAIL;
Christian Brabandt86eddce2024-03-26 18:42:52 +0100266 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100267 else
268 ++ectx->ec_stack.ga_len;
269 tv = STACK_TV_BOT(-1);
270 tv->v_type = VAR_DICT;
271 tv->v_lock = 0;
272 tv->vval.v_dict = dict;
273 if (dict != NULL)
274 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200275 return OK;
276}
277
278/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200279 * If debug_tick changed check if "ufunc" has a breakpoint and update
280 * "uf_has_breakpoint".
281 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000282 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200283update_has_breakpoint(ufunc_T *ufunc)
284{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000285 if (ufunc->uf_debug_tick == debug_tick)
286 return;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200287
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000288 linenr_T breakpoint;
289
290 ufunc->uf_debug_tick = debug_tick;
291 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
292 ufunc->uf_has_breakpoint = breakpoint > 0;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200293}
294
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200295static garray_T dict_stack = GA_EMPTY;
296
297/*
298 * Put a value on the dict stack. This consumes "tv".
299 */
300 static int
301dict_stack_save(typval_T *tv)
302{
303 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000304 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200305 if (ga_grow(&dict_stack, 1) == FAIL)
306 return FAIL;
307 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
308 ++dict_stack.ga_len;
309 return OK;
310}
311
312/*
313 * Get the typval at top of the dict stack.
314 */
315 static typval_T *
316dict_stack_get_tv(void)
317{
318 if (dict_stack.ga_len == 0)
319 return NULL;
320 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
321}
322
323/*
324 * Get the dict at top of the dict stack.
325 */
326 static dict_T *
327dict_stack_get_dict(void)
328{
329 typval_T *tv;
330
331 if (dict_stack.ga_len == 0)
332 return NULL;
333 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
334 if (tv->v_type == VAR_DICT)
335 return tv->vval.v_dict;
336 return NULL;
337}
338
339/*
340 * Drop an item from the dict stack.
341 */
342 static void
343dict_stack_drop(void)
344{
345 if (dict_stack.ga_len == 0)
346 {
347 iemsg("Dict stack underflow");
348 return;
349 }
350 --dict_stack.ga_len;
351 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
352}
353
354/*
355 * Drop items from the dict stack until the length is equal to "len".
356 */
357 static void
358dict_stack_clear(int len)
359{
360 while (dict_stack.ga_len > len)
361 dict_stack_drop();
362}
363
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200364/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000365 * Get a pointer to useful "pt_outer" of "pt".
366 */
367 static outer_T *
368get_pt_outer(partial_T *pt)
369{
370 partial_T *ptref = pt->pt_outer_partial;
371
372 if (ptref == NULL)
373 return &pt->pt_outer;
374
375 // partial using partial (recursively)
376 while (ptref->pt_outer_partial != NULL)
377 ptref = ptref->pt_outer_partial;
378 return &ptref->pt_outer;
379}
380
381/*
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000382 * Check "argcount" arguments on the stack against what "ufunc" expects.
383 * "off" is the offset of arguments on the stack.
384 * Return OK or FAIL.
385 */
386 static int
387check_ufunc_arg_types(ufunc_T *ufunc, int argcount, int off, ectx_T *ectx)
388{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000389 if (ufunc->uf_arg_types == NULL && ufunc->uf_va_type == NULL)
390 return OK;
391
392 typval_T *argv = STACK_TV_BOT(0) - argcount - off;
393
394 // The function can change at runtime, check that the argument
395 // types are correct.
396 for (int i = 0; i < argcount; ++i)
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000397 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000398 type_T *type = NULL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000399
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000400 // assume a v:none argument, using the default value, is always OK
401 if (argv[i].v_type == VAR_SPECIAL
402 && argv[i].vval.v_number == VVAL_NONE)
403 continue;
Ernie Raelb077b582023-12-14 20:11:44 +0100404 // only pass values to user functions, never types
405 if (check_typval_is_value(&argv[i]) == FAIL)
406 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000407
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000408 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
409 type = ufunc->uf_arg_types[i];
410 else if (ufunc->uf_va_type != NULL)
411 type = ufunc->uf_va_type->tt_member;
412 if (type != NULL && check_typval_arg_type(type,
413 &argv[i], NULL, i + 1) == FAIL)
414 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000415 }
416 return OK;
417}
418
419/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100421 * This adds a stack frame and sets the instruction pointer to the start of the
422 * called function.
Bram Moolenaar5800c792022-09-22 17:34:01 +0100423 * If "pt_arg" is not NULL use "pt_arg->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100424 *
425 * Stack has:
426 * - current arguments (already there)
427 * - omitted optional argument (default values) added here
428 * - stack frame:
429 * - pointer to calling function
430 * - Index of next instruction in calling function
431 * - previous frame pointer
432 * - reserved space for local variables
433 */
434 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100435call_dfunc(
436 int cdf_idx,
Bram Moolenaar5800c792022-09-22 17:34:01 +0100437 partial_T *pt_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100438 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100439 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100440{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100441 int argcount = argcount_arg;
442 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
443 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200444 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100445 int arg_to_add;
446 int vararg_count = 0;
447 int varcount;
448 int idx;
449 estack_T *entry;
450 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200451 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000452 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453
454 if (dfunc->df_deleted)
455 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100456 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000457 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100458 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100459 return FAIL;
460 }
461
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100462#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100463 if (do_profiling == PROF_YES)
464 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200465 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100466 {
467 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
468 + profile_info_ga.ga_len;
469 ++profile_info_ga.ga_len;
470 CLEAR_POINTER(info);
471 profile_may_start_func(info, ufunc,
472 (((dfunc_T *)def_functions.ga_data)
473 + ectx->ec_dfunc_idx)->df_ufunc);
474 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100475 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100476#endif
477
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200478 // When debugging and using "cont" switches to the not-debugged
479 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000480 compile_type = get_compile_type(ufunc);
481 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200482 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000483 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200484
485 // compile_def_function() may cause def_functions.ga_data to change
486 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
487 }
488 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200489 {
490 if (did_emsg_cumul + did_emsg == did_emsg_before)
491 semsg(_(e_function_is_not_compiled_str),
492 printable_func_name(ufunc));
493 return FAIL;
494 }
495
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200496 if (ufunc->uf_va_name != NULL)
497 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200498 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200499 // Stack at time of call with 2 varargs:
500 // normal_arg
501 // optional_arg
502 // vararg_1
503 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200504 // After creating the list:
505 // normal_arg
506 // optional_arg
507 // vararg-list
508 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200509 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200510 // After creating the list
511 // normal_arg
512 // (space for optional_arg)
513 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200514 vararg_count = argcount - ufunc->uf_args.ga_len;
515 if (vararg_count < 0)
516 vararg_count = 0;
517 else
518 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200519 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200520 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200521
522 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200523 }
524
Bram Moolenaarfe270812020-04-11 22:31:27 +0200525 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200526 if (arg_to_add < 0)
527 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100528 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
529 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200530 return FAIL;
531 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000532 else if (arg_to_add > ufunc->uf_def_args.ga_len)
533 {
534 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
535
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100536 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
537 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000538 return FAIL;
539 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200540
Bram Moolenaar574950d2023-01-03 19:08:50 +0000541 // If this is an object method, the object is just before the arguments.
542 typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
543
Yegappan Lakshmananc1946262023-09-25 21:00:46 +0200544 if (IS_OBJECT_METHOD(ufunc) && !IS_CONSTRUCTOR_METHOD(ufunc)
545 && obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL)
546 {
547 // If this is not a constructor method, then a valid object is
548 // needed.
549 emsg(_(e_using_null_object));
550 return FAIL;
551 }
552
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000553 // Check the argument types.
554 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
555 return FAIL;
556
mityua5550692023-11-25 15:41:20 +0100557 // While check_ufunc_arg_types call, def function compilation process may
558 // run. If so many def functions are compiled, def_functions array may be
559 // reallocated and dfunc may no longer have valid pointer. Get the object
560 // pointer from def_functions again here.
561 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
562
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200563 // Reserve space for:
564 // - missing arguments
565 // - stack frame
566 // - local variables
567 // - if needed: a counter for number of closures created in
568 // ectx->ec_funcrefs.
569 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100570 if (GA_GROW_FAILS(&ectx->ec_stack,
571 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 return FAIL;
573
Yegappan Lakshmanan1087b8c2023-10-07 22:03:18 +0200574 // The object pointer is in the execution typval stack. The GA_GROW call
575 // above may have reallocated the execution typval stack. So the object
576 // pointer may not be valid anymore. Get the object pointer again from the
577 // execution stack.
578 obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
579
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100580 // If depth of calling is getting too high, don't execute the function.
581 if (funcdepth_increment() == FAIL)
582 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200583 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100584
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100585 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200586 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100587 {
588 floc = ALLOC_ONE(funclocal_T);
589 if (floc == NULL)
590 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200591 *floc = ectx->ec_funclocal;
592 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100593 }
594
Bram Moolenaarfe270812020-04-11 22:31:27 +0200595 // Move the vararg-list to below the missing optional arguments.
596 if (vararg_count > 0 && arg_to_add > 0)
597 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100598
599 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200600 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200601 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200602 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603
604 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100605 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
606 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200607 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200608 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
609 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100610 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100611 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200612 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613
Bram Moolenaar5800c792022-09-22 17:34:01 +0100614 // Initialize all local variables to number zero. Also initialize the
615 // variable that counts how many closures were created. This is used in
616 // handle_closure_in_use().
617 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
618 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000619 {
620 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
621
622 tv->v_type = VAR_NUMBER;
623 tv->vval.v_number = 0;
624 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200625 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626
Bram Moolenaar574950d2023-01-03 19:08:50 +0000627 // For an object method move the object from just before the arguments to
628 // the first local variable.
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +0200629 if (IS_OBJECT_METHOD(ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +0000630 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200631 if (obj->v_type != VAR_OBJECT)
632 {
633 semsg(_(e_internal_error_str), "type in stack is not an object");
634 return FAIL;
635 }
636
Bram Moolenaar574950d2023-01-03 19:08:50 +0000637 *STACK_TV_VAR(0) = *obj;
638 obj->v_type = VAR_UNKNOWN;
639 }
640
Bram Moolenaar5800c792022-09-22 17:34:01 +0100641 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
642 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100643 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200644 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100645
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200646 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100647 return FAIL;
648 if (pt != NULL)
649 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000650 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200651 ++pt->pt_refcount;
652 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100653 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100654 else
655 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200656 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200657 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200658 {
659 vim_free(ref);
660 return FAIL;
661 }
662 ref->or_outer_allocated = TRUE;
663 ref->or_outer->out_stack = &ectx->ec_stack;
664 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
665 if (ectx->ec_outer_ref != NULL)
666 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100667 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200668 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100669 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100670 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200671 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100672
Bram Moolenaarc970e422021-03-17 15:03:04 +0100673 ++ufunc->uf_calls;
674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675 // Set execution state to the start of the called function.
676 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100677 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100678 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200679 if (entry != NULL)
680 {
681 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200682 // Save the current context so it can be restored on return.
683 entry->es_save_sctx = current_sctx;
684 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200685 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100686
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200687 // Start execution at the first instruction.
688 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689
690 return OK;
691}
692
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000693// Double linked list of funcstack_T in use.
694static funcstack_T *first_funcstack = NULL;
695
696 static void
697add_funcstack_to_list(funcstack_T *funcstack)
698{
699 // Link in list of funcstacks.
700 if (first_funcstack != NULL)
701 first_funcstack->fs_prev = funcstack;
702 funcstack->fs_next = first_funcstack;
703 funcstack->fs_prev = NULL;
704 first_funcstack = funcstack;
705}
706
707 static void
708remove_funcstack_from_list(funcstack_T *funcstack)
709{
710 if (funcstack->fs_prev == NULL)
711 first_funcstack = funcstack->fs_next;
712 else
713 funcstack->fs_prev->fs_next = funcstack->fs_next;
714 if (funcstack->fs_next != NULL)
715 funcstack->fs_next->fs_prev = funcstack->fs_prev;
716}
717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200719 * Used when returning from a function: Check if any closure is still
720 * referenced. If so then move the arguments and variables to a separate piece
721 * of stack to be used when the closure is called.
722 * When "free_arguments" is TRUE the arguments are to be freed.
723 * Returns FAIL when out of memory.
724 */
725 static int
726handle_closure_in_use(ectx_T *ectx, int free_arguments)
727{
728 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
729 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200730 int argcount;
731 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200732 int idx;
733 typval_T *tv;
734 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200735 garray_T *gap = &ectx->ec_funcrefs;
736 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200737
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200738 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200739 return OK; // function was freed
740 if (dfunc->df_has_closure == 0)
741 return OK; // no closures
742 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
743 closure_count = tv->vval.v_number;
744 if (closure_count == 0)
745 return OK; // no funcrefs created
746
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100747 // Compute "top": the first entry in the stack used by the function.
748 // This is the first argument (after that comes the stack frame and then
749 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200750 argcount = ufunc_argcount(dfunc->df_ufunc);
751 top = ectx->ec_frame_idx - argcount;
752
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200753 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200754 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200755 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200756 partial_T *pt;
757 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200758
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200759 if (off < 0)
760 continue; // count is off or already done
761 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200762 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200763 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200764 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200765 int i;
766
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000767 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200768 // unreferenced on return.
769 for (i = 0; i < dfunc->df_varcount; ++i)
770 {
771 typval_T *stv = STACK_TV(ectx->ec_frame_idx
772 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200773 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200774 --refcount;
775 }
776 if (refcount > 1)
777 {
778 closure_in_use = TRUE;
779 break;
780 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200781 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200782 }
783
784 if (closure_in_use)
785 {
786 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
787 typval_T *stack;
788
789 // A closure is using the arguments and/or local variables.
790 // Move them to the called function.
791 if (funcstack == NULL)
792 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000793
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200794 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100795 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
796 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200797 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
798 funcstack->fs_ga.ga_data = stack;
799 if (stack == NULL)
800 {
801 vim_free(funcstack);
802 return FAIL;
803 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000804 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200805
806 // Move or copy the arguments.
807 for (idx = 0; idx < argcount; ++idx)
808 {
809 tv = STACK_TV(top + idx);
810 if (free_arguments)
811 {
812 *(stack + idx) = *tv;
813 tv->v_type = VAR_UNKNOWN;
814 }
815 else
816 copy_tv(tv, stack + idx);
817 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100818 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200819 // Move the local variables.
820 for (idx = 0; idx < dfunc->df_varcount; ++idx)
821 {
822 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200823
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200824 // A partial created for a local function, that is also used as a
825 // local variable, has a reference count for the variable, thus
826 // will never go down to zero. When all these refcounts are one
827 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000828 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200829 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
830 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200831 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200832
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200833 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200834 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
835 gap->ga_len - closure_count + i])
836 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200837 }
838
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200839 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200840 tv->v_type = VAR_UNKNOWN;
841 }
842
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200843 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200844 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200845 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
846 - closure_count + idx];
847 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200848 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200849 ++funcstack->fs_refcount;
850 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100851 pt->pt_outer.out_stack = &funcstack->fs_ga;
852 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200853 }
854 }
855 }
856
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200857 for (idx = 0; idx < closure_count; ++idx)
858 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
859 - closure_count + idx]);
860 gap->ga_len -= closure_count;
861 if (gap->ga_len == 0)
862 ga_clear(gap);
863
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200864 return OK;
865}
866
867/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200868 * Called when a partial is freed or its reference count goes down to one. The
869 * funcstack may be the only reference to the partials in the local variables.
870 * Go over all of them, the funcref and can be freed if all partials
871 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100872 * Returns TRUE if the funcstack is freed, the partial referencing it will then
873 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200874 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100875 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200876funcstack_check_refcount(funcstack_T *funcstack)
877{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100878 int i;
879 garray_T *gap = &funcstack->fs_ga;
880 int done = 0;
881 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200882
883 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100884 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200885 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
886 {
887 typval_T *tv = ((typval_T *)gap->ga_data) + i;
888
889 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
890 && tv->vval.v_partial->pt_funcstack == funcstack
891 && tv->vval.v_partial->pt_refcount == 1)
892 ++done;
893 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100894 if (done != funcstack->fs_min_refcount)
895 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200896
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100897 stack = gap->ga_data;
898
899 // All partials referencing the funcstack have a reference count of
900 // one, thus the funcstack is no longer of use.
901 for (i = 0; i < gap->ga_len; ++i)
902 clear_tv(stack + i);
903 vim_free(stack);
904 remove_funcstack_from_list(funcstack);
905 vim_free(funcstack);
906
907 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200908}
909
910/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000911 * For garbage collecting: set references in all variables referenced by
912 * all funcstacks.
913 */
914 int
915set_ref_in_funcstacks(int copyID)
916{
917 funcstack_T *funcstack;
918
919 for (funcstack = first_funcstack; funcstack != NULL;
920 funcstack = funcstack->fs_next)
921 {
922 typval_T *stack = funcstack->fs_ga.ga_data;
923 int i;
924
925 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
926 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
927 return TRUE; // abort
928 }
929 return FALSE;
930}
931
Bram Moolenaar806a2732022-09-04 15:40:36 +0100932// Ugly static to avoid passing the execution context around through many
933// layers.
934static ectx_T *current_ectx = NULL;
935
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000936/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100937 * Return TRUE if currently executing a :def function.
938 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100939 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100940 int
941in_def_function(void)
942{
943 return current_ectx != NULL;
944}
945
946/*
Ernie Rael4c8da022023-10-11 21:35:11 +0200947 * If executing a class/object method, then fill in the lval_T.
948 * Set lr_tv to the executing item, and lr_exec_class to the executing class;
949 * use free_tv and class_unref when finished with the lval_root.
950 * For use by builtin functions.
951 *
952 * Return FAIL and do nothing if not executing in a class; otherwise OK.
953 */
954 int
955fill_exec_lval_root(lval_root_T *root)
956{
957 ectx_T *ectx = current_ectx;
958 if (ectx != NULL)
959 {
960 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
961 + current_ectx->ec_dfunc_idx;
962 ufunc_T *ufunc = dfunc->df_ufunc;
963 if (ufunc->uf_class != NULL) // executing a method?
964 {
965 typval_T *tv = alloc_tv();
966 if (tv != NULL)
967 {
968 CLEAR_POINTER(root);
969 root->lr_tv = tv;
970 copy_tv(STACK_TV_VAR(0), root->lr_tv);
971 root->lr_cl_exec = ufunc->uf_class;
972 ++root->lr_cl_exec->class_refcount;
973 return OK;
974 }
975 }
976 }
977
978 return FAIL;
979}
980
981/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100982 * Clear "current_ectx" and return the previous value. To be used when calling
983 * a user function.
984 */
985 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000986clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100987{
988 ectx_T *r = current_ectx;
989
990 current_ectx = NULL;
991 return r;
992}
993
994 void
995restore_current_ectx(ectx_T *ectx)
996{
997 if (current_ectx != NULL)
998 iemsg("Restoring current_ectx while it is not NULL");
999 current_ectx = ectx;
1000}
1001
1002/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01001003 * Add an entry for a deferred function call to the currently executing
1004 * function.
1005 * Return the list or NULL when failed.
1006 */
1007 static list_T *
1008add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001009{
1010 typval_T *defer_tv = STACK_TV_VAR(var_idx);
1011 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001012 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001013 typval_T listval;
1014
1015 if (defer_tv->v_type != VAR_LIST)
1016 {
Bram Moolenaara5348f22022-09-04 11:42:22 +01001017 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001018 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001019 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001020 }
1021 defer_l = defer_tv->vval.v_list;
1022
1023 l = list_alloc_with_items(argcount + 1);
1024 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001025 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001026 listval.v_type = VAR_LIST;
1027 listval.vval.v_list = l;
1028 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +01001029 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001030 {
1031 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001032 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001033 }
1034
Bram Moolenaar806a2732022-09-04 15:40:36 +01001035 return l;
1036}
1037
1038/*
1039 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
1040 * The local variable that lists deferred functions is "var_idx".
1041 * Returns OK or FAIL.
1042 */
1043 static int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001044defer_command(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001045{
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001046 list_T *l = add_defer_item(var_idx, argcount, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001047 int i;
1048 typval_T *func_tv;
1049
1050 if (l == NULL)
1051 return FAIL;
1052
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001053 func_tv = STACK_TV_BOT(-argcount - 1);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001054 if (func_tv->v_type != VAR_PARTIAL && func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001055 {
1056 semsg(_(e_expected_str_but_got_str),
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001057 "function or partial",
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001058 vartype_name(func_tv->v_type));
1059 return FAIL;
1060 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001061 list_set_item(l, 0, func_tv);
1062
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001063 for (i = 0; i < argcount; ++i)
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001064 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
1065 ectx->ec_stack.ga_len -= argcount + 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001066 return OK;
1067}
1068
1069/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001070 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001071 * Consumes "name", also on failure.
1072 * Only to be called when in_def_function() returns TRUE.
1073 */
1074 int
1075add_defer_function(char_u *name, int argcount, typval_T *argvars)
1076{
1077 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1078 + current_ectx->ec_dfunc_idx;
1079 list_T *l;
1080 typval_T func_tv;
1081 int i;
1082
1083 if (dfunc->df_defer_var_idx == 0)
1084 {
1085 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001086 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001087 return FAIL;
1088 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001089
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001090 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001091 if (l == NULL)
1092 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001093 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001094 return FAIL;
1095 }
1096
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001097 func_tv.v_type = VAR_FUNC;
1098 func_tv.v_lock = 0;
1099 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001100 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001101
Bram Moolenaar806a2732022-09-04 15:40:36 +01001102 for (i = 0; i < argcount; ++i)
1103 list_set_item(l, i + 1, argvars + i);
1104 return OK;
1105}
1106
1107/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001108 * Invoked when returning from a function: Invoke any deferred calls.
1109 */
1110 static void
1111invoke_defer_funcs(ectx_T *ectx)
1112{
1113 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1114 + ectx->ec_dfunc_idx;
1115 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1116 listitem_T *li;
1117
1118 if (defer_tv->v_type != VAR_LIST)
1119 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001120 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001121 {
1122 list_T *l = li->li_tv.vval.v_list;
1123 typval_T rettv;
1124 typval_T argvars[MAX_FUNC_ARGS];
1125 int i;
1126 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001127 typval_T *functv = &l->lv_first->li_tv;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001128 int argcount = l->lv_len - 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001129
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001130 if (functv->vval.v_string == NULL)
1131 // already being called, can happen if function does ":qa"
1132 continue;
1133
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001134 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001135 {
1136 arg_li = arg_li->li_next;
1137 argvars[i] = arg_li->li_tv;
1138 }
1139
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001140 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001141 CLEAR_FIELD(funcexe);
1142 funcexe.fe_evaluate = TRUE;
1143 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001144 if (functv->v_type == VAR_PARTIAL)
1145 {
1146 funcexe.fe_partial = functv->vval.v_partial;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001147 funcexe.fe_object = functv->vval.v_partial->pt_obj;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001148 if (funcexe.fe_object != NULL)
1149 ++funcexe.fe_object->obj_refcount;
1150 }
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001151
1152 char_u *name = functv->vval.v_string;
1153 functv->vval.v_string = NULL;
1154
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001155 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001156 // first statement in the function will be executed (because of the
1157 // exception). So save and restore the try/catch/throw exception
1158 // state.
1159 exception_state_T estate;
1160 exception_state_save(&estate);
1161 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001162
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001163 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1164
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001165 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001166
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001167 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001168 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001169 }
1170}
1171
1172/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 * Return from the current function.
1174 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001175 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001176func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001179 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001180 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1181 + ectx->ec_dfunc_idx;
1182 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001183 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001184 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1185 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001186 funclocal_T *floc;
1187#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001188 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1189 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190
Bram Moolenaar12d26532021-02-19 19:13:21 +01001191 if (do_profiling == PROF_YES)
1192 {
1193 ufunc_T *caller = prev_dfunc->df_ufunc;
1194
1195 if (dfunc->df_ufunc->uf_profiling
1196 || (caller != NULL && caller->uf_profiling))
1197 {
1198 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1199 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1200 --profile_info_ga.ga_len;
1201 }
1202 }
1203#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001204
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001205 if (dfunc->df_defer_var_idx > 0)
1206 invoke_defer_funcs(ectx);
1207
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001208 // No check for uf_refcount being zero, cannot think of a way that would
1209 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001210 --dfunc->df_ufunc->uf_calls;
1211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001213 entry = estack_pop();
1214 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001215 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001217 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1218 return FAIL;
1219
Bram Moolenaar574950d2023-01-03 19:08:50 +00001220 // Clear the arguments. If this was an object method also clear the
1221 // object, it is just before the arguments.
1222 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001223 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001224 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001225 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1226 clear_tv(STACK_TV(idx));
1227
1228 // Clear local variables and temp values, but not the return value.
1229 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001230 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001232
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001233 // The return value should be on top of the stack. However, when aborting
1234 // it may not be there and ec_frame_idx is the top of the stack.
1235 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001236 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001237 ret_idx = 0;
1238
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001239 if (ectx->ec_outer_ref != NULL)
1240 {
1241 if (ectx->ec_outer_ref->or_outer_allocated)
1242 vim_free(ectx->ec_outer_ref->or_outer);
1243 partial_unref(ectx->ec_outer_ref->or_partial);
1244 vim_free(ectx->ec_outer_ref);
1245 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001246
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001247 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001248 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001249 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1250 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001251 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1252 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001253 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001254 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001255 floc = (void *)STACK_TV(ectx->ec_frame_idx
1256 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001257 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001258 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1259 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001260
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001261 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001262 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001263 else
1264 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001265 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001266 vim_free(floc);
1267 }
1268
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001269 if (ret_idx > 0)
1270 {
1271 // Reset the stack to the position before the call, with a spot for the
1272 // return value, moved there from above the frame.
1273 ectx->ec_stack.ga_len = top + 1;
1274 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1275 }
1276 else
1277 // Reset the stack to the position before the call.
1278 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001279
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001280 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001281 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001282 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283}
1284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285/*
1286 * Prepare arguments and rettv for calling a builtin or user function.
1287 */
1288 static int
1289call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1290{
1291 int idx;
1292 typval_T *tv;
1293
1294 // Move arguments from bottom of the stack to argvars[] and add terminator.
1295 for (idx = 0; idx < argcount; ++idx)
1296 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1297 argvars[argcount].v_type = VAR_UNKNOWN;
1298
1299 // Result replaces the arguments on the stack.
1300 if (argcount > 0)
1301 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001302 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 return FAIL;
1304 else
1305 ++ectx->ec_stack.ga_len;
1306
1307 // Default return value is zero.
1308 tv = STACK_TV_BOT(-1);
1309 tv->v_type = VAR_NUMBER;
1310 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001311 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312
1313 return OK;
1314}
1315
1316/*
1317 * Call a builtin function by index.
1318 */
1319 static int
1320call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1321{
1322 typval_T argvars[MAX_FUNC_ARGS];
1323 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001324 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001325 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001326 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
1328 if (call_prepare(argcount, argvars, ectx) == FAIL)
1329 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001330 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001332 // Call the builtin function. Set "current_ectx" so that when it
1333 // recursively invokes call_def_function() a closure context can be set.
1334 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001336 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001337 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338
1339 // Clear the arguments.
1340 for (idx = 0; idx < argcount; ++idx)
1341 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001342
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001343 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001344 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 return OK;
1346}
1347
1348/*
1349 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001350 * If the function is compiled this will add a stack frame and set the
1351 * instruction pointer at the start of the function.
1352 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001353 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001354 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 */
1356 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001357call_ufunc(
1358 ufunc_T *ufunc,
1359 partial_T *pt,
1360 int argcount,
1361 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001362 isn_T *iptr,
1363 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364{
1365 typval_T argvars[MAX_FUNC_ARGS];
1366 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001367 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001369 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001370 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371
Bram Moolenaare99d4222021-06-13 14:01:26 +02001372 if (func_needs_compiling(ufunc, compile_type)
1373 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1374 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001375 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001376 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001377 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001378 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001379 if (error != FCERR_UNKNOWN)
1380 {
1381 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001382 semsg(_(e_too_many_arguments_for_function_str),
1383 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001384 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001385 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001386 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001387 return FAIL;
1388 }
1389
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001390 // The function has been compiled, can call it quickly. For a function
1391 // that was defined later: we can call it directly next time.
1392 if (iptr != NULL)
1393 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001394 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001395 iptr->isn_type = ISN_DCALL;
1396 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1397 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1398 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001399 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401
1402 if (call_prepare(argcount, argvars, ectx) == FAIL)
1403 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001404 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001405 funcexe.fe_evaluate = TRUE;
1406 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407
1408 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001410 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411
1412 // Clear the arguments.
1413 for (idx = 0; idx < argcount; ++idx)
1414 clear_tv(&argvars[idx]);
1415
1416 if (error != FCERR_NONE)
1417 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001418 user_func_error(error, printable_func_name(ufunc),
1419 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 return FAIL;
1421 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001422 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001423 // Error other than from calling the function itself.
1424 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 return OK;
1426}
1427
1428/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001429 * If command modifiers were applied restore them.
1430 */
1431 static void
1432may_restore_cmdmod(funclocal_T *funclocal)
1433{
1434 if (funclocal->floc_restore_cmdmod)
1435 {
1436 cmdmod.cmod_filter_regmatch.regprog = NULL;
1437 undo_cmdmod(&cmdmod);
1438 cmdmod = funclocal->floc_save_cmdmod;
1439 funclocal->floc_restore_cmdmod = FALSE;
1440 }
1441}
1442
1443/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001444 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1445 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001446 */
1447 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001448vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001449{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001450 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001451}
1452
1453/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 * Execute a function by "name".
1455 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001456 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 * Returns FAIL if not found without an error message.
1458 */
1459 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001460call_by_name(
1461 char_u *name,
1462 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001463 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001464 isn_T *iptr,
1465 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466{
1467 ufunc_T *ufunc;
1468
1469 if (builtin_function(name, -1))
1470 {
1471 int func_idx = find_internal_func(name);
1472
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001473 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001475 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 return FAIL;
1477 return call_bfunc(func_idx, argcount, ectx);
1478 }
1479
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001480 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001481
1482 if (ufunc == NULL)
1483 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001484 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001485
1486 if (script_autoload(name, TRUE))
1487 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001488 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001489
1490 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001491 return FAIL; // bail out if loading the script caused an error
1492 }
1493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001495 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001496 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1497 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001498
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001499 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501
1502 return FAIL;
1503}
1504
1505 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001506call_partial(
1507 typval_T *tv,
1508 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001509 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001511 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001512 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001514 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001515 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516
1517 if (tv->v_type == VAR_PARTIAL)
1518 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001519 partial_T *pt = tv->vval.v_partial;
1520 int i;
1521
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001522 if (pt->pt_obj != NULL)
1523 {
1524 // partial with an object method. Push the object before the
1525 // function arguments.
1526 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1527 return FAIL;
1528 for (i = 1; i <= argcount; ++i)
1529 *STACK_TV_BOT(-i + 1) = *STACK_TV_BOT(-i);
1530
1531 typval_T *obj_tv = STACK_TV_BOT(-argcount);
1532 obj_tv->v_type = VAR_OBJECT;
1533 obj_tv->v_lock = 0;
1534 obj_tv->vval.v_object = pt->pt_obj;
1535 ++pt->pt_obj->obj_refcount;
1536 ++ectx->ec_stack.ga_len;
1537 }
1538
Bram Moolenaara90afb92020-07-15 22:38:56 +02001539 if (pt->pt_argc > 0)
1540 {
1541 // Make space for arguments from the partial, shift the "argcount"
1542 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001543 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001544 return FAIL;
1545 for (i = 1; i <= argcount; ++i)
1546 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1547 ectx->ec_stack.ga_len += pt->pt_argc;
1548 argcount += pt->pt_argc;
1549
1550 // copy the arguments from the partial onto the stack
1551 for (i = 0; i < pt->pt_argc; ++i)
1552 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1553 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001554 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555
1556 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001557 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 name = pt->pt_name;
1560 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001561 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001563 if (name != NULL)
1564 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001565 char_u fname_buf[FLEN_FIXED + 1];
1566 char_u *tofree = NULL;
1567 funcerror_T error = FCERR_NONE;
1568 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001569
1570 // May need to translate <SNR>123_ to K_SNR.
1571 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1572 if (error != FCERR_NONE)
1573 res = FAIL;
1574 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001575 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001576 vim_free(tofree);
1577 }
1578
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001579 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 {
1581 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001582 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001583 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 return FAIL;
1585 }
1586 return OK;
1587}
1588
1589/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001590 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1591 * TRUE.
1592 */
1593 static int
1594error_if_locked(int lock, char *error)
1595{
1596 if (lock & (VAR_LOCKED | VAR_FIXED))
1597 {
1598 emsg(_(error));
1599 return TRUE;
1600 }
1601 return FALSE;
1602}
1603
1604/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001605 * Give an error if "tv" is not a number and return FAIL.
1606 */
1607 static int
1608check_for_number(typval_T *tv)
1609{
1610 if (tv->v_type != VAR_NUMBER)
1611 {
1612 semsg(_(e_expected_str_but_got_str),
1613 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1614 return FAIL;
1615 }
1616 return OK;
1617}
1618
1619/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001620 * Store "tv" in variable "name".
1621 * This is for s: and g: variables.
1622 */
1623 static void
1624store_var(char_u *name, typval_T *tv)
1625{
1626 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001627 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001628
Bram Moolenaard877a572021-04-01 19:42:48 +02001629 if (tv->v_lock)
1630 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001631 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001632 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001633 restore_funccal();
1634}
1635
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001636/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001637 * Convert "tv" to a string.
1638 * Return FAIL if not allowed.
1639 */
1640 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001641do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001642{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001643 if (tv->v_type == VAR_STRING)
1644 return OK;
1645
1646 char_u *str;
1647
1648 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001649 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001650 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001651 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001652 case VAR_SPECIAL:
1653 case VAR_BOOL:
1654 case VAR_NUMBER:
1655 case VAR_FLOAT:
1656 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001657
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001658 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001659 if (tolerant)
1660 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001661 char_u *s, *e, *p;
1662 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001663
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001664 ga_init2(&ga, sizeof(char_u *), 1);
1665
1666 // Convert to NL separated items, then
1667 // escape the items and replace the NL with
1668 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001669 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001670 if (str == NULL)
1671 return FAIL;
1672 s = str;
1673 while ((e = vim_strchr(s, '\n')) != NULL)
1674 {
1675 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001676 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001677 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001678 if (p != NULL)
1679 {
1680 ga_concat(&ga, p);
1681 ga_concat(&ga, (char_u *)" ");
1682 vim_free(p);
1683 }
1684 s = e + 1;
1685 }
1686 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001687 clear_tv(tv);
1688 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001689 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001690 return OK;
1691 }
1692 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001693 default: to_string_error(tv->v_type);
1694 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001695 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001696 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001697 str = typval_tostring(tv, TRUE);
1698 clear_tv(tv);
1699 tv->v_type = VAR_STRING;
1700 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001701 return OK;
1702}
1703
1704/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001705 * When the value of "sv" is a null list of dict, allocate it.
1706 */
1707 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001708allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001709{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001710 typval_T *tv = sv->sv_tv;
1711
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001712 switch (tv->v_type)
1713 {
1714 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001715 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001716 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001717 break;
1718 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001719 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001720 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001721 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001722 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001723 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001724 (void)rettv_blob_alloc(tv);
1725 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001726 default:
1727 break;
1728 }
1729}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001730
Bram Moolenaare7525c52021-01-09 13:20:37 +01001731/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001732 * Return the character "str[index]" where "index" is the character index,
1733 * including composing characters.
1734 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001735 */
1736 char_u *
1737char_from_string(char_u *str, varnumber_T index)
1738{
1739 size_t nbyte = 0;
1740 varnumber_T nchar = index;
1741 size_t slen;
1742
1743 if (str == NULL)
1744 return NULL;
1745 slen = STRLEN(str);
1746
Bram Moolenaarff871402021-03-26 13:34:05 +01001747 // Do the same as for a list: a negative index counts from the end.
1748 // Optimization to check the first byte to be below 0x80 (and no composing
1749 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001750 if (index < 0)
1751 {
1752 int clen = 0;
1753
1754 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001755 {
1756 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1757 ++nbyte;
1758 else if (enc_utf8)
1759 nbyte += utfc_ptr2len(str + nbyte);
1760 else
1761 nbyte += mb_ptr2len(str + nbyte);
1762 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001763 nchar = clen + index;
1764 if (nchar < 0)
1765 // unlike list: index out of range results in empty string
1766 return NULL;
1767 }
1768
1769 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001770 {
1771 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1772 ++nbyte;
1773 else if (enc_utf8)
1774 nbyte += utfc_ptr2len(str + nbyte);
1775 else
1776 nbyte += mb_ptr2len(str + nbyte);
1777 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001778 if (nbyte >= slen)
1779 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001780 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001781}
1782
1783/*
1784 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001785 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001786 * If going over the end return "str_len".
1787 * If "idx" is negative count from the end, -1 is the last character.
1788 * When going over the start return -1.
1789 */
1790 static long
1791char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1792{
1793 varnumber_T nchar = idx;
1794 size_t nbyte = 0;
1795
1796 if (nchar >= 0)
1797 {
1798 while (nchar > 0 && nbyte < str_len)
1799 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001800 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001801 --nchar;
1802 }
1803 }
1804 else
1805 {
1806 nbyte = str_len;
1807 while (nchar < 0 && nbyte > 0)
1808 {
1809 --nbyte;
1810 nbyte -= mb_head_off(str, str + nbyte);
1811 ++nchar;
1812 }
1813 if (nchar < 0)
1814 return -1;
1815 }
1816 return (long)nbyte;
1817}
1818
1819/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001820 * Return the slice "str[first : last]" using character indexes. Composing
1821 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001822 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001823 * Return NULL when the result is empty.
1824 */
1825 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001826string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001827{
1828 long start_byte, end_byte;
1829 size_t slen;
1830
1831 if (str == NULL)
1832 return NULL;
1833 slen = STRLEN(str);
1834 start_byte = char_idx2byte(str, slen, first);
1835 if (start_byte < 0)
1836 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001837 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001838 end_byte = (long)slen;
1839 else
1840 {
1841 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001842 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001843 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001844 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001845 }
1846
1847 if (start_byte >= (long)slen || end_byte <= start_byte)
1848 return NULL;
1849 return vim_strnsave(str + start_byte, end_byte - start_byte);
1850}
1851
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001852/*
1853 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1854 * When "dfunc_idx" is negative don't give an error.
1855 * Returns NULL for an error.
1856 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001857 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001858get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001859{
1860 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001861 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1862 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001863 svar_T *sv;
1864
1865 if (sref->sref_seq != si->sn_script_seq)
1866 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001867 // The script was reloaded after the function was compiled, the
1868 // script_idx may not be valid.
1869 if (dfunc != NULL)
1870 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1871 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001872 return NULL;
1873 }
1874 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001875 if (sv->sv_name == NULL)
1876 {
1877 if (dfunc != NULL)
1878 emsg(_(e_script_variable_was_deleted));
1879 return NULL;
1880 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001881 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001882 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001883 if (dfunc != NULL)
1884 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001885 return NULL;
1886 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001887
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001888 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1889 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001890 {
1891 if (dfunc != NULL)
1892 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1893 return NULL;
1894 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001895 return sv;
1896}
1897
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001898/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001899 * Function passed to do_cmdline() for splitting a script joined by NL
1900 * characters.
1901 */
1902 static char_u *
1903get_split_sourceline(
1904 int c UNUSED,
1905 void *cookie,
1906 int indent UNUSED,
1907 getline_opt_T options UNUSED)
1908{
1909 source_cookie_T *sp = (source_cookie_T *)cookie;
1910 char_u *p;
1911 char_u *line;
1912
Bram Moolenaar20677332021-06-06 17:02:53 +02001913 p = vim_strchr(sp->nextline, '\n');
1914 if (p == NULL)
1915 {
1916 line = vim_strsave(sp->nextline);
1917 sp->nextline += STRLEN(sp->nextline);
1918 }
1919 else
1920 {
1921 line = vim_strnsave(sp->nextline, p - sp->nextline);
1922 sp->nextline = p + 1;
1923 }
1924 return line;
1925}
1926
1927/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 * Execute a function by "name".
1929 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001930 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 */
1932 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001933call_eval_func(
1934 char_u *name,
1935 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001936 ectx_T *ectx,
1937 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938{
Bram Moolenaared677f52020-08-12 16:38:10 +02001939 int called_emsg_before = called_emsg;
1940 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001942 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001943 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001945 dictitem_T *v;
1946
1947 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001948 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1949 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001950 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001951 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001952 return FAIL;
1953 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001954 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001956 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957}
1958
1959/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001960 * When a function reference is used, fill a partial with the information
1961 * needed, especially when it is used as a closure.
1962 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001963 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001964fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001965 partial_T *pt,
1966 ufunc_T *ufunc,
1967 loopvarinfo_T *lvi,
1968 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001969{
1970 pt->pt_func = ufunc;
1971 pt->pt_refcount = 1;
1972
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001973 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001974 {
1975 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1976 + ectx->ec_dfunc_idx;
1977
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001978 // The closure may need to find arguments and local variables of the
1979 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001980 pt->pt_outer.out_stack = &ectx->ec_stack;
1981 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001982 if (ectx->ec_outer_ref != NULL)
1983 {
1984 // The current context already has a context, link to that one.
1985 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1986 if (ectx->ec_outer_ref->or_partial != NULL)
1987 {
1988 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1989 ++pt->pt_outer.out_up_partial->pt_refcount;
1990 }
1991 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001992
Bram Moolenaarcc341812022-09-19 15:54:34 +01001993 if (lvi != NULL)
1994 {
1995 int depth;
1996
1997 // The closure may need to find variables defined inside a loop,
1998 // for every nested loop. A new reference is made every time,
1999 // ISN_ENDLOOP will check if they are actually used.
2000 for (depth = 0; depth < lvi->lvi_depth; ++depth)
2001 {
2002 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
2003 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
2004 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
2005 pt->pt_outer.out_loop[depth].var_count =
2006 lvi->lvi_loop[depth].var_count;
2007 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002008 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002009 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002010 else
2011 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002012
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002013 // If the function currently executing returns and the closure is still
2014 // being referenced, we need to make a copy of the context (arguments
2015 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002016 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02002017 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01002018 {
2019 vim_free(pt);
2020 return FAIL;
2021 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002022 // Extra variable keeps the count of closures created in the current
2023 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01002024 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
2025 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
2026
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002027 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
2028 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002029 ++pt->pt_refcount;
2030 ++ectx->ec_funcrefs.ga_len;
2031 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002032 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002033 return OK;
2034}
2035
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002036/*
2037 * Execute iptr->isn_arg.string as an Ex command.
2038 */
2039 static int
Ernie Raelee865f32023-09-29 19:53:55 +02002040exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002041{
2042 source_cookie_T cookie;
2043
2044 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002045 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002046 CLEAR_FIELD(cookie);
2047 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02002048 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002049 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
2050 || did_emsg)
2051 return FAIL;
2052 return OK;
2053}
2054
Bram Moolenaar89445512022-04-14 12:58:23 +01002055/*
2056 * If script "sid" is not loaded yet then load it now.
2057 * Caller must make sure "sid" is a valid script ID.
2058 * "loaded" is set to TRUE if the script had to be loaded.
2059 * Returns FAIL if loading fails, OK if already loaded or loaded now.
2060 */
2061 int
2062may_load_script(int sid, int *loaded)
2063{
2064 scriptitem_T *si = SCRIPT_ITEM(sid);
2065
2066 if (si->sn_state == SN_STATE_NOT_LOADED)
2067 {
2068 *loaded = TRUE;
2069 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
2070 {
2071 semsg(_(e_cant_open_file_str), si->sn_name);
2072 return FAIL;
2073 }
2074 }
2075 return OK;
2076}
2077
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002078// used for v_instr of typval of VAR_INSTR
2079struct instr_S {
2080 ectx_T *instr_ectx;
2081 isn_T *instr_instr;
2082};
2083
Bram Moolenaar4c137212021-04-19 16:48:48 +02002084// used for substitute_instr
2085typedef struct subs_expr_S {
2086 ectx_T *subs_ectx;
2087 isn_T *subs_instr;
2088 int subs_status;
2089} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002091// Set when calling do_debug().
2092static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002093static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002094
2095/*
2096 * When debugging lookup "name" and return the typeval.
2097 * When not found return NULL.
2098 */
2099 typval_T *
2100lookup_debug_var(char_u *name)
2101{
2102 int idx;
2103 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002104 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002105 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002106 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002107
2108 if (ectx == NULL)
2109 return NULL;
2110 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2111
2112 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002113 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002114 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002115 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2116
2117 // the variable name may be NULL when not available in this block
2118 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002119 return STACK_TV_VAR(idx);
2120 }
2121
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002122 // Go through argument names.
2123 ufunc = dfunc->df_ufunc;
2124 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2125 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2126 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2127 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2128 - varargs_off + idx);
2129 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2130 return STACK_TV(ectx->ec_frame_idx - 1);
2131
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002132 return NULL;
2133}
2134
Bram Moolenaar26a44842021-09-02 18:49:06 +02002135/*
2136 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2137 * breakpoint was set in that function or when there is any expression.
2138 */
2139 int
2140may_break_in_function(ufunc_T *ufunc)
2141{
2142 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2143}
2144
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002145 static void
2146handle_debug(isn_T *iptr, ectx_T *ectx)
2147{
2148 char_u *line;
2149 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2150 + ectx->ec_dfunc_idx)->df_ufunc;
2151 isn_T *ni;
2152 int end_lnum = iptr->isn_lnum;
2153 garray_T ga;
2154 int lnum;
2155
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002156 if (ex_nesting_level > debug_break_level)
2157 {
2158 linenr_T breakpoint;
2159
Bram Moolenaar26a44842021-09-02 18:49:06 +02002160 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002161 return;
2162
2163 // check for the next breakpoint if needed
2164 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002165 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002166 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2167 return;
2168 }
2169
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002170 SOURCING_LNUM = iptr->isn_lnum;
2171 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002172 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002173
2174 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2175 if (ni->isn_type == ISN_DEBUG
2176 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002177 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002178 || ni->isn_type == ISN_RETURN_VOID)
2179 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002180 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002181 break;
2182 }
2183
2184 if (end_lnum > iptr->isn_lnum)
2185 {
2186 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002187 for (lnum = iptr->isn_lnum; lnum < end_lnum
2188 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002189 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002190 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002191
Bram Moolenaar303215d2021-07-07 20:10:43 +02002192 if (p == NULL)
2193 continue; // left over from continuation line
2194 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002195 if (*p == '#')
2196 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002197 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002198 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2199 if (STRNCMP(p, "def ", 4) == 0)
2200 break;
2201 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002202 line = ga_concat_strings(&ga, " ");
2203 vim_free(ga.ga_data);
2204 }
2205 else
2206 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002207
Dominique Pelle6e969552021-06-17 13:53:41 +02002208 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002209 debug_context = NULL;
2210
2211 if (end_lnum > iptr->isn_lnum)
2212 vim_free(line);
2213}
2214
Bram Moolenaar4c137212021-04-19 16:48:48 +02002215/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002216 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002217 * Returns OK, FAIL or NOTDONE (uncatchable error).
2218 */
2219 static int
2220execute_storeindex(isn_T *iptr, ectx_T *ectx)
2221{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002222 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002223 typval_T *tv;
2224 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002225 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002226 typval_T *tv_dest = STACK_TV_BOT(-1);
2227 int status = OK;
2228
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002229 if (tv_idx->v_type == VAR_NUMBER)
2230 lidx = (long)tv_idx->vval.v_number;
2231
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002232 // Stack contains:
2233 // -3 value to be stored
2234 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002235 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002236 tv = STACK_TV_BOT(-3);
2237 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002238
2239 // Make sure an object has been initialized
2240 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2241 {
2242 emsg(_(e_using_null_object));
2243 status = FAIL;
2244 }
2245 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002246 {
2247 dest_type = tv_dest->v_type;
2248 if (dest_type == VAR_DICT)
2249 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002250 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2251 {
2252 // Need to get the member index now that the class is known.
2253 object_T *obj = tv_dest->vval.v_object;
2254 class_T *cl = obj->obj_class;
2255 char_u *member = tv_idx->vval.v_string;
2256
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002257 int m_idx;
2258 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2259 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002260 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002261 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002262 {
Ernie Rael03042a22023-11-11 08:53:32 +01002263 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +02002264 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002265 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002266 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002267
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002268 lidx = m_idx;
2269 }
2270 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002271 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002272 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002273 status = FAIL;
2274 }
2275 }
2276 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2277 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002278 {
2279 emsg(_(e_number_expected));
2280 status = FAIL;
2281 }
2282 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002283
2284 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002285 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002286 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002287 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002288 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002289
Bram Moolenaare08be092022-02-17 13:08:26 +00002290 if (list == NULL)
2291 {
2292 emsg(_(e_list_not_set));
2293 return FAIL;
2294 }
2295 if (lidx < 0 && list->lv_len + lidx >= 0)
2296 // negative index is relative to the end
2297 lidx = list->lv_len + lidx;
2298 if (lidx < 0 || lidx > list->lv_len)
2299 {
2300 semsg(_(e_list_index_out_of_range_nr), lidx);
2301 return FAIL;
2302 }
2303 if (lidx < list->lv_len)
2304 {
2305 listitem_T *li = list_find(list, lidx);
2306
2307 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002308 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002309 return FAIL;
2310 // overwrite existing list item
2311 clear_tv(&li->li_tv);
2312 li->li_tv = *tv;
2313 }
2314 else
2315 {
2316 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2317 return FAIL;
2318 // append to list, only fails when out of memory
2319 if (list_append_tv(list, tv) == FAIL)
2320 return NOTDONE;
2321 clear_tv(tv);
2322 }
2323 }
2324 else if (dest_type == VAR_DICT)
2325 {
2326 char_u *key = tv_idx->vval.v_string;
2327 dict_T *dict = tv_dest->vval.v_dict;
2328 dictitem_T *di;
2329
2330 SOURCING_LNUM = iptr->isn_lnum;
2331 if (dict == NULL)
2332 {
2333 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002334 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002335 }
2336 if (key == NULL)
2337 key = (char_u *)"";
2338 di = dict_find(dict, key, -1);
2339 if (di != NULL)
2340 {
2341 if (error_if_locked(di->di_tv.v_lock,
2342 e_cannot_change_dict_item))
2343 return FAIL;
2344 // overwrite existing value
2345 clear_tv(&di->di_tv);
2346 di->di_tv = *tv;
2347 }
2348 else
2349 {
2350 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2351 return FAIL;
2352 // add to dict, only fails when out of memory
2353 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2354 return NOTDONE;
2355 clear_tv(tv);
2356 }
2357 }
2358 else if (dest_type == VAR_BLOB)
2359 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002360 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002361 varnumber_T nr;
2362 int error = FALSE;
2363 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002364
2365 if (blob == NULL)
2366 {
2367 emsg(_(e_blob_not_set));
2368 return FAIL;
2369 }
2370 len = blob_len(blob);
2371 if (lidx < 0 && len + lidx >= 0)
2372 // negative index is relative to the end
2373 lidx = len + lidx;
2374
2375 // Can add one byte at the end.
2376 if (lidx < 0 || lidx > len)
2377 {
2378 semsg(_(e_blob_index_out_of_range_nr), lidx);
2379 return FAIL;
2380 }
2381 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2382 return FAIL;
2383 nr = tv_get_number_chk(tv, &error);
2384 if (error)
2385 return FAIL;
2386 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002387 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002388 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2389 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002390 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002391
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002392 if (dest_type == VAR_OBJECT)
2393 {
2394 object_T *obj = tv_dest->vval.v_object;
2395
2396 otv = (typval_T *)(obj + 1);
2397 class_T *itf = iptr->isn_arg.storeindex.si_class;
2398 if (itf != NULL)
2399 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002400 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002401 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002402 }
2403 else
2404 {
2405 // VAR_CLASS
2406 class_T *class = tv_dest->vval.v_class;
2407 otv = class->class_members_tv;
2408 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002409
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002410 clear_tv(&otv[lidx]);
2411 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002412 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002413 else
2414 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002415 status = FAIL;
2416 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002417 }
2418 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002419
2420 clear_tv(tv_idx);
2421 clear_tv(tv_dest);
2422 ectx->ec_stack.ga_len -= 3;
2423 if (status == FAIL)
2424 {
2425 clear_tv(tv);
2426 return FAIL;
2427 }
2428 return OK;
2429}
2430
2431/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002432 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002433 */
2434 static int
2435execute_storerange(isn_T *iptr, ectx_T *ectx)
2436{
2437 typval_T *tv;
2438 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2439 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2440 typval_T *tv_dest = STACK_TV_BOT(-1);
2441 int status = OK;
2442
2443 // Stack contains:
2444 // -4 value to be stored
2445 // -3 first index or "none"
2446 // -2 second index or "none"
2447 // -1 destination list or blob
2448 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002449 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002450 if (tv_dest->v_type == VAR_LIST)
2451 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002452 long n1;
2453 long n2;
2454 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002455
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002456 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2457 if (tv_idx2->v_type == VAR_SPECIAL
2458 && tv_idx2->vval.v_number == VVAL_NONE)
2459 n2 = list_len(tv_dest->vval.v_list) - 1;
2460 else
2461 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2462
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002463 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002464 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002465 status = FAIL;
2466 else
2467 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002468 status = check_range_index_two(tv_dest->vval.v_list,
2469 &n1, li1, &n2, FALSE);
2470 if (status != FAIL)
2471 status = list_assign_range(
2472 tv_dest->vval.v_list,
2473 tv->vval.v_list,
2474 n1,
2475 n2,
2476 tv_idx2->v_type == VAR_SPECIAL,
2477 (char_u *)"=",
2478 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002479 }
2480 }
2481 else if (tv_dest->v_type == VAR_BLOB)
2482 {
2483 varnumber_T n1;
2484 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002485 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002486
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002487 n1 = tv_get_number_chk(tv_idx1, NULL);
2488 if (tv_idx2->v_type == VAR_SPECIAL
2489 && tv_idx2->vval.v_number == VVAL_NONE)
2490 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2491 else
2492 n2 = tv_get_number_chk(tv_idx2, NULL);
2493 bloblen = blob_len(tv_dest->vval.v_blob);
2494
2495 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2496 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002497 status = FAIL;
2498 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002499 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002500 }
2501 else
2502 {
2503 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002504 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002505 }
2506
2507 clear_tv(tv_idx1);
2508 clear_tv(tv_idx2);
2509 clear_tv(tv_dest);
2510 ectx->ec_stack.ga_len -= 4;
2511 clear_tv(tv);
2512
2513 return status;
2514}
2515
2516/*
2517 * Unlet item in list or dict variable.
2518 */
2519 static int
2520execute_unletindex(isn_T *iptr, ectx_T *ectx)
2521{
2522 typval_T *tv_idx = STACK_TV_BOT(-2);
2523 typval_T *tv_dest = STACK_TV_BOT(-1);
2524 int status = OK;
2525
2526 // Stack contains:
2527 // -2 index
2528 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002529 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002530 if (tv_dest->v_type == VAR_DICT)
2531 {
2532 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002533 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002534 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002535 semsg(_(e_expected_str_but_got_str),
2536 vartype_name(VAR_STRING),
2537 vartype_name(tv_idx->v_type));
2538 status = FAIL;
2539 }
2540 else
2541 {
2542 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002543 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002544 dictitem_T *di = NULL;
2545
2546 if (d != NULL && value_check_lock(
2547 d->dv_lock, NULL, FALSE))
2548 status = FAIL;
2549 else
2550 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002551 if (tv_idx->v_type == VAR_STRING)
2552 {
2553 key = tv_idx->vval.v_string;
2554 if (key == NULL)
2555 key = (char_u *)"";
2556 }
2557 else
2558 {
2559 key = tv_get_string(tv_idx);
2560 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002561 if (d != NULL)
2562 di = dict_find(d, key, (int)STRLEN(key));
2563 if (di == NULL)
2564 {
2565 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002566 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002567 status = FAIL;
2568 }
2569 else if (var_check_fixed(di->di_flags,
2570 NULL, FALSE)
2571 || var_check_ro(di->di_flags,
2572 NULL, FALSE))
2573 status = FAIL;
2574 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002575 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002576 }
2577 }
2578 }
2579 else if (tv_dest->v_type == VAR_LIST)
2580 {
2581 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002582 if (check_for_number(tv_idx) == FAIL)
2583 {
2584 status = FAIL;
2585 }
2586 else
2587 {
2588 list_T *l = tv_dest->vval.v_list;
2589 long n = (long)tv_idx->vval.v_number;
2590
2591 if (l != NULL && value_check_lock(
2592 l->lv_lock, NULL, FALSE))
2593 status = FAIL;
2594 else
2595 {
2596 listitem_T *li = list_find(l, n);
2597
2598 if (li == NULL)
2599 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002600 semsg(_(e_list_index_out_of_range_nr), n);
2601 status = FAIL;
2602 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002603 else
2604 listitem_remove(l, li);
2605 }
2606 }
2607 }
2608 else
2609 {
2610 status = FAIL;
2611 semsg(_(e_cannot_index_str),
2612 vartype_name(tv_dest->v_type));
2613 }
2614
2615 clear_tv(tv_idx);
2616 clear_tv(tv_dest);
2617 ectx->ec_stack.ga_len -= 2;
2618
2619 return status;
2620}
2621
2622/*
2623 * Unlet a range of items in a list variable.
2624 */
2625 static int
2626execute_unletrange(isn_T *iptr, ectx_T *ectx)
2627{
2628 // Stack contains:
2629 // -3 index1
2630 // -2 index2
2631 // -1 dict or list
2632 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2633 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2634 typval_T *tv_dest = STACK_TV_BOT(-1);
2635 int status = OK;
2636
2637 if (tv_dest->v_type == VAR_LIST)
2638 {
2639 // indexes must be a number
2640 SOURCING_LNUM = iptr->isn_lnum;
2641 if (check_for_number(tv_idx1) == FAIL
2642 || (tv_idx2->v_type != VAR_SPECIAL
2643 && check_for_number(tv_idx2) == FAIL))
2644 {
2645 status = FAIL;
2646 }
2647 else
2648 {
2649 list_T *l = tv_dest->vval.v_list;
2650 long n1 = (long)tv_idx1->vval.v_number;
2651 long n2 = tv_idx2->v_type == VAR_SPECIAL
2652 ? 0 : (long)tv_idx2->vval.v_number;
2653 listitem_T *li;
2654
2655 li = list_find_index(l, &n1);
2656 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002657 {
2658 semsg(_(e_list_index_out_of_range_nr),
2659 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002660 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002661 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002662 else
2663 {
2664 if (n1 < 0)
2665 n1 = list_idx_of_item(l, li);
2666 if (n2 < 0)
2667 {
2668 listitem_T *li2 = list_find(l, n2);
2669
2670 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002671 {
2672 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002673 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002674 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002675 else
2676 n2 = list_idx_of_item(l, li2);
2677 }
2678 if (status != FAIL
2679 && tv_idx2->v_type != VAR_SPECIAL
2680 && n2 < n1)
2681 {
2682 semsg(_(e_list_index_out_of_range_nr), n2);
2683 status = FAIL;
2684 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002685 if (status != FAIL)
2686 list_unlet_range(l, li, n1,
2687 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002688 }
2689 }
2690 }
2691 else
2692 {
2693 status = FAIL;
2694 SOURCING_LNUM = iptr->isn_lnum;
2695 semsg(_(e_cannot_index_str),
2696 vartype_name(tv_dest->v_type));
2697 }
2698
2699 clear_tv(tv_idx1);
2700 clear_tv(tv_idx2);
2701 clear_tv(tv_dest);
2702 ectx->ec_stack.ga_len -= 3;
2703
2704 return status;
2705}
2706
2707/*
2708 * Top of a for loop.
2709 */
2710 static int
2711execute_for(isn_T *iptr, ectx_T *ectx)
2712{
2713 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002714 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002715 typval_T *ltv = STACK_TV_BOT(-1);
2716 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002717 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002718
2719 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2720 return FAIL;
2721 if (ltv->v_type == VAR_LIST)
2722 {
2723 list_T *list = ltv->vval.v_list;
2724
2725 // push the next item from the list
2726 ++idxtv->vval.v_number;
2727 if (list == NULL
2728 || idxtv->vval.v_number >= list->lv_len)
2729 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002730 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002731 }
2732 else if (list->lv_first == &range_list_item)
2733 {
2734 // non-materialized range() list
2735 tv = STACK_TV_BOT(0);
2736 tv->v_type = VAR_NUMBER;
2737 tv->v_lock = 0;
2738 tv->vval.v_number = list_find_nr(
2739 list, idxtv->vval.v_number, NULL);
2740 ++ectx->ec_stack.ga_len;
2741 }
2742 else
2743 {
2744 listitem_T *li = list_find(list,
2745 idxtv->vval.v_number);
2746
2747 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2748 ++ectx->ec_stack.ga_len;
2749 }
2750 }
2751 else if (ltv->v_type == VAR_STRING)
2752 {
2753 char_u *str = ltv->vval.v_string;
2754
2755 // The index is for the last byte of the previous
2756 // character.
2757 ++idxtv->vval.v_number;
2758 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2759 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002760 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002761 }
2762 else
2763 {
2764 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2765
2766 // Push the next character from the string.
2767 tv = STACK_TV_BOT(0);
2768 tv->v_type = VAR_STRING;
2769 tv->vval.v_string = vim_strnsave(
2770 str + idxtv->vval.v_number, clen);
2771 ++ectx->ec_stack.ga_len;
2772 idxtv->vval.v_number += clen - 1;
2773 }
2774 }
2775 else if (ltv->v_type == VAR_BLOB)
2776 {
2777 blob_T *blob = ltv->vval.v_blob;
2778
2779 // When we get here the first time make a copy of the
2780 // blob, so that the iteration still works when it is
2781 // changed.
2782 if (idxtv->vval.v_number == -1 && blob != NULL)
2783 {
2784 blob_copy(blob, ltv);
2785 blob_unref(blob);
2786 blob = ltv->vval.v_blob;
2787 }
2788
2789 // The index is for the previous byte.
2790 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002791 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002792 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002793 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002794 }
2795 else
2796 {
2797 // Push the next byte from the blob.
2798 tv = STACK_TV_BOT(0);
2799 tv->v_type = VAR_NUMBER;
2800 tv->vval.v_number = blob_get(blob,
2801 idxtv->vval.v_number);
2802 ++ectx->ec_stack.ga_len;
2803 }
2804 }
2805 else
2806 {
2807 semsg(_(e_for_loop_on_str_not_supported),
2808 vartype_name(ltv->v_type));
2809 return FAIL;
2810 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002811
2812 if (jump)
2813 {
2814 // past the end of the list/string/blob, jump to "endfor"
2815 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2816 may_restore_cmdmod(&ectx->ec_funclocal);
2817 }
2818 else
2819 {
2820 // Store the current number of funcrefs, this may be used in
2821 // ISN_LOOPEND. The variable index is always one more than the loop
2822 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002823 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002824 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2825 }
2826
2827 return OK;
2828}
2829
2830/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002831 * Code for handling variables declared inside a loop and used in a closure.
2832 * This is very similar to what is done with funcstack_T. The difference is
2833 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2834 * scope of the block inside a loop and each loop may have its own.
2835 */
2836
2837// Double linked list of loopvars_T in use.
2838static loopvars_T *first_loopvars = NULL;
2839
2840 static void
2841add_loopvars_to_list(loopvars_T *loopvars)
2842{
2843 // Link in list of loopvarss.
2844 if (first_loopvars != NULL)
2845 first_loopvars->lvs_prev = loopvars;
2846 loopvars->lvs_next = first_loopvars;
2847 loopvars->lvs_prev = NULL;
2848 first_loopvars = loopvars;
2849}
2850
2851 static void
2852remove_loopvars_from_list(loopvars_T *loopvars)
2853{
2854 if (loopvars->lvs_prev == NULL)
2855 first_loopvars = loopvars->lvs_next;
2856 else
2857 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2858 if (loopvars->lvs_next != NULL)
2859 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2860}
2861
2862/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002863 * End of a for or while loop: Handle any variables used by a closure.
2864 */
2865 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002866execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002867{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002868 endloop_T *endloop = &iptr->isn_arg.endloop;
2869 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2870 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002871 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002872 garray_T *gap = &ectx->ec_funcrefs;
2873 int closure_in_use = FALSE;
2874 loopvars_T *loopvars;
2875 typval_T *stack;
2876 int idx;
2877
Bram Moolenaarcc341812022-09-19 15:54:34 +01002878 // Check if any created closure is still being referenced and loopvars have
2879 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002880 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2881 {
2882 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2883
Bram Moolenaarcc341812022-09-19 15:54:34 +01002884 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002885 {
2886 int refcount = pt->pt_refcount;
2887 int i;
2888
2889 // A Reference in a variable inside the loop doesn't count, it gets
2890 // unreferenced at the end of the loop.
2891 for (i = 0; i < endloop->end_var_count; ++i)
2892 {
2893 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2894
2895 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2896 --refcount;
2897 }
2898 if (refcount > 1)
2899 {
2900 closure_in_use = TRUE;
2901 break;
2902 }
2903 }
2904 }
2905
2906 // If no function reference were created since the start of the loop block
2907 // or it is no longer referenced there is nothing to do.
2908 if (!closure_in_use)
2909 return OK;
2910
2911 // A closure is using variables declared inside the loop.
2912 // Move them to the called function.
2913 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2914 if (loopvars == NULL)
2915 return FAIL;
2916
2917 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2918 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2919 loopvars->lvs_ga.ga_data = stack;
2920 if (stack == NULL)
2921 {
2922 vim_free(loopvars);
2923 return FAIL;
2924 }
2925 add_loopvars_to_list(loopvars);
2926
2927 // Move the variable values.
2928 for (idx = 0; idx < endloop->end_var_count; ++idx)
2929 {
2930 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2931
2932 *(stack + idx) = *tv;
2933 tv->v_type = VAR_UNKNOWN;
2934 }
2935
2936 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2937 {
2938 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2939
Bram Moolenaarcc341812022-09-19 15:54:34 +01002940 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002941 {
2942 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002943 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002944
Bram Moolenaarcc341812022-09-19 15:54:34 +01002945 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2946 pt->pt_outer.out_loop[depth].var_idx -=
2947 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002948 }
2949 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002950
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002951 return OK;
2952}
2953
2954/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002955 * Called when a partial is freed or its reference count goes down to one. The
2956 * loopvars may be the only reference to the partials in the local variables.
2957 * Go over all of them, the funcref and can be freed if all partials
2958 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002959 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002960 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002961 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002962loopvars_check_refcount(loopvars_T *loopvars)
2963{
2964 int i;
2965 garray_T *gap = &loopvars->lvs_ga;
2966 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002967 typval_T *stack = gap->ga_data;
2968
Bram Moolenaarcc341812022-09-19 15:54:34 +01002969 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2970 return FALSE;
2971 for (i = 0; i < gap->ga_len; ++i)
2972 {
2973 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2974
2975 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2976 && tv->vval.v_partial->pt_refcount == 1)
2977 {
2978 int depth;
2979
2980 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2981 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2982 ++done;
2983 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002984 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002985 if (done != loopvars->lvs_min_refcount)
2986 return FALSE;
2987
2988 // All partials referencing the loopvars have a reference count of
2989 // one, thus the loopvars is no longer of use.
2990 stack = gap->ga_data;
2991 for (i = 0; i < gap->ga_len; ++i)
2992 clear_tv(stack + i);
2993 vim_free(stack);
2994 remove_loopvars_from_list(loopvars);
2995 vim_free(loopvars);
2996 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002997}
2998
2999/*
3000 * For garbage collecting: set references in all variables referenced by
3001 * all loopvars.
3002 */
3003 int
3004set_ref_in_loopvars(int copyID)
3005{
3006 loopvars_T *loopvars;
3007
3008 for (loopvars = first_loopvars; loopvars != NULL;
3009 loopvars = loopvars->lvs_next)
3010 {
3011 typval_T *stack = loopvars->lvs_ga.ga_data;
3012 int i;
3013
3014 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
3015 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
3016 return TRUE; // abort
3017 }
3018 return FALSE;
3019}
3020
3021/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00003022 * Load instruction for w:/b:/g:/t: variable.
3023 * "isn_type" is used instead of "iptr->isn_type".
3024 */
3025 static int
3026load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
3027{
3028 dictitem_T *di = NULL;
3029 hashtab_T *ht = NULL;
3030 char namespace;
3031
3032 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3033 return NOTDONE;
3034 switch (isn_type)
3035 {
3036 case ISN_LOADG:
3037 ht = get_globvar_ht();
3038 namespace = 'g';
3039 break;
3040 case ISN_LOADB:
3041 ht = &curbuf->b_vars->dv_hashtab;
3042 namespace = 'b';
3043 break;
3044 case ISN_LOADW:
3045 ht = &curwin->w_vars->dv_hashtab;
3046 namespace = 'w';
3047 break;
3048 case ISN_LOADT:
3049 ht = &curtab->tp_vars->dv_hashtab;
3050 namespace = 't';
3051 break;
3052 default: // Cannot reach here
3053 return NOTDONE;
3054 }
3055 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3056
Bram Moolenaar06b77222022-01-25 15:51:56 +00003057 if (di == NULL)
3058 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003059 if (isn_type == ISN_LOADG)
3060 {
3061 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3062
3063 // g:Something could be a function
3064 if (ufunc != NULL)
3065 {
3066 typval_T *tv = STACK_TV_BOT(0);
3067
3068 ++ectx->ec_stack.ga_len;
3069 tv->v_type = VAR_FUNC;
3070 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3071 if (tv->vval.v_string == NULL)
3072 return FAIL;
3073 STRCPY(tv->vval.v_string, "g:");
3074 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3075 return OK;
3076 }
3077 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003078 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003079 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003080 // no check if the item exists in the script but
3081 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003082 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003083 else
3084 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003085 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003086 return FAIL;
3087 }
3088 else
3089 {
3090 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3091 ++ectx->ec_stack.ga_len;
3092 }
3093 return OK;
3094}
3095
Bram Moolenaard0200c82023-01-28 15:19:40 +00003096
3097 static void
3098object_required_error(typval_T *tv)
3099{
3100 garray_T type_list;
3101 ga_init2(&type_list, sizeof(type_T *), 10);
3102 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3103 char *tofree = NULL;
3104 char *typename = type_name(type, &tofree);
3105 semsg(_(e_object_required_found_str), typename);
3106 vim_free(tofree);
3107 clear_type_list(&type_list);
3108}
3109
Bram Moolenaar06b77222022-01-25 15:51:56 +00003110/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003111 * Execute instructions in execution context "ectx".
3112 * Return OK or FAIL;
3113 */
3114 static int
3115exec_instructions(ectx_T *ectx)
3116{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003117 int ret = FAIL;
3118 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003119 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003120
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003121 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003122 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003123
Bram Moolenaarff652882021-05-16 15:24:49 +02003124 // Only catch exceptions in this instruction list.
3125 ectx->ec_trylevel_at_start = trylevel;
3126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 for (;;)
3128 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003129 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003131 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003132
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003133 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003134 {
3135 line_breakcheck();
3136 breakcheck_count = 0;
3137 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003138 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003139 {
3140 // Turn CTRL-C into an exception.
3141 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003142 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003143 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003144 did_throw = TRUE;
3145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003147 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003148 {
3149 // Turn an error message into an exception.
3150 did_emsg = FALSE;
3151 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003152 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003153 did_throw = TRUE;
3154 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003155
3156 // This exception was not caught (yet).
3157 garray_T *trystack = &ectx->ec_trystack;
3158 if (trystack->ga_len > 0)
3159 {
3160 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3161 + trystack->ga_len - 1;
3162 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3163 trycmd->tcd_caught = FALSE;
3164 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003165 }
3166
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003167 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003169 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003170 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003171 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172
3173 // An exception jumps to the first catch, finally, or returns from
3174 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003175 while (index > 0)
3176 {
3177 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003178 // 1. after :try and before :catch - jump to first :catch
3179 // 2. in :catch block - jump to :finally
3180 // 3. in :catch block and no finally - jump to :endtry
3181 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3182 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003183 break;
3184 // In the catch and finally block of this try we have to go up
3185 // one level.
3186 --index;
3187 trycmd = NULL;
3188 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003189 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003191 if (trycmd->tcd_in_catch)
3192 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003193 if (trycmd->tcd_finally_idx > 0)
3194 {
3195 // exception inside ":catch", jump to ":finally" once
3196 ectx->ec_iidx = trycmd->tcd_finally_idx;
3197 trycmd->tcd_finally_idx = 0;
3198 }
3199 else
3200 {
3201 // exception inside ":catch" or ":finally", jump to
3202 // ":endtry"
3203 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3204 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003205 }
3206 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003207 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003208 // jump to first ":catch"
3209 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003210 trycmd->tcd_in_catch = TRUE;
3211 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003212 did_throw = FALSE; // don't come back here until :endtry
3213 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 }
3215 else
3216 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003217 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003218 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003219 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003220 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003221 tv = STACK_TV_BOT(0);
3222 tv->v_type = VAR_NUMBER;
3223 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003224 ++ectx->ec_stack.ga_len;
3225 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003227 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003228 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003229 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003230 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 goto done;
3232 }
3233
Bram Moolenaar4c137212021-04-19 16:48:48 +02003234 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003235 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 }
3237 continue;
3238 }
3239
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003240 /*
3241 * Big switch on the instruction. Most compilers will be turning this
3242 * into an efficient lookup table, since the "case" values are an enum
3243 * with sequential numbers. It may look ugly, but it should be the
3244 * most efficient way.
3245 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003246 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 switch (iptr->isn_type)
3248 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003249 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003250 case ISN_CONSTRUCT:
3251 // "this" is always the local variable at index zero
3252 tv = STACK_TV_VAR(0);
3253 tv->v_type = VAR_OBJECT;
3254 tv->vval.v_object = alloc_clear(
3255 iptr->isn_arg.construct.construct_size);
3256 tv->vval.v_object->obj_class =
3257 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003258 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003259 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003260 object_created(tv->vval.v_object);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01003261
3262 // When creating an enum value object, initialize the name and
3263 // ordinal object variables.
3264 class_T *en = tv->vval.v_object->obj_class;
3265 if (IS_ENUM(en))
3266 enum_set_internal_obj_vars(en, tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003267 break;
3268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 // execute Ex command line
3270 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003271 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003272 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 break;
3274
Bram Moolenaar20677332021-06-06 17:02:53 +02003275 // execute Ex command line split at NL characters.
3276 case ISN_EXEC_SPLIT:
3277 {
3278 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003279 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003280
3281 SOURCING_LNUM = iptr->isn_lnum;
3282 CLEAR_FIELD(cookie);
3283 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3284 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003285 line = get_split_sourceline(0, &cookie, 0, 0);
3286 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003287 get_split_sourceline, &cookie,
3288 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3289 == FAIL
3290 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003291 {
3292 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003293 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003294 }
3295 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003296 }
3297 break;
3298
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003299 // execute Ex command line that is only a range
3300 case ISN_EXECRANGE:
3301 {
3302 exarg_T ea;
3303 char *error = NULL;
3304
3305 CLEAR_FIELD(ea);
3306 ea.cmdidx = CMD_SIZE;
3307 ea.addr_type = ADDR_LINES;
3308 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003309 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003310 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003311 if (ea.cmd == NULL)
3312 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003313 // error is always NULL when using ADDR_LINES
3314 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003315 if (error != NULL)
3316 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003317 emsg(error);
3318 goto on_error;
3319 }
3320 }
3321 break;
3322
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003323 // Evaluate an expression with legacy syntax, push it onto the
3324 // stack.
3325 case ISN_LEGACY_EVAL:
3326 {
3327 char_u *arg = iptr->isn_arg.string;
3328 int res;
3329 int save_flags = cmdmod.cmod_flags;
3330
Bram Moolenaar35578162021-08-02 19:10:38 +02003331 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003332 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003333 tv = STACK_TV_BOT(0);
3334 init_tv(tv);
3335 cmdmod.cmod_flags |= CMOD_LEGACY;
3336 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3337 cmdmod.cmod_flags = save_flags;
3338 if (res == FAIL)
3339 goto on_error;
3340 ++ectx->ec_stack.ga_len;
3341 }
3342 break;
3343
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003344 // push typeval VAR_INSTR with instructions to be executed
3345 case ISN_INSTR:
3346 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003347 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003348 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003349 tv = STACK_TV_BOT(0);
3350 tv->vval.v_instr = ALLOC_ONE(instr_T);
3351 if (tv->vval.v_instr == NULL)
3352 goto on_error;
3353 ++ectx->ec_stack.ga_len;
3354
3355 tv->v_type = VAR_INSTR;
3356 tv->vval.v_instr->instr_ectx = ectx;
3357 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3358 }
3359 break;
3360
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003361 case ISN_SOURCE:
3362 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003363 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003364
Bram Moolenaar89445512022-04-14 12:58:23 +01003365 SOURCING_LNUM = iptr->isn_lnum;
3366 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003367 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003368 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003369 }
3370 break;
3371
Bram Moolenaar4c137212021-04-19 16:48:48 +02003372 // execute :substitute with an expression
3373 case ISN_SUBSTITUTE:
3374 {
3375 subs_T *subs = &iptr->isn_arg.subs;
3376 source_cookie_T cookie;
3377 struct subs_expr_S *save_instr = substitute_instr;
3378 struct subs_expr_S subs_instr;
3379 int res;
3380
3381 subs_instr.subs_ectx = ectx;
3382 subs_instr.subs_instr = subs->subs_instr;
3383 subs_instr.subs_status = OK;
3384 substitute_instr = &subs_instr;
3385
3386 SOURCING_LNUM = iptr->isn_lnum;
3387 // This is very much like ISN_EXEC
3388 CLEAR_FIELD(cookie);
3389 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3390 res = do_cmdline(subs->subs_cmd,
3391 getsourceline, &cookie,
3392 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3393 substitute_instr = save_instr;
3394
3395 if (res == FAIL || did_emsg
3396 || subs_instr.subs_status == FAIL)
3397 goto on_error;
3398 }
3399 break;
3400
3401 case ISN_FINISH:
3402 goto done;
3403
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003404 case ISN_REDIRSTART:
3405 // create a dummy entry for var_redir_str()
3406 if (alloc_redir_lval() == FAIL)
3407 goto on_error;
3408
3409 // The output is stored in growarray "redir_ga" until
3410 // redirection ends.
3411 init_redir_ga();
3412 redir_vname = 1;
3413 break;
3414
3415 case ISN_REDIREND:
3416 {
3417 char_u *res = get_clear_redir_ga();
3418
3419 // End redirection, put redirected text on the stack.
3420 clear_redir_lval();
3421 redir_vname = 0;
3422
Bram Moolenaar35578162021-08-02 19:10:38 +02003423 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003424 {
3425 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003426 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003427 }
3428 tv = STACK_TV_BOT(0);
3429 tv->v_type = VAR_STRING;
3430 tv->vval.v_string = res;
3431 ++ectx->ec_stack.ga_len;
3432 }
3433 break;
3434
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003435 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003436#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003437 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003438 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3439 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003440 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003441#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003442 break;
3443
3444 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003445#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003446 {
3447 exarg_T ea;
3448 int res;
3449
3450 CLEAR_FIELD(ea);
3451 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3452 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3453 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3454 --ectx->ec_stack.ga_len;
3455 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003456 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003457 res = cexpr_core(&ea, tv);
3458 clear_tv(tv);
3459 if (res == FAIL)
3460 goto on_error;
3461 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003462#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003463 break;
3464
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003465 // execute Ex command from pieces on the stack
3466 case ISN_EXECCONCAT:
3467 {
3468 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003469 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003470 int pass;
3471 int i;
3472 char_u *cmd = NULL;
3473 char_u *str;
3474
3475 for (pass = 1; pass <= 2; ++pass)
3476 {
3477 for (i = 0; i < count; ++i)
3478 {
3479 tv = STACK_TV_BOT(i - count);
3480 str = tv->vval.v_string;
3481 if (str != NULL && *str != NUL)
3482 {
3483 if (pass == 2)
3484 STRCPY(cmd + len, str);
3485 len += STRLEN(str);
3486 }
3487 if (pass == 2)
3488 clear_tv(tv);
3489 }
3490 if (pass == 1)
3491 {
3492 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003493 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003494 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003495 len = 0;
3496 }
3497 }
3498
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003499 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003500 do_cmdline_cmd(cmd);
3501 vim_free(cmd);
3502 }
3503 break;
3504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 // execute :echo {string} ...
3506 case ISN_ECHO:
3507 {
3508 int count = iptr->isn_arg.echo.echo_count;
3509 int atstart = TRUE;
3510 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003511 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512
3513 for (idx = 0; idx < count; ++idx)
3514 {
3515 tv = STACK_TV_BOT(idx - count);
3516 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3517 &atstart, &needclr);
3518 clear_tv(tv);
3519 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003520 if (needclr)
3521 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003522 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 }
3524 break;
3525
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003526 // :execute {string} ...
3527 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003528 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003529 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003530 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003531 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003532 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003533 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003534 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003535 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003536 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003537 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003538 garray_T ga;
3539 char_u buf[NUMBUFLEN];
3540 char_u *p;
3541 int len;
3542 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003543 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003544
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003545 if (iptr->isn_type == ISN_ECHOWINDOW)
3546 count = iptr->isn_arg.echowin.ewin_count;
3547 else
3548 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003549 ga_init2(&ga, 1, 80);
3550 for (idx = 0; idx < count; ++idx)
3551 {
3552 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003553 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003554 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003555 if (tv->v_type == VAR_CHANNEL
3556 || tv->v_type == VAR_JOB)
3557 {
3558 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003559 semsg(_(e_using_invalid_value_as_string_str),
3560 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003561 break;
3562 }
3563 else
3564 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003565 }
3566 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003567 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003568
3569 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003570 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003571 failed = TRUE;
3572 else
3573 {
3574 if (ga.ga_len > 0)
3575 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3576 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3577 ga.ga_len += len;
3578 }
3579 clear_tv(tv);
3580 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003581 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003582 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003583 {
3584 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003585 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003586 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003587
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003588 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003589 {
3590 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003591 {
3592 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003593 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003594 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003595 {
3596 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003597 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003598 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003599 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003600 else
3601 {
3602 msg_sb_eol();
3603 if (iptr->isn_type == ISN_ECHOMSG)
3604 {
3605 msg_attr(ga.ga_data, echo_attr);
3606 out_flush();
3607 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003608#ifdef HAS_MESSAGE_WINDOW
3609 else if (iptr->isn_type == ISN_ECHOWINDOW)
3610 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003611 start_echowindow(
3612 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003613 msg_attr(ga.ga_data, echo_attr);
3614 end_echowindow();
3615 }
3616#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003617 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3618 {
3619 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3620 TRUE);
3621 ui_write((char_u *)"\r\n", 2, TRUE);
3622 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003623 else
3624 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003625 SOURCING_LNUM = iptr->isn_lnum;
3626 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003627 }
3628 }
3629 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003630 ga_clear(&ga);
3631 }
3632 break;
3633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 // load local variable or argument
3635 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003636 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003637 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003638 tv = STACK_TV_VAR(iptr->isn_arg.number);
3639 if (tv->v_type == VAR_UNKNOWN)
3640 {
3641 // missing argument or default value v:none
3642 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3643 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3644 }
3645 else
3646 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003647 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 break;
3649
3650 // load v: variable
3651 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003652 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003653 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003655 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 break;
3657
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003658 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 case ISN_LOADSCRIPT:
3660 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003661 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 svar_T *sv;
3663
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003664 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003665 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003666 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003667 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003668 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003669 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003671 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 }
3673 break;
3674
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003675 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003677 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003679 int sid = iptr->isn_arg.loadstore.ls_sid;
3680 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003681 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 if (di == NULL)
3685 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003686 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003687 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003688 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 }
3690 else
3691 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003692 if (iptr->isn_type == ISN_LOADEXPORT)
3693 {
3694 int idx = get_script_item_idx(sid, name, 0,
3695 NULL, NULL);
3696 svar_T *sv;
3697
3698 if (idx >= 0)
3699 {
3700 sv = ((svar_T *)SCRIPT_ITEM(sid)
3701 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003702 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003703 {
3704 SOURCING_LNUM = iptr->isn_lnum;
3705 semsg(_(e_item_not_exported_in_script_str),
3706 name);
3707 goto on_error;
3708 }
3709 }
3710 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003711 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003712 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003714 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 }
3716 }
3717 break;
3718
Bram Moolenaard3aac292020-04-19 14:32:17 +02003719 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003721 case ISN_LOADB:
3722 case ISN_LOADW:
3723 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003725 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003726
Bram Moolenaar06b77222022-01-25 15:51:56 +00003727 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003728 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003729 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003730 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 break;
3734
Bram Moolenaar03290b82020-12-19 16:30:44 +01003735 // load autoload variable
3736 case ISN_LOADAUTO:
3737 {
3738 char_u *name = iptr->isn_arg.string;
3739
Bram Moolenaar35578162021-08-02 19:10:38 +02003740 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003741 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003742 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003743 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003744 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003745 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003746 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003747 }
3748 break;
3749
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003750 // load g:/b:/w:/t: namespace
3751 case ISN_LOADGDICT:
3752 case ISN_LOADBDICT:
3753 case ISN_LOADWDICT:
3754 case ISN_LOADTDICT:
3755 {
3756 dict_T *d = NULL;
3757
3758 switch (iptr->isn_type)
3759 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003760 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3761 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3762 case ISN_LOADWDICT: d = curwin->w_vars; break;
3763 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003764 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003765 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003766 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003767 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003768 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003769 tv = STACK_TV_BOT(0);
3770 tv->v_type = VAR_DICT;
3771 tv->v_lock = 0;
3772 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003773 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003774 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003775 }
3776 break;
3777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 // load &option
3779 case ISN_LOADOPT:
3780 {
3781 typval_T optval;
3782 char_u *name = iptr->isn_arg.string;
3783
Bram Moolenaara8c17702020-04-01 21:17:24 +02003784 // This is not expected to fail, name is checked during
3785 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003786 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003787 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003788 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003789 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003791 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 }
3793 break;
3794
3795 // load $ENV
3796 case ISN_LOADENV:
3797 {
3798 typval_T optval;
3799 char_u *name = iptr->isn_arg.string;
3800
Bram Moolenaar35578162021-08-02 19:10:38 +02003801 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003802 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003803 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003804 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003806 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 }
3808 break;
3809
3810 // load @register
3811 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003812 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003813 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 tv = STACK_TV_BOT(0);
3815 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003816 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003817 // This may result in NULL, which should be equivalent to an
3818 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 tv->vval.v_string = get_reg_contents(
3820 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003821 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 break;
3823
3824 // store local variable
3825 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003826 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 tv = STACK_TV_VAR(iptr->isn_arg.number);
Ernie Rael9ed53752023-12-11 17:40:46 +01003828 if (check_typval_is_value(STACK_TV_BOT(0)) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003829 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003830 clear_tv(STACK_TV_BOT(0));
3831 goto on_error;
3832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 clear_tv(tv);
3834 *tv = *STACK_TV_BOT(0);
3835 break;
3836
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003837 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003838 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003839 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003840 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003841 int sid = iptr->isn_arg.loadstore.ls_sid;
3842 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003843 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003844 dictitem_T *di = find_var_in_ht(ht, 0,
3845 iptr->isn_type == ISN_STORES
3846 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003847
Bram Moolenaar4c137212021-04-19 16:48:48 +02003848 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003849 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003850 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003851 {
3852 if (iptr->isn_type == ISN_STOREEXPORT)
3853 {
3854 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003855 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003856 goto on_error;
3857 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003858 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003859 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003860 else
3861 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003862 if (iptr->isn_type == ISN_STOREEXPORT)
3863 {
3864 int idx = get_script_item_idx(sid, name, 0,
3865 NULL, NULL);
3866
Bram Moolenaar06651632022-04-27 17:54:25 +01003867 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003868 if (idx >= 0)
3869 {
3870 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3871 ->sn_var_vals.ga_data) + idx;
3872
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003873 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003874 {
3875 semsg(_(e_item_not_exported_in_script_str),
3876 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003877 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003878 goto on_error;
3879 }
3880 }
3881 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003882 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003883 {
3884 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003885 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003886 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003887 clear_tv(&di->di_tv);
3888 di->di_tv = *STACK_TV_BOT(0);
3889 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003890 }
3891 break;
3892
3893 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 case ISN_STORESCRIPT:
3895 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003896 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3897 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003899 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003900 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003901 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003902 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003903
3904 // "const" and "final" are checked at compile time, locking
3905 // the value needs to be checked here.
3906 SOURCING_LNUM = iptr->isn_lnum;
3907 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003908 {
3909 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003910 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003911 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 clear_tv(sv->sv_tv);
3914 *sv->sv_tv = *STACK_TV_BOT(0);
3915 }
3916 break;
3917
3918 // store option
3919 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003920 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003922 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3923 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 long n = 0;
3925 char_u *s = NULL;
3926 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003927 char_u numbuf[NUMBUFLEN];
3928 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929
Bram Moolenaar4c137212021-04-19 16:48:48 +02003930 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003931 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 tv = STACK_TV_BOT(0);
3933 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003934 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003936 if (s == NULL)
3937 s = (char_u *)"";
3938 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003939 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3940 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003941 // If the option can be set to a function reference or
3942 // a lambda and the passed value is a function
3943 // reference, then convert it to the name (string) of
3944 // the function reference.
3945 s = tv2string(tv, &tofree, numbuf, 0);
3946 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003947 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003948 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003949 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003950 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003951 goto on_error;
3952 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003955 // must be VAR_NUMBER, CHECKTYPE makes sure
3956 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003957 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003958 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003959 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 if (msg != NULL)
3961 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003962 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003964 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 }
3967 break;
3968
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003969 // store $ENV
3970 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003971 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003972 tv = STACK_TV_BOT(0);
3973 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3974 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003975 break;
3976
3977 // store @r
3978 case ISN_STOREREG:
3979 {
3980 int reg = iptr->isn_arg.number;
3981
Bram Moolenaar4c137212021-04-19 16:48:48 +02003982 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003983 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003984 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003985 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003986 }
3987 break;
3988
3989 // store v: variable
3990 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003991 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003992 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3993 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003994 // should not happen, type is checked when compiling
3995 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003996 break;
3997
Bram Moolenaard3aac292020-04-19 14:32:17 +02003998 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02004000 case ISN_STOREB:
4001 case ISN_STOREW:
4002 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004004 dictitem_T *di;
4005 hashtab_T *ht;
4006 char_u *name = iptr->isn_arg.string + 2;
4007
Bram Moolenaard3aac292020-04-19 14:32:17 +02004008 switch (iptr->isn_type)
4009 {
4010 case ISN_STOREG:
4011 ht = get_globvar_ht();
4012 break;
4013 case ISN_STOREB:
4014 ht = &curbuf->b_vars->dv_hashtab;
4015 break;
4016 case ISN_STOREW:
4017 ht = &curwin->w_vars->dv_hashtab;
4018 break;
4019 case ISN_STORET:
4020 ht = &curtab->tp_vars->dv_hashtab;
4021 break;
4022 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004023 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004024 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025
Bram Moolenaar4c137212021-04-19 16:48:48 +02004026 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004027 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004029 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 else
4031 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004032 SOURCING_LNUM = iptr->isn_lnum;
4033 if (var_check_permission(di, name) == FAIL)
4034 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 clear_tv(&di->di_tv);
4036 di->di_tv = *STACK_TV_BOT(0);
4037 }
4038 }
4039 break;
4040
Bram Moolenaar03290b82020-12-19 16:30:44 +01004041 // store an autoload variable
4042 case ISN_STOREAUTO:
4043 SOURCING_LNUM = iptr->isn_lnum;
4044 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
4045 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004046 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004047 break;
4048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 // store number in local variable
4050 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004051 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 clear_tv(tv);
4053 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004054 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 break;
4056
Bram Moolenaar590162c2022-12-24 21:24:06 +00004057 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004058 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004059 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004060 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004061
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004062 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004063 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004064 if (res == NOTDONE)
4065 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004066 }
4067 break;
4068
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004069 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004070 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004071 if (execute_storerange(iptr, ectx) == FAIL)
4072 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004073 break;
4074
Bram Moolenaard505d172022-12-18 21:42:55 +00004075 case ISN_LOAD_CLASSMEMBER:
4076 {
4077 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4078 goto theend;
4079 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004080 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4081 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004082 ++ectx->ec_stack.ga_len;
4083 }
4084 break;
4085
4086 case ISN_STORE_CLASSMEMBER:
4087 {
4088 classmember_T *cm = &iptr->isn_arg.classmember;
4089 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4090 clear_tv(tv);
4091 *tv = *STACK_TV_BOT(-1);
4092 --ectx->ec_stack.ga_len;
4093 }
4094 break;
4095
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004096 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004097 case ISN_LOADOUTER:
4098 case ISN_STOREOUTER:
4099 {
4100 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004101 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4102 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004103
4104 while (depth > 1 && outer != NULL)
4105 {
4106 outer = outer->out_up;
4107 --depth;
4108 }
4109 if (outer == NULL)
4110 {
4111 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004112 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4113 || ectx->ec_outer_ref == NULL)
4114 // Possibly :def function called from legacy
4115 // context.
4116 emsg(_(e_closure_called_from_invalid_context));
4117 else
4118 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004119 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004120 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004121 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004122 // Variable declared in loop. May be copied if the
4123 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004124 tv = ((typval_T *)outer->out_loop[-depth - 1]
4125 .stack->ga_data)
4126 + outer->out_loop[-depth - 1].var_idx
4127 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004128 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004129 // Variable declared in a function. May be copied if
4130 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004131 tv = ((typval_T *)outer->out_stack->ga_data)
4132 + outer->out_frame_idx + STACK_FRAME_SIZE
4133 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004134 if (iptr->isn_type == ISN_LOADOUTER)
4135 {
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004136 typval_T *copy;
Bram Moolenaar35578162021-08-02 19:10:38 +02004137 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004138 goto theend;
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004139 // careful: ga_grow_inner may re-alloc the stack
4140 if (depth < 0)
4141 copy = ((typval_T *)outer->out_loop[-depth - 1]
4142 .stack->ga_data)
4143 + outer->out_loop[-depth - 1].var_idx
4144 + iptr->isn_arg.outer.outer_idx;
4145 else
4146 copy = ((typval_T *)outer->out_stack->ga_data)
4147 + outer->out_frame_idx + STACK_FRAME_SIZE
4148 + iptr->isn_arg.outer.outer_idx;
4149 // memory was freed, get tv again
4150 if (copy != tv)
4151 tv = copy;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004152 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004153 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004154 }
4155 else
4156 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004157 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004158 clear_tv(tv);
4159 *tv = *STACK_TV_BOT(0);
4160 }
4161 }
4162 break;
4163
Bram Moolenaar752fc692021-01-04 21:57:11 +01004164 // unlet item in list or dict variable
4165 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004166 if (execute_unletindex(iptr, ectx) == FAIL)
4167 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004168 break;
4169
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004170 // unlet range of items in list variable
4171 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004172 if (execute_unletrange(iptr, ectx) == FAIL)
4173 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004174 break;
4175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 // push constant
4177 case ISN_PUSHNR:
4178 case ISN_PUSHBOOL:
4179 case ISN_PUSHSPEC:
4180 case ISN_PUSHF:
4181 case ISN_PUSHS:
4182 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004183 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004184 case ISN_PUSHCHANNEL:
4185 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004186 case ISN_PUSHOBJ:
4187 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004188 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004189 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004191 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004192 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 switch (iptr->isn_type)
4194 {
4195 case ISN_PUSHNR:
4196 tv->v_type = VAR_NUMBER;
4197 tv->vval.v_number = iptr->isn_arg.number;
4198 break;
4199 case ISN_PUSHBOOL:
4200 tv->v_type = VAR_BOOL;
4201 tv->vval.v_number = iptr->isn_arg.number;
4202 break;
4203 case ISN_PUSHSPEC:
4204 tv->v_type = VAR_SPECIAL;
4205 tv->vval.v_number = iptr->isn_arg.number;
4206 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 case ISN_PUSHF:
4208 tv->v_type = VAR_FLOAT;
4209 tv->vval.v_float = iptr->isn_arg.fnumber;
4210 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 case ISN_PUSHBLOB:
4212 blob_copy(iptr->isn_arg.blob, tv);
4213 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004214 case ISN_PUSHFUNC:
4215 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004216 if (iptr->isn_arg.string == NULL)
4217 tv->vval.v_string = NULL;
4218 else
4219 tv->vval.v_string =
4220 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004221 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004222 case ISN_PUSHCHANNEL:
4223#ifdef FEAT_JOB_CHANNEL
4224 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004225 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004226#endif
4227 break;
4228 case ISN_PUSHJOB:
4229#ifdef FEAT_JOB_CHANNEL
4230 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004231 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004232#endif
4233 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004234 case ISN_PUSHOBJ:
4235 tv->v_type = VAR_OBJECT;
4236 tv->vval.v_object = NULL;
4237 break;
4238 case ISN_PUSHCLASS:
4239 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004240 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004241 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 default:
4243 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004244 tv->vval.v_string = iptr->isn_arg.string == NULL
4245 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 }
4247 break;
4248
Bram Moolenaar06b77222022-01-25 15:51:56 +00004249 case ISN_AUTOLOAD:
4250 {
4251 char_u *name = iptr->isn_arg.string;
4252
4253 (void)script_autoload(name, FALSE);
4254 if (find_func(name, TRUE))
4255 {
4256 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4257 goto theend;
4258 tv = STACK_TV_BOT(0);
4259 tv->v_lock = 0;
4260 ++ectx->ec_stack.ga_len;
4261 tv->v_type = VAR_FUNC;
4262 tv->vval.v_string = vim_strsave(name);
4263 }
4264 else
4265 {
4266 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4267
4268 if (res == NOTDONE)
4269 goto theend;
4270 if (res == FAIL)
4271 goto on_error;
4272 }
4273 }
4274 break;
4275
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004276 case ISN_UNLET:
4277 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4278 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004279 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004280 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004281 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004282 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004283 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004284
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004285 case ISN_LOCKUNLOCK:
4286 {
Ernie Raelee865f32023-09-29 19:53:55 +02004287#ifdef LOG_LOCKVAR
4288 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4289 iptr->isn_arg.string);
4290#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004291 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004292
4293 // Stack has the local variable, argument the whole :lock
4294 // or :unlock command, like ISN_EXEC.
4295 --ectx->ec_stack.ga_len;
Ernie Rael4c8da022023-10-11 21:35:11 +02004296 lval_root_T root = { .lr_tv = STACK_TV_BOT(0),
4297 .lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec,
4298 .lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg };
Ernie Rael64885642023-10-04 20:16:22 +02004299 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004300 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004301 iptr->isn_arg.lockunlock.lu_string);
4302 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004303 lval_root = lval_root_save;
4304 if (res == FAIL)
4305 goto on_error;
4306 }
4307 break;
4308
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004309 case ISN_LOCKCONST:
4310 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4311 break;
4312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 // create a list from items on the stack; uses a single allocation
4314 // for the list header and the items
4315 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004316 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004317 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 break;
4319
4320 // create a dict from items on the stack
4321 case ISN_NEWDICT:
4322 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004323 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004325 SOURCING_LNUM = iptr->isn_lnum;
4326 res = exe_newdict(iptr->isn_arg.number, ectx);
4327 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004328 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004329 if (res == MAYBE)
4330 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 }
4332 break;
4333
LemonBoy372bcce2022-04-25 12:43:20 +01004334 case ISN_CONCAT:
4335 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4336 goto theend;
4337 break;
4338
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004339 // create a partial with NULL value
4340 case ISN_NEWPARTIAL:
4341 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4342 goto theend;
4343 ++ectx->ec_stack.ga_len;
4344 tv = STACK_TV_BOT(-1);
4345 tv->v_type = VAR_PARTIAL;
4346 tv->v_lock = 0;
4347 tv->vval.v_partial = NULL;
4348 break;
4349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 // call a :def function
4351 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004352 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004353 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4354 NULL,
4355 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004356 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004357 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 break;
4359
Bram Moolenaard0200c82023-01-28 15:19:40 +00004360 // call a method on an interface
4361 case ISN_METHODCALL:
4362 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004363 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4364
Bram Moolenaard0200c82023-01-28 15:19:40 +00004365 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004366 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004367 if (tv->v_type != VAR_OBJECT)
4368 {
4369 object_required_error(tv);
4370 goto on_error;
4371 }
4372 object_T *obj = tv->vval.v_object;
4373 class_T *cl = obj->obj_class;
4374
4375 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004376 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004377 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004378
4379 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4380 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4381 goto on_error;
4382 }
4383 break;
4384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 // call a builtin function
4386 case ISN_BCALL:
4387 SOURCING_LNUM = iptr->isn_lnum;
4388 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4389 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004390 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004391 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 break;
4393
4394 // call a funcref or partial
4395 case ISN_PCALL:
4396 {
4397 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4398 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004399 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400
4401 SOURCING_LNUM = iptr->isn_lnum;
4402 if (pfunc->cpf_top)
4403 {
4404 // funcref is above the arguments
4405 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4406 }
4407 else
4408 {
4409 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004410 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004411 partial_tv = *STACK_TV_BOT(0);
4412 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004414 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004415 if (tv == &partial_tv)
4416 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004418 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 }
4420 break;
4421
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004422 case ISN_PCALL_END:
4423 // PCALL finished, arguments have been consumed and replaced by
4424 // the return value. Now clear the funcref from the stack,
4425 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004426 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004427 clear_tv(STACK_TV_BOT(-1));
4428 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4429 break;
4430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 // call a user defined function or funcref/partial
4432 case ISN_UCALL:
4433 {
4434 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4435
4436 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004437 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004438 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004439 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 }
4441 break;
4442
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004443 // :defer func(arg)
4444 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004445 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004446 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4447 goto on_error;
4448 break;
4449
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004450 // Return from a :def function call without a value.
4451 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004452 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004453 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004454 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004455 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004456 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004457 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004458 if (iptr->isn_type == ISN_RETURN_VOID)
4459 {
4460 tv->v_type = VAR_VOID;
4461 tv->vval.v_number = 0;
4462 tv->v_lock = 0;
4463 }
4464 else
4465 {
4466 *tv = *STACK_TV_VAR(0);
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01004467 object_T *obj = tv->vval.v_object;
4468 ++obj->obj_refcount;
4469
4470 // Lock all the constant object variables
4471 obj_lock_const_vars(obj);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004472 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004473 // FALLTHROUGH
4474
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004475 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 case ISN_RETURN:
4477 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004478 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004479 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004480
Ernie Raelb077b582023-12-14 20:11:44 +01004481 ///////////////////////////////////////////////////
4482 // TODO: If FAIL, line number in output not correct
4483 ///////////////////////////////////////////////////
4484 if (check_typval_is_value(STACK_TV_BOT(-1)) == FAIL)
4485 goto theend;
4486
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004487 if (trystack->ga_len > 0)
4488 trycmd = ((trycmd_T *)trystack->ga_data)
4489 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004490 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004491 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004493 // jump to ":finally" or ":endtry"
4494 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004495 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004496 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004497 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 trycmd->tcd_return = TRUE;
4499 }
4500 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004501 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 }
4503 break;
4504
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004505 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 case ISN_FUNCREF:
4507 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004508 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4509 ufunc_T *ufunc;
4510 funcref_T *funcref = &iptr->isn_arg.funcref;
4511 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004514 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004515 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004516 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004517 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004518 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004519 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004520 if (extra != NULL && extra->fre_class != NULL)
4521 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004522 class_T *cl;
4523 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004524 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004525 tv = STACK_TV_BOT(-1);
4526 if (tv->v_type != VAR_OBJECT)
4527 {
4528 object_required_error(tv);
4529 vim_free(pt);
4530 goto on_error;
4531 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004532
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004533 object_T *obj = tv->vval.v_object;
4534 cl = obj->obj_class;
4535 // drop the value from the stack
4536 clear_tv(tv);
4537 --ectx->ec_stack.ga_len;
4538
4539 pt->pt_obj = obj;
4540 ++obj->obj_refcount;
4541 }
4542 else
4543 cl = extra->fre_class;
4544
4545 if (extra->fre_object_method)
4546 {
4547 // object method
4548 // convert the interface index to the object index
4549 int idx =
4550 object_index_from_itf_index(extra->fre_class,
4551 TRUE, extra->fre_method_idx, cl);
4552 ufunc = cl->class_obj_methods[idx];
4553 }
4554 else
4555 {
4556 // class method
4557 ufunc =
4558 cl->class_class_functions[extra->fre_method_idx];
4559 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004560 }
4561 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004562 {
4563 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4564 + funcref->fr_dfunc_idx;
4565
4566 ufunc = pt_dfunc->df_ufunc;
4567 }
4568 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004569 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004570 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004571 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004572 if (ufunc == NULL)
4573 {
4574 SOURCING_LNUM = iptr->isn_lnum;
4575 iemsg("ufunc unexpectedly NULL for FUNCREF");
Christian Brabandt915f3bf2024-04-05 20:12:19 +02004576 vim_free(pt);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004577 goto theend;
4578 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004579 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004580 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004581 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004582 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004584 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 tv->vval.v_partial = pt;
4586 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004587 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 }
4589 break;
4590
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004591 // Create a global function from a lambda.
4592 case ISN_NEWFUNC:
4593 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004594 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004595
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004596 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004597 arg->nfa_global, &arg->nfa_loopvar_info,
4598 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004599 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004600 }
4601 break;
4602
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004603 // List functions
4604 case ISN_DEF:
4605 if (iptr->isn_arg.string == NULL)
4606 list_functions(NULL);
4607 else
4608 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004609 exarg_T ea;
4610 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004611
4612 CLEAR_FIELD(ea);
4613 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004614 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004615 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004616 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004617 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004618 }
4619 break;
4620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 // jump if a condition is met
4622 case ISN_JUMP:
4623 {
4624 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004625 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 int jump = TRUE;
4627
4628 if (when != JUMP_ALWAYS)
4629 {
4630 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004631 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004632 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004633 || when == JUMP_IF_COND_TRUE)
4634 {
4635 SOURCING_LNUM = iptr->isn_lnum;
4636 jump = tv_get_bool_chk(tv, &error);
4637 if (error)
4638 goto on_error;
4639 }
4640 else
4641 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004642 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004644 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 {
4646 // drop the value from the stack
4647 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004648 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 }
4650 }
4651 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004652 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 }
4654 break;
4655
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004656 // "while": jump to end if a condition is false
4657 case ISN_WHILE:
4658 {
4659 int error = FALSE;
4660 int jump = TRUE;
4661
4662 tv = STACK_TV_BOT(-1);
4663 SOURCING_LNUM = iptr->isn_lnum;
4664 jump = !tv_get_bool_chk(tv, &error);
4665 if (error)
4666 goto on_error;
4667 // drop the value from the stack
4668 clear_tv(tv);
4669 --ectx->ec_stack.ga_len;
4670 if (jump)
4671 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4672
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004673 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004674 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004675 tv = STACK_TV_VAR(
4676 iptr->isn_arg.whileloop.while_funcref_idx);
4677 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4678 }
4679 break;
4680
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004681 // Jump if an argument with a default value was already set and not
4682 // v:none.
4683 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004684 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004685 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004686 int arg_set = tv->v_type != VAR_UNKNOWN
4687 && !(tv->v_type == VAR_SPECIAL
4688 && tv->vval.v_number == VVAL_NONE);
4689 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004690 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004691 break;
4692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 // top of a for loop
4694 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004695 if (execute_for(iptr, ectx) == FAIL)
4696 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 break;
4698
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004699 // end of a for or while loop
4700 case ISN_ENDLOOP:
4701 if (execute_endloop(iptr, ectx) == FAIL)
4702 goto theend;
4703 break;
4704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 // start of ":try" block
4706 case ISN_TRY:
4707 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004708 trycmd_T *trycmd = NULL;
4709
Bram Moolenaar35578162021-08-02 19:10:38 +02004710 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004711 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004712 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4713 + ectx->ec_trystack.ga_len;
4714 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004716 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004717 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4718 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004719 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004720 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004721 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004722 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004723 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004724 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 }
4726 break;
4727
4728 case ISN_PUSHEXC:
4729 if (current_exception == NULL)
4730 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004731 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004733 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004735 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004736 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004738 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004740 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741 tv->vval.v_string = vim_strsave(
4742 (char_u *)current_exception->value);
4743 break;
4744
4745 case ISN_CATCH:
4746 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004747 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004748 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749
Bram Moolenaar4c137212021-04-19 16:48:48 +02004750 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004751 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004753 trycmd->tcd_caught = TRUE;
4754 trycmd->tcd_did_throw = FALSE;
4755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004757 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 catch_exception(current_exception);
4759 }
4760 break;
4761
Bram Moolenaarc150c092021-02-13 15:02:46 +01004762 case ISN_TRYCONT:
4763 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004764 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004765 trycont_T *trycont = &iptr->isn_arg.trycont;
4766 int i;
4767 trycmd_T *trycmd;
4768 int iidx = trycont->tct_where;
4769
4770 if (trystack->ga_len < trycont->tct_levels)
4771 {
4772 siemsg("TRYCONT: expected %d levels, found %d",
4773 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004774 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004775 }
4776 // Make :endtry jump to any outer try block and the last
4777 // :endtry inside the loop to the loop start.
4778 for (i = trycont->tct_levels; i > 0; --i)
4779 {
4780 trycmd = ((trycmd_T *)trystack->ga_data)
4781 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004782 // Add one to tcd_cont to be able to jump to
4783 // instruction with index zero.
4784 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004785 iidx = trycmd->tcd_finally_idx == 0
4786 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004787 }
4788 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004789 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004790 }
4791 break;
4792
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004793 case ISN_FINALLY:
4794 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004795 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004796 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4797 + trystack->ga_len - 1;
4798
4799 // Reset the index to avoid a return statement jumps here
4800 // again.
4801 trycmd->tcd_finally_idx = 0;
4802 break;
4803 }
4804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 // end of ":try" block
4806 case ISN_ENDTRY:
4807 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004808 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004809 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810
Bram Moolenaar06651632022-04-27 17:54:25 +01004811 --trystack->ga_len;
4812 --trylevel;
4813 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4814 if (trycmd->tcd_did_throw)
4815 did_throw = TRUE;
4816 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004818 // discard the exception
4819 if (caught_stack == current_exception)
4820 caught_stack = caught_stack->caught;
4821 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004823
4824 if (trycmd->tcd_return)
4825 goto func_return;
4826
4827 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4828 {
4829 --ectx->ec_stack.ga_len;
4830 clear_tv(STACK_TV_BOT(0));
4831 }
4832 if (trycmd->tcd_cont != 0)
4833 // handling :continue: jump to outer try block or
4834 // start of the loop
4835 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836 }
4837 break;
4838
4839 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004840 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004841 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004842
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004843 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4844 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004845 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004846 // the function to abort but not display an error.
4847 tv = STACK_TV_BOT(-1);
4848 clear_tv(tv);
4849 tv->v_type = VAR_NUMBER;
4850 tv->vval.v_number = 0;
4851 goto done;
4852 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004853 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004854 tv = STACK_TV_BOT(0);
4855 if (tv->vval.v_string == NULL
4856 || *skipwhite(tv->vval.v_string) == NUL)
4857 {
4858 vim_free(tv->vval.v_string);
4859 SOURCING_LNUM = iptr->isn_lnum;
4860 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004861 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004862 }
4863
4864 // Inside a "catch" we need to first discard the caught
4865 // exception.
4866 if (trystack->ga_len > 0)
4867 {
4868 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4869 + trystack->ga_len - 1;
4870 if (trycmd->tcd_caught && current_exception != NULL)
4871 {
4872 // discard the exception
4873 if (caught_stack == current_exception)
4874 caught_stack = caught_stack->caught;
4875 discard_current_exception();
4876 trycmd->tcd_caught = FALSE;
4877 }
4878 }
4879
Bram Moolenaar90a57162022-02-12 14:23:17 +00004880 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004881 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4882 == FAIL)
4883 {
4884 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004885 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004886 }
4887 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 break;
4890
4891 // compare with special values
4892 case ISN_COMPAREBOOL:
4893 case ISN_COMPARESPECIAL:
4894 {
4895 typval_T *tv1 = STACK_TV_BOT(-2);
4896 typval_T *tv2 = STACK_TV_BOT(-1);
4897 varnumber_T arg1 = tv1->vval.v_number;
4898 varnumber_T arg2 = tv2->vval.v_number;
4899 int res;
4900
Bram Moolenaar06651632022-04-27 17:54:25 +01004901 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4902 res = arg1 == arg2;
4903 else
4904 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905
Bram Moolenaar4c137212021-04-19 16:48:48 +02004906 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 tv1->v_type = VAR_BOOL;
4908 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4909 }
4910 break;
4911
Bram Moolenaar7a222242022-03-01 19:23:24 +00004912 case ISN_COMPARENULL:
4913 {
4914 typval_T *tv1 = STACK_TV_BOT(-2);
4915 typval_T *tv2 = STACK_TV_BOT(-1);
4916 int res;
4917
4918 res = typval_compare_null(tv1, tv2);
4919 if (res == MAYBE)
4920 goto on_error;
4921 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4922 res = !res;
4923 clear_tv(tv1);
4924 clear_tv(tv2);
4925 --ectx->ec_stack.ga_len;
4926 tv1->v_type = VAR_BOOL;
4927 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4928 }
4929 break;
4930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 // Operation with two number arguments
4932 case ISN_OPNR:
4933 case ISN_COMPARENR:
4934 {
4935 typval_T *tv1 = STACK_TV_BOT(-2);
4936 typval_T *tv2 = STACK_TV_BOT(-1);
4937 varnumber_T arg1 = tv1->vval.v_number;
4938 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004939 varnumber_T res = 0;
4940 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004942 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4943 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4944 {
4945 if (arg2 < 0)
4946 {
4947 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004948 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004949 goto on_error;
4950 }
4951 }
4952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 switch (iptr->isn_arg.op.op_type)
4954 {
4955 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004956 case EXPR_DIV: if (arg2 == 0)
4957 div_zero = TRUE;
4958 else
4959 res = arg1 / arg2;
4960 break;
4961 case EXPR_REM: if (arg2 == 0)
4962 div_zero = TRUE;
4963 else
4964 res = arg1 % arg2;
4965 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 case EXPR_SUB: res = arg1 - arg2; break;
4967 case EXPR_ADD: res = arg1 + arg2; break;
4968
4969 case EXPR_EQUAL: res = arg1 == arg2; break;
4970 case EXPR_NEQUAL: res = arg1 != arg2; break;
4971 case EXPR_GREATER: res = arg1 > arg2; break;
4972 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4973 case EXPR_SMALLER: res = arg1 < arg2; break;
4974 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004975 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4976 res = 0;
4977 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004978 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004979 break;
4980 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4981 res = 0;
4982 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004983 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004984 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004985 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 }
4987
Bram Moolenaar4c137212021-04-19 16:48:48 +02004988 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989 if (iptr->isn_type == ISN_COMPARENR)
4990 {
4991 tv1->v_type = VAR_BOOL;
4992 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4993 }
4994 else
4995 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004996 if (div_zero)
4997 {
4998 SOURCING_LNUM = iptr->isn_lnum;
4999 emsg(_(e_divide_by_zero));
5000 goto on_error;
5001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 }
5003 break;
5004
5005 // Computation with two float arguments
5006 case ISN_OPFLOAT:
5007 case ISN_COMPAREFLOAT:
5008 {
5009 typval_T *tv1 = STACK_TV_BOT(-2);
5010 typval_T *tv2 = STACK_TV_BOT(-1);
5011 float_T arg1 = tv1->vval.v_float;
5012 float_T arg2 = tv2->vval.v_float;
5013 float_T res = 0;
5014 int cmp = FALSE;
5015
5016 switch (iptr->isn_arg.op.op_type)
5017 {
5018 case EXPR_MULT: res = arg1 * arg2; break;
5019 case EXPR_DIV: res = arg1 / arg2; break;
5020 case EXPR_SUB: res = arg1 - arg2; break;
5021 case EXPR_ADD: res = arg1 + arg2; break;
5022
5023 case EXPR_EQUAL: cmp = arg1 == arg2; break;
5024 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
5025 case EXPR_GREATER: cmp = arg1 > arg2; break;
5026 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
5027 case EXPR_SMALLER: cmp = arg1 < arg2; break;
5028 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
5029 default: cmp = 0; break;
5030 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005031 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 if (iptr->isn_type == ISN_COMPAREFLOAT)
5033 {
5034 tv1->v_type = VAR_BOOL;
5035 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
5036 }
5037 else
5038 tv1->vval.v_float = res;
5039 }
5040 break;
5041
5042 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00005043 case ISN_COMPAREDICT:
5044 case ISN_COMPAREFUNC:
5045 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005047 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048 {
5049 typval_T *tv1 = STACK_TV_BOT(-2);
5050 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00005051 exprtype_T exprtype = iptr->isn_arg.op.op_type;
5052 int ic = iptr->isn_arg.op.op_ic;
5053 int res = FALSE;
5054 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055
Bram Moolenaar265f8112021-12-19 12:33:05 +00005056 SOURCING_LNUM = iptr->isn_lnum;
5057 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005059 status = typval_compare_list(tv1, tv2,
5060 exprtype, ic, &res);
5061 }
5062 else if (iptr->isn_type == ISN_COMPAREDICT)
5063 {
5064 status = typval_compare_dict(tv1, tv2,
5065 exprtype, ic, &res);
5066 }
5067 else if (iptr->isn_type == ISN_COMPAREFUNC)
5068 {
5069 status = typval_compare_func(tv1, tv2,
5070 exprtype, ic, &res);
5071 }
5072 else if (iptr->isn_type == ISN_COMPARESTRING)
5073 {
5074 status = typval_compare_string(tv1, tv2,
5075 exprtype, ic, &res);
5076 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005077 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005078 {
5079 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005081 else // ISN_COMPAREOBJECT
5082 {
5083 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005084 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005085 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005086 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087 clear_tv(tv1);
5088 clear_tv(tv2);
5089 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005090 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5091 if (status == FAIL)
5092 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 }
5094 break;
5095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 case ISN_COMPAREANY:
5097 {
5098 typval_T *tv1 = STACK_TV_BOT(-2);
5099 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005100 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005102 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103
Bram Moolenaareb26f432020-09-14 16:50:05 +02005104 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005105 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005107 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005108 if (status == FAIL)
5109 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 }
5111 break;
5112
5113 case ISN_ADDLIST:
5114 case ISN_ADDBLOB:
5115 {
5116 typval_T *tv1 = STACK_TV_BOT(-2);
5117 typval_T *tv2 = STACK_TV_BOT(-1);
5118
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005119 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005121 {
5122 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5123 && tv1->vval.v_list != NULL)
5124 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5125 NULL);
5126 else
5127 eval_addlist(tv1, tv2);
5128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129 else
5130 eval_addblob(tv1, tv2);
5131 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005132 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 }
5134 break;
5135
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005136 case ISN_LISTAPPEND:
5137 {
5138 typval_T *tv1 = STACK_TV_BOT(-2);
5139 typval_T *tv2 = STACK_TV_BOT(-1);
5140 list_T *l = tv1->vval.v_list;
5141
5142 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005143 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005144 if (l == NULL)
5145 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005146 emsg(_(e_cannot_add_to_null_list));
5147 goto on_error;
5148 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005149 if (value_check_lock(l->lv_lock, NULL, FALSE))
5150 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005151 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005152 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005153 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005154 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005155 }
5156 break;
5157
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005158 case ISN_BLOBAPPEND:
5159 {
5160 typval_T *tv1 = STACK_TV_BOT(-2);
5161 typval_T *tv2 = STACK_TV_BOT(-1);
5162 blob_T *b = tv1->vval.v_blob;
5163 int error = FALSE;
5164 varnumber_T n;
5165
5166 // add a number to a blob
5167 if (b == NULL)
5168 {
5169 SOURCING_LNUM = iptr->isn_lnum;
5170 emsg(_(e_cannot_add_to_null_blob));
5171 goto on_error;
5172 }
5173 n = tv_get_number_chk(tv2, &error);
5174 if (error)
5175 goto on_error;
5176 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005177 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005178 }
5179 break;
5180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 // Computation with two arguments of unknown type
5182 case ISN_OPANY:
5183 {
5184 typval_T *tv1 = STACK_TV_BOT(-2);
5185 typval_T *tv2 = STACK_TV_BOT(-1);
5186 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005187 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005188 int error = FALSE;
5189
5190 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5191 {
5192 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5193 {
5194 eval_addlist(tv1, tv2);
5195 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005196 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 break;
5198 }
5199 else if (tv1->v_type == VAR_BLOB
5200 && tv2->v_type == VAR_BLOB)
5201 {
5202 eval_addblob(tv1, tv2);
5203 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005204 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 break;
5206 }
5207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 if (tv1->v_type == VAR_FLOAT)
5209 {
5210 f1 = tv1->vval.v_float;
5211 n1 = 0;
5212 }
5213 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005215 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216 n1 = tv_get_number_chk(tv1, &error);
5217 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005218 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 if (tv2->v_type == VAR_FLOAT)
5220 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 if (tv2->v_type == VAR_FLOAT)
5223 {
5224 f2 = tv2->vval.v_float;
5225 n2 = 0;
5226 }
5227 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 {
5229 n2 = tv_get_number_chk(tv2, &error);
5230 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005231 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005232 if (tv1->v_type == VAR_FLOAT)
5233 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 // if there is a float on either side the result is a float
5236 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5237 {
5238 switch (iptr->isn_arg.op.op_type)
5239 {
5240 case EXPR_MULT: f1 = f1 * f2; break;
5241 case EXPR_DIV: f1 = f1 / f2; break;
5242 case EXPR_SUB: f1 = f1 - f2; break;
5243 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005244 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005245 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005246 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 }
5248 clear_tv(tv1);
5249 clear_tv(tv2);
5250 tv1->v_type = VAR_FLOAT;
5251 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005252 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253 }
5254 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005256 int failed = FALSE;
5257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258 switch (iptr->isn_arg.op.op_type)
5259 {
5260 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005261 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5262 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005263 goto on_error;
5264 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265 case EXPR_SUB: n1 = n1 - n2; break;
5266 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005267 default: n1 = num_modulus(n1, n2, &failed);
5268 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005269 goto on_error;
5270 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 }
5272 clear_tv(tv1);
5273 clear_tv(tv2);
5274 tv1->v_type = VAR_NUMBER;
5275 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005276 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005277 }
5278 }
5279 break;
5280
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005281 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005282 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005283 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005284 int is_slice = iptr->isn_type == ISN_STRSLICE;
5285 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005286 char_u *res;
5287
5288 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005289 // string slice: string is at stack-3, first index at
5290 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005291 if (is_slice)
5292 {
5293 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005294 n1 = tv->vval.v_number;
5295 }
5296
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005297 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005298 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005299
Bram Moolenaar4c137212021-04-19 16:48:48 +02005300 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005301 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005302 if (is_slice)
5303 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005304 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005305 else
5306 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005307 // single character (including composing characters).
5308 // If the index is too big or negative the result is
5309 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005310 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005311 vim_free(tv->vval.v_string);
5312 tv->vval.v_string = res;
5313 }
5314 break;
5315
5316 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005317 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005318 case ISN_BLOBINDEX:
5319 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005321 int is_slice = iptr->isn_type == ISN_LISTSLICE
5322 || iptr->isn_type == ISN_BLOBSLICE;
5323 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5324 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005325 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005326 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005327
5328 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005329 // list slice: list is at stack-3, indexes at stack-2 and
5330 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005331 // Same for blob.
5332 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333
5334 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005335 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005337
5338 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339 {
Bram Moolenaared591872020-08-15 22:14:53 +02005340 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005341 n1 = tv->vval.v_number;
5342 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005343 }
Bram Moolenaared591872020-08-15 22:14:53 +02005344
Bram Moolenaar4c137212021-04-19 16:48:48 +02005345 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005346 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005347 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005348 if (is_blob)
5349 {
5350 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5351 n1, n2, FALSE, tv) == FAIL)
5352 goto on_error;
5353 }
5354 else
5355 {
5356 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5357 n1, n2, FALSE, tv, TRUE) == FAIL)
5358 goto on_error;
5359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360 }
5361 break;
5362
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005363 case ISN_ANYINDEX:
5364 case ISN_ANYSLICE:
5365 {
5366 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5367 typval_T *var1, *var2;
5368 int res;
5369
5370 // index: composite is at stack-2, index at stack-1
5371 // slice: composite is at stack-3, indexes at stack-2 and
5372 // stack-1
5373 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005374 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005375 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5376 goto on_error;
5377 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5378 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005379 res = eval_index_inner(tv, is_slice, var1, var2,
5380 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005381 clear_tv(var1);
5382 if (is_slice)
5383 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005384 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005385 if (res == FAIL)
5386 goto on_error;
5387 }
5388 break;
5389
Bram Moolenaar9af78762020-06-16 11:34:42 +02005390 case ISN_SLICE:
5391 {
5392 list_T *list;
5393 int count = iptr->isn_arg.number;
5394
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005395 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005396 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005397 list = tv->vval.v_list;
5398
5399 // no error for short list, expect it to be checked earlier
5400 if (list != NULL && list->lv_len >= count)
5401 {
5402 list_T *newlist = list_slice(list,
5403 count, list->lv_len - 1);
5404
5405 if (newlist != NULL)
5406 {
5407 list_unref(list);
5408 tv->vval.v_list = newlist;
5409 ++newlist->lv_refcount;
5410 }
5411 }
5412 }
5413 break;
5414
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005415 case ISN_GETITEM:
5416 {
5417 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005418 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005419
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005420 // Get list item: list is at stack-1, push item.
5421 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005422 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5423 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005424
Bram Moolenaar35578162021-08-02 19:10:38 +02005425 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005426 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005427 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005428 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005429
5430 // Useful when used in unpack assignment. Reset at
5431 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005432 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005433 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005434 }
5435 break;
5436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005437 case ISN_MEMBER:
5438 {
5439 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005440 char_u *key;
5441 dictitem_T *di;
5442
5443 // dict member: dict is at stack-2, key at stack-1
5444 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005445 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005446 dict = tv->vval.v_dict;
5447
5448 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005449 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005450 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005451 if (key == NULL)
5452 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005453
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005454 if ((di = dict_find(dict, key, -1)) == NULL)
5455 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005456 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005457 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005458
5459 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005460 // stack contents makes sense and the dict stack is
5461 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005462 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005463 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005464 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005465 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005466 tv->v_type = VAR_NUMBER;
5467 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005468 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005469 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005470 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005471 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005472 // Put the dict used on the dict stack, it might be used by
5473 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005474 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005475 if (dict_stack_save(tv) == FAIL)
5476 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005477 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005478 }
5479 break;
5480
5481 // dict member with string key
5482 case ISN_STRINGMEMBER:
5483 {
5484 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005485 dictitem_T *di;
5486
5487 tv = STACK_TV_BOT(-1);
5488 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5489 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005490 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005491 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005492 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493 }
5494 dict = tv->vval.v_dict;
5495
5496 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5497 == NULL)
5498 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005499 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005500 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005501 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005502 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005503 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005504 // Put the dict used on the dict stack, it might be used by
5505 // a dict function later.
5506 if (dict_stack_save(tv) == FAIL)
5507 goto on_fatal_error;
5508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005510 }
5511 break;
5512
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005513 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005514 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005515 {
5516 tv = STACK_TV_BOT(-1);
5517 if (tv->v_type != VAR_OBJECT)
5518 {
5519 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005520 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005521 goto on_error;
5522 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005523
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005524 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005525 if (obj == NULL)
5526 {
5527 SOURCING_LNUM = iptr->isn_lnum;
5528 emsg(_(e_using_null_object));
5529 goto on_error;
5530 }
5531
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005532 int idx;
5533 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005534 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005535 else
5536 {
5537 idx = iptr->isn_arg.classmember.cm_idx;
5538 // convert the interface index to the object index
5539 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005540 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005541 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005542 }
5543
Ernie Rael18143d32023-09-04 22:30:41 +02005544 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005545 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005546 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005547
5548 // Unreference the object after getting the member, it may
5549 // be freed.
5550 object_unref(obj);
5551 }
5552 break;
5553
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005554 case ISN_STORE_THIS:
5555 {
5556 int idx = iptr->isn_arg.number;
5557 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5558 // the members are located right after the object struct
5559 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5560 clear_tv(mtv);
5561 *mtv = *STACK_TV_BOT(-1);
5562 --ectx->ec_stack.ga_len;
5563 }
5564 break;
5565
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005566 case ISN_CLEARDICT:
5567 dict_stack_drop();
5568 break;
5569
5570 case ISN_USEDICT:
5571 {
5572 typval_T *dict_tv = dict_stack_get_tv();
5573
5574 // Turn "dict.Func" into a partial for "Func" bound to
5575 // "dict". Don't do this when "Func" is already a partial
5576 // that was bound explicitly (pt_auto is FALSE).
5577 tv = STACK_TV_BOT(-1);
5578 if (dict_tv != NULL
5579 && dict_tv->v_type == VAR_DICT
5580 && dict_tv->vval.v_dict != NULL
5581 && (tv->v_type == VAR_FUNC
5582 || (tv->v_type == VAR_PARTIAL
5583 && (tv->vval.v_partial->pt_auto
5584 || tv->vval.v_partial->pt_dict == NULL))))
5585 dict_tv->vval.v_dict =
5586 make_partial(dict_tv->vval.v_dict, tv);
5587 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588 }
5589 break;
5590
5591 case ISN_NEGATENR:
5592 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005593 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005594 if (tv->v_type == VAR_FLOAT)
5595 tv->vval.v_float = -tv->vval.v_float;
5596 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005597 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005598 break;
5599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005600 case ISN_CHECKTYPE:
5601 {
5602 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005603 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005604 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005606 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005607 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005608 if (ct->ct_arg_idx > 0)
5609 {
5610 where.wt_index = ct->ct_arg_idx;
5611 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005612 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005613 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005614 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005615 if (r == FAIL)
5616 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005617
5618 // number 0 is FALSE, number 1 is TRUE
5619 if (tv->v_type == VAR_NUMBER
5620 && ct->ct_type->tt_type == VAR_BOOL
5621 && (tv->vval.v_number == 0
5622 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005623 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005624 tv->v_type = VAR_BOOL;
5625 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005626 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005627 }
5628 }
5629 break;
5630
Bram Moolenaar9af78762020-06-16 11:34:42 +02005631 case ISN_CHECKLEN:
5632 {
5633 int min_len = iptr->isn_arg.checklen.cl_min_len;
5634 list_T *list = NULL;
5635
5636 tv = STACK_TV_BOT(-1);
5637 if (tv->v_type == VAR_LIST)
5638 list = tv->vval.v_list;
5639 if (list == NULL || list->lv_len < min_len
5640 || (list->lv_len > min_len
5641 && !iptr->isn_arg.checklen.cl_more_OK))
5642 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005643 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005644 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005645 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005646 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005647 }
5648 }
5649 break;
5650
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005651 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005652 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005653 break;
5654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005655 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005656 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657 {
5658 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005659 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005660
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005661 if (iptr->isn_type == ISN_2BOOL)
5662 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005663 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005664 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005665 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005666 n = !n;
5667 }
5668 else
5669 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005670 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005671 SOURCING_LNUM = iptr->isn_lnum;
5672 n = tv_get_bool_chk(tv, &error);
5673 if (error)
5674 goto on_error;
5675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005676 clear_tv(tv);
5677 tv->v_type = VAR_BOOL;
5678 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5679 }
5680 break;
5681
5682 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005683 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005684 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005685 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5686 iptr->isn_type == ISN_2STRING_ANY,
5687 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005688 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005689 break;
5690
Bram Moolenaar08597872020-12-10 19:43:40 +01005691 case ISN_RANGE:
5692 {
5693 exarg_T ea;
5694 char *errormsg;
5695
Bram Moolenaarece0b872021-01-08 20:40:45 +01005696 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005697 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005698 ea.addr_type = ADDR_LINES;
5699 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005700 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005701 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005702 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005703
Bram Moolenaar35578162021-08-02 19:10:38 +02005704 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005705 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005706 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005707 tv = STACK_TV_BOT(-1);
5708 tv->v_type = VAR_NUMBER;
5709 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005710 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005711 }
5712 break;
5713
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005714 case ISN_PUT:
5715 {
5716 int regname = iptr->isn_arg.put.put_regname;
5717 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5718 char_u *expr = NULL;
5719 int dir = FORWARD;
5720
Bram Moolenaar08597872020-12-10 19:43:40 +01005721 if (lnum < -2)
5722 {
5723 // line number was put on the stack by ISN_RANGE
5724 tv = STACK_TV_BOT(-1);
5725 curwin->w_cursor.lnum = tv->vval.v_number;
5726 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5727 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005728 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005729 }
5730 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005731 // :put! above cursor
5732 dir = BACKWARD;
5733 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005734 {
5735 curwin->w_cursor.lnum = lnum;
5736 if (lnum == 0)
5737 // check_cursor() below will move to line 1
5738 dir = BACKWARD;
5739 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005740
5741 if (regname == '=')
5742 {
5743 tv = STACK_TV_BOT(-1);
5744 if (tv->v_type == VAR_STRING)
5745 expr = tv->vval.v_string;
5746 else
5747 {
5748 expr = typval2string(tv, TRUE); // allocates value
5749 clear_tv(tv);
5750 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005751 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005752 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005753 check_cursor();
5754 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5755 vim_free(expr);
5756 }
5757 break;
5758
Bram Moolenaar02194d22020-10-24 23:08:38 +02005759 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005760 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5761 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5762 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5763 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005764 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5765 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005766 break;
5767
Bram Moolenaar02194d22020-10-24 23:08:38 +02005768 case ISN_CMDMOD_REV:
5769 // filter regprog is owned by the instruction, don't free it
5770 cmdmod.cmod_filter_regmatch.regprog = NULL;
5771 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005772 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5773 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005774 break;
5775
Bram Moolenaar792f7862020-11-23 08:31:18 +01005776 case ISN_UNPACK:
5777 {
5778 int count = iptr->isn_arg.unpack.unp_count;
5779 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5780 list_T *l;
5781 listitem_T *li;
5782 int i;
5783
5784 // Check there is a valid list to unpack.
5785 tv = STACK_TV_BOT(-1);
5786 if (tv->v_type != VAR_LIST)
5787 {
5788 SOURCING_LNUM = iptr->isn_lnum;
5789 emsg(_(e_for_argument_must_be_sequence_of_lists));
5790 goto on_error;
5791 }
5792 l = tv->vval.v_list;
5793 if (l == NULL
5794 || l->lv_len < (semicolon ? count - 1 : count))
5795 {
5796 SOURCING_LNUM = iptr->isn_lnum;
5797 emsg(_(e_list_value_does_not_have_enough_items));
5798 goto on_error;
5799 }
5800 else if (!semicolon && l->lv_len > count)
5801 {
5802 SOURCING_LNUM = iptr->isn_lnum;
5803 emsg(_(e_list_value_has_more_items_than_targets));
5804 goto on_error;
5805 }
5806
5807 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005808 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005809 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005810 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005811
5812 // Variable after semicolon gets a list with the remaining
5813 // items.
5814 if (semicolon)
5815 {
5816 list_T *rem_list =
5817 list_alloc_with_items(l->lv_len - count + 1);
5818
5819 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005820 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005821 tv = STACK_TV_BOT(-count);
5822 tv->vval.v_list = rem_list;
5823 ++rem_list->lv_refcount;
5824 tv->v_lock = 0;
5825 li = l->lv_first;
5826 for (i = 0; i < count - 1; ++i)
5827 li = li->li_next;
5828 for (i = 0; li != NULL; ++i)
5829 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005830 typval_T tvcopy;
5831
5832 copy_tv(&li->li_tv, &tvcopy);
5833 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005834 li = li->li_next;
5835 }
5836 --count;
5837 }
5838
5839 // Produce the values in reverse order, first item last.
5840 li = l->lv_first;
5841 for (i = 0; i < count; ++i)
5842 {
5843 tv = STACK_TV_BOT(-i - 1);
5844 copy_tv(&li->li_tv, tv);
5845 li = li->li_next;
5846 }
5847
5848 list_unref(l);
5849 }
5850 break;
5851
Bram Moolenaarb2049902021-01-24 12:53:53 +01005852 case ISN_PROF_START:
5853 case ISN_PROF_END:
5854 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005855#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005856 funccall_T cookie;
5857 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005858 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005859 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005860
Bram Moolenaarca16c602022-09-06 18:57:08 +01005861 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005862 if (iptr->isn_type == ISN_PROF_START)
5863 {
5864 func_line_start(&cookie, iptr->isn_lnum);
5865 // if we get here the instruction is executed
5866 func_line_exec(&cookie);
5867 }
5868 else
5869 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005870#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005871 }
5872 break;
5873
Bram Moolenaare99d4222021-06-13 14:01:26 +02005874 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005875 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005876 break;
5877
Bram Moolenaar389df252020-07-09 21:20:47 +02005878 case ISN_SHUFFLE:
5879 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005880 typval_T tmp_tv;
5881 int item = iptr->isn_arg.shuffle.shfl_item;
5882 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005883
5884 tmp_tv = *STACK_TV_BOT(-item);
5885 for ( ; up > 0 && item > 1; --up)
5886 {
5887 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5888 --item;
5889 }
5890 *STACK_TV_BOT(-item) = tmp_tv;
5891 }
5892 break;
5893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005894 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005895 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005897 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898 break;
5899 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005900 continue;
5901
Bram Moolenaard032f342020-07-18 18:13:02 +02005902func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005903 // Restore previous function. If the frame pointer is where we started
5904 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005905 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005906 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005907
Bram Moolenaar4c137212021-04-19 16:48:48 +02005908 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005909 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005910 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005911 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005912
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005913on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005914 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005915 // If "emsg_silent" is set then ignore the error, unless it was set
5916 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005917 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005918 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005919 {
5920 // If a sequence of instructions causes an error while ":silent!"
5921 // was used, restore the stack length and jump ahead to restoring
5922 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005923 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005924 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005925 while (ectx->ec_stack.ga_len
5926 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005927 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005928 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005929 clear_tv(STACK_TV_BOT(0));
5930 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005931 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5932 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005933 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005934 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005935 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005936on_fatal_error:
5937 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005938 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005939 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005940 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941 }
5942
5943done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005944 ret = OK;
5945theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005946 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005947
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005948 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005949 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5950 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005951}
5952
5953/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005954 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005955 * "rettv".
5956 * Return OK or FAIL.
5957 */
5958 int
5959exe_typval_instr(typval_T *tv, typval_T *rettv)
5960{
5961 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5962 isn_T *save_instr = ectx->ec_instr;
5963 int save_iidx = ectx->ec_iidx;
5964 int res;
5965
LemonBoyf3b48952022-05-05 13:53:03 +01005966 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5967 // even when the compilation fails.
5968 rettv->v_type = VAR_UNKNOWN;
5969
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005970 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5971 res = exec_instructions(ectx);
5972 if (res == OK)
5973 {
5974 *rettv = *STACK_TV_BOT(-1);
5975 --ectx->ec_stack.ga_len;
5976 }
5977
5978 ectx->ec_instr = save_instr;
5979 ectx->ec_iidx = save_iidx;
5980
5981 return res;
5982}
5983
5984/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005985 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5986 * "substitute_instr".
5987 */
5988 char_u *
5989exe_substitute_instr(void)
5990{
5991 ectx_T *ectx = substitute_instr->subs_ectx;
5992 isn_T *save_instr = ectx->ec_instr;
5993 int save_iidx = ectx->ec_iidx;
5994 char_u *res;
5995
5996 ectx->ec_instr = substitute_instr->subs_instr;
5997 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005998 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005999 typval_T *tv = STACK_TV_BOT(-1);
6000
Bram Moolenaar27523602021-06-05 21:36:19 +02006001 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006002 --ectx->ec_stack.ga_len;
6003 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02006004 }
6005 else
6006 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006007 substitute_instr->subs_status = FAIL;
6008 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02006009 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006010
Bram Moolenaar4c137212021-04-19 16:48:48 +02006011 ectx->ec_instr = save_instr;
6012 ectx->ec_iidx = save_iidx;
6013
6014 return res;
6015}
6016
6017/*
6018 * Call a "def" function from old Vim script.
6019 * Return OK or FAIL.
6020 */
6021 int
6022call_def_function(
6023 ufunc_T *ufunc,
6024 int argc_arg, // nr of arguments
6025 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006026 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02006027 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006028 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01006029 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006030 typval_T *rettv) // return value
6031{
6032 ectx_T ectx; // execution context
6033 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006034 int partial_argc = partial == NULL
6035 || (flags & DEF_USE_PT_ARGV) == 0
6036 ? 0 : partial->pt_argc;
6037 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006038 typval_T *tv;
6039 int idx;
6040 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006041 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006042 sctx_T save_current_sctx = current_sctx;
6043 int did_emsg_before = did_emsg_cumul + did_emsg;
6044 int save_suppress_errthrow = suppress_errthrow;
6045 msglist_T **saved_msg_list = NULL;
6046 msglist_T *private_msg_list = NULL;
6047 int save_emsg_silent_def = emsg_silent_def;
6048 int save_did_emsg_def = did_emsg_def;
6049 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006050 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006051
6052// Get pointer to item in the stack.
6053#undef STACK_TV
6054#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6055
6056// Get pointer to item at the bottom of the stack, -1 is the bottom.
6057#undef STACK_TV_BOT
6058#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6059
6060// Get pointer to a local variable on the stack. Negative for arguments.
6061#undef STACK_TV_VAR
6062#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6063
6064 if (ufunc->uf_def_status == UF_NOT_COMPILED
6065 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006066 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6067 && compile_def_function(ufunc, FALSE,
6068 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006069 {
6070 if (did_emsg_cumul + did_emsg == did_emsg_before)
6071 semsg(_(e_function_is_not_compiled_str),
6072 printable_func_name(ufunc));
6073 return FAIL;
6074 }
6075
6076 {
6077 // Check the function was really compiled.
6078 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6079 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006080 if (dfunc->df_ufunc == NULL)
6081 {
6082 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6083 return FAIL;
6084 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006085 if (INSTRUCTIONS(dfunc) == NULL)
6086 {
6087 iemsg("using call_def_function() on not compiled function");
6088 return FAIL;
6089 }
6090 }
6091
6092 // If depth of calling is getting too high, don't execute the function.
6093 orig_funcdepth = funcdepth_get();
6094 if (funcdepth_increment() == FAIL)
6095 return FAIL;
6096
6097 CLEAR_FIELD(ectx);
6098 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6099 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006100 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006101 {
6102 funcdepth_decrement();
6103 return FAIL;
6104 }
6105 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6106 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6107 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006108 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006109
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006110 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006111 if (idx > 0 && ufunc->uf_va_name == NULL)
6112 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006113 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006114 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006115 goto failed_early;
6116 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006117 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006118 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006119 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006120 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006121 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006122 goto failed_early;
6123 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006124
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006125 // Put values from the partial and arguments on the stack, but no more than
6126 // what the function expects. A lambda can be called with more arguments
6127 // than it uses.
6128 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006129 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6130 ++idx)
6131 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006132 int argv_idx = idx - partial_argc;
6133
6134 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006135 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006136 && tv->v_type == VAR_SPECIAL
6137 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006138 {
6139 // Use the default value.
6140 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6141 }
6142 else
6143 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006144 int done = FALSE;
6145 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6146 {
6147 type_T *expected = ufunc->uf_arg_types[idx];
6148 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6149 {
6150 // When a float is expected and a number was given, convert
6151 // the value.
6152 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6153 STACK_TV_BOT(0)->v_lock = 0;
6154 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6155 done = TRUE;
6156 }
6157 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006158 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006159 goto failed_early;
6160 }
6161 if (!done)
6162 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006163 }
6164 ++ectx.ec_stack.ga_len;
6165 }
6166
6167 // Turn varargs into a list. Empty list if no args.
6168 if (ufunc->uf_va_name != NULL)
6169 {
6170 int vararg_count = argc - ufunc->uf_args.ga_len;
6171
6172 if (vararg_count < 0)
6173 vararg_count = 0;
6174 else
6175 argc -= vararg_count;
6176 if (exe_newlist(vararg_count, &ectx) == FAIL)
6177 goto failed_early;
6178
6179 // Check the type of the list items.
6180 tv = STACK_TV_BOT(-1);
6181 if (ufunc->uf_va_type != NULL
6182 && ufunc->uf_va_type != &t_list_any
6183 && ufunc->uf_va_type->tt_member != &t_any
6184 && tv->vval.v_list != NULL)
6185 {
6186 type_T *expected = ufunc->uf_va_type->tt_member;
6187 listitem_T *li = tv->vval.v_list->lv_first;
6188
6189 for (idx = 0; idx < vararg_count; ++idx)
6190 {
6191 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006192 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006193 goto failed_early;
6194 li = li->li_next;
6195 }
6196 }
6197
6198 if (defcount > 0)
6199 // Move varargs list to below missing default arguments.
6200 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6201 --ectx.ec_stack.ga_len;
6202 }
6203
6204 // Make space for omitted arguments, will store default value below.
6205 // Any varargs list goes after them.
6206 if (defcount > 0)
6207 for (idx = 0; idx < defcount; ++idx)
6208 {
6209 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6210 ++ectx.ec_stack.ga_len;
6211 }
6212 if (ufunc->uf_va_name != NULL)
6213 ++ectx.ec_stack.ga_len;
6214
6215 // Frame pointer points to just after arguments.
6216 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6217 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6218
6219 {
6220 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6221 + ufunc->uf_dfunc_idx;
6222 ufunc_T *base_ufunc = dfunc->df_ufunc;
6223
6224 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006225 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006226 if (partial != NULL || base_ufunc->uf_partial != NULL)
6227 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006228 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6229 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006230 goto failed_early;
6231 if (partial != NULL)
6232 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006233 outer_T *outer = get_pt_outer(partial);
6234
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006235 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006236 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006237 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006238 if (current_ectx != NULL)
6239 {
6240 if (current_ectx->ec_outer_ref != NULL
6241 && current_ectx->ec_outer_ref->or_outer != NULL)
6242 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006243 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006244 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006245 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006246 }
6247 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006248 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006249 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006250 ++partial->pt_refcount;
6251 ectx.ec_outer_ref->or_partial = partial;
6252 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006253 }
6254 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006255 {
6256 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6257 ++base_ufunc->uf_partial->pt_refcount;
6258 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6259 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006260 }
6261 }
6262
6263 // dummy frame entries
6264 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6265 {
6266 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6267 ++ectx.ec_stack.ga_len;
6268 }
6269
6270 {
6271 // Reserve space for local variables and any closure reference count.
6272 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6273 + ufunc->uf_dfunc_idx;
6274
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006275 // Initialize variables to zero. That avoids having to generate
6276 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006277 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006278 {
6279 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6280 STACK_TV_VAR(idx)->vval.v_number = 0;
6281 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006282 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006283
6284 if (object != NULL)
6285 {
6286 // the object is always the variable at index zero
6287 tv = STACK_TV_VAR(0);
6288 tv->v_type = VAR_OBJECT;
6289 tv->vval.v_object = object;
6290 }
6291
Bram Moolenaar4c137212021-04-19 16:48:48 +02006292 if (dfunc->df_has_closure)
6293 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006294 // Initialize the variable that counts how many closures were
6295 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006296 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6297 STACK_TV_VAR(idx)->vval.v_number = 0;
6298 ++ectx.ec_stack.ga_len;
6299 }
6300
6301 ectx.ec_instr = INSTRUCTIONS(dfunc);
6302 }
6303
Bram Moolenaar58779852022-09-06 18:31:14 +01006304 // Store the execution context in funccal, used by invoke_all_defer().
6305 if (funccal != NULL)
6306 funccal->fc_ectx = &ectx;
6307
Bram Moolenaar4c137212021-04-19 16:48:48 +02006308 // Following errors are in the function, not the caller.
6309 // Commands behave like vim9script.
6310 estack_push_ufunc(ufunc, 1);
6311 current_sctx = ufunc->uf_script_ctx;
6312 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6313
6314 // Use a specific location for storing error messages to be converted to an
6315 // exception.
6316 saved_msg_list = msg_list;
6317 msg_list = &private_msg_list;
6318
6319 // Do turn errors into exceptions.
6320 suppress_errthrow = FALSE;
6321
6322 // Do not delete the function while executing it.
6323 ++ufunc->uf_calls;
6324
6325 // When ":silent!" was used before calling then we still abort the
6326 // function. If ":silent!" is used in the function then we don't.
6327 emsg_silent_def = emsg_silent;
6328 did_emsg_def = 0;
6329
LemonBoyc5d27442023-08-19 13:02:35 +02006330 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006331
Bram Moolenaar5800c792022-09-22 17:34:01 +01006332 /*
6333 * Execute the instructions until done.
6334 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006335 ret = exec_instructions(&ectx);
6336 if (ret == OK)
6337 {
6338 // function finished, get result from the stack.
6339 if (ufunc->uf_ret_type == &t_void)
6340 {
6341 rettv->v_type = VAR_VOID;
6342 }
6343 else
6344 {
6345 tv = STACK_TV_BOT(-1);
6346 *rettv = *tv;
6347 tv->v_type = VAR_UNKNOWN;
6348 }
6349 }
6350
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006351 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006352 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006353
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006354 // Deal with any remaining closures, they may be in use somewhere.
6355 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006356 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006357 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006358 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006359 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006360
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006361 estack_pop();
6362 current_sctx = save_current_sctx;
6363
Bram Moolenaar2336c372021-12-06 15:06:54 +00006364 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6365 // Function was unreferenced while being used, free it now.
6366 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006367
Bram Moolenaar352134b2020-10-17 22:04:08 +02006368 if (*msg_list != NULL && saved_msg_list != NULL)
6369 {
6370 msglist_T **plist = saved_msg_list;
6371
6372 // Append entries from the current msg_list (uncaught exceptions) to
6373 // the saved msg_list.
6374 while (*plist != NULL)
6375 plist = &(*plist)->next;
6376
6377 *plist = *msg_list;
6378 }
6379 msg_list = saved_msg_list;
6380
Bram Moolenaar4c137212021-04-19 16:48:48 +02006381 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006382 {
6383 cmdmod.cmod_filter_regmatch.regprog = NULL;
6384 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006385 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006386 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006387 emsg_silent_def = save_emsg_silent_def;
6388 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006389
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006390failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006391 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006393 {
6394 tv = STACK_TV(idx);
6395 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6396 clear_tv(tv);
6397 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006398 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006401 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006402 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006403 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006404 if (ectx.ec_outer_ref->or_outer_allocated)
6405 vim_free(ectx.ec_outer_ref->or_outer);
6406 partial_unref(ectx.ec_outer_ref->or_partial);
6407 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006408 }
6409
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006410 // Not sure if this is necessary.
6411 suppress_errthrow = save_suppress_errthrow;
6412
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006413 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6414 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006415 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006416 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006417 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006418 return ret;
6419}
6420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006421/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006422 * Called when a def function has finished (possibly failed).
6423 * Invoke all the function returns to clean up and invoke deferred functions,
6424 * except the toplevel one.
6425 */
6426 void
6427unwind_def_callstack(ectx_T *ectx)
6428{
6429 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6430 func_return(ectx);
6431}
6432
6433/*
dundargocc57b5bc2022-11-02 13:30:51 +00006434 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006435 */
6436 void
6437may_invoke_defer_funcs(ectx_T *ectx)
6438{
6439 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6440
6441 if (dfunc->df_defer_var_idx > 0)
6442 invoke_defer_funcs(ectx);
6443}
6444
6445/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006446 * Return loopvarinfo in a printable form in allocated memory.
6447 */
6448 static char_u *
6449printable_loopvarinfo(loopvarinfo_T *lvi)
6450{
6451 garray_T ga;
6452 int depth;
6453
6454 ga_init2(&ga, 1, 100);
6455 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6456 {
6457 if (ga_grow(&ga, 50) == FAIL)
6458 break;
6459 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006460 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006461 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006462 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006463 lvi->lvi_loop[depth].var_idx,
6464 lvi->lvi_loop[depth].var_idx
6465 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006466 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006467 }
6468 return ga.ga_data;
6469}
6470
6471/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006472 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6473 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6474 * "pfx" is prefixed to every line.
6475 */
6476 static void
6477list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6478{
6479 int line_idx = 0;
6480 int prev_current = 0;
6481 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006482 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006483
6484 for (current = 0; current < instr_count; ++current)
6485 {
6486 isn_T *iptr = &instr[current];
6487 char *line;
6488
6489 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006490 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006491 while (line_idx < iptr->isn_lnum
6492 && line_idx < ufunc->uf_lines.ga_len)
6493 {
6494 if (current > prev_current)
6495 {
6496 msg_puts("\n\n");
6497 prev_current = current;
6498 }
6499 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6500 if (line != NULL)
6501 msg(line);
6502 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006503 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6504 {
6505 int first_def_arg = ufunc->uf_args.ga_len
6506 - ufunc->uf_def_args.ga_len;
6507
6508 if (def_arg_idx > 0)
6509 msg_puts("\n\n");
6510 msg_start();
6511 msg_puts(" ");
6512 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6513 first_def_arg + def_arg_idx]);
6514 msg_puts(" = ");
6515 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6516 msg_clr_eos();
6517 msg_end();
6518 }
6519 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006520
6521 switch (iptr->isn_type)
6522 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006523 case ISN_CONSTRUCT:
6524 smsg("%s%4d NEW %s size %d", pfx, current,
6525 iptr->isn_arg.construct.construct_class->class_name,
6526 (int)iptr->isn_arg.construct.construct_size);
6527 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006528 case ISN_EXEC:
6529 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6530 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006531 case ISN_EXEC_SPLIT:
6532 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6533 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006534 case ISN_EXECRANGE:
6535 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6536 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006537 case ISN_LEGACY_EVAL:
6538 smsg("%s%4d EVAL legacy %s", pfx, current,
6539 iptr->isn_arg.string);
6540 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006541 case ISN_REDIRSTART:
6542 smsg("%s%4d REDIR", pfx, current);
6543 break;
6544 case ISN_REDIREND:
6545 smsg("%s%4d REDIR END%s", pfx, current,
6546 iptr->isn_arg.number ? " append" : "");
6547 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006548 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006549#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006550 smsg("%s%4d CEXPR pre %s", pfx, current,
6551 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006552#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006553 break;
6554 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006555#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006556 {
6557 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6558
6559 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6560 cexpr_get_auname(cer->cer_cmdidx),
6561 cer->cer_forceit ? "!" : "",
6562 cer->cer_cmdline);
6563 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006564#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006565 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006566 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006567 smsg("%s%4d INSTR", pfx, current);
6568 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6569 msg(" -------------");
6570 break;
6571 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006572 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006573 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6574
6575 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006576 }
6577 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006578 case ISN_SUBSTITUTE:
6579 {
6580 subs_T *subs = &iptr->isn_arg.subs;
6581
6582 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6583 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6584 msg(" -------------");
6585 }
6586 break;
6587 case ISN_EXECCONCAT:
6588 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6589 (varnumber_T)iptr->isn_arg.number);
6590 break;
6591 case ISN_ECHO:
6592 {
6593 echo_T *echo = &iptr->isn_arg.echo;
6594
6595 smsg("%s%4d %s %d", pfx, current,
6596 echo->echo_with_white ? "ECHO" : "ECHON",
6597 echo->echo_count);
6598 }
6599 break;
6600 case ISN_EXECUTE:
6601 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006602 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006603 break;
6604 case ISN_ECHOMSG:
6605 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006606 (varnumber_T)(iptr->isn_arg.number));
6607 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006608 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006609 if (iptr->isn_arg.echowin.ewin_time > 0)
6610 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6611 iptr->isn_arg.echowin.ewin_count,
6612 iptr->isn_arg.echowin.ewin_time);
6613 else
6614 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6615 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006616 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006617 case ISN_ECHOCONSOLE:
6618 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6619 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006620 break;
6621 case ISN_ECHOERR:
6622 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006623 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006624 break;
6625 case ISN_LOAD:
6626 {
6627 if (iptr->isn_arg.number < 0)
6628 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6629 (varnumber_T)(iptr->isn_arg.number
6630 + STACK_FRAME_SIZE));
6631 else
6632 smsg("%s%4d LOAD $%lld", pfx, current,
6633 (varnumber_T)(iptr->isn_arg.number));
6634 }
6635 break;
6636 case ISN_LOADOUTER:
6637 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006638 isn_outer_T *outer = &iptr->isn_arg.outer;
6639
6640 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006641 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006642 outer->outer_depth,
6643 outer->outer_idx + STACK_FRAME_SIZE);
6644 else if (outer->outer_depth < 0)
6645 smsg("%s%4d LOADOUTER $%d in loop level %d",
6646 pfx, current,
6647 outer->outer_idx,
6648 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006649 else
6650 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006651 outer->outer_depth,
6652 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006653 }
6654 break;
6655 case ISN_LOADV:
6656 smsg("%s%4d LOADV v:%s", pfx, current,
6657 get_vim_var_name(iptr->isn_arg.number));
6658 break;
6659 case ISN_LOADSCRIPT:
6660 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006661 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6662 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6663 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006664
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006665 sv = get_script_svar(sref, -1);
6666 if (sv == NULL)
6667 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6668 pfx, current, si->sn_name);
6669 else
6670 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006671 sv->sv_name,
6672 sref->sref_idx,
6673 si->sn_name);
6674 }
6675 break;
6676 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006677 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006678 {
6679 scriptitem_T *si = SCRIPT_ITEM(
6680 iptr->isn_arg.loadstore.ls_sid);
6681
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006682 smsg("%s%4d %s s:%s from %s", pfx, current,
6683 iptr->isn_type == ISN_LOADS ? "LOADS"
6684 : "LOADEXPORT",
6685 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006686 }
6687 break;
6688 case ISN_LOADAUTO:
6689 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6690 break;
6691 case ISN_LOADG:
6692 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6693 break;
6694 case ISN_LOADB:
6695 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6696 break;
6697 case ISN_LOADW:
6698 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6699 break;
6700 case ISN_LOADT:
6701 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6702 break;
6703 case ISN_LOADGDICT:
6704 smsg("%s%4d LOAD g:", pfx, current);
6705 break;
6706 case ISN_LOADBDICT:
6707 smsg("%s%4d LOAD b:", pfx, current);
6708 break;
6709 case ISN_LOADWDICT:
6710 smsg("%s%4d LOAD w:", pfx, current);
6711 break;
6712 case ISN_LOADTDICT:
6713 smsg("%s%4d LOAD t:", pfx, current);
6714 break;
6715 case ISN_LOADOPT:
6716 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6717 break;
6718 case ISN_LOADENV:
6719 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6720 break;
6721 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006722 smsg("%s%4d LOADREG @%c", pfx, current,
6723 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006724 break;
6725
6726 case ISN_STORE:
6727 if (iptr->isn_arg.number < 0)
6728 smsg("%s%4d STORE arg[%lld]", pfx, current,
6729 iptr->isn_arg.number + STACK_FRAME_SIZE);
6730 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006731 smsg("%s%4d STORE $%lld", pfx, current,
6732 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006733 break;
6734 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006735 {
6736 isn_outer_T *outer = &iptr->isn_arg.outer;
6737
6738 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6739 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6740 pfx, current, outer->outer_idx);
6741 else
6742 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6743 outer->outer_depth, outer->outer_idx);
6744 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006745 break;
6746 case ISN_STOREV:
6747 smsg("%s%4d STOREV v:%s", pfx, current,
6748 get_vim_var_name(iptr->isn_arg.number));
6749 break;
6750 case ISN_STOREAUTO:
6751 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6752 break;
6753 case ISN_STOREG:
6754 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6755 break;
6756 case ISN_STOREB:
6757 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6758 break;
6759 case ISN_STOREW:
6760 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6761 break;
6762 case ISN_STORET:
6763 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6764 break;
6765 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006766 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006767 {
6768 scriptitem_T *si = SCRIPT_ITEM(
6769 iptr->isn_arg.loadstore.ls_sid);
6770
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006771 smsg("%s%4d %s %s in %s", pfx, current,
6772 iptr->isn_type == ISN_STORES
6773 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006774 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6775 }
6776 break;
6777 case ISN_STORESCRIPT:
6778 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006779 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6780 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6781 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006782
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006783 sv = get_script_svar(sref, -1);
6784 if (sv == NULL)
6785 smsg("%s%4d STORESCRIPT [deleted] in %s",
6786 pfx, current, si->sn_name);
6787 else
6788 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006789 sv->sv_name,
6790 sref->sref_idx,
6791 si->sn_name);
6792 }
6793 break;
6794 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006795 case ISN_STOREFUNCOPT:
6796 smsg("%s%4d %s &%s", pfx, current,
6797 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006798 iptr->isn_arg.storeopt.so_name);
6799 break;
6800 case ISN_STOREENV:
6801 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6802 break;
6803 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006804 smsg("%s%4d STOREREG @%c", pfx, current,
6805 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006806 break;
6807 case ISN_STORENR:
6808 smsg("%s%4d STORE %lld in $%d", pfx, current,
6809 iptr->isn_arg.storenr.stnr_val,
6810 iptr->isn_arg.storenr.stnr_idx);
6811 break;
6812
6813 case ISN_STOREINDEX:
6814 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006815 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006816 break;
6817
6818 case ISN_STORERANGE:
6819 smsg("%s%4d STORERANGE", pfx, current);
6820 break;
6821
Bram Moolenaard505d172022-12-18 21:42:55 +00006822 case ISN_LOAD_CLASSMEMBER:
6823 case ISN_STORE_CLASSMEMBER:
6824 {
6825 class_T *cl = iptr->isn_arg.classmember.cm_class;
6826 int idx = iptr->isn_arg.classmember.cm_idx;
6827 ocmember_T *ocm = &cl->class_class_members[idx];
6828 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6829 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6830 ? "LOAD" : "STORE",
6831 cl->class_name, ocm->ocm_name);
6832 }
6833 break;
6834
Bram Moolenaar4c137212021-04-19 16:48:48 +02006835 // constants
6836 case ISN_PUSHNR:
6837 smsg("%s%4d PUSHNR %lld", pfx, current,
6838 (varnumber_T)(iptr->isn_arg.number));
6839 break;
6840 case ISN_PUSHBOOL:
6841 case ISN_PUSHSPEC:
6842 smsg("%s%4d PUSH %s", pfx, current,
6843 get_var_special_name(iptr->isn_arg.number));
6844 break;
6845 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006846 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006847 break;
6848 case ISN_PUSHS:
6849 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6850 break;
6851 case ISN_PUSHBLOB:
6852 {
6853 char_u *r;
6854 char_u numbuf[NUMBUFLEN];
6855 char_u *tofree;
6856
6857 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6858 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6859 vim_free(tofree);
6860 }
6861 break;
6862 case ISN_PUSHFUNC:
6863 {
6864 char *name = (char *)iptr->isn_arg.string;
6865
6866 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6867 name == NULL ? "[none]" : name);
6868 }
6869 break;
6870 case ISN_PUSHCHANNEL:
6871#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006872 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006873#endif
6874 break;
6875 case ISN_PUSHJOB:
6876#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006877 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006878#endif
6879 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006880 case ISN_PUSHOBJ:
6881 smsg("%s%4d PUSHOBJ null", pfx, current);
6882 break;
6883 case ISN_PUSHCLASS:
6884 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006885 iptr->isn_arg.classarg == NULL ? "null"
6886 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006887 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006888 case ISN_PUSHEXC:
6889 smsg("%s%4d PUSH v:exception", pfx, current);
6890 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006891 case ISN_AUTOLOAD:
6892 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6893 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006894 case ISN_UNLET:
6895 smsg("%s%4d UNLET%s %s", pfx, current,
6896 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6897 iptr->isn_arg.unlet.ul_name);
6898 break;
6899 case ISN_UNLETENV:
6900 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6901 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6902 iptr->isn_arg.unlet.ul_name);
6903 break;
6904 case ISN_UNLETINDEX:
6905 smsg("%s%4d UNLETINDEX", pfx, current);
6906 break;
6907 case ISN_UNLETRANGE:
6908 smsg("%s%4d UNLETRANGE", pfx, current);
6909 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006910 case ISN_LOCKUNLOCK:
6911 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6912 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006913 case ISN_LOCKCONST:
6914 smsg("%s%4d LOCKCONST", pfx, current);
6915 break;
6916 case ISN_NEWLIST:
6917 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006918 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006919 break;
6920 case ISN_NEWDICT:
6921 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006922 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006923 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006924 case ISN_NEWPARTIAL:
6925 smsg("%s%4d NEWPARTIAL", pfx, current);
6926 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006927
6928 // function call
6929 case ISN_BCALL:
6930 {
6931 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6932
6933 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6934 internal_func_name(cbfunc->cbf_idx),
6935 cbfunc->cbf_argcount);
6936 }
6937 break;
6938 case ISN_DCALL:
6939 {
6940 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6941 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6942 + cdfunc->cdf_idx;
6943
6944 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006945 printable_func_name(df->df_ufunc),
6946 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006947 }
6948 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006949 case ISN_METHODCALL:
6950 {
6951 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6952
6953 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6954 mfunc->cmf_itf->class_name,
6955 mfunc->cmf_itf->class_obj_methods[
6956 mfunc->cmf_idx]->uf_name,
6957 mfunc->cmf_argcount);
6958 }
6959 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006960 case ISN_UCALL:
6961 {
6962 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6963
6964 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6965 cufunc->cuf_name, cufunc->cuf_argcount);
6966 }
6967 break;
6968 case ISN_PCALL:
6969 {
6970 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6971
6972 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6973 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6974 }
6975 break;
6976 case ISN_PCALL_END:
6977 smsg("%s%4d PCALL end", pfx, current);
6978 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006979 case ISN_DEFER:
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02006980 smsg("%s%4d DEFER %d args", pfx, current,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006981 (int)iptr->isn_arg.defer.defer_argcount);
6982 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006983 case ISN_RETURN:
6984 smsg("%s%4d RETURN", pfx, current);
6985 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006986 case ISN_RETURN_VOID:
6987 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006988 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006989 case ISN_RETURN_OBJECT:
6990 smsg("%s%4d RETURN object", pfx, current);
6991 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006992 case ISN_FUNCREF:
6993 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006994 funcref_T *funcref = &iptr->isn_arg.funcref;
6995 funcref_extra_T *extra = funcref->fr_extra;
6996 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006997
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006998 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006999 {
7000 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
7001 + funcref->fr_dfunc_idx;
7002 name = df->df_ufunc->uf_name;
7003 }
7004 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007005 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00007006 if (extra != NULL && extra->fre_class != NULL)
7007 {
7008 smsg("%s%4d FUNCREF %s.%s", pfx, current,
7009 extra->fre_class->class_name, name);
7010 }
7011 else if (extra == NULL
7012 || extra->fre_loopvar_info.lvi_depth == 0)
7013 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007014 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00007015 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007016 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007017 {
7018 char_u *info = printable_loopvarinfo(
7019 &extra->fre_loopvar_info);
7020
7021 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
7022 name, info);
7023 vim_free(info);
7024 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007025 }
7026 break;
7027
7028 case ISN_NEWFUNC:
7029 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01007030 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007031
Bram Moolenaarcc341812022-09-19 15:54:34 +01007032 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007033 smsg("%s%4d NEWFUNC %s %s", pfx, current,
7034 arg->nfa_lambda, arg->nfa_global);
7035 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007036 {
7037 char_u *info = printable_loopvarinfo(
7038 &arg->nfa_loopvar_info);
7039
7040 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
7041 arg->nfa_lambda, arg->nfa_global, info);
7042 vim_free(info);
7043 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007044 }
7045 break;
7046
7047 case ISN_DEF:
7048 {
7049 char_u *name = iptr->isn_arg.string;
7050
7051 smsg("%s%4d DEF %s", pfx, current,
7052 name == NULL ? (char_u *)"" : name);
7053 }
7054 break;
7055
7056 case ISN_JUMP:
7057 {
7058 char *when = "?";
7059
7060 switch (iptr->isn_arg.jump.jump_when)
7061 {
7062 case JUMP_ALWAYS:
7063 when = "JUMP";
7064 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007065 case JUMP_NEVER:
7066 iemsg("JUMP_NEVER should not be used");
7067 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007068 case JUMP_AND_KEEP_IF_TRUE:
7069 when = "JUMP_AND_KEEP_IF_TRUE";
7070 break;
7071 case JUMP_IF_FALSE:
7072 when = "JUMP_IF_FALSE";
7073 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007074 case JUMP_WHILE_FALSE:
7075 when = "JUMP_WHILE_FALSE"; // unused
7076 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007077 case JUMP_IF_COND_FALSE:
7078 when = "JUMP_IF_COND_FALSE";
7079 break;
7080 case JUMP_IF_COND_TRUE:
7081 when = "JUMP_IF_COND_TRUE";
7082 break;
7083 }
7084 smsg("%s%4d %s -> %d", pfx, current, when,
7085 iptr->isn_arg.jump.jump_where);
7086 }
7087 break;
7088
7089 case ISN_JUMP_IF_ARG_SET:
7090 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7091 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7092 iptr->isn_arg.jump.jump_where);
7093 break;
7094
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007095 case ISN_JUMP_IF_ARG_NOT_SET:
7096 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7097 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7098 iptr->isn_arg.jump.jump_where);
7099 break;
7100
Bram Moolenaar4c137212021-04-19 16:48:48 +02007101 case ISN_FOR:
7102 {
7103 forloop_T *forloop = &iptr->isn_arg.forloop;
7104
7105 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007106 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007107 }
7108 break;
7109
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007110 case ISN_ENDLOOP:
7111 {
7112 endloop_T *endloop = &iptr->isn_arg.endloop;
7113
Bram Moolenaarcc341812022-09-19 15:54:34 +01007114 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7115 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007116 endloop->end_funcref_idx,
7117 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007118 endloop->end_var_idx + endloop->end_var_count - 1,
7119 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007120 }
7121 break;
7122
7123 case ISN_WHILE:
7124 {
7125 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7126
7127 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7128 whileloop->while_funcref_idx,
7129 whileloop->while_end);
7130 }
7131 break;
7132
Bram Moolenaar4c137212021-04-19 16:48:48 +02007133 case ISN_TRY:
7134 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007135 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007136
7137 if (try->try_ref->try_finally == 0)
7138 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7139 pfx, current,
7140 try->try_ref->try_catch,
7141 try->try_ref->try_endtry);
7142 else
7143 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7144 pfx, current,
7145 try->try_ref->try_catch,
7146 try->try_ref->try_finally,
7147 try->try_ref->try_endtry);
7148 }
7149 break;
7150 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007151 smsg("%s%4d CATCH", pfx, current);
7152 break;
7153 case ISN_TRYCONT:
7154 {
7155 trycont_T *trycont = &iptr->isn_arg.trycont;
7156
7157 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7158 trycont->tct_levels,
7159 trycont->tct_levels == 1 ? "" : "s",
7160 trycont->tct_where);
7161 }
7162 break;
7163 case ISN_FINALLY:
7164 smsg("%s%4d FINALLY", pfx, current);
7165 break;
7166 case ISN_ENDTRY:
7167 smsg("%s%4d ENDTRY", pfx, current);
7168 break;
7169 case ISN_THROW:
7170 smsg("%s%4d THROW", pfx, current);
7171 break;
7172
7173 // expression operations on number
7174 case ISN_OPNR:
7175 case ISN_OPFLOAT:
7176 case ISN_OPANY:
7177 {
7178 char *what;
7179 char *ins;
7180
7181 switch (iptr->isn_arg.op.op_type)
7182 {
7183 case EXPR_MULT: what = "*"; break;
7184 case EXPR_DIV: what = "/"; break;
7185 case EXPR_REM: what = "%"; break;
7186 case EXPR_SUB: what = "-"; break;
7187 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007188 case EXPR_LSHIFT: what = "<<"; break;
7189 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007190 default: what = "???"; break;
7191 }
7192 switch (iptr->isn_type)
7193 {
7194 case ISN_OPNR: ins = "OPNR"; break;
7195 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7196 case ISN_OPANY: ins = "OPANY"; break;
7197 default: ins = "???"; break;
7198 }
7199 smsg("%s%4d %s %s", pfx, current, ins, what);
7200 }
7201 break;
7202
7203 case ISN_COMPAREBOOL:
7204 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007205 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007206 case ISN_COMPARENR:
7207 case ISN_COMPAREFLOAT:
7208 case ISN_COMPARESTRING:
7209 case ISN_COMPAREBLOB:
7210 case ISN_COMPARELIST:
7211 case ISN_COMPAREDICT:
7212 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007213 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007214 case ISN_COMPAREANY:
7215 {
7216 char *p;
7217 char buf[10];
7218 char *type;
7219
7220 switch (iptr->isn_arg.op.op_type)
7221 {
7222 case EXPR_EQUAL: p = "=="; break;
7223 case EXPR_NEQUAL: p = "!="; break;
7224 case EXPR_GREATER: p = ">"; break;
7225 case EXPR_GEQUAL: p = ">="; break;
7226 case EXPR_SMALLER: p = "<"; break;
7227 case EXPR_SEQUAL: p = "<="; break;
7228 case EXPR_MATCH: p = "=~"; break;
7229 case EXPR_IS: p = "is"; break;
7230 case EXPR_ISNOT: p = "isnot"; break;
7231 case EXPR_NOMATCH: p = "!~"; break;
7232 default: p = "???"; break;
7233 }
7234 STRCPY(buf, p);
7235 if (iptr->isn_arg.op.op_ic == TRUE)
7236 strcat(buf, "?");
7237 switch(iptr->isn_type)
7238 {
7239 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7240 case ISN_COMPARESPECIAL:
7241 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007242 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007243 case ISN_COMPARENR: type = "COMPARENR"; break;
7244 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7245 case ISN_COMPARESTRING:
7246 type = "COMPARESTRING"; break;
7247 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7248 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7249 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7250 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007251 case ISN_COMPAREOBJECT:
7252 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007253 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7254 default: type = "???"; break;
7255 }
7256
7257 smsg("%s%4d %s %s", pfx, current, type, buf);
7258 }
7259 break;
7260
7261 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7262 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7263
7264 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007265 case ISN_CONCAT:
7266 smsg("%s%4d CONCAT size %lld", pfx, current,
7267 (varnumber_T)(iptr->isn_arg.number));
7268 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007269 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7270 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7271 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7272 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7273 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7274 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7275 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7276 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7277 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7278 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7279 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007280 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007281 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7282 iptr->isn_arg.getitem.gi_index,
7283 iptr->isn_arg.getitem.gi_with_op ?
7284 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007285 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7286 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7287 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007288 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7289 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007290 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007291 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007292 pfx, current,
7293 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007294 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007295 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007296 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007297 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007298 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7299 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7300
Bram Moolenaar4c137212021-04-19 16:48:48 +02007301 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7302
Bram Moolenaar4c137212021-04-19 16:48:48 +02007303 case ISN_CHECKTYPE:
7304 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007305 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007306 char *tofree = NULL;
7307 char *typename;
7308
7309 if (ct->ct_type->tt_type == VAR_FLOAT
7310 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7311 typename = "float|number";
7312 else
7313 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007314
7315 if (ct->ct_arg_idx == 0)
7316 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007317 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007318 (int)ct->ct_off);
7319 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007320 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007321 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007322 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007323 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007324 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007325 (int)ct->ct_arg_idx);
7326 vim_free(tofree);
7327 break;
7328 }
7329 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7330 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7331 iptr->isn_arg.checklen.cl_min_len);
7332 break;
7333 case ISN_SETTYPE:
7334 {
7335 char *tofree;
7336
7337 smsg("%s%4d SETTYPE %s", pfx, current,
7338 type_name(iptr->isn_arg.type.ct_type, &tofree));
7339 vim_free(tofree);
7340 break;
7341 }
7342 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007343 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7344 smsg("%s%4d INVERT %d (!val)", pfx, current,
7345 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007346 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007347 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7348 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007349 break;
7350 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007351 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007352 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007353 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7354 pfx, current,
7355 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007356 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007357 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7358 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007359 break;
7360 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007361 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007362 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007363 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007364 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7365 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007366 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007367 else
7368 smsg("%s%4d PUT %c %ld", pfx, current,
7369 iptr->isn_arg.put.put_regname,
7370 (long)iptr->isn_arg.put.put_lnum);
7371 break;
7372
Bram Moolenaar4c137212021-04-19 16:48:48 +02007373 case ISN_CMDMOD:
7374 {
7375 char_u *buf;
7376 size_t len = produce_cmdmods(
7377 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7378
7379 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007380 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007381 {
7382 (void)produce_cmdmods(
7383 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7384 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7385 vim_free(buf);
7386 }
7387 break;
7388 }
7389 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7390
7391 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007392 smsg("%s%4d PROFILE START line %d", pfx, current,
7393 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007394 break;
7395
7396 case ISN_PROF_END:
7397 smsg("%s%4d PROFILE END", pfx, current);
7398 break;
7399
Bram Moolenaare99d4222021-06-13 14:01:26 +02007400 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007401 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7402 iptr->isn_arg.debug.dbg_break_lnum + 1,
7403 iptr->isn_lnum,
7404 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007405 break;
7406
Bram Moolenaar4c137212021-04-19 16:48:48 +02007407 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7408 iptr->isn_arg.unpack.unp_count,
7409 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7410 break;
7411 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7412 iptr->isn_arg.shuffle.shfl_item,
7413 iptr->isn_arg.shuffle.shfl_up);
7414 break;
7415 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7416
7417 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7418 return;
7419 }
7420
7421 out_flush(); // output one line at a time
7422 ui_breakcheck();
7423 if (got_int)
7424 break;
7425 }
7426}
7427
7428/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007429 * Handle command line completion for the :disassemble command.
7430 */
7431 void
7432set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7433{
7434 char_u *p;
7435
7436 // Default: expand user functions, "debug" and "profile"
7437 xp->xp_context = EXPAND_DISASSEMBLE;
7438 xp->xp_pattern = arg;
7439
7440 // first argument already typed: only user function names
7441 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7442 {
7443 xp->xp_context = EXPAND_USER_FUNC;
7444 xp->xp_pattern = skipwhite(p);
7445 }
7446}
7447
7448/*
7449 * Function given to ExpandGeneric() to obtain the list of :disassemble
7450 * arguments.
7451 */
7452 char_u *
7453get_disassemble_argument(expand_T *xp, int idx)
7454{
7455 if (idx == 0)
7456 return (char_u *)"debug";
7457 if (idx == 1)
7458 return (char_u *)"profile";
7459 return get_user_func_name(xp, idx - 2);
7460}
7461
7462/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007463 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007464 * We don't really need this at runtime, but we do have tests that require it,
7465 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466 */
7467 void
7468ex_disassemble(exarg_T *eap)
7469{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007470 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007471 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007473 isn_T *instr = NULL; // init to shut up gcc warning
7474 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007475 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007476
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007477 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007478 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007480 if (func_needs_compiling(ufunc, compile_type)
7481 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007482 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007483 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007484 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007485 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 return;
7487 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007488 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489
7490 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007491 switch (compile_type)
7492 {
7493 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007494#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007495 instr = dfunc->df_instr_prof;
7496 instr_count = dfunc->df_instr_prof_count;
7497 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007498#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007499 // FALLTHROUGH
7500 case CT_NONE:
7501 instr = dfunc->df_instr;
7502 instr_count = dfunc->df_instr_count;
7503 break;
7504 case CT_DEBUG:
7505 instr = dfunc->df_instr_debug;
7506 instr_count = dfunc->df_instr_debug_count;
7507 break;
7508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509
Bram Moolenaar4c137212021-04-19 16:48:48 +02007510 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511}
7512
7513/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007514 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515 * list, etc. Mostly like what JavaScript does, except that empty list and
7516 * empty dictionary are FALSE.
7517 */
7518 int
7519tv2bool(typval_T *tv)
7520{
7521 switch (tv->v_type)
7522 {
7523 case VAR_NUMBER:
7524 return tv->vval.v_number != 0;
7525 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007526 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007527 case VAR_PARTIAL:
7528 return tv->vval.v_partial != NULL;
7529 case VAR_FUNC:
7530 case VAR_STRING:
7531 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7532 case VAR_LIST:
7533 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7534 case VAR_DICT:
7535 return tv->vval.v_dict != NULL
7536 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7537 case VAR_BOOL:
7538 case VAR_SPECIAL:
7539 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7540 case VAR_JOB:
7541#ifdef FEAT_JOB_CHANNEL
7542 return tv->vval.v_job != NULL;
7543#else
7544 break;
7545#endif
7546 case VAR_CHANNEL:
7547#ifdef FEAT_JOB_CHANNEL
7548 return tv->vval.v_channel != NULL;
7549#else
7550 break;
7551#endif
7552 case VAR_BLOB:
7553 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7554 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007555 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007557 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007558 case VAR_CLASS:
7559 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007560 case VAR_TYPEALIAS:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007561 break;
7562 }
7563 return FALSE;
7564}
7565
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007566 void
7567emsg_using_string_as(typval_T *tv, int as_number)
7568{
7569 semsg(_(as_number ? e_using_string_as_number_str
7570 : e_using_string_as_bool_str),
7571 tv->vval.v_string == NULL
7572 ? (char_u *)"" : tv->vval.v_string);
7573}
7574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007575/*
7576 * If "tv" is a string give an error and return FAIL.
7577 */
7578 int
7579check_not_string(typval_T *tv)
7580{
7581 if (tv->v_type == VAR_STRING)
7582 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007583 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007584 clear_tv(tv);
7585 return FAIL;
7586 }
7587 return OK;
7588}
7589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007590
7591#endif // FEAT_EVAL