blob: 3c4f1a2583b342b2a41c91de78318d598ee746bb [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 Moolenaarec15b1c2022-03-27 16:29:53 +0100203 if (list != NULL)
204 ++list->lv_refcount;
205 return OK;
206}
207
208/*
209 * Implementation of ISN_NEWDICT.
210 * Returns FAIL on total failure, MAYBE on error.
211 */
212 static int
213exe_newdict(int count, ectx_T *ectx)
214{
215 dict_T *dict = NULL;
216 dictitem_T *item;
217 char_u *key;
218 int idx;
219 typval_T *tv;
220
221 if (count >= 0)
222 {
223 dict = dict_alloc();
224 if (unlikely(dict == NULL))
225 return FAIL;
226 for (idx = 0; idx < count; ++idx)
227 {
228 // have already checked key type is VAR_STRING
229 tv = STACK_TV_BOT(2 * (idx - count));
230 // check key is unique
231 key = tv->vval.v_string == NULL
232 ? (char_u *)"" : tv->vval.v_string;
233 item = dict_find(dict, key, -1);
234 if (item != NULL)
235 {
236 semsg(_(e_duplicate_key_in_dicitonary), key);
237 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 Moolenaar8a7d6542020-01-26 15:56:19 +0100379 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100380 * This adds a stack frame and sets the instruction pointer to the start of the
381 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200382 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 *
384 * Stack has:
385 * - current arguments (already there)
386 * - omitted optional argument (default values) added here
387 * - stack frame:
388 * - pointer to calling function
389 * - Index of next instruction in calling function
390 * - previous frame pointer
391 * - reserved space for local variables
392 */
393 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394call_dfunc(
395 int cdf_idx,
396 partial_T *pt,
397 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100400 int argcount = argcount_arg;
401 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
402 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200403 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100404 int arg_to_add;
405 int vararg_count = 0;
406 int varcount;
407 int idx;
408 estack_T *entry;
409 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200410 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000411 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412
413 if (dfunc->df_deleted)
414 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100415 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000416 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100417 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 return FAIL;
419 }
420
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100421#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100422 if (do_profiling == PROF_YES)
423 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200424 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100425 {
426 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
427 + profile_info_ga.ga_len;
428 ++profile_info_ga.ga_len;
429 CLEAR_POINTER(info);
430 profile_may_start_func(info, ufunc,
431 (((dfunc_T *)def_functions.ga_data)
432 + ectx->ec_dfunc_idx)->df_ufunc);
433 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100434 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100435#endif
436
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200437 // When debugging and using "cont" switches to the not-debugged
438 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000439 compile_type = get_compile_type(ufunc);
440 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200441 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000442 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200443
444 // compile_def_function() may cause def_functions.ga_data to change
445 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
446 }
447 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200448 {
449 if (did_emsg_cumul + did_emsg == did_emsg_before)
450 semsg(_(e_function_is_not_compiled_str),
451 printable_func_name(ufunc));
452 return FAIL;
453 }
454
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200455 if (ufunc->uf_va_name != NULL)
456 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200457 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200458 // Stack at time of call with 2 varargs:
459 // normal_arg
460 // optional_arg
461 // vararg_1
462 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200463 // After creating the list:
464 // normal_arg
465 // optional_arg
466 // vararg-list
467 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200468 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200469 // After creating the list
470 // normal_arg
471 // (space for optional_arg)
472 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200473 vararg_count = argcount - ufunc->uf_args.ga_len;
474 if (vararg_count < 0)
475 vararg_count = 0;
476 else
477 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200478 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200479 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200480
481 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200482 }
483
Bram Moolenaarfe270812020-04-11 22:31:27 +0200484 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200485 if (arg_to_add < 0)
486 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100487 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
488 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200489 return FAIL;
490 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000491 else if (arg_to_add > ufunc->uf_def_args.ga_len)
492 {
493 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
494
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100495 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
496 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000497 return FAIL;
498 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200499
500 // Reserve space for:
501 // - missing arguments
502 // - stack frame
503 // - local variables
504 // - if needed: a counter for number of closures created in
505 // ectx->ec_funcrefs.
506 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200507 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 return FAIL;
509
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100510 // If depth of calling is getting too high, don't execute the function.
511 if (funcdepth_increment() == FAIL)
512 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200513 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100514
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100515 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200516 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100517 {
518 floc = ALLOC_ONE(funclocal_T);
519 if (floc == NULL)
520 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200521 *floc = ectx->ec_funclocal;
522 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100523 }
524
Bram Moolenaarfe270812020-04-11 22:31:27 +0200525 // Move the vararg-list to below the missing optional arguments.
526 if (vararg_count > 0 && arg_to_add > 0)
527 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100528
529 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200530 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200531 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200532 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533
534 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100535 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
536 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200537 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200538 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
539 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100540 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100541 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200542 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543
544 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200545 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000546 {
547 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
548
549 tv->v_type = VAR_NUMBER;
550 tv->vval.v_number = 0;
551 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200552 if (dfunc->df_has_closure)
553 {
554 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
555
556 tv->v_type = VAR_NUMBER;
557 tv->vval.v_number = 0;
558 }
559 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100561 if (pt != NULL || ufunc->uf_partial != NULL
562 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100563 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200564 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100565
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200566 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100567 return FAIL;
568 if (pt != NULL)
569 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000570 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200571 ++pt->pt_refcount;
572 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100573 }
574 else if (ufunc->uf_partial != NULL)
575 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000576 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200577 ++ufunc->uf_partial->pt_refcount;
578 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100579 }
580 else
581 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200582 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200583 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200584 {
585 vim_free(ref);
586 return FAIL;
587 }
588 ref->or_outer_allocated = TRUE;
589 ref->or_outer->out_stack = &ectx->ec_stack;
590 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
591 if (ectx->ec_outer_ref != NULL)
592 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100593 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200594 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100595 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100596 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200597 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100598
Bram Moolenaarc970e422021-03-17 15:03:04 +0100599 ++ufunc->uf_calls;
600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 // Set execution state to the start of the called function.
602 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100603 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100604 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200605 if (entry != NULL)
606 {
607 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200608 // Save the current context so it can be restored on return.
609 entry->es_save_sctx = current_sctx;
610 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200611 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100612
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200613 // Start execution at the first instruction.
614 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615
616 return OK;
617}
618
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000619// Double linked list of funcstack_T in use.
620static funcstack_T *first_funcstack = NULL;
621
622 static void
623add_funcstack_to_list(funcstack_T *funcstack)
624{
625 // Link in list of funcstacks.
626 if (first_funcstack != NULL)
627 first_funcstack->fs_prev = funcstack;
628 funcstack->fs_next = first_funcstack;
629 funcstack->fs_prev = NULL;
630 first_funcstack = funcstack;
631}
632
633 static void
634remove_funcstack_from_list(funcstack_T *funcstack)
635{
636 if (funcstack->fs_prev == NULL)
637 first_funcstack = funcstack->fs_next;
638 else
639 funcstack->fs_prev->fs_next = funcstack->fs_next;
640 if (funcstack->fs_next != NULL)
641 funcstack->fs_next->fs_prev = funcstack->fs_prev;
642}
643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200645 * Used when returning from a function: Check if any closure is still
646 * referenced. If so then move the arguments and variables to a separate piece
647 * of stack to be used when the closure is called.
648 * When "free_arguments" is TRUE the arguments are to be freed.
649 * Returns FAIL when out of memory.
650 */
651 static int
652handle_closure_in_use(ectx_T *ectx, int free_arguments)
653{
654 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
655 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200656 int argcount;
657 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200658 int idx;
659 typval_T *tv;
660 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200661 garray_T *gap = &ectx->ec_funcrefs;
662 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200663
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200664 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200665 return OK; // function was freed
666 if (dfunc->df_has_closure == 0)
667 return OK; // no closures
668 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
669 closure_count = tv->vval.v_number;
670 if (closure_count == 0)
671 return OK; // no funcrefs created
672
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200673 argcount = ufunc_argcount(dfunc->df_ufunc);
674 top = ectx->ec_frame_idx - argcount;
675
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200676 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200677 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200678 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200679 partial_T *pt;
680 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200681
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200682 if (off < 0)
683 continue; // count is off or already done
684 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200685 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200686 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200687 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200688 int i;
689
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000690 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200691 // unreferenced on return.
692 for (i = 0; i < dfunc->df_varcount; ++i)
693 {
694 typval_T *stv = STACK_TV(ectx->ec_frame_idx
695 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200696 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200697 --refcount;
698 }
699 if (refcount > 1)
700 {
701 closure_in_use = TRUE;
702 break;
703 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200704 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200705 }
706
707 if (closure_in_use)
708 {
709 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
710 typval_T *stack;
711
712 // A closure is using the arguments and/or local variables.
713 // Move them to the called function.
714 if (funcstack == NULL)
715 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000716
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200717 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
718 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200719 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
720 funcstack->fs_ga.ga_data = stack;
721 if (stack == NULL)
722 {
723 vim_free(funcstack);
724 return FAIL;
725 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000726 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200727
728 // Move or copy the arguments.
729 for (idx = 0; idx < argcount; ++idx)
730 {
731 tv = STACK_TV(top + idx);
732 if (free_arguments)
733 {
734 *(stack + idx) = *tv;
735 tv->v_type = VAR_UNKNOWN;
736 }
737 else
738 copy_tv(tv, stack + idx);
739 }
740 // Move the local variables.
741 for (idx = 0; idx < dfunc->df_varcount; ++idx)
742 {
743 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200744
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200745 // A partial created for a local function, that is also used as a
746 // local variable, has a reference count for the variable, thus
747 // will never go down to zero. When all these refcounts are one
748 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000749 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200750 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
751 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200752 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200753
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200754 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200755 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
756 gap->ga_len - closure_count + i])
757 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200758 }
759
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200760 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200761 tv->v_type = VAR_UNKNOWN;
762 }
763
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200764 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200765 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200766 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
767 - closure_count + idx];
768 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200769 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200770 ++funcstack->fs_refcount;
771 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100772 pt->pt_outer.out_stack = &funcstack->fs_ga;
773 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200774 }
775 }
776 }
777
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200778 for (idx = 0; idx < closure_count; ++idx)
779 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
780 - closure_count + idx]);
781 gap->ga_len -= closure_count;
782 if (gap->ga_len == 0)
783 ga_clear(gap);
784
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200785 return OK;
786}
787
788/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200789 * Called when a partial is freed or its reference count goes down to one. The
790 * funcstack may be the only reference to the partials in the local variables.
791 * Go over all of them, the funcref and can be freed if all partials
792 * referencing the funcstack have a reference count of one.
793 */
794 void
795funcstack_check_refcount(funcstack_T *funcstack)
796{
797 int i;
798 garray_T *gap = &funcstack->fs_ga;
799 int done = 0;
800
801 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
802 return;
803 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
804 {
805 typval_T *tv = ((typval_T *)gap->ga_data) + i;
806
807 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
808 && tv->vval.v_partial->pt_funcstack == funcstack
809 && tv->vval.v_partial->pt_refcount == 1)
810 ++done;
811 }
812 if (done == funcstack->fs_min_refcount)
813 {
814 typval_T *stack = gap->ga_data;
815
816 // All partials referencing the funcstack have a reference count of
817 // one, thus the funcstack is no longer of use.
818 for (i = 0; i < gap->ga_len; ++i)
819 clear_tv(stack + i);
820 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000821 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200822 vim_free(funcstack);
823 }
824}
825
826/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000827 * For garbage collecting: set references in all variables referenced by
828 * all funcstacks.
829 */
830 int
831set_ref_in_funcstacks(int copyID)
832{
833 funcstack_T *funcstack;
834
835 for (funcstack = first_funcstack; funcstack != NULL;
836 funcstack = funcstack->fs_next)
837 {
838 typval_T *stack = funcstack->fs_ga.ga_data;
839 int i;
840
841 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
842 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
843 return TRUE; // abort
844 }
845 return FALSE;
846}
847
848/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100849 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
850 * The local variable that lists deferred functions is "var_idx".
851 * Returns OK or FAIL.
852 */
853 static int
854add_defer_func(int var_idx, int argcount, ectx_T *ectx)
855{
856 typval_T *defer_tv = STACK_TV_VAR(var_idx);
857 list_T *defer_l;
858 typval_T *func_tv;
859 list_T *l;
860 int i;
861 typval_T listval;
862
863 if (defer_tv->v_type != VAR_LIST)
864 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100865 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100866 if (rettv_list_alloc(defer_tv) == FAIL)
867 return FAIL;
868 }
869 defer_l = defer_tv->vval.v_list;
870
871 l = list_alloc_with_items(argcount + 1);
872 if (l == NULL)
873 return FAIL;
874 listval.v_type = VAR_LIST;
875 listval.vval.v_list = l;
876 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100877 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100878 {
879 vim_free(l);
880 return FAIL;
881 }
882
883 func_tv = STACK_TV_BOT(-argcount - 1);
884 // TODO: check type is a funcref
885 list_set_item(l, 0, func_tv);
886
887 for (i = 1; i <= argcount; ++i)
888 list_set_item(l, i, STACK_TV_BOT(-argcount + i - 1));
889 ectx->ec_stack.ga_len -= argcount + 1;
890 return OK;
891}
892
893/*
894 * Invoked when returning from a function: Invoke any deferred calls.
895 */
896 static void
897invoke_defer_funcs(ectx_T *ectx)
898{
899 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
900 + ectx->ec_dfunc_idx;
901 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
902 listitem_T *li;
903
904 if (defer_tv->v_type != VAR_LIST)
905 return; // no function added
906 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
907 {
908 list_T *l = li->li_tv.vval.v_list;
909 typval_T rettv;
910 typval_T argvars[MAX_FUNC_ARGS];
911 int i;
912 listitem_T *arg_li = l->lv_first;
913 funcexe_T funcexe;
914
915 for (i = 0; i < l->lv_len - 1; ++i)
916 {
917 arg_li = arg_li->li_next;
918 argvars[i] = arg_li->li_tv;
919 }
920
921 CLEAR_FIELD(funcexe);
922 funcexe.fe_evaluate = TRUE;
923 rettv.v_type = VAR_UNKNOWN;
924 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
925 &rettv, l->lv_len - 1, argvars, &funcexe);
926 clear_tv(&rettv);
927 }
928}
929
930/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 * Return from the current function.
932 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200933 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200934func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100937 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200938 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
939 + ectx->ec_dfunc_idx;
940 int argcount = ufunc_argcount(dfunc->df_ufunc);
941 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200942 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100943 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
944 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200945 funclocal_T *floc;
946#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100947 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
948 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949
Bram Moolenaar12d26532021-02-19 19:13:21 +0100950 if (do_profiling == PROF_YES)
951 {
952 ufunc_T *caller = prev_dfunc->df_ufunc;
953
954 if (dfunc->df_ufunc->uf_profiling
955 || (caller != NULL && caller->uf_profiling))
956 {
957 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
958 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
959 --profile_info_ga.ga_len;
960 }
961 }
962#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000963
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100964 if (dfunc->df_defer_var_idx > 0)
965 invoke_defer_funcs(ectx);
966
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000967 // No check for uf_refcount being zero, cannot think of a way that would
968 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100969 --dfunc->df_ufunc->uf_calls;
970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200972 entry = estack_pop();
973 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200974 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200976 if (handle_closure_in_use(ectx, TRUE) == FAIL)
977 return FAIL;
978
979 // Clear the arguments.
980 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
981 clear_tv(STACK_TV(idx));
982
983 // Clear local variables and temp values, but not the return value.
984 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100985 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100987
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100988 // The return value should be on top of the stack. However, when aborting
989 // it may not be there and ec_frame_idx is the top of the stack.
990 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100991 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100992 ret_idx = 0;
993
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200994 if (ectx->ec_outer_ref != NULL)
995 {
996 if (ectx->ec_outer_ref->or_outer_allocated)
997 vim_free(ectx->ec_outer_ref->or_outer);
998 partial_unref(ectx->ec_outer_ref->or_partial);
999 vim_free(ectx->ec_outer_ref);
1000 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001001
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001002 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001003 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001004 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1005 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001006 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1007 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001008 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001009 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001010 floc = (void *)STACK_TV(ectx->ec_frame_idx
1011 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001012 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001013 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1014 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001015
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001016 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001017 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001018 else
1019 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001020 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001021 vim_free(floc);
1022 }
1023
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001024 if (ret_idx > 0)
1025 {
1026 // Reset the stack to the position before the call, with a spot for the
1027 // return value, moved there from above the frame.
1028 ectx->ec_stack.ga_len = top + 1;
1029 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1030 }
1031 else
1032 // Reset the stack to the position before the call.
1033 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001034
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001035 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001036 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001037 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038}
1039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040/*
1041 * Prepare arguments and rettv for calling a builtin or user function.
1042 */
1043 static int
1044call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1045{
1046 int idx;
1047 typval_T *tv;
1048
1049 // Move arguments from bottom of the stack to argvars[] and add terminator.
1050 for (idx = 0; idx < argcount; ++idx)
1051 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1052 argvars[argcount].v_type = VAR_UNKNOWN;
1053
1054 // Result replaces the arguments on the stack.
1055 if (argcount > 0)
1056 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001057 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 return FAIL;
1059 else
1060 ++ectx->ec_stack.ga_len;
1061
1062 // Default return value is zero.
1063 tv = STACK_TV_BOT(-1);
1064 tv->v_type = VAR_NUMBER;
1065 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001066 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067
1068 return OK;
1069}
1070
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001071// Ugly global to avoid passing the execution context around through many
1072// layers.
1073static ectx_T *current_ectx = NULL;
1074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075/*
1076 * Call a builtin function by index.
1077 */
1078 static int
1079call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1080{
1081 typval_T argvars[MAX_FUNC_ARGS];
1082 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001083 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001084 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001085 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086
1087 if (call_prepare(argcount, argvars, ectx) == FAIL)
1088 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001089 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001091 // Call the builtin function. Set "current_ectx" so that when it
1092 // recursively invokes call_def_function() a closure context can be set.
1093 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001095 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001096 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097
1098 // Clear the arguments.
1099 for (idx = 0; idx < argcount; ++idx)
1100 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001101
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001102 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001103 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 return OK;
1105}
1106
1107/*
1108 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001109 * If the function is compiled this will add a stack frame and set the
1110 * instruction pointer at the start of the function.
1111 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001112 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001113 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114 */
1115 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001116call_ufunc(
1117 ufunc_T *ufunc,
1118 partial_T *pt,
1119 int argcount,
1120 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001121 isn_T *iptr,
1122 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123{
1124 typval_T argvars[MAX_FUNC_ARGS];
1125 funcexe_T funcexe;
1126 int error;
1127 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001128 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001129 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130
Bram Moolenaare99d4222021-06-13 14:01:26 +02001131 if (func_needs_compiling(ufunc, compile_type)
1132 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1133 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001134 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001135 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001136 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001137 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001138 if (error != FCERR_UNKNOWN)
1139 {
1140 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001141 semsg(_(e_too_many_arguments_for_function_str),
1142 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001143 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001144 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001145 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001146 return FAIL;
1147 }
1148
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001149 // The function has been compiled, can call it quickly. For a function
1150 // that was defined later: we can call it directly next time.
1151 if (iptr != NULL)
1152 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001153 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001154 iptr->isn_type = ISN_DCALL;
1155 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1156 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1157 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001158 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001159 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160
1161 if (call_prepare(argcount, argvars, ectx) == FAIL)
1162 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001163 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001164 funcexe.fe_evaluate = TRUE;
1165 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166
1167 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001169 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170
1171 // Clear the arguments.
1172 for (idx = 0; idx < argcount; ++idx)
1173 clear_tv(&argvars[idx]);
1174
1175 if (error != FCERR_NONE)
1176 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001177 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 return FAIL;
1179 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001180 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001181 // Error other than from calling the function itself.
1182 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 return OK;
1184}
1185
1186/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001187 * If command modifiers were applied restore them.
1188 */
1189 static void
1190may_restore_cmdmod(funclocal_T *funclocal)
1191{
1192 if (funclocal->floc_restore_cmdmod)
1193 {
1194 cmdmod.cmod_filter_regmatch.regprog = NULL;
1195 undo_cmdmod(&cmdmod);
1196 cmdmod = funclocal->floc_save_cmdmod;
1197 funclocal->floc_restore_cmdmod = FALSE;
1198 }
1199}
1200
1201/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001202 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1203 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001204 */
1205 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001206vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001207{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001208 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001209}
1210
1211/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 * Execute a function by "name".
1213 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001214 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 * Returns FAIL if not found without an error message.
1216 */
1217 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001218call_by_name(
1219 char_u *name,
1220 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001221 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001222 isn_T *iptr,
1223 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224{
1225 ufunc_T *ufunc;
1226
1227 if (builtin_function(name, -1))
1228 {
1229 int func_idx = find_internal_func(name);
1230
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001231 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001233 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 return FAIL;
1235 return call_bfunc(func_idx, argcount, ectx);
1236 }
1237
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001238 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001239
1240 if (ufunc == NULL)
1241 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001242 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001243
1244 if (script_autoload(name, TRUE))
1245 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001246 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001247
1248 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001249 return FAIL; // bail out if loading the script caused an error
1250 }
1251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001253 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001254 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001255 {
1256 int i;
1257 typval_T *argv = STACK_TV_BOT(0) - argcount;
1258
1259 // The function can change at runtime, check that the argument
1260 // types are correct.
1261 for (i = 0; i < argcount; ++i)
1262 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001263 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001264
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001265 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001266 type = ufunc->uf_arg_types[i];
1267 else if (ufunc->uf_va_type != NULL)
1268 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001269 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001270 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001271 return FAIL;
1272 }
1273 }
1274
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001275 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001276 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277
1278 return FAIL;
1279}
1280
1281 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001282call_partial(
1283 typval_T *tv,
1284 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001285 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001287 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001288 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001290 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001291 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292
1293 if (tv->v_type == VAR_PARTIAL)
1294 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001295 partial_T *pt = tv->vval.v_partial;
1296 int i;
1297
1298 if (pt->pt_argc > 0)
1299 {
1300 // Make space for arguments from the partial, shift the "argcount"
1301 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001302 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001303 return FAIL;
1304 for (i = 1; i <= argcount; ++i)
1305 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1306 ectx->ec_stack.ga_len += pt->pt_argc;
1307 argcount += pt->pt_argc;
1308
1309 // copy the arguments from the partial onto the stack
1310 for (i = 0; i < pt->pt_argc; ++i)
1311 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1312 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001313 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314
1315 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001316 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 name = pt->pt_name;
1319 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001320 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001322 if (name != NULL)
1323 {
1324 char_u fname_buf[FLEN_FIXED + 1];
1325 char_u *tofree = NULL;
1326 int error = FCERR_NONE;
1327 char_u *fname;
1328
1329 // May need to translate <SNR>123_ to K_SNR.
1330 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1331 if (error != FCERR_NONE)
1332 res = FAIL;
1333 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001334 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001335 vim_free(tofree);
1336 }
1337
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001338 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 {
1340 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001341 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001342 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 return FAIL;
1344 }
1345 return OK;
1346}
1347
1348/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001349 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1350 * TRUE.
1351 */
1352 static int
1353error_if_locked(int lock, char *error)
1354{
1355 if (lock & (VAR_LOCKED | VAR_FIXED))
1356 {
1357 emsg(_(error));
1358 return TRUE;
1359 }
1360 return FALSE;
1361}
1362
1363/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001364 * Give an error if "tv" is not a number and return FAIL.
1365 */
1366 static int
1367check_for_number(typval_T *tv)
1368{
1369 if (tv->v_type != VAR_NUMBER)
1370 {
1371 semsg(_(e_expected_str_but_got_str),
1372 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1373 return FAIL;
1374 }
1375 return OK;
1376}
1377
1378/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001379 * Store "tv" in variable "name".
1380 * This is for s: and g: variables.
1381 */
1382 static void
1383store_var(char_u *name, typval_T *tv)
1384{
1385 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001386 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001387
Bram Moolenaard877a572021-04-01 19:42:48 +02001388 if (tv->v_lock)
1389 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001390 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001391 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001392 restore_funccal();
1393}
1394
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001395/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001396 * Convert "tv" to a string.
1397 * Return FAIL if not allowed.
1398 */
1399 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001400do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001401{
1402 if (tv->v_type != VAR_STRING)
1403 {
1404 char_u *str;
1405
1406 if (is_2string_any)
1407 {
1408 switch (tv->v_type)
1409 {
1410 case VAR_SPECIAL:
1411 case VAR_BOOL:
1412 case VAR_NUMBER:
1413 case VAR_FLOAT:
1414 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001415
1416 case VAR_LIST:
1417 if (tolerant)
1418 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001419 char_u *s, *e, *p;
1420 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001421
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001422 ga_init2(&ga, sizeof(char_u *), 1);
1423
1424 // Convert to NL separated items, then
1425 // escape the items and replace the NL with
1426 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001427 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001428 if (str == NULL)
1429 return FAIL;
1430 s = str;
1431 while ((e = vim_strchr(s, '\n')) != NULL)
1432 {
1433 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001434 p = vim_strsave_fnameescape(s,
1435 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001436 if (p != NULL)
1437 {
1438 ga_concat(&ga, p);
1439 ga_concat(&ga, (char_u *)" ");
1440 vim_free(p);
1441 }
1442 s = e + 1;
1443 }
1444 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001445 clear_tv(tv);
1446 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001447 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001448 return OK;
1449 }
1450 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001451 default: to_string_error(tv->v_type);
1452 return FAIL;
1453 }
1454 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001455 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001456 clear_tv(tv);
1457 tv->v_type = VAR_STRING;
1458 tv->vval.v_string = str;
1459 }
1460 return OK;
1461}
1462
1463/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001464 * When the value of "sv" is a null list of dict, allocate it.
1465 */
1466 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001467allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001468{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001469 typval_T *tv = sv->sv_tv;
1470
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001471 switch (tv->v_type)
1472 {
1473 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001474 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001475 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001476 break;
1477 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001478 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001479 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001480 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001481 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001482 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001483 (void)rettv_blob_alloc(tv);
1484 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001485 default:
1486 break;
1487 }
1488}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001489
Bram Moolenaare7525c52021-01-09 13:20:37 +01001490/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001491 * Return the character "str[index]" where "index" is the character index,
1492 * including composing characters.
1493 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001494 */
1495 char_u *
1496char_from_string(char_u *str, varnumber_T index)
1497{
1498 size_t nbyte = 0;
1499 varnumber_T nchar = index;
1500 size_t slen;
1501
1502 if (str == NULL)
1503 return NULL;
1504 slen = STRLEN(str);
1505
Bram Moolenaarff871402021-03-26 13:34:05 +01001506 // Do the same as for a list: a negative index counts from the end.
1507 // Optimization to check the first byte to be below 0x80 (and no composing
1508 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001509 if (index < 0)
1510 {
1511 int clen = 0;
1512
1513 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001514 {
1515 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1516 ++nbyte;
1517 else if (enc_utf8)
1518 nbyte += utfc_ptr2len(str + nbyte);
1519 else
1520 nbyte += mb_ptr2len(str + nbyte);
1521 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001522 nchar = clen + index;
1523 if (nchar < 0)
1524 // unlike list: index out of range results in empty string
1525 return NULL;
1526 }
1527
1528 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001529 {
1530 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1531 ++nbyte;
1532 else if (enc_utf8)
1533 nbyte += utfc_ptr2len(str + nbyte);
1534 else
1535 nbyte += mb_ptr2len(str + nbyte);
1536 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001537 if (nbyte >= slen)
1538 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001539 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001540}
1541
1542/*
1543 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001544 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001545 * If going over the end return "str_len".
1546 * If "idx" is negative count from the end, -1 is the last character.
1547 * When going over the start return -1.
1548 */
1549 static long
1550char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1551{
1552 varnumber_T nchar = idx;
1553 size_t nbyte = 0;
1554
1555 if (nchar >= 0)
1556 {
1557 while (nchar > 0 && nbyte < str_len)
1558 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001559 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001560 --nchar;
1561 }
1562 }
1563 else
1564 {
1565 nbyte = str_len;
1566 while (nchar < 0 && nbyte > 0)
1567 {
1568 --nbyte;
1569 nbyte -= mb_head_off(str, str + nbyte);
1570 ++nchar;
1571 }
1572 if (nchar < 0)
1573 return -1;
1574 }
1575 return (long)nbyte;
1576}
1577
1578/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001579 * Return the slice "str[first : last]" using character indexes. Composing
1580 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001581 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001582 * Return NULL when the result is empty.
1583 */
1584 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001585string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001586{
1587 long start_byte, end_byte;
1588 size_t slen;
1589
1590 if (str == NULL)
1591 return NULL;
1592 slen = STRLEN(str);
1593 start_byte = char_idx2byte(str, slen, first);
1594 if (start_byte < 0)
1595 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001596 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001597 end_byte = (long)slen;
1598 else
1599 {
1600 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001601 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001602 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001603 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001604 }
1605
1606 if (start_byte >= (long)slen || end_byte <= start_byte)
1607 return NULL;
1608 return vim_strnsave(str + start_byte, end_byte - start_byte);
1609}
1610
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001611/*
1612 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1613 * When "dfunc_idx" is negative don't give an error.
1614 * Returns NULL for an error.
1615 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001616 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001617get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001618{
1619 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001620 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1621 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001622 svar_T *sv;
1623
1624 if (sref->sref_seq != si->sn_script_seq)
1625 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001626 // The script was reloaded after the function was compiled, the
1627 // script_idx may not be valid.
1628 if (dfunc != NULL)
1629 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1630 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001631 return NULL;
1632 }
1633 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001634 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001635 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001636 if (dfunc != NULL)
1637 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001638 return NULL;
1639 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001640
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001641 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1642 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001643 {
1644 if (dfunc != NULL)
1645 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1646 return NULL;
1647 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001648 return sv;
1649}
1650
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001651/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001652 * Function passed to do_cmdline() for splitting a script joined by NL
1653 * characters.
1654 */
1655 static char_u *
1656get_split_sourceline(
1657 int c UNUSED,
1658 void *cookie,
1659 int indent UNUSED,
1660 getline_opt_T options UNUSED)
1661{
1662 source_cookie_T *sp = (source_cookie_T *)cookie;
1663 char_u *p;
1664 char_u *line;
1665
Bram Moolenaar20677332021-06-06 17:02:53 +02001666 p = vim_strchr(sp->nextline, '\n');
1667 if (p == NULL)
1668 {
1669 line = vim_strsave(sp->nextline);
1670 sp->nextline += STRLEN(sp->nextline);
1671 }
1672 else
1673 {
1674 line = vim_strnsave(sp->nextline, p - sp->nextline);
1675 sp->nextline = p + 1;
1676 }
1677 return line;
1678}
1679
1680/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 * Execute a function by "name".
1682 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001683 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 */
1685 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001686call_eval_func(
1687 char_u *name,
1688 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001689 ectx_T *ectx,
1690 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691{
Bram Moolenaared677f52020-08-12 16:38:10 +02001692 int called_emsg_before = called_emsg;
1693 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001695 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001696 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001698 dictitem_T *v;
1699
1700 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001701 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1702 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001703 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001704 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001705 return FAIL;
1706 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001707 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001709 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710}
1711
1712/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001713 * When a function reference is used, fill a partial with the information
1714 * needed, especially when it is used as a closure.
1715 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001716 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001717fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1718{
1719 pt->pt_func = ufunc;
1720 pt->pt_refcount = 1;
1721
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001722 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001723 {
1724 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1725 + ectx->ec_dfunc_idx;
1726
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001727 // The closure may need to find arguments and local variables in the
1728 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001729 pt->pt_outer.out_stack = &ectx->ec_stack;
1730 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001731 if (ectx->ec_outer_ref != NULL)
1732 {
1733 // The current context already has a context, link to that one.
1734 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1735 if (ectx->ec_outer_ref->or_partial != NULL)
1736 {
1737 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1738 ++pt->pt_outer.out_up_partial->pt_refcount;
1739 }
1740 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001741
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001742 // If this function returns and the closure is still being used, we
1743 // need to make a copy of the context (arguments and local variables).
1744 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001745 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001746 {
1747 vim_free(pt);
1748 return FAIL;
1749 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001750 // Extra variable keeps the count of closures created in the current
1751 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001752 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1753 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1754
1755 ((partial_T **)ectx->ec_funcrefs.ga_data)
1756 [ectx->ec_funcrefs.ga_len] = pt;
1757 ++pt->pt_refcount;
1758 ++ectx->ec_funcrefs.ga_len;
1759 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001760 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001761 return OK;
1762}
1763
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001764/*
1765 * Execute iptr->isn_arg.string as an Ex command.
1766 */
1767 static int
1768exec_command(isn_T *iptr)
1769{
1770 source_cookie_T cookie;
1771
1772 SOURCING_LNUM = iptr->isn_lnum;
1773 // Pass getsourceline to get an error for a missing ":end"
1774 // command.
1775 CLEAR_FIELD(cookie);
1776 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1777 if (do_cmdline(iptr->isn_arg.string,
1778 getsourceline, &cookie,
1779 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1780 || did_emsg)
1781 return FAIL;
1782 return OK;
1783}
1784
Bram Moolenaar89445512022-04-14 12:58:23 +01001785/*
1786 * If script "sid" is not loaded yet then load it now.
1787 * Caller must make sure "sid" is a valid script ID.
1788 * "loaded" is set to TRUE if the script had to be loaded.
1789 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1790 */
1791 int
1792may_load_script(int sid, int *loaded)
1793{
1794 scriptitem_T *si = SCRIPT_ITEM(sid);
1795
1796 if (si->sn_state == SN_STATE_NOT_LOADED)
1797 {
1798 *loaded = TRUE;
1799 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1800 {
1801 semsg(_(e_cant_open_file_str), si->sn_name);
1802 return FAIL;
1803 }
1804 }
1805 return OK;
1806}
1807
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001808// used for v_instr of typval of VAR_INSTR
1809struct instr_S {
1810 ectx_T *instr_ectx;
1811 isn_T *instr_instr;
1812};
1813
Bram Moolenaar4c137212021-04-19 16:48:48 +02001814// used for substitute_instr
1815typedef struct subs_expr_S {
1816 ectx_T *subs_ectx;
1817 isn_T *subs_instr;
1818 int subs_status;
1819} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001821// Set when calling do_debug().
1822static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001823static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001824
1825/*
1826 * When debugging lookup "name" and return the typeval.
1827 * When not found return NULL.
1828 */
1829 typval_T *
1830lookup_debug_var(char_u *name)
1831{
1832 int idx;
1833 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001834 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001835 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001836 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001837
1838 if (ectx == NULL)
1839 return NULL;
1840 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1841
1842 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001843 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001844 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001845 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1846
1847 // the variable name may be NULL when not available in this block
1848 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001849 return STACK_TV_VAR(idx);
1850 }
1851
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001852 // Go through argument names.
1853 ufunc = dfunc->df_ufunc;
1854 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1855 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1856 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1857 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1858 - varargs_off + idx);
1859 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1860 return STACK_TV(ectx->ec_frame_idx - 1);
1861
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001862 return NULL;
1863}
1864
Bram Moolenaar26a44842021-09-02 18:49:06 +02001865/*
1866 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1867 * breakpoint was set in that function or when there is any expression.
1868 */
1869 int
1870may_break_in_function(ufunc_T *ufunc)
1871{
1872 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1873}
1874
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001875 static void
1876handle_debug(isn_T *iptr, ectx_T *ectx)
1877{
1878 char_u *line;
1879 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1880 + ectx->ec_dfunc_idx)->df_ufunc;
1881 isn_T *ni;
1882 int end_lnum = iptr->isn_lnum;
1883 garray_T ga;
1884 int lnum;
1885
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001886 if (ex_nesting_level > debug_break_level)
1887 {
1888 linenr_T breakpoint;
1889
Bram Moolenaar26a44842021-09-02 18:49:06 +02001890 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001891 return;
1892
1893 // check for the next breakpoint if needed
1894 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001895 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001896 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1897 return;
1898 }
1899
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001900 SOURCING_LNUM = iptr->isn_lnum;
1901 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001902 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001903
1904 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1905 if (ni->isn_type == ISN_DEBUG
1906 || ni->isn_type == ISN_RETURN
1907 || ni->isn_type == ISN_RETURN_VOID)
1908 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001909 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001910 break;
1911 }
1912
1913 if (end_lnum > iptr->isn_lnum)
1914 {
1915 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001916 for (lnum = iptr->isn_lnum; lnum < end_lnum
1917 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001918 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001919 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001920
Bram Moolenaar303215d2021-07-07 20:10:43 +02001921 if (p == NULL)
1922 continue; // left over from continuation line
1923 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001924 if (*p == '#')
1925 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001926 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001927 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1928 if (STRNCMP(p, "def ", 4) == 0)
1929 break;
1930 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001931 line = ga_concat_strings(&ga, " ");
1932 vim_free(ga.ga_data);
1933 }
1934 else
1935 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001936
Dominique Pelle6e969552021-06-17 13:53:41 +02001937 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001938 debug_context = NULL;
1939
1940 if (end_lnum > iptr->isn_lnum)
1941 vim_free(line);
1942}
1943
Bram Moolenaar4c137212021-04-19 16:48:48 +02001944/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001945 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001946 * Returns OK, FAIL or NOTDONE (uncatchable error).
1947 */
1948 static int
1949execute_storeindex(isn_T *iptr, ectx_T *ectx)
1950{
1951 vartype_T dest_type = iptr->isn_arg.vartype;
1952 typval_T *tv;
1953 typval_T *tv_idx = STACK_TV_BOT(-2);
1954 typval_T *tv_dest = STACK_TV_BOT(-1);
1955 int status = OK;
1956
1957 // Stack contains:
1958 // -3 value to be stored
1959 // -2 index
1960 // -1 dict or list
1961 tv = STACK_TV_BOT(-3);
1962 SOURCING_LNUM = iptr->isn_lnum;
1963 if (dest_type == VAR_ANY)
1964 {
1965 dest_type = tv_dest->v_type;
1966 if (dest_type == VAR_DICT)
1967 status = do_2string(tv_idx, TRUE, FALSE);
1968 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1969 {
1970 emsg(_(e_number_expected));
1971 status = FAIL;
1972 }
1973 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001974
1975 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001976 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001977 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001978 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001979 long lidx = (long)tv_idx->vval.v_number;
1980 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001981
Bram Moolenaare08be092022-02-17 13:08:26 +00001982 if (list == NULL)
1983 {
1984 emsg(_(e_list_not_set));
1985 return FAIL;
1986 }
1987 if (lidx < 0 && list->lv_len + lidx >= 0)
1988 // negative index is relative to the end
1989 lidx = list->lv_len + lidx;
1990 if (lidx < 0 || lidx > list->lv_len)
1991 {
1992 semsg(_(e_list_index_out_of_range_nr), lidx);
1993 return FAIL;
1994 }
1995 if (lidx < list->lv_len)
1996 {
1997 listitem_T *li = list_find(list, lidx);
1998
1999 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002000 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002001 return FAIL;
2002 // overwrite existing list item
2003 clear_tv(&li->li_tv);
2004 li->li_tv = *tv;
2005 }
2006 else
2007 {
2008 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2009 return FAIL;
2010 // append to list, only fails when out of memory
2011 if (list_append_tv(list, tv) == FAIL)
2012 return NOTDONE;
2013 clear_tv(tv);
2014 }
2015 }
2016 else if (dest_type == VAR_DICT)
2017 {
2018 char_u *key = tv_idx->vval.v_string;
2019 dict_T *dict = tv_dest->vval.v_dict;
2020 dictitem_T *di;
2021
2022 SOURCING_LNUM = iptr->isn_lnum;
2023 if (dict == NULL)
2024 {
2025 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002026 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002027 }
2028 if (key == NULL)
2029 key = (char_u *)"";
2030 di = dict_find(dict, key, -1);
2031 if (di != NULL)
2032 {
2033 if (error_if_locked(di->di_tv.v_lock,
2034 e_cannot_change_dict_item))
2035 return FAIL;
2036 // overwrite existing value
2037 clear_tv(&di->di_tv);
2038 di->di_tv = *tv;
2039 }
2040 else
2041 {
2042 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2043 return FAIL;
2044 // add to dict, only fails when out of memory
2045 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2046 return NOTDONE;
2047 clear_tv(tv);
2048 }
2049 }
2050 else if (dest_type == VAR_BLOB)
2051 {
2052 long lidx = (long)tv_idx->vval.v_number;
2053 blob_T *blob = tv_dest->vval.v_blob;
2054 varnumber_T nr;
2055 int error = FALSE;
2056 int len;
2057
2058 if (blob == NULL)
2059 {
2060 emsg(_(e_blob_not_set));
2061 return FAIL;
2062 }
2063 len = blob_len(blob);
2064 if (lidx < 0 && len + lidx >= 0)
2065 // negative index is relative to the end
2066 lidx = len + lidx;
2067
2068 // Can add one byte at the end.
2069 if (lidx < 0 || lidx > len)
2070 {
2071 semsg(_(e_blob_index_out_of_range_nr), lidx);
2072 return FAIL;
2073 }
2074 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2075 return FAIL;
2076 nr = tv_get_number_chk(tv, &error);
2077 if (error)
2078 return FAIL;
2079 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002080 }
2081 else
2082 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002083 status = FAIL;
2084 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002085 }
2086 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002087
2088 clear_tv(tv_idx);
2089 clear_tv(tv_dest);
2090 ectx->ec_stack.ga_len -= 3;
2091 if (status == FAIL)
2092 {
2093 clear_tv(tv);
2094 return FAIL;
2095 }
2096 return OK;
2097}
2098
2099/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002100 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002101 */
2102 static int
2103execute_storerange(isn_T *iptr, ectx_T *ectx)
2104{
2105 typval_T *tv;
2106 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2107 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2108 typval_T *tv_dest = STACK_TV_BOT(-1);
2109 int status = OK;
2110
2111 // Stack contains:
2112 // -4 value to be stored
2113 // -3 first index or "none"
2114 // -2 second index or "none"
2115 // -1 destination list or blob
2116 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002117 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002118 if (tv_dest->v_type == VAR_LIST)
2119 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002120 long n1;
2121 long n2;
2122 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002123
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002124 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2125 if (tv_idx2->v_type == VAR_SPECIAL
2126 && tv_idx2->vval.v_number == VVAL_NONE)
2127 n2 = list_len(tv_dest->vval.v_list) - 1;
2128 else
2129 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2130
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002131 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002132 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002133 status = FAIL;
2134 else
2135 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002136 status = check_range_index_two(tv_dest->vval.v_list,
2137 &n1, li1, &n2, FALSE);
2138 if (status != FAIL)
2139 status = list_assign_range(
2140 tv_dest->vval.v_list,
2141 tv->vval.v_list,
2142 n1,
2143 n2,
2144 tv_idx2->v_type == VAR_SPECIAL,
2145 (char_u *)"=",
2146 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002147 }
2148 }
2149 else if (tv_dest->v_type == VAR_BLOB)
2150 {
2151 varnumber_T n1;
2152 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002153 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002154
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002155 n1 = tv_get_number_chk(tv_idx1, NULL);
2156 if (tv_idx2->v_type == VAR_SPECIAL
2157 && tv_idx2->vval.v_number == VVAL_NONE)
2158 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2159 else
2160 n2 = tv_get_number_chk(tv_idx2, NULL);
2161 bloblen = blob_len(tv_dest->vval.v_blob);
2162
2163 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2164 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002165 status = FAIL;
2166 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002167 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002168 }
2169 else
2170 {
2171 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002172 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002173 }
2174
2175 clear_tv(tv_idx1);
2176 clear_tv(tv_idx2);
2177 clear_tv(tv_dest);
2178 ectx->ec_stack.ga_len -= 4;
2179 clear_tv(tv);
2180
2181 return status;
2182}
2183
2184/*
2185 * Unlet item in list or dict variable.
2186 */
2187 static int
2188execute_unletindex(isn_T *iptr, ectx_T *ectx)
2189{
2190 typval_T *tv_idx = STACK_TV_BOT(-2);
2191 typval_T *tv_dest = STACK_TV_BOT(-1);
2192 int status = OK;
2193
2194 // Stack contains:
2195 // -2 index
2196 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002197 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002198 if (tv_dest->v_type == VAR_DICT)
2199 {
2200 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002201 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002202 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002203 semsg(_(e_expected_str_but_got_str),
2204 vartype_name(VAR_STRING),
2205 vartype_name(tv_idx->v_type));
2206 status = FAIL;
2207 }
2208 else
2209 {
2210 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002211 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002212 dictitem_T *di = NULL;
2213
2214 if (d != NULL && value_check_lock(
2215 d->dv_lock, NULL, FALSE))
2216 status = FAIL;
2217 else
2218 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002219 if (tv_idx->v_type == VAR_STRING)
2220 {
2221 key = tv_idx->vval.v_string;
2222 if (key == NULL)
2223 key = (char_u *)"";
2224 }
2225 else
2226 {
2227 key = tv_get_string(tv_idx);
2228 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002229 if (d != NULL)
2230 di = dict_find(d, key, (int)STRLEN(key));
2231 if (di == NULL)
2232 {
2233 // NULL dict is equivalent to empty dict
2234 semsg(_(e_key_not_present_in_dictionary),
2235 key);
2236 status = FAIL;
2237 }
2238 else if (var_check_fixed(di->di_flags,
2239 NULL, FALSE)
2240 || var_check_ro(di->di_flags,
2241 NULL, FALSE))
2242 status = FAIL;
2243 else
2244 dictitem_remove(d, di);
2245 }
2246 }
2247 }
2248 else if (tv_dest->v_type == VAR_LIST)
2249 {
2250 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002251 if (check_for_number(tv_idx) == FAIL)
2252 {
2253 status = FAIL;
2254 }
2255 else
2256 {
2257 list_T *l = tv_dest->vval.v_list;
2258 long n = (long)tv_idx->vval.v_number;
2259
2260 if (l != NULL && value_check_lock(
2261 l->lv_lock, NULL, FALSE))
2262 status = FAIL;
2263 else
2264 {
2265 listitem_T *li = list_find(l, n);
2266
2267 if (li == NULL)
2268 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002269 semsg(_(e_list_index_out_of_range_nr), n);
2270 status = FAIL;
2271 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002272 else
2273 listitem_remove(l, li);
2274 }
2275 }
2276 }
2277 else
2278 {
2279 status = FAIL;
2280 semsg(_(e_cannot_index_str),
2281 vartype_name(tv_dest->v_type));
2282 }
2283
2284 clear_tv(tv_idx);
2285 clear_tv(tv_dest);
2286 ectx->ec_stack.ga_len -= 2;
2287
2288 return status;
2289}
2290
2291/*
2292 * Unlet a range of items in a list variable.
2293 */
2294 static int
2295execute_unletrange(isn_T *iptr, ectx_T *ectx)
2296{
2297 // Stack contains:
2298 // -3 index1
2299 // -2 index2
2300 // -1 dict or list
2301 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2302 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2303 typval_T *tv_dest = STACK_TV_BOT(-1);
2304 int status = OK;
2305
2306 if (tv_dest->v_type == VAR_LIST)
2307 {
2308 // indexes must be a number
2309 SOURCING_LNUM = iptr->isn_lnum;
2310 if (check_for_number(tv_idx1) == FAIL
2311 || (tv_idx2->v_type != VAR_SPECIAL
2312 && check_for_number(tv_idx2) == FAIL))
2313 {
2314 status = FAIL;
2315 }
2316 else
2317 {
2318 list_T *l = tv_dest->vval.v_list;
2319 long n1 = (long)tv_idx1->vval.v_number;
2320 long n2 = tv_idx2->v_type == VAR_SPECIAL
2321 ? 0 : (long)tv_idx2->vval.v_number;
2322 listitem_T *li;
2323
2324 li = list_find_index(l, &n1);
2325 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002326 {
2327 semsg(_(e_list_index_out_of_range_nr),
2328 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002329 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002330 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002331 else
2332 {
2333 if (n1 < 0)
2334 n1 = list_idx_of_item(l, li);
2335 if (n2 < 0)
2336 {
2337 listitem_T *li2 = list_find(l, n2);
2338
2339 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002340 {
2341 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002342 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002343 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002344 else
2345 n2 = list_idx_of_item(l, li2);
2346 }
2347 if (status != FAIL
2348 && tv_idx2->v_type != VAR_SPECIAL
2349 && n2 < n1)
2350 {
2351 semsg(_(e_list_index_out_of_range_nr), n2);
2352 status = FAIL;
2353 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002354 if (status != FAIL)
2355 list_unlet_range(l, li, n1,
2356 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002357 }
2358 }
2359 }
2360 else
2361 {
2362 status = FAIL;
2363 SOURCING_LNUM = iptr->isn_lnum;
2364 semsg(_(e_cannot_index_str),
2365 vartype_name(tv_dest->v_type));
2366 }
2367
2368 clear_tv(tv_idx1);
2369 clear_tv(tv_idx2);
2370 clear_tv(tv_dest);
2371 ectx->ec_stack.ga_len -= 3;
2372
2373 return status;
2374}
2375
2376/*
2377 * Top of a for loop.
2378 */
2379 static int
2380execute_for(isn_T *iptr, ectx_T *ectx)
2381{
2382 typval_T *tv;
2383 typval_T *ltv = STACK_TV_BOT(-1);
2384 typval_T *idxtv =
2385 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2386
2387 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2388 return FAIL;
2389 if (ltv->v_type == VAR_LIST)
2390 {
2391 list_T *list = ltv->vval.v_list;
2392
2393 // push the next item from the list
2394 ++idxtv->vval.v_number;
2395 if (list == NULL
2396 || idxtv->vval.v_number >= list->lv_len)
2397 {
2398 // past the end of the list, jump to "endfor"
2399 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2400 may_restore_cmdmod(&ectx->ec_funclocal);
2401 }
2402 else if (list->lv_first == &range_list_item)
2403 {
2404 // non-materialized range() list
2405 tv = STACK_TV_BOT(0);
2406 tv->v_type = VAR_NUMBER;
2407 tv->v_lock = 0;
2408 tv->vval.v_number = list_find_nr(
2409 list, idxtv->vval.v_number, NULL);
2410 ++ectx->ec_stack.ga_len;
2411 }
2412 else
2413 {
2414 listitem_T *li = list_find(list,
2415 idxtv->vval.v_number);
2416
2417 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2418 ++ectx->ec_stack.ga_len;
2419 }
2420 }
2421 else if (ltv->v_type == VAR_STRING)
2422 {
2423 char_u *str = ltv->vval.v_string;
2424
2425 // The index is for the last byte of the previous
2426 // character.
2427 ++idxtv->vval.v_number;
2428 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2429 {
2430 // past the end of the string, jump to "endfor"
2431 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2432 may_restore_cmdmod(&ectx->ec_funclocal);
2433 }
2434 else
2435 {
2436 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2437
2438 // Push the next character from the string.
2439 tv = STACK_TV_BOT(0);
2440 tv->v_type = VAR_STRING;
2441 tv->vval.v_string = vim_strnsave(
2442 str + idxtv->vval.v_number, clen);
2443 ++ectx->ec_stack.ga_len;
2444 idxtv->vval.v_number += clen - 1;
2445 }
2446 }
2447 else if (ltv->v_type == VAR_BLOB)
2448 {
2449 blob_T *blob = ltv->vval.v_blob;
2450
2451 // When we get here the first time make a copy of the
2452 // blob, so that the iteration still works when it is
2453 // changed.
2454 if (idxtv->vval.v_number == -1 && blob != NULL)
2455 {
2456 blob_copy(blob, ltv);
2457 blob_unref(blob);
2458 blob = ltv->vval.v_blob;
2459 }
2460
2461 // The index is for the previous byte.
2462 ++idxtv->vval.v_number;
2463 if (blob == NULL
2464 || idxtv->vval.v_number >= blob_len(blob))
2465 {
2466 // past the end of the blob, jump to "endfor"
2467 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2468 may_restore_cmdmod(&ectx->ec_funclocal);
2469 }
2470 else
2471 {
2472 // Push the next byte from the blob.
2473 tv = STACK_TV_BOT(0);
2474 tv->v_type = VAR_NUMBER;
2475 tv->vval.v_number = blob_get(blob,
2476 idxtv->vval.v_number);
2477 ++ectx->ec_stack.ga_len;
2478 }
2479 }
2480 else
2481 {
2482 semsg(_(e_for_loop_on_str_not_supported),
2483 vartype_name(ltv->v_type));
2484 return FAIL;
2485 }
2486 return OK;
2487}
2488
2489/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002490 * Load instruction for w:/b:/g:/t: variable.
2491 * "isn_type" is used instead of "iptr->isn_type".
2492 */
2493 static int
2494load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2495{
2496 dictitem_T *di = NULL;
2497 hashtab_T *ht = NULL;
2498 char namespace;
2499
2500 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2501 return NOTDONE;
2502 switch (isn_type)
2503 {
2504 case ISN_LOADG:
2505 ht = get_globvar_ht();
2506 namespace = 'g';
2507 break;
2508 case ISN_LOADB:
2509 ht = &curbuf->b_vars->dv_hashtab;
2510 namespace = 'b';
2511 break;
2512 case ISN_LOADW:
2513 ht = &curwin->w_vars->dv_hashtab;
2514 namespace = 'w';
2515 break;
2516 case ISN_LOADT:
2517 ht = &curtab->tp_vars->dv_hashtab;
2518 namespace = 't';
2519 break;
2520 default: // Cannot reach here
2521 return NOTDONE;
2522 }
2523 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2524
Bram Moolenaar06b77222022-01-25 15:51:56 +00002525 if (di == NULL)
2526 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002527 if (isn_type == ISN_LOADG)
2528 {
2529 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2530
2531 // g:Something could be a function
2532 if (ufunc != NULL)
2533 {
2534 typval_T *tv = STACK_TV_BOT(0);
2535
2536 ++ectx->ec_stack.ga_len;
2537 tv->v_type = VAR_FUNC;
2538 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2539 if (tv->vval.v_string == NULL)
2540 return FAIL;
2541 STRCPY(tv->vval.v_string, "g:");
2542 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2543 return OK;
2544 }
2545 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002546 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002547 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002548 // no check if the item exists in the script but
2549 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002550 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002551 else
2552 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002553 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002554 return FAIL;
2555 }
2556 else
2557 {
2558 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2559 ++ectx->ec_stack.ga_len;
2560 }
2561 return OK;
2562}
2563
2564/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002565 * Execute instructions in execution context "ectx".
2566 * Return OK or FAIL;
2567 */
2568 static int
2569exec_instructions(ectx_T *ectx)
2570{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002571 int ret = FAIL;
2572 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002573 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002574
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002575 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002576 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002577
Bram Moolenaarff652882021-05-16 15:24:49 +02002578 // Only catch exceptions in this instruction list.
2579 ectx->ec_trylevel_at_start = trylevel;
2580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 for (;;)
2582 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002583 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002585 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002586
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002587 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002588 {
2589 line_breakcheck();
2590 breakcheck_count = 0;
2591 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002592 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002593 {
2594 // Turn CTRL-C into an exception.
2595 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002596 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002597 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002598 did_throw = TRUE;
2599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002601 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002602 {
2603 // Turn an error message into an exception.
2604 did_emsg = FALSE;
2605 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002606 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002607 did_throw = TRUE;
2608 *msg_list = NULL;
2609 }
2610
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002611 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002613 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002614 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002615 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616
2617 // An exception jumps to the first catch, finally, or returns from
2618 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002619 while (index > 0)
2620 {
2621 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002622 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002623 break;
2624 // In the catch and finally block of this try we have to go up
2625 // one level.
2626 --index;
2627 trycmd = NULL;
2628 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002629 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002631 if (trycmd->tcd_in_catch)
2632 {
2633 // exception inside ":catch", jump to ":finally" once
2634 ectx->ec_iidx = trycmd->tcd_finally_idx;
2635 trycmd->tcd_finally_idx = 0;
2636 }
2637 else
2638 // jump to first ":catch"
2639 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002640 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002641 did_throw = FALSE; // don't come back here until :endtry
2642 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 }
2644 else
2645 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002646 // Not inside try or need to return from current functions.
2647 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002648 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002649 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002650 tv = STACK_TV_BOT(0);
2651 tv->v_type = VAR_NUMBER;
2652 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002653 ++ectx->ec_stack.ga_len;
2654 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002656 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002657 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002658 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002659 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 goto done;
2661 }
2662
Bram Moolenaar4c137212021-04-19 16:48:48 +02002663 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002664 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 }
2666 continue;
2667 }
2668
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002669 /*
2670 * Big switch on the instruction. Most compilers will be turning this
2671 * into an efficient lookup table, since the "case" values are an enum
2672 * with sequential numbers. It may look ugly, but it should be the
2673 * most efficient way.
2674 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002675 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 switch (iptr->isn_type)
2677 {
2678 // execute Ex command line
2679 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002680 if (exec_command(iptr) == FAIL)
2681 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 break;
2683
Bram Moolenaar20677332021-06-06 17:02:53 +02002684 // execute Ex command line split at NL characters.
2685 case ISN_EXEC_SPLIT:
2686 {
2687 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002688 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002689
2690 SOURCING_LNUM = iptr->isn_lnum;
2691 CLEAR_FIELD(cookie);
2692 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2693 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002694 line = get_split_sourceline(0, &cookie, 0, 0);
2695 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002696 get_split_sourceline, &cookie,
2697 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2698 == FAIL
2699 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002700 {
2701 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002702 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002703 }
2704 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002705 }
2706 break;
2707
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002708 // execute Ex command line that is only a range
2709 case ISN_EXECRANGE:
2710 {
2711 exarg_T ea;
2712 char *error = NULL;
2713
2714 CLEAR_FIELD(ea);
2715 ea.cmdidx = CMD_SIZE;
2716 ea.addr_type = ADDR_LINES;
2717 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002718 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002719 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002720 if (ea.cmd == NULL)
2721 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002722 // error is always NULL when using ADDR_LINES
2723 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002724 if (error != NULL)
2725 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002726 emsg(error);
2727 goto on_error;
2728 }
2729 }
2730 break;
2731
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002732 // Evaluate an expression with legacy syntax, push it onto the
2733 // stack.
2734 case ISN_LEGACY_EVAL:
2735 {
2736 char_u *arg = iptr->isn_arg.string;
2737 int res;
2738 int save_flags = cmdmod.cmod_flags;
2739
Bram Moolenaar35578162021-08-02 19:10:38 +02002740 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002741 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002742 tv = STACK_TV_BOT(0);
2743 init_tv(tv);
2744 cmdmod.cmod_flags |= CMOD_LEGACY;
2745 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2746 cmdmod.cmod_flags = save_flags;
2747 if (res == FAIL)
2748 goto on_error;
2749 ++ectx->ec_stack.ga_len;
2750 }
2751 break;
2752
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002753 // push typeval VAR_INSTR with instructions to be executed
2754 case ISN_INSTR:
2755 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002756 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002757 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002758 tv = STACK_TV_BOT(0);
2759 tv->vval.v_instr = ALLOC_ONE(instr_T);
2760 if (tv->vval.v_instr == NULL)
2761 goto on_error;
2762 ++ectx->ec_stack.ga_len;
2763
2764 tv->v_type = VAR_INSTR;
2765 tv->vval.v_instr->instr_ectx = ectx;
2766 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2767 }
2768 break;
2769
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002770 case ISN_SOURCE:
2771 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002772 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002773
Bram Moolenaar89445512022-04-14 12:58:23 +01002774 SOURCING_LNUM = iptr->isn_lnum;
2775 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002776 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002777 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002778 }
2779 break;
2780
Bram Moolenaar4c137212021-04-19 16:48:48 +02002781 // execute :substitute with an expression
2782 case ISN_SUBSTITUTE:
2783 {
2784 subs_T *subs = &iptr->isn_arg.subs;
2785 source_cookie_T cookie;
2786 struct subs_expr_S *save_instr = substitute_instr;
2787 struct subs_expr_S subs_instr;
2788 int res;
2789
2790 subs_instr.subs_ectx = ectx;
2791 subs_instr.subs_instr = subs->subs_instr;
2792 subs_instr.subs_status = OK;
2793 substitute_instr = &subs_instr;
2794
2795 SOURCING_LNUM = iptr->isn_lnum;
2796 // This is very much like ISN_EXEC
2797 CLEAR_FIELD(cookie);
2798 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2799 res = do_cmdline(subs->subs_cmd,
2800 getsourceline, &cookie,
2801 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2802 substitute_instr = save_instr;
2803
2804 if (res == FAIL || did_emsg
2805 || subs_instr.subs_status == FAIL)
2806 goto on_error;
2807 }
2808 break;
2809
2810 case ISN_FINISH:
2811 goto done;
2812
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002813 case ISN_REDIRSTART:
2814 // create a dummy entry for var_redir_str()
2815 if (alloc_redir_lval() == FAIL)
2816 goto on_error;
2817
2818 // The output is stored in growarray "redir_ga" until
2819 // redirection ends.
2820 init_redir_ga();
2821 redir_vname = 1;
2822 break;
2823
2824 case ISN_REDIREND:
2825 {
2826 char_u *res = get_clear_redir_ga();
2827
2828 // End redirection, put redirected text on the stack.
2829 clear_redir_lval();
2830 redir_vname = 0;
2831
Bram Moolenaar35578162021-08-02 19:10:38 +02002832 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002833 {
2834 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002835 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002836 }
2837 tv = STACK_TV_BOT(0);
2838 tv->v_type = VAR_STRING;
2839 tv->vval.v_string = res;
2840 ++ectx->ec_stack.ga_len;
2841 }
2842 break;
2843
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002844 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002845#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002846 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002847 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2848 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002849 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002850#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002851 break;
2852
2853 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002854#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002855 {
2856 exarg_T ea;
2857 int res;
2858
2859 CLEAR_FIELD(ea);
2860 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2861 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2862 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2863 --ectx->ec_stack.ga_len;
2864 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01002865 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002866 res = cexpr_core(&ea, tv);
2867 clear_tv(tv);
2868 if (res == FAIL)
2869 goto on_error;
2870 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002871#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002872 break;
2873
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002874 // execute Ex command from pieces on the stack
2875 case ISN_EXECCONCAT:
2876 {
2877 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002878 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002879 int pass;
2880 int i;
2881 char_u *cmd = NULL;
2882 char_u *str;
2883
2884 for (pass = 1; pass <= 2; ++pass)
2885 {
2886 for (i = 0; i < count; ++i)
2887 {
2888 tv = STACK_TV_BOT(i - count);
2889 str = tv->vval.v_string;
2890 if (str != NULL && *str != NUL)
2891 {
2892 if (pass == 2)
2893 STRCPY(cmd + len, str);
2894 len += STRLEN(str);
2895 }
2896 if (pass == 2)
2897 clear_tv(tv);
2898 }
2899 if (pass == 1)
2900 {
2901 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002902 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002903 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002904 len = 0;
2905 }
2906 }
2907
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002908 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002909 do_cmdline_cmd(cmd);
2910 vim_free(cmd);
2911 }
2912 break;
2913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 // execute :echo {string} ...
2915 case ISN_ECHO:
2916 {
2917 int count = iptr->isn_arg.echo.echo_count;
2918 int atstart = TRUE;
2919 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002920 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921
2922 for (idx = 0; idx < count; ++idx)
2923 {
2924 tv = STACK_TV_BOT(idx - count);
2925 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2926 &atstart, &needclr);
2927 clear_tv(tv);
2928 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002929 if (needclr)
2930 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002931 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 }
2933 break;
2934
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002935 // :execute {string} ...
2936 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002937 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002938 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002939 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002940 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002941 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002942 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002943 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002944 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002945 {
2946 int count = iptr->isn_arg.number;
2947 garray_T ga;
2948 char_u buf[NUMBUFLEN];
2949 char_u *p;
2950 int len;
2951 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002952 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002953
2954 ga_init2(&ga, 1, 80);
2955 for (idx = 0; idx < count; ++idx)
2956 {
2957 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002958 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002959 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002960 if (tv->v_type == VAR_CHANNEL
2961 || tv->v_type == VAR_JOB)
2962 {
2963 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002964 semsg(_(e_using_invalid_value_as_string_str),
2965 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002966 break;
2967 }
2968 else
2969 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002970 }
2971 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002972 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002973
2974 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002975 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002976 failed = TRUE;
2977 else
2978 {
2979 if (ga.ga_len > 0)
2980 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2981 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2982 ga.ga_len += len;
2983 }
2984 clear_tv(tv);
2985 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002986 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002987 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002988 {
2989 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002990 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002991 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002992
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002993 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002994 {
2995 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002996 {
2997 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002998 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002999 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003000 {
3001 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003002 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003003 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003004 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003005 else
3006 {
3007 msg_sb_eol();
3008 if (iptr->isn_type == ISN_ECHOMSG)
3009 {
3010 msg_attr(ga.ga_data, echo_attr);
3011 out_flush();
3012 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003013#ifdef HAS_MESSAGE_WINDOW
3014 else if (iptr->isn_type == ISN_ECHOWINDOW)
3015 {
3016 start_echowindow();
3017 msg_attr(ga.ga_data, echo_attr);
3018 end_echowindow();
3019 }
3020#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003021 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3022 {
3023 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3024 TRUE);
3025 ui_write((char_u *)"\r\n", 2, TRUE);
3026 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003027 else
3028 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003029 SOURCING_LNUM = iptr->isn_lnum;
3030 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003031 }
3032 }
3033 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003034 ga_clear(&ga);
3035 }
3036 break;
3037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 // load local variable or argument
3039 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003040 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003041 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003043 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 break;
3045
3046 // load v: variable
3047 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003048 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003049 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003051 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 break;
3053
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003054 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 case ISN_LOADSCRIPT:
3056 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003057 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 svar_T *sv;
3059
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003060 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003061 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003062 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003063 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003064 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003065 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003067 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 }
3069 break;
3070
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003071 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003073 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003075 int sid = iptr->isn_arg.loadstore.ls_sid;
3076 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003077 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 if (di == NULL)
3081 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003082 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003083 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003084 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 }
3086 else
3087 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003088 if (iptr->isn_type == ISN_LOADEXPORT)
3089 {
3090 int idx = get_script_item_idx(sid, name, 0,
3091 NULL, NULL);
3092 svar_T *sv;
3093
3094 if (idx >= 0)
3095 {
3096 sv = ((svar_T *)SCRIPT_ITEM(sid)
3097 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003098 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003099 {
3100 SOURCING_LNUM = iptr->isn_lnum;
3101 semsg(_(e_item_not_exported_in_script_str),
3102 name);
3103 goto on_error;
3104 }
3105 }
3106 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003107 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003108 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003110 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 }
3112 }
3113 break;
3114
Bram Moolenaard3aac292020-04-19 14:32:17 +02003115 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003117 case ISN_LOADB:
3118 case ISN_LOADW:
3119 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003121 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003122
Bram Moolenaar06b77222022-01-25 15:51:56 +00003123 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003124 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003125 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003126 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 break;
3130
Bram Moolenaar03290b82020-12-19 16:30:44 +01003131 // load autoload variable
3132 case ISN_LOADAUTO:
3133 {
3134 char_u *name = iptr->isn_arg.string;
3135
Bram Moolenaar35578162021-08-02 19:10:38 +02003136 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003137 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003138 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003139 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003140 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003141 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003142 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003143 }
3144 break;
3145
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003146 // load g:/b:/w:/t: namespace
3147 case ISN_LOADGDICT:
3148 case ISN_LOADBDICT:
3149 case ISN_LOADWDICT:
3150 case ISN_LOADTDICT:
3151 {
3152 dict_T *d = NULL;
3153
3154 switch (iptr->isn_type)
3155 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003156 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3157 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3158 case ISN_LOADWDICT: d = curwin->w_vars; break;
3159 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003160 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003161 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003162 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003163 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003164 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003165 tv = STACK_TV_BOT(0);
3166 tv->v_type = VAR_DICT;
3167 tv->v_lock = 0;
3168 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003169 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003170 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003171 }
3172 break;
3173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 // load &option
3175 case ISN_LOADOPT:
3176 {
3177 typval_T optval;
3178 char_u *name = iptr->isn_arg.string;
3179
Bram Moolenaara8c17702020-04-01 21:17:24 +02003180 // This is not expected to fail, name is checked during
3181 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003182 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003183 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003184 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003185 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003187 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 }
3189 break;
3190
3191 // load $ENV
3192 case ISN_LOADENV:
3193 {
3194 typval_T optval;
3195 char_u *name = iptr->isn_arg.string;
3196
Bram Moolenaar35578162021-08-02 19:10:38 +02003197 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003198 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003199 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003200 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003202 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 }
3204 break;
3205
3206 // load @register
3207 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003208 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003209 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 tv = STACK_TV_BOT(0);
3211 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003212 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003213 // This may result in NULL, which should be equivalent to an
3214 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 tv->vval.v_string = get_reg_contents(
3216 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003217 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 break;
3219
3220 // store local variable
3221 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003222 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 tv = STACK_TV_VAR(iptr->isn_arg.number);
3224 clear_tv(tv);
3225 *tv = *STACK_TV_BOT(0);
3226 break;
3227
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003228 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003229 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003230 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003231 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003232 int sid = iptr->isn_arg.loadstore.ls_sid;
3233 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003234 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003235 dictitem_T *di = find_var_in_ht(ht, 0,
3236 iptr->isn_type == ISN_STORES
3237 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003238
Bram Moolenaar4c137212021-04-19 16:48:48 +02003239 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003240 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003241 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003242 {
3243 if (iptr->isn_type == ISN_STOREEXPORT)
3244 {
3245 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003246 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003247 goto on_error;
3248 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003249 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003250 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003251 else
3252 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003253 if (iptr->isn_type == ISN_STOREEXPORT)
3254 {
3255 int idx = get_script_item_idx(sid, name, 0,
3256 NULL, NULL);
3257
Bram Moolenaar06651632022-04-27 17:54:25 +01003258 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003259 if (idx >= 0)
3260 {
3261 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3262 ->sn_var_vals.ga_data) + idx;
3263
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003264 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003265 {
3266 semsg(_(e_item_not_exported_in_script_str),
3267 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003268 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003269 goto on_error;
3270 }
3271 }
3272 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003273 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003274 {
3275 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003276 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003277 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003278 clear_tv(&di->di_tv);
3279 di->di_tv = *STACK_TV_BOT(0);
3280 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003281 }
3282 break;
3283
3284 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 case ISN_STORESCRIPT:
3286 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003287 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3288 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003290 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003291 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003292 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003293 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003294
3295 // "const" and "final" are checked at compile time, locking
3296 // the value needs to be checked here.
3297 SOURCING_LNUM = iptr->isn_lnum;
3298 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003299 {
3300 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003301 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003302 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 clear_tv(sv->sv_tv);
3305 *sv->sv_tv = *STACK_TV_BOT(0);
3306 }
3307 break;
3308
3309 // store option
3310 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003311 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003313 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3314 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 long n = 0;
3316 char_u *s = NULL;
3317 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003318 char_u numbuf[NUMBUFLEN];
3319 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320
Bram Moolenaar4c137212021-04-19 16:48:48 +02003321 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 tv = STACK_TV_BOT(0);
3323 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003324 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003326 if (s == NULL)
3327 s = (char_u *)"";
3328 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003329 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3330 {
3331 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003332 // If the option can be set to a function reference or
3333 // a lambda and the passed value is a function
3334 // reference, then convert it to the name (string) of
3335 // the function reference.
3336 s = tv2string(tv, &tofree, numbuf, 0);
3337 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003338 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003339 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003340 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003341 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003342 goto on_error;
3343 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003344 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003346 // must be VAR_NUMBER, CHECKTYPE makes sure
3347 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003348 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003349 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003350 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 if (msg != NULL)
3352 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003353 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003355 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 }
3358 break;
3359
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003360 // store $ENV
3361 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003362 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003363 tv = STACK_TV_BOT(0);
3364 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3365 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003366 break;
3367
3368 // store @r
3369 case ISN_STOREREG:
3370 {
3371 int reg = iptr->isn_arg.number;
3372
Bram Moolenaar4c137212021-04-19 16:48:48 +02003373 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003374 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003375 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003376 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003377 }
3378 break;
3379
3380 // store v: variable
3381 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003382 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003383 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3384 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003385 // should not happen, type is checked when compiling
3386 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003387 break;
3388
Bram Moolenaard3aac292020-04-19 14:32:17 +02003389 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003391 case ISN_STOREB:
3392 case ISN_STOREW:
3393 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003395 dictitem_T *di;
3396 hashtab_T *ht;
3397 char_u *name = iptr->isn_arg.string + 2;
3398
Bram Moolenaard3aac292020-04-19 14:32:17 +02003399 switch (iptr->isn_type)
3400 {
3401 case ISN_STOREG:
3402 ht = get_globvar_ht();
3403 break;
3404 case ISN_STOREB:
3405 ht = &curbuf->b_vars->dv_hashtab;
3406 break;
3407 case ISN_STOREW:
3408 ht = &curwin->w_vars->dv_hashtab;
3409 break;
3410 case ISN_STORET:
3411 ht = &curtab->tp_vars->dv_hashtab;
3412 break;
3413 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003414 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416
Bram Moolenaar4c137212021-04-19 16:48:48 +02003417 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003418 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003420 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 else
3422 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003423 SOURCING_LNUM = iptr->isn_lnum;
3424 if (var_check_permission(di, name) == FAIL)
3425 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 clear_tv(&di->di_tv);
3427 di->di_tv = *STACK_TV_BOT(0);
3428 }
3429 }
3430 break;
3431
Bram Moolenaar03290b82020-12-19 16:30:44 +01003432 // store an autoload variable
3433 case ISN_STOREAUTO:
3434 SOURCING_LNUM = iptr->isn_lnum;
3435 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3436 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003437 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003438 break;
3439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 // store number in local variable
3441 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003442 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 clear_tv(tv);
3444 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003445 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 break;
3447
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003448 // store value in list or dict variable
3449 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003450 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003451 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003452
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003453 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003454 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003455 if (res == NOTDONE)
3456 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003457 }
3458 break;
3459
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003460 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003461 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003462 if (execute_storerange(iptr, ectx) == FAIL)
3463 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003464 break;
3465
Bram Moolenaar0186e582021-01-10 18:33:11 +01003466 // load or store variable or argument from outer scope
3467 case ISN_LOADOUTER:
3468 case ISN_STOREOUTER:
3469 {
3470 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003471 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3472 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003473
3474 while (depth > 1 && outer != NULL)
3475 {
3476 outer = outer->out_up;
3477 --depth;
3478 }
3479 if (outer == NULL)
3480 {
3481 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003482 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3483 || ectx->ec_outer_ref == NULL)
3484 // Possibly :def function called from legacy
3485 // context.
3486 emsg(_(e_closure_called_from_invalid_context));
3487 else
3488 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003489 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003490 }
3491 tv = ((typval_T *)outer->out_stack->ga_data)
3492 + outer->out_frame_idx + STACK_FRAME_SIZE
3493 + iptr->isn_arg.outer.outer_idx;
3494 if (iptr->isn_type == ISN_LOADOUTER)
3495 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003496 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003497 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003498 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003499 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003500 }
3501 else
3502 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003503 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003504 clear_tv(tv);
3505 *tv = *STACK_TV_BOT(0);
3506 }
3507 }
3508 break;
3509
Bram Moolenaar752fc692021-01-04 21:57:11 +01003510 // unlet item in list or dict variable
3511 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003512 if (execute_unletindex(iptr, ectx) == FAIL)
3513 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003514 break;
3515
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003516 // unlet range of items in list variable
3517 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003518 if (execute_unletrange(iptr, ectx) == FAIL)
3519 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003520 break;
3521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 // push constant
3523 case ISN_PUSHNR:
3524 case ISN_PUSHBOOL:
3525 case ISN_PUSHSPEC:
3526 case ISN_PUSHF:
3527 case ISN_PUSHS:
3528 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003529 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003530 case ISN_PUSHCHANNEL:
3531 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003532 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003533 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003535 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003536 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 switch (iptr->isn_type)
3538 {
3539 case ISN_PUSHNR:
3540 tv->v_type = VAR_NUMBER;
3541 tv->vval.v_number = iptr->isn_arg.number;
3542 break;
3543 case ISN_PUSHBOOL:
3544 tv->v_type = VAR_BOOL;
3545 tv->vval.v_number = iptr->isn_arg.number;
3546 break;
3547 case ISN_PUSHSPEC:
3548 tv->v_type = VAR_SPECIAL;
3549 tv->vval.v_number = iptr->isn_arg.number;
3550 break;
3551#ifdef FEAT_FLOAT
3552 case ISN_PUSHF:
3553 tv->v_type = VAR_FLOAT;
3554 tv->vval.v_float = iptr->isn_arg.fnumber;
3555 break;
3556#endif
3557 case ISN_PUSHBLOB:
3558 blob_copy(iptr->isn_arg.blob, tv);
3559 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003560 case ISN_PUSHFUNC:
3561 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003562 if (iptr->isn_arg.string == NULL)
3563 tv->vval.v_string = NULL;
3564 else
3565 tv->vval.v_string =
3566 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003567 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003568 case ISN_PUSHCHANNEL:
3569#ifdef FEAT_JOB_CHANNEL
3570 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003571 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003572#endif
3573 break;
3574 case ISN_PUSHJOB:
3575#ifdef FEAT_JOB_CHANNEL
3576 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003577 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003578#endif
3579 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 default:
3581 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003582 tv->vval.v_string = iptr->isn_arg.string == NULL
3583 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 }
3585 break;
3586
Bram Moolenaar06b77222022-01-25 15:51:56 +00003587 case ISN_AUTOLOAD:
3588 {
3589 char_u *name = iptr->isn_arg.string;
3590
3591 (void)script_autoload(name, FALSE);
3592 if (find_func(name, TRUE))
3593 {
3594 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3595 goto theend;
3596 tv = STACK_TV_BOT(0);
3597 tv->v_lock = 0;
3598 ++ectx->ec_stack.ga_len;
3599 tv->v_type = VAR_FUNC;
3600 tv->vval.v_string = vim_strsave(name);
3601 }
3602 else
3603 {
3604 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3605
3606 if (res == NOTDONE)
3607 goto theend;
3608 if (res == FAIL)
3609 goto on_error;
3610 }
3611 }
3612 break;
3613
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003614 case ISN_UNLET:
3615 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3616 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003617 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003618 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003619 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003620 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003621 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003622
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003623 case ISN_LOCKUNLOCK:
3624 {
3625 typval_T *lval_root_save = lval_root;
3626 int res;
3627
3628 // Stack has the local variable, argument the whole :lock
3629 // or :unlock command, like ISN_EXEC.
3630 --ectx->ec_stack.ga_len;
3631 lval_root = STACK_TV_BOT(0);
3632 res = exec_command(iptr);
3633 clear_tv(lval_root);
3634 lval_root = lval_root_save;
3635 if (res == FAIL)
3636 goto on_error;
3637 }
3638 break;
3639
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003640 case ISN_LOCKCONST:
3641 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3642 break;
3643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 // create a list from items on the stack; uses a single allocation
3645 // for the list header and the items
3646 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003647 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003648 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 break;
3650
3651 // create a dict from items on the stack
3652 case ISN_NEWDICT:
3653 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003654 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003656 SOURCING_LNUM = iptr->isn_lnum;
3657 res = exe_newdict(iptr->isn_arg.number, ectx);
3658 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003659 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003660 if (res == MAYBE)
3661 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 }
3663 break;
3664
LemonBoy372bcce2022-04-25 12:43:20 +01003665 case ISN_CONCAT:
3666 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3667 goto theend;
3668 break;
3669
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003670 // create a partial with NULL value
3671 case ISN_NEWPARTIAL:
3672 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3673 goto theend;
3674 ++ectx->ec_stack.ga_len;
3675 tv = STACK_TV_BOT(-1);
3676 tv->v_type = VAR_PARTIAL;
3677 tv->v_lock = 0;
3678 tv->vval.v_partial = NULL;
3679 break;
3680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 // call a :def function
3682 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003683 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003684 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3685 NULL,
3686 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003687 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003688 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 break;
3690
3691 // call a builtin function
3692 case ISN_BCALL:
3693 SOURCING_LNUM = iptr->isn_lnum;
3694 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3695 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003696 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003697 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 break;
3699
3700 // call a funcref or partial
3701 case ISN_PCALL:
3702 {
3703 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3704 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003705 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706
3707 SOURCING_LNUM = iptr->isn_lnum;
3708 if (pfunc->cpf_top)
3709 {
3710 // funcref is above the arguments
3711 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3712 }
3713 else
3714 {
3715 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003716 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003717 partial_tv = *STACK_TV_BOT(0);
3718 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003720 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003721 if (tv == &partial_tv)
3722 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003724 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 }
3726 break;
3727
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003728 case ISN_PCALL_END:
3729 // PCALL finished, arguments have been consumed and replaced by
3730 // the return value. Now clear the funcref from the stack,
3731 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003732 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003733 clear_tv(STACK_TV_BOT(-1));
3734 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3735 break;
3736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 // call a user defined function or funcref/partial
3738 case ISN_UCALL:
3739 {
3740 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3741
3742 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003743 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003744 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003745 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 }
3747 break;
3748
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003749 // :defer func(arg)
3750 case ISN_DEFER:
3751 if (add_defer_func(iptr->isn_arg.defer.defer_var_idx,
3752 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
3753 goto on_error;
3754 break;
3755
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003756 // return from a :def function call without a value
3757 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003758 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003759 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003760 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003761 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003762 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003763 tv->vval.v_number = 0;
3764 tv->v_lock = 0;
3765 // FALLTHROUGH
3766
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003767 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 case ISN_RETURN:
3769 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003770 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003771 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003772
3773 if (trystack->ga_len > 0)
3774 trycmd = ((trycmd_T *)trystack->ga_data)
3775 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003776 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003777 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003779 // jump to ":finally" or ":endtry"
3780 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003781 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003782 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003783 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 trycmd->tcd_return = TRUE;
3785 }
3786 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003787 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 }
3789 break;
3790
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003791 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 case ISN_FUNCREF:
3793 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003794 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003795 ufunc_T *ufunc;
3796 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003799 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003800 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003801 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003802 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003803 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003804 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003805 if (funcref->fr_func_name == NULL)
3806 {
3807 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3808 + funcref->fr_dfunc_idx;
3809
3810 ufunc = pt_dfunc->df_ufunc;
3811 }
3812 else
3813 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003814 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003815 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003816 if (ufunc == NULL)
3817 {
3818 SOURCING_LNUM = iptr->isn_lnum;
3819 iemsg("ufunc unexpectedly NULL for FUNCREF");
3820 goto theend;
3821 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003822 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003823 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003825 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 tv->vval.v_partial = pt;
3827 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003828 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 }
3830 break;
3831
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003832 // Create a global function from a lambda.
3833 case ISN_NEWFUNC:
3834 {
3835 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3836
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003837 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003838 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003839 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003840 }
3841 break;
3842
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003843 // List functions
3844 case ISN_DEF:
3845 if (iptr->isn_arg.string == NULL)
3846 list_functions(NULL);
3847 else
3848 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003849 exarg_T ea;
3850 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003851
3852 CLEAR_FIELD(ea);
3853 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003854 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3855 define_function(&ea, NULL, &lines_to_free);
3856 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003857 }
3858 break;
3859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 // jump if a condition is met
3861 case ISN_JUMP:
3862 {
3863 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003864 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 int jump = TRUE;
3866
3867 if (when != JUMP_ALWAYS)
3868 {
3869 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003870 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003871 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003872 || when == JUMP_IF_COND_TRUE)
3873 {
3874 SOURCING_LNUM = iptr->isn_lnum;
3875 jump = tv_get_bool_chk(tv, &error);
3876 if (error)
3877 goto on_error;
3878 }
3879 else
3880 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01003881 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003883 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 {
3885 // drop the value from the stack
3886 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003887 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 }
3889 }
3890 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003891 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 }
3893 break;
3894
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003895 // Jump if an argument with a default value was already set and not
3896 // v:none.
3897 case ISN_JUMP_IF_ARG_SET:
3898 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3899 if (tv->v_type != VAR_UNKNOWN
3900 && !(tv->v_type == VAR_SPECIAL
3901 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003902 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003903 break;
3904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 // top of a for loop
3906 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003907 if (execute_for(iptr, ectx) == FAIL)
3908 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 break;
3910
3911 // start of ":try" block
3912 case ISN_TRY:
3913 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003914 trycmd_T *trycmd = NULL;
3915
Bram Moolenaar35578162021-08-02 19:10:38 +02003916 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003917 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003918 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3919 + ectx->ec_trystack.ga_len;
3920 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003922 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003923 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3924 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003925 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003926 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003927 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003928 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003929 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003930 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 }
3932 break;
3933
3934 case ISN_PUSHEXC:
3935 if (current_exception == NULL)
3936 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003937 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003939 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003941 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003942 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003944 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003946 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 tv->vval.v_string = vim_strsave(
3948 (char_u *)current_exception->value);
3949 break;
3950
3951 case ISN_CATCH:
3952 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003953 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01003954 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955
Bram Moolenaar4c137212021-04-19 16:48:48 +02003956 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01003957 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01003959 trycmd->tcd_caught = TRUE;
3960 trycmd->tcd_did_throw = FALSE;
3961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003963 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 catch_exception(current_exception);
3965 }
3966 break;
3967
Bram Moolenaarc150c092021-02-13 15:02:46 +01003968 case ISN_TRYCONT:
3969 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003970 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003971 trycont_T *trycont = &iptr->isn_arg.trycont;
3972 int i;
3973 trycmd_T *trycmd;
3974 int iidx = trycont->tct_where;
3975
3976 if (trystack->ga_len < trycont->tct_levels)
3977 {
3978 siemsg("TRYCONT: expected %d levels, found %d",
3979 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003980 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003981 }
3982 // Make :endtry jump to any outer try block and the last
3983 // :endtry inside the loop to the loop start.
3984 for (i = trycont->tct_levels; i > 0; --i)
3985 {
3986 trycmd = ((trycmd_T *)trystack->ga_data)
3987 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003988 // Add one to tcd_cont to be able to jump to
3989 // instruction with index zero.
3990 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003991 iidx = trycmd->tcd_finally_idx == 0
3992 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003993 }
3994 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003995 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003996 }
3997 break;
3998
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003999 case ISN_FINALLY:
4000 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004001 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004002 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4003 + trystack->ga_len - 1;
4004
4005 // Reset the index to avoid a return statement jumps here
4006 // again.
4007 trycmd->tcd_finally_idx = 0;
4008 break;
4009 }
4010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 // end of ":try" block
4012 case ISN_ENDTRY:
4013 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004014 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004015 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016
Bram Moolenaar06651632022-04-27 17:54:25 +01004017 --trystack->ga_len;
4018 --trylevel;
4019 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4020 if (trycmd->tcd_did_throw)
4021 did_throw = TRUE;
4022 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004024 // discard the exception
4025 if (caught_stack == current_exception)
4026 caught_stack = caught_stack->caught;
4027 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004029
4030 if (trycmd->tcd_return)
4031 goto func_return;
4032
4033 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4034 {
4035 --ectx->ec_stack.ga_len;
4036 clear_tv(STACK_TV_BOT(0));
4037 }
4038 if (trycmd->tcd_cont != 0)
4039 // handling :continue: jump to outer try block or
4040 // start of the loop
4041 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 }
4043 break;
4044
4045 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004046 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004047 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004048
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004049 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4050 {
4051 // throwing an exception while using "silent!" causes
4052 // the function to abort but not display an error.
4053 tv = STACK_TV_BOT(-1);
4054 clear_tv(tv);
4055 tv->v_type = VAR_NUMBER;
4056 tv->vval.v_number = 0;
4057 goto done;
4058 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004059 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004060 tv = STACK_TV_BOT(0);
4061 if (tv->vval.v_string == NULL
4062 || *skipwhite(tv->vval.v_string) == NUL)
4063 {
4064 vim_free(tv->vval.v_string);
4065 SOURCING_LNUM = iptr->isn_lnum;
4066 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004067 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004068 }
4069
4070 // Inside a "catch" we need to first discard the caught
4071 // exception.
4072 if (trystack->ga_len > 0)
4073 {
4074 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4075 + trystack->ga_len - 1;
4076 if (trycmd->tcd_caught && current_exception != NULL)
4077 {
4078 // discard the exception
4079 if (caught_stack == current_exception)
4080 caught_stack = caught_stack->caught;
4081 discard_current_exception();
4082 trycmd->tcd_caught = FALSE;
4083 }
4084 }
4085
Bram Moolenaar90a57162022-02-12 14:23:17 +00004086 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004087 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4088 == FAIL)
4089 {
4090 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004091 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004092 }
4093 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 break;
4096
4097 // compare with special values
4098 case ISN_COMPAREBOOL:
4099 case ISN_COMPARESPECIAL:
4100 {
4101 typval_T *tv1 = STACK_TV_BOT(-2);
4102 typval_T *tv2 = STACK_TV_BOT(-1);
4103 varnumber_T arg1 = tv1->vval.v_number;
4104 varnumber_T arg2 = tv2->vval.v_number;
4105 int res;
4106
Bram Moolenaar06651632022-04-27 17:54:25 +01004107 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4108 res = arg1 == arg2;
4109 else
4110 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111
Bram Moolenaar4c137212021-04-19 16:48:48 +02004112 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 tv1->v_type = VAR_BOOL;
4114 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4115 }
4116 break;
4117
Bram Moolenaar7a222242022-03-01 19:23:24 +00004118 case ISN_COMPARENULL:
4119 {
4120 typval_T *tv1 = STACK_TV_BOT(-2);
4121 typval_T *tv2 = STACK_TV_BOT(-1);
4122 int res;
4123
4124 res = typval_compare_null(tv1, tv2);
4125 if (res == MAYBE)
4126 goto on_error;
4127 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4128 res = !res;
4129 clear_tv(tv1);
4130 clear_tv(tv2);
4131 --ectx->ec_stack.ga_len;
4132 tv1->v_type = VAR_BOOL;
4133 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4134 }
4135 break;
4136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 // Operation with two number arguments
4138 case ISN_OPNR:
4139 case ISN_COMPARENR:
4140 {
4141 typval_T *tv1 = STACK_TV_BOT(-2);
4142 typval_T *tv2 = STACK_TV_BOT(-1);
4143 varnumber_T arg1 = tv1->vval.v_number;
4144 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004145 varnumber_T res = 0;
4146 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004148 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4149 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4150 {
4151 if (arg2 < 0)
4152 {
4153 SOURCING_LNUM = iptr->isn_lnum;
4154 emsg(_(e_bitshift_ops_must_be_postive));
4155 goto on_error;
4156 }
4157 }
4158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 switch (iptr->isn_arg.op.op_type)
4160 {
4161 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004162 case EXPR_DIV: if (arg2 == 0)
4163 div_zero = TRUE;
4164 else
4165 res = arg1 / arg2;
4166 break;
4167 case EXPR_REM: if (arg2 == 0)
4168 div_zero = TRUE;
4169 else
4170 res = arg1 % arg2;
4171 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 case EXPR_SUB: res = arg1 - arg2; break;
4173 case EXPR_ADD: res = arg1 + arg2; break;
4174
4175 case EXPR_EQUAL: res = arg1 == arg2; break;
4176 case EXPR_NEQUAL: res = arg1 != arg2; break;
4177 case EXPR_GREATER: res = arg1 > arg2; break;
4178 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4179 case EXPR_SMALLER: res = arg1 < arg2; break;
4180 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004181 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4182 res = 0;
4183 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004184 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004185 break;
4186 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4187 res = 0;
4188 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004189 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004190 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004191 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 }
4193
Bram Moolenaar4c137212021-04-19 16:48:48 +02004194 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 if (iptr->isn_type == ISN_COMPARENR)
4196 {
4197 tv1->v_type = VAR_BOOL;
4198 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4199 }
4200 else
4201 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004202 if (div_zero)
4203 {
4204 SOURCING_LNUM = iptr->isn_lnum;
4205 emsg(_(e_divide_by_zero));
4206 goto on_error;
4207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 }
4209 break;
4210
4211 // Computation with two float arguments
4212 case ISN_OPFLOAT:
4213 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004214#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 {
4216 typval_T *tv1 = STACK_TV_BOT(-2);
4217 typval_T *tv2 = STACK_TV_BOT(-1);
4218 float_T arg1 = tv1->vval.v_float;
4219 float_T arg2 = tv2->vval.v_float;
4220 float_T res = 0;
4221 int cmp = FALSE;
4222
4223 switch (iptr->isn_arg.op.op_type)
4224 {
4225 case EXPR_MULT: res = arg1 * arg2; break;
4226 case EXPR_DIV: res = arg1 / arg2; break;
4227 case EXPR_SUB: res = arg1 - arg2; break;
4228 case EXPR_ADD: res = arg1 + arg2; break;
4229
4230 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4231 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4232 case EXPR_GREATER: cmp = arg1 > arg2; break;
4233 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4234 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4235 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4236 default: cmp = 0; break;
4237 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004238 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 if (iptr->isn_type == ISN_COMPAREFLOAT)
4240 {
4241 tv1->v_type = VAR_BOOL;
4242 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4243 }
4244 else
4245 tv1->vval.v_float = res;
4246 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004247#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 break;
4249
4250 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004251 case ISN_COMPAREDICT:
4252 case ISN_COMPAREFUNC:
4253 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 case ISN_COMPAREBLOB:
4255 {
4256 typval_T *tv1 = STACK_TV_BOT(-2);
4257 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004258 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4259 int ic = iptr->isn_arg.op.op_ic;
4260 int res = FALSE;
4261 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262
Bram Moolenaar265f8112021-12-19 12:33:05 +00004263 SOURCING_LNUM = iptr->isn_lnum;
4264 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004266 status = typval_compare_list(tv1, tv2,
4267 exprtype, ic, &res);
4268 }
4269 else if (iptr->isn_type == ISN_COMPAREDICT)
4270 {
4271 status = typval_compare_dict(tv1, tv2,
4272 exprtype, ic, &res);
4273 }
4274 else if (iptr->isn_type == ISN_COMPAREFUNC)
4275 {
4276 status = typval_compare_func(tv1, tv2,
4277 exprtype, ic, &res);
4278 }
4279 else if (iptr->isn_type == ISN_COMPARESTRING)
4280 {
4281 status = typval_compare_string(tv1, tv2,
4282 exprtype, ic, &res);
4283 }
4284 else
4285 {
4286 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004288 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 clear_tv(tv1);
4290 clear_tv(tv2);
4291 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004292 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4293 if (status == FAIL)
4294 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 }
4296 break;
4297
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 case ISN_COMPAREANY:
4299 {
4300 typval_T *tv1 = STACK_TV_BOT(-2);
4301 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004302 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004304 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305
Bram Moolenaareb26f432020-09-14 16:50:05 +02004306 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004307 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004309 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004310 if (status == FAIL)
4311 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 }
4313 break;
4314
4315 case ISN_ADDLIST:
4316 case ISN_ADDBLOB:
4317 {
4318 typval_T *tv1 = STACK_TV_BOT(-2);
4319 typval_T *tv2 = STACK_TV_BOT(-1);
4320
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004321 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004323 {
4324 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4325 && tv1->vval.v_list != NULL)
4326 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4327 NULL);
4328 else
4329 eval_addlist(tv1, tv2);
4330 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 else
4332 eval_addblob(tv1, tv2);
4333 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004334 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 }
4336 break;
4337
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004338 case ISN_LISTAPPEND:
4339 {
4340 typval_T *tv1 = STACK_TV_BOT(-2);
4341 typval_T *tv2 = STACK_TV_BOT(-1);
4342 list_T *l = tv1->vval.v_list;
4343
4344 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004345 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004346 if (l == NULL)
4347 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004348 emsg(_(e_cannot_add_to_null_list));
4349 goto on_error;
4350 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004351 if (value_check_lock(l->lv_lock, NULL, FALSE))
4352 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004353 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004354 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004355 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004356 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004357 }
4358 break;
4359
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004360 case ISN_BLOBAPPEND:
4361 {
4362 typval_T *tv1 = STACK_TV_BOT(-2);
4363 typval_T *tv2 = STACK_TV_BOT(-1);
4364 blob_T *b = tv1->vval.v_blob;
4365 int error = FALSE;
4366 varnumber_T n;
4367
4368 // add a number to a blob
4369 if (b == NULL)
4370 {
4371 SOURCING_LNUM = iptr->isn_lnum;
4372 emsg(_(e_cannot_add_to_null_blob));
4373 goto on_error;
4374 }
4375 n = tv_get_number_chk(tv2, &error);
4376 if (error)
4377 goto on_error;
4378 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004379 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004380 }
4381 break;
4382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 // Computation with two arguments of unknown type
4384 case ISN_OPANY:
4385 {
4386 typval_T *tv1 = STACK_TV_BOT(-2);
4387 typval_T *tv2 = STACK_TV_BOT(-1);
4388 varnumber_T n1, n2;
4389#ifdef FEAT_FLOAT
4390 float_T f1 = 0, f2 = 0;
4391#endif
4392 int error = FALSE;
4393
4394 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4395 {
4396 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4397 {
4398 eval_addlist(tv1, tv2);
4399 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004400 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 break;
4402 }
4403 else if (tv1->v_type == VAR_BLOB
4404 && tv2->v_type == VAR_BLOB)
4405 {
4406 eval_addblob(tv1, tv2);
4407 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004408 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 break;
4410 }
4411 }
4412#ifdef FEAT_FLOAT
4413 if (tv1->v_type == VAR_FLOAT)
4414 {
4415 f1 = tv1->vval.v_float;
4416 n1 = 0;
4417 }
4418 else
4419#endif
4420 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004421 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 n1 = tv_get_number_chk(tv1, &error);
4423 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004424 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425#ifdef FEAT_FLOAT
4426 if (tv2->v_type == VAR_FLOAT)
4427 f1 = n1;
4428#endif
4429 }
4430#ifdef FEAT_FLOAT
4431 if (tv2->v_type == VAR_FLOAT)
4432 {
4433 f2 = tv2->vval.v_float;
4434 n2 = 0;
4435 }
4436 else
4437#endif
4438 {
4439 n2 = tv_get_number_chk(tv2, &error);
4440 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004441 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442#ifdef FEAT_FLOAT
4443 if (tv1->v_type == VAR_FLOAT)
4444 f2 = n2;
4445#endif
4446 }
4447#ifdef FEAT_FLOAT
4448 // if there is a float on either side the result is a float
4449 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4450 {
4451 switch (iptr->isn_arg.op.op_type)
4452 {
4453 case EXPR_MULT: f1 = f1 * f2; break;
4454 case EXPR_DIV: f1 = f1 / f2; break;
4455 case EXPR_SUB: f1 = f1 - f2; break;
4456 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004457 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004458 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004459 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 }
4461 clear_tv(tv1);
4462 clear_tv(tv2);
4463 tv1->v_type = VAR_FLOAT;
4464 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004465 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 }
4467 else
4468#endif
4469 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004470 int failed = FALSE;
4471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 switch (iptr->isn_arg.op.op_type)
4473 {
4474 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004475 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4476 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004477 goto on_error;
4478 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 case EXPR_SUB: n1 = n1 - n2; break;
4480 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004481 default: n1 = num_modulus(n1, n2, &failed);
4482 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004483 goto on_error;
4484 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 }
4486 clear_tv(tv1);
4487 clear_tv(tv2);
4488 tv1->v_type = VAR_NUMBER;
4489 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004490 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 }
4492 }
4493 break;
4494
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004495 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004496 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004497 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004498 int is_slice = iptr->isn_type == ISN_STRSLICE;
4499 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004500 char_u *res;
4501
4502 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004503 // string slice: string is at stack-3, first index at
4504 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004505 if (is_slice)
4506 {
4507 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004508 n1 = tv->vval.v_number;
4509 }
4510
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004511 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004512 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004513
Bram Moolenaar4c137212021-04-19 16:48:48 +02004514 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004515 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004516 if (is_slice)
4517 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004518 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004519 else
4520 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004521 // single character (including composing characters).
4522 // If the index is too big or negative the result is
4523 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004524 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004525 vim_free(tv->vval.v_string);
4526 tv->vval.v_string = res;
4527 }
4528 break;
4529
4530 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004531 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004532 case ISN_BLOBINDEX:
4533 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004535 int is_slice = iptr->isn_type == ISN_LISTSLICE
4536 || iptr->isn_type == ISN_BLOBSLICE;
4537 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4538 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004539 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004540 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541
4542 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004543 // list slice: list is at stack-3, indexes at stack-2 and
4544 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004545 // Same for blob.
4546 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547
4548 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004549 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004551
4552 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 {
Bram Moolenaared591872020-08-15 22:14:53 +02004554 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004555 n1 = tv->vval.v_number;
4556 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 }
Bram Moolenaared591872020-08-15 22:14:53 +02004558
Bram Moolenaar4c137212021-04-19 16:48:48 +02004559 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004560 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004561 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004562 if (is_blob)
4563 {
4564 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4565 n1, n2, FALSE, tv) == FAIL)
4566 goto on_error;
4567 }
4568 else
4569 {
4570 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4571 n1, n2, FALSE, tv, TRUE) == FAIL)
4572 goto on_error;
4573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 }
4575 break;
4576
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004577 case ISN_ANYINDEX:
4578 case ISN_ANYSLICE:
4579 {
4580 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4581 typval_T *var1, *var2;
4582 int res;
4583
4584 // index: composite is at stack-2, index at stack-1
4585 // slice: composite is at stack-3, indexes at stack-2 and
4586 // stack-1
4587 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004588 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004589 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4590 goto on_error;
4591 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4592 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004593 res = eval_index_inner(tv, is_slice, var1, var2,
4594 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004595 clear_tv(var1);
4596 if (is_slice)
4597 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004598 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004599 if (res == FAIL)
4600 goto on_error;
4601 }
4602 break;
4603
Bram Moolenaar9af78762020-06-16 11:34:42 +02004604 case ISN_SLICE:
4605 {
4606 list_T *list;
4607 int count = iptr->isn_arg.number;
4608
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004609 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004610 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004611 list = tv->vval.v_list;
4612
4613 // no error for short list, expect it to be checked earlier
4614 if (list != NULL && list->lv_len >= count)
4615 {
4616 list_T *newlist = list_slice(list,
4617 count, list->lv_len - 1);
4618
4619 if (newlist != NULL)
4620 {
4621 list_unref(list);
4622 tv->vval.v_list = newlist;
4623 ++newlist->lv_refcount;
4624 }
4625 }
4626 }
4627 break;
4628
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004629 case ISN_GETITEM:
4630 {
4631 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004632 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004633
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004634 // Get list item: list is at stack-1, push item.
4635 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004636 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4637 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004638
Bram Moolenaar35578162021-08-02 19:10:38 +02004639 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004640 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004641 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004642 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004643
4644 // Useful when used in unpack assignment. Reset at
4645 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004646 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004647 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004648 }
4649 break;
4650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 case ISN_MEMBER:
4652 {
4653 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004654 char_u *key;
4655 dictitem_T *di;
4656
4657 // dict member: dict is at stack-2, key at stack-1
4658 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004659 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004660 dict = tv->vval.v_dict;
4661
4662 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004663 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004664 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004665 if (key == NULL)
4666 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004667
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004668 if ((di = dict_find(dict, key, -1)) == NULL)
4669 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004670 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004671 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004672
4673 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004674 // stack contents makes sense and the dict stack is
4675 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004676 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004677 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004678 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004679 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004680 tv->v_type = VAR_NUMBER;
4681 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004682 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004683 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004684 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004685 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004686 // Put the dict used on the dict stack, it might be used by
4687 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004688 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004689 if (dict_stack_save(tv) == FAIL)
4690 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004691 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004692 }
4693 break;
4694
4695 // dict member with string key
4696 case ISN_STRINGMEMBER:
4697 {
4698 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 dictitem_T *di;
4700
4701 tv = STACK_TV_BOT(-1);
4702 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4703 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004704 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004705 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004706 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 }
4708 dict = tv->vval.v_dict;
4709
4710 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4711 == NULL)
4712 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004713 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004714 semsg(_(e_key_not_present_in_dictionary),
4715 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004716 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004718 // Put the dict used on the dict stack, it might be used by
4719 // a dict function later.
4720 if (dict_stack_save(tv) == FAIL)
4721 goto on_fatal_error;
4722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004724 }
4725 break;
4726
4727 case ISN_CLEARDICT:
4728 dict_stack_drop();
4729 break;
4730
4731 case ISN_USEDICT:
4732 {
4733 typval_T *dict_tv = dict_stack_get_tv();
4734
4735 // Turn "dict.Func" into a partial for "Func" bound to
4736 // "dict". Don't do this when "Func" is already a partial
4737 // that was bound explicitly (pt_auto is FALSE).
4738 tv = STACK_TV_BOT(-1);
4739 if (dict_tv != NULL
4740 && dict_tv->v_type == VAR_DICT
4741 && dict_tv->vval.v_dict != NULL
4742 && (tv->v_type == VAR_FUNC
4743 || (tv->v_type == VAR_PARTIAL
4744 && (tv->vval.v_partial->pt_auto
4745 || tv->vval.v_partial->pt_dict == NULL))))
4746 dict_tv->vval.v_dict =
4747 make_partial(dict_tv->vval.v_dict, tv);
4748 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 }
4750 break;
4751
4752 case ISN_NEGATENR:
4753 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004754 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004755#ifdef FEAT_FLOAT
4756 if (tv->v_type == VAR_FLOAT)
4757 tv->vval.v_float = -tv->vval.v_float;
4758 else
4759#endif
4760 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 break;
4762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 case ISN_CHECKTYPE:
4764 {
4765 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004766 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004767 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004769 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004770 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004771 if (!ectx->ec_where.wt_variable)
4772 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004773 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004774 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004775 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004776 if (r == FAIL)
4777 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004778 if (!ectx->ec_where.wt_variable)
4779 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004780
4781 // number 0 is FALSE, number 1 is TRUE
4782 if (tv->v_type == VAR_NUMBER
4783 && ct->ct_type->tt_type == VAR_BOOL
4784 && (tv->vval.v_number == 0
4785 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004787 tv->v_type = VAR_BOOL;
4788 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004789 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 }
4791 }
4792 break;
4793
Bram Moolenaar9af78762020-06-16 11:34:42 +02004794 case ISN_CHECKLEN:
4795 {
4796 int min_len = iptr->isn_arg.checklen.cl_min_len;
4797 list_T *list = NULL;
4798
4799 tv = STACK_TV_BOT(-1);
4800 if (tv->v_type == VAR_LIST)
4801 list = tv->vval.v_list;
4802 if (list == NULL || list->lv_len < min_len
4803 || (list->lv_len > min_len
4804 && !iptr->isn_arg.checklen.cl_more_OK))
4805 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004806 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004807 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004808 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004809 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004810 }
4811 }
4812 break;
4813
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004814 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004815 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004816 break;
4817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004818 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004819 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820 {
4821 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004822 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004824 if (iptr->isn_type == ISN_2BOOL)
4825 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004826 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004827 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004828 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004829 n = !n;
4830 }
4831 else
4832 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004833 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004834 SOURCING_LNUM = iptr->isn_lnum;
4835 n = tv_get_bool_chk(tv, &error);
4836 if (error)
4837 goto on_error;
4838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 clear_tv(tv);
4840 tv->v_type = VAR_BOOL;
4841 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4842 }
4843 break;
4844
4845 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004846 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004847 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004848 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4849 iptr->isn_type == ISN_2STRING_ANY,
4850 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004851 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 break;
4853
Bram Moolenaar08597872020-12-10 19:43:40 +01004854 case ISN_RANGE:
4855 {
4856 exarg_T ea;
4857 char *errormsg;
4858
Bram Moolenaarece0b872021-01-08 20:40:45 +01004859 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004860 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004861 ea.addr_type = ADDR_LINES;
4862 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004863 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004864 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004865 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004866
Bram Moolenaar35578162021-08-02 19:10:38 +02004867 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004868 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004869 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004870 tv = STACK_TV_BOT(-1);
4871 tv->v_type = VAR_NUMBER;
4872 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004873 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004874 }
4875 break;
4876
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004877 case ISN_PUT:
4878 {
4879 int regname = iptr->isn_arg.put.put_regname;
4880 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4881 char_u *expr = NULL;
4882 int dir = FORWARD;
4883
Bram Moolenaar08597872020-12-10 19:43:40 +01004884 if (lnum < -2)
4885 {
4886 // line number was put on the stack by ISN_RANGE
4887 tv = STACK_TV_BOT(-1);
4888 curwin->w_cursor.lnum = tv->vval.v_number;
4889 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4890 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004891 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004892 }
4893 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004894 // :put! above cursor
4895 dir = BACKWARD;
4896 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004897 {
4898 curwin->w_cursor.lnum = lnum;
4899 if (lnum == 0)
4900 // check_cursor() below will move to line 1
4901 dir = BACKWARD;
4902 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004903
4904 if (regname == '=')
4905 {
4906 tv = STACK_TV_BOT(-1);
4907 if (tv->v_type == VAR_STRING)
4908 expr = tv->vval.v_string;
4909 else
4910 {
4911 expr = typval2string(tv, TRUE); // allocates value
4912 clear_tv(tv);
4913 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004914 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004915 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004916 check_cursor();
4917 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4918 vim_free(expr);
4919 }
4920 break;
4921
Bram Moolenaar02194d22020-10-24 23:08:38 +02004922 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004923 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4924 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4925 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4926 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004927 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4928 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004929 break;
4930
Bram Moolenaar02194d22020-10-24 23:08:38 +02004931 case ISN_CMDMOD_REV:
4932 // filter regprog is owned by the instruction, don't free it
4933 cmdmod.cmod_filter_regmatch.regprog = NULL;
4934 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004935 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4936 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004937 break;
4938
Bram Moolenaar792f7862020-11-23 08:31:18 +01004939 case ISN_UNPACK:
4940 {
4941 int count = iptr->isn_arg.unpack.unp_count;
4942 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4943 list_T *l;
4944 listitem_T *li;
4945 int i;
4946
4947 // Check there is a valid list to unpack.
4948 tv = STACK_TV_BOT(-1);
4949 if (tv->v_type != VAR_LIST)
4950 {
4951 SOURCING_LNUM = iptr->isn_lnum;
4952 emsg(_(e_for_argument_must_be_sequence_of_lists));
4953 goto on_error;
4954 }
4955 l = tv->vval.v_list;
4956 if (l == NULL
4957 || l->lv_len < (semicolon ? count - 1 : count))
4958 {
4959 SOURCING_LNUM = iptr->isn_lnum;
4960 emsg(_(e_list_value_does_not_have_enough_items));
4961 goto on_error;
4962 }
4963 else if (!semicolon && l->lv_len > count)
4964 {
4965 SOURCING_LNUM = iptr->isn_lnum;
4966 emsg(_(e_list_value_has_more_items_than_targets));
4967 goto on_error;
4968 }
4969
4970 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004971 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004972 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004973 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004974
4975 // Variable after semicolon gets a list with the remaining
4976 // items.
4977 if (semicolon)
4978 {
4979 list_T *rem_list =
4980 list_alloc_with_items(l->lv_len - count + 1);
4981
4982 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004983 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004984 tv = STACK_TV_BOT(-count);
4985 tv->vval.v_list = rem_list;
4986 ++rem_list->lv_refcount;
4987 tv->v_lock = 0;
4988 li = l->lv_first;
4989 for (i = 0; i < count - 1; ++i)
4990 li = li->li_next;
4991 for (i = 0; li != NULL; ++i)
4992 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004993 typval_T tvcopy;
4994
4995 copy_tv(&li->li_tv, &tvcopy);
4996 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004997 li = li->li_next;
4998 }
4999 --count;
5000 }
5001
5002 // Produce the values in reverse order, first item last.
5003 li = l->lv_first;
5004 for (i = 0; i < count; ++i)
5005 {
5006 tv = STACK_TV_BOT(-i - 1);
5007 copy_tv(&li->li_tv, tv);
5008 li = li->li_next;
5009 }
5010
5011 list_unref(l);
5012 }
5013 break;
5014
Bram Moolenaarb2049902021-01-24 12:53:53 +01005015 case ISN_PROF_START:
5016 case ISN_PROF_END:
5017 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005018#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005019 funccall_T cookie;
5020 ufunc_T *cur_ufunc =
5021 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005022 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005023
5024 cookie.func = cur_ufunc;
5025 if (iptr->isn_type == ISN_PROF_START)
5026 {
5027 func_line_start(&cookie, iptr->isn_lnum);
5028 // if we get here the instruction is executed
5029 func_line_exec(&cookie);
5030 }
5031 else
5032 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005033#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005034 }
5035 break;
5036
Bram Moolenaare99d4222021-06-13 14:01:26 +02005037 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005038 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005039 break;
5040
Bram Moolenaar389df252020-07-09 21:20:47 +02005041 case ISN_SHUFFLE:
5042 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005043 typval_T tmp_tv;
5044 int item = iptr->isn_arg.shuffle.shfl_item;
5045 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005046
5047 tmp_tv = *STACK_TV_BOT(-item);
5048 for ( ; up > 0 && item > 1; --up)
5049 {
5050 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5051 --item;
5052 }
5053 *STACK_TV_BOT(-item) = tmp_tv;
5054 }
5055 break;
5056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005058 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005060 ectx->ec_where.wt_index = 0;
5061 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 break;
5063 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005064 continue;
5065
Bram Moolenaard032f342020-07-18 18:13:02 +02005066func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005067 // Restore previous function. If the frame pointer is where we started
5068 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005069 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005070 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005071
Bram Moolenaar4c137212021-04-19 16:48:48 +02005072 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005073 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005074 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005075 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005076
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005077on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005078 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005079 // If "emsg_silent" is set then ignore the error, unless it was set
5080 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005081 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005082 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005083 {
5084 // If a sequence of instructions causes an error while ":silent!"
5085 // was used, restore the stack length and jump ahead to restoring
5086 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005087 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005088 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005089 while (ectx->ec_stack.ga_len
5090 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005091 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005092 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005093 clear_tv(STACK_TV_BOT(0));
5094 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005095 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5096 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005097 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005098 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005099 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005100on_fatal_error:
5101 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005102 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005103 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005104 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 }
5106
5107done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005108 ret = OK;
5109theend:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005110 {
5111 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5112 + ectx->ec_dfunc_idx;
5113
5114 if (dfunc->df_defer_var_idx > 0)
5115 invoke_defer_funcs(ectx);
5116 }
5117
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005118 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005119 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5120 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005121}
5122
5123/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005124 * Execute the instructions from a VAR_INSTR typeval and put the result in
5125 * "rettv".
5126 * Return OK or FAIL.
5127 */
5128 int
5129exe_typval_instr(typval_T *tv, typval_T *rettv)
5130{
5131 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5132 isn_T *save_instr = ectx->ec_instr;
5133 int save_iidx = ectx->ec_iidx;
5134 int res;
5135
LemonBoyf3b48952022-05-05 13:53:03 +01005136 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5137 // even when the compilation fails.
5138 rettv->v_type = VAR_UNKNOWN;
5139
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005140 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5141 res = exec_instructions(ectx);
5142 if (res == OK)
5143 {
5144 *rettv = *STACK_TV_BOT(-1);
5145 --ectx->ec_stack.ga_len;
5146 }
5147
5148 ectx->ec_instr = save_instr;
5149 ectx->ec_iidx = save_iidx;
5150
5151 return res;
5152}
5153
5154/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005155 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5156 * "substitute_instr".
5157 */
5158 char_u *
5159exe_substitute_instr(void)
5160{
5161 ectx_T *ectx = substitute_instr->subs_ectx;
5162 isn_T *save_instr = ectx->ec_instr;
5163 int save_iidx = ectx->ec_iidx;
5164 char_u *res;
5165
5166 ectx->ec_instr = substitute_instr->subs_instr;
5167 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005168 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005169 typval_T *tv = STACK_TV_BOT(-1);
5170
Bram Moolenaar27523602021-06-05 21:36:19 +02005171 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005172 --ectx->ec_stack.ga_len;
5173 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005174 }
5175 else
5176 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005177 substitute_instr->subs_status = FAIL;
5178 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180
Bram Moolenaar4c137212021-04-19 16:48:48 +02005181 ectx->ec_instr = save_instr;
5182 ectx->ec_iidx = save_iidx;
5183
5184 return res;
5185}
5186
5187/*
5188 * Call a "def" function from old Vim script.
5189 * Return OK or FAIL.
5190 */
5191 int
5192call_def_function(
5193 ufunc_T *ufunc,
5194 int argc_arg, // nr of arguments
5195 typval_T *argv, // arguments
5196 partial_T *partial, // optional partial for context
5197 typval_T *rettv) // return value
5198{
5199 ectx_T ectx; // execution context
5200 int argc = argc_arg;
5201 typval_T *tv;
5202 int idx;
5203 int ret = FAIL;
5204 int defcount = ufunc->uf_args.ga_len - argc;
5205 sctx_T save_current_sctx = current_sctx;
5206 int did_emsg_before = did_emsg_cumul + did_emsg;
5207 int save_suppress_errthrow = suppress_errthrow;
5208 msglist_T **saved_msg_list = NULL;
5209 msglist_T *private_msg_list = NULL;
5210 int save_emsg_silent_def = emsg_silent_def;
5211 int save_did_emsg_def = did_emsg_def;
5212 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005213 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005214
5215// Get pointer to item in the stack.
5216#undef STACK_TV
5217#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5218
5219// Get pointer to item at the bottom of the stack, -1 is the bottom.
5220#undef STACK_TV_BOT
5221#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5222
5223// Get pointer to a local variable on the stack. Negative for arguments.
5224#undef STACK_TV_VAR
5225#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5226
5227 if (ufunc->uf_def_status == UF_NOT_COMPILED
5228 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005229 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5230 && compile_def_function(ufunc, FALSE,
5231 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005232 {
5233 if (did_emsg_cumul + did_emsg == did_emsg_before)
5234 semsg(_(e_function_is_not_compiled_str),
5235 printable_func_name(ufunc));
5236 return FAIL;
5237 }
5238
5239 {
5240 // Check the function was really compiled.
5241 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5242 + ufunc->uf_dfunc_idx;
5243 if (INSTRUCTIONS(dfunc) == NULL)
5244 {
5245 iemsg("using call_def_function() on not compiled function");
5246 return FAIL;
5247 }
5248 }
5249
5250 // If depth of calling is getting too high, don't execute the function.
5251 orig_funcdepth = funcdepth_get();
5252 if (funcdepth_increment() == FAIL)
5253 return FAIL;
5254
5255 CLEAR_FIELD(ectx);
5256 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5257 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005258 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005259 {
5260 funcdepth_decrement();
5261 return FAIL;
5262 }
5263 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5264 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5265 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005266 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005267
5268 idx = argc - ufunc->uf_args.ga_len;
5269 if (idx > 0 && ufunc->uf_va_name == NULL)
5270 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005271 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
5272 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005273 goto failed_early;
5274 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005275 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5276 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005277 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005278 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
5279 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005280 goto failed_early;
5281 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005282
5283 // Put arguments on the stack, but no more than what the function expects.
5284 // A lambda can be called with more arguments than it uses.
5285 for (idx = 0; idx < argc
5286 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5287 ++idx)
5288 {
5289 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5290 && argv[idx].v_type == VAR_SPECIAL
5291 && argv[idx].vval.v_number == VVAL_NONE)
5292 {
5293 // Use the default value.
5294 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5295 }
5296 else
5297 {
5298 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5299 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005300 ufunc->uf_arg_types[idx], &argv[idx],
5301 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005302 goto failed_early;
5303 copy_tv(&argv[idx], STACK_TV_BOT(0));
5304 }
5305 ++ectx.ec_stack.ga_len;
5306 }
5307
5308 // Turn varargs into a list. Empty list if no args.
5309 if (ufunc->uf_va_name != NULL)
5310 {
5311 int vararg_count = argc - ufunc->uf_args.ga_len;
5312
5313 if (vararg_count < 0)
5314 vararg_count = 0;
5315 else
5316 argc -= vararg_count;
5317 if (exe_newlist(vararg_count, &ectx) == FAIL)
5318 goto failed_early;
5319
5320 // Check the type of the list items.
5321 tv = STACK_TV_BOT(-1);
5322 if (ufunc->uf_va_type != NULL
5323 && ufunc->uf_va_type != &t_list_any
5324 && ufunc->uf_va_type->tt_member != &t_any
5325 && tv->vval.v_list != NULL)
5326 {
5327 type_T *expected = ufunc->uf_va_type->tt_member;
5328 listitem_T *li = tv->vval.v_list->lv_first;
5329
5330 for (idx = 0; idx < vararg_count; ++idx)
5331 {
5332 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005333 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005334 goto failed_early;
5335 li = li->li_next;
5336 }
5337 }
5338
5339 if (defcount > 0)
5340 // Move varargs list to below missing default arguments.
5341 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5342 --ectx.ec_stack.ga_len;
5343 }
5344
5345 // Make space for omitted arguments, will store default value below.
5346 // Any varargs list goes after them.
5347 if (defcount > 0)
5348 for (idx = 0; idx < defcount; ++idx)
5349 {
5350 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5351 ++ectx.ec_stack.ga_len;
5352 }
5353 if (ufunc->uf_va_name != NULL)
5354 ++ectx.ec_stack.ga_len;
5355
5356 // Frame pointer points to just after arguments.
5357 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5358 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5359
5360 {
5361 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5362 + ufunc->uf_dfunc_idx;
5363 ufunc_T *base_ufunc = dfunc->df_ufunc;
5364
5365 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5366 // by copy_func().
5367 if (partial != NULL || base_ufunc->uf_partial != NULL)
5368 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005369 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5370 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005371 goto failed_early;
5372 if (partial != NULL)
5373 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005374 outer_T *outer = get_pt_outer(partial);
5375
5376 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005377 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005378 if (current_ectx != NULL)
5379 {
5380 if (current_ectx->ec_outer_ref != NULL
5381 && current_ectx->ec_outer_ref->or_outer != NULL)
5382 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005383 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005384 }
5385 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005386 }
5387 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005388 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005389 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005390 ++partial->pt_refcount;
5391 ectx.ec_outer_ref->or_partial = partial;
5392 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005393 }
5394 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005395 {
5396 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5397 ++base_ufunc->uf_partial->pt_refcount;
5398 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5399 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005400 }
5401 }
5402
5403 // dummy frame entries
5404 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5405 {
5406 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5407 ++ectx.ec_stack.ga_len;
5408 }
5409
5410 {
5411 // Reserve space for local variables and any closure reference count.
5412 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5413 + ufunc->uf_dfunc_idx;
5414
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005415 // Initialize variables to zero. That avoids having to generate
5416 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005417 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005418 {
5419 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5420 STACK_TV_VAR(idx)->vval.v_number = 0;
5421 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005422 ectx.ec_stack.ga_len += dfunc->df_varcount;
5423 if (dfunc->df_has_closure)
5424 {
5425 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5426 STACK_TV_VAR(idx)->vval.v_number = 0;
5427 ++ectx.ec_stack.ga_len;
5428 }
5429
5430 ectx.ec_instr = INSTRUCTIONS(dfunc);
5431 }
5432
5433 // Following errors are in the function, not the caller.
5434 // Commands behave like vim9script.
5435 estack_push_ufunc(ufunc, 1);
5436 current_sctx = ufunc->uf_script_ctx;
5437 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5438
5439 // Use a specific location for storing error messages to be converted to an
5440 // exception.
5441 saved_msg_list = msg_list;
5442 msg_list = &private_msg_list;
5443
5444 // Do turn errors into exceptions.
5445 suppress_errthrow = FALSE;
5446
5447 // Do not delete the function while executing it.
5448 ++ufunc->uf_calls;
5449
5450 // When ":silent!" was used before calling then we still abort the
5451 // function. If ":silent!" is used in the function then we don't.
5452 emsg_silent_def = emsg_silent;
5453 did_emsg_def = 0;
5454
5455 ectx.ec_where.wt_index = 0;
5456 ectx.ec_where.wt_variable = FALSE;
5457
5458 // Execute the instructions until done.
5459 ret = exec_instructions(&ectx);
5460 if (ret == OK)
5461 {
5462 // function finished, get result from the stack.
5463 if (ufunc->uf_ret_type == &t_void)
5464 {
5465 rettv->v_type = VAR_VOID;
5466 }
5467 else
5468 {
5469 tv = STACK_TV_BOT(-1);
5470 *rettv = *tv;
5471 tv->v_type = VAR_UNKNOWN;
5472 }
5473 }
5474
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005475 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005476 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5477 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005478
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005479 // Deal with any remaining closures, they may be in use somewhere.
5480 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005481 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005482 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005483 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005484 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005485
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005486 estack_pop();
5487 current_sctx = save_current_sctx;
5488
Bram Moolenaar2336c372021-12-06 15:06:54 +00005489 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5490 // Function was unreferenced while being used, free it now.
5491 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005492
Bram Moolenaar352134b2020-10-17 22:04:08 +02005493 if (*msg_list != NULL && saved_msg_list != NULL)
5494 {
5495 msglist_T **plist = saved_msg_list;
5496
5497 // Append entries from the current msg_list (uncaught exceptions) to
5498 // the saved msg_list.
5499 while (*plist != NULL)
5500 plist = &(*plist)->next;
5501
5502 *plist = *msg_list;
5503 }
5504 msg_list = saved_msg_list;
5505
Bram Moolenaar4c137212021-04-19 16:48:48 +02005506 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005507 {
5508 cmdmod.cmod_filter_regmatch.regprog = NULL;
5509 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005510 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005511 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005512 emsg_silent_def = save_emsg_silent_def;
5513 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005514
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005515failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005516 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005517 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5518 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005519 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005521 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005522 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005523 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005524 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005525 if (ectx.ec_outer_ref->or_outer_allocated)
5526 vim_free(ectx.ec_outer_ref->or_outer);
5527 partial_unref(ectx.ec_outer_ref->or_partial);
5528 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005529 }
5530
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005531 // Not sure if this is necessary.
5532 suppress_errthrow = save_suppress_errthrow;
5533
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005534 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5535 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005536 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005537 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005538 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005539 return ret;
5540}
5541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005543 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5544 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5545 * "pfx" is prefixed to every line.
5546 */
5547 static void
5548list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5549{
5550 int line_idx = 0;
5551 int prev_current = 0;
5552 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005553 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005554
5555 for (current = 0; current < instr_count; ++current)
5556 {
5557 isn_T *iptr = &instr[current];
5558 char *line;
5559
5560 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005561 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005562 while (line_idx < iptr->isn_lnum
5563 && line_idx < ufunc->uf_lines.ga_len)
5564 {
5565 if (current > prev_current)
5566 {
5567 msg_puts("\n\n");
5568 prev_current = current;
5569 }
5570 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5571 if (line != NULL)
5572 msg(line);
5573 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005574 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5575 {
5576 int first_def_arg = ufunc->uf_args.ga_len
5577 - ufunc->uf_def_args.ga_len;
5578
5579 if (def_arg_idx > 0)
5580 msg_puts("\n\n");
5581 msg_start();
5582 msg_puts(" ");
5583 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5584 first_def_arg + def_arg_idx]);
5585 msg_puts(" = ");
5586 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5587 msg_clr_eos();
5588 msg_end();
5589 }
5590 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005591
5592 switch (iptr->isn_type)
5593 {
5594 case ISN_EXEC:
5595 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5596 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005597 case ISN_EXEC_SPLIT:
5598 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5599 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005600 case ISN_EXECRANGE:
5601 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5602 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005603 case ISN_LEGACY_EVAL:
5604 smsg("%s%4d EVAL legacy %s", pfx, current,
5605 iptr->isn_arg.string);
5606 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005607 case ISN_REDIRSTART:
5608 smsg("%s%4d REDIR", pfx, current);
5609 break;
5610 case ISN_REDIREND:
5611 smsg("%s%4d REDIR END%s", pfx, current,
5612 iptr->isn_arg.number ? " append" : "");
5613 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005614 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005615#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005616 smsg("%s%4d CEXPR pre %s", pfx, current,
5617 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005618#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005619 break;
5620 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005621#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005622 {
5623 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5624
5625 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5626 cexpr_get_auname(cer->cer_cmdidx),
5627 cer->cer_forceit ? "!" : "",
5628 cer->cer_cmdline);
5629 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005630#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005631 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005632 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005633 smsg("%s%4d INSTR", pfx, current);
5634 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5635 msg(" -------------");
5636 break;
5637 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005638 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005639 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5640
5641 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005642 }
5643 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005644 case ISN_SUBSTITUTE:
5645 {
5646 subs_T *subs = &iptr->isn_arg.subs;
5647
5648 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5649 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5650 msg(" -------------");
5651 }
5652 break;
5653 case ISN_EXECCONCAT:
5654 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5655 (varnumber_T)iptr->isn_arg.number);
5656 break;
5657 case ISN_ECHO:
5658 {
5659 echo_T *echo = &iptr->isn_arg.echo;
5660
5661 smsg("%s%4d %s %d", pfx, current,
5662 echo->echo_with_white ? "ECHO" : "ECHON",
5663 echo->echo_count);
5664 }
5665 break;
5666 case ISN_EXECUTE:
5667 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005668 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005669 break;
5670 case ISN_ECHOMSG:
5671 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005672 (varnumber_T)(iptr->isn_arg.number));
5673 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01005674 case ISN_ECHOWINDOW:
5675 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
5676 (varnumber_T)(iptr->isn_arg.number));
5677 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02005678 case ISN_ECHOCONSOLE:
5679 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5680 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005681 break;
5682 case ISN_ECHOERR:
5683 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005684 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005685 break;
5686 case ISN_LOAD:
5687 {
5688 if (iptr->isn_arg.number < 0)
5689 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5690 (varnumber_T)(iptr->isn_arg.number
5691 + STACK_FRAME_SIZE));
5692 else
5693 smsg("%s%4d LOAD $%lld", pfx, current,
5694 (varnumber_T)(iptr->isn_arg.number));
5695 }
5696 break;
5697 case ISN_LOADOUTER:
5698 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005699 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005700 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5701 iptr->isn_arg.outer.outer_depth,
5702 iptr->isn_arg.outer.outer_idx
5703 + STACK_FRAME_SIZE);
5704 else
5705 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5706 iptr->isn_arg.outer.outer_depth,
5707 iptr->isn_arg.outer.outer_idx);
5708 }
5709 break;
5710 case ISN_LOADV:
5711 smsg("%s%4d LOADV v:%s", pfx, current,
5712 get_vim_var_name(iptr->isn_arg.number));
5713 break;
5714 case ISN_LOADSCRIPT:
5715 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005716 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5717 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5718 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005719
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005720 sv = get_script_svar(sref, -1);
5721 if (sv == NULL)
5722 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5723 pfx, current, si->sn_name);
5724 else
5725 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005726 sv->sv_name,
5727 sref->sref_idx,
5728 si->sn_name);
5729 }
5730 break;
5731 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005732 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005733 {
5734 scriptitem_T *si = SCRIPT_ITEM(
5735 iptr->isn_arg.loadstore.ls_sid);
5736
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005737 smsg("%s%4d %s s:%s from %s", pfx, current,
5738 iptr->isn_type == ISN_LOADS ? "LOADS"
5739 : "LOADEXPORT",
5740 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005741 }
5742 break;
5743 case ISN_LOADAUTO:
5744 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5745 break;
5746 case ISN_LOADG:
5747 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5748 break;
5749 case ISN_LOADB:
5750 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5751 break;
5752 case ISN_LOADW:
5753 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5754 break;
5755 case ISN_LOADT:
5756 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5757 break;
5758 case ISN_LOADGDICT:
5759 smsg("%s%4d LOAD g:", pfx, current);
5760 break;
5761 case ISN_LOADBDICT:
5762 smsg("%s%4d LOAD b:", pfx, current);
5763 break;
5764 case ISN_LOADWDICT:
5765 smsg("%s%4d LOAD w:", pfx, current);
5766 break;
5767 case ISN_LOADTDICT:
5768 smsg("%s%4d LOAD t:", pfx, current);
5769 break;
5770 case ISN_LOADOPT:
5771 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5772 break;
5773 case ISN_LOADENV:
5774 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5775 break;
5776 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005777 smsg("%s%4d LOADREG @%c", pfx, current,
5778 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005779 break;
5780
5781 case ISN_STORE:
5782 if (iptr->isn_arg.number < 0)
5783 smsg("%s%4d STORE arg[%lld]", pfx, current,
5784 iptr->isn_arg.number + STACK_FRAME_SIZE);
5785 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005786 smsg("%s%4d STORE $%lld", pfx, current,
5787 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005788 break;
5789 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005790 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5791 iptr->isn_arg.outer.outer_depth,
5792 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005793 break;
5794 case ISN_STOREV:
5795 smsg("%s%4d STOREV v:%s", pfx, current,
5796 get_vim_var_name(iptr->isn_arg.number));
5797 break;
5798 case ISN_STOREAUTO:
5799 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5800 break;
5801 case ISN_STOREG:
5802 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5803 break;
5804 case ISN_STOREB:
5805 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5806 break;
5807 case ISN_STOREW:
5808 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5809 break;
5810 case ISN_STORET:
5811 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5812 break;
5813 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005814 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005815 {
5816 scriptitem_T *si = SCRIPT_ITEM(
5817 iptr->isn_arg.loadstore.ls_sid);
5818
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005819 smsg("%s%4d %s %s in %s", pfx, current,
5820 iptr->isn_type == ISN_STORES
5821 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005822 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5823 }
5824 break;
5825 case ISN_STORESCRIPT:
5826 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005827 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5828 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5829 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005830
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005831 sv = get_script_svar(sref, -1);
5832 if (sv == NULL)
5833 smsg("%s%4d STORESCRIPT [deleted] in %s",
5834 pfx, current, si->sn_name);
5835 else
5836 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005837 sv->sv_name,
5838 sref->sref_idx,
5839 si->sn_name);
5840 }
5841 break;
5842 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005843 case ISN_STOREFUNCOPT:
5844 smsg("%s%4d %s &%s", pfx, current,
5845 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005846 iptr->isn_arg.storeopt.so_name);
5847 break;
5848 case ISN_STOREENV:
5849 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5850 break;
5851 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005852 smsg("%s%4d STOREREG @%c", pfx, current,
5853 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005854 break;
5855 case ISN_STORENR:
5856 smsg("%s%4d STORE %lld in $%d", pfx, current,
5857 iptr->isn_arg.storenr.stnr_val,
5858 iptr->isn_arg.storenr.stnr_idx);
5859 break;
5860
5861 case ISN_STOREINDEX:
5862 smsg("%s%4d STOREINDEX %s", pfx, current,
5863 vartype_name(iptr->isn_arg.vartype));
5864 break;
5865
5866 case ISN_STORERANGE:
5867 smsg("%s%4d STORERANGE", pfx, current);
5868 break;
5869
5870 // constants
5871 case ISN_PUSHNR:
5872 smsg("%s%4d PUSHNR %lld", pfx, current,
5873 (varnumber_T)(iptr->isn_arg.number));
5874 break;
5875 case ISN_PUSHBOOL:
5876 case ISN_PUSHSPEC:
5877 smsg("%s%4d PUSH %s", pfx, current,
5878 get_var_special_name(iptr->isn_arg.number));
5879 break;
5880 case ISN_PUSHF:
5881#ifdef FEAT_FLOAT
5882 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5883#endif
5884 break;
5885 case ISN_PUSHS:
5886 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5887 break;
5888 case ISN_PUSHBLOB:
5889 {
5890 char_u *r;
5891 char_u numbuf[NUMBUFLEN];
5892 char_u *tofree;
5893
5894 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5895 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5896 vim_free(tofree);
5897 }
5898 break;
5899 case ISN_PUSHFUNC:
5900 {
5901 char *name = (char *)iptr->isn_arg.string;
5902
5903 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5904 name == NULL ? "[none]" : name);
5905 }
5906 break;
5907 case ISN_PUSHCHANNEL:
5908#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005909 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005910#endif
5911 break;
5912 case ISN_PUSHJOB:
5913#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005914 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005915#endif
5916 break;
5917 case ISN_PUSHEXC:
5918 smsg("%s%4d PUSH v:exception", pfx, current);
5919 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005920 case ISN_AUTOLOAD:
5921 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5922 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005923 case ISN_UNLET:
5924 smsg("%s%4d UNLET%s %s", pfx, current,
5925 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5926 iptr->isn_arg.unlet.ul_name);
5927 break;
5928 case ISN_UNLETENV:
5929 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5930 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5931 iptr->isn_arg.unlet.ul_name);
5932 break;
5933 case ISN_UNLETINDEX:
5934 smsg("%s%4d UNLETINDEX", pfx, current);
5935 break;
5936 case ISN_UNLETRANGE:
5937 smsg("%s%4d UNLETRANGE", pfx, current);
5938 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005939 case ISN_LOCKUNLOCK:
5940 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5941 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005942 case ISN_LOCKCONST:
5943 smsg("%s%4d LOCKCONST", pfx, current);
5944 break;
5945 case ISN_NEWLIST:
5946 smsg("%s%4d NEWLIST size %lld", pfx, current,
5947 (varnumber_T)(iptr->isn_arg.number));
5948 break;
5949 case ISN_NEWDICT:
5950 smsg("%s%4d NEWDICT size %lld", pfx, current,
5951 (varnumber_T)(iptr->isn_arg.number));
5952 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005953 case ISN_NEWPARTIAL:
5954 smsg("%s%4d NEWPARTIAL", pfx, current);
5955 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005956
5957 // function call
5958 case ISN_BCALL:
5959 {
5960 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5961
5962 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5963 internal_func_name(cbfunc->cbf_idx),
5964 cbfunc->cbf_argcount);
5965 }
5966 break;
5967 case ISN_DCALL:
5968 {
5969 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5970 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5971 + cdfunc->cdf_idx;
5972
5973 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005974 printable_func_name(df->df_ufunc),
5975 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005976 }
5977 break;
5978 case ISN_UCALL:
5979 {
5980 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5981
5982 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5983 cufunc->cuf_name, cufunc->cuf_argcount);
5984 }
5985 break;
5986 case ISN_PCALL:
5987 {
5988 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5989
5990 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5991 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5992 }
5993 break;
5994 case ISN_PCALL_END:
5995 smsg("%s%4d PCALL end", pfx, current);
5996 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005997 case ISN_DEFER:
5998 smsg("%s%4d DEFER %d args", pfx, current,
5999 (int)iptr->isn_arg.defer.defer_argcount);
6000 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006001 case ISN_RETURN:
6002 smsg("%s%4d RETURN", pfx, current);
6003 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006004 case ISN_RETURN_VOID:
6005 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006006 break;
6007 case ISN_FUNCREF:
6008 {
6009 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00006010 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006011
Bram Moolenaar38453522021-11-28 22:00:12 +00006012 if (funcref->fr_func_name == NULL)
6013 {
6014 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6015 + funcref->fr_dfunc_idx;
6016 name = df->df_ufunc->uf_name;
6017 }
6018 else
6019 name = funcref->fr_func_name;
6020 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006021 }
6022 break;
6023
6024 case ISN_NEWFUNC:
6025 {
6026 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
6027
6028 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6029 newfunc->nf_lambda, newfunc->nf_global);
6030 }
6031 break;
6032
6033 case ISN_DEF:
6034 {
6035 char_u *name = iptr->isn_arg.string;
6036
6037 smsg("%s%4d DEF %s", pfx, current,
6038 name == NULL ? (char_u *)"" : name);
6039 }
6040 break;
6041
6042 case ISN_JUMP:
6043 {
6044 char *when = "?";
6045
6046 switch (iptr->isn_arg.jump.jump_when)
6047 {
6048 case JUMP_ALWAYS:
6049 when = "JUMP";
6050 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006051 case JUMP_NEVER:
6052 iemsg("JUMP_NEVER should not be used");
6053 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006054 case JUMP_AND_KEEP_IF_TRUE:
6055 when = "JUMP_AND_KEEP_IF_TRUE";
6056 break;
6057 case JUMP_IF_FALSE:
6058 when = "JUMP_IF_FALSE";
6059 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006060 case JUMP_IF_COND_FALSE:
6061 when = "JUMP_IF_COND_FALSE";
6062 break;
6063 case JUMP_IF_COND_TRUE:
6064 when = "JUMP_IF_COND_TRUE";
6065 break;
6066 }
6067 smsg("%s%4d %s -> %d", pfx, current, when,
6068 iptr->isn_arg.jump.jump_where);
6069 }
6070 break;
6071
6072 case ISN_JUMP_IF_ARG_SET:
6073 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6074 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6075 iptr->isn_arg.jump.jump_where);
6076 break;
6077
6078 case ISN_FOR:
6079 {
6080 forloop_T *forloop = &iptr->isn_arg.forloop;
6081
6082 smsg("%s%4d FOR $%d -> %d", pfx, current,
6083 forloop->for_idx, forloop->for_end);
6084 }
6085 break;
6086
6087 case ISN_TRY:
6088 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006089 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006090
6091 if (try->try_ref->try_finally == 0)
6092 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6093 pfx, current,
6094 try->try_ref->try_catch,
6095 try->try_ref->try_endtry);
6096 else
6097 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6098 pfx, current,
6099 try->try_ref->try_catch,
6100 try->try_ref->try_finally,
6101 try->try_ref->try_endtry);
6102 }
6103 break;
6104 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006105 smsg("%s%4d CATCH", pfx, current);
6106 break;
6107 case ISN_TRYCONT:
6108 {
6109 trycont_T *trycont = &iptr->isn_arg.trycont;
6110
6111 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6112 trycont->tct_levels,
6113 trycont->tct_levels == 1 ? "" : "s",
6114 trycont->tct_where);
6115 }
6116 break;
6117 case ISN_FINALLY:
6118 smsg("%s%4d FINALLY", pfx, current);
6119 break;
6120 case ISN_ENDTRY:
6121 smsg("%s%4d ENDTRY", pfx, current);
6122 break;
6123 case ISN_THROW:
6124 smsg("%s%4d THROW", pfx, current);
6125 break;
6126
6127 // expression operations on number
6128 case ISN_OPNR:
6129 case ISN_OPFLOAT:
6130 case ISN_OPANY:
6131 {
6132 char *what;
6133 char *ins;
6134
6135 switch (iptr->isn_arg.op.op_type)
6136 {
6137 case EXPR_MULT: what = "*"; break;
6138 case EXPR_DIV: what = "/"; break;
6139 case EXPR_REM: what = "%"; break;
6140 case EXPR_SUB: what = "-"; break;
6141 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006142 case EXPR_LSHIFT: what = "<<"; break;
6143 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006144 default: what = "???"; break;
6145 }
6146 switch (iptr->isn_type)
6147 {
6148 case ISN_OPNR: ins = "OPNR"; break;
6149 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6150 case ISN_OPANY: ins = "OPANY"; break;
6151 default: ins = "???"; break;
6152 }
6153 smsg("%s%4d %s %s", pfx, current, ins, what);
6154 }
6155 break;
6156
6157 case ISN_COMPAREBOOL:
6158 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006159 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006160 case ISN_COMPARENR:
6161 case ISN_COMPAREFLOAT:
6162 case ISN_COMPARESTRING:
6163 case ISN_COMPAREBLOB:
6164 case ISN_COMPARELIST:
6165 case ISN_COMPAREDICT:
6166 case ISN_COMPAREFUNC:
6167 case ISN_COMPAREANY:
6168 {
6169 char *p;
6170 char buf[10];
6171 char *type;
6172
6173 switch (iptr->isn_arg.op.op_type)
6174 {
6175 case EXPR_EQUAL: p = "=="; break;
6176 case EXPR_NEQUAL: p = "!="; break;
6177 case EXPR_GREATER: p = ">"; break;
6178 case EXPR_GEQUAL: p = ">="; break;
6179 case EXPR_SMALLER: p = "<"; break;
6180 case EXPR_SEQUAL: p = "<="; break;
6181 case EXPR_MATCH: p = "=~"; break;
6182 case EXPR_IS: p = "is"; break;
6183 case EXPR_ISNOT: p = "isnot"; break;
6184 case EXPR_NOMATCH: p = "!~"; break;
6185 default: p = "???"; break;
6186 }
6187 STRCPY(buf, p);
6188 if (iptr->isn_arg.op.op_ic == TRUE)
6189 strcat(buf, "?");
6190 switch(iptr->isn_type)
6191 {
6192 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6193 case ISN_COMPARESPECIAL:
6194 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006195 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006196 case ISN_COMPARENR: type = "COMPARENR"; break;
6197 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6198 case ISN_COMPARESTRING:
6199 type = "COMPARESTRING"; break;
6200 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6201 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6202 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6203 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6204 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6205 default: type = "???"; break;
6206 }
6207
6208 smsg("%s%4d %s %s", pfx, current, type, buf);
6209 }
6210 break;
6211
6212 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6213 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6214
6215 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006216 case ISN_CONCAT:
6217 smsg("%s%4d CONCAT size %lld", pfx, current,
6218 (varnumber_T)(iptr->isn_arg.number));
6219 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006220 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6221 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6222 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6223 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6224 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6225 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6226 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6227 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6228 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6229 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6230 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006231 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006232 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6233 iptr->isn_arg.getitem.gi_index,
6234 iptr->isn_arg.getitem.gi_with_op ?
6235 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006236 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6237 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6238 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006239 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6240 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6241
Bram Moolenaar4c137212021-04-19 16:48:48 +02006242 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6243
Bram Moolenaar4c137212021-04-19 16:48:48 +02006244 case ISN_CHECKTYPE:
6245 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006246 checktype_T *ct = &iptr->isn_arg.type;
6247 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006248
6249 if (ct->ct_arg_idx == 0)
6250 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6251 type_name(ct->ct_type, &tofree),
6252 (int)ct->ct_off);
6253 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006254 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006255 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006256 type_name(ct->ct_type, &tofree),
6257 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006258 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006259 (int)ct->ct_arg_idx);
6260 vim_free(tofree);
6261 break;
6262 }
6263 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6264 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6265 iptr->isn_arg.checklen.cl_min_len);
6266 break;
6267 case ISN_SETTYPE:
6268 {
6269 char *tofree;
6270
6271 smsg("%s%4d SETTYPE %s", pfx, current,
6272 type_name(iptr->isn_arg.type.ct_type, &tofree));
6273 vim_free(tofree);
6274 break;
6275 }
6276 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006277 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6278 smsg("%s%4d INVERT %d (!val)", pfx, current,
6279 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006280 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006281 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6282 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006283 break;
6284 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006285 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006286 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006287 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6288 pfx, current,
6289 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006290 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006291 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6292 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006293 break;
6294 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006295 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006296 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006297 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006298 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6299 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006300 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006301 else
6302 smsg("%s%4d PUT %c %ld", pfx, current,
6303 iptr->isn_arg.put.put_regname,
6304 (long)iptr->isn_arg.put.put_lnum);
6305 break;
6306
Bram Moolenaar4c137212021-04-19 16:48:48 +02006307 case ISN_CMDMOD:
6308 {
6309 char_u *buf;
6310 size_t len = produce_cmdmods(
6311 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6312
6313 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006314 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006315 {
6316 (void)produce_cmdmods(
6317 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6318 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6319 vim_free(buf);
6320 }
6321 break;
6322 }
6323 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6324
6325 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006326 smsg("%s%4d PROFILE START line %d", pfx, current,
6327 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006328 break;
6329
6330 case ISN_PROF_END:
6331 smsg("%s%4d PROFILE END", pfx, current);
6332 break;
6333
Bram Moolenaare99d4222021-06-13 14:01:26 +02006334 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006335 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6336 iptr->isn_arg.debug.dbg_break_lnum + 1,
6337 iptr->isn_lnum,
6338 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006339 break;
6340
Bram Moolenaar4c137212021-04-19 16:48:48 +02006341 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6342 iptr->isn_arg.unpack.unp_count,
6343 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6344 break;
6345 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6346 iptr->isn_arg.shuffle.shfl_item,
6347 iptr->isn_arg.shuffle.shfl_up);
6348 break;
6349 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6350
6351 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6352 return;
6353 }
6354
6355 out_flush(); // output one line at a time
6356 ui_breakcheck();
6357 if (got_int)
6358 break;
6359 }
6360}
6361
6362/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006363 * Handle command line completion for the :disassemble command.
6364 */
6365 void
6366set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6367{
6368 char_u *p;
6369
6370 // Default: expand user functions, "debug" and "profile"
6371 xp->xp_context = EXPAND_DISASSEMBLE;
6372 xp->xp_pattern = arg;
6373
6374 // first argument already typed: only user function names
6375 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6376 {
6377 xp->xp_context = EXPAND_USER_FUNC;
6378 xp->xp_pattern = skipwhite(p);
6379 }
6380}
6381
6382/*
6383 * Function given to ExpandGeneric() to obtain the list of :disassemble
6384 * arguments.
6385 */
6386 char_u *
6387get_disassemble_argument(expand_T *xp, int idx)
6388{
6389 if (idx == 0)
6390 return (char_u *)"debug";
6391 if (idx == 1)
6392 return (char_u *)"profile";
6393 return get_user_func_name(xp, idx - 2);
6394}
6395
6396/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006397 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006398 * We don't really need this at runtime, but we do have tests that require it,
6399 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 */
6401 void
6402ex_disassemble(exarg_T *eap)
6403{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006404 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006405 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006407 isn_T *instr = NULL; // init to shut up gcc warning
6408 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006409 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006410
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006411 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006412 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006413 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006414 if (func_needs_compiling(ufunc, compile_type)
6415 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006416 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006417 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006418 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006419 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006420 return;
6421 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006422 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423
6424 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006425 switch (compile_type)
6426 {
6427 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006428#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006429 instr = dfunc->df_instr_prof;
6430 instr_count = dfunc->df_instr_prof_count;
6431 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006432#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006433 // FALLTHROUGH
6434 case CT_NONE:
6435 instr = dfunc->df_instr;
6436 instr_count = dfunc->df_instr_count;
6437 break;
6438 case CT_DEBUG:
6439 instr = dfunc->df_instr_debug;
6440 instr_count = dfunc->df_instr_debug_count;
6441 break;
6442 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006443
Bram Moolenaar4c137212021-04-19 16:48:48 +02006444 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006445}
6446
6447/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006448 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449 * list, etc. Mostly like what JavaScript does, except that empty list and
6450 * empty dictionary are FALSE.
6451 */
6452 int
6453tv2bool(typval_T *tv)
6454{
6455 switch (tv->v_type)
6456 {
6457 case VAR_NUMBER:
6458 return tv->vval.v_number != 0;
6459 case VAR_FLOAT:
6460#ifdef FEAT_FLOAT
6461 return tv->vval.v_float != 0.0;
6462#else
6463 break;
6464#endif
6465 case VAR_PARTIAL:
6466 return tv->vval.v_partial != NULL;
6467 case VAR_FUNC:
6468 case VAR_STRING:
6469 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6470 case VAR_LIST:
6471 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6472 case VAR_DICT:
6473 return tv->vval.v_dict != NULL
6474 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6475 case VAR_BOOL:
6476 case VAR_SPECIAL:
6477 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6478 case VAR_JOB:
6479#ifdef FEAT_JOB_CHANNEL
6480 return tv->vval.v_job != NULL;
6481#else
6482 break;
6483#endif
6484 case VAR_CHANNEL:
6485#ifdef FEAT_JOB_CHANNEL
6486 return tv->vval.v_channel != NULL;
6487#else
6488 break;
6489#endif
6490 case VAR_BLOB:
6491 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6492 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006493 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006495 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006496 break;
6497 }
6498 return FALSE;
6499}
6500
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006501 void
6502emsg_using_string_as(typval_T *tv, int as_number)
6503{
6504 semsg(_(as_number ? e_using_string_as_number_str
6505 : e_using_string_as_bool_str),
6506 tv->vval.v_string == NULL
6507 ? (char_u *)"" : tv->vval.v_string);
6508}
6509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006510/*
6511 * If "tv" is a string give an error and return FAIL.
6512 */
6513 int
6514check_not_string(typval_T *tv)
6515{
6516 if (tv->v_type == VAR_STRING)
6517 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006518 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006519 clear_tv(tv);
6520 return FAIL;
6521 }
6522 return OK;
6523}
6524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525
6526#endif // FEAT_EVAL