blob: 55f9d4327520e4daca68059e045d40c2c7bc7851 [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
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100113// Return value for functions used to execute instructions
114#define EXEC_FAIL 0
115#define EXEC_OK 1
116#define EXEC_DONE 2
117
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200118 void
119to_string_error(vartype_T vartype)
120{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200121 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200122}
123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100125 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126 */
127 static int
128ufunc_argcount(ufunc_T *ufunc)
129{
130 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
131}
132
133/*
LemonBoy372bcce2022-04-25 12:43:20 +0100134 * Create a new string from "count" items at the bottom of the stack.
135 * A trailing NUL is appended.
136 * When "count" is zero an empty string is added to the stack.
137 */
138 static int
139exe_concat(int count, ectx_T *ectx)
140{
141 int idx;
142 int len = 0;
143 typval_T *tv;
144 garray_T ga;
145
146 ga_init2(&ga, sizeof(char), 1);
147 // Preallocate enough space for the whole string to avoid having to grow
148 // and copy.
149 for (idx = 0; idx < count; ++idx)
150 {
151 tv = STACK_TV_BOT(idx - count);
152 if (tv->vval.v_string != NULL)
153 len += (int)STRLEN(tv->vval.v_string);
154 }
155
156 if (ga_grow(&ga, len + 1) == FAIL)
157 return FAIL;
158
159 for (idx = 0; idx < count; ++idx)
160 {
161 tv = STACK_TV_BOT(idx - count);
162 ga_concat(&ga, tv->vval.v_string);
163 clear_tv(tv);
164 }
165
166 // add a terminating NUL
167 ga_append(&ga, NUL);
168
169 ectx->ec_stack.ga_len -= count - 1;
170 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
171
172 return OK;
173}
174
175/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200176 * Create a new list from "count" items at the bottom of the stack.
177 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 */
180 static int
181exe_newlist(int count, ectx_T *ectx)
182{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100183 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200184 int idx;
185 typval_T *tv;
186
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100187 if (count >= 0)
188 {
189 list = list_alloc_with_items(count);
190 if (list == NULL)
191 return FAIL;
192 for (idx = 0; idx < count; ++idx)
193 list_set_item(list, idx, STACK_TV_BOT(idx - count));
194 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200195
196 if (count > 0)
197 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200198 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100199 {
200 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200201 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100202 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200203 else
204 ++ectx->ec_stack.ga_len;
205 tv = STACK_TV_BOT(-1);
206 tv->v_type = VAR_LIST;
207 tv->vval.v_list = list;
Bram Moolenaar566badc2022-09-18 13:46:08 +0100208 tv->v_lock = 0;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100209 if (list != NULL)
210 ++list->lv_refcount;
211 return OK;
212}
213
214/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100215 * Create a new tuple from "count" items at the bottom of the stack.
216 * When "count" is zero an empty tuple is added to the stack.
217 * When "count" is -1 a NULL tuple is added to the stack.
218 */
219 static int
220exe_newtuple(int count, ectx_T *ectx)
221{
222 tuple_T *tuple = NULL;
223 int idx;
224 typval_T *tv;
225
226 if (count >= 0)
227 {
228 tuple = tuple_alloc_with_items(count);
229 if (tuple == NULL)
230 return FAIL;
231 for (idx = 0; idx < count; ++idx)
232 tuple_set_item(tuple, idx, STACK_TV_BOT(idx - count));
233 }
234
235 if (count > 0)
236 ectx->ec_stack.ga_len -= count - 1;
237 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
238 {
239 tuple_unref(tuple);
240 return FAIL;
241 }
242 else
243 ++ectx->ec_stack.ga_len;
244 tv = STACK_TV_BOT(-1);
245 tv->v_type = VAR_TUPLE;
246 tv->vval.v_tuple = tuple;
247 tv->v_lock = 0;
248 if (tuple != NULL)
249 ++tuple->tv_refcount;
250
251 return OK;
252}
253
254/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100255 * Implementation of ISN_NEWDICT.
256 * Returns FAIL on total failure, MAYBE on error.
257 */
258 static int
259exe_newdict(int count, ectx_T *ectx)
260{
261 dict_T *dict = NULL;
262 dictitem_T *item;
263 char_u *key;
264 int idx;
265 typval_T *tv;
266
267 if (count >= 0)
268 {
269 dict = dict_alloc();
270 if (unlikely(dict == NULL))
271 return FAIL;
272 for (idx = 0; idx < count; ++idx)
273 {
274 // have already checked key type is VAR_STRING
275 tv = STACK_TV_BOT(2 * (idx - count));
276 // check key is unique
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000277 key = tv->vval.v_string == NULL ? (char_u *)"" : tv->vval.v_string;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100278 item = dict_find(dict, key, -1);
279 if (item != NULL)
280 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000281 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100282 dict_unref(dict);
283 return MAYBE;
284 }
285 item = dictitem_alloc(key);
286 clear_tv(tv);
287 if (unlikely(item == NULL))
288 {
289 dict_unref(dict);
290 return FAIL;
291 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100292 tv = STACK_TV_BOT(2 * (idx - count) + 1);
293 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100294 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100295 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100296 if (dict_add(dict, item) == FAIL)
297 {
298 // can this ever happen?
299 dict_unref(dict);
300 return FAIL;
301 }
302 }
303 }
304
305 if (count > 0)
306 ectx->ec_stack.ga_len -= 2 * count - 1;
307 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Christian Brabandt86eddce2024-03-26 18:42:52 +0100308 {
309 dict_unref(dict);
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100310 return FAIL;
Christian Brabandt86eddce2024-03-26 18:42:52 +0100311 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100312 else
313 ++ectx->ec_stack.ga_len;
314 tv = STACK_TV_BOT(-1);
315 tv->v_type = VAR_DICT;
316 tv->v_lock = 0;
317 tv->vval.v_dict = dict;
318 if (dict != NULL)
319 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200320 return OK;
321}
322
323/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200324 * If debug_tick changed check if "ufunc" has a breakpoint and update
325 * "uf_has_breakpoint".
326 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000327 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200328update_has_breakpoint(ufunc_T *ufunc)
329{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000330 if (ufunc->uf_debug_tick == debug_tick)
331 return;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200332
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000333 linenr_T breakpoint;
334
335 ufunc->uf_debug_tick = debug_tick;
336 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
337 ufunc->uf_has_breakpoint = breakpoint > 0;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200338}
339
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200340static garray_T dict_stack = GA_EMPTY;
341
342/*
343 * Put a value on the dict stack. This consumes "tv".
344 */
345 static int
346dict_stack_save(typval_T *tv)
347{
348 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000349 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200350 if (ga_grow(&dict_stack, 1) == FAIL)
351 return FAIL;
352 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
353 ++dict_stack.ga_len;
354 return OK;
355}
356
357/*
358 * Get the typval at top of the dict stack.
359 */
360 static typval_T *
361dict_stack_get_tv(void)
362{
363 if (dict_stack.ga_len == 0)
364 return NULL;
365 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
366}
367
368/*
369 * Get the dict at top of the dict stack.
370 */
371 static dict_T *
372dict_stack_get_dict(void)
373{
374 typval_T *tv;
375
376 if (dict_stack.ga_len == 0)
377 return NULL;
378 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
379 if (tv->v_type == VAR_DICT)
380 return tv->vval.v_dict;
381 return NULL;
382}
383
384/*
385 * Drop an item from the dict stack.
386 */
387 static void
388dict_stack_drop(void)
389{
390 if (dict_stack.ga_len == 0)
391 {
392 iemsg("Dict stack underflow");
393 return;
394 }
395 --dict_stack.ga_len;
396 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
397}
398
399/*
400 * Drop items from the dict stack until the length is equal to "len".
401 */
402 static void
403dict_stack_clear(int len)
404{
405 while (dict_stack.ga_len > len)
406 dict_stack_drop();
407}
408
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200409/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000410 * Get a pointer to useful "pt_outer" of "pt".
411 */
412 static outer_T *
413get_pt_outer(partial_T *pt)
414{
415 partial_T *ptref = pt->pt_outer_partial;
416
417 if (ptref == NULL)
418 return &pt->pt_outer;
419
420 // partial using partial (recursively)
421 while (ptref->pt_outer_partial != NULL)
422 ptref = ptref->pt_outer_partial;
423 return &ptref->pt_outer;
424}
425
426/*
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000427 * Check "argcount" arguments on the stack against what "ufunc" expects.
428 * "off" is the offset of arguments on the stack.
429 * Return OK or FAIL.
430 */
431 static int
432check_ufunc_arg_types(ufunc_T *ufunc, int argcount, int off, ectx_T *ectx)
433{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000434 if (ufunc->uf_arg_types == NULL && ufunc->uf_va_type == NULL)
435 return OK;
436
437 typval_T *argv = STACK_TV_BOT(0) - argcount - off;
438
439 // The function can change at runtime, check that the argument
440 // types are correct.
441 for (int i = 0; i < argcount; ++i)
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000442 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000443 type_T *type = NULL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000444
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000445 // assume a v:none argument, using the default value, is always OK
446 if (argv[i].v_type == VAR_SPECIAL
447 && argv[i].vval.v_number == VVAL_NONE)
448 continue;
Ernie Raelb077b582023-12-14 20:11:44 +0100449 // only pass values to user functions, never types
450 if (check_typval_is_value(&argv[i]) == FAIL)
451 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000452
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000453 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
454 type = ufunc->uf_arg_types[i];
455 else if (ufunc->uf_va_type != NULL)
456 type = ufunc->uf_va_type->tt_member;
457 if (type != NULL && check_typval_arg_type(type,
458 &argv[i], NULL, i + 1) == FAIL)
459 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000460 }
461 return OK;
462}
463
464/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100466 * This adds a stack frame and sets the instruction pointer to the start of the
467 * called function.
Bram Moolenaar5800c792022-09-22 17:34:01 +0100468 * If "pt_arg" is not NULL use "pt_arg->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100469 *
470 * Stack has:
471 * - current arguments (already there)
472 * - omitted optional argument (default values) added here
473 * - stack frame:
474 * - pointer to calling function
475 * - Index of next instruction in calling function
476 * - previous frame pointer
477 * - reserved space for local variables
478 */
479 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100480call_dfunc(
481 int cdf_idx,
Bram Moolenaar5800c792022-09-22 17:34:01 +0100482 partial_T *pt_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100483 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100484 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100485{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100486 int argcount = argcount_arg;
487 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
488 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200489 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100490 int arg_to_add;
491 int vararg_count = 0;
492 int varcount;
493 int idx;
494 estack_T *entry;
495 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200496 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000497 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100498
499 if (dfunc->df_deleted)
500 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100501 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000502 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100503 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 return FAIL;
505 }
506
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100507#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100508 if (do_profiling == PROF_YES)
509 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200510 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100511 {
512 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
513 + profile_info_ga.ga_len;
514 ++profile_info_ga.ga_len;
515 CLEAR_POINTER(info);
516 profile_may_start_func(info, ufunc,
517 (((dfunc_T *)def_functions.ga_data)
518 + ectx->ec_dfunc_idx)->df_ufunc);
519 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100520 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100521#endif
522
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200523 // When debugging and using "cont" switches to the not-debugged
524 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000525 compile_type = get_compile_type(ufunc);
526 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200527 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000528 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200529
530 // compile_def_function() may cause def_functions.ga_data to change
531 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
532 }
533 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200534 {
535 if (did_emsg_cumul + did_emsg == did_emsg_before)
536 semsg(_(e_function_is_not_compiled_str),
537 printable_func_name(ufunc));
538 return FAIL;
539 }
540
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200541 if (ufunc->uf_va_name != NULL)
542 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200543 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200544 // Stack at time of call with 2 varargs:
545 // normal_arg
546 // optional_arg
547 // vararg_1
548 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200549 // After creating the list:
550 // normal_arg
551 // optional_arg
552 // vararg-list
553 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200554 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200555 // After creating the list
556 // normal_arg
557 // (space for optional_arg)
558 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200559 vararg_count = argcount - ufunc->uf_args.ga_len;
560 if (vararg_count < 0)
561 vararg_count = 0;
562 else
563 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200564 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200565 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200566
567 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200568 }
569
Bram Moolenaarfe270812020-04-11 22:31:27 +0200570 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200571 if (arg_to_add < 0)
572 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100573 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
574 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200575 return FAIL;
576 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000577 else if (arg_to_add > ufunc->uf_def_args.ga_len)
578 {
579 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
580
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100581 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
582 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000583 return FAIL;
584 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200585
Bram Moolenaar574950d2023-01-03 19:08:50 +0000586 // If this is an object method, the object is just before the arguments.
587 typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
588
Yegappan Lakshmananc1946262023-09-25 21:00:46 +0200589 if (IS_OBJECT_METHOD(ufunc) && !IS_CONSTRUCTOR_METHOD(ufunc)
590 && obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL)
591 {
592 // If this is not a constructor method, then a valid object is
593 // needed.
594 emsg(_(e_using_null_object));
595 return FAIL;
596 }
597
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000598 // Check the argument types.
599 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
600 return FAIL;
601
mityua5550692023-11-25 15:41:20 +0100602 // While check_ufunc_arg_types call, def function compilation process may
603 // run. If so many def functions are compiled, def_functions array may be
604 // reallocated and dfunc may no longer have valid pointer. Get the object
605 // pointer from def_functions again here.
606 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
607
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200608 // Reserve space for:
609 // - missing arguments
610 // - stack frame
611 // - local variables
612 // - if needed: a counter for number of closures created in
613 // ectx->ec_funcrefs.
614 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100615 if (GA_GROW_FAILS(&ectx->ec_stack,
616 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 return FAIL;
618
Yegappan Lakshmanan1087b8c2023-10-07 22:03:18 +0200619 // The object pointer is in the execution typval stack. The GA_GROW call
620 // above may have reallocated the execution typval stack. So the object
621 // pointer may not be valid anymore. Get the object pointer again from the
622 // execution stack.
623 obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
624
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100625 // If depth of calling is getting too high, don't execute the function.
626 if (funcdepth_increment() == FAIL)
627 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200628 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100629
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100630 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200631 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100632 {
633 floc = ALLOC_ONE(funclocal_T);
634 if (floc == NULL)
635 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200636 *floc = ectx->ec_funclocal;
637 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100638 }
639
Bram Moolenaarfe270812020-04-11 22:31:27 +0200640 // Move the vararg-list to below the missing optional arguments.
641 if (vararg_count > 0 && arg_to_add > 0)
642 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100643
644 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200645 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200646 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200647 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100648
649 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100650 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
651 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200652 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200653 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
654 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100655 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100656 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200657 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658
Bram Moolenaar5800c792022-09-22 17:34:01 +0100659 // Initialize all local variables to number zero. Also initialize the
660 // variable that counts how many closures were created. This is used in
661 // handle_closure_in_use().
662 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
663 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000664 {
665 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
666
667 tv->v_type = VAR_NUMBER;
668 tv->vval.v_number = 0;
669 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200670 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671
Bram Moolenaar574950d2023-01-03 19:08:50 +0000672 // For an object method move the object from just before the arguments to
673 // the first local variable.
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +0200674 if (IS_OBJECT_METHOD(ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +0000675 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200676 if (obj->v_type != VAR_OBJECT)
677 {
678 semsg(_(e_internal_error_str), "type in stack is not an object");
679 return FAIL;
680 }
681
Bram Moolenaar574950d2023-01-03 19:08:50 +0000682 *STACK_TV_VAR(0) = *obj;
683 obj->v_type = VAR_UNKNOWN;
684 }
685
Bram Moolenaar5800c792022-09-22 17:34:01 +0100686 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
687 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100688 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200689 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100690
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200691 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100692 return FAIL;
693 if (pt != NULL)
694 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000695 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200696 ++pt->pt_refcount;
697 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100698 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100699 else
700 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200701 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200702 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200703 {
704 vim_free(ref);
705 return FAIL;
706 }
707 ref->or_outer_allocated = TRUE;
708 ref->or_outer->out_stack = &ectx->ec_stack;
709 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
710 if (ectx->ec_outer_ref != NULL)
711 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100712 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200713 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100714 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100715 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200716 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100717
Bram Moolenaarc970e422021-03-17 15:03:04 +0100718 ++ufunc->uf_calls;
719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 // Set execution state to the start of the called function.
721 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100722 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100723 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200724 if (entry != NULL)
725 {
726 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200727 // Save the current context so it can be restored on return.
728 entry->es_save_sctx = current_sctx;
729 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200730 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100731
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200732 // Start execution at the first instruction.
733 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734
735 return OK;
736}
737
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000738// Double linked list of funcstack_T in use.
739static funcstack_T *first_funcstack = NULL;
740
741 static void
742add_funcstack_to_list(funcstack_T *funcstack)
743{
744 // Link in list of funcstacks.
745 if (first_funcstack != NULL)
746 first_funcstack->fs_prev = funcstack;
747 funcstack->fs_next = first_funcstack;
748 funcstack->fs_prev = NULL;
749 first_funcstack = funcstack;
750}
751
752 static void
753remove_funcstack_from_list(funcstack_T *funcstack)
754{
755 if (funcstack->fs_prev == NULL)
756 first_funcstack = funcstack->fs_next;
757 else
758 funcstack->fs_prev->fs_next = funcstack->fs_next;
759 if (funcstack->fs_next != NULL)
760 funcstack->fs_next->fs_prev = funcstack->fs_prev;
761}
762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200764 * Used when returning from a function: Check if any closure is still
765 * referenced. If so then move the arguments and variables to a separate piece
766 * of stack to be used when the closure is called.
767 * When "free_arguments" is TRUE the arguments are to be freed.
768 * Returns FAIL when out of memory.
769 */
770 static int
771handle_closure_in_use(ectx_T *ectx, int free_arguments)
772{
773 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
774 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200775 int argcount;
776 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200777 int idx;
778 typval_T *tv;
779 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200780 garray_T *gap = &ectx->ec_funcrefs;
781 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200782
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200783 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200784 return OK; // function was freed
785 if (dfunc->df_has_closure == 0)
786 return OK; // no closures
787 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
788 closure_count = tv->vval.v_number;
789 if (closure_count == 0)
790 return OK; // no funcrefs created
791
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100792 // Compute "top": the first entry in the stack used by the function.
793 // This is the first argument (after that comes the stack frame and then
794 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200795 argcount = ufunc_argcount(dfunc->df_ufunc);
796 top = ectx->ec_frame_idx - argcount;
797
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200798 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200799 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200800 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200801 partial_T *pt;
802 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200803
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200804 if (off < 0)
805 continue; // count is off or already done
806 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200807 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200808 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200809 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200810 int i;
811
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000812 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200813 // unreferenced on return.
814 for (i = 0; i < dfunc->df_varcount; ++i)
815 {
816 typval_T *stv = STACK_TV(ectx->ec_frame_idx
817 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200818 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200819 --refcount;
820 }
821 if (refcount > 1)
822 {
823 closure_in_use = TRUE;
824 break;
825 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200826 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200827 }
828
829 if (closure_in_use)
830 {
831 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
832 typval_T *stack;
833
834 // A closure is using the arguments and/or local variables.
835 // Move them to the called function.
836 if (funcstack == NULL)
837 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000838
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200839 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100840 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
841 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200842 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
843 funcstack->fs_ga.ga_data = stack;
844 if (stack == NULL)
845 {
846 vim_free(funcstack);
847 return FAIL;
848 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000849 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200850
851 // Move or copy the arguments.
852 for (idx = 0; idx < argcount; ++idx)
853 {
854 tv = STACK_TV(top + idx);
855 if (free_arguments)
856 {
857 *(stack + idx) = *tv;
858 tv->v_type = VAR_UNKNOWN;
859 }
860 else
861 copy_tv(tv, stack + idx);
862 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100863 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200864 // Move the local variables.
865 for (idx = 0; idx < dfunc->df_varcount; ++idx)
866 {
867 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200868
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200869 // A partial created for a local function, that is also used as a
870 // local variable, has a reference count for the variable, thus
871 // will never go down to zero. When all these refcounts are one
872 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000873 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200874 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
875 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200876 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200877
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200878 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200879 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
880 gap->ga_len - closure_count + i])
881 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200882 }
883
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200884 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200885 tv->v_type = VAR_UNKNOWN;
886 }
887
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200888 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200889 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200890 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
891 - closure_count + idx];
892 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200893 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200894 ++funcstack->fs_refcount;
895 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100896 pt->pt_outer.out_stack = &funcstack->fs_ga;
897 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200898 }
899 }
900 }
901
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200902 for (idx = 0; idx < closure_count; ++idx)
903 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
904 - closure_count + idx]);
905 gap->ga_len -= closure_count;
906 if (gap->ga_len == 0)
907 ga_clear(gap);
908
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200909 return OK;
910}
911
912/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200913 * Called when a partial is freed or its reference count goes down to one. The
914 * funcstack may be the only reference to the partials in the local variables.
915 * Go over all of them, the funcref and can be freed if all partials
916 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100917 * Returns TRUE if the funcstack is freed, the partial referencing it will then
918 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200919 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100920 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200921funcstack_check_refcount(funcstack_T *funcstack)
922{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100923 int i;
924 garray_T *gap = &funcstack->fs_ga;
925 int done = 0;
926 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200927
928 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100929 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200930 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
931 {
932 typval_T *tv = ((typval_T *)gap->ga_data) + i;
933
934 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
935 && tv->vval.v_partial->pt_funcstack == funcstack
936 && tv->vval.v_partial->pt_refcount == 1)
937 ++done;
938 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100939 if (done != funcstack->fs_min_refcount)
940 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200941
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100942 stack = gap->ga_data;
943
944 // All partials referencing the funcstack have a reference count of
945 // one, thus the funcstack is no longer of use.
946 for (i = 0; i < gap->ga_len; ++i)
947 clear_tv(stack + i);
948 vim_free(stack);
949 remove_funcstack_from_list(funcstack);
950 vim_free(funcstack);
951
952 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200953}
954
955/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000956 * For garbage collecting: set references in all variables referenced by
957 * all funcstacks.
958 */
959 int
960set_ref_in_funcstacks(int copyID)
961{
962 funcstack_T *funcstack;
963
964 for (funcstack = first_funcstack; funcstack != NULL;
965 funcstack = funcstack->fs_next)
966 {
967 typval_T *stack = funcstack->fs_ga.ga_data;
968 int i;
969
970 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100971 if (set_ref_in_item(stack + i, copyID, NULL, NULL, NULL))
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000972 return TRUE; // abort
973 }
974 return FALSE;
975}
976
Bram Moolenaar806a2732022-09-04 15:40:36 +0100977// Ugly static to avoid passing the execution context around through many
978// layers.
979static ectx_T *current_ectx = NULL;
980
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000981/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100982 * Return TRUE if currently executing a :def function.
983 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100984 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100985 int
986in_def_function(void)
987{
988 return current_ectx != NULL;
989}
990
991/*
Ernie Rael4c8da022023-10-11 21:35:11 +0200992 * If executing a class/object method, then fill in the lval_T.
993 * Set lr_tv to the executing item, and lr_exec_class to the executing class;
994 * use free_tv and class_unref when finished with the lval_root.
995 * For use by builtin functions.
996 *
997 * Return FAIL and do nothing if not executing in a class; otherwise OK.
998 */
999 int
1000fill_exec_lval_root(lval_root_T *root)
1001{
1002 ectx_T *ectx = current_ectx;
1003 if (ectx != NULL)
1004 {
1005 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1006 + current_ectx->ec_dfunc_idx;
1007 ufunc_T *ufunc = dfunc->df_ufunc;
1008 if (ufunc->uf_class != NULL) // executing a method?
1009 {
1010 typval_T *tv = alloc_tv();
1011 if (tv != NULL)
1012 {
1013 CLEAR_POINTER(root);
1014 root->lr_tv = tv;
1015 copy_tv(STACK_TV_VAR(0), root->lr_tv);
1016 root->lr_cl_exec = ufunc->uf_class;
1017 ++root->lr_cl_exec->class_refcount;
1018 return OK;
1019 }
1020 }
1021 }
1022
1023 return FAIL;
1024}
1025
1026/*
Bram Moolenaar98aff652022-09-06 21:02:35 +01001027 * Clear "current_ectx" and return the previous value. To be used when calling
1028 * a user function.
1029 */
1030 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +00001031clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +01001032{
1033 ectx_T *r = current_ectx;
1034
1035 current_ectx = NULL;
1036 return r;
1037}
1038
1039 void
1040restore_current_ectx(ectx_T *ectx)
1041{
1042 if (current_ectx != NULL)
1043 iemsg("Restoring current_ectx while it is not NULL");
1044 current_ectx = ectx;
1045}
1046
1047/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01001048 * Add an entry for a deferred function call to the currently executing
1049 * function.
1050 * Return the list or NULL when failed.
1051 */
1052 static list_T *
1053add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001054{
1055 typval_T *defer_tv = STACK_TV_VAR(var_idx);
1056 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001057 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001058 typval_T listval;
1059
1060 if (defer_tv->v_type != VAR_LIST)
1061 {
Bram Moolenaara5348f22022-09-04 11:42:22 +01001062 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001063 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001064 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001065 }
1066 defer_l = defer_tv->vval.v_list;
1067
1068 l = list_alloc_with_items(argcount + 1);
1069 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001070 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001071 listval.v_type = VAR_LIST;
1072 listval.vval.v_list = l;
1073 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +01001074 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001075 {
1076 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001077 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001078 }
1079
Bram Moolenaar806a2732022-09-04 15:40:36 +01001080 return l;
1081}
1082
1083/*
1084 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
1085 * The local variable that lists deferred functions is "var_idx".
1086 * Returns OK or FAIL.
1087 */
1088 static int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001089defer_command(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001090{
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001091 list_T *l = add_defer_item(var_idx, argcount, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001092 int i;
1093 typval_T *func_tv;
1094
1095 if (l == NULL)
1096 return FAIL;
1097
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001098 func_tv = STACK_TV_BOT(-argcount - 1);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001099 if (func_tv->v_type != VAR_PARTIAL && func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001100 {
1101 semsg(_(e_expected_str_but_got_str),
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001102 "function or partial",
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001103 vartype_name(func_tv->v_type));
1104 return FAIL;
1105 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001106 list_set_item(l, 0, func_tv);
1107
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001108 for (i = 0; i < argcount; ++i)
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001109 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
1110 ectx->ec_stack.ga_len -= argcount + 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001111 return OK;
1112}
1113
1114/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001115 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001116 * Consumes "name", also on failure.
1117 * Only to be called when in_def_function() returns TRUE.
1118 */
1119 int
1120add_defer_function(char_u *name, int argcount, typval_T *argvars)
1121{
1122 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1123 + current_ectx->ec_dfunc_idx;
1124 list_T *l;
1125 typval_T func_tv;
1126 int i;
1127
1128 if (dfunc->df_defer_var_idx == 0)
1129 {
1130 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001131 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001132 return FAIL;
1133 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001134
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001135 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001136 if (l == NULL)
1137 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001138 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001139 return FAIL;
1140 }
1141
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001142 func_tv.v_type = VAR_FUNC;
1143 func_tv.v_lock = 0;
1144 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001145 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001146
Bram Moolenaar806a2732022-09-04 15:40:36 +01001147 for (i = 0; i < argcount; ++i)
1148 list_set_item(l, i + 1, argvars + i);
1149 return OK;
1150}
1151
1152/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001153 * Invoked when returning from a function: Invoke any deferred calls.
1154 */
1155 static void
1156invoke_defer_funcs(ectx_T *ectx)
1157{
1158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1159 + ectx->ec_dfunc_idx;
1160 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1161 listitem_T *li;
1162
1163 if (defer_tv->v_type != VAR_LIST)
1164 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001165 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001166 {
1167 list_T *l = li->li_tv.vval.v_list;
1168 typval_T rettv;
1169 typval_T argvars[MAX_FUNC_ARGS];
1170 int i;
1171 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001172 typval_T *functv = &l->lv_first->li_tv;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001173 int argcount = l->lv_len - 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001174
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001175 if (functv->vval.v_string == NULL)
1176 // already being called, can happen if function does ":qa"
1177 continue;
1178
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001179 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001180 {
1181 arg_li = arg_li->li_next;
1182 argvars[i] = arg_li->li_tv;
1183 }
1184
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001185 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001186 CLEAR_FIELD(funcexe);
1187 funcexe.fe_evaluate = TRUE;
1188 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001189 if (functv->v_type == VAR_PARTIAL)
1190 {
1191 funcexe.fe_partial = functv->vval.v_partial;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001192 funcexe.fe_object = functv->vval.v_partial->pt_obj;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001193 if (funcexe.fe_object != NULL)
1194 ++funcexe.fe_object->obj_refcount;
1195 }
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001196
1197 char_u *name = functv->vval.v_string;
1198 functv->vval.v_string = NULL;
1199
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001200 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001201 // first statement in the function will be executed (because of the
1202 // exception). So save and restore the try/catch/throw exception
1203 // state.
1204 exception_state_T estate;
1205 exception_state_save(&estate);
1206 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001207
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001208 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1209
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001210 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001211
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001212 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001213 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001214 }
1215}
1216
1217/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 * Return from the current function.
1219 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001220 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001221func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001224 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001225 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1226 + ectx->ec_dfunc_idx;
1227 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001228 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001229 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1230 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001231 funclocal_T *floc;
1232#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001233 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1234 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235
Bram Moolenaar12d26532021-02-19 19:13:21 +01001236 if (do_profiling == PROF_YES)
1237 {
1238 ufunc_T *caller = prev_dfunc->df_ufunc;
1239
1240 if (dfunc->df_ufunc->uf_profiling
1241 || (caller != NULL && caller->uf_profiling))
1242 {
1243 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1244 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1245 --profile_info_ga.ga_len;
1246 }
1247 }
1248#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001249
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001250 if (dfunc->df_defer_var_idx > 0)
1251 invoke_defer_funcs(ectx);
1252
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001253 // No check for uf_refcount being zero, cannot think of a way that would
1254 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001255 --dfunc->df_ufunc->uf_calls;
1256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001258 entry = estack_pop();
1259 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001260 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001262 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1263 return FAIL;
1264
Bram Moolenaar574950d2023-01-03 19:08:50 +00001265 // Clear the arguments. If this was an object method also clear the
1266 // object, it is just before the arguments.
1267 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001268 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001269 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001270 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1271 clear_tv(STACK_TV(idx));
1272
1273 // Clear local variables and temp values, but not the return value.
1274 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001275 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001277
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001278 // The return value should be on top of the stack. However, when aborting
1279 // it may not be there and ec_frame_idx is the top of the stack.
1280 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001281 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001282 ret_idx = 0;
1283
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001284 if (ectx->ec_outer_ref != NULL)
1285 {
1286 if (ectx->ec_outer_ref->or_outer_allocated)
1287 vim_free(ectx->ec_outer_ref->or_outer);
1288 partial_unref(ectx->ec_outer_ref->or_partial);
1289 vim_free(ectx->ec_outer_ref);
1290 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001291
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001292 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001293 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001294 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1295 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001296 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1297 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001298 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001299 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001300 floc = (void *)STACK_TV(ectx->ec_frame_idx
1301 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001302 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001303 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1304 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001305
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001306 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001307 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001308 else
1309 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001310 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001311 vim_free(floc);
1312 }
1313
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001314 if (ret_idx > 0)
1315 {
1316 // Reset the stack to the position before the call, with a spot for the
1317 // return value, moved there from above the frame.
1318 ectx->ec_stack.ga_len = top + 1;
1319 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1320 }
1321 else
1322 // Reset the stack to the position before the call.
1323 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001324
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001325 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001326 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001327 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328}
1329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330/*
1331 * Prepare arguments and rettv for calling a builtin or user function.
1332 */
1333 static int
1334call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1335{
1336 int idx;
1337 typval_T *tv;
1338
1339 // Move arguments from bottom of the stack to argvars[] and add terminator.
1340 for (idx = 0; idx < argcount; ++idx)
1341 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1342 argvars[argcount].v_type = VAR_UNKNOWN;
1343
1344 // Result replaces the arguments on the stack.
1345 if (argcount > 0)
1346 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001347 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 return FAIL;
1349 else
1350 ++ectx->ec_stack.ga_len;
1351
1352 // Default return value is zero.
1353 tv = STACK_TV_BOT(-1);
1354 tv->v_type = VAR_NUMBER;
1355 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001356 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357
1358 return OK;
1359}
1360
1361/*
1362 * Call a builtin function by index.
1363 */
1364 static int
1365call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1366{
1367 typval_T argvars[MAX_FUNC_ARGS];
1368 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001369 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001370 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001371 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372
1373 if (call_prepare(argcount, argvars, ectx) == FAIL)
1374 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001375 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001377 // Call the builtin function. Set "current_ectx" so that when it
1378 // recursively invokes call_def_function() a closure context can be set.
1379 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001381 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001382 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383
1384 // Clear the arguments.
1385 for (idx = 0; idx < argcount; ++idx)
1386 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001387
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001388 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001389 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390 return OK;
1391}
1392
1393/*
1394 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001395 * If the function is compiled this will add a stack frame and set the
1396 * instruction pointer at the start of the function.
1397 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001398 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001399 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 */
1401 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001402call_ufunc(
1403 ufunc_T *ufunc,
1404 partial_T *pt,
1405 int argcount,
1406 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001407 isn_T *iptr,
1408 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409{
1410 typval_T argvars[MAX_FUNC_ARGS];
1411 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001412 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001414 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001415 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416
Bram Moolenaare99d4222021-06-13 14:01:26 +02001417 if (func_needs_compiling(ufunc, compile_type)
1418 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1419 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001420 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001421 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001422 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001423 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001424 if (error != FCERR_UNKNOWN)
1425 {
1426 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001427 semsg(_(e_too_many_arguments_for_function_str),
1428 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001429 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001430 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001431 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001432 return FAIL;
1433 }
1434
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001435 // The function has been compiled, can call it quickly. For a function
1436 // that was defined later: we can call it directly next time.
1437 if (iptr != NULL)
1438 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001439 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001440 iptr->isn_type = ISN_DCALL;
1441 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1442 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1443 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001444 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446
1447 if (call_prepare(argcount, argvars, ectx) == FAIL)
1448 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001449 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001450 funcexe.fe_evaluate = TRUE;
1451 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452
1453 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001455 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456
1457 // Clear the arguments.
1458 for (idx = 0; idx < argcount; ++idx)
1459 clear_tv(&argvars[idx]);
1460
1461 if (error != FCERR_NONE)
1462 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001463 user_func_error(error, printable_func_name(ufunc),
1464 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 return FAIL;
1466 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001467 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001468 // Error other than from calling the function itself.
1469 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 return OK;
1471}
1472
1473/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001474 * If command modifiers were applied restore them.
1475 */
1476 static void
1477may_restore_cmdmod(funclocal_T *funclocal)
1478{
1479 if (funclocal->floc_restore_cmdmod)
1480 {
1481 cmdmod.cmod_filter_regmatch.regprog = NULL;
1482 undo_cmdmod(&cmdmod);
1483 cmdmod = funclocal->floc_save_cmdmod;
1484 funclocal->floc_restore_cmdmod = FALSE;
1485 }
1486}
1487
1488/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001489 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1490 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001491 */
1492 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001493vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001494{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001495 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001496}
1497
1498/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 * Execute a function by "name".
1500 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001501 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502 * Returns FAIL if not found without an error message.
1503 */
1504 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001505call_by_name(
1506 char_u *name,
1507 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001508 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001509 isn_T *iptr,
1510 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511{
1512 ufunc_T *ufunc;
1513
1514 if (builtin_function(name, -1))
1515 {
1516 int func_idx = find_internal_func(name);
1517
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001518 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001520 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 return FAIL;
1522 return call_bfunc(func_idx, argcount, ectx);
1523 }
1524
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001525 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001526
1527 if (ufunc == NULL)
1528 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001529 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001530
1531 if (script_autoload(name, TRUE))
1532 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001533 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001534
1535 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001536 return FAIL; // bail out if loading the script caused an error
1537 }
1538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001540 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001541 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1542 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001543
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001544 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001545 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546
1547 return FAIL;
1548}
1549
1550 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001551call_partial(
1552 typval_T *tv,
1553 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001554 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001556 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001557 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001559 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001560 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561
1562 if (tv->v_type == VAR_PARTIAL)
1563 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001564 partial_T *pt = tv->vval.v_partial;
1565 int i;
1566
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001567 if (pt->pt_obj != NULL)
1568 {
1569 // partial with an object method. Push the object before the
1570 // function arguments.
1571 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1572 return FAIL;
1573 for (i = 1; i <= argcount; ++i)
1574 *STACK_TV_BOT(-i + 1) = *STACK_TV_BOT(-i);
1575
1576 typval_T *obj_tv = STACK_TV_BOT(-argcount);
1577 obj_tv->v_type = VAR_OBJECT;
1578 obj_tv->v_lock = 0;
1579 obj_tv->vval.v_object = pt->pt_obj;
1580 ++pt->pt_obj->obj_refcount;
1581 ++ectx->ec_stack.ga_len;
1582 }
1583
Bram Moolenaara90afb92020-07-15 22:38:56 +02001584 if (pt->pt_argc > 0)
1585 {
1586 // Make space for arguments from the partial, shift the "argcount"
1587 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001588 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001589 return FAIL;
1590 for (i = 1; i <= argcount; ++i)
1591 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1592 ectx->ec_stack.ga_len += pt->pt_argc;
1593 argcount += pt->pt_argc;
1594
1595 // copy the arguments from the partial onto the stack
1596 for (i = 0; i < pt->pt_argc; ++i)
1597 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1598 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001599 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600
1601 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001602 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 name = pt->pt_name;
1605 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001606 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001608 if (name != NULL)
1609 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001610 char_u fname_buf[FLEN_FIXED + 1];
1611 char_u *tofree = NULL;
1612 funcerror_T error = FCERR_NONE;
1613 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001614
1615 // May need to translate <SNR>123_ to K_SNR.
1616 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1617 if (error != FCERR_NONE)
1618 res = FAIL;
1619 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001620 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001621 vim_free(tofree);
1622 }
1623
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001624 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 {
1626 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001627 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001628 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 return FAIL;
1630 }
1631 return OK;
1632}
1633
1634/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001635 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1636 * TRUE.
1637 */
1638 static int
1639error_if_locked(int lock, char *error)
1640{
1641 if (lock & (VAR_LOCKED | VAR_FIXED))
1642 {
1643 emsg(_(error));
1644 return TRUE;
1645 }
1646 return FALSE;
1647}
1648
1649/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001650 * Give an error if "tv" is not a number and return FAIL.
1651 */
1652 static int
1653check_for_number(typval_T *tv)
1654{
1655 if (tv->v_type != VAR_NUMBER)
1656 {
1657 semsg(_(e_expected_str_but_got_str),
1658 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1659 return FAIL;
1660 }
1661 return OK;
1662}
1663
1664/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001665 * Store "tv" in variable "name".
1666 * This is for s: and g: variables.
1667 */
1668 static void
1669store_var(char_u *name, typval_T *tv)
1670{
1671 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001672 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001673
Bram Moolenaard877a572021-04-01 19:42:48 +02001674 if (tv->v_lock)
1675 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001676 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001677 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001678 restore_funccal();
1679}
1680
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001681/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001682 * Convert "tv" to a string.
1683 * Return FAIL if not allowed.
1684 */
1685 static int
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001686do_2string(typval_T *tv, int is_2string_any, int tostring_flags)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001687{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001688 if (tv->v_type == VAR_STRING)
1689 return OK;
1690
1691 char_u *str;
1692
1693 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001694 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001695 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001696 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001697 case VAR_SPECIAL:
1698 case VAR_BOOL:
1699 case VAR_NUMBER:
1700 case VAR_FLOAT:
Yegappan Lakshmananf01493c2024-04-14 23:21:02 +02001701 case VAR_DICT:
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001702 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001703
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001704 case VAR_LIST:
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001705 if (tostring_flags & TOSTRING_TOLERANT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001706 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001707 char_u *s, *e, *p;
1708 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001709
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001710 ga_init2(&ga, sizeof(char_u *), 1);
1711
1712 // Convert to NL separated items, then
1713 // escape the items and replace the NL with
1714 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001715 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001716 if (str == NULL)
1717 return FAIL;
1718 s = str;
1719 while ((e = vim_strchr(s, '\n')) != NULL)
1720 {
1721 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001722 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001723 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001724 if (p != NULL)
1725 {
1726 ga_concat(&ga, p);
1727 ga_concat(&ga, (char_u *)" ");
1728 vim_free(p);
1729 }
1730 s = e + 1;
1731 }
1732 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001733 clear_tv(tv);
1734 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001735 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001736 return OK;
1737 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001738 if (tostring_flags & TOSTRING_INTERPOLATE)
1739 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001740 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001741 default: to_string_error(tv->v_type);
1742 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001743 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001744 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001745 str = typval_tostring(tv, TRUE);
1746 clear_tv(tv);
1747 tv->v_type = VAR_STRING;
1748 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001749 return OK;
1750}
1751
1752/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001753 * When the value of "sv" is a null list of dict, allocate it.
1754 */
1755 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001756allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001757{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001758 typval_T *tv = sv->sv_tv;
1759
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001760 switch (tv->v_type)
1761 {
1762 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001763 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001764 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001765 break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001766 case VAR_TUPLE:
1767 if (tv->vval.v_tuple == NULL && sv->sv_type != &t_tuple_empty)
1768 (void)rettv_tuple_alloc(tv);
1769 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001770 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001771 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001772 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001773 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001774 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001775 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001776 (void)rettv_blob_alloc(tv);
1777 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001778 default:
1779 break;
1780 }
1781}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001782
Bram Moolenaare7525c52021-01-09 13:20:37 +01001783/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001784 * Return the character "str[index]" where "index" is the character index,
1785 * including composing characters.
1786 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001787 */
1788 char_u *
1789char_from_string(char_u *str, varnumber_T index)
1790{
1791 size_t nbyte = 0;
1792 varnumber_T nchar = index;
1793 size_t slen;
1794
1795 if (str == NULL)
1796 return NULL;
1797 slen = STRLEN(str);
1798
Bram Moolenaarff871402021-03-26 13:34:05 +01001799 // Do the same as for a list: a negative index counts from the end.
1800 // Optimization to check the first byte to be below 0x80 (and no composing
1801 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001802 if (index < 0)
1803 {
1804 int clen = 0;
1805
1806 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001807 {
1808 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1809 ++nbyte;
1810 else if (enc_utf8)
1811 nbyte += utfc_ptr2len(str + nbyte);
1812 else
1813 nbyte += mb_ptr2len(str + nbyte);
1814 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001815 nchar = clen + index;
1816 if (nchar < 0)
1817 // unlike list: index out of range results in empty string
1818 return NULL;
1819 }
1820
1821 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001822 {
1823 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1824 ++nbyte;
1825 else if (enc_utf8)
1826 nbyte += utfc_ptr2len(str + nbyte);
1827 else
1828 nbyte += mb_ptr2len(str + nbyte);
1829 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001830 if (nbyte >= slen)
1831 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001832 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001833}
1834
1835/*
1836 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001837 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001838 * If going over the end return "str_len".
1839 * If "idx" is negative count from the end, -1 is the last character.
1840 * When going over the start return -1.
1841 */
1842 static long
1843char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1844{
1845 varnumber_T nchar = idx;
1846 size_t nbyte = 0;
1847
1848 if (nchar >= 0)
1849 {
1850 while (nchar > 0 && nbyte < str_len)
1851 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001852 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001853 --nchar;
1854 }
1855 }
1856 else
1857 {
1858 nbyte = str_len;
1859 while (nchar < 0 && nbyte > 0)
1860 {
1861 --nbyte;
1862 nbyte -= mb_head_off(str, str + nbyte);
1863 ++nchar;
1864 }
1865 if (nchar < 0)
1866 return -1;
1867 }
1868 return (long)nbyte;
1869}
1870
1871/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001872 * Return the slice "str[first : last]" using character indexes. Composing
1873 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001874 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001875 * Return NULL when the result is empty.
1876 */
1877 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001878string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001879{
1880 long start_byte, end_byte;
1881 size_t slen;
1882
1883 if (str == NULL)
1884 return NULL;
1885 slen = STRLEN(str);
1886 start_byte = char_idx2byte(str, slen, first);
1887 if (start_byte < 0)
1888 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001889 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001890 end_byte = (long)slen;
1891 else
1892 {
1893 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001894 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001895 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001896 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001897 }
1898
1899 if (start_byte >= (long)slen || end_byte <= start_byte)
1900 return NULL;
1901 return vim_strnsave(str + start_byte, end_byte - start_byte);
1902}
1903
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001904/*
1905 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1906 * When "dfunc_idx" is negative don't give an error.
1907 * Returns NULL for an error.
1908 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001909 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001910get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001911{
1912 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001913 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1914 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001915 svar_T *sv;
1916
1917 if (sref->sref_seq != si->sn_script_seq)
1918 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001919 // The script was reloaded after the function was compiled, the
1920 // script_idx may not be valid.
1921 if (dfunc != NULL)
1922 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1923 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001924 return NULL;
1925 }
1926 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001927 if (sv->sv_name == NULL)
1928 {
1929 if (dfunc != NULL)
1930 emsg(_(e_script_variable_was_deleted));
1931 return NULL;
1932 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001933 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001934 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001935 if (dfunc != NULL)
1936 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001937 return NULL;
1938 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001939
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001940 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1941 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001942 {
1943 if (dfunc != NULL)
1944 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1945 return NULL;
1946 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001947 return sv;
1948}
1949
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001950/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001951 * Function passed to do_cmdline() for splitting a script joined by NL
1952 * characters.
1953 */
1954 static char_u *
1955get_split_sourceline(
1956 int c UNUSED,
1957 void *cookie,
1958 int indent UNUSED,
1959 getline_opt_T options UNUSED)
1960{
1961 source_cookie_T *sp = (source_cookie_T *)cookie;
1962 char_u *p;
1963 char_u *line;
1964
Bram Moolenaar20677332021-06-06 17:02:53 +02001965 p = vim_strchr(sp->nextline, '\n');
1966 if (p == NULL)
1967 {
1968 line = vim_strsave(sp->nextline);
1969 sp->nextline += STRLEN(sp->nextline);
1970 }
1971 else
1972 {
1973 line = vim_strnsave(sp->nextline, p - sp->nextline);
1974 sp->nextline = p + 1;
1975 }
1976 return line;
1977}
1978
1979/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 * Execute a function by "name".
1981 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001982 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 */
1984 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001985call_eval_func(
1986 char_u *name,
1987 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001988 ectx_T *ectx,
1989 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990{
Bram Moolenaared677f52020-08-12 16:38:10 +02001991 int called_emsg_before = called_emsg;
1992 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001994 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001995 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001997 dictitem_T *v;
1998
1999 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01002000 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
2001 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002002 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01002003 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002004 return FAIL;
2005 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002006 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 }
Bram Moolenaared677f52020-08-12 16:38:10 +02002008 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009}
2010
2011/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01002012 * When a function reference is used, fill a partial with the information
2013 * needed, especially when it is used as a closure.
2014 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002015 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002016fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002017 partial_T *pt,
2018 ufunc_T *ufunc,
2019 loopvarinfo_T *lvi,
2020 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01002021{
2022 pt->pt_func = ufunc;
2023 pt->pt_refcount = 1;
2024
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002025 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01002026 {
2027 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2028 + ectx->ec_dfunc_idx;
2029
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002030 // The closure may need to find arguments and local variables of the
2031 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01002032 pt->pt_outer.out_stack = &ectx->ec_stack;
2033 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002034 if (ectx->ec_outer_ref != NULL)
2035 {
2036 // The current context already has a context, link to that one.
2037 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
2038 if (ectx->ec_outer_ref->or_partial != NULL)
2039 {
2040 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
2041 ++pt->pt_outer.out_up_partial->pt_refcount;
2042 }
2043 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002044
Bram Moolenaarcc341812022-09-19 15:54:34 +01002045 if (lvi != NULL)
2046 {
2047 int depth;
2048
2049 // The closure may need to find variables defined inside a loop,
2050 // for every nested loop. A new reference is made every time,
2051 // ISN_ENDLOOP will check if they are actually used.
2052 for (depth = 0; depth < lvi->lvi_depth; ++depth)
2053 {
2054 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
2055 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
2056 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
2057 pt->pt_outer.out_loop[depth].var_count =
2058 lvi->lvi_loop[depth].var_count;
2059 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002060 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002061 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002062 else
2063 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002064
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002065 // If the function currently executing returns and the closure is still
2066 // being referenced, we need to make a copy of the context (arguments
2067 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002068 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02002069 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01002070 {
2071 vim_free(pt);
2072 return FAIL;
2073 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002074 // Extra variable keeps the count of closures created in the current
2075 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01002076 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
2077 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
2078
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002079 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
2080 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002081 ++pt->pt_refcount;
2082 ++ectx->ec_funcrefs.ga_len;
2083 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002084 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002085 return OK;
2086}
2087
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002088/*
2089 * Execute iptr->isn_arg.string as an Ex command.
2090 */
2091 static int
Ernie Raelee865f32023-09-29 19:53:55 +02002092exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002093{
2094 source_cookie_T cookie;
2095
2096 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002097 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002098 CLEAR_FIELD(cookie);
2099 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02002100 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002101 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
2102 || did_emsg)
2103 return FAIL;
2104 return OK;
2105}
2106
Bram Moolenaar89445512022-04-14 12:58:23 +01002107/*
2108 * If script "sid" is not loaded yet then load it now.
2109 * Caller must make sure "sid" is a valid script ID.
2110 * "loaded" is set to TRUE if the script had to be loaded.
2111 * Returns FAIL if loading fails, OK if already loaded or loaded now.
2112 */
2113 int
2114may_load_script(int sid, int *loaded)
2115{
2116 scriptitem_T *si = SCRIPT_ITEM(sid);
2117
2118 if (si->sn_state == SN_STATE_NOT_LOADED)
2119 {
2120 *loaded = TRUE;
2121 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
2122 {
2123 semsg(_(e_cant_open_file_str), si->sn_name);
2124 return FAIL;
2125 }
2126 }
2127 return OK;
2128}
2129
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002130// used for v_instr of typval of VAR_INSTR
2131struct instr_S {
2132 ectx_T *instr_ectx;
2133 isn_T *instr_instr;
2134};
2135
Bram Moolenaar4c137212021-04-19 16:48:48 +02002136// used for substitute_instr
2137typedef struct subs_expr_S {
2138 ectx_T *subs_ectx;
2139 isn_T *subs_instr;
2140 int subs_status;
2141} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002143// Set when calling do_debug().
2144static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002145static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002146
2147/*
2148 * When debugging lookup "name" and return the typeval.
2149 * When not found return NULL.
2150 */
2151 typval_T *
2152lookup_debug_var(char_u *name)
2153{
2154 int idx;
2155 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002156 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002157 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002158 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002159
2160 if (ectx == NULL)
2161 return NULL;
2162 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2163
2164 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002165 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002166 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002167 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2168
2169 // the variable name may be NULL when not available in this block
2170 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002171 return STACK_TV_VAR(idx);
2172 }
2173
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002174 // Go through argument names.
2175 ufunc = dfunc->df_ufunc;
2176 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2177 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2178 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2179 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2180 - varargs_off + idx);
2181 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2182 return STACK_TV(ectx->ec_frame_idx - 1);
2183
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002184 return NULL;
2185}
2186
Bram Moolenaar26a44842021-09-02 18:49:06 +02002187/*
2188 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2189 * breakpoint was set in that function or when there is any expression.
2190 */
2191 int
2192may_break_in_function(ufunc_T *ufunc)
2193{
2194 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2195}
2196
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002197 static void
2198handle_debug(isn_T *iptr, ectx_T *ectx)
2199{
2200 char_u *line;
2201 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2202 + ectx->ec_dfunc_idx)->df_ufunc;
2203 isn_T *ni;
2204 int end_lnum = iptr->isn_lnum;
2205 garray_T ga;
2206 int lnum;
2207
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002208 if (ex_nesting_level > debug_break_level)
2209 {
2210 linenr_T breakpoint;
2211
Bram Moolenaar26a44842021-09-02 18:49:06 +02002212 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002213 return;
2214
2215 // check for the next breakpoint if needed
2216 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002217 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002218 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2219 return;
2220 }
2221
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002222 SOURCING_LNUM = iptr->isn_lnum;
2223 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002224 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002225
2226 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2227 if (ni->isn_type == ISN_DEBUG
2228 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002229 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002230 || ni->isn_type == ISN_RETURN_VOID)
2231 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002232 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002233 break;
2234 }
2235
2236 if (end_lnum > iptr->isn_lnum)
2237 {
2238 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002239 for (lnum = iptr->isn_lnum; lnum < end_lnum
2240 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002241 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002242 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002243
Bram Moolenaar303215d2021-07-07 20:10:43 +02002244 if (p == NULL)
2245 continue; // left over from continuation line
2246 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002247 if (*p == '#')
2248 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002249 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002250 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2251 if (STRNCMP(p, "def ", 4) == 0)
2252 break;
2253 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002254 line = ga_concat_strings(&ga, " ");
2255 vim_free(ga.ga_data);
2256 }
2257 else
2258 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002259
Dominique Pelle6e969552021-06-17 13:53:41 +02002260 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002261 debug_context = NULL;
2262
2263 if (end_lnum > iptr->isn_lnum)
2264 vim_free(line);
2265}
2266
Bram Moolenaar4c137212021-04-19 16:48:48 +02002267/*
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002268 * Do a runtime check of the RHS value against the LHS List member type.
2269 * This is used by the STOREINDEX instruction to perform a type check
2270 * at runtime if compile time type check cannot be performed (VAR_ANY).
2271 * Returns FAIL if there is a type mismatch.
2272 */
2273 static int
2274storeindex_check_list_member_type(
2275 list_T *lhs_list,
2276 typval_T *rhs_tv,
2277 ectx_T *ectx)
2278{
2279 if (lhs_list->lv_type == NULL || lhs_list->lv_type->tt_member == NULL)
2280 return OK;
2281
2282 return check_typval_type(lhs_list->lv_type->tt_member, rhs_tv,
2283 ectx->ec_where);
2284}
2285
2286/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002287 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002288 * Returns OK, FAIL or NOTDONE (uncatchable error).
2289 */
2290 static int
2291execute_storeindex(isn_T *iptr, ectx_T *ectx)
2292{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002293 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002294 typval_T *tv;
2295 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002296 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002297 typval_T *tv_dest = STACK_TV_BOT(-1);
2298 int status = OK;
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002299 int check_rhs_type = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002300
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002301 if (tv_idx->v_type == VAR_NUMBER)
2302 lidx = (long)tv_idx->vval.v_number;
2303
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002304 // Stack contains:
2305 // -3 value to be stored
2306 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002307 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002308 tv = STACK_TV_BOT(-3);
2309 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002310
2311 // Make sure an object has been initialized
2312 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2313 {
2314 emsg(_(e_using_null_object));
2315 status = FAIL;
2316 }
2317 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002318 {
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002319 check_rhs_type = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002320 dest_type = tv_dest->v_type;
2321 if (dest_type == VAR_DICT)
2322 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002323 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2324 {
2325 // Need to get the member index now that the class is known.
2326 object_T *obj = tv_dest->vval.v_object;
Ernie Raelbe828252024-07-26 18:37:02 +02002327 if (obj == NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002328 {
Ernie Raelbe828252024-07-26 18:37:02 +02002329 emsg(_(e_using_null_object));
2330 status = FAIL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002331 }
2332 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002333 {
Ernie Raelbe828252024-07-26 18:37:02 +02002334 class_T *cl = obj->obj_class;
2335 char_u *member = tv_idx->vval.v_string;
2336
2337 int m_idx;
2338 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2339 if (m != NULL)
2340 {
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01002341 // Get the current function
2342 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2343 + ectx->ec_dfunc_idx)->df_ufunc;
Yegappan Lakshmanane7984462024-11-12 21:03:00 +01002344 where_T where = WHERE_INIT;
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01002345
2346 // Check whether the member variable is writeable
2347 if ((m->ocm_access != VIM_ACCESS_ALL) &&
2348 (ufunc->uf_class == NULL ||
2349 !class_instance_of(ufunc->uf_class, cl)))
Ernie Raelbe828252024-07-26 18:37:02 +02002350 {
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01002351 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
2352 ? e_cannot_access_protected_variable_str
2353 : e_variable_is_not_writable_str;
2354 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
Ernie Raelbe828252024-07-26 18:37:02 +02002355 status = FAIL;
2356 }
Yegappan Lakshmanane7984462024-11-12 21:03:00 +01002357 // Fail if the variable is a const or final or the type
2358 // is not compatible
2359 else if (oc_var_check_ro(cl, m) ||
2360 check_typval_type(m->ocm_type, tv, where)
2361 == FAIL)
2362 status = FAIL;
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01002363 else
2364 lidx = m_idx;
Ernie Raelbe828252024-07-26 18:37:02 +02002365 }
2366 else
2367 {
2368 member_not_found_msg(cl, VAR_OBJECT, member, 0);
2369 status = FAIL;
2370 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002371 }
2372 }
2373 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2374 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002375 {
2376 emsg(_(e_number_expected));
2377 status = FAIL;
2378 }
2379 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002380
2381 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002382 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002383 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002384 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002385 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002386
Bram Moolenaare08be092022-02-17 13:08:26 +00002387 if (list == NULL)
2388 {
2389 emsg(_(e_list_not_set));
2390 return FAIL;
2391 }
2392 if (lidx < 0 && list->lv_len + lidx >= 0)
2393 // negative index is relative to the end
2394 lidx = list->lv_len + lidx;
2395 if (lidx < 0 || lidx > list->lv_len)
2396 {
2397 semsg(_(e_list_index_out_of_range_nr), lidx);
2398 return FAIL;
2399 }
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002400
2401 // Do a runtime type check for VAR_ANY
2402 if (check_rhs_type &&
2403 storeindex_check_list_member_type(list, tv, ectx) == FAIL)
2404 return FAIL;
2405
Bram Moolenaare08be092022-02-17 13:08:26 +00002406 if (lidx < list->lv_len)
2407 {
2408 listitem_T *li = list_find(list, lidx);
2409
2410 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002411 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002412 return FAIL;
2413 // overwrite existing list item
2414 clear_tv(&li->li_tv);
2415 li->li_tv = *tv;
2416 }
2417 else
2418 {
2419 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2420 return FAIL;
2421 // append to list, only fails when out of memory
2422 if (list_append_tv(list, tv) == FAIL)
2423 return NOTDONE;
2424 clear_tv(tv);
2425 }
2426 }
2427 else if (dest_type == VAR_DICT)
2428 {
2429 char_u *key = tv_idx->vval.v_string;
2430 dict_T *dict = tv_dest->vval.v_dict;
2431 dictitem_T *di;
2432
2433 SOURCING_LNUM = iptr->isn_lnum;
2434 if (dict == NULL)
2435 {
2436 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002437 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002438 }
2439 if (key == NULL)
2440 key = (char_u *)"";
2441 di = dict_find(dict, key, -1);
2442 if (di != NULL)
2443 {
2444 if (error_if_locked(di->di_tv.v_lock,
2445 e_cannot_change_dict_item))
2446 return FAIL;
2447 // overwrite existing value
2448 clear_tv(&di->di_tv);
2449 di->di_tv = *tv;
2450 }
2451 else
2452 {
2453 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2454 return FAIL;
2455 // add to dict, only fails when out of memory
2456 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2457 return NOTDONE;
2458 clear_tv(tv);
2459 }
2460 }
2461 else if (dest_type == VAR_BLOB)
2462 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002463 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002464 varnumber_T nr;
2465 int error = FALSE;
2466 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002467
2468 if (blob == NULL)
2469 {
2470 emsg(_(e_blob_not_set));
2471 return FAIL;
2472 }
2473 len = blob_len(blob);
2474 if (lidx < 0 && len + lidx >= 0)
2475 // negative index is relative to the end
2476 lidx = len + lidx;
2477
2478 // Can add one byte at the end.
2479 if (lidx < 0 || lidx > len)
2480 {
2481 semsg(_(e_blob_index_out_of_range_nr), lidx);
2482 return FAIL;
2483 }
2484 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2485 return FAIL;
2486 nr = tv_get_number_chk(tv, &error);
2487 if (error)
2488 return FAIL;
2489 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002490 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002491 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2492 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002493 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002494
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002495 if (dest_type == VAR_OBJECT)
2496 {
2497 object_T *obj = tv_dest->vval.v_object;
2498
2499 otv = (typval_T *)(obj + 1);
2500 class_T *itf = iptr->isn_arg.storeindex.si_class;
2501 if (itf != NULL)
2502 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002503 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002504 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002505 }
2506 else
2507 {
2508 // VAR_CLASS
2509 class_T *class = tv_dest->vval.v_class;
2510 otv = class->class_members_tv;
2511 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002512
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002513 clear_tv(&otv[lidx]);
2514 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002515 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002516 else if (dest_type == VAR_TUPLE)
2517 {
2518 emsg(_(e_tuple_is_immutable));
2519 status = FAIL;
2520 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002521 else
2522 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002523 status = FAIL;
2524 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002525 }
2526 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002527
2528 clear_tv(tv_idx);
2529 clear_tv(tv_dest);
2530 ectx->ec_stack.ga_len -= 3;
2531 if (status == FAIL)
2532 {
2533 clear_tv(tv);
2534 return FAIL;
2535 }
2536 return OK;
2537}
2538
2539/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002540 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002541 */
2542 static int
2543execute_storerange(isn_T *iptr, ectx_T *ectx)
2544{
2545 typval_T *tv;
2546 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2547 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2548 typval_T *tv_dest = STACK_TV_BOT(-1);
2549 int status = OK;
2550
2551 // Stack contains:
2552 // -4 value to be stored
2553 // -3 first index or "none"
2554 // -2 second index or "none"
2555 // -1 destination list or blob
2556 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002557 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002558 if (tv_dest->v_type == VAR_LIST)
2559 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002560 long n1;
2561 long n2;
2562 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002563
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002564 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2565 if (tv_idx2->v_type == VAR_SPECIAL
2566 && tv_idx2->vval.v_number == VVAL_NONE)
2567 n2 = list_len(tv_dest->vval.v_list) - 1;
2568 else
2569 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2570
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002571 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002572 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002573 status = FAIL;
2574 else
2575 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002576 status = check_range_index_two(tv_dest->vval.v_list,
2577 &n1, li1, &n2, FALSE);
2578 if (status != FAIL)
2579 status = list_assign_range(
2580 tv_dest->vval.v_list,
2581 tv->vval.v_list,
2582 n1,
2583 n2,
2584 tv_idx2->v_type == VAR_SPECIAL,
2585 (char_u *)"=",
2586 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002587 }
2588 }
2589 else if (tv_dest->v_type == VAR_BLOB)
2590 {
2591 varnumber_T n1;
2592 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002593 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002594
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002595 n1 = tv_get_number_chk(tv_idx1, NULL);
2596 if (tv_idx2->v_type == VAR_SPECIAL
2597 && tv_idx2->vval.v_number == VVAL_NONE)
2598 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2599 else
2600 n2 = tv_get_number_chk(tv_idx2, NULL);
2601 bloblen = blob_len(tv_dest->vval.v_blob);
2602
2603 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2604 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002605 status = FAIL;
2606 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002607 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002608 }
2609 else
2610 {
2611 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002612 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002613 }
2614
2615 clear_tv(tv_idx1);
2616 clear_tv(tv_idx2);
2617 clear_tv(tv_dest);
2618 ectx->ec_stack.ga_len -= 4;
2619 clear_tv(tv);
2620
2621 return status;
2622}
2623
2624/*
2625 * Unlet item in list or dict variable.
2626 */
2627 static int
2628execute_unletindex(isn_T *iptr, ectx_T *ectx)
2629{
2630 typval_T *tv_idx = STACK_TV_BOT(-2);
2631 typval_T *tv_dest = STACK_TV_BOT(-1);
2632 int status = OK;
2633
2634 // Stack contains:
2635 // -2 index
2636 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002637 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002638 if (tv_dest->v_type == VAR_DICT)
2639 {
2640 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002641 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002642 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002643 semsg(_(e_expected_str_but_got_str),
2644 vartype_name(VAR_STRING),
2645 vartype_name(tv_idx->v_type));
2646 status = FAIL;
2647 }
2648 else
2649 {
2650 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002651 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002652 dictitem_T *di = NULL;
2653
2654 if (d != NULL && value_check_lock(
2655 d->dv_lock, NULL, FALSE))
2656 status = FAIL;
2657 else
2658 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002659 if (tv_idx->v_type == VAR_STRING)
2660 {
2661 key = tv_idx->vval.v_string;
2662 if (key == NULL)
2663 key = (char_u *)"";
2664 }
2665 else
2666 {
2667 key = tv_get_string(tv_idx);
2668 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002669 if (d != NULL)
2670 di = dict_find(d, key, (int)STRLEN(key));
2671 if (di == NULL)
2672 {
2673 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002674 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002675 status = FAIL;
2676 }
2677 else if (var_check_fixed(di->di_flags,
2678 NULL, FALSE)
2679 || var_check_ro(di->di_flags,
2680 NULL, FALSE))
2681 status = FAIL;
2682 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002683 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002684 }
2685 }
2686 }
2687 else if (tv_dest->v_type == VAR_LIST)
2688 {
2689 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002690 if (check_for_number(tv_idx) == FAIL)
2691 {
2692 status = FAIL;
2693 }
2694 else
2695 {
2696 list_T *l = tv_dest->vval.v_list;
2697 long n = (long)tv_idx->vval.v_number;
2698
2699 if (l != NULL && value_check_lock(
2700 l->lv_lock, NULL, FALSE))
2701 status = FAIL;
2702 else
2703 {
2704 listitem_T *li = list_find(l, n);
2705
2706 if (li == NULL)
2707 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002708 semsg(_(e_list_index_out_of_range_nr), n);
2709 status = FAIL;
2710 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002711 else
2712 listitem_remove(l, li);
2713 }
2714 }
2715 }
2716 else
2717 {
2718 status = FAIL;
2719 semsg(_(e_cannot_index_str),
2720 vartype_name(tv_dest->v_type));
2721 }
2722
2723 clear_tv(tv_idx);
2724 clear_tv(tv_dest);
2725 ectx->ec_stack.ga_len -= 2;
2726
2727 return status;
2728}
2729
2730/*
2731 * Unlet a range of items in a list variable.
2732 */
2733 static int
2734execute_unletrange(isn_T *iptr, ectx_T *ectx)
2735{
2736 // Stack contains:
2737 // -3 index1
2738 // -2 index2
2739 // -1 dict or list
2740 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2741 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2742 typval_T *tv_dest = STACK_TV_BOT(-1);
2743 int status = OK;
2744
2745 if (tv_dest->v_type == VAR_LIST)
2746 {
2747 // indexes must be a number
2748 SOURCING_LNUM = iptr->isn_lnum;
2749 if (check_for_number(tv_idx1) == FAIL
2750 || (tv_idx2->v_type != VAR_SPECIAL
2751 && check_for_number(tv_idx2) == FAIL))
2752 {
2753 status = FAIL;
2754 }
2755 else
2756 {
2757 list_T *l = tv_dest->vval.v_list;
2758 long n1 = (long)tv_idx1->vval.v_number;
2759 long n2 = tv_idx2->v_type == VAR_SPECIAL
2760 ? 0 : (long)tv_idx2->vval.v_number;
2761 listitem_T *li;
2762
2763 li = list_find_index(l, &n1);
2764 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002765 {
2766 semsg(_(e_list_index_out_of_range_nr),
2767 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002768 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002769 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002770 else
2771 {
2772 if (n1 < 0)
2773 n1 = list_idx_of_item(l, li);
2774 if (n2 < 0)
2775 {
2776 listitem_T *li2 = list_find(l, n2);
2777
2778 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002779 {
2780 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002781 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002782 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002783 else
2784 n2 = list_idx_of_item(l, li2);
2785 }
2786 if (status != FAIL
2787 && tv_idx2->v_type != VAR_SPECIAL
2788 && n2 < n1)
2789 {
2790 semsg(_(e_list_index_out_of_range_nr), n2);
2791 status = FAIL;
2792 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002793 if (status != FAIL)
2794 list_unlet_range(l, li, n1,
2795 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002796 }
2797 }
2798 }
2799 else
2800 {
2801 status = FAIL;
2802 SOURCING_LNUM = iptr->isn_lnum;
2803 semsg(_(e_cannot_index_str),
2804 vartype_name(tv_dest->v_type));
2805 }
2806
2807 clear_tv(tv_idx1);
2808 clear_tv(tv_idx2);
2809 clear_tv(tv_dest);
2810 ectx->ec_stack.ga_len -= 3;
2811
2812 return status;
2813}
2814
2815/*
2816 * Top of a for loop.
2817 */
2818 static int
2819execute_for(isn_T *iptr, ectx_T *ectx)
2820{
2821 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002822 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002823 typval_T *ltv = STACK_TV_BOT(-1);
2824 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002825 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002826
2827 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2828 return FAIL;
2829 if (ltv->v_type == VAR_LIST)
2830 {
2831 list_T *list = ltv->vval.v_list;
2832
2833 // push the next item from the list
2834 ++idxtv->vval.v_number;
2835 if (list == NULL
2836 || idxtv->vval.v_number >= list->lv_len)
2837 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002838 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002839 }
2840 else if (list->lv_first == &range_list_item)
2841 {
2842 // non-materialized range() list
2843 tv = STACK_TV_BOT(0);
2844 tv->v_type = VAR_NUMBER;
2845 tv->v_lock = 0;
2846 tv->vval.v_number = list_find_nr(
2847 list, idxtv->vval.v_number, NULL);
2848 ++ectx->ec_stack.ga_len;
2849 }
2850 else
2851 {
2852 listitem_T *li = list_find(list,
2853 idxtv->vval.v_number);
2854
2855 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2856 ++ectx->ec_stack.ga_len;
2857 }
2858 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002859 else if (ltv->v_type == VAR_TUPLE)
2860 {
2861 tuple_T *tuple = ltv->vval.v_tuple;
2862
2863 // push the next item from the tuple
2864 ++idxtv->vval.v_number;
2865 if (tuple == NULL || idxtv->vval.v_number >= TUPLE_LEN(tuple))
2866 jump = TRUE;
2867 else
2868 {
2869 copy_tv(TUPLE_ITEM(tuple, idxtv->vval.v_number), STACK_TV_BOT(0));
2870 ++ectx->ec_stack.ga_len;
2871 }
2872 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002873 else if (ltv->v_type == VAR_STRING)
2874 {
2875 char_u *str = ltv->vval.v_string;
2876
2877 // The index is for the last byte of the previous
2878 // character.
2879 ++idxtv->vval.v_number;
2880 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2881 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002882 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002883 }
2884 else
2885 {
2886 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2887
2888 // Push the next character from the string.
2889 tv = STACK_TV_BOT(0);
2890 tv->v_type = VAR_STRING;
2891 tv->vval.v_string = vim_strnsave(
2892 str + idxtv->vval.v_number, clen);
2893 ++ectx->ec_stack.ga_len;
2894 idxtv->vval.v_number += clen - 1;
2895 }
2896 }
2897 else if (ltv->v_type == VAR_BLOB)
2898 {
2899 blob_T *blob = ltv->vval.v_blob;
2900
2901 // When we get here the first time make a copy of the
2902 // blob, so that the iteration still works when it is
2903 // changed.
2904 if (idxtv->vval.v_number == -1 && blob != NULL)
2905 {
2906 blob_copy(blob, ltv);
2907 blob_unref(blob);
2908 blob = ltv->vval.v_blob;
2909 }
2910
2911 // The index is for the previous byte.
2912 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002913 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002914 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002915 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002916 }
2917 else
2918 {
2919 // Push the next byte from the blob.
2920 tv = STACK_TV_BOT(0);
2921 tv->v_type = VAR_NUMBER;
2922 tv->vval.v_number = blob_get(blob,
2923 idxtv->vval.v_number);
2924 ++ectx->ec_stack.ga_len;
2925 }
2926 }
2927 else
2928 {
2929 semsg(_(e_for_loop_on_str_not_supported),
2930 vartype_name(ltv->v_type));
2931 return FAIL;
2932 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002933
2934 if (jump)
2935 {
2936 // past the end of the list/string/blob, jump to "endfor"
2937 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2938 may_restore_cmdmod(&ectx->ec_funclocal);
2939 }
2940 else
2941 {
2942 // Store the current number of funcrefs, this may be used in
2943 // ISN_LOOPEND. The variable index is always one more than the loop
2944 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002945 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002946 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2947 }
2948
2949 return OK;
2950}
2951
2952/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002953 * Code for handling variables declared inside a loop and used in a closure.
2954 * This is very similar to what is done with funcstack_T. The difference is
2955 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2956 * scope of the block inside a loop and each loop may have its own.
2957 */
2958
2959// Double linked list of loopvars_T in use.
2960static loopvars_T *first_loopvars = NULL;
2961
2962 static void
2963add_loopvars_to_list(loopvars_T *loopvars)
2964{
2965 // Link in list of loopvarss.
2966 if (first_loopvars != NULL)
2967 first_loopvars->lvs_prev = loopvars;
2968 loopvars->lvs_next = first_loopvars;
2969 loopvars->lvs_prev = NULL;
2970 first_loopvars = loopvars;
2971}
2972
2973 static void
2974remove_loopvars_from_list(loopvars_T *loopvars)
2975{
2976 if (loopvars->lvs_prev == NULL)
2977 first_loopvars = loopvars->lvs_next;
2978 else
2979 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2980 if (loopvars->lvs_next != NULL)
2981 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2982}
2983
2984/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002985 * End of a for or while loop: Handle any variables used by a closure.
2986 */
2987 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002988execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002989{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002990 endloop_T *endloop = &iptr->isn_arg.endloop;
2991 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2992 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002993 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002994 garray_T *gap = &ectx->ec_funcrefs;
2995 int closure_in_use = FALSE;
2996 loopvars_T *loopvars;
2997 typval_T *stack;
2998 int idx;
2999
Bram Moolenaarcc341812022-09-19 15:54:34 +01003000 // Check if any created closure is still being referenced and loopvars have
3001 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003002 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
3003 {
3004 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
3005
Bram Moolenaarcc341812022-09-19 15:54:34 +01003006 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003007 {
3008 int refcount = pt->pt_refcount;
3009 int i;
3010
3011 // A Reference in a variable inside the loop doesn't count, it gets
3012 // unreferenced at the end of the loop.
3013 for (i = 0; i < endloop->end_var_count; ++i)
3014 {
3015 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
3016
3017 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
3018 --refcount;
3019 }
3020 if (refcount > 1)
3021 {
3022 closure_in_use = TRUE;
3023 break;
3024 }
3025 }
3026 }
3027
3028 // If no function reference were created since the start of the loop block
3029 // or it is no longer referenced there is nothing to do.
3030 if (!closure_in_use)
3031 return OK;
3032
3033 // A closure is using variables declared inside the loop.
3034 // Move them to the called function.
3035 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
3036 if (loopvars == NULL)
3037 return FAIL;
3038
3039 loopvars->lvs_ga.ga_len = endloop->end_var_count;
3040 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
3041 loopvars->lvs_ga.ga_data = stack;
3042 if (stack == NULL)
3043 {
3044 vim_free(loopvars);
3045 return FAIL;
3046 }
3047 add_loopvars_to_list(loopvars);
3048
3049 // Move the variable values.
3050 for (idx = 0; idx < endloop->end_var_count; ++idx)
3051 {
3052 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
3053
3054 *(stack + idx) = *tv;
3055 tv->v_type = VAR_UNKNOWN;
3056 }
3057
3058 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
3059 {
3060 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
3061
Bram Moolenaarcc341812022-09-19 15:54:34 +01003062 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003063 {
3064 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01003065 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003066
Bram Moolenaarcc341812022-09-19 15:54:34 +01003067 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
3068 pt->pt_outer.out_loop[depth].var_idx -=
3069 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003070 }
3071 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01003072
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003073 return OK;
3074}
3075
3076/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003077 * Called when a partial is freed or its reference count goes down to one. The
3078 * loopvars may be the only reference to the partials in the local variables.
3079 * Go over all of them, the funcref and can be freed if all partials
3080 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01003081 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003082 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01003083 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003084loopvars_check_refcount(loopvars_T *loopvars)
3085{
3086 int i;
3087 garray_T *gap = &loopvars->lvs_ga;
3088 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003089 typval_T *stack = gap->ga_data;
3090
Bram Moolenaarcc341812022-09-19 15:54:34 +01003091 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
3092 return FALSE;
3093 for (i = 0; i < gap->ga_len; ++i)
3094 {
3095 typval_T *tv = ((typval_T *)gap->ga_data) + i;
3096
3097 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
3098 && tv->vval.v_partial->pt_refcount == 1)
3099 {
3100 int depth;
3101
3102 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
3103 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
3104 ++done;
3105 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003106 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003107 if (done != loopvars->lvs_min_refcount)
3108 return FALSE;
3109
3110 // All partials referencing the loopvars have a reference count of
3111 // one, thus the loopvars is no longer of use.
3112 stack = gap->ga_data;
3113 for (i = 0; i < gap->ga_len; ++i)
3114 clear_tv(stack + i);
3115 vim_free(stack);
3116 remove_loopvars_from_list(loopvars);
3117 vim_free(loopvars);
3118 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003119}
3120
3121/*
3122 * For garbage collecting: set references in all variables referenced by
3123 * all loopvars.
3124 */
3125 int
3126set_ref_in_loopvars(int copyID)
3127{
3128 loopvars_T *loopvars;
3129
3130 for (loopvars = first_loopvars; loopvars != NULL;
3131 loopvars = loopvars->lvs_next)
3132 {
3133 typval_T *stack = loopvars->lvs_ga.ga_data;
3134 int i;
3135
3136 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003137 if (set_ref_in_item(stack + i, copyID, NULL, NULL, NULL))
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003138 return TRUE; // abort
3139 }
3140 return FALSE;
3141}
3142
3143/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00003144 * Load instruction for w:/b:/g:/t: variable.
3145 * "isn_type" is used instead of "iptr->isn_type".
3146 */
3147 static int
3148load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
3149{
3150 dictitem_T *di = NULL;
3151 hashtab_T *ht = NULL;
3152 char namespace;
3153
3154 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3155 return NOTDONE;
3156 switch (isn_type)
3157 {
3158 case ISN_LOADG:
3159 ht = get_globvar_ht();
3160 namespace = 'g';
3161 break;
3162 case ISN_LOADB:
3163 ht = &curbuf->b_vars->dv_hashtab;
3164 namespace = 'b';
3165 break;
3166 case ISN_LOADW:
3167 ht = &curwin->w_vars->dv_hashtab;
3168 namespace = 'w';
3169 break;
3170 case ISN_LOADT:
3171 ht = &curtab->tp_vars->dv_hashtab;
3172 namespace = 't';
3173 break;
3174 default: // Cannot reach here
3175 return NOTDONE;
3176 }
3177 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3178
Bram Moolenaar06b77222022-01-25 15:51:56 +00003179 if (di == NULL)
3180 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003181 if (isn_type == ISN_LOADG)
3182 {
3183 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3184
3185 // g:Something could be a function
3186 if (ufunc != NULL)
3187 {
3188 typval_T *tv = STACK_TV_BOT(0);
3189
3190 ++ectx->ec_stack.ga_len;
3191 tv->v_type = VAR_FUNC;
3192 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3193 if (tv->vval.v_string == NULL)
3194 return FAIL;
3195 STRCPY(tv->vval.v_string, "g:");
3196 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3197 return OK;
3198 }
3199 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003200 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003201 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003202 // no check if the item exists in the script but
3203 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003204 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003205 else
3206 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003207 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003208 return FAIL;
3209 }
3210 else
3211 {
3212 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3213 ++ectx->ec_stack.ga_len;
3214 }
3215 return OK;
3216}
3217
Bram Moolenaard0200c82023-01-28 15:19:40 +00003218
3219 static void
3220object_required_error(typval_T *tv)
3221{
3222 garray_T type_list;
3223 ga_init2(&type_list, sizeof(type_T *), 10);
3224 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3225 char *tofree = NULL;
3226 char *typename = type_name(type, &tofree);
3227 semsg(_(e_object_required_found_str), typename);
3228 vim_free(tofree);
3229 clear_type_list(&type_list);
3230}
3231
Bram Moolenaar06b77222022-01-25 15:51:56 +00003232/*
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003233 * Accessing the variable or method of an object or a class stored in a
3234 * variable of type "any".
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003235 * Returns OK if the member variable is present.
3236 * Returns FAIL if the variable is not found.
3237 */
3238 static int
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003239var_any_get_oc_member(class_T *current_class, isn_T *iptr, typval_T *tv)
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003240{
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003241 int is_object = tv->v_type == VAR_OBJECT;
3242 class_T *tv_cl;
3243 object_T *obj = NULL;
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003244 typval_T mtv;
3245
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003246 if (is_object)
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003247 {
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003248 obj = tv->vval.v_object;
3249 if (obj == NULL)
3250 {
3251 SOURCING_LNUM = iptr->isn_lnum;
3252 emsg(_(e_using_null_object));
3253 return FAIL;
3254 }
3255 tv_cl = obj->obj_class;
3256 }
3257 else
3258 {
3259 tv_cl = tv->vval.v_class;
3260 if (tv_cl == NULL)
3261 {
3262 SOURCING_LNUM = iptr->isn_lnum;
3263 emsg(_(e_using_null_class));
3264 return FAIL;
3265 }
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003266 }
3267
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003268 // get_member_tv() needs the class/object information in the typval
3269 // argument. So set the object information.
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003270 copy_tv(tv, &mtv);
3271
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003272 // 'name' can either be an instance or class variable or method
Yegappan Lakshmanan084529c2024-12-24 09:50:01 +01003273 int namelen = (int)STRLEN(iptr->isn_arg.string);
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003274 int save_did_emsg = did_emsg;
3275
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003276 if (get_member_tv(tv_cl, is_object, iptr->isn_arg.string, namelen,
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003277 current_class, &mtv) == OK)
3278 {
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003279 // instance or class variable
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003280 copy_tv(&mtv, tv);
3281 clear_tv(&mtv);
3282 return OK;
3283 }
3284
3285 if (did_emsg != save_did_emsg)
3286 return FAIL;
3287
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003288 // could be a class or instance method
3289 ufunc_T *oc_method;
3290 int oc_method_idx;
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003291
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003292 oc_method = method_lookup(tv_cl, tv->v_type, iptr->isn_arg.string,
3293 namelen, &oc_method_idx);
3294 if (oc_method == NULL)
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003295 {
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003296 char *msg;
3297
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003298 SOURCING_LNUM = iptr->isn_lnum;
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003299 if (is_object)
3300 msg = e_variable_not_found_on_object_str_str;
3301 else
3302 msg = e_class_variable_str_not_found_in_class_str;
3303 semsg(_(msg), iptr->isn_arg.string, tv_cl->class_name);
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003304 return FAIL;
3305 }
3306
3307 // Protected methods are not accessible outside the class
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003308 if (*oc_method->uf_name == '_'
3309 && !class_instance_of(current_class, tv_cl))
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003310 {
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003311 semsg(_(e_cannot_access_protected_method_str), oc_method->uf_name);
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003312 return FAIL;
3313 }
3314
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003315 // Create a partial for the instance or class method
3316 if (obj_method_to_partial_tv(is_object ? obj : NULL, oc_method, tv)
3317 == FAIL)
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003318 return FAIL;
3319
3320 return OK;
3321}
3322
3323/*
64-bitmane08f10a2025-03-18 22:14:34 +01003324 * do ISN_PUT or ISN_IPUT instruction depending on fixindent parameter
3325 */
3326 static void
3327isn_put_do(ectx_T *ectx, isn_T *iptr, typval_T *tv, int fixindent)
3328{
3329 int regname = iptr->isn_arg.put.put_regname;
3330 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3331 char_u *expr = NULL;
3332 int dir = FORWARD;
3333
3334 if (lnum < -2)
3335 {
3336 // line number was put on the stack by ISN_RANGE
3337 tv = STACK_TV_BOT(-1);
3338 curwin->w_cursor.lnum = tv->vval.v_number;
3339 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3340 dir = BACKWARD;
3341 --ectx->ec_stack.ga_len;
3342 }
3343 else if (lnum == -2)
3344 // :put! above cursor
3345 dir = BACKWARD;
3346 else if (lnum >= 0)
3347 {
3348 curwin->w_cursor.lnum = lnum;
3349 if (lnum == 0)
3350 // check_cursor() below will move to line 1
3351 dir = BACKWARD;
3352 }
3353
3354 if (regname == '=')
3355 {
3356 tv = STACK_TV_BOT(-1);
3357 if (tv->v_type == VAR_STRING)
3358 expr = tv->vval.v_string;
3359 else
3360 {
3361 expr = typval2string(tv, TRUE); // allocates value
3362 clear_tv(tv);
3363 }
3364 --ectx->ec_stack.ga_len;
3365 }
3366 check_cursor();
3367
3368 if (fixindent)
3369 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE|PUT_FIXINDENT);
3370 else
3371 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3372 vim_free(expr);
3373}
3374
3375/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003376 * Execute the ISN_UNPACK instruction for a List
3377 */
3378 static int
3379exec_unpack_list(ectx_T *ectx, isn_T *iptr, typval_T *tv)
3380{
3381 int count = iptr->isn_arg.unpack.unp_count;
3382 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3383 list_T *l;
3384 listitem_T *li;
3385 int i;
3386
3387 l = tv->vval.v_list;
3388 if (l == NULL
3389 || l->lv_len < (semicolon ? count - 1 : count))
3390 {
3391 SOURCING_LNUM = iptr->isn_lnum;
3392 emsg(_(e_list_value_does_not_have_enough_items));
3393 return EXEC_FAIL;
3394 }
3395 else if (!semicolon && l->lv_len > count)
3396 {
3397 SOURCING_LNUM = iptr->isn_lnum;
3398 emsg(_(e_list_value_has_more_items_than_targets));
3399 return EXEC_FAIL;
3400 }
3401
3402 CHECK_LIST_MATERIALIZE(l);
3403 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
3404 return EXEC_DONE;
3405 ectx->ec_stack.ga_len += count - 1;
3406
3407 // Variable after semicolon gets a list with the remaining
3408 // items.
3409 if (semicolon)
3410 {
3411 list_T *rem_list =
3412 list_alloc_with_items(l->lv_len - count + 1);
3413
3414 if (rem_list == NULL)
3415 return EXEC_DONE;
3416 tv = STACK_TV_BOT(-count);
3417 tv->vval.v_list = rem_list;
3418 ++rem_list->lv_refcount;
3419 tv->v_lock = 0;
3420 li = l->lv_first;
3421 for (i = 0; i < count - 1; ++i)
3422 li = li->li_next;
3423 for (i = 0; li != NULL; ++i)
3424 {
3425 typval_T tvcopy;
3426
3427 copy_tv(&li->li_tv, &tvcopy);
3428 list_set_item(rem_list, i, &tvcopy);
3429 li = li->li_next;
3430 }
3431 --count;
3432 }
3433
3434 // Produce the values in reverse order, first item last.
3435 li = l->lv_first;
3436 for (i = 0; i < count; ++i)
3437 {
3438 tv = STACK_TV_BOT(-i - 1);
3439 copy_tv(&li->li_tv, tv);
3440 li = li->li_next;
3441 }
3442
3443 list_unref(l);
3444
3445 return EXEC_OK;
3446}
3447
3448/*
3449 * Execute the ISN_UNPACK instruction for a Tuple
3450 */
3451 static int
3452exec_unpack_tuple(ectx_T *ectx, isn_T *iptr, typval_T *tv)
3453{
3454 int count = iptr->isn_arg.unpack.unp_count;
3455 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3456 tuple_T *tuple;
3457 int i;
3458
3459 tuple = tv->vval.v_tuple;
3460 if (tuple == NULL
3461 || TUPLE_LEN(tuple) < (semicolon ? count - 1 : count))
3462 {
3463 SOURCING_LNUM = iptr->isn_lnum;
3464 emsg(_(e_more_targets_than_tuple_items));
3465 return EXEC_FAIL;
3466 }
3467 else if (!semicolon && TUPLE_LEN(tuple) > count)
3468 {
3469 SOURCING_LNUM = iptr->isn_lnum;
3470 emsg(_(e_less_targets_than_tuple_items));
3471 return EXEC_FAIL;
3472 }
3473
3474 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
3475 return EXEC_DONE;
3476 ectx->ec_stack.ga_len += count - 1;
3477
3478 // Variable after semicolon gets a list with the remaining
3479 // items.
3480 if (semicolon)
3481 {
3482 list_T *rem_list =
3483 list_alloc_with_items(TUPLE_LEN(tuple) - count + 1);
3484
3485 if (rem_list == NULL)
3486 return EXEC_DONE;
3487 tv = STACK_TV_BOT(-count);
3488 tv->v_type = VAR_LIST;
3489 tv->vval.v_list = rem_list;
3490 ++rem_list->lv_refcount;
3491 tv->v_lock = 0;
3492 for (i = count - 1; i < TUPLE_LEN(tuple); ++i)
3493 {
3494 typval_T tvcopy;
3495
3496 copy_tv(TUPLE_ITEM(tuple, i), &tvcopy);
3497 list_set_item(rem_list, i - (count - 1), &tvcopy);
3498 }
3499 --count;
3500 }
3501
3502 // Produce the values in reverse order, first item last.
3503 for (i = 0; i < count; ++i)
3504 {
3505 tv = STACK_TV_BOT(-i - 1);
3506 copy_tv(TUPLE_ITEM(tuple, i), tv);
3507 }
3508
3509 tuple_unref(tuple);
3510
3511 return EXEC_OK;
3512}
3513
3514/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003515 * Execute instructions in execution context "ectx".
3516 * Return OK or FAIL;
3517 */
3518 static int
3519exec_instructions(ectx_T *ectx)
3520{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003521 int ret = FAIL;
3522 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003523 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003524
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003525 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003526 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003527
Bram Moolenaarff652882021-05-16 15:24:49 +02003528 // Only catch exceptions in this instruction list.
3529 ectx->ec_trylevel_at_start = trylevel;
3530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 for (;;)
3532 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003533 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003535 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003536
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003537 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003538 {
3539 line_breakcheck();
3540 breakcheck_count = 0;
3541 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003542 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003543 {
3544 // Turn CTRL-C into an exception.
3545 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003546 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003547 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003548 did_throw = TRUE;
3549 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003551 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003552 {
3553 // Turn an error message into an exception.
3554 did_emsg = FALSE;
3555 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003556 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003557 did_throw = TRUE;
3558 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003559
3560 // This exception was not caught (yet).
3561 garray_T *trystack = &ectx->ec_trystack;
3562 if (trystack->ga_len > 0)
3563 {
3564 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3565 + trystack->ga_len - 1;
3566 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Yee Cheng Chin2051af12025-01-09 22:14:34 +01003567 {
3568 if (trycmd->tcd_caught)
3569 {
3570 // Inside a "catch" we need to first discard the caught
3571 // exception.
3572 finish_exception(caught_stack);
3573 trycmd->tcd_caught = FALSE;
3574 }
3575 }
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003576 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003577 }
3578
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003579 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003581 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003582 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003583 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584
3585 // An exception jumps to the first catch, finally, or returns from
3586 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003587 while (index > 0)
3588 {
3589 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003590 // 1. after :try and before :catch - jump to first :catch
3591 // 2. in :catch block - jump to :finally
3592 // 3. in :catch block and no finally - jump to :endtry
3593 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3594 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003595 break;
3596 // In the catch and finally block of this try we have to go up
3597 // one level.
3598 --index;
3599 trycmd = NULL;
3600 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003601 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003603 if (trycmd->tcd_in_catch)
3604 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003605 if (trycmd->tcd_finally_idx > 0)
3606 {
3607 // exception inside ":catch", jump to ":finally" once
3608 ectx->ec_iidx = trycmd->tcd_finally_idx;
3609 trycmd->tcd_finally_idx = 0;
3610 }
3611 else
3612 {
3613 // exception inside ":catch" or ":finally", jump to
3614 // ":endtry"
3615 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3616 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003617 }
3618 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003619 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003620 // jump to first ":catch"
3621 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003622 trycmd->tcd_in_catch = TRUE;
3623 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003624 did_throw = FALSE; // don't come back here until :endtry
3625 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 }
3627 else
3628 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003629 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003630 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003631 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003632 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003633 tv = STACK_TV_BOT(0);
3634 tv->v_type = VAR_NUMBER;
3635 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003636 ++ectx->ec_stack.ga_len;
3637 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003639 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003640 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003641 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003642 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 goto done;
3644 }
3645
Bram Moolenaar4c137212021-04-19 16:48:48 +02003646 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003647 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 }
3649 continue;
3650 }
3651
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003652 /*
3653 * Big switch on the instruction. Most compilers will be turning this
3654 * into an efficient lookup table, since the "case" values are an enum
3655 * with sequential numbers. It may look ugly, but it should be the
3656 * most efficient way.
3657 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003658 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 switch (iptr->isn_type)
3660 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003661 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003662 case ISN_CONSTRUCT:
3663 // "this" is always the local variable at index zero
3664 tv = STACK_TV_VAR(0);
3665 tv->v_type = VAR_OBJECT;
3666 tv->vval.v_object = alloc_clear(
3667 iptr->isn_arg.construct.construct_size);
3668 tv->vval.v_object->obj_class =
3669 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003670 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003671 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003672 object_created(tv->vval.v_object);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01003673
3674 // When creating an enum value object, initialize the name and
3675 // ordinal object variables.
3676 class_T *en = tv->vval.v_object->obj_class;
3677 if (IS_ENUM(en))
3678 enum_set_internal_obj_vars(en, tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003679 break;
3680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 // execute Ex command line
3682 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003683 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003684 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 break;
3686
Bram Moolenaar20677332021-06-06 17:02:53 +02003687 // execute Ex command line split at NL characters.
3688 case ISN_EXEC_SPLIT:
3689 {
3690 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003691 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003692
3693 SOURCING_LNUM = iptr->isn_lnum;
3694 CLEAR_FIELD(cookie);
3695 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3696 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003697 line = get_split_sourceline(0, &cookie, 0, 0);
3698 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003699 get_split_sourceline, &cookie,
3700 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3701 == FAIL
3702 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003703 {
3704 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003705 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003706 }
3707 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003708 }
3709 break;
3710
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003711 // execute Ex command line that is only a range
3712 case ISN_EXECRANGE:
3713 {
3714 exarg_T ea;
3715 char *error = NULL;
3716
3717 CLEAR_FIELD(ea);
3718 ea.cmdidx = CMD_SIZE;
3719 ea.addr_type = ADDR_LINES;
3720 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003721 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003722 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003723 if (ea.cmd == NULL)
3724 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003725 // error is always NULL when using ADDR_LINES
3726 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003727 if (error != NULL)
3728 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003729 emsg(error);
3730 goto on_error;
3731 }
3732 }
3733 break;
3734
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003735 // Evaluate an expression with legacy syntax, push it onto the
3736 // stack.
3737 case ISN_LEGACY_EVAL:
3738 {
3739 char_u *arg = iptr->isn_arg.string;
3740 int res;
3741 int save_flags = cmdmod.cmod_flags;
3742
Bram Moolenaar35578162021-08-02 19:10:38 +02003743 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003744 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003745 tv = STACK_TV_BOT(0);
3746 init_tv(tv);
3747 cmdmod.cmod_flags |= CMOD_LEGACY;
3748 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3749 cmdmod.cmod_flags = save_flags;
3750 if (res == FAIL)
3751 goto on_error;
3752 ++ectx->ec_stack.ga_len;
3753 }
3754 break;
3755
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003756 // push typeval VAR_INSTR with instructions to be executed
3757 case ISN_INSTR:
3758 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003759 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003760 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003761 tv = STACK_TV_BOT(0);
3762 tv->vval.v_instr = ALLOC_ONE(instr_T);
3763 if (tv->vval.v_instr == NULL)
3764 goto on_error;
3765 ++ectx->ec_stack.ga_len;
3766
3767 tv->v_type = VAR_INSTR;
3768 tv->vval.v_instr->instr_ectx = ectx;
3769 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3770 }
3771 break;
3772
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003773 case ISN_SOURCE:
3774 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003775 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003776
Bram Moolenaar89445512022-04-14 12:58:23 +01003777 SOURCING_LNUM = iptr->isn_lnum;
3778 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003779 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003780 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003781 }
3782 break;
3783
Bram Moolenaar4c137212021-04-19 16:48:48 +02003784 // execute :substitute with an expression
3785 case ISN_SUBSTITUTE:
3786 {
3787 subs_T *subs = &iptr->isn_arg.subs;
3788 source_cookie_T cookie;
3789 struct subs_expr_S *save_instr = substitute_instr;
3790 struct subs_expr_S subs_instr;
3791 int res;
3792
3793 subs_instr.subs_ectx = ectx;
3794 subs_instr.subs_instr = subs->subs_instr;
3795 subs_instr.subs_status = OK;
3796 substitute_instr = &subs_instr;
3797
3798 SOURCING_LNUM = iptr->isn_lnum;
3799 // This is very much like ISN_EXEC
3800 CLEAR_FIELD(cookie);
3801 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3802 res = do_cmdline(subs->subs_cmd,
3803 getsourceline, &cookie,
3804 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3805 substitute_instr = save_instr;
3806
3807 if (res == FAIL || did_emsg
3808 || subs_instr.subs_status == FAIL)
3809 goto on_error;
3810 }
3811 break;
3812
3813 case ISN_FINISH:
3814 goto done;
3815
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003816 case ISN_REDIRSTART:
3817 // create a dummy entry for var_redir_str()
3818 if (alloc_redir_lval() == FAIL)
3819 goto on_error;
3820
3821 // The output is stored in growarray "redir_ga" until
3822 // redirection ends.
3823 init_redir_ga();
3824 redir_vname = 1;
3825 break;
3826
3827 case ISN_REDIREND:
3828 {
3829 char_u *res = get_clear_redir_ga();
3830
3831 // End redirection, put redirected text on the stack.
3832 clear_redir_lval();
3833 redir_vname = 0;
3834
Bram Moolenaar35578162021-08-02 19:10:38 +02003835 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003836 {
3837 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003838 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003839 }
3840 tv = STACK_TV_BOT(0);
3841 tv->v_type = VAR_STRING;
3842 tv->vval.v_string = res;
3843 ++ectx->ec_stack.ga_len;
3844 }
3845 break;
3846
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003847 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003848#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003849 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003850 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3851 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003852 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003853#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003854 break;
3855
3856 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003857#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003858 {
3859 exarg_T ea;
3860 int res;
3861
3862 CLEAR_FIELD(ea);
3863 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3864 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3865 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3866 --ectx->ec_stack.ga_len;
3867 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003868 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003869 res = cexpr_core(&ea, tv);
3870 clear_tv(tv);
3871 if (res == FAIL)
3872 goto on_error;
3873 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003874#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003875 break;
3876
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003877 // execute Ex command from pieces on the stack
3878 case ISN_EXECCONCAT:
3879 {
3880 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003881 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003882 int pass;
3883 int i;
3884 char_u *cmd = NULL;
3885 char_u *str;
3886
3887 for (pass = 1; pass <= 2; ++pass)
3888 {
3889 for (i = 0; i < count; ++i)
3890 {
3891 tv = STACK_TV_BOT(i - count);
3892 str = tv->vval.v_string;
3893 if (str != NULL && *str != NUL)
3894 {
3895 if (pass == 2)
3896 STRCPY(cmd + len, str);
3897 len += STRLEN(str);
3898 }
3899 if (pass == 2)
3900 clear_tv(tv);
3901 }
3902 if (pass == 1)
3903 {
3904 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003905 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003906 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003907 len = 0;
3908 }
3909 }
3910
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003911 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003912 do_cmdline_cmd(cmd);
3913 vim_free(cmd);
3914 }
3915 break;
3916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 // execute :echo {string} ...
3918 case ISN_ECHO:
3919 {
3920 int count = iptr->isn_arg.echo.echo_count;
3921 int atstart = TRUE;
3922 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003923 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924
3925 for (idx = 0; idx < count; ++idx)
3926 {
3927 tv = STACK_TV_BOT(idx - count);
3928 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3929 &atstart, &needclr);
3930 clear_tv(tv);
3931 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003932 if (needclr)
3933 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003934 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 }
3936 break;
3937
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003938 // :execute {string} ...
3939 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003940 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003941 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003942 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003943 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003944 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003945 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003946 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003947 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003948 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003949 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003950 garray_T ga;
3951 char_u buf[NUMBUFLEN];
3952 char_u *p;
3953 int len;
3954 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003955 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003956
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003957 if (iptr->isn_type == ISN_ECHOWINDOW)
3958 count = iptr->isn_arg.echowin.ewin_count;
3959 else
3960 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003961 ga_init2(&ga, 1, 80);
3962 for (idx = 0; idx < count; ++idx)
3963 {
3964 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003965 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003966 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003967 if (tv->v_type == VAR_CHANNEL
3968 || tv->v_type == VAR_JOB)
3969 {
3970 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003971 semsg(_(e_using_invalid_value_as_string_str),
3972 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003973 break;
3974 }
3975 else
3976 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003977 }
3978 else
Ernie Raelbe828252024-07-26 18:37:02 +02003979 {
3980 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003981 p = tv_stringify(tv, buf);
Ernie Raelbe828252024-07-26 18:37:02 +02003982 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003983
3984 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003985 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003986 failed = TRUE;
3987 else
3988 {
3989 if (ga.ga_len > 0)
3990 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3991 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3992 ga.ga_len += len;
3993 }
3994 clear_tv(tv);
3995 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003996 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003997 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003998 {
3999 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02004000 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01004001 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01004002
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02004003 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004004 {
4005 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02004006 {
4007 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004008 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004009 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01004010 {
4011 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004012 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01004013 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02004014 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004015 else
4016 {
4017 msg_sb_eol();
4018 if (iptr->isn_type == ISN_ECHOMSG)
4019 {
4020 msg_attr(ga.ga_data, echo_attr);
4021 out_flush();
4022 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004023#ifdef HAS_MESSAGE_WINDOW
4024 else if (iptr->isn_type == ISN_ECHOWINDOW)
4025 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004026 start_echowindow(
4027 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004028 msg_attr(ga.ga_data, echo_attr);
4029 end_echowindow();
4030 }
4031#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02004032 else if (iptr->isn_type == ISN_ECHOCONSOLE)
4033 {
4034 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
4035 TRUE);
4036 ui_write((char_u *)"\r\n", 2, TRUE);
4037 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004038 else
4039 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004040 SOURCING_LNUM = iptr->isn_lnum;
4041 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004042 }
4043 }
4044 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01004045 ga_clear(&ga);
4046 }
4047 break;
4048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 // load local variable or argument
4050 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02004051 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004052 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01004053 tv = STACK_TV_VAR(iptr->isn_arg.number);
4054 if (tv->v_type == VAR_UNKNOWN)
4055 {
4056 // missing argument or default value v:none
4057 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
4058 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
4059 }
4060 else
4061 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004062 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 break;
4064
4065 // load v: variable
4066 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02004067 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004068 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004070 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 break;
4072
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004073 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 case ISN_LOADSCRIPT:
4075 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004076 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 svar_T *sv;
4078
Bram Moolenaar6db660b2021-08-01 14:08:54 +02004079 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004080 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004081 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01004082 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02004083 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004084 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004086 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 }
4088 break;
4089
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004090 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004092 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004094 int sid = iptr->isn_arg.loadstore.ls_sid;
4095 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004096 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 if (di == NULL)
4100 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004101 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004102 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004103 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 }
4105 else
4106 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004107 if (iptr->isn_type == ISN_LOADEXPORT)
4108 {
4109 int idx = get_script_item_idx(sid, name, 0,
4110 NULL, NULL);
4111 svar_T *sv;
4112
4113 if (idx >= 0)
4114 {
4115 sv = ((svar_T *)SCRIPT_ITEM(sid)
4116 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01004117 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004118 {
4119 SOURCING_LNUM = iptr->isn_lnum;
4120 semsg(_(e_item_not_exported_in_script_str),
4121 name);
4122 goto on_error;
4123 }
4124 }
4125 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004126 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004127 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004129 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 }
4131 }
4132 break;
4133
Bram Moolenaard3aac292020-04-19 14:32:17 +02004134 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02004136 case ISN_LOADB:
4137 case ISN_LOADW:
4138 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00004140 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004141
Bram Moolenaar06b77222022-01-25 15:51:56 +00004142 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00004143 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00004144 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004145 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00004147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 break;
4149
Bram Moolenaar03290b82020-12-19 16:30:44 +01004150 // load autoload variable
4151 case ISN_LOADAUTO:
4152 {
4153 char_u *name = iptr->isn_arg.string;
4154
Bram Moolenaar35578162021-08-02 19:10:38 +02004155 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004156 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004157 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004158 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004159 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01004160 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004161 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004162 }
4163 break;
4164
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004165 // load g:/b:/w:/t: namespace
4166 case ISN_LOADGDICT:
4167 case ISN_LOADBDICT:
4168 case ISN_LOADWDICT:
4169 case ISN_LOADTDICT:
4170 {
4171 dict_T *d = NULL;
4172
4173 switch (iptr->isn_type)
4174 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004175 case ISN_LOADGDICT: d = get_globvar_dict(); break;
4176 case ISN_LOADBDICT: d = curbuf->b_vars; break;
4177 case ISN_LOADWDICT: d = curwin->w_vars; break;
4178 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004179 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004180 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004181 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004182 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004183 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004184 tv = STACK_TV_BOT(0);
4185 tv->v_type = VAR_DICT;
4186 tv->v_lock = 0;
4187 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01004188 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004189 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004190 }
4191 break;
4192
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 // load &option
4194 case ISN_LOADOPT:
4195 {
4196 typval_T optval;
4197 char_u *name = iptr->isn_arg.string;
4198
Bram Moolenaara8c17702020-04-01 21:17:24 +02004199 // This is not expected to fail, name is checked during
4200 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02004201 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004202 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004203 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004204 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004206 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 }
4208 break;
4209
4210 // load $ENV
4211 case ISN_LOADENV:
4212 {
4213 typval_T optval;
4214 char_u *name = iptr->isn_arg.string;
4215
Bram Moolenaar35578162021-08-02 19:10:38 +02004216 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004217 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004218 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004219 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004221 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 }
4223 break;
4224
4225 // load @register
4226 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02004227 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004228 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 tv = STACK_TV_BOT(0);
4230 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004231 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004232 // This may result in NULL, which should be equivalent to an
4233 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 tv->vval.v_string = get_reg_contents(
4235 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004236 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 break;
4238
4239 // store local variable
4240 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004241 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 tv = STACK_TV_VAR(iptr->isn_arg.number);
Ernie Rael9ed53752023-12-11 17:40:46 +01004243 if (check_typval_is_value(STACK_TV_BOT(0)) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004244 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004245 clear_tv(STACK_TV_BOT(0));
4246 goto on_error;
4247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 clear_tv(tv);
4249 *tv = *STACK_TV_BOT(0);
4250 break;
4251
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004252 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004253 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004254 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004255 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004256 int sid = iptr->isn_arg.loadstore.ls_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004257 char_u *name = iptr->isn_arg.loadstore.ls_name;
Ernie Rael3f821d62024-04-24 20:07:50 +02004258 dictitem_T *di = NULL;
4259 // First check for a variable from an exported autoload
4260 // with an autoload_prefix; it would be in globals.
4261 if (iptr->isn_type == ISN_STOREEXPORT)
4262 di = find_var_autoload_prefix(name, sid, NULL, NULL);
4263 // Then look for a variable in the script's variables.
4264 if (di == NULL)
4265 {
4266 hashtab_T *ht = &SCRIPT_VARS(sid);
4267 di = find_var_in_ht(ht, 0, STRNCMP("s:", name, 2) == 0
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004268 ? name + 2 : name, TRUE);
Ernie Rael3f821d62024-04-24 20:07:50 +02004269 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004270
Bram Moolenaar4c137212021-04-19 16:48:48 +02004271 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004272 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004273 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004274 {
4275 if (iptr->isn_type == ISN_STOREEXPORT)
4276 {
4277 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01004278 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004279 goto on_error;
4280 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004281 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004282 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004283 else
4284 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004285 if (iptr->isn_type == ISN_STOREEXPORT)
4286 {
4287 int idx = get_script_item_idx(sid, name, 0,
4288 NULL, NULL);
4289
Bram Moolenaar06651632022-04-27 17:54:25 +01004290 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004291 if (idx >= 0)
4292 {
4293 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
4294 ->sn_var_vals.ga_data) + idx;
4295
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01004296 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004297 {
4298 semsg(_(e_item_not_exported_in_script_str),
4299 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01004300 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004301 goto on_error;
4302 }
4303 }
4304 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02004305 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02004306 {
4307 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02004308 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02004309 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004310 clear_tv(&di->di_tv);
4311 di->di_tv = *STACK_TV_BOT(0);
4312 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004313 }
4314 break;
4315
4316 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 case ISN_STORESCRIPT:
4318 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004319 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4320 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321
Bram Moolenaar6db660b2021-08-01 14:08:54 +02004322 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004323 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004324 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004325 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02004326
4327 // "const" and "final" are checked at compile time, locking
4328 // the value needs to be checked here.
4329 SOURCING_LNUM = iptr->isn_lnum;
4330 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02004331 {
4332 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02004333 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02004334 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02004335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 clear_tv(sv->sv_tv);
4337 *sv->sv_tv = *STACK_TV_BOT(0);
4338 }
4339 break;
4340
4341 // store option
4342 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004343 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004345 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
4346 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 long n = 0;
4348 char_u *s = NULL;
4349 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00004350 char_u numbuf[NUMBUFLEN];
4351 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352
Bram Moolenaar4c137212021-04-19 16:48:48 +02004353 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02004354 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 tv = STACK_TV_BOT(0);
4356 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01004357 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01004359 if (s == NULL)
4360 s = (char_u *)"";
4361 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004362 else if (iptr->isn_type == ISN_STOREFUNCOPT)
4363 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00004364 // If the option can be set to a function reference or
4365 // a lambda and the passed value is a function
4366 // reference, then convert it to the name (string) of
4367 // the function reference.
4368 s = tv2string(tv, &tofree, numbuf, 0);
4369 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004370 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004371 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004372 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004373 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004374 goto on_error;
4375 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004378 // must be VAR_NUMBER, CHECKTYPE makes sure
4379 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004380 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02004381 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00004382 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 if (msg != NULL)
4384 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004385 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02004387 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 }
4390 break;
4391
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004392 // store $ENV
4393 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004394 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004395 tv = STACK_TV_BOT(0);
4396 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
4397 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004398 break;
4399
4400 // store @r
4401 case ISN_STOREREG:
4402 {
4403 int reg = iptr->isn_arg.number;
4404
Bram Moolenaar4c137212021-04-19 16:48:48 +02004405 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01004406 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02004407 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01004408 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004409 }
4410 break;
4411
4412 // store v: variable
4413 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004414 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004415 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
4416 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004417 // should not happen, type is checked when compiling
4418 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004419 break;
4420
Bram Moolenaard3aac292020-04-19 14:32:17 +02004421 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02004423 case ISN_STOREB:
4424 case ISN_STOREW:
4425 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004427 dictitem_T *di;
4428 hashtab_T *ht;
4429 char_u *name = iptr->isn_arg.string + 2;
4430
Bram Moolenaard3aac292020-04-19 14:32:17 +02004431 switch (iptr->isn_type)
4432 {
4433 case ISN_STOREG:
4434 ht = get_globvar_ht();
4435 break;
4436 case ISN_STOREB:
4437 ht = &curbuf->b_vars->dv_hashtab;
4438 break;
4439 case ISN_STOREW:
4440 ht = &curwin->w_vars->dv_hashtab;
4441 break;
4442 case ISN_STORET:
4443 ht = &curtab->tp_vars->dv_hashtab;
4444 break;
4445 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004446 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448
Bram Moolenaar4c137212021-04-19 16:48:48 +02004449 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004450 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004452 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 else
4454 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004455 SOURCING_LNUM = iptr->isn_lnum;
4456 if (var_check_permission(di, name) == FAIL)
4457 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 clear_tv(&di->di_tv);
4459 di->di_tv = *STACK_TV_BOT(0);
4460 }
4461 }
4462 break;
4463
Bram Moolenaar03290b82020-12-19 16:30:44 +01004464 // store an autoload variable
4465 case ISN_STOREAUTO:
4466 SOURCING_LNUM = iptr->isn_lnum;
4467 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
4468 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004469 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004470 break;
4471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 // store number in local variable
4473 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004474 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 clear_tv(tv);
4476 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004477 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 break;
4479
Bram Moolenaar590162c2022-12-24 21:24:06 +00004480 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004481 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004482 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004483 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004484
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004485 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004486 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004487 if (res == NOTDONE)
4488 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004489 }
4490 break;
4491
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004492 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004493 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004494 if (execute_storerange(iptr, ectx) == FAIL)
4495 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004496 break;
4497
Bram Moolenaard505d172022-12-18 21:42:55 +00004498 case ISN_LOAD_CLASSMEMBER:
4499 {
4500 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4501 goto theend;
4502 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004503 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4504 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004505 ++ectx->ec_stack.ga_len;
4506 }
4507 break;
4508
4509 case ISN_STORE_CLASSMEMBER:
4510 {
4511 classmember_T *cm = &iptr->isn_arg.classmember;
4512 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4513 clear_tv(tv);
4514 *tv = *STACK_TV_BOT(-1);
4515 --ectx->ec_stack.ga_len;
4516 }
4517 break;
4518
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004519 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004520 case ISN_LOADOUTER:
4521 case ISN_STOREOUTER:
4522 {
4523 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004524 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4525 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004526
4527 while (depth > 1 && outer != NULL)
4528 {
4529 outer = outer->out_up;
4530 --depth;
4531 }
4532 if (outer == NULL)
4533 {
4534 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004535 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4536 || ectx->ec_outer_ref == NULL)
4537 // Possibly :def function called from legacy
4538 // context.
4539 emsg(_(e_closure_called_from_invalid_context));
4540 else
4541 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004542 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004543 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004544 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004545 // Variable declared in loop. May be copied if the
4546 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004547 tv = ((typval_T *)outer->out_loop[-depth - 1]
4548 .stack->ga_data)
4549 + outer->out_loop[-depth - 1].var_idx
4550 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004551 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004552 // Variable declared in a function. May be copied if
4553 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004554 tv = ((typval_T *)outer->out_stack->ga_data)
4555 + outer->out_frame_idx + STACK_FRAME_SIZE
4556 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004557 if (iptr->isn_type == ISN_LOADOUTER)
4558 {
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004559 typval_T *copy;
Bram Moolenaar35578162021-08-02 19:10:38 +02004560 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004561 goto theend;
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004562 // careful: ga_grow_inner may re-alloc the stack
4563 if (depth < 0)
4564 copy = ((typval_T *)outer->out_loop[-depth - 1]
4565 .stack->ga_data)
4566 + outer->out_loop[-depth - 1].var_idx
4567 + iptr->isn_arg.outer.outer_idx;
4568 else
4569 copy = ((typval_T *)outer->out_stack->ga_data)
4570 + outer->out_frame_idx + STACK_FRAME_SIZE
4571 + iptr->isn_arg.outer.outer_idx;
4572 // memory was freed, get tv again
4573 if (copy != tv)
4574 tv = copy;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004575 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004576 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004577 }
4578 else
4579 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004580 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004581 clear_tv(tv);
4582 *tv = *STACK_TV_BOT(0);
4583 }
4584 }
4585 break;
4586
Bram Moolenaar752fc692021-01-04 21:57:11 +01004587 // unlet item in list or dict variable
4588 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004589 if (execute_unletindex(iptr, ectx) == FAIL)
4590 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004591 break;
4592
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004593 // unlet range of items in list variable
4594 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004595 if (execute_unletrange(iptr, ectx) == FAIL)
4596 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004597 break;
4598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 // push constant
4600 case ISN_PUSHNR:
4601 case ISN_PUSHBOOL:
4602 case ISN_PUSHSPEC:
4603 case ISN_PUSHF:
4604 case ISN_PUSHS:
4605 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004606 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004607 case ISN_PUSHCHANNEL:
4608 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004609 case ISN_PUSHOBJ:
4610 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004611 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004612 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004614 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004615 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 switch (iptr->isn_type)
4617 {
4618 case ISN_PUSHNR:
4619 tv->v_type = VAR_NUMBER;
4620 tv->vval.v_number = iptr->isn_arg.number;
4621 break;
4622 case ISN_PUSHBOOL:
4623 tv->v_type = VAR_BOOL;
4624 tv->vval.v_number = iptr->isn_arg.number;
4625 break;
4626 case ISN_PUSHSPEC:
4627 tv->v_type = VAR_SPECIAL;
4628 tv->vval.v_number = iptr->isn_arg.number;
4629 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 case ISN_PUSHF:
4631 tv->v_type = VAR_FLOAT;
4632 tv->vval.v_float = iptr->isn_arg.fnumber;
4633 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 case ISN_PUSHBLOB:
4635 blob_copy(iptr->isn_arg.blob, tv);
4636 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004637 case ISN_PUSHFUNC:
4638 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004639 if (iptr->isn_arg.string == NULL)
4640 tv->vval.v_string = NULL;
4641 else
4642 tv->vval.v_string =
4643 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004644 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004645 case ISN_PUSHCHANNEL:
4646#ifdef FEAT_JOB_CHANNEL
4647 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004648 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004649#endif
4650 break;
4651 case ISN_PUSHJOB:
4652#ifdef FEAT_JOB_CHANNEL
4653 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004654 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004655#endif
4656 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004657 case ISN_PUSHOBJ:
4658 tv->v_type = VAR_OBJECT;
4659 tv->vval.v_object = NULL;
4660 break;
4661 case ISN_PUSHCLASS:
4662 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004663 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004664 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 default:
4666 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004667 tv->vval.v_string = iptr->isn_arg.string == NULL
4668 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 }
4670 break;
4671
Bram Moolenaar06b77222022-01-25 15:51:56 +00004672 case ISN_AUTOLOAD:
4673 {
4674 char_u *name = iptr->isn_arg.string;
4675
4676 (void)script_autoload(name, FALSE);
4677 if (find_func(name, TRUE))
4678 {
4679 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4680 goto theend;
4681 tv = STACK_TV_BOT(0);
4682 tv->v_lock = 0;
4683 ++ectx->ec_stack.ga_len;
4684 tv->v_type = VAR_FUNC;
4685 tv->vval.v_string = vim_strsave(name);
4686 }
4687 else
4688 {
4689 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4690
4691 if (res == NOTDONE)
4692 goto theend;
4693 if (res == FAIL)
4694 goto on_error;
4695 }
4696 }
4697 break;
4698
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004699 case ISN_UNLET:
4700 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4701 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004702 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004703 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004704 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004705 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004706 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004707
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004708 case ISN_LOCKUNLOCK:
4709 {
Ernie Raelee865f32023-09-29 19:53:55 +02004710#ifdef LOG_LOCKVAR
4711 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4712 iptr->isn_arg.string);
4713#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004714 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004715
4716 // Stack has the local variable, argument the whole :lock
4717 // or :unlock command, like ISN_EXEC.
4718 --ectx->ec_stack.ga_len;
Zoltan Arpadffy7b12ac32024-12-29 09:50:20 +01004719 lval_root_T root;
4720 root.lr_tv = STACK_TV_BOT(0);
4721 root.lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec;
4722 root.lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg;
Ernie Rael64885642023-10-04 20:16:22 +02004723 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004724 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004725 iptr->isn_arg.lockunlock.lu_string);
4726 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004727 lval_root = lval_root_save;
4728 if (res == FAIL)
4729 goto on_error;
4730 }
4731 break;
4732
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004733 case ISN_LOCKCONST:
4734 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4735 break;
4736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 // create a list from items on the stack; uses a single allocation
4738 // for the list header and the items
4739 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004740 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004741 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 break;
4743
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004744 // create a tuple from items on the stack
4745 case ISN_NEWTUPLE:
4746 if (exe_newtuple(iptr->isn_arg.number, ectx) == FAIL)
4747 goto theend;
4748 break;
4749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750 // create a dict from items on the stack
4751 case ISN_NEWDICT:
4752 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004753 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004755 SOURCING_LNUM = iptr->isn_lnum;
4756 res = exe_newdict(iptr->isn_arg.number, ectx);
4757 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004758 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004759 if (res == MAYBE)
4760 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 }
4762 break;
4763
LemonBoy372bcce2022-04-25 12:43:20 +01004764 case ISN_CONCAT:
4765 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4766 goto theend;
4767 break;
4768
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004769 // create a partial with NULL value
4770 case ISN_NEWPARTIAL:
4771 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4772 goto theend;
4773 ++ectx->ec_stack.ga_len;
4774 tv = STACK_TV_BOT(-1);
4775 tv->v_type = VAR_PARTIAL;
4776 tv->v_lock = 0;
4777 tv->vval.v_partial = NULL;
4778 break;
4779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 // call a :def function
4781 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004782 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004783 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4784 NULL,
4785 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004786 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004787 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 break;
4789
Bram Moolenaard0200c82023-01-28 15:19:40 +00004790 // call a method on an interface
4791 case ISN_METHODCALL:
4792 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004793 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4794
Bram Moolenaard0200c82023-01-28 15:19:40 +00004795 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004796 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004797 if (tv->v_type != VAR_OBJECT)
4798 {
4799 object_required_error(tv);
4800 goto on_error;
4801 }
Ernie Raelbe828252024-07-26 18:37:02 +02004802
Bram Moolenaard0200c82023-01-28 15:19:40 +00004803 object_T *obj = tv->vval.v_object;
Ernie Raelbe828252024-07-26 18:37:02 +02004804 if (obj == NULL)
4805 {
4806 emsg(_(e_using_null_object));
4807 goto on_error;
4808 }
4809
Ernie Rael58c95792024-08-13 23:27:22 +02004810 ufunc_T *ufunc;
4811 if (mfunc->cmf_is_super)
4812 // Doing "super.Func", use the specific ufunc.
4813 ufunc = mfunc->cmf_itf->class_obj_methods[
4814 mfunc->cmf_idx];
4815 else
4816 {
4817 class_T *cl = obj->obj_class;
Bram Moolenaard0200c82023-01-28 15:19:40 +00004818
Ernie Rael58c95792024-08-13 23:27:22 +02004819 // convert the interface index to the object index
4820 int idx = object_index_from_itf_index(mfunc->cmf_itf,
4821 TRUE, mfunc->cmf_idx, cl);
4822 ufunc = cl->class_obj_methods[idx];
4823 }
Bram Moolenaard0200c82023-01-28 15:19:40 +00004824
Ernie Rael58c95792024-08-13 23:27:22 +02004825 if (call_ufunc(ufunc, NULL, mfunc->cmf_argcount, ectx,
4826 NULL, NULL) == FAIL)
Bram Moolenaard0200c82023-01-28 15:19:40 +00004827 goto on_error;
4828 }
4829 break;
4830
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831 // call a builtin function
4832 case ISN_BCALL:
4833 SOURCING_LNUM = iptr->isn_lnum;
4834 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4835 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004836 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004837 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 break;
4839
4840 // call a funcref or partial
4841 case ISN_PCALL:
4842 {
4843 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4844 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004845 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846
4847 SOURCING_LNUM = iptr->isn_lnum;
4848 if (pfunc->cpf_top)
4849 {
4850 // funcref is above the arguments
4851 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4852 }
4853 else
4854 {
4855 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004856 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004857 partial_tv = *STACK_TV_BOT(0);
4858 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004860 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004861 if (tv == &partial_tv)
4862 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004864 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 }
4866 break;
4867
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004868 case ISN_PCALL_END:
4869 // PCALL finished, arguments have been consumed and replaced by
4870 // the return value. Now clear the funcref from the stack,
4871 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004872 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004873 clear_tv(STACK_TV_BOT(-1));
4874 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4875 break;
4876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 // call a user defined function or funcref/partial
4878 case ISN_UCALL:
4879 {
4880 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4881
4882 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004883 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004884 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004885 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 }
4887 break;
4888
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004889 // :defer func(arg)
4890 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004891 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004892 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4893 goto on_error;
4894 break;
4895
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004896 // Return from a :def function call without a value.
4897 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004898 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004899 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004900 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004901 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004902 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004903 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004904 if (iptr->isn_type == ISN_RETURN_VOID)
4905 {
4906 tv->v_type = VAR_VOID;
4907 tv->vval.v_number = 0;
4908 tv->v_lock = 0;
4909 }
4910 else
4911 {
4912 *tv = *STACK_TV_VAR(0);
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01004913 object_T *obj = tv->vval.v_object;
4914 ++obj->obj_refcount;
4915
4916 // Lock all the constant object variables
4917 obj_lock_const_vars(obj);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004918 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004919 // FALLTHROUGH
4920
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004921 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922 case ISN_RETURN:
4923 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004924 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004925 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004926
Ernie Raelb077b582023-12-14 20:11:44 +01004927 ///////////////////////////////////////////////////
4928 // TODO: If FAIL, line number in output not correct
4929 ///////////////////////////////////////////////////
4930 if (check_typval_is_value(STACK_TV_BOT(-1)) == FAIL)
4931 goto theend;
4932
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004933 if (trystack->ga_len > 0)
4934 trycmd = ((trycmd_T *)trystack->ga_data)
4935 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004936 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004937 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004939 // jump to ":finally" or ":endtry"
4940 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004941 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004942 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004943 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 trycmd->tcd_return = TRUE;
4945 }
4946 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004947 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 }
4949 break;
4950
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004951 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952 case ISN_FUNCREF:
4953 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004954 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4955 ufunc_T *ufunc;
4956 funcref_T *funcref = &iptr->isn_arg.funcref;
4957 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004960 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004961 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004962 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004963 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004964 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004965 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004966 if (extra != NULL && extra->fre_class != NULL)
4967 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004968 class_T *cl;
4969 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004970 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004971 tv = STACK_TV_BOT(-1);
4972 if (tv->v_type != VAR_OBJECT)
4973 {
Ernie Raelbe828252024-07-26 18:37:02 +02004974 SOURCING_LNUM = iptr->isn_lnum;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004975 object_required_error(tv);
4976 vim_free(pt);
4977 goto on_error;
4978 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004979
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004980 object_T *obj = tv->vval.v_object;
Ernie Raelbe828252024-07-26 18:37:02 +02004981 if (obj == NULL)
4982 {
4983 SOURCING_LNUM = iptr->isn_lnum;
4984 emsg(_(e_using_null_object));
4985 vim_free(pt);
4986 goto on_error;
4987 }
4988
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004989 cl = obj->obj_class;
4990 // drop the value from the stack
4991 clear_tv(tv);
4992 --ectx->ec_stack.ga_len;
4993
4994 pt->pt_obj = obj;
4995 ++obj->obj_refcount;
4996 }
4997 else
4998 cl = extra->fre_class;
4999
5000 if (extra->fre_object_method)
5001 {
5002 // object method
5003 // convert the interface index to the object index
5004 int idx =
5005 object_index_from_itf_index(extra->fre_class,
5006 TRUE, extra->fre_method_idx, cl);
5007 ufunc = cl->class_obj_methods[idx];
5008 }
5009 else
5010 {
5011 // class method
5012 ufunc =
5013 cl->class_class_functions[extra->fre_method_idx];
5014 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00005015 }
5016 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00005017 {
5018 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
5019 + funcref->fr_dfunc_idx;
5020
5021 ufunc = pt_dfunc->df_ufunc;
5022 }
5023 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00005024 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005025 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00005026 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00005027 if (ufunc == NULL)
5028 {
5029 SOURCING_LNUM = iptr->isn_lnum;
5030 iemsg("ufunc unexpectedly NULL for FUNCREF");
Christian Brabandt915f3bf2024-04-05 20:12:19 +02005031 vim_free(pt);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00005032 goto theend;
5033 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005034 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01005035 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005036 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005037 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005039 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040 tv->vval.v_partial = pt;
5041 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02005042 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043 }
5044 break;
5045
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005046 // Create a global function from a lambda.
5047 case ISN_NEWFUNC:
5048 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005049 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005050
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005051 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01005052 arg->nfa_global, &arg->nfa_loopvar_info,
5053 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005054 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005055 }
5056 break;
5057
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005058 // List functions
5059 case ISN_DEF:
5060 if (iptr->isn_arg.string == NULL)
5061 list_functions(NULL);
5062 else
5063 {
Bram Moolenaar14336722022-01-08 16:02:59 +00005064 exarg_T ea;
5065 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005066
5067 CLEAR_FIELD(ea);
5068 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00005069 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01005070 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02005071 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00005072 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005073 }
5074 break;
5075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 // jump if a condition is met
5077 case ISN_JUMP:
5078 {
5079 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005080 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 int jump = TRUE;
5082
5083 if (when != JUMP_ALWAYS)
5084 {
5085 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005086 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02005087 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005088 || when == JUMP_IF_COND_TRUE)
5089 {
5090 SOURCING_LNUM = iptr->isn_lnum;
5091 jump = tv_get_bool_chk(tv, &error);
5092 if (error)
5093 goto on_error;
5094 }
5095 else
5096 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005097 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01005099 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 {
5101 // drop the value from the stack
5102 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005103 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 }
5105 }
5106 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005107 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108 }
5109 break;
5110
Bram Moolenaarb46c0832022-09-15 17:19:37 +01005111 // "while": jump to end if a condition is false
5112 case ISN_WHILE:
5113 {
5114 int error = FALSE;
5115 int jump = TRUE;
5116
5117 tv = STACK_TV_BOT(-1);
5118 SOURCING_LNUM = iptr->isn_lnum;
5119 jump = !tv_get_bool_chk(tv, &error);
5120 if (error)
5121 goto on_error;
5122 // drop the value from the stack
5123 clear_tv(tv);
5124 --ectx->ec_stack.ga_len;
5125 if (jump)
5126 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
5127
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01005128 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01005129 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01005130 tv = STACK_TV_VAR(
5131 iptr->isn_arg.whileloop.while_funcref_idx);
5132 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
5133 }
5134 break;
5135
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02005136 // Jump if an argument with a default value was already set and not
5137 // v:none.
5138 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00005139 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02005140 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00005141 int arg_set = tv->v_type != VAR_UNKNOWN
5142 && !(tv->v_type == VAR_SPECIAL
5143 && tv->vval.v_number == VVAL_NONE);
Yegappan Lakshmananb04af4c2025-01-03 10:50:08 +01005144
5145 if (iptr->isn_type == ISN_JUMP_IF_ARG_NOT_SET && !arg_set)
5146 {
5147 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5148 + ectx->ec_dfunc_idx;
5149 ufunc_T *ufunc = df->df_ufunc;
5150 // jump_arg_off is negative for arguments
5151 size_t argidx = ufunc->uf_def_args.ga_len
5152 + iptr->isn_arg.jumparg.jump_arg_off
5153 + STACK_FRAME_SIZE;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005154 type_T *tuple = ufunc->uf_arg_types[argidx];
Yegappan Lakshmanan2050dcc2025-01-06 18:34:49 +01005155 CLEAR_POINTER(tv);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005156 tv->v_type = tuple->tt_type;
Yegappan Lakshmananb04af4c2025-01-03 10:50:08 +01005157 }
5158
Bram Moolenaar65b0d162022-12-13 18:43:22 +00005159 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005160 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02005161 break;
5162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163 // top of a for loop
5164 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00005165 if (execute_for(iptr, ectx) == FAIL)
5166 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 break;
5168
Bram Moolenaarb46c0832022-09-15 17:19:37 +01005169 // end of a for or while loop
5170 case ISN_ENDLOOP:
5171 if (execute_endloop(iptr, ectx) == FAIL)
5172 goto theend;
5173 break;
5174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005175 // start of ":try" block
5176 case ISN_TRY:
5177 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005178 trycmd_T *trycmd = NULL;
5179
Bram Moolenaar35578162021-08-02 19:10:38 +02005180 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005181 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005182 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
5183 + ectx->ec_trystack.ga_len;
5184 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005185 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01005186 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005187 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
5188 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01005189 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00005190 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01005191 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00005192 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01005193 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00005194 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195 }
5196 break;
5197
5198 case ISN_PUSHEXC:
5199 if (current_exception == NULL)
5200 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005201 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005203 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 }
Bram Moolenaar35578162021-08-02 19:10:38 +02005205 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005206 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005208 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02005210 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 tv->vval.v_string = vim_strsave(
5212 (char_u *)current_exception->value);
5213 break;
5214
5215 case ISN_CATCH:
5216 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005217 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01005218 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219
Bram Moolenaar4c137212021-04-19 16:48:48 +02005220 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01005221 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01005223 trycmd->tcd_caught = TRUE;
5224 trycmd->tcd_did_throw = FALSE;
5225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005226 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01005227 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 catch_exception(current_exception);
5229 }
5230 break;
5231
Bram Moolenaarc150c092021-02-13 15:02:46 +01005232 case ISN_TRYCONT:
5233 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005234 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01005235 trycont_T *trycont = &iptr->isn_arg.trycont;
5236 int i;
5237 trycmd_T *trycmd;
5238 int iidx = trycont->tct_where;
5239
5240 if (trystack->ga_len < trycont->tct_levels)
5241 {
5242 siemsg("TRYCONT: expected %d levels, found %d",
5243 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005244 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01005245 }
5246 // Make :endtry jump to any outer try block and the last
5247 // :endtry inside the loop to the loop start.
5248 for (i = trycont->tct_levels; i > 0; --i)
5249 {
5250 trycmd = ((trycmd_T *)trystack->ga_data)
5251 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01005252 // Add one to tcd_cont to be able to jump to
5253 // instruction with index zero.
5254 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01005255 iidx = trycmd->tcd_finally_idx == 0
5256 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01005257 }
5258 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02005259 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01005260 }
5261 break;
5262
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01005263 case ISN_FINALLY:
5264 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005265 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01005266 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
5267 + trystack->ga_len - 1;
5268
5269 // Reset the index to avoid a return statement jumps here
5270 // again.
5271 trycmd->tcd_finally_idx = 0;
Yee Cheng Chin2051af12025-01-09 22:14:34 +01005272 if (trycmd->tcd_caught)
5273 {
5274 // discard the exception
5275 finish_exception(caught_stack);
5276 trycmd->tcd_caught = FALSE;
5277 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01005278 break;
5279 }
5280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005281 // end of ":try" block
5282 case ISN_ENDTRY:
5283 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005284 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01005285 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286
Bram Moolenaar06651632022-04-27 17:54:25 +01005287 --trystack->ga_len;
5288 --trylevel;
5289 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
5290 if (trycmd->tcd_did_throw)
5291 did_throw = TRUE;
Yee Cheng Chin2051af12025-01-09 22:14:34 +01005292 if (trycmd->tcd_caught)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005293 {
Bram Moolenaar06651632022-04-27 17:54:25 +01005294 // discard the exception
Yee Cheng Chin2051af12025-01-09 22:14:34 +01005295 finish_exception(caught_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296 }
Bram Moolenaar06651632022-04-27 17:54:25 +01005297
5298 if (trycmd->tcd_return)
5299 goto func_return;
5300
5301 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
5302 {
5303 --ectx->ec_stack.ga_len;
5304 clear_tv(STACK_TV_BOT(0));
5305 }
5306 if (trycmd->tcd_cont != 0)
5307 // handling :continue: jump to outer try block or
5308 // start of the loop
5309 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310 }
5311 break;
5312
5313 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01005314 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005315 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02005316
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005317 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
5318 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01005319 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005320 // the function to abort but not display an error.
5321 tv = STACK_TV_BOT(-1);
5322 clear_tv(tv);
5323 tv->v_type = VAR_NUMBER;
5324 tv->vval.v_number = 0;
5325 goto done;
5326 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005327 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005328 tv = STACK_TV_BOT(0);
5329 if (tv->vval.v_string == NULL
5330 || *skipwhite(tv->vval.v_string) == NUL)
5331 {
5332 vim_free(tv->vval.v_string);
5333 SOURCING_LNUM = iptr->isn_lnum;
5334 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005335 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005336 }
5337
5338 // Inside a "catch" we need to first discard the caught
5339 // exception.
5340 if (trystack->ga_len > 0)
5341 {
5342 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
5343 + trystack->ga_len - 1;
Yee Cheng Chin2051af12025-01-09 22:14:34 +01005344 if (trycmd->tcd_caught)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005345 {
5346 // discard the exception
Yee Cheng Chin2051af12025-01-09 22:14:34 +01005347 finish_exception(caught_stack);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005348 trycmd->tcd_caught = FALSE;
5349 }
5350 }
5351
Bram Moolenaar90a57162022-02-12 14:23:17 +00005352 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005353 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
5354 == FAIL)
5355 {
5356 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005357 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005358 }
5359 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 break;
5362
5363 // compare with special values
5364 case ISN_COMPAREBOOL:
5365 case ISN_COMPARESPECIAL:
5366 {
5367 typval_T *tv1 = STACK_TV_BOT(-2);
5368 typval_T *tv2 = STACK_TV_BOT(-1);
5369 varnumber_T arg1 = tv1->vval.v_number;
5370 varnumber_T arg2 = tv2->vval.v_number;
5371 int res;
5372
Bram Moolenaar06651632022-04-27 17:54:25 +01005373 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
5374 res = arg1 == arg2;
5375 else
5376 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377
Bram Moolenaar4c137212021-04-19 16:48:48 +02005378 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005379 tv1->v_type = VAR_BOOL;
5380 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5381 }
5382 break;
5383
Bram Moolenaar7a222242022-03-01 19:23:24 +00005384 case ISN_COMPARENULL:
5385 {
5386 typval_T *tv1 = STACK_TV_BOT(-2);
5387 typval_T *tv2 = STACK_TV_BOT(-1);
5388 int res;
5389
5390 res = typval_compare_null(tv1, tv2);
5391 if (res == MAYBE)
5392 goto on_error;
5393 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
5394 res = !res;
5395 clear_tv(tv1);
5396 clear_tv(tv2);
5397 --ectx->ec_stack.ga_len;
5398 tv1->v_type = VAR_BOOL;
5399 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5400 }
5401 break;
5402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403 // Operation with two number arguments
5404 case ISN_OPNR:
5405 case ISN_COMPARENR:
5406 {
5407 typval_T *tv1 = STACK_TV_BOT(-2);
5408 typval_T *tv2 = STACK_TV_BOT(-1);
5409 varnumber_T arg1 = tv1->vval.v_number;
5410 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02005411 varnumber_T res = 0;
5412 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005413
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005414 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
5415 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
5416 {
5417 if (arg2 < 0)
5418 {
5419 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00005420 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005421 goto on_error;
5422 }
5423 }
5424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005425 switch (iptr->isn_arg.op.op_type)
5426 {
5427 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02005428 case EXPR_DIV: if (arg2 == 0)
5429 div_zero = TRUE;
5430 else
5431 res = arg1 / arg2;
5432 break;
5433 case EXPR_REM: if (arg2 == 0)
5434 div_zero = TRUE;
5435 else
5436 res = arg1 % arg2;
5437 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005438 case EXPR_SUB: res = arg1 - arg2; break;
5439 case EXPR_ADD: res = arg1 + arg2; break;
5440
5441 case EXPR_EQUAL: res = arg1 == arg2; break;
5442 case EXPR_NEQUAL: res = arg1 != arg2; break;
5443 case EXPR_GREATER: res = arg1 > arg2; break;
5444 case EXPR_GEQUAL: res = arg1 >= arg2; break;
5445 case EXPR_SMALLER: res = arg1 < arg2; break;
5446 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005447 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
5448 res = 0;
5449 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01005450 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005451 break;
5452 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
5453 res = 0;
5454 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01005455 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005456 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02005457 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458 }
5459
Bram Moolenaar4c137212021-04-19 16:48:48 +02005460 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005461 if (iptr->isn_type == ISN_COMPARENR)
5462 {
5463 tv1->v_type = VAR_BOOL;
5464 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5465 }
5466 else
5467 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02005468 if (div_zero)
5469 {
5470 SOURCING_LNUM = iptr->isn_lnum;
5471 emsg(_(e_divide_by_zero));
5472 goto on_error;
5473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474 }
5475 break;
5476
5477 // Computation with two float arguments
5478 case ISN_OPFLOAT:
5479 case ISN_COMPAREFLOAT:
5480 {
5481 typval_T *tv1 = STACK_TV_BOT(-2);
5482 typval_T *tv2 = STACK_TV_BOT(-1);
5483 float_T arg1 = tv1->vval.v_float;
5484 float_T arg2 = tv2->vval.v_float;
5485 float_T res = 0;
5486 int cmp = FALSE;
5487
5488 switch (iptr->isn_arg.op.op_type)
5489 {
5490 case EXPR_MULT: res = arg1 * arg2; break;
5491 case EXPR_DIV: res = arg1 / arg2; break;
5492 case EXPR_SUB: res = arg1 - arg2; break;
5493 case EXPR_ADD: res = arg1 + arg2; break;
5494
5495 case EXPR_EQUAL: cmp = arg1 == arg2; break;
5496 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
5497 case EXPR_GREATER: cmp = arg1 > arg2; break;
5498 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
5499 case EXPR_SMALLER: cmp = arg1 < arg2; break;
5500 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
5501 default: cmp = 0; break;
5502 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005503 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005504 if (iptr->isn_type == ISN_COMPAREFLOAT)
5505 {
5506 tv1->v_type = VAR_BOOL;
5507 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
5508 }
5509 else
5510 tv1->vval.v_float = res;
5511 }
5512 break;
5513
5514 case ISN_COMPARELIST:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005515 case ISN_COMPARETUPLE:
Bram Moolenaar265f8112021-12-19 12:33:05 +00005516 case ISN_COMPAREDICT:
5517 case ISN_COMPAREFUNC:
5518 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005520 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005521 {
5522 typval_T *tv1 = STACK_TV_BOT(-2);
5523 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00005524 exprtype_T exprtype = iptr->isn_arg.op.op_type;
5525 int ic = iptr->isn_arg.op.op_ic;
5526 int res = FALSE;
5527 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005528
Bram Moolenaar265f8112021-12-19 12:33:05 +00005529 SOURCING_LNUM = iptr->isn_lnum;
5530 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005532 status = typval_compare_list(tv1, tv2,
5533 exprtype, ic, &res);
5534 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005535 else if (iptr->isn_type == ISN_COMPARETUPLE)
5536 {
5537 status = typval_compare_tuple(tv1, tv2,
5538 exprtype, ic, &res);
5539 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00005540 else if (iptr->isn_type == ISN_COMPAREDICT)
5541 {
5542 status = typval_compare_dict(tv1, tv2,
5543 exprtype, ic, &res);
5544 }
5545 else if (iptr->isn_type == ISN_COMPAREFUNC)
5546 {
5547 status = typval_compare_func(tv1, tv2,
5548 exprtype, ic, &res);
5549 }
5550 else if (iptr->isn_type == ISN_COMPARESTRING)
5551 {
5552 status = typval_compare_string(tv1, tv2,
5553 exprtype, ic, &res);
5554 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005555 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005556 {
5557 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005558 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005559 else // ISN_COMPAREOBJECT
5560 {
5561 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005562 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005563 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005564 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565 clear_tv(tv1);
5566 clear_tv(tv2);
5567 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005568 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5569 if (status == FAIL)
5570 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005571 }
5572 break;
5573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005574 case ISN_COMPAREANY:
5575 {
5576 typval_T *tv1 = STACK_TV_BOT(-2);
5577 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005578 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005580 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581
Bram Moolenaareb26f432020-09-14 16:50:05 +02005582 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005583 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005585 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005586 if (status == FAIL)
5587 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588 }
5589 break;
5590
5591 case ISN_ADDLIST:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005592 case ISN_ADDTUPLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005593 case ISN_ADDBLOB:
5594 {
5595 typval_T *tv1 = STACK_TV_BOT(-2);
5596 typval_T *tv2 = STACK_TV_BOT(-1);
5597
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005598 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005599 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005600 {
5601 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5602 && tv1->vval.v_list != NULL)
5603 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5604 NULL);
5605 else
5606 eval_addlist(tv1, tv2);
5607 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005608 else if (iptr->isn_type == ISN_ADDTUPLE)
5609 eval_addtuple(tv1, tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005610 else
5611 eval_addblob(tv1, tv2);
5612 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005613 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005614 }
5615 break;
5616
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005617 case ISN_LISTAPPEND:
5618 {
5619 typval_T *tv1 = STACK_TV_BOT(-2);
5620 typval_T *tv2 = STACK_TV_BOT(-1);
5621 list_T *l = tv1->vval.v_list;
5622
5623 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005624 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005625 if (l == NULL)
5626 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005627 emsg(_(e_cannot_add_to_null_list));
5628 goto on_error;
5629 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005630 if (value_check_lock(l->lv_lock, NULL, FALSE))
5631 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005632 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005633 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005634 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005635 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005636 }
5637 break;
5638
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005639 case ISN_BLOBAPPEND:
5640 {
5641 typval_T *tv1 = STACK_TV_BOT(-2);
5642 typval_T *tv2 = STACK_TV_BOT(-1);
5643 blob_T *b = tv1->vval.v_blob;
5644 int error = FALSE;
5645 varnumber_T n;
5646
5647 // add a number to a blob
5648 if (b == NULL)
5649 {
5650 SOURCING_LNUM = iptr->isn_lnum;
5651 emsg(_(e_cannot_add_to_null_blob));
5652 goto on_error;
5653 }
5654 n = tv_get_number_chk(tv2, &error);
5655 if (error)
5656 goto on_error;
5657 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005658 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005659 }
5660 break;
5661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005662 // Computation with two arguments of unknown type
5663 case ISN_OPANY:
5664 {
5665 typval_T *tv1 = STACK_TV_BOT(-2);
5666 typval_T *tv2 = STACK_TV_BOT(-1);
5667 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005668 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005669 int error = FALSE;
5670
5671 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5672 {
5673 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5674 {
5675 eval_addlist(tv1, tv2);
5676 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005677 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005678 break;
5679 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005680 else if (tv1->v_type == VAR_TUPLE
5681 && tv2->v_type == VAR_TUPLE)
5682 {
5683 eval_addtuple(tv1, tv2);
5684 clear_tv(tv2);
5685 --ectx->ec_stack.ga_len;
5686 break;
5687 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005688 else if (tv1->v_type == VAR_BLOB
5689 && tv2->v_type == VAR_BLOB)
5690 {
5691 eval_addblob(tv1, tv2);
5692 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005693 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005694 break;
5695 }
5696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005697 if (tv1->v_type == VAR_FLOAT)
5698 {
5699 f1 = tv1->vval.v_float;
5700 n1 = 0;
5701 }
5702 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005703 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005704 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005705 n1 = tv_get_number_chk(tv1, &error);
5706 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005707 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708 if (tv2->v_type == VAR_FLOAT)
5709 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711 if (tv2->v_type == VAR_FLOAT)
5712 {
5713 f2 = tv2->vval.v_float;
5714 n2 = 0;
5715 }
5716 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005717 {
5718 n2 = tv_get_number_chk(tv2, &error);
5719 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005720 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005721 if (tv1->v_type == VAR_FLOAT)
5722 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005724 // if there is a float on either side the result is a float
5725 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5726 {
5727 switch (iptr->isn_arg.op.op_type)
5728 {
5729 case EXPR_MULT: f1 = f1 * f2; break;
5730 case EXPR_DIV: f1 = f1 / f2; break;
5731 case EXPR_SUB: f1 = f1 - f2; break;
5732 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005733 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005734 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005735 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005736 }
5737 clear_tv(tv1);
5738 clear_tv(tv2);
5739 tv1->v_type = VAR_FLOAT;
5740 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005741 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005742 }
5743 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005744 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005745 int failed = FALSE;
5746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005747 switch (iptr->isn_arg.op.op_type)
5748 {
5749 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005750 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5751 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005752 goto on_error;
5753 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005754 case EXPR_SUB: n1 = n1 - n2; break;
5755 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005756 default: n1 = num_modulus(n1, n2, &failed);
5757 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005758 goto on_error;
5759 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005760 }
5761 clear_tv(tv1);
5762 clear_tv(tv2);
5763 tv1->v_type = VAR_NUMBER;
5764 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005765 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005766 }
5767 }
5768 break;
5769
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005770 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005771 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005772 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005773 int is_slice = iptr->isn_type == ISN_STRSLICE;
5774 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005775 char_u *res;
5776
5777 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005778 // string slice: string is at stack-3, first index at
5779 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005780 if (is_slice)
5781 {
5782 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005783 n1 = tv->vval.v_number;
5784 }
5785
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005786 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005787 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005788
Bram Moolenaar4c137212021-04-19 16:48:48 +02005789 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005790 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005791 if (is_slice)
5792 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005793 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005794 else
5795 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005796 // single character (including composing characters).
5797 // If the index is too big or negative the result is
5798 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005799 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005800 vim_free(tv->vval.v_string);
5801 tv->vval.v_string = res;
5802 }
5803 break;
5804
5805 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005806 case ISN_LISTSLICE:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005807 case ISN_TUPLEINDEX:
5808 case ISN_TUPLESLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005809 case ISN_BLOBINDEX:
5810 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005811 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005812 int is_slice = iptr->isn_type == ISN_LISTSLICE
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005813 || iptr->isn_type == ISN_TUPLESLICE
5814 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005815 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5816 || iptr->isn_type == ISN_BLOBSLICE;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005817 int is_tuple = iptr->isn_type == ISN_TUPLEINDEX
5818 || iptr->isn_type == ISN_TUPLESLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005819 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005820 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005821
5822 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005823 // list slice: list is at stack-3, indexes at stack-2 and
5824 // stack-1
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005825 // Same for tuple and blob.
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005826 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005827
5828 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005829 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005830 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005831
5832 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005833 {
Bram Moolenaared591872020-08-15 22:14:53 +02005834 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005835 n1 = tv->vval.v_number;
5836 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005837 }
Bram Moolenaared591872020-08-15 22:14:53 +02005838
Bram Moolenaar4c137212021-04-19 16:48:48 +02005839 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005840 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005841 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005842 if (is_blob)
5843 {
5844 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5845 n1, n2, FALSE, tv) == FAIL)
5846 goto on_error;
5847 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005848 else if (is_tuple)
5849 {
5850 if (tuple_slice_or_index(val_tv->vval.v_tuple,
5851 is_slice, n1, n2, FALSE, tv, TRUE) == FAIL)
5852 goto on_error;
5853 }
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005854 else
5855 {
5856 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5857 n1, n2, FALSE, tv, TRUE) == FAIL)
5858 goto on_error;
5859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005860 }
5861 break;
5862
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005863 case ISN_ANYINDEX:
5864 case ISN_ANYSLICE:
5865 {
5866 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5867 typval_T *var1, *var2;
5868 int res;
5869
5870 // index: composite is at stack-2, index at stack-1
5871 // slice: composite is at stack-3, indexes at stack-2 and
5872 // stack-1
5873 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005874 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005875 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5876 goto on_error;
5877 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5878 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005879 res = eval_index_inner(tv, is_slice, var1, var2,
5880 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005881 clear_tv(var1);
5882 if (is_slice)
5883 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005884 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005885 if (res == FAIL)
5886 goto on_error;
5887 }
5888 break;
5889
Bram Moolenaar9af78762020-06-16 11:34:42 +02005890 case ISN_SLICE:
5891 {
Bram Moolenaar9af78762020-06-16 11:34:42 +02005892 int count = iptr->isn_arg.number;
5893
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005894 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005895 tv = STACK_TV_BOT(-1);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005896 if (tv->v_type == VAR_LIST)
Bram Moolenaar9af78762020-06-16 11:34:42 +02005897 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005898 list_T *list = tv->vval.v_list;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005899
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005900 // no error for short list, expect it to be checked
5901 // earlier
5902 if (list != NULL && list->lv_len >= count)
Bram Moolenaar9af78762020-06-16 11:34:42 +02005903 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005904 list_T *newlist = list_slice(list,
5905 count, list->lv_len - 1);
5906
5907 if (newlist != NULL)
5908 {
5909 list_unref(list);
5910 tv->vval.v_list = newlist;
5911 ++newlist->lv_refcount;
5912 }
5913 }
5914 }
5915 else
5916 {
5917 tuple_T *tuple = tv->vval.v_tuple;
5918
5919 // no error for short tuple, expect it to be checked
5920 // earlier
5921 if (tuple != NULL && TUPLE_LEN(tuple) >= count)
5922 {
5923 tuple_T *newtuple;
5924
5925 newtuple = tuple_slice(tuple, count,
5926 TUPLE_LEN(tuple) - 1);
5927 if (newtuple != NULL)
5928 {
5929 tuple_unref(tuple);
5930 tv->v_type = VAR_TUPLE;
5931 tv->vval.v_tuple = newtuple;
5932 ++newtuple->tv_refcount;
5933 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005934 }
5935 }
5936 }
5937 break;
5938
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005939 case ISN_GETITEM:
5940 {
5941 listitem_T *li;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005942 typval_T *item_tv;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005943 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005944
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005945 // Get list item: list is at stack-1, push item.
5946 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005947 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005948 if (tv->v_type == VAR_LIST)
5949 {
5950 li = list_find(tv->vval.v_list, gi->gi_index);
5951 item_tv = &li->li_tv;
5952 }
5953 else
5954 item_tv = TUPLE_ITEM(tv->vval.v_tuple, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005955
Bram Moolenaar35578162021-08-02 19:10:38 +02005956 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005957 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005958 ++ectx->ec_stack.ga_len;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005959 copy_tv(item_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005960
5961 // Useful when used in unpack assignment. Reset at
5962 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005963 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005964 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005965 }
5966 break;
5967
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01005968 // dict member with string key (dict['member'])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969 case ISN_MEMBER:
5970 {
5971 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005972 char_u *key;
5973 dictitem_T *di;
5974
5975 // dict member: dict is at stack-2, key at stack-1
5976 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005977 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005978 dict = tv->vval.v_dict;
5979
5980 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005981 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005982 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005983 if (key == NULL)
5984 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005985
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005986 if ((di = dict_find(dict, key, -1)) == NULL)
5987 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005988 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005989 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005990
5991 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005992 // stack contents makes sense and the dict stack is
5993 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005994 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005995 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005996 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005997 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005998 tv->v_type = VAR_NUMBER;
5999 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01006000 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006001 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006002 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006003 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006004 // Put the dict used on the dict stack, it might be used by
6005 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02006006 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006007 if (dict_stack_save(tv) == FAIL)
6008 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02006009 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006010 }
6011 break;
6012
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01006013 // dict member with string key (dict.member)
6014 // or can be an object
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006015 case ISN_STRINGMEMBER:
6016 {
6017 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018 dictitem_T *di;
6019
6020 tv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01006022 if (tv->v_type == VAR_OBJECT
6023 || tv->v_type == VAR_CLASS)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024 {
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01006025 if (dict_stack_save(tv) == FAIL)
6026 goto on_fatal_error;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006027
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01006028 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
6029 + ectx->ec_dfunc_idx)->df_ufunc;
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01006030 // Class or an object (not a Dict)
6031 if (var_any_get_oc_member(ufunc->uf_class, iptr, tv) == FAIL)
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01006032 goto on_error;
6033 }
6034 else
6035 {
6036 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
6037 {
6038 SOURCING_LNUM = iptr->isn_lnum;
6039 emsg(_(e_dictionary_required));
6040 goto on_error;
6041 }
6042 dict = tv->vval.v_dict;
6043
6044 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
6045 == NULL)
6046 {
6047 SOURCING_LNUM = iptr->isn_lnum;
6048 semsg(_(e_key_not_present_in_dictionary_str),
6049 iptr->isn_arg.string);
6050 goto on_error;
6051 }
6052 // Put the dict used on the dict stack, it might be
6053 // used by a dict function later.
6054 if (dict_stack_save(tv) == FAIL)
6055 goto on_fatal_error;
6056
6057 copy_tv(&di->di_tv, tv);
6058 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006059 }
6060 break;
6061
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00006062 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00006063 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006064 {
6065 tv = STACK_TV_BOT(-1);
6066 if (tv->v_type != VAR_OBJECT)
6067 {
6068 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006069 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006070 goto on_error;
6071 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00006072
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006073 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00006074 if (obj == NULL)
6075 {
6076 SOURCING_LNUM = iptr->isn_lnum;
6077 emsg(_(e_using_null_object));
6078 goto on_error;
6079 }
6080
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00006081 int idx;
6082 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02006083 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00006084 else
6085 {
6086 idx = iptr->isn_arg.classmember.cm_idx;
6087 // convert the interface index to the object index
6088 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02006089 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02006090 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00006091 }
6092
Ernie Rael18143d32023-09-04 22:30:41 +02006093 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02006094 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Yegappan Lakshmananb04af4c2025-01-03 10:50:08 +01006095 if (mtv->v_type == VAR_UNKNOWN)
6096 {
6097 // Referencing an object variable (without a type)
6098 // which is not yet initialized. So the type is not
6099 // yet known.
6100 ocmember_T *m = &obj->obj_class->class_obj_members[idx];
6101 SOURCING_LNUM = iptr->isn_lnum;
6102 semsg(_(e_uninitialized_object_var_reference),
6103 m->ocm_name);
6104 goto on_error;
6105 }
Bram Moolenaar590162c2022-12-24 21:24:06 +00006106 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006107
6108 // Unreference the object after getting the member, it may
6109 // be freed.
6110 object_unref(obj);
6111 }
6112 break;
6113
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00006114 case ISN_STORE_THIS:
6115 {
6116 int idx = iptr->isn_arg.number;
6117 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
6118 // the members are located right after the object struct
6119 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
6120 clear_tv(mtv);
6121 *mtv = *STACK_TV_BOT(-1);
6122 --ectx->ec_stack.ga_len;
6123 }
6124 break;
6125
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006126 case ISN_CLEARDICT:
6127 dict_stack_drop();
6128 break;
6129
6130 case ISN_USEDICT:
6131 {
6132 typval_T *dict_tv = dict_stack_get_tv();
6133
6134 // Turn "dict.Func" into a partial for "Func" bound to
6135 // "dict". Don't do this when "Func" is already a partial
6136 // that was bound explicitly (pt_auto is FALSE).
6137 tv = STACK_TV_BOT(-1);
6138 if (dict_tv != NULL
6139 && dict_tv->v_type == VAR_DICT
6140 && dict_tv->vval.v_dict != NULL
6141 && (tv->v_type == VAR_FUNC
6142 || (tv->v_type == VAR_PARTIAL
6143 && (tv->vval.v_partial->pt_auto
6144 || tv->vval.v_partial->pt_dict == NULL))))
6145 dict_tv->vval.v_dict =
6146 make_partial(dict_tv->vval.v_dict, tv);
6147 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006148 }
6149 break;
6150
6151 case ISN_NEGATENR:
6152 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00006153 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02006154 if (tv->v_type == VAR_FLOAT)
6155 tv->vval.v_float = -tv->vval.v_float;
6156 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02006157 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006158 break;
6159
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006160 case ISN_CHECKTYPE:
6161 {
6162 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01006163 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02006164 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01006166 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02006167 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02006168 if (ct->ct_arg_idx > 0)
6169 {
6170 where.wt_index = ct->ct_arg_idx;
6171 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02006172 }
LemonBoyf244b2f2023-08-19 16:02:04 +02006173 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02006174 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01006175 if (r == FAIL)
6176 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02006177
6178 // number 0 is FALSE, number 1 is TRUE
6179 if (tv->v_type == VAR_NUMBER
6180 && ct->ct_type->tt_type == VAR_BOOL
6181 && (tv->vval.v_number == 0
6182 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02006184 tv->v_type = VAR_BOOL;
6185 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02006186 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187 }
6188 }
6189 break;
6190
Bram Moolenaar9af78762020-06-16 11:34:42 +02006191 case ISN_CHECKLEN:
6192 {
6193 int min_len = iptr->isn_arg.checklen.cl_min_len;
6194 list_T *list = NULL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006195 tuple_T *tuple = NULL;
6196 int len = 0;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006197
6198 tv = STACK_TV_BOT(-1);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006199
6200 int len_check_failed = FALSE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006201 if (tv->v_type == VAR_LIST)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006202 {
6203 list = tv->vval.v_list;
6204 if (list == NULL || list->lv_len < min_len
Bram Moolenaar9af78762020-06-16 11:34:42 +02006205 || (list->lv_len > min_len
6206 && !iptr->isn_arg.checklen.cl_more_OK))
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006207 len_check_failed = TRUE;
6208 if (list != NULL)
6209 len = list->lv_len;
6210 }
6211 else if (tv->v_type == VAR_TUPLE)
6212 {
6213 tuple = tv->vval.v_tuple;
6214 if (tuple == NULL || TUPLE_LEN(tuple) < min_len
6215 || (TUPLE_LEN(tuple) > min_len
6216 && !iptr->isn_arg.checklen.cl_more_OK))
6217 len_check_failed = TRUE;
6218 if (tuple != NULL)
6219 len = TUPLE_LEN(tuple);
6220 }
6221 else
6222 len_check_failed = TRUE;
6223
6224 if (len_check_failed)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006225 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02006226 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006227 semsg(_(e_expected_nr_items_but_got_nr),
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006228 min_len, len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02006229 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02006230 }
6231 }
6232 break;
6233
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006234 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00006235 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006236 break;
6237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006238 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02006239 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240 {
6241 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02006242 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243
Bram Moolenaar2bb26582020-10-03 22:52:39 +02006244 if (iptr->isn_type == ISN_2BOOL)
6245 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006246 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02006247 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006248 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02006249 n = !n;
6250 }
6251 else
6252 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006253 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02006254 SOURCING_LNUM = iptr->isn_lnum;
6255 n = tv_get_bool_chk(tv, &error);
6256 if (error)
6257 goto on_error;
6258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 clear_tv(tv);
6260 tv->v_type = VAR_BOOL;
6261 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
6262 }
6263 break;
6264
6265 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02006266 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01006267 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006268 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
6269 iptr->isn_type == ISN_2STRING_ANY,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02006270 iptr->isn_arg.tostring.flags) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006271 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272 break;
6273
Bram Moolenaar08597872020-12-10 19:43:40 +01006274 case ISN_RANGE:
6275 {
6276 exarg_T ea;
6277 char *errormsg;
6278
Bram Moolenaarece0b872021-01-08 20:40:45 +01006279 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01006280 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01006281 ea.addr_type = ADDR_LINES;
6282 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01006283 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01006284 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01006285 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01006286
Bram Moolenaar35578162021-08-02 19:10:38 +02006287 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006288 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006289 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01006290 tv = STACK_TV_BOT(-1);
6291 tv->v_type = VAR_NUMBER;
6292 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01006293 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01006294 }
6295 break;
6296
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006297 case ISN_PUT:
64-bitmane08f10a2025-03-18 22:14:34 +01006298 tv = NULL; // initialize it so we don't get a warning
6299 isn_put_do(ectx, iptr, tv, FALSE);
6300 break;
6301 case ISN_IPUT:
6302 tv = NULL;
6303 isn_put_do(ectx, iptr, tv, TRUE);
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006304 break;
6305
Bram Moolenaar02194d22020-10-24 23:08:38 +02006306 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006307 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
6308 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
6309 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
6310 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006311 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
6312 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006313 break;
6314
Bram Moolenaar02194d22020-10-24 23:08:38 +02006315 case ISN_CMDMOD_REV:
6316 // filter regprog is owned by the instruction, don't free it
6317 cmdmod.cmod_filter_regmatch.regprog = NULL;
6318 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006319 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
6320 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006321 break;
6322
Bram Moolenaar792f7862020-11-23 08:31:18 +01006323 case ISN_UNPACK:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006324 // Check there is a valid list to unpack.
6325 tv = STACK_TV_BOT(-1);
6326 if (tv->v_type != VAR_LIST && tv->v_type != VAR_TUPLE)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006327 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006328 SOURCING_LNUM = iptr->isn_lnum;
6329 emsg(_(e_for_argument_must_be_sequence_of_lists_or_tuples));
6330 goto on_error;
6331 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006332
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006333 int rc;
6334 if (tv->v_type == VAR_LIST)
6335 rc = exec_unpack_list(ectx, iptr, tv);
6336 else
6337 rc = exec_unpack_tuple(ectx, iptr, tv);
6338 if (rc != EXEC_OK)
6339 {
6340 if (rc == EXEC_FAIL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006341 goto on_error;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006342 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006343 }
6344 break;
6345
Bram Moolenaarb2049902021-01-24 12:53:53 +01006346 case ISN_PROF_START:
6347 case ISN_PROF_END:
6348 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01006349#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01006350 funccall_T cookie;
6351 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01006352 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02006353 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006354
Bram Moolenaarca16c602022-09-06 18:57:08 +01006355 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006356 if (iptr->isn_type == ISN_PROF_START)
6357 {
6358 func_line_start(&cookie, iptr->isn_lnum);
6359 // if we get here the instruction is executed
6360 func_line_exec(&cookie);
6361 }
6362 else
6363 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01006364#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01006365 }
6366 break;
6367
Bram Moolenaare99d4222021-06-13 14:01:26 +02006368 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02006369 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006370 break;
6371
Bram Moolenaar389df252020-07-09 21:20:47 +02006372 case ISN_SHUFFLE:
6373 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01006374 typval_T tmp_tv;
6375 int item = iptr->isn_arg.shuffle.shfl_item;
6376 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02006377
6378 tmp_tv = *STACK_TV_BOT(-item);
6379 for ( ; up > 0 && item > 1; --up)
6380 {
6381 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
6382 --item;
6383 }
6384 *STACK_TV_BOT(-item) = tmp_tv;
6385 }
6386 break;
6387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006388 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006389 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006390 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02006391 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392 break;
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01006393
6394 case ISN_SCRIPTCTX_SET:
6395 // change the script context. Used to evaluate an object
6396 // member variable initialization expression in the context of
6397 // the script where the class is defined.
6398 current_sctx = iptr->isn_arg.setsctx;
6399 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02006401 continue;
6402
Bram Moolenaard032f342020-07-18 18:13:02 +02006403func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02006404 // Restore previous function. If the frame pointer is where we started
6405 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006406 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02006407 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02006408
Bram Moolenaar4c137212021-04-19 16:48:48 +02006409 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02006410 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006411 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006412 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02006413
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02006414on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01006415 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006416 // If "emsg_silent" is set then ignore the error, unless it was set
6417 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006418 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006419 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01006420 {
6421 // If a sequence of instructions causes an error while ":silent!"
6422 // was used, restore the stack length and jump ahead to restoring
6423 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006424 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01006425 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006426 while (ectx->ec_stack.ga_len
6427 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01006428 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006429 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01006430 clear_tv(STACK_TV_BOT(0));
6431 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006432 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
6433 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01006434 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01006435 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01006436 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01006437on_fatal_error:
6438 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01006439 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006440 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006441 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442 }
6443
6444done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006445 ret = OK;
6446theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01006447 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006448
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006449 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006450 ectx->ec_trylevel_at_start = save_trylevel_at_start;
6451 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006452}
6453
6454/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006455 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006456 * "rettv".
6457 * Return OK or FAIL.
6458 */
6459 int
6460exe_typval_instr(typval_T *tv, typval_T *rettv)
6461{
6462 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
6463 isn_T *save_instr = ectx->ec_instr;
6464 int save_iidx = ectx->ec_iidx;
6465 int res;
6466
LemonBoyf3b48952022-05-05 13:53:03 +01006467 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
6468 // even when the compilation fails.
6469 rettv->v_type = VAR_UNKNOWN;
6470
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006471 ectx->ec_instr = tv->vval.v_instr->instr_instr;
6472 res = exec_instructions(ectx);
6473 if (res == OK)
6474 {
6475 *rettv = *STACK_TV_BOT(-1);
6476 --ectx->ec_stack.ga_len;
6477 }
6478
6479 ectx->ec_instr = save_instr;
6480 ectx->ec_iidx = save_iidx;
6481
6482 return res;
6483}
6484
6485/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006486 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
6487 * "substitute_instr".
6488 */
6489 char_u *
6490exe_substitute_instr(void)
6491{
6492 ectx_T *ectx = substitute_instr->subs_ectx;
6493 isn_T *save_instr = ectx->ec_instr;
6494 int save_iidx = ectx->ec_iidx;
6495 char_u *res;
6496
6497 ectx->ec_instr = substitute_instr->subs_instr;
6498 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02006499 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006500 typval_T *tv = STACK_TV_BOT(-1);
6501
Bram Moolenaar27523602021-06-05 21:36:19 +02006502 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006503 --ectx->ec_stack.ga_len;
6504 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02006505 }
6506 else
6507 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006508 substitute_instr->subs_status = FAIL;
6509 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02006510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511
Bram Moolenaar4c137212021-04-19 16:48:48 +02006512 ectx->ec_instr = save_instr;
6513 ectx->ec_iidx = save_iidx;
6514
6515 return res;
6516}
6517
6518/*
6519 * Call a "def" function from old Vim script.
6520 * Return OK or FAIL.
6521 */
6522 int
6523call_def_function(
6524 ufunc_T *ufunc,
6525 int argc_arg, // nr of arguments
6526 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006527 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02006528 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006529 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01006530 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006531 typval_T *rettv) // return value
6532{
6533 ectx_T ectx; // execution context
6534 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006535 int partial_argc = partial == NULL
6536 || (flags & DEF_USE_PT_ARGV) == 0
6537 ? 0 : partial->pt_argc;
6538 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006539 typval_T *tv;
6540 int idx;
6541 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006542 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006543 sctx_T save_current_sctx = current_sctx;
6544 int did_emsg_before = did_emsg_cumul + did_emsg;
6545 int save_suppress_errthrow = suppress_errthrow;
6546 msglist_T **saved_msg_list = NULL;
6547 msglist_T *private_msg_list = NULL;
6548 int save_emsg_silent_def = emsg_silent_def;
6549 int save_did_emsg_def = did_emsg_def;
6550 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006551 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006552
6553// Get pointer to item in the stack.
6554#undef STACK_TV
6555#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6556
6557// Get pointer to item at the bottom of the stack, -1 is the bottom.
6558#undef STACK_TV_BOT
6559#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6560
6561// Get pointer to a local variable on the stack. Negative for arguments.
6562#undef STACK_TV_VAR
6563#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6564
6565 if (ufunc->uf_def_status == UF_NOT_COMPILED
6566 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006567 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6568 && compile_def_function(ufunc, FALSE,
6569 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006570 {
6571 if (did_emsg_cumul + did_emsg == did_emsg_before)
6572 semsg(_(e_function_is_not_compiled_str),
6573 printable_func_name(ufunc));
6574 return FAIL;
6575 }
6576
6577 {
6578 // Check the function was really compiled.
6579 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6580 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006581 if (dfunc->df_ufunc == NULL)
6582 {
6583 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6584 return FAIL;
6585 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006586 if (INSTRUCTIONS(dfunc) == NULL)
6587 {
6588 iemsg("using call_def_function() on not compiled function");
6589 return FAIL;
6590 }
6591 }
6592
6593 // If depth of calling is getting too high, don't execute the function.
6594 orig_funcdepth = funcdepth_get();
6595 if (funcdepth_increment() == FAIL)
6596 return FAIL;
6597
6598 CLEAR_FIELD(ectx);
6599 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6600 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006601 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006602 {
6603 funcdepth_decrement();
6604 return FAIL;
6605 }
6606 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6607 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6608 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006609 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006610
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006611 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006612 if (idx > 0 && ufunc->uf_va_name == NULL)
6613 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006614 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006615 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006616 goto failed_early;
6617 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006618 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006619 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006620 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006621 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006622 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006623 goto failed_early;
6624 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006625
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006626 // Put values from the partial and arguments on the stack, but no more than
6627 // what the function expects. A lambda can be called with more arguments
6628 // than it uses.
6629 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006630 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6631 ++idx)
6632 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006633 int argv_idx = idx - partial_argc;
6634
6635 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006636 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006637 && tv->v_type == VAR_SPECIAL
6638 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006639 {
6640 // Use the default value.
6641 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6642 }
6643 else
6644 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006645 int done = FALSE;
6646 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6647 {
6648 type_T *expected = ufunc->uf_arg_types[idx];
6649 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6650 {
6651 // When a float is expected and a number was given, convert
6652 // the value.
6653 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6654 STACK_TV_BOT(0)->v_lock = 0;
6655 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6656 done = TRUE;
6657 }
6658 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006659 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006660 goto failed_early;
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01006661 }
Bram Moolenaar47bba532023-01-20 18:49:46 +00006662 if (!done)
6663 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006664 }
6665 ++ectx.ec_stack.ga_len;
6666 }
6667
6668 // Turn varargs into a list. Empty list if no args.
6669 if (ufunc->uf_va_name != NULL)
6670 {
6671 int vararg_count = argc - ufunc->uf_args.ga_len;
6672
6673 if (vararg_count < 0)
6674 vararg_count = 0;
6675 else
6676 argc -= vararg_count;
6677 if (exe_newlist(vararg_count, &ectx) == FAIL)
6678 goto failed_early;
6679
6680 // Check the type of the list items.
6681 tv = STACK_TV_BOT(-1);
6682 if (ufunc->uf_va_type != NULL
6683 && ufunc->uf_va_type != &t_list_any
6684 && ufunc->uf_va_type->tt_member != &t_any
6685 && tv->vval.v_list != NULL)
6686 {
6687 type_T *expected = ufunc->uf_va_type->tt_member;
6688 listitem_T *li = tv->vval.v_list->lv_first;
6689
6690 for (idx = 0; idx < vararg_count; ++idx)
6691 {
6692 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006693 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006694 goto failed_early;
6695 li = li->li_next;
6696 }
6697 }
6698
6699 if (defcount > 0)
6700 // Move varargs list to below missing default arguments.
6701 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6702 --ectx.ec_stack.ga_len;
6703 }
6704
6705 // Make space for omitted arguments, will store default value below.
6706 // Any varargs list goes after them.
6707 if (defcount > 0)
6708 for (idx = 0; idx < defcount; ++idx)
6709 {
6710 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6711 ++ectx.ec_stack.ga_len;
6712 }
6713 if (ufunc->uf_va_name != NULL)
6714 ++ectx.ec_stack.ga_len;
6715
6716 // Frame pointer points to just after arguments.
6717 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6718 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6719
6720 {
6721 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6722 + ufunc->uf_dfunc_idx;
6723 ufunc_T *base_ufunc = dfunc->df_ufunc;
6724
6725 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006726 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006727 if (partial != NULL || base_ufunc->uf_partial != NULL)
6728 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006729 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6730 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006731 goto failed_early;
6732 if (partial != NULL)
6733 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006734 outer_T *outer = get_pt_outer(partial);
6735
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006736 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006737 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006738 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006739 if (current_ectx != NULL)
6740 {
6741 if (current_ectx->ec_outer_ref != NULL
6742 && current_ectx->ec_outer_ref->or_outer != NULL)
6743 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006744 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006745 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006746 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006747 }
6748 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006749 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006750 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006751 ++partial->pt_refcount;
6752 ectx.ec_outer_ref->or_partial = partial;
6753 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006754 }
6755 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006756 {
6757 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6758 ++base_ufunc->uf_partial->pt_refcount;
6759 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6760 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006761 }
6762 }
6763
6764 // dummy frame entries
6765 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6766 {
6767 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6768 ++ectx.ec_stack.ga_len;
6769 }
6770
6771 {
6772 // Reserve space for local variables and any closure reference count.
6773 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6774 + ufunc->uf_dfunc_idx;
6775
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006776 // Initialize variables to zero. That avoids having to generate
6777 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006778 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006779 {
6780 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6781 STACK_TV_VAR(idx)->vval.v_number = 0;
6782 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006783 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006784
6785 if (object != NULL)
6786 {
6787 // the object is always the variable at index zero
6788 tv = STACK_TV_VAR(0);
6789 tv->v_type = VAR_OBJECT;
6790 tv->vval.v_object = object;
6791 }
6792
Bram Moolenaar4c137212021-04-19 16:48:48 +02006793 if (dfunc->df_has_closure)
6794 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006795 // Initialize the variable that counts how many closures were
6796 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006797 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6798 STACK_TV_VAR(idx)->vval.v_number = 0;
6799 ++ectx.ec_stack.ga_len;
6800 }
6801
6802 ectx.ec_instr = INSTRUCTIONS(dfunc);
6803 }
6804
Bram Moolenaar58779852022-09-06 18:31:14 +01006805 // Store the execution context in funccal, used by invoke_all_defer().
6806 if (funccal != NULL)
6807 funccal->fc_ectx = &ectx;
6808
Bram Moolenaar4c137212021-04-19 16:48:48 +02006809 // Following errors are in the function, not the caller.
6810 // Commands behave like vim9script.
6811 estack_push_ufunc(ufunc, 1);
6812 current_sctx = ufunc->uf_script_ctx;
6813 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6814
6815 // Use a specific location for storing error messages to be converted to an
6816 // exception.
6817 saved_msg_list = msg_list;
6818 msg_list = &private_msg_list;
6819
6820 // Do turn errors into exceptions.
6821 suppress_errthrow = FALSE;
6822
6823 // Do not delete the function while executing it.
6824 ++ufunc->uf_calls;
6825
6826 // When ":silent!" was used before calling then we still abort the
6827 // function. If ":silent!" is used in the function then we don't.
6828 emsg_silent_def = emsg_silent;
6829 did_emsg_def = 0;
6830
LemonBoyc5d27442023-08-19 13:02:35 +02006831 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006832
Bram Moolenaar5800c792022-09-22 17:34:01 +01006833 /*
6834 * Execute the instructions until done.
6835 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006836 ret = exec_instructions(&ectx);
6837 if (ret == OK)
6838 {
6839 // function finished, get result from the stack.
6840 if (ufunc->uf_ret_type == &t_void)
6841 {
6842 rettv->v_type = VAR_VOID;
6843 }
6844 else
6845 {
6846 tv = STACK_TV_BOT(-1);
6847 *rettv = *tv;
6848 tv->v_type = VAR_UNKNOWN;
6849 }
6850 }
6851
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006852 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006853 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006854
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006855 // Deal with any remaining closures, they may be in use somewhere.
6856 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006857 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006858 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006859 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006860 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006861
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006862 estack_pop();
6863 current_sctx = save_current_sctx;
6864
Bram Moolenaar2336c372021-12-06 15:06:54 +00006865 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6866 // Function was unreferenced while being used, free it now.
6867 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006868
Bram Moolenaar352134b2020-10-17 22:04:08 +02006869 if (*msg_list != NULL && saved_msg_list != NULL)
6870 {
6871 msglist_T **plist = saved_msg_list;
6872
6873 // Append entries from the current msg_list (uncaught exceptions) to
6874 // the saved msg_list.
6875 while (*plist != NULL)
6876 plist = &(*plist)->next;
6877
6878 *plist = *msg_list;
6879 }
6880 msg_list = saved_msg_list;
6881
Bram Moolenaar4c137212021-04-19 16:48:48 +02006882 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006883 {
6884 cmdmod.cmod_filter_regmatch.regprog = NULL;
6885 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006886 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006887 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006888 emsg_silent_def = save_emsg_silent_def;
6889 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006890
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006891failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006892 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006893 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006894 {
6895 tv = STACK_TV(idx);
6896 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6897 clear_tv(tv);
6898 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006899 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006901 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006902 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006903 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006904 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006905 if (ectx.ec_outer_ref->or_outer_allocated)
6906 vim_free(ectx.ec_outer_ref->or_outer);
6907 partial_unref(ectx.ec_outer_ref->or_partial);
6908 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006909 }
6910
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006911 // Not sure if this is necessary.
6912 suppress_errthrow = save_suppress_errthrow;
6913
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006914 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6915 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006916 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006917 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006918 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919 return ret;
6920}
6921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006923 * Called when a def function has finished (possibly failed).
6924 * Invoke all the function returns to clean up and invoke deferred functions,
6925 * except the toplevel one.
6926 */
6927 void
6928unwind_def_callstack(ectx_T *ectx)
6929{
6930 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6931 func_return(ectx);
6932}
6933
6934/*
dundargocc57b5bc2022-11-02 13:30:51 +00006935 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006936 */
6937 void
6938may_invoke_defer_funcs(ectx_T *ectx)
6939{
6940 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6941
6942 if (dfunc->df_defer_var_idx > 0)
6943 invoke_defer_funcs(ectx);
6944}
6945
6946/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006947 * Return loopvarinfo in a printable form in allocated memory.
6948 */
6949 static char_u *
6950printable_loopvarinfo(loopvarinfo_T *lvi)
6951{
6952 garray_T ga;
6953 int depth;
6954
6955 ga_init2(&ga, 1, 100);
6956 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6957 {
6958 if (ga_grow(&ga, 50) == FAIL)
6959 break;
6960 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006961 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006962 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006963 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006964 lvi->lvi_loop[depth].var_idx,
6965 lvi->lvi_loop[depth].var_idx
6966 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006967 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006968 }
6969 return ga.ga_data;
6970}
6971
6972/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006973 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6974 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6975 * "pfx" is prefixed to every line.
6976 */
6977 static void
6978list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6979{
6980 int line_idx = 0;
6981 int prev_current = 0;
6982 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006983 int def_arg_idx = 0;
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01006984 sctx_T script_ctx = current_sctx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006985
6986 for (current = 0; current < instr_count; ++current)
6987 {
6988 isn_T *iptr = &instr[current];
6989 char *line;
6990
6991 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006992 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006993 while (line_idx < iptr->isn_lnum
6994 && line_idx < ufunc->uf_lines.ga_len)
6995 {
6996 if (current > prev_current)
6997 {
6998 msg_puts("\n\n");
6999 prev_current = current;
7000 }
7001 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
7002 if (line != NULL)
7003 msg(line);
7004 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02007005 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
7006 {
7007 int first_def_arg = ufunc->uf_args.ga_len
7008 - ufunc->uf_def_args.ga_len;
7009
7010 if (def_arg_idx > 0)
7011 msg_puts("\n\n");
7012 msg_start();
7013 msg_puts(" ");
7014 msg_puts(((char **)(ufunc->uf_args.ga_data))[
7015 first_def_arg + def_arg_idx]);
7016 msg_puts(" = ");
7017 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
7018 msg_clr_eos();
7019 msg_end();
7020 }
7021 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007022
7023 switch (iptr->isn_type)
7024 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007025 case ISN_CONSTRUCT:
7026 smsg("%s%4d NEW %s size %d", pfx, current,
7027 iptr->isn_arg.construct.construct_class->class_name,
7028 (int)iptr->isn_arg.construct.construct_size);
7029 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007030 case ISN_EXEC:
7031 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
7032 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02007033 case ISN_EXEC_SPLIT:
7034 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
7035 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00007036 case ISN_EXECRANGE:
7037 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
7038 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02007039 case ISN_LEGACY_EVAL:
7040 smsg("%s%4d EVAL legacy %s", pfx, current,
7041 iptr->isn_arg.string);
7042 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02007043 case ISN_REDIRSTART:
7044 smsg("%s%4d REDIR", pfx, current);
7045 break;
7046 case ISN_REDIREND:
7047 smsg("%s%4d REDIR END%s", pfx, current,
7048 iptr->isn_arg.number ? " append" : "");
7049 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007050 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02007051#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007052 smsg("%s%4d CEXPR pre %s", pfx, current,
7053 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02007054#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007055 break;
7056 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02007057#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007058 {
7059 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
7060
7061 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
7062 cexpr_get_auname(cer->cer_cmdidx),
7063 cer->cer_forceit ? "!" : "",
7064 cer->cer_cmdline);
7065 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02007066#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007067 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007068 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01007069 smsg("%s%4d INSTR", pfx, current);
7070 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
7071 msg(" -------------");
7072 break;
7073 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007074 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01007075 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
7076
7077 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007078 }
7079 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007080 case ISN_SUBSTITUTE:
7081 {
7082 subs_T *subs = &iptr->isn_arg.subs;
7083
7084 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
7085 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
7086 msg(" -------------");
7087 }
7088 break;
7089 case ISN_EXECCONCAT:
7090 smsg("%s%4d EXECCONCAT %lld", pfx, current,
7091 (varnumber_T)iptr->isn_arg.number);
7092 break;
7093 case ISN_ECHO:
7094 {
7095 echo_T *echo = &iptr->isn_arg.echo;
7096
7097 smsg("%s%4d %s %d", pfx, current,
7098 echo->echo_with_white ? "ECHO" : "ECHON",
7099 echo->echo_count);
7100 }
7101 break;
7102 case ISN_EXECUTE:
7103 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02007104 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007105 break;
7106 case ISN_ECHOMSG:
7107 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02007108 (varnumber_T)(iptr->isn_arg.number));
7109 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01007110 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007111 if (iptr->isn_arg.echowin.ewin_time > 0)
7112 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
7113 iptr->isn_arg.echowin.ewin_count,
7114 iptr->isn_arg.echowin.ewin_time);
7115 else
7116 smsg("%s%4d ECHOWINDOW %d", pfx, current,
7117 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01007118 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02007119 case ISN_ECHOCONSOLE:
7120 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
7121 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007122 break;
7123 case ISN_ECHOERR:
7124 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02007125 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007126 break;
7127 case ISN_LOAD:
7128 {
7129 if (iptr->isn_arg.number < 0)
7130 smsg("%s%4d LOAD arg[%lld]", pfx, current,
7131 (varnumber_T)(iptr->isn_arg.number
7132 + STACK_FRAME_SIZE));
7133 else
7134 smsg("%s%4d LOAD $%lld", pfx, current,
7135 (varnumber_T)(iptr->isn_arg.number));
7136 }
7137 break;
7138 case ISN_LOADOUTER:
7139 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007140 isn_outer_T *outer = &iptr->isn_arg.outer;
7141
7142 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007143 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007144 outer->outer_depth,
7145 outer->outer_idx + STACK_FRAME_SIZE);
7146 else if (outer->outer_depth < 0)
7147 smsg("%s%4d LOADOUTER $%d in loop level %d",
7148 pfx, current,
7149 outer->outer_idx,
7150 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007151 else
7152 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007153 outer->outer_depth,
7154 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007155 }
7156 break;
7157 case ISN_LOADV:
7158 smsg("%s%4d LOADV v:%s", pfx, current,
7159 get_vim_var_name(iptr->isn_arg.number));
7160 break;
7161 case ISN_LOADSCRIPT:
7162 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007163 scriptref_T *sref = iptr->isn_arg.script.scriptref;
7164 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
7165 svar_T *sv;
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01007166 sctx_T save_sctx = current_sctx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007167
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01007168 current_sctx = script_ctx;
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007169 sv = get_script_svar(sref, -1);
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01007170 current_sctx = save_sctx;
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007171 if (sv == NULL)
7172 smsg("%s%4d LOADSCRIPT [deleted] from %s",
7173 pfx, current, si->sn_name);
7174 else
7175 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007176 sv->sv_name,
7177 sref->sref_idx,
7178 si->sn_name);
7179 }
7180 break;
7181 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01007182 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007183 {
7184 scriptitem_T *si = SCRIPT_ITEM(
7185 iptr->isn_arg.loadstore.ls_sid);
7186
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01007187 smsg("%s%4d %s s:%s from %s", pfx, current,
7188 iptr->isn_type == ISN_LOADS ? "LOADS"
7189 : "LOADEXPORT",
7190 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007191 }
7192 break;
7193 case ISN_LOADAUTO:
7194 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
7195 break;
7196 case ISN_LOADG:
7197 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
7198 break;
7199 case ISN_LOADB:
7200 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
7201 break;
7202 case ISN_LOADW:
7203 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
7204 break;
7205 case ISN_LOADT:
7206 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
7207 break;
7208 case ISN_LOADGDICT:
7209 smsg("%s%4d LOAD g:", pfx, current);
7210 break;
7211 case ISN_LOADBDICT:
7212 smsg("%s%4d LOAD b:", pfx, current);
7213 break;
7214 case ISN_LOADWDICT:
7215 smsg("%s%4d LOAD w:", pfx, current);
7216 break;
7217 case ISN_LOADTDICT:
7218 smsg("%s%4d LOAD t:", pfx, current);
7219 break;
7220 case ISN_LOADOPT:
7221 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
7222 break;
7223 case ISN_LOADENV:
7224 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
7225 break;
7226 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007227 smsg("%s%4d LOADREG @%c", pfx, current,
7228 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007229 break;
7230
7231 case ISN_STORE:
7232 if (iptr->isn_arg.number < 0)
7233 smsg("%s%4d STORE arg[%lld]", pfx, current,
7234 iptr->isn_arg.number + STACK_FRAME_SIZE);
7235 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007236 smsg("%s%4d STORE $%lld", pfx, current,
7237 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007238 break;
7239 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007240 {
7241 isn_outer_T *outer = &iptr->isn_arg.outer;
7242
7243 if (outer->outer_depth == OUTER_LOOP_DEPTH)
7244 smsg("%s%4d STOREOUTER level 1 $%d in loop",
7245 pfx, current, outer->outer_idx);
7246 else
7247 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
7248 outer->outer_depth, outer->outer_idx);
7249 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007250 break;
7251 case ISN_STOREV:
7252 smsg("%s%4d STOREV v:%s", pfx, current,
7253 get_vim_var_name(iptr->isn_arg.number));
7254 break;
7255 case ISN_STOREAUTO:
7256 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
7257 break;
7258 case ISN_STOREG:
7259 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
7260 break;
7261 case ISN_STOREB:
7262 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
7263 break;
7264 case ISN_STOREW:
7265 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
7266 break;
7267 case ISN_STORET:
7268 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
7269 break;
7270 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01007271 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007272 {
7273 scriptitem_T *si = SCRIPT_ITEM(
7274 iptr->isn_arg.loadstore.ls_sid);
7275
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01007276 smsg("%s%4d %s %s in %s", pfx, current,
7277 iptr->isn_type == ISN_STORES
7278 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007279 iptr->isn_arg.loadstore.ls_name, si->sn_name);
7280 }
7281 break;
7282 case ISN_STORESCRIPT:
7283 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007284 scriptref_T *sref = iptr->isn_arg.script.scriptref;
7285 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
7286 svar_T *sv;
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01007287 sctx_T save_sctx = current_sctx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007288
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01007289 current_sctx = script_ctx;
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007290 sv = get_script_svar(sref, -1);
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01007291 current_sctx = save_sctx;
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007292 if (sv == NULL)
7293 smsg("%s%4d STORESCRIPT [deleted] in %s",
7294 pfx, current, si->sn_name);
7295 else
7296 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007297 sv->sv_name,
7298 sref->sref_idx,
7299 si->sn_name);
7300 }
7301 break;
7302 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00007303 case ISN_STOREFUNCOPT:
7304 smsg("%s%4d %s &%s", pfx, current,
7305 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007306 iptr->isn_arg.storeopt.so_name);
7307 break;
7308 case ISN_STOREENV:
7309 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
7310 break;
7311 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007312 smsg("%s%4d STOREREG @%c", pfx, current,
7313 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007314 break;
7315 case ISN_STORENR:
7316 smsg("%s%4d STORE %lld in $%d", pfx, current,
7317 iptr->isn_arg.storenr.stnr_val,
7318 iptr->isn_arg.storenr.stnr_idx);
7319 break;
7320
7321 case ISN_STOREINDEX:
7322 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00007323 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007324 break;
7325
7326 case ISN_STORERANGE:
7327 smsg("%s%4d STORERANGE", pfx, current);
7328 break;
7329
Bram Moolenaard505d172022-12-18 21:42:55 +00007330 case ISN_LOAD_CLASSMEMBER:
7331 case ISN_STORE_CLASSMEMBER:
7332 {
7333 class_T *cl = iptr->isn_arg.classmember.cm_class;
7334 int idx = iptr->isn_arg.classmember.cm_idx;
7335 ocmember_T *ocm = &cl->class_class_members[idx];
7336 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
7337 iptr->isn_type == ISN_LOAD_CLASSMEMBER
7338 ? "LOAD" : "STORE",
7339 cl->class_name, ocm->ocm_name);
7340 }
7341 break;
7342
Bram Moolenaar4c137212021-04-19 16:48:48 +02007343 // constants
7344 case ISN_PUSHNR:
7345 smsg("%s%4d PUSHNR %lld", pfx, current,
7346 (varnumber_T)(iptr->isn_arg.number));
7347 break;
7348 case ISN_PUSHBOOL:
7349 case ISN_PUSHSPEC:
7350 smsg("%s%4d PUSH %s", pfx, current,
7351 get_var_special_name(iptr->isn_arg.number));
7352 break;
7353 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007354 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007355 break;
7356 case ISN_PUSHS:
7357 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
7358 break;
7359 case ISN_PUSHBLOB:
7360 {
7361 char_u *r;
7362 char_u numbuf[NUMBUFLEN];
7363 char_u *tofree;
7364
7365 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
7366 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
7367 vim_free(tofree);
7368 }
7369 break;
7370 case ISN_PUSHFUNC:
7371 {
7372 char *name = (char *)iptr->isn_arg.string;
7373
7374 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
7375 name == NULL ? "[none]" : name);
7376 }
7377 break;
7378 case ISN_PUSHCHANNEL:
7379#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00007380 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007381#endif
7382 break;
7383 case ISN_PUSHJOB:
7384#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00007385 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007386#endif
7387 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00007388 case ISN_PUSHOBJ:
7389 smsg("%s%4d PUSHOBJ null", pfx, current);
7390 break;
7391 case ISN_PUSHCLASS:
7392 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00007393 iptr->isn_arg.classarg == NULL ? "null"
7394 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00007395 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007396 case ISN_PUSHEXC:
7397 smsg("%s%4d PUSH v:exception", pfx, current);
7398 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00007399 case ISN_AUTOLOAD:
7400 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
7401 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007402 case ISN_UNLET:
7403 smsg("%s%4d UNLET%s %s", pfx, current,
7404 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
7405 iptr->isn_arg.unlet.ul_name);
7406 break;
7407 case ISN_UNLETENV:
7408 smsg("%s%4d UNLETENV%s $%s", pfx, current,
7409 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
7410 iptr->isn_arg.unlet.ul_name);
7411 break;
7412 case ISN_UNLETINDEX:
7413 smsg("%s%4d UNLETINDEX", pfx, current);
7414 break;
7415 case ISN_UNLETRANGE:
7416 smsg("%s%4d UNLETRANGE", pfx, current);
7417 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007418 case ISN_LOCKUNLOCK:
7419 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
7420 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007421 case ISN_LOCKCONST:
7422 smsg("%s%4d LOCKCONST", pfx, current);
7423 break;
7424 case ISN_NEWLIST:
7425 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01007426 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007427 break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007428 case ISN_NEWTUPLE:
7429 smsg("%s%4d NEWTUPLE size %lld", pfx, current,
7430 (varnumber_T)(iptr->isn_arg.number));
7431 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007432 case ISN_NEWDICT:
7433 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01007434 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007435 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00007436 case ISN_NEWPARTIAL:
7437 smsg("%s%4d NEWPARTIAL", pfx, current);
7438 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007439
7440 // function call
7441 case ISN_BCALL:
7442 {
7443 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
7444
7445 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
7446 internal_func_name(cbfunc->cbf_idx),
7447 cbfunc->cbf_argcount);
7448 }
7449 break;
7450 case ISN_DCALL:
7451 {
7452 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
7453 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
7454 + cdfunc->cdf_idx;
7455
7456 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007457 printable_func_name(df->df_ufunc),
7458 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007459 }
7460 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00007461 case ISN_METHODCALL:
7462 {
7463 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
7464
7465 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
7466 mfunc->cmf_itf->class_name,
7467 mfunc->cmf_itf->class_obj_methods[
7468 mfunc->cmf_idx]->uf_name,
7469 mfunc->cmf_argcount);
7470 }
7471 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007472 case ISN_UCALL:
7473 {
7474 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
7475
7476 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
7477 cufunc->cuf_name, cufunc->cuf_argcount);
7478 }
7479 break;
7480 case ISN_PCALL:
7481 {
7482 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
7483
7484 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
7485 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
7486 }
7487 break;
7488 case ISN_PCALL_END:
7489 smsg("%s%4d PCALL end", pfx, current);
7490 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01007491 case ISN_DEFER:
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02007492 smsg("%s%4d DEFER %d args", pfx, current,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01007493 (int)iptr->isn_arg.defer.defer_argcount);
7494 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007495 case ISN_RETURN:
7496 smsg("%s%4d RETURN", pfx, current);
7497 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02007498 case ISN_RETURN_VOID:
7499 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007500 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007501 case ISN_RETURN_OBJECT:
7502 smsg("%s%4d RETURN object", pfx, current);
7503 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007504 case ISN_FUNCREF:
7505 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007506 funcref_T *funcref = &iptr->isn_arg.funcref;
7507 funcref_extra_T *extra = funcref->fr_extra;
7508 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007509
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007510 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00007511 {
7512 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
7513 + funcref->fr_dfunc_idx;
7514 name = df->df_ufunc->uf_name;
7515 }
7516 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007517 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00007518 if (extra != NULL && extra->fre_class != NULL)
7519 {
7520 smsg("%s%4d FUNCREF %s.%s", pfx, current,
7521 extra->fre_class->class_name, name);
7522 }
7523 else if (extra == NULL
7524 || extra->fre_loopvar_info.lvi_depth == 0)
7525 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007526 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00007527 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007528 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007529 {
7530 char_u *info = printable_loopvarinfo(
7531 &extra->fre_loopvar_info);
7532
7533 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
7534 name, info);
7535 vim_free(info);
7536 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007537 }
7538 break;
7539
7540 case ISN_NEWFUNC:
7541 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01007542 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007543
Bram Moolenaarcc341812022-09-19 15:54:34 +01007544 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007545 smsg("%s%4d NEWFUNC %s %s", pfx, current,
7546 arg->nfa_lambda, arg->nfa_global);
7547 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007548 {
7549 char_u *info = printable_loopvarinfo(
7550 &arg->nfa_loopvar_info);
7551
7552 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
7553 arg->nfa_lambda, arg->nfa_global, info);
7554 vim_free(info);
7555 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007556 }
7557 break;
7558
7559 case ISN_DEF:
7560 {
7561 char_u *name = iptr->isn_arg.string;
7562
7563 smsg("%s%4d DEF %s", pfx, current,
7564 name == NULL ? (char_u *)"" : name);
7565 }
7566 break;
7567
7568 case ISN_JUMP:
7569 {
7570 char *when = "?";
7571
7572 switch (iptr->isn_arg.jump.jump_when)
7573 {
7574 case JUMP_ALWAYS:
7575 when = "JUMP";
7576 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007577 case JUMP_NEVER:
7578 iemsg("JUMP_NEVER should not be used");
7579 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007580 case JUMP_AND_KEEP_IF_TRUE:
7581 when = "JUMP_AND_KEEP_IF_TRUE";
7582 break;
7583 case JUMP_IF_FALSE:
7584 when = "JUMP_IF_FALSE";
7585 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007586 case JUMP_WHILE_FALSE:
7587 when = "JUMP_WHILE_FALSE"; // unused
7588 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007589 case JUMP_IF_COND_FALSE:
7590 when = "JUMP_IF_COND_FALSE";
7591 break;
7592 case JUMP_IF_COND_TRUE:
7593 when = "JUMP_IF_COND_TRUE";
7594 break;
7595 }
7596 smsg("%s%4d %s -> %d", pfx, current, when,
7597 iptr->isn_arg.jump.jump_where);
7598 }
7599 break;
7600
7601 case ISN_JUMP_IF_ARG_SET:
7602 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7603 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7604 iptr->isn_arg.jump.jump_where);
7605 break;
7606
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007607 case ISN_JUMP_IF_ARG_NOT_SET:
7608 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7609 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7610 iptr->isn_arg.jump.jump_where);
7611 break;
7612
Bram Moolenaar4c137212021-04-19 16:48:48 +02007613 case ISN_FOR:
7614 {
7615 forloop_T *forloop = &iptr->isn_arg.forloop;
7616
7617 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007618 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007619 }
7620 break;
7621
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007622 case ISN_ENDLOOP:
7623 {
7624 endloop_T *endloop = &iptr->isn_arg.endloop;
7625
Bram Moolenaarcc341812022-09-19 15:54:34 +01007626 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7627 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007628 endloop->end_funcref_idx,
7629 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007630 endloop->end_var_idx + endloop->end_var_count - 1,
7631 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007632 }
7633 break;
7634
7635 case ISN_WHILE:
7636 {
7637 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7638
7639 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7640 whileloop->while_funcref_idx,
7641 whileloop->while_end);
7642 }
7643 break;
7644
Bram Moolenaar4c137212021-04-19 16:48:48 +02007645 case ISN_TRY:
7646 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007647 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007648
7649 if (try->try_ref->try_finally == 0)
7650 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7651 pfx, current,
7652 try->try_ref->try_catch,
7653 try->try_ref->try_endtry);
7654 else
7655 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7656 pfx, current,
7657 try->try_ref->try_catch,
7658 try->try_ref->try_finally,
7659 try->try_ref->try_endtry);
7660 }
7661 break;
7662 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007663 smsg("%s%4d CATCH", pfx, current);
7664 break;
7665 case ISN_TRYCONT:
7666 {
7667 trycont_T *trycont = &iptr->isn_arg.trycont;
7668
7669 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7670 trycont->tct_levels,
7671 trycont->tct_levels == 1 ? "" : "s",
7672 trycont->tct_where);
7673 }
7674 break;
7675 case ISN_FINALLY:
7676 smsg("%s%4d FINALLY", pfx, current);
7677 break;
7678 case ISN_ENDTRY:
7679 smsg("%s%4d ENDTRY", pfx, current);
7680 break;
7681 case ISN_THROW:
7682 smsg("%s%4d THROW", pfx, current);
7683 break;
7684
7685 // expression operations on number
7686 case ISN_OPNR:
7687 case ISN_OPFLOAT:
7688 case ISN_OPANY:
7689 {
7690 char *what;
7691 char *ins;
7692
7693 switch (iptr->isn_arg.op.op_type)
7694 {
7695 case EXPR_MULT: what = "*"; break;
7696 case EXPR_DIV: what = "/"; break;
7697 case EXPR_REM: what = "%"; break;
7698 case EXPR_SUB: what = "-"; break;
7699 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007700 case EXPR_LSHIFT: what = "<<"; break;
7701 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007702 default: what = "???"; break;
7703 }
7704 switch (iptr->isn_type)
7705 {
7706 case ISN_OPNR: ins = "OPNR"; break;
7707 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7708 case ISN_OPANY: ins = "OPANY"; break;
7709 default: ins = "???"; break;
7710 }
7711 smsg("%s%4d %s %s", pfx, current, ins, what);
7712 }
7713 break;
7714
7715 case ISN_COMPAREBOOL:
7716 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007717 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007718 case ISN_COMPARENR:
7719 case ISN_COMPAREFLOAT:
7720 case ISN_COMPARESTRING:
7721 case ISN_COMPAREBLOB:
7722 case ISN_COMPARELIST:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007723 case ISN_COMPARETUPLE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007724 case ISN_COMPAREDICT:
7725 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007726 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007727 case ISN_COMPAREANY:
7728 {
7729 char *p;
7730 char buf[10];
7731 char *type;
7732
7733 switch (iptr->isn_arg.op.op_type)
7734 {
7735 case EXPR_EQUAL: p = "=="; break;
7736 case EXPR_NEQUAL: p = "!="; break;
7737 case EXPR_GREATER: p = ">"; break;
7738 case EXPR_GEQUAL: p = ">="; break;
7739 case EXPR_SMALLER: p = "<"; break;
7740 case EXPR_SEQUAL: p = "<="; break;
7741 case EXPR_MATCH: p = "=~"; break;
7742 case EXPR_IS: p = "is"; break;
7743 case EXPR_ISNOT: p = "isnot"; break;
7744 case EXPR_NOMATCH: p = "!~"; break;
7745 default: p = "???"; break;
7746 }
7747 STRCPY(buf, p);
7748 if (iptr->isn_arg.op.op_ic == TRUE)
7749 strcat(buf, "?");
7750 switch(iptr->isn_type)
7751 {
7752 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7753 case ISN_COMPARESPECIAL:
7754 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007755 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007756 case ISN_COMPARENR: type = "COMPARENR"; break;
7757 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7758 case ISN_COMPARESTRING:
7759 type = "COMPARESTRING"; break;
7760 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7761 case ISN_COMPARELIST: type = "COMPARELIST"; break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007762 case ISN_COMPARETUPLE: type = "COMPARETUPLE"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007763 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7764 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007765 case ISN_COMPAREOBJECT:
7766 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007767 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7768 default: type = "???"; break;
7769 }
7770
7771 smsg("%s%4d %s %s", pfx, current, type, buf);
7772 }
7773 break;
7774
7775 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007776 case ISN_ADDTUPLE: smsg("%s%4d ADDTUPLE", pfx, current); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007777 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7778
7779 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007780 case ISN_CONCAT:
7781 smsg("%s%4d CONCAT size %lld", pfx, current,
7782 (varnumber_T)(iptr->isn_arg.number));
7783 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007784 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7785 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7786 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7787 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7788 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7789 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7790 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7791 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007792 case ISN_TUPLEINDEX: smsg("%s%4d TUPLEINDEX", pfx, current); break;
7793 case ISN_TUPLESLICE: smsg("%s%4d TUPLESLICE", pfx, current); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007794 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7795 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7796 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007797 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007798 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7799 iptr->isn_arg.getitem.gi_index,
7800 iptr->isn_arg.getitem.gi_with_op ?
7801 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007802 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7803 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7804 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007805 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7806 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007807 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007808 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007809 pfx, current,
7810 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007811 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007812 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007813 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007814 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007815 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7816 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7817
Bram Moolenaar4c137212021-04-19 16:48:48 +02007818 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7819
Bram Moolenaar4c137212021-04-19 16:48:48 +02007820 case ISN_CHECKTYPE:
7821 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007822 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007823 char *tofree = NULL;
7824 char *typename;
7825
7826 if (ct->ct_type->tt_type == VAR_FLOAT
7827 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7828 typename = "float|number";
7829 else
7830 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007831
7832 if (ct->ct_arg_idx == 0)
7833 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007834 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007835 (int)ct->ct_off);
7836 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007837 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007838 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007839 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007840 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007841 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007842 (int)ct->ct_arg_idx);
7843 vim_free(tofree);
7844 break;
7845 }
7846 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7847 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7848 iptr->isn_arg.checklen.cl_min_len);
7849 break;
7850 case ISN_SETTYPE:
7851 {
7852 char *tofree;
7853
7854 smsg("%s%4d SETTYPE %s", pfx, current,
7855 type_name(iptr->isn_arg.type.ct_type, &tofree));
7856 vim_free(tofree);
7857 break;
7858 }
7859 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007860 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7861 smsg("%s%4d INVERT %d (!val)", pfx, current,
7862 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007863 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007864 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7865 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007866 break;
7867 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007868 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007869 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007870 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7871 pfx, current,
7872 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007873 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007874 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7875 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007876 break;
7877 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007878 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007879 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007880 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007881 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7882 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007883 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007884 else
7885 smsg("%s%4d PUT %c %ld", pfx, current,
7886 iptr->isn_arg.put.put_regname,
7887 (long)iptr->isn_arg.put.put_lnum);
7888 break;
64-bitmane08f10a2025-03-18 22:14:34 +01007889 case ISN_IPUT:
7890 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
7891 smsg("%s%4d IPUT %c above range",
7892 pfx, current, iptr->isn_arg.put.put_regname);
7893 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7894 smsg("%s%4d IPUT %c range",
7895 pfx, current, iptr->isn_arg.put.put_regname);
7896 else
7897 smsg("%s%4d IPUT %c %ld", pfx, current,
7898 iptr->isn_arg.put.put_regname,
7899 (long)iptr->isn_arg.put.put_lnum);
7900 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007901 case ISN_CMDMOD:
7902 {
7903 char_u *buf;
7904 size_t len = produce_cmdmods(
7905 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7906
7907 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007908 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007909 {
7910 (void)produce_cmdmods(
7911 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7912 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7913 vim_free(buf);
7914 }
7915 break;
7916 }
7917 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7918
7919 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007920 smsg("%s%4d PROFILE START line %d", pfx, current,
7921 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007922 break;
7923
7924 case ISN_PROF_END:
7925 smsg("%s%4d PROFILE END", pfx, current);
7926 break;
7927
Bram Moolenaare99d4222021-06-13 14:01:26 +02007928 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007929 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7930 iptr->isn_arg.debug.dbg_break_lnum + 1,
7931 iptr->isn_lnum,
7932 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007933 break;
7934
Bram Moolenaar4c137212021-04-19 16:48:48 +02007935 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7936 iptr->isn_arg.unpack.unp_count,
7937 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7938 break;
7939 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7940 iptr->isn_arg.shuffle.shfl_item,
7941 iptr->isn_arg.shuffle.shfl_up);
7942 break;
7943 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7944
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01007945 case ISN_SCRIPTCTX_SET:
7946 {
7947 int sid = iptr->isn_arg.setsctx.sc_sid;
7948 scriptitem_T *si = SCRIPT_ITEM(sid);
7949 smsg("%s%4d SCRIPTCTX_SET %s", pfx, current, si->sn_name);
7950 script_ctx = iptr->isn_arg.setsctx;
7951 }
7952 break;
7953
Bram Moolenaar4c137212021-04-19 16:48:48 +02007954 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7955 return;
7956 }
7957
7958 out_flush(); // output one line at a time
7959 ui_breakcheck();
7960 if (got_int)
7961 break;
7962 }
7963}
7964
7965/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007966 * Handle command line completion for the :disassemble command.
7967 */
7968 void
7969set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7970{
7971 char_u *p;
7972
7973 // Default: expand user functions, "debug" and "profile"
7974 xp->xp_context = EXPAND_DISASSEMBLE;
7975 xp->xp_pattern = arg;
7976
7977 // first argument already typed: only user function names
7978 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7979 {
7980 xp->xp_context = EXPAND_USER_FUNC;
7981 xp->xp_pattern = skipwhite(p);
7982 }
7983}
7984
7985/*
7986 * Function given to ExpandGeneric() to obtain the list of :disassemble
7987 * arguments.
7988 */
7989 char_u *
7990get_disassemble_argument(expand_T *xp, int idx)
7991{
7992 if (idx == 0)
7993 return (char_u *)"debug";
7994 if (idx == 1)
7995 return (char_u *)"profile";
7996 return get_user_func_name(xp, idx - 2);
7997}
7998
7999/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008000 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01008001 * We don't really need this at runtime, but we do have tests that require it,
8002 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008003 */
8004 void
8005ex_disassemble(exarg_T *eap)
8006{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01008007 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01008008 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008009 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01008010 isn_T *instr = NULL; // init to shut up gcc warning
8011 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01008012 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02008013
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01008014 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02008015 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008016 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02008017 if (func_needs_compiling(ufunc, compile_type)
8018 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02008019 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008020 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008021 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008022 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008023 return;
8024 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02008025 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008026
8027 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02008028 switch (compile_type)
8029 {
8030 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01008031#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02008032 instr = dfunc->df_instr_prof;
8033 instr_count = dfunc->df_instr_prof_count;
8034 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008035#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02008036 // FALLTHROUGH
8037 case CT_NONE:
8038 instr = dfunc->df_instr;
8039 instr_count = dfunc->df_instr_count;
8040 break;
8041 case CT_DEBUG:
8042 instr = dfunc->df_instr_debug;
8043 instr_count = dfunc->df_instr_debug_count;
8044 break;
8045 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008046
Bram Moolenaar4c137212021-04-19 16:48:48 +02008047 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008048}
8049
8050/*
Bram Moolenaar13106602020-10-04 16:06:05 +02008051 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008052 * list, etc. Mostly like what JavaScript does, except that empty list and
8053 * empty dictionary are FALSE.
8054 */
8055 int
8056tv2bool(typval_T *tv)
8057{
8058 switch (tv->v_type)
8059 {
8060 case VAR_NUMBER:
8061 return tv->vval.v_number != 0;
8062 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008063 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008064 case VAR_PARTIAL:
8065 return tv->vval.v_partial != NULL;
8066 case VAR_FUNC:
8067 case VAR_STRING:
8068 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
8069 case VAR_LIST:
8070 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01008071 case VAR_TUPLE:
8072 return tuple_len(tv->vval.v_tuple) > 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008073 case VAR_DICT:
8074 return tv->vval.v_dict != NULL
8075 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
8076 case VAR_BOOL:
8077 case VAR_SPECIAL:
8078 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
8079 case VAR_JOB:
8080#ifdef FEAT_JOB_CHANNEL
8081 return tv->vval.v_job != NULL;
8082#else
8083 break;
8084#endif
8085 case VAR_CHANNEL:
8086#ifdef FEAT_JOB_CHANNEL
8087 return tv->vval.v_channel != NULL;
8088#else
8089 break;
8090#endif
8091 case VAR_BLOB:
8092 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
8093 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02008094 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008095 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02008096 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00008097 case VAR_CLASS:
8098 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02008099 case VAR_TYPEALIAS:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008100 break;
8101 }
8102 return FALSE;
8103}
8104
Bram Moolenaarea2d4072020-11-12 12:08:51 +01008105 void
8106emsg_using_string_as(typval_T *tv, int as_number)
8107{
8108 semsg(_(as_number ? e_using_string_as_number_str
8109 : e_using_string_as_bool_str),
8110 tv->vval.v_string == NULL
8111 ? (char_u *)"" : tv->vval.v_string);
8112}
8113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008114/*
8115 * If "tv" is a string give an error and return FAIL.
8116 */
8117 int
8118check_not_string(typval_T *tv)
8119{
8120 if (tv->v_type == VAR_STRING)
8121 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01008122 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008123 clear_tv(tv);
8124 return FAIL;
8125 }
8126 return OK;
8127}
8128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129#endif // FEAT_EVAL