blob: 1efed35283a9f24244e884cb944b1fe27e435b0c [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);
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01004458 object_T *obj = tv->vval.v_object;
4459 ++obj->obj_refcount;
4460
4461 // Lock all the constant object variables
4462 obj_lock_const_vars(obj);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004463 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004464 // FALLTHROUGH
4465
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004466 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 case ISN_RETURN:
4468 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004469 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004470 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004471
Ernie Raelb077b582023-12-14 20:11:44 +01004472 ///////////////////////////////////////////////////
4473 // TODO: If FAIL, line number in output not correct
4474 ///////////////////////////////////////////////////
4475 if (check_typval_is_value(STACK_TV_BOT(-1)) == FAIL)
4476 goto theend;
4477
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004478 if (trystack->ga_len > 0)
4479 trycmd = ((trycmd_T *)trystack->ga_data)
4480 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004481 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004482 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004484 // jump to ":finally" or ":endtry"
4485 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004486 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004487 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004488 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 trycmd->tcd_return = TRUE;
4490 }
4491 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004492 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 }
4494 break;
4495
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004496 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 case ISN_FUNCREF:
4498 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004499 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4500 ufunc_T *ufunc;
4501 funcref_T *funcref = &iptr->isn_arg.funcref;
4502 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004505 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004506 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004507 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004508 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004509 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004510 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004511 if (extra != NULL && extra->fre_class != NULL)
4512 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004513 class_T *cl;
4514 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004515 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004516 tv = STACK_TV_BOT(-1);
4517 if (tv->v_type != VAR_OBJECT)
4518 {
4519 object_required_error(tv);
4520 vim_free(pt);
4521 goto on_error;
4522 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004523
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004524 object_T *obj = tv->vval.v_object;
4525 cl = obj->obj_class;
4526 // drop the value from the stack
4527 clear_tv(tv);
4528 --ectx->ec_stack.ga_len;
4529
4530 pt->pt_obj = obj;
4531 ++obj->obj_refcount;
4532 }
4533 else
4534 cl = extra->fre_class;
4535
4536 if (extra->fre_object_method)
4537 {
4538 // object method
4539 // convert the interface index to the object index
4540 int idx =
4541 object_index_from_itf_index(extra->fre_class,
4542 TRUE, extra->fre_method_idx, cl);
4543 ufunc = cl->class_obj_methods[idx];
4544 }
4545 else
4546 {
4547 // class method
4548 ufunc =
4549 cl->class_class_functions[extra->fre_method_idx];
4550 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004551 }
4552 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004553 {
4554 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4555 + funcref->fr_dfunc_idx;
4556
4557 ufunc = pt_dfunc->df_ufunc;
4558 }
4559 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004560 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004561 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004562 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004563 if (ufunc == NULL)
4564 {
4565 SOURCING_LNUM = iptr->isn_lnum;
4566 iemsg("ufunc unexpectedly NULL for FUNCREF");
4567 goto theend;
4568 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004569 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004570 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004571 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004572 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004574 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 tv->vval.v_partial = pt;
4576 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004577 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 }
4579 break;
4580
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004581 // Create a global function from a lambda.
4582 case ISN_NEWFUNC:
4583 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004584 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004585
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004586 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004587 arg->nfa_global, &arg->nfa_loopvar_info,
4588 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004589 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004590 }
4591 break;
4592
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004593 // List functions
4594 case ISN_DEF:
4595 if (iptr->isn_arg.string == NULL)
4596 list_functions(NULL);
4597 else
4598 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004599 exarg_T ea;
4600 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004601
4602 CLEAR_FIELD(ea);
4603 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004604 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004605 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004606 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004607 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004608 }
4609 break;
4610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 // jump if a condition is met
4612 case ISN_JUMP:
4613 {
4614 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004615 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 int jump = TRUE;
4617
4618 if (when != JUMP_ALWAYS)
4619 {
4620 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004621 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004622 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004623 || when == JUMP_IF_COND_TRUE)
4624 {
4625 SOURCING_LNUM = iptr->isn_lnum;
4626 jump = tv_get_bool_chk(tv, &error);
4627 if (error)
4628 goto on_error;
4629 }
4630 else
4631 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004632 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004634 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 {
4636 // drop the value from the stack
4637 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004638 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 }
4640 }
4641 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004642 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 }
4644 break;
4645
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004646 // "while": jump to end if a condition is false
4647 case ISN_WHILE:
4648 {
4649 int error = FALSE;
4650 int jump = TRUE;
4651
4652 tv = STACK_TV_BOT(-1);
4653 SOURCING_LNUM = iptr->isn_lnum;
4654 jump = !tv_get_bool_chk(tv, &error);
4655 if (error)
4656 goto on_error;
4657 // drop the value from the stack
4658 clear_tv(tv);
4659 --ectx->ec_stack.ga_len;
4660 if (jump)
4661 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4662
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004663 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004664 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004665 tv = STACK_TV_VAR(
4666 iptr->isn_arg.whileloop.while_funcref_idx);
4667 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4668 }
4669 break;
4670
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004671 // Jump if an argument with a default value was already set and not
4672 // v:none.
4673 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004674 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004675 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004676 int arg_set = tv->v_type != VAR_UNKNOWN
4677 && !(tv->v_type == VAR_SPECIAL
4678 && tv->vval.v_number == VVAL_NONE);
4679 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004680 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004681 break;
4682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 // top of a for loop
4684 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004685 if (execute_for(iptr, ectx) == FAIL)
4686 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 break;
4688
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004689 // end of a for or while loop
4690 case ISN_ENDLOOP:
4691 if (execute_endloop(iptr, ectx) == FAIL)
4692 goto theend;
4693 break;
4694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 // start of ":try" block
4696 case ISN_TRY:
4697 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004698 trycmd_T *trycmd = NULL;
4699
Bram Moolenaar35578162021-08-02 19:10:38 +02004700 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004701 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004702 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4703 + ectx->ec_trystack.ga_len;
4704 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004706 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004707 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4708 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004709 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004710 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004711 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004712 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004713 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004714 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 }
4716 break;
4717
4718 case ISN_PUSHEXC:
4719 if (current_exception == NULL)
4720 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004721 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004723 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004725 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004726 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004728 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004730 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 tv->vval.v_string = vim_strsave(
4732 (char_u *)current_exception->value);
4733 break;
4734
4735 case ISN_CATCH:
4736 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004737 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004738 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739
Bram Moolenaar4c137212021-04-19 16:48:48 +02004740 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004741 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004743 trycmd->tcd_caught = TRUE;
4744 trycmd->tcd_did_throw = FALSE;
4745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004747 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 catch_exception(current_exception);
4749 }
4750 break;
4751
Bram Moolenaarc150c092021-02-13 15:02:46 +01004752 case ISN_TRYCONT:
4753 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004754 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004755 trycont_T *trycont = &iptr->isn_arg.trycont;
4756 int i;
4757 trycmd_T *trycmd;
4758 int iidx = trycont->tct_where;
4759
4760 if (trystack->ga_len < trycont->tct_levels)
4761 {
4762 siemsg("TRYCONT: expected %d levels, found %d",
4763 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004764 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004765 }
4766 // Make :endtry jump to any outer try block and the last
4767 // :endtry inside the loop to the loop start.
4768 for (i = trycont->tct_levels; i > 0; --i)
4769 {
4770 trycmd = ((trycmd_T *)trystack->ga_data)
4771 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004772 // Add one to tcd_cont to be able to jump to
4773 // instruction with index zero.
4774 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004775 iidx = trycmd->tcd_finally_idx == 0
4776 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004777 }
4778 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004779 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004780 }
4781 break;
4782
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004783 case ISN_FINALLY:
4784 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004785 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004786 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4787 + trystack->ga_len - 1;
4788
4789 // Reset the index to avoid a return statement jumps here
4790 // again.
4791 trycmd->tcd_finally_idx = 0;
4792 break;
4793 }
4794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 // end of ":try" block
4796 case ISN_ENDTRY:
4797 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004798 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004799 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800
Bram Moolenaar06651632022-04-27 17:54:25 +01004801 --trystack->ga_len;
4802 --trylevel;
4803 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4804 if (trycmd->tcd_did_throw)
4805 did_throw = TRUE;
4806 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004808 // discard the exception
4809 if (caught_stack == current_exception)
4810 caught_stack = caught_stack->caught;
4811 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004813
4814 if (trycmd->tcd_return)
4815 goto func_return;
4816
4817 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4818 {
4819 --ectx->ec_stack.ga_len;
4820 clear_tv(STACK_TV_BOT(0));
4821 }
4822 if (trycmd->tcd_cont != 0)
4823 // handling :continue: jump to outer try block or
4824 // start of the loop
4825 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826 }
4827 break;
4828
4829 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004830 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004831 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004832
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004833 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4834 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004835 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004836 // the function to abort but not display an error.
4837 tv = STACK_TV_BOT(-1);
4838 clear_tv(tv);
4839 tv->v_type = VAR_NUMBER;
4840 tv->vval.v_number = 0;
4841 goto done;
4842 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004843 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004844 tv = STACK_TV_BOT(0);
4845 if (tv->vval.v_string == NULL
4846 || *skipwhite(tv->vval.v_string) == NUL)
4847 {
4848 vim_free(tv->vval.v_string);
4849 SOURCING_LNUM = iptr->isn_lnum;
4850 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004851 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004852 }
4853
4854 // Inside a "catch" we need to first discard the caught
4855 // exception.
4856 if (trystack->ga_len > 0)
4857 {
4858 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4859 + trystack->ga_len - 1;
4860 if (trycmd->tcd_caught && current_exception != NULL)
4861 {
4862 // discard the exception
4863 if (caught_stack == current_exception)
4864 caught_stack = caught_stack->caught;
4865 discard_current_exception();
4866 trycmd->tcd_caught = FALSE;
4867 }
4868 }
4869
Bram Moolenaar90a57162022-02-12 14:23:17 +00004870 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004871 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4872 == FAIL)
4873 {
4874 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004875 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004876 }
4877 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879 break;
4880
4881 // compare with special values
4882 case ISN_COMPAREBOOL:
4883 case ISN_COMPARESPECIAL:
4884 {
4885 typval_T *tv1 = STACK_TV_BOT(-2);
4886 typval_T *tv2 = STACK_TV_BOT(-1);
4887 varnumber_T arg1 = tv1->vval.v_number;
4888 varnumber_T arg2 = tv2->vval.v_number;
4889 int res;
4890
Bram Moolenaar06651632022-04-27 17:54:25 +01004891 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4892 res = arg1 == arg2;
4893 else
4894 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895
Bram Moolenaar4c137212021-04-19 16:48:48 +02004896 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 tv1->v_type = VAR_BOOL;
4898 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4899 }
4900 break;
4901
Bram Moolenaar7a222242022-03-01 19:23:24 +00004902 case ISN_COMPARENULL:
4903 {
4904 typval_T *tv1 = STACK_TV_BOT(-2);
4905 typval_T *tv2 = STACK_TV_BOT(-1);
4906 int res;
4907
4908 res = typval_compare_null(tv1, tv2);
4909 if (res == MAYBE)
4910 goto on_error;
4911 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4912 res = !res;
4913 clear_tv(tv1);
4914 clear_tv(tv2);
4915 --ectx->ec_stack.ga_len;
4916 tv1->v_type = VAR_BOOL;
4917 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4918 }
4919 break;
4920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921 // Operation with two number arguments
4922 case ISN_OPNR:
4923 case ISN_COMPARENR:
4924 {
4925 typval_T *tv1 = STACK_TV_BOT(-2);
4926 typval_T *tv2 = STACK_TV_BOT(-1);
4927 varnumber_T arg1 = tv1->vval.v_number;
4928 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004929 varnumber_T res = 0;
4930 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004932 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4933 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4934 {
4935 if (arg2 < 0)
4936 {
4937 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004938 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004939 goto on_error;
4940 }
4941 }
4942
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 switch (iptr->isn_arg.op.op_type)
4944 {
4945 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004946 case EXPR_DIV: if (arg2 == 0)
4947 div_zero = TRUE;
4948 else
4949 res = arg1 / arg2;
4950 break;
4951 case EXPR_REM: if (arg2 == 0)
4952 div_zero = TRUE;
4953 else
4954 res = arg1 % arg2;
4955 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 case EXPR_SUB: res = arg1 - arg2; break;
4957 case EXPR_ADD: res = arg1 + arg2; break;
4958
4959 case EXPR_EQUAL: res = arg1 == arg2; break;
4960 case EXPR_NEQUAL: res = arg1 != arg2; break;
4961 case EXPR_GREATER: res = arg1 > arg2; break;
4962 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4963 case EXPR_SMALLER: res = arg1 < arg2; break;
4964 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004965 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4966 res = 0;
4967 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004968 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004969 break;
4970 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4971 res = 0;
4972 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004973 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004974 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004975 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 }
4977
Bram Moolenaar4c137212021-04-19 16:48:48 +02004978 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 if (iptr->isn_type == ISN_COMPARENR)
4980 {
4981 tv1->v_type = VAR_BOOL;
4982 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4983 }
4984 else
4985 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004986 if (div_zero)
4987 {
4988 SOURCING_LNUM = iptr->isn_lnum;
4989 emsg(_(e_divide_by_zero));
4990 goto on_error;
4991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 }
4993 break;
4994
4995 // Computation with two float arguments
4996 case ISN_OPFLOAT:
4997 case ISN_COMPAREFLOAT:
4998 {
4999 typval_T *tv1 = STACK_TV_BOT(-2);
5000 typval_T *tv2 = STACK_TV_BOT(-1);
5001 float_T arg1 = tv1->vval.v_float;
5002 float_T arg2 = tv2->vval.v_float;
5003 float_T res = 0;
5004 int cmp = FALSE;
5005
5006 switch (iptr->isn_arg.op.op_type)
5007 {
5008 case EXPR_MULT: res = arg1 * arg2; break;
5009 case EXPR_DIV: res = arg1 / arg2; break;
5010 case EXPR_SUB: res = arg1 - arg2; break;
5011 case EXPR_ADD: res = arg1 + arg2; break;
5012
5013 case EXPR_EQUAL: cmp = arg1 == arg2; break;
5014 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
5015 case EXPR_GREATER: cmp = arg1 > arg2; break;
5016 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
5017 case EXPR_SMALLER: cmp = arg1 < arg2; break;
5018 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
5019 default: cmp = 0; break;
5020 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005021 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022 if (iptr->isn_type == ISN_COMPAREFLOAT)
5023 {
5024 tv1->v_type = VAR_BOOL;
5025 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
5026 }
5027 else
5028 tv1->vval.v_float = res;
5029 }
5030 break;
5031
5032 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00005033 case ISN_COMPAREDICT:
5034 case ISN_COMPAREFUNC:
5035 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005037 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 {
5039 typval_T *tv1 = STACK_TV_BOT(-2);
5040 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00005041 exprtype_T exprtype = iptr->isn_arg.op.op_type;
5042 int ic = iptr->isn_arg.op.op_ic;
5043 int res = FALSE;
5044 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045
Bram Moolenaar265f8112021-12-19 12:33:05 +00005046 SOURCING_LNUM = iptr->isn_lnum;
5047 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005049 status = typval_compare_list(tv1, tv2,
5050 exprtype, ic, &res);
5051 }
5052 else if (iptr->isn_type == ISN_COMPAREDICT)
5053 {
5054 status = typval_compare_dict(tv1, tv2,
5055 exprtype, ic, &res);
5056 }
5057 else if (iptr->isn_type == ISN_COMPAREFUNC)
5058 {
5059 status = typval_compare_func(tv1, tv2,
5060 exprtype, ic, &res);
5061 }
5062 else if (iptr->isn_type == ISN_COMPARESTRING)
5063 {
5064 status = typval_compare_string(tv1, tv2,
5065 exprtype, ic, &res);
5066 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005067 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005068 {
5069 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005071 else // ISN_COMPAREOBJECT
5072 {
5073 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005074 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005075 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005076 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 clear_tv(tv1);
5078 clear_tv(tv2);
5079 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005080 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5081 if (status == FAIL)
5082 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083 }
5084 break;
5085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 case ISN_COMPAREANY:
5087 {
5088 typval_T *tv1 = STACK_TV_BOT(-2);
5089 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005090 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005092 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093
Bram Moolenaareb26f432020-09-14 16:50:05 +02005094 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005095 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005097 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005098 if (status == FAIL)
5099 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 }
5101 break;
5102
5103 case ISN_ADDLIST:
5104 case ISN_ADDBLOB:
5105 {
5106 typval_T *tv1 = STACK_TV_BOT(-2);
5107 typval_T *tv2 = STACK_TV_BOT(-1);
5108
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005109 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005111 {
5112 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5113 && tv1->vval.v_list != NULL)
5114 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5115 NULL);
5116 else
5117 eval_addlist(tv1, tv2);
5118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 else
5120 eval_addblob(tv1, tv2);
5121 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005122 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 }
5124 break;
5125
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005126 case ISN_LISTAPPEND:
5127 {
5128 typval_T *tv1 = STACK_TV_BOT(-2);
5129 typval_T *tv2 = STACK_TV_BOT(-1);
5130 list_T *l = tv1->vval.v_list;
5131
5132 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005133 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005134 if (l == NULL)
5135 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005136 emsg(_(e_cannot_add_to_null_list));
5137 goto on_error;
5138 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005139 if (value_check_lock(l->lv_lock, NULL, FALSE))
5140 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005141 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005142 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005143 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005144 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005145 }
5146 break;
5147
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005148 case ISN_BLOBAPPEND:
5149 {
5150 typval_T *tv1 = STACK_TV_BOT(-2);
5151 typval_T *tv2 = STACK_TV_BOT(-1);
5152 blob_T *b = tv1->vval.v_blob;
5153 int error = FALSE;
5154 varnumber_T n;
5155
5156 // add a number to a blob
5157 if (b == NULL)
5158 {
5159 SOURCING_LNUM = iptr->isn_lnum;
5160 emsg(_(e_cannot_add_to_null_blob));
5161 goto on_error;
5162 }
5163 n = tv_get_number_chk(tv2, &error);
5164 if (error)
5165 goto on_error;
5166 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005167 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005168 }
5169 break;
5170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171 // Computation with two arguments of unknown type
5172 case ISN_OPANY:
5173 {
5174 typval_T *tv1 = STACK_TV_BOT(-2);
5175 typval_T *tv2 = STACK_TV_BOT(-1);
5176 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178 int error = FALSE;
5179
5180 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5181 {
5182 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5183 {
5184 eval_addlist(tv1, tv2);
5185 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005186 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005187 break;
5188 }
5189 else if (tv1->v_type == VAR_BLOB
5190 && tv2->v_type == VAR_BLOB)
5191 {
5192 eval_addblob(tv1, tv2);
5193 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005194 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195 break;
5196 }
5197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198 if (tv1->v_type == VAR_FLOAT)
5199 {
5200 f1 = tv1->vval.v_float;
5201 n1 = 0;
5202 }
5203 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005205 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 n1 = tv_get_number_chk(tv1, &error);
5207 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005208 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209 if (tv2->v_type == VAR_FLOAT)
5210 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 if (tv2->v_type == VAR_FLOAT)
5213 {
5214 f2 = tv2->vval.v_float;
5215 n2 = 0;
5216 }
5217 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 {
5219 n2 = tv_get_number_chk(tv2, &error);
5220 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005221 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 if (tv1->v_type == VAR_FLOAT)
5223 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005224 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 // if there is a float on either side the result is a float
5226 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5227 {
5228 switch (iptr->isn_arg.op.op_type)
5229 {
5230 case EXPR_MULT: f1 = f1 * f2; break;
5231 case EXPR_DIV: f1 = f1 / f2; break;
5232 case EXPR_SUB: f1 = f1 - f2; break;
5233 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005234 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005235 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005236 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 }
5238 clear_tv(tv1);
5239 clear_tv(tv2);
5240 tv1->v_type = VAR_FLOAT;
5241 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005242 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243 }
5244 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005246 int failed = FALSE;
5247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248 switch (iptr->isn_arg.op.op_type)
5249 {
5250 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005251 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5252 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005253 goto on_error;
5254 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255 case EXPR_SUB: n1 = n1 - n2; break;
5256 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005257 default: n1 = num_modulus(n1, n2, &failed);
5258 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005259 goto on_error;
5260 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261 }
5262 clear_tv(tv1);
5263 clear_tv(tv2);
5264 tv1->v_type = VAR_NUMBER;
5265 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005266 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267 }
5268 }
5269 break;
5270
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005271 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005272 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005273 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005274 int is_slice = iptr->isn_type == ISN_STRSLICE;
5275 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005276 char_u *res;
5277
5278 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005279 // string slice: string is at stack-3, first index at
5280 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005281 if (is_slice)
5282 {
5283 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005284 n1 = tv->vval.v_number;
5285 }
5286
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005287 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005288 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005289
Bram Moolenaar4c137212021-04-19 16:48:48 +02005290 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005291 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005292 if (is_slice)
5293 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005294 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005295 else
5296 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005297 // single character (including composing characters).
5298 // If the index is too big or negative the result is
5299 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005300 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005301 vim_free(tv->vval.v_string);
5302 tv->vval.v_string = res;
5303 }
5304 break;
5305
5306 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005307 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005308 case ISN_BLOBINDEX:
5309 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005311 int is_slice = iptr->isn_type == ISN_LISTSLICE
5312 || iptr->isn_type == ISN_BLOBSLICE;
5313 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5314 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005315 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005316 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317
5318 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005319 // list slice: list is at stack-3, indexes at stack-2 and
5320 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005321 // Same for blob.
5322 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005323
5324 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005325 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005326 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005327
5328 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005329 {
Bram Moolenaared591872020-08-15 22:14:53 +02005330 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005331 n1 = tv->vval.v_number;
5332 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333 }
Bram Moolenaared591872020-08-15 22:14:53 +02005334
Bram Moolenaar4c137212021-04-19 16:48:48 +02005335 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005336 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005337 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005338 if (is_blob)
5339 {
5340 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5341 n1, n2, FALSE, tv) == FAIL)
5342 goto on_error;
5343 }
5344 else
5345 {
5346 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5347 n1, n2, FALSE, tv, TRUE) == FAIL)
5348 goto on_error;
5349 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350 }
5351 break;
5352
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005353 case ISN_ANYINDEX:
5354 case ISN_ANYSLICE:
5355 {
5356 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5357 typval_T *var1, *var2;
5358 int res;
5359
5360 // index: composite is at stack-2, index at stack-1
5361 // slice: composite is at stack-3, indexes at stack-2 and
5362 // stack-1
5363 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005364 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005365 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5366 goto on_error;
5367 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5368 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005369 res = eval_index_inner(tv, is_slice, var1, var2,
5370 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005371 clear_tv(var1);
5372 if (is_slice)
5373 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005374 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005375 if (res == FAIL)
5376 goto on_error;
5377 }
5378 break;
5379
Bram Moolenaar9af78762020-06-16 11:34:42 +02005380 case ISN_SLICE:
5381 {
5382 list_T *list;
5383 int count = iptr->isn_arg.number;
5384
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005385 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005386 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005387 list = tv->vval.v_list;
5388
5389 // no error for short list, expect it to be checked earlier
5390 if (list != NULL && list->lv_len >= count)
5391 {
5392 list_T *newlist = list_slice(list,
5393 count, list->lv_len - 1);
5394
5395 if (newlist != NULL)
5396 {
5397 list_unref(list);
5398 tv->vval.v_list = newlist;
5399 ++newlist->lv_refcount;
5400 }
5401 }
5402 }
5403 break;
5404
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005405 case ISN_GETITEM:
5406 {
5407 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005408 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005409
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005410 // Get list item: list is at stack-1, push item.
5411 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005412 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5413 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005414
Bram Moolenaar35578162021-08-02 19:10:38 +02005415 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005416 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005417 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005418 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005419
5420 // Useful when used in unpack assignment. Reset at
5421 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005422 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005423 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005424 }
5425 break;
5426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427 case ISN_MEMBER:
5428 {
5429 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005430 char_u *key;
5431 dictitem_T *di;
5432
5433 // dict member: dict is at stack-2, key at stack-1
5434 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005435 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005436 dict = tv->vval.v_dict;
5437
5438 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005439 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005440 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005441 if (key == NULL)
5442 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005443
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005444 if ((di = dict_find(dict, key, -1)) == NULL)
5445 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005446 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005447 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005448
5449 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005450 // stack contents makes sense and the dict stack is
5451 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005452 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005453 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005454 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005455 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005456 tv->v_type = VAR_NUMBER;
5457 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005458 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005459 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005460 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005461 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005462 // Put the dict used on the dict stack, it might be used by
5463 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005464 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005465 if (dict_stack_save(tv) == FAIL)
5466 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005467 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005468 }
5469 break;
5470
5471 // dict member with string key
5472 case ISN_STRINGMEMBER:
5473 {
5474 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005475 dictitem_T *di;
5476
5477 tv = STACK_TV_BOT(-1);
5478 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5479 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005480 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005481 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005482 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005483 }
5484 dict = tv->vval.v_dict;
5485
5486 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5487 == NULL)
5488 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005489 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005490 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005491 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005492 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005494 // Put the dict used on the dict stack, it might be used by
5495 // a dict function later.
5496 if (dict_stack_save(tv) == FAIL)
5497 goto on_fatal_error;
5498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005499 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005500 }
5501 break;
5502
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005503 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005504 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005505 {
5506 tv = STACK_TV_BOT(-1);
5507 if (tv->v_type != VAR_OBJECT)
5508 {
5509 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005510 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005511 goto on_error;
5512 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005513
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005514 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005515 if (obj == NULL)
5516 {
5517 SOURCING_LNUM = iptr->isn_lnum;
5518 emsg(_(e_using_null_object));
5519 goto on_error;
5520 }
5521
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005522 int idx;
5523 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005524 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005525 else
5526 {
5527 idx = iptr->isn_arg.classmember.cm_idx;
5528 // convert the interface index to the object index
5529 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005530 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005531 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005532 }
5533
Ernie Rael18143d32023-09-04 22:30:41 +02005534 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005535 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005536 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005537
5538 // Unreference the object after getting the member, it may
5539 // be freed.
5540 object_unref(obj);
5541 }
5542 break;
5543
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005544 case ISN_STORE_THIS:
5545 {
5546 int idx = iptr->isn_arg.number;
5547 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5548 // the members are located right after the object struct
5549 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5550 clear_tv(mtv);
5551 *mtv = *STACK_TV_BOT(-1);
5552 --ectx->ec_stack.ga_len;
5553 }
5554 break;
5555
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005556 case ISN_CLEARDICT:
5557 dict_stack_drop();
5558 break;
5559
5560 case ISN_USEDICT:
5561 {
5562 typval_T *dict_tv = dict_stack_get_tv();
5563
5564 // Turn "dict.Func" into a partial for "Func" bound to
5565 // "dict". Don't do this when "Func" is already a partial
5566 // that was bound explicitly (pt_auto is FALSE).
5567 tv = STACK_TV_BOT(-1);
5568 if (dict_tv != NULL
5569 && dict_tv->v_type == VAR_DICT
5570 && dict_tv->vval.v_dict != NULL
5571 && (tv->v_type == VAR_FUNC
5572 || (tv->v_type == VAR_PARTIAL
5573 && (tv->vval.v_partial->pt_auto
5574 || tv->vval.v_partial->pt_dict == NULL))))
5575 dict_tv->vval.v_dict =
5576 make_partial(dict_tv->vval.v_dict, tv);
5577 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005578 }
5579 break;
5580
5581 case ISN_NEGATENR:
5582 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005583 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005584 if (tv->v_type == VAR_FLOAT)
5585 tv->vval.v_float = -tv->vval.v_float;
5586 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005587 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588 break;
5589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590 case ISN_CHECKTYPE:
5591 {
5592 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005593 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005594 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005595
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005596 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005597 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005598 if (ct->ct_arg_idx > 0)
5599 {
5600 where.wt_index = ct->ct_arg_idx;
5601 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005602 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005603 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005604 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005605 if (r == FAIL)
5606 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005607
5608 // number 0 is FALSE, number 1 is TRUE
5609 if (tv->v_type == VAR_NUMBER
5610 && ct->ct_type->tt_type == VAR_BOOL
5611 && (tv->vval.v_number == 0
5612 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005614 tv->v_type = VAR_BOOL;
5615 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005616 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005617 }
5618 }
5619 break;
5620
Bram Moolenaar9af78762020-06-16 11:34:42 +02005621 case ISN_CHECKLEN:
5622 {
5623 int min_len = iptr->isn_arg.checklen.cl_min_len;
5624 list_T *list = NULL;
5625
5626 tv = STACK_TV_BOT(-1);
5627 if (tv->v_type == VAR_LIST)
5628 list = tv->vval.v_list;
5629 if (list == NULL || list->lv_len < min_len
5630 || (list->lv_len > min_len
5631 && !iptr->isn_arg.checklen.cl_more_OK))
5632 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005633 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005634 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005635 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005636 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005637 }
5638 }
5639 break;
5640
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005641 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005642 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005643 break;
5644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005645 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005646 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647 {
5648 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005649 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005650
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005651 if (iptr->isn_type == ISN_2BOOL)
5652 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005653 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005654 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005655 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005656 n = !n;
5657 }
5658 else
5659 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005660 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005661 SOURCING_LNUM = iptr->isn_lnum;
5662 n = tv_get_bool_chk(tv, &error);
5663 if (error)
5664 goto on_error;
5665 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666 clear_tv(tv);
5667 tv->v_type = VAR_BOOL;
5668 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5669 }
5670 break;
5671
5672 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005673 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005674 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005675 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5676 iptr->isn_type == ISN_2STRING_ANY,
5677 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005678 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005679 break;
5680
Bram Moolenaar08597872020-12-10 19:43:40 +01005681 case ISN_RANGE:
5682 {
5683 exarg_T ea;
5684 char *errormsg;
5685
Bram Moolenaarece0b872021-01-08 20:40:45 +01005686 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005687 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005688 ea.addr_type = ADDR_LINES;
5689 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005690 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005691 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005692 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005693
Bram Moolenaar35578162021-08-02 19:10:38 +02005694 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005695 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005696 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005697 tv = STACK_TV_BOT(-1);
5698 tv->v_type = VAR_NUMBER;
5699 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005700 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005701 }
5702 break;
5703
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005704 case ISN_PUT:
5705 {
5706 int regname = iptr->isn_arg.put.put_regname;
5707 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5708 char_u *expr = NULL;
5709 int dir = FORWARD;
5710
Bram Moolenaar08597872020-12-10 19:43:40 +01005711 if (lnum < -2)
5712 {
5713 // line number was put on the stack by ISN_RANGE
5714 tv = STACK_TV_BOT(-1);
5715 curwin->w_cursor.lnum = tv->vval.v_number;
5716 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5717 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005718 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005719 }
5720 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005721 // :put! above cursor
5722 dir = BACKWARD;
5723 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005724 {
5725 curwin->w_cursor.lnum = lnum;
5726 if (lnum == 0)
5727 // check_cursor() below will move to line 1
5728 dir = BACKWARD;
5729 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005730
5731 if (regname == '=')
5732 {
5733 tv = STACK_TV_BOT(-1);
5734 if (tv->v_type == VAR_STRING)
5735 expr = tv->vval.v_string;
5736 else
5737 {
5738 expr = typval2string(tv, TRUE); // allocates value
5739 clear_tv(tv);
5740 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005741 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005742 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005743 check_cursor();
5744 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5745 vim_free(expr);
5746 }
5747 break;
5748
Bram Moolenaar02194d22020-10-24 23:08:38 +02005749 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005750 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5751 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5752 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5753 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005754 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5755 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005756 break;
5757
Bram Moolenaar02194d22020-10-24 23:08:38 +02005758 case ISN_CMDMOD_REV:
5759 // filter regprog is owned by the instruction, don't free it
5760 cmdmod.cmod_filter_regmatch.regprog = NULL;
5761 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005762 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5763 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005764 break;
5765
Bram Moolenaar792f7862020-11-23 08:31:18 +01005766 case ISN_UNPACK:
5767 {
5768 int count = iptr->isn_arg.unpack.unp_count;
5769 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5770 list_T *l;
5771 listitem_T *li;
5772 int i;
5773
5774 // Check there is a valid list to unpack.
5775 tv = STACK_TV_BOT(-1);
5776 if (tv->v_type != VAR_LIST)
5777 {
5778 SOURCING_LNUM = iptr->isn_lnum;
5779 emsg(_(e_for_argument_must_be_sequence_of_lists));
5780 goto on_error;
5781 }
5782 l = tv->vval.v_list;
5783 if (l == NULL
5784 || l->lv_len < (semicolon ? count - 1 : count))
5785 {
5786 SOURCING_LNUM = iptr->isn_lnum;
5787 emsg(_(e_list_value_does_not_have_enough_items));
5788 goto on_error;
5789 }
5790 else if (!semicolon && l->lv_len > count)
5791 {
5792 SOURCING_LNUM = iptr->isn_lnum;
5793 emsg(_(e_list_value_has_more_items_than_targets));
5794 goto on_error;
5795 }
5796
5797 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005798 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005799 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005800 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005801
5802 // Variable after semicolon gets a list with the remaining
5803 // items.
5804 if (semicolon)
5805 {
5806 list_T *rem_list =
5807 list_alloc_with_items(l->lv_len - count + 1);
5808
5809 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005810 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005811 tv = STACK_TV_BOT(-count);
5812 tv->vval.v_list = rem_list;
5813 ++rem_list->lv_refcount;
5814 tv->v_lock = 0;
5815 li = l->lv_first;
5816 for (i = 0; i < count - 1; ++i)
5817 li = li->li_next;
5818 for (i = 0; li != NULL; ++i)
5819 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005820 typval_T tvcopy;
5821
5822 copy_tv(&li->li_tv, &tvcopy);
5823 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005824 li = li->li_next;
5825 }
5826 --count;
5827 }
5828
5829 // Produce the values in reverse order, first item last.
5830 li = l->lv_first;
5831 for (i = 0; i < count; ++i)
5832 {
5833 tv = STACK_TV_BOT(-i - 1);
5834 copy_tv(&li->li_tv, tv);
5835 li = li->li_next;
5836 }
5837
5838 list_unref(l);
5839 }
5840 break;
5841
Bram Moolenaarb2049902021-01-24 12:53:53 +01005842 case ISN_PROF_START:
5843 case ISN_PROF_END:
5844 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005845#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005846 funccall_T cookie;
5847 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005848 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005849 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005850
Bram Moolenaarca16c602022-09-06 18:57:08 +01005851 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005852 if (iptr->isn_type == ISN_PROF_START)
5853 {
5854 func_line_start(&cookie, iptr->isn_lnum);
5855 // if we get here the instruction is executed
5856 func_line_exec(&cookie);
5857 }
5858 else
5859 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005860#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005861 }
5862 break;
5863
Bram Moolenaare99d4222021-06-13 14:01:26 +02005864 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005865 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005866 break;
5867
Bram Moolenaar389df252020-07-09 21:20:47 +02005868 case ISN_SHUFFLE:
5869 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005870 typval_T tmp_tv;
5871 int item = iptr->isn_arg.shuffle.shfl_item;
5872 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005873
5874 tmp_tv = *STACK_TV_BOT(-item);
5875 for ( ; up > 0 && item > 1; --up)
5876 {
5877 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5878 --item;
5879 }
5880 *STACK_TV_BOT(-item) = tmp_tv;
5881 }
5882 break;
5883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005884 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005885 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005887 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005888 break;
5889 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005890 continue;
5891
Bram Moolenaard032f342020-07-18 18:13:02 +02005892func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005893 // Restore previous function. If the frame pointer is where we started
5894 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005895 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005896 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005897
Bram Moolenaar4c137212021-04-19 16:48:48 +02005898 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005899 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005900 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005901 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005902
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005903on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005904 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005905 // If "emsg_silent" is set then ignore the error, unless it was set
5906 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005907 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005908 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005909 {
5910 // If a sequence of instructions causes an error while ":silent!"
5911 // was used, restore the stack length and jump ahead to restoring
5912 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005913 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005914 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005915 while (ectx->ec_stack.ga_len
5916 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005917 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005918 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005919 clear_tv(STACK_TV_BOT(0));
5920 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005921 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5922 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005923 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005924 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005925 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005926on_fatal_error:
5927 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005928 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005929 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005930 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005931 }
5932
5933done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005934 ret = OK;
5935theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005936 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005937
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005938 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005939 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5940 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005941}
5942
5943/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005944 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005945 * "rettv".
5946 * Return OK or FAIL.
5947 */
5948 int
5949exe_typval_instr(typval_T *tv, typval_T *rettv)
5950{
5951 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5952 isn_T *save_instr = ectx->ec_instr;
5953 int save_iidx = ectx->ec_iidx;
5954 int res;
5955
LemonBoyf3b48952022-05-05 13:53:03 +01005956 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5957 // even when the compilation fails.
5958 rettv->v_type = VAR_UNKNOWN;
5959
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005960 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5961 res = exec_instructions(ectx);
5962 if (res == OK)
5963 {
5964 *rettv = *STACK_TV_BOT(-1);
5965 --ectx->ec_stack.ga_len;
5966 }
5967
5968 ectx->ec_instr = save_instr;
5969 ectx->ec_iidx = save_iidx;
5970
5971 return res;
5972}
5973
5974/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005975 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5976 * "substitute_instr".
5977 */
5978 char_u *
5979exe_substitute_instr(void)
5980{
5981 ectx_T *ectx = substitute_instr->subs_ectx;
5982 isn_T *save_instr = ectx->ec_instr;
5983 int save_iidx = ectx->ec_iidx;
5984 char_u *res;
5985
5986 ectx->ec_instr = substitute_instr->subs_instr;
5987 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005988 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005989 typval_T *tv = STACK_TV_BOT(-1);
5990
Bram Moolenaar27523602021-06-05 21:36:19 +02005991 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005992 --ectx->ec_stack.ga_len;
5993 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005994 }
5995 else
5996 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005997 substitute_instr->subs_status = FAIL;
5998 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005999 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000
Bram Moolenaar4c137212021-04-19 16:48:48 +02006001 ectx->ec_instr = save_instr;
6002 ectx->ec_iidx = save_iidx;
6003
6004 return res;
6005}
6006
6007/*
6008 * Call a "def" function from old Vim script.
6009 * Return OK or FAIL.
6010 */
6011 int
6012call_def_function(
6013 ufunc_T *ufunc,
6014 int argc_arg, // nr of arguments
6015 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006016 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02006017 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006018 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01006019 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006020 typval_T *rettv) // return value
6021{
6022 ectx_T ectx; // execution context
6023 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006024 int partial_argc = partial == NULL
6025 || (flags & DEF_USE_PT_ARGV) == 0
6026 ? 0 : partial->pt_argc;
6027 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006028 typval_T *tv;
6029 int idx;
6030 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006031 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006032 sctx_T save_current_sctx = current_sctx;
6033 int did_emsg_before = did_emsg_cumul + did_emsg;
6034 int save_suppress_errthrow = suppress_errthrow;
6035 msglist_T **saved_msg_list = NULL;
6036 msglist_T *private_msg_list = NULL;
6037 int save_emsg_silent_def = emsg_silent_def;
6038 int save_did_emsg_def = did_emsg_def;
6039 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006040 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006041
6042// Get pointer to item in the stack.
6043#undef STACK_TV
6044#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6045
6046// Get pointer to item at the bottom of the stack, -1 is the bottom.
6047#undef STACK_TV_BOT
6048#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6049
6050// Get pointer to a local variable on the stack. Negative for arguments.
6051#undef STACK_TV_VAR
6052#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6053
6054 if (ufunc->uf_def_status == UF_NOT_COMPILED
6055 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006056 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6057 && compile_def_function(ufunc, FALSE,
6058 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006059 {
6060 if (did_emsg_cumul + did_emsg == did_emsg_before)
6061 semsg(_(e_function_is_not_compiled_str),
6062 printable_func_name(ufunc));
6063 return FAIL;
6064 }
6065
6066 {
6067 // Check the function was really compiled.
6068 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6069 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006070 if (dfunc->df_ufunc == NULL)
6071 {
6072 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6073 return FAIL;
6074 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006075 if (INSTRUCTIONS(dfunc) == NULL)
6076 {
6077 iemsg("using call_def_function() on not compiled function");
6078 return FAIL;
6079 }
6080 }
6081
6082 // If depth of calling is getting too high, don't execute the function.
6083 orig_funcdepth = funcdepth_get();
6084 if (funcdepth_increment() == FAIL)
6085 return FAIL;
6086
6087 CLEAR_FIELD(ectx);
6088 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6089 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006090 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006091 {
6092 funcdepth_decrement();
6093 return FAIL;
6094 }
6095 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6096 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6097 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006098 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006099
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006100 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006101 if (idx > 0 && ufunc->uf_va_name == NULL)
6102 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006103 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006104 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006105 goto failed_early;
6106 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006107 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006108 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006109 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006110 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006111 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006112 goto failed_early;
6113 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006114
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006115 // Put values from the partial and arguments on the stack, but no more than
6116 // what the function expects. A lambda can be called with more arguments
6117 // than it uses.
6118 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006119 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6120 ++idx)
6121 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006122 int argv_idx = idx - partial_argc;
6123
6124 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006125 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006126 && tv->v_type == VAR_SPECIAL
6127 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006128 {
6129 // Use the default value.
6130 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6131 }
6132 else
6133 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006134 int done = FALSE;
6135 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6136 {
6137 type_T *expected = ufunc->uf_arg_types[idx];
6138 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6139 {
6140 // When a float is expected and a number was given, convert
6141 // the value.
6142 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6143 STACK_TV_BOT(0)->v_lock = 0;
6144 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6145 done = TRUE;
6146 }
6147 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006148 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006149 goto failed_early;
6150 }
6151 if (!done)
6152 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006153 }
6154 ++ectx.ec_stack.ga_len;
6155 }
6156
6157 // Turn varargs into a list. Empty list if no args.
6158 if (ufunc->uf_va_name != NULL)
6159 {
6160 int vararg_count = argc - ufunc->uf_args.ga_len;
6161
6162 if (vararg_count < 0)
6163 vararg_count = 0;
6164 else
6165 argc -= vararg_count;
6166 if (exe_newlist(vararg_count, &ectx) == FAIL)
6167 goto failed_early;
6168
6169 // Check the type of the list items.
6170 tv = STACK_TV_BOT(-1);
6171 if (ufunc->uf_va_type != NULL
6172 && ufunc->uf_va_type != &t_list_any
6173 && ufunc->uf_va_type->tt_member != &t_any
6174 && tv->vval.v_list != NULL)
6175 {
6176 type_T *expected = ufunc->uf_va_type->tt_member;
6177 listitem_T *li = tv->vval.v_list->lv_first;
6178
6179 for (idx = 0; idx < vararg_count; ++idx)
6180 {
6181 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006182 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006183 goto failed_early;
6184 li = li->li_next;
6185 }
6186 }
6187
6188 if (defcount > 0)
6189 // Move varargs list to below missing default arguments.
6190 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6191 --ectx.ec_stack.ga_len;
6192 }
6193
6194 // Make space for omitted arguments, will store default value below.
6195 // Any varargs list goes after them.
6196 if (defcount > 0)
6197 for (idx = 0; idx < defcount; ++idx)
6198 {
6199 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6200 ++ectx.ec_stack.ga_len;
6201 }
6202 if (ufunc->uf_va_name != NULL)
6203 ++ectx.ec_stack.ga_len;
6204
6205 // Frame pointer points to just after arguments.
6206 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6207 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6208
6209 {
6210 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6211 + ufunc->uf_dfunc_idx;
6212 ufunc_T *base_ufunc = dfunc->df_ufunc;
6213
6214 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006215 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006216 if (partial != NULL || base_ufunc->uf_partial != NULL)
6217 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006218 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6219 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006220 goto failed_early;
6221 if (partial != NULL)
6222 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006223 outer_T *outer = get_pt_outer(partial);
6224
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006225 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006226 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006227 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006228 if (current_ectx != NULL)
6229 {
6230 if (current_ectx->ec_outer_ref != NULL
6231 && current_ectx->ec_outer_ref->or_outer != NULL)
6232 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006233 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006234 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006235 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006236 }
6237 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006238 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006239 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006240 ++partial->pt_refcount;
6241 ectx.ec_outer_ref->or_partial = partial;
6242 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006243 }
6244 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006245 {
6246 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6247 ++base_ufunc->uf_partial->pt_refcount;
6248 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6249 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006250 }
6251 }
6252
6253 // dummy frame entries
6254 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6255 {
6256 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6257 ++ectx.ec_stack.ga_len;
6258 }
6259
6260 {
6261 // Reserve space for local variables and any closure reference count.
6262 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6263 + ufunc->uf_dfunc_idx;
6264
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006265 // Initialize variables to zero. That avoids having to generate
6266 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006267 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006268 {
6269 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6270 STACK_TV_VAR(idx)->vval.v_number = 0;
6271 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006272 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006273
6274 if (object != NULL)
6275 {
6276 // the object is always the variable at index zero
6277 tv = STACK_TV_VAR(0);
6278 tv->v_type = VAR_OBJECT;
6279 tv->vval.v_object = object;
6280 }
6281
Bram Moolenaar4c137212021-04-19 16:48:48 +02006282 if (dfunc->df_has_closure)
6283 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006284 // Initialize the variable that counts how many closures were
6285 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006286 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6287 STACK_TV_VAR(idx)->vval.v_number = 0;
6288 ++ectx.ec_stack.ga_len;
6289 }
6290
6291 ectx.ec_instr = INSTRUCTIONS(dfunc);
6292 }
6293
Bram Moolenaar58779852022-09-06 18:31:14 +01006294 // Store the execution context in funccal, used by invoke_all_defer().
6295 if (funccal != NULL)
6296 funccal->fc_ectx = &ectx;
6297
Bram Moolenaar4c137212021-04-19 16:48:48 +02006298 // Following errors are in the function, not the caller.
6299 // Commands behave like vim9script.
6300 estack_push_ufunc(ufunc, 1);
6301 current_sctx = ufunc->uf_script_ctx;
6302 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6303
6304 // Use a specific location for storing error messages to be converted to an
6305 // exception.
6306 saved_msg_list = msg_list;
6307 msg_list = &private_msg_list;
6308
6309 // Do turn errors into exceptions.
6310 suppress_errthrow = FALSE;
6311
6312 // Do not delete the function while executing it.
6313 ++ufunc->uf_calls;
6314
6315 // When ":silent!" was used before calling then we still abort the
6316 // function. If ":silent!" is used in the function then we don't.
6317 emsg_silent_def = emsg_silent;
6318 did_emsg_def = 0;
6319
LemonBoyc5d27442023-08-19 13:02:35 +02006320 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006321
Bram Moolenaar5800c792022-09-22 17:34:01 +01006322 /*
6323 * Execute the instructions until done.
6324 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006325 ret = exec_instructions(&ectx);
6326 if (ret == OK)
6327 {
6328 // function finished, get result from the stack.
6329 if (ufunc->uf_ret_type == &t_void)
6330 {
6331 rettv->v_type = VAR_VOID;
6332 }
6333 else
6334 {
6335 tv = STACK_TV_BOT(-1);
6336 *rettv = *tv;
6337 tv->v_type = VAR_UNKNOWN;
6338 }
6339 }
6340
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006341 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006342 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006343
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006344 // Deal with any remaining closures, they may be in use somewhere.
6345 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006346 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006347 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006348 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006349 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006350
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006351 estack_pop();
6352 current_sctx = save_current_sctx;
6353
Bram Moolenaar2336c372021-12-06 15:06:54 +00006354 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6355 // Function was unreferenced while being used, free it now.
6356 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006357
Bram Moolenaar352134b2020-10-17 22:04:08 +02006358 if (*msg_list != NULL && saved_msg_list != NULL)
6359 {
6360 msglist_T **plist = saved_msg_list;
6361
6362 // Append entries from the current msg_list (uncaught exceptions) to
6363 // the saved msg_list.
6364 while (*plist != NULL)
6365 plist = &(*plist)->next;
6366
6367 *plist = *msg_list;
6368 }
6369 msg_list = saved_msg_list;
6370
Bram Moolenaar4c137212021-04-19 16:48:48 +02006371 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006372 {
6373 cmdmod.cmod_filter_regmatch.regprog = NULL;
6374 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006375 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006376 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006377 emsg_silent_def = save_emsg_silent_def;
6378 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006379
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006380failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006381 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006383 {
6384 tv = STACK_TV(idx);
6385 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6386 clear_tv(tv);
6387 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006388 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006390 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006391 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006392 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006393 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006394 if (ectx.ec_outer_ref->or_outer_allocated)
6395 vim_free(ectx.ec_outer_ref->or_outer);
6396 partial_unref(ectx.ec_outer_ref->or_partial);
6397 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006398 }
6399
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006400 // Not sure if this is necessary.
6401 suppress_errthrow = save_suppress_errthrow;
6402
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006403 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6404 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006405 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006406 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006407 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 return ret;
6409}
6410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006411/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006412 * Called when a def function has finished (possibly failed).
6413 * Invoke all the function returns to clean up and invoke deferred functions,
6414 * except the toplevel one.
6415 */
6416 void
6417unwind_def_callstack(ectx_T *ectx)
6418{
6419 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6420 func_return(ectx);
6421}
6422
6423/*
dundargocc57b5bc2022-11-02 13:30:51 +00006424 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006425 */
6426 void
6427may_invoke_defer_funcs(ectx_T *ectx)
6428{
6429 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6430
6431 if (dfunc->df_defer_var_idx > 0)
6432 invoke_defer_funcs(ectx);
6433}
6434
6435/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006436 * Return loopvarinfo in a printable form in allocated memory.
6437 */
6438 static char_u *
6439printable_loopvarinfo(loopvarinfo_T *lvi)
6440{
6441 garray_T ga;
6442 int depth;
6443
6444 ga_init2(&ga, 1, 100);
6445 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6446 {
6447 if (ga_grow(&ga, 50) == FAIL)
6448 break;
6449 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006450 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006451 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006452 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006453 lvi->lvi_loop[depth].var_idx,
6454 lvi->lvi_loop[depth].var_idx
6455 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006456 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006457 }
6458 return ga.ga_data;
6459}
6460
6461/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006462 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6463 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6464 * "pfx" is prefixed to every line.
6465 */
6466 static void
6467list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6468{
6469 int line_idx = 0;
6470 int prev_current = 0;
6471 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006472 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006473
6474 for (current = 0; current < instr_count; ++current)
6475 {
6476 isn_T *iptr = &instr[current];
6477 char *line;
6478
6479 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006480 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006481 while (line_idx < iptr->isn_lnum
6482 && line_idx < ufunc->uf_lines.ga_len)
6483 {
6484 if (current > prev_current)
6485 {
6486 msg_puts("\n\n");
6487 prev_current = current;
6488 }
6489 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6490 if (line != NULL)
6491 msg(line);
6492 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006493 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6494 {
6495 int first_def_arg = ufunc->uf_args.ga_len
6496 - ufunc->uf_def_args.ga_len;
6497
6498 if (def_arg_idx > 0)
6499 msg_puts("\n\n");
6500 msg_start();
6501 msg_puts(" ");
6502 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6503 first_def_arg + def_arg_idx]);
6504 msg_puts(" = ");
6505 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6506 msg_clr_eos();
6507 msg_end();
6508 }
6509 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006510
6511 switch (iptr->isn_type)
6512 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006513 case ISN_CONSTRUCT:
6514 smsg("%s%4d NEW %s size %d", pfx, current,
6515 iptr->isn_arg.construct.construct_class->class_name,
6516 (int)iptr->isn_arg.construct.construct_size);
6517 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006518 case ISN_EXEC:
6519 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6520 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006521 case ISN_EXEC_SPLIT:
6522 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6523 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006524 case ISN_EXECRANGE:
6525 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6526 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006527 case ISN_LEGACY_EVAL:
6528 smsg("%s%4d EVAL legacy %s", pfx, current,
6529 iptr->isn_arg.string);
6530 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006531 case ISN_REDIRSTART:
6532 smsg("%s%4d REDIR", pfx, current);
6533 break;
6534 case ISN_REDIREND:
6535 smsg("%s%4d REDIR END%s", pfx, current,
6536 iptr->isn_arg.number ? " append" : "");
6537 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006538 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006539#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006540 smsg("%s%4d CEXPR pre %s", pfx, current,
6541 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006542#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006543 break;
6544 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006545#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006546 {
6547 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6548
6549 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6550 cexpr_get_auname(cer->cer_cmdidx),
6551 cer->cer_forceit ? "!" : "",
6552 cer->cer_cmdline);
6553 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006554#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006555 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006556 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006557 smsg("%s%4d INSTR", pfx, current);
6558 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6559 msg(" -------------");
6560 break;
6561 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006562 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006563 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6564
6565 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006566 }
6567 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006568 case ISN_SUBSTITUTE:
6569 {
6570 subs_T *subs = &iptr->isn_arg.subs;
6571
6572 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6573 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6574 msg(" -------------");
6575 }
6576 break;
6577 case ISN_EXECCONCAT:
6578 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6579 (varnumber_T)iptr->isn_arg.number);
6580 break;
6581 case ISN_ECHO:
6582 {
6583 echo_T *echo = &iptr->isn_arg.echo;
6584
6585 smsg("%s%4d %s %d", pfx, current,
6586 echo->echo_with_white ? "ECHO" : "ECHON",
6587 echo->echo_count);
6588 }
6589 break;
6590 case ISN_EXECUTE:
6591 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006592 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006593 break;
6594 case ISN_ECHOMSG:
6595 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006596 (varnumber_T)(iptr->isn_arg.number));
6597 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006598 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006599 if (iptr->isn_arg.echowin.ewin_time > 0)
6600 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6601 iptr->isn_arg.echowin.ewin_count,
6602 iptr->isn_arg.echowin.ewin_time);
6603 else
6604 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6605 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006606 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006607 case ISN_ECHOCONSOLE:
6608 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6609 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006610 break;
6611 case ISN_ECHOERR:
6612 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006613 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006614 break;
6615 case ISN_LOAD:
6616 {
6617 if (iptr->isn_arg.number < 0)
6618 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6619 (varnumber_T)(iptr->isn_arg.number
6620 + STACK_FRAME_SIZE));
6621 else
6622 smsg("%s%4d LOAD $%lld", pfx, current,
6623 (varnumber_T)(iptr->isn_arg.number));
6624 }
6625 break;
6626 case ISN_LOADOUTER:
6627 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006628 isn_outer_T *outer = &iptr->isn_arg.outer;
6629
6630 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006631 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006632 outer->outer_depth,
6633 outer->outer_idx + STACK_FRAME_SIZE);
6634 else if (outer->outer_depth < 0)
6635 smsg("%s%4d LOADOUTER $%d in loop level %d",
6636 pfx, current,
6637 outer->outer_idx,
6638 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006639 else
6640 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006641 outer->outer_depth,
6642 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006643 }
6644 break;
6645 case ISN_LOADV:
6646 smsg("%s%4d LOADV v:%s", pfx, current,
6647 get_vim_var_name(iptr->isn_arg.number));
6648 break;
6649 case ISN_LOADSCRIPT:
6650 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006651 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6652 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6653 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006654
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006655 sv = get_script_svar(sref, -1);
6656 if (sv == NULL)
6657 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6658 pfx, current, si->sn_name);
6659 else
6660 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006661 sv->sv_name,
6662 sref->sref_idx,
6663 si->sn_name);
6664 }
6665 break;
6666 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006667 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006668 {
6669 scriptitem_T *si = SCRIPT_ITEM(
6670 iptr->isn_arg.loadstore.ls_sid);
6671
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006672 smsg("%s%4d %s s:%s from %s", pfx, current,
6673 iptr->isn_type == ISN_LOADS ? "LOADS"
6674 : "LOADEXPORT",
6675 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006676 }
6677 break;
6678 case ISN_LOADAUTO:
6679 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6680 break;
6681 case ISN_LOADG:
6682 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6683 break;
6684 case ISN_LOADB:
6685 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6686 break;
6687 case ISN_LOADW:
6688 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6689 break;
6690 case ISN_LOADT:
6691 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6692 break;
6693 case ISN_LOADGDICT:
6694 smsg("%s%4d LOAD g:", pfx, current);
6695 break;
6696 case ISN_LOADBDICT:
6697 smsg("%s%4d LOAD b:", pfx, current);
6698 break;
6699 case ISN_LOADWDICT:
6700 smsg("%s%4d LOAD w:", pfx, current);
6701 break;
6702 case ISN_LOADTDICT:
6703 smsg("%s%4d LOAD t:", pfx, current);
6704 break;
6705 case ISN_LOADOPT:
6706 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6707 break;
6708 case ISN_LOADENV:
6709 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6710 break;
6711 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006712 smsg("%s%4d LOADREG @%c", pfx, current,
6713 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006714 break;
6715
6716 case ISN_STORE:
6717 if (iptr->isn_arg.number < 0)
6718 smsg("%s%4d STORE arg[%lld]", pfx, current,
6719 iptr->isn_arg.number + STACK_FRAME_SIZE);
6720 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006721 smsg("%s%4d STORE $%lld", pfx, current,
6722 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006723 break;
6724 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006725 {
6726 isn_outer_T *outer = &iptr->isn_arg.outer;
6727
6728 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6729 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6730 pfx, current, outer->outer_idx);
6731 else
6732 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6733 outer->outer_depth, outer->outer_idx);
6734 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006735 break;
6736 case ISN_STOREV:
6737 smsg("%s%4d STOREV v:%s", pfx, current,
6738 get_vim_var_name(iptr->isn_arg.number));
6739 break;
6740 case ISN_STOREAUTO:
6741 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6742 break;
6743 case ISN_STOREG:
6744 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6745 break;
6746 case ISN_STOREB:
6747 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6748 break;
6749 case ISN_STOREW:
6750 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6751 break;
6752 case ISN_STORET:
6753 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6754 break;
6755 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006756 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006757 {
6758 scriptitem_T *si = SCRIPT_ITEM(
6759 iptr->isn_arg.loadstore.ls_sid);
6760
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006761 smsg("%s%4d %s %s in %s", pfx, current,
6762 iptr->isn_type == ISN_STORES
6763 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006764 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6765 }
6766 break;
6767 case ISN_STORESCRIPT:
6768 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006769 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6770 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6771 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006772
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006773 sv = get_script_svar(sref, -1);
6774 if (sv == NULL)
6775 smsg("%s%4d STORESCRIPT [deleted] in %s",
6776 pfx, current, si->sn_name);
6777 else
6778 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006779 sv->sv_name,
6780 sref->sref_idx,
6781 si->sn_name);
6782 }
6783 break;
6784 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006785 case ISN_STOREFUNCOPT:
6786 smsg("%s%4d %s &%s", pfx, current,
6787 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006788 iptr->isn_arg.storeopt.so_name);
6789 break;
6790 case ISN_STOREENV:
6791 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6792 break;
6793 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006794 smsg("%s%4d STOREREG @%c", pfx, current,
6795 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006796 break;
6797 case ISN_STORENR:
6798 smsg("%s%4d STORE %lld in $%d", pfx, current,
6799 iptr->isn_arg.storenr.stnr_val,
6800 iptr->isn_arg.storenr.stnr_idx);
6801 break;
6802
6803 case ISN_STOREINDEX:
6804 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006805 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006806 break;
6807
6808 case ISN_STORERANGE:
6809 smsg("%s%4d STORERANGE", pfx, current);
6810 break;
6811
Bram Moolenaard505d172022-12-18 21:42:55 +00006812 case ISN_LOAD_CLASSMEMBER:
6813 case ISN_STORE_CLASSMEMBER:
6814 {
6815 class_T *cl = iptr->isn_arg.classmember.cm_class;
6816 int idx = iptr->isn_arg.classmember.cm_idx;
6817 ocmember_T *ocm = &cl->class_class_members[idx];
6818 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6819 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6820 ? "LOAD" : "STORE",
6821 cl->class_name, ocm->ocm_name);
6822 }
6823 break;
6824
Bram Moolenaar4c137212021-04-19 16:48:48 +02006825 // constants
6826 case ISN_PUSHNR:
6827 smsg("%s%4d PUSHNR %lld", pfx, current,
6828 (varnumber_T)(iptr->isn_arg.number));
6829 break;
6830 case ISN_PUSHBOOL:
6831 case ISN_PUSHSPEC:
6832 smsg("%s%4d PUSH %s", pfx, current,
6833 get_var_special_name(iptr->isn_arg.number));
6834 break;
6835 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006836 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006837 break;
6838 case ISN_PUSHS:
6839 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6840 break;
6841 case ISN_PUSHBLOB:
6842 {
6843 char_u *r;
6844 char_u numbuf[NUMBUFLEN];
6845 char_u *tofree;
6846
6847 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6848 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6849 vim_free(tofree);
6850 }
6851 break;
6852 case ISN_PUSHFUNC:
6853 {
6854 char *name = (char *)iptr->isn_arg.string;
6855
6856 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6857 name == NULL ? "[none]" : name);
6858 }
6859 break;
6860 case ISN_PUSHCHANNEL:
6861#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006862 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006863#endif
6864 break;
6865 case ISN_PUSHJOB:
6866#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006867 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006868#endif
6869 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006870 case ISN_PUSHOBJ:
6871 smsg("%s%4d PUSHOBJ null", pfx, current);
6872 break;
6873 case ISN_PUSHCLASS:
6874 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006875 iptr->isn_arg.classarg == NULL ? "null"
6876 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006877 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006878 case ISN_PUSHEXC:
6879 smsg("%s%4d PUSH v:exception", pfx, current);
6880 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006881 case ISN_AUTOLOAD:
6882 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6883 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006884 case ISN_UNLET:
6885 smsg("%s%4d UNLET%s %s", pfx, current,
6886 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6887 iptr->isn_arg.unlet.ul_name);
6888 break;
6889 case ISN_UNLETENV:
6890 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6891 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6892 iptr->isn_arg.unlet.ul_name);
6893 break;
6894 case ISN_UNLETINDEX:
6895 smsg("%s%4d UNLETINDEX", pfx, current);
6896 break;
6897 case ISN_UNLETRANGE:
6898 smsg("%s%4d UNLETRANGE", pfx, current);
6899 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006900 case ISN_LOCKUNLOCK:
6901 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6902 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006903 case ISN_LOCKCONST:
6904 smsg("%s%4d LOCKCONST", pfx, current);
6905 break;
6906 case ISN_NEWLIST:
6907 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006908 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006909 break;
6910 case ISN_NEWDICT:
6911 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006912 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006913 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006914 case ISN_NEWPARTIAL:
6915 smsg("%s%4d NEWPARTIAL", pfx, current);
6916 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006917
6918 // function call
6919 case ISN_BCALL:
6920 {
6921 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6922
6923 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6924 internal_func_name(cbfunc->cbf_idx),
6925 cbfunc->cbf_argcount);
6926 }
6927 break;
6928 case ISN_DCALL:
6929 {
6930 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6931 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6932 + cdfunc->cdf_idx;
6933
6934 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006935 printable_func_name(df->df_ufunc),
6936 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006937 }
6938 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006939 case ISN_METHODCALL:
6940 {
6941 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6942
6943 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6944 mfunc->cmf_itf->class_name,
6945 mfunc->cmf_itf->class_obj_methods[
6946 mfunc->cmf_idx]->uf_name,
6947 mfunc->cmf_argcount);
6948 }
6949 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006950 case ISN_UCALL:
6951 {
6952 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6953
6954 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6955 cufunc->cuf_name, cufunc->cuf_argcount);
6956 }
6957 break;
6958 case ISN_PCALL:
6959 {
6960 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6961
6962 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6963 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6964 }
6965 break;
6966 case ISN_PCALL_END:
6967 smsg("%s%4d PCALL end", pfx, current);
6968 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006969 case ISN_DEFER:
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02006970 smsg("%s%4d DEFER %d args", pfx, current,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006971 (int)iptr->isn_arg.defer.defer_argcount);
6972 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006973 case ISN_RETURN:
6974 smsg("%s%4d RETURN", pfx, current);
6975 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006976 case ISN_RETURN_VOID:
6977 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006978 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006979 case ISN_RETURN_OBJECT:
6980 smsg("%s%4d RETURN object", pfx, current);
6981 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006982 case ISN_FUNCREF:
6983 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006984 funcref_T *funcref = &iptr->isn_arg.funcref;
6985 funcref_extra_T *extra = funcref->fr_extra;
6986 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006987
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006988 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006989 {
6990 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6991 + funcref->fr_dfunc_idx;
6992 name = df->df_ufunc->uf_name;
6993 }
6994 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006995 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006996 if (extra != NULL && extra->fre_class != NULL)
6997 {
6998 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6999 extra->fre_class->class_name, name);
7000 }
7001 else if (extra == NULL
7002 || extra->fre_loopvar_info.lvi_depth == 0)
7003 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007004 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00007005 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007006 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007007 {
7008 char_u *info = printable_loopvarinfo(
7009 &extra->fre_loopvar_info);
7010
7011 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
7012 name, info);
7013 vim_free(info);
7014 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007015 }
7016 break;
7017
7018 case ISN_NEWFUNC:
7019 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01007020 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007021
Bram Moolenaarcc341812022-09-19 15:54:34 +01007022 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007023 smsg("%s%4d NEWFUNC %s %s", pfx, current,
7024 arg->nfa_lambda, arg->nfa_global);
7025 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007026 {
7027 char_u *info = printable_loopvarinfo(
7028 &arg->nfa_loopvar_info);
7029
7030 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
7031 arg->nfa_lambda, arg->nfa_global, info);
7032 vim_free(info);
7033 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007034 }
7035 break;
7036
7037 case ISN_DEF:
7038 {
7039 char_u *name = iptr->isn_arg.string;
7040
7041 smsg("%s%4d DEF %s", pfx, current,
7042 name == NULL ? (char_u *)"" : name);
7043 }
7044 break;
7045
7046 case ISN_JUMP:
7047 {
7048 char *when = "?";
7049
7050 switch (iptr->isn_arg.jump.jump_when)
7051 {
7052 case JUMP_ALWAYS:
7053 when = "JUMP";
7054 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007055 case JUMP_NEVER:
7056 iemsg("JUMP_NEVER should not be used");
7057 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007058 case JUMP_AND_KEEP_IF_TRUE:
7059 when = "JUMP_AND_KEEP_IF_TRUE";
7060 break;
7061 case JUMP_IF_FALSE:
7062 when = "JUMP_IF_FALSE";
7063 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007064 case JUMP_WHILE_FALSE:
7065 when = "JUMP_WHILE_FALSE"; // unused
7066 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007067 case JUMP_IF_COND_FALSE:
7068 when = "JUMP_IF_COND_FALSE";
7069 break;
7070 case JUMP_IF_COND_TRUE:
7071 when = "JUMP_IF_COND_TRUE";
7072 break;
7073 }
7074 smsg("%s%4d %s -> %d", pfx, current, when,
7075 iptr->isn_arg.jump.jump_where);
7076 }
7077 break;
7078
7079 case ISN_JUMP_IF_ARG_SET:
7080 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7081 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7082 iptr->isn_arg.jump.jump_where);
7083 break;
7084
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007085 case ISN_JUMP_IF_ARG_NOT_SET:
7086 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7087 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7088 iptr->isn_arg.jump.jump_where);
7089 break;
7090
Bram Moolenaar4c137212021-04-19 16:48:48 +02007091 case ISN_FOR:
7092 {
7093 forloop_T *forloop = &iptr->isn_arg.forloop;
7094
7095 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007096 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007097 }
7098 break;
7099
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007100 case ISN_ENDLOOP:
7101 {
7102 endloop_T *endloop = &iptr->isn_arg.endloop;
7103
Bram Moolenaarcc341812022-09-19 15:54:34 +01007104 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7105 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007106 endloop->end_funcref_idx,
7107 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007108 endloop->end_var_idx + endloop->end_var_count - 1,
7109 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007110 }
7111 break;
7112
7113 case ISN_WHILE:
7114 {
7115 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7116
7117 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7118 whileloop->while_funcref_idx,
7119 whileloop->while_end);
7120 }
7121 break;
7122
Bram Moolenaar4c137212021-04-19 16:48:48 +02007123 case ISN_TRY:
7124 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007125 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007126
7127 if (try->try_ref->try_finally == 0)
7128 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7129 pfx, current,
7130 try->try_ref->try_catch,
7131 try->try_ref->try_endtry);
7132 else
7133 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7134 pfx, current,
7135 try->try_ref->try_catch,
7136 try->try_ref->try_finally,
7137 try->try_ref->try_endtry);
7138 }
7139 break;
7140 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007141 smsg("%s%4d CATCH", pfx, current);
7142 break;
7143 case ISN_TRYCONT:
7144 {
7145 trycont_T *trycont = &iptr->isn_arg.trycont;
7146
7147 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7148 trycont->tct_levels,
7149 trycont->tct_levels == 1 ? "" : "s",
7150 trycont->tct_where);
7151 }
7152 break;
7153 case ISN_FINALLY:
7154 smsg("%s%4d FINALLY", pfx, current);
7155 break;
7156 case ISN_ENDTRY:
7157 smsg("%s%4d ENDTRY", pfx, current);
7158 break;
7159 case ISN_THROW:
7160 smsg("%s%4d THROW", pfx, current);
7161 break;
7162
7163 // expression operations on number
7164 case ISN_OPNR:
7165 case ISN_OPFLOAT:
7166 case ISN_OPANY:
7167 {
7168 char *what;
7169 char *ins;
7170
7171 switch (iptr->isn_arg.op.op_type)
7172 {
7173 case EXPR_MULT: what = "*"; break;
7174 case EXPR_DIV: what = "/"; break;
7175 case EXPR_REM: what = "%"; break;
7176 case EXPR_SUB: what = "-"; break;
7177 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007178 case EXPR_LSHIFT: what = "<<"; break;
7179 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007180 default: what = "???"; break;
7181 }
7182 switch (iptr->isn_type)
7183 {
7184 case ISN_OPNR: ins = "OPNR"; break;
7185 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7186 case ISN_OPANY: ins = "OPANY"; break;
7187 default: ins = "???"; break;
7188 }
7189 smsg("%s%4d %s %s", pfx, current, ins, what);
7190 }
7191 break;
7192
7193 case ISN_COMPAREBOOL:
7194 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007195 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007196 case ISN_COMPARENR:
7197 case ISN_COMPAREFLOAT:
7198 case ISN_COMPARESTRING:
7199 case ISN_COMPAREBLOB:
7200 case ISN_COMPARELIST:
7201 case ISN_COMPAREDICT:
7202 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007203 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007204 case ISN_COMPAREANY:
7205 {
7206 char *p;
7207 char buf[10];
7208 char *type;
7209
7210 switch (iptr->isn_arg.op.op_type)
7211 {
7212 case EXPR_EQUAL: p = "=="; break;
7213 case EXPR_NEQUAL: p = "!="; break;
7214 case EXPR_GREATER: p = ">"; break;
7215 case EXPR_GEQUAL: p = ">="; break;
7216 case EXPR_SMALLER: p = "<"; break;
7217 case EXPR_SEQUAL: p = "<="; break;
7218 case EXPR_MATCH: p = "=~"; break;
7219 case EXPR_IS: p = "is"; break;
7220 case EXPR_ISNOT: p = "isnot"; break;
7221 case EXPR_NOMATCH: p = "!~"; break;
7222 default: p = "???"; break;
7223 }
7224 STRCPY(buf, p);
7225 if (iptr->isn_arg.op.op_ic == TRUE)
7226 strcat(buf, "?");
7227 switch(iptr->isn_type)
7228 {
7229 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7230 case ISN_COMPARESPECIAL:
7231 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007232 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007233 case ISN_COMPARENR: type = "COMPARENR"; break;
7234 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7235 case ISN_COMPARESTRING:
7236 type = "COMPARESTRING"; break;
7237 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7238 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7239 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7240 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007241 case ISN_COMPAREOBJECT:
7242 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007243 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7244 default: type = "???"; break;
7245 }
7246
7247 smsg("%s%4d %s %s", pfx, current, type, buf);
7248 }
7249 break;
7250
7251 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7252 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7253
7254 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007255 case ISN_CONCAT:
7256 smsg("%s%4d CONCAT size %lld", pfx, current,
7257 (varnumber_T)(iptr->isn_arg.number));
7258 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007259 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7260 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7261 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7262 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7263 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7264 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7265 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7266 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7267 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7268 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7269 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007270 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007271 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7272 iptr->isn_arg.getitem.gi_index,
7273 iptr->isn_arg.getitem.gi_with_op ?
7274 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007275 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7276 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7277 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007278 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7279 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007280 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007281 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007282 pfx, current,
7283 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007284 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007285 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007286 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007287 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007288 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7289 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7290
Bram Moolenaar4c137212021-04-19 16:48:48 +02007291 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7292
Bram Moolenaar4c137212021-04-19 16:48:48 +02007293 case ISN_CHECKTYPE:
7294 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007295 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007296 char *tofree = NULL;
7297 char *typename;
7298
7299 if (ct->ct_type->tt_type == VAR_FLOAT
7300 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7301 typename = "float|number";
7302 else
7303 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007304
7305 if (ct->ct_arg_idx == 0)
7306 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007307 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007308 (int)ct->ct_off);
7309 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007310 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007311 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007312 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007313 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007314 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007315 (int)ct->ct_arg_idx);
7316 vim_free(tofree);
7317 break;
7318 }
7319 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7320 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7321 iptr->isn_arg.checklen.cl_min_len);
7322 break;
7323 case ISN_SETTYPE:
7324 {
7325 char *tofree;
7326
7327 smsg("%s%4d SETTYPE %s", pfx, current,
7328 type_name(iptr->isn_arg.type.ct_type, &tofree));
7329 vim_free(tofree);
7330 break;
7331 }
7332 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007333 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7334 smsg("%s%4d INVERT %d (!val)", pfx, current,
7335 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007336 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007337 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7338 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007339 break;
7340 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007341 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007342 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007343 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7344 pfx, current,
7345 (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_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7348 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007349 break;
7350 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007351 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007352 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007353 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007354 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7355 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007356 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007357 else
7358 smsg("%s%4d PUT %c %ld", pfx, current,
7359 iptr->isn_arg.put.put_regname,
7360 (long)iptr->isn_arg.put.put_lnum);
7361 break;
7362
Bram Moolenaar4c137212021-04-19 16:48:48 +02007363 case ISN_CMDMOD:
7364 {
7365 char_u *buf;
7366 size_t len = produce_cmdmods(
7367 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7368
7369 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007370 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007371 {
7372 (void)produce_cmdmods(
7373 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7374 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7375 vim_free(buf);
7376 }
7377 break;
7378 }
7379 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7380
7381 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007382 smsg("%s%4d PROFILE START line %d", pfx, current,
7383 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007384 break;
7385
7386 case ISN_PROF_END:
7387 smsg("%s%4d PROFILE END", pfx, current);
7388 break;
7389
Bram Moolenaare99d4222021-06-13 14:01:26 +02007390 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007391 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7392 iptr->isn_arg.debug.dbg_break_lnum + 1,
7393 iptr->isn_lnum,
7394 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007395 break;
7396
Bram Moolenaar4c137212021-04-19 16:48:48 +02007397 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7398 iptr->isn_arg.unpack.unp_count,
7399 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7400 break;
7401 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7402 iptr->isn_arg.shuffle.shfl_item,
7403 iptr->isn_arg.shuffle.shfl_up);
7404 break;
7405 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7406
7407 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7408 return;
7409 }
7410
7411 out_flush(); // output one line at a time
7412 ui_breakcheck();
7413 if (got_int)
7414 break;
7415 }
7416}
7417
7418/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007419 * Handle command line completion for the :disassemble command.
7420 */
7421 void
7422set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7423{
7424 char_u *p;
7425
7426 // Default: expand user functions, "debug" and "profile"
7427 xp->xp_context = EXPAND_DISASSEMBLE;
7428 xp->xp_pattern = arg;
7429
7430 // first argument already typed: only user function names
7431 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7432 {
7433 xp->xp_context = EXPAND_USER_FUNC;
7434 xp->xp_pattern = skipwhite(p);
7435 }
7436}
7437
7438/*
7439 * Function given to ExpandGeneric() to obtain the list of :disassemble
7440 * arguments.
7441 */
7442 char_u *
7443get_disassemble_argument(expand_T *xp, int idx)
7444{
7445 if (idx == 0)
7446 return (char_u *)"debug";
7447 if (idx == 1)
7448 return (char_u *)"profile";
7449 return get_user_func_name(xp, idx - 2);
7450}
7451
7452/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007453 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007454 * We don't really need this at runtime, but we do have tests that require it,
7455 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456 */
7457 void
7458ex_disassemble(exarg_T *eap)
7459{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007460 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007461 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007463 isn_T *instr = NULL; // init to shut up gcc warning
7464 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007465 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007466
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007467 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007468 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007470 if (func_needs_compiling(ufunc, compile_type)
7471 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007472 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007473 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007475 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476 return;
7477 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007478 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479
7480 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007481 switch (compile_type)
7482 {
7483 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007484#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007485 instr = dfunc->df_instr_prof;
7486 instr_count = dfunc->df_instr_prof_count;
7487 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007488#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007489 // FALLTHROUGH
7490 case CT_NONE:
7491 instr = dfunc->df_instr;
7492 instr_count = dfunc->df_instr_count;
7493 break;
7494 case CT_DEBUG:
7495 instr = dfunc->df_instr_debug;
7496 instr_count = dfunc->df_instr_debug_count;
7497 break;
7498 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007499
Bram Moolenaar4c137212021-04-19 16:48:48 +02007500 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007501}
7502
7503/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007504 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007505 * list, etc. Mostly like what JavaScript does, except that empty list and
7506 * empty dictionary are FALSE.
7507 */
7508 int
7509tv2bool(typval_T *tv)
7510{
7511 switch (tv->v_type)
7512 {
7513 case VAR_NUMBER:
7514 return tv->vval.v_number != 0;
7515 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007516 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517 case VAR_PARTIAL:
7518 return tv->vval.v_partial != NULL;
7519 case VAR_FUNC:
7520 case VAR_STRING:
7521 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7522 case VAR_LIST:
7523 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7524 case VAR_DICT:
7525 return tv->vval.v_dict != NULL
7526 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7527 case VAR_BOOL:
7528 case VAR_SPECIAL:
7529 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7530 case VAR_JOB:
7531#ifdef FEAT_JOB_CHANNEL
7532 return tv->vval.v_job != NULL;
7533#else
7534 break;
7535#endif
7536 case VAR_CHANNEL:
7537#ifdef FEAT_JOB_CHANNEL
7538 return tv->vval.v_channel != NULL;
7539#else
7540 break;
7541#endif
7542 case VAR_BLOB:
7543 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7544 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007545 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007546 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007547 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007548 case VAR_CLASS:
7549 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007550 case VAR_TYPEALIAS:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551 break;
7552 }
7553 return FALSE;
7554}
7555
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007556 void
7557emsg_using_string_as(typval_T *tv, int as_number)
7558{
7559 semsg(_(as_number ? e_using_string_as_number_str
7560 : e_using_string_as_bool_str),
7561 tv->vval.v_string == NULL
7562 ? (char_u *)"" : tv->vval.v_string);
7563}
7564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565/*
7566 * If "tv" is a string give an error and return FAIL.
7567 */
7568 int
7569check_not_string(typval_T *tv)
7570{
7571 if (tv->v_type == VAR_STRING)
7572 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007573 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574 clear_tv(tv);
7575 return FAIL;
7576 }
7577 return OK;
7578}
7579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580
7581#endif // FEAT_EVAL