blob: 4b69dfafa283d709bc4382debb22ba96a9302781 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010067// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100104// Get pointer to item in the stack.
105#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200108#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100110// Get pointer to a local variable on the stack. Negative for arguments.
111#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
112
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200113 void
114to_string_error(vartype_T vartype)
115{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200116 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200117}
118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100120 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 */
122 static int
123ufunc_argcount(ufunc_T *ufunc)
124{
125 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
126}
127
128/*
LemonBoy372bcce2022-04-25 12:43:20 +0100129 * Create a new string from "count" items at the bottom of the stack.
130 * A trailing NUL is appended.
131 * When "count" is zero an empty string is added to the stack.
132 */
133 static int
134exe_concat(int count, ectx_T *ectx)
135{
136 int idx;
137 int len = 0;
138 typval_T *tv;
139 garray_T ga;
140
141 ga_init2(&ga, sizeof(char), 1);
142 // Preallocate enough space for the whole string to avoid having to grow
143 // and copy.
144 for (idx = 0; idx < count; ++idx)
145 {
146 tv = STACK_TV_BOT(idx - count);
147 if (tv->vval.v_string != NULL)
148 len += (int)STRLEN(tv->vval.v_string);
149 }
150
151 if (ga_grow(&ga, len + 1) == FAIL)
152 return FAIL;
153
154 for (idx = 0; idx < count; ++idx)
155 {
156 tv = STACK_TV_BOT(idx - count);
157 ga_concat(&ga, tv->vval.v_string);
158 clear_tv(tv);
159 }
160
161 // add a terminating NUL
162 ga_append(&ga, NUL);
163
164 ectx->ec_stack.ga_len -= count - 1;
165 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
166
167 return OK;
168}
169
170/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200171 * Create a new list from "count" items at the bottom of the stack.
172 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100173 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200174 */
175 static int
176exe_newlist(int count, ectx_T *ectx)
177{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 int idx;
180 typval_T *tv;
181
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100182 if (count >= 0)
183 {
184 list = list_alloc_with_items(count);
185 if (list == NULL)
186 return FAIL;
187 for (idx = 0; idx < count; ++idx)
188 list_set_item(list, idx, STACK_TV_BOT(idx - count));
189 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190
191 if (count > 0)
192 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200193 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100194 {
195 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 else
199 ++ectx->ec_stack.ga_len;
200 tv = STACK_TV_BOT(-1);
201 tv->v_type = VAR_LIST;
202 tv->vval.v_list = list;
Bram Moolenaar566badc2022-09-18 13:46:08 +0100203 tv->v_lock = 0;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100204 if (list != NULL)
205 ++list->lv_refcount;
206 return OK;
207}
208
209/*
210 * Implementation of ISN_NEWDICT.
211 * Returns FAIL on total failure, MAYBE on error.
212 */
213 static int
214exe_newdict(int count, ectx_T *ectx)
215{
216 dict_T *dict = NULL;
217 dictitem_T *item;
218 char_u *key;
219 int idx;
220 typval_T *tv;
221
222 if (count >= 0)
223 {
224 dict = dict_alloc();
225 if (unlikely(dict == NULL))
226 return FAIL;
227 for (idx = 0; idx < count; ++idx)
228 {
229 // have already checked key type is VAR_STRING
230 tv = STACK_TV_BOT(2 * (idx - count));
231 // check key is unique
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000232 key = tv->vval.v_string == NULL ? (char_u *)"" : tv->vval.v_string;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100233 item = dict_find(dict, key, -1);
234 if (item != NULL)
235 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000236 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100237 dict_unref(dict);
238 return MAYBE;
239 }
240 item = dictitem_alloc(key);
241 clear_tv(tv);
242 if (unlikely(item == NULL))
243 {
244 dict_unref(dict);
245 return FAIL;
246 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100247 tv = STACK_TV_BOT(2 * (idx - count) + 1);
248 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100249 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100250 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100251 if (dict_add(dict, item) == FAIL)
252 {
253 // can this ever happen?
254 dict_unref(dict);
255 return FAIL;
256 }
257 }
258 }
259
260 if (count > 0)
261 ectx->ec_stack.ga_len -= 2 * count - 1;
262 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Christian Brabandt86eddce2024-03-26 18:42:52 +0100263 {
264 dict_unref(dict);
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100265 return FAIL;
Christian Brabandt86eddce2024-03-26 18:42:52 +0100266 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100267 else
268 ++ectx->ec_stack.ga_len;
269 tv = STACK_TV_BOT(-1);
270 tv->v_type = VAR_DICT;
271 tv->v_lock = 0;
272 tv->vval.v_dict = dict;
273 if (dict != NULL)
274 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200275 return OK;
276}
277
278/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200279 * If debug_tick changed check if "ufunc" has a breakpoint and update
280 * "uf_has_breakpoint".
281 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000282 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200283update_has_breakpoint(ufunc_T *ufunc)
284{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000285 if (ufunc->uf_debug_tick == debug_tick)
286 return;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200287
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000288 linenr_T breakpoint;
289
290 ufunc->uf_debug_tick = debug_tick;
291 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
292 ufunc->uf_has_breakpoint = breakpoint > 0;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200293}
294
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200295static garray_T dict_stack = GA_EMPTY;
296
297/*
298 * Put a value on the dict stack. This consumes "tv".
299 */
300 static int
301dict_stack_save(typval_T *tv)
302{
303 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000304 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200305 if (ga_grow(&dict_stack, 1) == FAIL)
306 return FAIL;
307 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
308 ++dict_stack.ga_len;
309 return OK;
310}
311
312/*
313 * Get the typval at top of the dict stack.
314 */
315 static typval_T *
316dict_stack_get_tv(void)
317{
318 if (dict_stack.ga_len == 0)
319 return NULL;
320 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
321}
322
323/*
324 * Get the dict at top of the dict stack.
325 */
326 static dict_T *
327dict_stack_get_dict(void)
328{
329 typval_T *tv;
330
331 if (dict_stack.ga_len == 0)
332 return NULL;
333 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
334 if (tv->v_type == VAR_DICT)
335 return tv->vval.v_dict;
336 return NULL;
337}
338
339/*
340 * Drop an item from the dict stack.
341 */
342 static void
343dict_stack_drop(void)
344{
345 if (dict_stack.ga_len == 0)
346 {
347 iemsg("Dict stack underflow");
348 return;
349 }
350 --dict_stack.ga_len;
351 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
352}
353
354/*
355 * Drop items from the dict stack until the length is equal to "len".
356 */
357 static void
358dict_stack_clear(int len)
359{
360 while (dict_stack.ga_len > len)
361 dict_stack_drop();
362}
363
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200364/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000365 * Get a pointer to useful "pt_outer" of "pt".
366 */
367 static outer_T *
368get_pt_outer(partial_T *pt)
369{
370 partial_T *ptref = pt->pt_outer_partial;
371
372 if (ptref == NULL)
373 return &pt->pt_outer;
374
375 // partial using partial (recursively)
376 while (ptref->pt_outer_partial != NULL)
377 ptref = ptref->pt_outer_partial;
378 return &ptref->pt_outer;
379}
380
381/*
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000382 * Check "argcount" arguments on the stack against what "ufunc" expects.
383 * "off" is the offset of arguments on the stack.
384 * Return OK or FAIL.
385 */
386 static int
387check_ufunc_arg_types(ufunc_T *ufunc, int argcount, int off, ectx_T *ectx)
388{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000389 if (ufunc->uf_arg_types == NULL && ufunc->uf_va_type == NULL)
390 return OK;
391
392 typval_T *argv = STACK_TV_BOT(0) - argcount - off;
393
394 // The function can change at runtime, check that the argument
395 // types are correct.
396 for (int i = 0; i < argcount; ++i)
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000397 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000398 type_T *type = NULL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000399
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000400 // assume a v:none argument, using the default value, is always OK
401 if (argv[i].v_type == VAR_SPECIAL
402 && argv[i].vval.v_number == VVAL_NONE)
403 continue;
Ernie Raelb077b582023-12-14 20:11:44 +0100404 // only pass values to user functions, never types
405 if (check_typval_is_value(&argv[i]) == FAIL)
406 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000407
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000408 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
409 type = ufunc->uf_arg_types[i];
410 else if (ufunc->uf_va_type != NULL)
411 type = ufunc->uf_va_type->tt_member;
412 if (type != NULL && check_typval_arg_type(type,
413 &argv[i], NULL, i + 1) == FAIL)
414 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000415 }
416 return OK;
417}
418
419/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100421 * This adds a stack frame and sets the instruction pointer to the start of the
422 * called function.
Bram Moolenaar5800c792022-09-22 17:34:01 +0100423 * If "pt_arg" is not NULL use "pt_arg->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100424 *
425 * Stack has:
426 * - current arguments (already there)
427 * - omitted optional argument (default values) added here
428 * - stack frame:
429 * - pointer to calling function
430 * - Index of next instruction in calling function
431 * - previous frame pointer
432 * - reserved space for local variables
433 */
434 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100435call_dfunc(
436 int cdf_idx,
Bram Moolenaar5800c792022-09-22 17:34:01 +0100437 partial_T *pt_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100438 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100439 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100440{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100441 int argcount = argcount_arg;
442 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
443 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200444 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100445 int arg_to_add;
446 int vararg_count = 0;
447 int varcount;
448 int idx;
449 estack_T *entry;
450 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200451 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000452 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453
454 if (dfunc->df_deleted)
455 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100456 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000457 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100458 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100459 return FAIL;
460 }
461
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100462#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100463 if (do_profiling == PROF_YES)
464 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200465 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100466 {
467 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
468 + profile_info_ga.ga_len;
469 ++profile_info_ga.ga_len;
470 CLEAR_POINTER(info);
471 profile_may_start_func(info, ufunc,
472 (((dfunc_T *)def_functions.ga_data)
473 + ectx->ec_dfunc_idx)->df_ufunc);
474 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100475 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100476#endif
477
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200478 // When debugging and using "cont" switches to the not-debugged
479 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000480 compile_type = get_compile_type(ufunc);
481 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200482 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000483 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200484
485 // compile_def_function() may cause def_functions.ga_data to change
486 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
487 }
488 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200489 {
490 if (did_emsg_cumul + did_emsg == did_emsg_before)
491 semsg(_(e_function_is_not_compiled_str),
492 printable_func_name(ufunc));
493 return FAIL;
494 }
495
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200496 if (ufunc->uf_va_name != NULL)
497 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200498 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200499 // Stack at time of call with 2 varargs:
500 // normal_arg
501 // optional_arg
502 // vararg_1
503 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200504 // After creating the list:
505 // normal_arg
506 // optional_arg
507 // vararg-list
508 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200509 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200510 // After creating the list
511 // normal_arg
512 // (space for optional_arg)
513 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200514 vararg_count = argcount - ufunc->uf_args.ga_len;
515 if (vararg_count < 0)
516 vararg_count = 0;
517 else
518 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200519 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200520 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200521
522 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200523 }
524
Bram Moolenaarfe270812020-04-11 22:31:27 +0200525 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200526 if (arg_to_add < 0)
527 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100528 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
529 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200530 return FAIL;
531 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000532 else if (arg_to_add > ufunc->uf_def_args.ga_len)
533 {
534 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
535
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100536 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
537 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000538 return FAIL;
539 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200540
Bram Moolenaar574950d2023-01-03 19:08:50 +0000541 // If this is an object method, the object is just before the arguments.
542 typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
543
Yegappan Lakshmananc1946262023-09-25 21:00:46 +0200544 if (IS_OBJECT_METHOD(ufunc) && !IS_CONSTRUCTOR_METHOD(ufunc)
545 && obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL)
546 {
547 // If this is not a constructor method, then a valid object is
548 // needed.
549 emsg(_(e_using_null_object));
550 return FAIL;
551 }
552
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000553 // Check the argument types.
554 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
555 return FAIL;
556
mityua5550692023-11-25 15:41:20 +0100557 // While check_ufunc_arg_types call, def function compilation process may
558 // run. If so many def functions are compiled, def_functions array may be
559 // reallocated and dfunc may no longer have valid pointer. Get the object
560 // pointer from def_functions again here.
561 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
562
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200563 // Reserve space for:
564 // - missing arguments
565 // - stack frame
566 // - local variables
567 // - if needed: a counter for number of closures created in
568 // ectx->ec_funcrefs.
569 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100570 if (GA_GROW_FAILS(&ectx->ec_stack,
571 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 return FAIL;
573
Yegappan Lakshmanan1087b8c2023-10-07 22:03:18 +0200574 // The object pointer is in the execution typval stack. The GA_GROW call
575 // above may have reallocated the execution typval stack. So the object
576 // pointer may not be valid anymore. Get the object pointer again from the
577 // execution stack.
578 obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
579
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100580 // If depth of calling is getting too high, don't execute the function.
581 if (funcdepth_increment() == FAIL)
582 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200583 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100584
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100585 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200586 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100587 {
588 floc = ALLOC_ONE(funclocal_T);
589 if (floc == NULL)
590 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200591 *floc = ectx->ec_funclocal;
592 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100593 }
594
Bram Moolenaarfe270812020-04-11 22:31:27 +0200595 // Move the vararg-list to below the missing optional arguments.
596 if (vararg_count > 0 && arg_to_add > 0)
597 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100598
599 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200600 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200601 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200602 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603
604 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100605 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
606 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200607 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200608 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
609 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100610 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100611 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200612 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613
Bram Moolenaar5800c792022-09-22 17:34:01 +0100614 // Initialize all local variables to number zero. Also initialize the
615 // variable that counts how many closures were created. This is used in
616 // handle_closure_in_use().
617 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
618 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000619 {
620 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
621
622 tv->v_type = VAR_NUMBER;
623 tv->vval.v_number = 0;
624 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200625 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626
Bram Moolenaar574950d2023-01-03 19:08:50 +0000627 // For an object method move the object from just before the arguments to
628 // the first local variable.
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +0200629 if (IS_OBJECT_METHOD(ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +0000630 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200631 if (obj->v_type != VAR_OBJECT)
632 {
633 semsg(_(e_internal_error_str), "type in stack is not an object");
634 return FAIL;
635 }
636
Bram Moolenaar574950d2023-01-03 19:08:50 +0000637 *STACK_TV_VAR(0) = *obj;
638 obj->v_type = VAR_UNKNOWN;
639 }
640
Bram Moolenaar5800c792022-09-22 17:34:01 +0100641 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
642 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100643 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200644 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100645
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200646 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100647 return FAIL;
648 if (pt != NULL)
649 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000650 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200651 ++pt->pt_refcount;
652 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100653 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100654 else
655 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200656 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200657 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200658 {
659 vim_free(ref);
660 return FAIL;
661 }
662 ref->or_outer_allocated = TRUE;
663 ref->or_outer->out_stack = &ectx->ec_stack;
664 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
665 if (ectx->ec_outer_ref != NULL)
666 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100667 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200668 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100669 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100670 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200671 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100672
Bram Moolenaarc970e422021-03-17 15:03:04 +0100673 ++ufunc->uf_calls;
674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675 // Set execution state to the start of the called function.
676 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100677 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100678 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200679 if (entry != NULL)
680 {
681 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200682 // Save the current context so it can be restored on return.
683 entry->es_save_sctx = current_sctx;
684 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200685 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100686
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200687 // Start execution at the first instruction.
688 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689
690 return OK;
691}
692
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000693// Double linked list of funcstack_T in use.
694static funcstack_T *first_funcstack = NULL;
695
696 static void
697add_funcstack_to_list(funcstack_T *funcstack)
698{
699 // Link in list of funcstacks.
700 if (first_funcstack != NULL)
701 first_funcstack->fs_prev = funcstack;
702 funcstack->fs_next = first_funcstack;
703 funcstack->fs_prev = NULL;
704 first_funcstack = funcstack;
705}
706
707 static void
708remove_funcstack_from_list(funcstack_T *funcstack)
709{
710 if (funcstack->fs_prev == NULL)
711 first_funcstack = funcstack->fs_next;
712 else
713 funcstack->fs_prev->fs_next = funcstack->fs_next;
714 if (funcstack->fs_next != NULL)
715 funcstack->fs_next->fs_prev = funcstack->fs_prev;
716}
717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200719 * Used when returning from a function: Check if any closure is still
720 * referenced. If so then move the arguments and variables to a separate piece
721 * of stack to be used when the closure is called.
722 * When "free_arguments" is TRUE the arguments are to be freed.
723 * Returns FAIL when out of memory.
724 */
725 static int
726handle_closure_in_use(ectx_T *ectx, int free_arguments)
727{
728 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
729 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200730 int argcount;
731 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200732 int idx;
733 typval_T *tv;
734 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200735 garray_T *gap = &ectx->ec_funcrefs;
736 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200737
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200738 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200739 return OK; // function was freed
740 if (dfunc->df_has_closure == 0)
741 return OK; // no closures
742 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
743 closure_count = tv->vval.v_number;
744 if (closure_count == 0)
745 return OK; // no funcrefs created
746
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100747 // Compute "top": the first entry in the stack used by the function.
748 // This is the first argument (after that comes the stack frame and then
749 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200750 argcount = ufunc_argcount(dfunc->df_ufunc);
751 top = ectx->ec_frame_idx - argcount;
752
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200753 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200754 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200755 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200756 partial_T *pt;
757 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200758
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200759 if (off < 0)
760 continue; // count is off or already done
761 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200762 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200763 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200764 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200765 int i;
766
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000767 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200768 // unreferenced on return.
769 for (i = 0; i < dfunc->df_varcount; ++i)
770 {
771 typval_T *stv = STACK_TV(ectx->ec_frame_idx
772 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200773 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200774 --refcount;
775 }
776 if (refcount > 1)
777 {
778 closure_in_use = TRUE;
779 break;
780 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200781 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200782 }
783
784 if (closure_in_use)
785 {
786 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
787 typval_T *stack;
788
789 // A closure is using the arguments and/or local variables.
790 // Move them to the called function.
791 if (funcstack == NULL)
792 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000793
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200794 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100795 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
796 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200797 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
798 funcstack->fs_ga.ga_data = stack;
799 if (stack == NULL)
800 {
801 vim_free(funcstack);
802 return FAIL;
803 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000804 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200805
806 // Move or copy the arguments.
807 for (idx = 0; idx < argcount; ++idx)
808 {
809 tv = STACK_TV(top + idx);
810 if (free_arguments)
811 {
812 *(stack + idx) = *tv;
813 tv->v_type = VAR_UNKNOWN;
814 }
815 else
816 copy_tv(tv, stack + idx);
817 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100818 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200819 // Move the local variables.
820 for (idx = 0; idx < dfunc->df_varcount; ++idx)
821 {
822 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200823
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200824 // A partial created for a local function, that is also used as a
825 // local variable, has a reference count for the variable, thus
826 // will never go down to zero. When all these refcounts are one
827 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000828 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200829 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
830 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200831 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200832
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200833 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200834 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
835 gap->ga_len - closure_count + i])
836 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200837 }
838
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200839 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200840 tv->v_type = VAR_UNKNOWN;
841 }
842
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200843 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200844 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200845 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
846 - closure_count + idx];
847 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200848 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200849 ++funcstack->fs_refcount;
850 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100851 pt->pt_outer.out_stack = &funcstack->fs_ga;
852 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200853 }
854 }
855 }
856
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200857 for (idx = 0; idx < closure_count; ++idx)
858 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
859 - closure_count + idx]);
860 gap->ga_len -= closure_count;
861 if (gap->ga_len == 0)
862 ga_clear(gap);
863
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200864 return OK;
865}
866
867/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200868 * Called when a partial is freed or its reference count goes down to one. The
869 * funcstack may be the only reference to the partials in the local variables.
870 * Go over all of them, the funcref and can be freed if all partials
871 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100872 * Returns TRUE if the funcstack is freed, the partial referencing it will then
873 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200874 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100875 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200876funcstack_check_refcount(funcstack_T *funcstack)
877{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100878 int i;
879 garray_T *gap = &funcstack->fs_ga;
880 int done = 0;
881 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200882
883 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100884 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200885 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
886 {
887 typval_T *tv = ((typval_T *)gap->ga_data) + i;
888
889 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
890 && tv->vval.v_partial->pt_funcstack == funcstack
891 && tv->vval.v_partial->pt_refcount == 1)
892 ++done;
893 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100894 if (done != funcstack->fs_min_refcount)
895 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200896
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100897 stack = gap->ga_data;
898
899 // All partials referencing the funcstack have a reference count of
900 // one, thus the funcstack is no longer of use.
901 for (i = 0; i < gap->ga_len; ++i)
902 clear_tv(stack + i);
903 vim_free(stack);
904 remove_funcstack_from_list(funcstack);
905 vim_free(funcstack);
906
907 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200908}
909
910/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000911 * For garbage collecting: set references in all variables referenced by
912 * all funcstacks.
913 */
914 int
915set_ref_in_funcstacks(int copyID)
916{
917 funcstack_T *funcstack;
918
919 for (funcstack = first_funcstack; funcstack != NULL;
920 funcstack = funcstack->fs_next)
921 {
922 typval_T *stack = funcstack->fs_ga.ga_data;
923 int i;
924
925 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
926 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
927 return TRUE; // abort
928 }
929 return FALSE;
930}
931
Bram Moolenaar806a2732022-09-04 15:40:36 +0100932// Ugly static to avoid passing the execution context around through many
933// layers.
934static ectx_T *current_ectx = NULL;
935
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000936/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100937 * Return TRUE if currently executing a :def function.
938 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100939 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100940 int
941in_def_function(void)
942{
943 return current_ectx != NULL;
944}
945
946/*
Ernie Rael4c8da022023-10-11 21:35:11 +0200947 * If executing a class/object method, then fill in the lval_T.
948 * Set lr_tv to the executing item, and lr_exec_class to the executing class;
949 * use free_tv and class_unref when finished with the lval_root.
950 * For use by builtin functions.
951 *
952 * Return FAIL and do nothing if not executing in a class; otherwise OK.
953 */
954 int
955fill_exec_lval_root(lval_root_T *root)
956{
957 ectx_T *ectx = current_ectx;
958 if (ectx != NULL)
959 {
960 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
961 + current_ectx->ec_dfunc_idx;
962 ufunc_T *ufunc = dfunc->df_ufunc;
963 if (ufunc->uf_class != NULL) // executing a method?
964 {
965 typval_T *tv = alloc_tv();
966 if (tv != NULL)
967 {
968 CLEAR_POINTER(root);
969 root->lr_tv = tv;
970 copy_tv(STACK_TV_VAR(0), root->lr_tv);
971 root->lr_cl_exec = ufunc->uf_class;
972 ++root->lr_cl_exec->class_refcount;
973 return OK;
974 }
975 }
976 }
977
978 return FAIL;
979}
980
981/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100982 * Clear "current_ectx" and return the previous value. To be used when calling
983 * a user function.
984 */
985 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000986clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100987{
988 ectx_T *r = current_ectx;
989
990 current_ectx = NULL;
991 return r;
992}
993
994 void
995restore_current_ectx(ectx_T *ectx)
996{
997 if (current_ectx != NULL)
998 iemsg("Restoring current_ectx while it is not NULL");
999 current_ectx = ectx;
1000}
1001
1002/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01001003 * Add an entry for a deferred function call to the currently executing
1004 * function.
1005 * Return the list or NULL when failed.
1006 */
1007 static list_T *
1008add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001009{
1010 typval_T *defer_tv = STACK_TV_VAR(var_idx);
1011 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001012 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001013 typval_T listval;
1014
1015 if (defer_tv->v_type != VAR_LIST)
1016 {
Bram Moolenaara5348f22022-09-04 11:42:22 +01001017 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001018 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001019 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001020 }
1021 defer_l = defer_tv->vval.v_list;
1022
1023 l = list_alloc_with_items(argcount + 1);
1024 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001025 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001026 listval.v_type = VAR_LIST;
1027 listval.vval.v_list = l;
1028 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +01001029 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001030 {
1031 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001032 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001033 }
1034
Bram Moolenaar806a2732022-09-04 15:40:36 +01001035 return l;
1036}
1037
1038/*
1039 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
1040 * The local variable that lists deferred functions is "var_idx".
1041 * Returns OK or FAIL.
1042 */
1043 static int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001044defer_command(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001045{
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001046 list_T *l = add_defer_item(var_idx, argcount, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001047 int i;
1048 typval_T *func_tv;
1049
1050 if (l == NULL)
1051 return FAIL;
1052
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001053 func_tv = STACK_TV_BOT(-argcount - 1);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001054 if (func_tv->v_type != VAR_PARTIAL && func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001055 {
1056 semsg(_(e_expected_str_but_got_str),
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001057 "function or partial",
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001058 vartype_name(func_tv->v_type));
1059 return FAIL;
1060 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001061 list_set_item(l, 0, func_tv);
1062
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001063 for (i = 0; i < argcount; ++i)
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001064 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
1065 ectx->ec_stack.ga_len -= argcount + 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001066 return OK;
1067}
1068
1069/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001070 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001071 * Consumes "name", also on failure.
1072 * Only to be called when in_def_function() returns TRUE.
1073 */
1074 int
1075add_defer_function(char_u *name, int argcount, typval_T *argvars)
1076{
1077 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1078 + current_ectx->ec_dfunc_idx;
1079 list_T *l;
1080 typval_T func_tv;
1081 int i;
1082
1083 if (dfunc->df_defer_var_idx == 0)
1084 {
1085 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001086 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001087 return FAIL;
1088 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001089
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001090 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001091 if (l == NULL)
1092 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001093 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001094 return FAIL;
1095 }
1096
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001097 func_tv.v_type = VAR_FUNC;
1098 func_tv.v_lock = 0;
1099 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001100 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001101
Bram Moolenaar806a2732022-09-04 15:40:36 +01001102 for (i = 0; i < argcount; ++i)
1103 list_set_item(l, i + 1, argvars + i);
1104 return OK;
1105}
1106
1107/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001108 * Invoked when returning from a function: Invoke any deferred calls.
1109 */
1110 static void
1111invoke_defer_funcs(ectx_T *ectx)
1112{
1113 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1114 + ectx->ec_dfunc_idx;
1115 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1116 listitem_T *li;
1117
1118 if (defer_tv->v_type != VAR_LIST)
1119 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001120 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001121 {
1122 list_T *l = li->li_tv.vval.v_list;
1123 typval_T rettv;
1124 typval_T argvars[MAX_FUNC_ARGS];
1125 int i;
1126 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001127 typval_T *functv = &l->lv_first->li_tv;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001128 int argcount = l->lv_len - 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001129
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001130 if (functv->vval.v_string == NULL)
1131 // already being called, can happen if function does ":qa"
1132 continue;
1133
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001134 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001135 {
1136 arg_li = arg_li->li_next;
1137 argvars[i] = arg_li->li_tv;
1138 }
1139
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001140 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001141 CLEAR_FIELD(funcexe);
1142 funcexe.fe_evaluate = TRUE;
1143 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001144 if (functv->v_type == VAR_PARTIAL)
1145 {
1146 funcexe.fe_partial = functv->vval.v_partial;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001147 funcexe.fe_object = functv->vval.v_partial->pt_obj;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001148 if (funcexe.fe_object != NULL)
1149 ++funcexe.fe_object->obj_refcount;
1150 }
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001151
1152 char_u *name = functv->vval.v_string;
1153 functv->vval.v_string = NULL;
1154
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001155 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001156 // first statement in the function will be executed (because of the
1157 // exception). So save and restore the try/catch/throw exception
1158 // state.
1159 exception_state_T estate;
1160 exception_state_save(&estate);
1161 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001162
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001163 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1164
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001165 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001166
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001167 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001168 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001169 }
1170}
1171
1172/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 * Return from the current function.
1174 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001175 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001176func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001179 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001180 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1181 + ectx->ec_dfunc_idx;
1182 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001183 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001184 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1185 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001186 funclocal_T *floc;
1187#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001188 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1189 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190
Bram Moolenaar12d26532021-02-19 19:13:21 +01001191 if (do_profiling == PROF_YES)
1192 {
1193 ufunc_T *caller = prev_dfunc->df_ufunc;
1194
1195 if (dfunc->df_ufunc->uf_profiling
1196 || (caller != NULL && caller->uf_profiling))
1197 {
1198 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1199 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1200 --profile_info_ga.ga_len;
1201 }
1202 }
1203#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001204
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001205 if (dfunc->df_defer_var_idx > 0)
1206 invoke_defer_funcs(ectx);
1207
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001208 // No check for uf_refcount being zero, cannot think of a way that would
1209 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001210 --dfunc->df_ufunc->uf_calls;
1211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001213 entry = estack_pop();
1214 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001215 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001217 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1218 return FAIL;
1219
Bram Moolenaar574950d2023-01-03 19:08:50 +00001220 // Clear the arguments. If this was an object method also clear the
1221 // object, it is just before the arguments.
1222 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001223 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001224 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001225 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1226 clear_tv(STACK_TV(idx));
1227
1228 // Clear local variables and temp values, but not the return value.
1229 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001230 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001232
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001233 // The return value should be on top of the stack. However, when aborting
1234 // it may not be there and ec_frame_idx is the top of the stack.
1235 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001236 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001237 ret_idx = 0;
1238
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001239 if (ectx->ec_outer_ref != NULL)
1240 {
1241 if (ectx->ec_outer_ref->or_outer_allocated)
1242 vim_free(ectx->ec_outer_ref->or_outer);
1243 partial_unref(ectx->ec_outer_ref->or_partial);
1244 vim_free(ectx->ec_outer_ref);
1245 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001246
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001247 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001248 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001249 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1250 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001251 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1252 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001253 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001254 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001255 floc = (void *)STACK_TV(ectx->ec_frame_idx
1256 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001257 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001258 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1259 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001260
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001261 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001262 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001263 else
1264 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001265 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001266 vim_free(floc);
1267 }
1268
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001269 if (ret_idx > 0)
1270 {
1271 // Reset the stack to the position before the call, with a spot for the
1272 // return value, moved there from above the frame.
1273 ectx->ec_stack.ga_len = top + 1;
1274 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1275 }
1276 else
1277 // Reset the stack to the position before the call.
1278 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001279
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001280 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001281 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001282 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283}
1284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285/*
1286 * Prepare arguments and rettv for calling a builtin or user function.
1287 */
1288 static int
1289call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1290{
1291 int idx;
1292 typval_T *tv;
1293
1294 // Move arguments from bottom of the stack to argvars[] and add terminator.
1295 for (idx = 0; idx < argcount; ++idx)
1296 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1297 argvars[argcount].v_type = VAR_UNKNOWN;
1298
1299 // Result replaces the arguments on the stack.
1300 if (argcount > 0)
1301 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001302 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 return FAIL;
1304 else
1305 ++ectx->ec_stack.ga_len;
1306
1307 // Default return value is zero.
1308 tv = STACK_TV_BOT(-1);
1309 tv->v_type = VAR_NUMBER;
1310 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001311 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312
1313 return OK;
1314}
1315
1316/*
1317 * Call a builtin function by index.
1318 */
1319 static int
1320call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1321{
1322 typval_T argvars[MAX_FUNC_ARGS];
1323 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001324 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001325 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001326 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
1328 if (call_prepare(argcount, argvars, ectx) == FAIL)
1329 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001330 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001332 // Call the builtin function. Set "current_ectx" so that when it
1333 // recursively invokes call_def_function() a closure context can be set.
1334 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001336 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001337 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338
1339 // Clear the arguments.
1340 for (idx = 0; idx < argcount; ++idx)
1341 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001342
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001343 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001344 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 return OK;
1346}
1347
1348/*
1349 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001350 * If the function is compiled this will add a stack frame and set the
1351 * instruction pointer at the start of the function.
1352 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001353 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001354 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 */
1356 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001357call_ufunc(
1358 ufunc_T *ufunc,
1359 partial_T *pt,
1360 int argcount,
1361 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001362 isn_T *iptr,
1363 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364{
1365 typval_T argvars[MAX_FUNC_ARGS];
1366 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001367 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001369 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001370 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371
Bram Moolenaare99d4222021-06-13 14:01:26 +02001372 if (func_needs_compiling(ufunc, compile_type)
1373 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1374 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001375 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001376 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001377 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001378 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001379 if (error != FCERR_UNKNOWN)
1380 {
1381 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001382 semsg(_(e_too_many_arguments_for_function_str),
1383 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001384 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001385 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001386 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001387 return FAIL;
1388 }
1389
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001390 // The function has been compiled, can call it quickly. For a function
1391 // that was defined later: we can call it directly next time.
1392 if (iptr != NULL)
1393 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001394 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001395 iptr->isn_type = ISN_DCALL;
1396 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1397 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1398 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001399 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401
1402 if (call_prepare(argcount, argvars, ectx) == FAIL)
1403 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001404 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001405 funcexe.fe_evaluate = TRUE;
1406 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407
1408 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001410 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411
1412 // Clear the arguments.
1413 for (idx = 0; idx < argcount; ++idx)
1414 clear_tv(&argvars[idx]);
1415
1416 if (error != FCERR_NONE)
1417 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001418 user_func_error(error, printable_func_name(ufunc),
1419 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 return FAIL;
1421 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001422 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001423 // Error other than from calling the function itself.
1424 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 return OK;
1426}
1427
1428/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001429 * If command modifiers were applied restore them.
1430 */
1431 static void
1432may_restore_cmdmod(funclocal_T *funclocal)
1433{
1434 if (funclocal->floc_restore_cmdmod)
1435 {
1436 cmdmod.cmod_filter_regmatch.regprog = NULL;
1437 undo_cmdmod(&cmdmod);
1438 cmdmod = funclocal->floc_save_cmdmod;
1439 funclocal->floc_restore_cmdmod = FALSE;
1440 }
1441}
1442
1443/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001444 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1445 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001446 */
1447 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001448vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001449{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001450 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001451}
1452
1453/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 * Execute a function by "name".
1455 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001456 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 * Returns FAIL if not found without an error message.
1458 */
1459 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001460call_by_name(
1461 char_u *name,
1462 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001463 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001464 isn_T *iptr,
1465 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466{
1467 ufunc_T *ufunc;
1468
1469 if (builtin_function(name, -1))
1470 {
1471 int func_idx = find_internal_func(name);
1472
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001473 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001475 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 return FAIL;
1477 return call_bfunc(func_idx, argcount, ectx);
1478 }
1479
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001480 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001481
1482 if (ufunc == NULL)
1483 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001484 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001485
1486 if (script_autoload(name, TRUE))
1487 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001488 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001489
1490 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001491 return FAIL; // bail out if loading the script caused an error
1492 }
1493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001495 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001496 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1497 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001498
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001499 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501
1502 return FAIL;
1503}
1504
1505 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001506call_partial(
1507 typval_T *tv,
1508 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001509 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001511 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001512 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001514 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001515 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516
1517 if (tv->v_type == VAR_PARTIAL)
1518 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001519 partial_T *pt = tv->vval.v_partial;
1520 int i;
1521
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001522 if (pt->pt_obj != NULL)
1523 {
1524 // partial with an object method. Push the object before the
1525 // function arguments.
1526 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1527 return FAIL;
1528 for (i = 1; i <= argcount; ++i)
1529 *STACK_TV_BOT(-i + 1) = *STACK_TV_BOT(-i);
1530
1531 typval_T *obj_tv = STACK_TV_BOT(-argcount);
1532 obj_tv->v_type = VAR_OBJECT;
1533 obj_tv->v_lock = 0;
1534 obj_tv->vval.v_object = pt->pt_obj;
1535 ++pt->pt_obj->obj_refcount;
1536 ++ectx->ec_stack.ga_len;
1537 }
1538
Bram Moolenaara90afb92020-07-15 22:38:56 +02001539 if (pt->pt_argc > 0)
1540 {
1541 // Make space for arguments from the partial, shift the "argcount"
1542 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001543 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001544 return FAIL;
1545 for (i = 1; i <= argcount; ++i)
1546 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1547 ectx->ec_stack.ga_len += pt->pt_argc;
1548 argcount += pt->pt_argc;
1549
1550 // copy the arguments from the partial onto the stack
1551 for (i = 0; i < pt->pt_argc; ++i)
1552 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1553 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001554 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555
1556 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001557 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 name = pt->pt_name;
1560 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001561 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001563 if (name != NULL)
1564 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001565 char_u fname_buf[FLEN_FIXED + 1];
1566 char_u *tofree = NULL;
1567 funcerror_T error = FCERR_NONE;
1568 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001569
1570 // May need to translate <SNR>123_ to K_SNR.
1571 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1572 if (error != FCERR_NONE)
1573 res = FAIL;
1574 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001575 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001576 vim_free(tofree);
1577 }
1578
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001579 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 {
1581 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001582 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001583 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 return FAIL;
1585 }
1586 return OK;
1587}
1588
1589/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001590 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1591 * TRUE.
1592 */
1593 static int
1594error_if_locked(int lock, char *error)
1595{
1596 if (lock & (VAR_LOCKED | VAR_FIXED))
1597 {
1598 emsg(_(error));
1599 return TRUE;
1600 }
1601 return FALSE;
1602}
1603
1604/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001605 * Give an error if "tv" is not a number and return FAIL.
1606 */
1607 static int
1608check_for_number(typval_T *tv)
1609{
1610 if (tv->v_type != VAR_NUMBER)
1611 {
1612 semsg(_(e_expected_str_but_got_str),
1613 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1614 return FAIL;
1615 }
1616 return OK;
1617}
1618
1619/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001620 * Store "tv" in variable "name".
1621 * This is for s: and g: variables.
1622 */
1623 static void
1624store_var(char_u *name, typval_T *tv)
1625{
1626 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001627 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001628
Bram Moolenaard877a572021-04-01 19:42:48 +02001629 if (tv->v_lock)
1630 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001631 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001632 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001633 restore_funccal();
1634}
1635
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001636/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001637 * Convert "tv" to a string.
1638 * Return FAIL if not allowed.
1639 */
1640 static int
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001641do_2string(typval_T *tv, int is_2string_any, int tostring_flags)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001642{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001643 if (tv->v_type == VAR_STRING)
1644 return OK;
1645
1646 char_u *str;
1647
1648 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001649 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001650 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001651 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001652 case VAR_SPECIAL:
1653 case VAR_BOOL:
1654 case VAR_NUMBER:
1655 case VAR_FLOAT:
Yegappan Lakshmananf01493c2024-04-14 23:21:02 +02001656 case VAR_DICT:
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001657 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001658
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001659 case VAR_LIST:
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001660 if (tostring_flags & TOSTRING_TOLERANT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001661 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001662 char_u *s, *e, *p;
1663 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001664
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001665 ga_init2(&ga, sizeof(char_u *), 1);
1666
1667 // Convert to NL separated items, then
1668 // escape the items and replace the NL with
1669 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001670 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001671 if (str == NULL)
1672 return FAIL;
1673 s = str;
1674 while ((e = vim_strchr(s, '\n')) != NULL)
1675 {
1676 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001677 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001678 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001679 if (p != NULL)
1680 {
1681 ga_concat(&ga, p);
1682 ga_concat(&ga, (char_u *)" ");
1683 vim_free(p);
1684 }
1685 s = e + 1;
1686 }
1687 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001688 clear_tv(tv);
1689 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001690 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001691 return OK;
1692 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001693 if (tostring_flags & TOSTRING_INTERPOLATE)
1694 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001695 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001696 default: to_string_error(tv->v_type);
1697 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001698 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001699 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001700 str = typval_tostring(tv, TRUE);
1701 clear_tv(tv);
1702 tv->v_type = VAR_STRING;
1703 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001704 return OK;
1705}
1706
1707/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001708 * When the value of "sv" is a null list of dict, allocate it.
1709 */
1710 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001711allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001712{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001713 typval_T *tv = sv->sv_tv;
1714
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001715 switch (tv->v_type)
1716 {
1717 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001718 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001719 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001720 break;
1721 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001722 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001723 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001724 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001725 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001726 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001727 (void)rettv_blob_alloc(tv);
1728 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001729 default:
1730 break;
1731 }
1732}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001733
Bram Moolenaare7525c52021-01-09 13:20:37 +01001734/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001735 * Return the character "str[index]" where "index" is the character index,
1736 * including composing characters.
1737 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001738 */
1739 char_u *
1740char_from_string(char_u *str, varnumber_T index)
1741{
1742 size_t nbyte = 0;
1743 varnumber_T nchar = index;
1744 size_t slen;
1745
1746 if (str == NULL)
1747 return NULL;
1748 slen = STRLEN(str);
1749
Bram Moolenaarff871402021-03-26 13:34:05 +01001750 // Do the same as for a list: a negative index counts from the end.
1751 // Optimization to check the first byte to be below 0x80 (and no composing
1752 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001753 if (index < 0)
1754 {
1755 int clen = 0;
1756
1757 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001758 {
1759 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1760 ++nbyte;
1761 else if (enc_utf8)
1762 nbyte += utfc_ptr2len(str + nbyte);
1763 else
1764 nbyte += mb_ptr2len(str + nbyte);
1765 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001766 nchar = clen + index;
1767 if (nchar < 0)
1768 // unlike list: index out of range results in empty string
1769 return NULL;
1770 }
1771
1772 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001773 {
1774 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1775 ++nbyte;
1776 else if (enc_utf8)
1777 nbyte += utfc_ptr2len(str + nbyte);
1778 else
1779 nbyte += mb_ptr2len(str + nbyte);
1780 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001781 if (nbyte >= slen)
1782 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001783 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001784}
1785
1786/*
1787 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001788 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001789 * If going over the end return "str_len".
1790 * If "idx" is negative count from the end, -1 is the last character.
1791 * When going over the start return -1.
1792 */
1793 static long
1794char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1795{
1796 varnumber_T nchar = idx;
1797 size_t nbyte = 0;
1798
1799 if (nchar >= 0)
1800 {
1801 while (nchar > 0 && nbyte < str_len)
1802 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001803 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001804 --nchar;
1805 }
1806 }
1807 else
1808 {
1809 nbyte = str_len;
1810 while (nchar < 0 && nbyte > 0)
1811 {
1812 --nbyte;
1813 nbyte -= mb_head_off(str, str + nbyte);
1814 ++nchar;
1815 }
1816 if (nchar < 0)
1817 return -1;
1818 }
1819 return (long)nbyte;
1820}
1821
1822/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001823 * Return the slice "str[first : last]" using character indexes. Composing
1824 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001825 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001826 * Return NULL when the result is empty.
1827 */
1828 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001829string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001830{
1831 long start_byte, end_byte;
1832 size_t slen;
1833
1834 if (str == NULL)
1835 return NULL;
1836 slen = STRLEN(str);
1837 start_byte = char_idx2byte(str, slen, first);
1838 if (start_byte < 0)
1839 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001840 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001841 end_byte = (long)slen;
1842 else
1843 {
1844 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001845 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001846 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001847 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001848 }
1849
1850 if (start_byte >= (long)slen || end_byte <= start_byte)
1851 return NULL;
1852 return vim_strnsave(str + start_byte, end_byte - start_byte);
1853}
1854
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001855/*
1856 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1857 * When "dfunc_idx" is negative don't give an error.
1858 * Returns NULL for an error.
1859 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001860 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001861get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001862{
1863 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001864 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1865 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001866 svar_T *sv;
1867
1868 if (sref->sref_seq != si->sn_script_seq)
1869 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001870 // The script was reloaded after the function was compiled, the
1871 // script_idx may not be valid.
1872 if (dfunc != NULL)
1873 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1874 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001875 return NULL;
1876 }
1877 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001878 if (sv->sv_name == NULL)
1879 {
1880 if (dfunc != NULL)
1881 emsg(_(e_script_variable_was_deleted));
1882 return NULL;
1883 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001884 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001885 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001886 if (dfunc != NULL)
1887 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001888 return NULL;
1889 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001890
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001891 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1892 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001893 {
1894 if (dfunc != NULL)
1895 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1896 return NULL;
1897 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001898 return sv;
1899}
1900
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001901/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001902 * Function passed to do_cmdline() for splitting a script joined by NL
1903 * characters.
1904 */
1905 static char_u *
1906get_split_sourceline(
1907 int c UNUSED,
1908 void *cookie,
1909 int indent UNUSED,
1910 getline_opt_T options UNUSED)
1911{
1912 source_cookie_T *sp = (source_cookie_T *)cookie;
1913 char_u *p;
1914 char_u *line;
1915
Bram Moolenaar20677332021-06-06 17:02:53 +02001916 p = vim_strchr(sp->nextline, '\n');
1917 if (p == NULL)
1918 {
1919 line = vim_strsave(sp->nextline);
1920 sp->nextline += STRLEN(sp->nextline);
1921 }
1922 else
1923 {
1924 line = vim_strnsave(sp->nextline, p - sp->nextline);
1925 sp->nextline = p + 1;
1926 }
1927 return line;
1928}
1929
1930/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 * Execute a function by "name".
1932 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001933 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 */
1935 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001936call_eval_func(
1937 char_u *name,
1938 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001939 ectx_T *ectx,
1940 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941{
Bram Moolenaared677f52020-08-12 16:38:10 +02001942 int called_emsg_before = called_emsg;
1943 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001945 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001946 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001948 dictitem_T *v;
1949
1950 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001951 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1952 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001953 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001954 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001955 return FAIL;
1956 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001957 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001959 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960}
1961
1962/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001963 * When a function reference is used, fill a partial with the information
1964 * needed, especially when it is used as a closure.
1965 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001966 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001967fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001968 partial_T *pt,
1969 ufunc_T *ufunc,
1970 loopvarinfo_T *lvi,
1971 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001972{
1973 pt->pt_func = ufunc;
1974 pt->pt_refcount = 1;
1975
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001976 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001977 {
1978 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1979 + ectx->ec_dfunc_idx;
1980
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001981 // The closure may need to find arguments and local variables of the
1982 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001983 pt->pt_outer.out_stack = &ectx->ec_stack;
1984 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001985 if (ectx->ec_outer_ref != NULL)
1986 {
1987 // The current context already has a context, link to that one.
1988 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1989 if (ectx->ec_outer_ref->or_partial != NULL)
1990 {
1991 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1992 ++pt->pt_outer.out_up_partial->pt_refcount;
1993 }
1994 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001995
Bram Moolenaarcc341812022-09-19 15:54:34 +01001996 if (lvi != NULL)
1997 {
1998 int depth;
1999
2000 // The closure may need to find variables defined inside a loop,
2001 // for every nested loop. A new reference is made every time,
2002 // ISN_ENDLOOP will check if they are actually used.
2003 for (depth = 0; depth < lvi->lvi_depth; ++depth)
2004 {
2005 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
2006 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
2007 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
2008 pt->pt_outer.out_loop[depth].var_count =
2009 lvi->lvi_loop[depth].var_count;
2010 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002011 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002012 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002013 else
2014 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002015
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002016 // If the function currently executing returns and the closure is still
2017 // being referenced, we need to make a copy of the context (arguments
2018 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002019 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02002020 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01002021 {
2022 vim_free(pt);
2023 return FAIL;
2024 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002025 // Extra variable keeps the count of closures created in the current
2026 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01002027 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
2028 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
2029
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002030 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
2031 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002032 ++pt->pt_refcount;
2033 ++ectx->ec_funcrefs.ga_len;
2034 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002035 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002036 return OK;
2037}
2038
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002039/*
2040 * Execute iptr->isn_arg.string as an Ex command.
2041 */
2042 static int
Ernie Raelee865f32023-09-29 19:53:55 +02002043exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002044{
2045 source_cookie_T cookie;
2046
2047 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002048 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002049 CLEAR_FIELD(cookie);
2050 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02002051 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002052 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
2053 || did_emsg)
2054 return FAIL;
2055 return OK;
2056}
2057
Bram Moolenaar89445512022-04-14 12:58:23 +01002058/*
2059 * If script "sid" is not loaded yet then load it now.
2060 * Caller must make sure "sid" is a valid script ID.
2061 * "loaded" is set to TRUE if the script had to be loaded.
2062 * Returns FAIL if loading fails, OK if already loaded or loaded now.
2063 */
2064 int
2065may_load_script(int sid, int *loaded)
2066{
2067 scriptitem_T *si = SCRIPT_ITEM(sid);
2068
2069 if (si->sn_state == SN_STATE_NOT_LOADED)
2070 {
2071 *loaded = TRUE;
2072 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
2073 {
2074 semsg(_(e_cant_open_file_str), si->sn_name);
2075 return FAIL;
2076 }
2077 }
2078 return OK;
2079}
2080
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002081// used for v_instr of typval of VAR_INSTR
2082struct instr_S {
2083 ectx_T *instr_ectx;
2084 isn_T *instr_instr;
2085};
2086
Bram Moolenaar4c137212021-04-19 16:48:48 +02002087// used for substitute_instr
2088typedef struct subs_expr_S {
2089 ectx_T *subs_ectx;
2090 isn_T *subs_instr;
2091 int subs_status;
2092} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002094// Set when calling do_debug().
2095static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002096static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002097
2098/*
2099 * When debugging lookup "name" and return the typeval.
2100 * When not found return NULL.
2101 */
2102 typval_T *
2103lookup_debug_var(char_u *name)
2104{
2105 int idx;
2106 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002107 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002108 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002109 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002110
2111 if (ectx == NULL)
2112 return NULL;
2113 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2114
2115 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002116 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002117 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002118 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2119
2120 // the variable name may be NULL when not available in this block
2121 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002122 return STACK_TV_VAR(idx);
2123 }
2124
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002125 // Go through argument names.
2126 ufunc = dfunc->df_ufunc;
2127 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2128 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2129 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2130 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2131 - varargs_off + idx);
2132 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2133 return STACK_TV(ectx->ec_frame_idx - 1);
2134
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002135 return NULL;
2136}
2137
Bram Moolenaar26a44842021-09-02 18:49:06 +02002138/*
2139 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2140 * breakpoint was set in that function or when there is any expression.
2141 */
2142 int
2143may_break_in_function(ufunc_T *ufunc)
2144{
2145 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2146}
2147
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002148 static void
2149handle_debug(isn_T *iptr, ectx_T *ectx)
2150{
2151 char_u *line;
2152 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2153 + ectx->ec_dfunc_idx)->df_ufunc;
2154 isn_T *ni;
2155 int end_lnum = iptr->isn_lnum;
2156 garray_T ga;
2157 int lnum;
2158
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002159 if (ex_nesting_level > debug_break_level)
2160 {
2161 linenr_T breakpoint;
2162
Bram Moolenaar26a44842021-09-02 18:49:06 +02002163 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002164 return;
2165
2166 // check for the next breakpoint if needed
2167 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002168 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002169 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2170 return;
2171 }
2172
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002173 SOURCING_LNUM = iptr->isn_lnum;
2174 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002175 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002176
2177 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2178 if (ni->isn_type == ISN_DEBUG
2179 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002180 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002181 || ni->isn_type == ISN_RETURN_VOID)
2182 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002183 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002184 break;
2185 }
2186
2187 if (end_lnum > iptr->isn_lnum)
2188 {
2189 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002190 for (lnum = iptr->isn_lnum; lnum < end_lnum
2191 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002192 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002193 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002194
Bram Moolenaar303215d2021-07-07 20:10:43 +02002195 if (p == NULL)
2196 continue; // left over from continuation line
2197 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002198 if (*p == '#')
2199 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002200 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002201 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2202 if (STRNCMP(p, "def ", 4) == 0)
2203 break;
2204 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002205 line = ga_concat_strings(&ga, " ");
2206 vim_free(ga.ga_data);
2207 }
2208 else
2209 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002210
Dominique Pelle6e969552021-06-17 13:53:41 +02002211 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002212 debug_context = NULL;
2213
2214 if (end_lnum > iptr->isn_lnum)
2215 vim_free(line);
2216}
2217
Bram Moolenaar4c137212021-04-19 16:48:48 +02002218/*
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002219 * Do a runtime check of the RHS value against the LHS List member type.
2220 * This is used by the STOREINDEX instruction to perform a type check
2221 * at runtime if compile time type check cannot be performed (VAR_ANY).
2222 * Returns FAIL if there is a type mismatch.
2223 */
2224 static int
2225storeindex_check_list_member_type(
2226 list_T *lhs_list,
2227 typval_T *rhs_tv,
2228 ectx_T *ectx)
2229{
2230 if (lhs_list->lv_type == NULL || lhs_list->lv_type->tt_member == NULL)
2231 return OK;
2232
2233 return check_typval_type(lhs_list->lv_type->tt_member, rhs_tv,
2234 ectx->ec_where);
2235}
2236
2237/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002238 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002239 * Returns OK, FAIL or NOTDONE (uncatchable error).
2240 */
2241 static int
2242execute_storeindex(isn_T *iptr, ectx_T *ectx)
2243{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002244 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002245 typval_T *tv;
2246 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002247 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002248 typval_T *tv_dest = STACK_TV_BOT(-1);
2249 int status = OK;
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002250 int check_rhs_type = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002251
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002252 if (tv_idx->v_type == VAR_NUMBER)
2253 lidx = (long)tv_idx->vval.v_number;
2254
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002255 // Stack contains:
2256 // -3 value to be stored
2257 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002258 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002259 tv = STACK_TV_BOT(-3);
2260 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002261
2262 // Make sure an object has been initialized
2263 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2264 {
2265 emsg(_(e_using_null_object));
2266 status = FAIL;
2267 }
2268 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002269 {
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002270 check_rhs_type = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002271 dest_type = tv_dest->v_type;
2272 if (dest_type == VAR_DICT)
2273 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002274 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2275 {
2276 // Need to get the member index now that the class is known.
2277 object_T *obj = tv_dest->vval.v_object;
Ernie Raelbe828252024-07-26 18:37:02 +02002278 if (obj == NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002279 {
Ernie Raelbe828252024-07-26 18:37:02 +02002280 emsg(_(e_using_null_object));
2281 status = FAIL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002282 }
2283 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002284 {
Ernie Raelbe828252024-07-26 18:37:02 +02002285 class_T *cl = obj->obj_class;
2286 char_u *member = tv_idx->vval.v_string;
2287
2288 int m_idx;
2289 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2290 if (m != NULL)
2291 {
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01002292 // Get the current function
2293 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2294 + ectx->ec_dfunc_idx)->df_ufunc;
Yegappan Lakshmanane7984462024-11-12 21:03:00 +01002295 where_T where = WHERE_INIT;
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01002296
2297 // Check whether the member variable is writeable
2298 if ((m->ocm_access != VIM_ACCESS_ALL) &&
2299 (ufunc->uf_class == NULL ||
2300 !class_instance_of(ufunc->uf_class, cl)))
Ernie Raelbe828252024-07-26 18:37:02 +02002301 {
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01002302 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
2303 ? e_cannot_access_protected_variable_str
2304 : e_variable_is_not_writable_str;
2305 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
Ernie Raelbe828252024-07-26 18:37:02 +02002306 status = FAIL;
2307 }
Yegappan Lakshmanane7984462024-11-12 21:03:00 +01002308 // Fail if the variable is a const or final or the type
2309 // is not compatible
2310 else if (oc_var_check_ro(cl, m) ||
2311 check_typval_type(m->ocm_type, tv, where)
2312 == FAIL)
2313 status = FAIL;
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01002314 else
2315 lidx = m_idx;
Ernie Raelbe828252024-07-26 18:37:02 +02002316 }
2317 else
2318 {
2319 member_not_found_msg(cl, VAR_OBJECT, member, 0);
2320 status = FAIL;
2321 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002322 }
2323 }
2324 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2325 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002326 {
2327 emsg(_(e_number_expected));
2328 status = FAIL;
2329 }
2330 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002331
2332 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002333 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002334 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002335 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002336 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002337
Bram Moolenaare08be092022-02-17 13:08:26 +00002338 if (list == NULL)
2339 {
2340 emsg(_(e_list_not_set));
2341 return FAIL;
2342 }
2343 if (lidx < 0 && list->lv_len + lidx >= 0)
2344 // negative index is relative to the end
2345 lidx = list->lv_len + lidx;
2346 if (lidx < 0 || lidx > list->lv_len)
2347 {
2348 semsg(_(e_list_index_out_of_range_nr), lidx);
2349 return FAIL;
2350 }
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002351
2352 // Do a runtime type check for VAR_ANY
2353 if (check_rhs_type &&
2354 storeindex_check_list_member_type(list, tv, ectx) == FAIL)
2355 return FAIL;
2356
Bram Moolenaare08be092022-02-17 13:08:26 +00002357 if (lidx < list->lv_len)
2358 {
2359 listitem_T *li = list_find(list, lidx);
2360
2361 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002362 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002363 return FAIL;
2364 // overwrite existing list item
2365 clear_tv(&li->li_tv);
2366 li->li_tv = *tv;
2367 }
2368 else
2369 {
2370 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2371 return FAIL;
2372 // append to list, only fails when out of memory
2373 if (list_append_tv(list, tv) == FAIL)
2374 return NOTDONE;
2375 clear_tv(tv);
2376 }
2377 }
2378 else if (dest_type == VAR_DICT)
2379 {
2380 char_u *key = tv_idx->vval.v_string;
2381 dict_T *dict = tv_dest->vval.v_dict;
2382 dictitem_T *di;
2383
2384 SOURCING_LNUM = iptr->isn_lnum;
2385 if (dict == NULL)
2386 {
2387 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002388 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002389 }
2390 if (key == NULL)
2391 key = (char_u *)"";
2392 di = dict_find(dict, key, -1);
2393 if (di != NULL)
2394 {
2395 if (error_if_locked(di->di_tv.v_lock,
2396 e_cannot_change_dict_item))
2397 return FAIL;
2398 // overwrite existing value
2399 clear_tv(&di->di_tv);
2400 di->di_tv = *tv;
2401 }
2402 else
2403 {
2404 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2405 return FAIL;
2406 // add to dict, only fails when out of memory
2407 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2408 return NOTDONE;
2409 clear_tv(tv);
2410 }
2411 }
2412 else if (dest_type == VAR_BLOB)
2413 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002414 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002415 varnumber_T nr;
2416 int error = FALSE;
2417 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002418
2419 if (blob == NULL)
2420 {
2421 emsg(_(e_blob_not_set));
2422 return FAIL;
2423 }
2424 len = blob_len(blob);
2425 if (lidx < 0 && len + lidx >= 0)
2426 // negative index is relative to the end
2427 lidx = len + lidx;
2428
2429 // Can add one byte at the end.
2430 if (lidx < 0 || lidx > len)
2431 {
2432 semsg(_(e_blob_index_out_of_range_nr), lidx);
2433 return FAIL;
2434 }
2435 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2436 return FAIL;
2437 nr = tv_get_number_chk(tv, &error);
2438 if (error)
2439 return FAIL;
2440 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002441 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002442 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2443 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002444 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002445
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002446 if (dest_type == VAR_OBJECT)
2447 {
2448 object_T *obj = tv_dest->vval.v_object;
2449
2450 otv = (typval_T *)(obj + 1);
2451 class_T *itf = iptr->isn_arg.storeindex.si_class;
2452 if (itf != NULL)
2453 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002454 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002455 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002456 }
2457 else
2458 {
2459 // VAR_CLASS
2460 class_T *class = tv_dest->vval.v_class;
2461 otv = class->class_members_tv;
2462 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002463
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002464 clear_tv(&otv[lidx]);
2465 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002466 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002467 else
2468 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002469 status = FAIL;
2470 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002471 }
2472 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002473
2474 clear_tv(tv_idx);
2475 clear_tv(tv_dest);
2476 ectx->ec_stack.ga_len -= 3;
2477 if (status == FAIL)
2478 {
2479 clear_tv(tv);
2480 return FAIL;
2481 }
2482 return OK;
2483}
2484
2485/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002486 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002487 */
2488 static int
2489execute_storerange(isn_T *iptr, ectx_T *ectx)
2490{
2491 typval_T *tv;
2492 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2493 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2494 typval_T *tv_dest = STACK_TV_BOT(-1);
2495 int status = OK;
2496
2497 // Stack contains:
2498 // -4 value to be stored
2499 // -3 first index or "none"
2500 // -2 second index or "none"
2501 // -1 destination list or blob
2502 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002503 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002504 if (tv_dest->v_type == VAR_LIST)
2505 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002506 long n1;
2507 long n2;
2508 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002509
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002510 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2511 if (tv_idx2->v_type == VAR_SPECIAL
2512 && tv_idx2->vval.v_number == VVAL_NONE)
2513 n2 = list_len(tv_dest->vval.v_list) - 1;
2514 else
2515 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2516
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002517 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002518 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002519 status = FAIL;
2520 else
2521 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002522 status = check_range_index_two(tv_dest->vval.v_list,
2523 &n1, li1, &n2, FALSE);
2524 if (status != FAIL)
2525 status = list_assign_range(
2526 tv_dest->vval.v_list,
2527 tv->vval.v_list,
2528 n1,
2529 n2,
2530 tv_idx2->v_type == VAR_SPECIAL,
2531 (char_u *)"=",
2532 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002533 }
2534 }
2535 else if (tv_dest->v_type == VAR_BLOB)
2536 {
2537 varnumber_T n1;
2538 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002539 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002540
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002541 n1 = tv_get_number_chk(tv_idx1, NULL);
2542 if (tv_idx2->v_type == VAR_SPECIAL
2543 && tv_idx2->vval.v_number == VVAL_NONE)
2544 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2545 else
2546 n2 = tv_get_number_chk(tv_idx2, NULL);
2547 bloblen = blob_len(tv_dest->vval.v_blob);
2548
2549 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2550 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002551 status = FAIL;
2552 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002553 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002554 }
2555 else
2556 {
2557 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002558 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002559 }
2560
2561 clear_tv(tv_idx1);
2562 clear_tv(tv_idx2);
2563 clear_tv(tv_dest);
2564 ectx->ec_stack.ga_len -= 4;
2565 clear_tv(tv);
2566
2567 return status;
2568}
2569
2570/*
2571 * Unlet item in list or dict variable.
2572 */
2573 static int
2574execute_unletindex(isn_T *iptr, ectx_T *ectx)
2575{
2576 typval_T *tv_idx = STACK_TV_BOT(-2);
2577 typval_T *tv_dest = STACK_TV_BOT(-1);
2578 int status = OK;
2579
2580 // Stack contains:
2581 // -2 index
2582 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002583 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002584 if (tv_dest->v_type == VAR_DICT)
2585 {
2586 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002587 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002588 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002589 semsg(_(e_expected_str_but_got_str),
2590 vartype_name(VAR_STRING),
2591 vartype_name(tv_idx->v_type));
2592 status = FAIL;
2593 }
2594 else
2595 {
2596 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002597 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002598 dictitem_T *di = NULL;
2599
2600 if (d != NULL && value_check_lock(
2601 d->dv_lock, NULL, FALSE))
2602 status = FAIL;
2603 else
2604 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002605 if (tv_idx->v_type == VAR_STRING)
2606 {
2607 key = tv_idx->vval.v_string;
2608 if (key == NULL)
2609 key = (char_u *)"";
2610 }
2611 else
2612 {
2613 key = tv_get_string(tv_idx);
2614 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002615 if (d != NULL)
2616 di = dict_find(d, key, (int)STRLEN(key));
2617 if (di == NULL)
2618 {
2619 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002620 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002621 status = FAIL;
2622 }
2623 else if (var_check_fixed(di->di_flags,
2624 NULL, FALSE)
2625 || var_check_ro(di->di_flags,
2626 NULL, FALSE))
2627 status = FAIL;
2628 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002629 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002630 }
2631 }
2632 }
2633 else if (tv_dest->v_type == VAR_LIST)
2634 {
2635 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002636 if (check_for_number(tv_idx) == FAIL)
2637 {
2638 status = FAIL;
2639 }
2640 else
2641 {
2642 list_T *l = tv_dest->vval.v_list;
2643 long n = (long)tv_idx->vval.v_number;
2644
2645 if (l != NULL && value_check_lock(
2646 l->lv_lock, NULL, FALSE))
2647 status = FAIL;
2648 else
2649 {
2650 listitem_T *li = list_find(l, n);
2651
2652 if (li == NULL)
2653 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002654 semsg(_(e_list_index_out_of_range_nr), n);
2655 status = FAIL;
2656 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002657 else
2658 listitem_remove(l, li);
2659 }
2660 }
2661 }
2662 else
2663 {
2664 status = FAIL;
2665 semsg(_(e_cannot_index_str),
2666 vartype_name(tv_dest->v_type));
2667 }
2668
2669 clear_tv(tv_idx);
2670 clear_tv(tv_dest);
2671 ectx->ec_stack.ga_len -= 2;
2672
2673 return status;
2674}
2675
2676/*
2677 * Unlet a range of items in a list variable.
2678 */
2679 static int
2680execute_unletrange(isn_T *iptr, ectx_T *ectx)
2681{
2682 // Stack contains:
2683 // -3 index1
2684 // -2 index2
2685 // -1 dict or list
2686 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2687 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2688 typval_T *tv_dest = STACK_TV_BOT(-1);
2689 int status = OK;
2690
2691 if (tv_dest->v_type == VAR_LIST)
2692 {
2693 // indexes must be a number
2694 SOURCING_LNUM = iptr->isn_lnum;
2695 if (check_for_number(tv_idx1) == FAIL
2696 || (tv_idx2->v_type != VAR_SPECIAL
2697 && check_for_number(tv_idx2) == FAIL))
2698 {
2699 status = FAIL;
2700 }
2701 else
2702 {
2703 list_T *l = tv_dest->vval.v_list;
2704 long n1 = (long)tv_idx1->vval.v_number;
2705 long n2 = tv_idx2->v_type == VAR_SPECIAL
2706 ? 0 : (long)tv_idx2->vval.v_number;
2707 listitem_T *li;
2708
2709 li = list_find_index(l, &n1);
2710 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002711 {
2712 semsg(_(e_list_index_out_of_range_nr),
2713 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002714 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002715 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002716 else
2717 {
2718 if (n1 < 0)
2719 n1 = list_idx_of_item(l, li);
2720 if (n2 < 0)
2721 {
2722 listitem_T *li2 = list_find(l, n2);
2723
2724 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002725 {
2726 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002727 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002728 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002729 else
2730 n2 = list_idx_of_item(l, li2);
2731 }
2732 if (status != FAIL
2733 && tv_idx2->v_type != VAR_SPECIAL
2734 && n2 < n1)
2735 {
2736 semsg(_(e_list_index_out_of_range_nr), n2);
2737 status = FAIL;
2738 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002739 if (status != FAIL)
2740 list_unlet_range(l, li, n1,
2741 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002742 }
2743 }
2744 }
2745 else
2746 {
2747 status = FAIL;
2748 SOURCING_LNUM = iptr->isn_lnum;
2749 semsg(_(e_cannot_index_str),
2750 vartype_name(tv_dest->v_type));
2751 }
2752
2753 clear_tv(tv_idx1);
2754 clear_tv(tv_idx2);
2755 clear_tv(tv_dest);
2756 ectx->ec_stack.ga_len -= 3;
2757
2758 return status;
2759}
2760
2761/*
2762 * Top of a for loop.
2763 */
2764 static int
2765execute_for(isn_T *iptr, ectx_T *ectx)
2766{
2767 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002768 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002769 typval_T *ltv = STACK_TV_BOT(-1);
2770 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002771 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002772
2773 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2774 return FAIL;
2775 if (ltv->v_type == VAR_LIST)
2776 {
2777 list_T *list = ltv->vval.v_list;
2778
2779 // push the next item from the list
2780 ++idxtv->vval.v_number;
2781 if (list == NULL
2782 || idxtv->vval.v_number >= list->lv_len)
2783 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002784 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002785 }
2786 else if (list->lv_first == &range_list_item)
2787 {
2788 // non-materialized range() list
2789 tv = STACK_TV_BOT(0);
2790 tv->v_type = VAR_NUMBER;
2791 tv->v_lock = 0;
2792 tv->vval.v_number = list_find_nr(
2793 list, idxtv->vval.v_number, NULL);
2794 ++ectx->ec_stack.ga_len;
2795 }
2796 else
2797 {
2798 listitem_T *li = list_find(list,
2799 idxtv->vval.v_number);
2800
2801 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2802 ++ectx->ec_stack.ga_len;
2803 }
2804 }
2805 else if (ltv->v_type == VAR_STRING)
2806 {
2807 char_u *str = ltv->vval.v_string;
2808
2809 // The index is for the last byte of the previous
2810 // character.
2811 ++idxtv->vval.v_number;
2812 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2813 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002814 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002815 }
2816 else
2817 {
2818 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2819
2820 // Push the next character from the string.
2821 tv = STACK_TV_BOT(0);
2822 tv->v_type = VAR_STRING;
2823 tv->vval.v_string = vim_strnsave(
2824 str + idxtv->vval.v_number, clen);
2825 ++ectx->ec_stack.ga_len;
2826 idxtv->vval.v_number += clen - 1;
2827 }
2828 }
2829 else if (ltv->v_type == VAR_BLOB)
2830 {
2831 blob_T *blob = ltv->vval.v_blob;
2832
2833 // When we get here the first time make a copy of the
2834 // blob, so that the iteration still works when it is
2835 // changed.
2836 if (idxtv->vval.v_number == -1 && blob != NULL)
2837 {
2838 blob_copy(blob, ltv);
2839 blob_unref(blob);
2840 blob = ltv->vval.v_blob;
2841 }
2842
2843 // The index is for the previous byte.
2844 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002845 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002846 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002847 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002848 }
2849 else
2850 {
2851 // Push the next byte from the blob.
2852 tv = STACK_TV_BOT(0);
2853 tv->v_type = VAR_NUMBER;
2854 tv->vval.v_number = blob_get(blob,
2855 idxtv->vval.v_number);
2856 ++ectx->ec_stack.ga_len;
2857 }
2858 }
2859 else
2860 {
2861 semsg(_(e_for_loop_on_str_not_supported),
2862 vartype_name(ltv->v_type));
2863 return FAIL;
2864 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002865
2866 if (jump)
2867 {
2868 // past the end of the list/string/blob, jump to "endfor"
2869 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2870 may_restore_cmdmod(&ectx->ec_funclocal);
2871 }
2872 else
2873 {
2874 // Store the current number of funcrefs, this may be used in
2875 // ISN_LOOPEND. The variable index is always one more than the loop
2876 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002877 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002878 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2879 }
2880
2881 return OK;
2882}
2883
2884/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002885 * Code for handling variables declared inside a loop and used in a closure.
2886 * This is very similar to what is done with funcstack_T. The difference is
2887 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2888 * scope of the block inside a loop and each loop may have its own.
2889 */
2890
2891// Double linked list of loopvars_T in use.
2892static loopvars_T *first_loopvars = NULL;
2893
2894 static void
2895add_loopvars_to_list(loopvars_T *loopvars)
2896{
2897 // Link in list of loopvarss.
2898 if (first_loopvars != NULL)
2899 first_loopvars->lvs_prev = loopvars;
2900 loopvars->lvs_next = first_loopvars;
2901 loopvars->lvs_prev = NULL;
2902 first_loopvars = loopvars;
2903}
2904
2905 static void
2906remove_loopvars_from_list(loopvars_T *loopvars)
2907{
2908 if (loopvars->lvs_prev == NULL)
2909 first_loopvars = loopvars->lvs_next;
2910 else
2911 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2912 if (loopvars->lvs_next != NULL)
2913 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2914}
2915
2916/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002917 * End of a for or while loop: Handle any variables used by a closure.
2918 */
2919 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002920execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002921{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002922 endloop_T *endloop = &iptr->isn_arg.endloop;
2923 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2924 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002925 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002926 garray_T *gap = &ectx->ec_funcrefs;
2927 int closure_in_use = FALSE;
2928 loopvars_T *loopvars;
2929 typval_T *stack;
2930 int idx;
2931
Bram Moolenaarcc341812022-09-19 15:54:34 +01002932 // Check if any created closure is still being referenced and loopvars have
2933 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002934 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2935 {
2936 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2937
Bram Moolenaarcc341812022-09-19 15:54:34 +01002938 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002939 {
2940 int refcount = pt->pt_refcount;
2941 int i;
2942
2943 // A Reference in a variable inside the loop doesn't count, it gets
2944 // unreferenced at the end of the loop.
2945 for (i = 0; i < endloop->end_var_count; ++i)
2946 {
2947 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2948
2949 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2950 --refcount;
2951 }
2952 if (refcount > 1)
2953 {
2954 closure_in_use = TRUE;
2955 break;
2956 }
2957 }
2958 }
2959
2960 // If no function reference were created since the start of the loop block
2961 // or it is no longer referenced there is nothing to do.
2962 if (!closure_in_use)
2963 return OK;
2964
2965 // A closure is using variables declared inside the loop.
2966 // Move them to the called function.
2967 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2968 if (loopvars == NULL)
2969 return FAIL;
2970
2971 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2972 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2973 loopvars->lvs_ga.ga_data = stack;
2974 if (stack == NULL)
2975 {
2976 vim_free(loopvars);
2977 return FAIL;
2978 }
2979 add_loopvars_to_list(loopvars);
2980
2981 // Move the variable values.
2982 for (idx = 0; idx < endloop->end_var_count; ++idx)
2983 {
2984 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2985
2986 *(stack + idx) = *tv;
2987 tv->v_type = VAR_UNKNOWN;
2988 }
2989
2990 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2991 {
2992 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2993
Bram Moolenaarcc341812022-09-19 15:54:34 +01002994 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002995 {
2996 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002997 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002998
Bram Moolenaarcc341812022-09-19 15:54:34 +01002999 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
3000 pt->pt_outer.out_loop[depth].var_idx -=
3001 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003002 }
3003 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01003004
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003005 return OK;
3006}
3007
3008/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003009 * Called when a partial is freed or its reference count goes down to one. The
3010 * loopvars may be the only reference to the partials in the local variables.
3011 * Go over all of them, the funcref and can be freed if all partials
3012 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01003013 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003014 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01003015 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003016loopvars_check_refcount(loopvars_T *loopvars)
3017{
3018 int i;
3019 garray_T *gap = &loopvars->lvs_ga;
3020 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003021 typval_T *stack = gap->ga_data;
3022
Bram Moolenaarcc341812022-09-19 15:54:34 +01003023 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
3024 return FALSE;
3025 for (i = 0; i < gap->ga_len; ++i)
3026 {
3027 typval_T *tv = ((typval_T *)gap->ga_data) + i;
3028
3029 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
3030 && tv->vval.v_partial->pt_refcount == 1)
3031 {
3032 int depth;
3033
3034 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
3035 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
3036 ++done;
3037 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003038 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003039 if (done != loopvars->lvs_min_refcount)
3040 return FALSE;
3041
3042 // All partials referencing the loopvars have a reference count of
3043 // one, thus the loopvars is no longer of use.
3044 stack = gap->ga_data;
3045 for (i = 0; i < gap->ga_len; ++i)
3046 clear_tv(stack + i);
3047 vim_free(stack);
3048 remove_loopvars_from_list(loopvars);
3049 vim_free(loopvars);
3050 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003051}
3052
3053/*
3054 * For garbage collecting: set references in all variables referenced by
3055 * all loopvars.
3056 */
3057 int
3058set_ref_in_loopvars(int copyID)
3059{
3060 loopvars_T *loopvars;
3061
3062 for (loopvars = first_loopvars; loopvars != NULL;
3063 loopvars = loopvars->lvs_next)
3064 {
3065 typval_T *stack = loopvars->lvs_ga.ga_data;
3066 int i;
3067
3068 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
3069 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
3070 return TRUE; // abort
3071 }
3072 return FALSE;
3073}
3074
3075/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00003076 * Load instruction for w:/b:/g:/t: variable.
3077 * "isn_type" is used instead of "iptr->isn_type".
3078 */
3079 static int
3080load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
3081{
3082 dictitem_T *di = NULL;
3083 hashtab_T *ht = NULL;
3084 char namespace;
3085
3086 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3087 return NOTDONE;
3088 switch (isn_type)
3089 {
3090 case ISN_LOADG:
3091 ht = get_globvar_ht();
3092 namespace = 'g';
3093 break;
3094 case ISN_LOADB:
3095 ht = &curbuf->b_vars->dv_hashtab;
3096 namespace = 'b';
3097 break;
3098 case ISN_LOADW:
3099 ht = &curwin->w_vars->dv_hashtab;
3100 namespace = 'w';
3101 break;
3102 case ISN_LOADT:
3103 ht = &curtab->tp_vars->dv_hashtab;
3104 namespace = 't';
3105 break;
3106 default: // Cannot reach here
3107 return NOTDONE;
3108 }
3109 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3110
Bram Moolenaar06b77222022-01-25 15:51:56 +00003111 if (di == NULL)
3112 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003113 if (isn_type == ISN_LOADG)
3114 {
3115 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3116
3117 // g:Something could be a function
3118 if (ufunc != NULL)
3119 {
3120 typval_T *tv = STACK_TV_BOT(0);
3121
3122 ++ectx->ec_stack.ga_len;
3123 tv->v_type = VAR_FUNC;
3124 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3125 if (tv->vval.v_string == NULL)
3126 return FAIL;
3127 STRCPY(tv->vval.v_string, "g:");
3128 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3129 return OK;
3130 }
3131 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003132 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003133 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003134 // no check if the item exists in the script but
3135 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003136 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003137 else
3138 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003139 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003140 return FAIL;
3141 }
3142 else
3143 {
3144 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3145 ++ectx->ec_stack.ga_len;
3146 }
3147 return OK;
3148}
3149
Bram Moolenaard0200c82023-01-28 15:19:40 +00003150
3151 static void
3152object_required_error(typval_T *tv)
3153{
3154 garray_T type_list;
3155 ga_init2(&type_list, sizeof(type_T *), 10);
3156 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3157 char *tofree = NULL;
3158 char *typename = type_name(type, &tofree);
3159 semsg(_(e_object_required_found_str), typename);
3160 vim_free(tofree);
3161 clear_type_list(&type_list);
3162}
3163
Bram Moolenaar06b77222022-01-25 15:51:56 +00003164/*
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003165 * Accessing the variable or method of an object or a class stored in a
3166 * variable of type "any".
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003167 * Returns OK if the member variable is present.
3168 * Returns FAIL if the variable is not found.
3169 */
3170 static int
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003171var_any_get_oc_member(class_T *current_class, isn_T *iptr, typval_T *tv)
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003172{
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003173 int is_object = tv->v_type == VAR_OBJECT;
3174 class_T *tv_cl;
3175 object_T *obj = NULL;
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003176 typval_T mtv;
3177
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003178 if (is_object)
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003179 {
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003180 obj = tv->vval.v_object;
3181 if (obj == NULL)
3182 {
3183 SOURCING_LNUM = iptr->isn_lnum;
3184 emsg(_(e_using_null_object));
3185 return FAIL;
3186 }
3187 tv_cl = obj->obj_class;
3188 }
3189 else
3190 {
3191 tv_cl = tv->vval.v_class;
3192 if (tv_cl == NULL)
3193 {
3194 SOURCING_LNUM = iptr->isn_lnum;
3195 emsg(_(e_using_null_class));
3196 return FAIL;
3197 }
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003198 }
3199
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003200 // get_member_tv() needs the class/object information in the typval
3201 // argument. So set the object information.
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003202 copy_tv(tv, &mtv);
3203
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003204 // 'name' can either be an instance or class variable or method
Yegappan Lakshmanan084529c2024-12-24 09:50:01 +01003205 int namelen = (int)STRLEN(iptr->isn_arg.string);
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003206 int save_did_emsg = did_emsg;
3207
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003208 if (get_member_tv(tv_cl, is_object, iptr->isn_arg.string, namelen,
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003209 current_class, &mtv) == OK)
3210 {
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003211 // instance or class variable
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003212 copy_tv(&mtv, tv);
3213 clear_tv(&mtv);
3214 return OK;
3215 }
3216
3217 if (did_emsg != save_did_emsg)
3218 return FAIL;
3219
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003220 // could be a class or instance method
3221 ufunc_T *oc_method;
3222 int oc_method_idx;
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003223
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003224 oc_method = method_lookup(tv_cl, tv->v_type, iptr->isn_arg.string,
3225 namelen, &oc_method_idx);
3226 if (oc_method == NULL)
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003227 {
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003228 char *msg;
3229
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003230 SOURCING_LNUM = iptr->isn_lnum;
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003231 if (is_object)
3232 msg = e_variable_not_found_on_object_str_str;
3233 else
3234 msg = e_class_variable_str_not_found_in_class_str;
3235 semsg(_(msg), iptr->isn_arg.string, tv_cl->class_name);
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003236 return FAIL;
3237 }
3238
3239 // Protected methods are not accessible outside the class
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003240 if (*oc_method->uf_name == '_'
3241 && !class_instance_of(current_class, tv_cl))
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003242 {
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003243 semsg(_(e_cannot_access_protected_method_str), oc_method->uf_name);
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003244 return FAIL;
3245 }
3246
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01003247 // Create a partial for the instance or class method
3248 if (obj_method_to_partial_tv(is_object ? obj : NULL, oc_method, tv)
3249 == FAIL)
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003250 return FAIL;
3251
3252 return OK;
3253}
3254
3255/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003256 * Execute instructions in execution context "ectx".
3257 * Return OK or FAIL;
3258 */
3259 static int
3260exec_instructions(ectx_T *ectx)
3261{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003262 int ret = FAIL;
3263 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003264 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003265
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003266 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003267 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003268
Bram Moolenaarff652882021-05-16 15:24:49 +02003269 // Only catch exceptions in this instruction list.
3270 ectx->ec_trylevel_at_start = trylevel;
3271
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 for (;;)
3273 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003274 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003276 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003277
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003278 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003279 {
3280 line_breakcheck();
3281 breakcheck_count = 0;
3282 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003283 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003284 {
3285 // Turn CTRL-C into an exception.
3286 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003287 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003288 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003289 did_throw = TRUE;
3290 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003292 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003293 {
3294 // Turn an error message into an exception.
3295 did_emsg = FALSE;
3296 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003297 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003298 did_throw = TRUE;
3299 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003300
3301 // This exception was not caught (yet).
3302 garray_T *trystack = &ectx->ec_trystack;
3303 if (trystack->ga_len > 0)
3304 {
3305 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3306 + trystack->ga_len - 1;
3307 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Yee Cheng Chin2051af12025-01-09 22:14:34 +01003308 {
3309 if (trycmd->tcd_caught)
3310 {
3311 // Inside a "catch" we need to first discard the caught
3312 // exception.
3313 finish_exception(caught_stack);
3314 trycmd->tcd_caught = FALSE;
3315 }
3316 }
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003317 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003318 }
3319
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003320 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003322 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003323 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003324 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325
3326 // An exception jumps to the first catch, finally, or returns from
3327 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003328 while (index > 0)
3329 {
3330 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003331 // 1. after :try and before :catch - jump to first :catch
3332 // 2. in :catch block - jump to :finally
3333 // 3. in :catch block and no finally - jump to :endtry
3334 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3335 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003336 break;
3337 // In the catch and finally block of this try we have to go up
3338 // one level.
3339 --index;
3340 trycmd = NULL;
3341 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003342 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003344 if (trycmd->tcd_in_catch)
3345 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003346 if (trycmd->tcd_finally_idx > 0)
3347 {
3348 // exception inside ":catch", jump to ":finally" once
3349 ectx->ec_iidx = trycmd->tcd_finally_idx;
3350 trycmd->tcd_finally_idx = 0;
3351 }
3352 else
3353 {
3354 // exception inside ":catch" or ":finally", jump to
3355 // ":endtry"
3356 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3357 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003358 }
3359 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003360 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003361 // jump to first ":catch"
3362 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003363 trycmd->tcd_in_catch = TRUE;
3364 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003365 did_throw = FALSE; // don't come back here until :endtry
3366 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 }
3368 else
3369 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003370 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003371 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003372 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003373 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003374 tv = STACK_TV_BOT(0);
3375 tv->v_type = VAR_NUMBER;
3376 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003377 ++ectx->ec_stack.ga_len;
3378 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003380 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003381 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003382 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003383 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 goto done;
3385 }
3386
Bram Moolenaar4c137212021-04-19 16:48:48 +02003387 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003388 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 }
3390 continue;
3391 }
3392
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003393 /*
3394 * Big switch on the instruction. Most compilers will be turning this
3395 * into an efficient lookup table, since the "case" values are an enum
3396 * with sequential numbers. It may look ugly, but it should be the
3397 * most efficient way.
3398 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003399 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 switch (iptr->isn_type)
3401 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003402 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003403 case ISN_CONSTRUCT:
3404 // "this" is always the local variable at index zero
3405 tv = STACK_TV_VAR(0);
3406 tv->v_type = VAR_OBJECT;
3407 tv->vval.v_object = alloc_clear(
3408 iptr->isn_arg.construct.construct_size);
3409 tv->vval.v_object->obj_class =
3410 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003411 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003412 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003413 object_created(tv->vval.v_object);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01003414
3415 // When creating an enum value object, initialize the name and
3416 // ordinal object variables.
3417 class_T *en = tv->vval.v_object->obj_class;
3418 if (IS_ENUM(en))
3419 enum_set_internal_obj_vars(en, tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003420 break;
3421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 // execute Ex command line
3423 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003424 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003425 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 break;
3427
Bram Moolenaar20677332021-06-06 17:02:53 +02003428 // execute Ex command line split at NL characters.
3429 case ISN_EXEC_SPLIT:
3430 {
3431 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003432 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003433
3434 SOURCING_LNUM = iptr->isn_lnum;
3435 CLEAR_FIELD(cookie);
3436 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3437 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003438 line = get_split_sourceline(0, &cookie, 0, 0);
3439 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003440 get_split_sourceline, &cookie,
3441 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3442 == FAIL
3443 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003444 {
3445 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003446 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003447 }
3448 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003449 }
3450 break;
3451
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003452 // execute Ex command line that is only a range
3453 case ISN_EXECRANGE:
3454 {
3455 exarg_T ea;
3456 char *error = NULL;
3457
3458 CLEAR_FIELD(ea);
3459 ea.cmdidx = CMD_SIZE;
3460 ea.addr_type = ADDR_LINES;
3461 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003462 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003463 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003464 if (ea.cmd == NULL)
3465 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003466 // error is always NULL when using ADDR_LINES
3467 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003468 if (error != NULL)
3469 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003470 emsg(error);
3471 goto on_error;
3472 }
3473 }
3474 break;
3475
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003476 // Evaluate an expression with legacy syntax, push it onto the
3477 // stack.
3478 case ISN_LEGACY_EVAL:
3479 {
3480 char_u *arg = iptr->isn_arg.string;
3481 int res;
3482 int save_flags = cmdmod.cmod_flags;
3483
Bram Moolenaar35578162021-08-02 19:10:38 +02003484 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003485 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003486 tv = STACK_TV_BOT(0);
3487 init_tv(tv);
3488 cmdmod.cmod_flags |= CMOD_LEGACY;
3489 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3490 cmdmod.cmod_flags = save_flags;
3491 if (res == FAIL)
3492 goto on_error;
3493 ++ectx->ec_stack.ga_len;
3494 }
3495 break;
3496
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003497 // push typeval VAR_INSTR with instructions to be executed
3498 case ISN_INSTR:
3499 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003500 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003501 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003502 tv = STACK_TV_BOT(0);
3503 tv->vval.v_instr = ALLOC_ONE(instr_T);
3504 if (tv->vval.v_instr == NULL)
3505 goto on_error;
3506 ++ectx->ec_stack.ga_len;
3507
3508 tv->v_type = VAR_INSTR;
3509 tv->vval.v_instr->instr_ectx = ectx;
3510 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3511 }
3512 break;
3513
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003514 case ISN_SOURCE:
3515 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003516 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003517
Bram Moolenaar89445512022-04-14 12:58:23 +01003518 SOURCING_LNUM = iptr->isn_lnum;
3519 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003520 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003521 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003522 }
3523 break;
3524
Bram Moolenaar4c137212021-04-19 16:48:48 +02003525 // execute :substitute with an expression
3526 case ISN_SUBSTITUTE:
3527 {
3528 subs_T *subs = &iptr->isn_arg.subs;
3529 source_cookie_T cookie;
3530 struct subs_expr_S *save_instr = substitute_instr;
3531 struct subs_expr_S subs_instr;
3532 int res;
3533
3534 subs_instr.subs_ectx = ectx;
3535 subs_instr.subs_instr = subs->subs_instr;
3536 subs_instr.subs_status = OK;
3537 substitute_instr = &subs_instr;
3538
3539 SOURCING_LNUM = iptr->isn_lnum;
3540 // This is very much like ISN_EXEC
3541 CLEAR_FIELD(cookie);
3542 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3543 res = do_cmdline(subs->subs_cmd,
3544 getsourceline, &cookie,
3545 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3546 substitute_instr = save_instr;
3547
3548 if (res == FAIL || did_emsg
3549 || subs_instr.subs_status == FAIL)
3550 goto on_error;
3551 }
3552 break;
3553
3554 case ISN_FINISH:
3555 goto done;
3556
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003557 case ISN_REDIRSTART:
3558 // create a dummy entry for var_redir_str()
3559 if (alloc_redir_lval() == FAIL)
3560 goto on_error;
3561
3562 // The output is stored in growarray "redir_ga" until
3563 // redirection ends.
3564 init_redir_ga();
3565 redir_vname = 1;
3566 break;
3567
3568 case ISN_REDIREND:
3569 {
3570 char_u *res = get_clear_redir_ga();
3571
3572 // End redirection, put redirected text on the stack.
3573 clear_redir_lval();
3574 redir_vname = 0;
3575
Bram Moolenaar35578162021-08-02 19:10:38 +02003576 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003577 {
3578 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003579 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003580 }
3581 tv = STACK_TV_BOT(0);
3582 tv->v_type = VAR_STRING;
3583 tv->vval.v_string = res;
3584 ++ectx->ec_stack.ga_len;
3585 }
3586 break;
3587
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003588 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003589#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003590 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003591 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3592 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003593 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003594#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003595 break;
3596
3597 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003598#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003599 {
3600 exarg_T ea;
3601 int res;
3602
3603 CLEAR_FIELD(ea);
3604 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3605 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3606 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3607 --ectx->ec_stack.ga_len;
3608 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003609 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003610 res = cexpr_core(&ea, tv);
3611 clear_tv(tv);
3612 if (res == FAIL)
3613 goto on_error;
3614 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003615#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003616 break;
3617
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003618 // execute Ex command from pieces on the stack
3619 case ISN_EXECCONCAT:
3620 {
3621 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003622 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003623 int pass;
3624 int i;
3625 char_u *cmd = NULL;
3626 char_u *str;
3627
3628 for (pass = 1; pass <= 2; ++pass)
3629 {
3630 for (i = 0; i < count; ++i)
3631 {
3632 tv = STACK_TV_BOT(i - count);
3633 str = tv->vval.v_string;
3634 if (str != NULL && *str != NUL)
3635 {
3636 if (pass == 2)
3637 STRCPY(cmd + len, str);
3638 len += STRLEN(str);
3639 }
3640 if (pass == 2)
3641 clear_tv(tv);
3642 }
3643 if (pass == 1)
3644 {
3645 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003646 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003647 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003648 len = 0;
3649 }
3650 }
3651
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003652 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003653 do_cmdline_cmd(cmd);
3654 vim_free(cmd);
3655 }
3656 break;
3657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 // execute :echo {string} ...
3659 case ISN_ECHO:
3660 {
3661 int count = iptr->isn_arg.echo.echo_count;
3662 int atstart = TRUE;
3663 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003664 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665
3666 for (idx = 0; idx < count; ++idx)
3667 {
3668 tv = STACK_TV_BOT(idx - count);
3669 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3670 &atstart, &needclr);
3671 clear_tv(tv);
3672 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003673 if (needclr)
3674 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003675 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 }
3677 break;
3678
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003679 // :execute {string} ...
3680 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003681 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003682 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003683 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003684 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003685 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003686 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003687 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003688 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003689 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003690 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003691 garray_T ga;
3692 char_u buf[NUMBUFLEN];
3693 char_u *p;
3694 int len;
3695 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003696 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003697
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003698 if (iptr->isn_type == ISN_ECHOWINDOW)
3699 count = iptr->isn_arg.echowin.ewin_count;
3700 else
3701 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003702 ga_init2(&ga, 1, 80);
3703 for (idx = 0; idx < count; ++idx)
3704 {
3705 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003706 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003707 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003708 if (tv->v_type == VAR_CHANNEL
3709 || tv->v_type == VAR_JOB)
3710 {
3711 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003712 semsg(_(e_using_invalid_value_as_string_str),
3713 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003714 break;
3715 }
3716 else
3717 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003718 }
3719 else
Ernie Raelbe828252024-07-26 18:37:02 +02003720 {
3721 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003722 p = tv_stringify(tv, buf);
Ernie Raelbe828252024-07-26 18:37:02 +02003723 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003724
3725 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003726 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003727 failed = TRUE;
3728 else
3729 {
3730 if (ga.ga_len > 0)
3731 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3732 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3733 ga.ga_len += len;
3734 }
3735 clear_tv(tv);
3736 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003737 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003738 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003739 {
3740 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003741 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003742 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003743
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003744 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003745 {
3746 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003747 {
3748 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003749 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003750 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003751 {
3752 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003753 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003754 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003755 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003756 else
3757 {
3758 msg_sb_eol();
3759 if (iptr->isn_type == ISN_ECHOMSG)
3760 {
3761 msg_attr(ga.ga_data, echo_attr);
3762 out_flush();
3763 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003764#ifdef HAS_MESSAGE_WINDOW
3765 else if (iptr->isn_type == ISN_ECHOWINDOW)
3766 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003767 start_echowindow(
3768 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003769 msg_attr(ga.ga_data, echo_attr);
3770 end_echowindow();
3771 }
3772#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003773 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3774 {
3775 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3776 TRUE);
3777 ui_write((char_u *)"\r\n", 2, TRUE);
3778 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003779 else
3780 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003781 SOURCING_LNUM = iptr->isn_lnum;
3782 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003783 }
3784 }
3785 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003786 ga_clear(&ga);
3787 }
3788 break;
3789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 // load local variable or argument
3791 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003792 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003793 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003794 tv = STACK_TV_VAR(iptr->isn_arg.number);
3795 if (tv->v_type == VAR_UNKNOWN)
3796 {
3797 // missing argument or default value v:none
3798 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3799 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3800 }
3801 else
3802 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003803 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 break;
3805
3806 // load v: variable
3807 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003808 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003809 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003811 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 break;
3813
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003814 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 case ISN_LOADSCRIPT:
3816 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003817 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 svar_T *sv;
3819
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003820 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003821 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003822 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003823 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003824 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003825 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003827 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 }
3829 break;
3830
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003831 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003833 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003835 int sid = iptr->isn_arg.loadstore.ls_sid;
3836 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003837 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 if (di == NULL)
3841 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003842 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003843 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003844 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 }
3846 else
3847 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003848 if (iptr->isn_type == ISN_LOADEXPORT)
3849 {
3850 int idx = get_script_item_idx(sid, name, 0,
3851 NULL, NULL);
3852 svar_T *sv;
3853
3854 if (idx >= 0)
3855 {
3856 sv = ((svar_T *)SCRIPT_ITEM(sid)
3857 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003858 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003859 {
3860 SOURCING_LNUM = iptr->isn_lnum;
3861 semsg(_(e_item_not_exported_in_script_str),
3862 name);
3863 goto on_error;
3864 }
3865 }
3866 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003867 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003868 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003870 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 }
3872 }
3873 break;
3874
Bram Moolenaard3aac292020-04-19 14:32:17 +02003875 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003877 case ISN_LOADB:
3878 case ISN_LOADW:
3879 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003881 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003882
Bram Moolenaar06b77222022-01-25 15:51:56 +00003883 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003884 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003885 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003886 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 break;
3890
Bram Moolenaar03290b82020-12-19 16:30:44 +01003891 // load autoload variable
3892 case ISN_LOADAUTO:
3893 {
3894 char_u *name = iptr->isn_arg.string;
3895
Bram Moolenaar35578162021-08-02 19:10:38 +02003896 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003897 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003898 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003899 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003900 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003901 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003902 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003903 }
3904 break;
3905
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003906 // load g:/b:/w:/t: namespace
3907 case ISN_LOADGDICT:
3908 case ISN_LOADBDICT:
3909 case ISN_LOADWDICT:
3910 case ISN_LOADTDICT:
3911 {
3912 dict_T *d = NULL;
3913
3914 switch (iptr->isn_type)
3915 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003916 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3917 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3918 case ISN_LOADWDICT: d = curwin->w_vars; break;
3919 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003920 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003921 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003922 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003923 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003924 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003925 tv = STACK_TV_BOT(0);
3926 tv->v_type = VAR_DICT;
3927 tv->v_lock = 0;
3928 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003929 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003930 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003931 }
3932 break;
3933
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 // load &option
3935 case ISN_LOADOPT:
3936 {
3937 typval_T optval;
3938 char_u *name = iptr->isn_arg.string;
3939
Bram Moolenaara8c17702020-04-01 21:17:24 +02003940 // This is not expected to fail, name is checked during
3941 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003942 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003943 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003944 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003945 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003947 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 }
3949 break;
3950
3951 // load $ENV
3952 case ISN_LOADENV:
3953 {
3954 typval_T optval;
3955 char_u *name = iptr->isn_arg.string;
3956
Bram Moolenaar35578162021-08-02 19:10:38 +02003957 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003958 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003959 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003960 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003962 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 }
3964 break;
3965
3966 // load @register
3967 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003968 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003969 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 tv = STACK_TV_BOT(0);
3971 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003972 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003973 // This may result in NULL, which should be equivalent to an
3974 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 tv->vval.v_string = get_reg_contents(
3976 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003977 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 break;
3979
3980 // store local variable
3981 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003982 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 tv = STACK_TV_VAR(iptr->isn_arg.number);
Ernie Rael9ed53752023-12-11 17:40:46 +01003984 if (check_typval_is_value(STACK_TV_BOT(0)) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003985 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003986 clear_tv(STACK_TV_BOT(0));
3987 goto on_error;
3988 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 clear_tv(tv);
3990 *tv = *STACK_TV_BOT(0);
3991 break;
3992
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003993 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003994 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003995 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003996 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003997 int sid = iptr->isn_arg.loadstore.ls_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003998 char_u *name = iptr->isn_arg.loadstore.ls_name;
Ernie Rael3f821d62024-04-24 20:07:50 +02003999 dictitem_T *di = NULL;
4000 // First check for a variable from an exported autoload
4001 // with an autoload_prefix; it would be in globals.
4002 if (iptr->isn_type == ISN_STOREEXPORT)
4003 di = find_var_autoload_prefix(name, sid, NULL, NULL);
4004 // Then look for a variable in the script's variables.
4005 if (di == NULL)
4006 {
4007 hashtab_T *ht = &SCRIPT_VARS(sid);
4008 di = find_var_in_ht(ht, 0, STRNCMP("s:", name, 2) == 0
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004009 ? name + 2 : name, TRUE);
Ernie Rael3f821d62024-04-24 20:07:50 +02004010 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004011
Bram Moolenaar4c137212021-04-19 16:48:48 +02004012 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004013 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004014 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004015 {
4016 if (iptr->isn_type == ISN_STOREEXPORT)
4017 {
4018 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01004019 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004020 goto on_error;
4021 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004022 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004023 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004024 else
4025 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004026 if (iptr->isn_type == ISN_STOREEXPORT)
4027 {
4028 int idx = get_script_item_idx(sid, name, 0,
4029 NULL, NULL);
4030
Bram Moolenaar06651632022-04-27 17:54:25 +01004031 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004032 if (idx >= 0)
4033 {
4034 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
4035 ->sn_var_vals.ga_data) + idx;
4036
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01004037 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004038 {
4039 semsg(_(e_item_not_exported_in_script_str),
4040 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01004041 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01004042 goto on_error;
4043 }
4044 }
4045 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02004046 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02004047 {
4048 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02004049 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02004050 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004051 clear_tv(&di->di_tv);
4052 di->di_tv = *STACK_TV_BOT(0);
4053 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004054 }
4055 break;
4056
4057 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 case ISN_STORESCRIPT:
4059 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004060 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4061 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062
Bram Moolenaar6db660b2021-08-01 14:08:54 +02004063 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004064 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004065 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004066 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02004067
4068 // "const" and "final" are checked at compile time, locking
4069 // the value needs to be checked here.
4070 SOURCING_LNUM = iptr->isn_lnum;
4071 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02004072 {
4073 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02004074 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02004075 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02004076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 clear_tv(sv->sv_tv);
4078 *sv->sv_tv = *STACK_TV_BOT(0);
4079 }
4080 break;
4081
4082 // store option
4083 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004084 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004086 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
4087 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 long n = 0;
4089 char_u *s = NULL;
4090 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00004091 char_u numbuf[NUMBUFLEN];
4092 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093
Bram Moolenaar4c137212021-04-19 16:48:48 +02004094 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02004095 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 tv = STACK_TV_BOT(0);
4097 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01004098 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01004100 if (s == NULL)
4101 s = (char_u *)"";
4102 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004103 else if (iptr->isn_type == ISN_STOREFUNCOPT)
4104 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00004105 // If the option can be set to a function reference or
4106 // a lambda and the passed value is a function
4107 // reference, then convert it to the name (string) of
4108 // the function reference.
4109 s = tv2string(tv, &tofree, numbuf, 0);
4110 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004111 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004112 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004113 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004114 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004115 goto on_error;
4116 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004119 // must be VAR_NUMBER, CHECKTYPE makes sure
4120 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004121 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02004122 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00004123 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 if (msg != NULL)
4125 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004126 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02004128 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 }
4131 break;
4132
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004133 // store $ENV
4134 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004135 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004136 tv = STACK_TV_BOT(0);
4137 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
4138 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004139 break;
4140
4141 // store @r
4142 case ISN_STOREREG:
4143 {
4144 int reg = iptr->isn_arg.number;
4145
Bram Moolenaar4c137212021-04-19 16:48:48 +02004146 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01004147 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02004148 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01004149 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004150 }
4151 break;
4152
4153 // store v: variable
4154 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004155 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004156 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
4157 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004158 // should not happen, type is checked when compiling
4159 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004160 break;
4161
Bram Moolenaard3aac292020-04-19 14:32:17 +02004162 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02004164 case ISN_STOREB:
4165 case ISN_STOREW:
4166 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004168 dictitem_T *di;
4169 hashtab_T *ht;
4170 char_u *name = iptr->isn_arg.string + 2;
4171
Bram Moolenaard3aac292020-04-19 14:32:17 +02004172 switch (iptr->isn_type)
4173 {
4174 case ISN_STOREG:
4175 ht = get_globvar_ht();
4176 break;
4177 case ISN_STOREB:
4178 ht = &curbuf->b_vars->dv_hashtab;
4179 break;
4180 case ISN_STOREW:
4181 ht = &curwin->w_vars->dv_hashtab;
4182 break;
4183 case ISN_STORET:
4184 ht = &curtab->tp_vars->dv_hashtab;
4185 break;
4186 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004187 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004188 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189
Bram Moolenaar4c137212021-04-19 16:48:48 +02004190 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004191 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004193 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 else
4195 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004196 SOURCING_LNUM = iptr->isn_lnum;
4197 if (var_check_permission(di, name) == FAIL)
4198 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 clear_tv(&di->di_tv);
4200 di->di_tv = *STACK_TV_BOT(0);
4201 }
4202 }
4203 break;
4204
Bram Moolenaar03290b82020-12-19 16:30:44 +01004205 // store an autoload variable
4206 case ISN_STOREAUTO:
4207 SOURCING_LNUM = iptr->isn_lnum;
4208 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
4209 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004210 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004211 break;
4212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 // store number in local variable
4214 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004215 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 clear_tv(tv);
4217 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004218 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 break;
4220
Bram Moolenaar590162c2022-12-24 21:24:06 +00004221 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004222 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004223 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004224 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004225
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004226 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004227 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004228 if (res == NOTDONE)
4229 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004230 }
4231 break;
4232
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004233 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004234 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004235 if (execute_storerange(iptr, ectx) == FAIL)
4236 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004237 break;
4238
Bram Moolenaard505d172022-12-18 21:42:55 +00004239 case ISN_LOAD_CLASSMEMBER:
4240 {
4241 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4242 goto theend;
4243 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004244 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4245 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004246 ++ectx->ec_stack.ga_len;
4247 }
4248 break;
4249
4250 case ISN_STORE_CLASSMEMBER:
4251 {
4252 classmember_T *cm = &iptr->isn_arg.classmember;
4253 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4254 clear_tv(tv);
4255 *tv = *STACK_TV_BOT(-1);
4256 --ectx->ec_stack.ga_len;
4257 }
4258 break;
4259
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004260 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004261 case ISN_LOADOUTER:
4262 case ISN_STOREOUTER:
4263 {
4264 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004265 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4266 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004267
4268 while (depth > 1 && outer != NULL)
4269 {
4270 outer = outer->out_up;
4271 --depth;
4272 }
4273 if (outer == NULL)
4274 {
4275 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004276 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4277 || ectx->ec_outer_ref == NULL)
4278 // Possibly :def function called from legacy
4279 // context.
4280 emsg(_(e_closure_called_from_invalid_context));
4281 else
4282 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004283 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004284 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004285 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004286 // Variable declared in loop. May be copied if the
4287 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004288 tv = ((typval_T *)outer->out_loop[-depth - 1]
4289 .stack->ga_data)
4290 + outer->out_loop[-depth - 1].var_idx
4291 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004292 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004293 // Variable declared in a function. May be copied if
4294 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004295 tv = ((typval_T *)outer->out_stack->ga_data)
4296 + outer->out_frame_idx + STACK_FRAME_SIZE
4297 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004298 if (iptr->isn_type == ISN_LOADOUTER)
4299 {
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004300 typval_T *copy;
Bram Moolenaar35578162021-08-02 19:10:38 +02004301 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004302 goto theend;
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004303 // careful: ga_grow_inner may re-alloc the stack
4304 if (depth < 0)
4305 copy = ((typval_T *)outer->out_loop[-depth - 1]
4306 .stack->ga_data)
4307 + outer->out_loop[-depth - 1].var_idx
4308 + iptr->isn_arg.outer.outer_idx;
4309 else
4310 copy = ((typval_T *)outer->out_stack->ga_data)
4311 + outer->out_frame_idx + STACK_FRAME_SIZE
4312 + iptr->isn_arg.outer.outer_idx;
4313 // memory was freed, get tv again
4314 if (copy != tv)
4315 tv = copy;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004316 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004317 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004318 }
4319 else
4320 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004321 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004322 clear_tv(tv);
4323 *tv = *STACK_TV_BOT(0);
4324 }
4325 }
4326 break;
4327
Bram Moolenaar752fc692021-01-04 21:57:11 +01004328 // unlet item in list or dict variable
4329 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004330 if (execute_unletindex(iptr, ectx) == FAIL)
4331 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004332 break;
4333
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004334 // unlet range of items in list variable
4335 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004336 if (execute_unletrange(iptr, ectx) == FAIL)
4337 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004338 break;
4339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 // push constant
4341 case ISN_PUSHNR:
4342 case ISN_PUSHBOOL:
4343 case ISN_PUSHSPEC:
4344 case ISN_PUSHF:
4345 case ISN_PUSHS:
4346 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004347 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004348 case ISN_PUSHCHANNEL:
4349 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004350 case ISN_PUSHOBJ:
4351 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004352 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004353 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004355 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004356 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 switch (iptr->isn_type)
4358 {
4359 case ISN_PUSHNR:
4360 tv->v_type = VAR_NUMBER;
4361 tv->vval.v_number = iptr->isn_arg.number;
4362 break;
4363 case ISN_PUSHBOOL:
4364 tv->v_type = VAR_BOOL;
4365 tv->vval.v_number = iptr->isn_arg.number;
4366 break;
4367 case ISN_PUSHSPEC:
4368 tv->v_type = VAR_SPECIAL;
4369 tv->vval.v_number = iptr->isn_arg.number;
4370 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 case ISN_PUSHF:
4372 tv->v_type = VAR_FLOAT;
4373 tv->vval.v_float = iptr->isn_arg.fnumber;
4374 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 case ISN_PUSHBLOB:
4376 blob_copy(iptr->isn_arg.blob, tv);
4377 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004378 case ISN_PUSHFUNC:
4379 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004380 if (iptr->isn_arg.string == NULL)
4381 tv->vval.v_string = NULL;
4382 else
4383 tv->vval.v_string =
4384 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004385 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004386 case ISN_PUSHCHANNEL:
4387#ifdef FEAT_JOB_CHANNEL
4388 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004389 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004390#endif
4391 break;
4392 case ISN_PUSHJOB:
4393#ifdef FEAT_JOB_CHANNEL
4394 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004395 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004396#endif
4397 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004398 case ISN_PUSHOBJ:
4399 tv->v_type = VAR_OBJECT;
4400 tv->vval.v_object = NULL;
4401 break;
4402 case ISN_PUSHCLASS:
4403 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004404 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004405 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 default:
4407 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004408 tv->vval.v_string = iptr->isn_arg.string == NULL
4409 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 }
4411 break;
4412
Bram Moolenaar06b77222022-01-25 15:51:56 +00004413 case ISN_AUTOLOAD:
4414 {
4415 char_u *name = iptr->isn_arg.string;
4416
4417 (void)script_autoload(name, FALSE);
4418 if (find_func(name, TRUE))
4419 {
4420 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4421 goto theend;
4422 tv = STACK_TV_BOT(0);
4423 tv->v_lock = 0;
4424 ++ectx->ec_stack.ga_len;
4425 tv->v_type = VAR_FUNC;
4426 tv->vval.v_string = vim_strsave(name);
4427 }
4428 else
4429 {
4430 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4431
4432 if (res == NOTDONE)
4433 goto theend;
4434 if (res == FAIL)
4435 goto on_error;
4436 }
4437 }
4438 break;
4439
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004440 case ISN_UNLET:
4441 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4442 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004443 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004444 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004445 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004446 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004447 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004448
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004449 case ISN_LOCKUNLOCK:
4450 {
Ernie Raelee865f32023-09-29 19:53:55 +02004451#ifdef LOG_LOCKVAR
4452 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4453 iptr->isn_arg.string);
4454#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004455 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004456
4457 // Stack has the local variable, argument the whole :lock
4458 // or :unlock command, like ISN_EXEC.
4459 --ectx->ec_stack.ga_len;
Zoltan Arpadffy7b12ac32024-12-29 09:50:20 +01004460 lval_root_T root;
4461 root.lr_tv = STACK_TV_BOT(0);
4462 root.lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec;
4463 root.lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg;
Ernie Rael64885642023-10-04 20:16:22 +02004464 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004465 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004466 iptr->isn_arg.lockunlock.lu_string);
4467 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004468 lval_root = lval_root_save;
4469 if (res == FAIL)
4470 goto on_error;
4471 }
4472 break;
4473
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004474 case ISN_LOCKCONST:
4475 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4476 break;
4477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 // create a list from items on the stack; uses a single allocation
4479 // for the list header and the items
4480 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004481 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004482 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483 break;
4484
4485 // create a dict from items on the stack
4486 case ISN_NEWDICT:
4487 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004488 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004490 SOURCING_LNUM = iptr->isn_lnum;
4491 res = exe_newdict(iptr->isn_arg.number, ectx);
4492 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004493 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004494 if (res == MAYBE)
4495 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 }
4497 break;
4498
LemonBoy372bcce2022-04-25 12:43:20 +01004499 case ISN_CONCAT:
4500 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4501 goto theend;
4502 break;
4503
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004504 // create a partial with NULL value
4505 case ISN_NEWPARTIAL:
4506 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4507 goto theend;
4508 ++ectx->ec_stack.ga_len;
4509 tv = STACK_TV_BOT(-1);
4510 tv->v_type = VAR_PARTIAL;
4511 tv->v_lock = 0;
4512 tv->vval.v_partial = NULL;
4513 break;
4514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 // call a :def function
4516 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004517 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004518 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4519 NULL,
4520 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004521 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004522 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 break;
4524
Bram Moolenaard0200c82023-01-28 15:19:40 +00004525 // call a method on an interface
4526 case ISN_METHODCALL:
4527 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004528 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4529
Bram Moolenaard0200c82023-01-28 15:19:40 +00004530 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004531 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004532 if (tv->v_type != VAR_OBJECT)
4533 {
4534 object_required_error(tv);
4535 goto on_error;
4536 }
Ernie Raelbe828252024-07-26 18:37:02 +02004537
Bram Moolenaard0200c82023-01-28 15:19:40 +00004538 object_T *obj = tv->vval.v_object;
Ernie Raelbe828252024-07-26 18:37:02 +02004539 if (obj == NULL)
4540 {
4541 emsg(_(e_using_null_object));
4542 goto on_error;
4543 }
4544
Ernie Rael58c95792024-08-13 23:27:22 +02004545 ufunc_T *ufunc;
4546 if (mfunc->cmf_is_super)
4547 // Doing "super.Func", use the specific ufunc.
4548 ufunc = mfunc->cmf_itf->class_obj_methods[
4549 mfunc->cmf_idx];
4550 else
4551 {
4552 class_T *cl = obj->obj_class;
Bram Moolenaard0200c82023-01-28 15:19:40 +00004553
Ernie Rael58c95792024-08-13 23:27:22 +02004554 // convert the interface index to the object index
4555 int idx = object_index_from_itf_index(mfunc->cmf_itf,
4556 TRUE, mfunc->cmf_idx, cl);
4557 ufunc = cl->class_obj_methods[idx];
4558 }
Bram Moolenaard0200c82023-01-28 15:19:40 +00004559
Ernie Rael58c95792024-08-13 23:27:22 +02004560 if (call_ufunc(ufunc, NULL, mfunc->cmf_argcount, ectx,
4561 NULL, NULL) == FAIL)
Bram Moolenaard0200c82023-01-28 15:19:40 +00004562 goto on_error;
4563 }
4564 break;
4565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 // call a builtin function
4567 case ISN_BCALL:
4568 SOURCING_LNUM = iptr->isn_lnum;
4569 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4570 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004571 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004572 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 break;
4574
4575 // call a funcref or partial
4576 case ISN_PCALL:
4577 {
4578 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4579 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004580 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581
4582 SOURCING_LNUM = iptr->isn_lnum;
4583 if (pfunc->cpf_top)
4584 {
4585 // funcref is above the arguments
4586 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4587 }
4588 else
4589 {
4590 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004591 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004592 partial_tv = *STACK_TV_BOT(0);
4593 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004595 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004596 if (tv == &partial_tv)
4597 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004599 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 }
4601 break;
4602
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004603 case ISN_PCALL_END:
4604 // PCALL finished, arguments have been consumed and replaced by
4605 // the return value. Now clear the funcref from the stack,
4606 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004607 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004608 clear_tv(STACK_TV_BOT(-1));
4609 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4610 break;
4611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 // call a user defined function or funcref/partial
4613 case ISN_UCALL:
4614 {
4615 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4616
4617 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004618 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004619 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004620 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 }
4622 break;
4623
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004624 // :defer func(arg)
4625 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004626 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004627 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4628 goto on_error;
4629 break;
4630
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004631 // Return from a :def function call without a value.
4632 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004633 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004634 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004635 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004636 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004637 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004638 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004639 if (iptr->isn_type == ISN_RETURN_VOID)
4640 {
4641 tv->v_type = VAR_VOID;
4642 tv->vval.v_number = 0;
4643 tv->v_lock = 0;
4644 }
4645 else
4646 {
4647 *tv = *STACK_TV_VAR(0);
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01004648 object_T *obj = tv->vval.v_object;
4649 ++obj->obj_refcount;
4650
4651 // Lock all the constant object variables
4652 obj_lock_const_vars(obj);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004653 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004654 // FALLTHROUGH
4655
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004656 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 case ISN_RETURN:
4658 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004659 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004660 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004661
Ernie Raelb077b582023-12-14 20:11:44 +01004662 ///////////////////////////////////////////////////
4663 // TODO: If FAIL, line number in output not correct
4664 ///////////////////////////////////////////////////
4665 if (check_typval_is_value(STACK_TV_BOT(-1)) == FAIL)
4666 goto theend;
4667
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004668 if (trystack->ga_len > 0)
4669 trycmd = ((trycmd_T *)trystack->ga_data)
4670 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004671 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004672 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004674 // jump to ":finally" or ":endtry"
4675 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004676 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004677 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004678 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 trycmd->tcd_return = TRUE;
4680 }
4681 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004682 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 }
4684 break;
4685
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004686 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 case ISN_FUNCREF:
4688 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004689 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4690 ufunc_T *ufunc;
4691 funcref_T *funcref = &iptr->isn_arg.funcref;
4692 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004695 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004696 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004697 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004698 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004699 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004700 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004701 if (extra != NULL && extra->fre_class != NULL)
4702 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004703 class_T *cl;
4704 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004705 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004706 tv = STACK_TV_BOT(-1);
4707 if (tv->v_type != VAR_OBJECT)
4708 {
Ernie Raelbe828252024-07-26 18:37:02 +02004709 SOURCING_LNUM = iptr->isn_lnum;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004710 object_required_error(tv);
4711 vim_free(pt);
4712 goto on_error;
4713 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004714
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004715 object_T *obj = tv->vval.v_object;
Ernie Raelbe828252024-07-26 18:37:02 +02004716 if (obj == NULL)
4717 {
4718 SOURCING_LNUM = iptr->isn_lnum;
4719 emsg(_(e_using_null_object));
4720 vim_free(pt);
4721 goto on_error;
4722 }
4723
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004724 cl = obj->obj_class;
4725 // drop the value from the stack
4726 clear_tv(tv);
4727 --ectx->ec_stack.ga_len;
4728
4729 pt->pt_obj = obj;
4730 ++obj->obj_refcount;
4731 }
4732 else
4733 cl = extra->fre_class;
4734
4735 if (extra->fre_object_method)
4736 {
4737 // object method
4738 // convert the interface index to the object index
4739 int idx =
4740 object_index_from_itf_index(extra->fre_class,
4741 TRUE, extra->fre_method_idx, cl);
4742 ufunc = cl->class_obj_methods[idx];
4743 }
4744 else
4745 {
4746 // class method
4747 ufunc =
4748 cl->class_class_functions[extra->fre_method_idx];
4749 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004750 }
4751 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004752 {
4753 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4754 + funcref->fr_dfunc_idx;
4755
4756 ufunc = pt_dfunc->df_ufunc;
4757 }
4758 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004759 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004760 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004761 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004762 if (ufunc == NULL)
4763 {
4764 SOURCING_LNUM = iptr->isn_lnum;
4765 iemsg("ufunc unexpectedly NULL for FUNCREF");
Christian Brabandt915f3bf2024-04-05 20:12:19 +02004766 vim_free(pt);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004767 goto theend;
4768 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004769 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004770 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004771 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004772 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004774 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 tv->vval.v_partial = pt;
4776 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004777 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 }
4779 break;
4780
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004781 // Create a global function from a lambda.
4782 case ISN_NEWFUNC:
4783 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004784 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004785
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004786 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004787 arg->nfa_global, &arg->nfa_loopvar_info,
4788 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004789 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004790 }
4791 break;
4792
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004793 // List functions
4794 case ISN_DEF:
4795 if (iptr->isn_arg.string == NULL)
4796 list_functions(NULL);
4797 else
4798 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004799 exarg_T ea;
4800 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004801
4802 CLEAR_FIELD(ea);
4803 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004804 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004805 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004806 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004807 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004808 }
4809 break;
4810
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 // jump if a condition is met
4812 case ISN_JUMP:
4813 {
4814 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004815 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 int jump = TRUE;
4817
4818 if (when != JUMP_ALWAYS)
4819 {
4820 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004821 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004822 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004823 || when == JUMP_IF_COND_TRUE)
4824 {
4825 SOURCING_LNUM = iptr->isn_lnum;
4826 jump = tv_get_bool_chk(tv, &error);
4827 if (error)
4828 goto on_error;
4829 }
4830 else
4831 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004832 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004834 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 {
4836 // drop the value from the stack
4837 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004838 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 }
4840 }
4841 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004842 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 }
4844 break;
4845
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004846 // "while": jump to end if a condition is false
4847 case ISN_WHILE:
4848 {
4849 int error = FALSE;
4850 int jump = TRUE;
4851
4852 tv = STACK_TV_BOT(-1);
4853 SOURCING_LNUM = iptr->isn_lnum;
4854 jump = !tv_get_bool_chk(tv, &error);
4855 if (error)
4856 goto on_error;
4857 // drop the value from the stack
4858 clear_tv(tv);
4859 --ectx->ec_stack.ga_len;
4860 if (jump)
4861 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4862
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004863 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004864 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004865 tv = STACK_TV_VAR(
4866 iptr->isn_arg.whileloop.while_funcref_idx);
4867 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4868 }
4869 break;
4870
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004871 // Jump if an argument with a default value was already set and not
4872 // v:none.
4873 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004874 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004875 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004876 int arg_set = tv->v_type != VAR_UNKNOWN
4877 && !(tv->v_type == VAR_SPECIAL
4878 && tv->vval.v_number == VVAL_NONE);
Yegappan Lakshmananb04af4c2025-01-03 10:50:08 +01004879
4880 if (iptr->isn_type == ISN_JUMP_IF_ARG_NOT_SET && !arg_set)
4881 {
4882 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4883 + ectx->ec_dfunc_idx;
4884 ufunc_T *ufunc = df->df_ufunc;
4885 // jump_arg_off is negative for arguments
4886 size_t argidx = ufunc->uf_def_args.ga_len
4887 + iptr->isn_arg.jumparg.jump_arg_off
4888 + STACK_FRAME_SIZE;
4889 type_T *t = ufunc->uf_arg_types[argidx];
Yegappan Lakshmanan2050dcc2025-01-06 18:34:49 +01004890 CLEAR_POINTER(tv);
Yegappan Lakshmananb04af4c2025-01-03 10:50:08 +01004891 tv->v_type = t->tt_type;
4892 }
4893
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004894 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004895 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004896 break;
4897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 // top of a for loop
4899 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004900 if (execute_for(iptr, ectx) == FAIL)
4901 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 break;
4903
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004904 // end of a for or while loop
4905 case ISN_ENDLOOP:
4906 if (execute_endloop(iptr, ectx) == FAIL)
4907 goto theend;
4908 break;
4909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 // start of ":try" block
4911 case ISN_TRY:
4912 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004913 trycmd_T *trycmd = NULL;
4914
Bram Moolenaar35578162021-08-02 19:10:38 +02004915 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004916 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004917 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4918 + ectx->ec_trystack.ga_len;
4919 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004921 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004922 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4923 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004924 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004925 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004926 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004927 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004928 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004929 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 }
4931 break;
4932
4933 case ISN_PUSHEXC:
4934 if (current_exception == NULL)
4935 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004936 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004938 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004940 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004941 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004943 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004945 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946 tv->vval.v_string = vim_strsave(
4947 (char_u *)current_exception->value);
4948 break;
4949
4950 case ISN_CATCH:
4951 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004952 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004953 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954
Bram Moolenaar4c137212021-04-19 16:48:48 +02004955 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004956 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004958 trycmd->tcd_caught = TRUE;
4959 trycmd->tcd_did_throw = FALSE;
4960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004962 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 catch_exception(current_exception);
4964 }
4965 break;
4966
Bram Moolenaarc150c092021-02-13 15:02:46 +01004967 case ISN_TRYCONT:
4968 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004969 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004970 trycont_T *trycont = &iptr->isn_arg.trycont;
4971 int i;
4972 trycmd_T *trycmd;
4973 int iidx = trycont->tct_where;
4974
4975 if (trystack->ga_len < trycont->tct_levels)
4976 {
4977 siemsg("TRYCONT: expected %d levels, found %d",
4978 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004979 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004980 }
4981 // Make :endtry jump to any outer try block and the last
4982 // :endtry inside the loop to the loop start.
4983 for (i = trycont->tct_levels; i > 0; --i)
4984 {
4985 trycmd = ((trycmd_T *)trystack->ga_data)
4986 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004987 // Add one to tcd_cont to be able to jump to
4988 // instruction with index zero.
4989 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004990 iidx = trycmd->tcd_finally_idx == 0
4991 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004992 }
4993 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004994 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004995 }
4996 break;
4997
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004998 case ISN_FINALLY:
4999 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005000 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01005001 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
5002 + trystack->ga_len - 1;
5003
5004 // Reset the index to avoid a return statement jumps here
5005 // again.
5006 trycmd->tcd_finally_idx = 0;
Yee Cheng Chin2051af12025-01-09 22:14:34 +01005007 if (trycmd->tcd_caught)
5008 {
5009 // discard the exception
5010 finish_exception(caught_stack);
5011 trycmd->tcd_caught = FALSE;
5012 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01005013 break;
5014 }
5015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 // end of ":try" block
5017 case ISN_ENDTRY:
5018 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005019 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01005020 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021
Bram Moolenaar06651632022-04-27 17:54:25 +01005022 --trystack->ga_len;
5023 --trylevel;
5024 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
5025 if (trycmd->tcd_did_throw)
5026 did_throw = TRUE;
Yee Cheng Chin2051af12025-01-09 22:14:34 +01005027 if (trycmd->tcd_caught)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 {
Bram Moolenaar06651632022-04-27 17:54:25 +01005029 // discard the exception
Yee Cheng Chin2051af12025-01-09 22:14:34 +01005030 finish_exception(caught_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 }
Bram Moolenaar06651632022-04-27 17:54:25 +01005032
5033 if (trycmd->tcd_return)
5034 goto func_return;
5035
5036 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
5037 {
5038 --ectx->ec_stack.ga_len;
5039 clear_tv(STACK_TV_BOT(0));
5040 }
5041 if (trycmd->tcd_cont != 0)
5042 // handling :continue: jump to outer try block or
5043 // start of the loop
5044 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 }
5046 break;
5047
5048 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01005049 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005050 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02005051
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005052 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
5053 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01005054 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005055 // the function to abort but not display an error.
5056 tv = STACK_TV_BOT(-1);
5057 clear_tv(tv);
5058 tv->v_type = VAR_NUMBER;
5059 tv->vval.v_number = 0;
5060 goto done;
5061 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005062 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005063 tv = STACK_TV_BOT(0);
5064 if (tv->vval.v_string == NULL
5065 || *skipwhite(tv->vval.v_string) == NUL)
5066 {
5067 vim_free(tv->vval.v_string);
5068 SOURCING_LNUM = iptr->isn_lnum;
5069 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005070 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005071 }
5072
5073 // Inside a "catch" we need to first discard the caught
5074 // exception.
5075 if (trystack->ga_len > 0)
5076 {
5077 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
5078 + trystack->ga_len - 1;
Yee Cheng Chin2051af12025-01-09 22:14:34 +01005079 if (trycmd->tcd_caught)
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005080 {
5081 // discard the exception
Yee Cheng Chin2051af12025-01-09 22:14:34 +01005082 finish_exception(caught_stack);
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005083 trycmd->tcd_caught = FALSE;
5084 }
5085 }
5086
Bram Moolenaar90a57162022-02-12 14:23:17 +00005087 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005088 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
5089 == FAIL)
5090 {
5091 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005092 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005093 }
5094 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 break;
5097
5098 // compare with special values
5099 case ISN_COMPAREBOOL:
5100 case ISN_COMPARESPECIAL:
5101 {
5102 typval_T *tv1 = STACK_TV_BOT(-2);
5103 typval_T *tv2 = STACK_TV_BOT(-1);
5104 varnumber_T arg1 = tv1->vval.v_number;
5105 varnumber_T arg2 = tv2->vval.v_number;
5106 int res;
5107
Bram Moolenaar06651632022-04-27 17:54:25 +01005108 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
5109 res = arg1 == arg2;
5110 else
5111 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112
Bram Moolenaar4c137212021-04-19 16:48:48 +02005113 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 tv1->v_type = VAR_BOOL;
5115 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5116 }
5117 break;
5118
Bram Moolenaar7a222242022-03-01 19:23:24 +00005119 case ISN_COMPARENULL:
5120 {
5121 typval_T *tv1 = STACK_TV_BOT(-2);
5122 typval_T *tv2 = STACK_TV_BOT(-1);
5123 int res;
5124
5125 res = typval_compare_null(tv1, tv2);
5126 if (res == MAYBE)
5127 goto on_error;
5128 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
5129 res = !res;
5130 clear_tv(tv1);
5131 clear_tv(tv2);
5132 --ectx->ec_stack.ga_len;
5133 tv1->v_type = VAR_BOOL;
5134 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5135 }
5136 break;
5137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 // Operation with two number arguments
5139 case ISN_OPNR:
5140 case ISN_COMPARENR:
5141 {
5142 typval_T *tv1 = STACK_TV_BOT(-2);
5143 typval_T *tv2 = STACK_TV_BOT(-1);
5144 varnumber_T arg1 = tv1->vval.v_number;
5145 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02005146 varnumber_T res = 0;
5147 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005149 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
5150 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
5151 {
5152 if (arg2 < 0)
5153 {
5154 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00005155 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005156 goto on_error;
5157 }
5158 }
5159
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005160 switch (iptr->isn_arg.op.op_type)
5161 {
5162 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02005163 case EXPR_DIV: if (arg2 == 0)
5164 div_zero = TRUE;
5165 else
5166 res = arg1 / arg2;
5167 break;
5168 case EXPR_REM: if (arg2 == 0)
5169 div_zero = TRUE;
5170 else
5171 res = arg1 % arg2;
5172 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 case EXPR_SUB: res = arg1 - arg2; break;
5174 case EXPR_ADD: res = arg1 + arg2; break;
5175
5176 case EXPR_EQUAL: res = arg1 == arg2; break;
5177 case EXPR_NEQUAL: res = arg1 != arg2; break;
5178 case EXPR_GREATER: res = arg1 > arg2; break;
5179 case EXPR_GEQUAL: res = arg1 >= arg2; break;
5180 case EXPR_SMALLER: res = arg1 < arg2; break;
5181 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005182 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
5183 res = 0;
5184 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01005185 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005186 break;
5187 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
5188 res = 0;
5189 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01005190 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005191 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02005192 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 }
5194
Bram Moolenaar4c137212021-04-19 16:48:48 +02005195 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196 if (iptr->isn_type == ISN_COMPARENR)
5197 {
5198 tv1->v_type = VAR_BOOL;
5199 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5200 }
5201 else
5202 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02005203 if (div_zero)
5204 {
5205 SOURCING_LNUM = iptr->isn_lnum;
5206 emsg(_(e_divide_by_zero));
5207 goto on_error;
5208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209 }
5210 break;
5211
5212 // Computation with two float arguments
5213 case ISN_OPFLOAT:
5214 case ISN_COMPAREFLOAT:
5215 {
5216 typval_T *tv1 = STACK_TV_BOT(-2);
5217 typval_T *tv2 = STACK_TV_BOT(-1);
5218 float_T arg1 = tv1->vval.v_float;
5219 float_T arg2 = tv2->vval.v_float;
5220 float_T res = 0;
5221 int cmp = FALSE;
5222
5223 switch (iptr->isn_arg.op.op_type)
5224 {
5225 case EXPR_MULT: res = arg1 * arg2; break;
5226 case EXPR_DIV: res = arg1 / arg2; break;
5227 case EXPR_SUB: res = arg1 - arg2; break;
5228 case EXPR_ADD: res = arg1 + arg2; break;
5229
5230 case EXPR_EQUAL: cmp = arg1 == arg2; break;
5231 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
5232 case EXPR_GREATER: cmp = arg1 > arg2; break;
5233 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
5234 case EXPR_SMALLER: cmp = arg1 < arg2; break;
5235 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
5236 default: cmp = 0; break;
5237 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005238 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239 if (iptr->isn_type == ISN_COMPAREFLOAT)
5240 {
5241 tv1->v_type = VAR_BOOL;
5242 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
5243 }
5244 else
5245 tv1->vval.v_float = res;
5246 }
5247 break;
5248
5249 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00005250 case ISN_COMPAREDICT:
5251 case ISN_COMPAREFUNC:
5252 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005254 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255 {
5256 typval_T *tv1 = STACK_TV_BOT(-2);
5257 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00005258 exprtype_T exprtype = iptr->isn_arg.op.op_type;
5259 int ic = iptr->isn_arg.op.op_ic;
5260 int res = FALSE;
5261 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005262
Bram Moolenaar265f8112021-12-19 12:33:05 +00005263 SOURCING_LNUM = iptr->isn_lnum;
5264 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005266 status = typval_compare_list(tv1, tv2,
5267 exprtype, ic, &res);
5268 }
5269 else if (iptr->isn_type == ISN_COMPAREDICT)
5270 {
5271 status = typval_compare_dict(tv1, tv2,
5272 exprtype, ic, &res);
5273 }
5274 else if (iptr->isn_type == ISN_COMPAREFUNC)
5275 {
5276 status = typval_compare_func(tv1, tv2,
5277 exprtype, ic, &res);
5278 }
5279 else if (iptr->isn_type == ISN_COMPARESTRING)
5280 {
5281 status = typval_compare_string(tv1, tv2,
5282 exprtype, ic, &res);
5283 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005284 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005285 {
5286 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005287 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005288 else // ISN_COMPAREOBJECT
5289 {
5290 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005291 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005292 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005293 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005294 clear_tv(tv1);
5295 clear_tv(tv2);
5296 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005297 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5298 if (status == FAIL)
5299 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005300 }
5301 break;
5302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303 case ISN_COMPAREANY:
5304 {
5305 typval_T *tv1 = STACK_TV_BOT(-2);
5306 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005307 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005309 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310
Bram Moolenaareb26f432020-09-14 16:50:05 +02005311 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005312 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005313 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005314 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005315 if (status == FAIL)
5316 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317 }
5318 break;
5319
5320 case ISN_ADDLIST:
5321 case ISN_ADDBLOB:
5322 {
5323 typval_T *tv1 = STACK_TV_BOT(-2);
5324 typval_T *tv2 = STACK_TV_BOT(-1);
5325
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005326 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005327 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005328 {
5329 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5330 && tv1->vval.v_list != NULL)
5331 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5332 NULL);
5333 else
5334 eval_addlist(tv1, tv2);
5335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336 else
5337 eval_addblob(tv1, tv2);
5338 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005339 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005340 }
5341 break;
5342
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005343 case ISN_LISTAPPEND:
5344 {
5345 typval_T *tv1 = STACK_TV_BOT(-2);
5346 typval_T *tv2 = STACK_TV_BOT(-1);
5347 list_T *l = tv1->vval.v_list;
5348
5349 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005350 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005351 if (l == NULL)
5352 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005353 emsg(_(e_cannot_add_to_null_list));
5354 goto on_error;
5355 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005356 if (value_check_lock(l->lv_lock, NULL, FALSE))
5357 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005358 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005359 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005360 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005361 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005362 }
5363 break;
5364
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005365 case ISN_BLOBAPPEND:
5366 {
5367 typval_T *tv1 = STACK_TV_BOT(-2);
5368 typval_T *tv2 = STACK_TV_BOT(-1);
5369 blob_T *b = tv1->vval.v_blob;
5370 int error = FALSE;
5371 varnumber_T n;
5372
5373 // add a number to a blob
5374 if (b == NULL)
5375 {
5376 SOURCING_LNUM = iptr->isn_lnum;
5377 emsg(_(e_cannot_add_to_null_blob));
5378 goto on_error;
5379 }
5380 n = tv_get_number_chk(tv2, &error);
5381 if (error)
5382 goto on_error;
5383 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005384 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005385 }
5386 break;
5387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005388 // Computation with two arguments of unknown type
5389 case ISN_OPANY:
5390 {
5391 typval_T *tv1 = STACK_TV_BOT(-2);
5392 typval_T *tv2 = STACK_TV_BOT(-1);
5393 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395 int error = FALSE;
5396
5397 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5398 {
5399 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5400 {
5401 eval_addlist(tv1, tv2);
5402 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005403 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005404 break;
5405 }
5406 else if (tv1->v_type == VAR_BLOB
5407 && tv2->v_type == VAR_BLOB)
5408 {
5409 eval_addblob(tv1, tv2);
5410 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005411 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005412 break;
5413 }
5414 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005415 if (tv1->v_type == VAR_FLOAT)
5416 {
5417 f1 = tv1->vval.v_float;
5418 n1 = 0;
5419 }
5420 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005422 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 n1 = tv_get_number_chk(tv1, &error);
5424 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005425 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 if (tv2->v_type == VAR_FLOAT)
5427 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429 if (tv2->v_type == VAR_FLOAT)
5430 {
5431 f2 = tv2->vval.v_float;
5432 n2 = 0;
5433 }
5434 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005435 {
5436 n2 = tv_get_number_chk(tv2, &error);
5437 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005438 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439 if (tv1->v_type == VAR_FLOAT)
5440 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 // if there is a float on either side the result is a float
5443 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5444 {
5445 switch (iptr->isn_arg.op.op_type)
5446 {
5447 case EXPR_MULT: f1 = f1 * f2; break;
5448 case EXPR_DIV: f1 = f1 / f2; break;
5449 case EXPR_SUB: f1 = f1 - f2; break;
5450 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005451 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005452 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005453 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454 }
5455 clear_tv(tv1);
5456 clear_tv(tv2);
5457 tv1->v_type = VAR_FLOAT;
5458 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005459 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005460 }
5461 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005463 int failed = FALSE;
5464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005465 switch (iptr->isn_arg.op.op_type)
5466 {
5467 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005468 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5469 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005470 goto on_error;
5471 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005472 case EXPR_SUB: n1 = n1 - n2; break;
5473 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005474 default: n1 = num_modulus(n1, n2, &failed);
5475 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005476 goto on_error;
5477 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005478 }
5479 clear_tv(tv1);
5480 clear_tv(tv2);
5481 tv1->v_type = VAR_NUMBER;
5482 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005483 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005484 }
5485 }
5486 break;
5487
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005488 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005489 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005490 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005491 int is_slice = iptr->isn_type == ISN_STRSLICE;
5492 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005493 char_u *res;
5494
5495 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005496 // string slice: string is at stack-3, first index at
5497 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005498 if (is_slice)
5499 {
5500 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005501 n1 = tv->vval.v_number;
5502 }
5503
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005504 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005505 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005506
Bram Moolenaar4c137212021-04-19 16:48:48 +02005507 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005508 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005509 if (is_slice)
5510 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005511 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005512 else
5513 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005514 // single character (including composing characters).
5515 // If the index is too big or negative the result is
5516 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005517 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005518 vim_free(tv->vval.v_string);
5519 tv->vval.v_string = res;
5520 }
5521 break;
5522
5523 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005524 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005525 case ISN_BLOBINDEX:
5526 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005527 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005528 int is_slice = iptr->isn_type == ISN_LISTSLICE
5529 || iptr->isn_type == ISN_BLOBSLICE;
5530 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5531 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005532 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005533 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005534
5535 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005536 // list slice: list is at stack-3, indexes at stack-2 and
5537 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005538 // Same for blob.
5539 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005540
5541 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005542 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005543 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005544
5545 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 {
Bram Moolenaared591872020-08-15 22:14:53 +02005547 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005548 n1 = tv->vval.v_number;
5549 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550 }
Bram Moolenaared591872020-08-15 22:14:53 +02005551
Bram Moolenaar4c137212021-04-19 16:48:48 +02005552 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005553 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005554 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005555 if (is_blob)
5556 {
5557 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5558 n1, n2, FALSE, tv) == FAIL)
5559 goto on_error;
5560 }
5561 else
5562 {
5563 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5564 n1, n2, FALSE, tv, TRUE) == FAIL)
5565 goto on_error;
5566 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005567 }
5568 break;
5569
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005570 case ISN_ANYINDEX:
5571 case ISN_ANYSLICE:
5572 {
5573 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5574 typval_T *var1, *var2;
5575 int res;
5576
5577 // index: composite is at stack-2, index at stack-1
5578 // slice: composite is at stack-3, indexes at stack-2 and
5579 // stack-1
5580 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005581 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005582 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5583 goto on_error;
5584 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5585 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005586 res = eval_index_inner(tv, is_slice, var1, var2,
5587 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005588 clear_tv(var1);
5589 if (is_slice)
5590 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005591 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005592 if (res == FAIL)
5593 goto on_error;
5594 }
5595 break;
5596
Bram Moolenaar9af78762020-06-16 11:34:42 +02005597 case ISN_SLICE:
5598 {
5599 list_T *list;
5600 int count = iptr->isn_arg.number;
5601
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005602 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005603 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005604 list = tv->vval.v_list;
5605
5606 // no error for short list, expect it to be checked earlier
5607 if (list != NULL && list->lv_len >= count)
5608 {
5609 list_T *newlist = list_slice(list,
5610 count, list->lv_len - 1);
5611
5612 if (newlist != NULL)
5613 {
5614 list_unref(list);
5615 tv->vval.v_list = newlist;
5616 ++newlist->lv_refcount;
5617 }
5618 }
5619 }
5620 break;
5621
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005622 case ISN_GETITEM:
5623 {
5624 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005625 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005626
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005627 // Get list item: list is at stack-1, push item.
5628 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005629 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5630 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005631
Bram Moolenaar35578162021-08-02 19:10:38 +02005632 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005633 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005634 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005635 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005636
5637 // Useful when used in unpack assignment. Reset at
5638 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005639 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005640 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005641 }
5642 break;
5643
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01005644 // dict member with string key (dict['member'])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005645 case ISN_MEMBER:
5646 {
5647 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005648 char_u *key;
5649 dictitem_T *di;
5650
5651 // dict member: dict is at stack-2, key at stack-1
5652 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005653 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005654 dict = tv->vval.v_dict;
5655
5656 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005657 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005658 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005659 if (key == NULL)
5660 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005661
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005662 if ((di = dict_find(dict, key, -1)) == NULL)
5663 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005664 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005665 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005666
5667 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005668 // stack contents makes sense and the dict stack is
5669 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005670 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005671 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005672 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005673 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005674 tv->v_type = VAR_NUMBER;
5675 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005676 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005677 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005678 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005679 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005680 // Put the dict used on the dict stack, it might be used by
5681 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005682 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005683 if (dict_stack_save(tv) == FAIL)
5684 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005685 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005686 }
5687 break;
5688
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01005689 // dict member with string key (dict.member)
5690 // or can be an object
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005691 case ISN_STRINGMEMBER:
5692 {
5693 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005694 dictitem_T *di;
5695
5696 tv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005697
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01005698 if (tv->v_type == VAR_OBJECT
5699 || tv->v_type == VAR_CLASS)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005700 {
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01005701 if (dict_stack_save(tv) == FAIL)
5702 goto on_fatal_error;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005703
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01005704 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
5705 + ectx->ec_dfunc_idx)->df_ufunc;
Yegappan Lakshmanan778ada42025-02-17 20:21:23 +01005706 // Class or an object (not a Dict)
5707 if (var_any_get_oc_member(ufunc->uf_class, iptr, tv) == FAIL)
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01005708 goto on_error;
5709 }
5710 else
5711 {
5712 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5713 {
5714 SOURCING_LNUM = iptr->isn_lnum;
5715 emsg(_(e_dictionary_required));
5716 goto on_error;
5717 }
5718 dict = tv->vval.v_dict;
5719
5720 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5721 == NULL)
5722 {
5723 SOURCING_LNUM = iptr->isn_lnum;
5724 semsg(_(e_key_not_present_in_dictionary_str),
5725 iptr->isn_arg.string);
5726 goto on_error;
5727 }
5728 // Put the dict used on the dict stack, it might be
5729 // used by a dict function later.
5730 if (dict_stack_save(tv) == FAIL)
5731 goto on_fatal_error;
5732
5733 copy_tv(&di->di_tv, tv);
5734 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005735 }
5736 break;
5737
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005738 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005739 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005740 {
5741 tv = STACK_TV_BOT(-1);
5742 if (tv->v_type != VAR_OBJECT)
5743 {
5744 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005745 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005746 goto on_error;
5747 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005748
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005749 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005750 if (obj == NULL)
5751 {
5752 SOURCING_LNUM = iptr->isn_lnum;
5753 emsg(_(e_using_null_object));
5754 goto on_error;
5755 }
5756
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005757 int idx;
5758 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005759 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005760 else
5761 {
5762 idx = iptr->isn_arg.classmember.cm_idx;
5763 // convert the interface index to the object index
5764 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005765 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005766 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005767 }
5768
Ernie Rael18143d32023-09-04 22:30:41 +02005769 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005770 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Yegappan Lakshmananb04af4c2025-01-03 10:50:08 +01005771 if (mtv->v_type == VAR_UNKNOWN)
5772 {
5773 // Referencing an object variable (without a type)
5774 // which is not yet initialized. So the type is not
5775 // yet known.
5776 ocmember_T *m = &obj->obj_class->class_obj_members[idx];
5777 SOURCING_LNUM = iptr->isn_lnum;
5778 semsg(_(e_uninitialized_object_var_reference),
5779 m->ocm_name);
5780 goto on_error;
5781 }
Bram Moolenaar590162c2022-12-24 21:24:06 +00005782 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005783
5784 // Unreference the object after getting the member, it may
5785 // be freed.
5786 object_unref(obj);
5787 }
5788 break;
5789
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005790 case ISN_STORE_THIS:
5791 {
5792 int idx = iptr->isn_arg.number;
5793 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5794 // the members are located right after the object struct
5795 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5796 clear_tv(mtv);
5797 *mtv = *STACK_TV_BOT(-1);
5798 --ectx->ec_stack.ga_len;
5799 }
5800 break;
5801
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005802 case ISN_CLEARDICT:
5803 dict_stack_drop();
5804 break;
5805
5806 case ISN_USEDICT:
5807 {
5808 typval_T *dict_tv = dict_stack_get_tv();
5809
5810 // Turn "dict.Func" into a partial for "Func" bound to
5811 // "dict". Don't do this when "Func" is already a partial
5812 // that was bound explicitly (pt_auto is FALSE).
5813 tv = STACK_TV_BOT(-1);
5814 if (dict_tv != NULL
5815 && dict_tv->v_type == VAR_DICT
5816 && dict_tv->vval.v_dict != NULL
5817 && (tv->v_type == VAR_FUNC
5818 || (tv->v_type == VAR_PARTIAL
5819 && (tv->vval.v_partial->pt_auto
5820 || tv->vval.v_partial->pt_dict == NULL))))
5821 dict_tv->vval.v_dict =
5822 make_partial(dict_tv->vval.v_dict, tv);
5823 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005824 }
5825 break;
5826
5827 case ISN_NEGATENR:
5828 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005829 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005830 if (tv->v_type == VAR_FLOAT)
5831 tv->vval.v_float = -tv->vval.v_float;
5832 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005833 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005834 break;
5835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005836 case ISN_CHECKTYPE:
5837 {
5838 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005839 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005840 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005841
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005842 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005843 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005844 if (ct->ct_arg_idx > 0)
5845 {
5846 where.wt_index = ct->ct_arg_idx;
5847 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005848 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005849 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005850 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005851 if (r == FAIL)
5852 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005853
5854 // number 0 is FALSE, number 1 is TRUE
5855 if (tv->v_type == VAR_NUMBER
5856 && ct->ct_type->tt_type == VAR_BOOL
5857 && (tv->vval.v_number == 0
5858 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005859 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005860 tv->v_type = VAR_BOOL;
5861 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005862 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005863 }
5864 }
5865 break;
5866
Bram Moolenaar9af78762020-06-16 11:34:42 +02005867 case ISN_CHECKLEN:
5868 {
5869 int min_len = iptr->isn_arg.checklen.cl_min_len;
5870 list_T *list = NULL;
5871
5872 tv = STACK_TV_BOT(-1);
5873 if (tv->v_type == VAR_LIST)
5874 list = tv->vval.v_list;
5875 if (list == NULL || list->lv_len < min_len
5876 || (list->lv_len > min_len
5877 && !iptr->isn_arg.checklen.cl_more_OK))
5878 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005879 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005880 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005881 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005882 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005883 }
5884 }
5885 break;
5886
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005887 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005888 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005889 break;
5890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005891 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005892 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005893 {
5894 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005895 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005897 if (iptr->isn_type == ISN_2BOOL)
5898 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005899 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005900 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005901 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005902 n = !n;
5903 }
5904 else
5905 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005906 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005907 SOURCING_LNUM = iptr->isn_lnum;
5908 n = tv_get_bool_chk(tv, &error);
5909 if (error)
5910 goto on_error;
5911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912 clear_tv(tv);
5913 tv->v_type = VAR_BOOL;
5914 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5915 }
5916 break;
5917
5918 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005919 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005920 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005921 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5922 iptr->isn_type == ISN_2STRING_ANY,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02005923 iptr->isn_arg.tostring.flags) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005924 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005925 break;
5926
Bram Moolenaar08597872020-12-10 19:43:40 +01005927 case ISN_RANGE:
5928 {
5929 exarg_T ea;
5930 char *errormsg;
5931
Bram Moolenaarece0b872021-01-08 20:40:45 +01005932 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005933 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005934 ea.addr_type = ADDR_LINES;
5935 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005936 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005937 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005938 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005939
Bram Moolenaar35578162021-08-02 19:10:38 +02005940 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005941 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005942 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005943 tv = STACK_TV_BOT(-1);
5944 tv->v_type = VAR_NUMBER;
5945 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005946 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005947 }
5948 break;
5949
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005950 case ISN_PUT:
Christian Brabandt762a79e2025-03-16 21:39:58 +01005951 {
5952 int regname = iptr->isn_arg.put.put_regname;
5953 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5954 char_u *expr = NULL;
5955 int dir = FORWARD;
5956
5957 if (lnum < -2)
5958 {
5959 // line number was put on the stack by ISN_RANGE
5960 tv = STACK_TV_BOT(-1);
5961 curwin->w_cursor.lnum = tv->vval.v_number;
5962 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5963 dir = BACKWARD;
5964 --ectx->ec_stack.ga_len;
5965 }
5966 else if (lnum == -2)
5967 // :put! above cursor
5968 dir = BACKWARD;
5969 else if (lnum >= 0)
5970 {
5971 curwin->w_cursor.lnum = lnum;
5972 if (lnum == 0)
5973 // check_cursor() below will move to line 1
5974 dir = BACKWARD;
5975 }
5976
5977 if (regname == '=')
5978 {
5979 tv = STACK_TV_BOT(-1);
5980 if (tv->v_type == VAR_STRING)
5981 expr = tv->vval.v_string;
5982 else
5983 {
5984 expr = typval2string(tv, TRUE); // allocates value
5985 clear_tv(tv);
5986 }
5987 --ectx->ec_stack.ga_len;
5988 }
5989 check_cursor();
5990 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5991 vim_free(expr);
5992 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005993 break;
5994
Bram Moolenaar02194d22020-10-24 23:08:38 +02005995 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005996 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5997 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5998 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5999 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006000 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
6001 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006002 break;
6003
Bram Moolenaar02194d22020-10-24 23:08:38 +02006004 case ISN_CMDMOD_REV:
6005 // filter regprog is owned by the instruction, don't free it
6006 cmdmod.cmod_filter_regmatch.regprog = NULL;
6007 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006008 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
6009 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006010 break;
6011
Bram Moolenaar792f7862020-11-23 08:31:18 +01006012 case ISN_UNPACK:
6013 {
6014 int count = iptr->isn_arg.unpack.unp_count;
6015 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
6016 list_T *l;
6017 listitem_T *li;
6018 int i;
6019
6020 // Check there is a valid list to unpack.
6021 tv = STACK_TV_BOT(-1);
6022 if (tv->v_type != VAR_LIST)
6023 {
6024 SOURCING_LNUM = iptr->isn_lnum;
6025 emsg(_(e_for_argument_must_be_sequence_of_lists));
6026 goto on_error;
6027 }
6028 l = tv->vval.v_list;
6029 if (l == NULL
6030 || l->lv_len < (semicolon ? count - 1 : count))
6031 {
6032 SOURCING_LNUM = iptr->isn_lnum;
6033 emsg(_(e_list_value_does_not_have_enough_items));
6034 goto on_error;
6035 }
6036 else if (!semicolon && l->lv_len > count)
6037 {
6038 SOURCING_LNUM = iptr->isn_lnum;
6039 emsg(_(e_list_value_has_more_items_than_targets));
6040 goto on_error;
6041 }
6042
6043 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02006044 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006045 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006046 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006047
6048 // Variable after semicolon gets a list with the remaining
6049 // items.
6050 if (semicolon)
6051 {
6052 list_T *rem_list =
6053 list_alloc_with_items(l->lv_len - count + 1);
6054
6055 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006056 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006057 tv = STACK_TV_BOT(-count);
6058 tv->vval.v_list = rem_list;
6059 ++rem_list->lv_refcount;
6060 tv->v_lock = 0;
6061 li = l->lv_first;
6062 for (i = 0; i < count - 1; ++i)
6063 li = li->li_next;
6064 for (i = 0; li != NULL; ++i)
6065 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00006066 typval_T tvcopy;
6067
6068 copy_tv(&li->li_tv, &tvcopy);
6069 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006070 li = li->li_next;
6071 }
6072 --count;
6073 }
6074
6075 // Produce the values in reverse order, first item last.
6076 li = l->lv_first;
6077 for (i = 0; i < count; ++i)
6078 {
6079 tv = STACK_TV_BOT(-i - 1);
6080 copy_tv(&li->li_tv, tv);
6081 li = li->li_next;
6082 }
6083
6084 list_unref(l);
6085 }
6086 break;
6087
Bram Moolenaarb2049902021-01-24 12:53:53 +01006088 case ISN_PROF_START:
6089 case ISN_PROF_END:
6090 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01006091#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01006092 funccall_T cookie;
6093 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01006094 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02006095 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006096
Bram Moolenaarca16c602022-09-06 18:57:08 +01006097 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006098 if (iptr->isn_type == ISN_PROF_START)
6099 {
6100 func_line_start(&cookie, iptr->isn_lnum);
6101 // if we get here the instruction is executed
6102 func_line_exec(&cookie);
6103 }
6104 else
6105 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01006106#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01006107 }
6108 break;
6109
Bram Moolenaare99d4222021-06-13 14:01:26 +02006110 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02006111 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006112 break;
6113
Bram Moolenaar389df252020-07-09 21:20:47 +02006114 case ISN_SHUFFLE:
6115 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01006116 typval_T tmp_tv;
6117 int item = iptr->isn_arg.shuffle.shfl_item;
6118 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02006119
6120 tmp_tv = *STACK_TV_BOT(-item);
6121 for ( ; up > 0 && item > 1; --up)
6122 {
6123 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
6124 --item;
6125 }
6126 *STACK_TV_BOT(-item) = tmp_tv;
6127 }
6128 break;
6129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006131 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006132 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02006133 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134 break;
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01006135
6136 case ISN_SCRIPTCTX_SET:
6137 // change the script context. Used to evaluate an object
6138 // member variable initialization expression in the context of
6139 // the script where the class is defined.
6140 current_sctx = iptr->isn_arg.setsctx;
6141 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006142 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02006143 continue;
6144
Bram Moolenaard032f342020-07-18 18:13:02 +02006145func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02006146 // Restore previous function. If the frame pointer is where we started
6147 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006148 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02006149 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02006150
Bram Moolenaar4c137212021-04-19 16:48:48 +02006151 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02006152 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006153 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006154 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02006155
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02006156on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01006157 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006158 // If "emsg_silent" is set then ignore the error, unless it was set
6159 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006160 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006161 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01006162 {
6163 // If a sequence of instructions causes an error while ":silent!"
6164 // was used, restore the stack length and jump ahead to restoring
6165 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006166 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01006167 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006168 while (ectx->ec_stack.ga_len
6169 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01006170 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006171 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01006172 clear_tv(STACK_TV_BOT(0));
6173 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006174 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
6175 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01006176 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01006177 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01006178 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01006179on_fatal_error:
6180 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01006181 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006182 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006183 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006184 }
6185
6186done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006187 ret = OK;
6188theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01006189 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006190
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006191 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006192 ectx->ec_trylevel_at_start = save_trylevel_at_start;
6193 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006194}
6195
6196/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006197 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006198 * "rettv".
6199 * Return OK or FAIL.
6200 */
6201 int
6202exe_typval_instr(typval_T *tv, typval_T *rettv)
6203{
6204 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
6205 isn_T *save_instr = ectx->ec_instr;
6206 int save_iidx = ectx->ec_iidx;
6207 int res;
6208
LemonBoyf3b48952022-05-05 13:53:03 +01006209 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
6210 // even when the compilation fails.
6211 rettv->v_type = VAR_UNKNOWN;
6212
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006213 ectx->ec_instr = tv->vval.v_instr->instr_instr;
6214 res = exec_instructions(ectx);
6215 if (res == OK)
6216 {
6217 *rettv = *STACK_TV_BOT(-1);
6218 --ectx->ec_stack.ga_len;
6219 }
6220
6221 ectx->ec_instr = save_instr;
6222 ectx->ec_iidx = save_iidx;
6223
6224 return res;
6225}
6226
6227/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006228 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
6229 * "substitute_instr".
6230 */
6231 char_u *
6232exe_substitute_instr(void)
6233{
6234 ectx_T *ectx = substitute_instr->subs_ectx;
6235 isn_T *save_instr = ectx->ec_instr;
6236 int save_iidx = ectx->ec_iidx;
6237 char_u *res;
6238
6239 ectx->ec_instr = substitute_instr->subs_instr;
6240 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02006241 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006242 typval_T *tv = STACK_TV_BOT(-1);
6243
Bram Moolenaar27523602021-06-05 21:36:19 +02006244 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006245 --ectx->ec_stack.ga_len;
6246 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02006247 }
6248 else
6249 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006250 substitute_instr->subs_status = FAIL;
6251 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02006252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253
Bram Moolenaar4c137212021-04-19 16:48:48 +02006254 ectx->ec_instr = save_instr;
6255 ectx->ec_iidx = save_iidx;
6256
6257 return res;
6258}
6259
6260/*
6261 * Call a "def" function from old Vim script.
6262 * Return OK or FAIL.
6263 */
6264 int
6265call_def_function(
6266 ufunc_T *ufunc,
6267 int argc_arg, // nr of arguments
6268 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006269 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02006270 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006271 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01006272 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006273 typval_T *rettv) // return value
6274{
6275 ectx_T ectx; // execution context
6276 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006277 int partial_argc = partial == NULL
6278 || (flags & DEF_USE_PT_ARGV) == 0
6279 ? 0 : partial->pt_argc;
6280 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006281 typval_T *tv;
6282 int idx;
6283 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006284 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006285 sctx_T save_current_sctx = current_sctx;
6286 int did_emsg_before = did_emsg_cumul + did_emsg;
6287 int save_suppress_errthrow = suppress_errthrow;
6288 msglist_T **saved_msg_list = NULL;
6289 msglist_T *private_msg_list = NULL;
6290 int save_emsg_silent_def = emsg_silent_def;
6291 int save_did_emsg_def = did_emsg_def;
6292 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006293 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006294
6295// Get pointer to item in the stack.
6296#undef STACK_TV
6297#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6298
6299// Get pointer to item at the bottom of the stack, -1 is the bottom.
6300#undef STACK_TV_BOT
6301#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6302
6303// Get pointer to a local variable on the stack. Negative for arguments.
6304#undef STACK_TV_VAR
6305#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6306
6307 if (ufunc->uf_def_status == UF_NOT_COMPILED
6308 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006309 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6310 && compile_def_function(ufunc, FALSE,
6311 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006312 {
6313 if (did_emsg_cumul + did_emsg == did_emsg_before)
6314 semsg(_(e_function_is_not_compiled_str),
6315 printable_func_name(ufunc));
6316 return FAIL;
6317 }
6318
6319 {
6320 // Check the function was really compiled.
6321 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6322 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006323 if (dfunc->df_ufunc == NULL)
6324 {
6325 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6326 return FAIL;
6327 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006328 if (INSTRUCTIONS(dfunc) == NULL)
6329 {
6330 iemsg("using call_def_function() on not compiled function");
6331 return FAIL;
6332 }
6333 }
6334
6335 // If depth of calling is getting too high, don't execute the function.
6336 orig_funcdepth = funcdepth_get();
6337 if (funcdepth_increment() == FAIL)
6338 return FAIL;
6339
6340 CLEAR_FIELD(ectx);
6341 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6342 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006343 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006344 {
6345 funcdepth_decrement();
6346 return FAIL;
6347 }
6348 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6349 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6350 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006351 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006352
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006353 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006354 if (idx > 0 && ufunc->uf_va_name == NULL)
6355 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006356 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006357 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006358 goto failed_early;
6359 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006360 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006361 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006362 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006363 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006364 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006365 goto failed_early;
6366 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006367
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006368 // Put values from the partial and arguments on the stack, but no more than
6369 // what the function expects. A lambda can be called with more arguments
6370 // than it uses.
6371 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006372 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6373 ++idx)
6374 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006375 int argv_idx = idx - partial_argc;
6376
6377 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006378 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006379 && tv->v_type == VAR_SPECIAL
6380 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006381 {
6382 // Use the default value.
6383 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6384 }
6385 else
6386 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006387 int done = FALSE;
6388 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6389 {
6390 type_T *expected = ufunc->uf_arg_types[idx];
6391 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6392 {
6393 // When a float is expected and a number was given, convert
6394 // the value.
6395 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6396 STACK_TV_BOT(0)->v_lock = 0;
6397 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6398 done = TRUE;
6399 }
6400 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006401 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006402 goto failed_early;
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01006403 }
Bram Moolenaar47bba532023-01-20 18:49:46 +00006404 if (!done)
6405 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006406 }
6407 ++ectx.ec_stack.ga_len;
6408 }
6409
6410 // Turn varargs into a list. Empty list if no args.
6411 if (ufunc->uf_va_name != NULL)
6412 {
6413 int vararg_count = argc - ufunc->uf_args.ga_len;
6414
6415 if (vararg_count < 0)
6416 vararg_count = 0;
6417 else
6418 argc -= vararg_count;
6419 if (exe_newlist(vararg_count, &ectx) == FAIL)
6420 goto failed_early;
6421
6422 // Check the type of the list items.
6423 tv = STACK_TV_BOT(-1);
6424 if (ufunc->uf_va_type != NULL
6425 && ufunc->uf_va_type != &t_list_any
6426 && ufunc->uf_va_type->tt_member != &t_any
6427 && tv->vval.v_list != NULL)
6428 {
6429 type_T *expected = ufunc->uf_va_type->tt_member;
6430 listitem_T *li = tv->vval.v_list->lv_first;
6431
6432 for (idx = 0; idx < vararg_count; ++idx)
6433 {
6434 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006435 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006436 goto failed_early;
6437 li = li->li_next;
6438 }
6439 }
6440
6441 if (defcount > 0)
6442 // Move varargs list to below missing default arguments.
6443 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6444 --ectx.ec_stack.ga_len;
6445 }
6446
6447 // Make space for omitted arguments, will store default value below.
6448 // Any varargs list goes after them.
6449 if (defcount > 0)
6450 for (idx = 0; idx < defcount; ++idx)
6451 {
6452 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6453 ++ectx.ec_stack.ga_len;
6454 }
6455 if (ufunc->uf_va_name != NULL)
6456 ++ectx.ec_stack.ga_len;
6457
6458 // Frame pointer points to just after arguments.
6459 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6460 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6461
6462 {
6463 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6464 + ufunc->uf_dfunc_idx;
6465 ufunc_T *base_ufunc = dfunc->df_ufunc;
6466
6467 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006468 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006469 if (partial != NULL || base_ufunc->uf_partial != NULL)
6470 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006471 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6472 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006473 goto failed_early;
6474 if (partial != NULL)
6475 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006476 outer_T *outer = get_pt_outer(partial);
6477
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006478 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006479 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006480 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006481 if (current_ectx != NULL)
6482 {
6483 if (current_ectx->ec_outer_ref != NULL
6484 && current_ectx->ec_outer_ref->or_outer != NULL)
6485 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006486 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006487 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006488 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006489 }
6490 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006491 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006492 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006493 ++partial->pt_refcount;
6494 ectx.ec_outer_ref->or_partial = partial;
6495 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006496 }
6497 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006498 {
6499 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6500 ++base_ufunc->uf_partial->pt_refcount;
6501 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6502 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006503 }
6504 }
6505
6506 // dummy frame entries
6507 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6508 {
6509 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6510 ++ectx.ec_stack.ga_len;
6511 }
6512
6513 {
6514 // Reserve space for local variables and any closure reference count.
6515 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6516 + ufunc->uf_dfunc_idx;
6517
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006518 // Initialize variables to zero. That avoids having to generate
6519 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006520 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006521 {
6522 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6523 STACK_TV_VAR(idx)->vval.v_number = 0;
6524 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006525 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006526
6527 if (object != NULL)
6528 {
6529 // the object is always the variable at index zero
6530 tv = STACK_TV_VAR(0);
6531 tv->v_type = VAR_OBJECT;
6532 tv->vval.v_object = object;
6533 }
6534
Bram Moolenaar4c137212021-04-19 16:48:48 +02006535 if (dfunc->df_has_closure)
6536 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006537 // Initialize the variable that counts how many closures were
6538 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006539 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6540 STACK_TV_VAR(idx)->vval.v_number = 0;
6541 ++ectx.ec_stack.ga_len;
6542 }
6543
6544 ectx.ec_instr = INSTRUCTIONS(dfunc);
6545 }
6546
Bram Moolenaar58779852022-09-06 18:31:14 +01006547 // Store the execution context in funccal, used by invoke_all_defer().
6548 if (funccal != NULL)
6549 funccal->fc_ectx = &ectx;
6550
Bram Moolenaar4c137212021-04-19 16:48:48 +02006551 // Following errors are in the function, not the caller.
6552 // Commands behave like vim9script.
6553 estack_push_ufunc(ufunc, 1);
6554 current_sctx = ufunc->uf_script_ctx;
6555 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6556
6557 // Use a specific location for storing error messages to be converted to an
6558 // exception.
6559 saved_msg_list = msg_list;
6560 msg_list = &private_msg_list;
6561
6562 // Do turn errors into exceptions.
6563 suppress_errthrow = FALSE;
6564
6565 // Do not delete the function while executing it.
6566 ++ufunc->uf_calls;
6567
6568 // When ":silent!" was used before calling then we still abort the
6569 // function. If ":silent!" is used in the function then we don't.
6570 emsg_silent_def = emsg_silent;
6571 did_emsg_def = 0;
6572
LemonBoyc5d27442023-08-19 13:02:35 +02006573 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006574
Bram Moolenaar5800c792022-09-22 17:34:01 +01006575 /*
6576 * Execute the instructions until done.
6577 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006578 ret = exec_instructions(&ectx);
6579 if (ret == OK)
6580 {
6581 // function finished, get result from the stack.
6582 if (ufunc->uf_ret_type == &t_void)
6583 {
6584 rettv->v_type = VAR_VOID;
6585 }
6586 else
6587 {
6588 tv = STACK_TV_BOT(-1);
6589 *rettv = *tv;
6590 tv->v_type = VAR_UNKNOWN;
6591 }
6592 }
6593
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006594 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006595 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006596
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006597 // Deal with any remaining closures, they may be in use somewhere.
6598 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006599 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006600 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006601 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006602 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006603
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006604 estack_pop();
6605 current_sctx = save_current_sctx;
6606
Bram Moolenaar2336c372021-12-06 15:06:54 +00006607 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6608 // Function was unreferenced while being used, free it now.
6609 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006610
Bram Moolenaar352134b2020-10-17 22:04:08 +02006611 if (*msg_list != NULL && saved_msg_list != NULL)
6612 {
6613 msglist_T **plist = saved_msg_list;
6614
6615 // Append entries from the current msg_list (uncaught exceptions) to
6616 // the saved msg_list.
6617 while (*plist != NULL)
6618 plist = &(*plist)->next;
6619
6620 *plist = *msg_list;
6621 }
6622 msg_list = saved_msg_list;
6623
Bram Moolenaar4c137212021-04-19 16:48:48 +02006624 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006625 {
6626 cmdmod.cmod_filter_regmatch.regprog = NULL;
6627 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006628 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006629 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006630 emsg_silent_def = save_emsg_silent_def;
6631 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006632
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006633failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006634 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006635 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006636 {
6637 tv = STACK_TV(idx);
6638 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6639 clear_tv(tv);
6640 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006641 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006644 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006645 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006646 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006647 if (ectx.ec_outer_ref->or_outer_allocated)
6648 vim_free(ectx.ec_outer_ref->or_outer);
6649 partial_unref(ectx.ec_outer_ref->or_partial);
6650 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006651 }
6652
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006653 // Not sure if this is necessary.
6654 suppress_errthrow = save_suppress_errthrow;
6655
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006656 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6657 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006658 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006659 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006660 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006661 return ret;
6662}
6663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006665 * Called when a def function has finished (possibly failed).
6666 * Invoke all the function returns to clean up and invoke deferred functions,
6667 * except the toplevel one.
6668 */
6669 void
6670unwind_def_callstack(ectx_T *ectx)
6671{
6672 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6673 func_return(ectx);
6674}
6675
6676/*
dundargocc57b5bc2022-11-02 13:30:51 +00006677 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006678 */
6679 void
6680may_invoke_defer_funcs(ectx_T *ectx)
6681{
6682 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6683
6684 if (dfunc->df_defer_var_idx > 0)
6685 invoke_defer_funcs(ectx);
6686}
6687
6688/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006689 * Return loopvarinfo in a printable form in allocated memory.
6690 */
6691 static char_u *
6692printable_loopvarinfo(loopvarinfo_T *lvi)
6693{
6694 garray_T ga;
6695 int depth;
6696
6697 ga_init2(&ga, 1, 100);
6698 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6699 {
6700 if (ga_grow(&ga, 50) == FAIL)
6701 break;
6702 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006703 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006704 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006705 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006706 lvi->lvi_loop[depth].var_idx,
6707 lvi->lvi_loop[depth].var_idx
6708 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006709 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006710 }
6711 return ga.ga_data;
6712}
6713
6714/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006715 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6716 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6717 * "pfx" is prefixed to every line.
6718 */
6719 static void
6720list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6721{
6722 int line_idx = 0;
6723 int prev_current = 0;
6724 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006725 int def_arg_idx = 0;
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01006726 sctx_T script_ctx = current_sctx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006727
6728 for (current = 0; current < instr_count; ++current)
6729 {
6730 isn_T *iptr = &instr[current];
6731 char *line;
6732
6733 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006734 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006735 while (line_idx < iptr->isn_lnum
6736 && line_idx < ufunc->uf_lines.ga_len)
6737 {
6738 if (current > prev_current)
6739 {
6740 msg_puts("\n\n");
6741 prev_current = current;
6742 }
6743 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6744 if (line != NULL)
6745 msg(line);
6746 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006747 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6748 {
6749 int first_def_arg = ufunc->uf_args.ga_len
6750 - ufunc->uf_def_args.ga_len;
6751
6752 if (def_arg_idx > 0)
6753 msg_puts("\n\n");
6754 msg_start();
6755 msg_puts(" ");
6756 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6757 first_def_arg + def_arg_idx]);
6758 msg_puts(" = ");
6759 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6760 msg_clr_eos();
6761 msg_end();
6762 }
6763 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006764
6765 switch (iptr->isn_type)
6766 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006767 case ISN_CONSTRUCT:
6768 smsg("%s%4d NEW %s size %d", pfx, current,
6769 iptr->isn_arg.construct.construct_class->class_name,
6770 (int)iptr->isn_arg.construct.construct_size);
6771 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006772 case ISN_EXEC:
6773 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6774 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006775 case ISN_EXEC_SPLIT:
6776 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6777 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006778 case ISN_EXECRANGE:
6779 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6780 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006781 case ISN_LEGACY_EVAL:
6782 smsg("%s%4d EVAL legacy %s", pfx, current,
6783 iptr->isn_arg.string);
6784 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006785 case ISN_REDIRSTART:
6786 smsg("%s%4d REDIR", pfx, current);
6787 break;
6788 case ISN_REDIREND:
6789 smsg("%s%4d REDIR END%s", pfx, current,
6790 iptr->isn_arg.number ? " append" : "");
6791 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006792 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006793#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006794 smsg("%s%4d CEXPR pre %s", pfx, current,
6795 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006796#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006797 break;
6798 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006799#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006800 {
6801 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6802
6803 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6804 cexpr_get_auname(cer->cer_cmdidx),
6805 cer->cer_forceit ? "!" : "",
6806 cer->cer_cmdline);
6807 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006808#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006809 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006810 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006811 smsg("%s%4d INSTR", pfx, current);
6812 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6813 msg(" -------------");
6814 break;
6815 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006816 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006817 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6818
6819 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006820 }
6821 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006822 case ISN_SUBSTITUTE:
6823 {
6824 subs_T *subs = &iptr->isn_arg.subs;
6825
6826 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6827 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6828 msg(" -------------");
6829 }
6830 break;
6831 case ISN_EXECCONCAT:
6832 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6833 (varnumber_T)iptr->isn_arg.number);
6834 break;
6835 case ISN_ECHO:
6836 {
6837 echo_T *echo = &iptr->isn_arg.echo;
6838
6839 smsg("%s%4d %s %d", pfx, current,
6840 echo->echo_with_white ? "ECHO" : "ECHON",
6841 echo->echo_count);
6842 }
6843 break;
6844 case ISN_EXECUTE:
6845 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006846 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006847 break;
6848 case ISN_ECHOMSG:
6849 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006850 (varnumber_T)(iptr->isn_arg.number));
6851 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006852 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006853 if (iptr->isn_arg.echowin.ewin_time > 0)
6854 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6855 iptr->isn_arg.echowin.ewin_count,
6856 iptr->isn_arg.echowin.ewin_time);
6857 else
6858 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6859 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006860 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006861 case ISN_ECHOCONSOLE:
6862 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6863 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006864 break;
6865 case ISN_ECHOERR:
6866 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006867 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006868 break;
6869 case ISN_LOAD:
6870 {
6871 if (iptr->isn_arg.number < 0)
6872 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6873 (varnumber_T)(iptr->isn_arg.number
6874 + STACK_FRAME_SIZE));
6875 else
6876 smsg("%s%4d LOAD $%lld", pfx, current,
6877 (varnumber_T)(iptr->isn_arg.number));
6878 }
6879 break;
6880 case ISN_LOADOUTER:
6881 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006882 isn_outer_T *outer = &iptr->isn_arg.outer;
6883
6884 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006885 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006886 outer->outer_depth,
6887 outer->outer_idx + STACK_FRAME_SIZE);
6888 else if (outer->outer_depth < 0)
6889 smsg("%s%4d LOADOUTER $%d in loop level %d",
6890 pfx, current,
6891 outer->outer_idx,
6892 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006893 else
6894 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006895 outer->outer_depth,
6896 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006897 }
6898 break;
6899 case ISN_LOADV:
6900 smsg("%s%4d LOADV v:%s", pfx, current,
6901 get_vim_var_name(iptr->isn_arg.number));
6902 break;
6903 case ISN_LOADSCRIPT:
6904 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006905 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6906 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6907 svar_T *sv;
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01006908 sctx_T save_sctx = current_sctx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006909
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01006910 current_sctx = script_ctx;
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006911 sv = get_script_svar(sref, -1);
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01006912 current_sctx = save_sctx;
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006913 if (sv == NULL)
6914 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6915 pfx, current, si->sn_name);
6916 else
6917 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006918 sv->sv_name,
6919 sref->sref_idx,
6920 si->sn_name);
6921 }
6922 break;
6923 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006924 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006925 {
6926 scriptitem_T *si = SCRIPT_ITEM(
6927 iptr->isn_arg.loadstore.ls_sid);
6928
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006929 smsg("%s%4d %s s:%s from %s", pfx, current,
6930 iptr->isn_type == ISN_LOADS ? "LOADS"
6931 : "LOADEXPORT",
6932 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006933 }
6934 break;
6935 case ISN_LOADAUTO:
6936 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6937 break;
6938 case ISN_LOADG:
6939 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6940 break;
6941 case ISN_LOADB:
6942 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6943 break;
6944 case ISN_LOADW:
6945 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6946 break;
6947 case ISN_LOADT:
6948 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6949 break;
6950 case ISN_LOADGDICT:
6951 smsg("%s%4d LOAD g:", pfx, current);
6952 break;
6953 case ISN_LOADBDICT:
6954 smsg("%s%4d LOAD b:", pfx, current);
6955 break;
6956 case ISN_LOADWDICT:
6957 smsg("%s%4d LOAD w:", pfx, current);
6958 break;
6959 case ISN_LOADTDICT:
6960 smsg("%s%4d LOAD t:", pfx, current);
6961 break;
6962 case ISN_LOADOPT:
6963 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6964 break;
6965 case ISN_LOADENV:
6966 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6967 break;
6968 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006969 smsg("%s%4d LOADREG @%c", pfx, current,
6970 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006971 break;
6972
6973 case ISN_STORE:
6974 if (iptr->isn_arg.number < 0)
6975 smsg("%s%4d STORE arg[%lld]", pfx, current,
6976 iptr->isn_arg.number + STACK_FRAME_SIZE);
6977 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006978 smsg("%s%4d STORE $%lld", pfx, current,
6979 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006980 break;
6981 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006982 {
6983 isn_outer_T *outer = &iptr->isn_arg.outer;
6984
6985 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6986 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6987 pfx, current, outer->outer_idx);
6988 else
6989 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6990 outer->outer_depth, outer->outer_idx);
6991 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006992 break;
6993 case ISN_STOREV:
6994 smsg("%s%4d STOREV v:%s", pfx, current,
6995 get_vim_var_name(iptr->isn_arg.number));
6996 break;
6997 case ISN_STOREAUTO:
6998 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6999 break;
7000 case ISN_STOREG:
7001 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
7002 break;
7003 case ISN_STOREB:
7004 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
7005 break;
7006 case ISN_STOREW:
7007 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
7008 break;
7009 case ISN_STORET:
7010 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
7011 break;
7012 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01007013 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007014 {
7015 scriptitem_T *si = SCRIPT_ITEM(
7016 iptr->isn_arg.loadstore.ls_sid);
7017
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01007018 smsg("%s%4d %s %s in %s", pfx, current,
7019 iptr->isn_type == ISN_STORES
7020 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007021 iptr->isn_arg.loadstore.ls_name, si->sn_name);
7022 }
7023 break;
7024 case ISN_STORESCRIPT:
7025 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007026 scriptref_T *sref = iptr->isn_arg.script.scriptref;
7027 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
7028 svar_T *sv;
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01007029 sctx_T save_sctx = current_sctx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007030
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01007031 current_sctx = script_ctx;
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007032 sv = get_script_svar(sref, -1);
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01007033 current_sctx = save_sctx;
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007034 if (sv == NULL)
7035 smsg("%s%4d STORESCRIPT [deleted] in %s",
7036 pfx, current, si->sn_name);
7037 else
7038 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007039 sv->sv_name,
7040 sref->sref_idx,
7041 si->sn_name);
7042 }
7043 break;
7044 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00007045 case ISN_STOREFUNCOPT:
7046 smsg("%s%4d %s &%s", pfx, current,
7047 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007048 iptr->isn_arg.storeopt.so_name);
7049 break;
7050 case ISN_STOREENV:
7051 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
7052 break;
7053 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007054 smsg("%s%4d STOREREG @%c", pfx, current,
7055 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007056 break;
7057 case ISN_STORENR:
7058 smsg("%s%4d STORE %lld in $%d", pfx, current,
7059 iptr->isn_arg.storenr.stnr_val,
7060 iptr->isn_arg.storenr.stnr_idx);
7061 break;
7062
7063 case ISN_STOREINDEX:
7064 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00007065 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007066 break;
7067
7068 case ISN_STORERANGE:
7069 smsg("%s%4d STORERANGE", pfx, current);
7070 break;
7071
Bram Moolenaard505d172022-12-18 21:42:55 +00007072 case ISN_LOAD_CLASSMEMBER:
7073 case ISN_STORE_CLASSMEMBER:
7074 {
7075 class_T *cl = iptr->isn_arg.classmember.cm_class;
7076 int idx = iptr->isn_arg.classmember.cm_idx;
7077 ocmember_T *ocm = &cl->class_class_members[idx];
7078 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
7079 iptr->isn_type == ISN_LOAD_CLASSMEMBER
7080 ? "LOAD" : "STORE",
7081 cl->class_name, ocm->ocm_name);
7082 }
7083 break;
7084
Bram Moolenaar4c137212021-04-19 16:48:48 +02007085 // constants
7086 case ISN_PUSHNR:
7087 smsg("%s%4d PUSHNR %lld", pfx, current,
7088 (varnumber_T)(iptr->isn_arg.number));
7089 break;
7090 case ISN_PUSHBOOL:
7091 case ISN_PUSHSPEC:
7092 smsg("%s%4d PUSH %s", pfx, current,
7093 get_var_special_name(iptr->isn_arg.number));
7094 break;
7095 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007096 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007097 break;
7098 case ISN_PUSHS:
7099 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
7100 break;
7101 case ISN_PUSHBLOB:
7102 {
7103 char_u *r;
7104 char_u numbuf[NUMBUFLEN];
7105 char_u *tofree;
7106
7107 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
7108 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
7109 vim_free(tofree);
7110 }
7111 break;
7112 case ISN_PUSHFUNC:
7113 {
7114 char *name = (char *)iptr->isn_arg.string;
7115
7116 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
7117 name == NULL ? "[none]" : name);
7118 }
7119 break;
7120 case ISN_PUSHCHANNEL:
7121#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00007122 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007123#endif
7124 break;
7125 case ISN_PUSHJOB:
7126#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00007127 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007128#endif
7129 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00007130 case ISN_PUSHOBJ:
7131 smsg("%s%4d PUSHOBJ null", pfx, current);
7132 break;
7133 case ISN_PUSHCLASS:
7134 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00007135 iptr->isn_arg.classarg == NULL ? "null"
7136 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00007137 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007138 case ISN_PUSHEXC:
7139 smsg("%s%4d PUSH v:exception", pfx, current);
7140 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00007141 case ISN_AUTOLOAD:
7142 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
7143 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007144 case ISN_UNLET:
7145 smsg("%s%4d UNLET%s %s", pfx, current,
7146 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
7147 iptr->isn_arg.unlet.ul_name);
7148 break;
7149 case ISN_UNLETENV:
7150 smsg("%s%4d UNLETENV%s $%s", pfx, current,
7151 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
7152 iptr->isn_arg.unlet.ul_name);
7153 break;
7154 case ISN_UNLETINDEX:
7155 smsg("%s%4d UNLETINDEX", pfx, current);
7156 break;
7157 case ISN_UNLETRANGE:
7158 smsg("%s%4d UNLETRANGE", pfx, current);
7159 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007160 case ISN_LOCKUNLOCK:
7161 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
7162 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007163 case ISN_LOCKCONST:
7164 smsg("%s%4d LOCKCONST", pfx, current);
7165 break;
7166 case ISN_NEWLIST:
7167 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01007168 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007169 break;
7170 case ISN_NEWDICT:
7171 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01007172 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007173 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00007174 case ISN_NEWPARTIAL:
7175 smsg("%s%4d NEWPARTIAL", pfx, current);
7176 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007177
7178 // function call
7179 case ISN_BCALL:
7180 {
7181 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
7182
7183 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
7184 internal_func_name(cbfunc->cbf_idx),
7185 cbfunc->cbf_argcount);
7186 }
7187 break;
7188 case ISN_DCALL:
7189 {
7190 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
7191 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
7192 + cdfunc->cdf_idx;
7193
7194 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007195 printable_func_name(df->df_ufunc),
7196 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007197 }
7198 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00007199 case ISN_METHODCALL:
7200 {
7201 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
7202
7203 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
7204 mfunc->cmf_itf->class_name,
7205 mfunc->cmf_itf->class_obj_methods[
7206 mfunc->cmf_idx]->uf_name,
7207 mfunc->cmf_argcount);
7208 }
7209 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007210 case ISN_UCALL:
7211 {
7212 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
7213
7214 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
7215 cufunc->cuf_name, cufunc->cuf_argcount);
7216 }
7217 break;
7218 case ISN_PCALL:
7219 {
7220 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
7221
7222 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
7223 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
7224 }
7225 break;
7226 case ISN_PCALL_END:
7227 smsg("%s%4d PCALL end", pfx, current);
7228 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01007229 case ISN_DEFER:
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02007230 smsg("%s%4d DEFER %d args", pfx, current,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01007231 (int)iptr->isn_arg.defer.defer_argcount);
7232 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007233 case ISN_RETURN:
7234 smsg("%s%4d RETURN", pfx, current);
7235 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02007236 case ISN_RETURN_VOID:
7237 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007238 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007239 case ISN_RETURN_OBJECT:
7240 smsg("%s%4d RETURN object", pfx, current);
7241 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007242 case ISN_FUNCREF:
7243 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007244 funcref_T *funcref = &iptr->isn_arg.funcref;
7245 funcref_extra_T *extra = funcref->fr_extra;
7246 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007247
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007248 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00007249 {
7250 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
7251 + funcref->fr_dfunc_idx;
7252 name = df->df_ufunc->uf_name;
7253 }
7254 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007255 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00007256 if (extra != NULL && extra->fre_class != NULL)
7257 {
7258 smsg("%s%4d FUNCREF %s.%s", pfx, current,
7259 extra->fre_class->class_name, name);
7260 }
7261 else if (extra == NULL
7262 || extra->fre_loopvar_info.lvi_depth == 0)
7263 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007264 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00007265 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007266 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007267 {
7268 char_u *info = printable_loopvarinfo(
7269 &extra->fre_loopvar_info);
7270
7271 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
7272 name, info);
7273 vim_free(info);
7274 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007275 }
7276 break;
7277
7278 case ISN_NEWFUNC:
7279 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01007280 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007281
Bram Moolenaarcc341812022-09-19 15:54:34 +01007282 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007283 smsg("%s%4d NEWFUNC %s %s", pfx, current,
7284 arg->nfa_lambda, arg->nfa_global);
7285 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007286 {
7287 char_u *info = printable_loopvarinfo(
7288 &arg->nfa_loopvar_info);
7289
7290 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
7291 arg->nfa_lambda, arg->nfa_global, info);
7292 vim_free(info);
7293 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007294 }
7295 break;
7296
7297 case ISN_DEF:
7298 {
7299 char_u *name = iptr->isn_arg.string;
7300
7301 smsg("%s%4d DEF %s", pfx, current,
7302 name == NULL ? (char_u *)"" : name);
7303 }
7304 break;
7305
7306 case ISN_JUMP:
7307 {
7308 char *when = "?";
7309
7310 switch (iptr->isn_arg.jump.jump_when)
7311 {
7312 case JUMP_ALWAYS:
7313 when = "JUMP";
7314 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007315 case JUMP_NEVER:
7316 iemsg("JUMP_NEVER should not be used");
7317 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007318 case JUMP_AND_KEEP_IF_TRUE:
7319 when = "JUMP_AND_KEEP_IF_TRUE";
7320 break;
7321 case JUMP_IF_FALSE:
7322 when = "JUMP_IF_FALSE";
7323 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007324 case JUMP_WHILE_FALSE:
7325 when = "JUMP_WHILE_FALSE"; // unused
7326 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007327 case JUMP_IF_COND_FALSE:
7328 when = "JUMP_IF_COND_FALSE";
7329 break;
7330 case JUMP_IF_COND_TRUE:
7331 when = "JUMP_IF_COND_TRUE";
7332 break;
7333 }
7334 smsg("%s%4d %s -> %d", pfx, current, when,
7335 iptr->isn_arg.jump.jump_where);
7336 }
7337 break;
7338
7339 case ISN_JUMP_IF_ARG_SET:
7340 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7341 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7342 iptr->isn_arg.jump.jump_where);
7343 break;
7344
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007345 case ISN_JUMP_IF_ARG_NOT_SET:
7346 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7347 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7348 iptr->isn_arg.jump.jump_where);
7349 break;
7350
Bram Moolenaar4c137212021-04-19 16:48:48 +02007351 case ISN_FOR:
7352 {
7353 forloop_T *forloop = &iptr->isn_arg.forloop;
7354
7355 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007356 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007357 }
7358 break;
7359
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007360 case ISN_ENDLOOP:
7361 {
7362 endloop_T *endloop = &iptr->isn_arg.endloop;
7363
Bram Moolenaarcc341812022-09-19 15:54:34 +01007364 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7365 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007366 endloop->end_funcref_idx,
7367 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007368 endloop->end_var_idx + endloop->end_var_count - 1,
7369 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007370 }
7371 break;
7372
7373 case ISN_WHILE:
7374 {
7375 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7376
7377 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7378 whileloop->while_funcref_idx,
7379 whileloop->while_end);
7380 }
7381 break;
7382
Bram Moolenaar4c137212021-04-19 16:48:48 +02007383 case ISN_TRY:
7384 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007385 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007386
7387 if (try->try_ref->try_finally == 0)
7388 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7389 pfx, current,
7390 try->try_ref->try_catch,
7391 try->try_ref->try_endtry);
7392 else
7393 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7394 pfx, current,
7395 try->try_ref->try_catch,
7396 try->try_ref->try_finally,
7397 try->try_ref->try_endtry);
7398 }
7399 break;
7400 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007401 smsg("%s%4d CATCH", pfx, current);
7402 break;
7403 case ISN_TRYCONT:
7404 {
7405 trycont_T *trycont = &iptr->isn_arg.trycont;
7406
7407 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7408 trycont->tct_levels,
7409 trycont->tct_levels == 1 ? "" : "s",
7410 trycont->tct_where);
7411 }
7412 break;
7413 case ISN_FINALLY:
7414 smsg("%s%4d FINALLY", pfx, current);
7415 break;
7416 case ISN_ENDTRY:
7417 smsg("%s%4d ENDTRY", pfx, current);
7418 break;
7419 case ISN_THROW:
7420 smsg("%s%4d THROW", pfx, current);
7421 break;
7422
7423 // expression operations on number
7424 case ISN_OPNR:
7425 case ISN_OPFLOAT:
7426 case ISN_OPANY:
7427 {
7428 char *what;
7429 char *ins;
7430
7431 switch (iptr->isn_arg.op.op_type)
7432 {
7433 case EXPR_MULT: what = "*"; break;
7434 case EXPR_DIV: what = "/"; break;
7435 case EXPR_REM: what = "%"; break;
7436 case EXPR_SUB: what = "-"; break;
7437 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007438 case EXPR_LSHIFT: what = "<<"; break;
7439 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007440 default: what = "???"; break;
7441 }
7442 switch (iptr->isn_type)
7443 {
7444 case ISN_OPNR: ins = "OPNR"; break;
7445 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7446 case ISN_OPANY: ins = "OPANY"; break;
7447 default: ins = "???"; break;
7448 }
7449 smsg("%s%4d %s %s", pfx, current, ins, what);
7450 }
7451 break;
7452
7453 case ISN_COMPAREBOOL:
7454 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007455 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007456 case ISN_COMPARENR:
7457 case ISN_COMPAREFLOAT:
7458 case ISN_COMPARESTRING:
7459 case ISN_COMPAREBLOB:
7460 case ISN_COMPARELIST:
7461 case ISN_COMPAREDICT:
7462 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007463 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007464 case ISN_COMPAREANY:
7465 {
7466 char *p;
7467 char buf[10];
7468 char *type;
7469
7470 switch (iptr->isn_arg.op.op_type)
7471 {
7472 case EXPR_EQUAL: p = "=="; break;
7473 case EXPR_NEQUAL: p = "!="; break;
7474 case EXPR_GREATER: p = ">"; break;
7475 case EXPR_GEQUAL: p = ">="; break;
7476 case EXPR_SMALLER: p = "<"; break;
7477 case EXPR_SEQUAL: p = "<="; break;
7478 case EXPR_MATCH: p = "=~"; break;
7479 case EXPR_IS: p = "is"; break;
7480 case EXPR_ISNOT: p = "isnot"; break;
7481 case EXPR_NOMATCH: p = "!~"; break;
7482 default: p = "???"; break;
7483 }
7484 STRCPY(buf, p);
7485 if (iptr->isn_arg.op.op_ic == TRUE)
7486 strcat(buf, "?");
7487 switch(iptr->isn_type)
7488 {
7489 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7490 case ISN_COMPARESPECIAL:
7491 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007492 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007493 case ISN_COMPARENR: type = "COMPARENR"; break;
7494 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7495 case ISN_COMPARESTRING:
7496 type = "COMPARESTRING"; break;
7497 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7498 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7499 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7500 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007501 case ISN_COMPAREOBJECT:
7502 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007503 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7504 default: type = "???"; break;
7505 }
7506
7507 smsg("%s%4d %s %s", pfx, current, type, buf);
7508 }
7509 break;
7510
7511 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7512 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7513
7514 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007515 case ISN_CONCAT:
7516 smsg("%s%4d CONCAT size %lld", pfx, current,
7517 (varnumber_T)(iptr->isn_arg.number));
7518 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007519 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7520 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7521 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7522 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7523 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7524 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7525 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7526 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7527 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7528 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7529 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007530 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007531 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7532 iptr->isn_arg.getitem.gi_index,
7533 iptr->isn_arg.getitem.gi_with_op ?
7534 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007535 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7536 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7537 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007538 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7539 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007540 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007541 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007542 pfx, current,
7543 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007544 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007545 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007546 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007547 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007548 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7549 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7550
Bram Moolenaar4c137212021-04-19 16:48:48 +02007551 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7552
Bram Moolenaar4c137212021-04-19 16:48:48 +02007553 case ISN_CHECKTYPE:
7554 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007555 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007556 char *tofree = NULL;
7557 char *typename;
7558
7559 if (ct->ct_type->tt_type == VAR_FLOAT
7560 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7561 typename = "float|number";
7562 else
7563 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007564
7565 if (ct->ct_arg_idx == 0)
7566 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007567 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007568 (int)ct->ct_off);
7569 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007570 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007571 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007572 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007573 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007574 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007575 (int)ct->ct_arg_idx);
7576 vim_free(tofree);
7577 break;
7578 }
7579 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7580 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7581 iptr->isn_arg.checklen.cl_min_len);
7582 break;
7583 case ISN_SETTYPE:
7584 {
7585 char *tofree;
7586
7587 smsg("%s%4d SETTYPE %s", pfx, current,
7588 type_name(iptr->isn_arg.type.ct_type, &tofree));
7589 vim_free(tofree);
7590 break;
7591 }
7592 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007593 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7594 smsg("%s%4d INVERT %d (!val)", pfx, current,
7595 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007596 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007597 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7598 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007599 break;
7600 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007601 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007602 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007603 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7604 pfx, current,
7605 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007606 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007607 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7608 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007609 break;
7610 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007611 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007612 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007613 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007614 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7615 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007616 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007617 else
7618 smsg("%s%4d PUT %c %ld", pfx, current,
7619 iptr->isn_arg.put.put_regname,
7620 (long)iptr->isn_arg.put.put_lnum);
7621 break;
Christian Brabandt762a79e2025-03-16 21:39:58 +01007622
Bram Moolenaar4c137212021-04-19 16:48:48 +02007623 case ISN_CMDMOD:
7624 {
7625 char_u *buf;
7626 size_t len = produce_cmdmods(
7627 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7628
7629 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007630 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007631 {
7632 (void)produce_cmdmods(
7633 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7634 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7635 vim_free(buf);
7636 }
7637 break;
7638 }
7639 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7640
7641 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007642 smsg("%s%4d PROFILE START line %d", pfx, current,
7643 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007644 break;
7645
7646 case ISN_PROF_END:
7647 smsg("%s%4d PROFILE END", pfx, current);
7648 break;
7649
Bram Moolenaare99d4222021-06-13 14:01:26 +02007650 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007651 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7652 iptr->isn_arg.debug.dbg_break_lnum + 1,
7653 iptr->isn_lnum,
7654 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007655 break;
7656
Bram Moolenaar4c137212021-04-19 16:48:48 +02007657 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7658 iptr->isn_arg.unpack.unp_count,
7659 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7660 break;
7661 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7662 iptr->isn_arg.shuffle.shfl_item,
7663 iptr->isn_arg.shuffle.shfl_up);
7664 break;
7665 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7666
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01007667 case ISN_SCRIPTCTX_SET:
7668 {
7669 int sid = iptr->isn_arg.setsctx.sc_sid;
7670 scriptitem_T *si = SCRIPT_ITEM(sid);
7671 smsg("%s%4d SCRIPTCTX_SET %s", pfx, current, si->sn_name);
7672 script_ctx = iptr->isn_arg.setsctx;
7673 }
7674 break;
7675
Bram Moolenaar4c137212021-04-19 16:48:48 +02007676 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7677 return;
7678 }
7679
7680 out_flush(); // output one line at a time
7681 ui_breakcheck();
7682 if (got_int)
7683 break;
7684 }
7685}
7686
7687/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007688 * Handle command line completion for the :disassemble command.
7689 */
7690 void
7691set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7692{
7693 char_u *p;
7694
7695 // Default: expand user functions, "debug" and "profile"
7696 xp->xp_context = EXPAND_DISASSEMBLE;
7697 xp->xp_pattern = arg;
7698
7699 // first argument already typed: only user function names
7700 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7701 {
7702 xp->xp_context = EXPAND_USER_FUNC;
7703 xp->xp_pattern = skipwhite(p);
7704 }
7705}
7706
7707/*
7708 * Function given to ExpandGeneric() to obtain the list of :disassemble
7709 * arguments.
7710 */
7711 char_u *
7712get_disassemble_argument(expand_T *xp, int idx)
7713{
7714 if (idx == 0)
7715 return (char_u *)"debug";
7716 if (idx == 1)
7717 return (char_u *)"profile";
7718 return get_user_func_name(xp, idx - 2);
7719}
7720
7721/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007722 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007723 * We don't really need this at runtime, but we do have tests that require it,
7724 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 */
7726 void
7727ex_disassemble(exarg_T *eap)
7728{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007729 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007730 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007732 isn_T *instr = NULL; // init to shut up gcc warning
7733 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007734 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007735
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007736 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007737 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007738 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007739 if (func_needs_compiling(ufunc, compile_type)
7740 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007741 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007742 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007743 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007744 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745 return;
7746 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007747 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007748
7749 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007750 switch (compile_type)
7751 {
7752 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007753#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007754 instr = dfunc->df_instr_prof;
7755 instr_count = dfunc->df_instr_prof_count;
7756 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007757#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007758 // FALLTHROUGH
7759 case CT_NONE:
7760 instr = dfunc->df_instr;
7761 instr_count = dfunc->df_instr_count;
7762 break;
7763 case CT_DEBUG:
7764 instr = dfunc->df_instr_debug;
7765 instr_count = dfunc->df_instr_debug_count;
7766 break;
7767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007768
Bram Moolenaar4c137212021-04-19 16:48:48 +02007769 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007770}
7771
7772/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007773 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007774 * list, etc. Mostly like what JavaScript does, except that empty list and
7775 * empty dictionary are FALSE.
7776 */
7777 int
7778tv2bool(typval_T *tv)
7779{
7780 switch (tv->v_type)
7781 {
7782 case VAR_NUMBER:
7783 return tv->vval.v_number != 0;
7784 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007786 case VAR_PARTIAL:
7787 return tv->vval.v_partial != NULL;
7788 case VAR_FUNC:
7789 case VAR_STRING:
7790 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7791 case VAR_LIST:
7792 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7793 case VAR_DICT:
7794 return tv->vval.v_dict != NULL
7795 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7796 case VAR_BOOL:
7797 case VAR_SPECIAL:
7798 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7799 case VAR_JOB:
7800#ifdef FEAT_JOB_CHANNEL
7801 return tv->vval.v_job != NULL;
7802#else
7803 break;
7804#endif
7805 case VAR_CHANNEL:
7806#ifdef FEAT_JOB_CHANNEL
7807 return tv->vval.v_channel != NULL;
7808#else
7809 break;
7810#endif
7811 case VAR_BLOB:
7812 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7813 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007814 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007816 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007817 case VAR_CLASS:
7818 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007819 case VAR_TYPEALIAS:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820 break;
7821 }
7822 return FALSE;
7823}
7824
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007825 void
7826emsg_using_string_as(typval_T *tv, int as_number)
7827{
7828 semsg(_(as_number ? e_using_string_as_number_str
7829 : e_using_string_as_bool_str),
7830 tv->vval.v_string == NULL
7831 ? (char_u *)"" : tv->vval.v_string);
7832}
7833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007834/*
7835 * If "tv" is a string give an error and return FAIL.
7836 */
7837 int
7838check_not_string(typval_T *tv)
7839{
7840 if (tv->v_type == VAR_STRING)
7841 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007842 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007843 clear_tv(tv);
7844 return FAIL;
7845 }
7846 return OK;
7847}
7848
Christian Brabandt762a79e2025-03-16 21:39:58 +01007849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850#endif // FEAT_EVAL