blob: da03d5e7d4535e58fad3bd99b1df7799a845d07c [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/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002219 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002220 * Returns OK, FAIL or NOTDONE (uncatchable error).
2221 */
2222 static int
2223execute_storeindex(isn_T *iptr, ectx_T *ectx)
2224{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002225 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002226 typval_T *tv;
2227 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002228 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002229 typval_T *tv_dest = STACK_TV_BOT(-1);
2230 int status = OK;
2231
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002232 if (tv_idx->v_type == VAR_NUMBER)
2233 lidx = (long)tv_idx->vval.v_number;
2234
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002235 // Stack contains:
2236 // -3 value to be stored
2237 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002238 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002239 tv = STACK_TV_BOT(-3);
2240 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002241
2242 // Make sure an object has been initialized
2243 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2244 {
2245 emsg(_(e_using_null_object));
2246 status = FAIL;
2247 }
2248 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002249 {
2250 dest_type = tv_dest->v_type;
2251 if (dest_type == VAR_DICT)
2252 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002253 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2254 {
2255 // Need to get the member index now that the class is known.
2256 object_T *obj = tv_dest->vval.v_object;
Ernie Raelbe828252024-07-26 18:37:02 +02002257 if (obj == NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002258 {
Ernie Raelbe828252024-07-26 18:37:02 +02002259 emsg(_(e_using_null_object));
2260 status = FAIL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002261 }
2262 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002263 {
Ernie Raelbe828252024-07-26 18:37:02 +02002264 class_T *cl = obj->obj_class;
2265 char_u *member = tv_idx->vval.v_string;
2266
2267 int m_idx;
2268 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2269 if (m != NULL)
2270 {
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01002271 // Get the current function
2272 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2273 + ectx->ec_dfunc_idx)->df_ufunc;
2274
2275 // Check whether the member variable is writeable
2276 if ((m->ocm_access != VIM_ACCESS_ALL) &&
2277 (ufunc->uf_class == NULL ||
2278 !class_instance_of(ufunc->uf_class, cl)))
Ernie Raelbe828252024-07-26 18:37:02 +02002279 {
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01002280 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
2281 ? e_cannot_access_protected_variable_str
2282 : e_variable_is_not_writable_str;
2283 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
Ernie Raelbe828252024-07-26 18:37:02 +02002284 status = FAIL;
2285 }
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01002286 else
2287 lidx = m_idx;
Ernie Raelbe828252024-07-26 18:37:02 +02002288 }
2289 else
2290 {
2291 member_not_found_msg(cl, VAR_OBJECT, member, 0);
2292 status = FAIL;
2293 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002294 }
2295 }
2296 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2297 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002298 {
2299 emsg(_(e_number_expected));
2300 status = FAIL;
2301 }
2302 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002303
2304 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002305 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002306 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002307 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002308 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002309
Bram Moolenaare08be092022-02-17 13:08:26 +00002310 if (list == NULL)
2311 {
2312 emsg(_(e_list_not_set));
2313 return FAIL;
2314 }
2315 if (lidx < 0 && list->lv_len + lidx >= 0)
2316 // negative index is relative to the end
2317 lidx = list->lv_len + lidx;
2318 if (lidx < 0 || lidx > list->lv_len)
2319 {
2320 semsg(_(e_list_index_out_of_range_nr), lidx);
2321 return FAIL;
2322 }
2323 if (lidx < list->lv_len)
2324 {
2325 listitem_T *li = list_find(list, lidx);
2326
2327 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002328 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002329 return FAIL;
2330 // overwrite existing list item
2331 clear_tv(&li->li_tv);
2332 li->li_tv = *tv;
2333 }
2334 else
2335 {
2336 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2337 return FAIL;
2338 // append to list, only fails when out of memory
2339 if (list_append_tv(list, tv) == FAIL)
2340 return NOTDONE;
2341 clear_tv(tv);
2342 }
2343 }
2344 else if (dest_type == VAR_DICT)
2345 {
2346 char_u *key = tv_idx->vval.v_string;
2347 dict_T *dict = tv_dest->vval.v_dict;
2348 dictitem_T *di;
2349
2350 SOURCING_LNUM = iptr->isn_lnum;
2351 if (dict == NULL)
2352 {
2353 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002354 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002355 }
2356 if (key == NULL)
2357 key = (char_u *)"";
2358 di = dict_find(dict, key, -1);
2359 if (di != NULL)
2360 {
2361 if (error_if_locked(di->di_tv.v_lock,
2362 e_cannot_change_dict_item))
2363 return FAIL;
2364 // overwrite existing value
2365 clear_tv(&di->di_tv);
2366 di->di_tv = *tv;
2367 }
2368 else
2369 {
2370 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2371 return FAIL;
2372 // add to dict, only fails when out of memory
2373 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2374 return NOTDONE;
2375 clear_tv(tv);
2376 }
2377 }
2378 else if (dest_type == VAR_BLOB)
2379 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002380 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002381 varnumber_T nr;
2382 int error = FALSE;
2383 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002384
2385 if (blob == NULL)
2386 {
2387 emsg(_(e_blob_not_set));
2388 return FAIL;
2389 }
2390 len = blob_len(blob);
2391 if (lidx < 0 && len + lidx >= 0)
2392 // negative index is relative to the end
2393 lidx = len + lidx;
2394
2395 // Can add one byte at the end.
2396 if (lidx < 0 || lidx > len)
2397 {
2398 semsg(_(e_blob_index_out_of_range_nr), lidx);
2399 return FAIL;
2400 }
2401 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2402 return FAIL;
2403 nr = tv_get_number_chk(tv, &error);
2404 if (error)
2405 return FAIL;
2406 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002407 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002408 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2409 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002410 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002411
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002412 if (dest_type == VAR_OBJECT)
2413 {
2414 object_T *obj = tv_dest->vval.v_object;
2415
2416 otv = (typval_T *)(obj + 1);
2417 class_T *itf = iptr->isn_arg.storeindex.si_class;
2418 if (itf != NULL)
2419 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002420 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002421 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002422 }
2423 else
2424 {
2425 // VAR_CLASS
2426 class_T *class = tv_dest->vval.v_class;
2427 otv = class->class_members_tv;
2428 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002429
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002430 clear_tv(&otv[lidx]);
2431 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002432 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002433 else
2434 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002435 status = FAIL;
2436 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002437 }
2438 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002439
2440 clear_tv(tv_idx);
2441 clear_tv(tv_dest);
2442 ectx->ec_stack.ga_len -= 3;
2443 if (status == FAIL)
2444 {
2445 clear_tv(tv);
2446 return FAIL;
2447 }
2448 return OK;
2449}
2450
2451/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002452 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002453 */
2454 static int
2455execute_storerange(isn_T *iptr, ectx_T *ectx)
2456{
2457 typval_T *tv;
2458 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2459 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2460 typval_T *tv_dest = STACK_TV_BOT(-1);
2461 int status = OK;
2462
2463 // Stack contains:
2464 // -4 value to be stored
2465 // -3 first index or "none"
2466 // -2 second index or "none"
2467 // -1 destination list or blob
2468 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002469 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002470 if (tv_dest->v_type == VAR_LIST)
2471 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002472 long n1;
2473 long n2;
2474 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002475
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002476 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2477 if (tv_idx2->v_type == VAR_SPECIAL
2478 && tv_idx2->vval.v_number == VVAL_NONE)
2479 n2 = list_len(tv_dest->vval.v_list) - 1;
2480 else
2481 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2482
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002483 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002484 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002485 status = FAIL;
2486 else
2487 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002488 status = check_range_index_two(tv_dest->vval.v_list,
2489 &n1, li1, &n2, FALSE);
2490 if (status != FAIL)
2491 status = list_assign_range(
2492 tv_dest->vval.v_list,
2493 tv->vval.v_list,
2494 n1,
2495 n2,
2496 tv_idx2->v_type == VAR_SPECIAL,
2497 (char_u *)"=",
2498 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002499 }
2500 }
2501 else if (tv_dest->v_type == VAR_BLOB)
2502 {
2503 varnumber_T n1;
2504 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002505 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002506
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002507 n1 = tv_get_number_chk(tv_idx1, NULL);
2508 if (tv_idx2->v_type == VAR_SPECIAL
2509 && tv_idx2->vval.v_number == VVAL_NONE)
2510 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2511 else
2512 n2 = tv_get_number_chk(tv_idx2, NULL);
2513 bloblen = blob_len(tv_dest->vval.v_blob);
2514
2515 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2516 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002517 status = FAIL;
2518 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002519 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002520 }
2521 else
2522 {
2523 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002524 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002525 }
2526
2527 clear_tv(tv_idx1);
2528 clear_tv(tv_idx2);
2529 clear_tv(tv_dest);
2530 ectx->ec_stack.ga_len -= 4;
2531 clear_tv(tv);
2532
2533 return status;
2534}
2535
2536/*
2537 * Unlet item in list or dict variable.
2538 */
2539 static int
2540execute_unletindex(isn_T *iptr, ectx_T *ectx)
2541{
2542 typval_T *tv_idx = STACK_TV_BOT(-2);
2543 typval_T *tv_dest = STACK_TV_BOT(-1);
2544 int status = OK;
2545
2546 // Stack contains:
2547 // -2 index
2548 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002549 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002550 if (tv_dest->v_type == VAR_DICT)
2551 {
2552 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002553 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002554 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002555 semsg(_(e_expected_str_but_got_str),
2556 vartype_name(VAR_STRING),
2557 vartype_name(tv_idx->v_type));
2558 status = FAIL;
2559 }
2560 else
2561 {
2562 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002563 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002564 dictitem_T *di = NULL;
2565
2566 if (d != NULL && value_check_lock(
2567 d->dv_lock, NULL, FALSE))
2568 status = FAIL;
2569 else
2570 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002571 if (tv_idx->v_type == VAR_STRING)
2572 {
2573 key = tv_idx->vval.v_string;
2574 if (key == NULL)
2575 key = (char_u *)"";
2576 }
2577 else
2578 {
2579 key = tv_get_string(tv_idx);
2580 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002581 if (d != NULL)
2582 di = dict_find(d, key, (int)STRLEN(key));
2583 if (di == NULL)
2584 {
2585 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002586 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002587 status = FAIL;
2588 }
2589 else if (var_check_fixed(di->di_flags,
2590 NULL, FALSE)
2591 || var_check_ro(di->di_flags,
2592 NULL, FALSE))
2593 status = FAIL;
2594 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002595 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002596 }
2597 }
2598 }
2599 else if (tv_dest->v_type == VAR_LIST)
2600 {
2601 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002602 if (check_for_number(tv_idx) == FAIL)
2603 {
2604 status = FAIL;
2605 }
2606 else
2607 {
2608 list_T *l = tv_dest->vval.v_list;
2609 long n = (long)tv_idx->vval.v_number;
2610
2611 if (l != NULL && value_check_lock(
2612 l->lv_lock, NULL, FALSE))
2613 status = FAIL;
2614 else
2615 {
2616 listitem_T *li = list_find(l, n);
2617
2618 if (li == NULL)
2619 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002620 semsg(_(e_list_index_out_of_range_nr), n);
2621 status = FAIL;
2622 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002623 else
2624 listitem_remove(l, li);
2625 }
2626 }
2627 }
2628 else
2629 {
2630 status = FAIL;
2631 semsg(_(e_cannot_index_str),
2632 vartype_name(tv_dest->v_type));
2633 }
2634
2635 clear_tv(tv_idx);
2636 clear_tv(tv_dest);
2637 ectx->ec_stack.ga_len -= 2;
2638
2639 return status;
2640}
2641
2642/*
2643 * Unlet a range of items in a list variable.
2644 */
2645 static int
2646execute_unletrange(isn_T *iptr, ectx_T *ectx)
2647{
2648 // Stack contains:
2649 // -3 index1
2650 // -2 index2
2651 // -1 dict or list
2652 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2653 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2654 typval_T *tv_dest = STACK_TV_BOT(-1);
2655 int status = OK;
2656
2657 if (tv_dest->v_type == VAR_LIST)
2658 {
2659 // indexes must be a number
2660 SOURCING_LNUM = iptr->isn_lnum;
2661 if (check_for_number(tv_idx1) == FAIL
2662 || (tv_idx2->v_type != VAR_SPECIAL
2663 && check_for_number(tv_idx2) == FAIL))
2664 {
2665 status = FAIL;
2666 }
2667 else
2668 {
2669 list_T *l = tv_dest->vval.v_list;
2670 long n1 = (long)tv_idx1->vval.v_number;
2671 long n2 = tv_idx2->v_type == VAR_SPECIAL
2672 ? 0 : (long)tv_idx2->vval.v_number;
2673 listitem_T *li;
2674
2675 li = list_find_index(l, &n1);
2676 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002677 {
2678 semsg(_(e_list_index_out_of_range_nr),
2679 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002680 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002681 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002682 else
2683 {
2684 if (n1 < 0)
2685 n1 = list_idx_of_item(l, li);
2686 if (n2 < 0)
2687 {
2688 listitem_T *li2 = list_find(l, n2);
2689
2690 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002691 {
2692 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002693 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002694 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002695 else
2696 n2 = list_idx_of_item(l, li2);
2697 }
2698 if (status != FAIL
2699 && tv_idx2->v_type != VAR_SPECIAL
2700 && n2 < n1)
2701 {
2702 semsg(_(e_list_index_out_of_range_nr), n2);
2703 status = FAIL;
2704 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002705 if (status != FAIL)
2706 list_unlet_range(l, li, n1,
2707 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002708 }
2709 }
2710 }
2711 else
2712 {
2713 status = FAIL;
2714 SOURCING_LNUM = iptr->isn_lnum;
2715 semsg(_(e_cannot_index_str),
2716 vartype_name(tv_dest->v_type));
2717 }
2718
2719 clear_tv(tv_idx1);
2720 clear_tv(tv_idx2);
2721 clear_tv(tv_dest);
2722 ectx->ec_stack.ga_len -= 3;
2723
2724 return status;
2725}
2726
2727/*
2728 * Top of a for loop.
2729 */
2730 static int
2731execute_for(isn_T *iptr, ectx_T *ectx)
2732{
2733 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002734 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002735 typval_T *ltv = STACK_TV_BOT(-1);
2736 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002737 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002738
2739 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2740 return FAIL;
2741 if (ltv->v_type == VAR_LIST)
2742 {
2743 list_T *list = ltv->vval.v_list;
2744
2745 // push the next item from the list
2746 ++idxtv->vval.v_number;
2747 if (list == NULL
2748 || idxtv->vval.v_number >= list->lv_len)
2749 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002750 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002751 }
2752 else if (list->lv_first == &range_list_item)
2753 {
2754 // non-materialized range() list
2755 tv = STACK_TV_BOT(0);
2756 tv->v_type = VAR_NUMBER;
2757 tv->v_lock = 0;
2758 tv->vval.v_number = list_find_nr(
2759 list, idxtv->vval.v_number, NULL);
2760 ++ectx->ec_stack.ga_len;
2761 }
2762 else
2763 {
2764 listitem_T *li = list_find(list,
2765 idxtv->vval.v_number);
2766
2767 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2768 ++ectx->ec_stack.ga_len;
2769 }
2770 }
2771 else if (ltv->v_type == VAR_STRING)
2772 {
2773 char_u *str = ltv->vval.v_string;
2774
2775 // The index is for the last byte of the previous
2776 // character.
2777 ++idxtv->vval.v_number;
2778 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2779 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002780 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002781 }
2782 else
2783 {
2784 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2785
2786 // Push the next character from the string.
2787 tv = STACK_TV_BOT(0);
2788 tv->v_type = VAR_STRING;
2789 tv->vval.v_string = vim_strnsave(
2790 str + idxtv->vval.v_number, clen);
2791 ++ectx->ec_stack.ga_len;
2792 idxtv->vval.v_number += clen - 1;
2793 }
2794 }
2795 else if (ltv->v_type == VAR_BLOB)
2796 {
2797 blob_T *blob = ltv->vval.v_blob;
2798
2799 // When we get here the first time make a copy of the
2800 // blob, so that the iteration still works when it is
2801 // changed.
2802 if (idxtv->vval.v_number == -1 && blob != NULL)
2803 {
2804 blob_copy(blob, ltv);
2805 blob_unref(blob);
2806 blob = ltv->vval.v_blob;
2807 }
2808
2809 // The index is for the previous byte.
2810 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002811 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002812 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002813 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002814 }
2815 else
2816 {
2817 // Push the next byte from the blob.
2818 tv = STACK_TV_BOT(0);
2819 tv->v_type = VAR_NUMBER;
2820 tv->vval.v_number = blob_get(blob,
2821 idxtv->vval.v_number);
2822 ++ectx->ec_stack.ga_len;
2823 }
2824 }
2825 else
2826 {
2827 semsg(_(e_for_loop_on_str_not_supported),
2828 vartype_name(ltv->v_type));
2829 return FAIL;
2830 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002831
2832 if (jump)
2833 {
2834 // past the end of the list/string/blob, jump to "endfor"
2835 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2836 may_restore_cmdmod(&ectx->ec_funclocal);
2837 }
2838 else
2839 {
2840 // Store the current number of funcrefs, this may be used in
2841 // ISN_LOOPEND. The variable index is always one more than the loop
2842 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002843 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002844 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2845 }
2846
2847 return OK;
2848}
2849
2850/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002851 * Code for handling variables declared inside a loop and used in a closure.
2852 * This is very similar to what is done with funcstack_T. The difference is
2853 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2854 * scope of the block inside a loop and each loop may have its own.
2855 */
2856
2857// Double linked list of loopvars_T in use.
2858static loopvars_T *first_loopvars = NULL;
2859
2860 static void
2861add_loopvars_to_list(loopvars_T *loopvars)
2862{
2863 // Link in list of loopvarss.
2864 if (first_loopvars != NULL)
2865 first_loopvars->lvs_prev = loopvars;
2866 loopvars->lvs_next = first_loopvars;
2867 loopvars->lvs_prev = NULL;
2868 first_loopvars = loopvars;
2869}
2870
2871 static void
2872remove_loopvars_from_list(loopvars_T *loopvars)
2873{
2874 if (loopvars->lvs_prev == NULL)
2875 first_loopvars = loopvars->lvs_next;
2876 else
2877 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2878 if (loopvars->lvs_next != NULL)
2879 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2880}
2881
2882/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002883 * End of a for or while loop: Handle any variables used by a closure.
2884 */
2885 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002886execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002887{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002888 endloop_T *endloop = &iptr->isn_arg.endloop;
2889 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2890 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002891 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002892 garray_T *gap = &ectx->ec_funcrefs;
2893 int closure_in_use = FALSE;
2894 loopvars_T *loopvars;
2895 typval_T *stack;
2896 int idx;
2897
Bram Moolenaarcc341812022-09-19 15:54:34 +01002898 // Check if any created closure is still being referenced and loopvars have
2899 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002900 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2901 {
2902 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2903
Bram Moolenaarcc341812022-09-19 15:54:34 +01002904 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002905 {
2906 int refcount = pt->pt_refcount;
2907 int i;
2908
2909 // A Reference in a variable inside the loop doesn't count, it gets
2910 // unreferenced at the end of the loop.
2911 for (i = 0; i < endloop->end_var_count; ++i)
2912 {
2913 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2914
2915 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2916 --refcount;
2917 }
2918 if (refcount > 1)
2919 {
2920 closure_in_use = TRUE;
2921 break;
2922 }
2923 }
2924 }
2925
2926 // If no function reference were created since the start of the loop block
2927 // or it is no longer referenced there is nothing to do.
2928 if (!closure_in_use)
2929 return OK;
2930
2931 // A closure is using variables declared inside the loop.
2932 // Move them to the called function.
2933 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2934 if (loopvars == NULL)
2935 return FAIL;
2936
2937 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2938 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2939 loopvars->lvs_ga.ga_data = stack;
2940 if (stack == NULL)
2941 {
2942 vim_free(loopvars);
2943 return FAIL;
2944 }
2945 add_loopvars_to_list(loopvars);
2946
2947 // Move the variable values.
2948 for (idx = 0; idx < endloop->end_var_count; ++idx)
2949 {
2950 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2951
2952 *(stack + idx) = *tv;
2953 tv->v_type = VAR_UNKNOWN;
2954 }
2955
2956 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2957 {
2958 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2959
Bram Moolenaarcc341812022-09-19 15:54:34 +01002960 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002961 {
2962 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002963 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002964
Bram Moolenaarcc341812022-09-19 15:54:34 +01002965 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2966 pt->pt_outer.out_loop[depth].var_idx -=
2967 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002968 }
2969 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002970
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002971 return OK;
2972}
2973
2974/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002975 * Called when a partial is freed or its reference count goes down to one. The
2976 * loopvars may be the only reference to the partials in the local variables.
2977 * Go over all of them, the funcref and can be freed if all partials
2978 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002979 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002980 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002981 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002982loopvars_check_refcount(loopvars_T *loopvars)
2983{
2984 int i;
2985 garray_T *gap = &loopvars->lvs_ga;
2986 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002987 typval_T *stack = gap->ga_data;
2988
Bram Moolenaarcc341812022-09-19 15:54:34 +01002989 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2990 return FALSE;
2991 for (i = 0; i < gap->ga_len; ++i)
2992 {
2993 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2994
2995 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2996 && tv->vval.v_partial->pt_refcount == 1)
2997 {
2998 int depth;
2999
3000 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
3001 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
3002 ++done;
3003 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003004 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003005 if (done != loopvars->lvs_min_refcount)
3006 return FALSE;
3007
3008 // All partials referencing the loopvars have a reference count of
3009 // one, thus the loopvars is no longer of use.
3010 stack = gap->ga_data;
3011 for (i = 0; i < gap->ga_len; ++i)
3012 clear_tv(stack + i);
3013 vim_free(stack);
3014 remove_loopvars_from_list(loopvars);
3015 vim_free(loopvars);
3016 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003017}
3018
3019/*
3020 * For garbage collecting: set references in all variables referenced by
3021 * all loopvars.
3022 */
3023 int
3024set_ref_in_loopvars(int copyID)
3025{
3026 loopvars_T *loopvars;
3027
3028 for (loopvars = first_loopvars; loopvars != NULL;
3029 loopvars = loopvars->lvs_next)
3030 {
3031 typval_T *stack = loopvars->lvs_ga.ga_data;
3032 int i;
3033
3034 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
3035 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
3036 return TRUE; // abort
3037 }
3038 return FALSE;
3039}
3040
3041/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00003042 * Load instruction for w:/b:/g:/t: variable.
3043 * "isn_type" is used instead of "iptr->isn_type".
3044 */
3045 static int
3046load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
3047{
3048 dictitem_T *di = NULL;
3049 hashtab_T *ht = NULL;
3050 char namespace;
3051
3052 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3053 return NOTDONE;
3054 switch (isn_type)
3055 {
3056 case ISN_LOADG:
3057 ht = get_globvar_ht();
3058 namespace = 'g';
3059 break;
3060 case ISN_LOADB:
3061 ht = &curbuf->b_vars->dv_hashtab;
3062 namespace = 'b';
3063 break;
3064 case ISN_LOADW:
3065 ht = &curwin->w_vars->dv_hashtab;
3066 namespace = 'w';
3067 break;
3068 case ISN_LOADT:
3069 ht = &curtab->tp_vars->dv_hashtab;
3070 namespace = 't';
3071 break;
3072 default: // Cannot reach here
3073 return NOTDONE;
3074 }
3075 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3076
Bram Moolenaar06b77222022-01-25 15:51:56 +00003077 if (di == NULL)
3078 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003079 if (isn_type == ISN_LOADG)
3080 {
3081 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3082
3083 // g:Something could be a function
3084 if (ufunc != NULL)
3085 {
3086 typval_T *tv = STACK_TV_BOT(0);
3087
3088 ++ectx->ec_stack.ga_len;
3089 tv->v_type = VAR_FUNC;
3090 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3091 if (tv->vval.v_string == NULL)
3092 return FAIL;
3093 STRCPY(tv->vval.v_string, "g:");
3094 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3095 return OK;
3096 }
3097 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003098 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003099 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003100 // no check if the item exists in the script but
3101 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003102 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003103 else
3104 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003105 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003106 return FAIL;
3107 }
3108 else
3109 {
3110 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3111 ++ectx->ec_stack.ga_len;
3112 }
3113 return OK;
3114}
3115
Bram Moolenaard0200c82023-01-28 15:19:40 +00003116
3117 static void
3118object_required_error(typval_T *tv)
3119{
3120 garray_T type_list;
3121 ga_init2(&type_list, sizeof(type_T *), 10);
3122 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3123 char *tofree = NULL;
3124 char *typename = type_name(type, &tofree);
3125 semsg(_(e_object_required_found_str), typename);
3126 vim_free(tofree);
3127 clear_type_list(&type_list);
3128}
3129
Bram Moolenaar06b77222022-01-25 15:51:56 +00003130/*
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01003131 * Accessing the member of an object stored in a variable of type "any".
3132 * Returns OK if the member variable is present.
3133 * Returns FAIL if the variable is not found.
3134 */
3135 static int
3136any_var_get_obj_member(class_T *current_class, isn_T *iptr, typval_T *tv)
3137{
3138 object_T *obj = tv->vval.v_object;
3139 typval_T mtv;
3140
3141 if (obj == NULL)
3142 {
3143 SOURCING_LNUM = iptr->isn_lnum;
3144 emsg(_(e_using_null_object));
3145 return FAIL;
3146 }
3147
3148 // get_member_tv() needs the object information in the typval argument.
3149 // So set the object information.
3150 copy_tv(tv, &mtv);
3151
3152 // 'name' can either be a object variable or a object method
3153 int namelen = STRLEN(iptr->isn_arg.string);
3154 int save_did_emsg = did_emsg;
3155
3156 if (get_member_tv(obj->obj_class, TRUE, iptr->isn_arg.string, namelen,
3157 current_class, &mtv) == OK)
3158 {
3159 copy_tv(&mtv, tv);
3160 clear_tv(&mtv);
3161 return OK;
3162 }
3163
3164 if (did_emsg != save_did_emsg)
3165 return FAIL;
3166
3167 // could be a member function
3168 ufunc_T *obj_method;
3169 int obj_method_idx;
3170
3171 obj_method = method_lookup(obj->obj_class, VAR_OBJECT,
3172 iptr->isn_arg.string, namelen,
3173 &obj_method_idx);
3174 if (obj_method == NULL)
3175 {
3176 SOURCING_LNUM = iptr->isn_lnum;
3177 semsg(_(e_variable_not_found_on_object_str_str), iptr->isn_arg.string,
3178 obj->obj_class->class_name);
3179 return FAIL;
3180 }
3181
3182 // Protected methods are not accessible outside the class
3183 if (*obj_method->uf_name == '_'
3184 && !class_instance_of(current_class, obj->obj_class))
3185 {
3186 semsg(_(e_cannot_access_protected_method_str), obj_method->uf_name);
3187 return FAIL;
3188 }
3189
3190 // Create a partial for the member function
3191 if (obj_method_to_partial_tv(obj, obj_method, tv) == FAIL)
3192 return FAIL;
3193
3194 return OK;
3195}
3196
3197/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003198 * Execute instructions in execution context "ectx".
3199 * Return OK or FAIL;
3200 */
3201 static int
3202exec_instructions(ectx_T *ectx)
3203{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003204 int ret = FAIL;
3205 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003206 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003207
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003208 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003209 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003210
Bram Moolenaarff652882021-05-16 15:24:49 +02003211 // Only catch exceptions in this instruction list.
3212 ectx->ec_trylevel_at_start = trylevel;
3213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 for (;;)
3215 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003216 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003218 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003219
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003220 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003221 {
3222 line_breakcheck();
3223 breakcheck_count = 0;
3224 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003225 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003226 {
3227 // Turn CTRL-C into an exception.
3228 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003229 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003230 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003231 did_throw = TRUE;
3232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003234 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003235 {
3236 // Turn an error message into an exception.
3237 did_emsg = FALSE;
3238 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003239 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003240 did_throw = TRUE;
3241 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003242
3243 // This exception was not caught (yet).
3244 garray_T *trystack = &ectx->ec_trystack;
3245 if (trystack->ga_len > 0)
3246 {
3247 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3248 + trystack->ga_len - 1;
3249 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3250 trycmd->tcd_caught = FALSE;
3251 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003252 }
3253
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003254 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003256 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003257 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003258 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259
3260 // An exception jumps to the first catch, finally, or returns from
3261 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003262 while (index > 0)
3263 {
3264 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003265 // 1. after :try and before :catch - jump to first :catch
3266 // 2. in :catch block - jump to :finally
3267 // 3. in :catch block and no finally - jump to :endtry
3268 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3269 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003270 break;
3271 // In the catch and finally block of this try we have to go up
3272 // one level.
3273 --index;
3274 trycmd = NULL;
3275 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003276 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003278 if (trycmd->tcd_in_catch)
3279 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003280 if (trycmd->tcd_finally_idx > 0)
3281 {
3282 // exception inside ":catch", jump to ":finally" once
3283 ectx->ec_iidx = trycmd->tcd_finally_idx;
3284 trycmd->tcd_finally_idx = 0;
3285 }
3286 else
3287 {
3288 // exception inside ":catch" or ":finally", jump to
3289 // ":endtry"
3290 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3291 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003292 }
3293 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003294 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003295 // jump to first ":catch"
3296 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003297 trycmd->tcd_in_catch = TRUE;
3298 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003299 did_throw = FALSE; // don't come back here until :endtry
3300 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 }
3302 else
3303 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003304 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003305 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003306 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003307 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003308 tv = STACK_TV_BOT(0);
3309 tv->v_type = VAR_NUMBER;
3310 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003311 ++ectx->ec_stack.ga_len;
3312 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003314 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003315 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003316 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003317 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 goto done;
3319 }
3320
Bram Moolenaar4c137212021-04-19 16:48:48 +02003321 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003322 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 }
3324 continue;
3325 }
3326
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003327 /*
3328 * Big switch on the instruction. Most compilers will be turning this
3329 * into an efficient lookup table, since the "case" values are an enum
3330 * with sequential numbers. It may look ugly, but it should be the
3331 * most efficient way.
3332 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003333 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 switch (iptr->isn_type)
3335 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003336 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003337 case ISN_CONSTRUCT:
3338 // "this" is always the local variable at index zero
3339 tv = STACK_TV_VAR(0);
3340 tv->v_type = VAR_OBJECT;
3341 tv->vval.v_object = alloc_clear(
3342 iptr->isn_arg.construct.construct_size);
3343 tv->vval.v_object->obj_class =
3344 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003345 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003346 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003347 object_created(tv->vval.v_object);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01003348
3349 // When creating an enum value object, initialize the name and
3350 // ordinal object variables.
3351 class_T *en = tv->vval.v_object->obj_class;
3352 if (IS_ENUM(en))
3353 enum_set_internal_obj_vars(en, tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003354 break;
3355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 // execute Ex command line
3357 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003358 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003359 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 break;
3361
Bram Moolenaar20677332021-06-06 17:02:53 +02003362 // execute Ex command line split at NL characters.
3363 case ISN_EXEC_SPLIT:
3364 {
3365 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003366 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003367
3368 SOURCING_LNUM = iptr->isn_lnum;
3369 CLEAR_FIELD(cookie);
3370 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3371 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003372 line = get_split_sourceline(0, &cookie, 0, 0);
3373 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003374 get_split_sourceline, &cookie,
3375 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3376 == FAIL
3377 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003378 {
3379 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003380 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003381 }
3382 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003383 }
3384 break;
3385
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003386 // execute Ex command line that is only a range
3387 case ISN_EXECRANGE:
3388 {
3389 exarg_T ea;
3390 char *error = NULL;
3391
3392 CLEAR_FIELD(ea);
3393 ea.cmdidx = CMD_SIZE;
3394 ea.addr_type = ADDR_LINES;
3395 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003396 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003397 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003398 if (ea.cmd == NULL)
3399 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003400 // error is always NULL when using ADDR_LINES
3401 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003402 if (error != NULL)
3403 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003404 emsg(error);
3405 goto on_error;
3406 }
3407 }
3408 break;
3409
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003410 // Evaluate an expression with legacy syntax, push it onto the
3411 // stack.
3412 case ISN_LEGACY_EVAL:
3413 {
3414 char_u *arg = iptr->isn_arg.string;
3415 int res;
3416 int save_flags = cmdmod.cmod_flags;
3417
Bram Moolenaar35578162021-08-02 19:10:38 +02003418 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003419 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003420 tv = STACK_TV_BOT(0);
3421 init_tv(tv);
3422 cmdmod.cmod_flags |= CMOD_LEGACY;
3423 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3424 cmdmod.cmod_flags = save_flags;
3425 if (res == FAIL)
3426 goto on_error;
3427 ++ectx->ec_stack.ga_len;
3428 }
3429 break;
3430
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003431 // push typeval VAR_INSTR with instructions to be executed
3432 case ISN_INSTR:
3433 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003434 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003435 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003436 tv = STACK_TV_BOT(0);
3437 tv->vval.v_instr = ALLOC_ONE(instr_T);
3438 if (tv->vval.v_instr == NULL)
3439 goto on_error;
3440 ++ectx->ec_stack.ga_len;
3441
3442 tv->v_type = VAR_INSTR;
3443 tv->vval.v_instr->instr_ectx = ectx;
3444 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3445 }
3446 break;
3447
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003448 case ISN_SOURCE:
3449 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003450 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003451
Bram Moolenaar89445512022-04-14 12:58:23 +01003452 SOURCING_LNUM = iptr->isn_lnum;
3453 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003454 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003455 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003456 }
3457 break;
3458
Bram Moolenaar4c137212021-04-19 16:48:48 +02003459 // execute :substitute with an expression
3460 case ISN_SUBSTITUTE:
3461 {
3462 subs_T *subs = &iptr->isn_arg.subs;
3463 source_cookie_T cookie;
3464 struct subs_expr_S *save_instr = substitute_instr;
3465 struct subs_expr_S subs_instr;
3466 int res;
3467
3468 subs_instr.subs_ectx = ectx;
3469 subs_instr.subs_instr = subs->subs_instr;
3470 subs_instr.subs_status = OK;
3471 substitute_instr = &subs_instr;
3472
3473 SOURCING_LNUM = iptr->isn_lnum;
3474 // This is very much like ISN_EXEC
3475 CLEAR_FIELD(cookie);
3476 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3477 res = do_cmdline(subs->subs_cmd,
3478 getsourceline, &cookie,
3479 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3480 substitute_instr = save_instr;
3481
3482 if (res == FAIL || did_emsg
3483 || subs_instr.subs_status == FAIL)
3484 goto on_error;
3485 }
3486 break;
3487
3488 case ISN_FINISH:
3489 goto done;
3490
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003491 case ISN_REDIRSTART:
3492 // create a dummy entry for var_redir_str()
3493 if (alloc_redir_lval() == FAIL)
3494 goto on_error;
3495
3496 // The output is stored in growarray "redir_ga" until
3497 // redirection ends.
3498 init_redir_ga();
3499 redir_vname = 1;
3500 break;
3501
3502 case ISN_REDIREND:
3503 {
3504 char_u *res = get_clear_redir_ga();
3505
3506 // End redirection, put redirected text on the stack.
3507 clear_redir_lval();
3508 redir_vname = 0;
3509
Bram Moolenaar35578162021-08-02 19:10:38 +02003510 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003511 {
3512 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003513 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003514 }
3515 tv = STACK_TV_BOT(0);
3516 tv->v_type = VAR_STRING;
3517 tv->vval.v_string = res;
3518 ++ectx->ec_stack.ga_len;
3519 }
3520 break;
3521
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003522 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003523#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003524 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003525 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3526 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003527 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003528#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003529 break;
3530
3531 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003532#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003533 {
3534 exarg_T ea;
3535 int res;
3536
3537 CLEAR_FIELD(ea);
3538 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3539 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3540 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3541 --ectx->ec_stack.ga_len;
3542 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003543 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003544 res = cexpr_core(&ea, tv);
3545 clear_tv(tv);
3546 if (res == FAIL)
3547 goto on_error;
3548 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003549#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003550 break;
3551
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003552 // execute Ex command from pieces on the stack
3553 case ISN_EXECCONCAT:
3554 {
3555 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003556 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003557 int pass;
3558 int i;
3559 char_u *cmd = NULL;
3560 char_u *str;
3561
3562 for (pass = 1; pass <= 2; ++pass)
3563 {
3564 for (i = 0; i < count; ++i)
3565 {
3566 tv = STACK_TV_BOT(i - count);
3567 str = tv->vval.v_string;
3568 if (str != NULL && *str != NUL)
3569 {
3570 if (pass == 2)
3571 STRCPY(cmd + len, str);
3572 len += STRLEN(str);
3573 }
3574 if (pass == 2)
3575 clear_tv(tv);
3576 }
3577 if (pass == 1)
3578 {
3579 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003580 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003581 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003582 len = 0;
3583 }
3584 }
3585
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003586 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003587 do_cmdline_cmd(cmd);
3588 vim_free(cmd);
3589 }
3590 break;
3591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 // execute :echo {string} ...
3593 case ISN_ECHO:
3594 {
3595 int count = iptr->isn_arg.echo.echo_count;
3596 int atstart = TRUE;
3597 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003598 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599
3600 for (idx = 0; idx < count; ++idx)
3601 {
3602 tv = STACK_TV_BOT(idx - count);
3603 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3604 &atstart, &needclr);
3605 clear_tv(tv);
3606 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003607 if (needclr)
3608 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003609 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 }
3611 break;
3612
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003613 // :execute {string} ...
3614 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003615 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003616 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003617 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003618 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003619 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003620 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003621 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003622 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003623 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003624 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003625 garray_T ga;
3626 char_u buf[NUMBUFLEN];
3627 char_u *p;
3628 int len;
3629 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003630 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003631
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003632 if (iptr->isn_type == ISN_ECHOWINDOW)
3633 count = iptr->isn_arg.echowin.ewin_count;
3634 else
3635 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003636 ga_init2(&ga, 1, 80);
3637 for (idx = 0; idx < count; ++idx)
3638 {
3639 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003640 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003641 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003642 if (tv->v_type == VAR_CHANNEL
3643 || tv->v_type == VAR_JOB)
3644 {
3645 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003646 semsg(_(e_using_invalid_value_as_string_str),
3647 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003648 break;
3649 }
3650 else
3651 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003652 }
3653 else
Ernie Raelbe828252024-07-26 18:37:02 +02003654 {
3655 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003656 p = tv_stringify(tv, buf);
Ernie Raelbe828252024-07-26 18:37:02 +02003657 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003658
3659 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003660 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003661 failed = TRUE;
3662 else
3663 {
3664 if (ga.ga_len > 0)
3665 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3666 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3667 ga.ga_len += len;
3668 }
3669 clear_tv(tv);
3670 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003671 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003672 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003673 {
3674 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003675 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003676 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003677
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003678 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003679 {
3680 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003681 {
3682 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003683 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003684 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003685 {
3686 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003687 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003688 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003689 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003690 else
3691 {
3692 msg_sb_eol();
3693 if (iptr->isn_type == ISN_ECHOMSG)
3694 {
3695 msg_attr(ga.ga_data, echo_attr);
3696 out_flush();
3697 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003698#ifdef HAS_MESSAGE_WINDOW
3699 else if (iptr->isn_type == ISN_ECHOWINDOW)
3700 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003701 start_echowindow(
3702 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003703 msg_attr(ga.ga_data, echo_attr);
3704 end_echowindow();
3705 }
3706#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003707 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3708 {
3709 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3710 TRUE);
3711 ui_write((char_u *)"\r\n", 2, TRUE);
3712 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003713 else
3714 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003715 SOURCING_LNUM = iptr->isn_lnum;
3716 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003717 }
3718 }
3719 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003720 ga_clear(&ga);
3721 }
3722 break;
3723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 // load local variable or argument
3725 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003726 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003727 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003728 tv = STACK_TV_VAR(iptr->isn_arg.number);
3729 if (tv->v_type == VAR_UNKNOWN)
3730 {
3731 // missing argument or default value v:none
3732 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3733 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3734 }
3735 else
3736 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003737 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 break;
3739
3740 // load v: variable
3741 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003742 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003743 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003745 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 break;
3747
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003748 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 case ISN_LOADSCRIPT:
3750 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003751 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 svar_T *sv;
3753
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003754 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003755 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003756 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003757 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003758 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003759 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003761 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 }
3763 break;
3764
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003765 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003767 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003769 int sid = iptr->isn_arg.loadstore.ls_sid;
3770 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003771 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 if (di == NULL)
3775 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003776 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003777 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003778 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 }
3780 else
3781 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003782 if (iptr->isn_type == ISN_LOADEXPORT)
3783 {
3784 int idx = get_script_item_idx(sid, name, 0,
3785 NULL, NULL);
3786 svar_T *sv;
3787
3788 if (idx >= 0)
3789 {
3790 sv = ((svar_T *)SCRIPT_ITEM(sid)
3791 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003792 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003793 {
3794 SOURCING_LNUM = iptr->isn_lnum;
3795 semsg(_(e_item_not_exported_in_script_str),
3796 name);
3797 goto on_error;
3798 }
3799 }
3800 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003801 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003802 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003804 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 }
3806 }
3807 break;
3808
Bram Moolenaard3aac292020-04-19 14:32:17 +02003809 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003811 case ISN_LOADB:
3812 case ISN_LOADW:
3813 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003815 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003816
Bram Moolenaar06b77222022-01-25 15:51:56 +00003817 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003818 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003819 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003820 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 break;
3824
Bram Moolenaar03290b82020-12-19 16:30:44 +01003825 // load autoload variable
3826 case ISN_LOADAUTO:
3827 {
3828 char_u *name = iptr->isn_arg.string;
3829
Bram Moolenaar35578162021-08-02 19:10:38 +02003830 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003831 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003832 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003833 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003834 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003835 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003836 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003837 }
3838 break;
3839
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003840 // load g:/b:/w:/t: namespace
3841 case ISN_LOADGDICT:
3842 case ISN_LOADBDICT:
3843 case ISN_LOADWDICT:
3844 case ISN_LOADTDICT:
3845 {
3846 dict_T *d = NULL;
3847
3848 switch (iptr->isn_type)
3849 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003850 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3851 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3852 case ISN_LOADWDICT: d = curwin->w_vars; break;
3853 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003854 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003855 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003856 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003857 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003858 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003859 tv = STACK_TV_BOT(0);
3860 tv->v_type = VAR_DICT;
3861 tv->v_lock = 0;
3862 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003863 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003864 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003865 }
3866 break;
3867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 // load &option
3869 case ISN_LOADOPT:
3870 {
3871 typval_T optval;
3872 char_u *name = iptr->isn_arg.string;
3873
Bram Moolenaara8c17702020-04-01 21:17:24 +02003874 // This is not expected to fail, name is checked during
3875 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003876 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003877 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003878 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003879 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003881 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 }
3883 break;
3884
3885 // load $ENV
3886 case ISN_LOADENV:
3887 {
3888 typval_T optval;
3889 char_u *name = iptr->isn_arg.string;
3890
Bram Moolenaar35578162021-08-02 19:10:38 +02003891 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003892 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003893 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003894 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003896 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 }
3898 break;
3899
3900 // load @register
3901 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003902 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003903 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 tv = STACK_TV_BOT(0);
3905 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003906 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003907 // This may result in NULL, which should be equivalent to an
3908 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 tv->vval.v_string = get_reg_contents(
3910 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003911 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 break;
3913
3914 // store local variable
3915 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003916 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 tv = STACK_TV_VAR(iptr->isn_arg.number);
Ernie Rael9ed53752023-12-11 17:40:46 +01003918 if (check_typval_is_value(STACK_TV_BOT(0)) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003919 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003920 clear_tv(STACK_TV_BOT(0));
3921 goto on_error;
3922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 clear_tv(tv);
3924 *tv = *STACK_TV_BOT(0);
3925 break;
3926
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003927 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003928 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003929 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003930 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003931 int sid = iptr->isn_arg.loadstore.ls_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003932 char_u *name = iptr->isn_arg.loadstore.ls_name;
Ernie Rael3f821d62024-04-24 20:07:50 +02003933 dictitem_T *di = NULL;
3934 // First check for a variable from an exported autoload
3935 // with an autoload_prefix; it would be in globals.
3936 if (iptr->isn_type == ISN_STOREEXPORT)
3937 di = find_var_autoload_prefix(name, sid, NULL, NULL);
3938 // Then look for a variable in the script's variables.
3939 if (di == NULL)
3940 {
3941 hashtab_T *ht = &SCRIPT_VARS(sid);
3942 di = find_var_in_ht(ht, 0, STRNCMP("s:", name, 2) == 0
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003943 ? name + 2 : name, TRUE);
Ernie Rael3f821d62024-04-24 20:07:50 +02003944 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003945
Bram Moolenaar4c137212021-04-19 16:48:48 +02003946 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003947 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003948 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003949 {
3950 if (iptr->isn_type == ISN_STOREEXPORT)
3951 {
3952 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003953 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003954 goto on_error;
3955 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003956 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003957 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003958 else
3959 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003960 if (iptr->isn_type == ISN_STOREEXPORT)
3961 {
3962 int idx = get_script_item_idx(sid, name, 0,
3963 NULL, NULL);
3964
Bram Moolenaar06651632022-04-27 17:54:25 +01003965 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003966 if (idx >= 0)
3967 {
3968 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3969 ->sn_var_vals.ga_data) + idx;
3970
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003971 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003972 {
3973 semsg(_(e_item_not_exported_in_script_str),
3974 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003975 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003976 goto on_error;
3977 }
3978 }
3979 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003980 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003981 {
3982 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003983 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003984 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003985 clear_tv(&di->di_tv);
3986 di->di_tv = *STACK_TV_BOT(0);
3987 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003988 }
3989 break;
3990
3991 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 case ISN_STORESCRIPT:
3993 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003994 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3995 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003997 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003998 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003999 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004000 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02004001
4002 // "const" and "final" are checked at compile time, locking
4003 // the value needs to be checked here.
4004 SOURCING_LNUM = iptr->isn_lnum;
4005 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02004006 {
4007 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02004008 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02004009 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02004010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 clear_tv(sv->sv_tv);
4012 *sv->sv_tv = *STACK_TV_BOT(0);
4013 }
4014 break;
4015
4016 // store option
4017 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004018 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004020 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
4021 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 long n = 0;
4023 char_u *s = NULL;
4024 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00004025 char_u numbuf[NUMBUFLEN];
4026 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027
Bram Moolenaar4c137212021-04-19 16:48:48 +02004028 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02004029 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 tv = STACK_TV_BOT(0);
4031 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01004032 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01004034 if (s == NULL)
4035 s = (char_u *)"";
4036 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004037 else if (iptr->isn_type == ISN_STOREFUNCOPT)
4038 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00004039 // If the option can be set to a function reference or
4040 // a lambda and the passed value is a function
4041 // reference, then convert it to the name (string) of
4042 // the function reference.
4043 s = tv2string(tv, &tofree, numbuf, 0);
4044 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004045 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004046 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004047 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004048 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004049 goto on_error;
4050 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004051 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004053 // must be VAR_NUMBER, CHECKTYPE makes sure
4054 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00004055 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02004056 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00004057 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 if (msg != NULL)
4059 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004060 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02004062 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 }
4065 break;
4066
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004067 // store $ENV
4068 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004069 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004070 tv = STACK_TV_BOT(0);
4071 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
4072 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004073 break;
4074
4075 // store @r
4076 case ISN_STOREREG:
4077 {
4078 int reg = iptr->isn_arg.number;
4079
Bram Moolenaar4c137212021-04-19 16:48:48 +02004080 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01004081 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02004082 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01004083 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004084 }
4085 break;
4086
4087 // store v: variable
4088 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004089 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004090 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
4091 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004092 // should not happen, type is checked when compiling
4093 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004094 break;
4095
Bram Moolenaard3aac292020-04-19 14:32:17 +02004096 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02004098 case ISN_STOREB:
4099 case ISN_STOREW:
4100 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004102 dictitem_T *di;
4103 hashtab_T *ht;
4104 char_u *name = iptr->isn_arg.string + 2;
4105
Bram Moolenaard3aac292020-04-19 14:32:17 +02004106 switch (iptr->isn_type)
4107 {
4108 case ISN_STOREG:
4109 ht = get_globvar_ht();
4110 break;
4111 case ISN_STOREB:
4112 ht = &curbuf->b_vars->dv_hashtab;
4113 break;
4114 case ISN_STOREW:
4115 ht = &curwin->w_vars->dv_hashtab;
4116 break;
4117 case ISN_STORET:
4118 ht = &curtab->tp_vars->dv_hashtab;
4119 break;
4120 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004121 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123
Bram Moolenaar4c137212021-04-19 16:48:48 +02004124 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004125 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004127 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 else
4129 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004130 SOURCING_LNUM = iptr->isn_lnum;
4131 if (var_check_permission(di, name) == FAIL)
4132 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 clear_tv(&di->di_tv);
4134 di->di_tv = *STACK_TV_BOT(0);
4135 }
4136 }
4137 break;
4138
Bram Moolenaar03290b82020-12-19 16:30:44 +01004139 // store an autoload variable
4140 case ISN_STOREAUTO:
4141 SOURCING_LNUM = iptr->isn_lnum;
4142 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
4143 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004144 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004145 break;
4146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 // store number in local variable
4148 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004149 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 clear_tv(tv);
4151 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004152 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 break;
4154
Bram Moolenaar590162c2022-12-24 21:24:06 +00004155 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004156 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004157 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004158 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004159
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004160 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004161 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004162 if (res == NOTDONE)
4163 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004164 }
4165 break;
4166
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004167 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004168 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004169 if (execute_storerange(iptr, ectx) == FAIL)
4170 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004171 break;
4172
Bram Moolenaard505d172022-12-18 21:42:55 +00004173 case ISN_LOAD_CLASSMEMBER:
4174 {
4175 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4176 goto theend;
4177 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004178 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4179 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004180 ++ectx->ec_stack.ga_len;
4181 }
4182 break;
4183
4184 case ISN_STORE_CLASSMEMBER:
4185 {
4186 classmember_T *cm = &iptr->isn_arg.classmember;
4187 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4188 clear_tv(tv);
4189 *tv = *STACK_TV_BOT(-1);
4190 --ectx->ec_stack.ga_len;
4191 }
4192 break;
4193
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004194 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004195 case ISN_LOADOUTER:
4196 case ISN_STOREOUTER:
4197 {
4198 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004199 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4200 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004201
4202 while (depth > 1 && outer != NULL)
4203 {
4204 outer = outer->out_up;
4205 --depth;
4206 }
4207 if (outer == NULL)
4208 {
4209 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004210 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4211 || ectx->ec_outer_ref == NULL)
4212 // Possibly :def function called from legacy
4213 // context.
4214 emsg(_(e_closure_called_from_invalid_context));
4215 else
4216 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004217 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004218 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004219 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004220 // Variable declared in loop. May be copied if the
4221 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004222 tv = ((typval_T *)outer->out_loop[-depth - 1]
4223 .stack->ga_data)
4224 + outer->out_loop[-depth - 1].var_idx
4225 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004226 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004227 // Variable declared in a function. May be copied if
4228 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004229 tv = ((typval_T *)outer->out_stack->ga_data)
4230 + outer->out_frame_idx + STACK_FRAME_SIZE
4231 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004232 if (iptr->isn_type == ISN_LOADOUTER)
4233 {
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004234 typval_T *copy;
Bram Moolenaar35578162021-08-02 19:10:38 +02004235 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004236 goto theend;
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004237 // careful: ga_grow_inner may re-alloc the stack
4238 if (depth < 0)
4239 copy = ((typval_T *)outer->out_loop[-depth - 1]
4240 .stack->ga_data)
4241 + outer->out_loop[-depth - 1].var_idx
4242 + iptr->isn_arg.outer.outer_idx;
4243 else
4244 copy = ((typval_T *)outer->out_stack->ga_data)
4245 + outer->out_frame_idx + STACK_FRAME_SIZE
4246 + iptr->isn_arg.outer.outer_idx;
4247 // memory was freed, get tv again
4248 if (copy != tv)
4249 tv = copy;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004250 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004251 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004252 }
4253 else
4254 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004255 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004256 clear_tv(tv);
4257 *tv = *STACK_TV_BOT(0);
4258 }
4259 }
4260 break;
4261
Bram Moolenaar752fc692021-01-04 21:57:11 +01004262 // unlet item in list or dict variable
4263 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004264 if (execute_unletindex(iptr, ectx) == FAIL)
4265 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004266 break;
4267
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004268 // unlet range of items in list variable
4269 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004270 if (execute_unletrange(iptr, ectx) == FAIL)
4271 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004272 break;
4273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 // push constant
4275 case ISN_PUSHNR:
4276 case ISN_PUSHBOOL:
4277 case ISN_PUSHSPEC:
4278 case ISN_PUSHF:
4279 case ISN_PUSHS:
4280 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004281 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004282 case ISN_PUSHCHANNEL:
4283 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004284 case ISN_PUSHOBJ:
4285 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004286 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004287 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004289 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004290 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 switch (iptr->isn_type)
4292 {
4293 case ISN_PUSHNR:
4294 tv->v_type = VAR_NUMBER;
4295 tv->vval.v_number = iptr->isn_arg.number;
4296 break;
4297 case ISN_PUSHBOOL:
4298 tv->v_type = VAR_BOOL;
4299 tv->vval.v_number = iptr->isn_arg.number;
4300 break;
4301 case ISN_PUSHSPEC:
4302 tv->v_type = VAR_SPECIAL;
4303 tv->vval.v_number = iptr->isn_arg.number;
4304 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 case ISN_PUSHF:
4306 tv->v_type = VAR_FLOAT;
4307 tv->vval.v_float = iptr->isn_arg.fnumber;
4308 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 case ISN_PUSHBLOB:
4310 blob_copy(iptr->isn_arg.blob, tv);
4311 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004312 case ISN_PUSHFUNC:
4313 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004314 if (iptr->isn_arg.string == NULL)
4315 tv->vval.v_string = NULL;
4316 else
4317 tv->vval.v_string =
4318 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004319 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004320 case ISN_PUSHCHANNEL:
4321#ifdef FEAT_JOB_CHANNEL
4322 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004323 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004324#endif
4325 break;
4326 case ISN_PUSHJOB:
4327#ifdef FEAT_JOB_CHANNEL
4328 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004329 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004330#endif
4331 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004332 case ISN_PUSHOBJ:
4333 tv->v_type = VAR_OBJECT;
4334 tv->vval.v_object = NULL;
4335 break;
4336 case ISN_PUSHCLASS:
4337 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004338 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004339 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 default:
4341 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004342 tv->vval.v_string = iptr->isn_arg.string == NULL
4343 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 }
4345 break;
4346
Bram Moolenaar06b77222022-01-25 15:51:56 +00004347 case ISN_AUTOLOAD:
4348 {
4349 char_u *name = iptr->isn_arg.string;
4350
4351 (void)script_autoload(name, FALSE);
4352 if (find_func(name, TRUE))
4353 {
4354 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4355 goto theend;
4356 tv = STACK_TV_BOT(0);
4357 tv->v_lock = 0;
4358 ++ectx->ec_stack.ga_len;
4359 tv->v_type = VAR_FUNC;
4360 tv->vval.v_string = vim_strsave(name);
4361 }
4362 else
4363 {
4364 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4365
4366 if (res == NOTDONE)
4367 goto theend;
4368 if (res == FAIL)
4369 goto on_error;
4370 }
4371 }
4372 break;
4373
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004374 case ISN_UNLET:
4375 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4376 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004377 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004378 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004379 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004380 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004381 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004382
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004383 case ISN_LOCKUNLOCK:
4384 {
Ernie Raelee865f32023-09-29 19:53:55 +02004385#ifdef LOG_LOCKVAR
4386 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4387 iptr->isn_arg.string);
4388#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004389 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004390
4391 // Stack has the local variable, argument the whole :lock
4392 // or :unlock command, like ISN_EXEC.
4393 --ectx->ec_stack.ga_len;
Ernie Rael4c8da022023-10-11 21:35:11 +02004394 lval_root_T root = { .lr_tv = STACK_TV_BOT(0),
4395 .lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec,
4396 .lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg };
Ernie Rael64885642023-10-04 20:16:22 +02004397 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004398 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004399 iptr->isn_arg.lockunlock.lu_string);
4400 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004401 lval_root = lval_root_save;
4402 if (res == FAIL)
4403 goto on_error;
4404 }
4405 break;
4406
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004407 case ISN_LOCKCONST:
4408 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4409 break;
4410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 // create a list from items on the stack; uses a single allocation
4412 // for the list header and the items
4413 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004414 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004415 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 break;
4417
4418 // create a dict from items on the stack
4419 case ISN_NEWDICT:
4420 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004421 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004423 SOURCING_LNUM = iptr->isn_lnum;
4424 res = exe_newdict(iptr->isn_arg.number, ectx);
4425 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004426 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004427 if (res == MAYBE)
4428 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 }
4430 break;
4431
LemonBoy372bcce2022-04-25 12:43:20 +01004432 case ISN_CONCAT:
4433 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4434 goto theend;
4435 break;
4436
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004437 // create a partial with NULL value
4438 case ISN_NEWPARTIAL:
4439 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4440 goto theend;
4441 ++ectx->ec_stack.ga_len;
4442 tv = STACK_TV_BOT(-1);
4443 tv->v_type = VAR_PARTIAL;
4444 tv->v_lock = 0;
4445 tv->vval.v_partial = NULL;
4446 break;
4447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 // call a :def function
4449 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004450 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004451 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4452 NULL,
4453 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004454 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004455 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 break;
4457
Bram Moolenaard0200c82023-01-28 15:19:40 +00004458 // call a method on an interface
4459 case ISN_METHODCALL:
4460 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004461 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4462
Bram Moolenaard0200c82023-01-28 15:19:40 +00004463 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004464 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004465 if (tv->v_type != VAR_OBJECT)
4466 {
4467 object_required_error(tv);
4468 goto on_error;
4469 }
Ernie Raelbe828252024-07-26 18:37:02 +02004470
Bram Moolenaard0200c82023-01-28 15:19:40 +00004471 object_T *obj = tv->vval.v_object;
Ernie Raelbe828252024-07-26 18:37:02 +02004472 if (obj == NULL)
4473 {
4474 emsg(_(e_using_null_object));
4475 goto on_error;
4476 }
4477
Ernie Rael58c95792024-08-13 23:27:22 +02004478 ufunc_T *ufunc;
4479 if (mfunc->cmf_is_super)
4480 // Doing "super.Func", use the specific ufunc.
4481 ufunc = mfunc->cmf_itf->class_obj_methods[
4482 mfunc->cmf_idx];
4483 else
4484 {
4485 class_T *cl = obj->obj_class;
Bram Moolenaard0200c82023-01-28 15:19:40 +00004486
Ernie Rael58c95792024-08-13 23:27:22 +02004487 // convert the interface index to the object index
4488 int idx = object_index_from_itf_index(mfunc->cmf_itf,
4489 TRUE, mfunc->cmf_idx, cl);
4490 ufunc = cl->class_obj_methods[idx];
4491 }
Bram Moolenaard0200c82023-01-28 15:19:40 +00004492
Ernie Rael58c95792024-08-13 23:27:22 +02004493 if (call_ufunc(ufunc, NULL, mfunc->cmf_argcount, ectx,
4494 NULL, NULL) == FAIL)
Bram Moolenaard0200c82023-01-28 15:19:40 +00004495 goto on_error;
4496 }
4497 break;
4498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 // call a builtin function
4500 case ISN_BCALL:
4501 SOURCING_LNUM = iptr->isn_lnum;
4502 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4503 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004504 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004505 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 break;
4507
4508 // call a funcref or partial
4509 case ISN_PCALL:
4510 {
4511 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4512 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004513 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514
4515 SOURCING_LNUM = iptr->isn_lnum;
4516 if (pfunc->cpf_top)
4517 {
4518 // funcref is above the arguments
4519 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4520 }
4521 else
4522 {
4523 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004524 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004525 partial_tv = *STACK_TV_BOT(0);
4526 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004528 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004529 if (tv == &partial_tv)
4530 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004532 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 }
4534 break;
4535
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004536 case ISN_PCALL_END:
4537 // PCALL finished, arguments have been consumed and replaced by
4538 // the return value. Now clear the funcref from the stack,
4539 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004540 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004541 clear_tv(STACK_TV_BOT(-1));
4542 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4543 break;
4544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 // call a user defined function or funcref/partial
4546 case ISN_UCALL:
4547 {
4548 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4549
4550 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004551 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004552 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004553 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 }
4555 break;
4556
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004557 // :defer func(arg)
4558 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004559 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004560 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4561 goto on_error;
4562 break;
4563
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004564 // Return from a :def function call without a value.
4565 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004566 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004567 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004568 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004569 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004570 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004571 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004572 if (iptr->isn_type == ISN_RETURN_VOID)
4573 {
4574 tv->v_type = VAR_VOID;
4575 tv->vval.v_number = 0;
4576 tv->v_lock = 0;
4577 }
4578 else
4579 {
4580 *tv = *STACK_TV_VAR(0);
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01004581 object_T *obj = tv->vval.v_object;
4582 ++obj->obj_refcount;
4583
4584 // Lock all the constant object variables
4585 obj_lock_const_vars(obj);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004586 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004587 // FALLTHROUGH
4588
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004589 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 case ISN_RETURN:
4591 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004592 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004593 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004594
Ernie Raelb077b582023-12-14 20:11:44 +01004595 ///////////////////////////////////////////////////
4596 // TODO: If FAIL, line number in output not correct
4597 ///////////////////////////////////////////////////
4598 if (check_typval_is_value(STACK_TV_BOT(-1)) == FAIL)
4599 goto theend;
4600
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004601 if (trystack->ga_len > 0)
4602 trycmd = ((trycmd_T *)trystack->ga_data)
4603 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004604 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004605 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004607 // jump to ":finally" or ":endtry"
4608 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004609 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004610 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004611 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 trycmd->tcd_return = TRUE;
4613 }
4614 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004615 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 }
4617 break;
4618
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004619 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 case ISN_FUNCREF:
4621 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004622 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4623 ufunc_T *ufunc;
4624 funcref_T *funcref = &iptr->isn_arg.funcref;
4625 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004628 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004629 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004630 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004631 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004632 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004633 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004634 if (extra != NULL && extra->fre_class != NULL)
4635 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004636 class_T *cl;
4637 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004638 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004639 tv = STACK_TV_BOT(-1);
4640 if (tv->v_type != VAR_OBJECT)
4641 {
Ernie Raelbe828252024-07-26 18:37:02 +02004642 SOURCING_LNUM = iptr->isn_lnum;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004643 object_required_error(tv);
4644 vim_free(pt);
4645 goto on_error;
4646 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004647
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004648 object_T *obj = tv->vval.v_object;
Ernie Raelbe828252024-07-26 18:37:02 +02004649 if (obj == NULL)
4650 {
4651 SOURCING_LNUM = iptr->isn_lnum;
4652 emsg(_(e_using_null_object));
4653 vim_free(pt);
4654 goto on_error;
4655 }
4656
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004657 cl = obj->obj_class;
4658 // drop the value from the stack
4659 clear_tv(tv);
4660 --ectx->ec_stack.ga_len;
4661
4662 pt->pt_obj = obj;
4663 ++obj->obj_refcount;
4664 }
4665 else
4666 cl = extra->fre_class;
4667
4668 if (extra->fre_object_method)
4669 {
4670 // object method
4671 // convert the interface index to the object index
4672 int idx =
4673 object_index_from_itf_index(extra->fre_class,
4674 TRUE, extra->fre_method_idx, cl);
4675 ufunc = cl->class_obj_methods[idx];
4676 }
4677 else
4678 {
4679 // class method
4680 ufunc =
4681 cl->class_class_functions[extra->fre_method_idx];
4682 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004683 }
4684 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004685 {
4686 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4687 + funcref->fr_dfunc_idx;
4688
4689 ufunc = pt_dfunc->df_ufunc;
4690 }
4691 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004692 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004693 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004694 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004695 if (ufunc == NULL)
4696 {
4697 SOURCING_LNUM = iptr->isn_lnum;
4698 iemsg("ufunc unexpectedly NULL for FUNCREF");
Christian Brabandt915f3bf2024-04-05 20:12:19 +02004699 vim_free(pt);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004700 goto theend;
4701 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004702 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004703 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004704 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004705 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004707 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 tv->vval.v_partial = pt;
4709 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004710 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711 }
4712 break;
4713
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004714 // Create a global function from a lambda.
4715 case ISN_NEWFUNC:
4716 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004717 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004718
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004719 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004720 arg->nfa_global, &arg->nfa_loopvar_info,
4721 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004722 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004723 }
4724 break;
4725
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004726 // List functions
4727 case ISN_DEF:
4728 if (iptr->isn_arg.string == NULL)
4729 list_functions(NULL);
4730 else
4731 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004732 exarg_T ea;
4733 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004734
4735 CLEAR_FIELD(ea);
4736 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004737 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004738 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004739 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004740 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004741 }
4742 break;
4743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 // jump if a condition is met
4745 case ISN_JUMP:
4746 {
4747 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004748 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 int jump = TRUE;
4750
4751 if (when != JUMP_ALWAYS)
4752 {
4753 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004754 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004755 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004756 || when == JUMP_IF_COND_TRUE)
4757 {
4758 SOURCING_LNUM = iptr->isn_lnum;
4759 jump = tv_get_bool_chk(tv, &error);
4760 if (error)
4761 goto on_error;
4762 }
4763 else
4764 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004765 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004767 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768 {
4769 // drop the value from the stack
4770 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004771 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 }
4773 }
4774 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004775 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 }
4777 break;
4778
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004779 // "while": jump to end if a condition is false
4780 case ISN_WHILE:
4781 {
4782 int error = FALSE;
4783 int jump = TRUE;
4784
4785 tv = STACK_TV_BOT(-1);
4786 SOURCING_LNUM = iptr->isn_lnum;
4787 jump = !tv_get_bool_chk(tv, &error);
4788 if (error)
4789 goto on_error;
4790 // drop the value from the stack
4791 clear_tv(tv);
4792 --ectx->ec_stack.ga_len;
4793 if (jump)
4794 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4795
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004796 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004797 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004798 tv = STACK_TV_VAR(
4799 iptr->isn_arg.whileloop.while_funcref_idx);
4800 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4801 }
4802 break;
4803
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004804 // Jump if an argument with a default value was already set and not
4805 // v:none.
4806 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004807 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004808 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004809 int arg_set = tv->v_type != VAR_UNKNOWN
4810 && !(tv->v_type == VAR_SPECIAL
4811 && tv->vval.v_number == VVAL_NONE);
4812 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004813 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004814 break;
4815
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 // top of a for loop
4817 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004818 if (execute_for(iptr, ectx) == FAIL)
4819 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820 break;
4821
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004822 // end of a for or while loop
4823 case ISN_ENDLOOP:
4824 if (execute_endloop(iptr, ectx) == FAIL)
4825 goto theend;
4826 break;
4827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 // start of ":try" block
4829 case ISN_TRY:
4830 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004831 trycmd_T *trycmd = NULL;
4832
Bram Moolenaar35578162021-08-02 19:10:38 +02004833 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004834 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004835 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4836 + ectx->ec_trystack.ga_len;
4837 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004839 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004840 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4841 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004842 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004843 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004844 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004845 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004846 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004847 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 }
4849 break;
4850
4851 case ISN_PUSHEXC:
4852 if (current_exception == NULL)
4853 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004854 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004856 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004858 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004859 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004861 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004863 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 tv->vval.v_string = vim_strsave(
4865 (char_u *)current_exception->value);
4866 break;
4867
4868 case ISN_CATCH:
4869 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004870 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004871 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872
Bram Moolenaar4c137212021-04-19 16:48:48 +02004873 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004874 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004876 trycmd->tcd_caught = TRUE;
4877 trycmd->tcd_did_throw = FALSE;
4878
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004880 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 catch_exception(current_exception);
4882 }
4883 break;
4884
Bram Moolenaarc150c092021-02-13 15:02:46 +01004885 case ISN_TRYCONT:
4886 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004887 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004888 trycont_T *trycont = &iptr->isn_arg.trycont;
4889 int i;
4890 trycmd_T *trycmd;
4891 int iidx = trycont->tct_where;
4892
4893 if (trystack->ga_len < trycont->tct_levels)
4894 {
4895 siemsg("TRYCONT: expected %d levels, found %d",
4896 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004897 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004898 }
4899 // Make :endtry jump to any outer try block and the last
4900 // :endtry inside the loop to the loop start.
4901 for (i = trycont->tct_levels; i > 0; --i)
4902 {
4903 trycmd = ((trycmd_T *)trystack->ga_data)
4904 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004905 // Add one to tcd_cont to be able to jump to
4906 // instruction with index zero.
4907 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004908 iidx = trycmd->tcd_finally_idx == 0
4909 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004910 }
4911 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004912 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004913 }
4914 break;
4915
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004916 case ISN_FINALLY:
4917 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004918 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004919 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4920 + trystack->ga_len - 1;
4921
4922 // Reset the index to avoid a return statement jumps here
4923 // again.
4924 trycmd->tcd_finally_idx = 0;
4925 break;
4926 }
4927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 // end of ":try" block
4929 case ISN_ENDTRY:
4930 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004931 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004932 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933
Bram Moolenaar06651632022-04-27 17:54:25 +01004934 --trystack->ga_len;
4935 --trylevel;
4936 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4937 if (trycmd->tcd_did_throw)
4938 did_throw = TRUE;
4939 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004941 // discard the exception
4942 if (caught_stack == current_exception)
4943 caught_stack = caught_stack->caught;
4944 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004946
4947 if (trycmd->tcd_return)
4948 goto func_return;
4949
4950 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4951 {
4952 --ectx->ec_stack.ga_len;
4953 clear_tv(STACK_TV_BOT(0));
4954 }
4955 if (trycmd->tcd_cont != 0)
4956 // handling :continue: jump to outer try block or
4957 // start of the loop
4958 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 }
4960 break;
4961
4962 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004963 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004964 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004965
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004966 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4967 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004968 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004969 // the function to abort but not display an error.
4970 tv = STACK_TV_BOT(-1);
4971 clear_tv(tv);
4972 tv->v_type = VAR_NUMBER;
4973 tv->vval.v_number = 0;
4974 goto done;
4975 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004976 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004977 tv = STACK_TV_BOT(0);
4978 if (tv->vval.v_string == NULL
4979 || *skipwhite(tv->vval.v_string) == NUL)
4980 {
4981 vim_free(tv->vval.v_string);
4982 SOURCING_LNUM = iptr->isn_lnum;
4983 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004984 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004985 }
4986
4987 // Inside a "catch" we need to first discard the caught
4988 // exception.
4989 if (trystack->ga_len > 0)
4990 {
4991 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4992 + trystack->ga_len - 1;
4993 if (trycmd->tcd_caught && current_exception != NULL)
4994 {
4995 // discard the exception
4996 if (caught_stack == current_exception)
4997 caught_stack = caught_stack->caught;
4998 discard_current_exception();
4999 trycmd->tcd_caught = FALSE;
5000 }
5001 }
5002
Bram Moolenaar90a57162022-02-12 14:23:17 +00005003 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005004 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
5005 == FAIL)
5006 {
5007 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005008 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01005009 }
5010 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 break;
5013
5014 // compare with special values
5015 case ISN_COMPAREBOOL:
5016 case ISN_COMPARESPECIAL:
5017 {
5018 typval_T *tv1 = STACK_TV_BOT(-2);
5019 typval_T *tv2 = STACK_TV_BOT(-1);
5020 varnumber_T arg1 = tv1->vval.v_number;
5021 varnumber_T arg2 = tv2->vval.v_number;
5022 int res;
5023
Bram Moolenaar06651632022-04-27 17:54:25 +01005024 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
5025 res = arg1 == arg2;
5026 else
5027 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028
Bram Moolenaar4c137212021-04-19 16:48:48 +02005029 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030 tv1->v_type = VAR_BOOL;
5031 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5032 }
5033 break;
5034
Bram Moolenaar7a222242022-03-01 19:23:24 +00005035 case ISN_COMPARENULL:
5036 {
5037 typval_T *tv1 = STACK_TV_BOT(-2);
5038 typval_T *tv2 = STACK_TV_BOT(-1);
5039 int res;
5040
5041 res = typval_compare_null(tv1, tv2);
5042 if (res == MAYBE)
5043 goto on_error;
5044 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
5045 res = !res;
5046 clear_tv(tv1);
5047 clear_tv(tv2);
5048 --ectx->ec_stack.ga_len;
5049 tv1->v_type = VAR_BOOL;
5050 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5051 }
5052 break;
5053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 // Operation with two number arguments
5055 case ISN_OPNR:
5056 case ISN_COMPARENR:
5057 {
5058 typval_T *tv1 = STACK_TV_BOT(-2);
5059 typval_T *tv2 = STACK_TV_BOT(-1);
5060 varnumber_T arg1 = tv1->vval.v_number;
5061 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02005062 varnumber_T res = 0;
5063 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005065 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
5066 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
5067 {
5068 if (arg2 < 0)
5069 {
5070 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00005071 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005072 goto on_error;
5073 }
5074 }
5075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 switch (iptr->isn_arg.op.op_type)
5077 {
5078 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02005079 case EXPR_DIV: if (arg2 == 0)
5080 div_zero = TRUE;
5081 else
5082 res = arg1 / arg2;
5083 break;
5084 case EXPR_REM: if (arg2 == 0)
5085 div_zero = TRUE;
5086 else
5087 res = arg1 % arg2;
5088 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 case EXPR_SUB: res = arg1 - arg2; break;
5090 case EXPR_ADD: res = arg1 + arg2; break;
5091
5092 case EXPR_EQUAL: res = arg1 == arg2; break;
5093 case EXPR_NEQUAL: res = arg1 != arg2; break;
5094 case EXPR_GREATER: res = arg1 > arg2; break;
5095 case EXPR_GEQUAL: res = arg1 >= arg2; break;
5096 case EXPR_SMALLER: res = arg1 < arg2; break;
5097 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005098 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
5099 res = 0;
5100 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01005101 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005102 break;
5103 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
5104 res = 0;
5105 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01005106 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005107 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02005108 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 }
5110
Bram Moolenaar4c137212021-04-19 16:48:48 +02005111 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 if (iptr->isn_type == ISN_COMPARENR)
5113 {
5114 tv1->v_type = VAR_BOOL;
5115 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5116 }
5117 else
5118 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02005119 if (div_zero)
5120 {
5121 SOURCING_LNUM = iptr->isn_lnum;
5122 emsg(_(e_divide_by_zero));
5123 goto on_error;
5124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 }
5126 break;
5127
5128 // Computation with two float arguments
5129 case ISN_OPFLOAT:
5130 case ISN_COMPAREFLOAT:
5131 {
5132 typval_T *tv1 = STACK_TV_BOT(-2);
5133 typval_T *tv2 = STACK_TV_BOT(-1);
5134 float_T arg1 = tv1->vval.v_float;
5135 float_T arg2 = tv2->vval.v_float;
5136 float_T res = 0;
5137 int cmp = FALSE;
5138
5139 switch (iptr->isn_arg.op.op_type)
5140 {
5141 case EXPR_MULT: res = arg1 * arg2; break;
5142 case EXPR_DIV: res = arg1 / arg2; break;
5143 case EXPR_SUB: res = arg1 - arg2; break;
5144 case EXPR_ADD: res = arg1 + arg2; break;
5145
5146 case EXPR_EQUAL: cmp = arg1 == arg2; break;
5147 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
5148 case EXPR_GREATER: cmp = arg1 > arg2; break;
5149 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
5150 case EXPR_SMALLER: cmp = arg1 < arg2; break;
5151 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
5152 default: cmp = 0; break;
5153 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005154 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155 if (iptr->isn_type == ISN_COMPAREFLOAT)
5156 {
5157 tv1->v_type = VAR_BOOL;
5158 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
5159 }
5160 else
5161 tv1->vval.v_float = res;
5162 }
5163 break;
5164
5165 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00005166 case ISN_COMPAREDICT:
5167 case ISN_COMPAREFUNC:
5168 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005169 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005170 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171 {
5172 typval_T *tv1 = STACK_TV_BOT(-2);
5173 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00005174 exprtype_T exprtype = iptr->isn_arg.op.op_type;
5175 int ic = iptr->isn_arg.op.op_ic;
5176 int res = FALSE;
5177 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178
Bram Moolenaar265f8112021-12-19 12:33:05 +00005179 SOURCING_LNUM = iptr->isn_lnum;
5180 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005182 status = typval_compare_list(tv1, tv2,
5183 exprtype, ic, &res);
5184 }
5185 else if (iptr->isn_type == ISN_COMPAREDICT)
5186 {
5187 status = typval_compare_dict(tv1, tv2,
5188 exprtype, ic, &res);
5189 }
5190 else if (iptr->isn_type == ISN_COMPAREFUNC)
5191 {
5192 status = typval_compare_func(tv1, tv2,
5193 exprtype, ic, &res);
5194 }
5195 else if (iptr->isn_type == ISN_COMPARESTRING)
5196 {
5197 status = typval_compare_string(tv1, tv2,
5198 exprtype, ic, &res);
5199 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005200 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005201 {
5202 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005204 else // ISN_COMPAREOBJECT
5205 {
5206 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005207 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005208 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005209 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 clear_tv(tv1);
5211 clear_tv(tv2);
5212 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005213 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5214 if (status == FAIL)
5215 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216 }
5217 break;
5218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 case ISN_COMPAREANY:
5220 {
5221 typval_T *tv1 = STACK_TV_BOT(-2);
5222 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005223 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005224 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005225 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005226
Bram Moolenaareb26f432020-09-14 16:50:05 +02005227 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005228 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005229 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005230 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005231 if (status == FAIL)
5232 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 }
5234 break;
5235
5236 case ISN_ADDLIST:
5237 case ISN_ADDBLOB:
5238 {
5239 typval_T *tv1 = STACK_TV_BOT(-2);
5240 typval_T *tv2 = STACK_TV_BOT(-1);
5241
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005242 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005244 {
5245 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5246 && tv1->vval.v_list != NULL)
5247 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5248 NULL);
5249 else
5250 eval_addlist(tv1, tv2);
5251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252 else
5253 eval_addblob(tv1, tv2);
5254 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005255 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 }
5257 break;
5258
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005259 case ISN_LISTAPPEND:
5260 {
5261 typval_T *tv1 = STACK_TV_BOT(-2);
5262 typval_T *tv2 = STACK_TV_BOT(-1);
5263 list_T *l = tv1->vval.v_list;
5264
5265 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005266 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005267 if (l == NULL)
5268 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005269 emsg(_(e_cannot_add_to_null_list));
5270 goto on_error;
5271 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005272 if (value_check_lock(l->lv_lock, NULL, FALSE))
5273 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005274 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005275 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005276 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005277 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005278 }
5279 break;
5280
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005281 case ISN_BLOBAPPEND:
5282 {
5283 typval_T *tv1 = STACK_TV_BOT(-2);
5284 typval_T *tv2 = STACK_TV_BOT(-1);
5285 blob_T *b = tv1->vval.v_blob;
5286 int error = FALSE;
5287 varnumber_T n;
5288
5289 // add a number to a blob
5290 if (b == NULL)
5291 {
5292 SOURCING_LNUM = iptr->isn_lnum;
5293 emsg(_(e_cannot_add_to_null_blob));
5294 goto on_error;
5295 }
5296 n = tv_get_number_chk(tv2, &error);
5297 if (error)
5298 goto on_error;
5299 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005300 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005301 }
5302 break;
5303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304 // Computation with two arguments of unknown type
5305 case ISN_OPANY:
5306 {
5307 typval_T *tv1 = STACK_TV_BOT(-2);
5308 typval_T *tv2 = STACK_TV_BOT(-1);
5309 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005311 int error = FALSE;
5312
5313 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5314 {
5315 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5316 {
5317 eval_addlist(tv1, tv2);
5318 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005319 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320 break;
5321 }
5322 else if (tv1->v_type == VAR_BLOB
5323 && tv2->v_type == VAR_BLOB)
5324 {
5325 eval_addblob(tv1, tv2);
5326 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005327 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328 break;
5329 }
5330 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 if (tv1->v_type == VAR_FLOAT)
5332 {
5333 f1 = tv1->vval.v_float;
5334 n1 = 0;
5335 }
5336 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005338 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339 n1 = tv_get_number_chk(tv1, &error);
5340 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005341 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 if (tv2->v_type == VAR_FLOAT)
5343 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005344 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345 if (tv2->v_type == VAR_FLOAT)
5346 {
5347 f2 = tv2->vval.v_float;
5348 n2 = 0;
5349 }
5350 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351 {
5352 n2 = tv_get_number_chk(tv2, &error);
5353 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005354 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355 if (tv1->v_type == VAR_FLOAT)
5356 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005358 // if there is a float on either side the result is a float
5359 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5360 {
5361 switch (iptr->isn_arg.op.op_type)
5362 {
5363 case EXPR_MULT: f1 = f1 * f2; break;
5364 case EXPR_DIV: f1 = f1 / f2; break;
5365 case EXPR_SUB: f1 = f1 - f2; break;
5366 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005367 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005368 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005369 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370 }
5371 clear_tv(tv1);
5372 clear_tv(tv2);
5373 tv1->v_type = VAR_FLOAT;
5374 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005375 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005376 }
5377 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005379 int failed = FALSE;
5380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005381 switch (iptr->isn_arg.op.op_type)
5382 {
5383 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005384 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5385 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005386 goto on_error;
5387 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005388 case EXPR_SUB: n1 = n1 - n2; break;
5389 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005390 default: n1 = num_modulus(n1, n2, &failed);
5391 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005392 goto on_error;
5393 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394 }
5395 clear_tv(tv1);
5396 clear_tv(tv2);
5397 tv1->v_type = VAR_NUMBER;
5398 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005399 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400 }
5401 }
5402 break;
5403
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005404 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005405 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005406 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005407 int is_slice = iptr->isn_type == ISN_STRSLICE;
5408 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005409 char_u *res;
5410
5411 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005412 // string slice: string is at stack-3, first index at
5413 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005414 if (is_slice)
5415 {
5416 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005417 n1 = tv->vval.v_number;
5418 }
5419
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005420 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005421 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005422
Bram Moolenaar4c137212021-04-19 16:48:48 +02005423 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005424 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005425 if (is_slice)
5426 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005427 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005428 else
5429 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005430 // single character (including composing characters).
5431 // If the index is too big or negative the result is
5432 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005433 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005434 vim_free(tv->vval.v_string);
5435 tv->vval.v_string = res;
5436 }
5437 break;
5438
5439 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005440 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005441 case ISN_BLOBINDEX:
5442 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005443 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005444 int is_slice = iptr->isn_type == ISN_LISTSLICE
5445 || iptr->isn_type == ISN_BLOBSLICE;
5446 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5447 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005448 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005449 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450
5451 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005452 // list slice: list is at stack-3, indexes at stack-2 and
5453 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005454 // Same for blob.
5455 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456
5457 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005458 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005460
5461 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462 {
Bram Moolenaared591872020-08-15 22:14:53 +02005463 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005464 n1 = tv->vval.v_number;
5465 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466 }
Bram Moolenaared591872020-08-15 22:14:53 +02005467
Bram Moolenaar4c137212021-04-19 16:48:48 +02005468 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005469 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005470 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005471 if (is_blob)
5472 {
5473 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5474 n1, n2, FALSE, tv) == FAIL)
5475 goto on_error;
5476 }
5477 else
5478 {
5479 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5480 n1, n2, FALSE, tv, TRUE) == FAIL)
5481 goto on_error;
5482 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005483 }
5484 break;
5485
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005486 case ISN_ANYINDEX:
5487 case ISN_ANYSLICE:
5488 {
5489 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5490 typval_T *var1, *var2;
5491 int res;
5492
5493 // index: composite is at stack-2, index at stack-1
5494 // slice: composite is at stack-3, indexes at stack-2 and
5495 // stack-1
5496 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005497 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005498 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5499 goto on_error;
5500 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5501 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005502 res = eval_index_inner(tv, is_slice, var1, var2,
5503 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005504 clear_tv(var1);
5505 if (is_slice)
5506 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005507 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005508 if (res == FAIL)
5509 goto on_error;
5510 }
5511 break;
5512
Bram Moolenaar9af78762020-06-16 11:34:42 +02005513 case ISN_SLICE:
5514 {
5515 list_T *list;
5516 int count = iptr->isn_arg.number;
5517
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005518 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005519 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005520 list = tv->vval.v_list;
5521
5522 // no error for short list, expect it to be checked earlier
5523 if (list != NULL && list->lv_len >= count)
5524 {
5525 list_T *newlist = list_slice(list,
5526 count, list->lv_len - 1);
5527
5528 if (newlist != NULL)
5529 {
5530 list_unref(list);
5531 tv->vval.v_list = newlist;
5532 ++newlist->lv_refcount;
5533 }
5534 }
5535 }
5536 break;
5537
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005538 case ISN_GETITEM:
5539 {
5540 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005541 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005542
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005543 // Get list item: list is at stack-1, push item.
5544 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005545 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5546 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005547
Bram Moolenaar35578162021-08-02 19:10:38 +02005548 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005549 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005550 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005551 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005552
5553 // Useful when used in unpack assignment. Reset at
5554 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005555 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005556 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005557 }
5558 break;
5559
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01005560 // dict member with string key (dict['member'])
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005561 case ISN_MEMBER:
5562 {
5563 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005564 char_u *key;
5565 dictitem_T *di;
5566
5567 // dict member: dict is at stack-2, key at stack-1
5568 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005569 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005570 dict = tv->vval.v_dict;
5571
5572 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005573 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005574 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005575 if (key == NULL)
5576 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005577
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005578 if ((di = dict_find(dict, key, -1)) == NULL)
5579 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005580 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005581 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005582
5583 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005584 // stack contents makes sense and the dict stack is
5585 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005586 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005587 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005588 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005589 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005590 tv->v_type = VAR_NUMBER;
5591 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005592 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005593 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005594 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005595 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005596 // Put the dict used on the dict stack, it might be used by
5597 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005598 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005599 if (dict_stack_save(tv) == FAIL)
5600 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005601 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005602 }
5603 break;
5604
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01005605 // dict member with string key (dict.member)
5606 // or can be an object
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005607 case ISN_STRINGMEMBER:
5608 {
5609 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005610 dictitem_T *di;
5611
5612 tv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01005614 if (tv->v_type == VAR_OBJECT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005615 {
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01005616 if (dict_stack_save(tv) == FAIL)
5617 goto on_fatal_error;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005618
Yegappan Lakshmanan56d45f12024-11-11 19:58:55 +01005619 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
5620 + ectx->ec_dfunc_idx)->df_ufunc;
5621 // Class object (not a Dict)
5622 if (any_var_get_obj_member(ufunc->uf_class, iptr, tv) == FAIL)
5623 goto on_error;
5624 }
5625 else
5626 {
5627 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5628 {
5629 SOURCING_LNUM = iptr->isn_lnum;
5630 emsg(_(e_dictionary_required));
5631 goto on_error;
5632 }
5633 dict = tv->vval.v_dict;
5634
5635 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5636 == NULL)
5637 {
5638 SOURCING_LNUM = iptr->isn_lnum;
5639 semsg(_(e_key_not_present_in_dictionary_str),
5640 iptr->isn_arg.string);
5641 goto on_error;
5642 }
5643 // Put the dict used on the dict stack, it might be
5644 // used by a dict function later.
5645 if (dict_stack_save(tv) == FAIL)
5646 goto on_fatal_error;
5647
5648 copy_tv(&di->di_tv, tv);
5649 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005650 }
5651 break;
5652
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005653 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005654 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005655 {
5656 tv = STACK_TV_BOT(-1);
5657 if (tv->v_type != VAR_OBJECT)
5658 {
5659 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005660 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005661 goto on_error;
5662 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005663
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005664 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005665 if (obj == NULL)
5666 {
5667 SOURCING_LNUM = iptr->isn_lnum;
5668 emsg(_(e_using_null_object));
5669 goto on_error;
5670 }
5671
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005672 int idx;
5673 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005674 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005675 else
5676 {
5677 idx = iptr->isn_arg.classmember.cm_idx;
5678 // convert the interface index to the object index
5679 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005680 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005681 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005682 }
5683
Ernie Rael18143d32023-09-04 22:30:41 +02005684 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005685 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005686 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005687
5688 // Unreference the object after getting the member, it may
5689 // be freed.
5690 object_unref(obj);
5691 }
5692 break;
5693
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005694 case ISN_STORE_THIS:
5695 {
5696 int idx = iptr->isn_arg.number;
5697 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5698 // the members are located right after the object struct
5699 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5700 clear_tv(mtv);
5701 *mtv = *STACK_TV_BOT(-1);
5702 --ectx->ec_stack.ga_len;
5703 }
5704 break;
5705
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005706 case ISN_CLEARDICT:
5707 dict_stack_drop();
5708 break;
5709
5710 case ISN_USEDICT:
5711 {
5712 typval_T *dict_tv = dict_stack_get_tv();
5713
5714 // Turn "dict.Func" into a partial for "Func" bound to
5715 // "dict". Don't do this when "Func" is already a partial
5716 // that was bound explicitly (pt_auto is FALSE).
5717 tv = STACK_TV_BOT(-1);
5718 if (dict_tv != NULL
5719 && dict_tv->v_type == VAR_DICT
5720 && dict_tv->vval.v_dict != NULL
5721 && (tv->v_type == VAR_FUNC
5722 || (tv->v_type == VAR_PARTIAL
5723 && (tv->vval.v_partial->pt_auto
5724 || tv->vval.v_partial->pt_dict == NULL))))
5725 dict_tv->vval.v_dict =
5726 make_partial(dict_tv->vval.v_dict, tv);
5727 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005728 }
5729 break;
5730
5731 case ISN_NEGATENR:
5732 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005733 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005734 if (tv->v_type == VAR_FLOAT)
5735 tv->vval.v_float = -tv->vval.v_float;
5736 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005737 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005738 break;
5739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005740 case ISN_CHECKTYPE:
5741 {
5742 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005743 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005744 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005745
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005746 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005747 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005748 if (ct->ct_arg_idx > 0)
5749 {
5750 where.wt_index = ct->ct_arg_idx;
5751 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005752 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005753 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005754 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005755 if (r == FAIL)
5756 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005757
5758 // number 0 is FALSE, number 1 is TRUE
5759 if (tv->v_type == VAR_NUMBER
5760 && ct->ct_type->tt_type == VAR_BOOL
5761 && (tv->vval.v_number == 0
5762 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005764 tv->v_type = VAR_BOOL;
5765 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005766 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005767 }
5768 }
5769 break;
5770
Bram Moolenaar9af78762020-06-16 11:34:42 +02005771 case ISN_CHECKLEN:
5772 {
5773 int min_len = iptr->isn_arg.checklen.cl_min_len;
5774 list_T *list = NULL;
5775
5776 tv = STACK_TV_BOT(-1);
5777 if (tv->v_type == VAR_LIST)
5778 list = tv->vval.v_list;
5779 if (list == NULL || list->lv_len < min_len
5780 || (list->lv_len > min_len
5781 && !iptr->isn_arg.checklen.cl_more_OK))
5782 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005783 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005784 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005785 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005786 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005787 }
5788 }
5789 break;
5790
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005791 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005792 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005793 break;
5794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005795 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005796 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005797 {
5798 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005799 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005800
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005801 if (iptr->isn_type == ISN_2BOOL)
5802 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005803 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005804 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005805 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005806 n = !n;
5807 }
5808 else
5809 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005810 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005811 SOURCING_LNUM = iptr->isn_lnum;
5812 n = tv_get_bool_chk(tv, &error);
5813 if (error)
5814 goto on_error;
5815 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005816 clear_tv(tv);
5817 tv->v_type = VAR_BOOL;
5818 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5819 }
5820 break;
5821
5822 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005823 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005824 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005825 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5826 iptr->isn_type == ISN_2STRING_ANY,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02005827 iptr->isn_arg.tostring.flags) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005828 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005829 break;
5830
Bram Moolenaar08597872020-12-10 19:43:40 +01005831 case ISN_RANGE:
5832 {
5833 exarg_T ea;
5834 char *errormsg;
5835
Bram Moolenaarece0b872021-01-08 20:40:45 +01005836 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005837 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005838 ea.addr_type = ADDR_LINES;
5839 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005840 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005841 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005842 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005843
Bram Moolenaar35578162021-08-02 19:10:38 +02005844 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005845 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005846 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005847 tv = STACK_TV_BOT(-1);
5848 tv->v_type = VAR_NUMBER;
5849 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005850 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005851 }
5852 break;
5853
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005854 case ISN_PUT:
5855 {
5856 int regname = iptr->isn_arg.put.put_regname;
5857 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5858 char_u *expr = NULL;
5859 int dir = FORWARD;
5860
Bram Moolenaar08597872020-12-10 19:43:40 +01005861 if (lnum < -2)
5862 {
5863 // line number was put on the stack by ISN_RANGE
5864 tv = STACK_TV_BOT(-1);
5865 curwin->w_cursor.lnum = tv->vval.v_number;
5866 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5867 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005868 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005869 }
5870 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005871 // :put! above cursor
5872 dir = BACKWARD;
5873 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005874 {
5875 curwin->w_cursor.lnum = lnum;
5876 if (lnum == 0)
5877 // check_cursor() below will move to line 1
5878 dir = BACKWARD;
5879 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005880
5881 if (regname == '=')
5882 {
5883 tv = STACK_TV_BOT(-1);
5884 if (tv->v_type == VAR_STRING)
5885 expr = tv->vval.v_string;
5886 else
5887 {
5888 expr = typval2string(tv, TRUE); // allocates value
5889 clear_tv(tv);
5890 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005891 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005892 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005893 check_cursor();
5894 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5895 vim_free(expr);
5896 }
5897 break;
5898
Bram Moolenaar02194d22020-10-24 23:08:38 +02005899 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005900 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5901 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5902 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5903 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005904 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5905 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005906 break;
5907
Bram Moolenaar02194d22020-10-24 23:08:38 +02005908 case ISN_CMDMOD_REV:
5909 // filter regprog is owned by the instruction, don't free it
5910 cmdmod.cmod_filter_regmatch.regprog = NULL;
5911 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005912 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5913 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005914 break;
5915
Bram Moolenaar792f7862020-11-23 08:31:18 +01005916 case ISN_UNPACK:
5917 {
5918 int count = iptr->isn_arg.unpack.unp_count;
5919 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5920 list_T *l;
5921 listitem_T *li;
5922 int i;
5923
5924 // Check there is a valid list to unpack.
5925 tv = STACK_TV_BOT(-1);
5926 if (tv->v_type != VAR_LIST)
5927 {
5928 SOURCING_LNUM = iptr->isn_lnum;
5929 emsg(_(e_for_argument_must_be_sequence_of_lists));
5930 goto on_error;
5931 }
5932 l = tv->vval.v_list;
5933 if (l == NULL
5934 || l->lv_len < (semicolon ? count - 1 : count))
5935 {
5936 SOURCING_LNUM = iptr->isn_lnum;
5937 emsg(_(e_list_value_does_not_have_enough_items));
5938 goto on_error;
5939 }
5940 else if (!semicolon && l->lv_len > count)
5941 {
5942 SOURCING_LNUM = iptr->isn_lnum;
5943 emsg(_(e_list_value_has_more_items_than_targets));
5944 goto on_error;
5945 }
5946
5947 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005948 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005949 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005950 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005951
5952 // Variable after semicolon gets a list with the remaining
5953 // items.
5954 if (semicolon)
5955 {
5956 list_T *rem_list =
5957 list_alloc_with_items(l->lv_len - count + 1);
5958
5959 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005960 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005961 tv = STACK_TV_BOT(-count);
5962 tv->vval.v_list = rem_list;
5963 ++rem_list->lv_refcount;
5964 tv->v_lock = 0;
5965 li = l->lv_first;
5966 for (i = 0; i < count - 1; ++i)
5967 li = li->li_next;
5968 for (i = 0; li != NULL; ++i)
5969 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005970 typval_T tvcopy;
5971
5972 copy_tv(&li->li_tv, &tvcopy);
5973 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005974 li = li->li_next;
5975 }
5976 --count;
5977 }
5978
5979 // Produce the values in reverse order, first item last.
5980 li = l->lv_first;
5981 for (i = 0; i < count; ++i)
5982 {
5983 tv = STACK_TV_BOT(-i - 1);
5984 copy_tv(&li->li_tv, tv);
5985 li = li->li_next;
5986 }
5987
5988 list_unref(l);
5989 }
5990 break;
5991
Bram Moolenaarb2049902021-01-24 12:53:53 +01005992 case ISN_PROF_START:
5993 case ISN_PROF_END:
5994 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005995#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005996 funccall_T cookie;
5997 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005998 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005999 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006000
Bram Moolenaarca16c602022-09-06 18:57:08 +01006001 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006002 if (iptr->isn_type == ISN_PROF_START)
6003 {
6004 func_line_start(&cookie, iptr->isn_lnum);
6005 // if we get here the instruction is executed
6006 func_line_exec(&cookie);
6007 }
6008 else
6009 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01006010#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01006011 }
6012 break;
6013
Bram Moolenaare99d4222021-06-13 14:01:26 +02006014 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02006015 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006016 break;
6017
Bram Moolenaar389df252020-07-09 21:20:47 +02006018 case ISN_SHUFFLE:
6019 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01006020 typval_T tmp_tv;
6021 int item = iptr->isn_arg.shuffle.shfl_item;
6022 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02006023
6024 tmp_tv = *STACK_TV_BOT(-item);
6025 for ( ; up > 0 && item > 1; --up)
6026 {
6027 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
6028 --item;
6029 }
6030 *STACK_TV_BOT(-item) = tmp_tv;
6031 }
6032 break;
6033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006035 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02006037 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038 break;
6039 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02006040 continue;
6041
Bram Moolenaard032f342020-07-18 18:13:02 +02006042func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02006043 // Restore previous function. If the frame pointer is where we started
6044 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006045 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02006046 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02006047
Bram Moolenaar4c137212021-04-19 16:48:48 +02006048 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02006049 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006050 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006051 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02006052
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02006053on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01006054 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006055 // If "emsg_silent" is set then ignore the error, unless it was set
6056 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006057 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006058 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01006059 {
6060 // If a sequence of instructions causes an error while ":silent!"
6061 // was used, restore the stack length and jump ahead to restoring
6062 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006063 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01006064 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006065 while (ectx->ec_stack.ga_len
6066 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01006067 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006068 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01006069 clear_tv(STACK_TV_BOT(0));
6070 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006071 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
6072 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01006073 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01006074 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01006075 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01006076on_fatal_error:
6077 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01006078 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006079 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006080 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081 }
6082
6083done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006084 ret = OK;
6085theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01006086 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006087
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006088 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02006089 ectx->ec_trylevel_at_start = save_trylevel_at_start;
6090 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006091}
6092
6093/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006094 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006095 * "rettv".
6096 * Return OK or FAIL.
6097 */
6098 int
6099exe_typval_instr(typval_T *tv, typval_T *rettv)
6100{
6101 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
6102 isn_T *save_instr = ectx->ec_instr;
6103 int save_iidx = ectx->ec_iidx;
6104 int res;
6105
LemonBoyf3b48952022-05-05 13:53:03 +01006106 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
6107 // even when the compilation fails.
6108 rettv->v_type = VAR_UNKNOWN;
6109
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006110 ectx->ec_instr = tv->vval.v_instr->instr_instr;
6111 res = exec_instructions(ectx);
6112 if (res == OK)
6113 {
6114 *rettv = *STACK_TV_BOT(-1);
6115 --ectx->ec_stack.ga_len;
6116 }
6117
6118 ectx->ec_instr = save_instr;
6119 ectx->ec_iidx = save_iidx;
6120
6121 return res;
6122}
6123
6124/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006125 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
6126 * "substitute_instr".
6127 */
6128 char_u *
6129exe_substitute_instr(void)
6130{
6131 ectx_T *ectx = substitute_instr->subs_ectx;
6132 isn_T *save_instr = ectx->ec_instr;
6133 int save_iidx = ectx->ec_iidx;
6134 char_u *res;
6135
6136 ectx->ec_instr = substitute_instr->subs_instr;
6137 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02006138 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006139 typval_T *tv = STACK_TV_BOT(-1);
6140
Bram Moolenaar27523602021-06-05 21:36:19 +02006141 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006142 --ectx->ec_stack.ga_len;
6143 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02006144 }
6145 else
6146 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006147 substitute_instr->subs_status = FAIL;
6148 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02006149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006150
Bram Moolenaar4c137212021-04-19 16:48:48 +02006151 ectx->ec_instr = save_instr;
6152 ectx->ec_iidx = save_iidx;
6153
6154 return res;
6155}
6156
6157/*
6158 * Call a "def" function from old Vim script.
6159 * Return OK or FAIL.
6160 */
6161 int
6162call_def_function(
6163 ufunc_T *ufunc,
6164 int argc_arg, // nr of arguments
6165 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006166 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02006167 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006168 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01006169 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006170 typval_T *rettv) // return value
6171{
6172 ectx_T ectx; // execution context
6173 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006174 int partial_argc = partial == NULL
6175 || (flags & DEF_USE_PT_ARGV) == 0
6176 ? 0 : partial->pt_argc;
6177 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006178 typval_T *tv;
6179 int idx;
6180 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006181 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006182 sctx_T save_current_sctx = current_sctx;
6183 int did_emsg_before = did_emsg_cumul + did_emsg;
6184 int save_suppress_errthrow = suppress_errthrow;
6185 msglist_T **saved_msg_list = NULL;
6186 msglist_T *private_msg_list = NULL;
6187 int save_emsg_silent_def = emsg_silent_def;
6188 int save_did_emsg_def = did_emsg_def;
6189 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006190 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006191
6192// Get pointer to item in the stack.
6193#undef STACK_TV
6194#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6195
6196// Get pointer to item at the bottom of the stack, -1 is the bottom.
6197#undef STACK_TV_BOT
6198#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6199
6200// Get pointer to a local variable on the stack. Negative for arguments.
6201#undef STACK_TV_VAR
6202#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6203
6204 if (ufunc->uf_def_status == UF_NOT_COMPILED
6205 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006206 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6207 && compile_def_function(ufunc, FALSE,
6208 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006209 {
6210 if (did_emsg_cumul + did_emsg == did_emsg_before)
6211 semsg(_(e_function_is_not_compiled_str),
6212 printable_func_name(ufunc));
6213 return FAIL;
6214 }
6215
6216 {
6217 // Check the function was really compiled.
6218 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6219 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006220 if (dfunc->df_ufunc == NULL)
6221 {
6222 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6223 return FAIL;
6224 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006225 if (INSTRUCTIONS(dfunc) == NULL)
6226 {
6227 iemsg("using call_def_function() on not compiled function");
6228 return FAIL;
6229 }
6230 }
6231
6232 // If depth of calling is getting too high, don't execute the function.
6233 orig_funcdepth = funcdepth_get();
6234 if (funcdepth_increment() == FAIL)
6235 return FAIL;
6236
6237 CLEAR_FIELD(ectx);
6238 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6239 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006240 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006241 {
6242 funcdepth_decrement();
6243 return FAIL;
6244 }
6245 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6246 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6247 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006248 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006249
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006250 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006251 if (idx > 0 && ufunc->uf_va_name == NULL)
6252 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006253 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006254 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006255 goto failed_early;
6256 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006257 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006258 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006259 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006260 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006261 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006262 goto failed_early;
6263 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006264
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006265 // Put values from the partial and arguments on the stack, but no more than
6266 // what the function expects. A lambda can be called with more arguments
6267 // than it uses.
6268 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006269 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6270 ++idx)
6271 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006272 int argv_idx = idx - partial_argc;
6273
6274 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006275 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006276 && tv->v_type == VAR_SPECIAL
6277 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006278 {
6279 // Use the default value.
6280 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6281 }
6282 else
6283 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006284 int done = FALSE;
6285 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6286 {
6287 type_T *expected = ufunc->uf_arg_types[idx];
6288 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6289 {
6290 // When a float is expected and a number was given, convert
6291 // the value.
6292 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6293 STACK_TV_BOT(0)->v_lock = 0;
6294 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6295 done = TRUE;
6296 }
6297 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006298 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006299 goto failed_early;
6300 }
6301 if (!done)
6302 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006303 }
6304 ++ectx.ec_stack.ga_len;
6305 }
6306
6307 // Turn varargs into a list. Empty list if no args.
6308 if (ufunc->uf_va_name != NULL)
6309 {
6310 int vararg_count = argc - ufunc->uf_args.ga_len;
6311
6312 if (vararg_count < 0)
6313 vararg_count = 0;
6314 else
6315 argc -= vararg_count;
6316 if (exe_newlist(vararg_count, &ectx) == FAIL)
6317 goto failed_early;
6318
6319 // Check the type of the list items.
6320 tv = STACK_TV_BOT(-1);
6321 if (ufunc->uf_va_type != NULL
6322 && ufunc->uf_va_type != &t_list_any
6323 && ufunc->uf_va_type->tt_member != &t_any
6324 && tv->vval.v_list != NULL)
6325 {
6326 type_T *expected = ufunc->uf_va_type->tt_member;
6327 listitem_T *li = tv->vval.v_list->lv_first;
6328
6329 for (idx = 0; idx < vararg_count; ++idx)
6330 {
6331 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006332 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006333 goto failed_early;
6334 li = li->li_next;
6335 }
6336 }
6337
6338 if (defcount > 0)
6339 // Move varargs list to below missing default arguments.
6340 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6341 --ectx.ec_stack.ga_len;
6342 }
6343
6344 // Make space for omitted arguments, will store default value below.
6345 // Any varargs list goes after them.
6346 if (defcount > 0)
6347 for (idx = 0; idx < defcount; ++idx)
6348 {
6349 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6350 ++ectx.ec_stack.ga_len;
6351 }
6352 if (ufunc->uf_va_name != NULL)
6353 ++ectx.ec_stack.ga_len;
6354
6355 // Frame pointer points to just after arguments.
6356 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6357 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6358
6359 {
6360 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6361 + ufunc->uf_dfunc_idx;
6362 ufunc_T *base_ufunc = dfunc->df_ufunc;
6363
6364 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006365 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006366 if (partial != NULL || base_ufunc->uf_partial != NULL)
6367 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006368 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6369 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006370 goto failed_early;
6371 if (partial != NULL)
6372 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006373 outer_T *outer = get_pt_outer(partial);
6374
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006375 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006376 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006377 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006378 if (current_ectx != NULL)
6379 {
6380 if (current_ectx->ec_outer_ref != NULL
6381 && current_ectx->ec_outer_ref->or_outer != NULL)
6382 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006383 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006384 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006385 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006386 }
6387 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006388 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006389 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006390 ++partial->pt_refcount;
6391 ectx.ec_outer_ref->or_partial = partial;
6392 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006393 }
6394 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006395 {
6396 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6397 ++base_ufunc->uf_partial->pt_refcount;
6398 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6399 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006400 }
6401 }
6402
6403 // dummy frame entries
6404 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6405 {
6406 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6407 ++ectx.ec_stack.ga_len;
6408 }
6409
6410 {
6411 // Reserve space for local variables and any closure reference count.
6412 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6413 + ufunc->uf_dfunc_idx;
6414
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006415 // Initialize variables to zero. That avoids having to generate
6416 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006417 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006418 {
6419 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6420 STACK_TV_VAR(idx)->vval.v_number = 0;
6421 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006422 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006423
6424 if (object != NULL)
6425 {
6426 // the object is always the variable at index zero
6427 tv = STACK_TV_VAR(0);
6428 tv->v_type = VAR_OBJECT;
6429 tv->vval.v_object = object;
6430 }
6431
Bram Moolenaar4c137212021-04-19 16:48:48 +02006432 if (dfunc->df_has_closure)
6433 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006434 // Initialize the variable that counts how many closures were
6435 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006436 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6437 STACK_TV_VAR(idx)->vval.v_number = 0;
6438 ++ectx.ec_stack.ga_len;
6439 }
6440
6441 ectx.ec_instr = INSTRUCTIONS(dfunc);
6442 }
6443
Bram Moolenaar58779852022-09-06 18:31:14 +01006444 // Store the execution context in funccal, used by invoke_all_defer().
6445 if (funccal != NULL)
6446 funccal->fc_ectx = &ectx;
6447
Bram Moolenaar4c137212021-04-19 16:48:48 +02006448 // Following errors are in the function, not the caller.
6449 // Commands behave like vim9script.
6450 estack_push_ufunc(ufunc, 1);
6451 current_sctx = ufunc->uf_script_ctx;
6452 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6453
6454 // Use a specific location for storing error messages to be converted to an
6455 // exception.
6456 saved_msg_list = msg_list;
6457 msg_list = &private_msg_list;
6458
6459 // Do turn errors into exceptions.
6460 suppress_errthrow = FALSE;
6461
6462 // Do not delete the function while executing it.
6463 ++ufunc->uf_calls;
6464
6465 // When ":silent!" was used before calling then we still abort the
6466 // function. If ":silent!" is used in the function then we don't.
6467 emsg_silent_def = emsg_silent;
6468 did_emsg_def = 0;
6469
LemonBoyc5d27442023-08-19 13:02:35 +02006470 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006471
Bram Moolenaar5800c792022-09-22 17:34:01 +01006472 /*
6473 * Execute the instructions until done.
6474 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006475 ret = exec_instructions(&ectx);
6476 if (ret == OK)
6477 {
6478 // function finished, get result from the stack.
6479 if (ufunc->uf_ret_type == &t_void)
6480 {
6481 rettv->v_type = VAR_VOID;
6482 }
6483 else
6484 {
6485 tv = STACK_TV_BOT(-1);
6486 *rettv = *tv;
6487 tv->v_type = VAR_UNKNOWN;
6488 }
6489 }
6490
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006491 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006492 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006493
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006494 // Deal with any remaining closures, they may be in use somewhere.
6495 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006496 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006497 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006498 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006499 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006500
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006501 estack_pop();
6502 current_sctx = save_current_sctx;
6503
Bram Moolenaar2336c372021-12-06 15:06:54 +00006504 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6505 // Function was unreferenced while being used, free it now.
6506 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006507
Bram Moolenaar352134b2020-10-17 22:04:08 +02006508 if (*msg_list != NULL && saved_msg_list != NULL)
6509 {
6510 msglist_T **plist = saved_msg_list;
6511
6512 // Append entries from the current msg_list (uncaught exceptions) to
6513 // the saved msg_list.
6514 while (*plist != NULL)
6515 plist = &(*plist)->next;
6516
6517 *plist = *msg_list;
6518 }
6519 msg_list = saved_msg_list;
6520
Bram Moolenaar4c137212021-04-19 16:48:48 +02006521 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006522 {
6523 cmdmod.cmod_filter_regmatch.regprog = NULL;
6524 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006525 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006526 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006527 emsg_silent_def = save_emsg_silent_def;
6528 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006529
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006530failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006531 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006532 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006533 {
6534 tv = STACK_TV(idx);
6535 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6536 clear_tv(tv);
6537 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006538 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006541 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006542 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006543 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006544 if (ectx.ec_outer_ref->or_outer_allocated)
6545 vim_free(ectx.ec_outer_ref->or_outer);
6546 partial_unref(ectx.ec_outer_ref->or_partial);
6547 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006548 }
6549
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006550 // Not sure if this is necessary.
6551 suppress_errthrow = save_suppress_errthrow;
6552
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006553 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6554 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006555 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006556 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006557 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558 return ret;
6559}
6560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006561/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006562 * Called when a def function has finished (possibly failed).
6563 * Invoke all the function returns to clean up and invoke deferred functions,
6564 * except the toplevel one.
6565 */
6566 void
6567unwind_def_callstack(ectx_T *ectx)
6568{
6569 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6570 func_return(ectx);
6571}
6572
6573/*
dundargocc57b5bc2022-11-02 13:30:51 +00006574 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006575 */
6576 void
6577may_invoke_defer_funcs(ectx_T *ectx)
6578{
6579 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6580
6581 if (dfunc->df_defer_var_idx > 0)
6582 invoke_defer_funcs(ectx);
6583}
6584
6585/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006586 * Return loopvarinfo in a printable form in allocated memory.
6587 */
6588 static char_u *
6589printable_loopvarinfo(loopvarinfo_T *lvi)
6590{
6591 garray_T ga;
6592 int depth;
6593
6594 ga_init2(&ga, 1, 100);
6595 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6596 {
6597 if (ga_grow(&ga, 50) == FAIL)
6598 break;
6599 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006600 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006601 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006602 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006603 lvi->lvi_loop[depth].var_idx,
6604 lvi->lvi_loop[depth].var_idx
6605 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006606 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006607 }
6608 return ga.ga_data;
6609}
6610
6611/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006612 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6613 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6614 * "pfx" is prefixed to every line.
6615 */
6616 static void
6617list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6618{
6619 int line_idx = 0;
6620 int prev_current = 0;
6621 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006622 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006623
6624 for (current = 0; current < instr_count; ++current)
6625 {
6626 isn_T *iptr = &instr[current];
6627 char *line;
6628
6629 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006630 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006631 while (line_idx < iptr->isn_lnum
6632 && line_idx < ufunc->uf_lines.ga_len)
6633 {
6634 if (current > prev_current)
6635 {
6636 msg_puts("\n\n");
6637 prev_current = current;
6638 }
6639 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6640 if (line != NULL)
6641 msg(line);
6642 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006643 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6644 {
6645 int first_def_arg = ufunc->uf_args.ga_len
6646 - ufunc->uf_def_args.ga_len;
6647
6648 if (def_arg_idx > 0)
6649 msg_puts("\n\n");
6650 msg_start();
6651 msg_puts(" ");
6652 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6653 first_def_arg + def_arg_idx]);
6654 msg_puts(" = ");
6655 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6656 msg_clr_eos();
6657 msg_end();
6658 }
6659 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006660
6661 switch (iptr->isn_type)
6662 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006663 case ISN_CONSTRUCT:
6664 smsg("%s%4d NEW %s size %d", pfx, current,
6665 iptr->isn_arg.construct.construct_class->class_name,
6666 (int)iptr->isn_arg.construct.construct_size);
6667 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006668 case ISN_EXEC:
6669 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6670 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006671 case ISN_EXEC_SPLIT:
6672 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6673 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006674 case ISN_EXECRANGE:
6675 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6676 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006677 case ISN_LEGACY_EVAL:
6678 smsg("%s%4d EVAL legacy %s", pfx, current,
6679 iptr->isn_arg.string);
6680 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006681 case ISN_REDIRSTART:
6682 smsg("%s%4d REDIR", pfx, current);
6683 break;
6684 case ISN_REDIREND:
6685 smsg("%s%4d REDIR END%s", pfx, current,
6686 iptr->isn_arg.number ? " append" : "");
6687 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006688 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006689#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006690 smsg("%s%4d CEXPR pre %s", pfx, current,
6691 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006692#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006693 break;
6694 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006695#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006696 {
6697 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6698
6699 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6700 cexpr_get_auname(cer->cer_cmdidx),
6701 cer->cer_forceit ? "!" : "",
6702 cer->cer_cmdline);
6703 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006704#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006705 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006706 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006707 smsg("%s%4d INSTR", pfx, current);
6708 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6709 msg(" -------------");
6710 break;
6711 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006712 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006713 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6714
6715 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006716 }
6717 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006718 case ISN_SUBSTITUTE:
6719 {
6720 subs_T *subs = &iptr->isn_arg.subs;
6721
6722 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6723 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6724 msg(" -------------");
6725 }
6726 break;
6727 case ISN_EXECCONCAT:
6728 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6729 (varnumber_T)iptr->isn_arg.number);
6730 break;
6731 case ISN_ECHO:
6732 {
6733 echo_T *echo = &iptr->isn_arg.echo;
6734
6735 smsg("%s%4d %s %d", pfx, current,
6736 echo->echo_with_white ? "ECHO" : "ECHON",
6737 echo->echo_count);
6738 }
6739 break;
6740 case ISN_EXECUTE:
6741 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006742 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006743 break;
6744 case ISN_ECHOMSG:
6745 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006746 (varnumber_T)(iptr->isn_arg.number));
6747 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006748 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006749 if (iptr->isn_arg.echowin.ewin_time > 0)
6750 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6751 iptr->isn_arg.echowin.ewin_count,
6752 iptr->isn_arg.echowin.ewin_time);
6753 else
6754 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6755 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006756 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006757 case ISN_ECHOCONSOLE:
6758 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6759 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006760 break;
6761 case ISN_ECHOERR:
6762 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006763 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006764 break;
6765 case ISN_LOAD:
6766 {
6767 if (iptr->isn_arg.number < 0)
6768 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6769 (varnumber_T)(iptr->isn_arg.number
6770 + STACK_FRAME_SIZE));
6771 else
6772 smsg("%s%4d LOAD $%lld", pfx, current,
6773 (varnumber_T)(iptr->isn_arg.number));
6774 }
6775 break;
6776 case ISN_LOADOUTER:
6777 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006778 isn_outer_T *outer = &iptr->isn_arg.outer;
6779
6780 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006781 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006782 outer->outer_depth,
6783 outer->outer_idx + STACK_FRAME_SIZE);
6784 else if (outer->outer_depth < 0)
6785 smsg("%s%4d LOADOUTER $%d in loop level %d",
6786 pfx, current,
6787 outer->outer_idx,
6788 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006789 else
6790 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006791 outer->outer_depth,
6792 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006793 }
6794 break;
6795 case ISN_LOADV:
6796 smsg("%s%4d LOADV v:%s", pfx, current,
6797 get_vim_var_name(iptr->isn_arg.number));
6798 break;
6799 case ISN_LOADSCRIPT:
6800 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006801 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6802 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6803 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006804
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006805 sv = get_script_svar(sref, -1);
6806 if (sv == NULL)
6807 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6808 pfx, current, si->sn_name);
6809 else
6810 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006811 sv->sv_name,
6812 sref->sref_idx,
6813 si->sn_name);
6814 }
6815 break;
6816 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006817 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006818 {
6819 scriptitem_T *si = SCRIPT_ITEM(
6820 iptr->isn_arg.loadstore.ls_sid);
6821
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006822 smsg("%s%4d %s s:%s from %s", pfx, current,
6823 iptr->isn_type == ISN_LOADS ? "LOADS"
6824 : "LOADEXPORT",
6825 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006826 }
6827 break;
6828 case ISN_LOADAUTO:
6829 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6830 break;
6831 case ISN_LOADG:
6832 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6833 break;
6834 case ISN_LOADB:
6835 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6836 break;
6837 case ISN_LOADW:
6838 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6839 break;
6840 case ISN_LOADT:
6841 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6842 break;
6843 case ISN_LOADGDICT:
6844 smsg("%s%4d LOAD g:", pfx, current);
6845 break;
6846 case ISN_LOADBDICT:
6847 smsg("%s%4d LOAD b:", pfx, current);
6848 break;
6849 case ISN_LOADWDICT:
6850 smsg("%s%4d LOAD w:", pfx, current);
6851 break;
6852 case ISN_LOADTDICT:
6853 smsg("%s%4d LOAD t:", pfx, current);
6854 break;
6855 case ISN_LOADOPT:
6856 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6857 break;
6858 case ISN_LOADENV:
6859 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6860 break;
6861 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006862 smsg("%s%4d LOADREG @%c", pfx, current,
6863 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006864 break;
6865
6866 case ISN_STORE:
6867 if (iptr->isn_arg.number < 0)
6868 smsg("%s%4d STORE arg[%lld]", pfx, current,
6869 iptr->isn_arg.number + STACK_FRAME_SIZE);
6870 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006871 smsg("%s%4d STORE $%lld", pfx, current,
6872 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006873 break;
6874 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006875 {
6876 isn_outer_T *outer = &iptr->isn_arg.outer;
6877
6878 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6879 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6880 pfx, current, outer->outer_idx);
6881 else
6882 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6883 outer->outer_depth, outer->outer_idx);
6884 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006885 break;
6886 case ISN_STOREV:
6887 smsg("%s%4d STOREV v:%s", pfx, current,
6888 get_vim_var_name(iptr->isn_arg.number));
6889 break;
6890 case ISN_STOREAUTO:
6891 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6892 break;
6893 case ISN_STOREG:
6894 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6895 break;
6896 case ISN_STOREB:
6897 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6898 break;
6899 case ISN_STOREW:
6900 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6901 break;
6902 case ISN_STORET:
6903 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6904 break;
6905 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006906 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006907 {
6908 scriptitem_T *si = SCRIPT_ITEM(
6909 iptr->isn_arg.loadstore.ls_sid);
6910
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006911 smsg("%s%4d %s %s in %s", pfx, current,
6912 iptr->isn_type == ISN_STORES
6913 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006914 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6915 }
6916 break;
6917 case ISN_STORESCRIPT:
6918 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006919 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6920 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6921 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006922
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006923 sv = get_script_svar(sref, -1);
6924 if (sv == NULL)
6925 smsg("%s%4d STORESCRIPT [deleted] in %s",
6926 pfx, current, si->sn_name);
6927 else
6928 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006929 sv->sv_name,
6930 sref->sref_idx,
6931 si->sn_name);
6932 }
6933 break;
6934 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006935 case ISN_STOREFUNCOPT:
6936 smsg("%s%4d %s &%s", pfx, current,
6937 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006938 iptr->isn_arg.storeopt.so_name);
6939 break;
6940 case ISN_STOREENV:
6941 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6942 break;
6943 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006944 smsg("%s%4d STOREREG @%c", pfx, current,
6945 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006946 break;
6947 case ISN_STORENR:
6948 smsg("%s%4d STORE %lld in $%d", pfx, current,
6949 iptr->isn_arg.storenr.stnr_val,
6950 iptr->isn_arg.storenr.stnr_idx);
6951 break;
6952
6953 case ISN_STOREINDEX:
6954 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006955 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006956 break;
6957
6958 case ISN_STORERANGE:
6959 smsg("%s%4d STORERANGE", pfx, current);
6960 break;
6961
Bram Moolenaard505d172022-12-18 21:42:55 +00006962 case ISN_LOAD_CLASSMEMBER:
6963 case ISN_STORE_CLASSMEMBER:
6964 {
6965 class_T *cl = iptr->isn_arg.classmember.cm_class;
6966 int idx = iptr->isn_arg.classmember.cm_idx;
6967 ocmember_T *ocm = &cl->class_class_members[idx];
6968 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6969 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6970 ? "LOAD" : "STORE",
6971 cl->class_name, ocm->ocm_name);
6972 }
6973 break;
6974
Bram Moolenaar4c137212021-04-19 16:48:48 +02006975 // constants
6976 case ISN_PUSHNR:
6977 smsg("%s%4d PUSHNR %lld", pfx, current,
6978 (varnumber_T)(iptr->isn_arg.number));
6979 break;
6980 case ISN_PUSHBOOL:
6981 case ISN_PUSHSPEC:
6982 smsg("%s%4d PUSH %s", pfx, current,
6983 get_var_special_name(iptr->isn_arg.number));
6984 break;
6985 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006986 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006987 break;
6988 case ISN_PUSHS:
6989 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6990 break;
6991 case ISN_PUSHBLOB:
6992 {
6993 char_u *r;
6994 char_u numbuf[NUMBUFLEN];
6995 char_u *tofree;
6996
6997 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6998 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6999 vim_free(tofree);
7000 }
7001 break;
7002 case ISN_PUSHFUNC:
7003 {
7004 char *name = (char *)iptr->isn_arg.string;
7005
7006 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
7007 name == NULL ? "[none]" : name);
7008 }
7009 break;
7010 case ISN_PUSHCHANNEL:
7011#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00007012 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007013#endif
7014 break;
7015 case ISN_PUSHJOB:
7016#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00007017 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007018#endif
7019 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00007020 case ISN_PUSHOBJ:
7021 smsg("%s%4d PUSHOBJ null", pfx, current);
7022 break;
7023 case ISN_PUSHCLASS:
7024 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00007025 iptr->isn_arg.classarg == NULL ? "null"
7026 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00007027 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007028 case ISN_PUSHEXC:
7029 smsg("%s%4d PUSH v:exception", pfx, current);
7030 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00007031 case ISN_AUTOLOAD:
7032 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
7033 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007034 case ISN_UNLET:
7035 smsg("%s%4d UNLET%s %s", pfx, current,
7036 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
7037 iptr->isn_arg.unlet.ul_name);
7038 break;
7039 case ISN_UNLETENV:
7040 smsg("%s%4d UNLETENV%s $%s", pfx, current,
7041 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
7042 iptr->isn_arg.unlet.ul_name);
7043 break;
7044 case ISN_UNLETINDEX:
7045 smsg("%s%4d UNLETINDEX", pfx, current);
7046 break;
7047 case ISN_UNLETRANGE:
7048 smsg("%s%4d UNLETRANGE", pfx, current);
7049 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02007050 case ISN_LOCKUNLOCK:
7051 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
7052 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007053 case ISN_LOCKCONST:
7054 smsg("%s%4d LOCKCONST", pfx, current);
7055 break;
7056 case ISN_NEWLIST:
7057 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01007058 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007059 break;
7060 case ISN_NEWDICT:
7061 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01007062 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007063 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00007064 case ISN_NEWPARTIAL:
7065 smsg("%s%4d NEWPARTIAL", pfx, current);
7066 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007067
7068 // function call
7069 case ISN_BCALL:
7070 {
7071 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
7072
7073 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
7074 internal_func_name(cbfunc->cbf_idx),
7075 cbfunc->cbf_argcount);
7076 }
7077 break;
7078 case ISN_DCALL:
7079 {
7080 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
7081 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
7082 + cdfunc->cdf_idx;
7083
7084 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007085 printable_func_name(df->df_ufunc),
7086 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007087 }
7088 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00007089 case ISN_METHODCALL:
7090 {
7091 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
7092
7093 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
7094 mfunc->cmf_itf->class_name,
7095 mfunc->cmf_itf->class_obj_methods[
7096 mfunc->cmf_idx]->uf_name,
7097 mfunc->cmf_argcount);
7098 }
7099 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007100 case ISN_UCALL:
7101 {
7102 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
7103
7104 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
7105 cufunc->cuf_name, cufunc->cuf_argcount);
7106 }
7107 break;
7108 case ISN_PCALL:
7109 {
7110 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
7111
7112 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
7113 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
7114 }
7115 break;
7116 case ISN_PCALL_END:
7117 smsg("%s%4d PCALL end", pfx, current);
7118 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01007119 case ISN_DEFER:
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02007120 smsg("%s%4d DEFER %d args", pfx, current,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01007121 (int)iptr->isn_arg.defer.defer_argcount);
7122 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007123 case ISN_RETURN:
7124 smsg("%s%4d RETURN", pfx, current);
7125 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02007126 case ISN_RETURN_VOID:
7127 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007128 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007129 case ISN_RETURN_OBJECT:
7130 smsg("%s%4d RETURN object", pfx, current);
7131 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007132 case ISN_FUNCREF:
7133 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007134 funcref_T *funcref = &iptr->isn_arg.funcref;
7135 funcref_extra_T *extra = funcref->fr_extra;
7136 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007137
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007138 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00007139 {
7140 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
7141 + funcref->fr_dfunc_idx;
7142 name = df->df_ufunc->uf_name;
7143 }
7144 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007145 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00007146 if (extra != NULL && extra->fre_class != NULL)
7147 {
7148 smsg("%s%4d FUNCREF %s.%s", pfx, current,
7149 extra->fre_class->class_name, name);
7150 }
7151 else if (extra == NULL
7152 || extra->fre_loopvar_info.lvi_depth == 0)
7153 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007154 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00007155 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007156 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007157 {
7158 char_u *info = printable_loopvarinfo(
7159 &extra->fre_loopvar_info);
7160
7161 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
7162 name, info);
7163 vim_free(info);
7164 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007165 }
7166 break;
7167
7168 case ISN_NEWFUNC:
7169 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01007170 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007171
Bram Moolenaarcc341812022-09-19 15:54:34 +01007172 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007173 smsg("%s%4d NEWFUNC %s %s", pfx, current,
7174 arg->nfa_lambda, arg->nfa_global);
7175 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007176 {
7177 char_u *info = printable_loopvarinfo(
7178 &arg->nfa_loopvar_info);
7179
7180 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
7181 arg->nfa_lambda, arg->nfa_global, info);
7182 vim_free(info);
7183 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007184 }
7185 break;
7186
7187 case ISN_DEF:
7188 {
7189 char_u *name = iptr->isn_arg.string;
7190
7191 smsg("%s%4d DEF %s", pfx, current,
7192 name == NULL ? (char_u *)"" : name);
7193 }
7194 break;
7195
7196 case ISN_JUMP:
7197 {
7198 char *when = "?";
7199
7200 switch (iptr->isn_arg.jump.jump_when)
7201 {
7202 case JUMP_ALWAYS:
7203 when = "JUMP";
7204 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007205 case JUMP_NEVER:
7206 iemsg("JUMP_NEVER should not be used");
7207 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007208 case JUMP_AND_KEEP_IF_TRUE:
7209 when = "JUMP_AND_KEEP_IF_TRUE";
7210 break;
7211 case JUMP_IF_FALSE:
7212 when = "JUMP_IF_FALSE";
7213 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007214 case JUMP_WHILE_FALSE:
7215 when = "JUMP_WHILE_FALSE"; // unused
7216 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007217 case JUMP_IF_COND_FALSE:
7218 when = "JUMP_IF_COND_FALSE";
7219 break;
7220 case JUMP_IF_COND_TRUE:
7221 when = "JUMP_IF_COND_TRUE";
7222 break;
7223 }
7224 smsg("%s%4d %s -> %d", pfx, current, when,
7225 iptr->isn_arg.jump.jump_where);
7226 }
7227 break;
7228
7229 case ISN_JUMP_IF_ARG_SET:
7230 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7231 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7232 iptr->isn_arg.jump.jump_where);
7233 break;
7234
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007235 case ISN_JUMP_IF_ARG_NOT_SET:
7236 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7237 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7238 iptr->isn_arg.jump.jump_where);
7239 break;
7240
Bram Moolenaar4c137212021-04-19 16:48:48 +02007241 case ISN_FOR:
7242 {
7243 forloop_T *forloop = &iptr->isn_arg.forloop;
7244
7245 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007246 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007247 }
7248 break;
7249
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007250 case ISN_ENDLOOP:
7251 {
7252 endloop_T *endloop = &iptr->isn_arg.endloop;
7253
Bram Moolenaarcc341812022-09-19 15:54:34 +01007254 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7255 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007256 endloop->end_funcref_idx,
7257 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007258 endloop->end_var_idx + endloop->end_var_count - 1,
7259 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007260 }
7261 break;
7262
7263 case ISN_WHILE:
7264 {
7265 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7266
7267 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7268 whileloop->while_funcref_idx,
7269 whileloop->while_end);
7270 }
7271 break;
7272
Bram Moolenaar4c137212021-04-19 16:48:48 +02007273 case ISN_TRY:
7274 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007275 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007276
7277 if (try->try_ref->try_finally == 0)
7278 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7279 pfx, current,
7280 try->try_ref->try_catch,
7281 try->try_ref->try_endtry);
7282 else
7283 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7284 pfx, current,
7285 try->try_ref->try_catch,
7286 try->try_ref->try_finally,
7287 try->try_ref->try_endtry);
7288 }
7289 break;
7290 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007291 smsg("%s%4d CATCH", pfx, current);
7292 break;
7293 case ISN_TRYCONT:
7294 {
7295 trycont_T *trycont = &iptr->isn_arg.trycont;
7296
7297 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7298 trycont->tct_levels,
7299 trycont->tct_levels == 1 ? "" : "s",
7300 trycont->tct_where);
7301 }
7302 break;
7303 case ISN_FINALLY:
7304 smsg("%s%4d FINALLY", pfx, current);
7305 break;
7306 case ISN_ENDTRY:
7307 smsg("%s%4d ENDTRY", pfx, current);
7308 break;
7309 case ISN_THROW:
7310 smsg("%s%4d THROW", pfx, current);
7311 break;
7312
7313 // expression operations on number
7314 case ISN_OPNR:
7315 case ISN_OPFLOAT:
7316 case ISN_OPANY:
7317 {
7318 char *what;
7319 char *ins;
7320
7321 switch (iptr->isn_arg.op.op_type)
7322 {
7323 case EXPR_MULT: what = "*"; break;
7324 case EXPR_DIV: what = "/"; break;
7325 case EXPR_REM: what = "%"; break;
7326 case EXPR_SUB: what = "-"; break;
7327 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007328 case EXPR_LSHIFT: what = "<<"; break;
7329 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007330 default: what = "???"; break;
7331 }
7332 switch (iptr->isn_type)
7333 {
7334 case ISN_OPNR: ins = "OPNR"; break;
7335 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7336 case ISN_OPANY: ins = "OPANY"; break;
7337 default: ins = "???"; break;
7338 }
7339 smsg("%s%4d %s %s", pfx, current, ins, what);
7340 }
7341 break;
7342
7343 case ISN_COMPAREBOOL:
7344 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007345 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007346 case ISN_COMPARENR:
7347 case ISN_COMPAREFLOAT:
7348 case ISN_COMPARESTRING:
7349 case ISN_COMPAREBLOB:
7350 case ISN_COMPARELIST:
7351 case ISN_COMPAREDICT:
7352 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007353 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007354 case ISN_COMPAREANY:
7355 {
7356 char *p;
7357 char buf[10];
7358 char *type;
7359
7360 switch (iptr->isn_arg.op.op_type)
7361 {
7362 case EXPR_EQUAL: p = "=="; break;
7363 case EXPR_NEQUAL: p = "!="; break;
7364 case EXPR_GREATER: p = ">"; break;
7365 case EXPR_GEQUAL: p = ">="; break;
7366 case EXPR_SMALLER: p = "<"; break;
7367 case EXPR_SEQUAL: p = "<="; break;
7368 case EXPR_MATCH: p = "=~"; break;
7369 case EXPR_IS: p = "is"; break;
7370 case EXPR_ISNOT: p = "isnot"; break;
7371 case EXPR_NOMATCH: p = "!~"; break;
7372 default: p = "???"; break;
7373 }
7374 STRCPY(buf, p);
7375 if (iptr->isn_arg.op.op_ic == TRUE)
7376 strcat(buf, "?");
7377 switch(iptr->isn_type)
7378 {
7379 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7380 case ISN_COMPARESPECIAL:
7381 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007382 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007383 case ISN_COMPARENR: type = "COMPARENR"; break;
7384 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7385 case ISN_COMPARESTRING:
7386 type = "COMPARESTRING"; break;
7387 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7388 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7389 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7390 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007391 case ISN_COMPAREOBJECT:
7392 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007393 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7394 default: type = "???"; break;
7395 }
7396
7397 smsg("%s%4d %s %s", pfx, current, type, buf);
7398 }
7399 break;
7400
7401 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7402 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7403
7404 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007405 case ISN_CONCAT:
7406 smsg("%s%4d CONCAT size %lld", pfx, current,
7407 (varnumber_T)(iptr->isn_arg.number));
7408 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007409 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7410 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7411 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7412 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7413 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7414 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7415 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7416 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7417 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7418 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7419 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007420 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007421 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7422 iptr->isn_arg.getitem.gi_index,
7423 iptr->isn_arg.getitem.gi_with_op ?
7424 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007425 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7426 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7427 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007428 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7429 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007430 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007431 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007432 pfx, current,
7433 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007434 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007435 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007436 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007437 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007438 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7439 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7440
Bram Moolenaar4c137212021-04-19 16:48:48 +02007441 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7442
Bram Moolenaar4c137212021-04-19 16:48:48 +02007443 case ISN_CHECKTYPE:
7444 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007445 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007446 char *tofree = NULL;
7447 char *typename;
7448
7449 if (ct->ct_type->tt_type == VAR_FLOAT
7450 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7451 typename = "float|number";
7452 else
7453 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007454
7455 if (ct->ct_arg_idx == 0)
7456 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007457 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007458 (int)ct->ct_off);
7459 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007460 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007461 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007462 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007463 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007464 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007465 (int)ct->ct_arg_idx);
7466 vim_free(tofree);
7467 break;
7468 }
7469 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7470 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7471 iptr->isn_arg.checklen.cl_min_len);
7472 break;
7473 case ISN_SETTYPE:
7474 {
7475 char *tofree;
7476
7477 smsg("%s%4d SETTYPE %s", pfx, current,
7478 type_name(iptr->isn_arg.type.ct_type, &tofree));
7479 vim_free(tofree);
7480 break;
7481 }
7482 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007483 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7484 smsg("%s%4d INVERT %d (!val)", pfx, current,
7485 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007486 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007487 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7488 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007489 break;
7490 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007491 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007492 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007493 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7494 pfx, current,
7495 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007496 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007497 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7498 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007499 break;
7500 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007501 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007502 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007503 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007504 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7505 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007506 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007507 else
7508 smsg("%s%4d PUT %c %ld", pfx, current,
7509 iptr->isn_arg.put.put_regname,
7510 (long)iptr->isn_arg.put.put_lnum);
7511 break;
7512
Bram Moolenaar4c137212021-04-19 16:48:48 +02007513 case ISN_CMDMOD:
7514 {
7515 char_u *buf;
7516 size_t len = produce_cmdmods(
7517 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7518
7519 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007520 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007521 {
7522 (void)produce_cmdmods(
7523 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7524 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7525 vim_free(buf);
7526 }
7527 break;
7528 }
7529 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7530
7531 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007532 smsg("%s%4d PROFILE START line %d", pfx, current,
7533 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007534 break;
7535
7536 case ISN_PROF_END:
7537 smsg("%s%4d PROFILE END", pfx, current);
7538 break;
7539
Bram Moolenaare99d4222021-06-13 14:01:26 +02007540 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007541 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7542 iptr->isn_arg.debug.dbg_break_lnum + 1,
7543 iptr->isn_lnum,
7544 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007545 break;
7546
Bram Moolenaar4c137212021-04-19 16:48:48 +02007547 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7548 iptr->isn_arg.unpack.unp_count,
7549 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7550 break;
7551 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7552 iptr->isn_arg.shuffle.shfl_item,
7553 iptr->isn_arg.shuffle.shfl_up);
7554 break;
7555 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7556
7557 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7558 return;
7559 }
7560
7561 out_flush(); // output one line at a time
7562 ui_breakcheck();
7563 if (got_int)
7564 break;
7565 }
7566}
7567
7568/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007569 * Handle command line completion for the :disassemble command.
7570 */
7571 void
7572set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7573{
7574 char_u *p;
7575
7576 // Default: expand user functions, "debug" and "profile"
7577 xp->xp_context = EXPAND_DISASSEMBLE;
7578 xp->xp_pattern = arg;
7579
7580 // first argument already typed: only user function names
7581 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7582 {
7583 xp->xp_context = EXPAND_USER_FUNC;
7584 xp->xp_pattern = skipwhite(p);
7585 }
7586}
7587
7588/*
7589 * Function given to ExpandGeneric() to obtain the list of :disassemble
7590 * arguments.
7591 */
7592 char_u *
7593get_disassemble_argument(expand_T *xp, int idx)
7594{
7595 if (idx == 0)
7596 return (char_u *)"debug";
7597 if (idx == 1)
7598 return (char_u *)"profile";
7599 return get_user_func_name(xp, idx - 2);
7600}
7601
7602/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007603 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007604 * We don't really need this at runtime, but we do have tests that require it,
7605 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007606 */
7607 void
7608ex_disassemble(exarg_T *eap)
7609{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007610 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007611 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007612 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007613 isn_T *instr = NULL; // init to shut up gcc warning
7614 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007615 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007616
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007617 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007618 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007619 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007620 if (func_needs_compiling(ufunc, compile_type)
7621 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007622 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007623 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007625 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 return;
7627 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007628 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629
7630 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007631 switch (compile_type)
7632 {
7633 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007634#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007635 instr = dfunc->df_instr_prof;
7636 instr_count = dfunc->df_instr_prof_count;
7637 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007638#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007639 // FALLTHROUGH
7640 case CT_NONE:
7641 instr = dfunc->df_instr;
7642 instr_count = dfunc->df_instr_count;
7643 break;
7644 case CT_DEBUG:
7645 instr = dfunc->df_instr_debug;
7646 instr_count = dfunc->df_instr_debug_count;
7647 break;
7648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649
Bram Moolenaar4c137212021-04-19 16:48:48 +02007650 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651}
7652
7653/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007654 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007655 * list, etc. Mostly like what JavaScript does, except that empty list and
7656 * empty dictionary are FALSE.
7657 */
7658 int
7659tv2bool(typval_T *tv)
7660{
7661 switch (tv->v_type)
7662 {
7663 case VAR_NUMBER:
7664 return tv->vval.v_number != 0;
7665 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007666 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667 case VAR_PARTIAL:
7668 return tv->vval.v_partial != NULL;
7669 case VAR_FUNC:
7670 case VAR_STRING:
7671 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7672 case VAR_LIST:
7673 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7674 case VAR_DICT:
7675 return tv->vval.v_dict != NULL
7676 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7677 case VAR_BOOL:
7678 case VAR_SPECIAL:
7679 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7680 case VAR_JOB:
7681#ifdef FEAT_JOB_CHANNEL
7682 return tv->vval.v_job != NULL;
7683#else
7684 break;
7685#endif
7686 case VAR_CHANNEL:
7687#ifdef FEAT_JOB_CHANNEL
7688 return tv->vval.v_channel != NULL;
7689#else
7690 break;
7691#endif
7692 case VAR_BLOB:
7693 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7694 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007695 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007696 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007697 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007698 case VAR_CLASS:
7699 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007700 case VAR_TYPEALIAS:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007701 break;
7702 }
7703 return FALSE;
7704}
7705
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007706 void
7707emsg_using_string_as(typval_T *tv, int as_number)
7708{
7709 semsg(_(as_number ? e_using_string_as_number_str
7710 : e_using_string_as_bool_str),
7711 tv->vval.v_string == NULL
7712 ? (char_u *)"" : tv->vval.v_string);
7713}
7714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715/*
7716 * If "tv" is a string give an error and return FAIL.
7717 */
7718 int
7719check_not_string(typval_T *tv)
7720{
7721 if (tv->v_type == VAR_STRING)
7722 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007723 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007724 clear_tv(tv);
7725 return FAIL;
7726 }
7727 return OK;
7728}
7729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007730
7731#endif // FEAT_EVAL