blob: 44cdb09e30f55a732aa5ae8d189984d5cf062f97 [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
mityua5550692023-11-25 15:41:20 +0100551 // While check_ufunc_arg_types call, def function compilation process may
552 // run. If so many def functions are compiled, def_functions array may be
553 // reallocated and dfunc may no longer have valid pointer. Get the object
554 // pointer from def_functions again here.
555 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
556
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200557 // Reserve space for:
558 // - missing arguments
559 // - stack frame
560 // - local variables
561 // - if needed: a counter for number of closures created in
562 // ectx->ec_funcrefs.
563 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100564 if (GA_GROW_FAILS(&ectx->ec_stack,
565 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 return FAIL;
567
Yegappan Lakshmanan1087b8c2023-10-07 22:03:18 +0200568 // The object pointer is in the execution typval stack. The GA_GROW call
569 // above may have reallocated the execution typval stack. So the object
570 // pointer may not be valid anymore. Get the object pointer again from the
571 // execution stack.
572 obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
573
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100574 // If depth of calling is getting too high, don't execute the function.
575 if (funcdepth_increment() == FAIL)
576 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200577 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100578
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100579 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200580 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100581 {
582 floc = ALLOC_ONE(funclocal_T);
583 if (floc == NULL)
584 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200585 *floc = ectx->ec_funclocal;
586 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100587 }
588
Bram Moolenaarfe270812020-04-11 22:31:27 +0200589 // Move the vararg-list to below the missing optional arguments.
590 if (vararg_count > 0 && arg_to_add > 0)
591 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100592
593 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200594 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200595 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200596 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597
598 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100599 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
600 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200601 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200602 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
603 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100604 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100605 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200606 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607
Bram Moolenaar5800c792022-09-22 17:34:01 +0100608 // Initialize all local variables to number zero. Also initialize the
609 // variable that counts how many closures were created. This is used in
610 // handle_closure_in_use().
611 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
612 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000613 {
614 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
615
616 tv->v_type = VAR_NUMBER;
617 tv->vval.v_number = 0;
618 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200619 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620
Bram Moolenaar574950d2023-01-03 19:08:50 +0000621 // For an object method move the object from just before the arguments to
622 // the first local variable.
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +0200623 if (IS_OBJECT_METHOD(ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +0000624 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200625 if (obj->v_type != VAR_OBJECT)
626 {
627 semsg(_(e_internal_error_str), "type in stack is not an object");
628 return FAIL;
629 }
630
Bram Moolenaar574950d2023-01-03 19:08:50 +0000631 *STACK_TV_VAR(0) = *obj;
632 obj->v_type = VAR_UNKNOWN;
633 }
634
Bram Moolenaar5800c792022-09-22 17:34:01 +0100635 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
636 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100637 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200638 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100639
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200640 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100641 return FAIL;
642 if (pt != NULL)
643 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000644 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200645 ++pt->pt_refcount;
646 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100647 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100648 else
649 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200650 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200651 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200652 {
653 vim_free(ref);
654 return FAIL;
655 }
656 ref->or_outer_allocated = TRUE;
657 ref->or_outer->out_stack = &ectx->ec_stack;
658 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
659 if (ectx->ec_outer_ref != NULL)
660 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100661 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200662 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100663 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100664 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200665 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100666
Bram Moolenaarc970e422021-03-17 15:03:04 +0100667 ++ufunc->uf_calls;
668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 // Set execution state to the start of the called function.
670 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100671 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100672 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200673 if (entry != NULL)
674 {
675 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200676 // Save the current context so it can be restored on return.
677 entry->es_save_sctx = current_sctx;
678 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200679 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100680
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200681 // Start execution at the first instruction.
682 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683
684 return OK;
685}
686
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000687// Double linked list of funcstack_T in use.
688static funcstack_T *first_funcstack = NULL;
689
690 static void
691add_funcstack_to_list(funcstack_T *funcstack)
692{
693 // Link in list of funcstacks.
694 if (first_funcstack != NULL)
695 first_funcstack->fs_prev = funcstack;
696 funcstack->fs_next = first_funcstack;
697 funcstack->fs_prev = NULL;
698 first_funcstack = funcstack;
699}
700
701 static void
702remove_funcstack_from_list(funcstack_T *funcstack)
703{
704 if (funcstack->fs_prev == NULL)
705 first_funcstack = funcstack->fs_next;
706 else
707 funcstack->fs_prev->fs_next = funcstack->fs_next;
708 if (funcstack->fs_next != NULL)
709 funcstack->fs_next->fs_prev = funcstack->fs_prev;
710}
711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100712/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200713 * Used when returning from a function: Check if any closure is still
714 * referenced. If so then move the arguments and variables to a separate piece
715 * of stack to be used when the closure is called.
716 * When "free_arguments" is TRUE the arguments are to be freed.
717 * Returns FAIL when out of memory.
718 */
719 static int
720handle_closure_in_use(ectx_T *ectx, int free_arguments)
721{
722 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
723 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200724 int argcount;
725 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200726 int idx;
727 typval_T *tv;
728 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200729 garray_T *gap = &ectx->ec_funcrefs;
730 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200731
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200732 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200733 return OK; // function was freed
734 if (dfunc->df_has_closure == 0)
735 return OK; // no closures
736 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
737 closure_count = tv->vval.v_number;
738 if (closure_count == 0)
739 return OK; // no funcrefs created
740
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100741 // Compute "top": the first entry in the stack used by the function.
742 // This is the first argument (after that comes the stack frame and then
743 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200744 argcount = ufunc_argcount(dfunc->df_ufunc);
745 top = ectx->ec_frame_idx - argcount;
746
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200747 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200748 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200749 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200750 partial_T *pt;
751 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200752
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200753 if (off < 0)
754 continue; // count is off or already done
755 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200756 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200757 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200758 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200759 int i;
760
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000761 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200762 // unreferenced on return.
763 for (i = 0; i < dfunc->df_varcount; ++i)
764 {
765 typval_T *stv = STACK_TV(ectx->ec_frame_idx
766 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200767 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200768 --refcount;
769 }
770 if (refcount > 1)
771 {
772 closure_in_use = TRUE;
773 break;
774 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200775 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200776 }
777
778 if (closure_in_use)
779 {
780 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
781 typval_T *stack;
782
783 // A closure is using the arguments and/or local variables.
784 // Move them to the called function.
785 if (funcstack == NULL)
786 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000787
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200788 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100789 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
790 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200791 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
792 funcstack->fs_ga.ga_data = stack;
793 if (stack == NULL)
794 {
795 vim_free(funcstack);
796 return FAIL;
797 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000798 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200799
800 // Move or copy the arguments.
801 for (idx = 0; idx < argcount; ++idx)
802 {
803 tv = STACK_TV(top + idx);
804 if (free_arguments)
805 {
806 *(stack + idx) = *tv;
807 tv->v_type = VAR_UNKNOWN;
808 }
809 else
810 copy_tv(tv, stack + idx);
811 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100812 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200813 // Move the local variables.
814 for (idx = 0; idx < dfunc->df_varcount; ++idx)
815 {
816 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200817
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200818 // A partial created for a local function, that is also used as a
819 // local variable, has a reference count for the variable, thus
820 // will never go down to zero. When all these refcounts are one
821 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000822 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200823 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
824 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200825 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200826
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200827 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200828 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
829 gap->ga_len - closure_count + i])
830 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200831 }
832
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200833 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200834 tv->v_type = VAR_UNKNOWN;
835 }
836
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200837 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200838 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200839 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
840 - closure_count + idx];
841 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200842 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200843 ++funcstack->fs_refcount;
844 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100845 pt->pt_outer.out_stack = &funcstack->fs_ga;
846 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200847 }
848 }
849 }
850
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200851 for (idx = 0; idx < closure_count; ++idx)
852 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
853 - closure_count + idx]);
854 gap->ga_len -= closure_count;
855 if (gap->ga_len == 0)
856 ga_clear(gap);
857
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200858 return OK;
859}
860
861/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200862 * Called when a partial is freed or its reference count goes down to one. The
863 * funcstack may be the only reference to the partials in the local variables.
864 * Go over all of them, the funcref and can be freed if all partials
865 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100866 * Returns TRUE if the funcstack is freed, the partial referencing it will then
867 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200868 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100869 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200870funcstack_check_refcount(funcstack_T *funcstack)
871{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100872 int i;
873 garray_T *gap = &funcstack->fs_ga;
874 int done = 0;
875 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200876
877 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100878 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200879 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
880 {
881 typval_T *tv = ((typval_T *)gap->ga_data) + i;
882
883 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
884 && tv->vval.v_partial->pt_funcstack == funcstack
885 && tv->vval.v_partial->pt_refcount == 1)
886 ++done;
887 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100888 if (done != funcstack->fs_min_refcount)
889 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200890
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100891 stack = gap->ga_data;
892
893 // All partials referencing the funcstack have a reference count of
894 // one, thus the funcstack is no longer of use.
895 for (i = 0; i < gap->ga_len; ++i)
896 clear_tv(stack + i);
897 vim_free(stack);
898 remove_funcstack_from_list(funcstack);
899 vim_free(funcstack);
900
901 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200902}
903
904/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000905 * For garbage collecting: set references in all variables referenced by
906 * all funcstacks.
907 */
908 int
909set_ref_in_funcstacks(int copyID)
910{
911 funcstack_T *funcstack;
912
913 for (funcstack = first_funcstack; funcstack != NULL;
914 funcstack = funcstack->fs_next)
915 {
916 typval_T *stack = funcstack->fs_ga.ga_data;
917 int i;
918
919 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
920 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
921 return TRUE; // abort
922 }
923 return FALSE;
924}
925
Bram Moolenaar806a2732022-09-04 15:40:36 +0100926// Ugly static to avoid passing the execution context around through many
927// layers.
928static ectx_T *current_ectx = NULL;
929
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000930/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100931 * Return TRUE if currently executing a :def function.
932 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100933 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100934 int
935in_def_function(void)
936{
937 return current_ectx != NULL;
938}
939
940/*
Ernie Rael4c8da022023-10-11 21:35:11 +0200941 * If executing a class/object method, then fill in the lval_T.
942 * Set lr_tv to the executing item, and lr_exec_class to the executing class;
943 * use free_tv and class_unref when finished with the lval_root.
944 * For use by builtin functions.
945 *
946 * Return FAIL and do nothing if not executing in a class; otherwise OK.
947 */
948 int
949fill_exec_lval_root(lval_root_T *root)
950{
951 ectx_T *ectx = current_ectx;
952 if (ectx != NULL)
953 {
954 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
955 + current_ectx->ec_dfunc_idx;
956 ufunc_T *ufunc = dfunc->df_ufunc;
957 if (ufunc->uf_class != NULL) // executing a method?
958 {
959 typval_T *tv = alloc_tv();
960 if (tv != NULL)
961 {
962 CLEAR_POINTER(root);
963 root->lr_tv = tv;
964 copy_tv(STACK_TV_VAR(0), root->lr_tv);
965 root->lr_cl_exec = ufunc->uf_class;
966 ++root->lr_cl_exec->class_refcount;
967 return OK;
968 }
969 }
970 }
971
972 return FAIL;
973}
974
975/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100976 * Clear "current_ectx" and return the previous value. To be used when calling
977 * a user function.
978 */
979 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000980clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100981{
982 ectx_T *r = current_ectx;
983
984 current_ectx = NULL;
985 return r;
986}
987
988 void
989restore_current_ectx(ectx_T *ectx)
990{
991 if (current_ectx != NULL)
992 iemsg("Restoring current_ectx while it is not NULL");
993 current_ectx = ectx;
994}
995
996/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100997 * Add an entry for a deferred function call to the currently executing
998 * function.
999 * Return the list or NULL when failed.
1000 */
1001 static list_T *
1002add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001003{
1004 typval_T *defer_tv = STACK_TV_VAR(var_idx);
1005 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001006 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001007 typval_T listval;
1008
1009 if (defer_tv->v_type != VAR_LIST)
1010 {
Bram Moolenaara5348f22022-09-04 11:42:22 +01001011 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001012 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001013 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001014 }
1015 defer_l = defer_tv->vval.v_list;
1016
1017 l = list_alloc_with_items(argcount + 1);
1018 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001019 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001020 listval.v_type = VAR_LIST;
1021 listval.vval.v_list = l;
1022 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +01001023 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001024 {
1025 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001026 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001027 }
1028
Bram Moolenaar806a2732022-09-04 15:40:36 +01001029 return l;
1030}
1031
1032/*
1033 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
1034 * The local variable that lists deferred functions is "var_idx".
1035 * Returns OK or FAIL.
1036 */
1037 static int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001038defer_command(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001039{
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001040 list_T *l = add_defer_item(var_idx, argcount, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001041 int i;
1042 typval_T *func_tv;
1043
1044 if (l == NULL)
1045 return FAIL;
1046
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001047 func_tv = STACK_TV_BOT(-argcount - 1);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001048 if (func_tv->v_type != VAR_PARTIAL && func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001049 {
1050 semsg(_(e_expected_str_but_got_str),
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001051 "function or partial",
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001052 vartype_name(func_tv->v_type));
1053 return FAIL;
1054 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001055 list_set_item(l, 0, func_tv);
1056
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001057 for (i = 0; i < argcount; ++i)
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001058 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
1059 ectx->ec_stack.ga_len -= argcount + 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001060 return OK;
1061}
1062
1063/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001064 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001065 * Consumes "name", also on failure.
1066 * Only to be called when in_def_function() returns TRUE.
1067 */
1068 int
1069add_defer_function(char_u *name, int argcount, typval_T *argvars)
1070{
1071 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1072 + current_ectx->ec_dfunc_idx;
1073 list_T *l;
1074 typval_T func_tv;
1075 int i;
1076
1077 if (dfunc->df_defer_var_idx == 0)
1078 {
1079 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001080 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001081 return FAIL;
1082 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001083
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001084 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001085 if (l == NULL)
1086 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001087 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001088 return FAIL;
1089 }
1090
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001091 func_tv.v_type = VAR_FUNC;
1092 func_tv.v_lock = 0;
1093 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001094 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001095
Bram Moolenaar806a2732022-09-04 15:40:36 +01001096 for (i = 0; i < argcount; ++i)
1097 list_set_item(l, i + 1, argvars + i);
1098 return OK;
1099}
1100
1101/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001102 * Invoked when returning from a function: Invoke any deferred calls.
1103 */
1104 static void
1105invoke_defer_funcs(ectx_T *ectx)
1106{
1107 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1108 + ectx->ec_dfunc_idx;
1109 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1110 listitem_T *li;
1111
1112 if (defer_tv->v_type != VAR_LIST)
1113 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001114 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001115 {
1116 list_T *l = li->li_tv.vval.v_list;
1117 typval_T rettv;
1118 typval_T argvars[MAX_FUNC_ARGS];
1119 int i;
1120 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001121 typval_T *functv = &l->lv_first->li_tv;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001122 int argcount = l->lv_len - 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001123
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001124 if (functv->vval.v_string == NULL)
1125 // already being called, can happen if function does ":qa"
1126 continue;
1127
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001128 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;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001141 funcexe.fe_object = functv->vval.v_partial->pt_obj;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001142 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
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001149 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001150 // first statement in the function will be executed (because of the
1151 // exception). So save and restore the try/catch/throw exception
1152 // state.
1153 exception_state_T estate;
1154 exception_state_save(&estate);
1155 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001156
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001157 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1158
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001159 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001160
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001161 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001162 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001163 }
1164}
1165
1166/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 * Return from the current function.
1168 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001169 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001170func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001173 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001174 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1175 + ectx->ec_dfunc_idx;
1176 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001177 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001178 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1179 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001180 funclocal_T *floc;
1181#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001182 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1183 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184
Bram Moolenaar12d26532021-02-19 19:13:21 +01001185 if (do_profiling == PROF_YES)
1186 {
1187 ufunc_T *caller = prev_dfunc->df_ufunc;
1188
1189 if (dfunc->df_ufunc->uf_profiling
1190 || (caller != NULL && caller->uf_profiling))
1191 {
1192 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1193 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1194 --profile_info_ga.ga_len;
1195 }
1196 }
1197#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001198
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001199 if (dfunc->df_defer_var_idx > 0)
1200 invoke_defer_funcs(ectx);
1201
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001202 // No check for uf_refcount being zero, cannot think of a way that would
1203 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001204 --dfunc->df_ufunc->uf_calls;
1205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001207 entry = estack_pop();
1208 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001209 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001211 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1212 return FAIL;
1213
Bram Moolenaar574950d2023-01-03 19:08:50 +00001214 // Clear the arguments. If this was an object method also clear the
1215 // object, it is just before the arguments.
1216 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001217 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001218 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001219 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1220 clear_tv(STACK_TV(idx));
1221
1222 // Clear local variables and temp values, but not the return value.
1223 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001224 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001226
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001227 // The return value should be on top of the stack. However, when aborting
1228 // it may not be there and ec_frame_idx is the top of the stack.
1229 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001230 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001231 ret_idx = 0;
1232
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001233 if (ectx->ec_outer_ref != NULL)
1234 {
1235 if (ectx->ec_outer_ref->or_outer_allocated)
1236 vim_free(ectx->ec_outer_ref->or_outer);
1237 partial_unref(ectx->ec_outer_ref->or_partial);
1238 vim_free(ectx->ec_outer_ref);
1239 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001240
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001241 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001242 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001243 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1244 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001245 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1246 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001247 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001248 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001249 floc = (void *)STACK_TV(ectx->ec_frame_idx
1250 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001251 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001252 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1253 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001254
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001255 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001256 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001257 else
1258 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001259 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001260 vim_free(floc);
1261 }
1262
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001263 if (ret_idx > 0)
1264 {
1265 // Reset the stack to the position before the call, with a spot for the
1266 // return value, moved there from above the frame.
1267 ectx->ec_stack.ga_len = top + 1;
1268 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1269 }
1270 else
1271 // Reset the stack to the position before the call.
1272 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001273
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001274 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001275 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001276 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277}
1278
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279/*
1280 * Prepare arguments and rettv for calling a builtin or user function.
1281 */
1282 static int
1283call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1284{
1285 int idx;
1286 typval_T *tv;
1287
1288 // Move arguments from bottom of the stack to argvars[] and add terminator.
1289 for (idx = 0; idx < argcount; ++idx)
1290 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1291 argvars[argcount].v_type = VAR_UNKNOWN;
1292
1293 // Result replaces the arguments on the stack.
1294 if (argcount > 0)
1295 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001296 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 return FAIL;
1298 else
1299 ++ectx->ec_stack.ga_len;
1300
1301 // Default return value is zero.
1302 tv = STACK_TV_BOT(-1);
1303 tv->v_type = VAR_NUMBER;
1304 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001305 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306
1307 return OK;
1308}
1309
1310/*
1311 * Call a builtin function by index.
1312 */
1313 static int
1314call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1315{
1316 typval_T argvars[MAX_FUNC_ARGS];
1317 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001318 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001319 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001320 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321
1322 if (call_prepare(argcount, argvars, ectx) == FAIL)
1323 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001324 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001326 // Call the builtin function. Set "current_ectx" so that when it
1327 // recursively invokes call_def_function() a closure context can be set.
1328 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001330 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001331 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332
1333 // Clear the arguments.
1334 for (idx = 0; idx < argcount; ++idx)
1335 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001336
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001337 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001338 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 return OK;
1340}
1341
1342/*
1343 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001344 * If the function is compiled this will add a stack frame and set the
1345 * instruction pointer at the start of the function.
1346 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001347 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001348 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 */
1350 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001351call_ufunc(
1352 ufunc_T *ufunc,
1353 partial_T *pt,
1354 int argcount,
1355 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001356 isn_T *iptr,
1357 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358{
1359 typval_T argvars[MAX_FUNC_ARGS];
1360 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001361 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001363 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001364 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365
Bram Moolenaare99d4222021-06-13 14:01:26 +02001366 if (func_needs_compiling(ufunc, compile_type)
1367 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1368 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001369 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001370 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001371 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001372 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001373 if (error != FCERR_UNKNOWN)
1374 {
1375 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001376 semsg(_(e_too_many_arguments_for_function_str),
1377 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001378 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001379 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001380 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001381 return FAIL;
1382 }
1383
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001384 // The function has been compiled, can call it quickly. For a function
1385 // that was defined later: we can call it directly next time.
1386 if (iptr != NULL)
1387 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001388 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001389 iptr->isn_type = ISN_DCALL;
1390 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1391 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1392 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001393 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395
1396 if (call_prepare(argcount, argvars, ectx) == FAIL)
1397 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001398 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001399 funcexe.fe_evaluate = TRUE;
1400 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401
1402 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001404 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405
1406 // Clear the arguments.
1407 for (idx = 0; idx < argcount; ++idx)
1408 clear_tv(&argvars[idx]);
1409
1410 if (error != FCERR_NONE)
1411 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001412 user_func_error(error, printable_func_name(ufunc),
1413 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 return FAIL;
1415 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001416 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001417 // Error other than from calling the function itself.
1418 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 return OK;
1420}
1421
1422/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001423 * If command modifiers were applied restore them.
1424 */
1425 static void
1426may_restore_cmdmod(funclocal_T *funclocal)
1427{
1428 if (funclocal->floc_restore_cmdmod)
1429 {
1430 cmdmod.cmod_filter_regmatch.regprog = NULL;
1431 undo_cmdmod(&cmdmod);
1432 cmdmod = funclocal->floc_save_cmdmod;
1433 funclocal->floc_restore_cmdmod = FALSE;
1434 }
1435}
1436
1437/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001438 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1439 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001440 */
1441 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001442vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001443{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001444 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001445}
1446
1447/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 * Execute a function by "name".
1449 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001450 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 * Returns FAIL if not found without an error message.
1452 */
1453 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001454call_by_name(
1455 char_u *name,
1456 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001457 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001458 isn_T *iptr,
1459 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460{
1461 ufunc_T *ufunc;
1462
1463 if (builtin_function(name, -1))
1464 {
1465 int func_idx = find_internal_func(name);
1466
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001467 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001469 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 return FAIL;
1471 return call_bfunc(func_idx, argcount, ectx);
1472 }
1473
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001474 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001475
1476 if (ufunc == NULL)
1477 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001478 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001479
1480 if (script_autoload(name, TRUE))
1481 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001482 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001483
1484 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001485 return FAIL; // bail out if loading the script caused an error
1486 }
1487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001489 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001490 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1491 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001492
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001493 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001494 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495
1496 return FAIL;
1497}
1498
1499 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001500call_partial(
1501 typval_T *tv,
1502 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001503 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001505 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001506 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001508 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001509 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510
1511 if (tv->v_type == VAR_PARTIAL)
1512 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001513 partial_T *pt = tv->vval.v_partial;
1514 int i;
1515
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001516 if (pt->pt_obj != NULL)
1517 {
1518 // partial with an object method. Push the object before the
1519 // function arguments.
1520 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1521 return FAIL;
1522 for (i = 1; i <= argcount; ++i)
1523 *STACK_TV_BOT(-i + 1) = *STACK_TV_BOT(-i);
1524
1525 typval_T *obj_tv = STACK_TV_BOT(-argcount);
1526 obj_tv->v_type = VAR_OBJECT;
1527 obj_tv->v_lock = 0;
1528 obj_tv->vval.v_object = pt->pt_obj;
1529 ++pt->pt_obj->obj_refcount;
1530 ++ectx->ec_stack.ga_len;
1531 }
1532
Bram Moolenaara90afb92020-07-15 22:38:56 +02001533 if (pt->pt_argc > 0)
1534 {
1535 // Make space for arguments from the partial, shift the "argcount"
1536 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001537 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001538 return FAIL;
1539 for (i = 1; i <= argcount; ++i)
1540 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1541 ectx->ec_stack.ga_len += pt->pt_argc;
1542 argcount += pt->pt_argc;
1543
1544 // copy the arguments from the partial onto the stack
1545 for (i = 0; i < pt->pt_argc; ++i)
1546 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1547 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001548 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549
1550 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001551 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 name = pt->pt_name;
1554 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001555 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001557 if (name != NULL)
1558 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001559 char_u fname_buf[FLEN_FIXED + 1];
1560 char_u *tofree = NULL;
1561 funcerror_T error = FCERR_NONE;
1562 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001563
1564 // May need to translate <SNR>123_ to K_SNR.
1565 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1566 if (error != FCERR_NONE)
1567 res = FAIL;
1568 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001569 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001570 vim_free(tofree);
1571 }
1572
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001573 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 {
1575 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001576 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001577 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 return FAIL;
1579 }
1580 return OK;
1581}
1582
1583/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001584 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1585 * TRUE.
1586 */
1587 static int
1588error_if_locked(int lock, char *error)
1589{
1590 if (lock & (VAR_LOCKED | VAR_FIXED))
1591 {
1592 emsg(_(error));
1593 return TRUE;
1594 }
1595 return FALSE;
1596}
1597
1598/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001599 * Give an error if "tv" is not a number and return FAIL.
1600 */
1601 static int
1602check_for_number(typval_T *tv)
1603{
1604 if (tv->v_type != VAR_NUMBER)
1605 {
1606 semsg(_(e_expected_str_but_got_str),
1607 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1608 return FAIL;
1609 }
1610 return OK;
1611}
1612
1613/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001614 * Store "tv" in variable "name".
1615 * This is for s: and g: variables.
1616 */
1617 static void
1618store_var(char_u *name, typval_T *tv)
1619{
1620 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001621 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001622
Bram Moolenaard877a572021-04-01 19:42:48 +02001623 if (tv->v_lock)
1624 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001625 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001626 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001627 restore_funccal();
1628}
1629
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001630/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001631 * Convert "tv" to a string.
1632 * Return FAIL if not allowed.
1633 */
1634 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001635do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001636{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001637 if (tv->v_type == VAR_STRING)
1638 return OK;
1639
1640 char_u *str;
1641
1642 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001643 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001644 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001645 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001646 case VAR_SPECIAL:
1647 case VAR_BOOL:
1648 case VAR_NUMBER:
1649 case VAR_FLOAT:
1650 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001651
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001652 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001653 if (tolerant)
1654 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001655 char_u *s, *e, *p;
1656 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001657
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001658 ga_init2(&ga, sizeof(char_u *), 1);
1659
1660 // Convert to NL separated items, then
1661 // escape the items and replace the NL with
1662 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001663 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001664 if (str == NULL)
1665 return FAIL;
1666 s = str;
1667 while ((e = vim_strchr(s, '\n')) != NULL)
1668 {
1669 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001670 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001671 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001672 if (p != NULL)
1673 {
1674 ga_concat(&ga, p);
1675 ga_concat(&ga, (char_u *)" ");
1676 vim_free(p);
1677 }
1678 s = e + 1;
1679 }
1680 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001681 clear_tv(tv);
1682 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001683 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001684 return OK;
1685 }
1686 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001687 default: to_string_error(tv->v_type);
1688 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001689 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001690 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001691 str = typval_tostring(tv, TRUE);
1692 clear_tv(tv);
1693 tv->v_type = VAR_STRING;
1694 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001695 return OK;
1696}
1697
1698/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001699 * When the value of "sv" is a null list of dict, allocate it.
1700 */
1701 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001702allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001703{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001704 typval_T *tv = sv->sv_tv;
1705
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001706 switch (tv->v_type)
1707 {
1708 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001709 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001710 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001711 break;
1712 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001713 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001714 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001715 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001716 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001717 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001718 (void)rettv_blob_alloc(tv);
1719 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001720 default:
1721 break;
1722 }
1723}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001724
Bram Moolenaare7525c52021-01-09 13:20:37 +01001725/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001726 * Return the character "str[index]" where "index" is the character index,
1727 * including composing characters.
1728 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001729 */
1730 char_u *
1731char_from_string(char_u *str, varnumber_T index)
1732{
1733 size_t nbyte = 0;
1734 varnumber_T nchar = index;
1735 size_t slen;
1736
1737 if (str == NULL)
1738 return NULL;
1739 slen = STRLEN(str);
1740
Bram Moolenaarff871402021-03-26 13:34:05 +01001741 // Do the same as for a list: a negative index counts from the end.
1742 // Optimization to check the first byte to be below 0x80 (and no composing
1743 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001744 if (index < 0)
1745 {
1746 int clen = 0;
1747
1748 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001749 {
1750 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1751 ++nbyte;
1752 else if (enc_utf8)
1753 nbyte += utfc_ptr2len(str + nbyte);
1754 else
1755 nbyte += mb_ptr2len(str + nbyte);
1756 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001757 nchar = clen + index;
1758 if (nchar < 0)
1759 // unlike list: index out of range results in empty string
1760 return NULL;
1761 }
1762
1763 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001764 {
1765 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1766 ++nbyte;
1767 else if (enc_utf8)
1768 nbyte += utfc_ptr2len(str + nbyte);
1769 else
1770 nbyte += mb_ptr2len(str + nbyte);
1771 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001772 if (nbyte >= slen)
1773 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001774 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001775}
1776
1777/*
1778 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001779 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001780 * If going over the end return "str_len".
1781 * If "idx" is negative count from the end, -1 is the last character.
1782 * When going over the start return -1.
1783 */
1784 static long
1785char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1786{
1787 varnumber_T nchar = idx;
1788 size_t nbyte = 0;
1789
1790 if (nchar >= 0)
1791 {
1792 while (nchar > 0 && nbyte < str_len)
1793 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001794 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001795 --nchar;
1796 }
1797 }
1798 else
1799 {
1800 nbyte = str_len;
1801 while (nchar < 0 && nbyte > 0)
1802 {
1803 --nbyte;
1804 nbyte -= mb_head_off(str, str + nbyte);
1805 ++nchar;
1806 }
1807 if (nchar < 0)
1808 return -1;
1809 }
1810 return (long)nbyte;
1811}
1812
1813/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001814 * Return the slice "str[first : last]" using character indexes. Composing
1815 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001816 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001817 * Return NULL when the result is empty.
1818 */
1819 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001820string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001821{
1822 long start_byte, end_byte;
1823 size_t slen;
1824
1825 if (str == NULL)
1826 return NULL;
1827 slen = STRLEN(str);
1828 start_byte = char_idx2byte(str, slen, first);
1829 if (start_byte < 0)
1830 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001831 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001832 end_byte = (long)slen;
1833 else
1834 {
1835 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001836 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001837 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001838 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001839 }
1840
1841 if (start_byte >= (long)slen || end_byte <= start_byte)
1842 return NULL;
1843 return vim_strnsave(str + start_byte, end_byte - start_byte);
1844}
1845
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001846/*
1847 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1848 * When "dfunc_idx" is negative don't give an error.
1849 * Returns NULL for an error.
1850 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001851 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001852get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001853{
1854 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001855 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1856 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001857 svar_T *sv;
1858
1859 if (sref->sref_seq != si->sn_script_seq)
1860 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001861 // The script was reloaded after the function was compiled, the
1862 // script_idx may not be valid.
1863 if (dfunc != NULL)
1864 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1865 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001866 return NULL;
1867 }
1868 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001869 if (sv->sv_name == NULL)
1870 {
1871 if (dfunc != NULL)
1872 emsg(_(e_script_variable_was_deleted));
1873 return NULL;
1874 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001875 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001876 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001877 if (dfunc != NULL)
1878 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001879 return NULL;
1880 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001881
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001882 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1883 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001884 {
1885 if (dfunc != NULL)
1886 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1887 return NULL;
1888 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001889 return sv;
1890}
1891
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001892/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001893 * Function passed to do_cmdline() for splitting a script joined by NL
1894 * characters.
1895 */
1896 static char_u *
1897get_split_sourceline(
1898 int c UNUSED,
1899 void *cookie,
1900 int indent UNUSED,
1901 getline_opt_T options UNUSED)
1902{
1903 source_cookie_T *sp = (source_cookie_T *)cookie;
1904 char_u *p;
1905 char_u *line;
1906
Bram Moolenaar20677332021-06-06 17:02:53 +02001907 p = vim_strchr(sp->nextline, '\n');
1908 if (p == NULL)
1909 {
1910 line = vim_strsave(sp->nextline);
1911 sp->nextline += STRLEN(sp->nextline);
1912 }
1913 else
1914 {
1915 line = vim_strnsave(sp->nextline, p - sp->nextline);
1916 sp->nextline = p + 1;
1917 }
1918 return line;
1919}
1920
1921/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 * Execute a function by "name".
1923 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001924 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 */
1926 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001927call_eval_func(
1928 char_u *name,
1929 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001930 ectx_T *ectx,
1931 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932{
Bram Moolenaared677f52020-08-12 16:38:10 +02001933 int called_emsg_before = called_emsg;
1934 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001936 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001937 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001939 dictitem_T *v;
1940
1941 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001942 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1943 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001944 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001945 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001946 return FAIL;
1947 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001948 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001950 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951}
1952
1953/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001954 * When a function reference is used, fill a partial with the information
1955 * needed, especially when it is used as a closure.
1956 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001957 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001958fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001959 partial_T *pt,
1960 ufunc_T *ufunc,
1961 loopvarinfo_T *lvi,
1962 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001963{
1964 pt->pt_func = ufunc;
1965 pt->pt_refcount = 1;
1966
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001967 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001968 {
1969 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1970 + ectx->ec_dfunc_idx;
1971
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001972 // The closure may need to find arguments and local variables of the
1973 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001974 pt->pt_outer.out_stack = &ectx->ec_stack;
1975 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001976 if (ectx->ec_outer_ref != NULL)
1977 {
1978 // The current context already has a context, link to that one.
1979 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1980 if (ectx->ec_outer_ref->or_partial != NULL)
1981 {
1982 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1983 ++pt->pt_outer.out_up_partial->pt_refcount;
1984 }
1985 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001986
Bram Moolenaarcc341812022-09-19 15:54:34 +01001987 if (lvi != NULL)
1988 {
1989 int depth;
1990
1991 // The closure may need to find variables defined inside a loop,
1992 // for every nested loop. A new reference is made every time,
1993 // ISN_ENDLOOP will check if they are actually used.
1994 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1995 {
1996 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1997 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1998 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1999 pt->pt_outer.out_loop[depth].var_count =
2000 lvi->lvi_loop[depth].var_count;
2001 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002002 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002003 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002004 else
2005 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002006
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002007 // If the function currently executing returns and the closure is still
2008 // being referenced, we need to make a copy of the context (arguments
2009 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002010 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02002011 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01002012 {
2013 vim_free(pt);
2014 return FAIL;
2015 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002016 // Extra variable keeps the count of closures created in the current
2017 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01002018 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
2019 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
2020
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002021 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
2022 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002023 ++pt->pt_refcount;
2024 ++ectx->ec_funcrefs.ga_len;
2025 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002026 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002027 return OK;
2028}
2029
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002030/*
2031 * Execute iptr->isn_arg.string as an Ex command.
2032 */
2033 static int
Ernie Raelee865f32023-09-29 19:53:55 +02002034exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002035{
2036 source_cookie_T cookie;
2037
2038 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002039 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002040 CLEAR_FIELD(cookie);
2041 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02002042 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002043 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
2044 || did_emsg)
2045 return FAIL;
2046 return OK;
2047}
2048
Bram Moolenaar89445512022-04-14 12:58:23 +01002049/*
2050 * If script "sid" is not loaded yet then load it now.
2051 * Caller must make sure "sid" is a valid script ID.
2052 * "loaded" is set to TRUE if the script had to be loaded.
2053 * Returns FAIL if loading fails, OK if already loaded or loaded now.
2054 */
2055 int
2056may_load_script(int sid, int *loaded)
2057{
2058 scriptitem_T *si = SCRIPT_ITEM(sid);
2059
2060 if (si->sn_state == SN_STATE_NOT_LOADED)
2061 {
2062 *loaded = TRUE;
2063 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
2064 {
2065 semsg(_(e_cant_open_file_str), si->sn_name);
2066 return FAIL;
2067 }
2068 }
2069 return OK;
2070}
2071
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002072// used for v_instr of typval of VAR_INSTR
2073struct instr_S {
2074 ectx_T *instr_ectx;
2075 isn_T *instr_instr;
2076};
2077
Bram Moolenaar4c137212021-04-19 16:48:48 +02002078// used for substitute_instr
2079typedef struct subs_expr_S {
2080 ectx_T *subs_ectx;
2081 isn_T *subs_instr;
2082 int subs_status;
2083} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002085// Set when calling do_debug().
2086static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002087static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002088
2089/*
2090 * When debugging lookup "name" and return the typeval.
2091 * When not found return NULL.
2092 */
2093 typval_T *
2094lookup_debug_var(char_u *name)
2095{
2096 int idx;
2097 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002098 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002099 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002100 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002101
2102 if (ectx == NULL)
2103 return NULL;
2104 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2105
2106 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002107 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002108 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002109 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2110
2111 // the variable name may be NULL when not available in this block
2112 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002113 return STACK_TV_VAR(idx);
2114 }
2115
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002116 // Go through argument names.
2117 ufunc = dfunc->df_ufunc;
2118 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2119 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2120 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2121 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2122 - varargs_off + idx);
2123 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2124 return STACK_TV(ectx->ec_frame_idx - 1);
2125
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002126 return NULL;
2127}
2128
Bram Moolenaar26a44842021-09-02 18:49:06 +02002129/*
2130 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2131 * breakpoint was set in that function or when there is any expression.
2132 */
2133 int
2134may_break_in_function(ufunc_T *ufunc)
2135{
2136 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2137}
2138
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002139 static void
2140handle_debug(isn_T *iptr, ectx_T *ectx)
2141{
2142 char_u *line;
2143 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2144 + ectx->ec_dfunc_idx)->df_ufunc;
2145 isn_T *ni;
2146 int end_lnum = iptr->isn_lnum;
2147 garray_T ga;
2148 int lnum;
2149
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002150 if (ex_nesting_level > debug_break_level)
2151 {
2152 linenr_T breakpoint;
2153
Bram Moolenaar26a44842021-09-02 18:49:06 +02002154 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002155 return;
2156
2157 // check for the next breakpoint if needed
2158 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002159 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002160 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2161 return;
2162 }
2163
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002164 SOURCING_LNUM = iptr->isn_lnum;
2165 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002166 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002167
2168 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2169 if (ni->isn_type == ISN_DEBUG
2170 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002171 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002172 || ni->isn_type == ISN_RETURN_VOID)
2173 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002174 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002175 break;
2176 }
2177
2178 if (end_lnum > iptr->isn_lnum)
2179 {
2180 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002181 for (lnum = iptr->isn_lnum; lnum < end_lnum
2182 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002183 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002184 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002185
Bram Moolenaar303215d2021-07-07 20:10:43 +02002186 if (p == NULL)
2187 continue; // left over from continuation line
2188 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002189 if (*p == '#')
2190 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002191 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002192 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2193 if (STRNCMP(p, "def ", 4) == 0)
2194 break;
2195 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002196 line = ga_concat_strings(&ga, " ");
2197 vim_free(ga.ga_data);
2198 }
2199 else
2200 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002201
Dominique Pelle6e969552021-06-17 13:53:41 +02002202 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002203 debug_context = NULL;
2204
2205 if (end_lnum > iptr->isn_lnum)
2206 vim_free(line);
2207}
2208
Bram Moolenaar4c137212021-04-19 16:48:48 +02002209/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002210 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002211 * Returns OK, FAIL or NOTDONE (uncatchable error).
2212 */
2213 static int
2214execute_storeindex(isn_T *iptr, ectx_T *ectx)
2215{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002216 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002217 typval_T *tv;
2218 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002219 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002220 typval_T *tv_dest = STACK_TV_BOT(-1);
2221 int status = OK;
2222
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002223 if (tv_idx->v_type == VAR_NUMBER)
2224 lidx = (long)tv_idx->vval.v_number;
2225
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002226 // Stack contains:
2227 // -3 value to be stored
2228 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002229 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002230 tv = STACK_TV_BOT(-3);
2231 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002232
2233 // Make sure an object has been initialized
2234 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2235 {
2236 emsg(_(e_using_null_object));
2237 status = FAIL;
2238 }
2239 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002240 {
2241 dest_type = tv_dest->v_type;
2242 if (dest_type == VAR_DICT)
2243 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002244 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2245 {
2246 // Need to get the member index now that the class is known.
2247 object_T *obj = tv_dest->vval.v_object;
2248 class_T *cl = obj->obj_class;
2249 char_u *member = tv_idx->vval.v_string;
2250
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002251 int m_idx;
2252 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2253 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002254 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002255 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002256 {
Ernie Rael03042a22023-11-11 08:53:32 +01002257 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +02002258 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002259 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002260 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002261
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002262 lidx = m_idx;
2263 }
2264 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002265 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002266 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002267 status = FAIL;
2268 }
2269 }
2270 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2271 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002272 {
2273 emsg(_(e_number_expected));
2274 status = FAIL;
2275 }
2276 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002277
2278 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002279 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002280 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002281 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002282 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002283
Bram Moolenaare08be092022-02-17 13:08:26 +00002284 if (list == NULL)
2285 {
2286 emsg(_(e_list_not_set));
2287 return FAIL;
2288 }
2289 if (lidx < 0 && list->lv_len + lidx >= 0)
2290 // negative index is relative to the end
2291 lidx = list->lv_len + lidx;
2292 if (lidx < 0 || lidx > list->lv_len)
2293 {
2294 semsg(_(e_list_index_out_of_range_nr), lidx);
2295 return FAIL;
2296 }
2297 if (lidx < list->lv_len)
2298 {
2299 listitem_T *li = list_find(list, lidx);
2300
2301 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002302 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002303 return FAIL;
2304 // overwrite existing list item
2305 clear_tv(&li->li_tv);
2306 li->li_tv = *tv;
2307 }
2308 else
2309 {
2310 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2311 return FAIL;
2312 // append to list, only fails when out of memory
2313 if (list_append_tv(list, tv) == FAIL)
2314 return NOTDONE;
2315 clear_tv(tv);
2316 }
2317 }
2318 else if (dest_type == VAR_DICT)
2319 {
2320 char_u *key = tv_idx->vval.v_string;
2321 dict_T *dict = tv_dest->vval.v_dict;
2322 dictitem_T *di;
2323
2324 SOURCING_LNUM = iptr->isn_lnum;
2325 if (dict == NULL)
2326 {
2327 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002328 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002329 }
2330 if (key == NULL)
2331 key = (char_u *)"";
2332 di = dict_find(dict, key, -1);
2333 if (di != NULL)
2334 {
2335 if (error_if_locked(di->di_tv.v_lock,
2336 e_cannot_change_dict_item))
2337 return FAIL;
2338 // overwrite existing value
2339 clear_tv(&di->di_tv);
2340 di->di_tv = *tv;
2341 }
2342 else
2343 {
2344 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2345 return FAIL;
2346 // add to dict, only fails when out of memory
2347 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2348 return NOTDONE;
2349 clear_tv(tv);
2350 }
2351 }
2352 else if (dest_type == VAR_BLOB)
2353 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002354 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002355 varnumber_T nr;
2356 int error = FALSE;
2357 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002358
2359 if (blob == NULL)
2360 {
2361 emsg(_(e_blob_not_set));
2362 return FAIL;
2363 }
2364 len = blob_len(blob);
2365 if (lidx < 0 && len + lidx >= 0)
2366 // negative index is relative to the end
2367 lidx = len + lidx;
2368
2369 // Can add one byte at the end.
2370 if (lidx < 0 || lidx > len)
2371 {
2372 semsg(_(e_blob_index_out_of_range_nr), lidx);
2373 return FAIL;
2374 }
2375 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2376 return FAIL;
2377 nr = tv_get_number_chk(tv, &error);
2378 if (error)
2379 return FAIL;
2380 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002381 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002382 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2383 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002384 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002385
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002386 if (dest_type == VAR_OBJECT)
2387 {
2388 object_T *obj = tv_dest->vval.v_object;
2389
2390 otv = (typval_T *)(obj + 1);
2391 class_T *itf = iptr->isn_arg.storeindex.si_class;
2392 if (itf != NULL)
2393 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002394 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002395 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002396 }
2397 else
2398 {
2399 // VAR_CLASS
2400 class_T *class = tv_dest->vval.v_class;
2401 otv = class->class_members_tv;
2402 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002403
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002404 clear_tv(&otv[lidx]);
2405 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002406 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002407 else
2408 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002409 status = FAIL;
2410 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002411 }
2412 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002413
2414 clear_tv(tv_idx);
2415 clear_tv(tv_dest);
2416 ectx->ec_stack.ga_len -= 3;
2417 if (status == FAIL)
2418 {
2419 clear_tv(tv);
2420 return FAIL;
2421 }
2422 return OK;
2423}
2424
2425/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002426 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002427 */
2428 static int
2429execute_storerange(isn_T *iptr, ectx_T *ectx)
2430{
2431 typval_T *tv;
2432 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2433 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2434 typval_T *tv_dest = STACK_TV_BOT(-1);
2435 int status = OK;
2436
2437 // Stack contains:
2438 // -4 value to be stored
2439 // -3 first index or "none"
2440 // -2 second index or "none"
2441 // -1 destination list or blob
2442 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002443 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002444 if (tv_dest->v_type == VAR_LIST)
2445 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002446 long n1;
2447 long n2;
2448 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002449
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002450 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2451 if (tv_idx2->v_type == VAR_SPECIAL
2452 && tv_idx2->vval.v_number == VVAL_NONE)
2453 n2 = list_len(tv_dest->vval.v_list) - 1;
2454 else
2455 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2456
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002457 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002458 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002459 status = FAIL;
2460 else
2461 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002462 status = check_range_index_two(tv_dest->vval.v_list,
2463 &n1, li1, &n2, FALSE);
2464 if (status != FAIL)
2465 status = list_assign_range(
2466 tv_dest->vval.v_list,
2467 tv->vval.v_list,
2468 n1,
2469 n2,
2470 tv_idx2->v_type == VAR_SPECIAL,
2471 (char_u *)"=",
2472 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002473 }
2474 }
2475 else if (tv_dest->v_type == VAR_BLOB)
2476 {
2477 varnumber_T n1;
2478 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002479 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002480
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002481 n1 = tv_get_number_chk(tv_idx1, NULL);
2482 if (tv_idx2->v_type == VAR_SPECIAL
2483 && tv_idx2->vval.v_number == VVAL_NONE)
2484 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2485 else
2486 n2 = tv_get_number_chk(tv_idx2, NULL);
2487 bloblen = blob_len(tv_dest->vval.v_blob);
2488
2489 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2490 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002491 status = FAIL;
2492 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002493 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002494 }
2495 else
2496 {
2497 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002498 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002499 }
2500
2501 clear_tv(tv_idx1);
2502 clear_tv(tv_idx2);
2503 clear_tv(tv_dest);
2504 ectx->ec_stack.ga_len -= 4;
2505 clear_tv(tv);
2506
2507 return status;
2508}
2509
2510/*
2511 * Unlet item in list or dict variable.
2512 */
2513 static int
2514execute_unletindex(isn_T *iptr, ectx_T *ectx)
2515{
2516 typval_T *tv_idx = STACK_TV_BOT(-2);
2517 typval_T *tv_dest = STACK_TV_BOT(-1);
2518 int status = OK;
2519
2520 // Stack contains:
2521 // -2 index
2522 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002523 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002524 if (tv_dest->v_type == VAR_DICT)
2525 {
2526 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002527 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002528 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002529 semsg(_(e_expected_str_but_got_str),
2530 vartype_name(VAR_STRING),
2531 vartype_name(tv_idx->v_type));
2532 status = FAIL;
2533 }
2534 else
2535 {
2536 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002537 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002538 dictitem_T *di = NULL;
2539
2540 if (d != NULL && value_check_lock(
2541 d->dv_lock, NULL, FALSE))
2542 status = FAIL;
2543 else
2544 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002545 if (tv_idx->v_type == VAR_STRING)
2546 {
2547 key = tv_idx->vval.v_string;
2548 if (key == NULL)
2549 key = (char_u *)"";
2550 }
2551 else
2552 {
2553 key = tv_get_string(tv_idx);
2554 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002555 if (d != NULL)
2556 di = dict_find(d, key, (int)STRLEN(key));
2557 if (di == NULL)
2558 {
2559 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002560 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002561 status = FAIL;
2562 }
2563 else if (var_check_fixed(di->di_flags,
2564 NULL, FALSE)
2565 || var_check_ro(di->di_flags,
2566 NULL, FALSE))
2567 status = FAIL;
2568 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002569 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002570 }
2571 }
2572 }
2573 else if (tv_dest->v_type == VAR_LIST)
2574 {
2575 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002576 if (check_for_number(tv_idx) == FAIL)
2577 {
2578 status = FAIL;
2579 }
2580 else
2581 {
2582 list_T *l = tv_dest->vval.v_list;
2583 long n = (long)tv_idx->vval.v_number;
2584
2585 if (l != NULL && value_check_lock(
2586 l->lv_lock, NULL, FALSE))
2587 status = FAIL;
2588 else
2589 {
2590 listitem_T *li = list_find(l, n);
2591
2592 if (li == NULL)
2593 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002594 semsg(_(e_list_index_out_of_range_nr), n);
2595 status = FAIL;
2596 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002597 else
2598 listitem_remove(l, li);
2599 }
2600 }
2601 }
2602 else
2603 {
2604 status = FAIL;
2605 semsg(_(e_cannot_index_str),
2606 vartype_name(tv_dest->v_type));
2607 }
2608
2609 clear_tv(tv_idx);
2610 clear_tv(tv_dest);
2611 ectx->ec_stack.ga_len -= 2;
2612
2613 return status;
2614}
2615
2616/*
2617 * Unlet a range of items in a list variable.
2618 */
2619 static int
2620execute_unletrange(isn_T *iptr, ectx_T *ectx)
2621{
2622 // Stack contains:
2623 // -3 index1
2624 // -2 index2
2625 // -1 dict or list
2626 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2627 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2628 typval_T *tv_dest = STACK_TV_BOT(-1);
2629 int status = OK;
2630
2631 if (tv_dest->v_type == VAR_LIST)
2632 {
2633 // indexes must be a number
2634 SOURCING_LNUM = iptr->isn_lnum;
2635 if (check_for_number(tv_idx1) == FAIL
2636 || (tv_idx2->v_type != VAR_SPECIAL
2637 && check_for_number(tv_idx2) == FAIL))
2638 {
2639 status = FAIL;
2640 }
2641 else
2642 {
2643 list_T *l = tv_dest->vval.v_list;
2644 long n1 = (long)tv_idx1->vval.v_number;
2645 long n2 = tv_idx2->v_type == VAR_SPECIAL
2646 ? 0 : (long)tv_idx2->vval.v_number;
2647 listitem_T *li;
2648
2649 li = list_find_index(l, &n1);
2650 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002651 {
2652 semsg(_(e_list_index_out_of_range_nr),
2653 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002654 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002655 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002656 else
2657 {
2658 if (n1 < 0)
2659 n1 = list_idx_of_item(l, li);
2660 if (n2 < 0)
2661 {
2662 listitem_T *li2 = list_find(l, n2);
2663
2664 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002665 {
2666 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002667 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002668 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002669 else
2670 n2 = list_idx_of_item(l, li2);
2671 }
2672 if (status != FAIL
2673 && tv_idx2->v_type != VAR_SPECIAL
2674 && n2 < n1)
2675 {
2676 semsg(_(e_list_index_out_of_range_nr), n2);
2677 status = FAIL;
2678 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002679 if (status != FAIL)
2680 list_unlet_range(l, li, n1,
2681 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002682 }
2683 }
2684 }
2685 else
2686 {
2687 status = FAIL;
2688 SOURCING_LNUM = iptr->isn_lnum;
2689 semsg(_(e_cannot_index_str),
2690 vartype_name(tv_dest->v_type));
2691 }
2692
2693 clear_tv(tv_idx1);
2694 clear_tv(tv_idx2);
2695 clear_tv(tv_dest);
2696 ectx->ec_stack.ga_len -= 3;
2697
2698 return status;
2699}
2700
2701/*
2702 * Top of a for loop.
2703 */
2704 static int
2705execute_for(isn_T *iptr, ectx_T *ectx)
2706{
2707 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002708 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002709 typval_T *ltv = STACK_TV_BOT(-1);
2710 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002711 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002712
2713 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2714 return FAIL;
2715 if (ltv->v_type == VAR_LIST)
2716 {
2717 list_T *list = ltv->vval.v_list;
2718
2719 // push the next item from the list
2720 ++idxtv->vval.v_number;
2721 if (list == NULL
2722 || idxtv->vval.v_number >= list->lv_len)
2723 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002724 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002725 }
2726 else if (list->lv_first == &range_list_item)
2727 {
2728 // non-materialized range() list
2729 tv = STACK_TV_BOT(0);
2730 tv->v_type = VAR_NUMBER;
2731 tv->v_lock = 0;
2732 tv->vval.v_number = list_find_nr(
2733 list, idxtv->vval.v_number, NULL);
2734 ++ectx->ec_stack.ga_len;
2735 }
2736 else
2737 {
2738 listitem_T *li = list_find(list,
2739 idxtv->vval.v_number);
2740
2741 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2742 ++ectx->ec_stack.ga_len;
2743 }
2744 }
2745 else if (ltv->v_type == VAR_STRING)
2746 {
2747 char_u *str = ltv->vval.v_string;
2748
2749 // The index is for the last byte of the previous
2750 // character.
2751 ++idxtv->vval.v_number;
2752 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2753 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002754 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002755 }
2756 else
2757 {
2758 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2759
2760 // Push the next character from the string.
2761 tv = STACK_TV_BOT(0);
2762 tv->v_type = VAR_STRING;
2763 tv->vval.v_string = vim_strnsave(
2764 str + idxtv->vval.v_number, clen);
2765 ++ectx->ec_stack.ga_len;
2766 idxtv->vval.v_number += clen - 1;
2767 }
2768 }
2769 else if (ltv->v_type == VAR_BLOB)
2770 {
2771 blob_T *blob = ltv->vval.v_blob;
2772
2773 // When we get here the first time make a copy of the
2774 // blob, so that the iteration still works when it is
2775 // changed.
2776 if (idxtv->vval.v_number == -1 && blob != NULL)
2777 {
2778 blob_copy(blob, ltv);
2779 blob_unref(blob);
2780 blob = ltv->vval.v_blob;
2781 }
2782
2783 // The index is for the previous byte.
2784 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002785 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002786 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002787 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002788 }
2789 else
2790 {
2791 // Push the next byte from the blob.
2792 tv = STACK_TV_BOT(0);
2793 tv->v_type = VAR_NUMBER;
2794 tv->vval.v_number = blob_get(blob,
2795 idxtv->vval.v_number);
2796 ++ectx->ec_stack.ga_len;
2797 }
2798 }
2799 else
2800 {
2801 semsg(_(e_for_loop_on_str_not_supported),
2802 vartype_name(ltv->v_type));
2803 return FAIL;
2804 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002805
2806 if (jump)
2807 {
2808 // past the end of the list/string/blob, jump to "endfor"
2809 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2810 may_restore_cmdmod(&ectx->ec_funclocal);
2811 }
2812 else
2813 {
2814 // Store the current number of funcrefs, this may be used in
2815 // ISN_LOOPEND. The variable index is always one more than the loop
2816 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002817 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002818 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2819 }
2820
2821 return OK;
2822}
2823
2824/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002825 * Code for handling variables declared inside a loop and used in a closure.
2826 * This is very similar to what is done with funcstack_T. The difference is
2827 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2828 * scope of the block inside a loop and each loop may have its own.
2829 */
2830
2831// Double linked list of loopvars_T in use.
2832static loopvars_T *first_loopvars = NULL;
2833
2834 static void
2835add_loopvars_to_list(loopvars_T *loopvars)
2836{
2837 // Link in list of loopvarss.
2838 if (first_loopvars != NULL)
2839 first_loopvars->lvs_prev = loopvars;
2840 loopvars->lvs_next = first_loopvars;
2841 loopvars->lvs_prev = NULL;
2842 first_loopvars = loopvars;
2843}
2844
2845 static void
2846remove_loopvars_from_list(loopvars_T *loopvars)
2847{
2848 if (loopvars->lvs_prev == NULL)
2849 first_loopvars = loopvars->lvs_next;
2850 else
2851 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2852 if (loopvars->lvs_next != NULL)
2853 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2854}
2855
2856/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002857 * End of a for or while loop: Handle any variables used by a closure.
2858 */
2859 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002860execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002861{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002862 endloop_T *endloop = &iptr->isn_arg.endloop;
2863 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2864 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002865 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002866 garray_T *gap = &ectx->ec_funcrefs;
2867 int closure_in_use = FALSE;
2868 loopvars_T *loopvars;
2869 typval_T *stack;
2870 int idx;
2871
Bram Moolenaarcc341812022-09-19 15:54:34 +01002872 // Check if any created closure is still being referenced and loopvars have
2873 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002874 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2875 {
2876 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2877
Bram Moolenaarcc341812022-09-19 15:54:34 +01002878 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002879 {
2880 int refcount = pt->pt_refcount;
2881 int i;
2882
2883 // A Reference in a variable inside the loop doesn't count, it gets
2884 // unreferenced at the end of the loop.
2885 for (i = 0; i < endloop->end_var_count; ++i)
2886 {
2887 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2888
2889 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2890 --refcount;
2891 }
2892 if (refcount > 1)
2893 {
2894 closure_in_use = TRUE;
2895 break;
2896 }
2897 }
2898 }
2899
2900 // If no function reference were created since the start of the loop block
2901 // or it is no longer referenced there is nothing to do.
2902 if (!closure_in_use)
2903 return OK;
2904
2905 // A closure is using variables declared inside the loop.
2906 // Move them to the called function.
2907 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2908 if (loopvars == NULL)
2909 return FAIL;
2910
2911 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2912 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2913 loopvars->lvs_ga.ga_data = stack;
2914 if (stack == NULL)
2915 {
2916 vim_free(loopvars);
2917 return FAIL;
2918 }
2919 add_loopvars_to_list(loopvars);
2920
2921 // Move the variable values.
2922 for (idx = 0; idx < endloop->end_var_count; ++idx)
2923 {
2924 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2925
2926 *(stack + idx) = *tv;
2927 tv->v_type = VAR_UNKNOWN;
2928 }
2929
2930 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2931 {
2932 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2933
Bram Moolenaarcc341812022-09-19 15:54:34 +01002934 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002935 {
2936 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002937 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002938
Bram Moolenaarcc341812022-09-19 15:54:34 +01002939 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2940 pt->pt_outer.out_loop[depth].var_idx -=
2941 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002942 }
2943 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002944
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002945 return OK;
2946}
2947
2948/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002949 * Called when a partial is freed or its reference count goes down to one. The
2950 * loopvars may be the only reference to the partials in the local variables.
2951 * Go over all of them, the funcref and can be freed if all partials
2952 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002953 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002954 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002955 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002956loopvars_check_refcount(loopvars_T *loopvars)
2957{
2958 int i;
2959 garray_T *gap = &loopvars->lvs_ga;
2960 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002961 typval_T *stack = gap->ga_data;
2962
Bram Moolenaarcc341812022-09-19 15:54:34 +01002963 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2964 return FALSE;
2965 for (i = 0; i < gap->ga_len; ++i)
2966 {
2967 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2968
2969 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2970 && tv->vval.v_partial->pt_refcount == 1)
2971 {
2972 int depth;
2973
2974 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2975 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2976 ++done;
2977 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002978 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002979 if (done != loopvars->lvs_min_refcount)
2980 return FALSE;
2981
2982 // All partials referencing the loopvars have a reference count of
2983 // one, thus the loopvars is no longer of use.
2984 stack = gap->ga_data;
2985 for (i = 0; i < gap->ga_len; ++i)
2986 clear_tv(stack + i);
2987 vim_free(stack);
2988 remove_loopvars_from_list(loopvars);
2989 vim_free(loopvars);
2990 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002991}
2992
2993/*
2994 * For garbage collecting: set references in all variables referenced by
2995 * all loopvars.
2996 */
2997 int
2998set_ref_in_loopvars(int copyID)
2999{
3000 loopvars_T *loopvars;
3001
3002 for (loopvars = first_loopvars; loopvars != NULL;
3003 loopvars = loopvars->lvs_next)
3004 {
3005 typval_T *stack = loopvars->lvs_ga.ga_data;
3006 int i;
3007
3008 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
3009 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
3010 return TRUE; // abort
3011 }
3012 return FALSE;
3013}
3014
3015/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00003016 * Load instruction for w:/b:/g:/t: variable.
3017 * "isn_type" is used instead of "iptr->isn_type".
3018 */
3019 static int
3020load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
3021{
3022 dictitem_T *di = NULL;
3023 hashtab_T *ht = NULL;
3024 char namespace;
3025
3026 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3027 return NOTDONE;
3028 switch (isn_type)
3029 {
3030 case ISN_LOADG:
3031 ht = get_globvar_ht();
3032 namespace = 'g';
3033 break;
3034 case ISN_LOADB:
3035 ht = &curbuf->b_vars->dv_hashtab;
3036 namespace = 'b';
3037 break;
3038 case ISN_LOADW:
3039 ht = &curwin->w_vars->dv_hashtab;
3040 namespace = 'w';
3041 break;
3042 case ISN_LOADT:
3043 ht = &curtab->tp_vars->dv_hashtab;
3044 namespace = 't';
3045 break;
3046 default: // Cannot reach here
3047 return NOTDONE;
3048 }
3049 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3050
Bram Moolenaar06b77222022-01-25 15:51:56 +00003051 if (di == NULL)
3052 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003053 if (isn_type == ISN_LOADG)
3054 {
3055 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3056
3057 // g:Something could be a function
3058 if (ufunc != NULL)
3059 {
3060 typval_T *tv = STACK_TV_BOT(0);
3061
3062 ++ectx->ec_stack.ga_len;
3063 tv->v_type = VAR_FUNC;
3064 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3065 if (tv->vval.v_string == NULL)
3066 return FAIL;
3067 STRCPY(tv->vval.v_string, "g:");
3068 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3069 return OK;
3070 }
3071 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003072 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003073 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003074 // no check if the item exists in the script but
3075 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003076 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003077 else
3078 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003079 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003080 return FAIL;
3081 }
3082 else
3083 {
3084 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3085 ++ectx->ec_stack.ga_len;
3086 }
3087 return OK;
3088}
3089
Bram Moolenaard0200c82023-01-28 15:19:40 +00003090
3091 static void
3092object_required_error(typval_T *tv)
3093{
3094 garray_T type_list;
3095 ga_init2(&type_list, sizeof(type_T *), 10);
3096 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3097 char *tofree = NULL;
3098 char *typename = type_name(type, &tofree);
3099 semsg(_(e_object_required_found_str), typename);
3100 vim_free(tofree);
3101 clear_type_list(&type_list);
3102}
3103
Bram Moolenaar06b77222022-01-25 15:51:56 +00003104/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003105 * Execute instructions in execution context "ectx".
3106 * Return OK or FAIL;
3107 */
3108 static int
3109exec_instructions(ectx_T *ectx)
3110{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003111 int ret = FAIL;
3112 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003113 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003114
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003115 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003116 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003117
Bram Moolenaarff652882021-05-16 15:24:49 +02003118 // Only catch exceptions in this instruction list.
3119 ectx->ec_trylevel_at_start = trylevel;
3120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 for (;;)
3122 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003123 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003125 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003126
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003127 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003128 {
3129 line_breakcheck();
3130 breakcheck_count = 0;
3131 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003132 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003133 {
3134 // Turn CTRL-C into an exception.
3135 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003136 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003137 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003138 did_throw = TRUE;
3139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003141 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003142 {
3143 // Turn an error message into an exception.
3144 did_emsg = FALSE;
3145 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003146 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003147 did_throw = TRUE;
3148 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003149
3150 // This exception was not caught (yet).
3151 garray_T *trystack = &ectx->ec_trystack;
3152 if (trystack->ga_len > 0)
3153 {
3154 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3155 + trystack->ga_len - 1;
3156 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3157 trycmd->tcd_caught = FALSE;
3158 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003159 }
3160
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003161 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003163 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003164 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003165 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166
3167 // An exception jumps to the first catch, finally, or returns from
3168 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003169 while (index > 0)
3170 {
3171 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003172 // 1. after :try and before :catch - jump to first :catch
3173 // 2. in :catch block - jump to :finally
3174 // 3. in :catch block and no finally - jump to :endtry
3175 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3176 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003177 break;
3178 // In the catch and finally block of this try we have to go up
3179 // one level.
3180 --index;
3181 trycmd = NULL;
3182 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003183 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003185 if (trycmd->tcd_in_catch)
3186 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003187 if (trycmd->tcd_finally_idx > 0)
3188 {
3189 // exception inside ":catch", jump to ":finally" once
3190 ectx->ec_iidx = trycmd->tcd_finally_idx;
3191 trycmd->tcd_finally_idx = 0;
3192 }
3193 else
3194 {
3195 // exception inside ":catch" or ":finally", jump to
3196 // ":endtry"
3197 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3198 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003199 }
3200 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003201 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003202 // jump to first ":catch"
3203 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003204 trycmd->tcd_in_catch = TRUE;
3205 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003206 did_throw = FALSE; // don't come back here until :endtry
3207 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 }
3209 else
3210 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003211 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003212 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003213 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003214 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003215 tv = STACK_TV_BOT(0);
3216 tv->v_type = VAR_NUMBER;
3217 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003218 ++ectx->ec_stack.ga_len;
3219 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003221 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003222 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003223 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003224 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 goto done;
3226 }
3227
Bram Moolenaar4c137212021-04-19 16:48:48 +02003228 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003229 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 }
3231 continue;
3232 }
3233
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003234 /*
3235 * Big switch on the instruction. Most compilers will be turning this
3236 * into an efficient lookup table, since the "case" values are an enum
3237 * with sequential numbers. It may look ugly, but it should be the
3238 * most efficient way.
3239 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003240 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 switch (iptr->isn_type)
3242 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003243 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003244 case ISN_CONSTRUCT:
3245 // "this" is always the local variable at index zero
3246 tv = STACK_TV_VAR(0);
3247 tv->v_type = VAR_OBJECT;
3248 tv->vval.v_object = alloc_clear(
3249 iptr->isn_arg.construct.construct_size);
3250 tv->vval.v_object->obj_class =
3251 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003252 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003253 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003254 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003255 break;
3256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 // execute Ex command line
3258 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003259 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003260 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 break;
3262
Bram Moolenaar20677332021-06-06 17:02:53 +02003263 // execute Ex command line split at NL characters.
3264 case ISN_EXEC_SPLIT:
3265 {
3266 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003267 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003268
3269 SOURCING_LNUM = iptr->isn_lnum;
3270 CLEAR_FIELD(cookie);
3271 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3272 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003273 line = get_split_sourceline(0, &cookie, 0, 0);
3274 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003275 get_split_sourceline, &cookie,
3276 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3277 == FAIL
3278 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003279 {
3280 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003281 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003282 }
3283 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003284 }
3285 break;
3286
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003287 // execute Ex command line that is only a range
3288 case ISN_EXECRANGE:
3289 {
3290 exarg_T ea;
3291 char *error = NULL;
3292
3293 CLEAR_FIELD(ea);
3294 ea.cmdidx = CMD_SIZE;
3295 ea.addr_type = ADDR_LINES;
3296 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003297 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003298 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003299 if (ea.cmd == NULL)
3300 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003301 // error is always NULL when using ADDR_LINES
3302 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003303 if (error != NULL)
3304 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003305 emsg(error);
3306 goto on_error;
3307 }
3308 }
3309 break;
3310
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003311 // Evaluate an expression with legacy syntax, push it onto the
3312 // stack.
3313 case ISN_LEGACY_EVAL:
3314 {
3315 char_u *arg = iptr->isn_arg.string;
3316 int res;
3317 int save_flags = cmdmod.cmod_flags;
3318
Bram Moolenaar35578162021-08-02 19:10:38 +02003319 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003320 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003321 tv = STACK_TV_BOT(0);
3322 init_tv(tv);
3323 cmdmod.cmod_flags |= CMOD_LEGACY;
3324 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3325 cmdmod.cmod_flags = save_flags;
3326 if (res == FAIL)
3327 goto on_error;
3328 ++ectx->ec_stack.ga_len;
3329 }
3330 break;
3331
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003332 // push typeval VAR_INSTR with instructions to be executed
3333 case ISN_INSTR:
3334 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003335 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003336 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003337 tv = STACK_TV_BOT(0);
3338 tv->vval.v_instr = ALLOC_ONE(instr_T);
3339 if (tv->vval.v_instr == NULL)
3340 goto on_error;
3341 ++ectx->ec_stack.ga_len;
3342
3343 tv->v_type = VAR_INSTR;
3344 tv->vval.v_instr->instr_ectx = ectx;
3345 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3346 }
3347 break;
3348
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003349 case ISN_SOURCE:
3350 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003351 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003352
Bram Moolenaar89445512022-04-14 12:58:23 +01003353 SOURCING_LNUM = iptr->isn_lnum;
3354 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003355 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003356 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003357 }
3358 break;
3359
Bram Moolenaar4c137212021-04-19 16:48:48 +02003360 // execute :substitute with an expression
3361 case ISN_SUBSTITUTE:
3362 {
3363 subs_T *subs = &iptr->isn_arg.subs;
3364 source_cookie_T cookie;
3365 struct subs_expr_S *save_instr = substitute_instr;
3366 struct subs_expr_S subs_instr;
3367 int res;
3368
3369 subs_instr.subs_ectx = ectx;
3370 subs_instr.subs_instr = subs->subs_instr;
3371 subs_instr.subs_status = OK;
3372 substitute_instr = &subs_instr;
3373
3374 SOURCING_LNUM = iptr->isn_lnum;
3375 // This is very much like ISN_EXEC
3376 CLEAR_FIELD(cookie);
3377 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3378 res = do_cmdline(subs->subs_cmd,
3379 getsourceline, &cookie,
3380 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3381 substitute_instr = save_instr;
3382
3383 if (res == FAIL || did_emsg
3384 || subs_instr.subs_status == FAIL)
3385 goto on_error;
3386 }
3387 break;
3388
3389 case ISN_FINISH:
3390 goto done;
3391
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003392 case ISN_REDIRSTART:
3393 // create a dummy entry for var_redir_str()
3394 if (alloc_redir_lval() == FAIL)
3395 goto on_error;
3396
3397 // The output is stored in growarray "redir_ga" until
3398 // redirection ends.
3399 init_redir_ga();
3400 redir_vname = 1;
3401 break;
3402
3403 case ISN_REDIREND:
3404 {
3405 char_u *res = get_clear_redir_ga();
3406
3407 // End redirection, put redirected text on the stack.
3408 clear_redir_lval();
3409 redir_vname = 0;
3410
Bram Moolenaar35578162021-08-02 19:10:38 +02003411 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003412 {
3413 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003414 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003415 }
3416 tv = STACK_TV_BOT(0);
3417 tv->v_type = VAR_STRING;
3418 tv->vval.v_string = res;
3419 ++ectx->ec_stack.ga_len;
3420 }
3421 break;
3422
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003423 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003424#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003425 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003426 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3427 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003428 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003429#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003430 break;
3431
3432 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003433#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003434 {
3435 exarg_T ea;
3436 int res;
3437
3438 CLEAR_FIELD(ea);
3439 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3440 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3441 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3442 --ectx->ec_stack.ga_len;
3443 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003444 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003445 res = cexpr_core(&ea, tv);
3446 clear_tv(tv);
3447 if (res == FAIL)
3448 goto on_error;
3449 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003450#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003451 break;
3452
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003453 // execute Ex command from pieces on the stack
3454 case ISN_EXECCONCAT:
3455 {
3456 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003457 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003458 int pass;
3459 int i;
3460 char_u *cmd = NULL;
3461 char_u *str;
3462
3463 for (pass = 1; pass <= 2; ++pass)
3464 {
3465 for (i = 0; i < count; ++i)
3466 {
3467 tv = STACK_TV_BOT(i - count);
3468 str = tv->vval.v_string;
3469 if (str != NULL && *str != NUL)
3470 {
3471 if (pass == 2)
3472 STRCPY(cmd + len, str);
3473 len += STRLEN(str);
3474 }
3475 if (pass == 2)
3476 clear_tv(tv);
3477 }
3478 if (pass == 1)
3479 {
3480 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003481 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003482 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003483 len = 0;
3484 }
3485 }
3486
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003487 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003488 do_cmdline_cmd(cmd);
3489 vim_free(cmd);
3490 }
3491 break;
3492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 // execute :echo {string} ...
3494 case ISN_ECHO:
3495 {
3496 int count = iptr->isn_arg.echo.echo_count;
3497 int atstart = TRUE;
3498 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003499 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500
3501 for (idx = 0; idx < count; ++idx)
3502 {
3503 tv = STACK_TV_BOT(idx - count);
3504 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3505 &atstart, &needclr);
3506 clear_tv(tv);
3507 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003508 if (needclr)
3509 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003510 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 }
3512 break;
3513
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003514 // :execute {string} ...
3515 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003516 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003517 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003518 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003519 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003520 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003521 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003522 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003523 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003524 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003525 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003526 garray_T ga;
3527 char_u buf[NUMBUFLEN];
3528 char_u *p;
3529 int len;
3530 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003531 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003532
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003533 if (iptr->isn_type == ISN_ECHOWINDOW)
3534 count = iptr->isn_arg.echowin.ewin_count;
3535 else
3536 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003537 ga_init2(&ga, 1, 80);
3538 for (idx = 0; idx < count; ++idx)
3539 {
3540 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003541 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003542 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003543 if (tv->v_type == VAR_CHANNEL
3544 || tv->v_type == VAR_JOB)
3545 {
3546 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003547 semsg(_(e_using_invalid_value_as_string_str),
3548 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003549 break;
3550 }
3551 else
3552 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003553 }
3554 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003555 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003556
3557 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003558 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003559 failed = TRUE;
3560 else
3561 {
3562 if (ga.ga_len > 0)
3563 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3564 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3565 ga.ga_len += len;
3566 }
3567 clear_tv(tv);
3568 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003569 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003570 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003571 {
3572 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003573 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003574 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003575
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003576 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003577 {
3578 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003579 {
3580 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003581 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003582 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003583 {
3584 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003585 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003586 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003587 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003588 else
3589 {
3590 msg_sb_eol();
3591 if (iptr->isn_type == ISN_ECHOMSG)
3592 {
3593 msg_attr(ga.ga_data, echo_attr);
3594 out_flush();
3595 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003596#ifdef HAS_MESSAGE_WINDOW
3597 else if (iptr->isn_type == ISN_ECHOWINDOW)
3598 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003599 start_echowindow(
3600 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003601 msg_attr(ga.ga_data, echo_attr);
3602 end_echowindow();
3603 }
3604#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003605 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3606 {
3607 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3608 TRUE);
3609 ui_write((char_u *)"\r\n", 2, TRUE);
3610 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003611 else
3612 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003613 SOURCING_LNUM = iptr->isn_lnum;
3614 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003615 }
3616 }
3617 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003618 ga_clear(&ga);
3619 }
3620 break;
3621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 // load local variable or argument
3623 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003624 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003625 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003626 tv = STACK_TV_VAR(iptr->isn_arg.number);
3627 if (tv->v_type == VAR_UNKNOWN)
3628 {
3629 // missing argument or default value v:none
3630 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3631 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3632 }
3633 else
3634 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003635 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 break;
3637
3638 // load v: variable
3639 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003640 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003641 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003643 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 break;
3645
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003646 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 case ISN_LOADSCRIPT:
3648 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003649 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 svar_T *sv;
3651
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003652 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003653 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003654 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003655 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003656 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003657 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003659 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 }
3661 break;
3662
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003663 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003665 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003667 int sid = iptr->isn_arg.loadstore.ls_sid;
3668 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003669 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 if (di == NULL)
3673 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003674 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003675 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003676 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 }
3678 else
3679 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003680 if (iptr->isn_type == ISN_LOADEXPORT)
3681 {
3682 int idx = get_script_item_idx(sid, name, 0,
3683 NULL, NULL);
3684 svar_T *sv;
3685
3686 if (idx >= 0)
3687 {
3688 sv = ((svar_T *)SCRIPT_ITEM(sid)
3689 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003690 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003691 {
3692 SOURCING_LNUM = iptr->isn_lnum;
3693 semsg(_(e_item_not_exported_in_script_str),
3694 name);
3695 goto on_error;
3696 }
3697 }
3698 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003699 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003700 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003702 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 }
3704 }
3705 break;
3706
Bram Moolenaard3aac292020-04-19 14:32:17 +02003707 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003709 case ISN_LOADB:
3710 case ISN_LOADW:
3711 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003713 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003714
Bram Moolenaar06b77222022-01-25 15:51:56 +00003715 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003716 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003717 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003718 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 break;
3722
Bram Moolenaar03290b82020-12-19 16:30:44 +01003723 // load autoload variable
3724 case ISN_LOADAUTO:
3725 {
3726 char_u *name = iptr->isn_arg.string;
3727
Bram Moolenaar35578162021-08-02 19:10:38 +02003728 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003729 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003730 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003731 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003732 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003733 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003734 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003735 }
3736 break;
3737
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003738 // load g:/b:/w:/t: namespace
3739 case ISN_LOADGDICT:
3740 case ISN_LOADBDICT:
3741 case ISN_LOADWDICT:
3742 case ISN_LOADTDICT:
3743 {
3744 dict_T *d = NULL;
3745
3746 switch (iptr->isn_type)
3747 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003748 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3749 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3750 case ISN_LOADWDICT: d = curwin->w_vars; break;
3751 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003752 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003753 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003754 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003755 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003756 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003757 tv = STACK_TV_BOT(0);
3758 tv->v_type = VAR_DICT;
3759 tv->v_lock = 0;
3760 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003761 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003762 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003763 }
3764 break;
3765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 // load &option
3767 case ISN_LOADOPT:
3768 {
3769 typval_T optval;
3770 char_u *name = iptr->isn_arg.string;
3771
Bram Moolenaara8c17702020-04-01 21:17:24 +02003772 // This is not expected to fail, name is checked during
3773 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003774 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003775 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003776 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003777 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003779 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 }
3781 break;
3782
3783 // load $ENV
3784 case ISN_LOADENV:
3785 {
3786 typval_T optval;
3787 char_u *name = iptr->isn_arg.string;
3788
Bram Moolenaar35578162021-08-02 19:10:38 +02003789 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003790 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003791 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003792 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003794 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 }
3796 break;
3797
3798 // load @register
3799 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003800 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003801 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 tv = STACK_TV_BOT(0);
3803 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003804 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003805 // This may result in NULL, which should be equivalent to an
3806 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 tv->vval.v_string = get_reg_contents(
3808 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003809 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 break;
3811
3812 // store local variable
3813 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003814 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 tv = STACK_TV_VAR(iptr->isn_arg.number);
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003816 if (STACK_TV_BOT(0)->v_type == VAR_TYPEALIAS)
3817 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02003818 semsg(_(e_using_typealias_as_value),
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003819 STACK_TV_BOT(0)->vval.v_typealias->ta_name);
3820 clear_tv(STACK_TV_BOT(0));
3821 goto on_error;
3822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 clear_tv(tv);
3824 *tv = *STACK_TV_BOT(0);
3825 break;
3826
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003827 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003828 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003829 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003830 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003831 int sid = iptr->isn_arg.loadstore.ls_sid;
3832 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003833 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003834 dictitem_T *di = find_var_in_ht(ht, 0,
3835 iptr->isn_type == ISN_STORES
3836 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003837
Bram Moolenaar4c137212021-04-19 16:48:48 +02003838 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003839 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003840 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003841 {
3842 if (iptr->isn_type == ISN_STOREEXPORT)
3843 {
3844 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003845 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003846 goto on_error;
3847 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003848 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003849 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003850 else
3851 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003852 if (iptr->isn_type == ISN_STOREEXPORT)
3853 {
3854 int idx = get_script_item_idx(sid, name, 0,
3855 NULL, NULL);
3856
Bram Moolenaar06651632022-04-27 17:54:25 +01003857 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003858 if (idx >= 0)
3859 {
3860 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3861 ->sn_var_vals.ga_data) + idx;
3862
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003863 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003864 {
3865 semsg(_(e_item_not_exported_in_script_str),
3866 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003867 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003868 goto on_error;
3869 }
3870 }
3871 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003872 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003873 {
3874 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003875 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003876 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003877 clear_tv(&di->di_tv);
3878 di->di_tv = *STACK_TV_BOT(0);
3879 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003880 }
3881 break;
3882
3883 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 case ISN_STORESCRIPT:
3885 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003886 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3887 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003889 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003890 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003891 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003892 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003893
3894 // "const" and "final" are checked at compile time, locking
3895 // the value needs to be checked here.
3896 SOURCING_LNUM = iptr->isn_lnum;
3897 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003898 {
3899 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003900 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003901 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 clear_tv(sv->sv_tv);
3904 *sv->sv_tv = *STACK_TV_BOT(0);
3905 }
3906 break;
3907
3908 // store option
3909 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003910 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003912 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3913 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 long n = 0;
3915 char_u *s = NULL;
3916 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003917 char_u numbuf[NUMBUFLEN];
3918 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919
Bram Moolenaar4c137212021-04-19 16:48:48 +02003920 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003921 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 tv = STACK_TV_BOT(0);
3923 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003924 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003926 if (s == NULL)
3927 s = (char_u *)"";
3928 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003929 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3930 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003931 // If the option can be set to a function reference or
3932 // a lambda and the passed value is a function
3933 // reference, then convert it to the name (string) of
3934 // the function reference.
3935 s = tv2string(tv, &tofree, numbuf, 0);
3936 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003937 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003938 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003939 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003940 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003941 goto on_error;
3942 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003945 // must be VAR_NUMBER, CHECKTYPE makes sure
3946 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003947 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003948 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003949 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 if (msg != NULL)
3951 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003952 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003954 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 }
3957 break;
3958
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003959 // store $ENV
3960 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003961 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003962 tv = STACK_TV_BOT(0);
3963 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3964 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003965 break;
3966
3967 // store @r
3968 case ISN_STOREREG:
3969 {
3970 int reg = iptr->isn_arg.number;
3971
Bram Moolenaar4c137212021-04-19 16:48:48 +02003972 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003973 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003974 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003975 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003976 }
3977 break;
3978
3979 // store v: variable
3980 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003981 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003982 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3983 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003984 // should not happen, type is checked when compiling
3985 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003986 break;
3987
Bram Moolenaard3aac292020-04-19 14:32:17 +02003988 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003990 case ISN_STOREB:
3991 case ISN_STOREW:
3992 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003994 dictitem_T *di;
3995 hashtab_T *ht;
3996 char_u *name = iptr->isn_arg.string + 2;
3997
Bram Moolenaard3aac292020-04-19 14:32:17 +02003998 switch (iptr->isn_type)
3999 {
4000 case ISN_STOREG:
4001 ht = get_globvar_ht();
4002 break;
4003 case ISN_STOREB:
4004 ht = &curbuf->b_vars->dv_hashtab;
4005 break;
4006 case ISN_STOREW:
4007 ht = &curwin->w_vars->dv_hashtab;
4008 break;
4009 case ISN_STORET:
4010 ht = &curtab->tp_vars->dv_hashtab;
4011 break;
4012 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004013 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004014 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015
Bram Moolenaar4c137212021-04-19 16:48:48 +02004016 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004017 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004019 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 else
4021 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004022 SOURCING_LNUM = iptr->isn_lnum;
4023 if (var_check_permission(di, name) == FAIL)
4024 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 clear_tv(&di->di_tv);
4026 di->di_tv = *STACK_TV_BOT(0);
4027 }
4028 }
4029 break;
4030
Bram Moolenaar03290b82020-12-19 16:30:44 +01004031 // store an autoload variable
4032 case ISN_STOREAUTO:
4033 SOURCING_LNUM = iptr->isn_lnum;
4034 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
4035 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004036 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004037 break;
4038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 // store number in local variable
4040 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004041 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 clear_tv(tv);
4043 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004044 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 break;
4046
Bram Moolenaar590162c2022-12-24 21:24:06 +00004047 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004048 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004049 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004050 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004051
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004052 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004053 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004054 if (res == NOTDONE)
4055 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004056 }
4057 break;
4058
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004059 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004060 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004061 if (execute_storerange(iptr, ectx) == FAIL)
4062 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004063 break;
4064
Bram Moolenaard505d172022-12-18 21:42:55 +00004065 case ISN_LOAD_CLASSMEMBER:
4066 {
4067 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4068 goto theend;
4069 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004070 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4071 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004072 ++ectx->ec_stack.ga_len;
4073 }
4074 break;
4075
4076 case ISN_STORE_CLASSMEMBER:
4077 {
4078 classmember_T *cm = &iptr->isn_arg.classmember;
4079 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4080 clear_tv(tv);
4081 *tv = *STACK_TV_BOT(-1);
4082 --ectx->ec_stack.ga_len;
4083 }
4084 break;
4085
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004086 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004087 case ISN_LOADOUTER:
4088 case ISN_STOREOUTER:
4089 {
4090 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004091 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4092 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004093
4094 while (depth > 1 && outer != NULL)
4095 {
4096 outer = outer->out_up;
4097 --depth;
4098 }
4099 if (outer == NULL)
4100 {
4101 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004102 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4103 || ectx->ec_outer_ref == NULL)
4104 // Possibly :def function called from legacy
4105 // context.
4106 emsg(_(e_closure_called_from_invalid_context));
4107 else
4108 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004109 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004110 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004111 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004112 // Variable declared in loop. May be copied if the
4113 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004114 tv = ((typval_T *)outer->out_loop[-depth - 1]
4115 .stack->ga_data)
4116 + outer->out_loop[-depth - 1].var_idx
4117 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004118 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004119 // Variable declared in a function. May be copied if
4120 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004121 tv = ((typval_T *)outer->out_stack->ga_data)
4122 + outer->out_frame_idx + STACK_FRAME_SIZE
4123 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004124 if (iptr->isn_type == ISN_LOADOUTER)
4125 {
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004126 typval_T *copy;
Bram Moolenaar35578162021-08-02 19:10:38 +02004127 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004128 goto theend;
Christian Brabandt5dd41d42023-12-04 22:52:23 +01004129 // careful: ga_grow_inner may re-alloc the stack
4130 if (depth < 0)
4131 copy = ((typval_T *)outer->out_loop[-depth - 1]
4132 .stack->ga_data)
4133 + outer->out_loop[-depth - 1].var_idx
4134 + iptr->isn_arg.outer.outer_idx;
4135 else
4136 copy = ((typval_T *)outer->out_stack->ga_data)
4137 + outer->out_frame_idx + STACK_FRAME_SIZE
4138 + iptr->isn_arg.outer.outer_idx;
4139 // memory was freed, get tv again
4140 if (copy != tv)
4141 tv = copy;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004142 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004143 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004144 }
4145 else
4146 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004147 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004148 clear_tv(tv);
4149 *tv = *STACK_TV_BOT(0);
4150 }
4151 }
4152 break;
4153
Bram Moolenaar752fc692021-01-04 21:57:11 +01004154 // unlet item in list or dict variable
4155 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004156 if (execute_unletindex(iptr, ectx) == FAIL)
4157 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004158 break;
4159
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004160 // unlet range of items in list variable
4161 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004162 if (execute_unletrange(iptr, ectx) == FAIL)
4163 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004164 break;
4165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 // push constant
4167 case ISN_PUSHNR:
4168 case ISN_PUSHBOOL:
4169 case ISN_PUSHSPEC:
4170 case ISN_PUSHF:
4171 case ISN_PUSHS:
4172 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004173 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004174 case ISN_PUSHCHANNEL:
4175 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004176 case ISN_PUSHOBJ:
4177 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004178 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004179 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004181 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004182 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 switch (iptr->isn_type)
4184 {
4185 case ISN_PUSHNR:
4186 tv->v_type = VAR_NUMBER;
4187 tv->vval.v_number = iptr->isn_arg.number;
4188 break;
4189 case ISN_PUSHBOOL:
4190 tv->v_type = VAR_BOOL;
4191 tv->vval.v_number = iptr->isn_arg.number;
4192 break;
4193 case ISN_PUSHSPEC:
4194 tv->v_type = VAR_SPECIAL;
4195 tv->vval.v_number = iptr->isn_arg.number;
4196 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 case ISN_PUSHF:
4198 tv->v_type = VAR_FLOAT;
4199 tv->vval.v_float = iptr->isn_arg.fnumber;
4200 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 case ISN_PUSHBLOB:
4202 blob_copy(iptr->isn_arg.blob, tv);
4203 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004204 case ISN_PUSHFUNC:
4205 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004206 if (iptr->isn_arg.string == NULL)
4207 tv->vval.v_string = NULL;
4208 else
4209 tv->vval.v_string =
4210 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004211 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004212 case ISN_PUSHCHANNEL:
4213#ifdef FEAT_JOB_CHANNEL
4214 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004215 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004216#endif
4217 break;
4218 case ISN_PUSHJOB:
4219#ifdef FEAT_JOB_CHANNEL
4220 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004221 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004222#endif
4223 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004224 case ISN_PUSHOBJ:
4225 tv->v_type = VAR_OBJECT;
4226 tv->vval.v_object = NULL;
4227 break;
4228 case ISN_PUSHCLASS:
4229 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004230 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004231 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 default:
4233 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004234 tv->vval.v_string = iptr->isn_arg.string == NULL
4235 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 }
4237 break;
4238
Bram Moolenaar06b77222022-01-25 15:51:56 +00004239 case ISN_AUTOLOAD:
4240 {
4241 char_u *name = iptr->isn_arg.string;
4242
4243 (void)script_autoload(name, FALSE);
4244 if (find_func(name, TRUE))
4245 {
4246 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4247 goto theend;
4248 tv = STACK_TV_BOT(0);
4249 tv->v_lock = 0;
4250 ++ectx->ec_stack.ga_len;
4251 tv->v_type = VAR_FUNC;
4252 tv->vval.v_string = vim_strsave(name);
4253 }
4254 else
4255 {
4256 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4257
4258 if (res == NOTDONE)
4259 goto theend;
4260 if (res == FAIL)
4261 goto on_error;
4262 }
4263 }
4264 break;
4265
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004266 case ISN_UNLET:
4267 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4268 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004269 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004270 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004271 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004272 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004273 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004274
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004275 case ISN_LOCKUNLOCK:
4276 {
Ernie Raelee865f32023-09-29 19:53:55 +02004277#ifdef LOG_LOCKVAR
4278 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4279 iptr->isn_arg.string);
4280#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004281 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004282
4283 // Stack has the local variable, argument the whole :lock
4284 // or :unlock command, like ISN_EXEC.
4285 --ectx->ec_stack.ga_len;
Ernie Rael4c8da022023-10-11 21:35:11 +02004286 lval_root_T root = { .lr_tv = STACK_TV_BOT(0),
4287 .lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec,
4288 .lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg };
Ernie Rael64885642023-10-04 20:16:22 +02004289 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004290 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004291 iptr->isn_arg.lockunlock.lu_string);
4292 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004293 lval_root = lval_root_save;
4294 if (res == FAIL)
4295 goto on_error;
4296 }
4297 break;
4298
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004299 case ISN_LOCKCONST:
4300 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4301 break;
4302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 // create a list from items on the stack; uses a single allocation
4304 // for the list header and the items
4305 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004306 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004307 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 break;
4309
4310 // create a dict from items on the stack
4311 case ISN_NEWDICT:
4312 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004313 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004315 SOURCING_LNUM = iptr->isn_lnum;
4316 res = exe_newdict(iptr->isn_arg.number, ectx);
4317 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004318 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004319 if (res == MAYBE)
4320 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 }
4322 break;
4323
LemonBoy372bcce2022-04-25 12:43:20 +01004324 case ISN_CONCAT:
4325 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4326 goto theend;
4327 break;
4328
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004329 // create a partial with NULL value
4330 case ISN_NEWPARTIAL:
4331 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4332 goto theend;
4333 ++ectx->ec_stack.ga_len;
4334 tv = STACK_TV_BOT(-1);
4335 tv->v_type = VAR_PARTIAL;
4336 tv->v_lock = 0;
4337 tv->vval.v_partial = NULL;
4338 break;
4339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 // call a :def function
4341 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004342 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004343 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4344 NULL,
4345 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004346 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004347 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 break;
4349
Bram Moolenaard0200c82023-01-28 15:19:40 +00004350 // call a method on an interface
4351 case ISN_METHODCALL:
4352 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004353 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4354
Bram Moolenaard0200c82023-01-28 15:19:40 +00004355 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004356 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004357 if (tv->v_type != VAR_OBJECT)
4358 {
4359 object_required_error(tv);
4360 goto on_error;
4361 }
4362 object_T *obj = tv->vval.v_object;
4363 class_T *cl = obj->obj_class;
4364
4365 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004366 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004367 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004368
4369 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4370 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4371 goto on_error;
4372 }
4373 break;
4374
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 // call a builtin function
4376 case ISN_BCALL:
4377 SOURCING_LNUM = iptr->isn_lnum;
4378 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4379 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004380 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004381 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 break;
4383
4384 // call a funcref or partial
4385 case ISN_PCALL:
4386 {
4387 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4388 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004389 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390
4391 SOURCING_LNUM = iptr->isn_lnum;
4392 if (pfunc->cpf_top)
4393 {
4394 // funcref is above the arguments
4395 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4396 }
4397 else
4398 {
4399 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004400 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004401 partial_tv = *STACK_TV_BOT(0);
4402 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004404 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004405 if (tv == &partial_tv)
4406 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004408 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 }
4410 break;
4411
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004412 case ISN_PCALL_END:
4413 // PCALL finished, arguments have been consumed and replaced by
4414 // the return value. Now clear the funcref from the stack,
4415 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004416 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004417 clear_tv(STACK_TV_BOT(-1));
4418 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4419 break;
4420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 // call a user defined function or funcref/partial
4422 case ISN_UCALL:
4423 {
4424 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4425
4426 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004427 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004428 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004429 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 }
4431 break;
4432
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004433 // :defer func(arg)
4434 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004435 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004436 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4437 goto on_error;
4438 break;
4439
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004440 // Return from a :def function call without a value.
4441 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004442 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004443 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004444 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004445 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004446 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004447 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004448 if (iptr->isn_type == ISN_RETURN_VOID)
4449 {
4450 tv->v_type = VAR_VOID;
4451 tv->vval.v_number = 0;
4452 tv->v_lock = 0;
4453 }
4454 else
4455 {
4456 *tv = *STACK_TV_VAR(0);
4457 ++tv->vval.v_object->obj_refcount;
4458 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004459 // FALLTHROUGH
4460
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004461 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 case ISN_RETURN:
4463 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004464 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004465 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004466
4467 if (trystack->ga_len > 0)
4468 trycmd = ((trycmd_T *)trystack->ga_data)
4469 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004470 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004471 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004473 // jump to ":finally" or ":endtry"
4474 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004475 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004476 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004477 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 trycmd->tcd_return = TRUE;
4479 }
4480 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004481 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 }
4483 break;
4484
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004485 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 case ISN_FUNCREF:
4487 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004488 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4489 ufunc_T *ufunc;
4490 funcref_T *funcref = &iptr->isn_arg.funcref;
4491 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004494 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004495 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004496 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004497 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004498 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004499 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004500 if (extra != NULL && extra->fre_class != NULL)
4501 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004502 class_T *cl;
4503 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004504 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004505 tv = STACK_TV_BOT(-1);
4506 if (tv->v_type != VAR_OBJECT)
4507 {
4508 object_required_error(tv);
4509 vim_free(pt);
4510 goto on_error;
4511 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004512
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004513 object_T *obj = tv->vval.v_object;
4514 cl = obj->obj_class;
4515 // drop the value from the stack
4516 clear_tv(tv);
4517 --ectx->ec_stack.ga_len;
4518
4519 pt->pt_obj = obj;
4520 ++obj->obj_refcount;
4521 }
4522 else
4523 cl = extra->fre_class;
4524
4525 if (extra->fre_object_method)
4526 {
4527 // object method
4528 // convert the interface index to the object index
4529 int idx =
4530 object_index_from_itf_index(extra->fre_class,
4531 TRUE, extra->fre_method_idx, cl);
4532 ufunc = cl->class_obj_methods[idx];
4533 }
4534 else
4535 {
4536 // class method
4537 ufunc =
4538 cl->class_class_functions[extra->fre_method_idx];
4539 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004540 }
4541 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004542 {
4543 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4544 + funcref->fr_dfunc_idx;
4545
4546 ufunc = pt_dfunc->df_ufunc;
4547 }
4548 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004549 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004550 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004551 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004552 if (ufunc == NULL)
4553 {
4554 SOURCING_LNUM = iptr->isn_lnum;
4555 iemsg("ufunc unexpectedly NULL for FUNCREF");
4556 goto theend;
4557 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004558 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004559 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004560 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004561 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004563 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 tv->vval.v_partial = pt;
4565 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004566 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 }
4568 break;
4569
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004570 // Create a global function from a lambda.
4571 case ISN_NEWFUNC:
4572 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004573 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004574
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004575 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004576 arg->nfa_global, &arg->nfa_loopvar_info,
4577 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004578 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004579 }
4580 break;
4581
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004582 // List functions
4583 case ISN_DEF:
4584 if (iptr->isn_arg.string == NULL)
4585 list_functions(NULL);
4586 else
4587 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004588 exarg_T ea;
4589 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004590
4591 CLEAR_FIELD(ea);
4592 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004593 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004594 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004595 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004596 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004597 }
4598 break;
4599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 // jump if a condition is met
4601 case ISN_JUMP:
4602 {
4603 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004604 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 int jump = TRUE;
4606
4607 if (when != JUMP_ALWAYS)
4608 {
4609 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004610 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004611 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004612 || when == JUMP_IF_COND_TRUE)
4613 {
4614 SOURCING_LNUM = iptr->isn_lnum;
4615 jump = tv_get_bool_chk(tv, &error);
4616 if (error)
4617 goto on_error;
4618 }
4619 else
4620 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004621 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004623 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 {
4625 // drop the value from the stack
4626 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004627 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 }
4629 }
4630 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004631 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 }
4633 break;
4634
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004635 // "while": jump to end if a condition is false
4636 case ISN_WHILE:
4637 {
4638 int error = FALSE;
4639 int jump = TRUE;
4640
4641 tv = STACK_TV_BOT(-1);
4642 SOURCING_LNUM = iptr->isn_lnum;
4643 jump = !tv_get_bool_chk(tv, &error);
4644 if (error)
4645 goto on_error;
4646 // drop the value from the stack
4647 clear_tv(tv);
4648 --ectx->ec_stack.ga_len;
4649 if (jump)
4650 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4651
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004652 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004653 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004654 tv = STACK_TV_VAR(
4655 iptr->isn_arg.whileloop.while_funcref_idx);
4656 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4657 }
4658 break;
4659
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004660 // Jump if an argument with a default value was already set and not
4661 // v:none.
4662 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004663 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004664 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004665 int arg_set = tv->v_type != VAR_UNKNOWN
4666 && !(tv->v_type == VAR_SPECIAL
4667 && tv->vval.v_number == VVAL_NONE);
4668 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004669 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004670 break;
4671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 // top of a for loop
4673 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004674 if (execute_for(iptr, ectx) == FAIL)
4675 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 break;
4677
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004678 // end of a for or while loop
4679 case ISN_ENDLOOP:
4680 if (execute_endloop(iptr, ectx) == FAIL)
4681 goto theend;
4682 break;
4683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 // start of ":try" block
4685 case ISN_TRY:
4686 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004687 trycmd_T *trycmd = NULL;
4688
Bram Moolenaar35578162021-08-02 19:10:38 +02004689 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004690 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004691 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4692 + ectx->ec_trystack.ga_len;
4693 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004695 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004696 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4697 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004698 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004699 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004700 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004701 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004702 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004703 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 }
4705 break;
4706
4707 case ISN_PUSHEXC:
4708 if (current_exception == NULL)
4709 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004710 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004712 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004714 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004715 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004717 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004719 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 tv->vval.v_string = vim_strsave(
4721 (char_u *)current_exception->value);
4722 break;
4723
4724 case ISN_CATCH:
4725 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004726 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004727 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728
Bram Moolenaar4c137212021-04-19 16:48:48 +02004729 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004730 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004732 trycmd->tcd_caught = TRUE;
4733 trycmd->tcd_did_throw = FALSE;
4734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004736 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 catch_exception(current_exception);
4738 }
4739 break;
4740
Bram Moolenaarc150c092021-02-13 15:02:46 +01004741 case ISN_TRYCONT:
4742 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004743 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004744 trycont_T *trycont = &iptr->isn_arg.trycont;
4745 int i;
4746 trycmd_T *trycmd;
4747 int iidx = trycont->tct_where;
4748
4749 if (trystack->ga_len < trycont->tct_levels)
4750 {
4751 siemsg("TRYCONT: expected %d levels, found %d",
4752 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004753 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004754 }
4755 // Make :endtry jump to any outer try block and the last
4756 // :endtry inside the loop to the loop start.
4757 for (i = trycont->tct_levels; i > 0; --i)
4758 {
4759 trycmd = ((trycmd_T *)trystack->ga_data)
4760 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004761 // Add one to tcd_cont to be able to jump to
4762 // instruction with index zero.
4763 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004764 iidx = trycmd->tcd_finally_idx == 0
4765 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004766 }
4767 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004768 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004769 }
4770 break;
4771
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004772 case ISN_FINALLY:
4773 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004774 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004775 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4776 + trystack->ga_len - 1;
4777
4778 // Reset the index to avoid a return statement jumps here
4779 // again.
4780 trycmd->tcd_finally_idx = 0;
4781 break;
4782 }
4783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 // end of ":try" block
4785 case ISN_ENDTRY:
4786 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004787 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004788 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789
Bram Moolenaar06651632022-04-27 17:54:25 +01004790 --trystack->ga_len;
4791 --trylevel;
4792 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4793 if (trycmd->tcd_did_throw)
4794 did_throw = TRUE;
4795 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004797 // discard the exception
4798 if (caught_stack == current_exception)
4799 caught_stack = caught_stack->caught;
4800 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004802
4803 if (trycmd->tcd_return)
4804 goto func_return;
4805
4806 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4807 {
4808 --ectx->ec_stack.ga_len;
4809 clear_tv(STACK_TV_BOT(0));
4810 }
4811 if (trycmd->tcd_cont != 0)
4812 // handling :continue: jump to outer try block or
4813 // start of the loop
4814 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 }
4816 break;
4817
4818 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004819 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004820 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004821
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004822 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4823 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004824 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004825 // the function to abort but not display an error.
4826 tv = STACK_TV_BOT(-1);
4827 clear_tv(tv);
4828 tv->v_type = VAR_NUMBER;
4829 tv->vval.v_number = 0;
4830 goto done;
4831 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004832 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004833 tv = STACK_TV_BOT(0);
4834 if (tv->vval.v_string == NULL
4835 || *skipwhite(tv->vval.v_string) == NUL)
4836 {
4837 vim_free(tv->vval.v_string);
4838 SOURCING_LNUM = iptr->isn_lnum;
4839 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004840 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004841 }
4842
4843 // Inside a "catch" we need to first discard the caught
4844 // exception.
4845 if (trystack->ga_len > 0)
4846 {
4847 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4848 + trystack->ga_len - 1;
4849 if (trycmd->tcd_caught && current_exception != NULL)
4850 {
4851 // discard the exception
4852 if (caught_stack == current_exception)
4853 caught_stack = caught_stack->caught;
4854 discard_current_exception();
4855 trycmd->tcd_caught = FALSE;
4856 }
4857 }
4858
Bram Moolenaar90a57162022-02-12 14:23:17 +00004859 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004860 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4861 == FAIL)
4862 {
4863 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004864 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004865 }
4866 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 break;
4869
4870 // compare with special values
4871 case ISN_COMPAREBOOL:
4872 case ISN_COMPARESPECIAL:
4873 {
4874 typval_T *tv1 = STACK_TV_BOT(-2);
4875 typval_T *tv2 = STACK_TV_BOT(-1);
4876 varnumber_T arg1 = tv1->vval.v_number;
4877 varnumber_T arg2 = tv2->vval.v_number;
4878 int res;
4879
Bram Moolenaar06651632022-04-27 17:54:25 +01004880 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4881 res = arg1 == arg2;
4882 else
4883 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884
Bram Moolenaar4c137212021-04-19 16:48:48 +02004885 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 tv1->v_type = VAR_BOOL;
4887 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4888 }
4889 break;
4890
Bram Moolenaar7a222242022-03-01 19:23:24 +00004891 case ISN_COMPARENULL:
4892 {
4893 typval_T *tv1 = STACK_TV_BOT(-2);
4894 typval_T *tv2 = STACK_TV_BOT(-1);
4895 int res;
4896
4897 res = typval_compare_null(tv1, tv2);
4898 if (res == MAYBE)
4899 goto on_error;
4900 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4901 res = !res;
4902 clear_tv(tv1);
4903 clear_tv(tv2);
4904 --ectx->ec_stack.ga_len;
4905 tv1->v_type = VAR_BOOL;
4906 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4907 }
4908 break;
4909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 // Operation with two number arguments
4911 case ISN_OPNR:
4912 case ISN_COMPARENR:
4913 {
4914 typval_T *tv1 = STACK_TV_BOT(-2);
4915 typval_T *tv2 = STACK_TV_BOT(-1);
4916 varnumber_T arg1 = tv1->vval.v_number;
4917 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004918 varnumber_T res = 0;
4919 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004921 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4922 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4923 {
4924 if (arg2 < 0)
4925 {
4926 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004927 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004928 goto on_error;
4929 }
4930 }
4931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 switch (iptr->isn_arg.op.op_type)
4933 {
4934 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004935 case EXPR_DIV: if (arg2 == 0)
4936 div_zero = TRUE;
4937 else
4938 res = arg1 / arg2;
4939 break;
4940 case EXPR_REM: if (arg2 == 0)
4941 div_zero = TRUE;
4942 else
4943 res = arg1 % arg2;
4944 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 case EXPR_SUB: res = arg1 - arg2; break;
4946 case EXPR_ADD: res = arg1 + arg2; break;
4947
4948 case EXPR_EQUAL: res = arg1 == arg2; break;
4949 case EXPR_NEQUAL: res = arg1 != arg2; break;
4950 case EXPR_GREATER: res = arg1 > arg2; break;
4951 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4952 case EXPR_SMALLER: res = arg1 < arg2; break;
4953 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004954 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4955 res = 0;
4956 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004957 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004958 break;
4959 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4960 res = 0;
4961 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004962 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004963 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004964 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 }
4966
Bram Moolenaar4c137212021-04-19 16:48:48 +02004967 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 if (iptr->isn_type == ISN_COMPARENR)
4969 {
4970 tv1->v_type = VAR_BOOL;
4971 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4972 }
4973 else
4974 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004975 if (div_zero)
4976 {
4977 SOURCING_LNUM = iptr->isn_lnum;
4978 emsg(_(e_divide_by_zero));
4979 goto on_error;
4980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 }
4982 break;
4983
4984 // Computation with two float arguments
4985 case ISN_OPFLOAT:
4986 case ISN_COMPAREFLOAT:
4987 {
4988 typval_T *tv1 = STACK_TV_BOT(-2);
4989 typval_T *tv2 = STACK_TV_BOT(-1);
4990 float_T arg1 = tv1->vval.v_float;
4991 float_T arg2 = tv2->vval.v_float;
4992 float_T res = 0;
4993 int cmp = FALSE;
4994
4995 switch (iptr->isn_arg.op.op_type)
4996 {
4997 case EXPR_MULT: res = arg1 * arg2; break;
4998 case EXPR_DIV: res = arg1 / arg2; break;
4999 case EXPR_SUB: res = arg1 - arg2; break;
5000 case EXPR_ADD: res = arg1 + arg2; break;
5001
5002 case EXPR_EQUAL: cmp = arg1 == arg2; break;
5003 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
5004 case EXPR_GREATER: cmp = arg1 > arg2; break;
5005 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
5006 case EXPR_SMALLER: cmp = arg1 < arg2; break;
5007 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
5008 default: cmp = 0; break;
5009 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005010 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 if (iptr->isn_type == ISN_COMPAREFLOAT)
5012 {
5013 tv1->v_type = VAR_BOOL;
5014 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
5015 }
5016 else
5017 tv1->vval.v_float = res;
5018 }
5019 break;
5020
5021 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00005022 case ISN_COMPAREDICT:
5023 case ISN_COMPAREFUNC:
5024 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005026 case ISN_COMPARECLASS:
5027 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 {
5029 typval_T *tv1 = STACK_TV_BOT(-2);
5030 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00005031 exprtype_T exprtype = iptr->isn_arg.op.op_type;
5032 int ic = iptr->isn_arg.op.op_ic;
5033 int res = FALSE;
5034 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035
Bram Moolenaar265f8112021-12-19 12:33:05 +00005036 SOURCING_LNUM = iptr->isn_lnum;
5037 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005039 status = typval_compare_list(tv1, tv2,
5040 exprtype, ic, &res);
5041 }
5042 else if (iptr->isn_type == ISN_COMPAREDICT)
5043 {
5044 status = typval_compare_dict(tv1, tv2,
5045 exprtype, ic, &res);
5046 }
5047 else if (iptr->isn_type == ISN_COMPAREFUNC)
5048 {
5049 status = typval_compare_func(tv1, tv2,
5050 exprtype, ic, &res);
5051 }
5052 else if (iptr->isn_type == ISN_COMPARESTRING)
5053 {
5054 status = typval_compare_string(tv1, tv2,
5055 exprtype, ic, &res);
5056 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005057 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005058 {
5059 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005061 else if (iptr->isn_type == ISN_COMPARECLASS)
5062 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005063 status = typval_compare_class(tv1, tv2,
5064 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005065 }
5066 else // ISN_COMPAREOBJECT
5067 {
5068 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005069 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005070 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005071 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 clear_tv(tv1);
5073 clear_tv(tv2);
5074 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005075 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5076 if (status == FAIL)
5077 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 }
5079 break;
5080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 case ISN_COMPAREANY:
5082 {
5083 typval_T *tv1 = STACK_TV_BOT(-2);
5084 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005085 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005087 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088
Bram Moolenaareb26f432020-09-14 16:50:05 +02005089 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005090 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005092 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005093 if (status == FAIL)
5094 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095 }
5096 break;
5097
5098 case ISN_ADDLIST:
5099 case ISN_ADDBLOB:
5100 {
5101 typval_T *tv1 = STACK_TV_BOT(-2);
5102 typval_T *tv2 = STACK_TV_BOT(-1);
5103
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005104 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005106 {
5107 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5108 && tv1->vval.v_list != NULL)
5109 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5110 NULL);
5111 else
5112 eval_addlist(tv1, tv2);
5113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 else
5115 eval_addblob(tv1, tv2);
5116 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005117 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118 }
5119 break;
5120
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005121 case ISN_LISTAPPEND:
5122 {
5123 typval_T *tv1 = STACK_TV_BOT(-2);
5124 typval_T *tv2 = STACK_TV_BOT(-1);
5125 list_T *l = tv1->vval.v_list;
5126
5127 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005128 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005129 if (l == NULL)
5130 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005131 emsg(_(e_cannot_add_to_null_list));
5132 goto on_error;
5133 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005134 if (value_check_lock(l->lv_lock, NULL, FALSE))
5135 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005136 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005137 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005138 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005139 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005140 }
5141 break;
5142
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005143 case ISN_BLOBAPPEND:
5144 {
5145 typval_T *tv1 = STACK_TV_BOT(-2);
5146 typval_T *tv2 = STACK_TV_BOT(-1);
5147 blob_T *b = tv1->vval.v_blob;
5148 int error = FALSE;
5149 varnumber_T n;
5150
5151 // add a number to a blob
5152 if (b == NULL)
5153 {
5154 SOURCING_LNUM = iptr->isn_lnum;
5155 emsg(_(e_cannot_add_to_null_blob));
5156 goto on_error;
5157 }
5158 n = tv_get_number_chk(tv2, &error);
5159 if (error)
5160 goto on_error;
5161 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005162 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005163 }
5164 break;
5165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005166 // Computation with two arguments of unknown type
5167 case ISN_OPANY:
5168 {
5169 typval_T *tv1 = STACK_TV_BOT(-2);
5170 typval_T *tv2 = STACK_TV_BOT(-1);
5171 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 int error = FALSE;
5174
5175 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5176 {
5177 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5178 {
5179 eval_addlist(tv1, tv2);
5180 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005181 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 break;
5183 }
5184 else if (tv1->v_type == VAR_BLOB
5185 && tv2->v_type == VAR_BLOB)
5186 {
5187 eval_addblob(tv1, tv2);
5188 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005189 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 break;
5191 }
5192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 if (tv1->v_type == VAR_FLOAT)
5194 {
5195 f1 = tv1->vval.v_float;
5196 n1 = 0;
5197 }
5198 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005200 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201 n1 = tv_get_number_chk(tv1, &error);
5202 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005203 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 if (tv2->v_type == VAR_FLOAT)
5205 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 if (tv2->v_type == VAR_FLOAT)
5208 {
5209 f2 = tv2->vval.v_float;
5210 n2 = 0;
5211 }
5212 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213 {
5214 n2 = tv_get_number_chk(tv2, &error);
5215 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005216 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217 if (tv1->v_type == VAR_FLOAT)
5218 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 // if there is a float on either side the result is a float
5221 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5222 {
5223 switch (iptr->isn_arg.op.op_type)
5224 {
5225 case EXPR_MULT: f1 = f1 * f2; break;
5226 case EXPR_DIV: f1 = f1 / f2; break;
5227 case EXPR_SUB: f1 = f1 - f2; break;
5228 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005229 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005230 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005231 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005232 }
5233 clear_tv(tv1);
5234 clear_tv(tv2);
5235 tv1->v_type = VAR_FLOAT;
5236 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005237 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005238 }
5239 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005241 int failed = FALSE;
5242
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243 switch (iptr->isn_arg.op.op_type)
5244 {
5245 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005246 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5247 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005248 goto on_error;
5249 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005250 case EXPR_SUB: n1 = n1 - n2; break;
5251 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005252 default: n1 = num_modulus(n1, n2, &failed);
5253 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005254 goto on_error;
5255 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 }
5257 clear_tv(tv1);
5258 clear_tv(tv2);
5259 tv1->v_type = VAR_NUMBER;
5260 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005261 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005262 }
5263 }
5264 break;
5265
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005266 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005267 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005268 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005269 int is_slice = iptr->isn_type == ISN_STRSLICE;
5270 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005271 char_u *res;
5272
5273 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005274 // string slice: string is at stack-3, first index at
5275 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005276 if (is_slice)
5277 {
5278 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005279 n1 = tv->vval.v_number;
5280 }
5281
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005282 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005283 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005284
Bram Moolenaar4c137212021-04-19 16:48:48 +02005285 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005286 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005287 if (is_slice)
5288 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005289 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005290 else
5291 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005292 // single character (including composing characters).
5293 // If the index is too big or negative the result is
5294 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005295 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005296 vim_free(tv->vval.v_string);
5297 tv->vval.v_string = res;
5298 }
5299 break;
5300
5301 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005302 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005303 case ISN_BLOBINDEX:
5304 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005305 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005306 int is_slice = iptr->isn_type == ISN_LISTSLICE
5307 || iptr->isn_type == ISN_BLOBSLICE;
5308 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5309 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005310 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005311 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312
5313 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005314 // list slice: list is at stack-3, indexes at stack-2 and
5315 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005316 // Same for blob.
5317 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318
5319 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005320 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005321 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005322
5323 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324 {
Bram Moolenaared591872020-08-15 22:14:53 +02005325 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005326 n1 = tv->vval.v_number;
5327 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328 }
Bram Moolenaared591872020-08-15 22:14:53 +02005329
Bram Moolenaar4c137212021-04-19 16:48:48 +02005330 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005331 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005332 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005333 if (is_blob)
5334 {
5335 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5336 n1, n2, FALSE, tv) == FAIL)
5337 goto on_error;
5338 }
5339 else
5340 {
5341 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5342 n1, n2, FALSE, tv, TRUE) == FAIL)
5343 goto on_error;
5344 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345 }
5346 break;
5347
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005348 case ISN_ANYINDEX:
5349 case ISN_ANYSLICE:
5350 {
5351 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5352 typval_T *var1, *var2;
5353 int res;
5354
5355 // index: composite is at stack-2, index at stack-1
5356 // slice: composite is at stack-3, indexes at stack-2 and
5357 // stack-1
5358 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005359 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005360 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5361 goto on_error;
5362 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5363 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005364 res = eval_index_inner(tv, is_slice, var1, var2,
5365 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005366 clear_tv(var1);
5367 if (is_slice)
5368 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005369 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005370 if (res == FAIL)
5371 goto on_error;
5372 }
5373 break;
5374
Bram Moolenaar9af78762020-06-16 11:34:42 +02005375 case ISN_SLICE:
5376 {
5377 list_T *list;
5378 int count = iptr->isn_arg.number;
5379
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005380 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005381 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005382 list = tv->vval.v_list;
5383
5384 // no error for short list, expect it to be checked earlier
5385 if (list != NULL && list->lv_len >= count)
5386 {
5387 list_T *newlist = list_slice(list,
5388 count, list->lv_len - 1);
5389
5390 if (newlist != NULL)
5391 {
5392 list_unref(list);
5393 tv->vval.v_list = newlist;
5394 ++newlist->lv_refcount;
5395 }
5396 }
5397 }
5398 break;
5399
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005400 case ISN_GETITEM:
5401 {
5402 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005403 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005404
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005405 // Get list item: list is at stack-1, push item.
5406 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005407 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5408 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005409
Bram Moolenaar35578162021-08-02 19:10:38 +02005410 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005411 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005412 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005413 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005414
5415 // Useful when used in unpack assignment. Reset at
5416 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005417 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005418 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005419 }
5420 break;
5421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422 case ISN_MEMBER:
5423 {
5424 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005425 char_u *key;
5426 dictitem_T *di;
5427
5428 // dict member: dict is at stack-2, key at stack-1
5429 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005430 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005431 dict = tv->vval.v_dict;
5432
5433 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005434 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005435 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005436 if (key == NULL)
5437 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005438
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005439 if ((di = dict_find(dict, key, -1)) == NULL)
5440 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005441 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005442 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005443
5444 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005445 // stack contents makes sense and the dict stack is
5446 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005447 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005448 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005449 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005450 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005451 tv->v_type = VAR_NUMBER;
5452 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005453 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005454 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005455 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005456 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005457 // Put the dict used on the dict stack, it might be used by
5458 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005459 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005460 if (dict_stack_save(tv) == FAIL)
5461 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005462 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005463 }
5464 break;
5465
5466 // dict member with string key
5467 case ISN_STRINGMEMBER:
5468 {
5469 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470 dictitem_T *di;
5471
5472 tv = STACK_TV_BOT(-1);
5473 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5474 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005475 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005476 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005477 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005478 }
5479 dict = tv->vval.v_dict;
5480
5481 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5482 == NULL)
5483 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005484 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005485 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005486 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005487 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005488 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005489 // Put the dict used on the dict stack, it might be used by
5490 // a dict function later.
5491 if (dict_stack_save(tv) == FAIL)
5492 goto on_fatal_error;
5493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005495 }
5496 break;
5497
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005498 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005499 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005500 {
5501 tv = STACK_TV_BOT(-1);
5502 if (tv->v_type != VAR_OBJECT)
5503 {
5504 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005505 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005506 goto on_error;
5507 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005508
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005509 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005510 if (obj == NULL)
5511 {
5512 SOURCING_LNUM = iptr->isn_lnum;
5513 emsg(_(e_using_null_object));
5514 goto on_error;
5515 }
5516
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005517 int idx;
5518 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005519 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005520 else
5521 {
5522 idx = iptr->isn_arg.classmember.cm_idx;
5523 // convert the interface index to the object index
5524 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005525 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005526 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005527 }
5528
Ernie Rael18143d32023-09-04 22:30:41 +02005529 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005530 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005531 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005532
5533 // Unreference the object after getting the member, it may
5534 // be freed.
5535 object_unref(obj);
5536 }
5537 break;
5538
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005539 case ISN_STORE_THIS:
5540 {
5541 int idx = iptr->isn_arg.number;
5542 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5543 // the members are located right after the object struct
5544 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5545 clear_tv(mtv);
5546 *mtv = *STACK_TV_BOT(-1);
5547 --ectx->ec_stack.ga_len;
5548 }
5549 break;
5550
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005551 case ISN_CLEARDICT:
5552 dict_stack_drop();
5553 break;
5554
5555 case ISN_USEDICT:
5556 {
5557 typval_T *dict_tv = dict_stack_get_tv();
5558
5559 // Turn "dict.Func" into a partial for "Func" bound to
5560 // "dict". Don't do this when "Func" is already a partial
5561 // that was bound explicitly (pt_auto is FALSE).
5562 tv = STACK_TV_BOT(-1);
5563 if (dict_tv != NULL
5564 && dict_tv->v_type == VAR_DICT
5565 && dict_tv->vval.v_dict != NULL
5566 && (tv->v_type == VAR_FUNC
5567 || (tv->v_type == VAR_PARTIAL
5568 && (tv->vval.v_partial->pt_auto
5569 || tv->vval.v_partial->pt_dict == NULL))))
5570 dict_tv->vval.v_dict =
5571 make_partial(dict_tv->vval.v_dict, tv);
5572 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005573 }
5574 break;
5575
5576 case ISN_NEGATENR:
5577 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005578 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005579 if (tv->v_type == VAR_FLOAT)
5580 tv->vval.v_float = -tv->vval.v_float;
5581 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005582 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005583 break;
5584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585 case ISN_CHECKTYPE:
5586 {
5587 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005588 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005589 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005591 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005592 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005593 if (ct->ct_arg_idx > 0)
5594 {
5595 where.wt_index = ct->ct_arg_idx;
5596 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005597 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005598 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005599 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005600 if (r == FAIL)
5601 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005602
5603 // number 0 is FALSE, number 1 is TRUE
5604 if (tv->v_type == VAR_NUMBER
5605 && ct->ct_type->tt_type == VAR_BOOL
5606 && (tv->vval.v_number == 0
5607 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005608 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005609 tv->v_type = VAR_BOOL;
5610 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005611 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612 }
5613 }
5614 break;
5615
Bram Moolenaar9af78762020-06-16 11:34:42 +02005616 case ISN_CHECKLEN:
5617 {
5618 int min_len = iptr->isn_arg.checklen.cl_min_len;
5619 list_T *list = NULL;
5620
5621 tv = STACK_TV_BOT(-1);
5622 if (tv->v_type == VAR_LIST)
5623 list = tv->vval.v_list;
5624 if (list == NULL || list->lv_len < min_len
5625 || (list->lv_len > min_len
5626 && !iptr->isn_arg.checklen.cl_more_OK))
5627 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005628 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005629 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005630 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005631 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005632 }
5633 }
5634 break;
5635
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005636 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005637 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005638 break;
5639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005641 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642 {
5643 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005644 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005645
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005646 if (iptr->isn_type == ISN_2BOOL)
5647 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005648 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005649 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005650 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005651 n = !n;
5652 }
5653 else
5654 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005655 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005656 SOURCING_LNUM = iptr->isn_lnum;
5657 n = tv_get_bool_chk(tv, &error);
5658 if (error)
5659 goto on_error;
5660 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005661 clear_tv(tv);
5662 tv->v_type = VAR_BOOL;
5663 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5664 }
5665 break;
5666
5667 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005668 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005669 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005670 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5671 iptr->isn_type == ISN_2STRING_ANY,
5672 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005673 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674 break;
5675
Bram Moolenaar08597872020-12-10 19:43:40 +01005676 case ISN_RANGE:
5677 {
5678 exarg_T ea;
5679 char *errormsg;
5680
Bram Moolenaarece0b872021-01-08 20:40:45 +01005681 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005682 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005683 ea.addr_type = ADDR_LINES;
5684 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005685 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005686 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005687 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005688
Bram Moolenaar35578162021-08-02 19:10:38 +02005689 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005690 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005691 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005692 tv = STACK_TV_BOT(-1);
5693 tv->v_type = VAR_NUMBER;
5694 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005695 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005696 }
5697 break;
5698
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005699 case ISN_PUT:
5700 {
5701 int regname = iptr->isn_arg.put.put_regname;
5702 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5703 char_u *expr = NULL;
5704 int dir = FORWARD;
5705
Bram Moolenaar08597872020-12-10 19:43:40 +01005706 if (lnum < -2)
5707 {
5708 // line number was put on the stack by ISN_RANGE
5709 tv = STACK_TV_BOT(-1);
5710 curwin->w_cursor.lnum = tv->vval.v_number;
5711 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5712 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005713 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005714 }
5715 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005716 // :put! above cursor
5717 dir = BACKWARD;
5718 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005719 {
5720 curwin->w_cursor.lnum = lnum;
5721 if (lnum == 0)
5722 // check_cursor() below will move to line 1
5723 dir = BACKWARD;
5724 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005725
5726 if (regname == '=')
5727 {
5728 tv = STACK_TV_BOT(-1);
5729 if (tv->v_type == VAR_STRING)
5730 expr = tv->vval.v_string;
5731 else
5732 {
5733 expr = typval2string(tv, TRUE); // allocates value
5734 clear_tv(tv);
5735 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005736 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005737 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005738 check_cursor();
5739 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5740 vim_free(expr);
5741 }
5742 break;
5743
Bram Moolenaar02194d22020-10-24 23:08:38 +02005744 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005745 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5746 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5747 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5748 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005749 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5750 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005751 break;
5752
Bram Moolenaar02194d22020-10-24 23:08:38 +02005753 case ISN_CMDMOD_REV:
5754 // filter regprog is owned by the instruction, don't free it
5755 cmdmod.cmod_filter_regmatch.regprog = NULL;
5756 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005757 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5758 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005759 break;
5760
Bram Moolenaar792f7862020-11-23 08:31:18 +01005761 case ISN_UNPACK:
5762 {
5763 int count = iptr->isn_arg.unpack.unp_count;
5764 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5765 list_T *l;
5766 listitem_T *li;
5767 int i;
5768
5769 // Check there is a valid list to unpack.
5770 tv = STACK_TV_BOT(-1);
5771 if (tv->v_type != VAR_LIST)
5772 {
5773 SOURCING_LNUM = iptr->isn_lnum;
5774 emsg(_(e_for_argument_must_be_sequence_of_lists));
5775 goto on_error;
5776 }
5777 l = tv->vval.v_list;
5778 if (l == NULL
5779 || l->lv_len < (semicolon ? count - 1 : count))
5780 {
5781 SOURCING_LNUM = iptr->isn_lnum;
5782 emsg(_(e_list_value_does_not_have_enough_items));
5783 goto on_error;
5784 }
5785 else if (!semicolon && l->lv_len > count)
5786 {
5787 SOURCING_LNUM = iptr->isn_lnum;
5788 emsg(_(e_list_value_has_more_items_than_targets));
5789 goto on_error;
5790 }
5791
5792 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005793 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005794 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005795 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005796
5797 // Variable after semicolon gets a list with the remaining
5798 // items.
5799 if (semicolon)
5800 {
5801 list_T *rem_list =
5802 list_alloc_with_items(l->lv_len - count + 1);
5803
5804 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005805 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005806 tv = STACK_TV_BOT(-count);
5807 tv->vval.v_list = rem_list;
5808 ++rem_list->lv_refcount;
5809 tv->v_lock = 0;
5810 li = l->lv_first;
5811 for (i = 0; i < count - 1; ++i)
5812 li = li->li_next;
5813 for (i = 0; li != NULL; ++i)
5814 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005815 typval_T tvcopy;
5816
5817 copy_tv(&li->li_tv, &tvcopy);
5818 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005819 li = li->li_next;
5820 }
5821 --count;
5822 }
5823
5824 // Produce the values in reverse order, first item last.
5825 li = l->lv_first;
5826 for (i = 0; i < count; ++i)
5827 {
5828 tv = STACK_TV_BOT(-i - 1);
5829 copy_tv(&li->li_tv, tv);
5830 li = li->li_next;
5831 }
5832
5833 list_unref(l);
5834 }
5835 break;
5836
Bram Moolenaarb2049902021-01-24 12:53:53 +01005837 case ISN_PROF_START:
5838 case ISN_PROF_END:
5839 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005840#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005841 funccall_T cookie;
5842 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005843 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005844 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005845
Bram Moolenaarca16c602022-09-06 18:57:08 +01005846 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005847 if (iptr->isn_type == ISN_PROF_START)
5848 {
5849 func_line_start(&cookie, iptr->isn_lnum);
5850 // if we get here the instruction is executed
5851 func_line_exec(&cookie);
5852 }
5853 else
5854 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005855#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005856 }
5857 break;
5858
Bram Moolenaare99d4222021-06-13 14:01:26 +02005859 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005860 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005861 break;
5862
Bram Moolenaar389df252020-07-09 21:20:47 +02005863 case ISN_SHUFFLE:
5864 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005865 typval_T tmp_tv;
5866 int item = iptr->isn_arg.shuffle.shfl_item;
5867 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005868
5869 tmp_tv = *STACK_TV_BOT(-item);
5870 for ( ; up > 0 && item > 1; --up)
5871 {
5872 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5873 --item;
5874 }
5875 *STACK_TV_BOT(-item) = tmp_tv;
5876 }
5877 break;
5878
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005879 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005880 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005881 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005882 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883 break;
5884 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005885 continue;
5886
Bram Moolenaard032f342020-07-18 18:13:02 +02005887func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005888 // Restore previous function. If the frame pointer is where we started
5889 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005890 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005891 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005892
Bram Moolenaar4c137212021-04-19 16:48:48 +02005893 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005894 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005895 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005896 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005897
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005898on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005899 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005900 // If "emsg_silent" is set then ignore the error, unless it was set
5901 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005902 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005903 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005904 {
5905 // If a sequence of instructions causes an error while ":silent!"
5906 // was used, restore the stack length and jump ahead to restoring
5907 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005908 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005909 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005910 while (ectx->ec_stack.ga_len
5911 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005912 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005913 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005914 clear_tv(STACK_TV_BOT(0));
5915 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005916 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5917 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005918 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005919 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005920 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005921on_fatal_error:
5922 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005923 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005924 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005925 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005926 }
5927
5928done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005929 ret = OK;
5930theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005931 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005932
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005933 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005934 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5935 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005936}
5937
5938/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005939 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005940 * "rettv".
5941 * Return OK or FAIL.
5942 */
5943 int
5944exe_typval_instr(typval_T *tv, typval_T *rettv)
5945{
5946 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5947 isn_T *save_instr = ectx->ec_instr;
5948 int save_iidx = ectx->ec_iidx;
5949 int res;
5950
LemonBoyf3b48952022-05-05 13:53:03 +01005951 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5952 // even when the compilation fails.
5953 rettv->v_type = VAR_UNKNOWN;
5954
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005955 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5956 res = exec_instructions(ectx);
5957 if (res == OK)
5958 {
5959 *rettv = *STACK_TV_BOT(-1);
5960 --ectx->ec_stack.ga_len;
5961 }
5962
5963 ectx->ec_instr = save_instr;
5964 ectx->ec_iidx = save_iidx;
5965
5966 return res;
5967}
5968
5969/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005970 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5971 * "substitute_instr".
5972 */
5973 char_u *
5974exe_substitute_instr(void)
5975{
5976 ectx_T *ectx = substitute_instr->subs_ectx;
5977 isn_T *save_instr = ectx->ec_instr;
5978 int save_iidx = ectx->ec_iidx;
5979 char_u *res;
5980
5981 ectx->ec_instr = substitute_instr->subs_instr;
5982 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005983 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005984 typval_T *tv = STACK_TV_BOT(-1);
5985
Bram Moolenaar27523602021-06-05 21:36:19 +02005986 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005987 --ectx->ec_stack.ga_len;
5988 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005989 }
5990 else
5991 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005992 substitute_instr->subs_status = FAIL;
5993 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005995
Bram Moolenaar4c137212021-04-19 16:48:48 +02005996 ectx->ec_instr = save_instr;
5997 ectx->ec_iidx = save_iidx;
5998
5999 return res;
6000}
6001
6002/*
6003 * Call a "def" function from old Vim script.
6004 * Return OK or FAIL.
6005 */
6006 int
6007call_def_function(
6008 ufunc_T *ufunc,
6009 int argc_arg, // nr of arguments
6010 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006011 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02006012 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006013 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01006014 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006015 typval_T *rettv) // return value
6016{
6017 ectx_T ectx; // execution context
6018 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006019 int partial_argc = partial == NULL
6020 || (flags & DEF_USE_PT_ARGV) == 0
6021 ? 0 : partial->pt_argc;
6022 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006023 typval_T *tv;
6024 int idx;
6025 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006026 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006027 sctx_T save_current_sctx = current_sctx;
6028 int did_emsg_before = did_emsg_cumul + did_emsg;
6029 int save_suppress_errthrow = suppress_errthrow;
6030 msglist_T **saved_msg_list = NULL;
6031 msglist_T *private_msg_list = NULL;
6032 int save_emsg_silent_def = emsg_silent_def;
6033 int save_did_emsg_def = did_emsg_def;
6034 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006035 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006036
6037// Get pointer to item in the stack.
6038#undef STACK_TV
6039#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6040
6041// Get pointer to item at the bottom of the stack, -1 is the bottom.
6042#undef STACK_TV_BOT
6043#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6044
6045// Get pointer to a local variable on the stack. Negative for arguments.
6046#undef STACK_TV_VAR
6047#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6048
6049 if (ufunc->uf_def_status == UF_NOT_COMPILED
6050 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006051 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6052 && compile_def_function(ufunc, FALSE,
6053 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006054 {
6055 if (did_emsg_cumul + did_emsg == did_emsg_before)
6056 semsg(_(e_function_is_not_compiled_str),
6057 printable_func_name(ufunc));
6058 return FAIL;
6059 }
6060
6061 {
6062 // Check the function was really compiled.
6063 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6064 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006065 if (dfunc->df_ufunc == NULL)
6066 {
6067 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6068 return FAIL;
6069 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006070 if (INSTRUCTIONS(dfunc) == NULL)
6071 {
6072 iemsg("using call_def_function() on not compiled function");
6073 return FAIL;
6074 }
6075 }
6076
6077 // If depth of calling is getting too high, don't execute the function.
6078 orig_funcdepth = funcdepth_get();
6079 if (funcdepth_increment() == FAIL)
6080 return FAIL;
6081
6082 CLEAR_FIELD(ectx);
6083 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6084 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006085 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006086 {
6087 funcdepth_decrement();
6088 return FAIL;
6089 }
6090 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6091 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6092 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006093 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006094
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006095 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006096 if (idx > 0 && ufunc->uf_va_name == NULL)
6097 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006098 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006099 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006100 goto failed_early;
6101 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006102 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006103 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006104 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006105 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006106 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006107 goto failed_early;
6108 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006109
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006110 // Put values from the partial and arguments on the stack, but no more than
6111 // what the function expects. A lambda can be called with more arguments
6112 // than it uses.
6113 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006114 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6115 ++idx)
6116 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006117 int argv_idx = idx - partial_argc;
6118
6119 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006120 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006121 && tv->v_type == VAR_SPECIAL
6122 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006123 {
6124 // Use the default value.
6125 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6126 }
6127 else
6128 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006129 int done = FALSE;
6130 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6131 {
6132 type_T *expected = ufunc->uf_arg_types[idx];
6133 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6134 {
6135 // When a float is expected and a number was given, convert
6136 // the value.
6137 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6138 STACK_TV_BOT(0)->v_lock = 0;
6139 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6140 done = TRUE;
6141 }
6142 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006143 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006144 goto failed_early;
6145 }
6146 if (!done)
6147 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006148 }
6149 ++ectx.ec_stack.ga_len;
6150 }
6151
6152 // Turn varargs into a list. Empty list if no args.
6153 if (ufunc->uf_va_name != NULL)
6154 {
6155 int vararg_count = argc - ufunc->uf_args.ga_len;
6156
6157 if (vararg_count < 0)
6158 vararg_count = 0;
6159 else
6160 argc -= vararg_count;
6161 if (exe_newlist(vararg_count, &ectx) == FAIL)
6162 goto failed_early;
6163
6164 // Check the type of the list items.
6165 tv = STACK_TV_BOT(-1);
6166 if (ufunc->uf_va_type != NULL
6167 && ufunc->uf_va_type != &t_list_any
6168 && ufunc->uf_va_type->tt_member != &t_any
6169 && tv->vval.v_list != NULL)
6170 {
6171 type_T *expected = ufunc->uf_va_type->tt_member;
6172 listitem_T *li = tv->vval.v_list->lv_first;
6173
6174 for (idx = 0; idx < vararg_count; ++idx)
6175 {
6176 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006177 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006178 goto failed_early;
6179 li = li->li_next;
6180 }
6181 }
6182
6183 if (defcount > 0)
6184 // Move varargs list to below missing default arguments.
6185 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6186 --ectx.ec_stack.ga_len;
6187 }
6188
6189 // Make space for omitted arguments, will store default value below.
6190 // Any varargs list goes after them.
6191 if (defcount > 0)
6192 for (idx = 0; idx < defcount; ++idx)
6193 {
6194 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6195 ++ectx.ec_stack.ga_len;
6196 }
6197 if (ufunc->uf_va_name != NULL)
6198 ++ectx.ec_stack.ga_len;
6199
6200 // Frame pointer points to just after arguments.
6201 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6202 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6203
6204 {
6205 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6206 + ufunc->uf_dfunc_idx;
6207 ufunc_T *base_ufunc = dfunc->df_ufunc;
6208
6209 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006210 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006211 if (partial != NULL || base_ufunc->uf_partial != NULL)
6212 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006213 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6214 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006215 goto failed_early;
6216 if (partial != NULL)
6217 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006218 outer_T *outer = get_pt_outer(partial);
6219
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006220 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006221 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006222 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006223 if (current_ectx != NULL)
6224 {
6225 if (current_ectx->ec_outer_ref != NULL
6226 && current_ectx->ec_outer_ref->or_outer != NULL)
6227 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006228 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006229 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006230 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006231 }
6232 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006233 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006234 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006235 ++partial->pt_refcount;
6236 ectx.ec_outer_ref->or_partial = partial;
6237 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006238 }
6239 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006240 {
6241 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6242 ++base_ufunc->uf_partial->pt_refcount;
6243 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6244 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006245 }
6246 }
6247
6248 // dummy frame entries
6249 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6250 {
6251 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6252 ++ectx.ec_stack.ga_len;
6253 }
6254
6255 {
6256 // Reserve space for local variables and any closure reference count.
6257 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6258 + ufunc->uf_dfunc_idx;
6259
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006260 // Initialize variables to zero. That avoids having to generate
6261 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006262 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006263 {
6264 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6265 STACK_TV_VAR(idx)->vval.v_number = 0;
6266 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006267 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006268
6269 if (object != NULL)
6270 {
6271 // the object is always the variable at index zero
6272 tv = STACK_TV_VAR(0);
6273 tv->v_type = VAR_OBJECT;
6274 tv->vval.v_object = object;
6275 }
6276
Bram Moolenaar4c137212021-04-19 16:48:48 +02006277 if (dfunc->df_has_closure)
6278 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006279 // Initialize the variable that counts how many closures were
6280 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006281 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6282 STACK_TV_VAR(idx)->vval.v_number = 0;
6283 ++ectx.ec_stack.ga_len;
6284 }
6285
6286 ectx.ec_instr = INSTRUCTIONS(dfunc);
6287 }
6288
Bram Moolenaar58779852022-09-06 18:31:14 +01006289 // Store the execution context in funccal, used by invoke_all_defer().
6290 if (funccal != NULL)
6291 funccal->fc_ectx = &ectx;
6292
Bram Moolenaar4c137212021-04-19 16:48:48 +02006293 // Following errors are in the function, not the caller.
6294 // Commands behave like vim9script.
6295 estack_push_ufunc(ufunc, 1);
6296 current_sctx = ufunc->uf_script_ctx;
6297 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6298
6299 // Use a specific location for storing error messages to be converted to an
6300 // exception.
6301 saved_msg_list = msg_list;
6302 msg_list = &private_msg_list;
6303
6304 // Do turn errors into exceptions.
6305 suppress_errthrow = FALSE;
6306
6307 // Do not delete the function while executing it.
6308 ++ufunc->uf_calls;
6309
6310 // When ":silent!" was used before calling then we still abort the
6311 // function. If ":silent!" is used in the function then we don't.
6312 emsg_silent_def = emsg_silent;
6313 did_emsg_def = 0;
6314
LemonBoyc5d27442023-08-19 13:02:35 +02006315 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006316
Bram Moolenaar5800c792022-09-22 17:34:01 +01006317 /*
6318 * Execute the instructions until done.
6319 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006320 ret = exec_instructions(&ectx);
6321 if (ret == OK)
6322 {
6323 // function finished, get result from the stack.
6324 if (ufunc->uf_ret_type == &t_void)
6325 {
6326 rettv->v_type = VAR_VOID;
6327 }
6328 else
6329 {
6330 tv = STACK_TV_BOT(-1);
6331 *rettv = *tv;
6332 tv->v_type = VAR_UNKNOWN;
6333 }
6334 }
6335
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006336 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006337 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006338
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006339 // Deal with any remaining closures, they may be in use somewhere.
6340 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006341 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006342 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006343 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006344 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006345
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006346 estack_pop();
6347 current_sctx = save_current_sctx;
6348
Bram Moolenaar2336c372021-12-06 15:06:54 +00006349 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6350 // Function was unreferenced while being used, free it now.
6351 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006352
Bram Moolenaar352134b2020-10-17 22:04:08 +02006353 if (*msg_list != NULL && saved_msg_list != NULL)
6354 {
6355 msglist_T **plist = saved_msg_list;
6356
6357 // Append entries from the current msg_list (uncaught exceptions) to
6358 // the saved msg_list.
6359 while (*plist != NULL)
6360 plist = &(*plist)->next;
6361
6362 *plist = *msg_list;
6363 }
6364 msg_list = saved_msg_list;
6365
Bram Moolenaar4c137212021-04-19 16:48:48 +02006366 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006367 {
6368 cmdmod.cmod_filter_regmatch.regprog = NULL;
6369 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006370 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006371 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006372 emsg_silent_def = save_emsg_silent_def;
6373 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006374
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006375failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006376 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006377 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006378 {
6379 tv = STACK_TV(idx);
6380 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6381 clear_tv(tv);
6382 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006383 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006385 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006386 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006387 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006388 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006389 if (ectx.ec_outer_ref->or_outer_allocated)
6390 vim_free(ectx.ec_outer_ref->or_outer);
6391 partial_unref(ectx.ec_outer_ref->or_partial);
6392 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006393 }
6394
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006395 // Not sure if this is necessary.
6396 suppress_errthrow = save_suppress_errthrow;
6397
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006398 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6399 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006400 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006401 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006402 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403 return ret;
6404}
6405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006407 * Called when a def function has finished (possibly failed).
6408 * Invoke all the function returns to clean up and invoke deferred functions,
6409 * except the toplevel one.
6410 */
6411 void
6412unwind_def_callstack(ectx_T *ectx)
6413{
6414 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6415 func_return(ectx);
6416}
6417
6418/*
dundargocc57b5bc2022-11-02 13:30:51 +00006419 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006420 */
6421 void
6422may_invoke_defer_funcs(ectx_T *ectx)
6423{
6424 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6425
6426 if (dfunc->df_defer_var_idx > 0)
6427 invoke_defer_funcs(ectx);
6428}
6429
6430/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006431 * Return loopvarinfo in a printable form in allocated memory.
6432 */
6433 static char_u *
6434printable_loopvarinfo(loopvarinfo_T *lvi)
6435{
6436 garray_T ga;
6437 int depth;
6438
6439 ga_init2(&ga, 1, 100);
6440 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6441 {
6442 if (ga_grow(&ga, 50) == FAIL)
6443 break;
6444 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006445 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006446 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006447 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006448 lvi->lvi_loop[depth].var_idx,
6449 lvi->lvi_loop[depth].var_idx
6450 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006451 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006452 }
6453 return ga.ga_data;
6454}
6455
6456/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006457 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6458 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6459 * "pfx" is prefixed to every line.
6460 */
6461 static void
6462list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6463{
6464 int line_idx = 0;
6465 int prev_current = 0;
6466 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006467 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006468
6469 for (current = 0; current < instr_count; ++current)
6470 {
6471 isn_T *iptr = &instr[current];
6472 char *line;
6473
6474 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006475 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006476 while (line_idx < iptr->isn_lnum
6477 && line_idx < ufunc->uf_lines.ga_len)
6478 {
6479 if (current > prev_current)
6480 {
6481 msg_puts("\n\n");
6482 prev_current = current;
6483 }
6484 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6485 if (line != NULL)
6486 msg(line);
6487 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006488 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6489 {
6490 int first_def_arg = ufunc->uf_args.ga_len
6491 - ufunc->uf_def_args.ga_len;
6492
6493 if (def_arg_idx > 0)
6494 msg_puts("\n\n");
6495 msg_start();
6496 msg_puts(" ");
6497 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6498 first_def_arg + def_arg_idx]);
6499 msg_puts(" = ");
6500 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6501 msg_clr_eos();
6502 msg_end();
6503 }
6504 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006505
6506 switch (iptr->isn_type)
6507 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006508 case ISN_CONSTRUCT:
6509 smsg("%s%4d NEW %s size %d", pfx, current,
6510 iptr->isn_arg.construct.construct_class->class_name,
6511 (int)iptr->isn_arg.construct.construct_size);
6512 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006513 case ISN_EXEC:
6514 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6515 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006516 case ISN_EXEC_SPLIT:
6517 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6518 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006519 case ISN_EXECRANGE:
6520 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6521 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006522 case ISN_LEGACY_EVAL:
6523 smsg("%s%4d EVAL legacy %s", pfx, current,
6524 iptr->isn_arg.string);
6525 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006526 case ISN_REDIRSTART:
6527 smsg("%s%4d REDIR", pfx, current);
6528 break;
6529 case ISN_REDIREND:
6530 smsg("%s%4d REDIR END%s", pfx, current,
6531 iptr->isn_arg.number ? " append" : "");
6532 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006533 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006534#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006535 smsg("%s%4d CEXPR pre %s", pfx, current,
6536 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006537#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006538 break;
6539 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006540#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006541 {
6542 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6543
6544 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6545 cexpr_get_auname(cer->cer_cmdidx),
6546 cer->cer_forceit ? "!" : "",
6547 cer->cer_cmdline);
6548 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006549#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006550 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006551 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006552 smsg("%s%4d INSTR", pfx, current);
6553 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6554 msg(" -------------");
6555 break;
6556 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006557 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006558 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6559
6560 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006561 }
6562 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006563 case ISN_SUBSTITUTE:
6564 {
6565 subs_T *subs = &iptr->isn_arg.subs;
6566
6567 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6568 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6569 msg(" -------------");
6570 }
6571 break;
6572 case ISN_EXECCONCAT:
6573 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6574 (varnumber_T)iptr->isn_arg.number);
6575 break;
6576 case ISN_ECHO:
6577 {
6578 echo_T *echo = &iptr->isn_arg.echo;
6579
6580 smsg("%s%4d %s %d", pfx, current,
6581 echo->echo_with_white ? "ECHO" : "ECHON",
6582 echo->echo_count);
6583 }
6584 break;
6585 case ISN_EXECUTE:
6586 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006587 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006588 break;
6589 case ISN_ECHOMSG:
6590 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006591 (varnumber_T)(iptr->isn_arg.number));
6592 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006593 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006594 if (iptr->isn_arg.echowin.ewin_time > 0)
6595 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6596 iptr->isn_arg.echowin.ewin_count,
6597 iptr->isn_arg.echowin.ewin_time);
6598 else
6599 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6600 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006601 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006602 case ISN_ECHOCONSOLE:
6603 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6604 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006605 break;
6606 case ISN_ECHOERR:
6607 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006608 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006609 break;
6610 case ISN_LOAD:
6611 {
6612 if (iptr->isn_arg.number < 0)
6613 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6614 (varnumber_T)(iptr->isn_arg.number
6615 + STACK_FRAME_SIZE));
6616 else
6617 smsg("%s%4d LOAD $%lld", pfx, current,
6618 (varnumber_T)(iptr->isn_arg.number));
6619 }
6620 break;
6621 case ISN_LOADOUTER:
6622 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006623 isn_outer_T *outer = &iptr->isn_arg.outer;
6624
6625 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006626 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006627 outer->outer_depth,
6628 outer->outer_idx + STACK_FRAME_SIZE);
6629 else if (outer->outer_depth < 0)
6630 smsg("%s%4d LOADOUTER $%d in loop level %d",
6631 pfx, current,
6632 outer->outer_idx,
6633 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006634 else
6635 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006636 outer->outer_depth,
6637 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006638 }
6639 break;
6640 case ISN_LOADV:
6641 smsg("%s%4d LOADV v:%s", pfx, current,
6642 get_vim_var_name(iptr->isn_arg.number));
6643 break;
6644 case ISN_LOADSCRIPT:
6645 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006646 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6647 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6648 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006649
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006650 sv = get_script_svar(sref, -1);
6651 if (sv == NULL)
6652 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6653 pfx, current, si->sn_name);
6654 else
6655 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006656 sv->sv_name,
6657 sref->sref_idx,
6658 si->sn_name);
6659 }
6660 break;
6661 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006662 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006663 {
6664 scriptitem_T *si = SCRIPT_ITEM(
6665 iptr->isn_arg.loadstore.ls_sid);
6666
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006667 smsg("%s%4d %s s:%s from %s", pfx, current,
6668 iptr->isn_type == ISN_LOADS ? "LOADS"
6669 : "LOADEXPORT",
6670 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006671 }
6672 break;
6673 case ISN_LOADAUTO:
6674 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6675 break;
6676 case ISN_LOADG:
6677 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6678 break;
6679 case ISN_LOADB:
6680 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6681 break;
6682 case ISN_LOADW:
6683 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6684 break;
6685 case ISN_LOADT:
6686 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6687 break;
6688 case ISN_LOADGDICT:
6689 smsg("%s%4d LOAD g:", pfx, current);
6690 break;
6691 case ISN_LOADBDICT:
6692 smsg("%s%4d LOAD b:", pfx, current);
6693 break;
6694 case ISN_LOADWDICT:
6695 smsg("%s%4d LOAD w:", pfx, current);
6696 break;
6697 case ISN_LOADTDICT:
6698 smsg("%s%4d LOAD t:", pfx, current);
6699 break;
6700 case ISN_LOADOPT:
6701 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6702 break;
6703 case ISN_LOADENV:
6704 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6705 break;
6706 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006707 smsg("%s%4d LOADREG @%c", pfx, current,
6708 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006709 break;
6710
6711 case ISN_STORE:
6712 if (iptr->isn_arg.number < 0)
6713 smsg("%s%4d STORE arg[%lld]", pfx, current,
6714 iptr->isn_arg.number + STACK_FRAME_SIZE);
6715 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006716 smsg("%s%4d STORE $%lld", pfx, current,
6717 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006718 break;
6719 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006720 {
6721 isn_outer_T *outer = &iptr->isn_arg.outer;
6722
6723 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6724 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6725 pfx, current, outer->outer_idx);
6726 else
6727 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6728 outer->outer_depth, outer->outer_idx);
6729 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006730 break;
6731 case ISN_STOREV:
6732 smsg("%s%4d STOREV v:%s", pfx, current,
6733 get_vim_var_name(iptr->isn_arg.number));
6734 break;
6735 case ISN_STOREAUTO:
6736 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6737 break;
6738 case ISN_STOREG:
6739 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6740 break;
6741 case ISN_STOREB:
6742 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6743 break;
6744 case ISN_STOREW:
6745 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6746 break;
6747 case ISN_STORET:
6748 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6749 break;
6750 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006751 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006752 {
6753 scriptitem_T *si = SCRIPT_ITEM(
6754 iptr->isn_arg.loadstore.ls_sid);
6755
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006756 smsg("%s%4d %s %s in %s", pfx, current,
6757 iptr->isn_type == ISN_STORES
6758 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006759 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6760 }
6761 break;
6762 case ISN_STORESCRIPT:
6763 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006764 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6765 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6766 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006767
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006768 sv = get_script_svar(sref, -1);
6769 if (sv == NULL)
6770 smsg("%s%4d STORESCRIPT [deleted] in %s",
6771 pfx, current, si->sn_name);
6772 else
6773 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006774 sv->sv_name,
6775 sref->sref_idx,
6776 si->sn_name);
6777 }
6778 break;
6779 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006780 case ISN_STOREFUNCOPT:
6781 smsg("%s%4d %s &%s", pfx, current,
6782 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006783 iptr->isn_arg.storeopt.so_name);
6784 break;
6785 case ISN_STOREENV:
6786 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6787 break;
6788 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006789 smsg("%s%4d STOREREG @%c", pfx, current,
6790 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006791 break;
6792 case ISN_STORENR:
6793 smsg("%s%4d STORE %lld in $%d", pfx, current,
6794 iptr->isn_arg.storenr.stnr_val,
6795 iptr->isn_arg.storenr.stnr_idx);
6796 break;
6797
6798 case ISN_STOREINDEX:
6799 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006800 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006801 break;
6802
6803 case ISN_STORERANGE:
6804 smsg("%s%4d STORERANGE", pfx, current);
6805 break;
6806
Bram Moolenaard505d172022-12-18 21:42:55 +00006807 case ISN_LOAD_CLASSMEMBER:
6808 case ISN_STORE_CLASSMEMBER:
6809 {
6810 class_T *cl = iptr->isn_arg.classmember.cm_class;
6811 int idx = iptr->isn_arg.classmember.cm_idx;
6812 ocmember_T *ocm = &cl->class_class_members[idx];
6813 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6814 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6815 ? "LOAD" : "STORE",
6816 cl->class_name, ocm->ocm_name);
6817 }
6818 break;
6819
Bram Moolenaar4c137212021-04-19 16:48:48 +02006820 // constants
6821 case ISN_PUSHNR:
6822 smsg("%s%4d PUSHNR %lld", pfx, current,
6823 (varnumber_T)(iptr->isn_arg.number));
6824 break;
6825 case ISN_PUSHBOOL:
6826 case ISN_PUSHSPEC:
6827 smsg("%s%4d PUSH %s", pfx, current,
6828 get_var_special_name(iptr->isn_arg.number));
6829 break;
6830 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006831 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006832 break;
6833 case ISN_PUSHS:
6834 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6835 break;
6836 case ISN_PUSHBLOB:
6837 {
6838 char_u *r;
6839 char_u numbuf[NUMBUFLEN];
6840 char_u *tofree;
6841
6842 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6843 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6844 vim_free(tofree);
6845 }
6846 break;
6847 case ISN_PUSHFUNC:
6848 {
6849 char *name = (char *)iptr->isn_arg.string;
6850
6851 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6852 name == NULL ? "[none]" : name);
6853 }
6854 break;
6855 case ISN_PUSHCHANNEL:
6856#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006857 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006858#endif
6859 break;
6860 case ISN_PUSHJOB:
6861#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006862 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006863#endif
6864 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006865 case ISN_PUSHOBJ:
6866 smsg("%s%4d PUSHOBJ null", pfx, current);
6867 break;
6868 case ISN_PUSHCLASS:
6869 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006870 iptr->isn_arg.classarg == NULL ? "null"
6871 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006872 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006873 case ISN_PUSHEXC:
6874 smsg("%s%4d PUSH v:exception", pfx, current);
6875 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006876 case ISN_AUTOLOAD:
6877 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6878 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006879 case ISN_UNLET:
6880 smsg("%s%4d UNLET%s %s", pfx, current,
6881 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6882 iptr->isn_arg.unlet.ul_name);
6883 break;
6884 case ISN_UNLETENV:
6885 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6886 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6887 iptr->isn_arg.unlet.ul_name);
6888 break;
6889 case ISN_UNLETINDEX:
6890 smsg("%s%4d UNLETINDEX", pfx, current);
6891 break;
6892 case ISN_UNLETRANGE:
6893 smsg("%s%4d UNLETRANGE", pfx, current);
6894 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006895 case ISN_LOCKUNLOCK:
6896 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6897 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006898 case ISN_LOCKCONST:
6899 smsg("%s%4d LOCKCONST", pfx, current);
6900 break;
6901 case ISN_NEWLIST:
6902 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006903 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006904 break;
6905 case ISN_NEWDICT:
6906 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006907 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006908 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006909 case ISN_NEWPARTIAL:
6910 smsg("%s%4d NEWPARTIAL", pfx, current);
6911 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006912
6913 // function call
6914 case ISN_BCALL:
6915 {
6916 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6917
6918 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6919 internal_func_name(cbfunc->cbf_idx),
6920 cbfunc->cbf_argcount);
6921 }
6922 break;
6923 case ISN_DCALL:
6924 {
6925 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6926 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6927 + cdfunc->cdf_idx;
6928
6929 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006930 printable_func_name(df->df_ufunc),
6931 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006932 }
6933 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006934 case ISN_METHODCALL:
6935 {
6936 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6937
6938 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6939 mfunc->cmf_itf->class_name,
6940 mfunc->cmf_itf->class_obj_methods[
6941 mfunc->cmf_idx]->uf_name,
6942 mfunc->cmf_argcount);
6943 }
6944 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006945 case ISN_UCALL:
6946 {
6947 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6948
6949 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6950 cufunc->cuf_name, cufunc->cuf_argcount);
6951 }
6952 break;
6953 case ISN_PCALL:
6954 {
6955 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6956
6957 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6958 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6959 }
6960 break;
6961 case ISN_PCALL_END:
6962 smsg("%s%4d PCALL end", pfx, current);
6963 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006964 case ISN_DEFER:
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02006965 smsg("%s%4d DEFER %d args", pfx, current,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006966 (int)iptr->isn_arg.defer.defer_argcount);
6967 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006968 case ISN_RETURN:
6969 smsg("%s%4d RETURN", pfx, current);
6970 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006971 case ISN_RETURN_VOID:
6972 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006973 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006974 case ISN_RETURN_OBJECT:
6975 smsg("%s%4d RETURN object", pfx, current);
6976 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006977 case ISN_FUNCREF:
6978 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006979 funcref_T *funcref = &iptr->isn_arg.funcref;
6980 funcref_extra_T *extra = funcref->fr_extra;
6981 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006982
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006983 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006984 {
6985 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6986 + funcref->fr_dfunc_idx;
6987 name = df->df_ufunc->uf_name;
6988 }
6989 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006990 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006991 if (extra != NULL && extra->fre_class != NULL)
6992 {
6993 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6994 extra->fre_class->class_name, name);
6995 }
6996 else if (extra == NULL
6997 || extra->fre_loopvar_info.lvi_depth == 0)
6998 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006999 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00007000 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007001 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007002 {
7003 char_u *info = printable_loopvarinfo(
7004 &extra->fre_loopvar_info);
7005
7006 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
7007 name, info);
7008 vim_free(info);
7009 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007010 }
7011 break;
7012
7013 case ISN_NEWFUNC:
7014 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01007015 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007016
Bram Moolenaarcc341812022-09-19 15:54:34 +01007017 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01007018 smsg("%s%4d NEWFUNC %s %s", pfx, current,
7019 arg->nfa_lambda, arg->nfa_global);
7020 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007021 {
7022 char_u *info = printable_loopvarinfo(
7023 &arg->nfa_loopvar_info);
7024
7025 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
7026 arg->nfa_lambda, arg->nfa_global, info);
7027 vim_free(info);
7028 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007029 }
7030 break;
7031
7032 case ISN_DEF:
7033 {
7034 char_u *name = iptr->isn_arg.string;
7035
7036 smsg("%s%4d DEF %s", pfx, current,
7037 name == NULL ? (char_u *)"" : name);
7038 }
7039 break;
7040
7041 case ISN_JUMP:
7042 {
7043 char *when = "?";
7044
7045 switch (iptr->isn_arg.jump.jump_when)
7046 {
7047 case JUMP_ALWAYS:
7048 when = "JUMP";
7049 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007050 case JUMP_NEVER:
7051 iemsg("JUMP_NEVER should not be used");
7052 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007053 case JUMP_AND_KEEP_IF_TRUE:
7054 when = "JUMP_AND_KEEP_IF_TRUE";
7055 break;
7056 case JUMP_IF_FALSE:
7057 when = "JUMP_IF_FALSE";
7058 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007059 case JUMP_WHILE_FALSE:
7060 when = "JUMP_WHILE_FALSE"; // unused
7061 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007062 case JUMP_IF_COND_FALSE:
7063 when = "JUMP_IF_COND_FALSE";
7064 break;
7065 case JUMP_IF_COND_TRUE:
7066 when = "JUMP_IF_COND_TRUE";
7067 break;
7068 }
7069 smsg("%s%4d %s -> %d", pfx, current, when,
7070 iptr->isn_arg.jump.jump_where);
7071 }
7072 break;
7073
7074 case ISN_JUMP_IF_ARG_SET:
7075 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7076 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7077 iptr->isn_arg.jump.jump_where);
7078 break;
7079
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007080 case ISN_JUMP_IF_ARG_NOT_SET:
7081 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7082 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7083 iptr->isn_arg.jump.jump_where);
7084 break;
7085
Bram Moolenaar4c137212021-04-19 16:48:48 +02007086 case ISN_FOR:
7087 {
7088 forloop_T *forloop = &iptr->isn_arg.forloop;
7089
7090 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007091 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007092 }
7093 break;
7094
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007095 case ISN_ENDLOOP:
7096 {
7097 endloop_T *endloop = &iptr->isn_arg.endloop;
7098
Bram Moolenaarcc341812022-09-19 15:54:34 +01007099 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7100 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007101 endloop->end_funcref_idx,
7102 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007103 endloop->end_var_idx + endloop->end_var_count - 1,
7104 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007105 }
7106 break;
7107
7108 case ISN_WHILE:
7109 {
7110 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7111
7112 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7113 whileloop->while_funcref_idx,
7114 whileloop->while_end);
7115 }
7116 break;
7117
Bram Moolenaar4c137212021-04-19 16:48:48 +02007118 case ISN_TRY:
7119 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007120 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007121
7122 if (try->try_ref->try_finally == 0)
7123 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7124 pfx, current,
7125 try->try_ref->try_catch,
7126 try->try_ref->try_endtry);
7127 else
7128 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7129 pfx, current,
7130 try->try_ref->try_catch,
7131 try->try_ref->try_finally,
7132 try->try_ref->try_endtry);
7133 }
7134 break;
7135 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007136 smsg("%s%4d CATCH", pfx, current);
7137 break;
7138 case ISN_TRYCONT:
7139 {
7140 trycont_T *trycont = &iptr->isn_arg.trycont;
7141
7142 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7143 trycont->tct_levels,
7144 trycont->tct_levels == 1 ? "" : "s",
7145 trycont->tct_where);
7146 }
7147 break;
7148 case ISN_FINALLY:
7149 smsg("%s%4d FINALLY", pfx, current);
7150 break;
7151 case ISN_ENDTRY:
7152 smsg("%s%4d ENDTRY", pfx, current);
7153 break;
7154 case ISN_THROW:
7155 smsg("%s%4d THROW", pfx, current);
7156 break;
7157
7158 // expression operations on number
7159 case ISN_OPNR:
7160 case ISN_OPFLOAT:
7161 case ISN_OPANY:
7162 {
7163 char *what;
7164 char *ins;
7165
7166 switch (iptr->isn_arg.op.op_type)
7167 {
7168 case EXPR_MULT: what = "*"; break;
7169 case EXPR_DIV: what = "/"; break;
7170 case EXPR_REM: what = "%"; break;
7171 case EXPR_SUB: what = "-"; break;
7172 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007173 case EXPR_LSHIFT: what = "<<"; break;
7174 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007175 default: what = "???"; break;
7176 }
7177 switch (iptr->isn_type)
7178 {
7179 case ISN_OPNR: ins = "OPNR"; break;
7180 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7181 case ISN_OPANY: ins = "OPANY"; break;
7182 default: ins = "???"; break;
7183 }
7184 smsg("%s%4d %s %s", pfx, current, ins, what);
7185 }
7186 break;
7187
7188 case ISN_COMPAREBOOL:
7189 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007190 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007191 case ISN_COMPARENR:
7192 case ISN_COMPAREFLOAT:
7193 case ISN_COMPARESTRING:
7194 case ISN_COMPAREBLOB:
7195 case ISN_COMPARELIST:
7196 case ISN_COMPAREDICT:
7197 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007198 case ISN_COMPARECLASS:
7199 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007200 case ISN_COMPAREANY:
7201 {
7202 char *p;
7203 char buf[10];
7204 char *type;
7205
7206 switch (iptr->isn_arg.op.op_type)
7207 {
7208 case EXPR_EQUAL: p = "=="; break;
7209 case EXPR_NEQUAL: p = "!="; break;
7210 case EXPR_GREATER: p = ">"; break;
7211 case EXPR_GEQUAL: p = ">="; break;
7212 case EXPR_SMALLER: p = "<"; break;
7213 case EXPR_SEQUAL: p = "<="; break;
7214 case EXPR_MATCH: p = "=~"; break;
7215 case EXPR_IS: p = "is"; break;
7216 case EXPR_ISNOT: p = "isnot"; break;
7217 case EXPR_NOMATCH: p = "!~"; break;
7218 default: p = "???"; break;
7219 }
7220 STRCPY(buf, p);
7221 if (iptr->isn_arg.op.op_ic == TRUE)
7222 strcat(buf, "?");
7223 switch(iptr->isn_type)
7224 {
7225 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7226 case ISN_COMPARESPECIAL:
7227 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007228 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007229 case ISN_COMPARENR: type = "COMPARENR"; break;
7230 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7231 case ISN_COMPARESTRING:
7232 type = "COMPARESTRING"; break;
7233 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7234 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7235 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7236 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007237 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7238 case ISN_COMPAREOBJECT:
7239 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007240 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7241 default: type = "???"; break;
7242 }
7243
7244 smsg("%s%4d %s %s", pfx, current, type, buf);
7245 }
7246 break;
7247
7248 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7249 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7250
7251 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007252 case ISN_CONCAT:
7253 smsg("%s%4d CONCAT size %lld", pfx, current,
7254 (varnumber_T)(iptr->isn_arg.number));
7255 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007256 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7257 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7258 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7259 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7260 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7261 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7262 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7263 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7264 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7265 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7266 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007267 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007268 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7269 iptr->isn_arg.getitem.gi_index,
7270 iptr->isn_arg.getitem.gi_with_op ?
7271 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007272 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7273 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7274 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007275 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7276 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007277 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007278 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007279 pfx, current,
7280 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007281 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007282 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007283 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007284 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007285 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7286 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7287
Bram Moolenaar4c137212021-04-19 16:48:48 +02007288 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7289
Bram Moolenaar4c137212021-04-19 16:48:48 +02007290 case ISN_CHECKTYPE:
7291 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007292 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007293 char *tofree = NULL;
7294 char *typename;
7295
7296 if (ct->ct_type->tt_type == VAR_FLOAT
7297 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7298 typename = "float|number";
7299 else
7300 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007301
7302 if (ct->ct_arg_idx == 0)
7303 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007304 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007305 (int)ct->ct_off);
7306 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007307 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007308 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007309 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007310 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007311 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007312 (int)ct->ct_arg_idx);
7313 vim_free(tofree);
7314 break;
7315 }
7316 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7317 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7318 iptr->isn_arg.checklen.cl_min_len);
7319 break;
7320 case ISN_SETTYPE:
7321 {
7322 char *tofree;
7323
7324 smsg("%s%4d SETTYPE %s", pfx, current,
7325 type_name(iptr->isn_arg.type.ct_type, &tofree));
7326 vim_free(tofree);
7327 break;
7328 }
7329 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007330 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7331 smsg("%s%4d INVERT %d (!val)", pfx, current,
7332 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007333 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007334 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7335 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007336 break;
7337 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007338 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007339 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007340 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7341 pfx, current,
7342 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007343 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007344 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7345 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007346 break;
7347 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007348 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007349 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007350 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007351 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7352 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007353 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007354 else
7355 smsg("%s%4d PUT %c %ld", pfx, current,
7356 iptr->isn_arg.put.put_regname,
7357 (long)iptr->isn_arg.put.put_lnum);
7358 break;
7359
Bram Moolenaar4c137212021-04-19 16:48:48 +02007360 case ISN_CMDMOD:
7361 {
7362 char_u *buf;
7363 size_t len = produce_cmdmods(
7364 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7365
7366 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007367 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007368 {
7369 (void)produce_cmdmods(
7370 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7371 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7372 vim_free(buf);
7373 }
7374 break;
7375 }
7376 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7377
7378 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007379 smsg("%s%4d PROFILE START line %d", pfx, current,
7380 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007381 break;
7382
7383 case ISN_PROF_END:
7384 smsg("%s%4d PROFILE END", pfx, current);
7385 break;
7386
Bram Moolenaare99d4222021-06-13 14:01:26 +02007387 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007388 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7389 iptr->isn_arg.debug.dbg_break_lnum + 1,
7390 iptr->isn_lnum,
7391 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007392 break;
7393
Bram Moolenaar4c137212021-04-19 16:48:48 +02007394 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7395 iptr->isn_arg.unpack.unp_count,
7396 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7397 break;
7398 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7399 iptr->isn_arg.shuffle.shfl_item,
7400 iptr->isn_arg.shuffle.shfl_up);
7401 break;
7402 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7403
7404 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7405 return;
7406 }
7407
7408 out_flush(); // output one line at a time
7409 ui_breakcheck();
7410 if (got_int)
7411 break;
7412 }
7413}
7414
7415/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007416 * Handle command line completion for the :disassemble command.
7417 */
7418 void
7419set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7420{
7421 char_u *p;
7422
7423 // Default: expand user functions, "debug" and "profile"
7424 xp->xp_context = EXPAND_DISASSEMBLE;
7425 xp->xp_pattern = arg;
7426
7427 // first argument already typed: only user function names
7428 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7429 {
7430 xp->xp_context = EXPAND_USER_FUNC;
7431 xp->xp_pattern = skipwhite(p);
7432 }
7433}
7434
7435/*
7436 * Function given to ExpandGeneric() to obtain the list of :disassemble
7437 * arguments.
7438 */
7439 char_u *
7440get_disassemble_argument(expand_T *xp, int idx)
7441{
7442 if (idx == 0)
7443 return (char_u *)"debug";
7444 if (idx == 1)
7445 return (char_u *)"profile";
7446 return get_user_func_name(xp, idx - 2);
7447}
7448
7449/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007450 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007451 * We don't really need this at runtime, but we do have tests that require it,
7452 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 */
7454 void
7455ex_disassemble(exarg_T *eap)
7456{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007457 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007458 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007460 isn_T *instr = NULL; // init to shut up gcc warning
7461 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007462 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007463
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007464 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007465 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007467 if (func_needs_compiling(ufunc, compile_type)
7468 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007469 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007470 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007472 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473 return;
7474 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007475 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476
7477 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007478 switch (compile_type)
7479 {
7480 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007481#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007482 instr = dfunc->df_instr_prof;
7483 instr_count = dfunc->df_instr_prof_count;
7484 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007485#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007486 // FALLTHROUGH
7487 case CT_NONE:
7488 instr = dfunc->df_instr;
7489 instr_count = dfunc->df_instr_count;
7490 break;
7491 case CT_DEBUG:
7492 instr = dfunc->df_instr_debug;
7493 instr_count = dfunc->df_instr_debug_count;
7494 break;
7495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496
Bram Moolenaar4c137212021-04-19 16:48:48 +02007497 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007498}
7499
7500/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007501 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502 * list, etc. Mostly like what JavaScript does, except that empty list and
7503 * empty dictionary are FALSE.
7504 */
7505 int
7506tv2bool(typval_T *tv)
7507{
7508 switch (tv->v_type)
7509 {
7510 case VAR_NUMBER:
7511 return tv->vval.v_number != 0;
7512 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007514 case VAR_PARTIAL:
7515 return tv->vval.v_partial != NULL;
7516 case VAR_FUNC:
7517 case VAR_STRING:
7518 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7519 case VAR_LIST:
7520 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7521 case VAR_DICT:
7522 return tv->vval.v_dict != NULL
7523 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7524 case VAR_BOOL:
7525 case VAR_SPECIAL:
7526 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7527 case VAR_JOB:
7528#ifdef FEAT_JOB_CHANNEL
7529 return tv->vval.v_job != NULL;
7530#else
7531 break;
7532#endif
7533 case VAR_CHANNEL:
7534#ifdef FEAT_JOB_CHANNEL
7535 return tv->vval.v_channel != NULL;
7536#else
7537 break;
7538#endif
7539 case VAR_BLOB:
7540 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7541 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007542 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007543 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007544 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007545 case VAR_CLASS:
7546 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007547 case VAR_TYPEALIAS:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548 break;
7549 }
7550 return FALSE;
7551}
7552
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007553 void
7554emsg_using_string_as(typval_T *tv, int as_number)
7555{
7556 semsg(_(as_number ? e_using_string_as_number_str
7557 : e_using_string_as_bool_str),
7558 tv->vval.v_string == NULL
7559 ? (char_u *)"" : tv->vval.v_string);
7560}
7561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562/*
7563 * If "tv" is a string give an error and return FAIL.
7564 */
7565 int
7566check_not_string(typval_T *tv)
7567{
7568 if (tv->v_type == VAR_STRING)
7569 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007570 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007571 clear_tv(tv);
7572 return FAIL;
7573 }
7574 return OK;
7575}
7576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007577
7578#endif // FEAT_EVAL