blob: d8087bf08dc877150208918f8594c9659549804e [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010067// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100104// Get pointer to item in the stack.
105#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200108#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100110// Get pointer to a local variable on the stack. Negative for arguments.
111#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
112
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200113 void
114to_string_error(vartype_T vartype)
115{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200116 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200117}
118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100120 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 */
122 static int
123ufunc_argcount(ufunc_T *ufunc)
124{
125 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
126}
127
128/*
LemonBoy372bcce2022-04-25 12:43:20 +0100129 * Create a new string from "count" items at the bottom of the stack.
130 * A trailing NUL is appended.
131 * When "count" is zero an empty string is added to the stack.
132 */
133 static int
134exe_concat(int count, ectx_T *ectx)
135{
136 int idx;
137 int len = 0;
138 typval_T *tv;
139 garray_T ga;
140
141 ga_init2(&ga, sizeof(char), 1);
142 // Preallocate enough space for the whole string to avoid having to grow
143 // and copy.
144 for (idx = 0; idx < count; ++idx)
145 {
146 tv = STACK_TV_BOT(idx - count);
147 if (tv->vval.v_string != NULL)
148 len += (int)STRLEN(tv->vval.v_string);
149 }
150
151 if (ga_grow(&ga, len + 1) == FAIL)
152 return FAIL;
153
154 for (idx = 0; idx < count; ++idx)
155 {
156 tv = STACK_TV_BOT(idx - count);
157 ga_concat(&ga, tv->vval.v_string);
158 clear_tv(tv);
159 }
160
161 // add a terminating NUL
162 ga_append(&ga, NUL);
163
164 ectx->ec_stack.ga_len -= count - 1;
165 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
166
167 return OK;
168}
169
170/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200171 * Create a new list from "count" items at the bottom of the stack.
172 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100173 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200174 */
175 static int
176exe_newlist(int count, ectx_T *ectx)
177{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 int idx;
180 typval_T *tv;
181
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100182 if (count >= 0)
183 {
184 list = list_alloc_with_items(count);
185 if (list == NULL)
186 return FAIL;
187 for (idx = 0; idx < count; ++idx)
188 list_set_item(list, idx, STACK_TV_BOT(idx - count));
189 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190
191 if (count > 0)
192 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200193 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100194 {
195 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 else
199 ++ectx->ec_stack.ga_len;
200 tv = STACK_TV_BOT(-1);
201 tv->v_type = VAR_LIST;
202 tv->vval.v_list = list;
Bram Moolenaar566badc2022-09-18 13:46:08 +0100203 tv->v_lock = 0;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100204 if (list != NULL)
205 ++list->lv_refcount;
206 return OK;
207}
208
209/*
210 * Implementation of ISN_NEWDICT.
211 * Returns FAIL on total failure, MAYBE on error.
212 */
213 static int
214exe_newdict(int count, ectx_T *ectx)
215{
216 dict_T *dict = NULL;
217 dictitem_T *item;
218 char_u *key;
219 int idx;
220 typval_T *tv;
221
222 if (count >= 0)
223 {
224 dict = dict_alloc();
225 if (unlikely(dict == NULL))
226 return FAIL;
227 for (idx = 0; idx < count; ++idx)
228 {
229 // have already checked key type is VAR_STRING
230 tv = STACK_TV_BOT(2 * (idx - count));
231 // check key is unique
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000232 key = tv->vval.v_string == NULL ? (char_u *)"" : tv->vval.v_string;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100233 item = dict_find(dict, key, -1);
234 if (item != NULL)
235 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000236 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100237 dict_unref(dict);
238 return MAYBE;
239 }
240 item = dictitem_alloc(key);
241 clear_tv(tv);
242 if (unlikely(item == NULL))
243 {
244 dict_unref(dict);
245 return FAIL;
246 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100247 tv = STACK_TV_BOT(2 * (idx - count) + 1);
248 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100249 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100250 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100251 if (dict_add(dict, item) == FAIL)
252 {
253 // can this ever happen?
254 dict_unref(dict);
255 return FAIL;
256 }
257 }
258 }
259
260 if (count > 0)
261 ectx->ec_stack.ga_len -= 2 * count - 1;
262 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
263 return FAIL;
264 else
265 ++ectx->ec_stack.ga_len;
266 tv = STACK_TV_BOT(-1);
267 tv->v_type = VAR_DICT;
268 tv->v_lock = 0;
269 tv->vval.v_dict = dict;
270 if (dict != NULL)
271 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200272 return OK;
273}
274
275/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200276 * If debug_tick changed check if "ufunc" has a breakpoint and update
277 * "uf_has_breakpoint".
278 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000279 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200280update_has_breakpoint(ufunc_T *ufunc)
281{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000282 if (ufunc->uf_debug_tick == debug_tick)
283 return;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200284
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000285 linenr_T breakpoint;
286
287 ufunc->uf_debug_tick = debug_tick;
288 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
289 ufunc->uf_has_breakpoint = breakpoint > 0;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200290}
291
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200292static garray_T dict_stack = GA_EMPTY;
293
294/*
295 * Put a value on the dict stack. This consumes "tv".
296 */
297 static int
298dict_stack_save(typval_T *tv)
299{
300 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000301 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200302 if (ga_grow(&dict_stack, 1) == FAIL)
303 return FAIL;
304 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
305 ++dict_stack.ga_len;
306 return OK;
307}
308
309/*
310 * Get the typval at top of the dict stack.
311 */
312 static typval_T *
313dict_stack_get_tv(void)
314{
315 if (dict_stack.ga_len == 0)
316 return NULL;
317 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
318}
319
320/*
321 * Get the dict at top of the dict stack.
322 */
323 static dict_T *
324dict_stack_get_dict(void)
325{
326 typval_T *tv;
327
328 if (dict_stack.ga_len == 0)
329 return NULL;
330 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
331 if (tv->v_type == VAR_DICT)
332 return tv->vval.v_dict;
333 return NULL;
334}
335
336/*
337 * Drop an item from the dict stack.
338 */
339 static void
340dict_stack_drop(void)
341{
342 if (dict_stack.ga_len == 0)
343 {
344 iemsg("Dict stack underflow");
345 return;
346 }
347 --dict_stack.ga_len;
348 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
349}
350
351/*
352 * Drop items from the dict stack until the length is equal to "len".
353 */
354 static void
355dict_stack_clear(int len)
356{
357 while (dict_stack.ga_len > len)
358 dict_stack_drop();
359}
360
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200361/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000362 * Get a pointer to useful "pt_outer" of "pt".
363 */
364 static outer_T *
365get_pt_outer(partial_T *pt)
366{
367 partial_T *ptref = pt->pt_outer_partial;
368
369 if (ptref == NULL)
370 return &pt->pt_outer;
371
372 // partial using partial (recursively)
373 while (ptref->pt_outer_partial != NULL)
374 ptref = ptref->pt_outer_partial;
375 return &ptref->pt_outer;
376}
377
378/*
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000379 * Check "argcount" arguments on the stack against what "ufunc" expects.
380 * "off" is the offset of arguments on the stack.
381 * Return OK or FAIL.
382 */
383 static int
384check_ufunc_arg_types(ufunc_T *ufunc, int argcount, int off, ectx_T *ectx)
385{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000386 if (ufunc->uf_arg_types == NULL && ufunc->uf_va_type == NULL)
387 return OK;
388
389 typval_T *argv = STACK_TV_BOT(0) - argcount - off;
390
391 // The function can change at runtime, check that the argument
392 // types are correct.
393 for (int i = 0; i < argcount; ++i)
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000394 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000395 type_T *type = NULL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000396
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000397 // assume a v:none argument, using the default value, is always OK
398 if (argv[i].v_type == VAR_SPECIAL
399 && argv[i].vval.v_number == VVAL_NONE)
400 continue;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000401
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000402 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
403 type = ufunc->uf_arg_types[i];
404 else if (ufunc->uf_va_type != NULL)
405 type = ufunc->uf_va_type->tt_member;
406 if (type != NULL && check_typval_arg_type(type,
407 &argv[i], NULL, i + 1) == FAIL)
408 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000409 }
410 return OK;
411}
412
413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100415 * This adds a stack frame and sets the instruction pointer to the start of the
416 * called function.
Bram Moolenaar5800c792022-09-22 17:34:01 +0100417 * If "pt_arg" is not NULL use "pt_arg->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 *
419 * Stack has:
420 * - current arguments (already there)
421 * - omitted optional argument (default values) added here
422 * - stack frame:
423 * - pointer to calling function
424 * - Index of next instruction in calling function
425 * - previous frame pointer
426 * - reserved space for local variables
427 */
428 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100429call_dfunc(
430 int cdf_idx,
Bram Moolenaar5800c792022-09-22 17:34:01 +0100431 partial_T *pt_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100432 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100433 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100435 int argcount = argcount_arg;
436 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
437 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200438 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100439 int arg_to_add;
440 int vararg_count = 0;
441 int varcount;
442 int idx;
443 estack_T *entry;
444 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200445 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000446 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100447
448 if (dfunc->df_deleted)
449 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100450 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000451 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100452 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 return FAIL;
454 }
455
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100456#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100457 if (do_profiling == PROF_YES)
458 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200459 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100460 {
461 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
462 + profile_info_ga.ga_len;
463 ++profile_info_ga.ga_len;
464 CLEAR_POINTER(info);
465 profile_may_start_func(info, ufunc,
466 (((dfunc_T *)def_functions.ga_data)
467 + ectx->ec_dfunc_idx)->df_ufunc);
468 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100469 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100470#endif
471
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200472 // When debugging and using "cont" switches to the not-debugged
473 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000474 compile_type = get_compile_type(ufunc);
475 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200476 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000477 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200478
479 // compile_def_function() may cause def_functions.ga_data to change
480 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
481 }
482 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200483 {
484 if (did_emsg_cumul + did_emsg == did_emsg_before)
485 semsg(_(e_function_is_not_compiled_str),
486 printable_func_name(ufunc));
487 return FAIL;
488 }
489
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200490 if (ufunc->uf_va_name != NULL)
491 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200492 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200493 // Stack at time of call with 2 varargs:
494 // normal_arg
495 // optional_arg
496 // vararg_1
497 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200498 // After creating the list:
499 // normal_arg
500 // optional_arg
501 // vararg-list
502 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200503 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200504 // After creating the list
505 // normal_arg
506 // (space for optional_arg)
507 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200508 vararg_count = argcount - ufunc->uf_args.ga_len;
509 if (vararg_count < 0)
510 vararg_count = 0;
511 else
512 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200513 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200514 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200515
516 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200517 }
518
Bram Moolenaarfe270812020-04-11 22:31:27 +0200519 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200520 if (arg_to_add < 0)
521 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100522 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
523 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200524 return FAIL;
525 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000526 else if (arg_to_add > ufunc->uf_def_args.ga_len)
527 {
528 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
529
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100530 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
531 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000532 return FAIL;
533 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200534
Bram Moolenaar574950d2023-01-03 19:08:50 +0000535 // If this is an object method, the object is just before the arguments.
536 typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
537
Yegappan Lakshmananc1946262023-09-25 21:00:46 +0200538 if (IS_OBJECT_METHOD(ufunc) && !IS_CONSTRUCTOR_METHOD(ufunc)
539 && obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL)
540 {
541 // If this is not a constructor method, then a valid object is
542 // needed.
543 emsg(_(e_using_null_object));
544 return FAIL;
545 }
546
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000547 // Check the argument types.
548 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
549 return FAIL;
550
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200551 // Reserve space for:
552 // - missing arguments
553 // - stack frame
554 // - local variables
555 // - if needed: a counter for number of closures created in
556 // ectx->ec_funcrefs.
557 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100558 if (GA_GROW_FAILS(&ectx->ec_stack,
559 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 return FAIL;
561
Yegappan Lakshmanan1087b8c2023-10-07 22:03:18 +0200562 // The object pointer is in the execution typval stack. The GA_GROW call
563 // above may have reallocated the execution typval stack. So the object
564 // pointer may not be valid anymore. Get the object pointer again from the
565 // execution stack.
566 obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
567
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100568 // If depth of calling is getting too high, don't execute the function.
569 if (funcdepth_increment() == FAIL)
570 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200571 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100572
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100573 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200574 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100575 {
576 floc = ALLOC_ONE(funclocal_T);
577 if (floc == NULL)
578 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200579 *floc = ectx->ec_funclocal;
580 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100581 }
582
Bram Moolenaarfe270812020-04-11 22:31:27 +0200583 // Move the vararg-list to below the missing optional arguments.
584 if (vararg_count > 0 && arg_to_add > 0)
585 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100586
587 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200588 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200589 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200590 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591
592 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100593 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
594 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200595 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200596 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
597 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100598 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100599 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200600 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601
Bram Moolenaar5800c792022-09-22 17:34:01 +0100602 // Initialize all local variables to number zero. Also initialize the
603 // variable that counts how many closures were created. This is used in
604 // handle_closure_in_use().
605 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
606 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000607 {
608 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
609
610 tv->v_type = VAR_NUMBER;
611 tv->vval.v_number = 0;
612 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200613 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614
Bram Moolenaar574950d2023-01-03 19:08:50 +0000615 // For an object method move the object from just before the arguments to
616 // the first local variable.
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +0200617 if (IS_OBJECT_METHOD(ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +0000618 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200619 if (obj->v_type != VAR_OBJECT)
620 {
621 semsg(_(e_internal_error_str), "type in stack is not an object");
622 return FAIL;
623 }
624
Bram Moolenaar574950d2023-01-03 19:08:50 +0000625 *STACK_TV_VAR(0) = *obj;
626 obj->v_type = VAR_UNKNOWN;
627 }
628
Bram Moolenaar5800c792022-09-22 17:34:01 +0100629 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
630 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100631 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200632 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100633
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200634 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100635 return FAIL;
636 if (pt != NULL)
637 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000638 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200639 ++pt->pt_refcount;
640 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100641 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100642 else
643 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200644 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200645 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200646 {
647 vim_free(ref);
648 return FAIL;
649 }
650 ref->or_outer_allocated = TRUE;
651 ref->or_outer->out_stack = &ectx->ec_stack;
652 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
653 if (ectx->ec_outer_ref != NULL)
654 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100655 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200656 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100657 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100658 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200659 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100660
Bram Moolenaarc970e422021-03-17 15:03:04 +0100661 ++ufunc->uf_calls;
662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663 // Set execution state to the start of the called function.
664 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100665 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100666 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200667 if (entry != NULL)
668 {
669 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200670 // Save the current context so it can be restored on return.
671 entry->es_save_sctx = current_sctx;
672 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200673 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100674
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200675 // Start execution at the first instruction.
676 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677
678 return OK;
679}
680
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000681// Double linked list of funcstack_T in use.
682static funcstack_T *first_funcstack = NULL;
683
684 static void
685add_funcstack_to_list(funcstack_T *funcstack)
686{
687 // Link in list of funcstacks.
688 if (first_funcstack != NULL)
689 first_funcstack->fs_prev = funcstack;
690 funcstack->fs_next = first_funcstack;
691 funcstack->fs_prev = NULL;
692 first_funcstack = funcstack;
693}
694
695 static void
696remove_funcstack_from_list(funcstack_T *funcstack)
697{
698 if (funcstack->fs_prev == NULL)
699 first_funcstack = funcstack->fs_next;
700 else
701 funcstack->fs_prev->fs_next = funcstack->fs_next;
702 if (funcstack->fs_next != NULL)
703 funcstack->fs_next->fs_prev = funcstack->fs_prev;
704}
705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200707 * Used when returning from a function: Check if any closure is still
708 * referenced. If so then move the arguments and variables to a separate piece
709 * of stack to be used when the closure is called.
710 * When "free_arguments" is TRUE the arguments are to be freed.
711 * Returns FAIL when out of memory.
712 */
713 static int
714handle_closure_in_use(ectx_T *ectx, int free_arguments)
715{
716 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
717 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200718 int argcount;
719 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 int idx;
721 typval_T *tv;
722 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200723 garray_T *gap = &ectx->ec_funcrefs;
724 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200725
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200726 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200727 return OK; // function was freed
728 if (dfunc->df_has_closure == 0)
729 return OK; // no closures
730 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
731 closure_count = tv->vval.v_number;
732 if (closure_count == 0)
733 return OK; // no funcrefs created
734
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100735 // Compute "top": the first entry in the stack used by the function.
736 // This is the first argument (after that comes the stack frame and then
737 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200738 argcount = ufunc_argcount(dfunc->df_ufunc);
739 top = ectx->ec_frame_idx - argcount;
740
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200741 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200742 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200743 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200744 partial_T *pt;
745 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200746
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200747 if (off < 0)
748 continue; // count is off or already done
749 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200750 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200751 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200752 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200753 int i;
754
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000755 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200756 // unreferenced on return.
757 for (i = 0; i < dfunc->df_varcount; ++i)
758 {
759 typval_T *stv = STACK_TV(ectx->ec_frame_idx
760 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200761 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200762 --refcount;
763 }
764 if (refcount > 1)
765 {
766 closure_in_use = TRUE;
767 break;
768 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200769 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200770 }
771
772 if (closure_in_use)
773 {
774 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
775 typval_T *stack;
776
777 // A closure is using the arguments and/or local variables.
778 // Move them to the called function.
779 if (funcstack == NULL)
780 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000781
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200782 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100783 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
784 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200785 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
786 funcstack->fs_ga.ga_data = stack;
787 if (stack == NULL)
788 {
789 vim_free(funcstack);
790 return FAIL;
791 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000792 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200793
794 // Move or copy the arguments.
795 for (idx = 0; idx < argcount; ++idx)
796 {
797 tv = STACK_TV(top + idx);
798 if (free_arguments)
799 {
800 *(stack + idx) = *tv;
801 tv->v_type = VAR_UNKNOWN;
802 }
803 else
804 copy_tv(tv, stack + idx);
805 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100806 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200807 // Move the local variables.
808 for (idx = 0; idx < dfunc->df_varcount; ++idx)
809 {
810 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200811
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200812 // A partial created for a local function, that is also used as a
813 // local variable, has a reference count for the variable, thus
814 // will never go down to zero. When all these refcounts are one
815 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000816 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200817 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
818 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200819 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200820
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200821 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200822 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
823 gap->ga_len - closure_count + i])
824 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200825 }
826
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200827 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200828 tv->v_type = VAR_UNKNOWN;
829 }
830
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200831 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200832 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200833 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
834 - closure_count + idx];
835 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200836 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200837 ++funcstack->fs_refcount;
838 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100839 pt->pt_outer.out_stack = &funcstack->fs_ga;
840 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200841 }
842 }
843 }
844
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200845 for (idx = 0; idx < closure_count; ++idx)
846 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
847 - closure_count + idx]);
848 gap->ga_len -= closure_count;
849 if (gap->ga_len == 0)
850 ga_clear(gap);
851
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200852 return OK;
853}
854
855/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200856 * Called when a partial is freed or its reference count goes down to one. The
857 * funcstack may be the only reference to the partials in the local variables.
858 * Go over all of them, the funcref and can be freed if all partials
859 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100860 * Returns TRUE if the funcstack is freed, the partial referencing it will then
861 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200862 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100863 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200864funcstack_check_refcount(funcstack_T *funcstack)
865{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100866 int i;
867 garray_T *gap = &funcstack->fs_ga;
868 int done = 0;
869 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200870
871 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100872 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200873 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
874 {
875 typval_T *tv = ((typval_T *)gap->ga_data) + i;
876
877 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
878 && tv->vval.v_partial->pt_funcstack == funcstack
879 && tv->vval.v_partial->pt_refcount == 1)
880 ++done;
881 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100882 if (done != funcstack->fs_min_refcount)
883 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200884
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100885 stack = gap->ga_data;
886
887 // All partials referencing the funcstack have a reference count of
888 // one, thus the funcstack is no longer of use.
889 for (i = 0; i < gap->ga_len; ++i)
890 clear_tv(stack + i);
891 vim_free(stack);
892 remove_funcstack_from_list(funcstack);
893 vim_free(funcstack);
894
895 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200896}
897
898/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000899 * For garbage collecting: set references in all variables referenced by
900 * all funcstacks.
901 */
902 int
903set_ref_in_funcstacks(int copyID)
904{
905 funcstack_T *funcstack;
906
907 for (funcstack = first_funcstack; funcstack != NULL;
908 funcstack = funcstack->fs_next)
909 {
910 typval_T *stack = funcstack->fs_ga.ga_data;
911 int i;
912
913 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
914 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
915 return TRUE; // abort
916 }
917 return FALSE;
918}
919
Bram Moolenaar806a2732022-09-04 15:40:36 +0100920// Ugly static to avoid passing the execution context around through many
921// layers.
922static ectx_T *current_ectx = NULL;
923
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000924/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100925 * Return TRUE if currently executing a :def function.
926 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100927 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100928 int
929in_def_function(void)
930{
931 return current_ectx != NULL;
932}
933
934/*
Ernie Rael4c8da022023-10-11 21:35:11 +0200935 * If executing a class/object method, then fill in the lval_T.
936 * Set lr_tv to the executing item, and lr_exec_class to the executing class;
937 * use free_tv and class_unref when finished with the lval_root.
938 * For use by builtin functions.
939 *
940 * Return FAIL and do nothing if not executing in a class; otherwise OK.
941 */
942 int
943fill_exec_lval_root(lval_root_T *root)
944{
945 ectx_T *ectx = current_ectx;
946 if (ectx != NULL)
947 {
948 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
949 + current_ectx->ec_dfunc_idx;
950 ufunc_T *ufunc = dfunc->df_ufunc;
951 if (ufunc->uf_class != NULL) // executing a method?
952 {
953 typval_T *tv = alloc_tv();
954 if (tv != NULL)
955 {
956 CLEAR_POINTER(root);
957 root->lr_tv = tv;
958 copy_tv(STACK_TV_VAR(0), root->lr_tv);
959 root->lr_cl_exec = ufunc->uf_class;
960 ++root->lr_cl_exec->class_refcount;
961 return OK;
962 }
963 }
964 }
965
966 return FAIL;
967}
968
969/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100970 * Clear "current_ectx" and return the previous value. To be used when calling
971 * a user function.
972 */
973 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000974clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100975{
976 ectx_T *r = current_ectx;
977
978 current_ectx = NULL;
979 return r;
980}
981
982 void
983restore_current_ectx(ectx_T *ectx)
984{
985 if (current_ectx != NULL)
986 iemsg("Restoring current_ectx while it is not NULL");
987 current_ectx = ectx;
988}
989
990/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100991 * Add an entry for a deferred function call to the currently executing
992 * function.
993 * Return the list or NULL when failed.
994 */
995 static list_T *
996add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100997{
998 typval_T *defer_tv = STACK_TV_VAR(var_idx);
999 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001000 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001001 typval_T listval;
1002
1003 if (defer_tv->v_type != VAR_LIST)
1004 {
Bram Moolenaara5348f22022-09-04 11:42:22 +01001005 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001006 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001007 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001008 }
1009 defer_l = defer_tv->vval.v_list;
1010
1011 l = list_alloc_with_items(argcount + 1);
1012 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001013 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001014 listval.v_type = VAR_LIST;
1015 listval.vval.v_list = l;
1016 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +01001017 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001018 {
1019 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001020 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001021 }
1022
Bram Moolenaar806a2732022-09-04 15:40:36 +01001023 return l;
1024}
1025
1026/*
1027 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
1028 * The local variable that lists deferred functions is "var_idx".
1029 * Returns OK or FAIL.
1030 */
1031 static int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001032defer_command(int var_idx, int has_obj, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001033{
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001034 int obj_off = has_obj ? 1 : 0;
1035 list_T *l = add_defer_item(var_idx, argcount + obj_off, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001036 int i;
1037 typval_T *func_tv;
1038
1039 if (l == NULL)
1040 return FAIL;
1041
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001042 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001043 if (has_obj ? func_tv->v_type != VAR_PARTIAL : func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001044 {
1045 semsg(_(e_expected_str_but_got_str),
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001046 has_obj ? "partial" : "function",
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001047 vartype_name(func_tv->v_type));
1048 return FAIL;
1049 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001050 list_set_item(l, 0, func_tv);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001051 if (has_obj)
1052 list_set_item(l, 1, STACK_TV_BOT(-argcount - 2));
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001053
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001054 for (i = 0; i < argcount; ++i)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001055 list_set_item(l, i + 1 + obj_off, STACK_TV_BOT(-argcount + i));
1056 ectx->ec_stack.ga_len -= argcount + 1 + obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001057 return OK;
1058}
1059
1060/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001061 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001062 * Consumes "name", also on failure.
1063 * Only to be called when in_def_function() returns TRUE.
1064 */
1065 int
1066add_defer_function(char_u *name, int argcount, typval_T *argvars)
1067{
1068 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1069 + current_ectx->ec_dfunc_idx;
1070 list_T *l;
1071 typval_T func_tv;
1072 int i;
1073
1074 if (dfunc->df_defer_var_idx == 0)
1075 {
1076 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001077 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001078 return FAIL;
1079 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001080
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001081 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001082 if (l == NULL)
1083 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001084 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001085 return FAIL;
1086 }
1087
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001088 func_tv.v_type = VAR_FUNC;
1089 func_tv.v_lock = 0;
1090 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001091 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001092
Bram Moolenaar806a2732022-09-04 15:40:36 +01001093 for (i = 0; i < argcount; ++i)
1094 list_set_item(l, i + 1, argvars + i);
1095 return OK;
1096}
1097
1098/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001099 * Invoked when returning from a function: Invoke any deferred calls.
1100 */
1101 static void
1102invoke_defer_funcs(ectx_T *ectx)
1103{
1104 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1105 + ectx->ec_dfunc_idx;
1106 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1107 listitem_T *li;
1108
1109 if (defer_tv->v_type != VAR_LIST)
1110 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001111 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001112 {
1113 list_T *l = li->li_tv.vval.v_list;
1114 typval_T rettv;
1115 typval_T argvars[MAX_FUNC_ARGS];
1116 int i;
1117 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001118 typval_T *functv = &l->lv_first->li_tv;
1119 int obj_off = functv->v_type == VAR_PARTIAL ? 1 : 0;
1120 int argcount = l->lv_len - 1 - obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001121
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001122 if (functv->vval.v_string == NULL)
1123 // already being called, can happen if function does ":qa"
1124 continue;
1125
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001126 if (obj_off == 1)
1127 arg_li = arg_li->li_next; // second list item is the object
1128 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001129 {
1130 arg_li = arg_li->li_next;
1131 argvars[i] = arg_li->li_tv;
1132 }
1133
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001134 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001135 CLEAR_FIELD(funcexe);
1136 funcexe.fe_evaluate = TRUE;
1137 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001138 if (functv->v_type == VAR_PARTIAL)
1139 {
1140 funcexe.fe_partial = functv->vval.v_partial;
1141 funcexe.fe_object = l->lv_first->li_next->li_tv.vval.v_object;
1142 if (funcexe.fe_object != NULL)
1143 ++funcexe.fe_object->obj_refcount;
1144 }
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001145
1146 char_u *name = functv->vval.v_string;
1147 functv->vval.v_string = NULL;
1148
1149 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1150
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001151 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001152 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001153 }
1154}
1155
1156/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 * Return from the current function.
1158 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001159 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001160func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001163 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001164 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1165 + ectx->ec_dfunc_idx;
1166 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001167 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001168 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1169 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001170 funclocal_T *floc;
1171#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001172 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1173 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174
Bram Moolenaar12d26532021-02-19 19:13:21 +01001175 if (do_profiling == PROF_YES)
1176 {
1177 ufunc_T *caller = prev_dfunc->df_ufunc;
1178
1179 if (dfunc->df_ufunc->uf_profiling
1180 || (caller != NULL && caller->uf_profiling))
1181 {
1182 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1183 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1184 --profile_info_ga.ga_len;
1185 }
1186 }
1187#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001188
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001189 if (dfunc->df_defer_var_idx > 0)
1190 invoke_defer_funcs(ectx);
1191
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001192 // No check for uf_refcount being zero, cannot think of a way that would
1193 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001194 --dfunc->df_ufunc->uf_calls;
1195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001197 entry = estack_pop();
1198 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001199 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001201 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1202 return FAIL;
1203
Bram Moolenaar574950d2023-01-03 19:08:50 +00001204 // Clear the arguments. If this was an object method also clear the
1205 // object, it is just before the arguments.
1206 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001207 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001208 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001209 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1210 clear_tv(STACK_TV(idx));
1211
1212 // Clear local variables and temp values, but not the return value.
1213 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001214 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001216
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001217 // The return value should be on top of the stack. However, when aborting
1218 // it may not be there and ec_frame_idx is the top of the stack.
1219 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001220 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001221 ret_idx = 0;
1222
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001223 if (ectx->ec_outer_ref != NULL)
1224 {
1225 if (ectx->ec_outer_ref->or_outer_allocated)
1226 vim_free(ectx->ec_outer_ref->or_outer);
1227 partial_unref(ectx->ec_outer_ref->or_partial);
1228 vim_free(ectx->ec_outer_ref);
1229 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001230
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001231 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001232 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001233 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1234 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001235 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1236 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001237 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001238 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001239 floc = (void *)STACK_TV(ectx->ec_frame_idx
1240 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001241 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001242 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1243 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001244
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001245 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001246 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001247 else
1248 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001249 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001250 vim_free(floc);
1251 }
1252
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001253 if (ret_idx > 0)
1254 {
1255 // Reset the stack to the position before the call, with a spot for the
1256 // return value, moved there from above the frame.
1257 ectx->ec_stack.ga_len = top + 1;
1258 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1259 }
1260 else
1261 // Reset the stack to the position before the call.
1262 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001263
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001264 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001265 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001266 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267}
1268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269/*
1270 * Prepare arguments and rettv for calling a builtin or user function.
1271 */
1272 static int
1273call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1274{
1275 int idx;
1276 typval_T *tv;
1277
1278 // Move arguments from bottom of the stack to argvars[] and add terminator.
1279 for (idx = 0; idx < argcount; ++idx)
1280 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1281 argvars[argcount].v_type = VAR_UNKNOWN;
1282
1283 // Result replaces the arguments on the stack.
1284 if (argcount > 0)
1285 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001286 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 return FAIL;
1288 else
1289 ++ectx->ec_stack.ga_len;
1290
1291 // Default return value is zero.
1292 tv = STACK_TV_BOT(-1);
1293 tv->v_type = VAR_NUMBER;
1294 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001295 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296
1297 return OK;
1298}
1299
1300/*
1301 * Call a builtin function by index.
1302 */
1303 static int
1304call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1305{
1306 typval_T argvars[MAX_FUNC_ARGS];
1307 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001308 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001309 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001310 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001311
1312 if (call_prepare(argcount, argvars, ectx) == FAIL)
1313 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001314 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001316 // Call the builtin function. Set "current_ectx" so that when it
1317 // recursively invokes call_def_function() a closure context can be set.
1318 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001320 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001321 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322
1323 // Clear the arguments.
1324 for (idx = 0; idx < argcount; ++idx)
1325 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001326
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001327 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001328 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329 return OK;
1330}
1331
1332/*
1333 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001334 * If the function is compiled this will add a stack frame and set the
1335 * instruction pointer at the start of the function.
1336 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001337 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001338 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 */
1340 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001341call_ufunc(
1342 ufunc_T *ufunc,
1343 partial_T *pt,
1344 int argcount,
1345 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001346 isn_T *iptr,
1347 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348{
1349 typval_T argvars[MAX_FUNC_ARGS];
1350 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001351 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001353 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001354 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355
Bram Moolenaare99d4222021-06-13 14:01:26 +02001356 if (func_needs_compiling(ufunc, compile_type)
1357 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1358 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001359 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001360 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001361 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001362 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001363 if (error != FCERR_UNKNOWN)
1364 {
1365 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001366 semsg(_(e_too_many_arguments_for_function_str),
1367 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001368 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001369 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001370 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001371 return FAIL;
1372 }
1373
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001374 // The function has been compiled, can call it quickly. For a function
1375 // that was defined later: we can call it directly next time.
1376 if (iptr != NULL)
1377 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001378 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001379 iptr->isn_type = ISN_DCALL;
1380 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1381 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1382 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001383 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385
1386 if (call_prepare(argcount, argvars, ectx) == FAIL)
1387 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001388 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001389 funcexe.fe_evaluate = TRUE;
1390 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391
1392 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001394 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395
1396 // Clear the arguments.
1397 for (idx = 0; idx < argcount; ++idx)
1398 clear_tv(&argvars[idx]);
1399
1400 if (error != FCERR_NONE)
1401 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001402 user_func_error(error, printable_func_name(ufunc),
1403 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 return FAIL;
1405 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001406 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001407 // Error other than from calling the function itself.
1408 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 return OK;
1410}
1411
1412/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001413 * If command modifiers were applied restore them.
1414 */
1415 static void
1416may_restore_cmdmod(funclocal_T *funclocal)
1417{
1418 if (funclocal->floc_restore_cmdmod)
1419 {
1420 cmdmod.cmod_filter_regmatch.regprog = NULL;
1421 undo_cmdmod(&cmdmod);
1422 cmdmod = funclocal->floc_save_cmdmod;
1423 funclocal->floc_restore_cmdmod = FALSE;
1424 }
1425}
1426
1427/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001428 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1429 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001430 */
1431 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001432vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001433{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001434 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001435}
1436
1437/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 * Execute a function by "name".
1439 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001440 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 * Returns FAIL if not found without an error message.
1442 */
1443 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001444call_by_name(
1445 char_u *name,
1446 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001447 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001448 isn_T *iptr,
1449 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450{
1451 ufunc_T *ufunc;
1452
1453 if (builtin_function(name, -1))
1454 {
1455 int func_idx = find_internal_func(name);
1456
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001457 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001459 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460 return FAIL;
1461 return call_bfunc(func_idx, argcount, ectx);
1462 }
1463
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001464 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001465
1466 if (ufunc == NULL)
1467 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001468 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001469
1470 if (script_autoload(name, TRUE))
1471 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001472 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001473
1474 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001475 return FAIL; // bail out if loading the script caused an error
1476 }
1477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001479 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001480 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1481 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001482
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001483 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001484 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485
1486 return FAIL;
1487}
1488
1489 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001490call_partial(
1491 typval_T *tv,
1492 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001493 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001495 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001496 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001498 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001499 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500
1501 if (tv->v_type == VAR_PARTIAL)
1502 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001503 partial_T *pt = tv->vval.v_partial;
1504 int i;
1505
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001506 if (pt->pt_obj != NULL)
1507 {
1508 // partial with an object method. Push the object before the
1509 // function arguments.
1510 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1511 return FAIL;
1512 for (i = 1; i <= argcount; ++i)
1513 *STACK_TV_BOT(-i + 1) = *STACK_TV_BOT(-i);
1514
1515 typval_T *obj_tv = STACK_TV_BOT(-argcount);
1516 obj_tv->v_type = VAR_OBJECT;
1517 obj_tv->v_lock = 0;
1518 obj_tv->vval.v_object = pt->pt_obj;
1519 ++pt->pt_obj->obj_refcount;
1520 ++ectx->ec_stack.ga_len;
1521 }
1522
Bram Moolenaara90afb92020-07-15 22:38:56 +02001523 if (pt->pt_argc > 0)
1524 {
1525 // Make space for arguments from the partial, shift the "argcount"
1526 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001527 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001528 return FAIL;
1529 for (i = 1; i <= argcount; ++i)
1530 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1531 ectx->ec_stack.ga_len += pt->pt_argc;
1532 argcount += pt->pt_argc;
1533
1534 // copy the arguments from the partial onto the stack
1535 for (i = 0; i < pt->pt_argc; ++i)
1536 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1537 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001538 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539
1540 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001541 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001542
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543 name = pt->pt_name;
1544 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001545 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001547 if (name != NULL)
1548 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001549 char_u fname_buf[FLEN_FIXED + 1];
1550 char_u *tofree = NULL;
1551 funcerror_T error = FCERR_NONE;
1552 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001553
1554 // May need to translate <SNR>123_ to K_SNR.
1555 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1556 if (error != FCERR_NONE)
1557 res = FAIL;
1558 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001559 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001560 vim_free(tofree);
1561 }
1562
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001563 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 {
1565 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001566 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001567 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 return FAIL;
1569 }
1570 return OK;
1571}
1572
1573/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001574 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1575 * TRUE.
1576 */
1577 static int
1578error_if_locked(int lock, char *error)
1579{
1580 if (lock & (VAR_LOCKED | VAR_FIXED))
1581 {
1582 emsg(_(error));
1583 return TRUE;
1584 }
1585 return FALSE;
1586}
1587
1588/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001589 * Give an error if "tv" is not a number and return FAIL.
1590 */
1591 static int
1592check_for_number(typval_T *tv)
1593{
1594 if (tv->v_type != VAR_NUMBER)
1595 {
1596 semsg(_(e_expected_str_but_got_str),
1597 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1598 return FAIL;
1599 }
1600 return OK;
1601}
1602
1603/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001604 * Store "tv" in variable "name".
1605 * This is for s: and g: variables.
1606 */
1607 static void
1608store_var(char_u *name, typval_T *tv)
1609{
1610 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001611 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001612
Bram Moolenaard877a572021-04-01 19:42:48 +02001613 if (tv->v_lock)
1614 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001615 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001616 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001617 restore_funccal();
1618}
1619
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001620/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001621 * Convert "tv" to a string.
1622 * Return FAIL if not allowed.
1623 */
1624 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001625do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001626{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001627 if (tv->v_type == VAR_STRING)
1628 return OK;
1629
1630 char_u *str;
1631
1632 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001633 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001634 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001635 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001636 case VAR_SPECIAL:
1637 case VAR_BOOL:
1638 case VAR_NUMBER:
1639 case VAR_FLOAT:
1640 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001641
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001642 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001643 if (tolerant)
1644 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001645 char_u *s, *e, *p;
1646 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001647
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001648 ga_init2(&ga, sizeof(char_u *), 1);
1649
1650 // Convert to NL separated items, then
1651 // escape the items and replace the NL with
1652 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001653 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001654 if (str == NULL)
1655 return FAIL;
1656 s = str;
1657 while ((e = vim_strchr(s, '\n')) != NULL)
1658 {
1659 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001660 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001661 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001662 if (p != NULL)
1663 {
1664 ga_concat(&ga, p);
1665 ga_concat(&ga, (char_u *)" ");
1666 vim_free(p);
1667 }
1668 s = e + 1;
1669 }
1670 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001671 clear_tv(tv);
1672 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001673 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001674 return OK;
1675 }
1676 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001677 default: to_string_error(tv->v_type);
1678 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001679 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001680 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001681 str = typval_tostring(tv, TRUE);
1682 clear_tv(tv);
1683 tv->v_type = VAR_STRING;
1684 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001685 return OK;
1686}
1687
1688/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001689 * When the value of "sv" is a null list of dict, allocate it.
1690 */
1691 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001692allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001693{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001694 typval_T *tv = sv->sv_tv;
1695
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001696 switch (tv->v_type)
1697 {
1698 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001699 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001700 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001701 break;
1702 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001703 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001704 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001705 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001706 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001707 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001708 (void)rettv_blob_alloc(tv);
1709 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001710 default:
1711 break;
1712 }
1713}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001714
Bram Moolenaare7525c52021-01-09 13:20:37 +01001715/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001716 * Return the character "str[index]" where "index" is the character index,
1717 * including composing characters.
1718 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001719 */
1720 char_u *
1721char_from_string(char_u *str, varnumber_T index)
1722{
1723 size_t nbyte = 0;
1724 varnumber_T nchar = index;
1725 size_t slen;
1726
1727 if (str == NULL)
1728 return NULL;
1729 slen = STRLEN(str);
1730
Bram Moolenaarff871402021-03-26 13:34:05 +01001731 // Do the same as for a list: a negative index counts from the end.
1732 // Optimization to check the first byte to be below 0x80 (and no composing
1733 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001734 if (index < 0)
1735 {
1736 int clen = 0;
1737
1738 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001739 {
1740 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1741 ++nbyte;
1742 else if (enc_utf8)
1743 nbyte += utfc_ptr2len(str + nbyte);
1744 else
1745 nbyte += mb_ptr2len(str + nbyte);
1746 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001747 nchar = clen + index;
1748 if (nchar < 0)
1749 // unlike list: index out of range results in empty string
1750 return NULL;
1751 }
1752
1753 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001754 {
1755 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1756 ++nbyte;
1757 else if (enc_utf8)
1758 nbyte += utfc_ptr2len(str + nbyte);
1759 else
1760 nbyte += mb_ptr2len(str + nbyte);
1761 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001762 if (nbyte >= slen)
1763 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001764 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001765}
1766
1767/*
1768 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001769 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001770 * If going over the end return "str_len".
1771 * If "idx" is negative count from the end, -1 is the last character.
1772 * When going over the start return -1.
1773 */
1774 static long
1775char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1776{
1777 varnumber_T nchar = idx;
1778 size_t nbyte = 0;
1779
1780 if (nchar >= 0)
1781 {
1782 while (nchar > 0 && nbyte < str_len)
1783 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001784 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001785 --nchar;
1786 }
1787 }
1788 else
1789 {
1790 nbyte = str_len;
1791 while (nchar < 0 && nbyte > 0)
1792 {
1793 --nbyte;
1794 nbyte -= mb_head_off(str, str + nbyte);
1795 ++nchar;
1796 }
1797 if (nchar < 0)
1798 return -1;
1799 }
1800 return (long)nbyte;
1801}
1802
1803/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001804 * Return the slice "str[first : last]" using character indexes. Composing
1805 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001806 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001807 * Return NULL when the result is empty.
1808 */
1809 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001810string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001811{
1812 long start_byte, end_byte;
1813 size_t slen;
1814
1815 if (str == NULL)
1816 return NULL;
1817 slen = STRLEN(str);
1818 start_byte = char_idx2byte(str, slen, first);
1819 if (start_byte < 0)
1820 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001821 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001822 end_byte = (long)slen;
1823 else
1824 {
1825 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001826 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001827 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001828 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001829 }
1830
1831 if (start_byte >= (long)slen || end_byte <= start_byte)
1832 return NULL;
1833 return vim_strnsave(str + start_byte, end_byte - start_byte);
1834}
1835
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001836/*
1837 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1838 * When "dfunc_idx" is negative don't give an error.
1839 * Returns NULL for an error.
1840 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001841 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001842get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001843{
1844 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001845 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1846 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001847 svar_T *sv;
1848
1849 if (sref->sref_seq != si->sn_script_seq)
1850 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001851 // The script was reloaded after the function was compiled, the
1852 // script_idx may not be valid.
1853 if (dfunc != NULL)
1854 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1855 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001856 return NULL;
1857 }
1858 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001859 if (sv->sv_name == NULL)
1860 {
1861 if (dfunc != NULL)
1862 emsg(_(e_script_variable_was_deleted));
1863 return NULL;
1864 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001865 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001866 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001867 if (dfunc != NULL)
1868 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001869 return NULL;
1870 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001871
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001872 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1873 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001874 {
1875 if (dfunc != NULL)
1876 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1877 return NULL;
1878 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001879 return sv;
1880}
1881
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001882/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001883 * Function passed to do_cmdline() for splitting a script joined by NL
1884 * characters.
1885 */
1886 static char_u *
1887get_split_sourceline(
1888 int c UNUSED,
1889 void *cookie,
1890 int indent UNUSED,
1891 getline_opt_T options UNUSED)
1892{
1893 source_cookie_T *sp = (source_cookie_T *)cookie;
1894 char_u *p;
1895 char_u *line;
1896
Bram Moolenaar20677332021-06-06 17:02:53 +02001897 p = vim_strchr(sp->nextline, '\n');
1898 if (p == NULL)
1899 {
1900 line = vim_strsave(sp->nextline);
1901 sp->nextline += STRLEN(sp->nextline);
1902 }
1903 else
1904 {
1905 line = vim_strnsave(sp->nextline, p - sp->nextline);
1906 sp->nextline = p + 1;
1907 }
1908 return line;
1909}
1910
1911/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 * Execute a function by "name".
1913 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001914 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 */
1916 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001917call_eval_func(
1918 char_u *name,
1919 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001920 ectx_T *ectx,
1921 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922{
Bram Moolenaared677f52020-08-12 16:38:10 +02001923 int called_emsg_before = called_emsg;
1924 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001926 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001927 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001929 dictitem_T *v;
1930
1931 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001932 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1933 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001934 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001935 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001936 return FAIL;
1937 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001938 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001940 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941}
1942
1943/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001944 * When a function reference is used, fill a partial with the information
1945 * needed, especially when it is used as a closure.
1946 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001947 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001948fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001949 partial_T *pt,
1950 ufunc_T *ufunc,
1951 loopvarinfo_T *lvi,
1952 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001953{
1954 pt->pt_func = ufunc;
1955 pt->pt_refcount = 1;
1956
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001957 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001958 {
1959 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1960 + ectx->ec_dfunc_idx;
1961
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001962 // The closure may need to find arguments and local variables of the
1963 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001964 pt->pt_outer.out_stack = &ectx->ec_stack;
1965 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001966 if (ectx->ec_outer_ref != NULL)
1967 {
1968 // The current context already has a context, link to that one.
1969 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1970 if (ectx->ec_outer_ref->or_partial != NULL)
1971 {
1972 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1973 ++pt->pt_outer.out_up_partial->pt_refcount;
1974 }
1975 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001976
Bram Moolenaarcc341812022-09-19 15:54:34 +01001977 if (lvi != NULL)
1978 {
1979 int depth;
1980
1981 // The closure may need to find variables defined inside a loop,
1982 // for every nested loop. A new reference is made every time,
1983 // ISN_ENDLOOP will check if they are actually used.
1984 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1985 {
1986 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1987 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1988 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1989 pt->pt_outer.out_loop[depth].var_count =
1990 lvi->lvi_loop[depth].var_count;
1991 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001992 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001993 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001994 else
1995 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001996
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001997 // If the function currently executing returns and the closure is still
1998 // being referenced, we need to make a copy of the context (arguments
1999 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002000 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02002001 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01002002 {
2003 vim_free(pt);
2004 return FAIL;
2005 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002006 // Extra variable keeps the count of closures created in the current
2007 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01002008 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
2009 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
2010
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002011 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
2012 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002013 ++pt->pt_refcount;
2014 ++ectx->ec_funcrefs.ga_len;
2015 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002016 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002017 return OK;
2018}
2019
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002020/*
2021 * Execute iptr->isn_arg.string as an Ex command.
2022 */
2023 static int
Ernie Raelee865f32023-09-29 19:53:55 +02002024exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002025{
2026 source_cookie_T cookie;
2027
2028 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002029 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002030 CLEAR_FIELD(cookie);
2031 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02002032 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002033 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
2034 || did_emsg)
2035 return FAIL;
2036 return OK;
2037}
2038
Bram Moolenaar89445512022-04-14 12:58:23 +01002039/*
2040 * If script "sid" is not loaded yet then load it now.
2041 * Caller must make sure "sid" is a valid script ID.
2042 * "loaded" is set to TRUE if the script had to be loaded.
2043 * Returns FAIL if loading fails, OK if already loaded or loaded now.
2044 */
2045 int
2046may_load_script(int sid, int *loaded)
2047{
2048 scriptitem_T *si = SCRIPT_ITEM(sid);
2049
2050 if (si->sn_state == SN_STATE_NOT_LOADED)
2051 {
2052 *loaded = TRUE;
2053 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
2054 {
2055 semsg(_(e_cant_open_file_str), si->sn_name);
2056 return FAIL;
2057 }
2058 }
2059 return OK;
2060}
2061
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002062// used for v_instr of typval of VAR_INSTR
2063struct instr_S {
2064 ectx_T *instr_ectx;
2065 isn_T *instr_instr;
2066};
2067
Bram Moolenaar4c137212021-04-19 16:48:48 +02002068// used for substitute_instr
2069typedef struct subs_expr_S {
2070 ectx_T *subs_ectx;
2071 isn_T *subs_instr;
2072 int subs_status;
2073} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002075// Set when calling do_debug().
2076static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002077static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002078
2079/*
2080 * When debugging lookup "name" and return the typeval.
2081 * When not found return NULL.
2082 */
2083 typval_T *
2084lookup_debug_var(char_u *name)
2085{
2086 int idx;
2087 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002088 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002089 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002090 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002091
2092 if (ectx == NULL)
2093 return NULL;
2094 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2095
2096 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002097 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002098 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002099 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2100
2101 // the variable name may be NULL when not available in this block
2102 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002103 return STACK_TV_VAR(idx);
2104 }
2105
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002106 // Go through argument names.
2107 ufunc = dfunc->df_ufunc;
2108 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2109 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2110 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2111 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2112 - varargs_off + idx);
2113 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2114 return STACK_TV(ectx->ec_frame_idx - 1);
2115
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002116 return NULL;
2117}
2118
Bram Moolenaar26a44842021-09-02 18:49:06 +02002119/*
2120 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2121 * breakpoint was set in that function or when there is any expression.
2122 */
2123 int
2124may_break_in_function(ufunc_T *ufunc)
2125{
2126 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2127}
2128
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002129 static void
2130handle_debug(isn_T *iptr, ectx_T *ectx)
2131{
2132 char_u *line;
2133 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2134 + ectx->ec_dfunc_idx)->df_ufunc;
2135 isn_T *ni;
2136 int end_lnum = iptr->isn_lnum;
2137 garray_T ga;
2138 int lnum;
2139
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002140 if (ex_nesting_level > debug_break_level)
2141 {
2142 linenr_T breakpoint;
2143
Bram Moolenaar26a44842021-09-02 18:49:06 +02002144 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002145 return;
2146
2147 // check for the next breakpoint if needed
2148 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002149 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002150 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2151 return;
2152 }
2153
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002154 SOURCING_LNUM = iptr->isn_lnum;
2155 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002156 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002157
2158 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2159 if (ni->isn_type == ISN_DEBUG
2160 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002161 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002162 || ni->isn_type == ISN_RETURN_VOID)
2163 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002164 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002165 break;
2166 }
2167
2168 if (end_lnum > iptr->isn_lnum)
2169 {
2170 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002171 for (lnum = iptr->isn_lnum; lnum < end_lnum
2172 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002173 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002174 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002175
Bram Moolenaar303215d2021-07-07 20:10:43 +02002176 if (p == NULL)
2177 continue; // left over from continuation line
2178 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002179 if (*p == '#')
2180 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002181 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002182 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2183 if (STRNCMP(p, "def ", 4) == 0)
2184 break;
2185 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002186 line = ga_concat_strings(&ga, " ");
2187 vim_free(ga.ga_data);
2188 }
2189 else
2190 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002191
Dominique Pelle6e969552021-06-17 13:53:41 +02002192 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002193 debug_context = NULL;
2194
2195 if (end_lnum > iptr->isn_lnum)
2196 vim_free(line);
2197}
2198
Bram Moolenaar4c137212021-04-19 16:48:48 +02002199/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002200 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002201 * Returns OK, FAIL or NOTDONE (uncatchable error).
2202 */
2203 static int
2204execute_storeindex(isn_T *iptr, ectx_T *ectx)
2205{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002206 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002207 typval_T *tv;
2208 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002209 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002210 typval_T *tv_dest = STACK_TV_BOT(-1);
2211 int status = OK;
2212
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002213 if (tv_idx->v_type == VAR_NUMBER)
2214 lidx = (long)tv_idx->vval.v_number;
2215
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002216 // Stack contains:
2217 // -3 value to be stored
2218 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002219 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002220 tv = STACK_TV_BOT(-3);
2221 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002222
2223 // Make sure an object has been initialized
2224 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2225 {
2226 emsg(_(e_using_null_object));
2227 status = FAIL;
2228 }
2229 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002230 {
2231 dest_type = tv_dest->v_type;
2232 if (dest_type == VAR_DICT)
2233 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002234 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2235 {
2236 // Need to get the member index now that the class is known.
2237 object_T *obj = tv_dest->vval.v_object;
2238 class_T *cl = obj->obj_class;
2239 char_u *member = tv_idx->vval.v_string;
2240
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002241 int m_idx;
2242 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2243 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002244 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002245 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002246 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02002247 emsg_var_cl_define(e_cannot_access_private_variable_str,
2248 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002249 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002250 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002251
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002252 lidx = m_idx;
2253 }
2254 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002255 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002256 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002257 status = FAIL;
2258 }
2259 }
2260 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2261 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002262 {
2263 emsg(_(e_number_expected));
2264 status = FAIL;
2265 }
2266 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002267
2268 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002269 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002270 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002271 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002272 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002273
Bram Moolenaare08be092022-02-17 13:08:26 +00002274 if (list == NULL)
2275 {
2276 emsg(_(e_list_not_set));
2277 return FAIL;
2278 }
2279 if (lidx < 0 && list->lv_len + lidx >= 0)
2280 // negative index is relative to the end
2281 lidx = list->lv_len + lidx;
2282 if (lidx < 0 || lidx > list->lv_len)
2283 {
2284 semsg(_(e_list_index_out_of_range_nr), lidx);
2285 return FAIL;
2286 }
2287 if (lidx < list->lv_len)
2288 {
2289 listitem_T *li = list_find(list, lidx);
2290
2291 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002292 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002293 return FAIL;
2294 // overwrite existing list item
2295 clear_tv(&li->li_tv);
2296 li->li_tv = *tv;
2297 }
2298 else
2299 {
2300 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2301 return FAIL;
2302 // append to list, only fails when out of memory
2303 if (list_append_tv(list, tv) == FAIL)
2304 return NOTDONE;
2305 clear_tv(tv);
2306 }
2307 }
2308 else if (dest_type == VAR_DICT)
2309 {
2310 char_u *key = tv_idx->vval.v_string;
2311 dict_T *dict = tv_dest->vval.v_dict;
2312 dictitem_T *di;
2313
2314 SOURCING_LNUM = iptr->isn_lnum;
2315 if (dict == NULL)
2316 {
2317 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002318 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002319 }
2320 if (key == NULL)
2321 key = (char_u *)"";
2322 di = dict_find(dict, key, -1);
2323 if (di != NULL)
2324 {
2325 if (error_if_locked(di->di_tv.v_lock,
2326 e_cannot_change_dict_item))
2327 return FAIL;
2328 // overwrite existing value
2329 clear_tv(&di->di_tv);
2330 di->di_tv = *tv;
2331 }
2332 else
2333 {
2334 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2335 return FAIL;
2336 // add to dict, only fails when out of memory
2337 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2338 return NOTDONE;
2339 clear_tv(tv);
2340 }
2341 }
2342 else if (dest_type == VAR_BLOB)
2343 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002344 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002345 varnumber_T nr;
2346 int error = FALSE;
2347 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002348
2349 if (blob == NULL)
2350 {
2351 emsg(_(e_blob_not_set));
2352 return FAIL;
2353 }
2354 len = blob_len(blob);
2355 if (lidx < 0 && len + lidx >= 0)
2356 // negative index is relative to the end
2357 lidx = len + lidx;
2358
2359 // Can add one byte at the end.
2360 if (lidx < 0 || lidx > len)
2361 {
2362 semsg(_(e_blob_index_out_of_range_nr), lidx);
2363 return FAIL;
2364 }
2365 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2366 return FAIL;
2367 nr = tv_get_number_chk(tv, &error);
2368 if (error)
2369 return FAIL;
2370 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002371 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002372 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2373 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002374 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002375
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002376 if (dest_type == VAR_OBJECT)
2377 {
2378 object_T *obj = tv_dest->vval.v_object;
2379
2380 otv = (typval_T *)(obj + 1);
2381 class_T *itf = iptr->isn_arg.storeindex.si_class;
2382 if (itf != NULL)
2383 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002384 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002385 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002386 }
2387 else
2388 {
2389 // VAR_CLASS
2390 class_T *class = tv_dest->vval.v_class;
2391 otv = class->class_members_tv;
2392 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002393
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002394 clear_tv(&otv[lidx]);
2395 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002396 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002397 else
2398 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002399 status = FAIL;
2400 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002401 }
2402 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002403
2404 clear_tv(tv_idx);
2405 clear_tv(tv_dest);
2406 ectx->ec_stack.ga_len -= 3;
2407 if (status == FAIL)
2408 {
2409 clear_tv(tv);
2410 return FAIL;
2411 }
2412 return OK;
2413}
2414
2415/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002416 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002417 */
2418 static int
2419execute_storerange(isn_T *iptr, ectx_T *ectx)
2420{
2421 typval_T *tv;
2422 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2423 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2424 typval_T *tv_dest = STACK_TV_BOT(-1);
2425 int status = OK;
2426
2427 // Stack contains:
2428 // -4 value to be stored
2429 // -3 first index or "none"
2430 // -2 second index or "none"
2431 // -1 destination list or blob
2432 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002433 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002434 if (tv_dest->v_type == VAR_LIST)
2435 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002436 long n1;
2437 long n2;
2438 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002439
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002440 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2441 if (tv_idx2->v_type == VAR_SPECIAL
2442 && tv_idx2->vval.v_number == VVAL_NONE)
2443 n2 = list_len(tv_dest->vval.v_list) - 1;
2444 else
2445 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2446
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002447 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002448 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002449 status = FAIL;
2450 else
2451 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002452 status = check_range_index_two(tv_dest->vval.v_list,
2453 &n1, li1, &n2, FALSE);
2454 if (status != FAIL)
2455 status = list_assign_range(
2456 tv_dest->vval.v_list,
2457 tv->vval.v_list,
2458 n1,
2459 n2,
2460 tv_idx2->v_type == VAR_SPECIAL,
2461 (char_u *)"=",
2462 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002463 }
2464 }
2465 else if (tv_dest->v_type == VAR_BLOB)
2466 {
2467 varnumber_T n1;
2468 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002469 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002470
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002471 n1 = tv_get_number_chk(tv_idx1, NULL);
2472 if (tv_idx2->v_type == VAR_SPECIAL
2473 && tv_idx2->vval.v_number == VVAL_NONE)
2474 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2475 else
2476 n2 = tv_get_number_chk(tv_idx2, NULL);
2477 bloblen = blob_len(tv_dest->vval.v_blob);
2478
2479 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2480 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002481 status = FAIL;
2482 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002483 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002484 }
2485 else
2486 {
2487 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002488 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002489 }
2490
2491 clear_tv(tv_idx1);
2492 clear_tv(tv_idx2);
2493 clear_tv(tv_dest);
2494 ectx->ec_stack.ga_len -= 4;
2495 clear_tv(tv);
2496
2497 return status;
2498}
2499
2500/*
2501 * Unlet item in list or dict variable.
2502 */
2503 static int
2504execute_unletindex(isn_T *iptr, ectx_T *ectx)
2505{
2506 typval_T *tv_idx = STACK_TV_BOT(-2);
2507 typval_T *tv_dest = STACK_TV_BOT(-1);
2508 int status = OK;
2509
2510 // Stack contains:
2511 // -2 index
2512 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002513 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002514 if (tv_dest->v_type == VAR_DICT)
2515 {
2516 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002517 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002518 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002519 semsg(_(e_expected_str_but_got_str),
2520 vartype_name(VAR_STRING),
2521 vartype_name(tv_idx->v_type));
2522 status = FAIL;
2523 }
2524 else
2525 {
2526 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002527 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002528 dictitem_T *di = NULL;
2529
2530 if (d != NULL && value_check_lock(
2531 d->dv_lock, NULL, FALSE))
2532 status = FAIL;
2533 else
2534 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002535 if (tv_idx->v_type == VAR_STRING)
2536 {
2537 key = tv_idx->vval.v_string;
2538 if (key == NULL)
2539 key = (char_u *)"";
2540 }
2541 else
2542 {
2543 key = tv_get_string(tv_idx);
2544 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002545 if (d != NULL)
2546 di = dict_find(d, key, (int)STRLEN(key));
2547 if (di == NULL)
2548 {
2549 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002550 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002551 status = FAIL;
2552 }
2553 else if (var_check_fixed(di->di_flags,
2554 NULL, FALSE)
2555 || var_check_ro(di->di_flags,
2556 NULL, FALSE))
2557 status = FAIL;
2558 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002559 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002560 }
2561 }
2562 }
2563 else if (tv_dest->v_type == VAR_LIST)
2564 {
2565 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002566 if (check_for_number(tv_idx) == FAIL)
2567 {
2568 status = FAIL;
2569 }
2570 else
2571 {
2572 list_T *l = tv_dest->vval.v_list;
2573 long n = (long)tv_idx->vval.v_number;
2574
2575 if (l != NULL && value_check_lock(
2576 l->lv_lock, NULL, FALSE))
2577 status = FAIL;
2578 else
2579 {
2580 listitem_T *li = list_find(l, n);
2581
2582 if (li == NULL)
2583 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002584 semsg(_(e_list_index_out_of_range_nr), n);
2585 status = FAIL;
2586 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002587 else
2588 listitem_remove(l, li);
2589 }
2590 }
2591 }
2592 else
2593 {
2594 status = FAIL;
2595 semsg(_(e_cannot_index_str),
2596 vartype_name(tv_dest->v_type));
2597 }
2598
2599 clear_tv(tv_idx);
2600 clear_tv(tv_dest);
2601 ectx->ec_stack.ga_len -= 2;
2602
2603 return status;
2604}
2605
2606/*
2607 * Unlet a range of items in a list variable.
2608 */
2609 static int
2610execute_unletrange(isn_T *iptr, ectx_T *ectx)
2611{
2612 // Stack contains:
2613 // -3 index1
2614 // -2 index2
2615 // -1 dict or list
2616 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2617 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2618 typval_T *tv_dest = STACK_TV_BOT(-1);
2619 int status = OK;
2620
2621 if (tv_dest->v_type == VAR_LIST)
2622 {
2623 // indexes must be a number
2624 SOURCING_LNUM = iptr->isn_lnum;
2625 if (check_for_number(tv_idx1) == FAIL
2626 || (tv_idx2->v_type != VAR_SPECIAL
2627 && check_for_number(tv_idx2) == FAIL))
2628 {
2629 status = FAIL;
2630 }
2631 else
2632 {
2633 list_T *l = tv_dest->vval.v_list;
2634 long n1 = (long)tv_idx1->vval.v_number;
2635 long n2 = tv_idx2->v_type == VAR_SPECIAL
2636 ? 0 : (long)tv_idx2->vval.v_number;
2637 listitem_T *li;
2638
2639 li = list_find_index(l, &n1);
2640 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002641 {
2642 semsg(_(e_list_index_out_of_range_nr),
2643 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002644 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002645 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002646 else
2647 {
2648 if (n1 < 0)
2649 n1 = list_idx_of_item(l, li);
2650 if (n2 < 0)
2651 {
2652 listitem_T *li2 = list_find(l, n2);
2653
2654 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002655 {
2656 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002657 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002658 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002659 else
2660 n2 = list_idx_of_item(l, li2);
2661 }
2662 if (status != FAIL
2663 && tv_idx2->v_type != VAR_SPECIAL
2664 && n2 < n1)
2665 {
2666 semsg(_(e_list_index_out_of_range_nr), n2);
2667 status = FAIL;
2668 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002669 if (status != FAIL)
2670 list_unlet_range(l, li, n1,
2671 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002672 }
2673 }
2674 }
2675 else
2676 {
2677 status = FAIL;
2678 SOURCING_LNUM = iptr->isn_lnum;
2679 semsg(_(e_cannot_index_str),
2680 vartype_name(tv_dest->v_type));
2681 }
2682
2683 clear_tv(tv_idx1);
2684 clear_tv(tv_idx2);
2685 clear_tv(tv_dest);
2686 ectx->ec_stack.ga_len -= 3;
2687
2688 return status;
2689}
2690
2691/*
2692 * Top of a for loop.
2693 */
2694 static int
2695execute_for(isn_T *iptr, ectx_T *ectx)
2696{
2697 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002698 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002699 typval_T *ltv = STACK_TV_BOT(-1);
2700 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002701 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002702
2703 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2704 return FAIL;
2705 if (ltv->v_type == VAR_LIST)
2706 {
2707 list_T *list = ltv->vval.v_list;
2708
2709 // push the next item from the list
2710 ++idxtv->vval.v_number;
2711 if (list == NULL
2712 || idxtv->vval.v_number >= list->lv_len)
2713 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002714 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002715 }
2716 else if (list->lv_first == &range_list_item)
2717 {
2718 // non-materialized range() list
2719 tv = STACK_TV_BOT(0);
2720 tv->v_type = VAR_NUMBER;
2721 tv->v_lock = 0;
2722 tv->vval.v_number = list_find_nr(
2723 list, idxtv->vval.v_number, NULL);
2724 ++ectx->ec_stack.ga_len;
2725 }
2726 else
2727 {
2728 listitem_T *li = list_find(list,
2729 idxtv->vval.v_number);
2730
2731 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2732 ++ectx->ec_stack.ga_len;
2733 }
2734 }
2735 else if (ltv->v_type == VAR_STRING)
2736 {
2737 char_u *str = ltv->vval.v_string;
2738
2739 // The index is for the last byte of the previous
2740 // character.
2741 ++idxtv->vval.v_number;
2742 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2743 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002744 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002745 }
2746 else
2747 {
2748 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2749
2750 // Push the next character from the string.
2751 tv = STACK_TV_BOT(0);
2752 tv->v_type = VAR_STRING;
2753 tv->vval.v_string = vim_strnsave(
2754 str + idxtv->vval.v_number, clen);
2755 ++ectx->ec_stack.ga_len;
2756 idxtv->vval.v_number += clen - 1;
2757 }
2758 }
2759 else if (ltv->v_type == VAR_BLOB)
2760 {
2761 blob_T *blob = ltv->vval.v_blob;
2762
2763 // When we get here the first time make a copy of the
2764 // blob, so that the iteration still works when it is
2765 // changed.
2766 if (idxtv->vval.v_number == -1 && blob != NULL)
2767 {
2768 blob_copy(blob, ltv);
2769 blob_unref(blob);
2770 blob = ltv->vval.v_blob;
2771 }
2772
2773 // The index is for the previous byte.
2774 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002775 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002776 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002777 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002778 }
2779 else
2780 {
2781 // Push the next byte from the blob.
2782 tv = STACK_TV_BOT(0);
2783 tv->v_type = VAR_NUMBER;
2784 tv->vval.v_number = blob_get(blob,
2785 idxtv->vval.v_number);
2786 ++ectx->ec_stack.ga_len;
2787 }
2788 }
2789 else
2790 {
2791 semsg(_(e_for_loop_on_str_not_supported),
2792 vartype_name(ltv->v_type));
2793 return FAIL;
2794 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002795
2796 if (jump)
2797 {
2798 // past the end of the list/string/blob, jump to "endfor"
2799 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2800 may_restore_cmdmod(&ectx->ec_funclocal);
2801 }
2802 else
2803 {
2804 // Store the current number of funcrefs, this may be used in
2805 // ISN_LOOPEND. The variable index is always one more than the loop
2806 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002807 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002808 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2809 }
2810
2811 return OK;
2812}
2813
2814/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002815 * Code for handling variables declared inside a loop and used in a closure.
2816 * This is very similar to what is done with funcstack_T. The difference is
2817 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2818 * scope of the block inside a loop and each loop may have its own.
2819 */
2820
2821// Double linked list of loopvars_T in use.
2822static loopvars_T *first_loopvars = NULL;
2823
2824 static void
2825add_loopvars_to_list(loopvars_T *loopvars)
2826{
2827 // Link in list of loopvarss.
2828 if (first_loopvars != NULL)
2829 first_loopvars->lvs_prev = loopvars;
2830 loopvars->lvs_next = first_loopvars;
2831 loopvars->lvs_prev = NULL;
2832 first_loopvars = loopvars;
2833}
2834
2835 static void
2836remove_loopvars_from_list(loopvars_T *loopvars)
2837{
2838 if (loopvars->lvs_prev == NULL)
2839 first_loopvars = loopvars->lvs_next;
2840 else
2841 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2842 if (loopvars->lvs_next != NULL)
2843 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2844}
2845
2846/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002847 * End of a for or while loop: Handle any variables used by a closure.
2848 */
2849 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002850execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002851{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002852 endloop_T *endloop = &iptr->isn_arg.endloop;
2853 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2854 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002855 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002856 garray_T *gap = &ectx->ec_funcrefs;
2857 int closure_in_use = FALSE;
2858 loopvars_T *loopvars;
2859 typval_T *stack;
2860 int idx;
2861
Bram Moolenaarcc341812022-09-19 15:54:34 +01002862 // Check if any created closure is still being referenced and loopvars have
2863 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002864 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2865 {
2866 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2867
Bram Moolenaarcc341812022-09-19 15:54:34 +01002868 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002869 {
2870 int refcount = pt->pt_refcount;
2871 int i;
2872
2873 // A Reference in a variable inside the loop doesn't count, it gets
2874 // unreferenced at the end of the loop.
2875 for (i = 0; i < endloop->end_var_count; ++i)
2876 {
2877 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2878
2879 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2880 --refcount;
2881 }
2882 if (refcount > 1)
2883 {
2884 closure_in_use = TRUE;
2885 break;
2886 }
2887 }
2888 }
2889
2890 // If no function reference were created since the start of the loop block
2891 // or it is no longer referenced there is nothing to do.
2892 if (!closure_in_use)
2893 return OK;
2894
2895 // A closure is using variables declared inside the loop.
2896 // Move them to the called function.
2897 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2898 if (loopvars == NULL)
2899 return FAIL;
2900
2901 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2902 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2903 loopvars->lvs_ga.ga_data = stack;
2904 if (stack == NULL)
2905 {
2906 vim_free(loopvars);
2907 return FAIL;
2908 }
2909 add_loopvars_to_list(loopvars);
2910
2911 // Move the variable values.
2912 for (idx = 0; idx < endloop->end_var_count; ++idx)
2913 {
2914 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2915
2916 *(stack + idx) = *tv;
2917 tv->v_type = VAR_UNKNOWN;
2918 }
2919
2920 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2921 {
2922 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2923
Bram Moolenaarcc341812022-09-19 15:54:34 +01002924 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002925 {
2926 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002927 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002928
Bram Moolenaarcc341812022-09-19 15:54:34 +01002929 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2930 pt->pt_outer.out_loop[depth].var_idx -=
2931 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002932 }
2933 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002934
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002935 return OK;
2936}
2937
2938/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002939 * Called when a partial is freed or its reference count goes down to one. The
2940 * loopvars may be the only reference to the partials in the local variables.
2941 * Go over all of them, the funcref and can be freed if all partials
2942 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002943 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002944 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002945 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002946loopvars_check_refcount(loopvars_T *loopvars)
2947{
2948 int i;
2949 garray_T *gap = &loopvars->lvs_ga;
2950 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002951 typval_T *stack = gap->ga_data;
2952
Bram Moolenaarcc341812022-09-19 15:54:34 +01002953 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2954 return FALSE;
2955 for (i = 0; i < gap->ga_len; ++i)
2956 {
2957 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2958
2959 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2960 && tv->vval.v_partial->pt_refcount == 1)
2961 {
2962 int depth;
2963
2964 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2965 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2966 ++done;
2967 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002968 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002969 if (done != loopvars->lvs_min_refcount)
2970 return FALSE;
2971
2972 // All partials referencing the loopvars have a reference count of
2973 // one, thus the loopvars is no longer of use.
2974 stack = gap->ga_data;
2975 for (i = 0; i < gap->ga_len; ++i)
2976 clear_tv(stack + i);
2977 vim_free(stack);
2978 remove_loopvars_from_list(loopvars);
2979 vim_free(loopvars);
2980 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002981}
2982
2983/*
2984 * For garbage collecting: set references in all variables referenced by
2985 * all loopvars.
2986 */
2987 int
2988set_ref_in_loopvars(int copyID)
2989{
2990 loopvars_T *loopvars;
2991
2992 for (loopvars = first_loopvars; loopvars != NULL;
2993 loopvars = loopvars->lvs_next)
2994 {
2995 typval_T *stack = loopvars->lvs_ga.ga_data;
2996 int i;
2997
2998 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2999 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
3000 return TRUE; // abort
3001 }
3002 return FALSE;
3003}
3004
3005/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00003006 * Load instruction for w:/b:/g:/t: variable.
3007 * "isn_type" is used instead of "iptr->isn_type".
3008 */
3009 static int
3010load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
3011{
3012 dictitem_T *di = NULL;
3013 hashtab_T *ht = NULL;
3014 char namespace;
3015
3016 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3017 return NOTDONE;
3018 switch (isn_type)
3019 {
3020 case ISN_LOADG:
3021 ht = get_globvar_ht();
3022 namespace = 'g';
3023 break;
3024 case ISN_LOADB:
3025 ht = &curbuf->b_vars->dv_hashtab;
3026 namespace = 'b';
3027 break;
3028 case ISN_LOADW:
3029 ht = &curwin->w_vars->dv_hashtab;
3030 namespace = 'w';
3031 break;
3032 case ISN_LOADT:
3033 ht = &curtab->tp_vars->dv_hashtab;
3034 namespace = 't';
3035 break;
3036 default: // Cannot reach here
3037 return NOTDONE;
3038 }
3039 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3040
Bram Moolenaar06b77222022-01-25 15:51:56 +00003041 if (di == NULL)
3042 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003043 if (isn_type == ISN_LOADG)
3044 {
3045 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3046
3047 // g:Something could be a function
3048 if (ufunc != NULL)
3049 {
3050 typval_T *tv = STACK_TV_BOT(0);
3051
3052 ++ectx->ec_stack.ga_len;
3053 tv->v_type = VAR_FUNC;
3054 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3055 if (tv->vval.v_string == NULL)
3056 return FAIL;
3057 STRCPY(tv->vval.v_string, "g:");
3058 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3059 return OK;
3060 }
3061 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003062 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003063 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003064 // no check if the item exists in the script but
3065 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003066 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003067 else
3068 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003069 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003070 return FAIL;
3071 }
3072 else
3073 {
3074 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3075 ++ectx->ec_stack.ga_len;
3076 }
3077 return OK;
3078}
3079
Bram Moolenaard0200c82023-01-28 15:19:40 +00003080
3081 static void
3082object_required_error(typval_T *tv)
3083{
3084 garray_T type_list;
3085 ga_init2(&type_list, sizeof(type_T *), 10);
3086 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3087 char *tofree = NULL;
3088 char *typename = type_name(type, &tofree);
3089 semsg(_(e_object_required_found_str), typename);
3090 vim_free(tofree);
3091 clear_type_list(&type_list);
3092}
3093
Bram Moolenaar06b77222022-01-25 15:51:56 +00003094/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003095 * Execute instructions in execution context "ectx".
3096 * Return OK or FAIL;
3097 */
3098 static int
3099exec_instructions(ectx_T *ectx)
3100{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003101 int ret = FAIL;
3102 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003103 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003104
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003105 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003106 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003107
Bram Moolenaarff652882021-05-16 15:24:49 +02003108 // Only catch exceptions in this instruction list.
3109 ectx->ec_trylevel_at_start = trylevel;
3110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 for (;;)
3112 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003113 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003115 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003116
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003117 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003118 {
3119 line_breakcheck();
3120 breakcheck_count = 0;
3121 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003122 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003123 {
3124 // Turn CTRL-C into an exception.
3125 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003126 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003127 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003128 did_throw = TRUE;
3129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003131 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003132 {
3133 // Turn an error message into an exception.
3134 did_emsg = FALSE;
3135 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003136 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003137 did_throw = TRUE;
3138 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003139
3140 // This exception was not caught (yet).
3141 garray_T *trystack = &ectx->ec_trystack;
3142 if (trystack->ga_len > 0)
3143 {
3144 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3145 + trystack->ga_len - 1;
3146 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3147 trycmd->tcd_caught = FALSE;
3148 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003149 }
3150
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003151 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003153 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003154 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003155 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156
3157 // An exception jumps to the first catch, finally, or returns from
3158 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003159 while (index > 0)
3160 {
3161 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003162 // 1. after :try and before :catch - jump to first :catch
3163 // 2. in :catch block - jump to :finally
3164 // 3. in :catch block and no finally - jump to :endtry
3165 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3166 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003167 break;
3168 // In the catch and finally block of this try we have to go up
3169 // one level.
3170 --index;
3171 trycmd = NULL;
3172 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003173 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003175 if (trycmd->tcd_in_catch)
3176 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003177 if (trycmd->tcd_finally_idx > 0)
3178 {
3179 // exception inside ":catch", jump to ":finally" once
3180 ectx->ec_iidx = trycmd->tcd_finally_idx;
3181 trycmd->tcd_finally_idx = 0;
3182 }
3183 else
3184 {
3185 // exception inside ":catch" or ":finally", jump to
3186 // ":endtry"
3187 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3188 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003189 }
3190 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003191 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003192 // jump to first ":catch"
3193 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003194 trycmd->tcd_in_catch = TRUE;
3195 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003196 did_throw = FALSE; // don't come back here until :endtry
3197 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 }
3199 else
3200 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003201 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003202 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003203 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003204 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003205 tv = STACK_TV_BOT(0);
3206 tv->v_type = VAR_NUMBER;
3207 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003208 ++ectx->ec_stack.ga_len;
3209 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003211 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003212 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003213 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003214 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 goto done;
3216 }
3217
Bram Moolenaar4c137212021-04-19 16:48:48 +02003218 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003219 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 }
3221 continue;
3222 }
3223
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003224 /*
3225 * Big switch on the instruction. Most compilers will be turning this
3226 * into an efficient lookup table, since the "case" values are an enum
3227 * with sequential numbers. It may look ugly, but it should be the
3228 * most efficient way.
3229 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003230 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 switch (iptr->isn_type)
3232 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003233 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003234 case ISN_CONSTRUCT:
3235 // "this" is always the local variable at index zero
3236 tv = STACK_TV_VAR(0);
3237 tv->v_type = VAR_OBJECT;
3238 tv->vval.v_object = alloc_clear(
3239 iptr->isn_arg.construct.construct_size);
3240 tv->vval.v_object->obj_class =
3241 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003242 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003243 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003244 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003245 break;
3246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 // execute Ex command line
3248 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003249 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003250 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 break;
3252
Bram Moolenaar20677332021-06-06 17:02:53 +02003253 // execute Ex command line split at NL characters.
3254 case ISN_EXEC_SPLIT:
3255 {
3256 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003257 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003258
3259 SOURCING_LNUM = iptr->isn_lnum;
3260 CLEAR_FIELD(cookie);
3261 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3262 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003263 line = get_split_sourceline(0, &cookie, 0, 0);
3264 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003265 get_split_sourceline, &cookie,
3266 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3267 == FAIL
3268 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003269 {
3270 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003271 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003272 }
3273 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003274 }
3275 break;
3276
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003277 // execute Ex command line that is only a range
3278 case ISN_EXECRANGE:
3279 {
3280 exarg_T ea;
3281 char *error = NULL;
3282
3283 CLEAR_FIELD(ea);
3284 ea.cmdidx = CMD_SIZE;
3285 ea.addr_type = ADDR_LINES;
3286 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003287 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003288 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003289 if (ea.cmd == NULL)
3290 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003291 // error is always NULL when using ADDR_LINES
3292 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003293 if (error != NULL)
3294 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003295 emsg(error);
3296 goto on_error;
3297 }
3298 }
3299 break;
3300
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003301 // Evaluate an expression with legacy syntax, push it onto the
3302 // stack.
3303 case ISN_LEGACY_EVAL:
3304 {
3305 char_u *arg = iptr->isn_arg.string;
3306 int res;
3307 int save_flags = cmdmod.cmod_flags;
3308
Bram Moolenaar35578162021-08-02 19:10:38 +02003309 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003310 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003311 tv = STACK_TV_BOT(0);
3312 init_tv(tv);
3313 cmdmod.cmod_flags |= CMOD_LEGACY;
3314 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3315 cmdmod.cmod_flags = save_flags;
3316 if (res == FAIL)
3317 goto on_error;
3318 ++ectx->ec_stack.ga_len;
3319 }
3320 break;
3321
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003322 // push typeval VAR_INSTR with instructions to be executed
3323 case ISN_INSTR:
3324 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003325 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003326 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003327 tv = STACK_TV_BOT(0);
3328 tv->vval.v_instr = ALLOC_ONE(instr_T);
3329 if (tv->vval.v_instr == NULL)
3330 goto on_error;
3331 ++ectx->ec_stack.ga_len;
3332
3333 tv->v_type = VAR_INSTR;
3334 tv->vval.v_instr->instr_ectx = ectx;
3335 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3336 }
3337 break;
3338
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003339 case ISN_SOURCE:
3340 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003341 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003342
Bram Moolenaar89445512022-04-14 12:58:23 +01003343 SOURCING_LNUM = iptr->isn_lnum;
3344 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003345 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003346 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003347 }
3348 break;
3349
Bram Moolenaar4c137212021-04-19 16:48:48 +02003350 // execute :substitute with an expression
3351 case ISN_SUBSTITUTE:
3352 {
3353 subs_T *subs = &iptr->isn_arg.subs;
3354 source_cookie_T cookie;
3355 struct subs_expr_S *save_instr = substitute_instr;
3356 struct subs_expr_S subs_instr;
3357 int res;
3358
3359 subs_instr.subs_ectx = ectx;
3360 subs_instr.subs_instr = subs->subs_instr;
3361 subs_instr.subs_status = OK;
3362 substitute_instr = &subs_instr;
3363
3364 SOURCING_LNUM = iptr->isn_lnum;
3365 // This is very much like ISN_EXEC
3366 CLEAR_FIELD(cookie);
3367 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3368 res = do_cmdline(subs->subs_cmd,
3369 getsourceline, &cookie,
3370 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3371 substitute_instr = save_instr;
3372
3373 if (res == FAIL || did_emsg
3374 || subs_instr.subs_status == FAIL)
3375 goto on_error;
3376 }
3377 break;
3378
3379 case ISN_FINISH:
3380 goto done;
3381
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003382 case ISN_REDIRSTART:
3383 // create a dummy entry for var_redir_str()
3384 if (alloc_redir_lval() == FAIL)
3385 goto on_error;
3386
3387 // The output is stored in growarray "redir_ga" until
3388 // redirection ends.
3389 init_redir_ga();
3390 redir_vname = 1;
3391 break;
3392
3393 case ISN_REDIREND:
3394 {
3395 char_u *res = get_clear_redir_ga();
3396
3397 // End redirection, put redirected text on the stack.
3398 clear_redir_lval();
3399 redir_vname = 0;
3400
Bram Moolenaar35578162021-08-02 19:10:38 +02003401 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003402 {
3403 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003404 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003405 }
3406 tv = STACK_TV_BOT(0);
3407 tv->v_type = VAR_STRING;
3408 tv->vval.v_string = res;
3409 ++ectx->ec_stack.ga_len;
3410 }
3411 break;
3412
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003413 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003414#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003415 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003416 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3417 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003418 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003419#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003420 break;
3421
3422 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003423#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003424 {
3425 exarg_T ea;
3426 int res;
3427
3428 CLEAR_FIELD(ea);
3429 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3430 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3431 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3432 --ectx->ec_stack.ga_len;
3433 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003434 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003435 res = cexpr_core(&ea, tv);
3436 clear_tv(tv);
3437 if (res == FAIL)
3438 goto on_error;
3439 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003440#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003441 break;
3442
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003443 // execute Ex command from pieces on the stack
3444 case ISN_EXECCONCAT:
3445 {
3446 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003447 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003448 int pass;
3449 int i;
3450 char_u *cmd = NULL;
3451 char_u *str;
3452
3453 for (pass = 1; pass <= 2; ++pass)
3454 {
3455 for (i = 0; i < count; ++i)
3456 {
3457 tv = STACK_TV_BOT(i - count);
3458 str = tv->vval.v_string;
3459 if (str != NULL && *str != NUL)
3460 {
3461 if (pass == 2)
3462 STRCPY(cmd + len, str);
3463 len += STRLEN(str);
3464 }
3465 if (pass == 2)
3466 clear_tv(tv);
3467 }
3468 if (pass == 1)
3469 {
3470 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003471 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003472 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003473 len = 0;
3474 }
3475 }
3476
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003477 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003478 do_cmdline_cmd(cmd);
3479 vim_free(cmd);
3480 }
3481 break;
3482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483 // execute :echo {string} ...
3484 case ISN_ECHO:
3485 {
3486 int count = iptr->isn_arg.echo.echo_count;
3487 int atstart = TRUE;
3488 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003489 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490
3491 for (idx = 0; idx < count; ++idx)
3492 {
3493 tv = STACK_TV_BOT(idx - count);
3494 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3495 &atstart, &needclr);
3496 clear_tv(tv);
3497 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003498 if (needclr)
3499 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003500 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 }
3502 break;
3503
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003504 // :execute {string} ...
3505 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003506 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003507 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003508 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003509 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003510 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003511 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003512 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003513 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003514 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003515 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003516 garray_T ga;
3517 char_u buf[NUMBUFLEN];
3518 char_u *p;
3519 int len;
3520 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003521 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003522
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003523 if (iptr->isn_type == ISN_ECHOWINDOW)
3524 count = iptr->isn_arg.echowin.ewin_count;
3525 else
3526 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003527 ga_init2(&ga, 1, 80);
3528 for (idx = 0; idx < count; ++idx)
3529 {
3530 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003531 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003532 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003533 if (tv->v_type == VAR_CHANNEL
3534 || tv->v_type == VAR_JOB)
3535 {
3536 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003537 semsg(_(e_using_invalid_value_as_string_str),
3538 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003539 break;
3540 }
3541 else
3542 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003543 }
3544 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003545 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003546
3547 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003548 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003549 failed = TRUE;
3550 else
3551 {
3552 if (ga.ga_len > 0)
3553 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3554 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3555 ga.ga_len += len;
3556 }
3557 clear_tv(tv);
3558 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003559 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003560 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003561 {
3562 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003563 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003564 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003565
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003566 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003567 {
3568 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003569 {
3570 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003571 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003572 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003573 {
3574 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003575 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003576 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003577 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003578 else
3579 {
3580 msg_sb_eol();
3581 if (iptr->isn_type == ISN_ECHOMSG)
3582 {
3583 msg_attr(ga.ga_data, echo_attr);
3584 out_flush();
3585 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003586#ifdef HAS_MESSAGE_WINDOW
3587 else if (iptr->isn_type == ISN_ECHOWINDOW)
3588 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003589 start_echowindow(
3590 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003591 msg_attr(ga.ga_data, echo_attr);
3592 end_echowindow();
3593 }
3594#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003595 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3596 {
3597 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3598 TRUE);
3599 ui_write((char_u *)"\r\n", 2, TRUE);
3600 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003601 else
3602 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003603 SOURCING_LNUM = iptr->isn_lnum;
3604 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003605 }
3606 }
3607 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003608 ga_clear(&ga);
3609 }
3610 break;
3611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 // load local variable or argument
3613 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003614 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003615 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003616 tv = STACK_TV_VAR(iptr->isn_arg.number);
3617 if (tv->v_type == VAR_UNKNOWN)
3618 {
3619 // missing argument or default value v:none
3620 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3621 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3622 }
3623 else
3624 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003625 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 break;
3627
3628 // load v: variable
3629 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003630 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003631 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003633 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 break;
3635
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003636 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 case ISN_LOADSCRIPT:
3638 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003639 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 svar_T *sv;
3641
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003642 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003643 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003644 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003645 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003646 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003647 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003649 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 }
3651 break;
3652
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003653 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003655 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003657 int sid = iptr->isn_arg.loadstore.ls_sid;
3658 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003659 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 if (di == NULL)
3663 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003664 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003665 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003666 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 }
3668 else
3669 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003670 if (iptr->isn_type == ISN_LOADEXPORT)
3671 {
3672 int idx = get_script_item_idx(sid, name, 0,
3673 NULL, NULL);
3674 svar_T *sv;
3675
3676 if (idx >= 0)
3677 {
3678 sv = ((svar_T *)SCRIPT_ITEM(sid)
3679 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003680 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003681 {
3682 SOURCING_LNUM = iptr->isn_lnum;
3683 semsg(_(e_item_not_exported_in_script_str),
3684 name);
3685 goto on_error;
3686 }
3687 }
3688 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003689 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003690 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003692 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 }
3694 }
3695 break;
3696
Bram Moolenaard3aac292020-04-19 14:32:17 +02003697 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003699 case ISN_LOADB:
3700 case ISN_LOADW:
3701 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003703 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003704
Bram Moolenaar06b77222022-01-25 15:51:56 +00003705 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003706 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003707 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003708 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 break;
3712
Bram Moolenaar03290b82020-12-19 16:30:44 +01003713 // load autoload variable
3714 case ISN_LOADAUTO:
3715 {
3716 char_u *name = iptr->isn_arg.string;
3717
Bram Moolenaar35578162021-08-02 19:10:38 +02003718 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003719 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003720 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003721 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003722 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003723 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003724 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003725 }
3726 break;
3727
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003728 // load g:/b:/w:/t: namespace
3729 case ISN_LOADGDICT:
3730 case ISN_LOADBDICT:
3731 case ISN_LOADWDICT:
3732 case ISN_LOADTDICT:
3733 {
3734 dict_T *d = NULL;
3735
3736 switch (iptr->isn_type)
3737 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003738 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3739 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3740 case ISN_LOADWDICT: d = curwin->w_vars; break;
3741 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003742 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003743 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003744 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003745 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003746 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003747 tv = STACK_TV_BOT(0);
3748 tv->v_type = VAR_DICT;
3749 tv->v_lock = 0;
3750 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003751 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003752 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003753 }
3754 break;
3755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 // load &option
3757 case ISN_LOADOPT:
3758 {
3759 typval_T optval;
3760 char_u *name = iptr->isn_arg.string;
3761
Bram Moolenaara8c17702020-04-01 21:17:24 +02003762 // This is not expected to fail, name is checked during
3763 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003764 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003765 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003766 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003767 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003769 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 }
3771 break;
3772
3773 // load $ENV
3774 case ISN_LOADENV:
3775 {
3776 typval_T optval;
3777 char_u *name = iptr->isn_arg.string;
3778
Bram Moolenaar35578162021-08-02 19:10:38 +02003779 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003780 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003781 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003782 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003784 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 }
3786 break;
3787
3788 // load @register
3789 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003790 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003791 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 tv = STACK_TV_BOT(0);
3793 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003794 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003795 // This may result in NULL, which should be equivalent to an
3796 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 tv->vval.v_string = get_reg_contents(
3798 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003799 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 break;
3801
3802 // store local variable
3803 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003804 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 tv = STACK_TV_VAR(iptr->isn_arg.number);
3806 clear_tv(tv);
3807 *tv = *STACK_TV_BOT(0);
3808 break;
3809
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003810 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003811 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003812 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003813 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003814 int sid = iptr->isn_arg.loadstore.ls_sid;
3815 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003816 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003817 dictitem_T *di = find_var_in_ht(ht, 0,
3818 iptr->isn_type == ISN_STORES
3819 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003820
Bram Moolenaar4c137212021-04-19 16:48:48 +02003821 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003822 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003823 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003824 {
3825 if (iptr->isn_type == ISN_STOREEXPORT)
3826 {
3827 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003828 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003829 goto on_error;
3830 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003831 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003832 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003833 else
3834 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003835 if (iptr->isn_type == ISN_STOREEXPORT)
3836 {
3837 int idx = get_script_item_idx(sid, name, 0,
3838 NULL, NULL);
3839
Bram Moolenaar06651632022-04-27 17:54:25 +01003840 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003841 if (idx >= 0)
3842 {
3843 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3844 ->sn_var_vals.ga_data) + idx;
3845
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003846 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003847 {
3848 semsg(_(e_item_not_exported_in_script_str),
3849 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003850 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003851 goto on_error;
3852 }
3853 }
3854 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003855 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003856 {
3857 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003858 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003859 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003860 clear_tv(&di->di_tv);
3861 di->di_tv = *STACK_TV_BOT(0);
3862 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003863 }
3864 break;
3865
3866 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 case ISN_STORESCRIPT:
3868 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003869 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3870 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003872 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003873 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003874 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003875 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003876
3877 // "const" and "final" are checked at compile time, locking
3878 // the value needs to be checked here.
3879 SOURCING_LNUM = iptr->isn_lnum;
3880 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003881 {
3882 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003883 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003884 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 clear_tv(sv->sv_tv);
3887 *sv->sv_tv = *STACK_TV_BOT(0);
3888 }
3889 break;
3890
3891 // store option
3892 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003893 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003895 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3896 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 long n = 0;
3898 char_u *s = NULL;
3899 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003900 char_u numbuf[NUMBUFLEN];
3901 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902
Bram Moolenaar4c137212021-04-19 16:48:48 +02003903 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003904 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 tv = STACK_TV_BOT(0);
3906 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003907 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003909 if (s == NULL)
3910 s = (char_u *)"";
3911 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003912 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3913 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003914 // If the option can be set to a function reference or
3915 // a lambda and the passed value is a function
3916 // reference, then convert it to the name (string) of
3917 // the function reference.
3918 s = tv2string(tv, &tofree, numbuf, 0);
3919 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003920 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003921 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003922 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003923 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003924 goto on_error;
3925 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003928 // must be VAR_NUMBER, CHECKTYPE makes sure
3929 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003930 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003931 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003932 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 if (msg != NULL)
3934 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003935 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003937 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 }
3940 break;
3941
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003942 // store $ENV
3943 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003944 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003945 tv = STACK_TV_BOT(0);
3946 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3947 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003948 break;
3949
3950 // store @r
3951 case ISN_STOREREG:
3952 {
3953 int reg = iptr->isn_arg.number;
3954
Bram Moolenaar4c137212021-04-19 16:48:48 +02003955 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003956 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003957 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003958 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003959 }
3960 break;
3961
3962 // store v: variable
3963 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003964 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003965 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3966 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003967 // should not happen, type is checked when compiling
3968 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003969 break;
3970
Bram Moolenaard3aac292020-04-19 14:32:17 +02003971 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003973 case ISN_STOREB:
3974 case ISN_STOREW:
3975 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003977 dictitem_T *di;
3978 hashtab_T *ht;
3979 char_u *name = iptr->isn_arg.string + 2;
3980
Bram Moolenaard3aac292020-04-19 14:32:17 +02003981 switch (iptr->isn_type)
3982 {
3983 case ISN_STOREG:
3984 ht = get_globvar_ht();
3985 break;
3986 case ISN_STOREB:
3987 ht = &curbuf->b_vars->dv_hashtab;
3988 break;
3989 case ISN_STOREW:
3990 ht = &curwin->w_vars->dv_hashtab;
3991 break;
3992 case ISN_STORET:
3993 ht = &curtab->tp_vars->dv_hashtab;
3994 break;
3995 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003996 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003997 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998
Bram Moolenaar4c137212021-04-19 16:48:48 +02003999 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004000 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004002 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 else
4004 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004005 SOURCING_LNUM = iptr->isn_lnum;
4006 if (var_check_permission(di, name) == FAIL)
4007 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 clear_tv(&di->di_tv);
4009 di->di_tv = *STACK_TV_BOT(0);
4010 }
4011 }
4012 break;
4013
Bram Moolenaar03290b82020-12-19 16:30:44 +01004014 // store an autoload variable
4015 case ISN_STOREAUTO:
4016 SOURCING_LNUM = iptr->isn_lnum;
4017 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
4018 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004019 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004020 break;
4021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 // store number in local variable
4023 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004024 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 clear_tv(tv);
4026 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004027 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 break;
4029
Bram Moolenaar590162c2022-12-24 21:24:06 +00004030 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004031 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004032 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004033 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004034
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004035 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004036 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004037 if (res == NOTDONE)
4038 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004039 }
4040 break;
4041
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004042 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004043 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004044 if (execute_storerange(iptr, ectx) == FAIL)
4045 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004046 break;
4047
Bram Moolenaard505d172022-12-18 21:42:55 +00004048 case ISN_LOAD_CLASSMEMBER:
4049 {
4050 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4051 goto theend;
4052 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004053 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4054 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004055 ++ectx->ec_stack.ga_len;
4056 }
4057 break;
4058
4059 case ISN_STORE_CLASSMEMBER:
4060 {
4061 classmember_T *cm = &iptr->isn_arg.classmember;
4062 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4063 clear_tv(tv);
4064 *tv = *STACK_TV_BOT(-1);
4065 --ectx->ec_stack.ga_len;
4066 }
4067 break;
4068
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004069 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004070 case ISN_LOADOUTER:
4071 case ISN_STOREOUTER:
4072 {
4073 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004074 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4075 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004076
4077 while (depth > 1 && outer != NULL)
4078 {
4079 outer = outer->out_up;
4080 --depth;
4081 }
4082 if (outer == NULL)
4083 {
4084 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004085 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4086 || ectx->ec_outer_ref == NULL)
4087 // Possibly :def function called from legacy
4088 // context.
4089 emsg(_(e_closure_called_from_invalid_context));
4090 else
4091 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004092 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004093 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004094 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004095 // Variable declared in loop. May be copied if the
4096 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004097 tv = ((typval_T *)outer->out_loop[-depth - 1]
4098 .stack->ga_data)
4099 + outer->out_loop[-depth - 1].var_idx
4100 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004101 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004102 // Variable declared in a function. May be copied if
4103 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004104 tv = ((typval_T *)outer->out_stack->ga_data)
4105 + outer->out_frame_idx + STACK_FRAME_SIZE
4106 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004107 if (iptr->isn_type == ISN_LOADOUTER)
4108 {
Bram Moolenaar35578162021-08-02 19:10:38 +02004109 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004110 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004111 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004112 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004113 }
4114 else
4115 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004116 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004117 clear_tv(tv);
4118 *tv = *STACK_TV_BOT(0);
4119 }
4120 }
4121 break;
4122
Bram Moolenaar752fc692021-01-04 21:57:11 +01004123 // unlet item in list or dict variable
4124 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004125 if (execute_unletindex(iptr, ectx) == FAIL)
4126 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004127 break;
4128
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004129 // unlet range of items in list variable
4130 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004131 if (execute_unletrange(iptr, ectx) == FAIL)
4132 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004133 break;
4134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 // push constant
4136 case ISN_PUSHNR:
4137 case ISN_PUSHBOOL:
4138 case ISN_PUSHSPEC:
4139 case ISN_PUSHF:
4140 case ISN_PUSHS:
4141 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004142 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004143 case ISN_PUSHCHANNEL:
4144 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004145 case ISN_PUSHOBJ:
4146 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004147 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004148 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004150 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004151 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 switch (iptr->isn_type)
4153 {
4154 case ISN_PUSHNR:
4155 tv->v_type = VAR_NUMBER;
4156 tv->vval.v_number = iptr->isn_arg.number;
4157 break;
4158 case ISN_PUSHBOOL:
4159 tv->v_type = VAR_BOOL;
4160 tv->vval.v_number = iptr->isn_arg.number;
4161 break;
4162 case ISN_PUSHSPEC:
4163 tv->v_type = VAR_SPECIAL;
4164 tv->vval.v_number = iptr->isn_arg.number;
4165 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 case ISN_PUSHF:
4167 tv->v_type = VAR_FLOAT;
4168 tv->vval.v_float = iptr->isn_arg.fnumber;
4169 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 case ISN_PUSHBLOB:
4171 blob_copy(iptr->isn_arg.blob, tv);
4172 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004173 case ISN_PUSHFUNC:
4174 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004175 if (iptr->isn_arg.string == NULL)
4176 tv->vval.v_string = NULL;
4177 else
4178 tv->vval.v_string =
4179 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004180 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004181 case ISN_PUSHCHANNEL:
4182#ifdef FEAT_JOB_CHANNEL
4183 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004184 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004185#endif
4186 break;
4187 case ISN_PUSHJOB:
4188#ifdef FEAT_JOB_CHANNEL
4189 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004190 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004191#endif
4192 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004193 case ISN_PUSHOBJ:
4194 tv->v_type = VAR_OBJECT;
4195 tv->vval.v_object = NULL;
4196 break;
4197 case ISN_PUSHCLASS:
4198 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004199 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004200 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 default:
4202 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004203 tv->vval.v_string = iptr->isn_arg.string == NULL
4204 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 }
4206 break;
4207
Bram Moolenaar06b77222022-01-25 15:51:56 +00004208 case ISN_AUTOLOAD:
4209 {
4210 char_u *name = iptr->isn_arg.string;
4211
4212 (void)script_autoload(name, FALSE);
4213 if (find_func(name, TRUE))
4214 {
4215 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4216 goto theend;
4217 tv = STACK_TV_BOT(0);
4218 tv->v_lock = 0;
4219 ++ectx->ec_stack.ga_len;
4220 tv->v_type = VAR_FUNC;
4221 tv->vval.v_string = vim_strsave(name);
4222 }
4223 else
4224 {
4225 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4226
4227 if (res == NOTDONE)
4228 goto theend;
4229 if (res == FAIL)
4230 goto on_error;
4231 }
4232 }
4233 break;
4234
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004235 case ISN_UNLET:
4236 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4237 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004238 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004239 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004240 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004241 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004242 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004243
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004244 case ISN_LOCKUNLOCK:
4245 {
Ernie Raelee865f32023-09-29 19:53:55 +02004246#ifdef LOG_LOCKVAR
4247 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4248 iptr->isn_arg.string);
4249#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004250 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004251
4252 // Stack has the local variable, argument the whole :lock
4253 // or :unlock command, like ISN_EXEC.
4254 --ectx->ec_stack.ga_len;
Ernie Rael4c8da022023-10-11 21:35:11 +02004255 lval_root_T root = { .lr_tv = STACK_TV_BOT(0),
4256 .lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec,
4257 .lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg };
Ernie Rael64885642023-10-04 20:16:22 +02004258 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004259 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004260 iptr->isn_arg.lockunlock.lu_string);
4261 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004262 lval_root = lval_root_save;
4263 if (res == FAIL)
4264 goto on_error;
4265 }
4266 break;
4267
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004268 case ISN_LOCKCONST:
4269 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4270 break;
4271
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 // create a list from items on the stack; uses a single allocation
4273 // for the list header and the items
4274 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004275 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004276 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 break;
4278
4279 // create a dict from items on the stack
4280 case ISN_NEWDICT:
4281 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004282 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004284 SOURCING_LNUM = iptr->isn_lnum;
4285 res = exe_newdict(iptr->isn_arg.number, ectx);
4286 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004287 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004288 if (res == MAYBE)
4289 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 }
4291 break;
4292
LemonBoy372bcce2022-04-25 12:43:20 +01004293 case ISN_CONCAT:
4294 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4295 goto theend;
4296 break;
4297
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004298 // create a partial with NULL value
4299 case ISN_NEWPARTIAL:
4300 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4301 goto theend;
4302 ++ectx->ec_stack.ga_len;
4303 tv = STACK_TV_BOT(-1);
4304 tv->v_type = VAR_PARTIAL;
4305 tv->v_lock = 0;
4306 tv->vval.v_partial = NULL;
4307 break;
4308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 // call a :def function
4310 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004311 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004312 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4313 NULL,
4314 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004315 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004316 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 break;
4318
Bram Moolenaard0200c82023-01-28 15:19:40 +00004319 // call a method on an interface
4320 case ISN_METHODCALL:
4321 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004322 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4323
Bram Moolenaard0200c82023-01-28 15:19:40 +00004324 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004325 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004326 if (tv->v_type != VAR_OBJECT)
4327 {
4328 object_required_error(tv);
4329 goto on_error;
4330 }
4331 object_T *obj = tv->vval.v_object;
4332 class_T *cl = obj->obj_class;
4333
4334 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004335 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004336 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004337
4338 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4339 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4340 goto on_error;
4341 }
4342 break;
4343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 // call a builtin function
4345 case ISN_BCALL:
4346 SOURCING_LNUM = iptr->isn_lnum;
4347 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4348 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004349 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004350 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 break;
4352
4353 // call a funcref or partial
4354 case ISN_PCALL:
4355 {
4356 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4357 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004358 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359
4360 SOURCING_LNUM = iptr->isn_lnum;
4361 if (pfunc->cpf_top)
4362 {
4363 // funcref is above the arguments
4364 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4365 }
4366 else
4367 {
4368 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004369 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004370 partial_tv = *STACK_TV_BOT(0);
4371 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004373 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004374 if (tv == &partial_tv)
4375 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004377 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 }
4379 break;
4380
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004381 case ISN_PCALL_END:
4382 // PCALL finished, arguments have been consumed and replaced by
4383 // the return value. Now clear the funcref from the stack,
4384 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004385 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004386 clear_tv(STACK_TV_BOT(-1));
4387 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4388 break;
4389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 // call a user defined function or funcref/partial
4391 case ISN_UCALL:
4392 {
4393 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4394
4395 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004396 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004397 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004398 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 }
4400 break;
4401
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004402 // :defer func(arg)
4403 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004404 case ISN_DEFEROBJ:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004405 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004406 iptr->isn_type == ISN_DEFEROBJ,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004407 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4408 goto on_error;
4409 break;
4410
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004411 // Return from a :def function call without a value.
4412 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004413 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004414 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004415 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004416 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004417 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004418 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004419 if (iptr->isn_type == ISN_RETURN_VOID)
4420 {
4421 tv->v_type = VAR_VOID;
4422 tv->vval.v_number = 0;
4423 tv->v_lock = 0;
4424 }
4425 else
4426 {
4427 *tv = *STACK_TV_VAR(0);
4428 ++tv->vval.v_object->obj_refcount;
4429 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004430 // FALLTHROUGH
4431
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004432 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 case ISN_RETURN:
4434 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004435 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004436 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004437
4438 if (trystack->ga_len > 0)
4439 trycmd = ((trycmd_T *)trystack->ga_data)
4440 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004441 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004442 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004444 // jump to ":finally" or ":endtry"
4445 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004446 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004447 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004448 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 trycmd->tcd_return = TRUE;
4450 }
4451 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004452 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 }
4454 break;
4455
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004456 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 case ISN_FUNCREF:
4458 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004459 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4460 ufunc_T *ufunc;
4461 funcref_T *funcref = &iptr->isn_arg.funcref;
4462 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004465 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004466 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004467 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004468 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004469 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004470 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004471 if (extra != NULL && extra->fre_class != NULL)
4472 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004473 class_T *cl;
4474 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004475 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004476 tv = STACK_TV_BOT(-1);
4477 if (tv->v_type != VAR_OBJECT)
4478 {
4479 object_required_error(tv);
4480 vim_free(pt);
4481 goto on_error;
4482 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004483
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004484 object_T *obj = tv->vval.v_object;
4485 cl = obj->obj_class;
4486 // drop the value from the stack
4487 clear_tv(tv);
4488 --ectx->ec_stack.ga_len;
4489
4490 pt->pt_obj = obj;
4491 ++obj->obj_refcount;
4492 }
4493 else
4494 cl = extra->fre_class;
4495
4496 if (extra->fre_object_method)
4497 {
4498 // object method
4499 // convert the interface index to the object index
4500 int idx =
4501 object_index_from_itf_index(extra->fre_class,
4502 TRUE, extra->fre_method_idx, cl);
4503 ufunc = cl->class_obj_methods[idx];
4504 }
4505 else
4506 {
4507 // class method
4508 ufunc =
4509 cl->class_class_functions[extra->fre_method_idx];
4510 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004511 }
4512 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004513 {
4514 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4515 + funcref->fr_dfunc_idx;
4516
4517 ufunc = pt_dfunc->df_ufunc;
4518 }
4519 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004520 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004521 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004522 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004523 if (ufunc == NULL)
4524 {
4525 SOURCING_LNUM = iptr->isn_lnum;
4526 iemsg("ufunc unexpectedly NULL for FUNCREF");
4527 goto theend;
4528 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004529 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004530 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004531 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004532 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004534 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 tv->vval.v_partial = pt;
4536 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004537 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 }
4539 break;
4540
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004541 // Create a global function from a lambda.
4542 case ISN_NEWFUNC:
4543 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004544 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004545
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004546 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004547 arg->nfa_global, &arg->nfa_loopvar_info,
4548 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004549 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004550 }
4551 break;
4552
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004553 // List functions
4554 case ISN_DEF:
4555 if (iptr->isn_arg.string == NULL)
4556 list_functions(NULL);
4557 else
4558 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004559 exarg_T ea;
4560 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004561
4562 CLEAR_FIELD(ea);
4563 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004564 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004565 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004566 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004567 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004568 }
4569 break;
4570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 // jump if a condition is met
4572 case ISN_JUMP:
4573 {
4574 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004575 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 int jump = TRUE;
4577
4578 if (when != JUMP_ALWAYS)
4579 {
4580 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004581 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004582 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004583 || when == JUMP_IF_COND_TRUE)
4584 {
4585 SOURCING_LNUM = iptr->isn_lnum;
4586 jump = tv_get_bool_chk(tv, &error);
4587 if (error)
4588 goto on_error;
4589 }
4590 else
4591 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004592 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004594 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 {
4596 // drop the value from the stack
4597 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004598 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 }
4600 }
4601 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004602 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 }
4604 break;
4605
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004606 // "while": jump to end if a condition is false
4607 case ISN_WHILE:
4608 {
4609 int error = FALSE;
4610 int jump = TRUE;
4611
4612 tv = STACK_TV_BOT(-1);
4613 SOURCING_LNUM = iptr->isn_lnum;
4614 jump = !tv_get_bool_chk(tv, &error);
4615 if (error)
4616 goto on_error;
4617 // drop the value from the stack
4618 clear_tv(tv);
4619 --ectx->ec_stack.ga_len;
4620 if (jump)
4621 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4622
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004623 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004624 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004625 tv = STACK_TV_VAR(
4626 iptr->isn_arg.whileloop.while_funcref_idx);
4627 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4628 }
4629 break;
4630
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004631 // Jump if an argument with a default value was already set and not
4632 // v:none.
4633 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004634 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004635 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004636 int arg_set = tv->v_type != VAR_UNKNOWN
4637 && !(tv->v_type == VAR_SPECIAL
4638 && tv->vval.v_number == VVAL_NONE);
4639 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004640 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004641 break;
4642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 // top of a for loop
4644 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004645 if (execute_for(iptr, ectx) == FAIL)
4646 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 break;
4648
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004649 // end of a for or while loop
4650 case ISN_ENDLOOP:
4651 if (execute_endloop(iptr, ectx) == FAIL)
4652 goto theend;
4653 break;
4654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 // start of ":try" block
4656 case ISN_TRY:
4657 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004658 trycmd_T *trycmd = NULL;
4659
Bram Moolenaar35578162021-08-02 19:10:38 +02004660 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004661 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004662 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4663 + ectx->ec_trystack.ga_len;
4664 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004666 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004667 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4668 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004669 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004670 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004671 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004672 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004673 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004674 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 }
4676 break;
4677
4678 case ISN_PUSHEXC:
4679 if (current_exception == NULL)
4680 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004681 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004683 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004685 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004686 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004688 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004690 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 tv->vval.v_string = vim_strsave(
4692 (char_u *)current_exception->value);
4693 break;
4694
4695 case ISN_CATCH:
4696 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004697 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004698 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699
Bram Moolenaar4c137212021-04-19 16:48:48 +02004700 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004701 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004703 trycmd->tcd_caught = TRUE;
4704 trycmd->tcd_did_throw = FALSE;
4705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004707 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 catch_exception(current_exception);
4709 }
4710 break;
4711
Bram Moolenaarc150c092021-02-13 15:02:46 +01004712 case ISN_TRYCONT:
4713 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004714 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004715 trycont_T *trycont = &iptr->isn_arg.trycont;
4716 int i;
4717 trycmd_T *trycmd;
4718 int iidx = trycont->tct_where;
4719
4720 if (trystack->ga_len < trycont->tct_levels)
4721 {
4722 siemsg("TRYCONT: expected %d levels, found %d",
4723 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004724 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004725 }
4726 // Make :endtry jump to any outer try block and the last
4727 // :endtry inside the loop to the loop start.
4728 for (i = trycont->tct_levels; i > 0; --i)
4729 {
4730 trycmd = ((trycmd_T *)trystack->ga_data)
4731 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004732 // Add one to tcd_cont to be able to jump to
4733 // instruction with index zero.
4734 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004735 iidx = trycmd->tcd_finally_idx == 0
4736 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004737 }
4738 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004739 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004740 }
4741 break;
4742
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004743 case ISN_FINALLY:
4744 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004745 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004746 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4747 + trystack->ga_len - 1;
4748
4749 // Reset the index to avoid a return statement jumps here
4750 // again.
4751 trycmd->tcd_finally_idx = 0;
4752 break;
4753 }
4754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 // end of ":try" block
4756 case ISN_ENDTRY:
4757 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004758 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004759 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760
Bram Moolenaar06651632022-04-27 17:54:25 +01004761 --trystack->ga_len;
4762 --trylevel;
4763 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4764 if (trycmd->tcd_did_throw)
4765 did_throw = TRUE;
4766 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004768 // discard the exception
4769 if (caught_stack == current_exception)
4770 caught_stack = caught_stack->caught;
4771 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004773
4774 if (trycmd->tcd_return)
4775 goto func_return;
4776
4777 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4778 {
4779 --ectx->ec_stack.ga_len;
4780 clear_tv(STACK_TV_BOT(0));
4781 }
4782 if (trycmd->tcd_cont != 0)
4783 // handling :continue: jump to outer try block or
4784 // start of the loop
4785 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 }
4787 break;
4788
4789 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004790 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004791 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004792
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004793 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4794 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004795 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004796 // the function to abort but not display an error.
4797 tv = STACK_TV_BOT(-1);
4798 clear_tv(tv);
4799 tv->v_type = VAR_NUMBER;
4800 tv->vval.v_number = 0;
4801 goto done;
4802 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004803 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004804 tv = STACK_TV_BOT(0);
4805 if (tv->vval.v_string == NULL
4806 || *skipwhite(tv->vval.v_string) == NUL)
4807 {
4808 vim_free(tv->vval.v_string);
4809 SOURCING_LNUM = iptr->isn_lnum;
4810 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004811 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004812 }
4813
4814 // Inside a "catch" we need to first discard the caught
4815 // exception.
4816 if (trystack->ga_len > 0)
4817 {
4818 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4819 + trystack->ga_len - 1;
4820 if (trycmd->tcd_caught && current_exception != NULL)
4821 {
4822 // discard the exception
4823 if (caught_stack == current_exception)
4824 caught_stack = caught_stack->caught;
4825 discard_current_exception();
4826 trycmd->tcd_caught = FALSE;
4827 }
4828 }
4829
Bram Moolenaar90a57162022-02-12 14:23:17 +00004830 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004831 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4832 == FAIL)
4833 {
4834 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004835 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004836 }
4837 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 break;
4840
4841 // compare with special values
4842 case ISN_COMPAREBOOL:
4843 case ISN_COMPARESPECIAL:
4844 {
4845 typval_T *tv1 = STACK_TV_BOT(-2);
4846 typval_T *tv2 = STACK_TV_BOT(-1);
4847 varnumber_T arg1 = tv1->vval.v_number;
4848 varnumber_T arg2 = tv2->vval.v_number;
4849 int res;
4850
Bram Moolenaar06651632022-04-27 17:54:25 +01004851 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4852 res = arg1 == arg2;
4853 else
4854 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855
Bram Moolenaar4c137212021-04-19 16:48:48 +02004856 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 tv1->v_type = VAR_BOOL;
4858 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4859 }
4860 break;
4861
Bram Moolenaar7a222242022-03-01 19:23:24 +00004862 case ISN_COMPARENULL:
4863 {
4864 typval_T *tv1 = STACK_TV_BOT(-2);
4865 typval_T *tv2 = STACK_TV_BOT(-1);
4866 int res;
4867
4868 res = typval_compare_null(tv1, tv2);
4869 if (res == MAYBE)
4870 goto on_error;
4871 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4872 res = !res;
4873 clear_tv(tv1);
4874 clear_tv(tv2);
4875 --ectx->ec_stack.ga_len;
4876 tv1->v_type = VAR_BOOL;
4877 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4878 }
4879 break;
4880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 // Operation with two number arguments
4882 case ISN_OPNR:
4883 case ISN_COMPARENR:
4884 {
4885 typval_T *tv1 = STACK_TV_BOT(-2);
4886 typval_T *tv2 = STACK_TV_BOT(-1);
4887 varnumber_T arg1 = tv1->vval.v_number;
4888 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004889 varnumber_T res = 0;
4890 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004892 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4893 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4894 {
4895 if (arg2 < 0)
4896 {
4897 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004898 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004899 goto on_error;
4900 }
4901 }
4902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 switch (iptr->isn_arg.op.op_type)
4904 {
4905 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004906 case EXPR_DIV: if (arg2 == 0)
4907 div_zero = TRUE;
4908 else
4909 res = arg1 / arg2;
4910 break;
4911 case EXPR_REM: if (arg2 == 0)
4912 div_zero = TRUE;
4913 else
4914 res = arg1 % arg2;
4915 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 case EXPR_SUB: res = arg1 - arg2; break;
4917 case EXPR_ADD: res = arg1 + arg2; break;
4918
4919 case EXPR_EQUAL: res = arg1 == arg2; break;
4920 case EXPR_NEQUAL: res = arg1 != arg2; break;
4921 case EXPR_GREATER: res = arg1 > arg2; break;
4922 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4923 case EXPR_SMALLER: res = arg1 < arg2; break;
4924 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004925 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4926 res = 0;
4927 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004928 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004929 break;
4930 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4931 res = 0;
4932 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004933 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004934 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004935 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 }
4937
Bram Moolenaar4c137212021-04-19 16:48:48 +02004938 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 if (iptr->isn_type == ISN_COMPARENR)
4940 {
4941 tv1->v_type = VAR_BOOL;
4942 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4943 }
4944 else
4945 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004946 if (div_zero)
4947 {
4948 SOURCING_LNUM = iptr->isn_lnum;
4949 emsg(_(e_divide_by_zero));
4950 goto on_error;
4951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952 }
4953 break;
4954
4955 // Computation with two float arguments
4956 case ISN_OPFLOAT:
4957 case ISN_COMPAREFLOAT:
4958 {
4959 typval_T *tv1 = STACK_TV_BOT(-2);
4960 typval_T *tv2 = STACK_TV_BOT(-1);
4961 float_T arg1 = tv1->vval.v_float;
4962 float_T arg2 = tv2->vval.v_float;
4963 float_T res = 0;
4964 int cmp = FALSE;
4965
4966 switch (iptr->isn_arg.op.op_type)
4967 {
4968 case EXPR_MULT: res = arg1 * arg2; break;
4969 case EXPR_DIV: res = arg1 / arg2; break;
4970 case EXPR_SUB: res = arg1 - arg2; break;
4971 case EXPR_ADD: res = arg1 + arg2; break;
4972
4973 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4974 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4975 case EXPR_GREATER: cmp = arg1 > arg2; break;
4976 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4977 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4978 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4979 default: cmp = 0; break;
4980 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004981 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 if (iptr->isn_type == ISN_COMPAREFLOAT)
4983 {
4984 tv1->v_type = VAR_BOOL;
4985 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4986 }
4987 else
4988 tv1->vval.v_float = res;
4989 }
4990 break;
4991
4992 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004993 case ISN_COMPAREDICT:
4994 case ISN_COMPAREFUNC:
4995 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004997 case ISN_COMPARECLASS:
4998 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 {
5000 typval_T *tv1 = STACK_TV_BOT(-2);
5001 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00005002 exprtype_T exprtype = iptr->isn_arg.op.op_type;
5003 int ic = iptr->isn_arg.op.op_ic;
5004 int res = FALSE;
5005 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006
Bram Moolenaar265f8112021-12-19 12:33:05 +00005007 SOURCING_LNUM = iptr->isn_lnum;
5008 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005010 status = typval_compare_list(tv1, tv2,
5011 exprtype, ic, &res);
5012 }
5013 else if (iptr->isn_type == ISN_COMPAREDICT)
5014 {
5015 status = typval_compare_dict(tv1, tv2,
5016 exprtype, ic, &res);
5017 }
5018 else if (iptr->isn_type == ISN_COMPAREFUNC)
5019 {
5020 status = typval_compare_func(tv1, tv2,
5021 exprtype, ic, &res);
5022 }
5023 else if (iptr->isn_type == ISN_COMPARESTRING)
5024 {
5025 status = typval_compare_string(tv1, tv2,
5026 exprtype, ic, &res);
5027 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005028 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005029 {
5030 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005032 else if (iptr->isn_type == ISN_COMPARECLASS)
5033 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005034 status = typval_compare_class(tv1, tv2,
5035 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005036 }
5037 else // ISN_COMPAREOBJECT
5038 {
5039 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005040 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005041 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005042 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043 clear_tv(tv1);
5044 clear_tv(tv2);
5045 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005046 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5047 if (status == FAIL)
5048 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 }
5050 break;
5051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052 case ISN_COMPAREANY:
5053 {
5054 typval_T *tv1 = STACK_TV_BOT(-2);
5055 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005056 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005058 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059
Bram Moolenaareb26f432020-09-14 16:50:05 +02005060 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005061 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005063 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005064 if (status == FAIL)
5065 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 }
5067 break;
5068
5069 case ISN_ADDLIST:
5070 case ISN_ADDBLOB:
5071 {
5072 typval_T *tv1 = STACK_TV_BOT(-2);
5073 typval_T *tv2 = STACK_TV_BOT(-1);
5074
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005075 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005077 {
5078 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5079 && tv1->vval.v_list != NULL)
5080 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5081 NULL);
5082 else
5083 eval_addlist(tv1, tv2);
5084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 else
5086 eval_addblob(tv1, tv2);
5087 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005088 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 }
5090 break;
5091
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005092 case ISN_LISTAPPEND:
5093 {
5094 typval_T *tv1 = STACK_TV_BOT(-2);
5095 typval_T *tv2 = STACK_TV_BOT(-1);
5096 list_T *l = tv1->vval.v_list;
5097
5098 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005099 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005100 if (l == NULL)
5101 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005102 emsg(_(e_cannot_add_to_null_list));
5103 goto on_error;
5104 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005105 if (value_check_lock(l->lv_lock, NULL, FALSE))
5106 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005107 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005108 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005109 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005110 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005111 }
5112 break;
5113
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005114 case ISN_BLOBAPPEND:
5115 {
5116 typval_T *tv1 = STACK_TV_BOT(-2);
5117 typval_T *tv2 = STACK_TV_BOT(-1);
5118 blob_T *b = tv1->vval.v_blob;
5119 int error = FALSE;
5120 varnumber_T n;
5121
5122 // add a number to a blob
5123 if (b == NULL)
5124 {
5125 SOURCING_LNUM = iptr->isn_lnum;
5126 emsg(_(e_cannot_add_to_null_blob));
5127 goto on_error;
5128 }
5129 n = tv_get_number_chk(tv2, &error);
5130 if (error)
5131 goto on_error;
5132 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005133 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005134 }
5135 break;
5136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 // Computation with two arguments of unknown type
5138 case ISN_OPANY:
5139 {
5140 typval_T *tv1 = STACK_TV_BOT(-2);
5141 typval_T *tv2 = STACK_TV_BOT(-1);
5142 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005143 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 int error = FALSE;
5145
5146 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5147 {
5148 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5149 {
5150 eval_addlist(tv1, tv2);
5151 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005152 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153 break;
5154 }
5155 else if (tv1->v_type == VAR_BLOB
5156 && tv2->v_type == VAR_BLOB)
5157 {
5158 eval_addblob(tv1, tv2);
5159 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005160 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161 break;
5162 }
5163 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164 if (tv1->v_type == VAR_FLOAT)
5165 {
5166 f1 = tv1->vval.v_float;
5167 n1 = 0;
5168 }
5169 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005171 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172 n1 = tv_get_number_chk(tv1, &error);
5173 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005174 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005175 if (tv2->v_type == VAR_FLOAT)
5176 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178 if (tv2->v_type == VAR_FLOAT)
5179 {
5180 f2 = tv2->vval.v_float;
5181 n2 = 0;
5182 }
5183 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184 {
5185 n2 = tv_get_number_chk(tv2, &error);
5186 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005187 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005188 if (tv1->v_type == VAR_FLOAT)
5189 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005191 // if there is a float on either side the result is a float
5192 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5193 {
5194 switch (iptr->isn_arg.op.op_type)
5195 {
5196 case EXPR_MULT: f1 = f1 * f2; break;
5197 case EXPR_DIV: f1 = f1 / f2; break;
5198 case EXPR_SUB: f1 = f1 - f2; break;
5199 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005200 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005201 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005202 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203 }
5204 clear_tv(tv1);
5205 clear_tv(tv2);
5206 tv1->v_type = VAR_FLOAT;
5207 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005208 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209 }
5210 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005212 int failed = FALSE;
5213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 switch (iptr->isn_arg.op.op_type)
5215 {
5216 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005217 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5218 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005219 goto on_error;
5220 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 case EXPR_SUB: n1 = n1 - n2; break;
5222 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005223 default: n1 = num_modulus(n1, n2, &failed);
5224 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005225 goto on_error;
5226 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227 }
5228 clear_tv(tv1);
5229 clear_tv(tv2);
5230 tv1->v_type = VAR_NUMBER;
5231 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005232 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 }
5234 }
5235 break;
5236
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005237 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005238 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005239 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005240 int is_slice = iptr->isn_type == ISN_STRSLICE;
5241 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005242 char_u *res;
5243
5244 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005245 // string slice: string is at stack-3, first index at
5246 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005247 if (is_slice)
5248 {
5249 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005250 n1 = tv->vval.v_number;
5251 }
5252
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005253 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005254 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005255
Bram Moolenaar4c137212021-04-19 16:48:48 +02005256 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005257 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005258 if (is_slice)
5259 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005260 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005261 else
5262 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005263 // single character (including composing characters).
5264 // If the index is too big or negative the result is
5265 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005266 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005267 vim_free(tv->vval.v_string);
5268 tv->vval.v_string = res;
5269 }
5270 break;
5271
5272 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005273 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005274 case ISN_BLOBINDEX:
5275 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005276 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005277 int is_slice = iptr->isn_type == ISN_LISTSLICE
5278 || iptr->isn_type == ISN_BLOBSLICE;
5279 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5280 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005281 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005282 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005283
5284 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005285 // list slice: list is at stack-3, indexes at stack-2 and
5286 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005287 // Same for blob.
5288 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289
5290 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005291 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005293
5294 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295 {
Bram Moolenaared591872020-08-15 22:14:53 +02005296 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005297 n1 = tv->vval.v_number;
5298 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005299 }
Bram Moolenaared591872020-08-15 22:14:53 +02005300
Bram Moolenaar4c137212021-04-19 16:48:48 +02005301 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005302 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005303 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005304 if (is_blob)
5305 {
5306 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5307 n1, n2, FALSE, tv) == FAIL)
5308 goto on_error;
5309 }
5310 else
5311 {
5312 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5313 n1, n2, FALSE, tv, TRUE) == FAIL)
5314 goto on_error;
5315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005316 }
5317 break;
5318
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005319 case ISN_ANYINDEX:
5320 case ISN_ANYSLICE:
5321 {
5322 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5323 typval_T *var1, *var2;
5324 int res;
5325
5326 // index: composite is at stack-2, index at stack-1
5327 // slice: composite is at stack-3, indexes at stack-2 and
5328 // stack-1
5329 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005330 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005331 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5332 goto on_error;
5333 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5334 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005335 res = eval_index_inner(tv, is_slice, var1, var2,
5336 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005337 clear_tv(var1);
5338 if (is_slice)
5339 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005340 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005341 if (res == FAIL)
5342 goto on_error;
5343 }
5344 break;
5345
Bram Moolenaar9af78762020-06-16 11:34:42 +02005346 case ISN_SLICE:
5347 {
5348 list_T *list;
5349 int count = iptr->isn_arg.number;
5350
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005351 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005352 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005353 list = tv->vval.v_list;
5354
5355 // no error for short list, expect it to be checked earlier
5356 if (list != NULL && list->lv_len >= count)
5357 {
5358 list_T *newlist = list_slice(list,
5359 count, list->lv_len - 1);
5360
5361 if (newlist != NULL)
5362 {
5363 list_unref(list);
5364 tv->vval.v_list = newlist;
5365 ++newlist->lv_refcount;
5366 }
5367 }
5368 }
5369 break;
5370
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005371 case ISN_GETITEM:
5372 {
5373 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005374 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005375
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005376 // Get list item: list is at stack-1, push item.
5377 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005378 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5379 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005380
Bram Moolenaar35578162021-08-02 19:10:38 +02005381 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005382 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005383 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005384 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005385
5386 // Useful when used in unpack assignment. Reset at
5387 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005388 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005389 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005390 }
5391 break;
5392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393 case ISN_MEMBER:
5394 {
5395 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005396 char_u *key;
5397 dictitem_T *di;
5398
5399 // dict member: dict is at stack-2, key at stack-1
5400 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005401 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005402 dict = tv->vval.v_dict;
5403
5404 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005405 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005406 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005407 if (key == NULL)
5408 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005409
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005410 if ((di = dict_find(dict, key, -1)) == NULL)
5411 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005412 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005413 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005414
5415 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005416 // stack contents makes sense and the dict stack is
5417 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005418 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005419 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005420 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005421 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005422 tv->v_type = VAR_NUMBER;
5423 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005424 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005425 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005426 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005427 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005428 // Put the dict used on the dict stack, it might be used by
5429 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005430 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005431 if (dict_stack_save(tv) == FAIL)
5432 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005433 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005434 }
5435 break;
5436
5437 // dict member with string key
5438 case ISN_STRINGMEMBER:
5439 {
5440 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005441 dictitem_T *di;
5442
5443 tv = STACK_TV_BOT(-1);
5444 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5445 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005446 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005447 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005448 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449 }
5450 dict = tv->vval.v_dict;
5451
5452 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5453 == NULL)
5454 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005455 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005456 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005457 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005458 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005460 // Put the dict used on the dict stack, it might be used by
5461 // a dict function later.
5462 if (dict_stack_save(tv) == FAIL)
5463 goto on_fatal_error;
5464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005465 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005466 }
5467 break;
5468
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005469 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005470 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005471 {
5472 tv = STACK_TV_BOT(-1);
5473 if (tv->v_type != VAR_OBJECT)
5474 {
5475 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005476 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005477 goto on_error;
5478 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005479
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005480 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005481 if (obj == NULL)
5482 {
5483 SOURCING_LNUM = iptr->isn_lnum;
5484 emsg(_(e_using_null_object));
5485 goto on_error;
5486 }
5487
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005488 int idx;
5489 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005490 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005491 else
5492 {
5493 idx = iptr->isn_arg.classmember.cm_idx;
5494 // convert the interface index to the object index
5495 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005496 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005497 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005498 }
5499
Ernie Rael18143d32023-09-04 22:30:41 +02005500 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005501 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005502 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005503
5504 // Unreference the object after getting the member, it may
5505 // be freed.
5506 object_unref(obj);
5507 }
5508 break;
5509
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005510 case ISN_STORE_THIS:
5511 {
5512 int idx = iptr->isn_arg.number;
5513 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5514 // the members are located right after the object struct
5515 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5516 clear_tv(mtv);
5517 *mtv = *STACK_TV_BOT(-1);
5518 --ectx->ec_stack.ga_len;
5519 }
5520 break;
5521
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005522 case ISN_CLEARDICT:
5523 dict_stack_drop();
5524 break;
5525
5526 case ISN_USEDICT:
5527 {
5528 typval_T *dict_tv = dict_stack_get_tv();
5529
5530 // Turn "dict.Func" into a partial for "Func" bound to
5531 // "dict". Don't do this when "Func" is already a partial
5532 // that was bound explicitly (pt_auto is FALSE).
5533 tv = STACK_TV_BOT(-1);
5534 if (dict_tv != NULL
5535 && dict_tv->v_type == VAR_DICT
5536 && dict_tv->vval.v_dict != NULL
5537 && (tv->v_type == VAR_FUNC
5538 || (tv->v_type == VAR_PARTIAL
5539 && (tv->vval.v_partial->pt_auto
5540 || tv->vval.v_partial->pt_dict == NULL))))
5541 dict_tv->vval.v_dict =
5542 make_partial(dict_tv->vval.v_dict, tv);
5543 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544 }
5545 break;
5546
5547 case ISN_NEGATENR:
5548 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005549 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005550 if (tv->v_type == VAR_FLOAT)
5551 tv->vval.v_float = -tv->vval.v_float;
5552 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005553 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005554 break;
5555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556 case ISN_CHECKTYPE:
5557 {
5558 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005559 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005560 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005561
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005562 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005563 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005564 if (ct->ct_arg_idx > 0)
5565 {
5566 where.wt_index = ct->ct_arg_idx;
5567 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005568 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005569 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005570 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005571 if (r == FAIL)
5572 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005573
5574 // number 0 is FALSE, number 1 is TRUE
5575 if (tv->v_type == VAR_NUMBER
5576 && ct->ct_type->tt_type == VAR_BOOL
5577 && (tv->vval.v_number == 0
5578 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005580 tv->v_type = VAR_BOOL;
5581 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005582 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005583 }
5584 }
5585 break;
5586
Bram Moolenaar9af78762020-06-16 11:34:42 +02005587 case ISN_CHECKLEN:
5588 {
5589 int min_len = iptr->isn_arg.checklen.cl_min_len;
5590 list_T *list = NULL;
5591
5592 tv = STACK_TV_BOT(-1);
5593 if (tv->v_type == VAR_LIST)
5594 list = tv->vval.v_list;
5595 if (list == NULL || list->lv_len < min_len
5596 || (list->lv_len > min_len
5597 && !iptr->isn_arg.checklen.cl_more_OK))
5598 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005599 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005600 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005601 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005602 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005603 }
5604 }
5605 break;
5606
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005607 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005608 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005609 break;
5610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005611 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005612 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613 {
5614 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005615 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005616
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005617 if (iptr->isn_type == ISN_2BOOL)
5618 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005619 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005620 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005621 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005622 n = !n;
5623 }
5624 else
5625 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005626 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005627 SOURCING_LNUM = iptr->isn_lnum;
5628 n = tv_get_bool_chk(tv, &error);
5629 if (error)
5630 goto on_error;
5631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005632 clear_tv(tv);
5633 tv->v_type = VAR_BOOL;
5634 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5635 }
5636 break;
5637
5638 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005639 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005640 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005641 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5642 iptr->isn_type == ISN_2STRING_ANY,
5643 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005644 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005645 break;
5646
Bram Moolenaar08597872020-12-10 19:43:40 +01005647 case ISN_RANGE:
5648 {
5649 exarg_T ea;
5650 char *errormsg;
5651
Bram Moolenaarece0b872021-01-08 20:40:45 +01005652 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005653 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005654 ea.addr_type = ADDR_LINES;
5655 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005656 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005657 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005658 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005659
Bram Moolenaar35578162021-08-02 19:10:38 +02005660 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005661 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005662 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005663 tv = STACK_TV_BOT(-1);
5664 tv->v_type = VAR_NUMBER;
5665 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005666 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005667 }
5668 break;
5669
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005670 case ISN_PUT:
5671 {
5672 int regname = iptr->isn_arg.put.put_regname;
5673 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5674 char_u *expr = NULL;
5675 int dir = FORWARD;
5676
Bram Moolenaar08597872020-12-10 19:43:40 +01005677 if (lnum < -2)
5678 {
5679 // line number was put on the stack by ISN_RANGE
5680 tv = STACK_TV_BOT(-1);
5681 curwin->w_cursor.lnum = tv->vval.v_number;
5682 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5683 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005684 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005685 }
5686 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005687 // :put! above cursor
5688 dir = BACKWARD;
5689 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005690 {
5691 curwin->w_cursor.lnum = lnum;
5692 if (lnum == 0)
5693 // check_cursor() below will move to line 1
5694 dir = BACKWARD;
5695 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005696
5697 if (regname == '=')
5698 {
5699 tv = STACK_TV_BOT(-1);
5700 if (tv->v_type == VAR_STRING)
5701 expr = tv->vval.v_string;
5702 else
5703 {
5704 expr = typval2string(tv, TRUE); // allocates value
5705 clear_tv(tv);
5706 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005707 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005708 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005709 check_cursor();
5710 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5711 vim_free(expr);
5712 }
5713 break;
5714
Bram Moolenaar02194d22020-10-24 23:08:38 +02005715 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005716 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5717 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5718 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5719 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005720 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5721 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005722 break;
5723
Bram Moolenaar02194d22020-10-24 23:08:38 +02005724 case ISN_CMDMOD_REV:
5725 // filter regprog is owned by the instruction, don't free it
5726 cmdmod.cmod_filter_regmatch.regprog = NULL;
5727 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005728 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5729 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005730 break;
5731
Bram Moolenaar792f7862020-11-23 08:31:18 +01005732 case ISN_UNPACK:
5733 {
5734 int count = iptr->isn_arg.unpack.unp_count;
5735 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5736 list_T *l;
5737 listitem_T *li;
5738 int i;
5739
5740 // Check there is a valid list to unpack.
5741 tv = STACK_TV_BOT(-1);
5742 if (tv->v_type != VAR_LIST)
5743 {
5744 SOURCING_LNUM = iptr->isn_lnum;
5745 emsg(_(e_for_argument_must_be_sequence_of_lists));
5746 goto on_error;
5747 }
5748 l = tv->vval.v_list;
5749 if (l == NULL
5750 || l->lv_len < (semicolon ? count - 1 : count))
5751 {
5752 SOURCING_LNUM = iptr->isn_lnum;
5753 emsg(_(e_list_value_does_not_have_enough_items));
5754 goto on_error;
5755 }
5756 else if (!semicolon && l->lv_len > count)
5757 {
5758 SOURCING_LNUM = iptr->isn_lnum;
5759 emsg(_(e_list_value_has_more_items_than_targets));
5760 goto on_error;
5761 }
5762
5763 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005764 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005765 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005766 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005767
5768 // Variable after semicolon gets a list with the remaining
5769 // items.
5770 if (semicolon)
5771 {
5772 list_T *rem_list =
5773 list_alloc_with_items(l->lv_len - count + 1);
5774
5775 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005776 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005777 tv = STACK_TV_BOT(-count);
5778 tv->vval.v_list = rem_list;
5779 ++rem_list->lv_refcount;
5780 tv->v_lock = 0;
5781 li = l->lv_first;
5782 for (i = 0; i < count - 1; ++i)
5783 li = li->li_next;
5784 for (i = 0; li != NULL; ++i)
5785 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005786 typval_T tvcopy;
5787
5788 copy_tv(&li->li_tv, &tvcopy);
5789 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005790 li = li->li_next;
5791 }
5792 --count;
5793 }
5794
5795 // Produce the values in reverse order, first item last.
5796 li = l->lv_first;
5797 for (i = 0; i < count; ++i)
5798 {
5799 tv = STACK_TV_BOT(-i - 1);
5800 copy_tv(&li->li_tv, tv);
5801 li = li->li_next;
5802 }
5803
5804 list_unref(l);
5805 }
5806 break;
5807
Bram Moolenaarb2049902021-01-24 12:53:53 +01005808 case ISN_PROF_START:
5809 case ISN_PROF_END:
5810 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005811#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005812 funccall_T cookie;
5813 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005814 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005815 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005816
Bram Moolenaarca16c602022-09-06 18:57:08 +01005817 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005818 if (iptr->isn_type == ISN_PROF_START)
5819 {
5820 func_line_start(&cookie, iptr->isn_lnum);
5821 // if we get here the instruction is executed
5822 func_line_exec(&cookie);
5823 }
5824 else
5825 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005826#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005827 }
5828 break;
5829
Bram Moolenaare99d4222021-06-13 14:01:26 +02005830 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005831 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005832 break;
5833
Bram Moolenaar389df252020-07-09 21:20:47 +02005834 case ISN_SHUFFLE:
5835 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005836 typval_T tmp_tv;
5837 int item = iptr->isn_arg.shuffle.shfl_item;
5838 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005839
5840 tmp_tv = *STACK_TV_BOT(-item);
5841 for ( ; up > 0 && item > 1; --up)
5842 {
5843 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5844 --item;
5845 }
5846 *STACK_TV_BOT(-item) = tmp_tv;
5847 }
5848 break;
5849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005851 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005853 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005854 break;
5855 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005856 continue;
5857
Bram Moolenaard032f342020-07-18 18:13:02 +02005858func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005859 // Restore previous function. If the frame pointer is where we started
5860 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005861 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005862 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005863
Bram Moolenaar4c137212021-04-19 16:48:48 +02005864 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005865 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005866 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005867 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005868
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005869on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005870 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005871 // If "emsg_silent" is set then ignore the error, unless it was set
5872 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005873 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005874 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005875 {
5876 // If a sequence of instructions causes an error while ":silent!"
5877 // was used, restore the stack length and jump ahead to restoring
5878 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005879 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005880 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005881 while (ectx->ec_stack.ga_len
5882 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005883 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005884 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005885 clear_tv(STACK_TV_BOT(0));
5886 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005887 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5888 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005889 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005890 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005891 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005892on_fatal_error:
5893 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005894 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005895 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005896 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897 }
5898
5899done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005900 ret = OK;
5901theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005902 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005903
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005904 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005905 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5906 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005907}
5908
5909/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005910 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005911 * "rettv".
5912 * Return OK or FAIL.
5913 */
5914 int
5915exe_typval_instr(typval_T *tv, typval_T *rettv)
5916{
5917 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5918 isn_T *save_instr = ectx->ec_instr;
5919 int save_iidx = ectx->ec_iidx;
5920 int res;
5921
LemonBoyf3b48952022-05-05 13:53:03 +01005922 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5923 // even when the compilation fails.
5924 rettv->v_type = VAR_UNKNOWN;
5925
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005926 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5927 res = exec_instructions(ectx);
5928 if (res == OK)
5929 {
5930 *rettv = *STACK_TV_BOT(-1);
5931 --ectx->ec_stack.ga_len;
5932 }
5933
5934 ectx->ec_instr = save_instr;
5935 ectx->ec_iidx = save_iidx;
5936
5937 return res;
5938}
5939
5940/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005941 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5942 * "substitute_instr".
5943 */
5944 char_u *
5945exe_substitute_instr(void)
5946{
5947 ectx_T *ectx = substitute_instr->subs_ectx;
5948 isn_T *save_instr = ectx->ec_instr;
5949 int save_iidx = ectx->ec_iidx;
5950 char_u *res;
5951
5952 ectx->ec_instr = substitute_instr->subs_instr;
5953 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005954 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005955 typval_T *tv = STACK_TV_BOT(-1);
5956
Bram Moolenaar27523602021-06-05 21:36:19 +02005957 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005958 --ectx->ec_stack.ga_len;
5959 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005960 }
5961 else
5962 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005963 substitute_instr->subs_status = FAIL;
5964 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005966
Bram Moolenaar4c137212021-04-19 16:48:48 +02005967 ectx->ec_instr = save_instr;
5968 ectx->ec_iidx = save_iidx;
5969
5970 return res;
5971}
5972
5973/*
5974 * Call a "def" function from old Vim script.
5975 * Return OK or FAIL.
5976 */
5977 int
5978call_def_function(
5979 ufunc_T *ufunc,
5980 int argc_arg, // nr of arguments
5981 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005982 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005983 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005984 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005985 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005986 typval_T *rettv) // return value
5987{
5988 ectx_T ectx; // execution context
5989 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005990 int partial_argc = partial == NULL
5991 || (flags & DEF_USE_PT_ARGV) == 0
5992 ? 0 : partial->pt_argc;
5993 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005994 typval_T *tv;
5995 int idx;
5996 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005997 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005998 sctx_T save_current_sctx = current_sctx;
5999 int did_emsg_before = did_emsg_cumul + did_emsg;
6000 int save_suppress_errthrow = suppress_errthrow;
6001 msglist_T **saved_msg_list = NULL;
6002 msglist_T *private_msg_list = NULL;
6003 int save_emsg_silent_def = emsg_silent_def;
6004 int save_did_emsg_def = did_emsg_def;
6005 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006006 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006007
6008// Get pointer to item in the stack.
6009#undef STACK_TV
6010#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6011
6012// Get pointer to item at the bottom of the stack, -1 is the bottom.
6013#undef STACK_TV_BOT
6014#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6015
6016// Get pointer to a local variable on the stack. Negative for arguments.
6017#undef STACK_TV_VAR
6018#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6019
6020 if (ufunc->uf_def_status == UF_NOT_COMPILED
6021 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006022 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6023 && compile_def_function(ufunc, FALSE,
6024 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006025 {
6026 if (did_emsg_cumul + did_emsg == did_emsg_before)
6027 semsg(_(e_function_is_not_compiled_str),
6028 printable_func_name(ufunc));
6029 return FAIL;
6030 }
6031
6032 {
6033 // Check the function was really compiled.
6034 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6035 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006036 if (dfunc->df_ufunc == NULL)
6037 {
6038 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6039 return FAIL;
6040 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006041 if (INSTRUCTIONS(dfunc) == NULL)
6042 {
6043 iemsg("using call_def_function() on not compiled function");
6044 return FAIL;
6045 }
6046 }
6047
6048 // If depth of calling is getting too high, don't execute the function.
6049 orig_funcdepth = funcdepth_get();
6050 if (funcdepth_increment() == FAIL)
6051 return FAIL;
6052
6053 CLEAR_FIELD(ectx);
6054 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6055 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006056 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006057 {
6058 funcdepth_decrement();
6059 return FAIL;
6060 }
6061 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6062 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6063 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006064 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006065
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006066 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006067 if (idx > 0 && ufunc->uf_va_name == NULL)
6068 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006069 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006070 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006071 goto failed_early;
6072 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006073 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006074 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006075 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006076 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006077 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006078 goto failed_early;
6079 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006080
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006081 // Put values from the partial and arguments on the stack, but no more than
6082 // what the function expects. A lambda can be called with more arguments
6083 // than it uses.
6084 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006085 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6086 ++idx)
6087 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006088 int argv_idx = idx - partial_argc;
6089
6090 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006091 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006092 && tv->v_type == VAR_SPECIAL
6093 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006094 {
6095 // Use the default value.
6096 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6097 }
6098 else
6099 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006100 int done = FALSE;
6101 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6102 {
6103 type_T *expected = ufunc->uf_arg_types[idx];
6104 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6105 {
6106 // When a float is expected and a number was given, convert
6107 // the value.
6108 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6109 STACK_TV_BOT(0)->v_lock = 0;
6110 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6111 done = TRUE;
6112 }
6113 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006114 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006115 goto failed_early;
6116 }
6117 if (!done)
6118 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006119 }
6120 ++ectx.ec_stack.ga_len;
6121 }
6122
6123 // Turn varargs into a list. Empty list if no args.
6124 if (ufunc->uf_va_name != NULL)
6125 {
6126 int vararg_count = argc - ufunc->uf_args.ga_len;
6127
6128 if (vararg_count < 0)
6129 vararg_count = 0;
6130 else
6131 argc -= vararg_count;
6132 if (exe_newlist(vararg_count, &ectx) == FAIL)
6133 goto failed_early;
6134
6135 // Check the type of the list items.
6136 tv = STACK_TV_BOT(-1);
6137 if (ufunc->uf_va_type != NULL
6138 && ufunc->uf_va_type != &t_list_any
6139 && ufunc->uf_va_type->tt_member != &t_any
6140 && tv->vval.v_list != NULL)
6141 {
6142 type_T *expected = ufunc->uf_va_type->tt_member;
6143 listitem_T *li = tv->vval.v_list->lv_first;
6144
6145 for (idx = 0; idx < vararg_count; ++idx)
6146 {
6147 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006148 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006149 goto failed_early;
6150 li = li->li_next;
6151 }
6152 }
6153
6154 if (defcount > 0)
6155 // Move varargs list to below missing default arguments.
6156 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6157 --ectx.ec_stack.ga_len;
6158 }
6159
6160 // Make space for omitted arguments, will store default value below.
6161 // Any varargs list goes after them.
6162 if (defcount > 0)
6163 for (idx = 0; idx < defcount; ++idx)
6164 {
6165 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6166 ++ectx.ec_stack.ga_len;
6167 }
6168 if (ufunc->uf_va_name != NULL)
6169 ++ectx.ec_stack.ga_len;
6170
6171 // Frame pointer points to just after arguments.
6172 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6173 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6174
6175 {
6176 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6177 + ufunc->uf_dfunc_idx;
6178 ufunc_T *base_ufunc = dfunc->df_ufunc;
6179
6180 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006181 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006182 if (partial != NULL || base_ufunc->uf_partial != NULL)
6183 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006184 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6185 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006186 goto failed_early;
6187 if (partial != NULL)
6188 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006189 outer_T *outer = get_pt_outer(partial);
6190
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006191 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006192 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006193 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006194 if (current_ectx != NULL)
6195 {
6196 if (current_ectx->ec_outer_ref != NULL
6197 && current_ectx->ec_outer_ref->or_outer != NULL)
6198 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006199 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006200 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006201 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006202 }
6203 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006204 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006205 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006206 ++partial->pt_refcount;
6207 ectx.ec_outer_ref->or_partial = partial;
6208 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006209 }
6210 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006211 {
6212 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6213 ++base_ufunc->uf_partial->pt_refcount;
6214 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6215 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006216 }
6217 }
6218
6219 // dummy frame entries
6220 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6221 {
6222 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6223 ++ectx.ec_stack.ga_len;
6224 }
6225
6226 {
6227 // Reserve space for local variables and any closure reference count.
6228 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6229 + ufunc->uf_dfunc_idx;
6230
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006231 // Initialize variables to zero. That avoids having to generate
6232 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006233 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006234 {
6235 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6236 STACK_TV_VAR(idx)->vval.v_number = 0;
6237 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006238 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006239
6240 if (object != NULL)
6241 {
6242 // the object is always the variable at index zero
6243 tv = STACK_TV_VAR(0);
6244 tv->v_type = VAR_OBJECT;
6245 tv->vval.v_object = object;
6246 }
6247
Bram Moolenaar4c137212021-04-19 16:48:48 +02006248 if (dfunc->df_has_closure)
6249 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006250 // Initialize the variable that counts how many closures were
6251 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006252 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6253 STACK_TV_VAR(idx)->vval.v_number = 0;
6254 ++ectx.ec_stack.ga_len;
6255 }
6256
6257 ectx.ec_instr = INSTRUCTIONS(dfunc);
6258 }
6259
Bram Moolenaar58779852022-09-06 18:31:14 +01006260 // Store the execution context in funccal, used by invoke_all_defer().
6261 if (funccal != NULL)
6262 funccal->fc_ectx = &ectx;
6263
Bram Moolenaar4c137212021-04-19 16:48:48 +02006264 // Following errors are in the function, not the caller.
6265 // Commands behave like vim9script.
6266 estack_push_ufunc(ufunc, 1);
6267 current_sctx = ufunc->uf_script_ctx;
6268 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6269
6270 // Use a specific location for storing error messages to be converted to an
6271 // exception.
6272 saved_msg_list = msg_list;
6273 msg_list = &private_msg_list;
6274
6275 // Do turn errors into exceptions.
6276 suppress_errthrow = FALSE;
6277
6278 // Do not delete the function while executing it.
6279 ++ufunc->uf_calls;
6280
6281 // When ":silent!" was used before calling then we still abort the
6282 // function. If ":silent!" is used in the function then we don't.
6283 emsg_silent_def = emsg_silent;
6284 did_emsg_def = 0;
6285
LemonBoyc5d27442023-08-19 13:02:35 +02006286 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006287
Bram Moolenaar5800c792022-09-22 17:34:01 +01006288 /*
6289 * Execute the instructions until done.
6290 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006291 ret = exec_instructions(&ectx);
6292 if (ret == OK)
6293 {
6294 // function finished, get result from the stack.
6295 if (ufunc->uf_ret_type == &t_void)
6296 {
6297 rettv->v_type = VAR_VOID;
6298 }
6299 else
6300 {
6301 tv = STACK_TV_BOT(-1);
6302 *rettv = *tv;
6303 tv->v_type = VAR_UNKNOWN;
6304 }
6305 }
6306
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006307 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006308 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006309
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006310 // Deal with any remaining closures, they may be in use somewhere.
6311 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006312 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006313 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006314 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006315 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006316
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006317 estack_pop();
6318 current_sctx = save_current_sctx;
6319
Bram Moolenaar2336c372021-12-06 15:06:54 +00006320 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6321 // Function was unreferenced while being used, free it now.
6322 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006323
Bram Moolenaar352134b2020-10-17 22:04:08 +02006324 if (*msg_list != NULL && saved_msg_list != NULL)
6325 {
6326 msglist_T **plist = saved_msg_list;
6327
6328 // Append entries from the current msg_list (uncaught exceptions) to
6329 // the saved msg_list.
6330 while (*plist != NULL)
6331 plist = &(*plist)->next;
6332
6333 *plist = *msg_list;
6334 }
6335 msg_list = saved_msg_list;
6336
Bram Moolenaar4c137212021-04-19 16:48:48 +02006337 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006338 {
6339 cmdmod.cmod_filter_regmatch.regprog = NULL;
6340 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006341 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006342 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006343 emsg_silent_def = save_emsg_silent_def;
6344 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006345
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006346failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006347 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006349 {
6350 tv = STACK_TV(idx);
6351 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6352 clear_tv(tv);
6353 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006354 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006357 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006358 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006359 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006360 if (ectx.ec_outer_ref->or_outer_allocated)
6361 vim_free(ectx.ec_outer_ref->or_outer);
6362 partial_unref(ectx.ec_outer_ref->or_partial);
6363 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006364 }
6365
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006366 // Not sure if this is necessary.
6367 suppress_errthrow = save_suppress_errthrow;
6368
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006369 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6370 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006371 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006372 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006373 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006374 return ret;
6375}
6376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006377/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006378 * Called when a def function has finished (possibly failed).
6379 * Invoke all the function returns to clean up and invoke deferred functions,
6380 * except the toplevel one.
6381 */
6382 void
6383unwind_def_callstack(ectx_T *ectx)
6384{
6385 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6386 func_return(ectx);
6387}
6388
6389/*
dundargocc57b5bc2022-11-02 13:30:51 +00006390 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006391 */
6392 void
6393may_invoke_defer_funcs(ectx_T *ectx)
6394{
6395 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6396
6397 if (dfunc->df_defer_var_idx > 0)
6398 invoke_defer_funcs(ectx);
6399}
6400
6401/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006402 * Return loopvarinfo in a printable form in allocated memory.
6403 */
6404 static char_u *
6405printable_loopvarinfo(loopvarinfo_T *lvi)
6406{
6407 garray_T ga;
6408 int depth;
6409
6410 ga_init2(&ga, 1, 100);
6411 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6412 {
6413 if (ga_grow(&ga, 50) == FAIL)
6414 break;
6415 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006416 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006417 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006418 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006419 lvi->lvi_loop[depth].var_idx,
6420 lvi->lvi_loop[depth].var_idx
6421 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006422 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006423 }
6424 return ga.ga_data;
6425}
6426
6427/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006428 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6429 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6430 * "pfx" is prefixed to every line.
6431 */
6432 static void
6433list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6434{
6435 int line_idx = 0;
6436 int prev_current = 0;
6437 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006438 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006439
6440 for (current = 0; current < instr_count; ++current)
6441 {
6442 isn_T *iptr = &instr[current];
6443 char *line;
6444
6445 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006446 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006447 while (line_idx < iptr->isn_lnum
6448 && line_idx < ufunc->uf_lines.ga_len)
6449 {
6450 if (current > prev_current)
6451 {
6452 msg_puts("\n\n");
6453 prev_current = current;
6454 }
6455 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6456 if (line != NULL)
6457 msg(line);
6458 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006459 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6460 {
6461 int first_def_arg = ufunc->uf_args.ga_len
6462 - ufunc->uf_def_args.ga_len;
6463
6464 if (def_arg_idx > 0)
6465 msg_puts("\n\n");
6466 msg_start();
6467 msg_puts(" ");
6468 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6469 first_def_arg + def_arg_idx]);
6470 msg_puts(" = ");
6471 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6472 msg_clr_eos();
6473 msg_end();
6474 }
6475 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006476
6477 switch (iptr->isn_type)
6478 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006479 case ISN_CONSTRUCT:
6480 smsg("%s%4d NEW %s size %d", pfx, current,
6481 iptr->isn_arg.construct.construct_class->class_name,
6482 (int)iptr->isn_arg.construct.construct_size);
6483 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006484 case ISN_EXEC:
6485 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6486 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006487 case ISN_EXEC_SPLIT:
6488 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6489 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006490 case ISN_EXECRANGE:
6491 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6492 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006493 case ISN_LEGACY_EVAL:
6494 smsg("%s%4d EVAL legacy %s", pfx, current,
6495 iptr->isn_arg.string);
6496 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006497 case ISN_REDIRSTART:
6498 smsg("%s%4d REDIR", pfx, current);
6499 break;
6500 case ISN_REDIREND:
6501 smsg("%s%4d REDIR END%s", pfx, current,
6502 iptr->isn_arg.number ? " append" : "");
6503 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006504 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006505#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006506 smsg("%s%4d CEXPR pre %s", pfx, current,
6507 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006508#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006509 break;
6510 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006511#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006512 {
6513 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6514
6515 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6516 cexpr_get_auname(cer->cer_cmdidx),
6517 cer->cer_forceit ? "!" : "",
6518 cer->cer_cmdline);
6519 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006520#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006521 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006522 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006523 smsg("%s%4d INSTR", pfx, current);
6524 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6525 msg(" -------------");
6526 break;
6527 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006528 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006529 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6530
6531 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006532 }
6533 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006534 case ISN_SUBSTITUTE:
6535 {
6536 subs_T *subs = &iptr->isn_arg.subs;
6537
6538 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6539 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6540 msg(" -------------");
6541 }
6542 break;
6543 case ISN_EXECCONCAT:
6544 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6545 (varnumber_T)iptr->isn_arg.number);
6546 break;
6547 case ISN_ECHO:
6548 {
6549 echo_T *echo = &iptr->isn_arg.echo;
6550
6551 smsg("%s%4d %s %d", pfx, current,
6552 echo->echo_with_white ? "ECHO" : "ECHON",
6553 echo->echo_count);
6554 }
6555 break;
6556 case ISN_EXECUTE:
6557 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006558 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006559 break;
6560 case ISN_ECHOMSG:
6561 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006562 (varnumber_T)(iptr->isn_arg.number));
6563 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006564 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006565 if (iptr->isn_arg.echowin.ewin_time > 0)
6566 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6567 iptr->isn_arg.echowin.ewin_count,
6568 iptr->isn_arg.echowin.ewin_time);
6569 else
6570 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6571 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006572 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006573 case ISN_ECHOCONSOLE:
6574 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6575 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006576 break;
6577 case ISN_ECHOERR:
6578 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006579 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006580 break;
6581 case ISN_LOAD:
6582 {
6583 if (iptr->isn_arg.number < 0)
6584 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6585 (varnumber_T)(iptr->isn_arg.number
6586 + STACK_FRAME_SIZE));
6587 else
6588 smsg("%s%4d LOAD $%lld", pfx, current,
6589 (varnumber_T)(iptr->isn_arg.number));
6590 }
6591 break;
6592 case ISN_LOADOUTER:
6593 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006594 isn_outer_T *outer = &iptr->isn_arg.outer;
6595
6596 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006597 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006598 outer->outer_depth,
6599 outer->outer_idx + STACK_FRAME_SIZE);
6600 else if (outer->outer_depth < 0)
6601 smsg("%s%4d LOADOUTER $%d in loop level %d",
6602 pfx, current,
6603 outer->outer_idx,
6604 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006605 else
6606 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006607 outer->outer_depth,
6608 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006609 }
6610 break;
6611 case ISN_LOADV:
6612 smsg("%s%4d LOADV v:%s", pfx, current,
6613 get_vim_var_name(iptr->isn_arg.number));
6614 break;
6615 case ISN_LOADSCRIPT:
6616 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006617 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6618 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6619 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006620
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006621 sv = get_script_svar(sref, -1);
6622 if (sv == NULL)
6623 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6624 pfx, current, si->sn_name);
6625 else
6626 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006627 sv->sv_name,
6628 sref->sref_idx,
6629 si->sn_name);
6630 }
6631 break;
6632 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006633 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006634 {
6635 scriptitem_T *si = SCRIPT_ITEM(
6636 iptr->isn_arg.loadstore.ls_sid);
6637
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006638 smsg("%s%4d %s s:%s from %s", pfx, current,
6639 iptr->isn_type == ISN_LOADS ? "LOADS"
6640 : "LOADEXPORT",
6641 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006642 }
6643 break;
6644 case ISN_LOADAUTO:
6645 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6646 break;
6647 case ISN_LOADG:
6648 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6649 break;
6650 case ISN_LOADB:
6651 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6652 break;
6653 case ISN_LOADW:
6654 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6655 break;
6656 case ISN_LOADT:
6657 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6658 break;
6659 case ISN_LOADGDICT:
6660 smsg("%s%4d LOAD g:", pfx, current);
6661 break;
6662 case ISN_LOADBDICT:
6663 smsg("%s%4d LOAD b:", pfx, current);
6664 break;
6665 case ISN_LOADWDICT:
6666 smsg("%s%4d LOAD w:", pfx, current);
6667 break;
6668 case ISN_LOADTDICT:
6669 smsg("%s%4d LOAD t:", pfx, current);
6670 break;
6671 case ISN_LOADOPT:
6672 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6673 break;
6674 case ISN_LOADENV:
6675 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6676 break;
6677 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006678 smsg("%s%4d LOADREG @%c", pfx, current,
6679 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006680 break;
6681
6682 case ISN_STORE:
6683 if (iptr->isn_arg.number < 0)
6684 smsg("%s%4d STORE arg[%lld]", pfx, current,
6685 iptr->isn_arg.number + STACK_FRAME_SIZE);
6686 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006687 smsg("%s%4d STORE $%lld", pfx, current,
6688 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006689 break;
6690 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006691 {
6692 isn_outer_T *outer = &iptr->isn_arg.outer;
6693
6694 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6695 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6696 pfx, current, outer->outer_idx);
6697 else
6698 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6699 outer->outer_depth, outer->outer_idx);
6700 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006701 break;
6702 case ISN_STOREV:
6703 smsg("%s%4d STOREV v:%s", pfx, current,
6704 get_vim_var_name(iptr->isn_arg.number));
6705 break;
6706 case ISN_STOREAUTO:
6707 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6708 break;
6709 case ISN_STOREG:
6710 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6711 break;
6712 case ISN_STOREB:
6713 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6714 break;
6715 case ISN_STOREW:
6716 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6717 break;
6718 case ISN_STORET:
6719 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6720 break;
6721 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006722 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006723 {
6724 scriptitem_T *si = SCRIPT_ITEM(
6725 iptr->isn_arg.loadstore.ls_sid);
6726
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006727 smsg("%s%4d %s %s in %s", pfx, current,
6728 iptr->isn_type == ISN_STORES
6729 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006730 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6731 }
6732 break;
6733 case ISN_STORESCRIPT:
6734 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006735 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6736 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6737 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006738
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006739 sv = get_script_svar(sref, -1);
6740 if (sv == NULL)
6741 smsg("%s%4d STORESCRIPT [deleted] in %s",
6742 pfx, current, si->sn_name);
6743 else
6744 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006745 sv->sv_name,
6746 sref->sref_idx,
6747 si->sn_name);
6748 }
6749 break;
6750 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006751 case ISN_STOREFUNCOPT:
6752 smsg("%s%4d %s &%s", pfx, current,
6753 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006754 iptr->isn_arg.storeopt.so_name);
6755 break;
6756 case ISN_STOREENV:
6757 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6758 break;
6759 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006760 smsg("%s%4d STOREREG @%c", pfx, current,
6761 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006762 break;
6763 case ISN_STORENR:
6764 smsg("%s%4d STORE %lld in $%d", pfx, current,
6765 iptr->isn_arg.storenr.stnr_val,
6766 iptr->isn_arg.storenr.stnr_idx);
6767 break;
6768
6769 case ISN_STOREINDEX:
6770 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006771 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006772 break;
6773
6774 case ISN_STORERANGE:
6775 smsg("%s%4d STORERANGE", pfx, current);
6776 break;
6777
Bram Moolenaard505d172022-12-18 21:42:55 +00006778 case ISN_LOAD_CLASSMEMBER:
6779 case ISN_STORE_CLASSMEMBER:
6780 {
6781 class_T *cl = iptr->isn_arg.classmember.cm_class;
6782 int idx = iptr->isn_arg.classmember.cm_idx;
6783 ocmember_T *ocm = &cl->class_class_members[idx];
6784 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6785 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6786 ? "LOAD" : "STORE",
6787 cl->class_name, ocm->ocm_name);
6788 }
6789 break;
6790
Bram Moolenaar4c137212021-04-19 16:48:48 +02006791 // constants
6792 case ISN_PUSHNR:
6793 smsg("%s%4d PUSHNR %lld", pfx, current,
6794 (varnumber_T)(iptr->isn_arg.number));
6795 break;
6796 case ISN_PUSHBOOL:
6797 case ISN_PUSHSPEC:
6798 smsg("%s%4d PUSH %s", pfx, current,
6799 get_var_special_name(iptr->isn_arg.number));
6800 break;
6801 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006802 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006803 break;
6804 case ISN_PUSHS:
6805 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6806 break;
6807 case ISN_PUSHBLOB:
6808 {
6809 char_u *r;
6810 char_u numbuf[NUMBUFLEN];
6811 char_u *tofree;
6812
6813 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6814 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6815 vim_free(tofree);
6816 }
6817 break;
6818 case ISN_PUSHFUNC:
6819 {
6820 char *name = (char *)iptr->isn_arg.string;
6821
6822 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6823 name == NULL ? "[none]" : name);
6824 }
6825 break;
6826 case ISN_PUSHCHANNEL:
6827#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006828 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006829#endif
6830 break;
6831 case ISN_PUSHJOB:
6832#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006833 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006834#endif
6835 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006836 case ISN_PUSHOBJ:
6837 smsg("%s%4d PUSHOBJ null", pfx, current);
6838 break;
6839 case ISN_PUSHCLASS:
6840 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006841 iptr->isn_arg.classarg == NULL ? "null"
6842 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006843 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006844 case ISN_PUSHEXC:
6845 smsg("%s%4d PUSH v:exception", pfx, current);
6846 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006847 case ISN_AUTOLOAD:
6848 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6849 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006850 case ISN_UNLET:
6851 smsg("%s%4d UNLET%s %s", pfx, current,
6852 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6853 iptr->isn_arg.unlet.ul_name);
6854 break;
6855 case ISN_UNLETENV:
6856 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6857 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6858 iptr->isn_arg.unlet.ul_name);
6859 break;
6860 case ISN_UNLETINDEX:
6861 smsg("%s%4d UNLETINDEX", pfx, current);
6862 break;
6863 case ISN_UNLETRANGE:
6864 smsg("%s%4d UNLETRANGE", pfx, current);
6865 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006866 case ISN_LOCKUNLOCK:
6867 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6868 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006869 case ISN_LOCKCONST:
6870 smsg("%s%4d LOCKCONST", pfx, current);
6871 break;
6872 case ISN_NEWLIST:
6873 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006874 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006875 break;
6876 case ISN_NEWDICT:
6877 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006878 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006879 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006880 case ISN_NEWPARTIAL:
6881 smsg("%s%4d NEWPARTIAL", pfx, current);
6882 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006883
6884 // function call
6885 case ISN_BCALL:
6886 {
6887 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6888
6889 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6890 internal_func_name(cbfunc->cbf_idx),
6891 cbfunc->cbf_argcount);
6892 }
6893 break;
6894 case ISN_DCALL:
6895 {
6896 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6897 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6898 + cdfunc->cdf_idx;
6899
6900 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006901 printable_func_name(df->df_ufunc),
6902 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006903 }
6904 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006905 case ISN_METHODCALL:
6906 {
6907 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6908
6909 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6910 mfunc->cmf_itf->class_name,
6911 mfunc->cmf_itf->class_obj_methods[
6912 mfunc->cmf_idx]->uf_name,
6913 mfunc->cmf_argcount);
6914 }
6915 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006916 case ISN_UCALL:
6917 {
6918 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6919
6920 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6921 cufunc->cuf_name, cufunc->cuf_argcount);
6922 }
6923 break;
6924 case ISN_PCALL:
6925 {
6926 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6927
6928 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6929 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6930 }
6931 break;
6932 case ISN_PCALL_END:
6933 smsg("%s%4d PCALL end", pfx, current);
6934 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006935 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00006936 case ISN_DEFEROBJ:
6937 smsg("%s%4d %s %d args", pfx, current,
6938 iptr->isn_type == ISN_DEFER ? "DEFER" : "DEFEROBJ",
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006939 (int)iptr->isn_arg.defer.defer_argcount);
6940 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006941 case ISN_RETURN:
6942 smsg("%s%4d RETURN", pfx, current);
6943 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006944 case ISN_RETURN_VOID:
6945 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006946 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006947 case ISN_RETURN_OBJECT:
6948 smsg("%s%4d RETURN object", pfx, current);
6949 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006950 case ISN_FUNCREF:
6951 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006952 funcref_T *funcref = &iptr->isn_arg.funcref;
6953 funcref_extra_T *extra = funcref->fr_extra;
6954 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006955
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006956 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006957 {
6958 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6959 + funcref->fr_dfunc_idx;
6960 name = df->df_ufunc->uf_name;
6961 }
6962 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006963 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006964 if (extra != NULL && extra->fre_class != NULL)
6965 {
6966 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6967 extra->fre_class->class_name, name);
6968 }
6969 else if (extra == NULL
6970 || extra->fre_loopvar_info.lvi_depth == 0)
6971 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006972 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006973 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006974 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006975 {
6976 char_u *info = printable_loopvarinfo(
6977 &extra->fre_loopvar_info);
6978
6979 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6980 name, info);
6981 vim_free(info);
6982 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006983 }
6984 break;
6985
6986 case ISN_NEWFUNC:
6987 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006988 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006989
Bram Moolenaarcc341812022-09-19 15:54:34 +01006990 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006991 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6992 arg->nfa_lambda, arg->nfa_global);
6993 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006994 {
6995 char_u *info = printable_loopvarinfo(
6996 &arg->nfa_loopvar_info);
6997
6998 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6999 arg->nfa_lambda, arg->nfa_global, info);
7000 vim_free(info);
7001 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007002 }
7003 break;
7004
7005 case ISN_DEF:
7006 {
7007 char_u *name = iptr->isn_arg.string;
7008
7009 smsg("%s%4d DEF %s", pfx, current,
7010 name == NULL ? (char_u *)"" : name);
7011 }
7012 break;
7013
7014 case ISN_JUMP:
7015 {
7016 char *when = "?";
7017
7018 switch (iptr->isn_arg.jump.jump_when)
7019 {
7020 case JUMP_ALWAYS:
7021 when = "JUMP";
7022 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007023 case JUMP_NEVER:
7024 iemsg("JUMP_NEVER should not be used");
7025 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007026 case JUMP_AND_KEEP_IF_TRUE:
7027 when = "JUMP_AND_KEEP_IF_TRUE";
7028 break;
7029 case JUMP_IF_FALSE:
7030 when = "JUMP_IF_FALSE";
7031 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007032 case JUMP_WHILE_FALSE:
7033 when = "JUMP_WHILE_FALSE"; // unused
7034 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007035 case JUMP_IF_COND_FALSE:
7036 when = "JUMP_IF_COND_FALSE";
7037 break;
7038 case JUMP_IF_COND_TRUE:
7039 when = "JUMP_IF_COND_TRUE";
7040 break;
7041 }
7042 smsg("%s%4d %s -> %d", pfx, current, when,
7043 iptr->isn_arg.jump.jump_where);
7044 }
7045 break;
7046
7047 case ISN_JUMP_IF_ARG_SET:
7048 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7049 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7050 iptr->isn_arg.jump.jump_where);
7051 break;
7052
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007053 case ISN_JUMP_IF_ARG_NOT_SET:
7054 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7055 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7056 iptr->isn_arg.jump.jump_where);
7057 break;
7058
Bram Moolenaar4c137212021-04-19 16:48:48 +02007059 case ISN_FOR:
7060 {
7061 forloop_T *forloop = &iptr->isn_arg.forloop;
7062
7063 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007064 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007065 }
7066 break;
7067
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007068 case ISN_ENDLOOP:
7069 {
7070 endloop_T *endloop = &iptr->isn_arg.endloop;
7071
Bram Moolenaarcc341812022-09-19 15:54:34 +01007072 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7073 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007074 endloop->end_funcref_idx,
7075 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007076 endloop->end_var_idx + endloop->end_var_count - 1,
7077 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007078 }
7079 break;
7080
7081 case ISN_WHILE:
7082 {
7083 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7084
7085 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7086 whileloop->while_funcref_idx,
7087 whileloop->while_end);
7088 }
7089 break;
7090
Bram Moolenaar4c137212021-04-19 16:48:48 +02007091 case ISN_TRY:
7092 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007093 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007094
7095 if (try->try_ref->try_finally == 0)
7096 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7097 pfx, current,
7098 try->try_ref->try_catch,
7099 try->try_ref->try_endtry);
7100 else
7101 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7102 pfx, current,
7103 try->try_ref->try_catch,
7104 try->try_ref->try_finally,
7105 try->try_ref->try_endtry);
7106 }
7107 break;
7108 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007109 smsg("%s%4d CATCH", pfx, current);
7110 break;
7111 case ISN_TRYCONT:
7112 {
7113 trycont_T *trycont = &iptr->isn_arg.trycont;
7114
7115 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7116 trycont->tct_levels,
7117 trycont->tct_levels == 1 ? "" : "s",
7118 trycont->tct_where);
7119 }
7120 break;
7121 case ISN_FINALLY:
7122 smsg("%s%4d FINALLY", pfx, current);
7123 break;
7124 case ISN_ENDTRY:
7125 smsg("%s%4d ENDTRY", pfx, current);
7126 break;
7127 case ISN_THROW:
7128 smsg("%s%4d THROW", pfx, current);
7129 break;
7130
7131 // expression operations on number
7132 case ISN_OPNR:
7133 case ISN_OPFLOAT:
7134 case ISN_OPANY:
7135 {
7136 char *what;
7137 char *ins;
7138
7139 switch (iptr->isn_arg.op.op_type)
7140 {
7141 case EXPR_MULT: what = "*"; break;
7142 case EXPR_DIV: what = "/"; break;
7143 case EXPR_REM: what = "%"; break;
7144 case EXPR_SUB: what = "-"; break;
7145 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007146 case EXPR_LSHIFT: what = "<<"; break;
7147 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007148 default: what = "???"; break;
7149 }
7150 switch (iptr->isn_type)
7151 {
7152 case ISN_OPNR: ins = "OPNR"; break;
7153 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7154 case ISN_OPANY: ins = "OPANY"; break;
7155 default: ins = "???"; break;
7156 }
7157 smsg("%s%4d %s %s", pfx, current, ins, what);
7158 }
7159 break;
7160
7161 case ISN_COMPAREBOOL:
7162 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007163 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007164 case ISN_COMPARENR:
7165 case ISN_COMPAREFLOAT:
7166 case ISN_COMPARESTRING:
7167 case ISN_COMPAREBLOB:
7168 case ISN_COMPARELIST:
7169 case ISN_COMPAREDICT:
7170 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007171 case ISN_COMPARECLASS:
7172 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007173 case ISN_COMPAREANY:
7174 {
7175 char *p;
7176 char buf[10];
7177 char *type;
7178
7179 switch (iptr->isn_arg.op.op_type)
7180 {
7181 case EXPR_EQUAL: p = "=="; break;
7182 case EXPR_NEQUAL: p = "!="; break;
7183 case EXPR_GREATER: p = ">"; break;
7184 case EXPR_GEQUAL: p = ">="; break;
7185 case EXPR_SMALLER: p = "<"; break;
7186 case EXPR_SEQUAL: p = "<="; break;
7187 case EXPR_MATCH: p = "=~"; break;
7188 case EXPR_IS: p = "is"; break;
7189 case EXPR_ISNOT: p = "isnot"; break;
7190 case EXPR_NOMATCH: p = "!~"; break;
7191 default: p = "???"; break;
7192 }
7193 STRCPY(buf, p);
7194 if (iptr->isn_arg.op.op_ic == TRUE)
7195 strcat(buf, "?");
7196 switch(iptr->isn_type)
7197 {
7198 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7199 case ISN_COMPARESPECIAL:
7200 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007201 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007202 case ISN_COMPARENR: type = "COMPARENR"; break;
7203 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7204 case ISN_COMPARESTRING:
7205 type = "COMPARESTRING"; break;
7206 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7207 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7208 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7209 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007210 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7211 case ISN_COMPAREOBJECT:
7212 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007213 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7214 default: type = "???"; break;
7215 }
7216
7217 smsg("%s%4d %s %s", pfx, current, type, buf);
7218 }
7219 break;
7220
7221 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7222 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7223
7224 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007225 case ISN_CONCAT:
7226 smsg("%s%4d CONCAT size %lld", pfx, current,
7227 (varnumber_T)(iptr->isn_arg.number));
7228 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007229 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7230 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7231 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7232 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7233 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7234 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7235 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7236 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7237 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7238 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7239 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007240 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007241 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7242 iptr->isn_arg.getitem.gi_index,
7243 iptr->isn_arg.getitem.gi_with_op ?
7244 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007245 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7246 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7247 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007248 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7249 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007250 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007251 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007252 pfx, current,
7253 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007254 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007255 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007256 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007257 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007258 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7259 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7260
Bram Moolenaar4c137212021-04-19 16:48:48 +02007261 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7262
Bram Moolenaar4c137212021-04-19 16:48:48 +02007263 case ISN_CHECKTYPE:
7264 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007265 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007266 char *tofree = NULL;
7267 char *typename;
7268
7269 if (ct->ct_type->tt_type == VAR_FLOAT
7270 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7271 typename = "float|number";
7272 else
7273 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007274
7275 if (ct->ct_arg_idx == 0)
7276 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007277 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007278 (int)ct->ct_off);
7279 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007280 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007281 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007282 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007283 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007284 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007285 (int)ct->ct_arg_idx);
7286 vim_free(tofree);
7287 break;
7288 }
7289 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7290 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7291 iptr->isn_arg.checklen.cl_min_len);
7292 break;
7293 case ISN_SETTYPE:
7294 {
7295 char *tofree;
7296
7297 smsg("%s%4d SETTYPE %s", pfx, current,
7298 type_name(iptr->isn_arg.type.ct_type, &tofree));
7299 vim_free(tofree);
7300 break;
7301 }
7302 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007303 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7304 smsg("%s%4d INVERT %d (!val)", pfx, current,
7305 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007306 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007307 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7308 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007309 break;
7310 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007311 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007312 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007313 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7314 pfx, current,
7315 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007316 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007317 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7318 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007319 break;
7320 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007321 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007322 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007323 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007324 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7325 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007326 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007327 else
7328 smsg("%s%4d PUT %c %ld", pfx, current,
7329 iptr->isn_arg.put.put_regname,
7330 (long)iptr->isn_arg.put.put_lnum);
7331 break;
7332
Bram Moolenaar4c137212021-04-19 16:48:48 +02007333 case ISN_CMDMOD:
7334 {
7335 char_u *buf;
7336 size_t len = produce_cmdmods(
7337 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7338
7339 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007340 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007341 {
7342 (void)produce_cmdmods(
7343 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7344 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7345 vim_free(buf);
7346 }
7347 break;
7348 }
7349 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7350
7351 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007352 smsg("%s%4d PROFILE START line %d", pfx, current,
7353 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007354 break;
7355
7356 case ISN_PROF_END:
7357 smsg("%s%4d PROFILE END", pfx, current);
7358 break;
7359
Bram Moolenaare99d4222021-06-13 14:01:26 +02007360 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007361 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7362 iptr->isn_arg.debug.dbg_break_lnum + 1,
7363 iptr->isn_lnum,
7364 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007365 break;
7366
Bram Moolenaar4c137212021-04-19 16:48:48 +02007367 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7368 iptr->isn_arg.unpack.unp_count,
7369 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7370 break;
7371 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7372 iptr->isn_arg.shuffle.shfl_item,
7373 iptr->isn_arg.shuffle.shfl_up);
7374 break;
7375 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7376
7377 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7378 return;
7379 }
7380
7381 out_flush(); // output one line at a time
7382 ui_breakcheck();
7383 if (got_int)
7384 break;
7385 }
7386}
7387
7388/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007389 * Handle command line completion for the :disassemble command.
7390 */
7391 void
7392set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7393{
7394 char_u *p;
7395
7396 // Default: expand user functions, "debug" and "profile"
7397 xp->xp_context = EXPAND_DISASSEMBLE;
7398 xp->xp_pattern = arg;
7399
7400 // first argument already typed: only user function names
7401 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7402 {
7403 xp->xp_context = EXPAND_USER_FUNC;
7404 xp->xp_pattern = skipwhite(p);
7405 }
7406}
7407
7408/*
7409 * Function given to ExpandGeneric() to obtain the list of :disassemble
7410 * arguments.
7411 */
7412 char_u *
7413get_disassemble_argument(expand_T *xp, int idx)
7414{
7415 if (idx == 0)
7416 return (char_u *)"debug";
7417 if (idx == 1)
7418 return (char_u *)"profile";
7419 return get_user_func_name(xp, idx - 2);
7420}
7421
7422/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007423 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007424 * We don't really need this at runtime, but we do have tests that require it,
7425 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 */
7427 void
7428ex_disassemble(exarg_T *eap)
7429{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007430 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007431 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007433 isn_T *instr = NULL; // init to shut up gcc warning
7434 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007435 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007436
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007437 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007438 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007440 if (func_needs_compiling(ufunc, compile_type)
7441 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007442 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007443 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007445 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446 return;
7447 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007448 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449
7450 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007451 switch (compile_type)
7452 {
7453 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007454#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007455 instr = dfunc->df_instr_prof;
7456 instr_count = dfunc->df_instr_prof_count;
7457 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007458#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007459 // FALLTHROUGH
7460 case CT_NONE:
7461 instr = dfunc->df_instr;
7462 instr_count = dfunc->df_instr_count;
7463 break;
7464 case CT_DEBUG:
7465 instr = dfunc->df_instr_debug;
7466 instr_count = dfunc->df_instr_debug_count;
7467 break;
7468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469
Bram Moolenaar4c137212021-04-19 16:48:48 +02007470 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471}
7472
7473/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007474 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475 * list, etc. Mostly like what JavaScript does, except that empty list and
7476 * empty dictionary are FALSE.
7477 */
7478 int
7479tv2bool(typval_T *tv)
7480{
7481 switch (tv->v_type)
7482 {
7483 case VAR_NUMBER:
7484 return tv->vval.v_number != 0;
7485 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 case VAR_PARTIAL:
7488 return tv->vval.v_partial != NULL;
7489 case VAR_FUNC:
7490 case VAR_STRING:
7491 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7492 case VAR_LIST:
7493 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7494 case VAR_DICT:
7495 return tv->vval.v_dict != NULL
7496 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7497 case VAR_BOOL:
7498 case VAR_SPECIAL:
7499 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7500 case VAR_JOB:
7501#ifdef FEAT_JOB_CHANNEL
7502 return tv->vval.v_job != NULL;
7503#else
7504 break;
7505#endif
7506 case VAR_CHANNEL:
7507#ifdef FEAT_JOB_CHANNEL
7508 return tv->vval.v_channel != NULL;
7509#else
7510 break;
7511#endif
7512 case VAR_BLOB:
7513 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7514 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007515 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007516 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007517 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007518 case VAR_CLASS:
7519 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007520 break;
7521 }
7522 return FALSE;
7523}
7524
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007525 void
7526emsg_using_string_as(typval_T *tv, int as_number)
7527{
7528 semsg(_(as_number ? e_using_string_as_number_str
7529 : e_using_string_as_bool_str),
7530 tv->vval.v_string == NULL
7531 ? (char_u *)"" : tv->vval.v_string);
7532}
7533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007534/*
7535 * If "tv" is a string give an error and return FAIL.
7536 */
7537 int
7538check_not_string(typval_T *tv)
7539{
7540 if (tv->v_type == VAR_STRING)
7541 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007542 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007543 clear_tv(tv);
7544 return FAIL;
7545 }
7546 return OK;
7547}
7548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549
7550#endif // FEAT_EVAL