blob: b3e89396e82afe338b8e9d2c8ffc1a20bb40fcea [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:
Yegappan Lakshmananf01493c2024-04-14 23:21:02 +02001656 case VAR_DICT:
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001657 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001658
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001659 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001660 if (tolerant)
1661 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001662 char_u *s, *e, *p;
1663 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001664
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001665 ga_init2(&ga, sizeof(char_u *), 1);
1666
1667 // Convert to NL separated items, then
1668 // escape the items and replace the NL with
1669 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001670 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001671 if (str == NULL)
1672 return FAIL;
1673 s = str;
1674 while ((e = vim_strchr(s, '\n')) != NULL)
1675 {
1676 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001677 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001678 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001679 if (p != NULL)
1680 {
1681 ga_concat(&ga, p);
1682 ga_concat(&ga, (char_u *)" ");
1683 vim_free(p);
1684 }
1685 s = e + 1;
1686 }
1687 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001688 clear_tv(tv);
1689 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001690 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001691 return OK;
1692 }
1693 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001694 default: to_string_error(tv->v_type);
1695 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001696 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001697 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001698 str = typval_tostring(tv, TRUE);
1699 clear_tv(tv);
1700 tv->v_type = VAR_STRING;
1701 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001702 return OK;
1703}
1704
1705/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001706 * When the value of "sv" is a null list of dict, allocate it.
1707 */
1708 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001709allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001710{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001711 typval_T *tv = sv->sv_tv;
1712
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001713 switch (tv->v_type)
1714 {
1715 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001716 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001717 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001718 break;
1719 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001720 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001721 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001722 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001723 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001724 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001725 (void)rettv_blob_alloc(tv);
1726 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001727 default:
1728 break;
1729 }
1730}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001731
Bram Moolenaare7525c52021-01-09 13:20:37 +01001732/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001733 * Return the character "str[index]" where "index" is the character index,
1734 * including composing characters.
1735 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001736 */
1737 char_u *
1738char_from_string(char_u *str, varnumber_T index)
1739{
1740 size_t nbyte = 0;
1741 varnumber_T nchar = index;
1742 size_t slen;
1743
1744 if (str == NULL)
1745 return NULL;
1746 slen = STRLEN(str);
1747
Bram Moolenaarff871402021-03-26 13:34:05 +01001748 // Do the same as for a list: a negative index counts from the end.
1749 // Optimization to check the first byte to be below 0x80 (and no composing
1750 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001751 if (index < 0)
1752 {
1753 int clen = 0;
1754
1755 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001756 {
1757 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1758 ++nbyte;
1759 else if (enc_utf8)
1760 nbyte += utfc_ptr2len(str + nbyte);
1761 else
1762 nbyte += mb_ptr2len(str + nbyte);
1763 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001764 nchar = clen + index;
1765 if (nchar < 0)
1766 // unlike list: index out of range results in empty string
1767 return NULL;
1768 }
1769
1770 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001771 {
1772 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1773 ++nbyte;
1774 else if (enc_utf8)
1775 nbyte += utfc_ptr2len(str + nbyte);
1776 else
1777 nbyte += mb_ptr2len(str + nbyte);
1778 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001779 if (nbyte >= slen)
1780 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001781 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001782}
1783
1784/*
1785 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001786 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001787 * If going over the end return "str_len".
1788 * If "idx" is negative count from the end, -1 is the last character.
1789 * When going over the start return -1.
1790 */
1791 static long
1792char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1793{
1794 varnumber_T nchar = idx;
1795 size_t nbyte = 0;
1796
1797 if (nchar >= 0)
1798 {
1799 while (nchar > 0 && nbyte < str_len)
1800 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001801 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001802 --nchar;
1803 }
1804 }
1805 else
1806 {
1807 nbyte = str_len;
1808 while (nchar < 0 && nbyte > 0)
1809 {
1810 --nbyte;
1811 nbyte -= mb_head_off(str, str + nbyte);
1812 ++nchar;
1813 }
1814 if (nchar < 0)
1815 return -1;
1816 }
1817 return (long)nbyte;
1818}
1819
1820/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001821 * Return the slice "str[first : last]" using character indexes. Composing
1822 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001823 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001824 * Return NULL when the result is empty.
1825 */
1826 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001827string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001828{
1829 long start_byte, end_byte;
1830 size_t slen;
1831
1832 if (str == NULL)
1833 return NULL;
1834 slen = STRLEN(str);
1835 start_byte = char_idx2byte(str, slen, first);
1836 if (start_byte < 0)
1837 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001838 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001839 end_byte = (long)slen;
1840 else
1841 {
1842 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001843 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001844 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001845 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001846 }
1847
1848 if (start_byte >= (long)slen || end_byte <= start_byte)
1849 return NULL;
1850 return vim_strnsave(str + start_byte, end_byte - start_byte);
1851}
1852
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001853/*
1854 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1855 * When "dfunc_idx" is negative don't give an error.
1856 * Returns NULL for an error.
1857 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001858 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001859get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001860{
1861 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001862 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1863 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001864 svar_T *sv;
1865
1866 if (sref->sref_seq != si->sn_script_seq)
1867 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001868 // The script was reloaded after the function was compiled, the
1869 // script_idx may not be valid.
1870 if (dfunc != NULL)
1871 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1872 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001873 return NULL;
1874 }
1875 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001876 if (sv->sv_name == NULL)
1877 {
1878 if (dfunc != NULL)
1879 emsg(_(e_script_variable_was_deleted));
1880 return NULL;
1881 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001882 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001883 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001884 if (dfunc != NULL)
1885 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001886 return NULL;
1887 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001888
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001889 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1890 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001891 {
1892 if (dfunc != NULL)
1893 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1894 return NULL;
1895 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001896 return sv;
1897}
1898
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001899/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001900 * Function passed to do_cmdline() for splitting a script joined by NL
1901 * characters.
1902 */
1903 static char_u *
1904get_split_sourceline(
1905 int c UNUSED,
1906 void *cookie,
1907 int indent UNUSED,
1908 getline_opt_T options UNUSED)
1909{
1910 source_cookie_T *sp = (source_cookie_T *)cookie;
1911 char_u *p;
1912 char_u *line;
1913
Bram Moolenaar20677332021-06-06 17:02:53 +02001914 p = vim_strchr(sp->nextline, '\n');
1915 if (p == NULL)
1916 {
1917 line = vim_strsave(sp->nextline);
1918 sp->nextline += STRLEN(sp->nextline);
1919 }
1920 else
1921 {
1922 line = vim_strnsave(sp->nextline, p - sp->nextline);
1923 sp->nextline = p + 1;
1924 }
1925 return line;
1926}
1927
1928/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 * Execute a function by "name".
1930 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001931 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 */
1933 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001934call_eval_func(
1935 char_u *name,
1936 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001937 ectx_T *ectx,
1938 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939{
Bram Moolenaared677f52020-08-12 16:38:10 +02001940 int called_emsg_before = called_emsg;
1941 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001943 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001944 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001946 dictitem_T *v;
1947
1948 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001949 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1950 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001951 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001952 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001953 return FAIL;
1954 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001955 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001957 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958}
1959
1960/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001961 * When a function reference is used, fill a partial with the information
1962 * needed, especially when it is used as a closure.
1963 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001964 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001965fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001966 partial_T *pt,
1967 ufunc_T *ufunc,
1968 loopvarinfo_T *lvi,
1969 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001970{
1971 pt->pt_func = ufunc;
1972 pt->pt_refcount = 1;
1973
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001974 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001975 {
1976 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1977 + ectx->ec_dfunc_idx;
1978
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001979 // The closure may need to find arguments and local variables of the
1980 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001981 pt->pt_outer.out_stack = &ectx->ec_stack;
1982 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001983 if (ectx->ec_outer_ref != NULL)
1984 {
1985 // The current context already has a context, link to that one.
1986 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1987 if (ectx->ec_outer_ref->or_partial != NULL)
1988 {
1989 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1990 ++pt->pt_outer.out_up_partial->pt_refcount;
1991 }
1992 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001993
Bram Moolenaarcc341812022-09-19 15:54:34 +01001994 if (lvi != NULL)
1995 {
1996 int depth;
1997
1998 // The closure may need to find variables defined inside a loop,
1999 // for every nested loop. A new reference is made every time,
2000 // ISN_ENDLOOP will check if they are actually used.
2001 for (depth = 0; depth < lvi->lvi_depth; ++depth)
2002 {
2003 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
2004 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
2005 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
2006 pt->pt_outer.out_loop[depth].var_count =
2007 lvi->lvi_loop[depth].var_count;
2008 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002009 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002010 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002011 else
2012 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002013
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002014 // If the function currently executing returns and the closure is still
2015 // being referenced, we need to make a copy of the context (arguments
2016 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002017 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02002018 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01002019 {
2020 vim_free(pt);
2021 return FAIL;
2022 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002023 // Extra variable keeps the count of closures created in the current
2024 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01002025 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
2026 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
2027
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002028 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
2029 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002030 ++pt->pt_refcount;
2031 ++ectx->ec_funcrefs.ga_len;
2032 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002033 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002034 return OK;
2035}
2036
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002037/*
2038 * Execute iptr->isn_arg.string as an Ex command.
2039 */
2040 static int
Ernie Raelee865f32023-09-29 19:53:55 +02002041exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002042{
2043 source_cookie_T cookie;
2044
2045 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002046 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002047 CLEAR_FIELD(cookie);
2048 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02002049 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002050 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
2051 || did_emsg)
2052 return FAIL;
2053 return OK;
2054}
2055
Bram Moolenaar89445512022-04-14 12:58:23 +01002056/*
2057 * If script "sid" is not loaded yet then load it now.
2058 * Caller must make sure "sid" is a valid script ID.
2059 * "loaded" is set to TRUE if the script had to be loaded.
2060 * Returns FAIL if loading fails, OK if already loaded or loaded now.
2061 */
2062 int
2063may_load_script(int sid, int *loaded)
2064{
2065 scriptitem_T *si = SCRIPT_ITEM(sid);
2066
2067 if (si->sn_state == SN_STATE_NOT_LOADED)
2068 {
2069 *loaded = TRUE;
2070 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
2071 {
2072 semsg(_(e_cant_open_file_str), si->sn_name);
2073 return FAIL;
2074 }
2075 }
2076 return OK;
2077}
2078
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002079// used for v_instr of typval of VAR_INSTR
2080struct instr_S {
2081 ectx_T *instr_ectx;
2082 isn_T *instr_instr;
2083};
2084
Bram Moolenaar4c137212021-04-19 16:48:48 +02002085// used for substitute_instr
2086typedef struct subs_expr_S {
2087 ectx_T *subs_ectx;
2088 isn_T *subs_instr;
2089 int subs_status;
2090} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002092// Set when calling do_debug().
2093static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002094static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002095
2096/*
2097 * When debugging lookup "name" and return the typeval.
2098 * When not found return NULL.
2099 */
2100 typval_T *
2101lookup_debug_var(char_u *name)
2102{
2103 int idx;
2104 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002105 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002106 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002107 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002108
2109 if (ectx == NULL)
2110 return NULL;
2111 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2112
2113 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002114 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002115 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002116 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2117
2118 // the variable name may be NULL when not available in this block
2119 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002120 return STACK_TV_VAR(idx);
2121 }
2122
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002123 // Go through argument names.
2124 ufunc = dfunc->df_ufunc;
2125 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2126 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2127 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2128 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2129 - varargs_off + idx);
2130 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2131 return STACK_TV(ectx->ec_frame_idx - 1);
2132
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002133 return NULL;
2134}
2135
Bram Moolenaar26a44842021-09-02 18:49:06 +02002136/*
2137 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2138 * breakpoint was set in that function or when there is any expression.
2139 */
2140 int
2141may_break_in_function(ufunc_T *ufunc)
2142{
2143 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2144}
2145
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002146 static void
2147handle_debug(isn_T *iptr, ectx_T *ectx)
2148{
2149 char_u *line;
2150 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2151 + ectx->ec_dfunc_idx)->df_ufunc;
2152 isn_T *ni;
2153 int end_lnum = iptr->isn_lnum;
2154 garray_T ga;
2155 int lnum;
2156
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002157 if (ex_nesting_level > debug_break_level)
2158 {
2159 linenr_T breakpoint;
2160
Bram Moolenaar26a44842021-09-02 18:49:06 +02002161 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002162 return;
2163
2164 // check for the next breakpoint if needed
2165 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002166 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002167 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2168 return;
2169 }
2170
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002171 SOURCING_LNUM = iptr->isn_lnum;
2172 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002173 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002174
2175 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2176 if (ni->isn_type == ISN_DEBUG
2177 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002178 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002179 || ni->isn_type == ISN_RETURN_VOID)
2180 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002181 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002182 break;
2183 }
2184
2185 if (end_lnum > iptr->isn_lnum)
2186 {
2187 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002188 for (lnum = iptr->isn_lnum; lnum < end_lnum
2189 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002190 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002191 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002192
Bram Moolenaar303215d2021-07-07 20:10:43 +02002193 if (p == NULL)
2194 continue; // left over from continuation line
2195 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002196 if (*p == '#')
2197 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002198 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002199 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2200 if (STRNCMP(p, "def ", 4) == 0)
2201 break;
2202 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002203 line = ga_concat_strings(&ga, " ");
2204 vim_free(ga.ga_data);
2205 }
2206 else
2207 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002208
Dominique Pelle6e969552021-06-17 13:53:41 +02002209 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002210 debug_context = NULL;
2211
2212 if (end_lnum > iptr->isn_lnum)
2213 vim_free(line);
2214}
2215
Bram Moolenaar4c137212021-04-19 16:48:48 +02002216/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002217 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002218 * Returns OK, FAIL or NOTDONE (uncatchable error).
2219 */
2220 static int
2221execute_storeindex(isn_T *iptr, ectx_T *ectx)
2222{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002223 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002224 typval_T *tv;
2225 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002226 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002227 typval_T *tv_dest = STACK_TV_BOT(-1);
2228 int status = OK;
2229
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002230 if (tv_idx->v_type == VAR_NUMBER)
2231 lidx = (long)tv_idx->vval.v_number;
2232
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002233 // Stack contains:
2234 // -3 value to be stored
2235 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002236 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002237 tv = STACK_TV_BOT(-3);
2238 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002239
2240 // Make sure an object has been initialized
2241 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2242 {
2243 emsg(_(e_using_null_object));
2244 status = FAIL;
2245 }
2246 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002247 {
2248 dest_type = tv_dest->v_type;
2249 if (dest_type == VAR_DICT)
2250 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002251 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2252 {
2253 // Need to get the member index now that the class is known.
2254 object_T *obj = tv_dest->vval.v_object;
2255 class_T *cl = obj->obj_class;
2256 char_u *member = tv_idx->vval.v_string;
2257
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002258 int m_idx;
2259 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2260 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002261 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002262 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002263 {
Ernie Rael03042a22023-11-11 08:53:32 +01002264 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +02002265 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002266 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002267 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002268
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002269 lidx = m_idx;
2270 }
2271 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002272 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002273 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002274 status = FAIL;
2275 }
2276 }
2277 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2278 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002279 {
2280 emsg(_(e_number_expected));
2281 status = FAIL;
2282 }
2283 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002284
2285 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002286 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002287 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002288 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002289 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002290
Bram Moolenaare08be092022-02-17 13:08:26 +00002291 if (list == NULL)
2292 {
2293 emsg(_(e_list_not_set));
2294 return FAIL;
2295 }
2296 if (lidx < 0 && list->lv_len + lidx >= 0)
2297 // negative index is relative to the end
2298 lidx = list->lv_len + lidx;
2299 if (lidx < 0 || lidx > list->lv_len)
2300 {
2301 semsg(_(e_list_index_out_of_range_nr), lidx);
2302 return FAIL;
2303 }
2304 if (lidx < list->lv_len)
2305 {
2306 listitem_T *li = list_find(list, lidx);
2307
2308 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002309 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002310 return FAIL;
2311 // overwrite existing list item
2312 clear_tv(&li->li_tv);
2313 li->li_tv = *tv;
2314 }
2315 else
2316 {
2317 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2318 return FAIL;
2319 // append to list, only fails when out of memory
2320 if (list_append_tv(list, tv) == FAIL)
2321 return NOTDONE;
2322 clear_tv(tv);
2323 }
2324 }
2325 else if (dest_type == VAR_DICT)
2326 {
2327 char_u *key = tv_idx->vval.v_string;
2328 dict_T *dict = tv_dest->vval.v_dict;
2329 dictitem_T *di;
2330
2331 SOURCING_LNUM = iptr->isn_lnum;
2332 if (dict == NULL)
2333 {
2334 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002335 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002336 }
2337 if (key == NULL)
2338 key = (char_u *)"";
2339 di = dict_find(dict, key, -1);
2340 if (di != NULL)
2341 {
2342 if (error_if_locked(di->di_tv.v_lock,
2343 e_cannot_change_dict_item))
2344 return FAIL;
2345 // overwrite existing value
2346 clear_tv(&di->di_tv);
2347 di->di_tv = *tv;
2348 }
2349 else
2350 {
2351 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2352 return FAIL;
2353 // add to dict, only fails when out of memory
2354 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2355 return NOTDONE;
2356 clear_tv(tv);
2357 }
2358 }
2359 else if (dest_type == VAR_BLOB)
2360 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002361 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002362 varnumber_T nr;
2363 int error = FALSE;
2364 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002365
2366 if (blob == NULL)
2367 {
2368 emsg(_(e_blob_not_set));
2369 return FAIL;
2370 }
2371 len = blob_len(blob);
2372 if (lidx < 0 && len + lidx >= 0)
2373 // negative index is relative to the end
2374 lidx = len + lidx;
2375
2376 // Can add one byte at the end.
2377 if (lidx < 0 || lidx > len)
2378 {
2379 semsg(_(e_blob_index_out_of_range_nr), lidx);
2380 return FAIL;
2381 }
2382 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2383 return FAIL;
2384 nr = tv_get_number_chk(tv, &error);
2385 if (error)
2386 return FAIL;
2387 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002388 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002389 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2390 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002391 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002392
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002393 if (dest_type == VAR_OBJECT)
2394 {
2395 object_T *obj = tv_dest->vval.v_object;
2396
2397 otv = (typval_T *)(obj + 1);
2398 class_T *itf = iptr->isn_arg.storeindex.si_class;
2399 if (itf != NULL)
2400 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002401 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002402 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002403 }
2404 else
2405 {
2406 // VAR_CLASS
2407 class_T *class = tv_dest->vval.v_class;
2408 otv = class->class_members_tv;
2409 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002410
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002411 clear_tv(&otv[lidx]);
2412 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002413 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002414 else
2415 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002416 status = FAIL;
2417 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002418 }
2419 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002420
2421 clear_tv(tv_idx);
2422 clear_tv(tv_dest);
2423 ectx->ec_stack.ga_len -= 3;
2424 if (status == FAIL)
2425 {
2426 clear_tv(tv);
2427 return FAIL;
2428 }
2429 return OK;
2430}
2431
2432/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002433 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002434 */
2435 static int
2436execute_storerange(isn_T *iptr, ectx_T *ectx)
2437{
2438 typval_T *tv;
2439 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2440 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2441 typval_T *tv_dest = STACK_TV_BOT(-1);
2442 int status = OK;
2443
2444 // Stack contains:
2445 // -4 value to be stored
2446 // -3 first index or "none"
2447 // -2 second index or "none"
2448 // -1 destination list or blob
2449 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002450 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002451 if (tv_dest->v_type == VAR_LIST)
2452 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002453 long n1;
2454 long n2;
2455 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002456
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002457 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2458 if (tv_idx2->v_type == VAR_SPECIAL
2459 && tv_idx2->vval.v_number == VVAL_NONE)
2460 n2 = list_len(tv_dest->vval.v_list) - 1;
2461 else
2462 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2463
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002464 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002465 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002466 status = FAIL;
2467 else
2468 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002469 status = check_range_index_two(tv_dest->vval.v_list,
2470 &n1, li1, &n2, FALSE);
2471 if (status != FAIL)
2472 status = list_assign_range(
2473 tv_dest->vval.v_list,
2474 tv->vval.v_list,
2475 n1,
2476 n2,
2477 tv_idx2->v_type == VAR_SPECIAL,
2478 (char_u *)"=",
2479 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002480 }
2481 }
2482 else if (tv_dest->v_type == VAR_BLOB)
2483 {
2484 varnumber_T n1;
2485 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002486 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002487
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002488 n1 = tv_get_number_chk(tv_idx1, NULL);
2489 if (tv_idx2->v_type == VAR_SPECIAL
2490 && tv_idx2->vval.v_number == VVAL_NONE)
2491 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2492 else
2493 n2 = tv_get_number_chk(tv_idx2, NULL);
2494 bloblen = blob_len(tv_dest->vval.v_blob);
2495
2496 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2497 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002498 status = FAIL;
2499 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002500 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002501 }
2502 else
2503 {
2504 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002505 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002506 }
2507
2508 clear_tv(tv_idx1);
2509 clear_tv(tv_idx2);
2510 clear_tv(tv_dest);
2511 ectx->ec_stack.ga_len -= 4;
2512 clear_tv(tv);
2513
2514 return status;
2515}
2516
2517/*
2518 * Unlet item in list or dict variable.
2519 */
2520 static int
2521execute_unletindex(isn_T *iptr, ectx_T *ectx)
2522{
2523 typval_T *tv_idx = STACK_TV_BOT(-2);
2524 typval_T *tv_dest = STACK_TV_BOT(-1);
2525 int status = OK;
2526
2527 // Stack contains:
2528 // -2 index
2529 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002530 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002531 if (tv_dest->v_type == VAR_DICT)
2532 {
2533 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002534 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002535 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002536 semsg(_(e_expected_str_but_got_str),
2537 vartype_name(VAR_STRING),
2538 vartype_name(tv_idx->v_type));
2539 status = FAIL;
2540 }
2541 else
2542 {
2543 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002544 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002545 dictitem_T *di = NULL;
2546
2547 if (d != NULL && value_check_lock(
2548 d->dv_lock, NULL, FALSE))
2549 status = FAIL;
2550 else
2551 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002552 if (tv_idx->v_type == VAR_STRING)
2553 {
2554 key = tv_idx->vval.v_string;
2555 if (key == NULL)
2556 key = (char_u *)"";
2557 }
2558 else
2559 {
2560 key = tv_get_string(tv_idx);
2561 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002562 if (d != NULL)
2563 di = dict_find(d, key, (int)STRLEN(key));
2564 if (di == NULL)
2565 {
2566 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002567 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002568 status = FAIL;
2569 }
2570 else if (var_check_fixed(di->di_flags,
2571 NULL, FALSE)
2572 || var_check_ro(di->di_flags,
2573 NULL, FALSE))
2574 status = FAIL;
2575 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002576 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002577 }
2578 }
2579 }
2580 else if (tv_dest->v_type == VAR_LIST)
2581 {
2582 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002583 if (check_for_number(tv_idx) == FAIL)
2584 {
2585 status = FAIL;
2586 }
2587 else
2588 {
2589 list_T *l = tv_dest->vval.v_list;
2590 long n = (long)tv_idx->vval.v_number;
2591
2592 if (l != NULL && value_check_lock(
2593 l->lv_lock, NULL, FALSE))
2594 status = FAIL;
2595 else
2596 {
2597 listitem_T *li = list_find(l, n);
2598
2599 if (li == NULL)
2600 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002601 semsg(_(e_list_index_out_of_range_nr), n);
2602 status = FAIL;
2603 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002604 else
2605 listitem_remove(l, li);
2606 }
2607 }
2608 }
2609 else
2610 {
2611 status = FAIL;
2612 semsg(_(e_cannot_index_str),
2613 vartype_name(tv_dest->v_type));
2614 }
2615
2616 clear_tv(tv_idx);
2617 clear_tv(tv_dest);
2618 ectx->ec_stack.ga_len -= 2;
2619
2620 return status;
2621}
2622
2623/*
2624 * Unlet a range of items in a list variable.
2625 */
2626 static int
2627execute_unletrange(isn_T *iptr, ectx_T *ectx)
2628{
2629 // Stack contains:
2630 // -3 index1
2631 // -2 index2
2632 // -1 dict or list
2633 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2634 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2635 typval_T *tv_dest = STACK_TV_BOT(-1);
2636 int status = OK;
2637
2638 if (tv_dest->v_type == VAR_LIST)
2639 {
2640 // indexes must be a number
2641 SOURCING_LNUM = iptr->isn_lnum;
2642 if (check_for_number(tv_idx1) == FAIL
2643 || (tv_idx2->v_type != VAR_SPECIAL
2644 && check_for_number(tv_idx2) == FAIL))
2645 {
2646 status = FAIL;
2647 }
2648 else
2649 {
2650 list_T *l = tv_dest->vval.v_list;
2651 long n1 = (long)tv_idx1->vval.v_number;
2652 long n2 = tv_idx2->v_type == VAR_SPECIAL
2653 ? 0 : (long)tv_idx2->vval.v_number;
2654 listitem_T *li;
2655
2656 li = list_find_index(l, &n1);
2657 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002658 {
2659 semsg(_(e_list_index_out_of_range_nr),
2660 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002661 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002662 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002663 else
2664 {
2665 if (n1 < 0)
2666 n1 = list_idx_of_item(l, li);
2667 if (n2 < 0)
2668 {
2669 listitem_T *li2 = list_find(l, n2);
2670
2671 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002672 {
2673 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002674 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002675 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002676 else
2677 n2 = list_idx_of_item(l, li2);
2678 }
2679 if (status != FAIL
2680 && tv_idx2->v_type != VAR_SPECIAL
2681 && n2 < n1)
2682 {
2683 semsg(_(e_list_index_out_of_range_nr), n2);
2684 status = FAIL;
2685 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002686 if (status != FAIL)
2687 list_unlet_range(l, li, n1,
2688 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002689 }
2690 }
2691 }
2692 else
2693 {
2694 status = FAIL;
2695 SOURCING_LNUM = iptr->isn_lnum;
2696 semsg(_(e_cannot_index_str),
2697 vartype_name(tv_dest->v_type));
2698 }
2699
2700 clear_tv(tv_idx1);
2701 clear_tv(tv_idx2);
2702 clear_tv(tv_dest);
2703 ectx->ec_stack.ga_len -= 3;
2704
2705 return status;
2706}
2707
2708/*
2709 * Top of a for loop.
2710 */
2711 static int
2712execute_for(isn_T *iptr, ectx_T *ectx)
2713{
2714 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002715 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002716 typval_T *ltv = STACK_TV_BOT(-1);
2717 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002718 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002719
2720 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2721 return FAIL;
2722 if (ltv->v_type == VAR_LIST)
2723 {
2724 list_T *list = ltv->vval.v_list;
2725
2726 // push the next item from the list
2727 ++idxtv->vval.v_number;
2728 if (list == NULL
2729 || idxtv->vval.v_number >= list->lv_len)
2730 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002731 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002732 }
2733 else if (list->lv_first == &range_list_item)
2734 {
2735 // non-materialized range() list
2736 tv = STACK_TV_BOT(0);
2737 tv->v_type = VAR_NUMBER;
2738 tv->v_lock = 0;
2739 tv->vval.v_number = list_find_nr(
2740 list, idxtv->vval.v_number, NULL);
2741 ++ectx->ec_stack.ga_len;
2742 }
2743 else
2744 {
2745 listitem_T *li = list_find(list,
2746 idxtv->vval.v_number);
2747
2748 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2749 ++ectx->ec_stack.ga_len;
2750 }
2751 }
2752 else if (ltv->v_type == VAR_STRING)
2753 {
2754 char_u *str = ltv->vval.v_string;
2755
2756 // The index is for the last byte of the previous
2757 // character.
2758 ++idxtv->vval.v_number;
2759 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2760 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002761 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002762 }
2763 else
2764 {
2765 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2766
2767 // Push the next character from the string.
2768 tv = STACK_TV_BOT(0);
2769 tv->v_type = VAR_STRING;
2770 tv->vval.v_string = vim_strnsave(
2771 str + idxtv->vval.v_number, clen);
2772 ++ectx->ec_stack.ga_len;
2773 idxtv->vval.v_number += clen - 1;
2774 }
2775 }
2776 else if (ltv->v_type == VAR_BLOB)
2777 {
2778 blob_T *blob = ltv->vval.v_blob;
2779
2780 // When we get here the first time make a copy of the
2781 // blob, so that the iteration still works when it is
2782 // changed.
2783 if (idxtv->vval.v_number == -1 && blob != NULL)
2784 {
2785 blob_copy(blob, ltv);
2786 blob_unref(blob);
2787 blob = ltv->vval.v_blob;
2788 }
2789
2790 // The index is for the previous byte.
2791 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002792 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002793 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002794 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002795 }
2796 else
2797 {
2798 // Push the next byte from the blob.
2799 tv = STACK_TV_BOT(0);
2800 tv->v_type = VAR_NUMBER;
2801 tv->vval.v_number = blob_get(blob,
2802 idxtv->vval.v_number);
2803 ++ectx->ec_stack.ga_len;
2804 }
2805 }
2806 else
2807 {
2808 semsg(_(e_for_loop_on_str_not_supported),
2809 vartype_name(ltv->v_type));
2810 return FAIL;
2811 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002812
2813 if (jump)
2814 {
2815 // past the end of the list/string/blob, jump to "endfor"
2816 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2817 may_restore_cmdmod(&ectx->ec_funclocal);
2818 }
2819 else
2820 {
2821 // Store the current number of funcrefs, this may be used in
2822 // ISN_LOOPEND. The variable index is always one more than the loop
2823 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002824 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002825 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2826 }
2827
2828 return OK;
2829}
2830
2831/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002832 * Code for handling variables declared inside a loop and used in a closure.
2833 * This is very similar to what is done with funcstack_T. The difference is
2834 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2835 * scope of the block inside a loop and each loop may have its own.
2836 */
2837
2838// Double linked list of loopvars_T in use.
2839static loopvars_T *first_loopvars = NULL;
2840
2841 static void
2842add_loopvars_to_list(loopvars_T *loopvars)
2843{
2844 // Link in list of loopvarss.
2845 if (first_loopvars != NULL)
2846 first_loopvars->lvs_prev = loopvars;
2847 loopvars->lvs_next = first_loopvars;
2848 loopvars->lvs_prev = NULL;
2849 first_loopvars = loopvars;
2850}
2851
2852 static void
2853remove_loopvars_from_list(loopvars_T *loopvars)
2854{
2855 if (loopvars->lvs_prev == NULL)
2856 first_loopvars = loopvars->lvs_next;
2857 else
2858 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2859 if (loopvars->lvs_next != NULL)
2860 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2861}
2862
2863/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002864 * End of a for or while loop: Handle any variables used by a closure.
2865 */
2866 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002867execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002868{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002869 endloop_T *endloop = &iptr->isn_arg.endloop;
2870 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2871 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002872 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002873 garray_T *gap = &ectx->ec_funcrefs;
2874 int closure_in_use = FALSE;
2875 loopvars_T *loopvars;
2876 typval_T *stack;
2877 int idx;
2878
Bram Moolenaarcc341812022-09-19 15:54:34 +01002879 // Check if any created closure is still being referenced and loopvars have
2880 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002881 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2882 {
2883 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2884
Bram Moolenaarcc341812022-09-19 15:54:34 +01002885 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002886 {
2887 int refcount = pt->pt_refcount;
2888 int i;
2889
2890 // A Reference in a variable inside the loop doesn't count, it gets
2891 // unreferenced at the end of the loop.
2892 for (i = 0; i < endloop->end_var_count; ++i)
2893 {
2894 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2895
2896 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2897 --refcount;
2898 }
2899 if (refcount > 1)
2900 {
2901 closure_in_use = TRUE;
2902 break;
2903 }
2904 }
2905 }
2906
2907 // If no function reference were created since the start of the loop block
2908 // or it is no longer referenced there is nothing to do.
2909 if (!closure_in_use)
2910 return OK;
2911
2912 // A closure is using variables declared inside the loop.
2913 // Move them to the called function.
2914 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2915 if (loopvars == NULL)
2916 return FAIL;
2917
2918 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2919 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2920 loopvars->lvs_ga.ga_data = stack;
2921 if (stack == NULL)
2922 {
2923 vim_free(loopvars);
2924 return FAIL;
2925 }
2926 add_loopvars_to_list(loopvars);
2927
2928 // Move the variable values.
2929 for (idx = 0; idx < endloop->end_var_count; ++idx)
2930 {
2931 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2932
2933 *(stack + idx) = *tv;
2934 tv->v_type = VAR_UNKNOWN;
2935 }
2936
2937 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2938 {
2939 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2940
Bram Moolenaarcc341812022-09-19 15:54:34 +01002941 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002942 {
2943 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002944 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002945
Bram Moolenaarcc341812022-09-19 15:54:34 +01002946 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2947 pt->pt_outer.out_loop[depth].var_idx -=
2948 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002949 }
2950 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002951
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002952 return OK;
2953}
2954
2955/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002956 * Called when a partial is freed or its reference count goes down to one. The
2957 * loopvars may be the only reference to the partials in the local variables.
2958 * Go over all of them, the funcref and can be freed if all partials
2959 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002960 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002961 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002962 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002963loopvars_check_refcount(loopvars_T *loopvars)
2964{
2965 int i;
2966 garray_T *gap = &loopvars->lvs_ga;
2967 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002968 typval_T *stack = gap->ga_data;
2969
Bram Moolenaarcc341812022-09-19 15:54:34 +01002970 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2971 return FALSE;
2972 for (i = 0; i < gap->ga_len; ++i)
2973 {
2974 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2975
2976 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2977 && tv->vval.v_partial->pt_refcount == 1)
2978 {
2979 int depth;
2980
2981 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2982 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2983 ++done;
2984 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002985 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002986 if (done != loopvars->lvs_min_refcount)
2987 return FALSE;
2988
2989 // All partials referencing the loopvars have a reference count of
2990 // one, thus the loopvars is no longer of use.
2991 stack = gap->ga_data;
2992 for (i = 0; i < gap->ga_len; ++i)
2993 clear_tv(stack + i);
2994 vim_free(stack);
2995 remove_loopvars_from_list(loopvars);
2996 vim_free(loopvars);
2997 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002998}
2999
3000/*
3001 * For garbage collecting: set references in all variables referenced by
3002 * all loopvars.
3003 */
3004 int
3005set_ref_in_loopvars(int copyID)
3006{
3007 loopvars_T *loopvars;
3008
3009 for (loopvars = first_loopvars; loopvars != NULL;
3010 loopvars = loopvars->lvs_next)
3011 {
3012 typval_T *stack = loopvars->lvs_ga.ga_data;
3013 int i;
3014
3015 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
3016 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
3017 return TRUE; // abort
3018 }
3019 return FALSE;
3020}
3021
3022/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00003023 * Load instruction for w:/b:/g:/t: variable.
3024 * "isn_type" is used instead of "iptr->isn_type".
3025 */
3026 static int
3027load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
3028{
3029 dictitem_T *di = NULL;
3030 hashtab_T *ht = NULL;
3031 char namespace;
3032
3033 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3034 return NOTDONE;
3035 switch (isn_type)
3036 {
3037 case ISN_LOADG:
3038 ht = get_globvar_ht();
3039 namespace = 'g';
3040 break;
3041 case ISN_LOADB:
3042 ht = &curbuf->b_vars->dv_hashtab;
3043 namespace = 'b';
3044 break;
3045 case ISN_LOADW:
3046 ht = &curwin->w_vars->dv_hashtab;
3047 namespace = 'w';
3048 break;
3049 case ISN_LOADT:
3050 ht = &curtab->tp_vars->dv_hashtab;
3051 namespace = 't';
3052 break;
3053 default: // Cannot reach here
3054 return NOTDONE;
3055 }
3056 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3057
Bram Moolenaar06b77222022-01-25 15:51:56 +00003058 if (di == NULL)
3059 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003060 if (isn_type == ISN_LOADG)
3061 {
3062 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3063
3064 // g:Something could be a function
3065 if (ufunc != NULL)
3066 {
3067 typval_T *tv = STACK_TV_BOT(0);
3068
3069 ++ectx->ec_stack.ga_len;
3070 tv->v_type = VAR_FUNC;
3071 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3072 if (tv->vval.v_string == NULL)
3073 return FAIL;
3074 STRCPY(tv->vval.v_string, "g:");
3075 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3076 return OK;
3077 }
3078 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003079 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003080 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003081 // no check if the item exists in the script but
3082 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003083 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003084 else
3085 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003086 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003087 return FAIL;
3088 }
3089 else
3090 {
3091 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3092 ++ectx->ec_stack.ga_len;
3093 }
3094 return OK;
3095}
3096
Bram Moolenaard0200c82023-01-28 15:19:40 +00003097
3098 static void
3099object_required_error(typval_T *tv)
3100{
3101 garray_T type_list;
3102 ga_init2(&type_list, sizeof(type_T *), 10);
3103 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3104 char *tofree = NULL;
3105 char *typename = type_name(type, &tofree);
3106 semsg(_(e_object_required_found_str), typename);
3107 vim_free(tofree);
3108 clear_type_list(&type_list);
3109}
3110
Bram Moolenaar06b77222022-01-25 15:51:56 +00003111/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003112 * Execute instructions in execution context "ectx".
3113 * Return OK or FAIL;
3114 */
3115 static int
3116exec_instructions(ectx_T *ectx)
3117{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003118 int ret = FAIL;
3119 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003120 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003121
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003122 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003123 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003124
Bram Moolenaarff652882021-05-16 15:24:49 +02003125 // Only catch exceptions in this instruction list.
3126 ectx->ec_trylevel_at_start = trylevel;
3127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 for (;;)
3129 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003130 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003132 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003133
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003134 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003135 {
3136 line_breakcheck();
3137 breakcheck_count = 0;
3138 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003139 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003140 {
3141 // Turn CTRL-C into an exception.
3142 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003143 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003144 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003145 did_throw = TRUE;
3146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003148 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003149 {
3150 // Turn an error message into an exception.
3151 did_emsg = FALSE;
3152 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003153 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003154 did_throw = TRUE;
3155 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003156
3157 // This exception was not caught (yet).
3158 garray_T *trystack = &ectx->ec_trystack;
3159 if (trystack->ga_len > 0)
3160 {
3161 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3162 + trystack->ga_len - 1;
3163 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3164 trycmd->tcd_caught = FALSE;
3165 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003166 }
3167
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003168 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003170 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003171 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003172 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173
3174 // An exception jumps to the first catch, finally, or returns from
3175 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003176 while (index > 0)
3177 {
3178 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003179 // 1. after :try and before :catch - jump to first :catch
3180 // 2. in :catch block - jump to :finally
3181 // 3. in :catch block and no finally - jump to :endtry
3182 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3183 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003184 break;
3185 // In the catch and finally block of this try we have to go up
3186 // one level.
3187 --index;
3188 trycmd = NULL;
3189 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003190 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003192 if (trycmd->tcd_in_catch)
3193 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003194 if (trycmd->tcd_finally_idx > 0)
3195 {
3196 // exception inside ":catch", jump to ":finally" once
3197 ectx->ec_iidx = trycmd->tcd_finally_idx;
3198 trycmd->tcd_finally_idx = 0;
3199 }
3200 else
3201 {
3202 // exception inside ":catch" or ":finally", jump to
3203 // ":endtry"
3204 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3205 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003206 }
3207 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003208 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003209 // jump to first ":catch"
3210 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003211 trycmd->tcd_in_catch = TRUE;
3212 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003213 did_throw = FALSE; // don't come back here until :endtry
3214 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 }
3216 else
3217 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003218 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003219 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003220 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003221 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003222 tv = STACK_TV_BOT(0);
3223 tv->v_type = VAR_NUMBER;
3224 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003225 ++ectx->ec_stack.ga_len;
3226 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003228 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003229 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003230 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003231 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 goto done;
3233 }
3234
Bram Moolenaar4c137212021-04-19 16:48:48 +02003235 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003236 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 }
3238 continue;
3239 }
3240
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003241 /*
3242 * Big switch on the instruction. Most compilers will be turning this
3243 * into an efficient lookup table, since the "case" values are an enum
3244 * with sequential numbers. It may look ugly, but it should be the
3245 * most efficient way.
3246 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003247 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 switch (iptr->isn_type)
3249 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003250 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003251 case ISN_CONSTRUCT:
3252 // "this" is always the local variable at index zero
3253 tv = STACK_TV_VAR(0);
3254 tv->v_type = VAR_OBJECT;
3255 tv->vval.v_object = alloc_clear(
3256 iptr->isn_arg.construct.construct_size);
3257 tv->vval.v_object->obj_class =
3258 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003259 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003260 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003261 object_created(tv->vval.v_object);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01003262
3263 // When creating an enum value object, initialize the name and
3264 // ordinal object variables.
3265 class_T *en = tv->vval.v_object->obj_class;
3266 if (IS_ENUM(en))
3267 enum_set_internal_obj_vars(en, tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003268 break;
3269
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 // execute Ex command line
3271 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003272 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003273 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 break;
3275
Bram Moolenaar20677332021-06-06 17:02:53 +02003276 // execute Ex command line split at NL characters.
3277 case ISN_EXEC_SPLIT:
3278 {
3279 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003280 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003281
3282 SOURCING_LNUM = iptr->isn_lnum;
3283 CLEAR_FIELD(cookie);
3284 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3285 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003286 line = get_split_sourceline(0, &cookie, 0, 0);
3287 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003288 get_split_sourceline, &cookie,
3289 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3290 == FAIL
3291 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003292 {
3293 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003294 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003295 }
3296 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003297 }
3298 break;
3299
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003300 // execute Ex command line that is only a range
3301 case ISN_EXECRANGE:
3302 {
3303 exarg_T ea;
3304 char *error = NULL;
3305
3306 CLEAR_FIELD(ea);
3307 ea.cmdidx = CMD_SIZE;
3308 ea.addr_type = ADDR_LINES;
3309 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003310 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003311 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003312 if (ea.cmd == NULL)
3313 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003314 // error is always NULL when using ADDR_LINES
3315 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003316 if (error != NULL)
3317 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003318 emsg(error);
3319 goto on_error;
3320 }
3321 }
3322 break;
3323
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003324 // Evaluate an expression with legacy syntax, push it onto the
3325 // stack.
3326 case ISN_LEGACY_EVAL:
3327 {
3328 char_u *arg = iptr->isn_arg.string;
3329 int res;
3330 int save_flags = cmdmod.cmod_flags;
3331
Bram Moolenaar35578162021-08-02 19:10:38 +02003332 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003333 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003334 tv = STACK_TV_BOT(0);
3335 init_tv(tv);
3336 cmdmod.cmod_flags |= CMOD_LEGACY;
3337 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3338 cmdmod.cmod_flags = save_flags;
3339 if (res == FAIL)
3340 goto on_error;
3341 ++ectx->ec_stack.ga_len;
3342 }
3343 break;
3344
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003345 // push typeval VAR_INSTR with instructions to be executed
3346 case ISN_INSTR:
3347 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003348 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003349 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003350 tv = STACK_TV_BOT(0);
3351 tv->vval.v_instr = ALLOC_ONE(instr_T);
3352 if (tv->vval.v_instr == NULL)
3353 goto on_error;
3354 ++ectx->ec_stack.ga_len;
3355
3356 tv->v_type = VAR_INSTR;
3357 tv->vval.v_instr->instr_ectx = ectx;
3358 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3359 }
3360 break;
3361
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003362 case ISN_SOURCE:
3363 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003364 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003365
Bram Moolenaar89445512022-04-14 12:58:23 +01003366 SOURCING_LNUM = iptr->isn_lnum;
3367 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003368 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003369 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003370 }
3371 break;
3372
Bram Moolenaar4c137212021-04-19 16:48:48 +02003373 // execute :substitute with an expression
3374 case ISN_SUBSTITUTE:
3375 {
3376 subs_T *subs = &iptr->isn_arg.subs;
3377 source_cookie_T cookie;
3378 struct subs_expr_S *save_instr = substitute_instr;
3379 struct subs_expr_S subs_instr;
3380 int res;
3381
3382 subs_instr.subs_ectx = ectx;
3383 subs_instr.subs_instr = subs->subs_instr;
3384 subs_instr.subs_status = OK;
3385 substitute_instr = &subs_instr;
3386
3387 SOURCING_LNUM = iptr->isn_lnum;
3388 // This is very much like ISN_EXEC
3389 CLEAR_FIELD(cookie);
3390 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3391 res = do_cmdline(subs->subs_cmd,
3392 getsourceline, &cookie,
3393 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3394 substitute_instr = save_instr;
3395
3396 if (res == FAIL || did_emsg
3397 || subs_instr.subs_status == FAIL)
3398 goto on_error;
3399 }
3400 break;
3401
3402 case ISN_FINISH:
3403 goto done;
3404
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003405 case ISN_REDIRSTART:
3406 // create a dummy entry for var_redir_str()
3407 if (alloc_redir_lval() == FAIL)
3408 goto on_error;
3409
3410 // The output is stored in growarray "redir_ga" until
3411 // redirection ends.
3412 init_redir_ga();
3413 redir_vname = 1;
3414 break;
3415
3416 case ISN_REDIREND:
3417 {
3418 char_u *res = get_clear_redir_ga();
3419
3420 // End redirection, put redirected text on the stack.
3421 clear_redir_lval();
3422 redir_vname = 0;
3423
Bram Moolenaar35578162021-08-02 19:10:38 +02003424 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003425 {
3426 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003427 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003428 }
3429 tv = STACK_TV_BOT(0);
3430 tv->v_type = VAR_STRING;
3431 tv->vval.v_string = res;
3432 ++ectx->ec_stack.ga_len;
3433 }
3434 break;
3435
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003436 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003437#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003438 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003439 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3440 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003441 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003442#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003443 break;
3444
3445 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003446#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003447 {
3448 exarg_T ea;
3449 int res;
3450
3451 CLEAR_FIELD(ea);
3452 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3453 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3454 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3455 --ectx->ec_stack.ga_len;
3456 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003457 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003458 res = cexpr_core(&ea, tv);
3459 clear_tv(tv);
3460 if (res == FAIL)
3461 goto on_error;
3462 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003463#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003464 break;
3465
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003466 // execute Ex command from pieces on the stack
3467 case ISN_EXECCONCAT:
3468 {
3469 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003470 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003471 int pass;
3472 int i;
3473 char_u *cmd = NULL;
3474 char_u *str;
3475
3476 for (pass = 1; pass <= 2; ++pass)
3477 {
3478 for (i = 0; i < count; ++i)
3479 {
3480 tv = STACK_TV_BOT(i - count);
3481 str = tv->vval.v_string;
3482 if (str != NULL && *str != NUL)
3483 {
3484 if (pass == 2)
3485 STRCPY(cmd + len, str);
3486 len += STRLEN(str);
3487 }
3488 if (pass == 2)
3489 clear_tv(tv);
3490 }
3491 if (pass == 1)
3492 {
3493 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003494 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003495 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003496 len = 0;
3497 }
3498 }
3499
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003500 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003501 do_cmdline_cmd(cmd);
3502 vim_free(cmd);
3503 }
3504 break;
3505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 // execute :echo {string} ...
3507 case ISN_ECHO:
3508 {
3509 int count = iptr->isn_arg.echo.echo_count;
3510 int atstart = TRUE;
3511 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003512 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513
3514 for (idx = 0; idx < count; ++idx)
3515 {
3516 tv = STACK_TV_BOT(idx - count);
3517 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3518 &atstart, &needclr);
3519 clear_tv(tv);
3520 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003521 if (needclr)
3522 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003523 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 }
3525 break;
3526
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003527 // :execute {string} ...
3528 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003529 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003530 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003531 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003532 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003533 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003534 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003535 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003536 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003537 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003538 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003539 garray_T ga;
3540 char_u buf[NUMBUFLEN];
3541 char_u *p;
3542 int len;
3543 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003544 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003545
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003546 if (iptr->isn_type == ISN_ECHOWINDOW)
3547 count = iptr->isn_arg.echowin.ewin_count;
3548 else
3549 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003550 ga_init2(&ga, 1, 80);
3551 for (idx = 0; idx < count; ++idx)
3552 {
3553 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003554 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003555 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003556 if (tv->v_type == VAR_CHANNEL
3557 || tv->v_type == VAR_JOB)
3558 {
3559 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003560 semsg(_(e_using_invalid_value_as_string_str),
3561 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003562 break;
3563 }
3564 else
3565 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003566 }
3567 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003568 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003569
3570 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003571 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003572 failed = TRUE;
3573 else
3574 {
3575 if (ga.ga_len > 0)
3576 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3577 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3578 ga.ga_len += len;
3579 }
3580 clear_tv(tv);
3581 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003582 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003583 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003584 {
3585 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003586 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003587 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003588
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003589 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003590 {
3591 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003592 {
3593 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003594 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003595 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003596 {
3597 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003598 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003599 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003600 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003601 else
3602 {
3603 msg_sb_eol();
3604 if (iptr->isn_type == ISN_ECHOMSG)
3605 {
3606 msg_attr(ga.ga_data, echo_attr);
3607 out_flush();
3608 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003609#ifdef HAS_MESSAGE_WINDOW
3610 else if (iptr->isn_type == ISN_ECHOWINDOW)
3611 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003612 start_echowindow(
3613 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003614 msg_attr(ga.ga_data, echo_attr);
3615 end_echowindow();
3616 }
3617#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003618 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3619 {
3620 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3621 TRUE);
3622 ui_write((char_u *)"\r\n", 2, TRUE);
3623 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003624 else
3625 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003626 SOURCING_LNUM = iptr->isn_lnum;
3627 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003628 }
3629 }
3630 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003631 ga_clear(&ga);
3632 }
3633 break;
3634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 // load local variable or argument
3636 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003637 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003638 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003639 tv = STACK_TV_VAR(iptr->isn_arg.number);
3640 if (tv->v_type == VAR_UNKNOWN)
3641 {
3642 // missing argument or default value v:none
3643 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3644 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3645 }
3646 else
3647 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003648 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 break;
3650
3651 // load v: variable
3652 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003653 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003654 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003656 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 break;
3658
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003659 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 case ISN_LOADSCRIPT:
3661 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003662 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 svar_T *sv;
3664
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003665 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003666 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003667 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003668 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003669 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003670 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003672 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 }
3674 break;
3675
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003676 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003678 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003680 int sid = iptr->isn_arg.loadstore.ls_sid;
3681 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003682 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 if (di == NULL)
3686 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003687 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003688 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003689 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 }
3691 else
3692 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003693 if (iptr->isn_type == ISN_LOADEXPORT)
3694 {
3695 int idx = get_script_item_idx(sid, name, 0,
3696 NULL, NULL);
3697 svar_T *sv;
3698
3699 if (idx >= 0)
3700 {
3701 sv = ((svar_T *)SCRIPT_ITEM(sid)
3702 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003703 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003704 {
3705 SOURCING_LNUM = iptr->isn_lnum;
3706 semsg(_(e_item_not_exported_in_script_str),
3707 name);
3708 goto on_error;
3709 }
3710 }
3711 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003712 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003713 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003715 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 }
3717 }
3718 break;
3719
Bram Moolenaard3aac292020-04-19 14:32:17 +02003720 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003722 case ISN_LOADB:
3723 case ISN_LOADW:
3724 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003726 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003727
Bram Moolenaar06b77222022-01-25 15:51:56 +00003728 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003729 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003730 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003731 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 break;
3735
Bram Moolenaar03290b82020-12-19 16:30:44 +01003736 // load autoload variable
3737 case ISN_LOADAUTO:
3738 {
3739 char_u *name = iptr->isn_arg.string;
3740
Bram Moolenaar35578162021-08-02 19:10:38 +02003741 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003742 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003743 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003744 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003745 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003746 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003747 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003748 }
3749 break;
3750
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003751 // load g:/b:/w:/t: namespace
3752 case ISN_LOADGDICT:
3753 case ISN_LOADBDICT:
3754 case ISN_LOADWDICT:
3755 case ISN_LOADTDICT:
3756 {
3757 dict_T *d = NULL;
3758
3759 switch (iptr->isn_type)
3760 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003761 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3762 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3763 case ISN_LOADWDICT: d = curwin->w_vars; break;
3764 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003765 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003766 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003767 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003768 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003769 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003770 tv = STACK_TV_BOT(0);
3771 tv->v_type = VAR_DICT;
3772 tv->v_lock = 0;
3773 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003774 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003775 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003776 }
3777 break;
3778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 // load &option
3780 case ISN_LOADOPT:
3781 {
3782 typval_T optval;
3783 char_u *name = iptr->isn_arg.string;
3784
Bram Moolenaara8c17702020-04-01 21:17:24 +02003785 // This is not expected to fail, name is checked during
3786 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003787 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003788 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003789 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003790 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003792 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 }
3794 break;
3795
3796 // load $ENV
3797 case ISN_LOADENV:
3798 {
3799 typval_T optval;
3800 char_u *name = iptr->isn_arg.string;
3801
Bram Moolenaar35578162021-08-02 19:10:38 +02003802 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003803 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003804 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003805 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003807 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 }
3809 break;
3810
3811 // load @register
3812 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003813 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003814 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 tv = STACK_TV_BOT(0);
3816 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003817 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003818 // This may result in NULL, which should be equivalent to an
3819 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 tv->vval.v_string = get_reg_contents(
3821 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003822 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 break;
3824
3825 // store local variable
3826 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003827 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 tv = STACK_TV_VAR(iptr->isn_arg.number);
Ernie Rael9ed53752023-12-11 17:40:46 +01003829 if (check_typval_is_value(STACK_TV_BOT(0)) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003830 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003831 clear_tv(STACK_TV_BOT(0));
3832 goto on_error;
3833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 clear_tv(tv);
3835 *tv = *STACK_TV_BOT(0);
3836 break;
3837
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003838 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003839 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003840 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003841 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003842 int sid = iptr->isn_arg.loadstore.ls_sid;
3843 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003844 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003845 dictitem_T *di = find_var_in_ht(ht, 0,
3846 iptr->isn_type == ISN_STORES
3847 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003848
Bram Moolenaar4c137212021-04-19 16:48:48 +02003849 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003850 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003851 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003852 {
3853 if (iptr->isn_type == ISN_STOREEXPORT)
3854 {
3855 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003856 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003857 goto on_error;
3858 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003859 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003860 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003861 else
3862 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003863 if (iptr->isn_type == ISN_STOREEXPORT)
3864 {
3865 int idx = get_script_item_idx(sid, name, 0,
3866 NULL, NULL);
3867
Bram Moolenaar06651632022-04-27 17:54:25 +01003868 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003869 if (idx >= 0)
3870 {
3871 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3872 ->sn_var_vals.ga_data) + idx;
3873
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003874 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003875 {
3876 semsg(_(e_item_not_exported_in_script_str),
3877 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003878 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003879 goto on_error;
3880 }
3881 }
3882 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003883 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003884 {
3885 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003886 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003887 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003888 clear_tv(&di->di_tv);
3889 di->di_tv = *STACK_TV_BOT(0);
3890 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003891 }
3892 break;
3893
3894 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 case ISN_STORESCRIPT:
3896 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003897 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3898 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003900 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003901 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003902 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003903 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003904
3905 // "const" and "final" are checked at compile time, locking
3906 // the value needs to be checked here.
3907 SOURCING_LNUM = iptr->isn_lnum;
3908 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003909 {
3910 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003911 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003912 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 clear_tv(sv->sv_tv);
3915 *sv->sv_tv = *STACK_TV_BOT(0);
3916 }
3917 break;
3918
3919 // store option
3920 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003921 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003923 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3924 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 long n = 0;
3926 char_u *s = NULL;
3927 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003928 char_u numbuf[NUMBUFLEN];
3929 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930
Bram Moolenaar4c137212021-04-19 16:48:48 +02003931 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003932 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 tv = STACK_TV_BOT(0);
3934 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003935 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003937 if (s == NULL)
3938 s = (char_u *)"";
3939 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003940 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3941 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003942 // If the option can be set to a function reference or
3943 // a lambda and the passed value is a function
3944 // reference, then convert it to the name (string) of
3945 // the function reference.
3946 s = tv2string(tv, &tofree, numbuf, 0);
3947 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003948 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003949 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003950 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003951 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003952 goto on_error;
3953 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003956 // must be VAR_NUMBER, CHECKTYPE makes sure
3957 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003958 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003959 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003960 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 if (msg != NULL)
3962 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003963 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003965 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 }
3968 break;
3969
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003970 // store $ENV
3971 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003972 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003973 tv = STACK_TV_BOT(0);
3974 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3975 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003976 break;
3977
3978 // store @r
3979 case ISN_STOREREG:
3980 {
3981 int reg = iptr->isn_arg.number;
3982
Bram Moolenaar4c137212021-04-19 16:48:48 +02003983 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003984 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003985 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003986 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003987 }
3988 break;
3989
3990 // store v: variable
3991 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003992 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003993 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3994 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003995 // should not happen, type is checked when compiling
3996 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003997 break;
3998
Bram Moolenaard3aac292020-04-19 14:32:17 +02003999 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02004001 case ISN_STOREB:
4002 case ISN_STOREW:
4003 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004005 dictitem_T *di;
4006 hashtab_T *ht;
4007 char_u *name = iptr->isn_arg.string + 2;
4008
Bram Moolenaard3aac292020-04-19 14:32:17 +02004009 switch (iptr->isn_type)
4010 {
4011 case ISN_STOREG:
4012 ht = get_globvar_ht();
4013 break;
4014 case ISN_STOREB:
4015 ht = &curbuf->b_vars->dv_hashtab;
4016 break;
4017 case ISN_STOREW:
4018 ht = &curwin->w_vars->dv_hashtab;
4019 break;
4020 case ISN_STORET:
4021 ht = &curtab->tp_vars->dv_hashtab;
4022 break;
4023 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004024 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026
Bram Moolenaar4c137212021-04-19 16:48:48 +02004027 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004028 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004030 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 else
4032 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004033 SOURCING_LNUM = iptr->isn_lnum;
4034 if (var_check_permission(di, name) == FAIL)
4035 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 clear_tv(&di->di_tv);
4037 di->di_tv = *STACK_TV_BOT(0);
4038 }
4039 }
4040 break;
4041
Bram Moolenaar03290b82020-12-19 16:30:44 +01004042 // store an autoload variable
4043 case ISN_STOREAUTO:
4044 SOURCING_LNUM = iptr->isn_lnum;
4045 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
4046 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004047 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004048 break;
4049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 // store number in local variable
4051 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004052 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 clear_tv(tv);
4054 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004055 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 break;
4057
Bram Moolenaar590162c2022-12-24 21:24:06 +00004058 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004059 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004060 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004061 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004062
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004063 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004064 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004065 if (res == NOTDONE)
4066 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004067 }
4068 break;
4069
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004070 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004071 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004072 if (execute_storerange(iptr, ectx) == FAIL)
4073 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004074 break;
4075
Bram Moolenaard505d172022-12-18 21:42:55 +00004076 case ISN_LOAD_CLASSMEMBER:
4077 {
4078 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4079 goto theend;
4080 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004081 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4082 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004083 ++ectx->ec_stack.ga_len;
4084 }
4085 break;
4086
4087 case ISN_STORE_CLASSMEMBER:
4088 {
4089 classmember_T *cm = &iptr->isn_arg.classmember;
4090 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4091 clear_tv(tv);
4092 *tv = *STACK_TV_BOT(-1);
4093 --ectx->ec_stack.ga_len;
4094 }
4095 break;
4096
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004097 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004098 case ISN_LOADOUTER:
4099 case ISN_STOREOUTER:
4100 {
4101 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004102 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4103 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004104
4105 while (depth > 1 && outer != NULL)
4106 {
4107 outer = outer->out_up;
4108 --depth;
4109 }
4110 if (outer == NULL)
4111 {
4112 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004113 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4114 || ectx->ec_outer_ref == NULL)
4115 // Possibly :def function called from legacy
4116 // context.
4117 emsg(_(e_closure_called_from_invalid_context));
4118 else
4119 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004120 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004121 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004122 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004123 // Variable declared in loop. May be copied if the
4124 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004125 tv = ((typval_T *)outer->out_loop[-depth - 1]
4126 .stack->ga_data)
4127 + outer->out_loop[-depth - 1].var_idx
4128 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004129 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004130 // Variable declared in a function. May be copied if
4131 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004132 tv = ((typval_T *)outer->out_stack->ga_data)
4133 + outer->out_frame_idx + STACK_FRAME_SIZE
4134 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004135 if (iptr->isn_type == ISN_LOADOUTER)
4136 {
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004137 typval_T *copy;
Bram Moolenaar35578162021-08-02 19:10:38 +02004138 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004139 goto theend;
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004140 // careful: ga_grow_inner may re-alloc the stack
4141 if (depth < 0)
4142 copy = ((typval_T *)outer->out_loop[-depth - 1]
4143 .stack->ga_data)
4144 + outer->out_loop[-depth - 1].var_idx
4145 + iptr->isn_arg.outer.outer_idx;
4146 else
4147 copy = ((typval_T *)outer->out_stack->ga_data)
4148 + outer->out_frame_idx + STACK_FRAME_SIZE
4149 + iptr->isn_arg.outer.outer_idx;
4150 // memory was freed, get tv again
4151 if (copy != tv)
4152 tv = copy;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004153 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004154 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004155 }
4156 else
4157 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004158 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004159 clear_tv(tv);
4160 *tv = *STACK_TV_BOT(0);
4161 }
4162 }
4163 break;
4164
Bram Moolenaar752fc692021-01-04 21:57:11 +01004165 // unlet item in list or dict variable
4166 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004167 if (execute_unletindex(iptr, ectx) == FAIL)
4168 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004169 break;
4170
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004171 // unlet range of items in list variable
4172 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004173 if (execute_unletrange(iptr, ectx) == FAIL)
4174 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004175 break;
4176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 // push constant
4178 case ISN_PUSHNR:
4179 case ISN_PUSHBOOL:
4180 case ISN_PUSHSPEC:
4181 case ISN_PUSHF:
4182 case ISN_PUSHS:
4183 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004184 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004185 case ISN_PUSHCHANNEL:
4186 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004187 case ISN_PUSHOBJ:
4188 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004189 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004190 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004192 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004193 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 switch (iptr->isn_type)
4195 {
4196 case ISN_PUSHNR:
4197 tv->v_type = VAR_NUMBER;
4198 tv->vval.v_number = iptr->isn_arg.number;
4199 break;
4200 case ISN_PUSHBOOL:
4201 tv->v_type = VAR_BOOL;
4202 tv->vval.v_number = iptr->isn_arg.number;
4203 break;
4204 case ISN_PUSHSPEC:
4205 tv->v_type = VAR_SPECIAL;
4206 tv->vval.v_number = iptr->isn_arg.number;
4207 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 case ISN_PUSHF:
4209 tv->v_type = VAR_FLOAT;
4210 tv->vval.v_float = iptr->isn_arg.fnumber;
4211 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 case ISN_PUSHBLOB:
4213 blob_copy(iptr->isn_arg.blob, tv);
4214 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004215 case ISN_PUSHFUNC:
4216 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004217 if (iptr->isn_arg.string == NULL)
4218 tv->vval.v_string = NULL;
4219 else
4220 tv->vval.v_string =
4221 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004222 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004223 case ISN_PUSHCHANNEL:
4224#ifdef FEAT_JOB_CHANNEL
4225 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004226 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004227#endif
4228 break;
4229 case ISN_PUSHJOB:
4230#ifdef FEAT_JOB_CHANNEL
4231 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004232 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004233#endif
4234 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004235 case ISN_PUSHOBJ:
4236 tv->v_type = VAR_OBJECT;
4237 tv->vval.v_object = NULL;
4238 break;
4239 case ISN_PUSHCLASS:
4240 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004241 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004242 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 default:
4244 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004245 tv->vval.v_string = iptr->isn_arg.string == NULL
4246 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 }
4248 break;
4249
Bram Moolenaar06b77222022-01-25 15:51:56 +00004250 case ISN_AUTOLOAD:
4251 {
4252 char_u *name = iptr->isn_arg.string;
4253
4254 (void)script_autoload(name, FALSE);
4255 if (find_func(name, TRUE))
4256 {
4257 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4258 goto theend;
4259 tv = STACK_TV_BOT(0);
4260 tv->v_lock = 0;
4261 ++ectx->ec_stack.ga_len;
4262 tv->v_type = VAR_FUNC;
4263 tv->vval.v_string = vim_strsave(name);
4264 }
4265 else
4266 {
4267 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4268
4269 if (res == NOTDONE)
4270 goto theend;
4271 if (res == FAIL)
4272 goto on_error;
4273 }
4274 }
4275 break;
4276
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004277 case ISN_UNLET:
4278 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4279 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004280 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004281 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004282 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004283 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004284 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004285
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004286 case ISN_LOCKUNLOCK:
4287 {
Ernie Raelee865f32023-09-29 19:53:55 +02004288#ifdef LOG_LOCKVAR
4289 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4290 iptr->isn_arg.string);
4291#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004292 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004293
4294 // Stack has the local variable, argument the whole :lock
4295 // or :unlock command, like ISN_EXEC.
4296 --ectx->ec_stack.ga_len;
Ernie Rael4c8da022023-10-11 21:35:11 +02004297 lval_root_T root = { .lr_tv = STACK_TV_BOT(0),
4298 .lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec,
4299 .lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg };
Ernie Rael64885642023-10-04 20:16:22 +02004300 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004301 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004302 iptr->isn_arg.lockunlock.lu_string);
4303 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004304 lval_root = lval_root_save;
4305 if (res == FAIL)
4306 goto on_error;
4307 }
4308 break;
4309
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004310 case ISN_LOCKCONST:
4311 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4312 break;
4313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 // create a list from items on the stack; uses a single allocation
4315 // for the list header and the items
4316 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004317 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004318 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 break;
4320
4321 // create a dict from items on the stack
4322 case ISN_NEWDICT:
4323 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004324 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004326 SOURCING_LNUM = iptr->isn_lnum;
4327 res = exe_newdict(iptr->isn_arg.number, ectx);
4328 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004329 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004330 if (res == MAYBE)
4331 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 }
4333 break;
4334
LemonBoy372bcce2022-04-25 12:43:20 +01004335 case ISN_CONCAT:
4336 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4337 goto theend;
4338 break;
4339
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004340 // create a partial with NULL value
4341 case ISN_NEWPARTIAL:
4342 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4343 goto theend;
4344 ++ectx->ec_stack.ga_len;
4345 tv = STACK_TV_BOT(-1);
4346 tv->v_type = VAR_PARTIAL;
4347 tv->v_lock = 0;
4348 tv->vval.v_partial = NULL;
4349 break;
4350
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 // call a :def function
4352 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004353 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004354 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4355 NULL,
4356 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004357 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004358 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 break;
4360
Bram Moolenaard0200c82023-01-28 15:19:40 +00004361 // call a method on an interface
4362 case ISN_METHODCALL:
4363 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004364 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4365
Bram Moolenaard0200c82023-01-28 15:19:40 +00004366 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004367 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004368 if (tv->v_type != VAR_OBJECT)
4369 {
4370 object_required_error(tv);
4371 goto on_error;
4372 }
4373 object_T *obj = tv->vval.v_object;
4374 class_T *cl = obj->obj_class;
4375
4376 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004377 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004378 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004379
4380 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4381 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4382 goto on_error;
4383 }
4384 break;
4385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 // call a builtin function
4387 case ISN_BCALL:
4388 SOURCING_LNUM = iptr->isn_lnum;
4389 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4390 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004391 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004392 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 break;
4394
4395 // call a funcref or partial
4396 case ISN_PCALL:
4397 {
4398 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4399 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004400 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401
4402 SOURCING_LNUM = iptr->isn_lnum;
4403 if (pfunc->cpf_top)
4404 {
4405 // funcref is above the arguments
4406 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4407 }
4408 else
4409 {
4410 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004411 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004412 partial_tv = *STACK_TV_BOT(0);
4413 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004415 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004416 if (tv == &partial_tv)
4417 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004419 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 }
4421 break;
4422
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004423 case ISN_PCALL_END:
4424 // PCALL finished, arguments have been consumed and replaced by
4425 // the return value. Now clear the funcref from the stack,
4426 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004427 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004428 clear_tv(STACK_TV_BOT(-1));
4429 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4430 break;
4431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 // call a user defined function or funcref/partial
4433 case ISN_UCALL:
4434 {
4435 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4436
4437 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004438 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004439 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004440 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 }
4442 break;
4443
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004444 // :defer func(arg)
4445 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004446 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004447 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4448 goto on_error;
4449 break;
4450
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004451 // Return from a :def function call without a value.
4452 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004453 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004454 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004455 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004456 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004457 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004458 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004459 if (iptr->isn_type == ISN_RETURN_VOID)
4460 {
4461 tv->v_type = VAR_VOID;
4462 tv->vval.v_number = 0;
4463 tv->v_lock = 0;
4464 }
4465 else
4466 {
4467 *tv = *STACK_TV_VAR(0);
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01004468 object_T *obj = tv->vval.v_object;
4469 ++obj->obj_refcount;
4470
4471 // Lock all the constant object variables
4472 obj_lock_const_vars(obj);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004473 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004474 // FALLTHROUGH
4475
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004476 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477 case ISN_RETURN:
4478 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004479 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004480 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004481
Ernie Raelb077b582023-12-14 20:11:44 +01004482 ///////////////////////////////////////////////////
4483 // TODO: If FAIL, line number in output not correct
4484 ///////////////////////////////////////////////////
4485 if (check_typval_is_value(STACK_TV_BOT(-1)) == FAIL)
4486 goto theend;
4487
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004488 if (trystack->ga_len > 0)
4489 trycmd = ((trycmd_T *)trystack->ga_data)
4490 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004491 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004492 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004494 // jump to ":finally" or ":endtry"
4495 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004496 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004497 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004498 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 trycmd->tcd_return = TRUE;
4500 }
4501 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004502 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 }
4504 break;
4505
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004506 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 case ISN_FUNCREF:
4508 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004509 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4510 ufunc_T *ufunc;
4511 funcref_T *funcref = &iptr->isn_arg.funcref;
4512 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004515 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004516 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004517 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004518 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004519 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004520 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004521 if (extra != NULL && extra->fre_class != NULL)
4522 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004523 class_T *cl;
4524 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004525 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004526 tv = STACK_TV_BOT(-1);
4527 if (tv->v_type != VAR_OBJECT)
4528 {
4529 object_required_error(tv);
4530 vim_free(pt);
4531 goto on_error;
4532 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004533
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004534 object_T *obj = tv->vval.v_object;
4535 cl = obj->obj_class;
4536 // drop the value from the stack
4537 clear_tv(tv);
4538 --ectx->ec_stack.ga_len;
4539
4540 pt->pt_obj = obj;
4541 ++obj->obj_refcount;
4542 }
4543 else
4544 cl = extra->fre_class;
4545
4546 if (extra->fre_object_method)
4547 {
4548 // object method
4549 // convert the interface index to the object index
4550 int idx =
4551 object_index_from_itf_index(extra->fre_class,
4552 TRUE, extra->fre_method_idx, cl);
4553 ufunc = cl->class_obj_methods[idx];
4554 }
4555 else
4556 {
4557 // class method
4558 ufunc =
4559 cl->class_class_functions[extra->fre_method_idx];
4560 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004561 }
4562 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004563 {
4564 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4565 + funcref->fr_dfunc_idx;
4566
4567 ufunc = pt_dfunc->df_ufunc;
4568 }
4569 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004570 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004571 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004572 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004573 if (ufunc == NULL)
4574 {
4575 SOURCING_LNUM = iptr->isn_lnum;
4576 iemsg("ufunc unexpectedly NULL for FUNCREF");
Christian Brabandt915f3bf2024-04-05 20:12:19 +02004577 vim_free(pt);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004578 goto theend;
4579 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004580 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004581 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004582 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004583 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004585 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 tv->vval.v_partial = pt;
4587 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004588 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 }
4590 break;
4591
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004592 // Create a global function from a lambda.
4593 case ISN_NEWFUNC:
4594 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004595 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004596
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004597 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004598 arg->nfa_global, &arg->nfa_loopvar_info,
4599 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004600 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004601 }
4602 break;
4603
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004604 // List functions
4605 case ISN_DEF:
4606 if (iptr->isn_arg.string == NULL)
4607 list_functions(NULL);
4608 else
4609 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004610 exarg_T ea;
4611 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004612
4613 CLEAR_FIELD(ea);
4614 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004615 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004616 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004617 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004618 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004619 }
4620 break;
4621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 // jump if a condition is met
4623 case ISN_JUMP:
4624 {
4625 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004626 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 int jump = TRUE;
4628
4629 if (when != JUMP_ALWAYS)
4630 {
4631 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004632 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004633 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004634 || when == JUMP_IF_COND_TRUE)
4635 {
4636 SOURCING_LNUM = iptr->isn_lnum;
4637 jump = tv_get_bool_chk(tv, &error);
4638 if (error)
4639 goto on_error;
4640 }
4641 else
4642 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004643 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004645 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 {
4647 // drop the value from the stack
4648 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004649 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 }
4651 }
4652 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004653 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 }
4655 break;
4656
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004657 // "while": jump to end if a condition is false
4658 case ISN_WHILE:
4659 {
4660 int error = FALSE;
4661 int jump = TRUE;
4662
4663 tv = STACK_TV_BOT(-1);
4664 SOURCING_LNUM = iptr->isn_lnum;
4665 jump = !tv_get_bool_chk(tv, &error);
4666 if (error)
4667 goto on_error;
4668 // drop the value from the stack
4669 clear_tv(tv);
4670 --ectx->ec_stack.ga_len;
4671 if (jump)
4672 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4673
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004674 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004675 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004676 tv = STACK_TV_VAR(
4677 iptr->isn_arg.whileloop.while_funcref_idx);
4678 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4679 }
4680 break;
4681
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004682 // Jump if an argument with a default value was already set and not
4683 // v:none.
4684 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004685 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004686 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004687 int arg_set = tv->v_type != VAR_UNKNOWN
4688 && !(tv->v_type == VAR_SPECIAL
4689 && tv->vval.v_number == VVAL_NONE);
4690 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004691 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004692 break;
4693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 // top of a for loop
4695 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004696 if (execute_for(iptr, ectx) == FAIL)
4697 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 break;
4699
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004700 // end of a for or while loop
4701 case ISN_ENDLOOP:
4702 if (execute_endloop(iptr, ectx) == FAIL)
4703 goto theend;
4704 break;
4705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 // start of ":try" block
4707 case ISN_TRY:
4708 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004709 trycmd_T *trycmd = NULL;
4710
Bram Moolenaar35578162021-08-02 19:10:38 +02004711 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004712 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004713 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4714 + ectx->ec_trystack.ga_len;
4715 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004717 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004718 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4719 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004720 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004721 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004722 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004723 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004724 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004725 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 }
4727 break;
4728
4729 case ISN_PUSHEXC:
4730 if (current_exception == NULL)
4731 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004732 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004734 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004736 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004737 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004739 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004741 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 tv->vval.v_string = vim_strsave(
4743 (char_u *)current_exception->value);
4744 break;
4745
4746 case ISN_CATCH:
4747 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004748 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004749 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750
Bram Moolenaar4c137212021-04-19 16:48:48 +02004751 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004752 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004754 trycmd->tcd_caught = TRUE;
4755 trycmd->tcd_did_throw = FALSE;
4756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004758 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759 catch_exception(current_exception);
4760 }
4761 break;
4762
Bram Moolenaarc150c092021-02-13 15:02:46 +01004763 case ISN_TRYCONT:
4764 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004765 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004766 trycont_T *trycont = &iptr->isn_arg.trycont;
4767 int i;
4768 trycmd_T *trycmd;
4769 int iidx = trycont->tct_where;
4770
4771 if (trystack->ga_len < trycont->tct_levels)
4772 {
4773 siemsg("TRYCONT: expected %d levels, found %d",
4774 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004775 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004776 }
4777 // Make :endtry jump to any outer try block and the last
4778 // :endtry inside the loop to the loop start.
4779 for (i = trycont->tct_levels; i > 0; --i)
4780 {
4781 trycmd = ((trycmd_T *)trystack->ga_data)
4782 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004783 // Add one to tcd_cont to be able to jump to
4784 // instruction with index zero.
4785 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004786 iidx = trycmd->tcd_finally_idx == 0
4787 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004788 }
4789 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004790 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004791 }
4792 break;
4793
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004794 case ISN_FINALLY:
4795 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004796 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004797 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4798 + trystack->ga_len - 1;
4799
4800 // Reset the index to avoid a return statement jumps here
4801 // again.
4802 trycmd->tcd_finally_idx = 0;
4803 break;
4804 }
4805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 // end of ":try" block
4807 case ISN_ENDTRY:
4808 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004809 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004810 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811
Bram Moolenaar06651632022-04-27 17:54:25 +01004812 --trystack->ga_len;
4813 --trylevel;
4814 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4815 if (trycmd->tcd_did_throw)
4816 did_throw = TRUE;
4817 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004818 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004819 // discard the exception
4820 if (caught_stack == current_exception)
4821 caught_stack = caught_stack->caught;
4822 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004824
4825 if (trycmd->tcd_return)
4826 goto func_return;
4827
4828 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4829 {
4830 --ectx->ec_stack.ga_len;
4831 clear_tv(STACK_TV_BOT(0));
4832 }
4833 if (trycmd->tcd_cont != 0)
4834 // handling :continue: jump to outer try block or
4835 // start of the loop
4836 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 }
4838 break;
4839
4840 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004841 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004842 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004843
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004844 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4845 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004846 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004847 // the function to abort but not display an error.
4848 tv = STACK_TV_BOT(-1);
4849 clear_tv(tv);
4850 tv->v_type = VAR_NUMBER;
4851 tv->vval.v_number = 0;
4852 goto done;
4853 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004854 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004855 tv = STACK_TV_BOT(0);
4856 if (tv->vval.v_string == NULL
4857 || *skipwhite(tv->vval.v_string) == NUL)
4858 {
4859 vim_free(tv->vval.v_string);
4860 SOURCING_LNUM = iptr->isn_lnum;
4861 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004862 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004863 }
4864
4865 // Inside a "catch" we need to first discard the caught
4866 // exception.
4867 if (trystack->ga_len > 0)
4868 {
4869 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4870 + trystack->ga_len - 1;
4871 if (trycmd->tcd_caught && current_exception != NULL)
4872 {
4873 // discard the exception
4874 if (caught_stack == current_exception)
4875 caught_stack = caught_stack->caught;
4876 discard_current_exception();
4877 trycmd->tcd_caught = FALSE;
4878 }
4879 }
4880
Bram Moolenaar90a57162022-02-12 14:23:17 +00004881 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004882 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4883 == FAIL)
4884 {
4885 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004886 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004887 }
4888 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890 break;
4891
4892 // compare with special values
4893 case ISN_COMPAREBOOL:
4894 case ISN_COMPARESPECIAL:
4895 {
4896 typval_T *tv1 = STACK_TV_BOT(-2);
4897 typval_T *tv2 = STACK_TV_BOT(-1);
4898 varnumber_T arg1 = tv1->vval.v_number;
4899 varnumber_T arg2 = tv2->vval.v_number;
4900 int res;
4901
Bram Moolenaar06651632022-04-27 17:54:25 +01004902 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4903 res = arg1 == arg2;
4904 else
4905 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906
Bram Moolenaar4c137212021-04-19 16:48:48 +02004907 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 tv1->v_type = VAR_BOOL;
4909 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4910 }
4911 break;
4912
Bram Moolenaar7a222242022-03-01 19:23:24 +00004913 case ISN_COMPARENULL:
4914 {
4915 typval_T *tv1 = STACK_TV_BOT(-2);
4916 typval_T *tv2 = STACK_TV_BOT(-1);
4917 int res;
4918
4919 res = typval_compare_null(tv1, tv2);
4920 if (res == MAYBE)
4921 goto on_error;
4922 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4923 res = !res;
4924 clear_tv(tv1);
4925 clear_tv(tv2);
4926 --ectx->ec_stack.ga_len;
4927 tv1->v_type = VAR_BOOL;
4928 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4929 }
4930 break;
4931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 // Operation with two number arguments
4933 case ISN_OPNR:
4934 case ISN_COMPARENR:
4935 {
4936 typval_T *tv1 = STACK_TV_BOT(-2);
4937 typval_T *tv2 = STACK_TV_BOT(-1);
4938 varnumber_T arg1 = tv1->vval.v_number;
4939 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004940 varnumber_T res = 0;
4941 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004943 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4944 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4945 {
4946 if (arg2 < 0)
4947 {
4948 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004949 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004950 goto on_error;
4951 }
4952 }
4953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 switch (iptr->isn_arg.op.op_type)
4955 {
4956 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004957 case EXPR_DIV: if (arg2 == 0)
4958 div_zero = TRUE;
4959 else
4960 res = arg1 / arg2;
4961 break;
4962 case EXPR_REM: if (arg2 == 0)
4963 div_zero = TRUE;
4964 else
4965 res = arg1 % arg2;
4966 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 case EXPR_SUB: res = arg1 - arg2; break;
4968 case EXPR_ADD: res = arg1 + arg2; break;
4969
4970 case EXPR_EQUAL: res = arg1 == arg2; break;
4971 case EXPR_NEQUAL: res = arg1 != arg2; break;
4972 case EXPR_GREATER: res = arg1 > arg2; break;
4973 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4974 case EXPR_SMALLER: res = arg1 < arg2; break;
4975 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004976 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4977 res = 0;
4978 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004979 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004980 break;
4981 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4982 res = 0;
4983 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004984 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004985 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004986 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 }
4988
Bram Moolenaar4c137212021-04-19 16:48:48 +02004989 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 if (iptr->isn_type == ISN_COMPARENR)
4991 {
4992 tv1->v_type = VAR_BOOL;
4993 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4994 }
4995 else
4996 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004997 if (div_zero)
4998 {
4999 SOURCING_LNUM = iptr->isn_lnum;
5000 emsg(_(e_divide_by_zero));
5001 goto on_error;
5002 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 }
5004 break;
5005
5006 // Computation with two float arguments
5007 case ISN_OPFLOAT:
5008 case ISN_COMPAREFLOAT:
5009 {
5010 typval_T *tv1 = STACK_TV_BOT(-2);
5011 typval_T *tv2 = STACK_TV_BOT(-1);
5012 float_T arg1 = tv1->vval.v_float;
5013 float_T arg2 = tv2->vval.v_float;
5014 float_T res = 0;
5015 int cmp = FALSE;
5016
5017 switch (iptr->isn_arg.op.op_type)
5018 {
5019 case EXPR_MULT: res = arg1 * arg2; break;
5020 case EXPR_DIV: res = arg1 / arg2; break;
5021 case EXPR_SUB: res = arg1 - arg2; break;
5022 case EXPR_ADD: res = arg1 + arg2; break;
5023
5024 case EXPR_EQUAL: cmp = arg1 == arg2; break;
5025 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
5026 case EXPR_GREATER: cmp = arg1 > arg2; break;
5027 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
5028 case EXPR_SMALLER: cmp = arg1 < arg2; break;
5029 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
5030 default: cmp = 0; break;
5031 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005032 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 if (iptr->isn_type == ISN_COMPAREFLOAT)
5034 {
5035 tv1->v_type = VAR_BOOL;
5036 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
5037 }
5038 else
5039 tv1->vval.v_float = res;
5040 }
5041 break;
5042
5043 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00005044 case ISN_COMPAREDICT:
5045 case ISN_COMPAREFUNC:
5046 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005048 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 {
5050 typval_T *tv1 = STACK_TV_BOT(-2);
5051 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00005052 exprtype_T exprtype = iptr->isn_arg.op.op_type;
5053 int ic = iptr->isn_arg.op.op_ic;
5054 int res = FALSE;
5055 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056
Bram Moolenaar265f8112021-12-19 12:33:05 +00005057 SOURCING_LNUM = iptr->isn_lnum;
5058 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005060 status = typval_compare_list(tv1, tv2,
5061 exprtype, ic, &res);
5062 }
5063 else if (iptr->isn_type == ISN_COMPAREDICT)
5064 {
5065 status = typval_compare_dict(tv1, tv2,
5066 exprtype, ic, &res);
5067 }
5068 else if (iptr->isn_type == ISN_COMPAREFUNC)
5069 {
5070 status = typval_compare_func(tv1, tv2,
5071 exprtype, ic, &res);
5072 }
5073 else if (iptr->isn_type == ISN_COMPARESTRING)
5074 {
5075 status = typval_compare_string(tv1, tv2,
5076 exprtype, ic, &res);
5077 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005078 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005079 {
5080 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005082 else // ISN_COMPAREOBJECT
5083 {
5084 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005085 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005086 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005087 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088 clear_tv(tv1);
5089 clear_tv(tv2);
5090 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005091 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5092 if (status == FAIL)
5093 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 }
5095 break;
5096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 case ISN_COMPAREANY:
5098 {
5099 typval_T *tv1 = STACK_TV_BOT(-2);
5100 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005101 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005103 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104
Bram Moolenaareb26f432020-09-14 16:50:05 +02005105 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005106 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005108 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005109 if (status == FAIL)
5110 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 }
5112 break;
5113
5114 case ISN_ADDLIST:
5115 case ISN_ADDBLOB:
5116 {
5117 typval_T *tv1 = STACK_TV_BOT(-2);
5118 typval_T *tv2 = STACK_TV_BOT(-1);
5119
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005120 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005122 {
5123 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5124 && tv1->vval.v_list != NULL)
5125 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5126 NULL);
5127 else
5128 eval_addlist(tv1, tv2);
5129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 else
5131 eval_addblob(tv1, tv2);
5132 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005133 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 }
5135 break;
5136
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005137 case ISN_LISTAPPEND:
5138 {
5139 typval_T *tv1 = STACK_TV_BOT(-2);
5140 typval_T *tv2 = STACK_TV_BOT(-1);
5141 list_T *l = tv1->vval.v_list;
5142
5143 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005144 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005145 if (l == NULL)
5146 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005147 emsg(_(e_cannot_add_to_null_list));
5148 goto on_error;
5149 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005150 if (value_check_lock(l->lv_lock, NULL, FALSE))
5151 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005152 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005153 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005154 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005155 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005156 }
5157 break;
5158
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005159 case ISN_BLOBAPPEND:
5160 {
5161 typval_T *tv1 = STACK_TV_BOT(-2);
5162 typval_T *tv2 = STACK_TV_BOT(-1);
5163 blob_T *b = tv1->vval.v_blob;
5164 int error = FALSE;
5165 varnumber_T n;
5166
5167 // add a number to a blob
5168 if (b == NULL)
5169 {
5170 SOURCING_LNUM = iptr->isn_lnum;
5171 emsg(_(e_cannot_add_to_null_blob));
5172 goto on_error;
5173 }
5174 n = tv_get_number_chk(tv2, &error);
5175 if (error)
5176 goto on_error;
5177 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005178 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005179 }
5180 break;
5181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 // Computation with two arguments of unknown type
5183 case ISN_OPANY:
5184 {
5185 typval_T *tv1 = STACK_TV_BOT(-2);
5186 typval_T *tv2 = STACK_TV_BOT(-1);
5187 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005188 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 int error = FALSE;
5190
5191 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5192 {
5193 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5194 {
5195 eval_addlist(tv1, tv2);
5196 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005197 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198 break;
5199 }
5200 else if (tv1->v_type == VAR_BLOB
5201 && tv2->v_type == VAR_BLOB)
5202 {
5203 eval_addblob(tv1, tv2);
5204 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005205 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 break;
5207 }
5208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209 if (tv1->v_type == VAR_FLOAT)
5210 {
5211 f1 = tv1->vval.v_float;
5212 n1 = 0;
5213 }
5214 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005216 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217 n1 = tv_get_number_chk(tv1, &error);
5218 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005219 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 if (tv2->v_type == VAR_FLOAT)
5221 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 if (tv2->v_type == VAR_FLOAT)
5224 {
5225 f2 = tv2->vval.v_float;
5226 n2 = 0;
5227 }
5228 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005229 {
5230 n2 = tv_get_number_chk(tv2, &error);
5231 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005232 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 if (tv1->v_type == VAR_FLOAT)
5234 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236 // if there is a float on either side the result is a float
5237 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5238 {
5239 switch (iptr->isn_arg.op.op_type)
5240 {
5241 case EXPR_MULT: f1 = f1 * f2; break;
5242 case EXPR_DIV: f1 = f1 / f2; break;
5243 case EXPR_SUB: f1 = f1 - f2; break;
5244 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005245 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005246 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005247 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248 }
5249 clear_tv(tv1);
5250 clear_tv(tv2);
5251 tv1->v_type = VAR_FLOAT;
5252 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005253 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254 }
5255 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005257 int failed = FALSE;
5258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259 switch (iptr->isn_arg.op.op_type)
5260 {
5261 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005262 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5263 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005264 goto on_error;
5265 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005266 case EXPR_SUB: n1 = n1 - n2; break;
5267 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005268 default: n1 = num_modulus(n1, n2, &failed);
5269 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005270 goto on_error;
5271 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005272 }
5273 clear_tv(tv1);
5274 clear_tv(tv2);
5275 tv1->v_type = VAR_NUMBER;
5276 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005277 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005278 }
5279 }
5280 break;
5281
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005282 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005283 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005284 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005285 int is_slice = iptr->isn_type == ISN_STRSLICE;
5286 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005287 char_u *res;
5288
5289 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005290 // string slice: string is at stack-3, first index at
5291 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005292 if (is_slice)
5293 {
5294 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005295 n1 = tv->vval.v_number;
5296 }
5297
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005298 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005299 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005300
Bram Moolenaar4c137212021-04-19 16:48:48 +02005301 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005302 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005303 if (is_slice)
5304 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005305 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005306 else
5307 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005308 // single character (including composing characters).
5309 // If the index is too big or negative the result is
5310 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005311 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005312 vim_free(tv->vval.v_string);
5313 tv->vval.v_string = res;
5314 }
5315 break;
5316
5317 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005318 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005319 case ISN_BLOBINDEX:
5320 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005321 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005322 int is_slice = iptr->isn_type == ISN_LISTSLICE
5323 || iptr->isn_type == ISN_BLOBSLICE;
5324 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5325 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005326 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005327 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328
5329 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005330 // list slice: list is at stack-3, indexes at stack-2 and
5331 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005332 // Same for blob.
5333 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005334
5335 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005336 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005338
5339 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005340 {
Bram Moolenaared591872020-08-15 22:14:53 +02005341 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005342 n1 = tv->vval.v_number;
5343 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005344 }
Bram Moolenaared591872020-08-15 22:14:53 +02005345
Bram Moolenaar4c137212021-04-19 16:48:48 +02005346 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005347 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005348 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005349 if (is_blob)
5350 {
5351 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5352 n1, n2, FALSE, tv) == FAIL)
5353 goto on_error;
5354 }
5355 else
5356 {
5357 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5358 n1, n2, FALSE, tv, TRUE) == FAIL)
5359 goto on_error;
5360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 }
5362 break;
5363
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005364 case ISN_ANYINDEX:
5365 case ISN_ANYSLICE:
5366 {
5367 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5368 typval_T *var1, *var2;
5369 int res;
5370
5371 // index: composite is at stack-2, index at stack-1
5372 // slice: composite is at stack-3, indexes at stack-2 and
5373 // stack-1
5374 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005375 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005376 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5377 goto on_error;
5378 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5379 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005380 res = eval_index_inner(tv, is_slice, var1, var2,
5381 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005382 clear_tv(var1);
5383 if (is_slice)
5384 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005385 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005386 if (res == FAIL)
5387 goto on_error;
5388 }
5389 break;
5390
Bram Moolenaar9af78762020-06-16 11:34:42 +02005391 case ISN_SLICE:
5392 {
5393 list_T *list;
5394 int count = iptr->isn_arg.number;
5395
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005396 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005397 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005398 list = tv->vval.v_list;
5399
5400 // no error for short list, expect it to be checked earlier
5401 if (list != NULL && list->lv_len >= count)
5402 {
5403 list_T *newlist = list_slice(list,
5404 count, list->lv_len - 1);
5405
5406 if (newlist != NULL)
5407 {
5408 list_unref(list);
5409 tv->vval.v_list = newlist;
5410 ++newlist->lv_refcount;
5411 }
5412 }
5413 }
5414 break;
5415
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005416 case ISN_GETITEM:
5417 {
5418 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005419 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005420
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005421 // Get list item: list is at stack-1, push item.
5422 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005423 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5424 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005425
Bram Moolenaar35578162021-08-02 19:10:38 +02005426 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005427 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005428 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005429 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005430
5431 // Useful when used in unpack assignment. Reset at
5432 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005433 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005434 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005435 }
5436 break;
5437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005438 case ISN_MEMBER:
5439 {
5440 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005441 char_u *key;
5442 dictitem_T *di;
5443
5444 // dict member: dict is at stack-2, key at stack-1
5445 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005446 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005447 dict = tv->vval.v_dict;
5448
5449 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005450 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005451 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005452 if (key == NULL)
5453 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005454
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005455 if ((di = dict_find(dict, key, -1)) == NULL)
5456 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005457 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005458 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005459
5460 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005461 // stack contents makes sense and the dict stack is
5462 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005463 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005464 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005465 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005466 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005467 tv->v_type = VAR_NUMBER;
5468 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005469 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005470 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005471 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005472 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005473 // Put the dict used on the dict stack, it might be used by
5474 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005475 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005476 if (dict_stack_save(tv) == FAIL)
5477 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005478 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005479 }
5480 break;
5481
5482 // dict member with string key
5483 case ISN_STRINGMEMBER:
5484 {
5485 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005486 dictitem_T *di;
5487
5488 tv = STACK_TV_BOT(-1);
5489 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5490 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005491 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005492 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005493 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494 }
5495 dict = tv->vval.v_dict;
5496
5497 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5498 == NULL)
5499 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005500 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005501 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005502 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005503 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005504 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005505 // Put the dict used on the dict stack, it might be used by
5506 // a dict function later.
5507 if (dict_stack_save(tv) == FAIL)
5508 goto on_fatal_error;
5509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005510 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005511 }
5512 break;
5513
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005514 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005515 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005516 {
5517 tv = STACK_TV_BOT(-1);
5518 if (tv->v_type != VAR_OBJECT)
5519 {
5520 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005521 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005522 goto on_error;
5523 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005524
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005525 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005526 if (obj == NULL)
5527 {
5528 SOURCING_LNUM = iptr->isn_lnum;
5529 emsg(_(e_using_null_object));
5530 goto on_error;
5531 }
5532
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005533 int idx;
5534 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005535 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005536 else
5537 {
5538 idx = iptr->isn_arg.classmember.cm_idx;
5539 // convert the interface index to the object index
5540 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005541 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005542 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005543 }
5544
Ernie Rael18143d32023-09-04 22:30:41 +02005545 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005546 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005547 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005548
5549 // Unreference the object after getting the member, it may
5550 // be freed.
5551 object_unref(obj);
5552 }
5553 break;
5554
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005555 case ISN_STORE_THIS:
5556 {
5557 int idx = iptr->isn_arg.number;
5558 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5559 // the members are located right after the object struct
5560 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5561 clear_tv(mtv);
5562 *mtv = *STACK_TV_BOT(-1);
5563 --ectx->ec_stack.ga_len;
5564 }
5565 break;
5566
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005567 case ISN_CLEARDICT:
5568 dict_stack_drop();
5569 break;
5570
5571 case ISN_USEDICT:
5572 {
5573 typval_T *dict_tv = dict_stack_get_tv();
5574
5575 // Turn "dict.Func" into a partial for "Func" bound to
5576 // "dict". Don't do this when "Func" is already a partial
5577 // that was bound explicitly (pt_auto is FALSE).
5578 tv = STACK_TV_BOT(-1);
5579 if (dict_tv != NULL
5580 && dict_tv->v_type == VAR_DICT
5581 && dict_tv->vval.v_dict != NULL
5582 && (tv->v_type == VAR_FUNC
5583 || (tv->v_type == VAR_PARTIAL
5584 && (tv->vval.v_partial->pt_auto
5585 || tv->vval.v_partial->pt_dict == NULL))))
5586 dict_tv->vval.v_dict =
5587 make_partial(dict_tv->vval.v_dict, tv);
5588 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005589 }
5590 break;
5591
5592 case ISN_NEGATENR:
5593 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005594 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005595 if (tv->v_type == VAR_FLOAT)
5596 tv->vval.v_float = -tv->vval.v_float;
5597 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005598 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005599 break;
5600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005601 case ISN_CHECKTYPE:
5602 {
5603 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005604 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005605 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005606
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005607 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005608 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005609 if (ct->ct_arg_idx > 0)
5610 {
5611 where.wt_index = ct->ct_arg_idx;
5612 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005613 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005614 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005615 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005616 if (r == FAIL)
5617 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005618
5619 // number 0 is FALSE, number 1 is TRUE
5620 if (tv->v_type == VAR_NUMBER
5621 && ct->ct_type->tt_type == VAR_BOOL
5622 && (tv->vval.v_number == 0
5623 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005624 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005625 tv->v_type = VAR_BOOL;
5626 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005627 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005628 }
5629 }
5630 break;
5631
Bram Moolenaar9af78762020-06-16 11:34:42 +02005632 case ISN_CHECKLEN:
5633 {
5634 int min_len = iptr->isn_arg.checklen.cl_min_len;
5635 list_T *list = NULL;
5636
5637 tv = STACK_TV_BOT(-1);
5638 if (tv->v_type == VAR_LIST)
5639 list = tv->vval.v_list;
5640 if (list == NULL || list->lv_len < min_len
5641 || (list->lv_len > min_len
5642 && !iptr->isn_arg.checklen.cl_more_OK))
5643 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005644 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005645 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005646 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005647 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005648 }
5649 }
5650 break;
5651
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005652 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005653 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005654 break;
5655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005656 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005657 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658 {
5659 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005660 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005661
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005662 if (iptr->isn_type == ISN_2BOOL)
5663 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005664 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005665 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005666 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005667 n = !n;
5668 }
5669 else
5670 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005671 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005672 SOURCING_LNUM = iptr->isn_lnum;
5673 n = tv_get_bool_chk(tv, &error);
5674 if (error)
5675 goto on_error;
5676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005677 clear_tv(tv);
5678 tv->v_type = VAR_BOOL;
5679 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5680 }
5681 break;
5682
5683 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005684 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005685 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005686 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5687 iptr->isn_type == ISN_2STRING_ANY,
5688 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005689 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005690 break;
5691
Bram Moolenaar08597872020-12-10 19:43:40 +01005692 case ISN_RANGE:
5693 {
5694 exarg_T ea;
5695 char *errormsg;
5696
Bram Moolenaarece0b872021-01-08 20:40:45 +01005697 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005698 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005699 ea.addr_type = ADDR_LINES;
5700 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005701 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005702 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005703 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005704
Bram Moolenaar35578162021-08-02 19:10:38 +02005705 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005706 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005707 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005708 tv = STACK_TV_BOT(-1);
5709 tv->v_type = VAR_NUMBER;
5710 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005711 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005712 }
5713 break;
5714
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005715 case ISN_PUT:
5716 {
5717 int regname = iptr->isn_arg.put.put_regname;
5718 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5719 char_u *expr = NULL;
5720 int dir = FORWARD;
5721
Bram Moolenaar08597872020-12-10 19:43:40 +01005722 if (lnum < -2)
5723 {
5724 // line number was put on the stack by ISN_RANGE
5725 tv = STACK_TV_BOT(-1);
5726 curwin->w_cursor.lnum = tv->vval.v_number;
5727 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5728 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005729 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005730 }
5731 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005732 // :put! above cursor
5733 dir = BACKWARD;
5734 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005735 {
5736 curwin->w_cursor.lnum = lnum;
5737 if (lnum == 0)
5738 // check_cursor() below will move to line 1
5739 dir = BACKWARD;
5740 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005741
5742 if (regname == '=')
5743 {
5744 tv = STACK_TV_BOT(-1);
5745 if (tv->v_type == VAR_STRING)
5746 expr = tv->vval.v_string;
5747 else
5748 {
5749 expr = typval2string(tv, TRUE); // allocates value
5750 clear_tv(tv);
5751 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005752 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005753 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005754 check_cursor();
5755 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5756 vim_free(expr);
5757 }
5758 break;
5759
Bram Moolenaar02194d22020-10-24 23:08:38 +02005760 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005761 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5762 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5763 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5764 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005765 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5766 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005767 break;
5768
Bram Moolenaar02194d22020-10-24 23:08:38 +02005769 case ISN_CMDMOD_REV:
5770 // filter regprog is owned by the instruction, don't free it
5771 cmdmod.cmod_filter_regmatch.regprog = NULL;
5772 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005773 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5774 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005775 break;
5776
Bram Moolenaar792f7862020-11-23 08:31:18 +01005777 case ISN_UNPACK:
5778 {
5779 int count = iptr->isn_arg.unpack.unp_count;
5780 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5781 list_T *l;
5782 listitem_T *li;
5783 int i;
5784
5785 // Check there is a valid list to unpack.
5786 tv = STACK_TV_BOT(-1);
5787 if (tv->v_type != VAR_LIST)
5788 {
5789 SOURCING_LNUM = iptr->isn_lnum;
5790 emsg(_(e_for_argument_must_be_sequence_of_lists));
5791 goto on_error;
5792 }
5793 l = tv->vval.v_list;
5794 if (l == NULL
5795 || l->lv_len < (semicolon ? count - 1 : count))
5796 {
5797 SOURCING_LNUM = iptr->isn_lnum;
5798 emsg(_(e_list_value_does_not_have_enough_items));
5799 goto on_error;
5800 }
5801 else if (!semicolon && l->lv_len > count)
5802 {
5803 SOURCING_LNUM = iptr->isn_lnum;
5804 emsg(_(e_list_value_has_more_items_than_targets));
5805 goto on_error;
5806 }
5807
5808 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005809 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005810 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005811 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005812
5813 // Variable after semicolon gets a list with the remaining
5814 // items.
5815 if (semicolon)
5816 {
5817 list_T *rem_list =
5818 list_alloc_with_items(l->lv_len - count + 1);
5819
5820 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005821 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005822 tv = STACK_TV_BOT(-count);
5823 tv->vval.v_list = rem_list;
5824 ++rem_list->lv_refcount;
5825 tv->v_lock = 0;
5826 li = l->lv_first;
5827 for (i = 0; i < count - 1; ++i)
5828 li = li->li_next;
5829 for (i = 0; li != NULL; ++i)
5830 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005831 typval_T tvcopy;
5832
5833 copy_tv(&li->li_tv, &tvcopy);
5834 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005835 li = li->li_next;
5836 }
5837 --count;
5838 }
5839
5840 // Produce the values in reverse order, first item last.
5841 li = l->lv_first;
5842 for (i = 0; i < count; ++i)
5843 {
5844 tv = STACK_TV_BOT(-i - 1);
5845 copy_tv(&li->li_tv, tv);
5846 li = li->li_next;
5847 }
5848
5849 list_unref(l);
5850 }
5851 break;
5852
Bram Moolenaarb2049902021-01-24 12:53:53 +01005853 case ISN_PROF_START:
5854 case ISN_PROF_END:
5855 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005856#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005857 funccall_T cookie;
5858 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005859 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005860 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005861
Bram Moolenaarca16c602022-09-06 18:57:08 +01005862 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005863 if (iptr->isn_type == ISN_PROF_START)
5864 {
5865 func_line_start(&cookie, iptr->isn_lnum);
5866 // if we get here the instruction is executed
5867 func_line_exec(&cookie);
5868 }
5869 else
5870 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005871#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005872 }
5873 break;
5874
Bram Moolenaare99d4222021-06-13 14:01:26 +02005875 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005876 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005877 break;
5878
Bram Moolenaar389df252020-07-09 21:20:47 +02005879 case ISN_SHUFFLE:
5880 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005881 typval_T tmp_tv;
5882 int item = iptr->isn_arg.shuffle.shfl_item;
5883 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005884
5885 tmp_tv = *STACK_TV_BOT(-item);
5886 for ( ; up > 0 && item > 1; --up)
5887 {
5888 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5889 --item;
5890 }
5891 *STACK_TV_BOT(-item) = tmp_tv;
5892 }
5893 break;
5894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005896 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005898 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005899 break;
5900 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005901 continue;
5902
Bram Moolenaard032f342020-07-18 18:13:02 +02005903func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005904 // Restore previous function. If the frame pointer is where we started
5905 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005906 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005907 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005908
Bram Moolenaar4c137212021-04-19 16:48:48 +02005909 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005910 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005911 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005912 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005913
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005914on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005915 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005916 // If "emsg_silent" is set then ignore the error, unless it was set
5917 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005918 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005919 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005920 {
5921 // If a sequence of instructions causes an error while ":silent!"
5922 // was used, restore the stack length and jump ahead to restoring
5923 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005924 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005925 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005926 while (ectx->ec_stack.ga_len
5927 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005928 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005929 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005930 clear_tv(STACK_TV_BOT(0));
5931 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005932 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5933 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005934 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005935 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005936 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005937on_fatal_error:
5938 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005939 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005940 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005941 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942 }
5943
5944done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005945 ret = OK;
5946theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005947 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005948
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005949 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005950 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5951 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005952}
5953
5954/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005955 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005956 * "rettv".
5957 * Return OK or FAIL.
5958 */
5959 int
5960exe_typval_instr(typval_T *tv, typval_T *rettv)
5961{
5962 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5963 isn_T *save_instr = ectx->ec_instr;
5964 int save_iidx = ectx->ec_iidx;
5965 int res;
5966
LemonBoyf3b48952022-05-05 13:53:03 +01005967 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5968 // even when the compilation fails.
5969 rettv->v_type = VAR_UNKNOWN;
5970
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005971 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5972 res = exec_instructions(ectx);
5973 if (res == OK)
5974 {
5975 *rettv = *STACK_TV_BOT(-1);
5976 --ectx->ec_stack.ga_len;
5977 }
5978
5979 ectx->ec_instr = save_instr;
5980 ectx->ec_iidx = save_iidx;
5981
5982 return res;
5983}
5984
5985/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005986 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5987 * "substitute_instr".
5988 */
5989 char_u *
5990exe_substitute_instr(void)
5991{
5992 ectx_T *ectx = substitute_instr->subs_ectx;
5993 isn_T *save_instr = ectx->ec_instr;
5994 int save_iidx = ectx->ec_iidx;
5995 char_u *res;
5996
5997 ectx->ec_instr = substitute_instr->subs_instr;
5998 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005999 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006000 typval_T *tv = STACK_TV_BOT(-1);
6001
Bram Moolenaar27523602021-06-05 21:36:19 +02006002 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006003 --ectx->ec_stack.ga_len;
6004 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02006005 }
6006 else
6007 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006008 substitute_instr->subs_status = FAIL;
6009 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02006010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011
Bram Moolenaar4c137212021-04-19 16:48:48 +02006012 ectx->ec_instr = save_instr;
6013 ectx->ec_iidx = save_iidx;
6014
6015 return res;
6016}
6017
6018/*
6019 * Call a "def" function from old Vim script.
6020 * Return OK or FAIL.
6021 */
6022 int
6023call_def_function(
6024 ufunc_T *ufunc,
6025 int argc_arg, // nr of arguments
6026 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006027 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02006028 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006029 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01006030 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006031 typval_T *rettv) // return value
6032{
6033 ectx_T ectx; // execution context
6034 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006035 int partial_argc = partial == NULL
6036 || (flags & DEF_USE_PT_ARGV) == 0
6037 ? 0 : partial->pt_argc;
6038 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006039 typval_T *tv;
6040 int idx;
6041 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006042 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006043 sctx_T save_current_sctx = current_sctx;
6044 int did_emsg_before = did_emsg_cumul + did_emsg;
6045 int save_suppress_errthrow = suppress_errthrow;
6046 msglist_T **saved_msg_list = NULL;
6047 msglist_T *private_msg_list = NULL;
6048 int save_emsg_silent_def = emsg_silent_def;
6049 int save_did_emsg_def = did_emsg_def;
6050 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006051 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006052
6053// Get pointer to item in the stack.
6054#undef STACK_TV
6055#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6056
6057// Get pointer to item at the bottom of the stack, -1 is the bottom.
6058#undef STACK_TV_BOT
6059#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6060
6061// Get pointer to a local variable on the stack. Negative for arguments.
6062#undef STACK_TV_VAR
6063#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6064
6065 if (ufunc->uf_def_status == UF_NOT_COMPILED
6066 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006067 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6068 && compile_def_function(ufunc, FALSE,
6069 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006070 {
6071 if (did_emsg_cumul + did_emsg == did_emsg_before)
6072 semsg(_(e_function_is_not_compiled_str),
6073 printable_func_name(ufunc));
6074 return FAIL;
6075 }
6076
6077 {
6078 // Check the function was really compiled.
6079 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6080 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006081 if (dfunc->df_ufunc == NULL)
6082 {
6083 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6084 return FAIL;
6085 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006086 if (INSTRUCTIONS(dfunc) == NULL)
6087 {
6088 iemsg("using call_def_function() on not compiled function");
6089 return FAIL;
6090 }
6091 }
6092
6093 // If depth of calling is getting too high, don't execute the function.
6094 orig_funcdepth = funcdepth_get();
6095 if (funcdepth_increment() == FAIL)
6096 return FAIL;
6097
6098 CLEAR_FIELD(ectx);
6099 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6100 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006101 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006102 {
6103 funcdepth_decrement();
6104 return FAIL;
6105 }
6106 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6107 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6108 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006109 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006110
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006111 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006112 if (idx > 0 && ufunc->uf_va_name == NULL)
6113 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006114 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006115 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006116 goto failed_early;
6117 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006118 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006119 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006120 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006121 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006122 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006123 goto failed_early;
6124 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006125
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006126 // Put values from the partial and arguments on the stack, but no more than
6127 // what the function expects. A lambda can be called with more arguments
6128 // than it uses.
6129 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006130 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6131 ++idx)
6132 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006133 int argv_idx = idx - partial_argc;
6134
6135 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006136 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006137 && tv->v_type == VAR_SPECIAL
6138 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006139 {
6140 // Use the default value.
6141 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6142 }
6143 else
6144 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006145 int done = FALSE;
6146 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6147 {
6148 type_T *expected = ufunc->uf_arg_types[idx];
6149 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6150 {
6151 // When a float is expected and a number was given, convert
6152 // the value.
6153 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6154 STACK_TV_BOT(0)->v_lock = 0;
6155 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6156 done = TRUE;
6157 }
6158 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006159 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006160 goto failed_early;
6161 }
6162 if (!done)
6163 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006164 }
6165 ++ectx.ec_stack.ga_len;
6166 }
6167
6168 // Turn varargs into a list. Empty list if no args.
6169 if (ufunc->uf_va_name != NULL)
6170 {
6171 int vararg_count = argc - ufunc->uf_args.ga_len;
6172
6173 if (vararg_count < 0)
6174 vararg_count = 0;
6175 else
6176 argc -= vararg_count;
6177 if (exe_newlist(vararg_count, &ectx) == FAIL)
6178 goto failed_early;
6179
6180 // Check the type of the list items.
6181 tv = STACK_TV_BOT(-1);
6182 if (ufunc->uf_va_type != NULL
6183 && ufunc->uf_va_type != &t_list_any
6184 && ufunc->uf_va_type->tt_member != &t_any
6185 && tv->vval.v_list != NULL)
6186 {
6187 type_T *expected = ufunc->uf_va_type->tt_member;
6188 listitem_T *li = tv->vval.v_list->lv_first;
6189
6190 for (idx = 0; idx < vararg_count; ++idx)
6191 {
6192 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006193 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006194 goto failed_early;
6195 li = li->li_next;
6196 }
6197 }
6198
6199 if (defcount > 0)
6200 // Move varargs list to below missing default arguments.
6201 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6202 --ectx.ec_stack.ga_len;
6203 }
6204
6205 // Make space for omitted arguments, will store default value below.
6206 // Any varargs list goes after them.
6207 if (defcount > 0)
6208 for (idx = 0; idx < defcount; ++idx)
6209 {
6210 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6211 ++ectx.ec_stack.ga_len;
6212 }
6213 if (ufunc->uf_va_name != NULL)
6214 ++ectx.ec_stack.ga_len;
6215
6216 // Frame pointer points to just after arguments.
6217 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6218 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6219
6220 {
6221 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6222 + ufunc->uf_dfunc_idx;
6223 ufunc_T *base_ufunc = dfunc->df_ufunc;
6224
6225 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006226 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006227 if (partial != NULL || base_ufunc->uf_partial != NULL)
6228 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006229 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6230 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006231 goto failed_early;
6232 if (partial != NULL)
6233 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006234 outer_T *outer = get_pt_outer(partial);
6235
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006236 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006237 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006238 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006239 if (current_ectx != NULL)
6240 {
6241 if (current_ectx->ec_outer_ref != NULL
6242 && current_ectx->ec_outer_ref->or_outer != NULL)
6243 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006244 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006245 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006246 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006247 }
6248 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006249 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006250 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006251 ++partial->pt_refcount;
6252 ectx.ec_outer_ref->or_partial = partial;
6253 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006254 }
6255 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006256 {
6257 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6258 ++base_ufunc->uf_partial->pt_refcount;
6259 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6260 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006261 }
6262 }
6263
6264 // dummy frame entries
6265 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6266 {
6267 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6268 ++ectx.ec_stack.ga_len;
6269 }
6270
6271 {
6272 // Reserve space for local variables and any closure reference count.
6273 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6274 + ufunc->uf_dfunc_idx;
6275
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006276 // Initialize variables to zero. That avoids having to generate
6277 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006278 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006279 {
6280 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6281 STACK_TV_VAR(idx)->vval.v_number = 0;
6282 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006283 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006284
6285 if (object != NULL)
6286 {
6287 // the object is always the variable at index zero
6288 tv = STACK_TV_VAR(0);
6289 tv->v_type = VAR_OBJECT;
6290 tv->vval.v_object = object;
6291 }
6292
Bram Moolenaar4c137212021-04-19 16:48:48 +02006293 if (dfunc->df_has_closure)
6294 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006295 // Initialize the variable that counts how many closures were
6296 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006297 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6298 STACK_TV_VAR(idx)->vval.v_number = 0;
6299 ++ectx.ec_stack.ga_len;
6300 }
6301
6302 ectx.ec_instr = INSTRUCTIONS(dfunc);
6303 }
6304
Bram Moolenaar58779852022-09-06 18:31:14 +01006305 // Store the execution context in funccal, used by invoke_all_defer().
6306 if (funccal != NULL)
6307 funccal->fc_ectx = &ectx;
6308
Bram Moolenaar4c137212021-04-19 16:48:48 +02006309 // Following errors are in the function, not the caller.
6310 // Commands behave like vim9script.
6311 estack_push_ufunc(ufunc, 1);
6312 current_sctx = ufunc->uf_script_ctx;
6313 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6314
6315 // Use a specific location for storing error messages to be converted to an
6316 // exception.
6317 saved_msg_list = msg_list;
6318 msg_list = &private_msg_list;
6319
6320 // Do turn errors into exceptions.
6321 suppress_errthrow = FALSE;
6322
6323 // Do not delete the function while executing it.
6324 ++ufunc->uf_calls;
6325
6326 // When ":silent!" was used before calling then we still abort the
6327 // function. If ":silent!" is used in the function then we don't.
6328 emsg_silent_def = emsg_silent;
6329 did_emsg_def = 0;
6330
LemonBoyc5d27442023-08-19 13:02:35 +02006331 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006332
Bram Moolenaar5800c792022-09-22 17:34:01 +01006333 /*
6334 * Execute the instructions until done.
6335 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006336 ret = exec_instructions(&ectx);
6337 if (ret == OK)
6338 {
6339 // function finished, get result from the stack.
6340 if (ufunc->uf_ret_type == &t_void)
6341 {
6342 rettv->v_type = VAR_VOID;
6343 }
6344 else
6345 {
6346 tv = STACK_TV_BOT(-1);
6347 *rettv = *tv;
6348 tv->v_type = VAR_UNKNOWN;
6349 }
6350 }
6351
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006352 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006353 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006354
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006355 // Deal with any remaining closures, they may be in use somewhere.
6356 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006357 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006358 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006359 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006360 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006361
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006362 estack_pop();
6363 current_sctx = save_current_sctx;
6364
Bram Moolenaar2336c372021-12-06 15:06:54 +00006365 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6366 // Function was unreferenced while being used, free it now.
6367 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006368
Bram Moolenaar352134b2020-10-17 22:04:08 +02006369 if (*msg_list != NULL && saved_msg_list != NULL)
6370 {
6371 msglist_T **plist = saved_msg_list;
6372
6373 // Append entries from the current msg_list (uncaught exceptions) to
6374 // the saved msg_list.
6375 while (*plist != NULL)
6376 plist = &(*plist)->next;
6377
6378 *plist = *msg_list;
6379 }
6380 msg_list = saved_msg_list;
6381
Bram Moolenaar4c137212021-04-19 16:48:48 +02006382 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006383 {
6384 cmdmod.cmod_filter_regmatch.regprog = NULL;
6385 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006386 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006387 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006388 emsg_silent_def = save_emsg_silent_def;
6389 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006390
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006391failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006392 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006393 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006394 {
6395 tv = STACK_TV(idx);
6396 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6397 clear_tv(tv);
6398 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006399 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006402 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006403 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006404 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006405 if (ectx.ec_outer_ref->or_outer_allocated)
6406 vim_free(ectx.ec_outer_ref->or_outer);
6407 partial_unref(ectx.ec_outer_ref->or_partial);
6408 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006409 }
6410
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006411 // Not sure if this is necessary.
6412 suppress_errthrow = save_suppress_errthrow;
6413
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006414 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6415 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006416 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006417 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006418 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419 return ret;
6420}
6421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006422/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006423 * Called when a def function has finished (possibly failed).
6424 * Invoke all the function returns to clean up and invoke deferred functions,
6425 * except the toplevel one.
6426 */
6427 void
6428unwind_def_callstack(ectx_T *ectx)
6429{
6430 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6431 func_return(ectx);
6432}
6433
6434/*
dundargocc57b5bc2022-11-02 13:30:51 +00006435 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006436 */
6437 void
6438may_invoke_defer_funcs(ectx_T *ectx)
6439{
6440 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6441
6442 if (dfunc->df_defer_var_idx > 0)
6443 invoke_defer_funcs(ectx);
6444}
6445
6446/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006447 * Return loopvarinfo in a printable form in allocated memory.
6448 */
6449 static char_u *
6450printable_loopvarinfo(loopvarinfo_T *lvi)
6451{
6452 garray_T ga;
6453 int depth;
6454
6455 ga_init2(&ga, 1, 100);
6456 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6457 {
6458 if (ga_grow(&ga, 50) == FAIL)
6459 break;
6460 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006461 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006462 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006463 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006464 lvi->lvi_loop[depth].var_idx,
6465 lvi->lvi_loop[depth].var_idx
6466 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006467 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006468 }
6469 return ga.ga_data;
6470}
6471
6472/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006473 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6474 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6475 * "pfx" is prefixed to every line.
6476 */
6477 static void
6478list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6479{
6480 int line_idx = 0;
6481 int prev_current = 0;
6482 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006483 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006484
6485 for (current = 0; current < instr_count; ++current)
6486 {
6487 isn_T *iptr = &instr[current];
6488 char *line;
6489
6490 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006491 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006492 while (line_idx < iptr->isn_lnum
6493 && line_idx < ufunc->uf_lines.ga_len)
6494 {
6495 if (current > prev_current)
6496 {
6497 msg_puts("\n\n");
6498 prev_current = current;
6499 }
6500 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6501 if (line != NULL)
6502 msg(line);
6503 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006504 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6505 {
6506 int first_def_arg = ufunc->uf_args.ga_len
6507 - ufunc->uf_def_args.ga_len;
6508
6509 if (def_arg_idx > 0)
6510 msg_puts("\n\n");
6511 msg_start();
6512 msg_puts(" ");
6513 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6514 first_def_arg + def_arg_idx]);
6515 msg_puts(" = ");
6516 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6517 msg_clr_eos();
6518 msg_end();
6519 }
6520 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006521
6522 switch (iptr->isn_type)
6523 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006524 case ISN_CONSTRUCT:
6525 smsg("%s%4d NEW %s size %d", pfx, current,
6526 iptr->isn_arg.construct.construct_class->class_name,
6527 (int)iptr->isn_arg.construct.construct_size);
6528 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006529 case ISN_EXEC:
6530 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6531 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006532 case ISN_EXEC_SPLIT:
6533 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6534 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006535 case ISN_EXECRANGE:
6536 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6537 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006538 case ISN_LEGACY_EVAL:
6539 smsg("%s%4d EVAL legacy %s", pfx, current,
6540 iptr->isn_arg.string);
6541 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006542 case ISN_REDIRSTART:
6543 smsg("%s%4d REDIR", pfx, current);
6544 break;
6545 case ISN_REDIREND:
6546 smsg("%s%4d REDIR END%s", pfx, current,
6547 iptr->isn_arg.number ? " append" : "");
6548 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006549 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006550#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006551 smsg("%s%4d CEXPR pre %s", pfx, current,
6552 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006553#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006554 break;
6555 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006556#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006557 {
6558 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6559
6560 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6561 cexpr_get_auname(cer->cer_cmdidx),
6562 cer->cer_forceit ? "!" : "",
6563 cer->cer_cmdline);
6564 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006565#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006566 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006567 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006568 smsg("%s%4d INSTR", pfx, current);
6569 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6570 msg(" -------------");
6571 break;
6572 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006573 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006574 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6575
6576 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006577 }
6578 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006579 case ISN_SUBSTITUTE:
6580 {
6581 subs_T *subs = &iptr->isn_arg.subs;
6582
6583 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6584 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6585 msg(" -------------");
6586 }
6587 break;
6588 case ISN_EXECCONCAT:
6589 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6590 (varnumber_T)iptr->isn_arg.number);
6591 break;
6592 case ISN_ECHO:
6593 {
6594 echo_T *echo = &iptr->isn_arg.echo;
6595
6596 smsg("%s%4d %s %d", pfx, current,
6597 echo->echo_with_white ? "ECHO" : "ECHON",
6598 echo->echo_count);
6599 }
6600 break;
6601 case ISN_EXECUTE:
6602 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006603 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006604 break;
6605 case ISN_ECHOMSG:
6606 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006607 (varnumber_T)(iptr->isn_arg.number));
6608 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006609 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006610 if (iptr->isn_arg.echowin.ewin_time > 0)
6611 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6612 iptr->isn_arg.echowin.ewin_count,
6613 iptr->isn_arg.echowin.ewin_time);
6614 else
6615 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6616 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006617 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006618 case ISN_ECHOCONSOLE:
6619 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6620 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006621 break;
6622 case ISN_ECHOERR:
6623 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006624 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006625 break;
6626 case ISN_LOAD:
6627 {
6628 if (iptr->isn_arg.number < 0)
6629 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6630 (varnumber_T)(iptr->isn_arg.number
6631 + STACK_FRAME_SIZE));
6632 else
6633 smsg("%s%4d LOAD $%lld", pfx, current,
6634 (varnumber_T)(iptr->isn_arg.number));
6635 }
6636 break;
6637 case ISN_LOADOUTER:
6638 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006639 isn_outer_T *outer = &iptr->isn_arg.outer;
6640
6641 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006642 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006643 outer->outer_depth,
6644 outer->outer_idx + STACK_FRAME_SIZE);
6645 else if (outer->outer_depth < 0)
6646 smsg("%s%4d LOADOUTER $%d in loop level %d",
6647 pfx, current,
6648 outer->outer_idx,
6649 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006650 else
6651 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006652 outer->outer_depth,
6653 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006654 }
6655 break;
6656 case ISN_LOADV:
6657 smsg("%s%4d LOADV v:%s", pfx, current,
6658 get_vim_var_name(iptr->isn_arg.number));
6659 break;
6660 case ISN_LOADSCRIPT:
6661 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006662 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6663 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6664 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006665
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006666 sv = get_script_svar(sref, -1);
6667 if (sv == NULL)
6668 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6669 pfx, current, si->sn_name);
6670 else
6671 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006672 sv->sv_name,
6673 sref->sref_idx,
6674 si->sn_name);
6675 }
6676 break;
6677 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006678 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006679 {
6680 scriptitem_T *si = SCRIPT_ITEM(
6681 iptr->isn_arg.loadstore.ls_sid);
6682
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006683 smsg("%s%4d %s s:%s from %s", pfx, current,
6684 iptr->isn_type == ISN_LOADS ? "LOADS"
6685 : "LOADEXPORT",
6686 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006687 }
6688 break;
6689 case ISN_LOADAUTO:
6690 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6691 break;
6692 case ISN_LOADG:
6693 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6694 break;
6695 case ISN_LOADB:
6696 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6697 break;
6698 case ISN_LOADW:
6699 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6700 break;
6701 case ISN_LOADT:
6702 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6703 break;
6704 case ISN_LOADGDICT:
6705 smsg("%s%4d LOAD g:", pfx, current);
6706 break;
6707 case ISN_LOADBDICT:
6708 smsg("%s%4d LOAD b:", pfx, current);
6709 break;
6710 case ISN_LOADWDICT:
6711 smsg("%s%4d LOAD w:", pfx, current);
6712 break;
6713 case ISN_LOADTDICT:
6714 smsg("%s%4d LOAD t:", pfx, current);
6715 break;
6716 case ISN_LOADOPT:
6717 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6718 break;
6719 case ISN_LOADENV:
6720 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6721 break;
6722 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006723 smsg("%s%4d LOADREG @%c", pfx, current,
6724 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006725 break;
6726
6727 case ISN_STORE:
6728 if (iptr->isn_arg.number < 0)
6729 smsg("%s%4d STORE arg[%lld]", pfx, current,
6730 iptr->isn_arg.number + STACK_FRAME_SIZE);
6731 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006732 smsg("%s%4d STORE $%lld", pfx, current,
6733 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006734 break;
6735 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006736 {
6737 isn_outer_T *outer = &iptr->isn_arg.outer;
6738
6739 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6740 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6741 pfx, current, outer->outer_idx);
6742 else
6743 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6744 outer->outer_depth, outer->outer_idx);
6745 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006746 break;
6747 case ISN_STOREV:
6748 smsg("%s%4d STOREV v:%s", pfx, current,
6749 get_vim_var_name(iptr->isn_arg.number));
6750 break;
6751 case ISN_STOREAUTO:
6752 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6753 break;
6754 case ISN_STOREG:
6755 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6756 break;
6757 case ISN_STOREB:
6758 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6759 break;
6760 case ISN_STOREW:
6761 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6762 break;
6763 case ISN_STORET:
6764 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6765 break;
6766 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006767 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006768 {
6769 scriptitem_T *si = SCRIPT_ITEM(
6770 iptr->isn_arg.loadstore.ls_sid);
6771
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006772 smsg("%s%4d %s %s in %s", pfx, current,
6773 iptr->isn_type == ISN_STORES
6774 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006775 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6776 }
6777 break;
6778 case ISN_STORESCRIPT:
6779 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006780 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6781 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6782 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006783
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006784 sv = get_script_svar(sref, -1);
6785 if (sv == NULL)
6786 smsg("%s%4d STORESCRIPT [deleted] in %s",
6787 pfx, current, si->sn_name);
6788 else
6789 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006790 sv->sv_name,
6791 sref->sref_idx,
6792 si->sn_name);
6793 }
6794 break;
6795 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006796 case ISN_STOREFUNCOPT:
6797 smsg("%s%4d %s &%s", pfx, current,
6798 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006799 iptr->isn_arg.storeopt.so_name);
6800 break;
6801 case ISN_STOREENV:
6802 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6803 break;
6804 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006805 smsg("%s%4d STOREREG @%c", pfx, current,
6806 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006807 break;
6808 case ISN_STORENR:
6809 smsg("%s%4d STORE %lld in $%d", pfx, current,
6810 iptr->isn_arg.storenr.stnr_val,
6811 iptr->isn_arg.storenr.stnr_idx);
6812 break;
6813
6814 case ISN_STOREINDEX:
6815 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006816 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006817 break;
6818
6819 case ISN_STORERANGE:
6820 smsg("%s%4d STORERANGE", pfx, current);
6821 break;
6822
Bram Moolenaard505d172022-12-18 21:42:55 +00006823 case ISN_LOAD_CLASSMEMBER:
6824 case ISN_STORE_CLASSMEMBER:
6825 {
6826 class_T *cl = iptr->isn_arg.classmember.cm_class;
6827 int idx = iptr->isn_arg.classmember.cm_idx;
6828 ocmember_T *ocm = &cl->class_class_members[idx];
6829 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6830 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6831 ? "LOAD" : "STORE",
6832 cl->class_name, ocm->ocm_name);
6833 }
6834 break;
6835
Bram Moolenaar4c137212021-04-19 16:48:48 +02006836 // constants
6837 case ISN_PUSHNR:
6838 smsg("%s%4d PUSHNR %lld", pfx, current,
6839 (varnumber_T)(iptr->isn_arg.number));
6840 break;
6841 case ISN_PUSHBOOL:
6842 case ISN_PUSHSPEC:
6843 smsg("%s%4d PUSH %s", pfx, current,
6844 get_var_special_name(iptr->isn_arg.number));
6845 break;
6846 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006847 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006848 break;
6849 case ISN_PUSHS:
6850 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6851 break;
6852 case ISN_PUSHBLOB:
6853 {
6854 char_u *r;
6855 char_u numbuf[NUMBUFLEN];
6856 char_u *tofree;
6857
6858 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6859 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6860 vim_free(tofree);
6861 }
6862 break;
6863 case ISN_PUSHFUNC:
6864 {
6865 char *name = (char *)iptr->isn_arg.string;
6866
6867 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6868 name == NULL ? "[none]" : name);
6869 }
6870 break;
6871 case ISN_PUSHCHANNEL:
6872#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006873 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006874#endif
6875 break;
6876 case ISN_PUSHJOB:
6877#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006878 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006879#endif
6880 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006881 case ISN_PUSHOBJ:
6882 smsg("%s%4d PUSHOBJ null", pfx, current);
6883 break;
6884 case ISN_PUSHCLASS:
6885 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006886 iptr->isn_arg.classarg == NULL ? "null"
6887 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006888 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006889 case ISN_PUSHEXC:
6890 smsg("%s%4d PUSH v:exception", pfx, current);
6891 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006892 case ISN_AUTOLOAD:
6893 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6894 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006895 case ISN_UNLET:
6896 smsg("%s%4d UNLET%s %s", pfx, current,
6897 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6898 iptr->isn_arg.unlet.ul_name);
6899 break;
6900 case ISN_UNLETENV:
6901 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6902 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6903 iptr->isn_arg.unlet.ul_name);
6904 break;
6905 case ISN_UNLETINDEX:
6906 smsg("%s%4d UNLETINDEX", pfx, current);
6907 break;
6908 case ISN_UNLETRANGE:
6909 smsg("%s%4d UNLETRANGE", pfx, current);
6910 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006911 case ISN_LOCKUNLOCK:
6912 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6913 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006914 case ISN_LOCKCONST:
6915 smsg("%s%4d LOCKCONST", pfx, current);
6916 break;
6917 case ISN_NEWLIST:
6918 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006919 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006920 break;
6921 case ISN_NEWDICT:
6922 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006923 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006924 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006925 case ISN_NEWPARTIAL:
6926 smsg("%s%4d NEWPARTIAL", pfx, current);
6927 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006928
6929 // function call
6930 case ISN_BCALL:
6931 {
6932 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6933
6934 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6935 internal_func_name(cbfunc->cbf_idx),
6936 cbfunc->cbf_argcount);
6937 }
6938 break;
6939 case ISN_DCALL:
6940 {
6941 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6942 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6943 + cdfunc->cdf_idx;
6944
6945 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006946 printable_func_name(df->df_ufunc),
6947 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006948 }
6949 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006950 case ISN_METHODCALL:
6951 {
6952 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6953
6954 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6955 mfunc->cmf_itf->class_name,
6956 mfunc->cmf_itf->class_obj_methods[
6957 mfunc->cmf_idx]->uf_name,
6958 mfunc->cmf_argcount);
6959 }
6960 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006961 case ISN_UCALL:
6962 {
6963 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6964
6965 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6966 cufunc->cuf_name, cufunc->cuf_argcount);
6967 }
6968 break;
6969 case ISN_PCALL:
6970 {
6971 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6972
6973 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6974 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6975 }
6976 break;
6977 case ISN_PCALL_END:
6978 smsg("%s%4d PCALL end", pfx, current);
6979 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006980 case ISN_DEFER:
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02006981 smsg("%s%4d DEFER %d args", pfx, current,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006982 (int)iptr->isn_arg.defer.defer_argcount);
6983 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006984 case ISN_RETURN:
6985 smsg("%s%4d RETURN", pfx, current);
6986 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006987 case ISN_RETURN_VOID:
6988 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006989 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006990 case ISN_RETURN_OBJECT:
6991 smsg("%s%4d RETURN object", pfx, current);
6992 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006993 case ISN_FUNCREF:
6994 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006995 funcref_T *funcref = &iptr->isn_arg.funcref;
6996 funcref_extra_T *extra = funcref->fr_extra;
6997 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006998
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006999 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00007000 {
7001 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
7002 + funcref->fr_dfunc_idx;
7003 name = df->df_ufunc->uf_name;
7004 }
7005 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007006 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00007007 if (extra != NULL && extra->fre_class != NULL)
7008 {
7009 smsg("%s%4d FUNCREF %s.%s", pfx, current,
7010 extra->fre_class->class_name, name);
7011 }
7012 else if (extra == NULL
7013 || extra->fre_loopvar_info.lvi_depth == 0)
7014 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007015 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00007016 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007017 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007018 {
7019 char_u *info = printable_loopvarinfo(
7020 &extra->fre_loopvar_info);
7021
7022 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
7023 name, info);
7024 vim_free(info);
7025 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007026 }
7027 break;
7028
7029 case ISN_NEWFUNC:
7030 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01007031 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007032
Bram Moolenaarcc341812022-09-19 15:54:34 +01007033 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007034 smsg("%s%4d NEWFUNC %s %s", pfx, current,
7035 arg->nfa_lambda, arg->nfa_global);
7036 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007037 {
7038 char_u *info = printable_loopvarinfo(
7039 &arg->nfa_loopvar_info);
7040
7041 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
7042 arg->nfa_lambda, arg->nfa_global, info);
7043 vim_free(info);
7044 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007045 }
7046 break;
7047
7048 case ISN_DEF:
7049 {
7050 char_u *name = iptr->isn_arg.string;
7051
7052 smsg("%s%4d DEF %s", pfx, current,
7053 name == NULL ? (char_u *)"" : name);
7054 }
7055 break;
7056
7057 case ISN_JUMP:
7058 {
7059 char *when = "?";
7060
7061 switch (iptr->isn_arg.jump.jump_when)
7062 {
7063 case JUMP_ALWAYS:
7064 when = "JUMP";
7065 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007066 case JUMP_NEVER:
7067 iemsg("JUMP_NEVER should not be used");
7068 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007069 case JUMP_AND_KEEP_IF_TRUE:
7070 when = "JUMP_AND_KEEP_IF_TRUE";
7071 break;
7072 case JUMP_IF_FALSE:
7073 when = "JUMP_IF_FALSE";
7074 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007075 case JUMP_WHILE_FALSE:
7076 when = "JUMP_WHILE_FALSE"; // unused
7077 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007078 case JUMP_IF_COND_FALSE:
7079 when = "JUMP_IF_COND_FALSE";
7080 break;
7081 case JUMP_IF_COND_TRUE:
7082 when = "JUMP_IF_COND_TRUE";
7083 break;
7084 }
7085 smsg("%s%4d %s -> %d", pfx, current, when,
7086 iptr->isn_arg.jump.jump_where);
7087 }
7088 break;
7089
7090 case ISN_JUMP_IF_ARG_SET:
7091 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7092 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7093 iptr->isn_arg.jump.jump_where);
7094 break;
7095
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007096 case ISN_JUMP_IF_ARG_NOT_SET:
7097 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7098 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7099 iptr->isn_arg.jump.jump_where);
7100 break;
7101
Bram Moolenaar4c137212021-04-19 16:48:48 +02007102 case ISN_FOR:
7103 {
7104 forloop_T *forloop = &iptr->isn_arg.forloop;
7105
7106 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007107 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007108 }
7109 break;
7110
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007111 case ISN_ENDLOOP:
7112 {
7113 endloop_T *endloop = &iptr->isn_arg.endloop;
7114
Bram Moolenaarcc341812022-09-19 15:54:34 +01007115 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7116 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007117 endloop->end_funcref_idx,
7118 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007119 endloop->end_var_idx + endloop->end_var_count - 1,
7120 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007121 }
7122 break;
7123
7124 case ISN_WHILE:
7125 {
7126 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7127
7128 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7129 whileloop->while_funcref_idx,
7130 whileloop->while_end);
7131 }
7132 break;
7133
Bram Moolenaar4c137212021-04-19 16:48:48 +02007134 case ISN_TRY:
7135 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007136 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007137
7138 if (try->try_ref->try_finally == 0)
7139 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7140 pfx, current,
7141 try->try_ref->try_catch,
7142 try->try_ref->try_endtry);
7143 else
7144 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7145 pfx, current,
7146 try->try_ref->try_catch,
7147 try->try_ref->try_finally,
7148 try->try_ref->try_endtry);
7149 }
7150 break;
7151 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007152 smsg("%s%4d CATCH", pfx, current);
7153 break;
7154 case ISN_TRYCONT:
7155 {
7156 trycont_T *trycont = &iptr->isn_arg.trycont;
7157
7158 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7159 trycont->tct_levels,
7160 trycont->tct_levels == 1 ? "" : "s",
7161 trycont->tct_where);
7162 }
7163 break;
7164 case ISN_FINALLY:
7165 smsg("%s%4d FINALLY", pfx, current);
7166 break;
7167 case ISN_ENDTRY:
7168 smsg("%s%4d ENDTRY", pfx, current);
7169 break;
7170 case ISN_THROW:
7171 smsg("%s%4d THROW", pfx, current);
7172 break;
7173
7174 // expression operations on number
7175 case ISN_OPNR:
7176 case ISN_OPFLOAT:
7177 case ISN_OPANY:
7178 {
7179 char *what;
7180 char *ins;
7181
7182 switch (iptr->isn_arg.op.op_type)
7183 {
7184 case EXPR_MULT: what = "*"; break;
7185 case EXPR_DIV: what = "/"; break;
7186 case EXPR_REM: what = "%"; break;
7187 case EXPR_SUB: what = "-"; break;
7188 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007189 case EXPR_LSHIFT: what = "<<"; break;
7190 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007191 default: what = "???"; break;
7192 }
7193 switch (iptr->isn_type)
7194 {
7195 case ISN_OPNR: ins = "OPNR"; break;
7196 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7197 case ISN_OPANY: ins = "OPANY"; break;
7198 default: ins = "???"; break;
7199 }
7200 smsg("%s%4d %s %s", pfx, current, ins, what);
7201 }
7202 break;
7203
7204 case ISN_COMPAREBOOL:
7205 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007206 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007207 case ISN_COMPARENR:
7208 case ISN_COMPAREFLOAT:
7209 case ISN_COMPARESTRING:
7210 case ISN_COMPAREBLOB:
7211 case ISN_COMPARELIST:
7212 case ISN_COMPAREDICT:
7213 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007214 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007215 case ISN_COMPAREANY:
7216 {
7217 char *p;
7218 char buf[10];
7219 char *type;
7220
7221 switch (iptr->isn_arg.op.op_type)
7222 {
7223 case EXPR_EQUAL: p = "=="; break;
7224 case EXPR_NEQUAL: p = "!="; break;
7225 case EXPR_GREATER: p = ">"; break;
7226 case EXPR_GEQUAL: p = ">="; break;
7227 case EXPR_SMALLER: p = "<"; break;
7228 case EXPR_SEQUAL: p = "<="; break;
7229 case EXPR_MATCH: p = "=~"; break;
7230 case EXPR_IS: p = "is"; break;
7231 case EXPR_ISNOT: p = "isnot"; break;
7232 case EXPR_NOMATCH: p = "!~"; break;
7233 default: p = "???"; break;
7234 }
7235 STRCPY(buf, p);
7236 if (iptr->isn_arg.op.op_ic == TRUE)
7237 strcat(buf, "?");
7238 switch(iptr->isn_type)
7239 {
7240 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7241 case ISN_COMPARESPECIAL:
7242 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007243 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007244 case ISN_COMPARENR: type = "COMPARENR"; break;
7245 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7246 case ISN_COMPARESTRING:
7247 type = "COMPARESTRING"; break;
7248 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7249 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7250 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7251 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007252 case ISN_COMPAREOBJECT:
7253 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007254 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7255 default: type = "???"; break;
7256 }
7257
7258 smsg("%s%4d %s %s", pfx, current, type, buf);
7259 }
7260 break;
7261
7262 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7263 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7264
7265 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007266 case ISN_CONCAT:
7267 smsg("%s%4d CONCAT size %lld", pfx, current,
7268 (varnumber_T)(iptr->isn_arg.number));
7269 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007270 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7271 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7272 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7273 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7274 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7275 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7276 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7277 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7278 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7279 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7280 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007281 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007282 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7283 iptr->isn_arg.getitem.gi_index,
7284 iptr->isn_arg.getitem.gi_with_op ?
7285 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007286 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7287 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7288 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007289 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7290 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007291 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007292 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007293 pfx, current,
7294 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007295 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007296 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007297 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007298 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007299 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7300 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7301
Bram Moolenaar4c137212021-04-19 16:48:48 +02007302 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7303
Bram Moolenaar4c137212021-04-19 16:48:48 +02007304 case ISN_CHECKTYPE:
7305 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007306 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007307 char *tofree = NULL;
7308 char *typename;
7309
7310 if (ct->ct_type->tt_type == VAR_FLOAT
7311 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7312 typename = "float|number";
7313 else
7314 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007315
7316 if (ct->ct_arg_idx == 0)
7317 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007318 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007319 (int)ct->ct_off);
7320 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007321 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007322 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007323 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007324 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007325 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007326 (int)ct->ct_arg_idx);
7327 vim_free(tofree);
7328 break;
7329 }
7330 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7331 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7332 iptr->isn_arg.checklen.cl_min_len);
7333 break;
7334 case ISN_SETTYPE:
7335 {
7336 char *tofree;
7337
7338 smsg("%s%4d SETTYPE %s", pfx, current,
7339 type_name(iptr->isn_arg.type.ct_type, &tofree));
7340 vim_free(tofree);
7341 break;
7342 }
7343 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007344 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7345 smsg("%s%4d INVERT %d (!val)", pfx, current,
7346 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007347 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007348 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7349 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007350 break;
7351 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007352 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007353 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007354 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7355 pfx, current,
7356 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007357 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007358 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7359 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007360 break;
7361 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007362 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007363 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007364 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007365 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7366 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007367 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007368 else
7369 smsg("%s%4d PUT %c %ld", pfx, current,
7370 iptr->isn_arg.put.put_regname,
7371 (long)iptr->isn_arg.put.put_lnum);
7372 break;
7373
Bram Moolenaar4c137212021-04-19 16:48:48 +02007374 case ISN_CMDMOD:
7375 {
7376 char_u *buf;
7377 size_t len = produce_cmdmods(
7378 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7379
7380 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007381 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007382 {
7383 (void)produce_cmdmods(
7384 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7385 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7386 vim_free(buf);
7387 }
7388 break;
7389 }
7390 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7391
7392 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007393 smsg("%s%4d PROFILE START line %d", pfx, current,
7394 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007395 break;
7396
7397 case ISN_PROF_END:
7398 smsg("%s%4d PROFILE END", pfx, current);
7399 break;
7400
Bram Moolenaare99d4222021-06-13 14:01:26 +02007401 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007402 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7403 iptr->isn_arg.debug.dbg_break_lnum + 1,
7404 iptr->isn_lnum,
7405 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007406 break;
7407
Bram Moolenaar4c137212021-04-19 16:48:48 +02007408 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7409 iptr->isn_arg.unpack.unp_count,
7410 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7411 break;
7412 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7413 iptr->isn_arg.shuffle.shfl_item,
7414 iptr->isn_arg.shuffle.shfl_up);
7415 break;
7416 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7417
7418 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7419 return;
7420 }
7421
7422 out_flush(); // output one line at a time
7423 ui_breakcheck();
7424 if (got_int)
7425 break;
7426 }
7427}
7428
7429/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007430 * Handle command line completion for the :disassemble command.
7431 */
7432 void
7433set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7434{
7435 char_u *p;
7436
7437 // Default: expand user functions, "debug" and "profile"
7438 xp->xp_context = EXPAND_DISASSEMBLE;
7439 xp->xp_pattern = arg;
7440
7441 // first argument already typed: only user function names
7442 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7443 {
7444 xp->xp_context = EXPAND_USER_FUNC;
7445 xp->xp_pattern = skipwhite(p);
7446 }
7447}
7448
7449/*
7450 * Function given to ExpandGeneric() to obtain the list of :disassemble
7451 * arguments.
7452 */
7453 char_u *
7454get_disassemble_argument(expand_T *xp, int idx)
7455{
7456 if (idx == 0)
7457 return (char_u *)"debug";
7458 if (idx == 1)
7459 return (char_u *)"profile";
7460 return get_user_func_name(xp, idx - 2);
7461}
7462
7463/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007464 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007465 * We don't really need this at runtime, but we do have tests that require it,
7466 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 */
7468 void
7469ex_disassemble(exarg_T *eap)
7470{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007471 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007472 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007474 isn_T *instr = NULL; // init to shut up gcc warning
7475 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007476 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007477
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007478 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007479 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007481 if (func_needs_compiling(ufunc, compile_type)
7482 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007483 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007484 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007485 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007486 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 return;
7488 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007489 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007490
7491 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007492 switch (compile_type)
7493 {
7494 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007495#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007496 instr = dfunc->df_instr_prof;
7497 instr_count = dfunc->df_instr_prof_count;
7498 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007499#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007500 // FALLTHROUGH
7501 case CT_NONE:
7502 instr = dfunc->df_instr;
7503 instr_count = dfunc->df_instr_count;
7504 break;
7505 case CT_DEBUG:
7506 instr = dfunc->df_instr_debug;
7507 instr_count = dfunc->df_instr_debug_count;
7508 break;
7509 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510
Bram Moolenaar4c137212021-04-19 16:48:48 +02007511 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007512}
7513
7514/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007515 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007516 * list, etc. Mostly like what JavaScript does, except that empty list and
7517 * empty dictionary are FALSE.
7518 */
7519 int
7520tv2bool(typval_T *tv)
7521{
7522 switch (tv->v_type)
7523 {
7524 case VAR_NUMBER:
7525 return tv->vval.v_number != 0;
7526 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007527 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007528 case VAR_PARTIAL:
7529 return tv->vval.v_partial != NULL;
7530 case VAR_FUNC:
7531 case VAR_STRING:
7532 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7533 case VAR_LIST:
7534 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7535 case VAR_DICT:
7536 return tv->vval.v_dict != NULL
7537 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7538 case VAR_BOOL:
7539 case VAR_SPECIAL:
7540 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7541 case VAR_JOB:
7542#ifdef FEAT_JOB_CHANNEL
7543 return tv->vval.v_job != NULL;
7544#else
7545 break;
7546#endif
7547 case VAR_CHANNEL:
7548#ifdef FEAT_JOB_CHANNEL
7549 return tv->vval.v_channel != NULL;
7550#else
7551 break;
7552#endif
7553 case VAR_BLOB:
7554 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7555 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007556 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007557 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007558 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007559 case VAR_CLASS:
7560 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007561 case VAR_TYPEALIAS:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562 break;
7563 }
7564 return FALSE;
7565}
7566
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007567 void
7568emsg_using_string_as(typval_T *tv, int as_number)
7569{
7570 semsg(_(as_number ? e_using_string_as_number_str
7571 : e_using_string_as_bool_str),
7572 tv->vval.v_string == NULL
7573 ? (char_u *)"" : tv->vval.v_string);
7574}
7575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576/*
7577 * If "tv" is a string give an error and return FAIL.
7578 */
7579 int
7580check_not_string(typval_T *tv)
7581{
7582 if (tv->v_type == VAR_STRING)
7583 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007584 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585 clear_tv(tv);
7586 return FAIL;
7587 }
7588 return OK;
7589}
7590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007591
7592#endif // FEAT_EVAL