blob: dd2dbe0aa5a1dc4c60346787ddfcca4b76132474 [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{
282 if (ufunc->uf_debug_tick != debug_tick)
283 {
284 linenr_T breakpoint;
285
286 ufunc->uf_debug_tick = debug_tick;
287 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
288 ufunc->uf_has_breakpoint = breakpoint > 0;
289 }
290}
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{
386 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
387 {
388 typval_T *argv = STACK_TV_BOT(0) - argcount - off;
389
390 // The function can change at runtime, check that the argument
391 // types are correct.
392 for (int i = 0; i < argcount; ++i)
393 {
394 type_T *type = NULL;
395
396 // assume a v:none argument, using the default value, is always OK
397 if (argv[i].v_type == VAR_SPECIAL
398 && argv[i].vval.v_number == VVAL_NONE)
399 continue;
400
401 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
402 type = ufunc->uf_arg_types[i];
403 else if (ufunc->uf_va_type != NULL)
404 type = ufunc->uf_va_type->tt_member;
405 if (type != NULL && check_typval_arg_type(type,
406 &argv[i], NULL, i + 1) == FAIL)
407 return FAIL;
408 }
409 }
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
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000538 // Check the argument types.
539 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
540 return FAIL;
541
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200542 // Reserve space for:
543 // - missing arguments
544 // - stack frame
545 // - local variables
546 // - if needed: a counter for number of closures created in
547 // ectx->ec_funcrefs.
548 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100549 if (GA_GROW_FAILS(&ectx->ec_stack,
550 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 return FAIL;
552
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100553 // If depth of calling is getting too high, don't execute the function.
554 if (funcdepth_increment() == FAIL)
555 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200556 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100557
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100558 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200559 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100560 {
561 floc = ALLOC_ONE(funclocal_T);
562 if (floc == NULL)
563 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200564 *floc = ectx->ec_funclocal;
565 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100566 }
567
Bram Moolenaarfe270812020-04-11 22:31:27 +0200568 // Move the vararg-list to below the missing optional arguments.
569 if (vararg_count > 0 && arg_to_add > 0)
570 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100571
572 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200573 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200574 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200575 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576
577 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100578 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
579 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200580 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200581 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
582 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100583 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100584 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200585 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586
Bram Moolenaar5800c792022-09-22 17:34:01 +0100587 // Initialize all local variables to number zero. Also initialize the
588 // variable that counts how many closures were created. This is used in
589 // handle_closure_in_use().
590 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
591 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000592 {
593 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
594
595 tv->v_type = VAR_NUMBER;
596 tv->vval.v_number = 0;
597 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200598 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599
Bram Moolenaar574950d2023-01-03 19:08:50 +0000600 // For an object method move the object from just before the arguments to
601 // the first local variable.
602 if (ufunc->uf_flags & FC_OBJECT)
603 {
604 *STACK_TV_VAR(0) = *obj;
605 obj->v_type = VAR_UNKNOWN;
606 }
607
Bram Moolenaar5800c792022-09-22 17:34:01 +0100608 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
609 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100610 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200611 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100612
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200613 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100614 return FAIL;
615 if (pt != NULL)
616 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000617 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200618 ++pt->pt_refcount;
619 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100620 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100621 else
622 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200623 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200624 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200625 {
626 vim_free(ref);
627 return FAIL;
628 }
629 ref->or_outer_allocated = TRUE;
630 ref->or_outer->out_stack = &ectx->ec_stack;
631 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
632 if (ectx->ec_outer_ref != NULL)
633 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100634 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200635 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100636 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100637 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200638 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100639
Bram Moolenaarc970e422021-03-17 15:03:04 +0100640 ++ufunc->uf_calls;
641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 // Set execution state to the start of the called function.
643 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100644 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100645 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200646 if (entry != NULL)
647 {
648 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200649 // Save the current context so it can be restored on return.
650 entry->es_save_sctx = current_sctx;
651 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200652 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100653
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200654 // Start execution at the first instruction.
655 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656
657 return OK;
658}
659
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000660// Double linked list of funcstack_T in use.
661static funcstack_T *first_funcstack = NULL;
662
663 static void
664add_funcstack_to_list(funcstack_T *funcstack)
665{
666 // Link in list of funcstacks.
667 if (first_funcstack != NULL)
668 first_funcstack->fs_prev = funcstack;
669 funcstack->fs_next = first_funcstack;
670 funcstack->fs_prev = NULL;
671 first_funcstack = funcstack;
672}
673
674 static void
675remove_funcstack_from_list(funcstack_T *funcstack)
676{
677 if (funcstack->fs_prev == NULL)
678 first_funcstack = funcstack->fs_next;
679 else
680 funcstack->fs_prev->fs_next = funcstack->fs_next;
681 if (funcstack->fs_next != NULL)
682 funcstack->fs_next->fs_prev = funcstack->fs_prev;
683}
684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200686 * Used when returning from a function: Check if any closure is still
687 * referenced. If so then move the arguments and variables to a separate piece
688 * of stack to be used when the closure is called.
689 * When "free_arguments" is TRUE the arguments are to be freed.
690 * Returns FAIL when out of memory.
691 */
692 static int
693handle_closure_in_use(ectx_T *ectx, int free_arguments)
694{
695 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
696 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200697 int argcount;
698 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200699 int idx;
700 typval_T *tv;
701 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200702 garray_T *gap = &ectx->ec_funcrefs;
703 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200704
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200705 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200706 return OK; // function was freed
707 if (dfunc->df_has_closure == 0)
708 return OK; // no closures
709 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
710 closure_count = tv->vval.v_number;
711 if (closure_count == 0)
712 return OK; // no funcrefs created
713
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100714 // Compute "top": the first entry in the stack used by the function.
715 // This is the first argument (after that comes the stack frame and then
716 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200717 argcount = ufunc_argcount(dfunc->df_ufunc);
718 top = ectx->ec_frame_idx - argcount;
719
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200721 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200722 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200723 partial_T *pt;
724 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200725
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200726 if (off < 0)
727 continue; // count is off or already done
728 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200729 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200730 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200731 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200732 int i;
733
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000734 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200735 // unreferenced on return.
736 for (i = 0; i < dfunc->df_varcount; ++i)
737 {
738 typval_T *stv = STACK_TV(ectx->ec_frame_idx
739 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200740 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200741 --refcount;
742 }
743 if (refcount > 1)
744 {
745 closure_in_use = TRUE;
746 break;
747 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200748 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200749 }
750
751 if (closure_in_use)
752 {
753 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
754 typval_T *stack;
755
756 // A closure is using the arguments and/or local variables.
757 // Move them to the called function.
758 if (funcstack == NULL)
759 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000760
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200761 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100762 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
763 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200764 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
765 funcstack->fs_ga.ga_data = stack;
766 if (stack == NULL)
767 {
768 vim_free(funcstack);
769 return FAIL;
770 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000771 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200772
773 // Move or copy the arguments.
774 for (idx = 0; idx < argcount; ++idx)
775 {
776 tv = STACK_TV(top + idx);
777 if (free_arguments)
778 {
779 *(stack + idx) = *tv;
780 tv->v_type = VAR_UNKNOWN;
781 }
782 else
783 copy_tv(tv, stack + idx);
784 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100785 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200786 // Move the local variables.
787 for (idx = 0; idx < dfunc->df_varcount; ++idx)
788 {
789 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200790
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200791 // A partial created for a local function, that is also used as a
792 // local variable, has a reference count for the variable, thus
793 // will never go down to zero. When all these refcounts are one
794 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000795 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200796 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
797 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200798 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200799
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200800 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200801 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
802 gap->ga_len - closure_count + i])
803 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200804 }
805
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200806 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200807 tv->v_type = VAR_UNKNOWN;
808 }
809
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200810 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200811 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200812 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
813 - closure_count + idx];
814 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200815 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200816 ++funcstack->fs_refcount;
817 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100818 pt->pt_outer.out_stack = &funcstack->fs_ga;
819 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200820 }
821 }
822 }
823
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200824 for (idx = 0; idx < closure_count; ++idx)
825 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
826 - closure_count + idx]);
827 gap->ga_len -= closure_count;
828 if (gap->ga_len == 0)
829 ga_clear(gap);
830
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200831 return OK;
832}
833
834/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200835 * Called when a partial is freed or its reference count goes down to one. The
836 * funcstack may be the only reference to the partials in the local variables.
837 * Go over all of them, the funcref and can be freed if all partials
838 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100839 * Returns TRUE if the funcstack is freed, the partial referencing it will then
840 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200841 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100842 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200843funcstack_check_refcount(funcstack_T *funcstack)
844{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100845 int i;
846 garray_T *gap = &funcstack->fs_ga;
847 int done = 0;
848 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200849
850 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100851 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200852 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
853 {
854 typval_T *tv = ((typval_T *)gap->ga_data) + i;
855
856 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
857 && tv->vval.v_partial->pt_funcstack == funcstack
858 && tv->vval.v_partial->pt_refcount == 1)
859 ++done;
860 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100861 if (done != funcstack->fs_min_refcount)
862 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200863
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100864 stack = gap->ga_data;
865
866 // All partials referencing the funcstack have a reference count of
867 // one, thus the funcstack is no longer of use.
868 for (i = 0; i < gap->ga_len; ++i)
869 clear_tv(stack + i);
870 vim_free(stack);
871 remove_funcstack_from_list(funcstack);
872 vim_free(funcstack);
873
874 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200875}
876
877/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000878 * For garbage collecting: set references in all variables referenced by
879 * all funcstacks.
880 */
881 int
882set_ref_in_funcstacks(int copyID)
883{
884 funcstack_T *funcstack;
885
886 for (funcstack = first_funcstack; funcstack != NULL;
887 funcstack = funcstack->fs_next)
888 {
889 typval_T *stack = funcstack->fs_ga.ga_data;
890 int i;
891
892 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
893 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
894 return TRUE; // abort
895 }
896 return FALSE;
897}
898
Bram Moolenaar806a2732022-09-04 15:40:36 +0100899// Ugly static to avoid passing the execution context around through many
900// layers.
901static ectx_T *current_ectx = NULL;
902
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000903/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100904 * Return TRUE if currently executing a :def function.
905 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100906 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100907 int
908in_def_function(void)
909{
910 return current_ectx != NULL;
911}
912
913/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100914 * Clear "current_ectx" and return the previous value. To be used when calling
915 * a user function.
916 */
917 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000918clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100919{
920 ectx_T *r = current_ectx;
921
922 current_ectx = NULL;
923 return r;
924}
925
926 void
927restore_current_ectx(ectx_T *ectx)
928{
929 if (current_ectx != NULL)
930 iemsg("Restoring current_ectx while it is not NULL");
931 current_ectx = ectx;
932}
933
934/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100935 * Add an entry for a deferred function call to the currently executing
936 * function.
937 * Return the list or NULL when failed.
938 */
939 static list_T *
940add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100941{
942 typval_T *defer_tv = STACK_TV_VAR(var_idx);
943 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100944 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100945 typval_T listval;
946
947 if (defer_tv->v_type != VAR_LIST)
948 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100949 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100950 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100951 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100952 }
953 defer_l = defer_tv->vval.v_list;
954
955 l = list_alloc_with_items(argcount + 1);
956 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100957 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100958 listval.v_type = VAR_LIST;
959 listval.vval.v_list = l;
960 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100961 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100962 {
963 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100964 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100965 }
966
Bram Moolenaar806a2732022-09-04 15:40:36 +0100967 return l;
968}
969
970/*
971 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
972 * The local variable that lists deferred functions is "var_idx".
973 * Returns OK or FAIL.
974 */
975 static int
976defer_command(int var_idx, int argcount, ectx_T *ectx)
977{
978 list_T *l = add_defer_item(var_idx, argcount, ectx);
979 int i;
980 typval_T *func_tv;
981
982 if (l == NULL)
983 return FAIL;
984
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100985 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100986 if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL)
987 {
988 semsg(_(e_expected_str_but_got_str),
989 "function or partial",
990 vartype_name(func_tv->v_type));
991 return FAIL;
992 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100993 list_set_item(l, 0, func_tv);
994
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100995 for (i = 0; i < argcount; ++i)
996 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100997 ectx->ec_stack.ga_len -= argcount + 1;
998 return OK;
999}
1000
1001/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01001002 * Add a deferred function "name" with one argument "arg_tv".
1003 * Consumes "name", also on failure.
1004 * Only to be called when in_def_function() returns TRUE.
1005 */
1006 int
1007add_defer_function(char_u *name, int argcount, typval_T *argvars)
1008{
1009 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1010 + current_ectx->ec_dfunc_idx;
1011 list_T *l;
1012 typval_T func_tv;
1013 int i;
1014
1015 if (dfunc->df_defer_var_idx == 0)
1016 {
1017 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001018 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001019 return FAIL;
1020 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001021
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001022 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001023 if (l == NULL)
1024 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001025 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001026 return FAIL;
1027 }
1028
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001029 func_tv.v_type = VAR_FUNC;
1030 func_tv.v_lock = 0;
1031 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001032 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001033
Bram Moolenaar806a2732022-09-04 15:40:36 +01001034 for (i = 0; i < argcount; ++i)
1035 list_set_item(l, i + 1, argvars + i);
1036 return OK;
1037}
1038
1039/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001040 * Invoked when returning from a function: Invoke any deferred calls.
1041 */
1042 static void
1043invoke_defer_funcs(ectx_T *ectx)
1044{
1045 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1046 + ectx->ec_dfunc_idx;
1047 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1048 listitem_T *li;
1049
1050 if (defer_tv->v_type != VAR_LIST)
1051 return; // no function added
1052 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1053 {
1054 list_T *l = li->li_tv.vval.v_list;
1055 typval_T rettv;
1056 typval_T argvars[MAX_FUNC_ARGS];
1057 int i;
1058 listitem_T *arg_li = l->lv_first;
1059 funcexe_T funcexe;
1060
1061 for (i = 0; i < l->lv_len - 1; ++i)
1062 {
1063 arg_li = arg_li->li_next;
1064 argvars[i] = arg_li->li_tv;
1065 }
1066
1067 CLEAR_FIELD(funcexe);
1068 funcexe.fe_evaluate = TRUE;
1069 rettv.v_type = VAR_UNKNOWN;
1070 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1071 &rettv, l->lv_len - 1, argvars, &funcexe);
1072 clear_tv(&rettv);
1073 }
1074}
1075
1076/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 * Return from the current function.
1078 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001079 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001080func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001083 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001084 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1085 + ectx->ec_dfunc_idx;
1086 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001087 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001088 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1089 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001090 funclocal_T *floc;
1091#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001092 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1093 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094
Bram Moolenaar12d26532021-02-19 19:13:21 +01001095 if (do_profiling == PROF_YES)
1096 {
1097 ufunc_T *caller = prev_dfunc->df_ufunc;
1098
1099 if (dfunc->df_ufunc->uf_profiling
1100 || (caller != NULL && caller->uf_profiling))
1101 {
1102 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1103 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1104 --profile_info_ga.ga_len;
1105 }
1106 }
1107#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001108
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001109 if (dfunc->df_defer_var_idx > 0)
1110 invoke_defer_funcs(ectx);
1111
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001112 // No check for uf_refcount being zero, cannot think of a way that would
1113 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001114 --dfunc->df_ufunc->uf_calls;
1115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001117 entry = estack_pop();
1118 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001119 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001121 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1122 return FAIL;
1123
Bram Moolenaar574950d2023-01-03 19:08:50 +00001124 // Clear the arguments. If this was an object method also clear the
1125 // object, it is just before the arguments.
1126 int top = ectx->ec_frame_idx - argcount;
1127 if (dfunc->df_ufunc->uf_flags & FC_OBJECT)
1128 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001129 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1130 clear_tv(STACK_TV(idx));
1131
1132 // Clear local variables and temp values, but not the return value.
1133 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001134 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001136
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001137 // The return value should be on top of the stack. However, when aborting
1138 // it may not be there and ec_frame_idx is the top of the stack.
1139 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001140 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001141 ret_idx = 0;
1142
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001143 if (ectx->ec_outer_ref != NULL)
1144 {
1145 if (ectx->ec_outer_ref->or_outer_allocated)
1146 vim_free(ectx->ec_outer_ref->or_outer);
1147 partial_unref(ectx->ec_outer_ref->or_partial);
1148 vim_free(ectx->ec_outer_ref);
1149 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001150
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001151 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001152 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001153 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1154 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001155 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1156 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001157 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001158 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001159 floc = (void *)STACK_TV(ectx->ec_frame_idx
1160 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001161 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001162 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1163 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001164
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001165 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001166 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001167 else
1168 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001169 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001170 vim_free(floc);
1171 }
1172
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001173 if (ret_idx > 0)
1174 {
1175 // Reset the stack to the position before the call, with a spot for the
1176 // return value, moved there from above the frame.
1177 ectx->ec_stack.ga_len = top + 1;
1178 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1179 }
1180 else
1181 // Reset the stack to the position before the call.
1182 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001183
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001184 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001185 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001186 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187}
1188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189/*
1190 * Prepare arguments and rettv for calling a builtin or user function.
1191 */
1192 static int
1193call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1194{
1195 int idx;
1196 typval_T *tv;
1197
1198 // Move arguments from bottom of the stack to argvars[] and add terminator.
1199 for (idx = 0; idx < argcount; ++idx)
1200 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1201 argvars[argcount].v_type = VAR_UNKNOWN;
1202
1203 // Result replaces the arguments on the stack.
1204 if (argcount > 0)
1205 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001206 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 return FAIL;
1208 else
1209 ++ectx->ec_stack.ga_len;
1210
1211 // Default return value is zero.
1212 tv = STACK_TV_BOT(-1);
1213 tv->v_type = VAR_NUMBER;
1214 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001215 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216
1217 return OK;
1218}
1219
1220/*
1221 * Call a builtin function by index.
1222 */
1223 static int
1224call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1225{
1226 typval_T argvars[MAX_FUNC_ARGS];
1227 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001228 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001229 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001230 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231
1232 if (call_prepare(argcount, argvars, ectx) == FAIL)
1233 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001234 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001236 // Call the builtin function. Set "current_ectx" so that when it
1237 // recursively invokes call_def_function() a closure context can be set.
1238 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001240 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001241 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242
1243 // Clear the arguments.
1244 for (idx = 0; idx < argcount; ++idx)
1245 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001246
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001247 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001248 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 return OK;
1250}
1251
1252/*
1253 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001254 * If the function is compiled this will add a stack frame and set the
1255 * instruction pointer at the start of the function.
1256 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001257 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001258 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 */
1260 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001261call_ufunc(
1262 ufunc_T *ufunc,
1263 partial_T *pt,
1264 int argcount,
1265 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001266 isn_T *iptr,
1267 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268{
1269 typval_T argvars[MAX_FUNC_ARGS];
1270 funcexe_T funcexe;
1271 int error;
1272 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001273 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001274 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275
Bram Moolenaare99d4222021-06-13 14:01:26 +02001276 if (func_needs_compiling(ufunc, compile_type)
1277 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1278 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001279 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001280 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001281 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001282 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001283 if (error != FCERR_UNKNOWN)
1284 {
1285 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001286 semsg(_(e_too_many_arguments_for_function_str),
1287 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001288 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001289 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001290 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001291 return FAIL;
1292 }
1293
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001294 // The function has been compiled, can call it quickly. For a function
1295 // that was defined later: we can call it directly next time.
1296 if (iptr != NULL)
1297 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001298 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001299 iptr->isn_type = ISN_DCALL;
1300 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1301 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1302 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001303 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305
1306 if (call_prepare(argcount, argvars, ectx) == FAIL)
1307 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001308 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001309 funcexe.fe_evaluate = TRUE;
1310 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001311
1312 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001314 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315
1316 // Clear the arguments.
1317 for (idx = 0; idx < argcount; ++idx)
1318 clear_tv(&argvars[idx]);
1319
1320 if (error != FCERR_NONE)
1321 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001322 user_func_error(error, printable_func_name(ufunc),
1323 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 return FAIL;
1325 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001326 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001327 // Error other than from calling the function itself.
1328 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329 return OK;
1330}
1331
1332/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001333 * If command modifiers were applied restore them.
1334 */
1335 static void
1336may_restore_cmdmod(funclocal_T *funclocal)
1337{
1338 if (funclocal->floc_restore_cmdmod)
1339 {
1340 cmdmod.cmod_filter_regmatch.regprog = NULL;
1341 undo_cmdmod(&cmdmod);
1342 cmdmod = funclocal->floc_save_cmdmod;
1343 funclocal->floc_restore_cmdmod = FALSE;
1344 }
1345}
1346
1347/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001348 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1349 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001350 */
1351 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001352vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001353{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001354 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001355}
1356
1357/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 * Execute a function by "name".
1359 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001360 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 * Returns FAIL if not found without an error message.
1362 */
1363 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001364call_by_name(
1365 char_u *name,
1366 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001367 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001368 isn_T *iptr,
1369 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370{
1371 ufunc_T *ufunc;
1372
1373 if (builtin_function(name, -1))
1374 {
1375 int func_idx = find_internal_func(name);
1376
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001377 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001379 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 return FAIL;
1381 return call_bfunc(func_idx, argcount, ectx);
1382 }
1383
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001384 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001385
1386 if (ufunc == NULL)
1387 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001388 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001389
1390 if (script_autoload(name, TRUE))
1391 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001392 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001393
1394 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001395 return FAIL; // bail out if loading the script caused an error
1396 }
1397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001399 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001400 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1401 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001402
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001403 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001404 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405
1406 return FAIL;
1407}
1408
1409 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001410call_partial(
1411 typval_T *tv,
1412 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001413 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001415 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001416 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001418 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001419 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420
1421 if (tv->v_type == VAR_PARTIAL)
1422 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001423 partial_T *pt = tv->vval.v_partial;
1424 int i;
1425
1426 if (pt->pt_argc > 0)
1427 {
1428 // Make space for arguments from the partial, shift the "argcount"
1429 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001430 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001431 return FAIL;
1432 for (i = 1; i <= argcount; ++i)
1433 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1434 ectx->ec_stack.ga_len += pt->pt_argc;
1435 argcount += pt->pt_argc;
1436
1437 // copy the arguments from the partial onto the stack
1438 for (i = 0; i < pt->pt_argc; ++i)
1439 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1440 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001441 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442
1443 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001444 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 name = pt->pt_name;
1447 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001448 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001450 if (name != NULL)
1451 {
1452 char_u fname_buf[FLEN_FIXED + 1];
1453 char_u *tofree = NULL;
1454 int error = FCERR_NONE;
1455 char_u *fname;
1456
1457 // May need to translate <SNR>123_ to K_SNR.
1458 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1459 if (error != FCERR_NONE)
1460 res = FAIL;
1461 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001462 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001463 vim_free(tofree);
1464 }
1465
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001466 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 {
1468 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001469 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001470 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 return FAIL;
1472 }
1473 return OK;
1474}
1475
1476/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001477 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1478 * TRUE.
1479 */
1480 static int
1481error_if_locked(int lock, char *error)
1482{
1483 if (lock & (VAR_LOCKED | VAR_FIXED))
1484 {
1485 emsg(_(error));
1486 return TRUE;
1487 }
1488 return FALSE;
1489}
1490
1491/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001492 * Give an error if "tv" is not a number and return FAIL.
1493 */
1494 static int
1495check_for_number(typval_T *tv)
1496{
1497 if (tv->v_type != VAR_NUMBER)
1498 {
1499 semsg(_(e_expected_str_but_got_str),
1500 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1501 return FAIL;
1502 }
1503 return OK;
1504}
1505
1506/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001507 * Store "tv" in variable "name".
1508 * This is for s: and g: variables.
1509 */
1510 static void
1511store_var(char_u *name, typval_T *tv)
1512{
1513 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001514 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001515
Bram Moolenaard877a572021-04-01 19:42:48 +02001516 if (tv->v_lock)
1517 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001518 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001519 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001520 restore_funccal();
1521}
1522
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001523/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001524 * Convert "tv" to a string.
1525 * Return FAIL if not allowed.
1526 */
1527 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001528do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001529{
1530 if (tv->v_type != VAR_STRING)
1531 {
1532 char_u *str;
1533
1534 if (is_2string_any)
1535 {
1536 switch (tv->v_type)
1537 {
1538 case VAR_SPECIAL:
1539 case VAR_BOOL:
1540 case VAR_NUMBER:
1541 case VAR_FLOAT:
1542 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001543
1544 case VAR_LIST:
1545 if (tolerant)
1546 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001547 char_u *s, *e, *p;
1548 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001549
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001550 ga_init2(&ga, sizeof(char_u *), 1);
1551
1552 // Convert to NL separated items, then
1553 // escape the items and replace the NL with
1554 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001555 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001556 if (str == NULL)
1557 return FAIL;
1558 s = str;
1559 while ((e = vim_strchr(s, '\n')) != NULL)
1560 {
1561 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001562 p = vim_strsave_fnameescape(s,
1563 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001564 if (p != NULL)
1565 {
1566 ga_concat(&ga, p);
1567 ga_concat(&ga, (char_u *)" ");
1568 vim_free(p);
1569 }
1570 s = e + 1;
1571 }
1572 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001573 clear_tv(tv);
1574 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001575 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001576 return OK;
1577 }
1578 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001579 default: to_string_error(tv->v_type);
1580 return FAIL;
1581 }
1582 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001583 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001584 clear_tv(tv);
1585 tv->v_type = VAR_STRING;
1586 tv->vval.v_string = str;
1587 }
1588 return OK;
1589}
1590
1591/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001592 * When the value of "sv" is a null list of dict, allocate it.
1593 */
1594 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001595allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001596{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001597 typval_T *tv = sv->sv_tv;
1598
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001599 switch (tv->v_type)
1600 {
1601 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001602 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001603 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001604 break;
1605 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001606 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001607 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001608 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001609 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001610 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001611 (void)rettv_blob_alloc(tv);
1612 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001613 default:
1614 break;
1615 }
1616}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001617
Bram Moolenaare7525c52021-01-09 13:20:37 +01001618/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001619 * Return the character "str[index]" where "index" is the character index,
1620 * including composing characters.
1621 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001622 */
1623 char_u *
1624char_from_string(char_u *str, varnumber_T index)
1625{
1626 size_t nbyte = 0;
1627 varnumber_T nchar = index;
1628 size_t slen;
1629
1630 if (str == NULL)
1631 return NULL;
1632 slen = STRLEN(str);
1633
Bram Moolenaarff871402021-03-26 13:34:05 +01001634 // Do the same as for a list: a negative index counts from the end.
1635 // Optimization to check the first byte to be below 0x80 (and no composing
1636 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001637 if (index < 0)
1638 {
1639 int clen = 0;
1640
1641 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001642 {
1643 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1644 ++nbyte;
1645 else if (enc_utf8)
1646 nbyte += utfc_ptr2len(str + nbyte);
1647 else
1648 nbyte += mb_ptr2len(str + nbyte);
1649 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001650 nchar = clen + index;
1651 if (nchar < 0)
1652 // unlike list: index out of range results in empty string
1653 return NULL;
1654 }
1655
1656 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001657 {
1658 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1659 ++nbyte;
1660 else if (enc_utf8)
1661 nbyte += utfc_ptr2len(str + nbyte);
1662 else
1663 nbyte += mb_ptr2len(str + nbyte);
1664 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001665 if (nbyte >= slen)
1666 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001667 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001668}
1669
1670/*
1671 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001672 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001673 * If going over the end return "str_len".
1674 * If "idx" is negative count from the end, -1 is the last character.
1675 * When going over the start return -1.
1676 */
1677 static long
1678char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1679{
1680 varnumber_T nchar = idx;
1681 size_t nbyte = 0;
1682
1683 if (nchar >= 0)
1684 {
1685 while (nchar > 0 && nbyte < str_len)
1686 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001687 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001688 --nchar;
1689 }
1690 }
1691 else
1692 {
1693 nbyte = str_len;
1694 while (nchar < 0 && nbyte > 0)
1695 {
1696 --nbyte;
1697 nbyte -= mb_head_off(str, str + nbyte);
1698 ++nchar;
1699 }
1700 if (nchar < 0)
1701 return -1;
1702 }
1703 return (long)nbyte;
1704}
1705
1706/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001707 * Return the slice "str[first : last]" using character indexes. Composing
1708 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001709 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001710 * Return NULL when the result is empty.
1711 */
1712 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001713string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001714{
1715 long start_byte, end_byte;
1716 size_t slen;
1717
1718 if (str == NULL)
1719 return NULL;
1720 slen = STRLEN(str);
1721 start_byte = char_idx2byte(str, slen, first);
1722 if (start_byte < 0)
1723 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001724 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001725 end_byte = (long)slen;
1726 else
1727 {
1728 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001729 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001730 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001731 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001732 }
1733
1734 if (start_byte >= (long)slen || end_byte <= start_byte)
1735 return NULL;
1736 return vim_strnsave(str + start_byte, end_byte - start_byte);
1737}
1738
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001739/*
1740 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1741 * When "dfunc_idx" is negative don't give an error.
1742 * Returns NULL for an error.
1743 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001744 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001745get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001746{
1747 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001748 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1749 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001750 svar_T *sv;
1751
1752 if (sref->sref_seq != si->sn_script_seq)
1753 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001754 // The script was reloaded after the function was compiled, the
1755 // script_idx may not be valid.
1756 if (dfunc != NULL)
1757 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1758 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001759 return NULL;
1760 }
1761 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001762 if (sv->sv_name == NULL)
1763 {
1764 if (dfunc != NULL)
1765 emsg(_(e_script_variable_was_deleted));
1766 return NULL;
1767 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001768 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001769 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001770 if (dfunc != NULL)
1771 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001772 return NULL;
1773 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001774
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001775 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1776 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001777 {
1778 if (dfunc != NULL)
1779 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1780 return NULL;
1781 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001782 return sv;
1783}
1784
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001785/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001786 * Function passed to do_cmdline() for splitting a script joined by NL
1787 * characters.
1788 */
1789 static char_u *
1790get_split_sourceline(
1791 int c UNUSED,
1792 void *cookie,
1793 int indent UNUSED,
1794 getline_opt_T options UNUSED)
1795{
1796 source_cookie_T *sp = (source_cookie_T *)cookie;
1797 char_u *p;
1798 char_u *line;
1799
Bram Moolenaar20677332021-06-06 17:02:53 +02001800 p = vim_strchr(sp->nextline, '\n');
1801 if (p == NULL)
1802 {
1803 line = vim_strsave(sp->nextline);
1804 sp->nextline += STRLEN(sp->nextline);
1805 }
1806 else
1807 {
1808 line = vim_strnsave(sp->nextline, p - sp->nextline);
1809 sp->nextline = p + 1;
1810 }
1811 return line;
1812}
1813
1814/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 * Execute a function by "name".
1816 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001817 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 */
1819 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001820call_eval_func(
1821 char_u *name,
1822 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001823 ectx_T *ectx,
1824 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825{
Bram Moolenaared677f52020-08-12 16:38:10 +02001826 int called_emsg_before = called_emsg;
1827 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001829 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001830 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001832 dictitem_T *v;
1833
1834 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001835 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1836 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001837 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001838 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001839 return FAIL;
1840 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001841 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001843 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844}
1845
1846/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001847 * When a function reference is used, fill a partial with the information
1848 * needed, especially when it is used as a closure.
1849 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001850 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001851fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001852 partial_T *pt,
1853 ufunc_T *ufunc,
1854 loopvarinfo_T *lvi,
1855 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001856{
1857 pt->pt_func = ufunc;
1858 pt->pt_refcount = 1;
1859
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001860 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001861 {
1862 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1863 + ectx->ec_dfunc_idx;
1864
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001865 // The closure may need to find arguments and local variables of the
1866 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001867 pt->pt_outer.out_stack = &ectx->ec_stack;
1868 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001869 if (ectx->ec_outer_ref != NULL)
1870 {
1871 // The current context already has a context, link to that one.
1872 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1873 if (ectx->ec_outer_ref->or_partial != NULL)
1874 {
1875 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1876 ++pt->pt_outer.out_up_partial->pt_refcount;
1877 }
1878 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001879
Bram Moolenaarcc341812022-09-19 15:54:34 +01001880 if (lvi != NULL)
1881 {
1882 int depth;
1883
1884 // The closure may need to find variables defined inside a loop,
1885 // for every nested loop. A new reference is made every time,
1886 // ISN_ENDLOOP will check if they are actually used.
1887 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1888 {
1889 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1890 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1891 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1892 pt->pt_outer.out_loop[depth].var_count =
1893 lvi->lvi_loop[depth].var_count;
1894 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001895 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001896 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001897 else
1898 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001899
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001900 // If the function currently executing returns and the closure is still
1901 // being referenced, we need to make a copy of the context (arguments
1902 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001903 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001904 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001905 {
1906 vim_free(pt);
1907 return FAIL;
1908 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001909 // Extra variable keeps the count of closures created in the current
1910 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001911 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1912 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1913
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001914 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1915 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001916 ++pt->pt_refcount;
1917 ++ectx->ec_funcrefs.ga_len;
1918 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001919 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001920 return OK;
1921}
1922
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001923/*
1924 * Execute iptr->isn_arg.string as an Ex command.
1925 */
1926 static int
1927exec_command(isn_T *iptr)
1928{
1929 source_cookie_T cookie;
1930
1931 SOURCING_LNUM = iptr->isn_lnum;
1932 // Pass getsourceline to get an error for a missing ":end"
1933 // command.
1934 CLEAR_FIELD(cookie);
1935 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1936 if (do_cmdline(iptr->isn_arg.string,
1937 getsourceline, &cookie,
1938 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1939 || did_emsg)
1940 return FAIL;
1941 return OK;
1942}
1943
Bram Moolenaar89445512022-04-14 12:58:23 +01001944/*
1945 * If script "sid" is not loaded yet then load it now.
1946 * Caller must make sure "sid" is a valid script ID.
1947 * "loaded" is set to TRUE if the script had to be loaded.
1948 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1949 */
1950 int
1951may_load_script(int sid, int *loaded)
1952{
1953 scriptitem_T *si = SCRIPT_ITEM(sid);
1954
1955 if (si->sn_state == SN_STATE_NOT_LOADED)
1956 {
1957 *loaded = TRUE;
1958 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1959 {
1960 semsg(_(e_cant_open_file_str), si->sn_name);
1961 return FAIL;
1962 }
1963 }
1964 return OK;
1965}
1966
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001967// used for v_instr of typval of VAR_INSTR
1968struct instr_S {
1969 ectx_T *instr_ectx;
1970 isn_T *instr_instr;
1971};
1972
Bram Moolenaar4c137212021-04-19 16:48:48 +02001973// used for substitute_instr
1974typedef struct subs_expr_S {
1975 ectx_T *subs_ectx;
1976 isn_T *subs_instr;
1977 int subs_status;
1978} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001980// Set when calling do_debug().
1981static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001982static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001983
1984/*
1985 * When debugging lookup "name" and return the typeval.
1986 * When not found return NULL.
1987 */
1988 typval_T *
1989lookup_debug_var(char_u *name)
1990{
1991 int idx;
1992 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001993 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001994 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001995 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001996
1997 if (ectx == NULL)
1998 return NULL;
1999 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2000
2001 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002002 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002003 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002004 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2005
2006 // the variable name may be NULL when not available in this block
2007 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002008 return STACK_TV_VAR(idx);
2009 }
2010
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002011 // Go through argument names.
2012 ufunc = dfunc->df_ufunc;
2013 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2014 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2015 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2016 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2017 - varargs_off + idx);
2018 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2019 return STACK_TV(ectx->ec_frame_idx - 1);
2020
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002021 return NULL;
2022}
2023
Bram Moolenaar26a44842021-09-02 18:49:06 +02002024/*
2025 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2026 * breakpoint was set in that function or when there is any expression.
2027 */
2028 int
2029may_break_in_function(ufunc_T *ufunc)
2030{
2031 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2032}
2033
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002034 static void
2035handle_debug(isn_T *iptr, ectx_T *ectx)
2036{
2037 char_u *line;
2038 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2039 + ectx->ec_dfunc_idx)->df_ufunc;
2040 isn_T *ni;
2041 int end_lnum = iptr->isn_lnum;
2042 garray_T ga;
2043 int lnum;
2044
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002045 if (ex_nesting_level > debug_break_level)
2046 {
2047 linenr_T breakpoint;
2048
Bram Moolenaar26a44842021-09-02 18:49:06 +02002049 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002050 return;
2051
2052 // check for the next breakpoint if needed
2053 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002054 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002055 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2056 return;
2057 }
2058
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002059 SOURCING_LNUM = iptr->isn_lnum;
2060 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002061 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002062
2063 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2064 if (ni->isn_type == ISN_DEBUG
2065 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002066 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002067 || ni->isn_type == ISN_RETURN_VOID)
2068 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002069 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002070 break;
2071 }
2072
2073 if (end_lnum > iptr->isn_lnum)
2074 {
2075 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002076 for (lnum = iptr->isn_lnum; lnum < end_lnum
2077 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002078 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002079 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002080
Bram Moolenaar303215d2021-07-07 20:10:43 +02002081 if (p == NULL)
2082 continue; // left over from continuation line
2083 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002084 if (*p == '#')
2085 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002086 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002087 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2088 if (STRNCMP(p, "def ", 4) == 0)
2089 break;
2090 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002091 line = ga_concat_strings(&ga, " ");
2092 vim_free(ga.ga_data);
2093 }
2094 else
2095 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002096
Dominique Pelle6e969552021-06-17 13:53:41 +02002097 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002098 debug_context = NULL;
2099
2100 if (end_lnum > iptr->isn_lnum)
2101 vim_free(line);
2102}
2103
Bram Moolenaar4c137212021-04-19 16:48:48 +02002104/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002105 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002106 * Returns OK, FAIL or NOTDONE (uncatchable error).
2107 */
2108 static int
2109execute_storeindex(isn_T *iptr, ectx_T *ectx)
2110{
2111 vartype_T dest_type = iptr->isn_arg.vartype;
2112 typval_T *tv;
2113 typval_T *tv_idx = STACK_TV_BOT(-2);
2114 typval_T *tv_dest = STACK_TV_BOT(-1);
2115 int status = OK;
2116
2117 // Stack contains:
2118 // -3 value to be stored
2119 // -2 index
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002120 // -1 dict, list, blob or object
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002121 tv = STACK_TV_BOT(-3);
2122 SOURCING_LNUM = iptr->isn_lnum;
2123 if (dest_type == VAR_ANY)
2124 {
2125 dest_type = tv_dest->v_type;
2126 if (dest_type == VAR_DICT)
2127 status = do_2string(tv_idx, TRUE, FALSE);
2128 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2129 {
2130 emsg(_(e_number_expected));
2131 status = FAIL;
2132 }
2133 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002134
2135 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002136 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002137 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002138 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002139 long lidx = (long)tv_idx->vval.v_number;
2140 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002141
Bram Moolenaare08be092022-02-17 13:08:26 +00002142 if (list == NULL)
2143 {
2144 emsg(_(e_list_not_set));
2145 return FAIL;
2146 }
2147 if (lidx < 0 && list->lv_len + lidx >= 0)
2148 // negative index is relative to the end
2149 lidx = list->lv_len + lidx;
2150 if (lidx < 0 || lidx > list->lv_len)
2151 {
2152 semsg(_(e_list_index_out_of_range_nr), lidx);
2153 return FAIL;
2154 }
2155 if (lidx < list->lv_len)
2156 {
2157 listitem_T *li = list_find(list, lidx);
2158
2159 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002160 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002161 return FAIL;
2162 // overwrite existing list item
2163 clear_tv(&li->li_tv);
2164 li->li_tv = *tv;
2165 }
2166 else
2167 {
2168 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2169 return FAIL;
2170 // append to list, only fails when out of memory
2171 if (list_append_tv(list, tv) == FAIL)
2172 return NOTDONE;
2173 clear_tv(tv);
2174 }
2175 }
2176 else if (dest_type == VAR_DICT)
2177 {
2178 char_u *key = tv_idx->vval.v_string;
2179 dict_T *dict = tv_dest->vval.v_dict;
2180 dictitem_T *di;
2181
2182 SOURCING_LNUM = iptr->isn_lnum;
2183 if (dict == NULL)
2184 {
2185 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002186 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002187 }
2188 if (key == NULL)
2189 key = (char_u *)"";
2190 di = dict_find(dict, key, -1);
2191 if (di != NULL)
2192 {
2193 if (error_if_locked(di->di_tv.v_lock,
2194 e_cannot_change_dict_item))
2195 return FAIL;
2196 // overwrite existing value
2197 clear_tv(&di->di_tv);
2198 di->di_tv = *tv;
2199 }
2200 else
2201 {
2202 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2203 return FAIL;
2204 // add to dict, only fails when out of memory
2205 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2206 return NOTDONE;
2207 clear_tv(tv);
2208 }
2209 }
2210 else if (dest_type == VAR_BLOB)
2211 {
2212 long lidx = (long)tv_idx->vval.v_number;
2213 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002214 varnumber_T nr;
2215 int error = FALSE;
2216 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002217
2218 if (blob == NULL)
2219 {
2220 emsg(_(e_blob_not_set));
2221 return FAIL;
2222 }
2223 len = blob_len(blob);
2224 if (lidx < 0 && len + lidx >= 0)
2225 // negative index is relative to the end
2226 lidx = len + lidx;
2227
2228 // Can add one byte at the end.
2229 if (lidx < 0 || lidx > len)
2230 {
2231 semsg(_(e_blob_index_out_of_range_nr), lidx);
2232 return FAIL;
2233 }
2234 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2235 return FAIL;
2236 nr = tv_get_number_chk(tv, &error);
2237 if (error)
2238 return FAIL;
2239 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002240 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002241 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2242 {
2243 long idx = (long)tv_idx->vval.v_number;
2244 object_T *obj = tv_dest->vval.v_object;
2245 typval_T *otv = (typval_T *)(obj + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002246 clear_tv(&otv[idx]);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002247 otv[idx] = *tv;
2248 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002249 else
2250 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002251 status = FAIL;
2252 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002253 }
2254 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002255
2256 clear_tv(tv_idx);
2257 clear_tv(tv_dest);
2258 ectx->ec_stack.ga_len -= 3;
2259 if (status == FAIL)
2260 {
2261 clear_tv(tv);
2262 return FAIL;
2263 }
2264 return OK;
2265}
2266
2267/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002268 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002269 */
2270 static int
2271execute_storerange(isn_T *iptr, ectx_T *ectx)
2272{
2273 typval_T *tv;
2274 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2275 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2276 typval_T *tv_dest = STACK_TV_BOT(-1);
2277 int status = OK;
2278
2279 // Stack contains:
2280 // -4 value to be stored
2281 // -3 first index or "none"
2282 // -2 second index or "none"
2283 // -1 destination list or blob
2284 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002285 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002286 if (tv_dest->v_type == VAR_LIST)
2287 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002288 long n1;
2289 long n2;
2290 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002291
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002292 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2293 if (tv_idx2->v_type == VAR_SPECIAL
2294 && tv_idx2->vval.v_number == VVAL_NONE)
2295 n2 = list_len(tv_dest->vval.v_list) - 1;
2296 else
2297 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2298
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002299 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002300 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002301 status = FAIL;
2302 else
2303 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002304 status = check_range_index_two(tv_dest->vval.v_list,
2305 &n1, li1, &n2, FALSE);
2306 if (status != FAIL)
2307 status = list_assign_range(
2308 tv_dest->vval.v_list,
2309 tv->vval.v_list,
2310 n1,
2311 n2,
2312 tv_idx2->v_type == VAR_SPECIAL,
2313 (char_u *)"=",
2314 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002315 }
2316 }
2317 else if (tv_dest->v_type == VAR_BLOB)
2318 {
2319 varnumber_T n1;
2320 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002321 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002322
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002323 n1 = tv_get_number_chk(tv_idx1, NULL);
2324 if (tv_idx2->v_type == VAR_SPECIAL
2325 && tv_idx2->vval.v_number == VVAL_NONE)
2326 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2327 else
2328 n2 = tv_get_number_chk(tv_idx2, NULL);
2329 bloblen = blob_len(tv_dest->vval.v_blob);
2330
2331 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2332 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002333 status = FAIL;
2334 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002335 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002336 }
2337 else
2338 {
2339 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002340 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002341 }
2342
2343 clear_tv(tv_idx1);
2344 clear_tv(tv_idx2);
2345 clear_tv(tv_dest);
2346 ectx->ec_stack.ga_len -= 4;
2347 clear_tv(tv);
2348
2349 return status;
2350}
2351
2352/*
2353 * Unlet item in list or dict variable.
2354 */
2355 static int
2356execute_unletindex(isn_T *iptr, ectx_T *ectx)
2357{
2358 typval_T *tv_idx = STACK_TV_BOT(-2);
2359 typval_T *tv_dest = STACK_TV_BOT(-1);
2360 int status = OK;
2361
2362 // Stack contains:
2363 // -2 index
2364 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002365 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002366 if (tv_dest->v_type == VAR_DICT)
2367 {
2368 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002369 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002370 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002371 semsg(_(e_expected_str_but_got_str),
2372 vartype_name(VAR_STRING),
2373 vartype_name(tv_idx->v_type));
2374 status = FAIL;
2375 }
2376 else
2377 {
2378 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002379 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002380 dictitem_T *di = NULL;
2381
2382 if (d != NULL && value_check_lock(
2383 d->dv_lock, NULL, FALSE))
2384 status = FAIL;
2385 else
2386 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002387 if (tv_idx->v_type == VAR_STRING)
2388 {
2389 key = tv_idx->vval.v_string;
2390 if (key == NULL)
2391 key = (char_u *)"";
2392 }
2393 else
2394 {
2395 key = tv_get_string(tv_idx);
2396 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002397 if (d != NULL)
2398 di = dict_find(d, key, (int)STRLEN(key));
2399 if (di == NULL)
2400 {
2401 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002402 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002403 status = FAIL;
2404 }
2405 else if (var_check_fixed(di->di_flags,
2406 NULL, FALSE)
2407 || var_check_ro(di->di_flags,
2408 NULL, FALSE))
2409 status = FAIL;
2410 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002411 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002412 }
2413 }
2414 }
2415 else if (tv_dest->v_type == VAR_LIST)
2416 {
2417 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002418 if (check_for_number(tv_idx) == FAIL)
2419 {
2420 status = FAIL;
2421 }
2422 else
2423 {
2424 list_T *l = tv_dest->vval.v_list;
2425 long n = (long)tv_idx->vval.v_number;
2426
2427 if (l != NULL && value_check_lock(
2428 l->lv_lock, NULL, FALSE))
2429 status = FAIL;
2430 else
2431 {
2432 listitem_T *li = list_find(l, n);
2433
2434 if (li == NULL)
2435 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002436 semsg(_(e_list_index_out_of_range_nr), n);
2437 status = FAIL;
2438 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002439 else
2440 listitem_remove(l, li);
2441 }
2442 }
2443 }
2444 else
2445 {
2446 status = FAIL;
2447 semsg(_(e_cannot_index_str),
2448 vartype_name(tv_dest->v_type));
2449 }
2450
2451 clear_tv(tv_idx);
2452 clear_tv(tv_dest);
2453 ectx->ec_stack.ga_len -= 2;
2454
2455 return status;
2456}
2457
2458/*
2459 * Unlet a range of items in a list variable.
2460 */
2461 static int
2462execute_unletrange(isn_T *iptr, ectx_T *ectx)
2463{
2464 // Stack contains:
2465 // -3 index1
2466 // -2 index2
2467 // -1 dict or list
2468 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2469 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2470 typval_T *tv_dest = STACK_TV_BOT(-1);
2471 int status = OK;
2472
2473 if (tv_dest->v_type == VAR_LIST)
2474 {
2475 // indexes must be a number
2476 SOURCING_LNUM = iptr->isn_lnum;
2477 if (check_for_number(tv_idx1) == FAIL
2478 || (tv_idx2->v_type != VAR_SPECIAL
2479 && check_for_number(tv_idx2) == FAIL))
2480 {
2481 status = FAIL;
2482 }
2483 else
2484 {
2485 list_T *l = tv_dest->vval.v_list;
2486 long n1 = (long)tv_idx1->vval.v_number;
2487 long n2 = tv_idx2->v_type == VAR_SPECIAL
2488 ? 0 : (long)tv_idx2->vval.v_number;
2489 listitem_T *li;
2490
2491 li = list_find_index(l, &n1);
2492 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002493 {
2494 semsg(_(e_list_index_out_of_range_nr),
2495 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002496 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002497 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002498 else
2499 {
2500 if (n1 < 0)
2501 n1 = list_idx_of_item(l, li);
2502 if (n2 < 0)
2503 {
2504 listitem_T *li2 = list_find(l, n2);
2505
2506 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002507 {
2508 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002509 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002510 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002511 else
2512 n2 = list_idx_of_item(l, li2);
2513 }
2514 if (status != FAIL
2515 && tv_idx2->v_type != VAR_SPECIAL
2516 && n2 < n1)
2517 {
2518 semsg(_(e_list_index_out_of_range_nr), n2);
2519 status = FAIL;
2520 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002521 if (status != FAIL)
2522 list_unlet_range(l, li, n1,
2523 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002524 }
2525 }
2526 }
2527 else
2528 {
2529 status = FAIL;
2530 SOURCING_LNUM = iptr->isn_lnum;
2531 semsg(_(e_cannot_index_str),
2532 vartype_name(tv_dest->v_type));
2533 }
2534
2535 clear_tv(tv_idx1);
2536 clear_tv(tv_idx2);
2537 clear_tv(tv_dest);
2538 ectx->ec_stack.ga_len -= 3;
2539
2540 return status;
2541}
2542
2543/*
2544 * Top of a for loop.
2545 */
2546 static int
2547execute_for(isn_T *iptr, ectx_T *ectx)
2548{
2549 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002550 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002551 typval_T *ltv = STACK_TV_BOT(-1);
2552 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002553 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002554
2555 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2556 return FAIL;
2557 if (ltv->v_type == VAR_LIST)
2558 {
2559 list_T *list = ltv->vval.v_list;
2560
2561 // push the next item from the list
2562 ++idxtv->vval.v_number;
2563 if (list == NULL
2564 || idxtv->vval.v_number >= list->lv_len)
2565 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002566 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002567 }
2568 else if (list->lv_first == &range_list_item)
2569 {
2570 // non-materialized range() list
2571 tv = STACK_TV_BOT(0);
2572 tv->v_type = VAR_NUMBER;
2573 tv->v_lock = 0;
2574 tv->vval.v_number = list_find_nr(
2575 list, idxtv->vval.v_number, NULL);
2576 ++ectx->ec_stack.ga_len;
2577 }
2578 else
2579 {
2580 listitem_T *li = list_find(list,
2581 idxtv->vval.v_number);
2582
2583 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2584 ++ectx->ec_stack.ga_len;
2585 }
2586 }
2587 else if (ltv->v_type == VAR_STRING)
2588 {
2589 char_u *str = ltv->vval.v_string;
2590
2591 // The index is for the last byte of the previous
2592 // character.
2593 ++idxtv->vval.v_number;
2594 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2595 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002596 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002597 }
2598 else
2599 {
2600 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2601
2602 // Push the next character from the string.
2603 tv = STACK_TV_BOT(0);
2604 tv->v_type = VAR_STRING;
2605 tv->vval.v_string = vim_strnsave(
2606 str + idxtv->vval.v_number, clen);
2607 ++ectx->ec_stack.ga_len;
2608 idxtv->vval.v_number += clen - 1;
2609 }
2610 }
2611 else if (ltv->v_type == VAR_BLOB)
2612 {
2613 blob_T *blob = ltv->vval.v_blob;
2614
2615 // When we get here the first time make a copy of the
2616 // blob, so that the iteration still works when it is
2617 // changed.
2618 if (idxtv->vval.v_number == -1 && blob != NULL)
2619 {
2620 blob_copy(blob, ltv);
2621 blob_unref(blob);
2622 blob = ltv->vval.v_blob;
2623 }
2624
2625 // The index is for the previous byte.
2626 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002627 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002628 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002629 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002630 }
2631 else
2632 {
2633 // Push the next byte from the blob.
2634 tv = STACK_TV_BOT(0);
2635 tv->v_type = VAR_NUMBER;
2636 tv->vval.v_number = blob_get(blob,
2637 idxtv->vval.v_number);
2638 ++ectx->ec_stack.ga_len;
2639 }
2640 }
2641 else
2642 {
2643 semsg(_(e_for_loop_on_str_not_supported),
2644 vartype_name(ltv->v_type));
2645 return FAIL;
2646 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002647
2648 if (jump)
2649 {
2650 // past the end of the list/string/blob, jump to "endfor"
2651 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2652 may_restore_cmdmod(&ectx->ec_funclocal);
2653 }
2654 else
2655 {
2656 // Store the current number of funcrefs, this may be used in
2657 // ISN_LOOPEND. The variable index is always one more than the loop
2658 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002659 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002660 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2661 }
2662
2663 return OK;
2664}
2665
2666/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002667 * Code for handling variables declared inside a loop and used in a closure.
2668 * This is very similar to what is done with funcstack_T. The difference is
2669 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2670 * scope of the block inside a loop and each loop may have its own.
2671 */
2672
2673// Double linked list of loopvars_T in use.
2674static loopvars_T *first_loopvars = NULL;
2675
2676 static void
2677add_loopvars_to_list(loopvars_T *loopvars)
2678{
2679 // Link in list of loopvarss.
2680 if (first_loopvars != NULL)
2681 first_loopvars->lvs_prev = loopvars;
2682 loopvars->lvs_next = first_loopvars;
2683 loopvars->lvs_prev = NULL;
2684 first_loopvars = loopvars;
2685}
2686
2687 static void
2688remove_loopvars_from_list(loopvars_T *loopvars)
2689{
2690 if (loopvars->lvs_prev == NULL)
2691 first_loopvars = loopvars->lvs_next;
2692 else
2693 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2694 if (loopvars->lvs_next != NULL)
2695 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2696}
2697
2698/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002699 * End of a for or while loop: Handle any variables used by a closure.
2700 */
2701 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002702execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002703{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002704 endloop_T *endloop = &iptr->isn_arg.endloop;
2705 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2706 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002707 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002708 garray_T *gap = &ectx->ec_funcrefs;
2709 int closure_in_use = FALSE;
2710 loopvars_T *loopvars;
2711 typval_T *stack;
2712 int idx;
2713
Bram Moolenaarcc341812022-09-19 15:54:34 +01002714 // Check if any created closure is still being referenced and loopvars have
2715 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002716 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2717 {
2718 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2719
Bram Moolenaarcc341812022-09-19 15:54:34 +01002720 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002721 {
2722 int refcount = pt->pt_refcount;
2723 int i;
2724
2725 // A Reference in a variable inside the loop doesn't count, it gets
2726 // unreferenced at the end of the loop.
2727 for (i = 0; i < endloop->end_var_count; ++i)
2728 {
2729 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2730
2731 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2732 --refcount;
2733 }
2734 if (refcount > 1)
2735 {
2736 closure_in_use = TRUE;
2737 break;
2738 }
2739 }
2740 }
2741
2742 // If no function reference were created since the start of the loop block
2743 // or it is no longer referenced there is nothing to do.
2744 if (!closure_in_use)
2745 return OK;
2746
2747 // A closure is using variables declared inside the loop.
2748 // Move them to the called function.
2749 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2750 if (loopvars == NULL)
2751 return FAIL;
2752
2753 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2754 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2755 loopvars->lvs_ga.ga_data = stack;
2756 if (stack == NULL)
2757 {
2758 vim_free(loopvars);
2759 return FAIL;
2760 }
2761 add_loopvars_to_list(loopvars);
2762
2763 // Move the variable values.
2764 for (idx = 0; idx < endloop->end_var_count; ++idx)
2765 {
2766 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2767
2768 *(stack + idx) = *tv;
2769 tv->v_type = VAR_UNKNOWN;
2770 }
2771
2772 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2773 {
2774 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2775
Bram Moolenaarcc341812022-09-19 15:54:34 +01002776 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002777 {
2778 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002779 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002780
Bram Moolenaarcc341812022-09-19 15:54:34 +01002781 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2782 pt->pt_outer.out_loop[depth].var_idx -=
2783 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002784 }
2785 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002786
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002787 return OK;
2788}
2789
2790/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002791 * Called when a partial is freed or its reference count goes down to one. The
2792 * loopvars may be the only reference to the partials in the local variables.
2793 * Go over all of them, the funcref and can be freed if all partials
2794 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002795 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002796 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002797 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002798loopvars_check_refcount(loopvars_T *loopvars)
2799{
2800 int i;
2801 garray_T *gap = &loopvars->lvs_ga;
2802 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002803 typval_T *stack = gap->ga_data;
2804
Bram Moolenaarcc341812022-09-19 15:54:34 +01002805 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2806 return FALSE;
2807 for (i = 0; i < gap->ga_len; ++i)
2808 {
2809 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2810
2811 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2812 && tv->vval.v_partial->pt_refcount == 1)
2813 {
2814 int depth;
2815
2816 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2817 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2818 ++done;
2819 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002820 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002821 if (done != loopvars->lvs_min_refcount)
2822 return FALSE;
2823
2824 // All partials referencing the loopvars have a reference count of
2825 // one, thus the loopvars is no longer of use.
2826 stack = gap->ga_data;
2827 for (i = 0; i < gap->ga_len; ++i)
2828 clear_tv(stack + i);
2829 vim_free(stack);
2830 remove_loopvars_from_list(loopvars);
2831 vim_free(loopvars);
2832 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002833}
2834
2835/*
2836 * For garbage collecting: set references in all variables referenced by
2837 * all loopvars.
2838 */
2839 int
2840set_ref_in_loopvars(int copyID)
2841{
2842 loopvars_T *loopvars;
2843
2844 for (loopvars = first_loopvars; loopvars != NULL;
2845 loopvars = loopvars->lvs_next)
2846 {
2847 typval_T *stack = loopvars->lvs_ga.ga_data;
2848 int i;
2849
2850 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2851 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2852 return TRUE; // abort
2853 }
2854 return FALSE;
2855}
2856
2857/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002858 * Load instruction for w:/b:/g:/t: variable.
2859 * "isn_type" is used instead of "iptr->isn_type".
2860 */
2861 static int
2862load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2863{
2864 dictitem_T *di = NULL;
2865 hashtab_T *ht = NULL;
2866 char namespace;
2867
2868 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2869 return NOTDONE;
2870 switch (isn_type)
2871 {
2872 case ISN_LOADG:
2873 ht = get_globvar_ht();
2874 namespace = 'g';
2875 break;
2876 case ISN_LOADB:
2877 ht = &curbuf->b_vars->dv_hashtab;
2878 namespace = 'b';
2879 break;
2880 case ISN_LOADW:
2881 ht = &curwin->w_vars->dv_hashtab;
2882 namespace = 'w';
2883 break;
2884 case ISN_LOADT:
2885 ht = &curtab->tp_vars->dv_hashtab;
2886 namespace = 't';
2887 break;
2888 default: // Cannot reach here
2889 return NOTDONE;
2890 }
2891 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2892
Bram Moolenaar06b77222022-01-25 15:51:56 +00002893 if (di == NULL)
2894 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002895 if (isn_type == ISN_LOADG)
2896 {
2897 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2898
2899 // g:Something could be a function
2900 if (ufunc != NULL)
2901 {
2902 typval_T *tv = STACK_TV_BOT(0);
2903
2904 ++ectx->ec_stack.ga_len;
2905 tv->v_type = VAR_FUNC;
2906 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2907 if (tv->vval.v_string == NULL)
2908 return FAIL;
2909 STRCPY(tv->vval.v_string, "g:");
2910 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2911 return OK;
2912 }
2913 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002914 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002915 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002916 // no check if the item exists in the script but
2917 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002918 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002919 else
2920 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002921 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002922 return FAIL;
2923 }
2924 else
2925 {
2926 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2927 ++ectx->ec_stack.ga_len;
2928 }
2929 return OK;
2930}
2931
2932/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002933 * Execute instructions in execution context "ectx".
2934 * Return OK or FAIL;
2935 */
2936 static int
2937exec_instructions(ectx_T *ectx)
2938{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002939 int ret = FAIL;
2940 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002941 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002942
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002943 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002944 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002945
Bram Moolenaarff652882021-05-16 15:24:49 +02002946 // Only catch exceptions in this instruction list.
2947 ectx->ec_trylevel_at_start = trylevel;
2948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 for (;;)
2950 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002951 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002953 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002954
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002955 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002956 {
2957 line_breakcheck();
2958 breakcheck_count = 0;
2959 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002960 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002961 {
2962 // Turn CTRL-C into an exception.
2963 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002964 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002965 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002966 did_throw = TRUE;
2967 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002969 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002970 {
2971 // Turn an error message into an exception.
2972 did_emsg = FALSE;
2973 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002974 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002975 did_throw = TRUE;
2976 *msg_list = NULL;
2977 }
2978
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002979 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002981 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002982 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002983 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984
2985 // An exception jumps to the first catch, finally, or returns from
2986 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002987 while (index > 0)
2988 {
2989 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002990 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002991 break;
2992 // In the catch and finally block of this try we have to go up
2993 // one level.
2994 --index;
2995 trycmd = NULL;
2996 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002997 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002999 if (trycmd->tcd_in_catch)
3000 {
3001 // exception inside ":catch", jump to ":finally" once
3002 ectx->ec_iidx = trycmd->tcd_finally_idx;
3003 trycmd->tcd_finally_idx = 0;
3004 }
3005 else
3006 // jump to first ":catch"
3007 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003008 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003009 did_throw = FALSE; // don't come back here until :endtry
3010 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 }
3012 else
3013 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003014 // Not inside try or need to return from current functions.
3015 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003016 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003017 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003018 tv = STACK_TV_BOT(0);
3019 tv->v_type = VAR_NUMBER;
3020 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003021 ++ectx->ec_stack.ga_len;
3022 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003024 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003025 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003026 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003027 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 goto done;
3029 }
3030
Bram Moolenaar4c137212021-04-19 16:48:48 +02003031 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003032 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 }
3034 continue;
3035 }
3036
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003037 /*
3038 * Big switch on the instruction. Most compilers will be turning this
3039 * into an efficient lookup table, since the "case" values are an enum
3040 * with sequential numbers. It may look ugly, but it should be the
3041 * most efficient way.
3042 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003043 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 switch (iptr->isn_type)
3045 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003046 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003047 case ISN_CONSTRUCT:
3048 // "this" is always the local variable at index zero
3049 tv = STACK_TV_VAR(0);
3050 tv->v_type = VAR_OBJECT;
3051 tv->vval.v_object = alloc_clear(
3052 iptr->isn_arg.construct.construct_size);
3053 tv->vval.v_object->obj_class =
3054 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003055 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003056 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003057 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003058 break;
3059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 // execute Ex command line
3061 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003062 if (exec_command(iptr) == FAIL)
3063 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 break;
3065
Bram Moolenaar20677332021-06-06 17:02:53 +02003066 // execute Ex command line split at NL characters.
3067 case ISN_EXEC_SPLIT:
3068 {
3069 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003070 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003071
3072 SOURCING_LNUM = iptr->isn_lnum;
3073 CLEAR_FIELD(cookie);
3074 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3075 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003076 line = get_split_sourceline(0, &cookie, 0, 0);
3077 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003078 get_split_sourceline, &cookie,
3079 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3080 == FAIL
3081 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003082 {
3083 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003084 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003085 }
3086 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003087 }
3088 break;
3089
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003090 // execute Ex command line that is only a range
3091 case ISN_EXECRANGE:
3092 {
3093 exarg_T ea;
3094 char *error = NULL;
3095
3096 CLEAR_FIELD(ea);
3097 ea.cmdidx = CMD_SIZE;
3098 ea.addr_type = ADDR_LINES;
3099 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003100 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003101 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003102 if (ea.cmd == NULL)
3103 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003104 // error is always NULL when using ADDR_LINES
3105 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003106 if (error != NULL)
3107 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003108 emsg(error);
3109 goto on_error;
3110 }
3111 }
3112 break;
3113
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003114 // Evaluate an expression with legacy syntax, push it onto the
3115 // stack.
3116 case ISN_LEGACY_EVAL:
3117 {
3118 char_u *arg = iptr->isn_arg.string;
3119 int res;
3120 int save_flags = cmdmod.cmod_flags;
3121
Bram Moolenaar35578162021-08-02 19:10:38 +02003122 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003123 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003124 tv = STACK_TV_BOT(0);
3125 init_tv(tv);
3126 cmdmod.cmod_flags |= CMOD_LEGACY;
3127 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3128 cmdmod.cmod_flags = save_flags;
3129 if (res == FAIL)
3130 goto on_error;
3131 ++ectx->ec_stack.ga_len;
3132 }
3133 break;
3134
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003135 // push typeval VAR_INSTR with instructions to be executed
3136 case ISN_INSTR:
3137 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003138 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003139 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003140 tv = STACK_TV_BOT(0);
3141 tv->vval.v_instr = ALLOC_ONE(instr_T);
3142 if (tv->vval.v_instr == NULL)
3143 goto on_error;
3144 ++ectx->ec_stack.ga_len;
3145
3146 tv->v_type = VAR_INSTR;
3147 tv->vval.v_instr->instr_ectx = ectx;
3148 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3149 }
3150 break;
3151
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003152 case ISN_SOURCE:
3153 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003154 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003155
Bram Moolenaar89445512022-04-14 12:58:23 +01003156 SOURCING_LNUM = iptr->isn_lnum;
3157 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003158 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003159 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003160 }
3161 break;
3162
Bram Moolenaar4c137212021-04-19 16:48:48 +02003163 // execute :substitute with an expression
3164 case ISN_SUBSTITUTE:
3165 {
3166 subs_T *subs = &iptr->isn_arg.subs;
3167 source_cookie_T cookie;
3168 struct subs_expr_S *save_instr = substitute_instr;
3169 struct subs_expr_S subs_instr;
3170 int res;
3171
3172 subs_instr.subs_ectx = ectx;
3173 subs_instr.subs_instr = subs->subs_instr;
3174 subs_instr.subs_status = OK;
3175 substitute_instr = &subs_instr;
3176
3177 SOURCING_LNUM = iptr->isn_lnum;
3178 // This is very much like ISN_EXEC
3179 CLEAR_FIELD(cookie);
3180 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3181 res = do_cmdline(subs->subs_cmd,
3182 getsourceline, &cookie,
3183 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3184 substitute_instr = save_instr;
3185
3186 if (res == FAIL || did_emsg
3187 || subs_instr.subs_status == FAIL)
3188 goto on_error;
3189 }
3190 break;
3191
3192 case ISN_FINISH:
3193 goto done;
3194
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003195 case ISN_REDIRSTART:
3196 // create a dummy entry for var_redir_str()
3197 if (alloc_redir_lval() == FAIL)
3198 goto on_error;
3199
3200 // The output is stored in growarray "redir_ga" until
3201 // redirection ends.
3202 init_redir_ga();
3203 redir_vname = 1;
3204 break;
3205
3206 case ISN_REDIREND:
3207 {
3208 char_u *res = get_clear_redir_ga();
3209
3210 // End redirection, put redirected text on the stack.
3211 clear_redir_lval();
3212 redir_vname = 0;
3213
Bram Moolenaar35578162021-08-02 19:10:38 +02003214 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003215 {
3216 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003217 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003218 }
3219 tv = STACK_TV_BOT(0);
3220 tv->v_type = VAR_STRING;
3221 tv->vval.v_string = res;
3222 ++ectx->ec_stack.ga_len;
3223 }
3224 break;
3225
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003226 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003227#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003228 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003229 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3230 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003231 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003232#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003233 break;
3234
3235 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003236#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003237 {
3238 exarg_T ea;
3239 int res;
3240
3241 CLEAR_FIELD(ea);
3242 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3243 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3244 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3245 --ectx->ec_stack.ga_len;
3246 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003247 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003248 res = cexpr_core(&ea, tv);
3249 clear_tv(tv);
3250 if (res == FAIL)
3251 goto on_error;
3252 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003253#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003254 break;
3255
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003256 // execute Ex command from pieces on the stack
3257 case ISN_EXECCONCAT:
3258 {
3259 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003260 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003261 int pass;
3262 int i;
3263 char_u *cmd = NULL;
3264 char_u *str;
3265
3266 for (pass = 1; pass <= 2; ++pass)
3267 {
3268 for (i = 0; i < count; ++i)
3269 {
3270 tv = STACK_TV_BOT(i - count);
3271 str = tv->vval.v_string;
3272 if (str != NULL && *str != NUL)
3273 {
3274 if (pass == 2)
3275 STRCPY(cmd + len, str);
3276 len += STRLEN(str);
3277 }
3278 if (pass == 2)
3279 clear_tv(tv);
3280 }
3281 if (pass == 1)
3282 {
3283 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003284 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003285 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003286 len = 0;
3287 }
3288 }
3289
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003290 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003291 do_cmdline_cmd(cmd);
3292 vim_free(cmd);
3293 }
3294 break;
3295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 // execute :echo {string} ...
3297 case ISN_ECHO:
3298 {
3299 int count = iptr->isn_arg.echo.echo_count;
3300 int atstart = TRUE;
3301 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003302 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303
3304 for (idx = 0; idx < count; ++idx)
3305 {
3306 tv = STACK_TV_BOT(idx - count);
3307 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3308 &atstart, &needclr);
3309 clear_tv(tv);
3310 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003311 if (needclr)
3312 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003313 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 }
3315 break;
3316
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003317 // :execute {string} ...
3318 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003319 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003320 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003321 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003322 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003323 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003324 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003325 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003326 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003327 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003328 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003329 garray_T ga;
3330 char_u buf[NUMBUFLEN];
3331 char_u *p;
3332 int len;
3333 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003334 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003335
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003336 if (iptr->isn_type == ISN_ECHOWINDOW)
3337 count = iptr->isn_arg.echowin.ewin_count;
3338 else
3339 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003340 ga_init2(&ga, 1, 80);
3341 for (idx = 0; idx < count; ++idx)
3342 {
3343 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003344 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003345 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003346 if (tv->v_type == VAR_CHANNEL
3347 || tv->v_type == VAR_JOB)
3348 {
3349 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003350 semsg(_(e_using_invalid_value_as_string_str),
3351 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003352 break;
3353 }
3354 else
3355 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003356 }
3357 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003358 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003359
3360 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003361 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003362 failed = TRUE;
3363 else
3364 {
3365 if (ga.ga_len > 0)
3366 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3367 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3368 ga.ga_len += len;
3369 }
3370 clear_tv(tv);
3371 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003372 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003373 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003374 {
3375 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003376 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003377 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003378
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003379 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003380 {
3381 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003382 {
3383 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003384 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003385 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003386 {
3387 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003388 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003389 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003390 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003391 else
3392 {
3393 msg_sb_eol();
3394 if (iptr->isn_type == ISN_ECHOMSG)
3395 {
3396 msg_attr(ga.ga_data, echo_attr);
3397 out_flush();
3398 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003399#ifdef HAS_MESSAGE_WINDOW
3400 else if (iptr->isn_type == ISN_ECHOWINDOW)
3401 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003402 start_echowindow(
3403 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003404 msg_attr(ga.ga_data, echo_attr);
3405 end_echowindow();
3406 }
3407#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003408 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3409 {
3410 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3411 TRUE);
3412 ui_write((char_u *)"\r\n", 2, TRUE);
3413 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003414 else
3415 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003416 SOURCING_LNUM = iptr->isn_lnum;
3417 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003418 }
3419 }
3420 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003421 ga_clear(&ga);
3422 }
3423 break;
3424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 // load local variable or argument
3426 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003427 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003428 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003430 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 break;
3432
3433 // load v: variable
3434 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003435 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003436 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003438 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 break;
3440
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003441 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 case ISN_LOADSCRIPT:
3443 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003444 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 svar_T *sv;
3446
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003447 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003448 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003449 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003450 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003451 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003452 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003454 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 }
3456 break;
3457
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003458 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003460 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003462 int sid = iptr->isn_arg.loadstore.ls_sid;
3463 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003464 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003466
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 if (di == NULL)
3468 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003469 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003470 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003471 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 }
3473 else
3474 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003475 if (iptr->isn_type == ISN_LOADEXPORT)
3476 {
3477 int idx = get_script_item_idx(sid, name, 0,
3478 NULL, NULL);
3479 svar_T *sv;
3480
3481 if (idx >= 0)
3482 {
3483 sv = ((svar_T *)SCRIPT_ITEM(sid)
3484 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003485 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003486 {
3487 SOURCING_LNUM = iptr->isn_lnum;
3488 semsg(_(e_item_not_exported_in_script_str),
3489 name);
3490 goto on_error;
3491 }
3492 }
3493 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003494 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003495 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003497 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 }
3499 }
3500 break;
3501
Bram Moolenaard3aac292020-04-19 14:32:17 +02003502 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003504 case ISN_LOADB:
3505 case ISN_LOADW:
3506 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003508 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003509
Bram Moolenaar06b77222022-01-25 15:51:56 +00003510 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003511 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003512 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003513 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 break;
3517
Bram Moolenaar03290b82020-12-19 16:30:44 +01003518 // load autoload variable
3519 case ISN_LOADAUTO:
3520 {
3521 char_u *name = iptr->isn_arg.string;
3522
Bram Moolenaar35578162021-08-02 19:10:38 +02003523 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003524 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003525 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003526 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003527 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003528 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003529 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003530 }
3531 break;
3532
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003533 // load g:/b:/w:/t: namespace
3534 case ISN_LOADGDICT:
3535 case ISN_LOADBDICT:
3536 case ISN_LOADWDICT:
3537 case ISN_LOADTDICT:
3538 {
3539 dict_T *d = NULL;
3540
3541 switch (iptr->isn_type)
3542 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003543 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3544 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3545 case ISN_LOADWDICT: d = curwin->w_vars; break;
3546 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003547 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003548 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003549 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003550 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003551 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003552 tv = STACK_TV_BOT(0);
3553 tv->v_type = VAR_DICT;
3554 tv->v_lock = 0;
3555 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003556 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003557 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003558 }
3559 break;
3560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 // load &option
3562 case ISN_LOADOPT:
3563 {
3564 typval_T optval;
3565 char_u *name = iptr->isn_arg.string;
3566
Bram Moolenaara8c17702020-04-01 21:17:24 +02003567 // This is not expected to fail, name is checked during
3568 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003569 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003570 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003571 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003572 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003574 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 }
3576 break;
3577
3578 // load $ENV
3579 case ISN_LOADENV:
3580 {
3581 typval_T optval;
3582 char_u *name = iptr->isn_arg.string;
3583
Bram Moolenaar35578162021-08-02 19:10:38 +02003584 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003585 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003586 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003587 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003589 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 }
3591 break;
3592
3593 // load @register
3594 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003595 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003596 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 tv = STACK_TV_BOT(0);
3598 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003599 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003600 // This may result in NULL, which should be equivalent to an
3601 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 tv->vval.v_string = get_reg_contents(
3603 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003604 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 break;
3606
3607 // store local variable
3608 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003609 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 tv = STACK_TV_VAR(iptr->isn_arg.number);
3611 clear_tv(tv);
3612 *tv = *STACK_TV_BOT(0);
3613 break;
3614
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003615 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003616 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003617 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003618 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003619 int sid = iptr->isn_arg.loadstore.ls_sid;
3620 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003621 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003622 dictitem_T *di = find_var_in_ht(ht, 0,
3623 iptr->isn_type == ISN_STORES
3624 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003625
Bram Moolenaar4c137212021-04-19 16:48:48 +02003626 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003627 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003628 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003629 {
3630 if (iptr->isn_type == ISN_STOREEXPORT)
3631 {
3632 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003633 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003634 goto on_error;
3635 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003636 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003637 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003638 else
3639 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003640 if (iptr->isn_type == ISN_STOREEXPORT)
3641 {
3642 int idx = get_script_item_idx(sid, name, 0,
3643 NULL, NULL);
3644
Bram Moolenaar06651632022-04-27 17:54:25 +01003645 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003646 if (idx >= 0)
3647 {
3648 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3649 ->sn_var_vals.ga_data) + idx;
3650
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003651 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003652 {
3653 semsg(_(e_item_not_exported_in_script_str),
3654 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003655 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003656 goto on_error;
3657 }
3658 }
3659 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003660 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003661 {
3662 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003663 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003664 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003665 clear_tv(&di->di_tv);
3666 di->di_tv = *STACK_TV_BOT(0);
3667 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003668 }
3669 break;
3670
3671 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 case ISN_STORESCRIPT:
3673 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003674 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3675 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003677 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003678 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003679 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003680 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003681
3682 // "const" and "final" are checked at compile time, locking
3683 // the value needs to be checked here.
3684 SOURCING_LNUM = iptr->isn_lnum;
3685 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003686 {
3687 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003688 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003689 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 clear_tv(sv->sv_tv);
3692 *sv->sv_tv = *STACK_TV_BOT(0);
3693 }
3694 break;
3695
3696 // store option
3697 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003698 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003700 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3701 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 long n = 0;
3703 char_u *s = NULL;
3704 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003705 char_u numbuf[NUMBUFLEN];
3706 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707
Bram Moolenaar4c137212021-04-19 16:48:48 +02003708 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 tv = STACK_TV_BOT(0);
3710 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003711 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003713 if (s == NULL)
3714 s = (char_u *)"";
3715 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003716 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3717 {
3718 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003719 // If the option can be set to a function reference or
3720 // a lambda and the passed value is a function
3721 // reference, then convert it to the name (string) of
3722 // the function reference.
3723 s = tv2string(tv, &tofree, numbuf, 0);
3724 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003725 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003726 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003727 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003728 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003729 goto on_error;
3730 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003731 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003733 // must be VAR_NUMBER, CHECKTYPE makes sure
3734 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003735 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003736 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003737 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 if (msg != NULL)
3739 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003740 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003742 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 }
3745 break;
3746
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003747 // store $ENV
3748 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003749 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003750 tv = STACK_TV_BOT(0);
3751 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3752 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003753 break;
3754
3755 // store @r
3756 case ISN_STOREREG:
3757 {
3758 int reg = iptr->isn_arg.number;
3759
Bram Moolenaar4c137212021-04-19 16:48:48 +02003760 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003761 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003762 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003763 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003764 }
3765 break;
3766
3767 // store v: variable
3768 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003769 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003770 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3771 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003772 // should not happen, type is checked when compiling
3773 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003774 break;
3775
Bram Moolenaard3aac292020-04-19 14:32:17 +02003776 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003778 case ISN_STOREB:
3779 case ISN_STOREW:
3780 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003782 dictitem_T *di;
3783 hashtab_T *ht;
3784 char_u *name = iptr->isn_arg.string + 2;
3785
Bram Moolenaard3aac292020-04-19 14:32:17 +02003786 switch (iptr->isn_type)
3787 {
3788 case ISN_STOREG:
3789 ht = get_globvar_ht();
3790 break;
3791 case ISN_STOREB:
3792 ht = &curbuf->b_vars->dv_hashtab;
3793 break;
3794 case ISN_STOREW:
3795 ht = &curwin->w_vars->dv_hashtab;
3796 break;
3797 case ISN_STORET:
3798 ht = &curtab->tp_vars->dv_hashtab;
3799 break;
3800 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003801 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803
Bram Moolenaar4c137212021-04-19 16:48:48 +02003804 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003805 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003807 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 else
3809 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003810 SOURCING_LNUM = iptr->isn_lnum;
3811 if (var_check_permission(di, name) == FAIL)
3812 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 clear_tv(&di->di_tv);
3814 di->di_tv = *STACK_TV_BOT(0);
3815 }
3816 }
3817 break;
3818
Bram Moolenaar03290b82020-12-19 16:30:44 +01003819 // store an autoload variable
3820 case ISN_STOREAUTO:
3821 SOURCING_LNUM = iptr->isn_lnum;
3822 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3823 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003824 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003825 break;
3826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 // store number in local variable
3828 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003829 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 clear_tv(tv);
3831 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003832 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 break;
3834
Bram Moolenaar590162c2022-12-24 21:24:06 +00003835 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003836 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003837 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003838 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003839
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003840 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003841 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003842 if (res == NOTDONE)
3843 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003844 }
3845 break;
3846
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003847 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003848 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003849 if (execute_storerange(iptr, ectx) == FAIL)
3850 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003851 break;
3852
Bram Moolenaard505d172022-12-18 21:42:55 +00003853 case ISN_LOAD_CLASSMEMBER:
3854 {
3855 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3856 goto theend;
3857 classmember_T *cm = &iptr->isn_arg.classmember;
3858 *STACK_TV_BOT(0) =
3859 cm->cm_class->class_members_tv[cm->cm_idx];
3860 ++ectx->ec_stack.ga_len;
3861 }
3862 break;
3863
3864 case ISN_STORE_CLASSMEMBER:
3865 {
3866 classmember_T *cm = &iptr->isn_arg.classmember;
3867 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
3868 clear_tv(tv);
3869 *tv = *STACK_TV_BOT(-1);
3870 --ectx->ec_stack.ga_len;
3871 }
3872 break;
3873
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003874 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003875 case ISN_LOADOUTER:
3876 case ISN_STOREOUTER:
3877 {
3878 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003879 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3880 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003881
3882 while (depth > 1 && outer != NULL)
3883 {
3884 outer = outer->out_up;
3885 --depth;
3886 }
3887 if (outer == NULL)
3888 {
3889 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003890 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3891 || ectx->ec_outer_ref == NULL)
3892 // Possibly :def function called from legacy
3893 // context.
3894 emsg(_(e_closure_called_from_invalid_context));
3895 else
3896 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003897 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003898 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003899 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003900 // Variable declared in loop. May be copied if the
3901 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01003902 tv = ((typval_T *)outer->out_loop[-depth - 1]
3903 .stack->ga_data)
3904 + outer->out_loop[-depth - 1].var_idx
3905 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003906 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003907 // Variable declared in a function. May be copied if
3908 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003909 tv = ((typval_T *)outer->out_stack->ga_data)
3910 + outer->out_frame_idx + STACK_FRAME_SIZE
3911 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003912 if (iptr->isn_type == ISN_LOADOUTER)
3913 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003914 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003915 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003916 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003917 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003918 }
3919 else
3920 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003921 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003922 clear_tv(tv);
3923 *tv = *STACK_TV_BOT(0);
3924 }
3925 }
3926 break;
3927
Bram Moolenaar752fc692021-01-04 21:57:11 +01003928 // unlet item in list or dict variable
3929 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003930 if (execute_unletindex(iptr, ectx) == FAIL)
3931 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003932 break;
3933
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003934 // unlet range of items in list variable
3935 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003936 if (execute_unletrange(iptr, ectx) == FAIL)
3937 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003938 break;
3939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 // push constant
3941 case ISN_PUSHNR:
3942 case ISN_PUSHBOOL:
3943 case ISN_PUSHSPEC:
3944 case ISN_PUSHF:
3945 case ISN_PUSHS:
3946 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003947 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003948 case ISN_PUSHCHANNEL:
3949 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003950 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003951 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003953 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003954 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 switch (iptr->isn_type)
3956 {
3957 case ISN_PUSHNR:
3958 tv->v_type = VAR_NUMBER;
3959 tv->vval.v_number = iptr->isn_arg.number;
3960 break;
3961 case ISN_PUSHBOOL:
3962 tv->v_type = VAR_BOOL;
3963 tv->vval.v_number = iptr->isn_arg.number;
3964 break;
3965 case ISN_PUSHSPEC:
3966 tv->v_type = VAR_SPECIAL;
3967 tv->vval.v_number = iptr->isn_arg.number;
3968 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 case ISN_PUSHF:
3970 tv->v_type = VAR_FLOAT;
3971 tv->vval.v_float = iptr->isn_arg.fnumber;
3972 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 case ISN_PUSHBLOB:
3974 blob_copy(iptr->isn_arg.blob, tv);
3975 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003976 case ISN_PUSHFUNC:
3977 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003978 if (iptr->isn_arg.string == NULL)
3979 tv->vval.v_string = NULL;
3980 else
3981 tv->vval.v_string =
3982 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003983 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003984 case ISN_PUSHCHANNEL:
3985#ifdef FEAT_JOB_CHANNEL
3986 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003987 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003988#endif
3989 break;
3990 case ISN_PUSHJOB:
3991#ifdef FEAT_JOB_CHANNEL
3992 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003993 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003994#endif
3995 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 default:
3997 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003998 tv->vval.v_string = iptr->isn_arg.string == NULL
3999 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 }
4001 break;
4002
Bram Moolenaar06b77222022-01-25 15:51:56 +00004003 case ISN_AUTOLOAD:
4004 {
4005 char_u *name = iptr->isn_arg.string;
4006
4007 (void)script_autoload(name, FALSE);
4008 if (find_func(name, TRUE))
4009 {
4010 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4011 goto theend;
4012 tv = STACK_TV_BOT(0);
4013 tv->v_lock = 0;
4014 ++ectx->ec_stack.ga_len;
4015 tv->v_type = VAR_FUNC;
4016 tv->vval.v_string = vim_strsave(name);
4017 }
4018 else
4019 {
4020 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4021
4022 if (res == NOTDONE)
4023 goto theend;
4024 if (res == FAIL)
4025 goto on_error;
4026 }
4027 }
4028 break;
4029
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004030 case ISN_UNLET:
4031 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4032 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004033 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004034 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004035 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004036 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004037 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004038
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004039 case ISN_LOCKUNLOCK:
4040 {
4041 typval_T *lval_root_save = lval_root;
4042 int res;
4043
4044 // Stack has the local variable, argument the whole :lock
4045 // or :unlock command, like ISN_EXEC.
4046 --ectx->ec_stack.ga_len;
4047 lval_root = STACK_TV_BOT(0);
4048 res = exec_command(iptr);
4049 clear_tv(lval_root);
4050 lval_root = lval_root_save;
4051 if (res == FAIL)
4052 goto on_error;
4053 }
4054 break;
4055
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004056 case ISN_LOCKCONST:
4057 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4058 break;
4059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 // create a list from items on the stack; uses a single allocation
4061 // for the list header and the items
4062 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004063 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004064 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 break;
4066
4067 // create a dict from items on the stack
4068 case ISN_NEWDICT:
4069 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004070 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004072 SOURCING_LNUM = iptr->isn_lnum;
4073 res = exe_newdict(iptr->isn_arg.number, ectx);
4074 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004075 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004076 if (res == MAYBE)
4077 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 }
4079 break;
4080
LemonBoy372bcce2022-04-25 12:43:20 +01004081 case ISN_CONCAT:
4082 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4083 goto theend;
4084 break;
4085
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004086 // create a partial with NULL value
4087 case ISN_NEWPARTIAL:
4088 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4089 goto theend;
4090 ++ectx->ec_stack.ga_len;
4091 tv = STACK_TV_BOT(-1);
4092 tv->v_type = VAR_PARTIAL;
4093 tv->v_lock = 0;
4094 tv->vval.v_partial = NULL;
4095 break;
4096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 // call a :def function
4098 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004099 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004100 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4101 NULL,
4102 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004103 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004104 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 break;
4106
4107 // call a builtin function
4108 case ISN_BCALL:
4109 SOURCING_LNUM = iptr->isn_lnum;
4110 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4111 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004112 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004113 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 break;
4115
4116 // call a funcref or partial
4117 case ISN_PCALL:
4118 {
4119 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4120 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004121 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122
4123 SOURCING_LNUM = iptr->isn_lnum;
4124 if (pfunc->cpf_top)
4125 {
4126 // funcref is above the arguments
4127 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4128 }
4129 else
4130 {
4131 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004132 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004133 partial_tv = *STACK_TV_BOT(0);
4134 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004136 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004137 if (tv == &partial_tv)
4138 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004140 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 }
4142 break;
4143
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004144 case ISN_PCALL_END:
4145 // PCALL finished, arguments have been consumed and replaced by
4146 // the return value. Now clear the funcref from the stack,
4147 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004148 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004149 clear_tv(STACK_TV_BOT(-1));
4150 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4151 break;
4152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 // call a user defined function or funcref/partial
4154 case ISN_UCALL:
4155 {
4156 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4157
4158 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004159 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004160 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004161 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 }
4163 break;
4164
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004165 // :defer func(arg)
4166 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004167 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004168 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4169 goto on_error;
4170 break;
4171
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004172 // Return from a :def function call without a value.
4173 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004174 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004175 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004176 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004177 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004178 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004179 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004180 if (iptr->isn_type == ISN_RETURN_VOID)
4181 {
4182 tv->v_type = VAR_VOID;
4183 tv->vval.v_number = 0;
4184 tv->v_lock = 0;
4185 }
4186 else
4187 {
4188 *tv = *STACK_TV_VAR(0);
4189 ++tv->vval.v_object->obj_refcount;
4190 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004191 // FALLTHROUGH
4192
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004193 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 case ISN_RETURN:
4195 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004196 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004197 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004198
4199 if (trystack->ga_len > 0)
4200 trycmd = ((trycmd_T *)trystack->ga_data)
4201 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004202 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004203 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004205 // jump to ":finally" or ":endtry"
4206 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004207 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004208 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004209 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 trycmd->tcd_return = TRUE;
4211 }
4212 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004213 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 }
4215 break;
4216
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004217 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 case ISN_FUNCREF:
4219 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004220 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4221 ufunc_T *ufunc;
4222 funcref_T *funcref = &iptr->isn_arg.funcref;
4223 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004226 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004227 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004228 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004229 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004230 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004231 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004232 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004233 {
4234 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4235 + funcref->fr_dfunc_idx;
4236
4237 ufunc = pt_dfunc->df_ufunc;
4238 }
4239 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004240 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004241 if (ufunc == NULL)
4242 {
4243 SOURCING_LNUM = iptr->isn_lnum;
4244 iemsg("ufunc unexpectedly NULL for FUNCREF");
4245 goto theend;
4246 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004247 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004248 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004249 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004250 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004252 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 tv->vval.v_partial = pt;
4254 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004255 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 }
4257 break;
4258
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004259 // Create a global function from a lambda.
4260 case ISN_NEWFUNC:
4261 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004262 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004263
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004264 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004265 arg->nfa_global, &arg->nfa_loopvar_info,
4266 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004267 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004268 }
4269 break;
4270
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004271 // List functions
4272 case ISN_DEF:
4273 if (iptr->isn_arg.string == NULL)
4274 list_functions(NULL);
4275 else
4276 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004277 exarg_T ea;
4278 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004279
4280 CLEAR_FIELD(ea);
4281 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004282 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +00004283 define_function(&ea, NULL, &lines_to_free, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004284 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004285 }
4286 break;
4287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 // jump if a condition is met
4289 case ISN_JUMP:
4290 {
4291 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004292 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 int jump = TRUE;
4294
4295 if (when != JUMP_ALWAYS)
4296 {
4297 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004298 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004299 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004300 || when == JUMP_IF_COND_TRUE)
4301 {
4302 SOURCING_LNUM = iptr->isn_lnum;
4303 jump = tv_get_bool_chk(tv, &error);
4304 if (error)
4305 goto on_error;
4306 }
4307 else
4308 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004309 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004311 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 {
4313 // drop the value from the stack
4314 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004315 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 }
4317 }
4318 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004319 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 }
4321 break;
4322
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004323 // "while": jump to end if a condition is false
4324 case ISN_WHILE:
4325 {
4326 int error = FALSE;
4327 int jump = TRUE;
4328
4329 tv = STACK_TV_BOT(-1);
4330 SOURCING_LNUM = iptr->isn_lnum;
4331 jump = !tv_get_bool_chk(tv, &error);
4332 if (error)
4333 goto on_error;
4334 // drop the value from the stack
4335 clear_tv(tv);
4336 --ectx->ec_stack.ga_len;
4337 if (jump)
4338 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4339
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004340 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004341 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004342 tv = STACK_TV_VAR(
4343 iptr->isn_arg.whileloop.while_funcref_idx);
4344 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4345 }
4346 break;
4347
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004348 // Jump if an argument with a default value was already set and not
4349 // v:none.
4350 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004351 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004352 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004353 int arg_set = tv->v_type != VAR_UNKNOWN
4354 && !(tv->v_type == VAR_SPECIAL
4355 && tv->vval.v_number == VVAL_NONE);
4356 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004357 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004358 break;
4359
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 // top of a for loop
4361 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004362 if (execute_for(iptr, ectx) == FAIL)
4363 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 break;
4365
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004366 // end of a for or while loop
4367 case ISN_ENDLOOP:
4368 if (execute_endloop(iptr, ectx) == FAIL)
4369 goto theend;
4370 break;
4371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 // start of ":try" block
4373 case ISN_TRY:
4374 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004375 trycmd_T *trycmd = NULL;
4376
Bram Moolenaar35578162021-08-02 19:10:38 +02004377 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004378 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004379 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4380 + ectx->ec_trystack.ga_len;
4381 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004383 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004384 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4385 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004386 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004387 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004388 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004389 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004390 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004391 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 }
4393 break;
4394
4395 case ISN_PUSHEXC:
4396 if (current_exception == NULL)
4397 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004398 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004400 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004402 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004403 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004405 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004407 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 tv->vval.v_string = vim_strsave(
4409 (char_u *)current_exception->value);
4410 break;
4411
4412 case ISN_CATCH:
4413 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004414 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004415 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416
Bram Moolenaar4c137212021-04-19 16:48:48 +02004417 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004418 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004420 trycmd->tcd_caught = TRUE;
4421 trycmd->tcd_did_throw = FALSE;
4422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004424 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 catch_exception(current_exception);
4426 }
4427 break;
4428
Bram Moolenaarc150c092021-02-13 15:02:46 +01004429 case ISN_TRYCONT:
4430 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004431 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004432 trycont_T *trycont = &iptr->isn_arg.trycont;
4433 int i;
4434 trycmd_T *trycmd;
4435 int iidx = trycont->tct_where;
4436
4437 if (trystack->ga_len < trycont->tct_levels)
4438 {
4439 siemsg("TRYCONT: expected %d levels, found %d",
4440 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004441 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004442 }
4443 // Make :endtry jump to any outer try block and the last
4444 // :endtry inside the loop to the loop start.
4445 for (i = trycont->tct_levels; i > 0; --i)
4446 {
4447 trycmd = ((trycmd_T *)trystack->ga_data)
4448 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004449 // Add one to tcd_cont to be able to jump to
4450 // instruction with index zero.
4451 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004452 iidx = trycmd->tcd_finally_idx == 0
4453 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004454 }
4455 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004456 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004457 }
4458 break;
4459
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004460 case ISN_FINALLY:
4461 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004462 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004463 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4464 + trystack->ga_len - 1;
4465
4466 // Reset the index to avoid a return statement jumps here
4467 // again.
4468 trycmd->tcd_finally_idx = 0;
4469 break;
4470 }
4471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 // end of ":try" block
4473 case ISN_ENDTRY:
4474 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004475 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004476 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477
Bram Moolenaar06651632022-04-27 17:54:25 +01004478 --trystack->ga_len;
4479 --trylevel;
4480 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4481 if (trycmd->tcd_did_throw)
4482 did_throw = TRUE;
4483 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004485 // discard the exception
4486 if (caught_stack == current_exception)
4487 caught_stack = caught_stack->caught;
4488 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004490
4491 if (trycmd->tcd_return)
4492 goto func_return;
4493
4494 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4495 {
4496 --ectx->ec_stack.ga_len;
4497 clear_tv(STACK_TV_BOT(0));
4498 }
4499 if (trycmd->tcd_cont != 0)
4500 // handling :continue: jump to outer try block or
4501 // start of the loop
4502 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 }
4504 break;
4505
4506 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004507 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004508 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004509
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004510 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4511 {
4512 // throwing an exception while using "silent!" causes
4513 // the function to abort but not display an error.
4514 tv = STACK_TV_BOT(-1);
4515 clear_tv(tv);
4516 tv->v_type = VAR_NUMBER;
4517 tv->vval.v_number = 0;
4518 goto done;
4519 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004520 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004521 tv = STACK_TV_BOT(0);
4522 if (tv->vval.v_string == NULL
4523 || *skipwhite(tv->vval.v_string) == NUL)
4524 {
4525 vim_free(tv->vval.v_string);
4526 SOURCING_LNUM = iptr->isn_lnum;
4527 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004528 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004529 }
4530
4531 // Inside a "catch" we need to first discard the caught
4532 // exception.
4533 if (trystack->ga_len > 0)
4534 {
4535 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4536 + trystack->ga_len - 1;
4537 if (trycmd->tcd_caught && current_exception != NULL)
4538 {
4539 // discard the exception
4540 if (caught_stack == current_exception)
4541 caught_stack = caught_stack->caught;
4542 discard_current_exception();
4543 trycmd->tcd_caught = FALSE;
4544 }
4545 }
4546
Bram Moolenaar90a57162022-02-12 14:23:17 +00004547 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004548 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4549 == FAIL)
4550 {
4551 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004552 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004553 }
4554 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556 break;
4557
4558 // compare with special values
4559 case ISN_COMPAREBOOL:
4560 case ISN_COMPARESPECIAL:
4561 {
4562 typval_T *tv1 = STACK_TV_BOT(-2);
4563 typval_T *tv2 = STACK_TV_BOT(-1);
4564 varnumber_T arg1 = tv1->vval.v_number;
4565 varnumber_T arg2 = tv2->vval.v_number;
4566 int res;
4567
Bram Moolenaar06651632022-04-27 17:54:25 +01004568 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4569 res = arg1 == arg2;
4570 else
4571 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572
Bram Moolenaar4c137212021-04-19 16:48:48 +02004573 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 tv1->v_type = VAR_BOOL;
4575 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4576 }
4577 break;
4578
Bram Moolenaar7a222242022-03-01 19:23:24 +00004579 case ISN_COMPARENULL:
4580 {
4581 typval_T *tv1 = STACK_TV_BOT(-2);
4582 typval_T *tv2 = STACK_TV_BOT(-1);
4583 int res;
4584
4585 res = typval_compare_null(tv1, tv2);
4586 if (res == MAYBE)
4587 goto on_error;
4588 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4589 res = !res;
4590 clear_tv(tv1);
4591 clear_tv(tv2);
4592 --ectx->ec_stack.ga_len;
4593 tv1->v_type = VAR_BOOL;
4594 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4595 }
4596 break;
4597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 // Operation with two number arguments
4599 case ISN_OPNR:
4600 case ISN_COMPARENR:
4601 {
4602 typval_T *tv1 = STACK_TV_BOT(-2);
4603 typval_T *tv2 = STACK_TV_BOT(-1);
4604 varnumber_T arg1 = tv1->vval.v_number;
4605 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004606 varnumber_T res = 0;
4607 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004609 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4610 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4611 {
4612 if (arg2 < 0)
4613 {
4614 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004615 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004616 goto on_error;
4617 }
4618 }
4619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 switch (iptr->isn_arg.op.op_type)
4621 {
4622 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004623 case EXPR_DIV: if (arg2 == 0)
4624 div_zero = TRUE;
4625 else
4626 res = arg1 / arg2;
4627 break;
4628 case EXPR_REM: if (arg2 == 0)
4629 div_zero = TRUE;
4630 else
4631 res = arg1 % arg2;
4632 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 case EXPR_SUB: res = arg1 - arg2; break;
4634 case EXPR_ADD: res = arg1 + arg2; break;
4635
4636 case EXPR_EQUAL: res = arg1 == arg2; break;
4637 case EXPR_NEQUAL: res = arg1 != arg2; break;
4638 case EXPR_GREATER: res = arg1 > arg2; break;
4639 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4640 case EXPR_SMALLER: res = arg1 < arg2; break;
4641 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004642 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4643 res = 0;
4644 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004645 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004646 break;
4647 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4648 res = 0;
4649 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004650 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004651 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004652 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 }
4654
Bram Moolenaar4c137212021-04-19 16:48:48 +02004655 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 if (iptr->isn_type == ISN_COMPARENR)
4657 {
4658 tv1->v_type = VAR_BOOL;
4659 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4660 }
4661 else
4662 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004663 if (div_zero)
4664 {
4665 SOURCING_LNUM = iptr->isn_lnum;
4666 emsg(_(e_divide_by_zero));
4667 goto on_error;
4668 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 }
4670 break;
4671
4672 // Computation with two float arguments
4673 case ISN_OPFLOAT:
4674 case ISN_COMPAREFLOAT:
4675 {
4676 typval_T *tv1 = STACK_TV_BOT(-2);
4677 typval_T *tv2 = STACK_TV_BOT(-1);
4678 float_T arg1 = tv1->vval.v_float;
4679 float_T arg2 = tv2->vval.v_float;
4680 float_T res = 0;
4681 int cmp = FALSE;
4682
4683 switch (iptr->isn_arg.op.op_type)
4684 {
4685 case EXPR_MULT: res = arg1 * arg2; break;
4686 case EXPR_DIV: res = arg1 / arg2; break;
4687 case EXPR_SUB: res = arg1 - arg2; break;
4688 case EXPR_ADD: res = arg1 + arg2; break;
4689
4690 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4691 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4692 case EXPR_GREATER: cmp = arg1 > arg2; break;
4693 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4694 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4695 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4696 default: cmp = 0; break;
4697 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004698 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 if (iptr->isn_type == ISN_COMPAREFLOAT)
4700 {
4701 tv1->v_type = VAR_BOOL;
4702 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4703 }
4704 else
4705 tv1->vval.v_float = res;
4706 }
4707 break;
4708
4709 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004710 case ISN_COMPAREDICT:
4711 case ISN_COMPAREFUNC:
4712 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004714 case ISN_COMPARECLASS:
4715 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 {
4717 typval_T *tv1 = STACK_TV_BOT(-2);
4718 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004719 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4720 int ic = iptr->isn_arg.op.op_ic;
4721 int res = FALSE;
4722 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723
Bram Moolenaar265f8112021-12-19 12:33:05 +00004724 SOURCING_LNUM = iptr->isn_lnum;
4725 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004727 status = typval_compare_list(tv1, tv2,
4728 exprtype, ic, &res);
4729 }
4730 else if (iptr->isn_type == ISN_COMPAREDICT)
4731 {
4732 status = typval_compare_dict(tv1, tv2,
4733 exprtype, ic, &res);
4734 }
4735 else if (iptr->isn_type == ISN_COMPAREFUNC)
4736 {
4737 status = typval_compare_func(tv1, tv2,
4738 exprtype, ic, &res);
4739 }
4740 else if (iptr->isn_type == ISN_COMPARESTRING)
4741 {
4742 status = typval_compare_string(tv1, tv2,
4743 exprtype, ic, &res);
4744 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004745 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00004746 {
4747 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004749 else if (iptr->isn_type == ISN_COMPARECLASS)
4750 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004751 status = typval_compare_class(tv1, tv2,
4752 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004753 }
4754 else // ISN_COMPAREOBJECT
4755 {
4756 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004757 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004758 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004759 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 clear_tv(tv1);
4761 clear_tv(tv2);
4762 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004763 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4764 if (status == FAIL)
4765 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 }
4767 break;
4768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 case ISN_COMPAREANY:
4770 {
4771 typval_T *tv1 = STACK_TV_BOT(-2);
4772 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004773 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004775 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776
Bram Moolenaareb26f432020-09-14 16:50:05 +02004777 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004778 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004780 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004781 if (status == FAIL)
4782 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783 }
4784 break;
4785
4786 case ISN_ADDLIST:
4787 case ISN_ADDBLOB:
4788 {
4789 typval_T *tv1 = STACK_TV_BOT(-2);
4790 typval_T *tv2 = STACK_TV_BOT(-1);
4791
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004792 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004794 {
4795 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4796 && tv1->vval.v_list != NULL)
4797 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4798 NULL);
4799 else
4800 eval_addlist(tv1, tv2);
4801 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 else
4803 eval_addblob(tv1, tv2);
4804 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004805 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 }
4807 break;
4808
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004809 case ISN_LISTAPPEND:
4810 {
4811 typval_T *tv1 = STACK_TV_BOT(-2);
4812 typval_T *tv2 = STACK_TV_BOT(-1);
4813 list_T *l = tv1->vval.v_list;
4814
4815 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004816 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004817 if (l == NULL)
4818 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004819 emsg(_(e_cannot_add_to_null_list));
4820 goto on_error;
4821 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004822 if (value_check_lock(l->lv_lock, NULL, FALSE))
4823 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004824 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004825 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004826 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004827 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004828 }
4829 break;
4830
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004831 case ISN_BLOBAPPEND:
4832 {
4833 typval_T *tv1 = STACK_TV_BOT(-2);
4834 typval_T *tv2 = STACK_TV_BOT(-1);
4835 blob_T *b = tv1->vval.v_blob;
4836 int error = FALSE;
4837 varnumber_T n;
4838
4839 // add a number to a blob
4840 if (b == NULL)
4841 {
4842 SOURCING_LNUM = iptr->isn_lnum;
4843 emsg(_(e_cannot_add_to_null_blob));
4844 goto on_error;
4845 }
4846 n = tv_get_number_chk(tv2, &error);
4847 if (error)
4848 goto on_error;
4849 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004850 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004851 }
4852 break;
4853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854 // Computation with two arguments of unknown type
4855 case ISN_OPANY:
4856 {
4857 typval_T *tv1 = STACK_TV_BOT(-2);
4858 typval_T *tv2 = STACK_TV_BOT(-1);
4859 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 int error = FALSE;
4862
4863 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4864 {
4865 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4866 {
4867 eval_addlist(tv1, tv2);
4868 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004869 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 break;
4871 }
4872 else if (tv1->v_type == VAR_BLOB
4873 && tv2->v_type == VAR_BLOB)
4874 {
4875 eval_addblob(tv1, tv2);
4876 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004877 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 break;
4879 }
4880 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 if (tv1->v_type == VAR_FLOAT)
4882 {
4883 f1 = tv1->vval.v_float;
4884 n1 = 0;
4885 }
4886 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004888 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 n1 = tv_get_number_chk(tv1, &error);
4890 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004891 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 if (tv2->v_type == VAR_FLOAT)
4893 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 if (tv2->v_type == VAR_FLOAT)
4896 {
4897 f2 = tv2->vval.v_float;
4898 n2 = 0;
4899 }
4900 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 {
4902 n2 = tv_get_number_chk(tv2, &error);
4903 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004904 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 if (tv1->v_type == VAR_FLOAT)
4906 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 // if there is a float on either side the result is a float
4909 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4910 {
4911 switch (iptr->isn_arg.op.op_type)
4912 {
4913 case EXPR_MULT: f1 = f1 * f2; break;
4914 case EXPR_DIV: f1 = f1 / f2; break;
4915 case EXPR_SUB: f1 = f1 - f2; break;
4916 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004917 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004918 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004919 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 }
4921 clear_tv(tv1);
4922 clear_tv(tv2);
4923 tv1->v_type = VAR_FLOAT;
4924 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004925 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 }
4927 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004929 int failed = FALSE;
4930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 switch (iptr->isn_arg.op.op_type)
4932 {
4933 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004934 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4935 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004936 goto on_error;
4937 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 case EXPR_SUB: n1 = n1 - n2; break;
4939 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004940 default: n1 = num_modulus(n1, n2, &failed);
4941 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004942 goto on_error;
4943 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 }
4945 clear_tv(tv1);
4946 clear_tv(tv2);
4947 tv1->v_type = VAR_NUMBER;
4948 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004949 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 }
4951 }
4952 break;
4953
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004954 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004955 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004956 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004957 int is_slice = iptr->isn_type == ISN_STRSLICE;
4958 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004959 char_u *res;
4960
4961 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004962 // string slice: string is at stack-3, first index at
4963 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004964 if (is_slice)
4965 {
4966 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004967 n1 = tv->vval.v_number;
4968 }
4969
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004970 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004971 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004972
Bram Moolenaar4c137212021-04-19 16:48:48 +02004973 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004974 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004975 if (is_slice)
4976 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004977 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004978 else
4979 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004980 // single character (including composing characters).
4981 // If the index is too big or negative the result is
4982 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004983 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004984 vim_free(tv->vval.v_string);
4985 tv->vval.v_string = res;
4986 }
4987 break;
4988
4989 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004990 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004991 case ISN_BLOBINDEX:
4992 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004994 int is_slice = iptr->isn_type == ISN_LISTSLICE
4995 || iptr->isn_type == ISN_BLOBSLICE;
4996 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4997 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004998 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004999 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000
5001 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005002 // list slice: list is at stack-3, indexes at stack-2 and
5003 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005004 // Same for blob.
5005 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006
5007 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005008 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005010
5011 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 {
Bram Moolenaared591872020-08-15 22:14:53 +02005013 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005014 n1 = tv->vval.v_number;
5015 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 }
Bram Moolenaared591872020-08-15 22:14:53 +02005017
Bram Moolenaar4c137212021-04-19 16:48:48 +02005018 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005019 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005020 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005021 if (is_blob)
5022 {
5023 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5024 n1, n2, FALSE, tv) == FAIL)
5025 goto on_error;
5026 }
5027 else
5028 {
5029 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5030 n1, n2, FALSE, tv, TRUE) == FAIL)
5031 goto on_error;
5032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 }
5034 break;
5035
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005036 case ISN_ANYINDEX:
5037 case ISN_ANYSLICE:
5038 {
5039 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5040 typval_T *var1, *var2;
5041 int res;
5042
5043 // index: composite is at stack-2, index at stack-1
5044 // slice: composite is at stack-3, indexes at stack-2 and
5045 // stack-1
5046 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005047 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005048 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5049 goto on_error;
5050 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5051 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005052 res = eval_index_inner(tv, is_slice, var1, var2,
5053 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005054 clear_tv(var1);
5055 if (is_slice)
5056 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005057 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005058 if (res == FAIL)
5059 goto on_error;
5060 }
5061 break;
5062
Bram Moolenaar9af78762020-06-16 11:34:42 +02005063 case ISN_SLICE:
5064 {
5065 list_T *list;
5066 int count = iptr->isn_arg.number;
5067
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005068 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005069 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005070 list = tv->vval.v_list;
5071
5072 // no error for short list, expect it to be checked earlier
5073 if (list != NULL && list->lv_len >= count)
5074 {
5075 list_T *newlist = list_slice(list,
5076 count, list->lv_len - 1);
5077
5078 if (newlist != NULL)
5079 {
5080 list_unref(list);
5081 tv->vval.v_list = newlist;
5082 ++newlist->lv_refcount;
5083 }
5084 }
5085 }
5086 break;
5087
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005088 case ISN_GETITEM:
5089 {
5090 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005091 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005092
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005093 // Get list item: list is at stack-1, push item.
5094 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005095 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5096 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005097
Bram Moolenaar35578162021-08-02 19:10:38 +02005098 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005099 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005100 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005101 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005102
5103 // Useful when used in unpack assignment. Reset at
5104 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005105 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005106 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005107 }
5108 break;
5109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 case ISN_MEMBER:
5111 {
5112 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005113 char_u *key;
5114 dictitem_T *di;
5115
5116 // dict member: dict is at stack-2, key at stack-1
5117 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005118 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005119 dict = tv->vval.v_dict;
5120
5121 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005122 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005123 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005124 if (key == NULL)
5125 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005126
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005127 if ((di = dict_find(dict, key, -1)) == NULL)
5128 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005129 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005130 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005131
5132 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005133 // stack contents makes sense and the dict stack is
5134 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005135 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005136 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005137 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005138 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005139 tv->v_type = VAR_NUMBER;
5140 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005141 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005142 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005143 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005144 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005145 // Put the dict used on the dict stack, it might be used by
5146 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005147 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005148 if (dict_stack_save(tv) == FAIL)
5149 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005150 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005151 }
5152 break;
5153
5154 // dict member with string key
5155 case ISN_STRINGMEMBER:
5156 {
5157 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005158 dictitem_T *di;
5159
5160 tv = STACK_TV_BOT(-1);
5161 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5162 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005163 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005164 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005165 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005166 }
5167 dict = tv->vval.v_dict;
5168
5169 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5170 == NULL)
5171 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005172 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005173 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005174 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005175 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005177 // Put the dict used on the dict stack, it might be used by
5178 // a dict function later.
5179 if (dict_stack_save(tv) == FAIL)
5180 goto on_fatal_error;
5181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005183 }
5184 break;
5185
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005186 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005187 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005188 {
5189 tv = STACK_TV_BOT(-1);
5190 if (tv->v_type != VAR_OBJECT)
5191 {
5192 SOURCING_LNUM = iptr->isn_lnum;
5193 garray_T type_list;
5194 ga_init2(&type_list, sizeof(type_T *), 10);
5195 type_T *type = typval2type(tv, get_copyID(),
5196 &type_list, TVTT_DO_MEMBER);
5197 char *tofree = NULL;
5198 char *typename = type_name(type, &tofree);
5199 semsg(_(e_object_required_found_str), typename);
5200 vim_free(tofree);
5201 clear_type_list(&type_list);
5202 goto on_error;
5203 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005204
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005205 object_T *obj = tv->vval.v_object;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005206 int idx;
5207 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
5208 idx = iptr->isn_arg.number;
5209 else
5210 {
5211 idx = iptr->isn_arg.classmember.cm_idx;
5212 // convert the interface index to the object index
5213 idx = object_index_from_itf_index(
5214 iptr->isn_arg.classmember.cm_class,
5215 idx, obj->obj_class);
5216 }
5217
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005218 // the members are located right after the object struct
5219 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005220 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005221
5222 // Unreference the object after getting the member, it may
5223 // be freed.
5224 object_unref(obj);
5225 }
5226 break;
5227
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005228 case ISN_STORE_THIS:
5229 {
5230 int idx = iptr->isn_arg.number;
5231 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5232 // the members are located right after the object struct
5233 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5234 clear_tv(mtv);
5235 *mtv = *STACK_TV_BOT(-1);
5236 --ectx->ec_stack.ga_len;
5237 }
5238 break;
5239
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005240 case ISN_CLEARDICT:
5241 dict_stack_drop();
5242 break;
5243
5244 case ISN_USEDICT:
5245 {
5246 typval_T *dict_tv = dict_stack_get_tv();
5247
5248 // Turn "dict.Func" into a partial for "Func" bound to
5249 // "dict". Don't do this when "Func" is already a partial
5250 // that was bound explicitly (pt_auto is FALSE).
5251 tv = STACK_TV_BOT(-1);
5252 if (dict_tv != NULL
5253 && dict_tv->v_type == VAR_DICT
5254 && dict_tv->vval.v_dict != NULL
5255 && (tv->v_type == VAR_FUNC
5256 || (tv->v_type == VAR_PARTIAL
5257 && (tv->vval.v_partial->pt_auto
5258 || tv->vval.v_partial->pt_dict == NULL))))
5259 dict_tv->vval.v_dict =
5260 make_partial(dict_tv->vval.v_dict, tv);
5261 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005262 }
5263 break;
5264
5265 case ISN_NEGATENR:
5266 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005267 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005268 if (tv->v_type == VAR_FLOAT)
5269 tv->vval.v_float = -tv->vval.v_float;
5270 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005271 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005272 break;
5273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005274 case ISN_CHECKTYPE:
5275 {
5276 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005277 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005278 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005280 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005281 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005282 if (!ectx->ec_where.wt_variable)
5283 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005284 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005285 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005286 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005287 if (r == FAIL)
5288 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005289 if (!ectx->ec_where.wt_variable)
5290 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005291
5292 // number 0 is FALSE, number 1 is TRUE
5293 if (tv->v_type == VAR_NUMBER
5294 && ct->ct_type->tt_type == VAR_BOOL
5295 && (tv->vval.v_number == 0
5296 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005298 tv->v_type = VAR_BOOL;
5299 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005300 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301 }
5302 }
5303 break;
5304
Bram Moolenaar9af78762020-06-16 11:34:42 +02005305 case ISN_CHECKLEN:
5306 {
5307 int min_len = iptr->isn_arg.checklen.cl_min_len;
5308 list_T *list = NULL;
5309
5310 tv = STACK_TV_BOT(-1);
5311 if (tv->v_type == VAR_LIST)
5312 list = tv->vval.v_list;
5313 if (list == NULL || list->lv_len < min_len
5314 || (list->lv_len > min_len
5315 && !iptr->isn_arg.checklen.cl_more_OK))
5316 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005317 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005318 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005319 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005320 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005321 }
5322 }
5323 break;
5324
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005325 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005326 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005327 break;
5328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005329 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005330 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 {
5332 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005333 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005334
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005335 if (iptr->isn_type == ISN_2BOOL)
5336 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005337 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005338 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005339 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005340 n = !n;
5341 }
5342 else
5343 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005344 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005345 SOURCING_LNUM = iptr->isn_lnum;
5346 n = tv_get_bool_chk(tv, &error);
5347 if (error)
5348 goto on_error;
5349 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350 clear_tv(tv);
5351 tv->v_type = VAR_BOOL;
5352 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5353 }
5354 break;
5355
5356 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005357 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005358 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005359 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5360 iptr->isn_type == ISN_2STRING_ANY,
5361 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005362 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005363 break;
5364
Bram Moolenaar08597872020-12-10 19:43:40 +01005365 case ISN_RANGE:
5366 {
5367 exarg_T ea;
5368 char *errormsg;
5369
Bram Moolenaarece0b872021-01-08 20:40:45 +01005370 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005371 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005372 ea.addr_type = ADDR_LINES;
5373 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005374 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005375 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005376 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005377
Bram Moolenaar35578162021-08-02 19:10:38 +02005378 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005379 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005380 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005381 tv = STACK_TV_BOT(-1);
5382 tv->v_type = VAR_NUMBER;
5383 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005384 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005385 }
5386 break;
5387
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005388 case ISN_PUT:
5389 {
5390 int regname = iptr->isn_arg.put.put_regname;
5391 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5392 char_u *expr = NULL;
5393 int dir = FORWARD;
5394
Bram Moolenaar08597872020-12-10 19:43:40 +01005395 if (lnum < -2)
5396 {
5397 // line number was put on the stack by ISN_RANGE
5398 tv = STACK_TV_BOT(-1);
5399 curwin->w_cursor.lnum = tv->vval.v_number;
5400 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5401 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005402 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005403 }
5404 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005405 // :put! above cursor
5406 dir = BACKWARD;
5407 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005408 {
5409 curwin->w_cursor.lnum = lnum;
5410 if (lnum == 0)
5411 // check_cursor() below will move to line 1
5412 dir = BACKWARD;
5413 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005414
5415 if (regname == '=')
5416 {
5417 tv = STACK_TV_BOT(-1);
5418 if (tv->v_type == VAR_STRING)
5419 expr = tv->vval.v_string;
5420 else
5421 {
5422 expr = typval2string(tv, TRUE); // allocates value
5423 clear_tv(tv);
5424 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005425 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005426 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005427 check_cursor();
5428 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5429 vim_free(expr);
5430 }
5431 break;
5432
Bram Moolenaar02194d22020-10-24 23:08:38 +02005433 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005434 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5435 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5436 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5437 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005438 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5439 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005440 break;
5441
Bram Moolenaar02194d22020-10-24 23:08:38 +02005442 case ISN_CMDMOD_REV:
5443 // filter regprog is owned by the instruction, don't free it
5444 cmdmod.cmod_filter_regmatch.regprog = NULL;
5445 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005446 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5447 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005448 break;
5449
Bram Moolenaar792f7862020-11-23 08:31:18 +01005450 case ISN_UNPACK:
5451 {
5452 int count = iptr->isn_arg.unpack.unp_count;
5453 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5454 list_T *l;
5455 listitem_T *li;
5456 int i;
5457
5458 // Check there is a valid list to unpack.
5459 tv = STACK_TV_BOT(-1);
5460 if (tv->v_type != VAR_LIST)
5461 {
5462 SOURCING_LNUM = iptr->isn_lnum;
5463 emsg(_(e_for_argument_must_be_sequence_of_lists));
5464 goto on_error;
5465 }
5466 l = tv->vval.v_list;
5467 if (l == NULL
5468 || l->lv_len < (semicolon ? count - 1 : count))
5469 {
5470 SOURCING_LNUM = iptr->isn_lnum;
5471 emsg(_(e_list_value_does_not_have_enough_items));
5472 goto on_error;
5473 }
5474 else if (!semicolon && l->lv_len > count)
5475 {
5476 SOURCING_LNUM = iptr->isn_lnum;
5477 emsg(_(e_list_value_has_more_items_than_targets));
5478 goto on_error;
5479 }
5480
5481 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005482 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005483 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005484 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005485
5486 // Variable after semicolon gets a list with the remaining
5487 // items.
5488 if (semicolon)
5489 {
5490 list_T *rem_list =
5491 list_alloc_with_items(l->lv_len - count + 1);
5492
5493 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005494 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005495 tv = STACK_TV_BOT(-count);
5496 tv->vval.v_list = rem_list;
5497 ++rem_list->lv_refcount;
5498 tv->v_lock = 0;
5499 li = l->lv_first;
5500 for (i = 0; i < count - 1; ++i)
5501 li = li->li_next;
5502 for (i = 0; li != NULL; ++i)
5503 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005504 typval_T tvcopy;
5505
5506 copy_tv(&li->li_tv, &tvcopy);
5507 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005508 li = li->li_next;
5509 }
5510 --count;
5511 }
5512
5513 // Produce the values in reverse order, first item last.
5514 li = l->lv_first;
5515 for (i = 0; i < count; ++i)
5516 {
5517 tv = STACK_TV_BOT(-i - 1);
5518 copy_tv(&li->li_tv, tv);
5519 li = li->li_next;
5520 }
5521
5522 list_unref(l);
5523 }
5524 break;
5525
Bram Moolenaarb2049902021-01-24 12:53:53 +01005526 case ISN_PROF_START:
5527 case ISN_PROF_END:
5528 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005529#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005530 funccall_T cookie;
5531 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005532 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005533 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005534
Bram Moolenaarca16c602022-09-06 18:57:08 +01005535 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005536 if (iptr->isn_type == ISN_PROF_START)
5537 {
5538 func_line_start(&cookie, iptr->isn_lnum);
5539 // if we get here the instruction is executed
5540 func_line_exec(&cookie);
5541 }
5542 else
5543 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005544#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005545 }
5546 break;
5547
Bram Moolenaare99d4222021-06-13 14:01:26 +02005548 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005549 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005550 break;
5551
Bram Moolenaar389df252020-07-09 21:20:47 +02005552 case ISN_SHUFFLE:
5553 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005554 typval_T tmp_tv;
5555 int item = iptr->isn_arg.shuffle.shfl_item;
5556 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005557
5558 tmp_tv = *STACK_TV_BOT(-item);
5559 for ( ; up > 0 && item > 1; --up)
5560 {
5561 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5562 --item;
5563 }
5564 *STACK_TV_BOT(-item) = tmp_tv;
5565 }
5566 break;
5567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005568 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005569 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005570 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005571 ectx->ec_where.wt_index = 0;
5572 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005573 break;
5574 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005575 continue;
5576
Bram Moolenaard032f342020-07-18 18:13:02 +02005577func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005578 // Restore previous function. If the frame pointer is where we started
5579 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005580 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005581 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005582
Bram Moolenaar4c137212021-04-19 16:48:48 +02005583 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005584 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005585 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005586 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005587
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005588on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005589 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005590 // If "emsg_silent" is set then ignore the error, unless it was set
5591 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005592 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005593 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005594 {
5595 // If a sequence of instructions causes an error while ":silent!"
5596 // was used, restore the stack length and jump ahead to restoring
5597 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005598 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005599 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005600 while (ectx->ec_stack.ga_len
5601 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005602 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005603 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005604 clear_tv(STACK_TV_BOT(0));
5605 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005606 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5607 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005608 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005609 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005610 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005611on_fatal_error:
5612 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005613 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005614 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005615 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005616 }
5617
5618done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005619 ret = OK;
5620theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005621 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005622
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005623 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005624 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5625 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005626}
5627
5628/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005629 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005630 * "rettv".
5631 * Return OK or FAIL.
5632 */
5633 int
5634exe_typval_instr(typval_T *tv, typval_T *rettv)
5635{
5636 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5637 isn_T *save_instr = ectx->ec_instr;
5638 int save_iidx = ectx->ec_iidx;
5639 int res;
5640
LemonBoyf3b48952022-05-05 13:53:03 +01005641 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5642 // even when the compilation fails.
5643 rettv->v_type = VAR_UNKNOWN;
5644
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005645 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5646 res = exec_instructions(ectx);
5647 if (res == OK)
5648 {
5649 *rettv = *STACK_TV_BOT(-1);
5650 --ectx->ec_stack.ga_len;
5651 }
5652
5653 ectx->ec_instr = save_instr;
5654 ectx->ec_iidx = save_iidx;
5655
5656 return res;
5657}
5658
5659/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005660 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5661 * "substitute_instr".
5662 */
5663 char_u *
5664exe_substitute_instr(void)
5665{
5666 ectx_T *ectx = substitute_instr->subs_ectx;
5667 isn_T *save_instr = ectx->ec_instr;
5668 int save_iidx = ectx->ec_iidx;
5669 char_u *res;
5670
5671 ectx->ec_instr = substitute_instr->subs_instr;
5672 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005673 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005674 typval_T *tv = STACK_TV_BOT(-1);
5675
Bram Moolenaar27523602021-06-05 21:36:19 +02005676 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005677 --ectx->ec_stack.ga_len;
5678 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005679 }
5680 else
5681 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005682 substitute_instr->subs_status = FAIL;
5683 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005685
Bram Moolenaar4c137212021-04-19 16:48:48 +02005686 ectx->ec_instr = save_instr;
5687 ectx->ec_iidx = save_iidx;
5688
5689 return res;
5690}
5691
5692/*
5693 * Call a "def" function from old Vim script.
5694 * Return OK or FAIL.
5695 */
5696 int
5697call_def_function(
5698 ufunc_T *ufunc,
5699 int argc_arg, // nr of arguments
5700 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005701 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005702 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005703 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005704 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005705 typval_T *rettv) // return value
5706{
5707 ectx_T ectx; // execution context
5708 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005709 int partial_argc = partial == NULL
5710 || (flags & DEF_USE_PT_ARGV) == 0
5711 ? 0 : partial->pt_argc;
5712 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005713 typval_T *tv;
5714 int idx;
5715 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005716 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005717 sctx_T save_current_sctx = current_sctx;
5718 int did_emsg_before = did_emsg_cumul + did_emsg;
5719 int save_suppress_errthrow = suppress_errthrow;
5720 msglist_T **saved_msg_list = NULL;
5721 msglist_T *private_msg_list = NULL;
5722 int save_emsg_silent_def = emsg_silent_def;
5723 int save_did_emsg_def = did_emsg_def;
5724 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005725 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005726
5727// Get pointer to item in the stack.
5728#undef STACK_TV
5729#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5730
5731// Get pointer to item at the bottom of the stack, -1 is the bottom.
5732#undef STACK_TV_BOT
5733#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5734
5735// Get pointer to a local variable on the stack. Negative for arguments.
5736#undef STACK_TV_VAR
5737#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5738
5739 if (ufunc->uf_def_status == UF_NOT_COMPILED
5740 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005741 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5742 && compile_def_function(ufunc, FALSE,
5743 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005744 {
5745 if (did_emsg_cumul + did_emsg == did_emsg_before)
5746 semsg(_(e_function_is_not_compiled_str),
5747 printable_func_name(ufunc));
5748 return FAIL;
5749 }
5750
5751 {
5752 // Check the function was really compiled.
5753 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5754 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005755 if (dfunc->df_ufunc == NULL)
5756 {
5757 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5758 return FAIL;
5759 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005760 if (INSTRUCTIONS(dfunc) == NULL)
5761 {
5762 iemsg("using call_def_function() on not compiled function");
5763 return FAIL;
5764 }
5765 }
5766
5767 // If depth of calling is getting too high, don't execute the function.
5768 orig_funcdepth = funcdepth_get();
5769 if (funcdepth_increment() == FAIL)
5770 return FAIL;
5771
5772 CLEAR_FIELD(ectx);
5773 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5774 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005775 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005776 {
5777 funcdepth_decrement();
5778 return FAIL;
5779 }
5780 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5781 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5782 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005783 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005784
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005785 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005786 if (idx > 0 && ufunc->uf_va_name == NULL)
5787 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005788 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005789 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005790 goto failed_early;
5791 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005792 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005793 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005794 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005795 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005796 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005797 goto failed_early;
5798 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005799
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005800 // Put values from the partial and arguments on the stack, but no more than
5801 // what the function expects. A lambda can be called with more arguments
5802 // than it uses.
5803 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005804 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5805 ++idx)
5806 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005807 int argv_idx = idx - partial_argc;
5808
5809 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005810 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005811 && tv->v_type == VAR_SPECIAL
5812 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005813 {
5814 // Use the default value.
5815 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5816 }
5817 else
5818 {
5819 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5820 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005821 ufunc->uf_arg_types[idx], tv,
5822 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005823 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005824 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005825 }
5826 ++ectx.ec_stack.ga_len;
5827 }
5828
5829 // Turn varargs into a list. Empty list if no args.
5830 if (ufunc->uf_va_name != NULL)
5831 {
5832 int vararg_count = argc - ufunc->uf_args.ga_len;
5833
5834 if (vararg_count < 0)
5835 vararg_count = 0;
5836 else
5837 argc -= vararg_count;
5838 if (exe_newlist(vararg_count, &ectx) == FAIL)
5839 goto failed_early;
5840
5841 // Check the type of the list items.
5842 tv = STACK_TV_BOT(-1);
5843 if (ufunc->uf_va_type != NULL
5844 && ufunc->uf_va_type != &t_list_any
5845 && ufunc->uf_va_type->tt_member != &t_any
5846 && tv->vval.v_list != NULL)
5847 {
5848 type_T *expected = ufunc->uf_va_type->tt_member;
5849 listitem_T *li = tv->vval.v_list->lv_first;
5850
5851 for (idx = 0; idx < vararg_count; ++idx)
5852 {
5853 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005854 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005855 goto failed_early;
5856 li = li->li_next;
5857 }
5858 }
5859
5860 if (defcount > 0)
5861 // Move varargs list to below missing default arguments.
5862 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5863 --ectx.ec_stack.ga_len;
5864 }
5865
5866 // Make space for omitted arguments, will store default value below.
5867 // Any varargs list goes after them.
5868 if (defcount > 0)
5869 for (idx = 0; idx < defcount; ++idx)
5870 {
5871 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5872 ++ectx.ec_stack.ga_len;
5873 }
5874 if (ufunc->uf_va_name != NULL)
5875 ++ectx.ec_stack.ga_len;
5876
5877 // Frame pointer points to just after arguments.
5878 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5879 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5880
5881 {
5882 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5883 + ufunc->uf_dfunc_idx;
5884 ufunc_T *base_ufunc = dfunc->df_ufunc;
5885
5886 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005887 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005888 if (partial != NULL || base_ufunc->uf_partial != NULL)
5889 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005890 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5891 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005892 goto failed_early;
5893 if (partial != NULL)
5894 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005895 outer_T *outer = get_pt_outer(partial);
5896
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005897 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005898 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005899 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005900 if (current_ectx != NULL)
5901 {
5902 if (current_ectx->ec_outer_ref != NULL
5903 && current_ectx->ec_outer_ref->or_outer != NULL)
5904 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005905 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005906 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005907 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005908 }
5909 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005910 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005911 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005912 ++partial->pt_refcount;
5913 ectx.ec_outer_ref->or_partial = partial;
5914 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005915 }
5916 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005917 {
5918 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5919 ++base_ufunc->uf_partial->pt_refcount;
5920 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5921 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005922 }
5923 }
5924
5925 // dummy frame entries
5926 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5927 {
5928 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5929 ++ectx.ec_stack.ga_len;
5930 }
5931
5932 {
5933 // Reserve space for local variables and any closure reference count.
5934 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5935 + ufunc->uf_dfunc_idx;
5936
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005937 // Initialize variables to zero. That avoids having to generate
5938 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005939 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005940 {
5941 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5942 STACK_TV_VAR(idx)->vval.v_number = 0;
5943 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005944 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005945
5946 if (object != NULL)
5947 {
5948 // the object is always the variable at index zero
5949 tv = STACK_TV_VAR(0);
5950 tv->v_type = VAR_OBJECT;
5951 tv->vval.v_object = object;
5952 }
5953
Bram Moolenaar4c137212021-04-19 16:48:48 +02005954 if (dfunc->df_has_closure)
5955 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01005956 // Initialize the variable that counts how many closures were
5957 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005958 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5959 STACK_TV_VAR(idx)->vval.v_number = 0;
5960 ++ectx.ec_stack.ga_len;
5961 }
5962
5963 ectx.ec_instr = INSTRUCTIONS(dfunc);
5964 }
5965
Bram Moolenaar58779852022-09-06 18:31:14 +01005966 // Store the execution context in funccal, used by invoke_all_defer().
5967 if (funccal != NULL)
5968 funccal->fc_ectx = &ectx;
5969
Bram Moolenaar4c137212021-04-19 16:48:48 +02005970 // Following errors are in the function, not the caller.
5971 // Commands behave like vim9script.
5972 estack_push_ufunc(ufunc, 1);
5973 current_sctx = ufunc->uf_script_ctx;
5974 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5975
5976 // Use a specific location for storing error messages to be converted to an
5977 // exception.
5978 saved_msg_list = msg_list;
5979 msg_list = &private_msg_list;
5980
5981 // Do turn errors into exceptions.
5982 suppress_errthrow = FALSE;
5983
5984 // Do not delete the function while executing it.
5985 ++ufunc->uf_calls;
5986
5987 // When ":silent!" was used before calling then we still abort the
5988 // function. If ":silent!" is used in the function then we don't.
5989 emsg_silent_def = emsg_silent;
5990 did_emsg_def = 0;
5991
5992 ectx.ec_where.wt_index = 0;
5993 ectx.ec_where.wt_variable = FALSE;
5994
Bram Moolenaar5800c792022-09-22 17:34:01 +01005995 /*
5996 * Execute the instructions until done.
5997 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02005998 ret = exec_instructions(&ectx);
5999 if (ret == OK)
6000 {
6001 // function finished, get result from the stack.
6002 if (ufunc->uf_ret_type == &t_void)
6003 {
6004 rettv->v_type = VAR_VOID;
6005 }
6006 else
6007 {
6008 tv = STACK_TV_BOT(-1);
6009 *rettv = *tv;
6010 tv->v_type = VAR_UNKNOWN;
6011 }
6012 }
6013
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006014 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006015 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006016
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006017 // Deal with any remaining closures, they may be in use somewhere.
6018 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006019 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006020 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006021 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006022 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006023
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006024 estack_pop();
6025 current_sctx = save_current_sctx;
6026
Bram Moolenaar2336c372021-12-06 15:06:54 +00006027 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6028 // Function was unreferenced while being used, free it now.
6029 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006030
Bram Moolenaar352134b2020-10-17 22:04:08 +02006031 if (*msg_list != NULL && saved_msg_list != NULL)
6032 {
6033 msglist_T **plist = saved_msg_list;
6034
6035 // Append entries from the current msg_list (uncaught exceptions) to
6036 // the saved msg_list.
6037 while (*plist != NULL)
6038 plist = &(*plist)->next;
6039
6040 *plist = *msg_list;
6041 }
6042 msg_list = saved_msg_list;
6043
Bram Moolenaar4c137212021-04-19 16:48:48 +02006044 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006045 {
6046 cmdmod.cmod_filter_regmatch.regprog = NULL;
6047 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006048 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006049 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006050 emsg_silent_def = save_emsg_silent_def;
6051 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006052
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006053failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006054 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006056 {
6057 tv = STACK_TV(idx);
6058 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6059 clear_tv(tv);
6060 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006061 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006064 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006065 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006066 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006067 if (ectx.ec_outer_ref->or_outer_allocated)
6068 vim_free(ectx.ec_outer_ref->or_outer);
6069 partial_unref(ectx.ec_outer_ref->or_partial);
6070 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006071 }
6072
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006073 // Not sure if this is necessary.
6074 suppress_errthrow = save_suppress_errthrow;
6075
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006076 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6077 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006078 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006079 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006080 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081 return ret;
6082}
6083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006084/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006085 * Called when a def function has finished (possibly failed).
6086 * Invoke all the function returns to clean up and invoke deferred functions,
6087 * except the toplevel one.
6088 */
6089 void
6090unwind_def_callstack(ectx_T *ectx)
6091{
6092 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6093 func_return(ectx);
6094}
6095
6096/*
dundargocc57b5bc2022-11-02 13:30:51 +00006097 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006098 */
6099 void
6100may_invoke_defer_funcs(ectx_T *ectx)
6101{
6102 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6103
6104 if (dfunc->df_defer_var_idx > 0)
6105 invoke_defer_funcs(ectx);
6106}
6107
6108/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006109 * Return loopvarinfo in a printable form in allocated memory.
6110 */
6111 static char_u *
6112printable_loopvarinfo(loopvarinfo_T *lvi)
6113{
6114 garray_T ga;
6115 int depth;
6116
6117 ga_init2(&ga, 1, 100);
6118 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6119 {
6120 if (ga_grow(&ga, 50) == FAIL)
6121 break;
6122 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006123 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006124 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006125 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006126 lvi->lvi_loop[depth].var_idx,
6127 lvi->lvi_loop[depth].var_idx
6128 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006129 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006130 }
6131 return ga.ga_data;
6132}
6133
6134/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006135 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6136 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6137 * "pfx" is prefixed to every line.
6138 */
6139 static void
6140list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6141{
6142 int line_idx = 0;
6143 int prev_current = 0;
6144 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006145 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006146
6147 for (current = 0; current < instr_count; ++current)
6148 {
6149 isn_T *iptr = &instr[current];
6150 char *line;
6151
6152 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006153 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006154 while (line_idx < iptr->isn_lnum
6155 && line_idx < ufunc->uf_lines.ga_len)
6156 {
6157 if (current > prev_current)
6158 {
6159 msg_puts("\n\n");
6160 prev_current = current;
6161 }
6162 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6163 if (line != NULL)
6164 msg(line);
6165 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006166 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6167 {
6168 int first_def_arg = ufunc->uf_args.ga_len
6169 - ufunc->uf_def_args.ga_len;
6170
6171 if (def_arg_idx > 0)
6172 msg_puts("\n\n");
6173 msg_start();
6174 msg_puts(" ");
6175 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6176 first_def_arg + def_arg_idx]);
6177 msg_puts(" = ");
6178 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6179 msg_clr_eos();
6180 msg_end();
6181 }
6182 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006183
6184 switch (iptr->isn_type)
6185 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006186 case ISN_CONSTRUCT:
6187 smsg("%s%4d NEW %s size %d", pfx, current,
6188 iptr->isn_arg.construct.construct_class->class_name,
6189 (int)iptr->isn_arg.construct.construct_size);
6190 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006191 case ISN_EXEC:
6192 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6193 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006194 case ISN_EXEC_SPLIT:
6195 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6196 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006197 case ISN_EXECRANGE:
6198 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6199 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006200 case ISN_LEGACY_EVAL:
6201 smsg("%s%4d EVAL legacy %s", pfx, current,
6202 iptr->isn_arg.string);
6203 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006204 case ISN_REDIRSTART:
6205 smsg("%s%4d REDIR", pfx, current);
6206 break;
6207 case ISN_REDIREND:
6208 smsg("%s%4d REDIR END%s", pfx, current,
6209 iptr->isn_arg.number ? " append" : "");
6210 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006211 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006212#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006213 smsg("%s%4d CEXPR pre %s", pfx, current,
6214 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006215#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006216 break;
6217 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006218#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006219 {
6220 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6221
6222 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6223 cexpr_get_auname(cer->cer_cmdidx),
6224 cer->cer_forceit ? "!" : "",
6225 cer->cer_cmdline);
6226 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006227#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006228 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006229 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006230 smsg("%s%4d INSTR", pfx, current);
6231 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6232 msg(" -------------");
6233 break;
6234 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006235 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006236 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6237
6238 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006239 }
6240 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006241 case ISN_SUBSTITUTE:
6242 {
6243 subs_T *subs = &iptr->isn_arg.subs;
6244
6245 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6246 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6247 msg(" -------------");
6248 }
6249 break;
6250 case ISN_EXECCONCAT:
6251 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6252 (varnumber_T)iptr->isn_arg.number);
6253 break;
6254 case ISN_ECHO:
6255 {
6256 echo_T *echo = &iptr->isn_arg.echo;
6257
6258 smsg("%s%4d %s %d", pfx, current,
6259 echo->echo_with_white ? "ECHO" : "ECHON",
6260 echo->echo_count);
6261 }
6262 break;
6263 case ISN_EXECUTE:
6264 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006265 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006266 break;
6267 case ISN_ECHOMSG:
6268 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006269 (varnumber_T)(iptr->isn_arg.number));
6270 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006271 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006272 if (iptr->isn_arg.echowin.ewin_time > 0)
6273 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6274 iptr->isn_arg.echowin.ewin_count,
6275 iptr->isn_arg.echowin.ewin_time);
6276 else
6277 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6278 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006279 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006280 case ISN_ECHOCONSOLE:
6281 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6282 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006283 break;
6284 case ISN_ECHOERR:
6285 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006286 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006287 break;
6288 case ISN_LOAD:
6289 {
6290 if (iptr->isn_arg.number < 0)
6291 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6292 (varnumber_T)(iptr->isn_arg.number
6293 + STACK_FRAME_SIZE));
6294 else
6295 smsg("%s%4d LOAD $%lld", pfx, current,
6296 (varnumber_T)(iptr->isn_arg.number));
6297 }
6298 break;
6299 case ISN_LOADOUTER:
6300 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006301 isn_outer_T *outer = &iptr->isn_arg.outer;
6302
6303 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006304 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006305 outer->outer_depth,
6306 outer->outer_idx + STACK_FRAME_SIZE);
6307 else if (outer->outer_depth < 0)
6308 smsg("%s%4d LOADOUTER $%d in loop level %d",
6309 pfx, current,
6310 outer->outer_idx,
6311 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006312 else
6313 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006314 outer->outer_depth,
6315 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006316 }
6317 break;
6318 case ISN_LOADV:
6319 smsg("%s%4d LOADV v:%s", pfx, current,
6320 get_vim_var_name(iptr->isn_arg.number));
6321 break;
6322 case ISN_LOADSCRIPT:
6323 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006324 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6325 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6326 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006327
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006328 sv = get_script_svar(sref, -1);
6329 if (sv == NULL)
6330 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6331 pfx, current, si->sn_name);
6332 else
6333 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006334 sv->sv_name,
6335 sref->sref_idx,
6336 si->sn_name);
6337 }
6338 break;
6339 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006340 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006341 {
6342 scriptitem_T *si = SCRIPT_ITEM(
6343 iptr->isn_arg.loadstore.ls_sid);
6344
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006345 smsg("%s%4d %s s:%s from %s", pfx, current,
6346 iptr->isn_type == ISN_LOADS ? "LOADS"
6347 : "LOADEXPORT",
6348 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006349 }
6350 break;
6351 case ISN_LOADAUTO:
6352 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6353 break;
6354 case ISN_LOADG:
6355 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6356 break;
6357 case ISN_LOADB:
6358 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6359 break;
6360 case ISN_LOADW:
6361 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6362 break;
6363 case ISN_LOADT:
6364 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6365 break;
6366 case ISN_LOADGDICT:
6367 smsg("%s%4d LOAD g:", pfx, current);
6368 break;
6369 case ISN_LOADBDICT:
6370 smsg("%s%4d LOAD b:", pfx, current);
6371 break;
6372 case ISN_LOADWDICT:
6373 smsg("%s%4d LOAD w:", pfx, current);
6374 break;
6375 case ISN_LOADTDICT:
6376 smsg("%s%4d LOAD t:", pfx, current);
6377 break;
6378 case ISN_LOADOPT:
6379 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6380 break;
6381 case ISN_LOADENV:
6382 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6383 break;
6384 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006385 smsg("%s%4d LOADREG @%c", pfx, current,
6386 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006387 break;
6388
6389 case ISN_STORE:
6390 if (iptr->isn_arg.number < 0)
6391 smsg("%s%4d STORE arg[%lld]", pfx, current,
6392 iptr->isn_arg.number + STACK_FRAME_SIZE);
6393 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006394 smsg("%s%4d STORE $%lld", pfx, current,
6395 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006396 break;
6397 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006398 {
6399 isn_outer_T *outer = &iptr->isn_arg.outer;
6400
6401 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6402 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6403 pfx, current, outer->outer_idx);
6404 else
6405 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6406 outer->outer_depth, outer->outer_idx);
6407 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006408 break;
6409 case ISN_STOREV:
6410 smsg("%s%4d STOREV v:%s", pfx, current,
6411 get_vim_var_name(iptr->isn_arg.number));
6412 break;
6413 case ISN_STOREAUTO:
6414 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6415 break;
6416 case ISN_STOREG:
6417 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6418 break;
6419 case ISN_STOREB:
6420 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6421 break;
6422 case ISN_STOREW:
6423 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6424 break;
6425 case ISN_STORET:
6426 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6427 break;
6428 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006429 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006430 {
6431 scriptitem_T *si = SCRIPT_ITEM(
6432 iptr->isn_arg.loadstore.ls_sid);
6433
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006434 smsg("%s%4d %s %s in %s", pfx, current,
6435 iptr->isn_type == ISN_STORES
6436 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006437 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6438 }
6439 break;
6440 case ISN_STORESCRIPT:
6441 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006442 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6443 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6444 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006445
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006446 sv = get_script_svar(sref, -1);
6447 if (sv == NULL)
6448 smsg("%s%4d STORESCRIPT [deleted] in %s",
6449 pfx, current, si->sn_name);
6450 else
6451 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006452 sv->sv_name,
6453 sref->sref_idx,
6454 si->sn_name);
6455 }
6456 break;
6457 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006458 case ISN_STOREFUNCOPT:
6459 smsg("%s%4d %s &%s", pfx, current,
6460 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006461 iptr->isn_arg.storeopt.so_name);
6462 break;
6463 case ISN_STOREENV:
6464 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6465 break;
6466 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006467 smsg("%s%4d STOREREG @%c", pfx, current,
6468 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006469 break;
6470 case ISN_STORENR:
6471 smsg("%s%4d STORE %lld in $%d", pfx, current,
6472 iptr->isn_arg.storenr.stnr_val,
6473 iptr->isn_arg.storenr.stnr_idx);
6474 break;
6475
6476 case ISN_STOREINDEX:
6477 smsg("%s%4d STOREINDEX %s", pfx, current,
6478 vartype_name(iptr->isn_arg.vartype));
6479 break;
6480
6481 case ISN_STORERANGE:
6482 smsg("%s%4d STORERANGE", pfx, current);
6483 break;
6484
Bram Moolenaard505d172022-12-18 21:42:55 +00006485 case ISN_LOAD_CLASSMEMBER:
6486 case ISN_STORE_CLASSMEMBER:
6487 {
6488 class_T *cl = iptr->isn_arg.classmember.cm_class;
6489 int idx = iptr->isn_arg.classmember.cm_idx;
6490 ocmember_T *ocm = &cl->class_class_members[idx];
6491 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6492 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6493 ? "LOAD" : "STORE",
6494 cl->class_name, ocm->ocm_name);
6495 }
6496 break;
6497
Bram Moolenaar4c137212021-04-19 16:48:48 +02006498 // constants
6499 case ISN_PUSHNR:
6500 smsg("%s%4d PUSHNR %lld", pfx, current,
6501 (varnumber_T)(iptr->isn_arg.number));
6502 break;
6503 case ISN_PUSHBOOL:
6504 case ISN_PUSHSPEC:
6505 smsg("%s%4d PUSH %s", pfx, current,
6506 get_var_special_name(iptr->isn_arg.number));
6507 break;
6508 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006509 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006510 break;
6511 case ISN_PUSHS:
6512 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6513 break;
6514 case ISN_PUSHBLOB:
6515 {
6516 char_u *r;
6517 char_u numbuf[NUMBUFLEN];
6518 char_u *tofree;
6519
6520 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6521 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6522 vim_free(tofree);
6523 }
6524 break;
6525 case ISN_PUSHFUNC:
6526 {
6527 char *name = (char *)iptr->isn_arg.string;
6528
6529 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6530 name == NULL ? "[none]" : name);
6531 }
6532 break;
6533 case ISN_PUSHCHANNEL:
6534#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006535 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006536#endif
6537 break;
6538 case ISN_PUSHJOB:
6539#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006540 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006541#endif
6542 break;
6543 case ISN_PUSHEXC:
6544 smsg("%s%4d PUSH v:exception", pfx, current);
6545 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006546 case ISN_AUTOLOAD:
6547 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6548 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006549 case ISN_UNLET:
6550 smsg("%s%4d UNLET%s %s", pfx, current,
6551 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6552 iptr->isn_arg.unlet.ul_name);
6553 break;
6554 case ISN_UNLETENV:
6555 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6556 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6557 iptr->isn_arg.unlet.ul_name);
6558 break;
6559 case ISN_UNLETINDEX:
6560 smsg("%s%4d UNLETINDEX", pfx, current);
6561 break;
6562 case ISN_UNLETRANGE:
6563 smsg("%s%4d UNLETRANGE", pfx, current);
6564 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006565 case ISN_LOCKUNLOCK:
6566 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6567 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006568 case ISN_LOCKCONST:
6569 smsg("%s%4d LOCKCONST", pfx, current);
6570 break;
6571 case ISN_NEWLIST:
6572 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006573 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006574 break;
6575 case ISN_NEWDICT:
6576 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006577 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006578 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006579 case ISN_NEWPARTIAL:
6580 smsg("%s%4d NEWPARTIAL", pfx, current);
6581 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006582
6583 // function call
6584 case ISN_BCALL:
6585 {
6586 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6587
6588 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6589 internal_func_name(cbfunc->cbf_idx),
6590 cbfunc->cbf_argcount);
6591 }
6592 break;
6593 case ISN_DCALL:
6594 {
6595 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6596 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6597 + cdfunc->cdf_idx;
6598
6599 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006600 printable_func_name(df->df_ufunc),
6601 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006602 }
6603 break;
6604 case ISN_UCALL:
6605 {
6606 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6607
6608 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6609 cufunc->cuf_name, cufunc->cuf_argcount);
6610 }
6611 break;
6612 case ISN_PCALL:
6613 {
6614 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6615
6616 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6617 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6618 }
6619 break;
6620 case ISN_PCALL_END:
6621 smsg("%s%4d PCALL end", pfx, current);
6622 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006623 case ISN_DEFER:
6624 smsg("%s%4d DEFER %d args", pfx, current,
6625 (int)iptr->isn_arg.defer.defer_argcount);
6626 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006627 case ISN_RETURN:
6628 smsg("%s%4d RETURN", pfx, current);
6629 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006630 case ISN_RETURN_VOID:
6631 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006632 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006633 case ISN_RETURN_OBJECT:
6634 smsg("%s%4d RETURN object", pfx, current);
6635 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006636 case ISN_FUNCREF:
6637 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006638 funcref_T *funcref = &iptr->isn_arg.funcref;
6639 funcref_extra_T *extra = funcref->fr_extra;
6640 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006641
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006642 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006643 {
6644 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6645 + funcref->fr_dfunc_idx;
6646 name = df->df_ufunc->uf_name;
6647 }
6648 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006649 name = extra->fre_func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006650 if (extra == NULL || extra->fre_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006651 smsg("%s%4d FUNCREF %s", pfx, current, name);
6652 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006653 {
6654 char_u *info = printable_loopvarinfo(
6655 &extra->fre_loopvar_info);
6656
6657 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6658 name, info);
6659 vim_free(info);
6660 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006661 }
6662 break;
6663
6664 case ISN_NEWFUNC:
6665 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006666 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006667
Bram Moolenaarcc341812022-09-19 15:54:34 +01006668 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006669 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6670 arg->nfa_lambda, arg->nfa_global);
6671 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006672 {
6673 char_u *info = printable_loopvarinfo(
6674 &arg->nfa_loopvar_info);
6675
6676 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6677 arg->nfa_lambda, arg->nfa_global, info);
6678 vim_free(info);
6679 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006680 }
6681 break;
6682
6683 case ISN_DEF:
6684 {
6685 char_u *name = iptr->isn_arg.string;
6686
6687 smsg("%s%4d DEF %s", pfx, current,
6688 name == NULL ? (char_u *)"" : name);
6689 }
6690 break;
6691
6692 case ISN_JUMP:
6693 {
6694 char *when = "?";
6695
6696 switch (iptr->isn_arg.jump.jump_when)
6697 {
6698 case JUMP_ALWAYS:
6699 when = "JUMP";
6700 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006701 case JUMP_NEVER:
6702 iemsg("JUMP_NEVER should not be used");
6703 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006704 case JUMP_AND_KEEP_IF_TRUE:
6705 when = "JUMP_AND_KEEP_IF_TRUE";
6706 break;
6707 case JUMP_IF_FALSE:
6708 when = "JUMP_IF_FALSE";
6709 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006710 case JUMP_WHILE_FALSE:
6711 when = "JUMP_WHILE_FALSE"; // unused
6712 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006713 case JUMP_IF_COND_FALSE:
6714 when = "JUMP_IF_COND_FALSE";
6715 break;
6716 case JUMP_IF_COND_TRUE:
6717 when = "JUMP_IF_COND_TRUE";
6718 break;
6719 }
6720 smsg("%s%4d %s -> %d", pfx, current, when,
6721 iptr->isn_arg.jump.jump_where);
6722 }
6723 break;
6724
6725 case ISN_JUMP_IF_ARG_SET:
6726 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6727 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6728 iptr->isn_arg.jump.jump_where);
6729 break;
6730
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006731 case ISN_JUMP_IF_ARG_NOT_SET:
6732 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6733 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6734 iptr->isn_arg.jump.jump_where);
6735 break;
6736
Bram Moolenaar4c137212021-04-19 16:48:48 +02006737 case ISN_FOR:
6738 {
6739 forloop_T *forloop = &iptr->isn_arg.forloop;
6740
6741 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006742 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006743 }
6744 break;
6745
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006746 case ISN_ENDLOOP:
6747 {
6748 endloop_T *endloop = &iptr->isn_arg.endloop;
6749
Bram Moolenaarcc341812022-09-19 15:54:34 +01006750 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6751 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006752 endloop->end_funcref_idx,
6753 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006754 endloop->end_var_idx + endloop->end_var_count - 1,
6755 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006756 }
6757 break;
6758
6759 case ISN_WHILE:
6760 {
6761 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6762
6763 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6764 whileloop->while_funcref_idx,
6765 whileloop->while_end);
6766 }
6767 break;
6768
Bram Moolenaar4c137212021-04-19 16:48:48 +02006769 case ISN_TRY:
6770 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006771 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006772
6773 if (try->try_ref->try_finally == 0)
6774 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6775 pfx, current,
6776 try->try_ref->try_catch,
6777 try->try_ref->try_endtry);
6778 else
6779 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6780 pfx, current,
6781 try->try_ref->try_catch,
6782 try->try_ref->try_finally,
6783 try->try_ref->try_endtry);
6784 }
6785 break;
6786 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006787 smsg("%s%4d CATCH", pfx, current);
6788 break;
6789 case ISN_TRYCONT:
6790 {
6791 trycont_T *trycont = &iptr->isn_arg.trycont;
6792
6793 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6794 trycont->tct_levels,
6795 trycont->tct_levels == 1 ? "" : "s",
6796 trycont->tct_where);
6797 }
6798 break;
6799 case ISN_FINALLY:
6800 smsg("%s%4d FINALLY", pfx, current);
6801 break;
6802 case ISN_ENDTRY:
6803 smsg("%s%4d ENDTRY", pfx, current);
6804 break;
6805 case ISN_THROW:
6806 smsg("%s%4d THROW", pfx, current);
6807 break;
6808
6809 // expression operations on number
6810 case ISN_OPNR:
6811 case ISN_OPFLOAT:
6812 case ISN_OPANY:
6813 {
6814 char *what;
6815 char *ins;
6816
6817 switch (iptr->isn_arg.op.op_type)
6818 {
6819 case EXPR_MULT: what = "*"; break;
6820 case EXPR_DIV: what = "/"; break;
6821 case EXPR_REM: what = "%"; break;
6822 case EXPR_SUB: what = "-"; break;
6823 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006824 case EXPR_LSHIFT: what = "<<"; break;
6825 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006826 default: what = "???"; break;
6827 }
6828 switch (iptr->isn_type)
6829 {
6830 case ISN_OPNR: ins = "OPNR"; break;
6831 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6832 case ISN_OPANY: ins = "OPANY"; break;
6833 default: ins = "???"; break;
6834 }
6835 smsg("%s%4d %s %s", pfx, current, ins, what);
6836 }
6837 break;
6838
6839 case ISN_COMPAREBOOL:
6840 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006841 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006842 case ISN_COMPARENR:
6843 case ISN_COMPAREFLOAT:
6844 case ISN_COMPARESTRING:
6845 case ISN_COMPAREBLOB:
6846 case ISN_COMPARELIST:
6847 case ISN_COMPAREDICT:
6848 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00006849 case ISN_COMPARECLASS:
6850 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006851 case ISN_COMPAREANY:
6852 {
6853 char *p;
6854 char buf[10];
6855 char *type;
6856
6857 switch (iptr->isn_arg.op.op_type)
6858 {
6859 case EXPR_EQUAL: p = "=="; break;
6860 case EXPR_NEQUAL: p = "!="; break;
6861 case EXPR_GREATER: p = ">"; break;
6862 case EXPR_GEQUAL: p = ">="; break;
6863 case EXPR_SMALLER: p = "<"; break;
6864 case EXPR_SEQUAL: p = "<="; break;
6865 case EXPR_MATCH: p = "=~"; break;
6866 case EXPR_IS: p = "is"; break;
6867 case EXPR_ISNOT: p = "isnot"; break;
6868 case EXPR_NOMATCH: p = "!~"; break;
6869 default: p = "???"; break;
6870 }
6871 STRCPY(buf, p);
6872 if (iptr->isn_arg.op.op_ic == TRUE)
6873 strcat(buf, "?");
6874 switch(iptr->isn_type)
6875 {
6876 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6877 case ISN_COMPARESPECIAL:
6878 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006879 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006880 case ISN_COMPARENR: type = "COMPARENR"; break;
6881 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6882 case ISN_COMPARESTRING:
6883 type = "COMPARESTRING"; break;
6884 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6885 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6886 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6887 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00006888 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
6889 case ISN_COMPAREOBJECT:
6890 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006891 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6892 default: type = "???"; break;
6893 }
6894
6895 smsg("%s%4d %s %s", pfx, current, type, buf);
6896 }
6897 break;
6898
6899 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6900 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6901
6902 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006903 case ISN_CONCAT:
6904 smsg("%s%4d CONCAT size %lld", pfx, current,
6905 (varnumber_T)(iptr->isn_arg.number));
6906 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006907 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6908 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6909 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6910 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6911 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6912 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6913 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6914 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6915 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6916 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6917 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006918 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006919 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6920 iptr->isn_arg.getitem.gi_index,
6921 iptr->isn_arg.getitem.gi_with_op ?
6922 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006923 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6924 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6925 iptr->isn_arg.string); break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00006926 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
6927 (int)iptr->isn_arg.number); break;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00006928 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
6929 pfx, current,
6930 (int)iptr->isn_arg.classmember.cm_idx,
6931 iptr->isn_arg.classmember.cm_class->class_name);
6932 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00006933 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006934 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006935 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6936 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6937
Bram Moolenaar4c137212021-04-19 16:48:48 +02006938 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6939
Bram Moolenaar4c137212021-04-19 16:48:48 +02006940 case ISN_CHECKTYPE:
6941 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006942 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006943 char *tofree = NULL;
6944 char *typename;
6945
6946 if (ct->ct_type->tt_type == VAR_FLOAT
6947 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
6948 typename = "float|number";
6949 else
6950 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006951
6952 if (ct->ct_arg_idx == 0)
6953 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006954 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006955 (int)ct->ct_off);
6956 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006957 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006958 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006959 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006960 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006961 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006962 (int)ct->ct_arg_idx);
6963 vim_free(tofree);
6964 break;
6965 }
6966 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6967 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6968 iptr->isn_arg.checklen.cl_min_len);
6969 break;
6970 case ISN_SETTYPE:
6971 {
6972 char *tofree;
6973
6974 smsg("%s%4d SETTYPE %s", pfx, current,
6975 type_name(iptr->isn_arg.type.ct_type, &tofree));
6976 vim_free(tofree);
6977 break;
6978 }
6979 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006980 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6981 smsg("%s%4d INVERT %d (!val)", pfx, current,
6982 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006983 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006984 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6985 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006986 break;
6987 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006988 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006989 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006990 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6991 pfx, current,
6992 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006993 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006994 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6995 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006996 break;
6997 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006998 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006999 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007000 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007001 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7002 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007003 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007004 else
7005 smsg("%s%4d PUT %c %ld", pfx, current,
7006 iptr->isn_arg.put.put_regname,
7007 (long)iptr->isn_arg.put.put_lnum);
7008 break;
7009
Bram Moolenaar4c137212021-04-19 16:48:48 +02007010 case ISN_CMDMOD:
7011 {
7012 char_u *buf;
7013 size_t len = produce_cmdmods(
7014 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7015
7016 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007017 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007018 {
7019 (void)produce_cmdmods(
7020 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7021 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7022 vim_free(buf);
7023 }
7024 break;
7025 }
7026 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7027
7028 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007029 smsg("%s%4d PROFILE START line %d", pfx, current,
7030 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007031 break;
7032
7033 case ISN_PROF_END:
7034 smsg("%s%4d PROFILE END", pfx, current);
7035 break;
7036
Bram Moolenaare99d4222021-06-13 14:01:26 +02007037 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007038 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7039 iptr->isn_arg.debug.dbg_break_lnum + 1,
7040 iptr->isn_lnum,
7041 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007042 break;
7043
Bram Moolenaar4c137212021-04-19 16:48:48 +02007044 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7045 iptr->isn_arg.unpack.unp_count,
7046 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7047 break;
7048 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7049 iptr->isn_arg.shuffle.shfl_item,
7050 iptr->isn_arg.shuffle.shfl_up);
7051 break;
7052 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7053
7054 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7055 return;
7056 }
7057
7058 out_flush(); // output one line at a time
7059 ui_breakcheck();
7060 if (got_int)
7061 break;
7062 }
7063}
7064
7065/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007066 * Handle command line completion for the :disassemble command.
7067 */
7068 void
7069set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7070{
7071 char_u *p;
7072
7073 // Default: expand user functions, "debug" and "profile"
7074 xp->xp_context = EXPAND_DISASSEMBLE;
7075 xp->xp_pattern = arg;
7076
7077 // first argument already typed: only user function names
7078 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7079 {
7080 xp->xp_context = EXPAND_USER_FUNC;
7081 xp->xp_pattern = skipwhite(p);
7082 }
7083}
7084
7085/*
7086 * Function given to ExpandGeneric() to obtain the list of :disassemble
7087 * arguments.
7088 */
7089 char_u *
7090get_disassemble_argument(expand_T *xp, int idx)
7091{
7092 if (idx == 0)
7093 return (char_u *)"debug";
7094 if (idx == 1)
7095 return (char_u *)"profile";
7096 return get_user_func_name(xp, idx - 2);
7097}
7098
7099/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007100 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007101 * We don't really need this at runtime, but we do have tests that require it,
7102 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 */
7104 void
7105ex_disassemble(exarg_T *eap)
7106{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007107 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007108 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007109 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007110 isn_T *instr = NULL; // init to shut up gcc warning
7111 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007112 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007113
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007114 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007115 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007116 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007117 if (func_needs_compiling(ufunc, compile_type)
7118 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007119 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007120 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007122 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123 return;
7124 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007125 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126
7127 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007128 switch (compile_type)
7129 {
7130 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007131#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007132 instr = dfunc->df_instr_prof;
7133 instr_count = dfunc->df_instr_prof_count;
7134 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007135#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007136 // FALLTHROUGH
7137 case CT_NONE:
7138 instr = dfunc->df_instr;
7139 instr_count = dfunc->df_instr_count;
7140 break;
7141 case CT_DEBUG:
7142 instr = dfunc->df_instr_debug;
7143 instr_count = dfunc->df_instr_debug_count;
7144 break;
7145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146
Bram Moolenaar4c137212021-04-19 16:48:48 +02007147 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007148}
7149
7150/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007151 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152 * list, etc. Mostly like what JavaScript does, except that empty list and
7153 * empty dictionary are FALSE.
7154 */
7155 int
7156tv2bool(typval_T *tv)
7157{
7158 switch (tv->v_type)
7159 {
7160 case VAR_NUMBER:
7161 return tv->vval.v_number != 0;
7162 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 case VAR_PARTIAL:
7165 return tv->vval.v_partial != NULL;
7166 case VAR_FUNC:
7167 case VAR_STRING:
7168 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7169 case VAR_LIST:
7170 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7171 case VAR_DICT:
7172 return tv->vval.v_dict != NULL
7173 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7174 case VAR_BOOL:
7175 case VAR_SPECIAL:
7176 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7177 case VAR_JOB:
7178#ifdef FEAT_JOB_CHANNEL
7179 return tv->vval.v_job != NULL;
7180#else
7181 break;
7182#endif
7183 case VAR_CHANNEL:
7184#ifdef FEAT_JOB_CHANNEL
7185 return tv->vval.v_channel != NULL;
7186#else
7187 break;
7188#endif
7189 case VAR_BLOB:
7190 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7191 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007192 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007193 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007194 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007195 case VAR_CLASS:
7196 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007197 break;
7198 }
7199 return FALSE;
7200}
7201
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007202 void
7203emsg_using_string_as(typval_T *tv, int as_number)
7204{
7205 semsg(_(as_number ? e_using_string_as_number_str
7206 : e_using_string_as_bool_str),
7207 tv->vval.v_string == NULL
7208 ? (char_u *)"" : tv->vval.v_string);
7209}
7210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211/*
7212 * If "tv" is a string give an error and return FAIL.
7213 */
7214 int
7215check_not_string(typval_T *tv)
7216{
7217 if (tv->v_type == VAR_STRING)
7218 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007219 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007220 clear_tv(tv);
7221 return FAIL;
7222 }
7223 return OK;
7224}
7225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007226
7227#endif // FEAT_EVAL