blob: 882b13c61aa5c4f460a9476d74438019ecb10a20 [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))
263 return FAIL;
264 else
265 ++ectx->ec_stack.ga_len;
266 tv = STACK_TV_BOT(-1);
267 tv->v_type = VAR_DICT;
268 tv->v_lock = 0;
269 tv->vval.v_dict = dict;
270 if (dict != NULL)
271 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200272 return OK;
273}
274
275/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200276 * If debug_tick changed check if "ufunc" has a breakpoint and update
277 * "uf_has_breakpoint".
278 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000279 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200280update_has_breakpoint(ufunc_T *ufunc)
281{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000282 if (ufunc->uf_debug_tick == debug_tick)
283 return;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200284
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000285 linenr_T breakpoint;
286
287 ufunc->uf_debug_tick = debug_tick;
288 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
289 ufunc->uf_has_breakpoint = breakpoint > 0;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200290}
291
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200292static garray_T dict_stack = GA_EMPTY;
293
294/*
295 * Put a value on the dict stack. This consumes "tv".
296 */
297 static int
298dict_stack_save(typval_T *tv)
299{
300 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000301 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200302 if (ga_grow(&dict_stack, 1) == FAIL)
303 return FAIL;
304 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
305 ++dict_stack.ga_len;
306 return OK;
307}
308
309/*
310 * Get the typval at top of the dict stack.
311 */
312 static typval_T *
313dict_stack_get_tv(void)
314{
315 if (dict_stack.ga_len == 0)
316 return NULL;
317 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
318}
319
320/*
321 * Get the dict at top of the dict stack.
322 */
323 static dict_T *
324dict_stack_get_dict(void)
325{
326 typval_T *tv;
327
328 if (dict_stack.ga_len == 0)
329 return NULL;
330 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
331 if (tv->v_type == VAR_DICT)
332 return tv->vval.v_dict;
333 return NULL;
334}
335
336/*
337 * Drop an item from the dict stack.
338 */
339 static void
340dict_stack_drop(void)
341{
342 if (dict_stack.ga_len == 0)
343 {
344 iemsg("Dict stack underflow");
345 return;
346 }
347 --dict_stack.ga_len;
348 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
349}
350
351/*
352 * Drop items from the dict stack until the length is equal to "len".
353 */
354 static void
355dict_stack_clear(int len)
356{
357 while (dict_stack.ga_len > len)
358 dict_stack_drop();
359}
360
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200361/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000362 * Get a pointer to useful "pt_outer" of "pt".
363 */
364 static outer_T *
365get_pt_outer(partial_T *pt)
366{
367 partial_T *ptref = pt->pt_outer_partial;
368
369 if (ptref == NULL)
370 return &pt->pt_outer;
371
372 // partial using partial (recursively)
373 while (ptref->pt_outer_partial != NULL)
374 ptref = ptref->pt_outer_partial;
375 return &ptref->pt_outer;
376}
377
378/*
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000379 * Check "argcount" arguments on the stack against what "ufunc" expects.
380 * "off" is the offset of arguments on the stack.
381 * Return OK or FAIL.
382 */
383 static int
384check_ufunc_arg_types(ufunc_T *ufunc, int argcount, int off, ectx_T *ectx)
385{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000386 if (ufunc->uf_arg_types == NULL && ufunc->uf_va_type == NULL)
387 return OK;
388
389 typval_T *argv = STACK_TV_BOT(0) - argcount - off;
390
391 // The function can change at runtime, check that the argument
392 // types are correct.
393 for (int i = 0; i < argcount; ++i)
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000394 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000395 type_T *type = NULL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000396
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000397 // assume a v:none argument, using the default value, is always OK
398 if (argv[i].v_type == VAR_SPECIAL
399 && argv[i].vval.v_number == VVAL_NONE)
400 continue;
Ernie Raelb077b582023-12-14 20:11:44 +0100401 // only pass values to user functions, never types
402 if (check_typval_is_value(&argv[i]) == FAIL)
403 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000404
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000405 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
406 type = ufunc->uf_arg_types[i];
407 else if (ufunc->uf_va_type != NULL)
408 type = ufunc->uf_va_type->tt_member;
409 if (type != NULL && check_typval_arg_type(type,
410 &argv[i], NULL, i + 1) == FAIL)
411 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000412 }
413 return OK;
414}
415
416/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100417 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100418 * This adds a stack frame and sets the instruction pointer to the start of the
419 * called function.
Bram Moolenaar5800c792022-09-22 17:34:01 +0100420 * If "pt_arg" is not NULL use "pt_arg->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100421 *
422 * Stack has:
423 * - current arguments (already there)
424 * - omitted optional argument (default values) added here
425 * - stack frame:
426 * - pointer to calling function
427 * - Index of next instruction in calling function
428 * - previous frame pointer
429 * - reserved space for local variables
430 */
431 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100432call_dfunc(
433 int cdf_idx,
Bram Moolenaar5800c792022-09-22 17:34:01 +0100434 partial_T *pt_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100435 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100436 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100438 int argcount = argcount_arg;
439 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
440 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200441 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100442 int arg_to_add;
443 int vararg_count = 0;
444 int varcount;
445 int idx;
446 estack_T *entry;
447 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200448 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000449 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450
451 if (dfunc->df_deleted)
452 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100453 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000454 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100455 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456 return FAIL;
457 }
458
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100459#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100460 if (do_profiling == PROF_YES)
461 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200462 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100463 {
464 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
465 + profile_info_ga.ga_len;
466 ++profile_info_ga.ga_len;
467 CLEAR_POINTER(info);
468 profile_may_start_func(info, ufunc,
469 (((dfunc_T *)def_functions.ga_data)
470 + ectx->ec_dfunc_idx)->df_ufunc);
471 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100472 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100473#endif
474
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200475 // When debugging and using "cont" switches to the not-debugged
476 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000477 compile_type = get_compile_type(ufunc);
478 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200479 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000480 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200481
482 // compile_def_function() may cause def_functions.ga_data to change
483 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
484 }
485 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200486 {
487 if (did_emsg_cumul + did_emsg == did_emsg_before)
488 semsg(_(e_function_is_not_compiled_str),
489 printable_func_name(ufunc));
490 return FAIL;
491 }
492
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200493 if (ufunc->uf_va_name != NULL)
494 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200495 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200496 // Stack at time of call with 2 varargs:
497 // normal_arg
498 // optional_arg
499 // vararg_1
500 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200501 // After creating the list:
502 // normal_arg
503 // optional_arg
504 // vararg-list
505 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200506 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200507 // After creating the list
508 // normal_arg
509 // (space for optional_arg)
510 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200511 vararg_count = argcount - ufunc->uf_args.ga_len;
512 if (vararg_count < 0)
513 vararg_count = 0;
514 else
515 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200516 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200517 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200518
519 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200520 }
521
Bram Moolenaarfe270812020-04-11 22:31:27 +0200522 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200523 if (arg_to_add < 0)
524 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100525 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
526 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200527 return FAIL;
528 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000529 else if (arg_to_add > ufunc->uf_def_args.ga_len)
530 {
531 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
532
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100533 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
534 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000535 return FAIL;
536 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200537
Bram Moolenaar574950d2023-01-03 19:08:50 +0000538 // If this is an object method, the object is just before the arguments.
539 typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
540
Yegappan Lakshmananc1946262023-09-25 21:00:46 +0200541 if (IS_OBJECT_METHOD(ufunc) && !IS_CONSTRUCTOR_METHOD(ufunc)
542 && obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL)
543 {
544 // If this is not a constructor method, then a valid object is
545 // needed.
546 emsg(_(e_using_null_object));
547 return FAIL;
548 }
549
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000550 // Check the argument types.
551 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
552 return FAIL;
553
mityua5550692023-11-25 15:41:20 +0100554 // While check_ufunc_arg_types call, def function compilation process may
555 // run. If so many def functions are compiled, def_functions array may be
556 // reallocated and dfunc may no longer have valid pointer. Get the object
557 // pointer from def_functions again here.
558 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
559
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200560 // Reserve space for:
561 // - missing arguments
562 // - stack frame
563 // - local variables
564 // - if needed: a counter for number of closures created in
565 // ectx->ec_funcrefs.
566 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100567 if (GA_GROW_FAILS(&ectx->ec_stack,
568 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 return FAIL;
570
Yegappan Lakshmanan1087b8c2023-10-07 22:03:18 +0200571 // The object pointer is in the execution typval stack. The GA_GROW call
572 // above may have reallocated the execution typval stack. So the object
573 // pointer may not be valid anymore. Get the object pointer again from the
574 // execution stack.
575 obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
576
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100577 // If depth of calling is getting too high, don't execute the function.
578 if (funcdepth_increment() == FAIL)
579 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200580 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100581
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100582 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200583 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100584 {
585 floc = ALLOC_ONE(funclocal_T);
586 if (floc == NULL)
587 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200588 *floc = ectx->ec_funclocal;
589 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100590 }
591
Bram Moolenaarfe270812020-04-11 22:31:27 +0200592 // Move the vararg-list to below the missing optional arguments.
593 if (vararg_count > 0 && arg_to_add > 0)
594 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100595
596 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200597 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200598 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200599 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600
601 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100602 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
603 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200604 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200605 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
606 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100607 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100608 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200609 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610
Bram Moolenaar5800c792022-09-22 17:34:01 +0100611 // Initialize all local variables to number zero. Also initialize the
612 // variable that counts how many closures were created. This is used in
613 // handle_closure_in_use().
614 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
615 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000616 {
617 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
618
619 tv->v_type = VAR_NUMBER;
620 tv->vval.v_number = 0;
621 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200622 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623
Bram Moolenaar574950d2023-01-03 19:08:50 +0000624 // For an object method move the object from just before the arguments to
625 // the first local variable.
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +0200626 if (IS_OBJECT_METHOD(ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +0000627 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200628 if (obj->v_type != VAR_OBJECT)
629 {
630 semsg(_(e_internal_error_str), "type in stack is not an object");
631 return FAIL;
632 }
633
Bram Moolenaar574950d2023-01-03 19:08:50 +0000634 *STACK_TV_VAR(0) = *obj;
635 obj->v_type = VAR_UNKNOWN;
636 }
637
Bram Moolenaar5800c792022-09-22 17:34:01 +0100638 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
639 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100640 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200641 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100642
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200643 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100644 return FAIL;
645 if (pt != NULL)
646 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000647 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200648 ++pt->pt_refcount;
649 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100650 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100651 else
652 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200653 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200654 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200655 {
656 vim_free(ref);
657 return FAIL;
658 }
659 ref->or_outer_allocated = TRUE;
660 ref->or_outer->out_stack = &ectx->ec_stack;
661 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
662 if (ectx->ec_outer_ref != NULL)
663 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100664 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200665 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100666 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100667 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200668 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100669
Bram Moolenaarc970e422021-03-17 15:03:04 +0100670 ++ufunc->uf_calls;
671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 // Set execution state to the start of the called function.
673 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100674 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100675 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200676 if (entry != NULL)
677 {
678 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200679 // Save the current context so it can be restored on return.
680 entry->es_save_sctx = current_sctx;
681 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200682 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100683
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200684 // Start execution at the first instruction.
685 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686
687 return OK;
688}
689
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000690// Double linked list of funcstack_T in use.
691static funcstack_T *first_funcstack = NULL;
692
693 static void
694add_funcstack_to_list(funcstack_T *funcstack)
695{
696 // Link in list of funcstacks.
697 if (first_funcstack != NULL)
698 first_funcstack->fs_prev = funcstack;
699 funcstack->fs_next = first_funcstack;
700 funcstack->fs_prev = NULL;
701 first_funcstack = funcstack;
702}
703
704 static void
705remove_funcstack_from_list(funcstack_T *funcstack)
706{
707 if (funcstack->fs_prev == NULL)
708 first_funcstack = funcstack->fs_next;
709 else
710 funcstack->fs_prev->fs_next = funcstack->fs_next;
711 if (funcstack->fs_next != NULL)
712 funcstack->fs_next->fs_prev = funcstack->fs_prev;
713}
714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200716 * Used when returning from a function: Check if any closure is still
717 * referenced. If so then move the arguments and variables to a separate piece
718 * of stack to be used when the closure is called.
719 * When "free_arguments" is TRUE the arguments are to be freed.
720 * Returns FAIL when out of memory.
721 */
722 static int
723handle_closure_in_use(ectx_T *ectx, int free_arguments)
724{
725 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
726 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200727 int argcount;
728 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200729 int idx;
730 typval_T *tv;
731 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200732 garray_T *gap = &ectx->ec_funcrefs;
733 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200734
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200735 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200736 return OK; // function was freed
737 if (dfunc->df_has_closure == 0)
738 return OK; // no closures
739 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
740 closure_count = tv->vval.v_number;
741 if (closure_count == 0)
742 return OK; // no funcrefs created
743
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100744 // Compute "top": the first entry in the stack used by the function.
745 // This is the first argument (after that comes the stack frame and then
746 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200747 argcount = ufunc_argcount(dfunc->df_ufunc);
748 top = ectx->ec_frame_idx - argcount;
749
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200750 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200751 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200752 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200753 partial_T *pt;
754 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200755
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200756 if (off < 0)
757 continue; // count is off or already done
758 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200759 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200760 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200761 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200762 int i;
763
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000764 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200765 // unreferenced on return.
766 for (i = 0; i < dfunc->df_varcount; ++i)
767 {
768 typval_T *stv = STACK_TV(ectx->ec_frame_idx
769 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200770 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200771 --refcount;
772 }
773 if (refcount > 1)
774 {
775 closure_in_use = TRUE;
776 break;
777 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200778 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200779 }
780
781 if (closure_in_use)
782 {
783 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
784 typval_T *stack;
785
786 // A closure is using the arguments and/or local variables.
787 // Move them to the called function.
788 if (funcstack == NULL)
789 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000790
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200791 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100792 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
793 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200794 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
795 funcstack->fs_ga.ga_data = stack;
796 if (stack == NULL)
797 {
798 vim_free(funcstack);
799 return FAIL;
800 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000801 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200802
803 // Move or copy the arguments.
804 for (idx = 0; idx < argcount; ++idx)
805 {
806 tv = STACK_TV(top + idx);
807 if (free_arguments)
808 {
809 *(stack + idx) = *tv;
810 tv->v_type = VAR_UNKNOWN;
811 }
812 else
813 copy_tv(tv, stack + idx);
814 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100815 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200816 // Move the local variables.
817 for (idx = 0; idx < dfunc->df_varcount; ++idx)
818 {
819 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200820
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200821 // A partial created for a local function, that is also used as a
822 // local variable, has a reference count for the variable, thus
823 // will never go down to zero. When all these refcounts are one
824 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000825 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200826 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
827 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200828 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200829
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200830 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200831 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
832 gap->ga_len - closure_count + i])
833 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200834 }
835
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200836 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200837 tv->v_type = VAR_UNKNOWN;
838 }
839
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200840 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200841 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200842 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
843 - closure_count + idx];
844 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200845 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200846 ++funcstack->fs_refcount;
847 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100848 pt->pt_outer.out_stack = &funcstack->fs_ga;
849 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200850 }
851 }
852 }
853
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200854 for (idx = 0; idx < closure_count; ++idx)
855 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
856 - closure_count + idx]);
857 gap->ga_len -= closure_count;
858 if (gap->ga_len == 0)
859 ga_clear(gap);
860
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200861 return OK;
862}
863
864/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200865 * Called when a partial is freed or its reference count goes down to one. The
866 * funcstack may be the only reference to the partials in the local variables.
867 * Go over all of them, the funcref and can be freed if all partials
868 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100869 * Returns TRUE if the funcstack is freed, the partial referencing it will then
870 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200871 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100872 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200873funcstack_check_refcount(funcstack_T *funcstack)
874{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100875 int i;
876 garray_T *gap = &funcstack->fs_ga;
877 int done = 0;
878 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200879
880 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100881 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200882 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
883 {
884 typval_T *tv = ((typval_T *)gap->ga_data) + i;
885
886 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
887 && tv->vval.v_partial->pt_funcstack == funcstack
888 && tv->vval.v_partial->pt_refcount == 1)
889 ++done;
890 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100891 if (done != funcstack->fs_min_refcount)
892 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200893
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100894 stack = gap->ga_data;
895
896 // All partials referencing the funcstack have a reference count of
897 // one, thus the funcstack is no longer of use.
898 for (i = 0; i < gap->ga_len; ++i)
899 clear_tv(stack + i);
900 vim_free(stack);
901 remove_funcstack_from_list(funcstack);
902 vim_free(funcstack);
903
904 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200905}
906
907/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000908 * For garbage collecting: set references in all variables referenced by
909 * all funcstacks.
910 */
911 int
912set_ref_in_funcstacks(int copyID)
913{
914 funcstack_T *funcstack;
915
916 for (funcstack = first_funcstack; funcstack != NULL;
917 funcstack = funcstack->fs_next)
918 {
919 typval_T *stack = funcstack->fs_ga.ga_data;
920 int i;
921
922 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
923 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
924 return TRUE; // abort
925 }
926 return FALSE;
927}
928
Bram Moolenaar806a2732022-09-04 15:40:36 +0100929// Ugly static to avoid passing the execution context around through many
930// layers.
931static ectx_T *current_ectx = NULL;
932
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000933/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100934 * Return TRUE if currently executing a :def function.
935 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100936 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100937 int
938in_def_function(void)
939{
940 return current_ectx != NULL;
941}
942
943/*
Ernie Rael4c8da022023-10-11 21:35:11 +0200944 * If executing a class/object method, then fill in the lval_T.
945 * Set lr_tv to the executing item, and lr_exec_class to the executing class;
946 * use free_tv and class_unref when finished with the lval_root.
947 * For use by builtin functions.
948 *
949 * Return FAIL and do nothing if not executing in a class; otherwise OK.
950 */
951 int
952fill_exec_lval_root(lval_root_T *root)
953{
954 ectx_T *ectx = current_ectx;
955 if (ectx != NULL)
956 {
957 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
958 + current_ectx->ec_dfunc_idx;
959 ufunc_T *ufunc = dfunc->df_ufunc;
960 if (ufunc->uf_class != NULL) // executing a method?
961 {
962 typval_T *tv = alloc_tv();
963 if (tv != NULL)
964 {
965 CLEAR_POINTER(root);
966 root->lr_tv = tv;
967 copy_tv(STACK_TV_VAR(0), root->lr_tv);
968 root->lr_cl_exec = ufunc->uf_class;
969 ++root->lr_cl_exec->class_refcount;
970 return OK;
971 }
972 }
973 }
974
975 return FAIL;
976}
977
978/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100979 * Clear "current_ectx" and return the previous value. To be used when calling
980 * a user function.
981 */
982 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000983clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100984{
985 ectx_T *r = current_ectx;
986
987 current_ectx = NULL;
988 return r;
989}
990
991 void
992restore_current_ectx(ectx_T *ectx)
993{
994 if (current_ectx != NULL)
995 iemsg("Restoring current_ectx while it is not NULL");
996 current_ectx = ectx;
997}
998
999/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01001000 * Add an entry for a deferred function call to the currently executing
1001 * function.
1002 * Return the list or NULL when failed.
1003 */
1004 static list_T *
1005add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001006{
1007 typval_T *defer_tv = STACK_TV_VAR(var_idx);
1008 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001009 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001010 typval_T listval;
1011
1012 if (defer_tv->v_type != VAR_LIST)
1013 {
Bram Moolenaara5348f22022-09-04 11:42:22 +01001014 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001015 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001016 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001017 }
1018 defer_l = defer_tv->vval.v_list;
1019
1020 l = list_alloc_with_items(argcount + 1);
1021 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001022 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001023 listval.v_type = VAR_LIST;
1024 listval.vval.v_list = l;
1025 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +01001026 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001027 {
1028 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001029 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001030 }
1031
Bram Moolenaar806a2732022-09-04 15:40:36 +01001032 return l;
1033}
1034
1035/*
1036 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
1037 * The local variable that lists deferred functions is "var_idx".
1038 * Returns OK or FAIL.
1039 */
1040 static int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001041defer_command(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001042{
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001043 list_T *l = add_defer_item(var_idx, argcount, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001044 int i;
1045 typval_T *func_tv;
1046
1047 if (l == NULL)
1048 return FAIL;
1049
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001050 func_tv = STACK_TV_BOT(-argcount - 1);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001051 if (func_tv->v_type != VAR_PARTIAL && func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001052 {
1053 semsg(_(e_expected_str_but_got_str),
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001054 "function or partial",
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001055 vartype_name(func_tv->v_type));
1056 return FAIL;
1057 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001058 list_set_item(l, 0, func_tv);
1059
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001060 for (i = 0; i < argcount; ++i)
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001061 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
1062 ectx->ec_stack.ga_len -= argcount + 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001063 return OK;
1064}
1065
1066/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001067 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001068 * Consumes "name", also on failure.
1069 * Only to be called when in_def_function() returns TRUE.
1070 */
1071 int
1072add_defer_function(char_u *name, int argcount, typval_T *argvars)
1073{
1074 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1075 + current_ectx->ec_dfunc_idx;
1076 list_T *l;
1077 typval_T func_tv;
1078 int i;
1079
1080 if (dfunc->df_defer_var_idx == 0)
1081 {
1082 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001083 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001084 return FAIL;
1085 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001086
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001087 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001088 if (l == NULL)
1089 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001090 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001091 return FAIL;
1092 }
1093
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001094 func_tv.v_type = VAR_FUNC;
1095 func_tv.v_lock = 0;
1096 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001097 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001098
Bram Moolenaar806a2732022-09-04 15:40:36 +01001099 for (i = 0; i < argcount; ++i)
1100 list_set_item(l, i + 1, argvars + i);
1101 return OK;
1102}
1103
1104/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001105 * Invoked when returning from a function: Invoke any deferred calls.
1106 */
1107 static void
1108invoke_defer_funcs(ectx_T *ectx)
1109{
1110 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1111 + ectx->ec_dfunc_idx;
1112 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1113 listitem_T *li;
1114
1115 if (defer_tv->v_type != VAR_LIST)
1116 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001117 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001118 {
1119 list_T *l = li->li_tv.vval.v_list;
1120 typval_T rettv;
1121 typval_T argvars[MAX_FUNC_ARGS];
1122 int i;
1123 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001124 typval_T *functv = &l->lv_first->li_tv;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001125 int argcount = l->lv_len - 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001126
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001127 if (functv->vval.v_string == NULL)
1128 // already being called, can happen if function does ":qa"
1129 continue;
1130
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001131 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001132 {
1133 arg_li = arg_li->li_next;
1134 argvars[i] = arg_li->li_tv;
1135 }
1136
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001137 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001138 CLEAR_FIELD(funcexe);
1139 funcexe.fe_evaluate = TRUE;
1140 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001141 if (functv->v_type == VAR_PARTIAL)
1142 {
1143 funcexe.fe_partial = functv->vval.v_partial;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001144 funcexe.fe_object = functv->vval.v_partial->pt_obj;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001145 if (funcexe.fe_object != NULL)
1146 ++funcexe.fe_object->obj_refcount;
1147 }
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001148
1149 char_u *name = functv->vval.v_string;
1150 functv->vval.v_string = NULL;
1151
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001152 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001153 // first statement in the function will be executed (because of the
1154 // exception). So save and restore the try/catch/throw exception
1155 // state.
1156 exception_state_T estate;
1157 exception_state_save(&estate);
1158 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001159
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001160 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1161
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001162 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001163
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001164 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001165 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001166 }
1167}
1168
1169/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 * Return from the current function.
1171 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001172 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001173func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001176 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001177 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1178 + ectx->ec_dfunc_idx;
1179 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001180 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001181 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1182 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001183 funclocal_T *floc;
1184#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001185 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1186 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187
Bram Moolenaar12d26532021-02-19 19:13:21 +01001188 if (do_profiling == PROF_YES)
1189 {
1190 ufunc_T *caller = prev_dfunc->df_ufunc;
1191
1192 if (dfunc->df_ufunc->uf_profiling
1193 || (caller != NULL && caller->uf_profiling))
1194 {
1195 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1196 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1197 --profile_info_ga.ga_len;
1198 }
1199 }
1200#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001201
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001202 if (dfunc->df_defer_var_idx > 0)
1203 invoke_defer_funcs(ectx);
1204
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001205 // No check for uf_refcount being zero, cannot think of a way that would
1206 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001207 --dfunc->df_ufunc->uf_calls;
1208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001210 entry = estack_pop();
1211 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001212 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001214 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1215 return FAIL;
1216
Bram Moolenaar574950d2023-01-03 19:08:50 +00001217 // Clear the arguments. If this was an object method also clear the
1218 // object, it is just before the arguments.
1219 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001220 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001221 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001222 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1223 clear_tv(STACK_TV(idx));
1224
1225 // Clear local variables and temp values, but not the return value.
1226 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001227 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001229
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001230 // The return value should be on top of the stack. However, when aborting
1231 // it may not be there and ec_frame_idx is the top of the stack.
1232 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001233 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001234 ret_idx = 0;
1235
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001236 if (ectx->ec_outer_ref != NULL)
1237 {
1238 if (ectx->ec_outer_ref->or_outer_allocated)
1239 vim_free(ectx->ec_outer_ref->or_outer);
1240 partial_unref(ectx->ec_outer_ref->or_partial);
1241 vim_free(ectx->ec_outer_ref);
1242 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001243
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001244 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001245 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001246 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1247 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001248 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1249 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001250 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001251 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001252 floc = (void *)STACK_TV(ectx->ec_frame_idx
1253 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001254 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001255 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1256 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001257
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001258 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001259 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001260 else
1261 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001262 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001263 vim_free(floc);
1264 }
1265
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001266 if (ret_idx > 0)
1267 {
1268 // Reset the stack to the position before the call, with a spot for the
1269 // return value, moved there from above the frame.
1270 ectx->ec_stack.ga_len = top + 1;
1271 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1272 }
1273 else
1274 // Reset the stack to the position before the call.
1275 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001276
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001277 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001278 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001279 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280}
1281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282/*
1283 * Prepare arguments and rettv for calling a builtin or user function.
1284 */
1285 static int
1286call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1287{
1288 int idx;
1289 typval_T *tv;
1290
1291 // Move arguments from bottom of the stack to argvars[] and add terminator.
1292 for (idx = 0; idx < argcount; ++idx)
1293 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1294 argvars[argcount].v_type = VAR_UNKNOWN;
1295
1296 // Result replaces the arguments on the stack.
1297 if (argcount > 0)
1298 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001299 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 return FAIL;
1301 else
1302 ++ectx->ec_stack.ga_len;
1303
1304 // Default return value is zero.
1305 tv = STACK_TV_BOT(-1);
1306 tv->v_type = VAR_NUMBER;
1307 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001308 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309
1310 return OK;
1311}
1312
1313/*
1314 * Call a builtin function by index.
1315 */
1316 static int
1317call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1318{
1319 typval_T argvars[MAX_FUNC_ARGS];
1320 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001321 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001322 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001323 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324
1325 if (call_prepare(argcount, argvars, ectx) == FAIL)
1326 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001327 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001329 // Call the builtin function. Set "current_ectx" so that when it
1330 // recursively invokes call_def_function() a closure context can be set.
1331 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001333 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001334 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335
1336 // Clear the arguments.
1337 for (idx = 0; idx < argcount; ++idx)
1338 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001339
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001340 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001341 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 return OK;
1343}
1344
1345/*
1346 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001347 * If the function is compiled this will add a stack frame and set the
1348 * instruction pointer at the start of the function.
1349 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001350 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001351 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 */
1353 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001354call_ufunc(
1355 ufunc_T *ufunc,
1356 partial_T *pt,
1357 int argcount,
1358 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001359 isn_T *iptr,
1360 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361{
1362 typval_T argvars[MAX_FUNC_ARGS];
1363 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001364 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001366 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001367 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368
Bram Moolenaare99d4222021-06-13 14:01:26 +02001369 if (func_needs_compiling(ufunc, compile_type)
1370 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1371 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001372 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001373 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001374 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001375 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001376 if (error != FCERR_UNKNOWN)
1377 {
1378 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001379 semsg(_(e_too_many_arguments_for_function_str),
1380 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001381 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001382 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001383 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001384 return FAIL;
1385 }
1386
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001387 // The function has been compiled, can call it quickly. For a function
1388 // that was defined later: we can call it directly next time.
1389 if (iptr != NULL)
1390 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001391 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001392 iptr->isn_type = ISN_DCALL;
1393 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1394 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1395 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001396 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001397 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398
1399 if (call_prepare(argcount, argvars, ectx) == FAIL)
1400 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001401 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001402 funcexe.fe_evaluate = TRUE;
1403 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404
1405 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001407 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408
1409 // Clear the arguments.
1410 for (idx = 0; idx < argcount; ++idx)
1411 clear_tv(&argvars[idx]);
1412
1413 if (error != FCERR_NONE)
1414 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001415 user_func_error(error, printable_func_name(ufunc),
1416 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 return FAIL;
1418 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001419 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001420 // Error other than from calling the function itself.
1421 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422 return OK;
1423}
1424
1425/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001426 * If command modifiers were applied restore them.
1427 */
1428 static void
1429may_restore_cmdmod(funclocal_T *funclocal)
1430{
1431 if (funclocal->floc_restore_cmdmod)
1432 {
1433 cmdmod.cmod_filter_regmatch.regprog = NULL;
1434 undo_cmdmod(&cmdmod);
1435 cmdmod = funclocal->floc_save_cmdmod;
1436 funclocal->floc_restore_cmdmod = FALSE;
1437 }
1438}
1439
1440/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001441 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1442 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001443 */
1444 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001445vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001446{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001447 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001448}
1449
1450/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 * Execute a function by "name".
1452 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001453 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 * Returns FAIL if not found without an error message.
1455 */
1456 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001457call_by_name(
1458 char_u *name,
1459 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001460 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001461 isn_T *iptr,
1462 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463{
1464 ufunc_T *ufunc;
1465
1466 if (builtin_function(name, -1))
1467 {
1468 int func_idx = find_internal_func(name);
1469
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001470 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001472 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 return FAIL;
1474 return call_bfunc(func_idx, argcount, ectx);
1475 }
1476
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001477 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001478
1479 if (ufunc == NULL)
1480 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001481 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001482
1483 if (script_autoload(name, TRUE))
1484 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001485 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001486
1487 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001488 return FAIL; // bail out if loading the script caused an error
1489 }
1490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001492 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001493 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1494 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001495
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001496 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001497 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498
1499 return FAIL;
1500}
1501
1502 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001503call_partial(
1504 typval_T *tv,
1505 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001506 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001508 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001509 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001511 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001512 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513
1514 if (tv->v_type == VAR_PARTIAL)
1515 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001516 partial_T *pt = tv->vval.v_partial;
1517 int i;
1518
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001519 if (pt->pt_obj != NULL)
1520 {
1521 // partial with an object method. Push the object before the
1522 // function arguments.
1523 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1524 return FAIL;
1525 for (i = 1; i <= argcount; ++i)
1526 *STACK_TV_BOT(-i + 1) = *STACK_TV_BOT(-i);
1527
1528 typval_T *obj_tv = STACK_TV_BOT(-argcount);
1529 obj_tv->v_type = VAR_OBJECT;
1530 obj_tv->v_lock = 0;
1531 obj_tv->vval.v_object = pt->pt_obj;
1532 ++pt->pt_obj->obj_refcount;
1533 ++ectx->ec_stack.ga_len;
1534 }
1535
Bram Moolenaara90afb92020-07-15 22:38:56 +02001536 if (pt->pt_argc > 0)
1537 {
1538 // Make space for arguments from the partial, shift the "argcount"
1539 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001540 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001541 return FAIL;
1542 for (i = 1; i <= argcount; ++i)
1543 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1544 ectx->ec_stack.ga_len += pt->pt_argc;
1545 argcount += pt->pt_argc;
1546
1547 // copy the arguments from the partial onto the stack
1548 for (i = 0; i < pt->pt_argc; ++i)
1549 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1550 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001551 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552
1553 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001554 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 name = pt->pt_name;
1557 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001558 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001560 if (name != NULL)
1561 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001562 char_u fname_buf[FLEN_FIXED + 1];
1563 char_u *tofree = NULL;
1564 funcerror_T error = FCERR_NONE;
1565 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001566
1567 // May need to translate <SNR>123_ to K_SNR.
1568 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1569 if (error != FCERR_NONE)
1570 res = FAIL;
1571 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001572 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001573 vim_free(tofree);
1574 }
1575
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001576 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 {
1578 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001579 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001580 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 return FAIL;
1582 }
1583 return OK;
1584}
1585
1586/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001587 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1588 * TRUE.
1589 */
1590 static int
1591error_if_locked(int lock, char *error)
1592{
1593 if (lock & (VAR_LOCKED | VAR_FIXED))
1594 {
1595 emsg(_(error));
1596 return TRUE;
1597 }
1598 return FALSE;
1599}
1600
1601/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001602 * Give an error if "tv" is not a number and return FAIL.
1603 */
1604 static int
1605check_for_number(typval_T *tv)
1606{
1607 if (tv->v_type != VAR_NUMBER)
1608 {
1609 semsg(_(e_expected_str_but_got_str),
1610 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1611 return FAIL;
1612 }
1613 return OK;
1614}
1615
1616/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001617 * Store "tv" in variable "name".
1618 * This is for s: and g: variables.
1619 */
1620 static void
1621store_var(char_u *name, typval_T *tv)
1622{
1623 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001624 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001625
Bram Moolenaard877a572021-04-01 19:42:48 +02001626 if (tv->v_lock)
1627 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001628 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001629 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001630 restore_funccal();
1631}
1632
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001633/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001634 * Convert "tv" to a string.
1635 * Return FAIL if not allowed.
1636 */
1637 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001638do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001639{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001640 if (tv->v_type == VAR_STRING)
1641 return OK;
1642
1643 char_u *str;
1644
1645 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001646 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001647 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001648 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001649 case VAR_SPECIAL:
1650 case VAR_BOOL:
1651 case VAR_NUMBER:
1652 case VAR_FLOAT:
1653 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001654
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001655 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001656 if (tolerant)
1657 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001658 char_u *s, *e, *p;
1659 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001660
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001661 ga_init2(&ga, sizeof(char_u *), 1);
1662
1663 // Convert to NL separated items, then
1664 // escape the items and replace the NL with
1665 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001666 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001667 if (str == NULL)
1668 return FAIL;
1669 s = str;
1670 while ((e = vim_strchr(s, '\n')) != NULL)
1671 {
1672 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001673 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001674 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001675 if (p != NULL)
1676 {
1677 ga_concat(&ga, p);
1678 ga_concat(&ga, (char_u *)" ");
1679 vim_free(p);
1680 }
1681 s = e + 1;
1682 }
1683 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001684 clear_tv(tv);
1685 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001686 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001687 return OK;
1688 }
1689 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001690 default: to_string_error(tv->v_type);
1691 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001692 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001693 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001694 str = typval_tostring(tv, TRUE);
1695 clear_tv(tv);
1696 tv->v_type = VAR_STRING;
1697 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001698 return OK;
1699}
1700
1701/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001702 * When the value of "sv" is a null list of dict, allocate it.
1703 */
1704 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001705allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001706{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001707 typval_T *tv = sv->sv_tv;
1708
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001709 switch (tv->v_type)
1710 {
1711 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001712 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001713 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001714 break;
1715 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001716 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001717 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001718 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001719 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001720 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001721 (void)rettv_blob_alloc(tv);
1722 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001723 default:
1724 break;
1725 }
1726}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001727
Bram Moolenaare7525c52021-01-09 13:20:37 +01001728/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001729 * Return the character "str[index]" where "index" is the character index,
1730 * including composing characters.
1731 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001732 */
1733 char_u *
1734char_from_string(char_u *str, varnumber_T index)
1735{
1736 size_t nbyte = 0;
1737 varnumber_T nchar = index;
1738 size_t slen;
1739
1740 if (str == NULL)
1741 return NULL;
1742 slen = STRLEN(str);
1743
Bram Moolenaarff871402021-03-26 13:34:05 +01001744 // Do the same as for a list: a negative index counts from the end.
1745 // Optimization to check the first byte to be below 0x80 (and no composing
1746 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001747 if (index < 0)
1748 {
1749 int clen = 0;
1750
1751 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001752 {
1753 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1754 ++nbyte;
1755 else if (enc_utf8)
1756 nbyte += utfc_ptr2len(str + nbyte);
1757 else
1758 nbyte += mb_ptr2len(str + nbyte);
1759 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001760 nchar = clen + index;
1761 if (nchar < 0)
1762 // unlike list: index out of range results in empty string
1763 return NULL;
1764 }
1765
1766 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001767 {
1768 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1769 ++nbyte;
1770 else if (enc_utf8)
1771 nbyte += utfc_ptr2len(str + nbyte);
1772 else
1773 nbyte += mb_ptr2len(str + nbyte);
1774 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001775 if (nbyte >= slen)
1776 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001777 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001778}
1779
1780/*
1781 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001782 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001783 * If going over the end return "str_len".
1784 * If "idx" is negative count from the end, -1 is the last character.
1785 * When going over the start return -1.
1786 */
1787 static long
1788char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1789{
1790 varnumber_T nchar = idx;
1791 size_t nbyte = 0;
1792
1793 if (nchar >= 0)
1794 {
1795 while (nchar > 0 && nbyte < str_len)
1796 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001797 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001798 --nchar;
1799 }
1800 }
1801 else
1802 {
1803 nbyte = str_len;
1804 while (nchar < 0 && nbyte > 0)
1805 {
1806 --nbyte;
1807 nbyte -= mb_head_off(str, str + nbyte);
1808 ++nchar;
1809 }
1810 if (nchar < 0)
1811 return -1;
1812 }
1813 return (long)nbyte;
1814}
1815
1816/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001817 * Return the slice "str[first : last]" using character indexes. Composing
1818 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001819 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001820 * Return NULL when the result is empty.
1821 */
1822 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001823string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001824{
1825 long start_byte, end_byte;
1826 size_t slen;
1827
1828 if (str == NULL)
1829 return NULL;
1830 slen = STRLEN(str);
1831 start_byte = char_idx2byte(str, slen, first);
1832 if (start_byte < 0)
1833 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001834 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001835 end_byte = (long)slen;
1836 else
1837 {
1838 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001839 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001840 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001841 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001842 }
1843
1844 if (start_byte >= (long)slen || end_byte <= start_byte)
1845 return NULL;
1846 return vim_strnsave(str + start_byte, end_byte - start_byte);
1847}
1848
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001849/*
1850 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1851 * When "dfunc_idx" is negative don't give an error.
1852 * Returns NULL for an error.
1853 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001854 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001855get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001856{
1857 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001858 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1859 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001860 svar_T *sv;
1861
1862 if (sref->sref_seq != si->sn_script_seq)
1863 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001864 // The script was reloaded after the function was compiled, the
1865 // script_idx may not be valid.
1866 if (dfunc != NULL)
1867 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1868 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001869 return NULL;
1870 }
1871 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001872 if (sv->sv_name == NULL)
1873 {
1874 if (dfunc != NULL)
1875 emsg(_(e_script_variable_was_deleted));
1876 return NULL;
1877 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001878 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001879 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001880 if (dfunc != NULL)
1881 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001882 return NULL;
1883 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001884
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001885 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1886 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001887 {
1888 if (dfunc != NULL)
1889 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1890 return NULL;
1891 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001892 return sv;
1893}
1894
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001895/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001896 * Function passed to do_cmdline() for splitting a script joined by NL
1897 * characters.
1898 */
1899 static char_u *
1900get_split_sourceline(
1901 int c UNUSED,
1902 void *cookie,
1903 int indent UNUSED,
1904 getline_opt_T options UNUSED)
1905{
1906 source_cookie_T *sp = (source_cookie_T *)cookie;
1907 char_u *p;
1908 char_u *line;
1909
Bram Moolenaar20677332021-06-06 17:02:53 +02001910 p = vim_strchr(sp->nextline, '\n');
1911 if (p == NULL)
1912 {
1913 line = vim_strsave(sp->nextline);
1914 sp->nextline += STRLEN(sp->nextline);
1915 }
1916 else
1917 {
1918 line = vim_strnsave(sp->nextline, p - sp->nextline);
1919 sp->nextline = p + 1;
1920 }
1921 return line;
1922}
1923
1924/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 * Execute a function by "name".
1926 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001927 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 */
1929 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001930call_eval_func(
1931 char_u *name,
1932 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001933 ectx_T *ectx,
1934 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935{
Bram Moolenaared677f52020-08-12 16:38:10 +02001936 int called_emsg_before = called_emsg;
1937 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001939 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001940 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001942 dictitem_T *v;
1943
1944 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001945 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1946 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001947 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001948 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001949 return FAIL;
1950 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001951 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001953 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954}
1955
1956/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001957 * When a function reference is used, fill a partial with the information
1958 * needed, especially when it is used as a closure.
1959 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001960 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001961fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001962 partial_T *pt,
1963 ufunc_T *ufunc,
1964 loopvarinfo_T *lvi,
1965 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001966{
1967 pt->pt_func = ufunc;
1968 pt->pt_refcount = 1;
1969
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001970 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001971 {
1972 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1973 + ectx->ec_dfunc_idx;
1974
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001975 // The closure may need to find arguments and local variables of the
1976 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001977 pt->pt_outer.out_stack = &ectx->ec_stack;
1978 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001979 if (ectx->ec_outer_ref != NULL)
1980 {
1981 // The current context already has a context, link to that one.
1982 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1983 if (ectx->ec_outer_ref->or_partial != NULL)
1984 {
1985 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1986 ++pt->pt_outer.out_up_partial->pt_refcount;
1987 }
1988 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001989
Bram Moolenaarcc341812022-09-19 15:54:34 +01001990 if (lvi != NULL)
1991 {
1992 int depth;
1993
1994 // The closure may need to find variables defined inside a loop,
1995 // for every nested loop. A new reference is made every time,
1996 // ISN_ENDLOOP will check if they are actually used.
1997 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1998 {
1999 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
2000 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
2001 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
2002 pt->pt_outer.out_loop[depth].var_count =
2003 lvi->lvi_loop[depth].var_count;
2004 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002005 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002006 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002007 else
2008 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002009
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002010 // If the function currently executing returns and the closure is still
2011 // being referenced, we need to make a copy of the context (arguments
2012 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002013 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02002014 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01002015 {
2016 vim_free(pt);
2017 return FAIL;
2018 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002019 // Extra variable keeps the count of closures created in the current
2020 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01002021 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
2022 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
2023
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002024 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
2025 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002026 ++pt->pt_refcount;
2027 ++ectx->ec_funcrefs.ga_len;
2028 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002029 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002030 return OK;
2031}
2032
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002033/*
2034 * Execute iptr->isn_arg.string as an Ex command.
2035 */
2036 static int
Ernie Raelee865f32023-09-29 19:53:55 +02002037exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002038{
2039 source_cookie_T cookie;
2040
2041 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002042 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002043 CLEAR_FIELD(cookie);
2044 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02002045 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002046 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
2047 || did_emsg)
2048 return FAIL;
2049 return OK;
2050}
2051
Bram Moolenaar89445512022-04-14 12:58:23 +01002052/*
2053 * If script "sid" is not loaded yet then load it now.
2054 * Caller must make sure "sid" is a valid script ID.
2055 * "loaded" is set to TRUE if the script had to be loaded.
2056 * Returns FAIL if loading fails, OK if already loaded or loaded now.
2057 */
2058 int
2059may_load_script(int sid, int *loaded)
2060{
2061 scriptitem_T *si = SCRIPT_ITEM(sid);
2062
2063 if (si->sn_state == SN_STATE_NOT_LOADED)
2064 {
2065 *loaded = TRUE;
2066 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
2067 {
2068 semsg(_(e_cant_open_file_str), si->sn_name);
2069 return FAIL;
2070 }
2071 }
2072 return OK;
2073}
2074
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002075// used for v_instr of typval of VAR_INSTR
2076struct instr_S {
2077 ectx_T *instr_ectx;
2078 isn_T *instr_instr;
2079};
2080
Bram Moolenaar4c137212021-04-19 16:48:48 +02002081// used for substitute_instr
2082typedef struct subs_expr_S {
2083 ectx_T *subs_ectx;
2084 isn_T *subs_instr;
2085 int subs_status;
2086} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002088// Set when calling do_debug().
2089static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002090static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002091
2092/*
2093 * When debugging lookup "name" and return the typeval.
2094 * When not found return NULL.
2095 */
2096 typval_T *
2097lookup_debug_var(char_u *name)
2098{
2099 int idx;
2100 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002101 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002102 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002103 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002104
2105 if (ectx == NULL)
2106 return NULL;
2107 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2108
2109 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002110 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002111 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002112 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2113
2114 // the variable name may be NULL when not available in this block
2115 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002116 return STACK_TV_VAR(idx);
2117 }
2118
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002119 // Go through argument names.
2120 ufunc = dfunc->df_ufunc;
2121 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2122 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2123 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2124 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2125 - varargs_off + idx);
2126 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2127 return STACK_TV(ectx->ec_frame_idx - 1);
2128
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002129 return NULL;
2130}
2131
Bram Moolenaar26a44842021-09-02 18:49:06 +02002132/*
2133 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2134 * breakpoint was set in that function or when there is any expression.
2135 */
2136 int
2137may_break_in_function(ufunc_T *ufunc)
2138{
2139 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2140}
2141
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002142 static void
2143handle_debug(isn_T *iptr, ectx_T *ectx)
2144{
2145 char_u *line;
2146 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2147 + ectx->ec_dfunc_idx)->df_ufunc;
2148 isn_T *ni;
2149 int end_lnum = iptr->isn_lnum;
2150 garray_T ga;
2151 int lnum;
2152
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002153 if (ex_nesting_level > debug_break_level)
2154 {
2155 linenr_T breakpoint;
2156
Bram Moolenaar26a44842021-09-02 18:49:06 +02002157 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002158 return;
2159
2160 // check for the next breakpoint if needed
2161 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002162 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002163 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2164 return;
2165 }
2166
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002167 SOURCING_LNUM = iptr->isn_lnum;
2168 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002169 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002170
2171 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2172 if (ni->isn_type == ISN_DEBUG
2173 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002174 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002175 || ni->isn_type == ISN_RETURN_VOID)
2176 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002177 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002178 break;
2179 }
2180
2181 if (end_lnum > iptr->isn_lnum)
2182 {
2183 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002184 for (lnum = iptr->isn_lnum; lnum < end_lnum
2185 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002186 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002187 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002188
Bram Moolenaar303215d2021-07-07 20:10:43 +02002189 if (p == NULL)
2190 continue; // left over from continuation line
2191 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002192 if (*p == '#')
2193 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002194 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002195 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2196 if (STRNCMP(p, "def ", 4) == 0)
2197 break;
2198 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002199 line = ga_concat_strings(&ga, " ");
2200 vim_free(ga.ga_data);
2201 }
2202 else
2203 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002204
Dominique Pelle6e969552021-06-17 13:53:41 +02002205 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002206 debug_context = NULL;
2207
2208 if (end_lnum > iptr->isn_lnum)
2209 vim_free(line);
2210}
2211
Bram Moolenaar4c137212021-04-19 16:48:48 +02002212/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002213 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002214 * Returns OK, FAIL or NOTDONE (uncatchable error).
2215 */
2216 static int
2217execute_storeindex(isn_T *iptr, ectx_T *ectx)
2218{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002219 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002220 typval_T *tv;
2221 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002222 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002223 typval_T *tv_dest = STACK_TV_BOT(-1);
2224 int status = OK;
2225
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002226 if (tv_idx->v_type == VAR_NUMBER)
2227 lidx = (long)tv_idx->vval.v_number;
2228
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002229 // Stack contains:
2230 // -3 value to be stored
2231 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002232 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002233 tv = STACK_TV_BOT(-3);
2234 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002235
2236 // Make sure an object has been initialized
2237 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2238 {
2239 emsg(_(e_using_null_object));
2240 status = FAIL;
2241 }
2242 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002243 {
2244 dest_type = tv_dest->v_type;
2245 if (dest_type == VAR_DICT)
2246 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002247 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2248 {
2249 // Need to get the member index now that the class is known.
2250 object_T *obj = tv_dest->vval.v_object;
2251 class_T *cl = obj->obj_class;
2252 char_u *member = tv_idx->vval.v_string;
2253
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002254 int m_idx;
2255 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2256 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002257 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002258 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002259 {
Ernie Rael03042a22023-11-11 08:53:32 +01002260 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +02002261 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002262 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002263 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002264
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002265 lidx = m_idx;
2266 }
2267 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002268 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002269 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002270 status = FAIL;
2271 }
2272 }
2273 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2274 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002275 {
2276 emsg(_(e_number_expected));
2277 status = FAIL;
2278 }
2279 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002280
2281 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002282 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002283 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002284 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002285 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002286
Bram Moolenaare08be092022-02-17 13:08:26 +00002287 if (list == NULL)
2288 {
2289 emsg(_(e_list_not_set));
2290 return FAIL;
2291 }
2292 if (lidx < 0 && list->lv_len + lidx >= 0)
2293 // negative index is relative to the end
2294 lidx = list->lv_len + lidx;
2295 if (lidx < 0 || lidx > list->lv_len)
2296 {
2297 semsg(_(e_list_index_out_of_range_nr), lidx);
2298 return FAIL;
2299 }
2300 if (lidx < list->lv_len)
2301 {
2302 listitem_T *li = list_find(list, lidx);
2303
2304 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002305 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002306 return FAIL;
2307 // overwrite existing list item
2308 clear_tv(&li->li_tv);
2309 li->li_tv = *tv;
2310 }
2311 else
2312 {
2313 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2314 return FAIL;
2315 // append to list, only fails when out of memory
2316 if (list_append_tv(list, tv) == FAIL)
2317 return NOTDONE;
2318 clear_tv(tv);
2319 }
2320 }
2321 else if (dest_type == VAR_DICT)
2322 {
2323 char_u *key = tv_idx->vval.v_string;
2324 dict_T *dict = tv_dest->vval.v_dict;
2325 dictitem_T *di;
2326
2327 SOURCING_LNUM = iptr->isn_lnum;
2328 if (dict == NULL)
2329 {
2330 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002331 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002332 }
2333 if (key == NULL)
2334 key = (char_u *)"";
2335 di = dict_find(dict, key, -1);
2336 if (di != NULL)
2337 {
2338 if (error_if_locked(di->di_tv.v_lock,
2339 e_cannot_change_dict_item))
2340 return FAIL;
2341 // overwrite existing value
2342 clear_tv(&di->di_tv);
2343 di->di_tv = *tv;
2344 }
2345 else
2346 {
2347 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2348 return FAIL;
2349 // add to dict, only fails when out of memory
2350 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2351 return NOTDONE;
2352 clear_tv(tv);
2353 }
2354 }
2355 else if (dest_type == VAR_BLOB)
2356 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002357 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002358 varnumber_T nr;
2359 int error = FALSE;
2360 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002361
2362 if (blob == NULL)
2363 {
2364 emsg(_(e_blob_not_set));
2365 return FAIL;
2366 }
2367 len = blob_len(blob);
2368 if (lidx < 0 && len + lidx >= 0)
2369 // negative index is relative to the end
2370 lidx = len + lidx;
2371
2372 // Can add one byte at the end.
2373 if (lidx < 0 || lidx > len)
2374 {
2375 semsg(_(e_blob_index_out_of_range_nr), lidx);
2376 return FAIL;
2377 }
2378 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2379 return FAIL;
2380 nr = tv_get_number_chk(tv, &error);
2381 if (error)
2382 return FAIL;
2383 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002384 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002385 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2386 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002387 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002388
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002389 if (dest_type == VAR_OBJECT)
2390 {
2391 object_T *obj = tv_dest->vval.v_object;
2392
2393 otv = (typval_T *)(obj + 1);
2394 class_T *itf = iptr->isn_arg.storeindex.si_class;
2395 if (itf != NULL)
2396 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002397 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002398 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002399 }
2400 else
2401 {
2402 // VAR_CLASS
2403 class_T *class = tv_dest->vval.v_class;
2404 otv = class->class_members_tv;
2405 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002406
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002407 clear_tv(&otv[lidx]);
2408 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002409 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002410 else
2411 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002412 status = FAIL;
2413 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002414 }
2415 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002416
2417 clear_tv(tv_idx);
2418 clear_tv(tv_dest);
2419 ectx->ec_stack.ga_len -= 3;
2420 if (status == FAIL)
2421 {
2422 clear_tv(tv);
2423 return FAIL;
2424 }
2425 return OK;
2426}
2427
2428/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002429 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002430 */
2431 static int
2432execute_storerange(isn_T *iptr, ectx_T *ectx)
2433{
2434 typval_T *tv;
2435 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2436 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2437 typval_T *tv_dest = STACK_TV_BOT(-1);
2438 int status = OK;
2439
2440 // Stack contains:
2441 // -4 value to be stored
2442 // -3 first index or "none"
2443 // -2 second index or "none"
2444 // -1 destination list or blob
2445 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002446 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002447 if (tv_dest->v_type == VAR_LIST)
2448 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002449 long n1;
2450 long n2;
2451 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002452
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002453 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2454 if (tv_idx2->v_type == VAR_SPECIAL
2455 && tv_idx2->vval.v_number == VVAL_NONE)
2456 n2 = list_len(tv_dest->vval.v_list) - 1;
2457 else
2458 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2459
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002460 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002461 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002462 status = FAIL;
2463 else
2464 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002465 status = check_range_index_two(tv_dest->vval.v_list,
2466 &n1, li1, &n2, FALSE);
2467 if (status != FAIL)
2468 status = list_assign_range(
2469 tv_dest->vval.v_list,
2470 tv->vval.v_list,
2471 n1,
2472 n2,
2473 tv_idx2->v_type == VAR_SPECIAL,
2474 (char_u *)"=",
2475 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002476 }
2477 }
2478 else if (tv_dest->v_type == VAR_BLOB)
2479 {
2480 varnumber_T n1;
2481 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002482 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002483
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002484 n1 = tv_get_number_chk(tv_idx1, NULL);
2485 if (tv_idx2->v_type == VAR_SPECIAL
2486 && tv_idx2->vval.v_number == VVAL_NONE)
2487 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2488 else
2489 n2 = tv_get_number_chk(tv_idx2, NULL);
2490 bloblen = blob_len(tv_dest->vval.v_blob);
2491
2492 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2493 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002494 status = FAIL;
2495 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002496 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002497 }
2498 else
2499 {
2500 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002501 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002502 }
2503
2504 clear_tv(tv_idx1);
2505 clear_tv(tv_idx2);
2506 clear_tv(tv_dest);
2507 ectx->ec_stack.ga_len -= 4;
2508 clear_tv(tv);
2509
2510 return status;
2511}
2512
2513/*
2514 * Unlet item in list or dict variable.
2515 */
2516 static int
2517execute_unletindex(isn_T *iptr, ectx_T *ectx)
2518{
2519 typval_T *tv_idx = STACK_TV_BOT(-2);
2520 typval_T *tv_dest = STACK_TV_BOT(-1);
2521 int status = OK;
2522
2523 // Stack contains:
2524 // -2 index
2525 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002526 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002527 if (tv_dest->v_type == VAR_DICT)
2528 {
2529 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002530 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002531 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002532 semsg(_(e_expected_str_but_got_str),
2533 vartype_name(VAR_STRING),
2534 vartype_name(tv_idx->v_type));
2535 status = FAIL;
2536 }
2537 else
2538 {
2539 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002540 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002541 dictitem_T *di = NULL;
2542
2543 if (d != NULL && value_check_lock(
2544 d->dv_lock, NULL, FALSE))
2545 status = FAIL;
2546 else
2547 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002548 if (tv_idx->v_type == VAR_STRING)
2549 {
2550 key = tv_idx->vval.v_string;
2551 if (key == NULL)
2552 key = (char_u *)"";
2553 }
2554 else
2555 {
2556 key = tv_get_string(tv_idx);
2557 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002558 if (d != NULL)
2559 di = dict_find(d, key, (int)STRLEN(key));
2560 if (di == NULL)
2561 {
2562 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002563 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002564 status = FAIL;
2565 }
2566 else if (var_check_fixed(di->di_flags,
2567 NULL, FALSE)
2568 || var_check_ro(di->di_flags,
2569 NULL, FALSE))
2570 status = FAIL;
2571 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002572 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002573 }
2574 }
2575 }
2576 else if (tv_dest->v_type == VAR_LIST)
2577 {
2578 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002579 if (check_for_number(tv_idx) == FAIL)
2580 {
2581 status = FAIL;
2582 }
2583 else
2584 {
2585 list_T *l = tv_dest->vval.v_list;
2586 long n = (long)tv_idx->vval.v_number;
2587
2588 if (l != NULL && value_check_lock(
2589 l->lv_lock, NULL, FALSE))
2590 status = FAIL;
2591 else
2592 {
2593 listitem_T *li = list_find(l, n);
2594
2595 if (li == NULL)
2596 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002597 semsg(_(e_list_index_out_of_range_nr), n);
2598 status = FAIL;
2599 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002600 else
2601 listitem_remove(l, li);
2602 }
2603 }
2604 }
2605 else
2606 {
2607 status = FAIL;
2608 semsg(_(e_cannot_index_str),
2609 vartype_name(tv_dest->v_type));
2610 }
2611
2612 clear_tv(tv_idx);
2613 clear_tv(tv_dest);
2614 ectx->ec_stack.ga_len -= 2;
2615
2616 return status;
2617}
2618
2619/*
2620 * Unlet a range of items in a list variable.
2621 */
2622 static int
2623execute_unletrange(isn_T *iptr, ectx_T *ectx)
2624{
2625 // Stack contains:
2626 // -3 index1
2627 // -2 index2
2628 // -1 dict or list
2629 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2630 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2631 typval_T *tv_dest = STACK_TV_BOT(-1);
2632 int status = OK;
2633
2634 if (tv_dest->v_type == VAR_LIST)
2635 {
2636 // indexes must be a number
2637 SOURCING_LNUM = iptr->isn_lnum;
2638 if (check_for_number(tv_idx1) == FAIL
2639 || (tv_idx2->v_type != VAR_SPECIAL
2640 && check_for_number(tv_idx2) == FAIL))
2641 {
2642 status = FAIL;
2643 }
2644 else
2645 {
2646 list_T *l = tv_dest->vval.v_list;
2647 long n1 = (long)tv_idx1->vval.v_number;
2648 long n2 = tv_idx2->v_type == VAR_SPECIAL
2649 ? 0 : (long)tv_idx2->vval.v_number;
2650 listitem_T *li;
2651
2652 li = list_find_index(l, &n1);
2653 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002654 {
2655 semsg(_(e_list_index_out_of_range_nr),
2656 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002657 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002658 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002659 else
2660 {
2661 if (n1 < 0)
2662 n1 = list_idx_of_item(l, li);
2663 if (n2 < 0)
2664 {
2665 listitem_T *li2 = list_find(l, n2);
2666
2667 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002668 {
2669 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002670 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002671 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002672 else
2673 n2 = list_idx_of_item(l, li2);
2674 }
2675 if (status != FAIL
2676 && tv_idx2->v_type != VAR_SPECIAL
2677 && n2 < n1)
2678 {
2679 semsg(_(e_list_index_out_of_range_nr), n2);
2680 status = FAIL;
2681 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002682 if (status != FAIL)
2683 list_unlet_range(l, li, n1,
2684 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002685 }
2686 }
2687 }
2688 else
2689 {
2690 status = FAIL;
2691 SOURCING_LNUM = iptr->isn_lnum;
2692 semsg(_(e_cannot_index_str),
2693 vartype_name(tv_dest->v_type));
2694 }
2695
2696 clear_tv(tv_idx1);
2697 clear_tv(tv_idx2);
2698 clear_tv(tv_dest);
2699 ectx->ec_stack.ga_len -= 3;
2700
2701 return status;
2702}
2703
2704/*
2705 * Top of a for loop.
2706 */
2707 static int
2708execute_for(isn_T *iptr, ectx_T *ectx)
2709{
2710 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002711 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002712 typval_T *ltv = STACK_TV_BOT(-1);
2713 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002714 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002715
2716 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2717 return FAIL;
2718 if (ltv->v_type == VAR_LIST)
2719 {
2720 list_T *list = ltv->vval.v_list;
2721
2722 // push the next item from the list
2723 ++idxtv->vval.v_number;
2724 if (list == NULL
2725 || idxtv->vval.v_number >= list->lv_len)
2726 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002727 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002728 }
2729 else if (list->lv_first == &range_list_item)
2730 {
2731 // non-materialized range() list
2732 tv = STACK_TV_BOT(0);
2733 tv->v_type = VAR_NUMBER;
2734 tv->v_lock = 0;
2735 tv->vval.v_number = list_find_nr(
2736 list, idxtv->vval.v_number, NULL);
2737 ++ectx->ec_stack.ga_len;
2738 }
2739 else
2740 {
2741 listitem_T *li = list_find(list,
2742 idxtv->vval.v_number);
2743
2744 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2745 ++ectx->ec_stack.ga_len;
2746 }
2747 }
2748 else if (ltv->v_type == VAR_STRING)
2749 {
2750 char_u *str = ltv->vval.v_string;
2751
2752 // The index is for the last byte of the previous
2753 // character.
2754 ++idxtv->vval.v_number;
2755 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2756 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002757 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002758 }
2759 else
2760 {
2761 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2762
2763 // Push the next character from the string.
2764 tv = STACK_TV_BOT(0);
2765 tv->v_type = VAR_STRING;
2766 tv->vval.v_string = vim_strnsave(
2767 str + idxtv->vval.v_number, clen);
2768 ++ectx->ec_stack.ga_len;
2769 idxtv->vval.v_number += clen - 1;
2770 }
2771 }
2772 else if (ltv->v_type == VAR_BLOB)
2773 {
2774 blob_T *blob = ltv->vval.v_blob;
2775
2776 // When we get here the first time make a copy of the
2777 // blob, so that the iteration still works when it is
2778 // changed.
2779 if (idxtv->vval.v_number == -1 && blob != NULL)
2780 {
2781 blob_copy(blob, ltv);
2782 blob_unref(blob);
2783 blob = ltv->vval.v_blob;
2784 }
2785
2786 // The index is for the previous byte.
2787 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002788 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002789 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002790 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002791 }
2792 else
2793 {
2794 // Push the next byte from the blob.
2795 tv = STACK_TV_BOT(0);
2796 tv->v_type = VAR_NUMBER;
2797 tv->vval.v_number = blob_get(blob,
2798 idxtv->vval.v_number);
2799 ++ectx->ec_stack.ga_len;
2800 }
2801 }
2802 else
2803 {
2804 semsg(_(e_for_loop_on_str_not_supported),
2805 vartype_name(ltv->v_type));
2806 return FAIL;
2807 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002808
2809 if (jump)
2810 {
2811 // past the end of the list/string/blob, jump to "endfor"
2812 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2813 may_restore_cmdmod(&ectx->ec_funclocal);
2814 }
2815 else
2816 {
2817 // Store the current number of funcrefs, this may be used in
2818 // ISN_LOOPEND. The variable index is always one more than the loop
2819 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002820 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002821 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2822 }
2823
2824 return OK;
2825}
2826
2827/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002828 * Code for handling variables declared inside a loop and used in a closure.
2829 * This is very similar to what is done with funcstack_T. The difference is
2830 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2831 * scope of the block inside a loop and each loop may have its own.
2832 */
2833
2834// Double linked list of loopvars_T in use.
2835static loopvars_T *first_loopvars = NULL;
2836
2837 static void
2838add_loopvars_to_list(loopvars_T *loopvars)
2839{
2840 // Link in list of loopvarss.
2841 if (first_loopvars != NULL)
2842 first_loopvars->lvs_prev = loopvars;
2843 loopvars->lvs_next = first_loopvars;
2844 loopvars->lvs_prev = NULL;
2845 first_loopvars = loopvars;
2846}
2847
2848 static void
2849remove_loopvars_from_list(loopvars_T *loopvars)
2850{
2851 if (loopvars->lvs_prev == NULL)
2852 first_loopvars = loopvars->lvs_next;
2853 else
2854 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2855 if (loopvars->lvs_next != NULL)
2856 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2857}
2858
2859/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002860 * End of a for or while loop: Handle any variables used by a closure.
2861 */
2862 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002863execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002864{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002865 endloop_T *endloop = &iptr->isn_arg.endloop;
2866 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2867 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002868 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002869 garray_T *gap = &ectx->ec_funcrefs;
2870 int closure_in_use = FALSE;
2871 loopvars_T *loopvars;
2872 typval_T *stack;
2873 int idx;
2874
Bram Moolenaarcc341812022-09-19 15:54:34 +01002875 // Check if any created closure is still being referenced and loopvars have
2876 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002877 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2878 {
2879 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2880
Bram Moolenaarcc341812022-09-19 15:54:34 +01002881 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002882 {
2883 int refcount = pt->pt_refcount;
2884 int i;
2885
2886 // A Reference in a variable inside the loop doesn't count, it gets
2887 // unreferenced at the end of the loop.
2888 for (i = 0; i < endloop->end_var_count; ++i)
2889 {
2890 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2891
2892 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2893 --refcount;
2894 }
2895 if (refcount > 1)
2896 {
2897 closure_in_use = TRUE;
2898 break;
2899 }
2900 }
2901 }
2902
2903 // If no function reference were created since the start of the loop block
2904 // or it is no longer referenced there is nothing to do.
2905 if (!closure_in_use)
2906 return OK;
2907
2908 // A closure is using variables declared inside the loop.
2909 // Move them to the called function.
2910 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2911 if (loopvars == NULL)
2912 return FAIL;
2913
2914 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2915 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2916 loopvars->lvs_ga.ga_data = stack;
2917 if (stack == NULL)
2918 {
2919 vim_free(loopvars);
2920 return FAIL;
2921 }
2922 add_loopvars_to_list(loopvars);
2923
2924 // Move the variable values.
2925 for (idx = 0; idx < endloop->end_var_count; ++idx)
2926 {
2927 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2928
2929 *(stack + idx) = *tv;
2930 tv->v_type = VAR_UNKNOWN;
2931 }
2932
2933 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2934 {
2935 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2936
Bram Moolenaarcc341812022-09-19 15:54:34 +01002937 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002938 {
2939 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002940 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002941
Bram Moolenaarcc341812022-09-19 15:54:34 +01002942 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2943 pt->pt_outer.out_loop[depth].var_idx -=
2944 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002945 }
2946 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002947
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002948 return OK;
2949}
2950
2951/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002952 * Called when a partial is freed or its reference count goes down to one. The
2953 * loopvars may be the only reference to the partials in the local variables.
2954 * Go over all of them, the funcref and can be freed if all partials
2955 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002956 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002957 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002958 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002959loopvars_check_refcount(loopvars_T *loopvars)
2960{
2961 int i;
2962 garray_T *gap = &loopvars->lvs_ga;
2963 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002964 typval_T *stack = gap->ga_data;
2965
Bram Moolenaarcc341812022-09-19 15:54:34 +01002966 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2967 return FALSE;
2968 for (i = 0; i < gap->ga_len; ++i)
2969 {
2970 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2971
2972 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2973 && tv->vval.v_partial->pt_refcount == 1)
2974 {
2975 int depth;
2976
2977 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2978 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2979 ++done;
2980 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002981 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002982 if (done != loopvars->lvs_min_refcount)
2983 return FALSE;
2984
2985 // All partials referencing the loopvars have a reference count of
2986 // one, thus the loopvars is no longer of use.
2987 stack = gap->ga_data;
2988 for (i = 0; i < gap->ga_len; ++i)
2989 clear_tv(stack + i);
2990 vim_free(stack);
2991 remove_loopvars_from_list(loopvars);
2992 vim_free(loopvars);
2993 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002994}
2995
2996/*
2997 * For garbage collecting: set references in all variables referenced by
2998 * all loopvars.
2999 */
3000 int
3001set_ref_in_loopvars(int copyID)
3002{
3003 loopvars_T *loopvars;
3004
3005 for (loopvars = first_loopvars; loopvars != NULL;
3006 loopvars = loopvars->lvs_next)
3007 {
3008 typval_T *stack = loopvars->lvs_ga.ga_data;
3009 int i;
3010
3011 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
3012 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
3013 return TRUE; // abort
3014 }
3015 return FALSE;
3016}
3017
3018/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00003019 * Load instruction for w:/b:/g:/t: variable.
3020 * "isn_type" is used instead of "iptr->isn_type".
3021 */
3022 static int
3023load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
3024{
3025 dictitem_T *di = NULL;
3026 hashtab_T *ht = NULL;
3027 char namespace;
3028
3029 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3030 return NOTDONE;
3031 switch (isn_type)
3032 {
3033 case ISN_LOADG:
3034 ht = get_globvar_ht();
3035 namespace = 'g';
3036 break;
3037 case ISN_LOADB:
3038 ht = &curbuf->b_vars->dv_hashtab;
3039 namespace = 'b';
3040 break;
3041 case ISN_LOADW:
3042 ht = &curwin->w_vars->dv_hashtab;
3043 namespace = 'w';
3044 break;
3045 case ISN_LOADT:
3046 ht = &curtab->tp_vars->dv_hashtab;
3047 namespace = 't';
3048 break;
3049 default: // Cannot reach here
3050 return NOTDONE;
3051 }
3052 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3053
Bram Moolenaar06b77222022-01-25 15:51:56 +00003054 if (di == NULL)
3055 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003056 if (isn_type == ISN_LOADG)
3057 {
3058 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3059
3060 // g:Something could be a function
3061 if (ufunc != NULL)
3062 {
3063 typval_T *tv = STACK_TV_BOT(0);
3064
3065 ++ectx->ec_stack.ga_len;
3066 tv->v_type = VAR_FUNC;
3067 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3068 if (tv->vval.v_string == NULL)
3069 return FAIL;
3070 STRCPY(tv->vval.v_string, "g:");
3071 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3072 return OK;
3073 }
3074 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003075 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003076 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003077 // no check if the item exists in the script but
3078 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003079 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003080 else
3081 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003082 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003083 return FAIL;
3084 }
3085 else
3086 {
3087 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3088 ++ectx->ec_stack.ga_len;
3089 }
3090 return OK;
3091}
3092
Bram Moolenaard0200c82023-01-28 15:19:40 +00003093
3094 static void
3095object_required_error(typval_T *tv)
3096{
3097 garray_T type_list;
3098 ga_init2(&type_list, sizeof(type_T *), 10);
3099 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3100 char *tofree = NULL;
3101 char *typename = type_name(type, &tofree);
3102 semsg(_(e_object_required_found_str), typename);
3103 vim_free(tofree);
3104 clear_type_list(&type_list);
3105}
3106
Bram Moolenaar06b77222022-01-25 15:51:56 +00003107/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003108 * Execute instructions in execution context "ectx".
3109 * Return OK or FAIL;
3110 */
3111 static int
3112exec_instructions(ectx_T *ectx)
3113{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003114 int ret = FAIL;
3115 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003116 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003117
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003118 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003119 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003120
Bram Moolenaarff652882021-05-16 15:24:49 +02003121 // Only catch exceptions in this instruction list.
3122 ectx->ec_trylevel_at_start = trylevel;
3123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 for (;;)
3125 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003126 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003128 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003129
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003130 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003131 {
3132 line_breakcheck();
3133 breakcheck_count = 0;
3134 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003135 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003136 {
3137 // Turn CTRL-C into an exception.
3138 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003139 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003140 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003141 did_throw = TRUE;
3142 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003144 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003145 {
3146 // Turn an error message into an exception.
3147 did_emsg = FALSE;
3148 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003149 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003150 did_throw = TRUE;
3151 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003152
3153 // This exception was not caught (yet).
3154 garray_T *trystack = &ectx->ec_trystack;
3155 if (trystack->ga_len > 0)
3156 {
3157 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3158 + trystack->ga_len - 1;
3159 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3160 trycmd->tcd_caught = FALSE;
3161 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003162 }
3163
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003164 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003166 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003167 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003168 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169
3170 // An exception jumps to the first catch, finally, or returns from
3171 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003172 while (index > 0)
3173 {
3174 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003175 // 1. after :try and before :catch - jump to first :catch
3176 // 2. in :catch block - jump to :finally
3177 // 3. in :catch block and no finally - jump to :endtry
3178 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3179 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003180 break;
3181 // In the catch and finally block of this try we have to go up
3182 // one level.
3183 --index;
3184 trycmd = NULL;
3185 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003186 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003188 if (trycmd->tcd_in_catch)
3189 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003190 if (trycmd->tcd_finally_idx > 0)
3191 {
3192 // exception inside ":catch", jump to ":finally" once
3193 ectx->ec_iidx = trycmd->tcd_finally_idx;
3194 trycmd->tcd_finally_idx = 0;
3195 }
3196 else
3197 {
3198 // exception inside ":catch" or ":finally", jump to
3199 // ":endtry"
3200 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3201 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003202 }
3203 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003204 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003205 // jump to first ":catch"
3206 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003207 trycmd->tcd_in_catch = TRUE;
3208 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003209 did_throw = FALSE; // don't come back here until :endtry
3210 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 }
3212 else
3213 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003214 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003215 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003216 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003217 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003218 tv = STACK_TV_BOT(0);
3219 tv->v_type = VAR_NUMBER;
3220 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003221 ++ectx->ec_stack.ga_len;
3222 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003224 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003225 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003226 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003227 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 goto done;
3229 }
3230
Bram Moolenaar4c137212021-04-19 16:48:48 +02003231 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003232 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 }
3234 continue;
3235 }
3236
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003237 /*
3238 * Big switch on the instruction. Most compilers will be turning this
3239 * into an efficient lookup table, since the "case" values are an enum
3240 * with sequential numbers. It may look ugly, but it should be the
3241 * most efficient way.
3242 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003243 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 switch (iptr->isn_type)
3245 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003246 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003247 case ISN_CONSTRUCT:
3248 // "this" is always the local variable at index zero
3249 tv = STACK_TV_VAR(0);
3250 tv->v_type = VAR_OBJECT;
3251 tv->vval.v_object = alloc_clear(
3252 iptr->isn_arg.construct.construct_size);
3253 tv->vval.v_object->obj_class =
3254 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003255 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003256 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003257 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003258 break;
3259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 // execute Ex command line
3261 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003262 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003263 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 break;
3265
Bram Moolenaar20677332021-06-06 17:02:53 +02003266 // execute Ex command line split at NL characters.
3267 case ISN_EXEC_SPLIT:
3268 {
3269 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003270 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003271
3272 SOURCING_LNUM = iptr->isn_lnum;
3273 CLEAR_FIELD(cookie);
3274 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3275 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003276 line = get_split_sourceline(0, &cookie, 0, 0);
3277 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003278 get_split_sourceline, &cookie,
3279 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3280 == FAIL
3281 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003282 {
3283 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003284 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003285 }
3286 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003287 }
3288 break;
3289
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003290 // execute Ex command line that is only a range
3291 case ISN_EXECRANGE:
3292 {
3293 exarg_T ea;
3294 char *error = NULL;
3295
3296 CLEAR_FIELD(ea);
3297 ea.cmdidx = CMD_SIZE;
3298 ea.addr_type = ADDR_LINES;
3299 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003300 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003301 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003302 if (ea.cmd == NULL)
3303 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003304 // error is always NULL when using ADDR_LINES
3305 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003306 if (error != NULL)
3307 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003308 emsg(error);
3309 goto on_error;
3310 }
3311 }
3312 break;
3313
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003314 // Evaluate an expression with legacy syntax, push it onto the
3315 // stack.
3316 case ISN_LEGACY_EVAL:
3317 {
3318 char_u *arg = iptr->isn_arg.string;
3319 int res;
3320 int save_flags = cmdmod.cmod_flags;
3321
Bram Moolenaar35578162021-08-02 19:10:38 +02003322 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003323 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003324 tv = STACK_TV_BOT(0);
3325 init_tv(tv);
3326 cmdmod.cmod_flags |= CMOD_LEGACY;
3327 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3328 cmdmod.cmod_flags = save_flags;
3329 if (res == FAIL)
3330 goto on_error;
3331 ++ectx->ec_stack.ga_len;
3332 }
3333 break;
3334
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003335 // push typeval VAR_INSTR with instructions to be executed
3336 case ISN_INSTR:
3337 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003338 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003339 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003340 tv = STACK_TV_BOT(0);
3341 tv->vval.v_instr = ALLOC_ONE(instr_T);
3342 if (tv->vval.v_instr == NULL)
3343 goto on_error;
3344 ++ectx->ec_stack.ga_len;
3345
3346 tv->v_type = VAR_INSTR;
3347 tv->vval.v_instr->instr_ectx = ectx;
3348 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3349 }
3350 break;
3351
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003352 case ISN_SOURCE:
3353 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003354 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003355
Bram Moolenaar89445512022-04-14 12:58:23 +01003356 SOURCING_LNUM = iptr->isn_lnum;
3357 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003358 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003359 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003360 }
3361 break;
3362
Bram Moolenaar4c137212021-04-19 16:48:48 +02003363 // execute :substitute with an expression
3364 case ISN_SUBSTITUTE:
3365 {
3366 subs_T *subs = &iptr->isn_arg.subs;
3367 source_cookie_T cookie;
3368 struct subs_expr_S *save_instr = substitute_instr;
3369 struct subs_expr_S subs_instr;
3370 int res;
3371
3372 subs_instr.subs_ectx = ectx;
3373 subs_instr.subs_instr = subs->subs_instr;
3374 subs_instr.subs_status = OK;
3375 substitute_instr = &subs_instr;
3376
3377 SOURCING_LNUM = iptr->isn_lnum;
3378 // This is very much like ISN_EXEC
3379 CLEAR_FIELD(cookie);
3380 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3381 res = do_cmdline(subs->subs_cmd,
3382 getsourceline, &cookie,
3383 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3384 substitute_instr = save_instr;
3385
3386 if (res == FAIL || did_emsg
3387 || subs_instr.subs_status == FAIL)
3388 goto on_error;
3389 }
3390 break;
3391
3392 case ISN_FINISH:
3393 goto done;
3394
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003395 case ISN_REDIRSTART:
3396 // create a dummy entry for var_redir_str()
3397 if (alloc_redir_lval() == FAIL)
3398 goto on_error;
3399
3400 // The output is stored in growarray "redir_ga" until
3401 // redirection ends.
3402 init_redir_ga();
3403 redir_vname = 1;
3404 break;
3405
3406 case ISN_REDIREND:
3407 {
3408 char_u *res = get_clear_redir_ga();
3409
3410 // End redirection, put redirected text on the stack.
3411 clear_redir_lval();
3412 redir_vname = 0;
3413
Bram Moolenaar35578162021-08-02 19:10:38 +02003414 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003415 {
3416 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003417 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003418 }
3419 tv = STACK_TV_BOT(0);
3420 tv->v_type = VAR_STRING;
3421 tv->vval.v_string = res;
3422 ++ectx->ec_stack.ga_len;
3423 }
3424 break;
3425
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003426 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003427#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003428 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003429 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3430 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003431 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003432#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003433 break;
3434
3435 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003436#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003437 {
3438 exarg_T ea;
3439 int res;
3440
3441 CLEAR_FIELD(ea);
3442 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3443 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3444 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3445 --ectx->ec_stack.ga_len;
3446 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003447 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003448 res = cexpr_core(&ea, tv);
3449 clear_tv(tv);
3450 if (res == FAIL)
3451 goto on_error;
3452 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003453#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003454 break;
3455
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003456 // execute Ex command from pieces on the stack
3457 case ISN_EXECCONCAT:
3458 {
3459 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003460 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003461 int pass;
3462 int i;
3463 char_u *cmd = NULL;
3464 char_u *str;
3465
3466 for (pass = 1; pass <= 2; ++pass)
3467 {
3468 for (i = 0; i < count; ++i)
3469 {
3470 tv = STACK_TV_BOT(i - count);
3471 str = tv->vval.v_string;
3472 if (str != NULL && *str != NUL)
3473 {
3474 if (pass == 2)
3475 STRCPY(cmd + len, str);
3476 len += STRLEN(str);
3477 }
3478 if (pass == 2)
3479 clear_tv(tv);
3480 }
3481 if (pass == 1)
3482 {
3483 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003484 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003485 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003486 len = 0;
3487 }
3488 }
3489
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003490 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003491 do_cmdline_cmd(cmd);
3492 vim_free(cmd);
3493 }
3494 break;
3495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 // execute :echo {string} ...
3497 case ISN_ECHO:
3498 {
3499 int count = iptr->isn_arg.echo.echo_count;
3500 int atstart = TRUE;
3501 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003502 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503
3504 for (idx = 0; idx < count; ++idx)
3505 {
3506 tv = STACK_TV_BOT(idx - count);
3507 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3508 &atstart, &needclr);
3509 clear_tv(tv);
3510 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003511 if (needclr)
3512 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003513 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 }
3515 break;
3516
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003517 // :execute {string} ...
3518 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003519 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003520 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003521 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003522 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003523 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003524 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003525 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003526 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003527 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003528 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003529 garray_T ga;
3530 char_u buf[NUMBUFLEN];
3531 char_u *p;
3532 int len;
3533 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003534 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003535
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003536 if (iptr->isn_type == ISN_ECHOWINDOW)
3537 count = iptr->isn_arg.echowin.ewin_count;
3538 else
3539 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003540 ga_init2(&ga, 1, 80);
3541 for (idx = 0; idx < count; ++idx)
3542 {
3543 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003544 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003545 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003546 if (tv->v_type == VAR_CHANNEL
3547 || tv->v_type == VAR_JOB)
3548 {
3549 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003550 semsg(_(e_using_invalid_value_as_string_str),
3551 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003552 break;
3553 }
3554 else
3555 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003556 }
3557 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003558 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003559
3560 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003561 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003562 failed = TRUE;
3563 else
3564 {
3565 if (ga.ga_len > 0)
3566 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3567 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3568 ga.ga_len += len;
3569 }
3570 clear_tv(tv);
3571 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003572 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003573 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003574 {
3575 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003576 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003577 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003578
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003579 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003580 {
3581 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003582 {
3583 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003584 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003585 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003586 {
3587 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003588 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003589 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003590 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003591 else
3592 {
3593 msg_sb_eol();
3594 if (iptr->isn_type == ISN_ECHOMSG)
3595 {
3596 msg_attr(ga.ga_data, echo_attr);
3597 out_flush();
3598 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003599#ifdef HAS_MESSAGE_WINDOW
3600 else if (iptr->isn_type == ISN_ECHOWINDOW)
3601 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003602 start_echowindow(
3603 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003604 msg_attr(ga.ga_data, echo_attr);
3605 end_echowindow();
3606 }
3607#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003608 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3609 {
3610 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3611 TRUE);
3612 ui_write((char_u *)"\r\n", 2, TRUE);
3613 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003614 else
3615 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003616 SOURCING_LNUM = iptr->isn_lnum;
3617 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003618 }
3619 }
3620 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003621 ga_clear(&ga);
3622 }
3623 break;
3624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 // load local variable or argument
3626 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003627 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003628 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003629 tv = STACK_TV_VAR(iptr->isn_arg.number);
3630 if (tv->v_type == VAR_UNKNOWN)
3631 {
3632 // missing argument or default value v:none
3633 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3634 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3635 }
3636 else
3637 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003638 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 break;
3640
3641 // load v: variable
3642 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003643 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003644 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003646 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 break;
3648
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003649 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 case ISN_LOADSCRIPT:
3651 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003652 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 svar_T *sv;
3654
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003655 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003656 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003657 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003658 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003659 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003660 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003662 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 }
3664 break;
3665
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003666 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003668 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003670 int sid = iptr->isn_arg.loadstore.ls_sid;
3671 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003672 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 if (di == NULL)
3676 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003677 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003678 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003679 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 }
3681 else
3682 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003683 if (iptr->isn_type == ISN_LOADEXPORT)
3684 {
3685 int idx = get_script_item_idx(sid, name, 0,
3686 NULL, NULL);
3687 svar_T *sv;
3688
3689 if (idx >= 0)
3690 {
3691 sv = ((svar_T *)SCRIPT_ITEM(sid)
3692 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003693 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003694 {
3695 SOURCING_LNUM = iptr->isn_lnum;
3696 semsg(_(e_item_not_exported_in_script_str),
3697 name);
3698 goto on_error;
3699 }
3700 }
3701 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003702 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003703 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003705 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 }
3707 }
3708 break;
3709
Bram Moolenaard3aac292020-04-19 14:32:17 +02003710 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003712 case ISN_LOADB:
3713 case ISN_LOADW:
3714 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003716 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003717
Bram Moolenaar06b77222022-01-25 15:51:56 +00003718 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003719 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003720 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003721 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 break;
3725
Bram Moolenaar03290b82020-12-19 16:30:44 +01003726 // load autoload variable
3727 case ISN_LOADAUTO:
3728 {
3729 char_u *name = iptr->isn_arg.string;
3730
Bram Moolenaar35578162021-08-02 19:10:38 +02003731 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003732 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003733 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003734 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003735 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003736 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003737 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003738 }
3739 break;
3740
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003741 // load g:/b:/w:/t: namespace
3742 case ISN_LOADGDICT:
3743 case ISN_LOADBDICT:
3744 case ISN_LOADWDICT:
3745 case ISN_LOADTDICT:
3746 {
3747 dict_T *d = NULL;
3748
3749 switch (iptr->isn_type)
3750 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003751 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3752 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3753 case ISN_LOADWDICT: d = curwin->w_vars; break;
3754 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003755 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003756 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003757 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003758 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003759 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003760 tv = STACK_TV_BOT(0);
3761 tv->v_type = VAR_DICT;
3762 tv->v_lock = 0;
3763 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003764 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003765 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003766 }
3767 break;
3768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 // load &option
3770 case ISN_LOADOPT:
3771 {
3772 typval_T optval;
3773 char_u *name = iptr->isn_arg.string;
3774
Bram Moolenaara8c17702020-04-01 21:17:24 +02003775 // This is not expected to fail, name is checked during
3776 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003777 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003778 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003779 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003780 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003782 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 }
3784 break;
3785
3786 // load $ENV
3787 case ISN_LOADENV:
3788 {
3789 typval_T optval;
3790 char_u *name = iptr->isn_arg.string;
3791
Bram Moolenaar35578162021-08-02 19:10:38 +02003792 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003793 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003794 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003795 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003797 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 }
3799 break;
3800
3801 // load @register
3802 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003803 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003804 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 tv = STACK_TV_BOT(0);
3806 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003807 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003808 // This may result in NULL, which should be equivalent to an
3809 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 tv->vval.v_string = get_reg_contents(
3811 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003812 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 break;
3814
3815 // store local variable
3816 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003817 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 tv = STACK_TV_VAR(iptr->isn_arg.number);
Ernie Rael9ed53752023-12-11 17:40:46 +01003819 if (check_typval_is_value(STACK_TV_BOT(0)) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003820 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003821 clear_tv(STACK_TV_BOT(0));
3822 goto on_error;
3823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 clear_tv(tv);
3825 *tv = *STACK_TV_BOT(0);
3826 break;
3827
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003828 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003829 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003830 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003831 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003832 int sid = iptr->isn_arg.loadstore.ls_sid;
3833 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003834 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003835 dictitem_T *di = find_var_in_ht(ht, 0,
3836 iptr->isn_type == ISN_STORES
3837 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003838
Bram Moolenaar4c137212021-04-19 16:48:48 +02003839 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003840 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003841 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003842 {
3843 if (iptr->isn_type == ISN_STOREEXPORT)
3844 {
3845 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003846 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003847 goto on_error;
3848 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003849 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003850 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003851 else
3852 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003853 if (iptr->isn_type == ISN_STOREEXPORT)
3854 {
3855 int idx = get_script_item_idx(sid, name, 0,
3856 NULL, NULL);
3857
Bram Moolenaar06651632022-04-27 17:54:25 +01003858 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003859 if (idx >= 0)
3860 {
3861 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3862 ->sn_var_vals.ga_data) + idx;
3863
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003864 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003865 {
3866 semsg(_(e_item_not_exported_in_script_str),
3867 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003868 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003869 goto on_error;
3870 }
3871 }
3872 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003873 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003874 {
3875 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003876 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003877 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003878 clear_tv(&di->di_tv);
3879 di->di_tv = *STACK_TV_BOT(0);
3880 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003881 }
3882 break;
3883
3884 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 case ISN_STORESCRIPT:
3886 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003887 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3888 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003890 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003891 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003892 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003893 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003894
3895 // "const" and "final" are checked at compile time, locking
3896 // the value needs to be checked here.
3897 SOURCING_LNUM = iptr->isn_lnum;
3898 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003899 {
3900 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003901 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003902 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 clear_tv(sv->sv_tv);
3905 *sv->sv_tv = *STACK_TV_BOT(0);
3906 }
3907 break;
3908
3909 // store option
3910 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003911 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003913 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3914 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 long n = 0;
3916 char_u *s = NULL;
3917 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003918 char_u numbuf[NUMBUFLEN];
3919 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920
Bram Moolenaar4c137212021-04-19 16:48:48 +02003921 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003922 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 tv = STACK_TV_BOT(0);
3924 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003925 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003927 if (s == NULL)
3928 s = (char_u *)"";
3929 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003930 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3931 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003932 // If the option can be set to a function reference or
3933 // a lambda and the passed value is a function
3934 // reference, then convert it to the name (string) of
3935 // the function reference.
3936 s = tv2string(tv, &tofree, numbuf, 0);
3937 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003938 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003939 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003940 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003941 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003942 goto on_error;
3943 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003944 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003946 // must be VAR_NUMBER, CHECKTYPE makes sure
3947 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003948 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003949 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003950 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 if (msg != NULL)
3952 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003953 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003955 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 }
3958 break;
3959
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003960 // store $ENV
3961 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003962 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003963 tv = STACK_TV_BOT(0);
3964 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3965 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003966 break;
3967
3968 // store @r
3969 case ISN_STOREREG:
3970 {
3971 int reg = iptr->isn_arg.number;
3972
Bram Moolenaar4c137212021-04-19 16:48:48 +02003973 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003974 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003975 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003976 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003977 }
3978 break;
3979
3980 // store v: variable
3981 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003982 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003983 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3984 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003985 // should not happen, type is checked when compiling
3986 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003987 break;
3988
Bram Moolenaard3aac292020-04-19 14:32:17 +02003989 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003991 case ISN_STOREB:
3992 case ISN_STOREW:
3993 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003995 dictitem_T *di;
3996 hashtab_T *ht;
3997 char_u *name = iptr->isn_arg.string + 2;
3998
Bram Moolenaard3aac292020-04-19 14:32:17 +02003999 switch (iptr->isn_type)
4000 {
4001 case ISN_STOREG:
4002 ht = get_globvar_ht();
4003 break;
4004 case ISN_STOREB:
4005 ht = &curbuf->b_vars->dv_hashtab;
4006 break;
4007 case ISN_STOREW:
4008 ht = &curwin->w_vars->dv_hashtab;
4009 break;
4010 case ISN_STORET:
4011 ht = &curtab->tp_vars->dv_hashtab;
4012 break;
4013 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004014 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016
Bram Moolenaar4c137212021-04-19 16:48:48 +02004017 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004018 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004020 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 else
4022 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004023 SOURCING_LNUM = iptr->isn_lnum;
4024 if (var_check_permission(di, name) == FAIL)
4025 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 clear_tv(&di->di_tv);
4027 di->di_tv = *STACK_TV_BOT(0);
4028 }
4029 }
4030 break;
4031
Bram Moolenaar03290b82020-12-19 16:30:44 +01004032 // store an autoload variable
4033 case ISN_STOREAUTO:
4034 SOURCING_LNUM = iptr->isn_lnum;
4035 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
4036 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004037 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004038 break;
4039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 // store number in local variable
4041 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004042 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 clear_tv(tv);
4044 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004045 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 break;
4047
Bram Moolenaar590162c2022-12-24 21:24:06 +00004048 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004049 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004050 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004051 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004052
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004053 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004054 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004055 if (res == NOTDONE)
4056 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004057 }
4058 break;
4059
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004060 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004061 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004062 if (execute_storerange(iptr, ectx) == FAIL)
4063 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004064 break;
4065
Bram Moolenaard505d172022-12-18 21:42:55 +00004066 case ISN_LOAD_CLASSMEMBER:
4067 {
4068 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4069 goto theend;
4070 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004071 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4072 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004073 ++ectx->ec_stack.ga_len;
4074 }
4075 break;
4076
4077 case ISN_STORE_CLASSMEMBER:
4078 {
4079 classmember_T *cm = &iptr->isn_arg.classmember;
4080 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4081 clear_tv(tv);
4082 *tv = *STACK_TV_BOT(-1);
4083 --ectx->ec_stack.ga_len;
4084 }
4085 break;
4086
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004087 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004088 case ISN_LOADOUTER:
4089 case ISN_STOREOUTER:
4090 {
4091 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004092 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4093 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004094
4095 while (depth > 1 && outer != NULL)
4096 {
4097 outer = outer->out_up;
4098 --depth;
4099 }
4100 if (outer == NULL)
4101 {
4102 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004103 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4104 || ectx->ec_outer_ref == NULL)
4105 // Possibly :def function called from legacy
4106 // context.
4107 emsg(_(e_closure_called_from_invalid_context));
4108 else
4109 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004110 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004111 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004112 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004113 // Variable declared in loop. May be copied if the
4114 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004115 tv = ((typval_T *)outer->out_loop[-depth - 1]
4116 .stack->ga_data)
4117 + outer->out_loop[-depth - 1].var_idx
4118 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004119 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004120 // Variable declared in a function. May be copied if
4121 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004122 tv = ((typval_T *)outer->out_stack->ga_data)
4123 + outer->out_frame_idx + STACK_FRAME_SIZE
4124 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004125 if (iptr->isn_type == ISN_LOADOUTER)
4126 {
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004127 typval_T *copy;
Bram Moolenaar35578162021-08-02 19:10:38 +02004128 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004129 goto theend;
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004130 // careful: ga_grow_inner may re-alloc the stack
4131 if (depth < 0)
4132 copy = ((typval_T *)outer->out_loop[-depth - 1]
4133 .stack->ga_data)
4134 + outer->out_loop[-depth - 1].var_idx
4135 + iptr->isn_arg.outer.outer_idx;
4136 else
4137 copy = ((typval_T *)outer->out_stack->ga_data)
4138 + outer->out_frame_idx + STACK_FRAME_SIZE
4139 + iptr->isn_arg.outer.outer_idx;
4140 // memory was freed, get tv again
4141 if (copy != tv)
4142 tv = copy;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004143 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004144 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004145 }
4146 else
4147 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004148 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004149 clear_tv(tv);
4150 *tv = *STACK_TV_BOT(0);
4151 }
4152 }
4153 break;
4154
Bram Moolenaar752fc692021-01-04 21:57:11 +01004155 // unlet item in list or dict variable
4156 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004157 if (execute_unletindex(iptr, ectx) == FAIL)
4158 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004159 break;
4160
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004161 // unlet range of items in list variable
4162 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004163 if (execute_unletrange(iptr, ectx) == FAIL)
4164 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004165 break;
4166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 // push constant
4168 case ISN_PUSHNR:
4169 case ISN_PUSHBOOL:
4170 case ISN_PUSHSPEC:
4171 case ISN_PUSHF:
4172 case ISN_PUSHS:
4173 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004174 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004175 case ISN_PUSHCHANNEL:
4176 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004177 case ISN_PUSHOBJ:
4178 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004179 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004180 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004182 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004183 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 switch (iptr->isn_type)
4185 {
4186 case ISN_PUSHNR:
4187 tv->v_type = VAR_NUMBER;
4188 tv->vval.v_number = iptr->isn_arg.number;
4189 break;
4190 case ISN_PUSHBOOL:
4191 tv->v_type = VAR_BOOL;
4192 tv->vval.v_number = iptr->isn_arg.number;
4193 break;
4194 case ISN_PUSHSPEC:
4195 tv->v_type = VAR_SPECIAL;
4196 tv->vval.v_number = iptr->isn_arg.number;
4197 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 case ISN_PUSHF:
4199 tv->v_type = VAR_FLOAT;
4200 tv->vval.v_float = iptr->isn_arg.fnumber;
4201 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 case ISN_PUSHBLOB:
4203 blob_copy(iptr->isn_arg.blob, tv);
4204 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004205 case ISN_PUSHFUNC:
4206 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004207 if (iptr->isn_arg.string == NULL)
4208 tv->vval.v_string = NULL;
4209 else
4210 tv->vval.v_string =
4211 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004212 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004213 case ISN_PUSHCHANNEL:
4214#ifdef FEAT_JOB_CHANNEL
4215 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004216 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004217#endif
4218 break;
4219 case ISN_PUSHJOB:
4220#ifdef FEAT_JOB_CHANNEL
4221 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004222 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004223#endif
4224 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004225 case ISN_PUSHOBJ:
4226 tv->v_type = VAR_OBJECT;
4227 tv->vval.v_object = NULL;
4228 break;
4229 case ISN_PUSHCLASS:
4230 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004231 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004232 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 default:
4234 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004235 tv->vval.v_string = iptr->isn_arg.string == NULL
4236 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 }
4238 break;
4239
Bram Moolenaar06b77222022-01-25 15:51:56 +00004240 case ISN_AUTOLOAD:
4241 {
4242 char_u *name = iptr->isn_arg.string;
4243
4244 (void)script_autoload(name, FALSE);
4245 if (find_func(name, TRUE))
4246 {
4247 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4248 goto theend;
4249 tv = STACK_TV_BOT(0);
4250 tv->v_lock = 0;
4251 ++ectx->ec_stack.ga_len;
4252 tv->v_type = VAR_FUNC;
4253 tv->vval.v_string = vim_strsave(name);
4254 }
4255 else
4256 {
4257 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4258
4259 if (res == NOTDONE)
4260 goto theend;
4261 if (res == FAIL)
4262 goto on_error;
4263 }
4264 }
4265 break;
4266
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004267 case ISN_UNLET:
4268 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4269 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004270 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004271 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004272 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004273 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004274 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004275
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004276 case ISN_LOCKUNLOCK:
4277 {
Ernie Raelee865f32023-09-29 19:53:55 +02004278#ifdef LOG_LOCKVAR
4279 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4280 iptr->isn_arg.string);
4281#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004282 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004283
4284 // Stack has the local variable, argument the whole :lock
4285 // or :unlock command, like ISN_EXEC.
4286 --ectx->ec_stack.ga_len;
Ernie Rael4c8da022023-10-11 21:35:11 +02004287 lval_root_T root = { .lr_tv = STACK_TV_BOT(0),
4288 .lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec,
4289 .lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg };
Ernie Rael64885642023-10-04 20:16:22 +02004290 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004291 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004292 iptr->isn_arg.lockunlock.lu_string);
4293 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004294 lval_root = lval_root_save;
4295 if (res == FAIL)
4296 goto on_error;
4297 }
4298 break;
4299
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004300 case ISN_LOCKCONST:
4301 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4302 break;
4303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 // create a list from items on the stack; uses a single allocation
4305 // for the list header and the items
4306 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004307 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004308 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 break;
4310
4311 // create a dict from items on the stack
4312 case ISN_NEWDICT:
4313 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004314 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004316 SOURCING_LNUM = iptr->isn_lnum;
4317 res = exe_newdict(iptr->isn_arg.number, ectx);
4318 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004319 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004320 if (res == MAYBE)
4321 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 }
4323 break;
4324
LemonBoy372bcce2022-04-25 12:43:20 +01004325 case ISN_CONCAT:
4326 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4327 goto theend;
4328 break;
4329
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004330 // create a partial with NULL value
4331 case ISN_NEWPARTIAL:
4332 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4333 goto theend;
4334 ++ectx->ec_stack.ga_len;
4335 tv = STACK_TV_BOT(-1);
4336 tv->v_type = VAR_PARTIAL;
4337 tv->v_lock = 0;
4338 tv->vval.v_partial = NULL;
4339 break;
4340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 // call a :def function
4342 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004343 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004344 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4345 NULL,
4346 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004347 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004348 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 break;
4350
Bram Moolenaard0200c82023-01-28 15:19:40 +00004351 // call a method on an interface
4352 case ISN_METHODCALL:
4353 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004354 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4355
Bram Moolenaard0200c82023-01-28 15:19:40 +00004356 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004357 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004358 if (tv->v_type != VAR_OBJECT)
4359 {
4360 object_required_error(tv);
4361 goto on_error;
4362 }
4363 object_T *obj = tv->vval.v_object;
4364 class_T *cl = obj->obj_class;
4365
4366 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004367 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004368 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004369
4370 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4371 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4372 goto on_error;
4373 }
4374 break;
4375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 // call a builtin function
4377 case ISN_BCALL:
4378 SOURCING_LNUM = iptr->isn_lnum;
4379 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4380 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004381 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004382 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 break;
4384
4385 // call a funcref or partial
4386 case ISN_PCALL:
4387 {
4388 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4389 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004390 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391
4392 SOURCING_LNUM = iptr->isn_lnum;
4393 if (pfunc->cpf_top)
4394 {
4395 // funcref is above the arguments
4396 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4397 }
4398 else
4399 {
4400 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004401 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004402 partial_tv = *STACK_TV_BOT(0);
4403 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004405 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004406 if (tv == &partial_tv)
4407 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004409 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 }
4411 break;
4412
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004413 case ISN_PCALL_END:
4414 // PCALL finished, arguments have been consumed and replaced by
4415 // the return value. Now clear the funcref from the stack,
4416 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004417 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004418 clear_tv(STACK_TV_BOT(-1));
4419 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4420 break;
4421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 // call a user defined function or funcref/partial
4423 case ISN_UCALL:
4424 {
4425 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4426
4427 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004428 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004429 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004430 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 }
4432 break;
4433
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004434 // :defer func(arg)
4435 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004436 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004437 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4438 goto on_error;
4439 break;
4440
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004441 // Return from a :def function call without a value.
4442 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004443 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004444 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004445 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004446 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004447 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004448 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004449 if (iptr->isn_type == ISN_RETURN_VOID)
4450 {
4451 tv->v_type = VAR_VOID;
4452 tv->vval.v_number = 0;
4453 tv->v_lock = 0;
4454 }
4455 else
4456 {
4457 *tv = *STACK_TV_VAR(0);
4458 ++tv->vval.v_object->obj_refcount;
4459 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004460 // FALLTHROUGH
4461
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004462 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 case ISN_RETURN:
4464 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004465 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004466 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004467
Ernie Raelb077b582023-12-14 20:11:44 +01004468 ///////////////////////////////////////////////////
4469 // TODO: If FAIL, line number in output not correct
4470 ///////////////////////////////////////////////////
4471 if (check_typval_is_value(STACK_TV_BOT(-1)) == FAIL)
4472 goto theend;
4473
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004474 if (trystack->ga_len > 0)
4475 trycmd = ((trycmd_T *)trystack->ga_data)
4476 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004477 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004478 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004480 // jump to ":finally" or ":endtry"
4481 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004482 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004483 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004484 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 trycmd->tcd_return = TRUE;
4486 }
4487 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004488 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 }
4490 break;
4491
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004492 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 case ISN_FUNCREF:
4494 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004495 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4496 ufunc_T *ufunc;
4497 funcref_T *funcref = &iptr->isn_arg.funcref;
4498 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004501 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004502 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004503 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004504 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004505 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004506 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004507 if (extra != NULL && extra->fre_class != NULL)
4508 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004509 class_T *cl;
4510 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004511 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004512 tv = STACK_TV_BOT(-1);
4513 if (tv->v_type != VAR_OBJECT)
4514 {
4515 object_required_error(tv);
4516 vim_free(pt);
4517 goto on_error;
4518 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004519
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004520 object_T *obj = tv->vval.v_object;
4521 cl = obj->obj_class;
4522 // drop the value from the stack
4523 clear_tv(tv);
4524 --ectx->ec_stack.ga_len;
4525
4526 pt->pt_obj = obj;
4527 ++obj->obj_refcount;
4528 }
4529 else
4530 cl = extra->fre_class;
4531
4532 if (extra->fre_object_method)
4533 {
4534 // object method
4535 // convert the interface index to the object index
4536 int idx =
4537 object_index_from_itf_index(extra->fre_class,
4538 TRUE, extra->fre_method_idx, cl);
4539 ufunc = cl->class_obj_methods[idx];
4540 }
4541 else
4542 {
4543 // class method
4544 ufunc =
4545 cl->class_class_functions[extra->fre_method_idx];
4546 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004547 }
4548 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004549 {
4550 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4551 + funcref->fr_dfunc_idx;
4552
4553 ufunc = pt_dfunc->df_ufunc;
4554 }
4555 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004556 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004557 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004558 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004559 if (ufunc == NULL)
4560 {
4561 SOURCING_LNUM = iptr->isn_lnum;
4562 iemsg("ufunc unexpectedly NULL for FUNCREF");
4563 goto theend;
4564 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004565 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004566 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004567 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004568 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004570 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 tv->vval.v_partial = pt;
4572 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004573 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 }
4575 break;
4576
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004577 // Create a global function from a lambda.
4578 case ISN_NEWFUNC:
4579 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004580 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004581
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004582 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004583 arg->nfa_global, &arg->nfa_loopvar_info,
4584 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004585 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004586 }
4587 break;
4588
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004589 // List functions
4590 case ISN_DEF:
4591 if (iptr->isn_arg.string == NULL)
4592 list_functions(NULL);
4593 else
4594 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004595 exarg_T ea;
4596 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004597
4598 CLEAR_FIELD(ea);
4599 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004600 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004601 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004602 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004603 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004604 }
4605 break;
4606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 // jump if a condition is met
4608 case ISN_JUMP:
4609 {
4610 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004611 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 int jump = TRUE;
4613
4614 if (when != JUMP_ALWAYS)
4615 {
4616 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004617 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004618 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004619 || when == JUMP_IF_COND_TRUE)
4620 {
4621 SOURCING_LNUM = iptr->isn_lnum;
4622 jump = tv_get_bool_chk(tv, &error);
4623 if (error)
4624 goto on_error;
4625 }
4626 else
4627 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004628 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004630 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 {
4632 // drop the value from the stack
4633 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004634 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 }
4636 }
4637 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004638 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 }
4640 break;
4641
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004642 // "while": jump to end if a condition is false
4643 case ISN_WHILE:
4644 {
4645 int error = FALSE;
4646 int jump = TRUE;
4647
4648 tv = STACK_TV_BOT(-1);
4649 SOURCING_LNUM = iptr->isn_lnum;
4650 jump = !tv_get_bool_chk(tv, &error);
4651 if (error)
4652 goto on_error;
4653 // drop the value from the stack
4654 clear_tv(tv);
4655 --ectx->ec_stack.ga_len;
4656 if (jump)
4657 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4658
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004659 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004660 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004661 tv = STACK_TV_VAR(
4662 iptr->isn_arg.whileloop.while_funcref_idx);
4663 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4664 }
4665 break;
4666
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004667 // Jump if an argument with a default value was already set and not
4668 // v:none.
4669 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004670 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004671 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004672 int arg_set = tv->v_type != VAR_UNKNOWN
4673 && !(tv->v_type == VAR_SPECIAL
4674 && tv->vval.v_number == VVAL_NONE);
4675 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004676 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004677 break;
4678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 // top of a for loop
4680 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004681 if (execute_for(iptr, ectx) == FAIL)
4682 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 break;
4684
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004685 // end of a for or while loop
4686 case ISN_ENDLOOP:
4687 if (execute_endloop(iptr, ectx) == FAIL)
4688 goto theend;
4689 break;
4690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 // start of ":try" block
4692 case ISN_TRY:
4693 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004694 trycmd_T *trycmd = NULL;
4695
Bram Moolenaar35578162021-08-02 19:10:38 +02004696 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004697 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004698 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4699 + ectx->ec_trystack.ga_len;
4700 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004702 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004703 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4704 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004705 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004706 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004707 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004708 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004709 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004710 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711 }
4712 break;
4713
4714 case ISN_PUSHEXC:
4715 if (current_exception == NULL)
4716 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004717 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004719 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004721 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004722 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004724 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004726 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 tv->vval.v_string = vim_strsave(
4728 (char_u *)current_exception->value);
4729 break;
4730
4731 case ISN_CATCH:
4732 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004733 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004734 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735
Bram Moolenaar4c137212021-04-19 16:48:48 +02004736 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004737 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004739 trycmd->tcd_caught = TRUE;
4740 trycmd->tcd_did_throw = FALSE;
4741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004743 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 catch_exception(current_exception);
4745 }
4746 break;
4747
Bram Moolenaarc150c092021-02-13 15:02:46 +01004748 case ISN_TRYCONT:
4749 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004750 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004751 trycont_T *trycont = &iptr->isn_arg.trycont;
4752 int i;
4753 trycmd_T *trycmd;
4754 int iidx = trycont->tct_where;
4755
4756 if (trystack->ga_len < trycont->tct_levels)
4757 {
4758 siemsg("TRYCONT: expected %d levels, found %d",
4759 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004760 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004761 }
4762 // Make :endtry jump to any outer try block and the last
4763 // :endtry inside the loop to the loop start.
4764 for (i = trycont->tct_levels; i > 0; --i)
4765 {
4766 trycmd = ((trycmd_T *)trystack->ga_data)
4767 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004768 // Add one to tcd_cont to be able to jump to
4769 // instruction with index zero.
4770 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004771 iidx = trycmd->tcd_finally_idx == 0
4772 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004773 }
4774 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004775 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004776 }
4777 break;
4778
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004779 case ISN_FINALLY:
4780 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004781 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004782 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4783 + trystack->ga_len - 1;
4784
4785 // Reset the index to avoid a return statement jumps here
4786 // again.
4787 trycmd->tcd_finally_idx = 0;
4788 break;
4789 }
4790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 // end of ":try" block
4792 case ISN_ENDTRY:
4793 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004794 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004795 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796
Bram Moolenaar06651632022-04-27 17:54:25 +01004797 --trystack->ga_len;
4798 --trylevel;
4799 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4800 if (trycmd->tcd_did_throw)
4801 did_throw = TRUE;
4802 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004804 // discard the exception
4805 if (caught_stack == current_exception)
4806 caught_stack = caught_stack->caught;
4807 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004809
4810 if (trycmd->tcd_return)
4811 goto func_return;
4812
4813 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4814 {
4815 --ectx->ec_stack.ga_len;
4816 clear_tv(STACK_TV_BOT(0));
4817 }
4818 if (trycmd->tcd_cont != 0)
4819 // handling :continue: jump to outer try block or
4820 // start of the loop
4821 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 }
4823 break;
4824
4825 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004826 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004827 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004828
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004829 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4830 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004831 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004832 // the function to abort but not display an error.
4833 tv = STACK_TV_BOT(-1);
4834 clear_tv(tv);
4835 tv->v_type = VAR_NUMBER;
4836 tv->vval.v_number = 0;
4837 goto done;
4838 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004839 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004840 tv = STACK_TV_BOT(0);
4841 if (tv->vval.v_string == NULL
4842 || *skipwhite(tv->vval.v_string) == NUL)
4843 {
4844 vim_free(tv->vval.v_string);
4845 SOURCING_LNUM = iptr->isn_lnum;
4846 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004847 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004848 }
4849
4850 // Inside a "catch" we need to first discard the caught
4851 // exception.
4852 if (trystack->ga_len > 0)
4853 {
4854 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4855 + trystack->ga_len - 1;
4856 if (trycmd->tcd_caught && current_exception != NULL)
4857 {
4858 // discard the exception
4859 if (caught_stack == current_exception)
4860 caught_stack = caught_stack->caught;
4861 discard_current_exception();
4862 trycmd->tcd_caught = FALSE;
4863 }
4864 }
4865
Bram Moolenaar90a57162022-02-12 14:23:17 +00004866 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004867 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4868 == FAIL)
4869 {
4870 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004871 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004872 }
4873 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 break;
4876
4877 // compare with special values
4878 case ISN_COMPAREBOOL:
4879 case ISN_COMPARESPECIAL:
4880 {
4881 typval_T *tv1 = STACK_TV_BOT(-2);
4882 typval_T *tv2 = STACK_TV_BOT(-1);
4883 varnumber_T arg1 = tv1->vval.v_number;
4884 varnumber_T arg2 = tv2->vval.v_number;
4885 int res;
4886
Bram Moolenaar06651632022-04-27 17:54:25 +01004887 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4888 res = arg1 == arg2;
4889 else
4890 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891
Bram Moolenaar4c137212021-04-19 16:48:48 +02004892 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 tv1->v_type = VAR_BOOL;
4894 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4895 }
4896 break;
4897
Bram Moolenaar7a222242022-03-01 19:23:24 +00004898 case ISN_COMPARENULL:
4899 {
4900 typval_T *tv1 = STACK_TV_BOT(-2);
4901 typval_T *tv2 = STACK_TV_BOT(-1);
4902 int res;
4903
4904 res = typval_compare_null(tv1, tv2);
4905 if (res == MAYBE)
4906 goto on_error;
4907 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4908 res = !res;
4909 clear_tv(tv1);
4910 clear_tv(tv2);
4911 --ectx->ec_stack.ga_len;
4912 tv1->v_type = VAR_BOOL;
4913 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4914 }
4915 break;
4916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 // Operation with two number arguments
4918 case ISN_OPNR:
4919 case ISN_COMPARENR:
4920 {
4921 typval_T *tv1 = STACK_TV_BOT(-2);
4922 typval_T *tv2 = STACK_TV_BOT(-1);
4923 varnumber_T arg1 = tv1->vval.v_number;
4924 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004925 varnumber_T res = 0;
4926 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004927
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004928 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4929 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4930 {
4931 if (arg2 < 0)
4932 {
4933 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004934 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004935 goto on_error;
4936 }
4937 }
4938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 switch (iptr->isn_arg.op.op_type)
4940 {
4941 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004942 case EXPR_DIV: if (arg2 == 0)
4943 div_zero = TRUE;
4944 else
4945 res = arg1 / arg2;
4946 break;
4947 case EXPR_REM: if (arg2 == 0)
4948 div_zero = TRUE;
4949 else
4950 res = arg1 % arg2;
4951 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952 case EXPR_SUB: res = arg1 - arg2; break;
4953 case EXPR_ADD: res = arg1 + arg2; break;
4954
4955 case EXPR_EQUAL: res = arg1 == arg2; break;
4956 case EXPR_NEQUAL: res = arg1 != arg2; break;
4957 case EXPR_GREATER: res = arg1 > arg2; break;
4958 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4959 case EXPR_SMALLER: res = arg1 < arg2; break;
4960 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004961 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4962 res = 0;
4963 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004964 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004965 break;
4966 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4967 res = 0;
4968 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004969 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004970 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004971 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 }
4973
Bram Moolenaar4c137212021-04-19 16:48:48 +02004974 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 if (iptr->isn_type == ISN_COMPARENR)
4976 {
4977 tv1->v_type = VAR_BOOL;
4978 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4979 }
4980 else
4981 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004982 if (div_zero)
4983 {
4984 SOURCING_LNUM = iptr->isn_lnum;
4985 emsg(_(e_divide_by_zero));
4986 goto on_error;
4987 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 }
4989 break;
4990
4991 // Computation with two float arguments
4992 case ISN_OPFLOAT:
4993 case ISN_COMPAREFLOAT:
4994 {
4995 typval_T *tv1 = STACK_TV_BOT(-2);
4996 typval_T *tv2 = STACK_TV_BOT(-1);
4997 float_T arg1 = tv1->vval.v_float;
4998 float_T arg2 = tv2->vval.v_float;
4999 float_T res = 0;
5000 int cmp = FALSE;
5001
5002 switch (iptr->isn_arg.op.op_type)
5003 {
5004 case EXPR_MULT: res = arg1 * arg2; break;
5005 case EXPR_DIV: res = arg1 / arg2; break;
5006 case EXPR_SUB: res = arg1 - arg2; break;
5007 case EXPR_ADD: res = arg1 + arg2; break;
5008
5009 case EXPR_EQUAL: cmp = arg1 == arg2; break;
5010 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
5011 case EXPR_GREATER: cmp = arg1 > arg2; break;
5012 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
5013 case EXPR_SMALLER: cmp = arg1 < arg2; break;
5014 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
5015 default: cmp = 0; break;
5016 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005017 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 if (iptr->isn_type == ISN_COMPAREFLOAT)
5019 {
5020 tv1->v_type = VAR_BOOL;
5021 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
5022 }
5023 else
5024 tv1->vval.v_float = res;
5025 }
5026 break;
5027
5028 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00005029 case ISN_COMPAREDICT:
5030 case ISN_COMPAREFUNC:
5031 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005033 case ISN_COMPARECLASS:
5034 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 {
5036 typval_T *tv1 = STACK_TV_BOT(-2);
5037 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00005038 exprtype_T exprtype = iptr->isn_arg.op.op_type;
5039 int ic = iptr->isn_arg.op.op_ic;
5040 int res = FALSE;
5041 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042
Bram Moolenaar265f8112021-12-19 12:33:05 +00005043 SOURCING_LNUM = iptr->isn_lnum;
5044 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005046 status = typval_compare_list(tv1, tv2,
5047 exprtype, ic, &res);
5048 }
5049 else if (iptr->isn_type == ISN_COMPAREDICT)
5050 {
5051 status = typval_compare_dict(tv1, tv2,
5052 exprtype, ic, &res);
5053 }
5054 else if (iptr->isn_type == ISN_COMPAREFUNC)
5055 {
5056 status = typval_compare_func(tv1, tv2,
5057 exprtype, ic, &res);
5058 }
5059 else if (iptr->isn_type == ISN_COMPARESTRING)
5060 {
5061 status = typval_compare_string(tv1, tv2,
5062 exprtype, ic, &res);
5063 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005064 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005065 {
5066 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005068 else if (iptr->isn_type == ISN_COMPARECLASS)
5069 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005070 status = typval_compare_class(tv1, tv2,
5071 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005072 }
5073 else // ISN_COMPAREOBJECT
5074 {
5075 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005076 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005077 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005078 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 clear_tv(tv1);
5080 clear_tv(tv2);
5081 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005082 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5083 if (status == FAIL)
5084 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 }
5086 break;
5087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088 case ISN_COMPAREANY:
5089 {
5090 typval_T *tv1 = STACK_TV_BOT(-2);
5091 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005092 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005094 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095
Bram Moolenaareb26f432020-09-14 16:50:05 +02005096 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005097 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005099 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005100 if (status == FAIL)
5101 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 }
5103 break;
5104
5105 case ISN_ADDLIST:
5106 case ISN_ADDBLOB:
5107 {
5108 typval_T *tv1 = STACK_TV_BOT(-2);
5109 typval_T *tv2 = STACK_TV_BOT(-1);
5110
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005111 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005113 {
5114 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5115 && tv1->vval.v_list != NULL)
5116 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5117 NULL);
5118 else
5119 eval_addlist(tv1, tv2);
5120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 else
5122 eval_addblob(tv1, tv2);
5123 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005124 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 }
5126 break;
5127
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005128 case ISN_LISTAPPEND:
5129 {
5130 typval_T *tv1 = STACK_TV_BOT(-2);
5131 typval_T *tv2 = STACK_TV_BOT(-1);
5132 list_T *l = tv1->vval.v_list;
5133
5134 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005135 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005136 if (l == NULL)
5137 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005138 emsg(_(e_cannot_add_to_null_list));
5139 goto on_error;
5140 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005141 if (value_check_lock(l->lv_lock, NULL, FALSE))
5142 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005143 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005144 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005145 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005146 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005147 }
5148 break;
5149
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005150 case ISN_BLOBAPPEND:
5151 {
5152 typval_T *tv1 = STACK_TV_BOT(-2);
5153 typval_T *tv2 = STACK_TV_BOT(-1);
5154 blob_T *b = tv1->vval.v_blob;
5155 int error = FALSE;
5156 varnumber_T n;
5157
5158 // add a number to a blob
5159 if (b == NULL)
5160 {
5161 SOURCING_LNUM = iptr->isn_lnum;
5162 emsg(_(e_cannot_add_to_null_blob));
5163 goto on_error;
5164 }
5165 n = tv_get_number_chk(tv2, &error);
5166 if (error)
5167 goto on_error;
5168 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005169 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005170 }
5171 break;
5172
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 // Computation with two arguments of unknown type
5174 case ISN_OPANY:
5175 {
5176 typval_T *tv1 = STACK_TV_BOT(-2);
5177 typval_T *tv2 = STACK_TV_BOT(-1);
5178 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 int error = FALSE;
5181
5182 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5183 {
5184 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5185 {
5186 eval_addlist(tv1, tv2);
5187 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005188 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 break;
5190 }
5191 else if (tv1->v_type == VAR_BLOB
5192 && tv2->v_type == VAR_BLOB)
5193 {
5194 eval_addblob(tv1, tv2);
5195 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005196 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 break;
5198 }
5199 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 if (tv1->v_type == VAR_FLOAT)
5201 {
5202 f1 = tv1->vval.v_float;
5203 n1 = 0;
5204 }
5205 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005207 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 n1 = tv_get_number_chk(tv1, &error);
5209 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005210 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 if (tv2->v_type == VAR_FLOAT)
5212 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 if (tv2->v_type == VAR_FLOAT)
5215 {
5216 f2 = tv2->vval.v_float;
5217 n2 = 0;
5218 }
5219 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 {
5221 n2 = tv_get_number_chk(tv2, &error);
5222 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005223 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005224 if (tv1->v_type == VAR_FLOAT)
5225 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227 // if there is a float on either side the result is a float
5228 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5229 {
5230 switch (iptr->isn_arg.op.op_type)
5231 {
5232 case EXPR_MULT: f1 = f1 * f2; break;
5233 case EXPR_DIV: f1 = f1 / f2; break;
5234 case EXPR_SUB: f1 = f1 - f2; break;
5235 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005236 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005237 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005238 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239 }
5240 clear_tv(tv1);
5241 clear_tv(tv2);
5242 tv1->v_type = VAR_FLOAT;
5243 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005244 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 }
5246 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005248 int failed = FALSE;
5249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005250 switch (iptr->isn_arg.op.op_type)
5251 {
5252 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005253 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5254 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005255 goto on_error;
5256 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005257 case EXPR_SUB: n1 = n1 - n2; break;
5258 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005259 default: n1 = num_modulus(n1, n2, &failed);
5260 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005261 goto on_error;
5262 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263 }
5264 clear_tv(tv1);
5265 clear_tv(tv2);
5266 tv1->v_type = VAR_NUMBER;
5267 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005268 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269 }
5270 }
5271 break;
5272
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005273 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005274 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005275 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005276 int is_slice = iptr->isn_type == ISN_STRSLICE;
5277 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005278 char_u *res;
5279
5280 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005281 // string slice: string is at stack-3, first index at
5282 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005283 if (is_slice)
5284 {
5285 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005286 n1 = tv->vval.v_number;
5287 }
5288
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005289 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005290 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005291
Bram Moolenaar4c137212021-04-19 16:48:48 +02005292 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005293 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005294 if (is_slice)
5295 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005296 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005297 else
5298 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005299 // single character (including composing characters).
5300 // If the index is too big or negative the result is
5301 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005302 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005303 vim_free(tv->vval.v_string);
5304 tv->vval.v_string = res;
5305 }
5306 break;
5307
5308 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005309 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005310 case ISN_BLOBINDEX:
5311 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005313 int is_slice = iptr->isn_type == ISN_LISTSLICE
5314 || iptr->isn_type == ISN_BLOBSLICE;
5315 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5316 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005317 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005318 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005319
5320 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005321 // list slice: list is at stack-3, indexes at stack-2 and
5322 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005323 // Same for blob.
5324 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005325
5326 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005327 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005329
5330 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 {
Bram Moolenaared591872020-08-15 22:14:53 +02005332 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005333 n1 = tv->vval.v_number;
5334 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335 }
Bram Moolenaared591872020-08-15 22:14:53 +02005336
Bram Moolenaar4c137212021-04-19 16:48:48 +02005337 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005338 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005339 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005340 if (is_blob)
5341 {
5342 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5343 n1, n2, FALSE, tv) == FAIL)
5344 goto on_error;
5345 }
5346 else
5347 {
5348 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5349 n1, n2, FALSE, tv, TRUE) == FAIL)
5350 goto on_error;
5351 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005352 }
5353 break;
5354
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005355 case ISN_ANYINDEX:
5356 case ISN_ANYSLICE:
5357 {
5358 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5359 typval_T *var1, *var2;
5360 int res;
5361
5362 // index: composite is at stack-2, index at stack-1
5363 // slice: composite is at stack-3, indexes at stack-2 and
5364 // stack-1
5365 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005366 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005367 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5368 goto on_error;
5369 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5370 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005371 res = eval_index_inner(tv, is_slice, var1, var2,
5372 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005373 clear_tv(var1);
5374 if (is_slice)
5375 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005376 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005377 if (res == FAIL)
5378 goto on_error;
5379 }
5380 break;
5381
Bram Moolenaar9af78762020-06-16 11:34:42 +02005382 case ISN_SLICE:
5383 {
5384 list_T *list;
5385 int count = iptr->isn_arg.number;
5386
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005387 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005388 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005389 list = tv->vval.v_list;
5390
5391 // no error for short list, expect it to be checked earlier
5392 if (list != NULL && list->lv_len >= count)
5393 {
5394 list_T *newlist = list_slice(list,
5395 count, list->lv_len - 1);
5396
5397 if (newlist != NULL)
5398 {
5399 list_unref(list);
5400 tv->vval.v_list = newlist;
5401 ++newlist->lv_refcount;
5402 }
5403 }
5404 }
5405 break;
5406
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005407 case ISN_GETITEM:
5408 {
5409 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005410 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005411
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005412 // Get list item: list is at stack-1, push item.
5413 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005414 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5415 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005416
Bram Moolenaar35578162021-08-02 19:10:38 +02005417 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005418 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005419 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005420 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005421
5422 // Useful when used in unpack assignment. Reset at
5423 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005424 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005425 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005426 }
5427 break;
5428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429 case ISN_MEMBER:
5430 {
5431 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005432 char_u *key;
5433 dictitem_T *di;
5434
5435 // dict member: dict is at stack-2, key at stack-1
5436 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005437 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005438 dict = tv->vval.v_dict;
5439
5440 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005441 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005442 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005443 if (key == NULL)
5444 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005445
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005446 if ((di = dict_find(dict, key, -1)) == NULL)
5447 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005448 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005449 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005450
5451 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005452 // stack contents makes sense and the dict stack is
5453 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005454 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005455 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005456 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005457 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005458 tv->v_type = VAR_NUMBER;
5459 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005460 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005461 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005462 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005463 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005464 // Put the dict used on the dict stack, it might be used by
5465 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005466 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005467 if (dict_stack_save(tv) == FAIL)
5468 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005469 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005470 }
5471 break;
5472
5473 // dict member with string key
5474 case ISN_STRINGMEMBER:
5475 {
5476 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005477 dictitem_T *di;
5478
5479 tv = STACK_TV_BOT(-1);
5480 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5481 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005482 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005483 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005484 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005485 }
5486 dict = tv->vval.v_dict;
5487
5488 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5489 == NULL)
5490 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005491 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005492 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005493 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005494 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005495 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005496 // Put the dict used on the dict stack, it might be used by
5497 // a dict function later.
5498 if (dict_stack_save(tv) == FAIL)
5499 goto on_fatal_error;
5500
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005501 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005502 }
5503 break;
5504
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005505 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005506 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005507 {
5508 tv = STACK_TV_BOT(-1);
5509 if (tv->v_type != VAR_OBJECT)
5510 {
5511 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005512 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005513 goto on_error;
5514 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005515
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005516 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005517 if (obj == NULL)
5518 {
5519 SOURCING_LNUM = iptr->isn_lnum;
5520 emsg(_(e_using_null_object));
5521 goto on_error;
5522 }
5523
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005524 int idx;
5525 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005526 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005527 else
5528 {
5529 idx = iptr->isn_arg.classmember.cm_idx;
5530 // convert the interface index to the object index
5531 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005532 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005533 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005534 }
5535
Ernie Rael18143d32023-09-04 22:30:41 +02005536 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005537 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005538 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005539
5540 // Unreference the object after getting the member, it may
5541 // be freed.
5542 object_unref(obj);
5543 }
5544 break;
5545
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005546 case ISN_STORE_THIS:
5547 {
5548 int idx = iptr->isn_arg.number;
5549 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5550 // the members are located right after the object struct
5551 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5552 clear_tv(mtv);
5553 *mtv = *STACK_TV_BOT(-1);
5554 --ectx->ec_stack.ga_len;
5555 }
5556 break;
5557
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005558 case ISN_CLEARDICT:
5559 dict_stack_drop();
5560 break;
5561
5562 case ISN_USEDICT:
5563 {
5564 typval_T *dict_tv = dict_stack_get_tv();
5565
5566 // Turn "dict.Func" into a partial for "Func" bound to
5567 // "dict". Don't do this when "Func" is already a partial
5568 // that was bound explicitly (pt_auto is FALSE).
5569 tv = STACK_TV_BOT(-1);
5570 if (dict_tv != NULL
5571 && dict_tv->v_type == VAR_DICT
5572 && dict_tv->vval.v_dict != NULL
5573 && (tv->v_type == VAR_FUNC
5574 || (tv->v_type == VAR_PARTIAL
5575 && (tv->vval.v_partial->pt_auto
5576 || tv->vval.v_partial->pt_dict == NULL))))
5577 dict_tv->vval.v_dict =
5578 make_partial(dict_tv->vval.v_dict, tv);
5579 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005580 }
5581 break;
5582
5583 case ISN_NEGATENR:
5584 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005585 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005586 if (tv->v_type == VAR_FLOAT)
5587 tv->vval.v_float = -tv->vval.v_float;
5588 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005589 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590 break;
5591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592 case ISN_CHECKTYPE:
5593 {
5594 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005595 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005596 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005597
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005598 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005599 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005600 if (ct->ct_arg_idx > 0)
5601 {
5602 where.wt_index = ct->ct_arg_idx;
5603 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005604 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005605 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005606 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005607 if (r == FAIL)
5608 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005609
5610 // number 0 is FALSE, number 1 is TRUE
5611 if (tv->v_type == VAR_NUMBER
5612 && ct->ct_type->tt_type == VAR_BOOL
5613 && (tv->vval.v_number == 0
5614 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005615 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005616 tv->v_type = VAR_BOOL;
5617 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005618 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005619 }
5620 }
5621 break;
5622
Bram Moolenaar9af78762020-06-16 11:34:42 +02005623 case ISN_CHECKLEN:
5624 {
5625 int min_len = iptr->isn_arg.checklen.cl_min_len;
5626 list_T *list = NULL;
5627
5628 tv = STACK_TV_BOT(-1);
5629 if (tv->v_type == VAR_LIST)
5630 list = tv->vval.v_list;
5631 if (list == NULL || list->lv_len < min_len
5632 || (list->lv_len > min_len
5633 && !iptr->isn_arg.checklen.cl_more_OK))
5634 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005635 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005636 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005637 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005638 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005639 }
5640 }
5641 break;
5642
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005643 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005644 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005645 break;
5646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005648 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005649 {
5650 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005651 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005653 if (iptr->isn_type == ISN_2BOOL)
5654 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005655 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005656 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005657 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005658 n = !n;
5659 }
5660 else
5661 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005662 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005663 SOURCING_LNUM = iptr->isn_lnum;
5664 n = tv_get_bool_chk(tv, &error);
5665 if (error)
5666 goto on_error;
5667 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005668 clear_tv(tv);
5669 tv->v_type = VAR_BOOL;
5670 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5671 }
5672 break;
5673
5674 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005675 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005676 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005677 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5678 iptr->isn_type == ISN_2STRING_ANY,
5679 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005680 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681 break;
5682
Bram Moolenaar08597872020-12-10 19:43:40 +01005683 case ISN_RANGE:
5684 {
5685 exarg_T ea;
5686 char *errormsg;
5687
Bram Moolenaarece0b872021-01-08 20:40:45 +01005688 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005689 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005690 ea.addr_type = ADDR_LINES;
5691 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005692 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005693 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005694 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005695
Bram Moolenaar35578162021-08-02 19:10:38 +02005696 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005697 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005698 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005699 tv = STACK_TV_BOT(-1);
5700 tv->v_type = VAR_NUMBER;
5701 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005702 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005703 }
5704 break;
5705
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005706 case ISN_PUT:
5707 {
5708 int regname = iptr->isn_arg.put.put_regname;
5709 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5710 char_u *expr = NULL;
5711 int dir = FORWARD;
5712
Bram Moolenaar08597872020-12-10 19:43:40 +01005713 if (lnum < -2)
5714 {
5715 // line number was put on the stack by ISN_RANGE
5716 tv = STACK_TV_BOT(-1);
5717 curwin->w_cursor.lnum = tv->vval.v_number;
5718 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5719 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005720 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005721 }
5722 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005723 // :put! above cursor
5724 dir = BACKWARD;
5725 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005726 {
5727 curwin->w_cursor.lnum = lnum;
5728 if (lnum == 0)
5729 // check_cursor() below will move to line 1
5730 dir = BACKWARD;
5731 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005732
5733 if (regname == '=')
5734 {
5735 tv = STACK_TV_BOT(-1);
5736 if (tv->v_type == VAR_STRING)
5737 expr = tv->vval.v_string;
5738 else
5739 {
5740 expr = typval2string(tv, TRUE); // allocates value
5741 clear_tv(tv);
5742 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005743 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005744 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005745 check_cursor();
5746 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5747 vim_free(expr);
5748 }
5749 break;
5750
Bram Moolenaar02194d22020-10-24 23:08:38 +02005751 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005752 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5753 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5754 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5755 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005756 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5757 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005758 break;
5759
Bram Moolenaar02194d22020-10-24 23:08:38 +02005760 case ISN_CMDMOD_REV:
5761 // filter regprog is owned by the instruction, don't free it
5762 cmdmod.cmod_filter_regmatch.regprog = NULL;
5763 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005764 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5765 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005766 break;
5767
Bram Moolenaar792f7862020-11-23 08:31:18 +01005768 case ISN_UNPACK:
5769 {
5770 int count = iptr->isn_arg.unpack.unp_count;
5771 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5772 list_T *l;
5773 listitem_T *li;
5774 int i;
5775
5776 // Check there is a valid list to unpack.
5777 tv = STACK_TV_BOT(-1);
5778 if (tv->v_type != VAR_LIST)
5779 {
5780 SOURCING_LNUM = iptr->isn_lnum;
5781 emsg(_(e_for_argument_must_be_sequence_of_lists));
5782 goto on_error;
5783 }
5784 l = tv->vval.v_list;
5785 if (l == NULL
5786 || l->lv_len < (semicolon ? count - 1 : count))
5787 {
5788 SOURCING_LNUM = iptr->isn_lnum;
5789 emsg(_(e_list_value_does_not_have_enough_items));
5790 goto on_error;
5791 }
5792 else if (!semicolon && l->lv_len > count)
5793 {
5794 SOURCING_LNUM = iptr->isn_lnum;
5795 emsg(_(e_list_value_has_more_items_than_targets));
5796 goto on_error;
5797 }
5798
5799 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005800 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005801 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005802 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005803
5804 // Variable after semicolon gets a list with the remaining
5805 // items.
5806 if (semicolon)
5807 {
5808 list_T *rem_list =
5809 list_alloc_with_items(l->lv_len - count + 1);
5810
5811 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005812 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005813 tv = STACK_TV_BOT(-count);
5814 tv->vval.v_list = rem_list;
5815 ++rem_list->lv_refcount;
5816 tv->v_lock = 0;
5817 li = l->lv_first;
5818 for (i = 0; i < count - 1; ++i)
5819 li = li->li_next;
5820 for (i = 0; li != NULL; ++i)
5821 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005822 typval_T tvcopy;
5823
5824 copy_tv(&li->li_tv, &tvcopy);
5825 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005826 li = li->li_next;
5827 }
5828 --count;
5829 }
5830
5831 // Produce the values in reverse order, first item last.
5832 li = l->lv_first;
5833 for (i = 0; i < count; ++i)
5834 {
5835 tv = STACK_TV_BOT(-i - 1);
5836 copy_tv(&li->li_tv, tv);
5837 li = li->li_next;
5838 }
5839
5840 list_unref(l);
5841 }
5842 break;
5843
Bram Moolenaarb2049902021-01-24 12:53:53 +01005844 case ISN_PROF_START:
5845 case ISN_PROF_END:
5846 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005847#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005848 funccall_T cookie;
5849 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005850 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005851 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005852
Bram Moolenaarca16c602022-09-06 18:57:08 +01005853 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005854 if (iptr->isn_type == ISN_PROF_START)
5855 {
5856 func_line_start(&cookie, iptr->isn_lnum);
5857 // if we get here the instruction is executed
5858 func_line_exec(&cookie);
5859 }
5860 else
5861 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005862#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005863 }
5864 break;
5865
Bram Moolenaare99d4222021-06-13 14:01:26 +02005866 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005867 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005868 break;
5869
Bram Moolenaar389df252020-07-09 21:20:47 +02005870 case ISN_SHUFFLE:
5871 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005872 typval_T tmp_tv;
5873 int item = iptr->isn_arg.shuffle.shfl_item;
5874 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005875
5876 tmp_tv = *STACK_TV_BOT(-item);
5877 for ( ; up > 0 && item > 1; --up)
5878 {
5879 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5880 --item;
5881 }
5882 *STACK_TV_BOT(-item) = tmp_tv;
5883 }
5884 break;
5885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005887 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005888 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005889 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890 break;
5891 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005892 continue;
5893
Bram Moolenaard032f342020-07-18 18:13:02 +02005894func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005895 // Restore previous function. If the frame pointer is where we started
5896 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005897 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005898 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005899
Bram Moolenaar4c137212021-04-19 16:48:48 +02005900 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005901 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005902 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005903 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005904
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005905on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005906 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005907 // If "emsg_silent" is set then ignore the error, unless it was set
5908 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005909 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005910 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005911 {
5912 // If a sequence of instructions causes an error while ":silent!"
5913 // was used, restore the stack length and jump ahead to restoring
5914 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005915 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005916 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005917 while (ectx->ec_stack.ga_len
5918 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005919 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005920 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005921 clear_tv(STACK_TV_BOT(0));
5922 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005923 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5924 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005925 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005926 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005927 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005928on_fatal_error:
5929 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005930 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005931 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005932 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933 }
5934
5935done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005936 ret = OK;
5937theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005938 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005939
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005940 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005941 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5942 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005943}
5944
5945/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005946 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005947 * "rettv".
5948 * Return OK or FAIL.
5949 */
5950 int
5951exe_typval_instr(typval_T *tv, typval_T *rettv)
5952{
5953 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5954 isn_T *save_instr = ectx->ec_instr;
5955 int save_iidx = ectx->ec_iidx;
5956 int res;
5957
LemonBoyf3b48952022-05-05 13:53:03 +01005958 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5959 // even when the compilation fails.
5960 rettv->v_type = VAR_UNKNOWN;
5961
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005962 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5963 res = exec_instructions(ectx);
5964 if (res == OK)
5965 {
5966 *rettv = *STACK_TV_BOT(-1);
5967 --ectx->ec_stack.ga_len;
5968 }
5969
5970 ectx->ec_instr = save_instr;
5971 ectx->ec_iidx = save_iidx;
5972
5973 return res;
5974}
5975
5976/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005977 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5978 * "substitute_instr".
5979 */
5980 char_u *
5981exe_substitute_instr(void)
5982{
5983 ectx_T *ectx = substitute_instr->subs_ectx;
5984 isn_T *save_instr = ectx->ec_instr;
5985 int save_iidx = ectx->ec_iidx;
5986 char_u *res;
5987
5988 ectx->ec_instr = substitute_instr->subs_instr;
5989 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005990 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005991 typval_T *tv = STACK_TV_BOT(-1);
5992
Bram Moolenaar27523602021-06-05 21:36:19 +02005993 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005994 --ectx->ec_stack.ga_len;
5995 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005996 }
5997 else
5998 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005999 substitute_instr->subs_status = FAIL;
6000 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02006001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002
Bram Moolenaar4c137212021-04-19 16:48:48 +02006003 ectx->ec_instr = save_instr;
6004 ectx->ec_iidx = save_iidx;
6005
6006 return res;
6007}
6008
6009/*
6010 * Call a "def" function from old Vim script.
6011 * Return OK or FAIL.
6012 */
6013 int
6014call_def_function(
6015 ufunc_T *ufunc,
6016 int argc_arg, // nr of arguments
6017 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006018 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02006019 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006020 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01006021 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006022 typval_T *rettv) // return value
6023{
6024 ectx_T ectx; // execution context
6025 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006026 int partial_argc = partial == NULL
6027 || (flags & DEF_USE_PT_ARGV) == 0
6028 ? 0 : partial->pt_argc;
6029 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006030 typval_T *tv;
6031 int idx;
6032 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006033 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006034 sctx_T save_current_sctx = current_sctx;
6035 int did_emsg_before = did_emsg_cumul + did_emsg;
6036 int save_suppress_errthrow = suppress_errthrow;
6037 msglist_T **saved_msg_list = NULL;
6038 msglist_T *private_msg_list = NULL;
6039 int save_emsg_silent_def = emsg_silent_def;
6040 int save_did_emsg_def = did_emsg_def;
6041 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006042 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006043
6044// Get pointer to item in the stack.
6045#undef STACK_TV
6046#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6047
6048// Get pointer to item at the bottom of the stack, -1 is the bottom.
6049#undef STACK_TV_BOT
6050#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6051
6052// Get pointer to a local variable on the stack. Negative for arguments.
6053#undef STACK_TV_VAR
6054#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6055
6056 if (ufunc->uf_def_status == UF_NOT_COMPILED
6057 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006058 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6059 && compile_def_function(ufunc, FALSE,
6060 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006061 {
6062 if (did_emsg_cumul + did_emsg == did_emsg_before)
6063 semsg(_(e_function_is_not_compiled_str),
6064 printable_func_name(ufunc));
6065 return FAIL;
6066 }
6067
6068 {
6069 // Check the function was really compiled.
6070 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6071 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006072 if (dfunc->df_ufunc == NULL)
6073 {
6074 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6075 return FAIL;
6076 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006077 if (INSTRUCTIONS(dfunc) == NULL)
6078 {
6079 iemsg("using call_def_function() on not compiled function");
6080 return FAIL;
6081 }
6082 }
6083
6084 // If depth of calling is getting too high, don't execute the function.
6085 orig_funcdepth = funcdepth_get();
6086 if (funcdepth_increment() == FAIL)
6087 return FAIL;
6088
6089 CLEAR_FIELD(ectx);
6090 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6091 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006092 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006093 {
6094 funcdepth_decrement();
6095 return FAIL;
6096 }
6097 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6098 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6099 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006100 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006101
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006102 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006103 if (idx > 0 && ufunc->uf_va_name == NULL)
6104 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006105 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006106 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006107 goto failed_early;
6108 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006109 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006110 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006111 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006112 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006113 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006114 goto failed_early;
6115 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006116
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006117 // Put values from the partial and arguments on the stack, but no more than
6118 // what the function expects. A lambda can be called with more arguments
6119 // than it uses.
6120 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006121 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6122 ++idx)
6123 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006124 int argv_idx = idx - partial_argc;
6125
6126 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006127 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006128 && tv->v_type == VAR_SPECIAL
6129 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006130 {
6131 // Use the default value.
6132 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6133 }
6134 else
6135 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006136 int done = FALSE;
6137 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6138 {
6139 type_T *expected = ufunc->uf_arg_types[idx];
6140 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6141 {
6142 // When a float is expected and a number was given, convert
6143 // the value.
6144 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6145 STACK_TV_BOT(0)->v_lock = 0;
6146 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6147 done = TRUE;
6148 }
6149 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006150 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006151 goto failed_early;
6152 }
6153 if (!done)
6154 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006155 }
6156 ++ectx.ec_stack.ga_len;
6157 }
6158
6159 // Turn varargs into a list. Empty list if no args.
6160 if (ufunc->uf_va_name != NULL)
6161 {
6162 int vararg_count = argc - ufunc->uf_args.ga_len;
6163
6164 if (vararg_count < 0)
6165 vararg_count = 0;
6166 else
6167 argc -= vararg_count;
6168 if (exe_newlist(vararg_count, &ectx) == FAIL)
6169 goto failed_early;
6170
6171 // Check the type of the list items.
6172 tv = STACK_TV_BOT(-1);
6173 if (ufunc->uf_va_type != NULL
6174 && ufunc->uf_va_type != &t_list_any
6175 && ufunc->uf_va_type->tt_member != &t_any
6176 && tv->vval.v_list != NULL)
6177 {
6178 type_T *expected = ufunc->uf_va_type->tt_member;
6179 listitem_T *li = tv->vval.v_list->lv_first;
6180
6181 for (idx = 0; idx < vararg_count; ++idx)
6182 {
6183 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006184 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006185 goto failed_early;
6186 li = li->li_next;
6187 }
6188 }
6189
6190 if (defcount > 0)
6191 // Move varargs list to below missing default arguments.
6192 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6193 --ectx.ec_stack.ga_len;
6194 }
6195
6196 // Make space for omitted arguments, will store default value below.
6197 // Any varargs list goes after them.
6198 if (defcount > 0)
6199 for (idx = 0; idx < defcount; ++idx)
6200 {
6201 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6202 ++ectx.ec_stack.ga_len;
6203 }
6204 if (ufunc->uf_va_name != NULL)
6205 ++ectx.ec_stack.ga_len;
6206
6207 // Frame pointer points to just after arguments.
6208 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6209 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6210
6211 {
6212 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6213 + ufunc->uf_dfunc_idx;
6214 ufunc_T *base_ufunc = dfunc->df_ufunc;
6215
6216 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006217 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006218 if (partial != NULL || base_ufunc->uf_partial != NULL)
6219 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006220 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6221 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006222 goto failed_early;
6223 if (partial != NULL)
6224 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006225 outer_T *outer = get_pt_outer(partial);
6226
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006227 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006228 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006229 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006230 if (current_ectx != NULL)
6231 {
6232 if (current_ectx->ec_outer_ref != NULL
6233 && current_ectx->ec_outer_ref->or_outer != NULL)
6234 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006235 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006236 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006237 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006238 }
6239 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006240 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006241 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006242 ++partial->pt_refcount;
6243 ectx.ec_outer_ref->or_partial = partial;
6244 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006245 }
6246 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006247 {
6248 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6249 ++base_ufunc->uf_partial->pt_refcount;
6250 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6251 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006252 }
6253 }
6254
6255 // dummy frame entries
6256 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6257 {
6258 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6259 ++ectx.ec_stack.ga_len;
6260 }
6261
6262 {
6263 // Reserve space for local variables and any closure reference count.
6264 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6265 + ufunc->uf_dfunc_idx;
6266
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006267 // Initialize variables to zero. That avoids having to generate
6268 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006269 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006270 {
6271 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6272 STACK_TV_VAR(idx)->vval.v_number = 0;
6273 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006274 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006275
6276 if (object != NULL)
6277 {
6278 // the object is always the variable at index zero
6279 tv = STACK_TV_VAR(0);
6280 tv->v_type = VAR_OBJECT;
6281 tv->vval.v_object = object;
6282 }
6283
Bram Moolenaar4c137212021-04-19 16:48:48 +02006284 if (dfunc->df_has_closure)
6285 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006286 // Initialize the variable that counts how many closures were
6287 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006288 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6289 STACK_TV_VAR(idx)->vval.v_number = 0;
6290 ++ectx.ec_stack.ga_len;
6291 }
6292
6293 ectx.ec_instr = INSTRUCTIONS(dfunc);
6294 }
6295
Bram Moolenaar58779852022-09-06 18:31:14 +01006296 // Store the execution context in funccal, used by invoke_all_defer().
6297 if (funccal != NULL)
6298 funccal->fc_ectx = &ectx;
6299
Bram Moolenaar4c137212021-04-19 16:48:48 +02006300 // Following errors are in the function, not the caller.
6301 // Commands behave like vim9script.
6302 estack_push_ufunc(ufunc, 1);
6303 current_sctx = ufunc->uf_script_ctx;
6304 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6305
6306 // Use a specific location for storing error messages to be converted to an
6307 // exception.
6308 saved_msg_list = msg_list;
6309 msg_list = &private_msg_list;
6310
6311 // Do turn errors into exceptions.
6312 suppress_errthrow = FALSE;
6313
6314 // Do not delete the function while executing it.
6315 ++ufunc->uf_calls;
6316
6317 // When ":silent!" was used before calling then we still abort the
6318 // function. If ":silent!" is used in the function then we don't.
6319 emsg_silent_def = emsg_silent;
6320 did_emsg_def = 0;
6321
LemonBoyc5d27442023-08-19 13:02:35 +02006322 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006323
Bram Moolenaar5800c792022-09-22 17:34:01 +01006324 /*
6325 * Execute the instructions until done.
6326 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006327 ret = exec_instructions(&ectx);
6328 if (ret == OK)
6329 {
6330 // function finished, get result from the stack.
6331 if (ufunc->uf_ret_type == &t_void)
6332 {
6333 rettv->v_type = VAR_VOID;
6334 }
6335 else
6336 {
6337 tv = STACK_TV_BOT(-1);
6338 *rettv = *tv;
6339 tv->v_type = VAR_UNKNOWN;
6340 }
6341 }
6342
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006343 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006344 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006345
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006346 // Deal with any remaining closures, they may be in use somewhere.
6347 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006348 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006349 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006350 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006351 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006352
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006353 estack_pop();
6354 current_sctx = save_current_sctx;
6355
Bram Moolenaar2336c372021-12-06 15:06:54 +00006356 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6357 // Function was unreferenced while being used, free it now.
6358 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006359
Bram Moolenaar352134b2020-10-17 22:04:08 +02006360 if (*msg_list != NULL && saved_msg_list != NULL)
6361 {
6362 msglist_T **plist = saved_msg_list;
6363
6364 // Append entries from the current msg_list (uncaught exceptions) to
6365 // the saved msg_list.
6366 while (*plist != NULL)
6367 plist = &(*plist)->next;
6368
6369 *plist = *msg_list;
6370 }
6371 msg_list = saved_msg_list;
6372
Bram Moolenaar4c137212021-04-19 16:48:48 +02006373 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006374 {
6375 cmdmod.cmod_filter_regmatch.regprog = NULL;
6376 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006377 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006378 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006379 emsg_silent_def = save_emsg_silent_def;
6380 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006381
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006382failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006383 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006384 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006385 {
6386 tv = STACK_TV(idx);
6387 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6388 clear_tv(tv);
6389 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006390 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006391
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006393 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006394 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006395 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006396 if (ectx.ec_outer_ref->or_outer_allocated)
6397 vim_free(ectx.ec_outer_ref->or_outer);
6398 partial_unref(ectx.ec_outer_ref->or_partial);
6399 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006400 }
6401
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006402 // Not sure if this is necessary.
6403 suppress_errthrow = save_suppress_errthrow;
6404
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006405 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6406 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006407 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006408 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006409 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006410 return ret;
6411}
6412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006413/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006414 * Called when a def function has finished (possibly failed).
6415 * Invoke all the function returns to clean up and invoke deferred functions,
6416 * except the toplevel one.
6417 */
6418 void
6419unwind_def_callstack(ectx_T *ectx)
6420{
6421 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6422 func_return(ectx);
6423}
6424
6425/*
dundargocc57b5bc2022-11-02 13:30:51 +00006426 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006427 */
6428 void
6429may_invoke_defer_funcs(ectx_T *ectx)
6430{
6431 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6432
6433 if (dfunc->df_defer_var_idx > 0)
6434 invoke_defer_funcs(ectx);
6435}
6436
6437/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006438 * Return loopvarinfo in a printable form in allocated memory.
6439 */
6440 static char_u *
6441printable_loopvarinfo(loopvarinfo_T *lvi)
6442{
6443 garray_T ga;
6444 int depth;
6445
6446 ga_init2(&ga, 1, 100);
6447 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6448 {
6449 if (ga_grow(&ga, 50) == FAIL)
6450 break;
6451 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006452 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006453 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006454 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006455 lvi->lvi_loop[depth].var_idx,
6456 lvi->lvi_loop[depth].var_idx
6457 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006458 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006459 }
6460 return ga.ga_data;
6461}
6462
6463/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006464 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6465 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6466 * "pfx" is prefixed to every line.
6467 */
6468 static void
6469list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6470{
6471 int line_idx = 0;
6472 int prev_current = 0;
6473 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006474 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006475
6476 for (current = 0; current < instr_count; ++current)
6477 {
6478 isn_T *iptr = &instr[current];
6479 char *line;
6480
6481 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006482 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006483 while (line_idx < iptr->isn_lnum
6484 && line_idx < ufunc->uf_lines.ga_len)
6485 {
6486 if (current > prev_current)
6487 {
6488 msg_puts("\n\n");
6489 prev_current = current;
6490 }
6491 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6492 if (line != NULL)
6493 msg(line);
6494 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006495 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6496 {
6497 int first_def_arg = ufunc->uf_args.ga_len
6498 - ufunc->uf_def_args.ga_len;
6499
6500 if (def_arg_idx > 0)
6501 msg_puts("\n\n");
6502 msg_start();
6503 msg_puts(" ");
6504 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6505 first_def_arg + def_arg_idx]);
6506 msg_puts(" = ");
6507 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6508 msg_clr_eos();
6509 msg_end();
6510 }
6511 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006512
6513 switch (iptr->isn_type)
6514 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006515 case ISN_CONSTRUCT:
6516 smsg("%s%4d NEW %s size %d", pfx, current,
6517 iptr->isn_arg.construct.construct_class->class_name,
6518 (int)iptr->isn_arg.construct.construct_size);
6519 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006520 case ISN_EXEC:
6521 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6522 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006523 case ISN_EXEC_SPLIT:
6524 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6525 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006526 case ISN_EXECRANGE:
6527 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6528 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006529 case ISN_LEGACY_EVAL:
6530 smsg("%s%4d EVAL legacy %s", pfx, current,
6531 iptr->isn_arg.string);
6532 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006533 case ISN_REDIRSTART:
6534 smsg("%s%4d REDIR", pfx, current);
6535 break;
6536 case ISN_REDIREND:
6537 smsg("%s%4d REDIR END%s", pfx, current,
6538 iptr->isn_arg.number ? " append" : "");
6539 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006540 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006541#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006542 smsg("%s%4d CEXPR pre %s", pfx, current,
6543 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006544#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006545 break;
6546 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006547#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006548 {
6549 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6550
6551 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6552 cexpr_get_auname(cer->cer_cmdidx),
6553 cer->cer_forceit ? "!" : "",
6554 cer->cer_cmdline);
6555 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006556#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006557 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006558 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006559 smsg("%s%4d INSTR", pfx, current);
6560 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6561 msg(" -------------");
6562 break;
6563 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006564 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006565 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6566
6567 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006568 }
6569 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006570 case ISN_SUBSTITUTE:
6571 {
6572 subs_T *subs = &iptr->isn_arg.subs;
6573
6574 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6575 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6576 msg(" -------------");
6577 }
6578 break;
6579 case ISN_EXECCONCAT:
6580 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6581 (varnumber_T)iptr->isn_arg.number);
6582 break;
6583 case ISN_ECHO:
6584 {
6585 echo_T *echo = &iptr->isn_arg.echo;
6586
6587 smsg("%s%4d %s %d", pfx, current,
6588 echo->echo_with_white ? "ECHO" : "ECHON",
6589 echo->echo_count);
6590 }
6591 break;
6592 case ISN_EXECUTE:
6593 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006594 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006595 break;
6596 case ISN_ECHOMSG:
6597 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006598 (varnumber_T)(iptr->isn_arg.number));
6599 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006600 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006601 if (iptr->isn_arg.echowin.ewin_time > 0)
6602 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6603 iptr->isn_arg.echowin.ewin_count,
6604 iptr->isn_arg.echowin.ewin_time);
6605 else
6606 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6607 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006608 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006609 case ISN_ECHOCONSOLE:
6610 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6611 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006612 break;
6613 case ISN_ECHOERR:
6614 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006615 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006616 break;
6617 case ISN_LOAD:
6618 {
6619 if (iptr->isn_arg.number < 0)
6620 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6621 (varnumber_T)(iptr->isn_arg.number
6622 + STACK_FRAME_SIZE));
6623 else
6624 smsg("%s%4d LOAD $%lld", pfx, current,
6625 (varnumber_T)(iptr->isn_arg.number));
6626 }
6627 break;
6628 case ISN_LOADOUTER:
6629 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006630 isn_outer_T *outer = &iptr->isn_arg.outer;
6631
6632 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006633 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006634 outer->outer_depth,
6635 outer->outer_idx + STACK_FRAME_SIZE);
6636 else if (outer->outer_depth < 0)
6637 smsg("%s%4d LOADOUTER $%d in loop level %d",
6638 pfx, current,
6639 outer->outer_idx,
6640 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006641 else
6642 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006643 outer->outer_depth,
6644 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006645 }
6646 break;
6647 case ISN_LOADV:
6648 smsg("%s%4d LOADV v:%s", pfx, current,
6649 get_vim_var_name(iptr->isn_arg.number));
6650 break;
6651 case ISN_LOADSCRIPT:
6652 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006653 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6654 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6655 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006656
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006657 sv = get_script_svar(sref, -1);
6658 if (sv == NULL)
6659 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6660 pfx, current, si->sn_name);
6661 else
6662 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006663 sv->sv_name,
6664 sref->sref_idx,
6665 si->sn_name);
6666 }
6667 break;
6668 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006669 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006670 {
6671 scriptitem_T *si = SCRIPT_ITEM(
6672 iptr->isn_arg.loadstore.ls_sid);
6673
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006674 smsg("%s%4d %s s:%s from %s", pfx, current,
6675 iptr->isn_type == ISN_LOADS ? "LOADS"
6676 : "LOADEXPORT",
6677 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006678 }
6679 break;
6680 case ISN_LOADAUTO:
6681 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6682 break;
6683 case ISN_LOADG:
6684 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6685 break;
6686 case ISN_LOADB:
6687 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6688 break;
6689 case ISN_LOADW:
6690 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6691 break;
6692 case ISN_LOADT:
6693 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6694 break;
6695 case ISN_LOADGDICT:
6696 smsg("%s%4d LOAD g:", pfx, current);
6697 break;
6698 case ISN_LOADBDICT:
6699 smsg("%s%4d LOAD b:", pfx, current);
6700 break;
6701 case ISN_LOADWDICT:
6702 smsg("%s%4d LOAD w:", pfx, current);
6703 break;
6704 case ISN_LOADTDICT:
6705 smsg("%s%4d LOAD t:", pfx, current);
6706 break;
6707 case ISN_LOADOPT:
6708 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6709 break;
6710 case ISN_LOADENV:
6711 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6712 break;
6713 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006714 smsg("%s%4d LOADREG @%c", pfx, current,
6715 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006716 break;
6717
6718 case ISN_STORE:
6719 if (iptr->isn_arg.number < 0)
6720 smsg("%s%4d STORE arg[%lld]", pfx, current,
6721 iptr->isn_arg.number + STACK_FRAME_SIZE);
6722 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006723 smsg("%s%4d STORE $%lld", pfx, current,
6724 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006725 break;
6726 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006727 {
6728 isn_outer_T *outer = &iptr->isn_arg.outer;
6729
6730 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6731 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6732 pfx, current, outer->outer_idx);
6733 else
6734 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6735 outer->outer_depth, outer->outer_idx);
6736 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006737 break;
6738 case ISN_STOREV:
6739 smsg("%s%4d STOREV v:%s", pfx, current,
6740 get_vim_var_name(iptr->isn_arg.number));
6741 break;
6742 case ISN_STOREAUTO:
6743 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6744 break;
6745 case ISN_STOREG:
6746 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6747 break;
6748 case ISN_STOREB:
6749 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6750 break;
6751 case ISN_STOREW:
6752 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6753 break;
6754 case ISN_STORET:
6755 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6756 break;
6757 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006758 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006759 {
6760 scriptitem_T *si = SCRIPT_ITEM(
6761 iptr->isn_arg.loadstore.ls_sid);
6762
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006763 smsg("%s%4d %s %s in %s", pfx, current,
6764 iptr->isn_type == ISN_STORES
6765 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006766 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6767 }
6768 break;
6769 case ISN_STORESCRIPT:
6770 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006771 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6772 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6773 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006774
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006775 sv = get_script_svar(sref, -1);
6776 if (sv == NULL)
6777 smsg("%s%4d STORESCRIPT [deleted] in %s",
6778 pfx, current, si->sn_name);
6779 else
6780 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006781 sv->sv_name,
6782 sref->sref_idx,
6783 si->sn_name);
6784 }
6785 break;
6786 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006787 case ISN_STOREFUNCOPT:
6788 smsg("%s%4d %s &%s", pfx, current,
6789 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006790 iptr->isn_arg.storeopt.so_name);
6791 break;
6792 case ISN_STOREENV:
6793 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6794 break;
6795 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006796 smsg("%s%4d STOREREG @%c", pfx, current,
6797 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006798 break;
6799 case ISN_STORENR:
6800 smsg("%s%4d STORE %lld in $%d", pfx, current,
6801 iptr->isn_arg.storenr.stnr_val,
6802 iptr->isn_arg.storenr.stnr_idx);
6803 break;
6804
6805 case ISN_STOREINDEX:
6806 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006807 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006808 break;
6809
6810 case ISN_STORERANGE:
6811 smsg("%s%4d STORERANGE", pfx, current);
6812 break;
6813
Bram Moolenaard505d172022-12-18 21:42:55 +00006814 case ISN_LOAD_CLASSMEMBER:
6815 case ISN_STORE_CLASSMEMBER:
6816 {
6817 class_T *cl = iptr->isn_arg.classmember.cm_class;
6818 int idx = iptr->isn_arg.classmember.cm_idx;
6819 ocmember_T *ocm = &cl->class_class_members[idx];
6820 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6821 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6822 ? "LOAD" : "STORE",
6823 cl->class_name, ocm->ocm_name);
6824 }
6825 break;
6826
Bram Moolenaar4c137212021-04-19 16:48:48 +02006827 // constants
6828 case ISN_PUSHNR:
6829 smsg("%s%4d PUSHNR %lld", pfx, current,
6830 (varnumber_T)(iptr->isn_arg.number));
6831 break;
6832 case ISN_PUSHBOOL:
6833 case ISN_PUSHSPEC:
6834 smsg("%s%4d PUSH %s", pfx, current,
6835 get_var_special_name(iptr->isn_arg.number));
6836 break;
6837 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006838 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006839 break;
6840 case ISN_PUSHS:
6841 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6842 break;
6843 case ISN_PUSHBLOB:
6844 {
6845 char_u *r;
6846 char_u numbuf[NUMBUFLEN];
6847 char_u *tofree;
6848
6849 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6850 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6851 vim_free(tofree);
6852 }
6853 break;
6854 case ISN_PUSHFUNC:
6855 {
6856 char *name = (char *)iptr->isn_arg.string;
6857
6858 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6859 name == NULL ? "[none]" : name);
6860 }
6861 break;
6862 case ISN_PUSHCHANNEL:
6863#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006864 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006865#endif
6866 break;
6867 case ISN_PUSHJOB:
6868#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006869 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006870#endif
6871 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006872 case ISN_PUSHOBJ:
6873 smsg("%s%4d PUSHOBJ null", pfx, current);
6874 break;
6875 case ISN_PUSHCLASS:
6876 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006877 iptr->isn_arg.classarg == NULL ? "null"
6878 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006879 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006880 case ISN_PUSHEXC:
6881 smsg("%s%4d PUSH v:exception", pfx, current);
6882 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006883 case ISN_AUTOLOAD:
6884 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6885 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006886 case ISN_UNLET:
6887 smsg("%s%4d UNLET%s %s", pfx, current,
6888 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6889 iptr->isn_arg.unlet.ul_name);
6890 break;
6891 case ISN_UNLETENV:
6892 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6893 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6894 iptr->isn_arg.unlet.ul_name);
6895 break;
6896 case ISN_UNLETINDEX:
6897 smsg("%s%4d UNLETINDEX", pfx, current);
6898 break;
6899 case ISN_UNLETRANGE:
6900 smsg("%s%4d UNLETRANGE", pfx, current);
6901 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006902 case ISN_LOCKUNLOCK:
6903 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6904 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006905 case ISN_LOCKCONST:
6906 smsg("%s%4d LOCKCONST", pfx, current);
6907 break;
6908 case ISN_NEWLIST:
6909 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006910 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006911 break;
6912 case ISN_NEWDICT:
6913 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006914 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006915 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006916 case ISN_NEWPARTIAL:
6917 smsg("%s%4d NEWPARTIAL", pfx, current);
6918 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006919
6920 // function call
6921 case ISN_BCALL:
6922 {
6923 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6924
6925 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6926 internal_func_name(cbfunc->cbf_idx),
6927 cbfunc->cbf_argcount);
6928 }
6929 break;
6930 case ISN_DCALL:
6931 {
6932 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6933 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6934 + cdfunc->cdf_idx;
6935
6936 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006937 printable_func_name(df->df_ufunc),
6938 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006939 }
6940 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006941 case ISN_METHODCALL:
6942 {
6943 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6944
6945 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6946 mfunc->cmf_itf->class_name,
6947 mfunc->cmf_itf->class_obj_methods[
6948 mfunc->cmf_idx]->uf_name,
6949 mfunc->cmf_argcount);
6950 }
6951 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006952 case ISN_UCALL:
6953 {
6954 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6955
6956 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6957 cufunc->cuf_name, cufunc->cuf_argcount);
6958 }
6959 break;
6960 case ISN_PCALL:
6961 {
6962 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6963
6964 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6965 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6966 }
6967 break;
6968 case ISN_PCALL_END:
6969 smsg("%s%4d PCALL end", pfx, current);
6970 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006971 case ISN_DEFER:
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02006972 smsg("%s%4d DEFER %d args", pfx, current,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006973 (int)iptr->isn_arg.defer.defer_argcount);
6974 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006975 case ISN_RETURN:
6976 smsg("%s%4d RETURN", pfx, current);
6977 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006978 case ISN_RETURN_VOID:
6979 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006980 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006981 case ISN_RETURN_OBJECT:
6982 smsg("%s%4d RETURN object", pfx, current);
6983 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006984 case ISN_FUNCREF:
6985 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006986 funcref_T *funcref = &iptr->isn_arg.funcref;
6987 funcref_extra_T *extra = funcref->fr_extra;
6988 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006989
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006990 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006991 {
6992 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6993 + funcref->fr_dfunc_idx;
6994 name = df->df_ufunc->uf_name;
6995 }
6996 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006997 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006998 if (extra != NULL && extra->fre_class != NULL)
6999 {
7000 smsg("%s%4d FUNCREF %s.%s", pfx, current,
7001 extra->fre_class->class_name, name);
7002 }
7003 else if (extra == NULL
7004 || extra->fre_loopvar_info.lvi_depth == 0)
7005 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007006 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00007007 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007008 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007009 {
7010 char_u *info = printable_loopvarinfo(
7011 &extra->fre_loopvar_info);
7012
7013 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
7014 name, info);
7015 vim_free(info);
7016 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007017 }
7018 break;
7019
7020 case ISN_NEWFUNC:
7021 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01007022 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007023
Bram Moolenaarcc341812022-09-19 15:54:34 +01007024 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007025 smsg("%s%4d NEWFUNC %s %s", pfx, current,
7026 arg->nfa_lambda, arg->nfa_global);
7027 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007028 {
7029 char_u *info = printable_loopvarinfo(
7030 &arg->nfa_loopvar_info);
7031
7032 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
7033 arg->nfa_lambda, arg->nfa_global, info);
7034 vim_free(info);
7035 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007036 }
7037 break;
7038
7039 case ISN_DEF:
7040 {
7041 char_u *name = iptr->isn_arg.string;
7042
7043 smsg("%s%4d DEF %s", pfx, current,
7044 name == NULL ? (char_u *)"" : name);
7045 }
7046 break;
7047
7048 case ISN_JUMP:
7049 {
7050 char *when = "?";
7051
7052 switch (iptr->isn_arg.jump.jump_when)
7053 {
7054 case JUMP_ALWAYS:
7055 when = "JUMP";
7056 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007057 case JUMP_NEVER:
7058 iemsg("JUMP_NEVER should not be used");
7059 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007060 case JUMP_AND_KEEP_IF_TRUE:
7061 when = "JUMP_AND_KEEP_IF_TRUE";
7062 break;
7063 case JUMP_IF_FALSE:
7064 when = "JUMP_IF_FALSE";
7065 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007066 case JUMP_WHILE_FALSE:
7067 when = "JUMP_WHILE_FALSE"; // unused
7068 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007069 case JUMP_IF_COND_FALSE:
7070 when = "JUMP_IF_COND_FALSE";
7071 break;
7072 case JUMP_IF_COND_TRUE:
7073 when = "JUMP_IF_COND_TRUE";
7074 break;
7075 }
7076 smsg("%s%4d %s -> %d", pfx, current, when,
7077 iptr->isn_arg.jump.jump_where);
7078 }
7079 break;
7080
7081 case ISN_JUMP_IF_ARG_SET:
7082 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7083 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7084 iptr->isn_arg.jump.jump_where);
7085 break;
7086
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007087 case ISN_JUMP_IF_ARG_NOT_SET:
7088 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7089 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7090 iptr->isn_arg.jump.jump_where);
7091 break;
7092
Bram Moolenaar4c137212021-04-19 16:48:48 +02007093 case ISN_FOR:
7094 {
7095 forloop_T *forloop = &iptr->isn_arg.forloop;
7096
7097 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007098 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007099 }
7100 break;
7101
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007102 case ISN_ENDLOOP:
7103 {
7104 endloop_T *endloop = &iptr->isn_arg.endloop;
7105
Bram Moolenaarcc341812022-09-19 15:54:34 +01007106 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7107 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007108 endloop->end_funcref_idx,
7109 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007110 endloop->end_var_idx + endloop->end_var_count - 1,
7111 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007112 }
7113 break;
7114
7115 case ISN_WHILE:
7116 {
7117 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7118
7119 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7120 whileloop->while_funcref_idx,
7121 whileloop->while_end);
7122 }
7123 break;
7124
Bram Moolenaar4c137212021-04-19 16:48:48 +02007125 case ISN_TRY:
7126 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007127 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007128
7129 if (try->try_ref->try_finally == 0)
7130 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7131 pfx, current,
7132 try->try_ref->try_catch,
7133 try->try_ref->try_endtry);
7134 else
7135 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7136 pfx, current,
7137 try->try_ref->try_catch,
7138 try->try_ref->try_finally,
7139 try->try_ref->try_endtry);
7140 }
7141 break;
7142 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007143 smsg("%s%4d CATCH", pfx, current);
7144 break;
7145 case ISN_TRYCONT:
7146 {
7147 trycont_T *trycont = &iptr->isn_arg.trycont;
7148
7149 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7150 trycont->tct_levels,
7151 trycont->tct_levels == 1 ? "" : "s",
7152 trycont->tct_where);
7153 }
7154 break;
7155 case ISN_FINALLY:
7156 smsg("%s%4d FINALLY", pfx, current);
7157 break;
7158 case ISN_ENDTRY:
7159 smsg("%s%4d ENDTRY", pfx, current);
7160 break;
7161 case ISN_THROW:
7162 smsg("%s%4d THROW", pfx, current);
7163 break;
7164
7165 // expression operations on number
7166 case ISN_OPNR:
7167 case ISN_OPFLOAT:
7168 case ISN_OPANY:
7169 {
7170 char *what;
7171 char *ins;
7172
7173 switch (iptr->isn_arg.op.op_type)
7174 {
7175 case EXPR_MULT: what = "*"; break;
7176 case EXPR_DIV: what = "/"; break;
7177 case EXPR_REM: what = "%"; break;
7178 case EXPR_SUB: what = "-"; break;
7179 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007180 case EXPR_LSHIFT: what = "<<"; break;
7181 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007182 default: what = "???"; break;
7183 }
7184 switch (iptr->isn_type)
7185 {
7186 case ISN_OPNR: ins = "OPNR"; break;
7187 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7188 case ISN_OPANY: ins = "OPANY"; break;
7189 default: ins = "???"; break;
7190 }
7191 smsg("%s%4d %s %s", pfx, current, ins, what);
7192 }
7193 break;
7194
7195 case ISN_COMPAREBOOL:
7196 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007197 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007198 case ISN_COMPARENR:
7199 case ISN_COMPAREFLOAT:
7200 case ISN_COMPARESTRING:
7201 case ISN_COMPAREBLOB:
7202 case ISN_COMPARELIST:
7203 case ISN_COMPAREDICT:
7204 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007205 case ISN_COMPARECLASS:
7206 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007207 case ISN_COMPAREANY:
7208 {
7209 char *p;
7210 char buf[10];
7211 char *type;
7212
7213 switch (iptr->isn_arg.op.op_type)
7214 {
7215 case EXPR_EQUAL: p = "=="; break;
7216 case EXPR_NEQUAL: p = "!="; break;
7217 case EXPR_GREATER: p = ">"; break;
7218 case EXPR_GEQUAL: p = ">="; break;
7219 case EXPR_SMALLER: p = "<"; break;
7220 case EXPR_SEQUAL: p = "<="; break;
7221 case EXPR_MATCH: p = "=~"; break;
7222 case EXPR_IS: p = "is"; break;
7223 case EXPR_ISNOT: p = "isnot"; break;
7224 case EXPR_NOMATCH: p = "!~"; break;
7225 default: p = "???"; break;
7226 }
7227 STRCPY(buf, p);
7228 if (iptr->isn_arg.op.op_ic == TRUE)
7229 strcat(buf, "?");
7230 switch(iptr->isn_type)
7231 {
7232 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7233 case ISN_COMPARESPECIAL:
7234 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007235 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007236 case ISN_COMPARENR: type = "COMPARENR"; break;
7237 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7238 case ISN_COMPARESTRING:
7239 type = "COMPARESTRING"; break;
7240 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7241 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7242 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7243 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007244 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7245 case ISN_COMPAREOBJECT:
7246 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007247 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7248 default: type = "???"; break;
7249 }
7250
7251 smsg("%s%4d %s %s", pfx, current, type, buf);
7252 }
7253 break;
7254
7255 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7256 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7257
7258 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007259 case ISN_CONCAT:
7260 smsg("%s%4d CONCAT size %lld", pfx, current,
7261 (varnumber_T)(iptr->isn_arg.number));
7262 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007263 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7264 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7265 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7266 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7267 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7268 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7269 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7270 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7271 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7272 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7273 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007274 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007275 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7276 iptr->isn_arg.getitem.gi_index,
7277 iptr->isn_arg.getitem.gi_with_op ?
7278 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007279 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7280 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7281 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007282 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7283 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007284 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007285 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007286 pfx, current,
7287 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007288 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007289 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007290 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007291 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007292 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7293 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7294
Bram Moolenaar4c137212021-04-19 16:48:48 +02007295 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7296
Bram Moolenaar4c137212021-04-19 16:48:48 +02007297 case ISN_CHECKTYPE:
7298 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007299 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007300 char *tofree = NULL;
7301 char *typename;
7302
7303 if (ct->ct_type->tt_type == VAR_FLOAT
7304 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7305 typename = "float|number";
7306 else
7307 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007308
7309 if (ct->ct_arg_idx == 0)
7310 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007311 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007312 (int)ct->ct_off);
7313 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007314 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007315 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007316 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007317 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007318 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007319 (int)ct->ct_arg_idx);
7320 vim_free(tofree);
7321 break;
7322 }
7323 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7324 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7325 iptr->isn_arg.checklen.cl_min_len);
7326 break;
7327 case ISN_SETTYPE:
7328 {
7329 char *tofree;
7330
7331 smsg("%s%4d SETTYPE %s", pfx, current,
7332 type_name(iptr->isn_arg.type.ct_type, &tofree));
7333 vim_free(tofree);
7334 break;
7335 }
7336 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007337 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7338 smsg("%s%4d INVERT %d (!val)", pfx, current,
7339 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007340 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007341 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7342 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007343 break;
7344 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007345 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007346 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007347 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7348 pfx, current,
7349 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007350 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007351 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7352 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007353 break;
7354 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007355 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007356 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007357 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007358 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7359 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007360 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007361 else
7362 smsg("%s%4d PUT %c %ld", pfx, current,
7363 iptr->isn_arg.put.put_regname,
7364 (long)iptr->isn_arg.put.put_lnum);
7365 break;
7366
Bram Moolenaar4c137212021-04-19 16:48:48 +02007367 case ISN_CMDMOD:
7368 {
7369 char_u *buf;
7370 size_t len = produce_cmdmods(
7371 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7372
7373 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007374 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007375 {
7376 (void)produce_cmdmods(
7377 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7378 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7379 vim_free(buf);
7380 }
7381 break;
7382 }
7383 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7384
7385 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007386 smsg("%s%4d PROFILE START line %d", pfx, current,
7387 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007388 break;
7389
7390 case ISN_PROF_END:
7391 smsg("%s%4d PROFILE END", pfx, current);
7392 break;
7393
Bram Moolenaare99d4222021-06-13 14:01:26 +02007394 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007395 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7396 iptr->isn_arg.debug.dbg_break_lnum + 1,
7397 iptr->isn_lnum,
7398 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007399 break;
7400
Bram Moolenaar4c137212021-04-19 16:48:48 +02007401 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7402 iptr->isn_arg.unpack.unp_count,
7403 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7404 break;
7405 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7406 iptr->isn_arg.shuffle.shfl_item,
7407 iptr->isn_arg.shuffle.shfl_up);
7408 break;
7409 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7410
7411 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7412 return;
7413 }
7414
7415 out_flush(); // output one line at a time
7416 ui_breakcheck();
7417 if (got_int)
7418 break;
7419 }
7420}
7421
7422/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007423 * Handle command line completion for the :disassemble command.
7424 */
7425 void
7426set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7427{
7428 char_u *p;
7429
7430 // Default: expand user functions, "debug" and "profile"
7431 xp->xp_context = EXPAND_DISASSEMBLE;
7432 xp->xp_pattern = arg;
7433
7434 // first argument already typed: only user function names
7435 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7436 {
7437 xp->xp_context = EXPAND_USER_FUNC;
7438 xp->xp_pattern = skipwhite(p);
7439 }
7440}
7441
7442/*
7443 * Function given to ExpandGeneric() to obtain the list of :disassemble
7444 * arguments.
7445 */
7446 char_u *
7447get_disassemble_argument(expand_T *xp, int idx)
7448{
7449 if (idx == 0)
7450 return (char_u *)"debug";
7451 if (idx == 1)
7452 return (char_u *)"profile";
7453 return get_user_func_name(xp, idx - 2);
7454}
7455
7456/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007457 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007458 * We don't really need this at runtime, but we do have tests that require it,
7459 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 */
7461 void
7462ex_disassemble(exarg_T *eap)
7463{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007464 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007465 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007467 isn_T *instr = NULL; // init to shut up gcc warning
7468 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007469 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007470
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007471 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007472 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007474 if (func_needs_compiling(ufunc, compile_type)
7475 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007476 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007477 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007479 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 return;
7481 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007482 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483
7484 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007485 switch (compile_type)
7486 {
7487 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007488#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007489 instr = dfunc->df_instr_prof;
7490 instr_count = dfunc->df_instr_prof_count;
7491 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007492#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007493 // FALLTHROUGH
7494 case CT_NONE:
7495 instr = dfunc->df_instr;
7496 instr_count = dfunc->df_instr_count;
7497 break;
7498 case CT_DEBUG:
7499 instr = dfunc->df_instr_debug;
7500 instr_count = dfunc->df_instr_debug_count;
7501 break;
7502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007503
Bram Moolenaar4c137212021-04-19 16:48:48 +02007504 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007505}
7506
7507/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007508 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509 * list, etc. Mostly like what JavaScript does, except that empty list and
7510 * empty dictionary are FALSE.
7511 */
7512 int
7513tv2bool(typval_T *tv)
7514{
7515 switch (tv->v_type)
7516 {
7517 case VAR_NUMBER:
7518 return tv->vval.v_number != 0;
7519 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007520 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521 case VAR_PARTIAL:
7522 return tv->vval.v_partial != NULL;
7523 case VAR_FUNC:
7524 case VAR_STRING:
7525 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7526 case VAR_LIST:
7527 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7528 case VAR_DICT:
7529 return tv->vval.v_dict != NULL
7530 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7531 case VAR_BOOL:
7532 case VAR_SPECIAL:
7533 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7534 case VAR_JOB:
7535#ifdef FEAT_JOB_CHANNEL
7536 return tv->vval.v_job != NULL;
7537#else
7538 break;
7539#endif
7540 case VAR_CHANNEL:
7541#ifdef FEAT_JOB_CHANNEL
7542 return tv->vval.v_channel != NULL;
7543#else
7544 break;
7545#endif
7546 case VAR_BLOB:
7547 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7548 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007549 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007550 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007551 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007552 case VAR_CLASS:
7553 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007554 case VAR_TYPEALIAS:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007555 break;
7556 }
7557 return FALSE;
7558}
7559
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007560 void
7561emsg_using_string_as(typval_T *tv, int as_number)
7562{
7563 semsg(_(as_number ? e_using_string_as_number_str
7564 : e_using_string_as_bool_str),
7565 tv->vval.v_string == NULL
7566 ? (char_u *)"" : tv->vval.v_string);
7567}
7568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007569/*
7570 * If "tv" is a string give an error and return FAIL.
7571 */
7572 int
7573check_not_string(typval_T *tv)
7574{
7575 if (tv->v_type == VAR_STRING)
7576 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007577 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007578 clear_tv(tv);
7579 return FAIL;
7580 }
7581 return OK;
7582}
7583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007584
7585#endif // FEAT_EVAL