blob: 46251545ed9497ae297d1ef9125ebcc05e44c4e0 [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
Bram Moolenaar806a2732022-09-04 15:40:36 +0100848// Ugly static to avoid passing the execution context around through many
849// layers.
850static ectx_T *current_ectx = NULL;
851
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000852/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100853 * Return TRUE if currently executing a :def function.
854 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100855 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100856 int
857in_def_function(void)
858{
859 return current_ectx != NULL;
860}
861
862/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100863 * Clear "current_ectx" and return the previous value. To be used when calling
864 * a user function.
865 */
866 ectx_T *
867clear_currrent_ectx(void)
868{
869 ectx_T *r = current_ectx;
870
871 current_ectx = NULL;
872 return r;
873}
874
875 void
876restore_current_ectx(ectx_T *ectx)
877{
878 if (current_ectx != NULL)
879 iemsg("Restoring current_ectx while it is not NULL");
880 current_ectx = ectx;
881}
882
883/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100884 * Add an entry for a deferred function call to the currently executing
885 * function.
886 * Return the list or NULL when failed.
887 */
888 static list_T *
889add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100890{
891 typval_T *defer_tv = STACK_TV_VAR(var_idx);
892 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100893 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100894 typval_T listval;
895
896 if (defer_tv->v_type != VAR_LIST)
897 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100898 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100899 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100900 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100901 }
902 defer_l = defer_tv->vval.v_list;
903
904 l = list_alloc_with_items(argcount + 1);
905 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100906 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100907 listval.v_type = VAR_LIST;
908 listval.vval.v_list = l;
909 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100910 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100911 {
912 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100913 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100914 }
915
Bram Moolenaar806a2732022-09-04 15:40:36 +0100916 return l;
917}
918
919/*
920 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
921 * The local variable that lists deferred functions is "var_idx".
922 * Returns OK or FAIL.
923 */
924 static int
925defer_command(int var_idx, int argcount, ectx_T *ectx)
926{
927 list_T *l = add_defer_item(var_idx, argcount, ectx);
928 int i;
929 typval_T *func_tv;
930
931 if (l == NULL)
932 return FAIL;
933
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100934 func_tv = STACK_TV_BOT(-argcount - 1);
935 // TODO: check type is a funcref
936 list_set_item(l, 0, func_tv);
937
938 for (i = 1; i <= argcount; ++i)
939 list_set_item(l, i, STACK_TV_BOT(-argcount + i - 1));
940 ectx->ec_stack.ga_len -= argcount + 1;
941 return OK;
942}
943
944/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100945 * Add a deferred function "name" with one argument "arg_tv".
946 * Consumes "name", also on failure.
947 * Only to be called when in_def_function() returns TRUE.
948 */
949 int
950add_defer_function(char_u *name, int argcount, typval_T *argvars)
951{
952 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
953 + current_ectx->ec_dfunc_idx;
954 list_T *l;
955 typval_T func_tv;
956 int i;
957
958 if (dfunc->df_defer_var_idx == 0)
959 {
960 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100961 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100962 return FAIL;
963 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100964
965 l = add_defer_item(dfunc->df_defer_var_idx - 1, 1, current_ectx);
966 if (l == NULL)
967 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100968 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100969 return FAIL;
970 }
971
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100972 func_tv.v_type = VAR_FUNC;
973 func_tv.v_lock = 0;
974 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100975 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100976
Bram Moolenaar806a2732022-09-04 15:40:36 +0100977 for (i = 0; i < argcount; ++i)
978 list_set_item(l, i + 1, argvars + i);
979 return OK;
980}
981
982/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100983 * Invoked when returning from a function: Invoke any deferred calls.
984 */
985 static void
986invoke_defer_funcs(ectx_T *ectx)
987{
988 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
989 + ectx->ec_dfunc_idx;
990 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
991 listitem_T *li;
992
993 if (defer_tv->v_type != VAR_LIST)
994 return; // no function added
995 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
996 {
997 list_T *l = li->li_tv.vval.v_list;
998 typval_T rettv;
999 typval_T argvars[MAX_FUNC_ARGS];
1000 int i;
1001 listitem_T *arg_li = l->lv_first;
1002 funcexe_T funcexe;
1003
1004 for (i = 0; i < l->lv_len - 1; ++i)
1005 {
1006 arg_li = arg_li->li_next;
1007 argvars[i] = arg_li->li_tv;
1008 }
1009
1010 CLEAR_FIELD(funcexe);
1011 funcexe.fe_evaluate = TRUE;
1012 rettv.v_type = VAR_UNKNOWN;
1013 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1014 &rettv, l->lv_len - 1, argvars, &funcexe);
1015 clear_tv(&rettv);
1016 }
1017}
1018
1019/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 * Return from the current function.
1021 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001022 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001023func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001025 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001026 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001027 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1028 + ectx->ec_dfunc_idx;
1029 int argcount = ufunc_argcount(dfunc->df_ufunc);
1030 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001031 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001032 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1033 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001034 funclocal_T *floc;
1035#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001036 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1037 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038
Bram Moolenaar12d26532021-02-19 19:13:21 +01001039 if (do_profiling == PROF_YES)
1040 {
1041 ufunc_T *caller = prev_dfunc->df_ufunc;
1042
1043 if (dfunc->df_ufunc->uf_profiling
1044 || (caller != NULL && caller->uf_profiling))
1045 {
1046 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1047 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1048 --profile_info_ga.ga_len;
1049 }
1050 }
1051#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001052
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001053 if (dfunc->df_defer_var_idx > 0)
1054 invoke_defer_funcs(ectx);
1055
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001056 // No check for uf_refcount being zero, cannot think of a way that would
1057 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001058 --dfunc->df_ufunc->uf_calls;
1059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001061 entry = estack_pop();
1062 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001063 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001065 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1066 return FAIL;
1067
1068 // Clear the arguments.
1069 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1070 clear_tv(STACK_TV(idx));
1071
1072 // Clear local variables and temp values, but not the return value.
1073 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001074 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001076
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001077 // The return value should be on top of the stack. However, when aborting
1078 // it may not be there and ec_frame_idx is the top of the stack.
1079 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001080 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001081 ret_idx = 0;
1082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001083 if (ectx->ec_outer_ref != NULL)
1084 {
1085 if (ectx->ec_outer_ref->or_outer_allocated)
1086 vim_free(ectx->ec_outer_ref->or_outer);
1087 partial_unref(ectx->ec_outer_ref->or_partial);
1088 vim_free(ectx->ec_outer_ref);
1089 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001090
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001091 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001092 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001093 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1094 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001095 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1096 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001097 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001098 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001099 floc = (void *)STACK_TV(ectx->ec_frame_idx
1100 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001101 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001102 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1103 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001104
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001105 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001106 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001107 else
1108 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001109 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001110 vim_free(floc);
1111 }
1112
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001113 if (ret_idx > 0)
1114 {
1115 // Reset the stack to the position before the call, with a spot for the
1116 // return value, moved there from above the frame.
1117 ectx->ec_stack.ga_len = top + 1;
1118 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1119 }
1120 else
1121 // Reset the stack to the position before the call.
1122 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001123
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001124 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001125 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001126 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127}
1128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129/*
1130 * Prepare arguments and rettv for calling a builtin or user function.
1131 */
1132 static int
1133call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1134{
1135 int idx;
1136 typval_T *tv;
1137
1138 // Move arguments from bottom of the stack to argvars[] and add terminator.
1139 for (idx = 0; idx < argcount; ++idx)
1140 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1141 argvars[argcount].v_type = VAR_UNKNOWN;
1142
1143 // Result replaces the arguments on the stack.
1144 if (argcount > 0)
1145 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001146 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147 return FAIL;
1148 else
1149 ++ectx->ec_stack.ga_len;
1150
1151 // Default return value is zero.
1152 tv = STACK_TV_BOT(-1);
1153 tv->v_type = VAR_NUMBER;
1154 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001155 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156
1157 return OK;
1158}
1159
1160/*
1161 * Call a builtin function by index.
1162 */
1163 static int
1164call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1165{
1166 typval_T argvars[MAX_FUNC_ARGS];
1167 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001168 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001169 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001170 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171
1172 if (call_prepare(argcount, argvars, ectx) == FAIL)
1173 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001174 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001176 // Call the builtin function. Set "current_ectx" so that when it
1177 // recursively invokes call_def_function() a closure context can be set.
1178 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001180 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001181 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182
1183 // Clear the arguments.
1184 for (idx = 0; idx < argcount; ++idx)
1185 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001186
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001187 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001188 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 return OK;
1190}
1191
1192/*
1193 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001194 * If the function is compiled this will add a stack frame and set the
1195 * instruction pointer at the start of the function.
1196 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001197 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001198 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 */
1200 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001201call_ufunc(
1202 ufunc_T *ufunc,
1203 partial_T *pt,
1204 int argcount,
1205 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001206 isn_T *iptr,
1207 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208{
1209 typval_T argvars[MAX_FUNC_ARGS];
1210 funcexe_T funcexe;
1211 int error;
1212 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001213 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001214 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215
Bram Moolenaare99d4222021-06-13 14:01:26 +02001216 if (func_needs_compiling(ufunc, compile_type)
1217 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1218 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001219 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001220 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001221 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001222 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001223 if (error != FCERR_UNKNOWN)
1224 {
1225 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001226 semsg(_(e_too_many_arguments_for_function_str),
1227 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001228 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001229 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001230 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001231 return FAIL;
1232 }
1233
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001234 // The function has been compiled, can call it quickly. For a function
1235 // that was defined later: we can call it directly next time.
1236 if (iptr != NULL)
1237 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001238 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001239 iptr->isn_type = ISN_DCALL;
1240 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1241 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1242 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001243 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245
1246 if (call_prepare(argcount, argvars, ectx) == FAIL)
1247 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001248 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001249 funcexe.fe_evaluate = TRUE;
1250 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251
1252 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001254 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255
1256 // Clear the arguments.
1257 for (idx = 0; idx < argcount; ++idx)
1258 clear_tv(&argvars[idx]);
1259
1260 if (error != FCERR_NONE)
1261 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001262 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 return FAIL;
1264 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001265 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001266 // Error other than from calling the function itself.
1267 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 return OK;
1269}
1270
1271/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001272 * If command modifiers were applied restore them.
1273 */
1274 static void
1275may_restore_cmdmod(funclocal_T *funclocal)
1276{
1277 if (funclocal->floc_restore_cmdmod)
1278 {
1279 cmdmod.cmod_filter_regmatch.regprog = NULL;
1280 undo_cmdmod(&cmdmod);
1281 cmdmod = funclocal->floc_save_cmdmod;
1282 funclocal->floc_restore_cmdmod = FALSE;
1283 }
1284}
1285
1286/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001287 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1288 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001289 */
1290 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001291vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001292{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001293 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001294}
1295
1296/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 * Execute a function by "name".
1298 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001299 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 * Returns FAIL if not found without an error message.
1301 */
1302 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001303call_by_name(
1304 char_u *name,
1305 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001306 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001307 isn_T *iptr,
1308 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309{
1310 ufunc_T *ufunc;
1311
1312 if (builtin_function(name, -1))
1313 {
1314 int func_idx = find_internal_func(name);
1315
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001316 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001318 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 return FAIL;
1320 return call_bfunc(func_idx, argcount, ectx);
1321 }
1322
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001323 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001324
1325 if (ufunc == NULL)
1326 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001327 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001328
1329 if (script_autoload(name, TRUE))
1330 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001331 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001332
1333 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001334 return FAIL; // bail out if loading the script caused an error
1335 }
1336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001338 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001339 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001340 {
1341 int i;
1342 typval_T *argv = STACK_TV_BOT(0) - argcount;
1343
1344 // The function can change at runtime, check that the argument
1345 // types are correct.
1346 for (i = 0; i < argcount; ++i)
1347 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001348 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001349
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001350 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001351 type = ufunc->uf_arg_types[i];
1352 else if (ufunc->uf_va_type != NULL)
1353 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001354 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001355 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001356 return FAIL;
1357 }
1358 }
1359
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001360 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001361 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362
1363 return FAIL;
1364}
1365
1366 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001367call_partial(
1368 typval_T *tv,
1369 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001370 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001372 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001373 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001375 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001376 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377
1378 if (tv->v_type == VAR_PARTIAL)
1379 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001380 partial_T *pt = tv->vval.v_partial;
1381 int i;
1382
1383 if (pt->pt_argc > 0)
1384 {
1385 // Make space for arguments from the partial, shift the "argcount"
1386 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001387 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001388 return FAIL;
1389 for (i = 1; i <= argcount; ++i)
1390 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1391 ectx->ec_stack.ga_len += pt->pt_argc;
1392 argcount += pt->pt_argc;
1393
1394 // copy the arguments from the partial onto the stack
1395 for (i = 0; i < pt->pt_argc; ++i)
1396 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1397 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001398 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399
1400 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001401 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 name = pt->pt_name;
1404 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001405 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001407 if (name != NULL)
1408 {
1409 char_u fname_buf[FLEN_FIXED + 1];
1410 char_u *tofree = NULL;
1411 int error = FCERR_NONE;
1412 char_u *fname;
1413
1414 // May need to translate <SNR>123_ to K_SNR.
1415 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1416 if (error != FCERR_NONE)
1417 res = FAIL;
1418 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001419 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001420 vim_free(tofree);
1421 }
1422
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001423 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 {
1425 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001426 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001427 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428 return FAIL;
1429 }
1430 return OK;
1431}
1432
1433/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001434 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1435 * TRUE.
1436 */
1437 static int
1438error_if_locked(int lock, char *error)
1439{
1440 if (lock & (VAR_LOCKED | VAR_FIXED))
1441 {
1442 emsg(_(error));
1443 return TRUE;
1444 }
1445 return FALSE;
1446}
1447
1448/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001449 * Give an error if "tv" is not a number and return FAIL.
1450 */
1451 static int
1452check_for_number(typval_T *tv)
1453{
1454 if (tv->v_type != VAR_NUMBER)
1455 {
1456 semsg(_(e_expected_str_but_got_str),
1457 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1458 return FAIL;
1459 }
1460 return OK;
1461}
1462
1463/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001464 * Store "tv" in variable "name".
1465 * This is for s: and g: variables.
1466 */
1467 static void
1468store_var(char_u *name, typval_T *tv)
1469{
1470 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001471 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001472
Bram Moolenaard877a572021-04-01 19:42:48 +02001473 if (tv->v_lock)
1474 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001475 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001476 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001477 restore_funccal();
1478}
1479
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001480/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001481 * Convert "tv" to a string.
1482 * Return FAIL if not allowed.
1483 */
1484 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001485do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001486{
1487 if (tv->v_type != VAR_STRING)
1488 {
1489 char_u *str;
1490
1491 if (is_2string_any)
1492 {
1493 switch (tv->v_type)
1494 {
1495 case VAR_SPECIAL:
1496 case VAR_BOOL:
1497 case VAR_NUMBER:
1498 case VAR_FLOAT:
1499 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001500
1501 case VAR_LIST:
1502 if (tolerant)
1503 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001504 char_u *s, *e, *p;
1505 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001506
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001507 ga_init2(&ga, sizeof(char_u *), 1);
1508
1509 // Convert to NL separated items, then
1510 // escape the items and replace the NL with
1511 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001512 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001513 if (str == NULL)
1514 return FAIL;
1515 s = str;
1516 while ((e = vim_strchr(s, '\n')) != NULL)
1517 {
1518 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001519 p = vim_strsave_fnameescape(s,
1520 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001521 if (p != NULL)
1522 {
1523 ga_concat(&ga, p);
1524 ga_concat(&ga, (char_u *)" ");
1525 vim_free(p);
1526 }
1527 s = e + 1;
1528 }
1529 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001530 clear_tv(tv);
1531 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001532 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001533 return OK;
1534 }
1535 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001536 default: to_string_error(tv->v_type);
1537 return FAIL;
1538 }
1539 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001540 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001541 clear_tv(tv);
1542 tv->v_type = VAR_STRING;
1543 tv->vval.v_string = str;
1544 }
1545 return OK;
1546}
1547
1548/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001549 * When the value of "sv" is a null list of dict, allocate it.
1550 */
1551 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001552allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001553{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001554 typval_T *tv = sv->sv_tv;
1555
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001556 switch (tv->v_type)
1557 {
1558 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001559 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001560 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001561 break;
1562 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001563 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001564 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001565 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001566 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001567 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001568 (void)rettv_blob_alloc(tv);
1569 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001570 default:
1571 break;
1572 }
1573}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001574
Bram Moolenaare7525c52021-01-09 13:20:37 +01001575/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001576 * Return the character "str[index]" where "index" is the character index,
1577 * including composing characters.
1578 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001579 */
1580 char_u *
1581char_from_string(char_u *str, varnumber_T index)
1582{
1583 size_t nbyte = 0;
1584 varnumber_T nchar = index;
1585 size_t slen;
1586
1587 if (str == NULL)
1588 return NULL;
1589 slen = STRLEN(str);
1590
Bram Moolenaarff871402021-03-26 13:34:05 +01001591 // Do the same as for a list: a negative index counts from the end.
1592 // Optimization to check the first byte to be below 0x80 (and no composing
1593 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001594 if (index < 0)
1595 {
1596 int clen = 0;
1597
1598 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001599 {
1600 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1601 ++nbyte;
1602 else if (enc_utf8)
1603 nbyte += utfc_ptr2len(str + nbyte);
1604 else
1605 nbyte += mb_ptr2len(str + nbyte);
1606 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001607 nchar = clen + index;
1608 if (nchar < 0)
1609 // unlike list: index out of range results in empty string
1610 return NULL;
1611 }
1612
1613 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001614 {
1615 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1616 ++nbyte;
1617 else if (enc_utf8)
1618 nbyte += utfc_ptr2len(str + nbyte);
1619 else
1620 nbyte += mb_ptr2len(str + nbyte);
1621 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001622 if (nbyte >= slen)
1623 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001624 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001625}
1626
1627/*
1628 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001629 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001630 * If going over the end return "str_len".
1631 * If "idx" is negative count from the end, -1 is the last character.
1632 * When going over the start return -1.
1633 */
1634 static long
1635char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1636{
1637 varnumber_T nchar = idx;
1638 size_t nbyte = 0;
1639
1640 if (nchar >= 0)
1641 {
1642 while (nchar > 0 && nbyte < str_len)
1643 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001644 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001645 --nchar;
1646 }
1647 }
1648 else
1649 {
1650 nbyte = str_len;
1651 while (nchar < 0 && nbyte > 0)
1652 {
1653 --nbyte;
1654 nbyte -= mb_head_off(str, str + nbyte);
1655 ++nchar;
1656 }
1657 if (nchar < 0)
1658 return -1;
1659 }
1660 return (long)nbyte;
1661}
1662
1663/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001664 * Return the slice "str[first : last]" using character indexes. Composing
1665 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001666 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001667 * Return NULL when the result is empty.
1668 */
1669 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001670string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001671{
1672 long start_byte, end_byte;
1673 size_t slen;
1674
1675 if (str == NULL)
1676 return NULL;
1677 slen = STRLEN(str);
1678 start_byte = char_idx2byte(str, slen, first);
1679 if (start_byte < 0)
1680 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001681 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001682 end_byte = (long)slen;
1683 else
1684 {
1685 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001686 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001687 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001688 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001689 }
1690
1691 if (start_byte >= (long)slen || end_byte <= start_byte)
1692 return NULL;
1693 return vim_strnsave(str + start_byte, end_byte - start_byte);
1694}
1695
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001696/*
1697 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1698 * When "dfunc_idx" is negative don't give an error.
1699 * Returns NULL for an error.
1700 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001701 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001702get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001703{
1704 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001705 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1706 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001707 svar_T *sv;
1708
1709 if (sref->sref_seq != si->sn_script_seq)
1710 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001711 // The script was reloaded after the function was compiled, the
1712 // script_idx may not be valid.
1713 if (dfunc != NULL)
1714 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1715 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001716 return NULL;
1717 }
1718 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001719 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001720 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001721 if (dfunc != NULL)
1722 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001723 return NULL;
1724 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001725
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001726 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1727 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001728 {
1729 if (dfunc != NULL)
1730 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1731 return NULL;
1732 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001733 return sv;
1734}
1735
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001736/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001737 * Function passed to do_cmdline() for splitting a script joined by NL
1738 * characters.
1739 */
1740 static char_u *
1741get_split_sourceline(
1742 int c UNUSED,
1743 void *cookie,
1744 int indent UNUSED,
1745 getline_opt_T options UNUSED)
1746{
1747 source_cookie_T *sp = (source_cookie_T *)cookie;
1748 char_u *p;
1749 char_u *line;
1750
Bram Moolenaar20677332021-06-06 17:02:53 +02001751 p = vim_strchr(sp->nextline, '\n');
1752 if (p == NULL)
1753 {
1754 line = vim_strsave(sp->nextline);
1755 sp->nextline += STRLEN(sp->nextline);
1756 }
1757 else
1758 {
1759 line = vim_strnsave(sp->nextline, p - sp->nextline);
1760 sp->nextline = p + 1;
1761 }
1762 return line;
1763}
1764
1765/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766 * Execute a function by "name".
1767 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001768 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 */
1770 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001771call_eval_func(
1772 char_u *name,
1773 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001774 ectx_T *ectx,
1775 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776{
Bram Moolenaared677f52020-08-12 16:38:10 +02001777 int called_emsg_before = called_emsg;
1778 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001780 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001781 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001783 dictitem_T *v;
1784
1785 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001786 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1787 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001788 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001789 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001790 return FAIL;
1791 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001792 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001794 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795}
1796
1797/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001798 * When a function reference is used, fill a partial with the information
1799 * needed, especially when it is used as a closure.
1800 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001801 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001802fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1803{
1804 pt->pt_func = ufunc;
1805 pt->pt_refcount = 1;
1806
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001807 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001808 {
1809 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1810 + ectx->ec_dfunc_idx;
1811
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001812 // The closure may need to find arguments and local variables in the
1813 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001814 pt->pt_outer.out_stack = &ectx->ec_stack;
1815 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001816 if (ectx->ec_outer_ref != NULL)
1817 {
1818 // The current context already has a context, link to that one.
1819 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1820 if (ectx->ec_outer_ref->or_partial != NULL)
1821 {
1822 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1823 ++pt->pt_outer.out_up_partial->pt_refcount;
1824 }
1825 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001826
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001827 // If this function returns and the closure is still being used, we
1828 // need to make a copy of the context (arguments and local variables).
1829 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001830 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001831 {
1832 vim_free(pt);
1833 return FAIL;
1834 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001835 // Extra variable keeps the count of closures created in the current
1836 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001837 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1838 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1839
1840 ((partial_T **)ectx->ec_funcrefs.ga_data)
1841 [ectx->ec_funcrefs.ga_len] = pt;
1842 ++pt->pt_refcount;
1843 ++ectx->ec_funcrefs.ga_len;
1844 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001845 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001846 return OK;
1847}
1848
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001849/*
1850 * Execute iptr->isn_arg.string as an Ex command.
1851 */
1852 static int
1853exec_command(isn_T *iptr)
1854{
1855 source_cookie_T cookie;
1856
1857 SOURCING_LNUM = iptr->isn_lnum;
1858 // Pass getsourceline to get an error for a missing ":end"
1859 // command.
1860 CLEAR_FIELD(cookie);
1861 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1862 if (do_cmdline(iptr->isn_arg.string,
1863 getsourceline, &cookie,
1864 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1865 || did_emsg)
1866 return FAIL;
1867 return OK;
1868}
1869
Bram Moolenaar89445512022-04-14 12:58:23 +01001870/*
1871 * If script "sid" is not loaded yet then load it now.
1872 * Caller must make sure "sid" is a valid script ID.
1873 * "loaded" is set to TRUE if the script had to be loaded.
1874 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1875 */
1876 int
1877may_load_script(int sid, int *loaded)
1878{
1879 scriptitem_T *si = SCRIPT_ITEM(sid);
1880
1881 if (si->sn_state == SN_STATE_NOT_LOADED)
1882 {
1883 *loaded = TRUE;
1884 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1885 {
1886 semsg(_(e_cant_open_file_str), si->sn_name);
1887 return FAIL;
1888 }
1889 }
1890 return OK;
1891}
1892
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001893// used for v_instr of typval of VAR_INSTR
1894struct instr_S {
1895 ectx_T *instr_ectx;
1896 isn_T *instr_instr;
1897};
1898
Bram Moolenaar4c137212021-04-19 16:48:48 +02001899// used for substitute_instr
1900typedef struct subs_expr_S {
1901 ectx_T *subs_ectx;
1902 isn_T *subs_instr;
1903 int subs_status;
1904} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001906// Set when calling do_debug().
1907static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001908static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001909
1910/*
1911 * When debugging lookup "name" and return the typeval.
1912 * When not found return NULL.
1913 */
1914 typval_T *
1915lookup_debug_var(char_u *name)
1916{
1917 int idx;
1918 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001919 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001920 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001921 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001922
1923 if (ectx == NULL)
1924 return NULL;
1925 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1926
1927 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001928 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001929 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001930 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1931
1932 // the variable name may be NULL when not available in this block
1933 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001934 return STACK_TV_VAR(idx);
1935 }
1936
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001937 // Go through argument names.
1938 ufunc = dfunc->df_ufunc;
1939 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1940 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1941 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1942 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1943 - varargs_off + idx);
1944 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1945 return STACK_TV(ectx->ec_frame_idx - 1);
1946
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001947 return NULL;
1948}
1949
Bram Moolenaar26a44842021-09-02 18:49:06 +02001950/*
1951 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1952 * breakpoint was set in that function or when there is any expression.
1953 */
1954 int
1955may_break_in_function(ufunc_T *ufunc)
1956{
1957 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1958}
1959
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001960 static void
1961handle_debug(isn_T *iptr, ectx_T *ectx)
1962{
1963 char_u *line;
1964 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1965 + ectx->ec_dfunc_idx)->df_ufunc;
1966 isn_T *ni;
1967 int end_lnum = iptr->isn_lnum;
1968 garray_T ga;
1969 int lnum;
1970
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001971 if (ex_nesting_level > debug_break_level)
1972 {
1973 linenr_T breakpoint;
1974
Bram Moolenaar26a44842021-09-02 18:49:06 +02001975 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001976 return;
1977
1978 // check for the next breakpoint if needed
1979 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001980 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001981 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1982 return;
1983 }
1984
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001985 SOURCING_LNUM = iptr->isn_lnum;
1986 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001987 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001988
1989 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1990 if (ni->isn_type == ISN_DEBUG
1991 || ni->isn_type == ISN_RETURN
1992 || ni->isn_type == ISN_RETURN_VOID)
1993 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001994 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001995 break;
1996 }
1997
1998 if (end_lnum > iptr->isn_lnum)
1999 {
2000 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002001 for (lnum = iptr->isn_lnum; lnum < end_lnum
2002 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002003 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002004 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002005
Bram Moolenaar303215d2021-07-07 20:10:43 +02002006 if (p == NULL)
2007 continue; // left over from continuation line
2008 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002009 if (*p == '#')
2010 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002011 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002012 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2013 if (STRNCMP(p, "def ", 4) == 0)
2014 break;
2015 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002016 line = ga_concat_strings(&ga, " ");
2017 vim_free(ga.ga_data);
2018 }
2019 else
2020 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002021
Dominique Pelle6e969552021-06-17 13:53:41 +02002022 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002023 debug_context = NULL;
2024
2025 if (end_lnum > iptr->isn_lnum)
2026 vim_free(line);
2027}
2028
Bram Moolenaar4c137212021-04-19 16:48:48 +02002029/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002030 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002031 * Returns OK, FAIL or NOTDONE (uncatchable error).
2032 */
2033 static int
2034execute_storeindex(isn_T *iptr, ectx_T *ectx)
2035{
2036 vartype_T dest_type = iptr->isn_arg.vartype;
2037 typval_T *tv;
2038 typval_T *tv_idx = STACK_TV_BOT(-2);
2039 typval_T *tv_dest = STACK_TV_BOT(-1);
2040 int status = OK;
2041
2042 // Stack contains:
2043 // -3 value to be stored
2044 // -2 index
2045 // -1 dict or list
2046 tv = STACK_TV_BOT(-3);
2047 SOURCING_LNUM = iptr->isn_lnum;
2048 if (dest_type == VAR_ANY)
2049 {
2050 dest_type = tv_dest->v_type;
2051 if (dest_type == VAR_DICT)
2052 status = do_2string(tv_idx, TRUE, FALSE);
2053 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2054 {
2055 emsg(_(e_number_expected));
2056 status = FAIL;
2057 }
2058 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002059
2060 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002061 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002062 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002063 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002064 long lidx = (long)tv_idx->vval.v_number;
2065 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002066
Bram Moolenaare08be092022-02-17 13:08:26 +00002067 if (list == NULL)
2068 {
2069 emsg(_(e_list_not_set));
2070 return FAIL;
2071 }
2072 if (lidx < 0 && list->lv_len + lidx >= 0)
2073 // negative index is relative to the end
2074 lidx = list->lv_len + lidx;
2075 if (lidx < 0 || lidx > list->lv_len)
2076 {
2077 semsg(_(e_list_index_out_of_range_nr), lidx);
2078 return FAIL;
2079 }
2080 if (lidx < list->lv_len)
2081 {
2082 listitem_T *li = list_find(list, lidx);
2083
2084 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002085 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002086 return FAIL;
2087 // overwrite existing list item
2088 clear_tv(&li->li_tv);
2089 li->li_tv = *tv;
2090 }
2091 else
2092 {
2093 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2094 return FAIL;
2095 // append to list, only fails when out of memory
2096 if (list_append_tv(list, tv) == FAIL)
2097 return NOTDONE;
2098 clear_tv(tv);
2099 }
2100 }
2101 else if (dest_type == VAR_DICT)
2102 {
2103 char_u *key = tv_idx->vval.v_string;
2104 dict_T *dict = tv_dest->vval.v_dict;
2105 dictitem_T *di;
2106
2107 SOURCING_LNUM = iptr->isn_lnum;
2108 if (dict == NULL)
2109 {
2110 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002111 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002112 }
2113 if (key == NULL)
2114 key = (char_u *)"";
2115 di = dict_find(dict, key, -1);
2116 if (di != NULL)
2117 {
2118 if (error_if_locked(di->di_tv.v_lock,
2119 e_cannot_change_dict_item))
2120 return FAIL;
2121 // overwrite existing value
2122 clear_tv(&di->di_tv);
2123 di->di_tv = *tv;
2124 }
2125 else
2126 {
2127 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2128 return FAIL;
2129 // add to dict, only fails when out of memory
2130 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2131 return NOTDONE;
2132 clear_tv(tv);
2133 }
2134 }
2135 else if (dest_type == VAR_BLOB)
2136 {
2137 long lidx = (long)tv_idx->vval.v_number;
2138 blob_T *blob = tv_dest->vval.v_blob;
2139 varnumber_T nr;
2140 int error = FALSE;
2141 int len;
2142
2143 if (blob == NULL)
2144 {
2145 emsg(_(e_blob_not_set));
2146 return FAIL;
2147 }
2148 len = blob_len(blob);
2149 if (lidx < 0 && len + lidx >= 0)
2150 // negative index is relative to the end
2151 lidx = len + lidx;
2152
2153 // Can add one byte at the end.
2154 if (lidx < 0 || lidx > len)
2155 {
2156 semsg(_(e_blob_index_out_of_range_nr), lidx);
2157 return FAIL;
2158 }
2159 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2160 return FAIL;
2161 nr = tv_get_number_chk(tv, &error);
2162 if (error)
2163 return FAIL;
2164 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002165 }
2166 else
2167 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002168 status = FAIL;
2169 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002170 }
2171 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002172
2173 clear_tv(tv_idx);
2174 clear_tv(tv_dest);
2175 ectx->ec_stack.ga_len -= 3;
2176 if (status == FAIL)
2177 {
2178 clear_tv(tv);
2179 return FAIL;
2180 }
2181 return OK;
2182}
2183
2184/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002185 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002186 */
2187 static int
2188execute_storerange(isn_T *iptr, ectx_T *ectx)
2189{
2190 typval_T *tv;
2191 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2192 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2193 typval_T *tv_dest = STACK_TV_BOT(-1);
2194 int status = OK;
2195
2196 // Stack contains:
2197 // -4 value to be stored
2198 // -3 first index or "none"
2199 // -2 second index or "none"
2200 // -1 destination list or blob
2201 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002202 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002203 if (tv_dest->v_type == VAR_LIST)
2204 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002205 long n1;
2206 long n2;
2207 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002208
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002209 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2210 if (tv_idx2->v_type == VAR_SPECIAL
2211 && tv_idx2->vval.v_number == VVAL_NONE)
2212 n2 = list_len(tv_dest->vval.v_list) - 1;
2213 else
2214 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2215
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002216 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002217 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002218 status = FAIL;
2219 else
2220 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002221 status = check_range_index_two(tv_dest->vval.v_list,
2222 &n1, li1, &n2, FALSE);
2223 if (status != FAIL)
2224 status = list_assign_range(
2225 tv_dest->vval.v_list,
2226 tv->vval.v_list,
2227 n1,
2228 n2,
2229 tv_idx2->v_type == VAR_SPECIAL,
2230 (char_u *)"=",
2231 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002232 }
2233 }
2234 else if (tv_dest->v_type == VAR_BLOB)
2235 {
2236 varnumber_T n1;
2237 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002238 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002239
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002240 n1 = tv_get_number_chk(tv_idx1, NULL);
2241 if (tv_idx2->v_type == VAR_SPECIAL
2242 && tv_idx2->vval.v_number == VVAL_NONE)
2243 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2244 else
2245 n2 = tv_get_number_chk(tv_idx2, NULL);
2246 bloblen = blob_len(tv_dest->vval.v_blob);
2247
2248 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2249 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002250 status = FAIL;
2251 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002252 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002253 }
2254 else
2255 {
2256 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002257 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002258 }
2259
2260 clear_tv(tv_idx1);
2261 clear_tv(tv_idx2);
2262 clear_tv(tv_dest);
2263 ectx->ec_stack.ga_len -= 4;
2264 clear_tv(tv);
2265
2266 return status;
2267}
2268
2269/*
2270 * Unlet item in list or dict variable.
2271 */
2272 static int
2273execute_unletindex(isn_T *iptr, ectx_T *ectx)
2274{
2275 typval_T *tv_idx = STACK_TV_BOT(-2);
2276 typval_T *tv_dest = STACK_TV_BOT(-1);
2277 int status = OK;
2278
2279 // Stack contains:
2280 // -2 index
2281 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002282 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002283 if (tv_dest->v_type == VAR_DICT)
2284 {
2285 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002286 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002287 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002288 semsg(_(e_expected_str_but_got_str),
2289 vartype_name(VAR_STRING),
2290 vartype_name(tv_idx->v_type));
2291 status = FAIL;
2292 }
2293 else
2294 {
2295 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002296 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002297 dictitem_T *di = NULL;
2298
2299 if (d != NULL && value_check_lock(
2300 d->dv_lock, NULL, FALSE))
2301 status = FAIL;
2302 else
2303 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002304 if (tv_idx->v_type == VAR_STRING)
2305 {
2306 key = tv_idx->vval.v_string;
2307 if (key == NULL)
2308 key = (char_u *)"";
2309 }
2310 else
2311 {
2312 key = tv_get_string(tv_idx);
2313 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002314 if (d != NULL)
2315 di = dict_find(d, key, (int)STRLEN(key));
2316 if (di == NULL)
2317 {
2318 // NULL dict is equivalent to empty dict
2319 semsg(_(e_key_not_present_in_dictionary),
2320 key);
2321 status = FAIL;
2322 }
2323 else if (var_check_fixed(di->di_flags,
2324 NULL, FALSE)
2325 || var_check_ro(di->di_flags,
2326 NULL, FALSE))
2327 status = FAIL;
2328 else
2329 dictitem_remove(d, di);
2330 }
2331 }
2332 }
2333 else if (tv_dest->v_type == VAR_LIST)
2334 {
2335 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002336 if (check_for_number(tv_idx) == FAIL)
2337 {
2338 status = FAIL;
2339 }
2340 else
2341 {
2342 list_T *l = tv_dest->vval.v_list;
2343 long n = (long)tv_idx->vval.v_number;
2344
2345 if (l != NULL && value_check_lock(
2346 l->lv_lock, NULL, FALSE))
2347 status = FAIL;
2348 else
2349 {
2350 listitem_T *li = list_find(l, n);
2351
2352 if (li == NULL)
2353 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002354 semsg(_(e_list_index_out_of_range_nr), n);
2355 status = FAIL;
2356 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002357 else
2358 listitem_remove(l, li);
2359 }
2360 }
2361 }
2362 else
2363 {
2364 status = FAIL;
2365 semsg(_(e_cannot_index_str),
2366 vartype_name(tv_dest->v_type));
2367 }
2368
2369 clear_tv(tv_idx);
2370 clear_tv(tv_dest);
2371 ectx->ec_stack.ga_len -= 2;
2372
2373 return status;
2374}
2375
2376/*
2377 * Unlet a range of items in a list variable.
2378 */
2379 static int
2380execute_unletrange(isn_T *iptr, ectx_T *ectx)
2381{
2382 // Stack contains:
2383 // -3 index1
2384 // -2 index2
2385 // -1 dict or list
2386 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2387 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2388 typval_T *tv_dest = STACK_TV_BOT(-1);
2389 int status = OK;
2390
2391 if (tv_dest->v_type == VAR_LIST)
2392 {
2393 // indexes must be a number
2394 SOURCING_LNUM = iptr->isn_lnum;
2395 if (check_for_number(tv_idx1) == FAIL
2396 || (tv_idx2->v_type != VAR_SPECIAL
2397 && check_for_number(tv_idx2) == FAIL))
2398 {
2399 status = FAIL;
2400 }
2401 else
2402 {
2403 list_T *l = tv_dest->vval.v_list;
2404 long n1 = (long)tv_idx1->vval.v_number;
2405 long n2 = tv_idx2->v_type == VAR_SPECIAL
2406 ? 0 : (long)tv_idx2->vval.v_number;
2407 listitem_T *li;
2408
2409 li = list_find_index(l, &n1);
2410 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002411 {
2412 semsg(_(e_list_index_out_of_range_nr),
2413 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002414 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002415 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002416 else
2417 {
2418 if (n1 < 0)
2419 n1 = list_idx_of_item(l, li);
2420 if (n2 < 0)
2421 {
2422 listitem_T *li2 = list_find(l, n2);
2423
2424 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002425 {
2426 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002427 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002428 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002429 else
2430 n2 = list_idx_of_item(l, li2);
2431 }
2432 if (status != FAIL
2433 && tv_idx2->v_type != VAR_SPECIAL
2434 && n2 < n1)
2435 {
2436 semsg(_(e_list_index_out_of_range_nr), n2);
2437 status = FAIL;
2438 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002439 if (status != FAIL)
2440 list_unlet_range(l, li, n1,
2441 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002442 }
2443 }
2444 }
2445 else
2446 {
2447 status = FAIL;
2448 SOURCING_LNUM = iptr->isn_lnum;
2449 semsg(_(e_cannot_index_str),
2450 vartype_name(tv_dest->v_type));
2451 }
2452
2453 clear_tv(tv_idx1);
2454 clear_tv(tv_idx2);
2455 clear_tv(tv_dest);
2456 ectx->ec_stack.ga_len -= 3;
2457
2458 return status;
2459}
2460
2461/*
2462 * Top of a for loop.
2463 */
2464 static int
2465execute_for(isn_T *iptr, ectx_T *ectx)
2466{
2467 typval_T *tv;
2468 typval_T *ltv = STACK_TV_BOT(-1);
2469 typval_T *idxtv =
2470 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2471
2472 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2473 return FAIL;
2474 if (ltv->v_type == VAR_LIST)
2475 {
2476 list_T *list = ltv->vval.v_list;
2477
2478 // push the next item from the list
2479 ++idxtv->vval.v_number;
2480 if (list == NULL
2481 || idxtv->vval.v_number >= list->lv_len)
2482 {
2483 // past the end of the list, jump to "endfor"
2484 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2485 may_restore_cmdmod(&ectx->ec_funclocal);
2486 }
2487 else if (list->lv_first == &range_list_item)
2488 {
2489 // non-materialized range() list
2490 tv = STACK_TV_BOT(0);
2491 tv->v_type = VAR_NUMBER;
2492 tv->v_lock = 0;
2493 tv->vval.v_number = list_find_nr(
2494 list, idxtv->vval.v_number, NULL);
2495 ++ectx->ec_stack.ga_len;
2496 }
2497 else
2498 {
2499 listitem_T *li = list_find(list,
2500 idxtv->vval.v_number);
2501
2502 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2503 ++ectx->ec_stack.ga_len;
2504 }
2505 }
2506 else if (ltv->v_type == VAR_STRING)
2507 {
2508 char_u *str = ltv->vval.v_string;
2509
2510 // The index is for the last byte of the previous
2511 // character.
2512 ++idxtv->vval.v_number;
2513 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2514 {
2515 // past the end of the string, jump to "endfor"
2516 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2517 may_restore_cmdmod(&ectx->ec_funclocal);
2518 }
2519 else
2520 {
2521 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2522
2523 // Push the next character from the string.
2524 tv = STACK_TV_BOT(0);
2525 tv->v_type = VAR_STRING;
2526 tv->vval.v_string = vim_strnsave(
2527 str + idxtv->vval.v_number, clen);
2528 ++ectx->ec_stack.ga_len;
2529 idxtv->vval.v_number += clen - 1;
2530 }
2531 }
2532 else if (ltv->v_type == VAR_BLOB)
2533 {
2534 blob_T *blob = ltv->vval.v_blob;
2535
2536 // When we get here the first time make a copy of the
2537 // blob, so that the iteration still works when it is
2538 // changed.
2539 if (idxtv->vval.v_number == -1 && blob != NULL)
2540 {
2541 blob_copy(blob, ltv);
2542 blob_unref(blob);
2543 blob = ltv->vval.v_blob;
2544 }
2545
2546 // The index is for the previous byte.
2547 ++idxtv->vval.v_number;
2548 if (blob == NULL
2549 || idxtv->vval.v_number >= blob_len(blob))
2550 {
2551 // past the end of the blob, jump to "endfor"
2552 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2553 may_restore_cmdmod(&ectx->ec_funclocal);
2554 }
2555 else
2556 {
2557 // Push the next byte from the blob.
2558 tv = STACK_TV_BOT(0);
2559 tv->v_type = VAR_NUMBER;
2560 tv->vval.v_number = blob_get(blob,
2561 idxtv->vval.v_number);
2562 ++ectx->ec_stack.ga_len;
2563 }
2564 }
2565 else
2566 {
2567 semsg(_(e_for_loop_on_str_not_supported),
2568 vartype_name(ltv->v_type));
2569 return FAIL;
2570 }
2571 return OK;
2572}
2573
2574/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002575 * Load instruction for w:/b:/g:/t: variable.
2576 * "isn_type" is used instead of "iptr->isn_type".
2577 */
2578 static int
2579load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2580{
2581 dictitem_T *di = NULL;
2582 hashtab_T *ht = NULL;
2583 char namespace;
2584
2585 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2586 return NOTDONE;
2587 switch (isn_type)
2588 {
2589 case ISN_LOADG:
2590 ht = get_globvar_ht();
2591 namespace = 'g';
2592 break;
2593 case ISN_LOADB:
2594 ht = &curbuf->b_vars->dv_hashtab;
2595 namespace = 'b';
2596 break;
2597 case ISN_LOADW:
2598 ht = &curwin->w_vars->dv_hashtab;
2599 namespace = 'w';
2600 break;
2601 case ISN_LOADT:
2602 ht = &curtab->tp_vars->dv_hashtab;
2603 namespace = 't';
2604 break;
2605 default: // Cannot reach here
2606 return NOTDONE;
2607 }
2608 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2609
Bram Moolenaar06b77222022-01-25 15:51:56 +00002610 if (di == NULL)
2611 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002612 if (isn_type == ISN_LOADG)
2613 {
2614 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2615
2616 // g:Something could be a function
2617 if (ufunc != NULL)
2618 {
2619 typval_T *tv = STACK_TV_BOT(0);
2620
2621 ++ectx->ec_stack.ga_len;
2622 tv->v_type = VAR_FUNC;
2623 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2624 if (tv->vval.v_string == NULL)
2625 return FAIL;
2626 STRCPY(tv->vval.v_string, "g:");
2627 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2628 return OK;
2629 }
2630 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002631 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002632 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002633 // no check if the item exists in the script but
2634 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002635 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002636 else
2637 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002638 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002639 return FAIL;
2640 }
2641 else
2642 {
2643 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2644 ++ectx->ec_stack.ga_len;
2645 }
2646 return OK;
2647}
2648
2649/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002650 * Execute instructions in execution context "ectx".
2651 * Return OK or FAIL;
2652 */
2653 static int
2654exec_instructions(ectx_T *ectx)
2655{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002656 int ret = FAIL;
2657 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002658 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002659
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002660 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002661 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002662
Bram Moolenaarff652882021-05-16 15:24:49 +02002663 // Only catch exceptions in this instruction list.
2664 ectx->ec_trylevel_at_start = trylevel;
2665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 for (;;)
2667 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002668 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002670 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002671
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002672 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002673 {
2674 line_breakcheck();
2675 breakcheck_count = 0;
2676 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002677 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002678 {
2679 // Turn CTRL-C into an exception.
2680 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002681 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002682 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002683 did_throw = TRUE;
2684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002686 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002687 {
2688 // Turn an error message into an exception.
2689 did_emsg = FALSE;
2690 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002691 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002692 did_throw = TRUE;
2693 *msg_list = NULL;
2694 }
2695
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002696 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002698 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002699 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002700 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701
2702 // An exception jumps to the first catch, finally, or returns from
2703 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002704 while (index > 0)
2705 {
2706 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002707 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002708 break;
2709 // In the catch and finally block of this try we have to go up
2710 // one level.
2711 --index;
2712 trycmd = NULL;
2713 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002714 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002716 if (trycmd->tcd_in_catch)
2717 {
2718 // exception inside ":catch", jump to ":finally" once
2719 ectx->ec_iidx = trycmd->tcd_finally_idx;
2720 trycmd->tcd_finally_idx = 0;
2721 }
2722 else
2723 // jump to first ":catch"
2724 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002725 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002726 did_throw = FALSE; // don't come back here until :endtry
2727 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 }
2729 else
2730 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002731 // Not inside try or need to return from current functions.
2732 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002733 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002734 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002735 tv = STACK_TV_BOT(0);
2736 tv->v_type = VAR_NUMBER;
2737 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002738 ++ectx->ec_stack.ga_len;
2739 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002741 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002742 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002743 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002744 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 goto done;
2746 }
2747
Bram Moolenaar4c137212021-04-19 16:48:48 +02002748 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002749 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 }
2751 continue;
2752 }
2753
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002754 /*
2755 * Big switch on the instruction. Most compilers will be turning this
2756 * into an efficient lookup table, since the "case" values are an enum
2757 * with sequential numbers. It may look ugly, but it should be the
2758 * most efficient way.
2759 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002760 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 switch (iptr->isn_type)
2762 {
2763 // execute Ex command line
2764 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002765 if (exec_command(iptr) == FAIL)
2766 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 break;
2768
Bram Moolenaar20677332021-06-06 17:02:53 +02002769 // execute Ex command line split at NL characters.
2770 case ISN_EXEC_SPLIT:
2771 {
2772 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002773 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002774
2775 SOURCING_LNUM = iptr->isn_lnum;
2776 CLEAR_FIELD(cookie);
2777 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2778 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002779 line = get_split_sourceline(0, &cookie, 0, 0);
2780 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002781 get_split_sourceline, &cookie,
2782 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2783 == FAIL
2784 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002785 {
2786 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002787 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002788 }
2789 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002790 }
2791 break;
2792
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002793 // execute Ex command line that is only a range
2794 case ISN_EXECRANGE:
2795 {
2796 exarg_T ea;
2797 char *error = NULL;
2798
2799 CLEAR_FIELD(ea);
2800 ea.cmdidx = CMD_SIZE;
2801 ea.addr_type = ADDR_LINES;
2802 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002803 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002804 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002805 if (ea.cmd == NULL)
2806 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002807 // error is always NULL when using ADDR_LINES
2808 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002809 if (error != NULL)
2810 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002811 emsg(error);
2812 goto on_error;
2813 }
2814 }
2815 break;
2816
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002817 // Evaluate an expression with legacy syntax, push it onto the
2818 // stack.
2819 case ISN_LEGACY_EVAL:
2820 {
2821 char_u *arg = iptr->isn_arg.string;
2822 int res;
2823 int save_flags = cmdmod.cmod_flags;
2824
Bram Moolenaar35578162021-08-02 19:10:38 +02002825 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002826 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002827 tv = STACK_TV_BOT(0);
2828 init_tv(tv);
2829 cmdmod.cmod_flags |= CMOD_LEGACY;
2830 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2831 cmdmod.cmod_flags = save_flags;
2832 if (res == FAIL)
2833 goto on_error;
2834 ++ectx->ec_stack.ga_len;
2835 }
2836 break;
2837
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002838 // push typeval VAR_INSTR with instructions to be executed
2839 case ISN_INSTR:
2840 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002841 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002842 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002843 tv = STACK_TV_BOT(0);
2844 tv->vval.v_instr = ALLOC_ONE(instr_T);
2845 if (tv->vval.v_instr == NULL)
2846 goto on_error;
2847 ++ectx->ec_stack.ga_len;
2848
2849 tv->v_type = VAR_INSTR;
2850 tv->vval.v_instr->instr_ectx = ectx;
2851 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2852 }
2853 break;
2854
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002855 case ISN_SOURCE:
2856 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002857 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002858
Bram Moolenaar89445512022-04-14 12:58:23 +01002859 SOURCING_LNUM = iptr->isn_lnum;
2860 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002861 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002862 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002863 }
2864 break;
2865
Bram Moolenaar4c137212021-04-19 16:48:48 +02002866 // execute :substitute with an expression
2867 case ISN_SUBSTITUTE:
2868 {
2869 subs_T *subs = &iptr->isn_arg.subs;
2870 source_cookie_T cookie;
2871 struct subs_expr_S *save_instr = substitute_instr;
2872 struct subs_expr_S subs_instr;
2873 int res;
2874
2875 subs_instr.subs_ectx = ectx;
2876 subs_instr.subs_instr = subs->subs_instr;
2877 subs_instr.subs_status = OK;
2878 substitute_instr = &subs_instr;
2879
2880 SOURCING_LNUM = iptr->isn_lnum;
2881 // This is very much like ISN_EXEC
2882 CLEAR_FIELD(cookie);
2883 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2884 res = do_cmdline(subs->subs_cmd,
2885 getsourceline, &cookie,
2886 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2887 substitute_instr = save_instr;
2888
2889 if (res == FAIL || did_emsg
2890 || subs_instr.subs_status == FAIL)
2891 goto on_error;
2892 }
2893 break;
2894
2895 case ISN_FINISH:
2896 goto done;
2897
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002898 case ISN_REDIRSTART:
2899 // create a dummy entry for var_redir_str()
2900 if (alloc_redir_lval() == FAIL)
2901 goto on_error;
2902
2903 // The output is stored in growarray "redir_ga" until
2904 // redirection ends.
2905 init_redir_ga();
2906 redir_vname = 1;
2907 break;
2908
2909 case ISN_REDIREND:
2910 {
2911 char_u *res = get_clear_redir_ga();
2912
2913 // End redirection, put redirected text on the stack.
2914 clear_redir_lval();
2915 redir_vname = 0;
2916
Bram Moolenaar35578162021-08-02 19:10:38 +02002917 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002918 {
2919 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002920 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002921 }
2922 tv = STACK_TV_BOT(0);
2923 tv->v_type = VAR_STRING;
2924 tv->vval.v_string = res;
2925 ++ectx->ec_stack.ga_len;
2926 }
2927 break;
2928
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002929 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002930#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002931 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002932 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2933 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002934 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002935#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002936 break;
2937
2938 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002939#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002940 {
2941 exarg_T ea;
2942 int res;
2943
2944 CLEAR_FIELD(ea);
2945 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2946 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2947 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2948 --ectx->ec_stack.ga_len;
2949 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01002950 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002951 res = cexpr_core(&ea, tv);
2952 clear_tv(tv);
2953 if (res == FAIL)
2954 goto on_error;
2955 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002956#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002957 break;
2958
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002959 // execute Ex command from pieces on the stack
2960 case ISN_EXECCONCAT:
2961 {
2962 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002963 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002964 int pass;
2965 int i;
2966 char_u *cmd = NULL;
2967 char_u *str;
2968
2969 for (pass = 1; pass <= 2; ++pass)
2970 {
2971 for (i = 0; i < count; ++i)
2972 {
2973 tv = STACK_TV_BOT(i - count);
2974 str = tv->vval.v_string;
2975 if (str != NULL && *str != NUL)
2976 {
2977 if (pass == 2)
2978 STRCPY(cmd + len, str);
2979 len += STRLEN(str);
2980 }
2981 if (pass == 2)
2982 clear_tv(tv);
2983 }
2984 if (pass == 1)
2985 {
2986 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002987 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002988 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002989 len = 0;
2990 }
2991 }
2992
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002993 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002994 do_cmdline_cmd(cmd);
2995 vim_free(cmd);
2996 }
2997 break;
2998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 // execute :echo {string} ...
3000 case ISN_ECHO:
3001 {
3002 int count = iptr->isn_arg.echo.echo_count;
3003 int atstart = TRUE;
3004 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003005 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006
3007 for (idx = 0; idx < count; ++idx)
3008 {
3009 tv = STACK_TV_BOT(idx - count);
3010 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3011 &atstart, &needclr);
3012 clear_tv(tv);
3013 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003014 if (needclr)
3015 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003016 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 }
3018 break;
3019
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003020 // :execute {string} ...
3021 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003022 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003023 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003024 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003025 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003026 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003027 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003028 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003029 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003030 {
3031 int count = iptr->isn_arg.number;
3032 garray_T ga;
3033 char_u buf[NUMBUFLEN];
3034 char_u *p;
3035 int len;
3036 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003037 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003038
3039 ga_init2(&ga, 1, 80);
3040 for (idx = 0; idx < count; ++idx)
3041 {
3042 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003043 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003044 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003045 if (tv->v_type == VAR_CHANNEL
3046 || tv->v_type == VAR_JOB)
3047 {
3048 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003049 semsg(_(e_using_invalid_value_as_string_str),
3050 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003051 break;
3052 }
3053 else
3054 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003055 }
3056 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003057 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003058
3059 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003060 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003061 failed = TRUE;
3062 else
3063 {
3064 if (ga.ga_len > 0)
3065 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3066 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3067 ga.ga_len += len;
3068 }
3069 clear_tv(tv);
3070 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003071 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003072 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003073 {
3074 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003075 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003076 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003077
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003078 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003079 {
3080 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003081 {
3082 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003083 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003084 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003085 {
3086 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003087 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003088 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003089 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003090 else
3091 {
3092 msg_sb_eol();
3093 if (iptr->isn_type == ISN_ECHOMSG)
3094 {
3095 msg_attr(ga.ga_data, echo_attr);
3096 out_flush();
3097 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003098#ifdef HAS_MESSAGE_WINDOW
3099 else if (iptr->isn_type == ISN_ECHOWINDOW)
3100 {
3101 start_echowindow();
3102 msg_attr(ga.ga_data, echo_attr);
3103 end_echowindow();
3104 }
3105#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003106 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3107 {
3108 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3109 TRUE);
3110 ui_write((char_u *)"\r\n", 2, TRUE);
3111 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003112 else
3113 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003114 SOURCING_LNUM = iptr->isn_lnum;
3115 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003116 }
3117 }
3118 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003119 ga_clear(&ga);
3120 }
3121 break;
3122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 // load local variable or argument
3124 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003125 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003126 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003128 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 break;
3130
3131 // load v: variable
3132 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003133 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003134 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003136 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 break;
3138
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003139 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 case ISN_LOADSCRIPT:
3141 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003142 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 svar_T *sv;
3144
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003145 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003146 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003147 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003148 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003149 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003150 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003152 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 }
3154 break;
3155
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003156 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003158 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003160 int sid = iptr->isn_arg.loadstore.ls_sid;
3161 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003162 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003164
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 if (di == NULL)
3166 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003167 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003168 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003169 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 }
3171 else
3172 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003173 if (iptr->isn_type == ISN_LOADEXPORT)
3174 {
3175 int idx = get_script_item_idx(sid, name, 0,
3176 NULL, NULL);
3177 svar_T *sv;
3178
3179 if (idx >= 0)
3180 {
3181 sv = ((svar_T *)SCRIPT_ITEM(sid)
3182 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003183 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003184 {
3185 SOURCING_LNUM = iptr->isn_lnum;
3186 semsg(_(e_item_not_exported_in_script_str),
3187 name);
3188 goto on_error;
3189 }
3190 }
3191 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003192 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003193 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003195 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 }
3197 }
3198 break;
3199
Bram Moolenaard3aac292020-04-19 14:32:17 +02003200 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003202 case ISN_LOADB:
3203 case ISN_LOADW:
3204 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003206 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003207
Bram Moolenaar06b77222022-01-25 15:51:56 +00003208 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003209 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003210 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003211 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 break;
3215
Bram Moolenaar03290b82020-12-19 16:30:44 +01003216 // load autoload variable
3217 case ISN_LOADAUTO:
3218 {
3219 char_u *name = iptr->isn_arg.string;
3220
Bram Moolenaar35578162021-08-02 19:10:38 +02003221 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003222 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003223 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003224 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003225 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003226 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003227 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003228 }
3229 break;
3230
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003231 // load g:/b:/w:/t: namespace
3232 case ISN_LOADGDICT:
3233 case ISN_LOADBDICT:
3234 case ISN_LOADWDICT:
3235 case ISN_LOADTDICT:
3236 {
3237 dict_T *d = NULL;
3238
3239 switch (iptr->isn_type)
3240 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003241 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3242 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3243 case ISN_LOADWDICT: d = curwin->w_vars; break;
3244 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003245 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003246 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003247 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003248 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003249 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003250 tv = STACK_TV_BOT(0);
3251 tv->v_type = VAR_DICT;
3252 tv->v_lock = 0;
3253 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003254 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003255 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003256 }
3257 break;
3258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 // load &option
3260 case ISN_LOADOPT:
3261 {
3262 typval_T optval;
3263 char_u *name = iptr->isn_arg.string;
3264
Bram Moolenaara8c17702020-04-01 21:17:24 +02003265 // This is not expected to fail, name is checked during
3266 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003267 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003268 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003269 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003270 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003272 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 }
3274 break;
3275
3276 // load $ENV
3277 case ISN_LOADENV:
3278 {
3279 typval_T optval;
3280 char_u *name = iptr->isn_arg.string;
3281
Bram Moolenaar35578162021-08-02 19:10:38 +02003282 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003283 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003284 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003285 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003287 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 }
3289 break;
3290
3291 // load @register
3292 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003293 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003294 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 tv = STACK_TV_BOT(0);
3296 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003297 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003298 // This may result in NULL, which should be equivalent to an
3299 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 tv->vval.v_string = get_reg_contents(
3301 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003302 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 break;
3304
3305 // store local variable
3306 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003307 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 tv = STACK_TV_VAR(iptr->isn_arg.number);
3309 clear_tv(tv);
3310 *tv = *STACK_TV_BOT(0);
3311 break;
3312
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003313 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003314 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003315 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003316 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003317 int sid = iptr->isn_arg.loadstore.ls_sid;
3318 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003319 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003320 dictitem_T *di = find_var_in_ht(ht, 0,
3321 iptr->isn_type == ISN_STORES
3322 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003323
Bram Moolenaar4c137212021-04-19 16:48:48 +02003324 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003325 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003326 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003327 {
3328 if (iptr->isn_type == ISN_STOREEXPORT)
3329 {
3330 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003331 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003332 goto on_error;
3333 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003334 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003335 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003336 else
3337 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003338 if (iptr->isn_type == ISN_STOREEXPORT)
3339 {
3340 int idx = get_script_item_idx(sid, name, 0,
3341 NULL, NULL);
3342
Bram Moolenaar06651632022-04-27 17:54:25 +01003343 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003344 if (idx >= 0)
3345 {
3346 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3347 ->sn_var_vals.ga_data) + idx;
3348
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003349 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003350 {
3351 semsg(_(e_item_not_exported_in_script_str),
3352 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003353 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003354 goto on_error;
3355 }
3356 }
3357 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003358 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003359 {
3360 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003361 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003362 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003363 clear_tv(&di->di_tv);
3364 di->di_tv = *STACK_TV_BOT(0);
3365 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003366 }
3367 break;
3368
3369 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 case ISN_STORESCRIPT:
3371 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003372 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3373 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003375 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003376 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003377 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003378 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003379
3380 // "const" and "final" are checked at compile time, locking
3381 // the value needs to be checked here.
3382 SOURCING_LNUM = iptr->isn_lnum;
3383 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003384 {
3385 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003386 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003387 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 clear_tv(sv->sv_tv);
3390 *sv->sv_tv = *STACK_TV_BOT(0);
3391 }
3392 break;
3393
3394 // store option
3395 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003396 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003398 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3399 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 long n = 0;
3401 char_u *s = NULL;
3402 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003403 char_u numbuf[NUMBUFLEN];
3404 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405
Bram Moolenaar4c137212021-04-19 16:48:48 +02003406 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 tv = STACK_TV_BOT(0);
3408 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003409 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003411 if (s == NULL)
3412 s = (char_u *)"";
3413 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003414 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3415 {
3416 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003417 // If the option can be set to a function reference or
3418 // a lambda and the passed value is a function
3419 // reference, then convert it to the name (string) of
3420 // the function reference.
3421 s = tv2string(tv, &tofree, numbuf, 0);
3422 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003423 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003424 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003425 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003426 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003427 goto on_error;
3428 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003429 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003431 // must be VAR_NUMBER, CHECKTYPE makes sure
3432 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003433 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003434 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003435 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 if (msg != NULL)
3437 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003438 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003440 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 }
3443 break;
3444
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003445 // store $ENV
3446 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003447 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003448 tv = STACK_TV_BOT(0);
3449 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3450 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003451 break;
3452
3453 // store @r
3454 case ISN_STOREREG:
3455 {
3456 int reg = iptr->isn_arg.number;
3457
Bram Moolenaar4c137212021-04-19 16:48:48 +02003458 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003459 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003460 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003461 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003462 }
3463 break;
3464
3465 // store v: variable
3466 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003467 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003468 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3469 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003470 // should not happen, type is checked when compiling
3471 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003472 break;
3473
Bram Moolenaard3aac292020-04-19 14:32:17 +02003474 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003476 case ISN_STOREB:
3477 case ISN_STOREW:
3478 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003480 dictitem_T *di;
3481 hashtab_T *ht;
3482 char_u *name = iptr->isn_arg.string + 2;
3483
Bram Moolenaard3aac292020-04-19 14:32:17 +02003484 switch (iptr->isn_type)
3485 {
3486 case ISN_STOREG:
3487 ht = get_globvar_ht();
3488 break;
3489 case ISN_STOREB:
3490 ht = &curbuf->b_vars->dv_hashtab;
3491 break;
3492 case ISN_STOREW:
3493 ht = &curwin->w_vars->dv_hashtab;
3494 break;
3495 case ISN_STORET:
3496 ht = &curtab->tp_vars->dv_hashtab;
3497 break;
3498 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003499 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501
Bram Moolenaar4c137212021-04-19 16:48:48 +02003502 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003503 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003505 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 else
3507 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003508 SOURCING_LNUM = iptr->isn_lnum;
3509 if (var_check_permission(di, name) == FAIL)
3510 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 clear_tv(&di->di_tv);
3512 di->di_tv = *STACK_TV_BOT(0);
3513 }
3514 }
3515 break;
3516
Bram Moolenaar03290b82020-12-19 16:30:44 +01003517 // store an autoload variable
3518 case ISN_STOREAUTO:
3519 SOURCING_LNUM = iptr->isn_lnum;
3520 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3521 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003522 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003523 break;
3524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 // store number in local variable
3526 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003527 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 clear_tv(tv);
3529 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003530 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 break;
3532
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003533 // store value in list or dict variable
3534 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003535 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003536 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003537
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003538 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003539 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003540 if (res == NOTDONE)
3541 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003542 }
3543 break;
3544
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003545 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003546 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003547 if (execute_storerange(iptr, ectx) == FAIL)
3548 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003549 break;
3550
Bram Moolenaar0186e582021-01-10 18:33:11 +01003551 // load or store variable or argument from outer scope
3552 case ISN_LOADOUTER:
3553 case ISN_STOREOUTER:
3554 {
3555 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003556 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3557 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003558
3559 while (depth > 1 && outer != NULL)
3560 {
3561 outer = outer->out_up;
3562 --depth;
3563 }
3564 if (outer == NULL)
3565 {
3566 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003567 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3568 || ectx->ec_outer_ref == NULL)
3569 // Possibly :def function called from legacy
3570 // context.
3571 emsg(_(e_closure_called_from_invalid_context));
3572 else
3573 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003574 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003575 }
3576 tv = ((typval_T *)outer->out_stack->ga_data)
3577 + outer->out_frame_idx + STACK_FRAME_SIZE
3578 + iptr->isn_arg.outer.outer_idx;
3579 if (iptr->isn_type == ISN_LOADOUTER)
3580 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003581 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003582 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003583 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003584 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003585 }
3586 else
3587 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003588 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003589 clear_tv(tv);
3590 *tv = *STACK_TV_BOT(0);
3591 }
3592 }
3593 break;
3594
Bram Moolenaar752fc692021-01-04 21:57:11 +01003595 // unlet item in list or dict variable
3596 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003597 if (execute_unletindex(iptr, ectx) == FAIL)
3598 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003599 break;
3600
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003601 // unlet range of items in list variable
3602 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003603 if (execute_unletrange(iptr, ectx) == FAIL)
3604 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003605 break;
3606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 // push constant
3608 case ISN_PUSHNR:
3609 case ISN_PUSHBOOL:
3610 case ISN_PUSHSPEC:
3611 case ISN_PUSHF:
3612 case ISN_PUSHS:
3613 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003614 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003615 case ISN_PUSHCHANNEL:
3616 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003617 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003618 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003620 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003621 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 switch (iptr->isn_type)
3623 {
3624 case ISN_PUSHNR:
3625 tv->v_type = VAR_NUMBER;
3626 tv->vval.v_number = iptr->isn_arg.number;
3627 break;
3628 case ISN_PUSHBOOL:
3629 tv->v_type = VAR_BOOL;
3630 tv->vval.v_number = iptr->isn_arg.number;
3631 break;
3632 case ISN_PUSHSPEC:
3633 tv->v_type = VAR_SPECIAL;
3634 tv->vval.v_number = iptr->isn_arg.number;
3635 break;
3636#ifdef FEAT_FLOAT
3637 case ISN_PUSHF:
3638 tv->v_type = VAR_FLOAT;
3639 tv->vval.v_float = iptr->isn_arg.fnumber;
3640 break;
3641#endif
3642 case ISN_PUSHBLOB:
3643 blob_copy(iptr->isn_arg.blob, tv);
3644 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003645 case ISN_PUSHFUNC:
3646 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003647 if (iptr->isn_arg.string == NULL)
3648 tv->vval.v_string = NULL;
3649 else
3650 tv->vval.v_string =
3651 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003652 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003653 case ISN_PUSHCHANNEL:
3654#ifdef FEAT_JOB_CHANNEL
3655 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003656 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003657#endif
3658 break;
3659 case ISN_PUSHJOB:
3660#ifdef FEAT_JOB_CHANNEL
3661 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003662 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003663#endif
3664 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 default:
3666 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003667 tv->vval.v_string = iptr->isn_arg.string == NULL
3668 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 }
3670 break;
3671
Bram Moolenaar06b77222022-01-25 15:51:56 +00003672 case ISN_AUTOLOAD:
3673 {
3674 char_u *name = iptr->isn_arg.string;
3675
3676 (void)script_autoload(name, FALSE);
3677 if (find_func(name, TRUE))
3678 {
3679 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3680 goto theend;
3681 tv = STACK_TV_BOT(0);
3682 tv->v_lock = 0;
3683 ++ectx->ec_stack.ga_len;
3684 tv->v_type = VAR_FUNC;
3685 tv->vval.v_string = vim_strsave(name);
3686 }
3687 else
3688 {
3689 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3690
3691 if (res == NOTDONE)
3692 goto theend;
3693 if (res == FAIL)
3694 goto on_error;
3695 }
3696 }
3697 break;
3698
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003699 case ISN_UNLET:
3700 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3701 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003702 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003703 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003704 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003705 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003706 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003707
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003708 case ISN_LOCKUNLOCK:
3709 {
3710 typval_T *lval_root_save = lval_root;
3711 int res;
3712
3713 // Stack has the local variable, argument the whole :lock
3714 // or :unlock command, like ISN_EXEC.
3715 --ectx->ec_stack.ga_len;
3716 lval_root = STACK_TV_BOT(0);
3717 res = exec_command(iptr);
3718 clear_tv(lval_root);
3719 lval_root = lval_root_save;
3720 if (res == FAIL)
3721 goto on_error;
3722 }
3723 break;
3724
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003725 case ISN_LOCKCONST:
3726 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3727 break;
3728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 // create a list from items on the stack; uses a single allocation
3730 // for the list header and the items
3731 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003732 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003733 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 break;
3735
3736 // create a dict from items on the stack
3737 case ISN_NEWDICT:
3738 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003739 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003741 SOURCING_LNUM = iptr->isn_lnum;
3742 res = exe_newdict(iptr->isn_arg.number, ectx);
3743 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003744 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003745 if (res == MAYBE)
3746 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 }
3748 break;
3749
LemonBoy372bcce2022-04-25 12:43:20 +01003750 case ISN_CONCAT:
3751 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3752 goto theend;
3753 break;
3754
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003755 // create a partial with NULL value
3756 case ISN_NEWPARTIAL:
3757 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3758 goto theend;
3759 ++ectx->ec_stack.ga_len;
3760 tv = STACK_TV_BOT(-1);
3761 tv->v_type = VAR_PARTIAL;
3762 tv->v_lock = 0;
3763 tv->vval.v_partial = NULL;
3764 break;
3765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 // call a :def function
3767 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003768 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003769 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3770 NULL,
3771 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003772 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003773 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 break;
3775
3776 // call a builtin function
3777 case ISN_BCALL:
3778 SOURCING_LNUM = iptr->isn_lnum;
3779 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3780 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003781 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003782 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 break;
3784
3785 // call a funcref or partial
3786 case ISN_PCALL:
3787 {
3788 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3789 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003790 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791
3792 SOURCING_LNUM = iptr->isn_lnum;
3793 if (pfunc->cpf_top)
3794 {
3795 // funcref is above the arguments
3796 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3797 }
3798 else
3799 {
3800 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003801 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003802 partial_tv = *STACK_TV_BOT(0);
3803 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003805 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003806 if (tv == &partial_tv)
3807 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003809 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 }
3811 break;
3812
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003813 case ISN_PCALL_END:
3814 // PCALL finished, arguments have been consumed and replaced by
3815 // the return value. Now clear the funcref from the stack,
3816 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003817 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003818 clear_tv(STACK_TV_BOT(-1));
3819 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3820 break;
3821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 // call a user defined function or funcref/partial
3823 case ISN_UCALL:
3824 {
3825 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3826
3827 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003828 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003829 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003830 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 }
3832 break;
3833
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003834 // :defer func(arg)
3835 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01003836 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003837 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
3838 goto on_error;
3839 break;
3840
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003841 // return from a :def function call without a value
3842 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003843 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003844 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003845 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003846 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003847 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003848 tv->vval.v_number = 0;
3849 tv->v_lock = 0;
3850 // FALLTHROUGH
3851
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003852 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 case ISN_RETURN:
3854 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003855 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003856 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003857
3858 if (trystack->ga_len > 0)
3859 trycmd = ((trycmd_T *)trystack->ga_data)
3860 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003861 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003862 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003864 // jump to ":finally" or ":endtry"
3865 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003866 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003867 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003868 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 trycmd->tcd_return = TRUE;
3870 }
3871 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003872 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 }
3874 break;
3875
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003876 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 case ISN_FUNCREF:
3878 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003879 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003880 ufunc_T *ufunc;
3881 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003884 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003885 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003886 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003887 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003888 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003889 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003890 if (funcref->fr_func_name == NULL)
3891 {
3892 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3893 + funcref->fr_dfunc_idx;
3894
3895 ufunc = pt_dfunc->df_ufunc;
3896 }
3897 else
3898 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003899 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003900 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003901 if (ufunc == NULL)
3902 {
3903 SOURCING_LNUM = iptr->isn_lnum;
3904 iemsg("ufunc unexpectedly NULL for FUNCREF");
3905 goto theend;
3906 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003907 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003908 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003910 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 tv->vval.v_partial = pt;
3912 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003913 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 }
3915 break;
3916
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003917 // Create a global function from a lambda.
3918 case ISN_NEWFUNC:
3919 {
3920 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3921
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003922 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003923 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003924 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003925 }
3926 break;
3927
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003928 // List functions
3929 case ISN_DEF:
3930 if (iptr->isn_arg.string == NULL)
3931 list_functions(NULL);
3932 else
3933 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003934 exarg_T ea;
3935 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003936
3937 CLEAR_FIELD(ea);
3938 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003939 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3940 define_function(&ea, NULL, &lines_to_free);
3941 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003942 }
3943 break;
3944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 // jump if a condition is met
3946 case ISN_JUMP:
3947 {
3948 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003949 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 int jump = TRUE;
3951
3952 if (when != JUMP_ALWAYS)
3953 {
3954 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003955 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003956 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003957 || when == JUMP_IF_COND_TRUE)
3958 {
3959 SOURCING_LNUM = iptr->isn_lnum;
3960 jump = tv_get_bool_chk(tv, &error);
3961 if (error)
3962 goto on_error;
3963 }
3964 else
3965 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01003966 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003968 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 {
3970 // drop the value from the stack
3971 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003972 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 }
3974 }
3975 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003976 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 }
3978 break;
3979
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003980 // Jump if an argument with a default value was already set and not
3981 // v:none.
3982 case ISN_JUMP_IF_ARG_SET:
3983 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3984 if (tv->v_type != VAR_UNKNOWN
3985 && !(tv->v_type == VAR_SPECIAL
3986 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003987 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003988 break;
3989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 // top of a for loop
3991 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003992 if (execute_for(iptr, ectx) == FAIL)
3993 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 break;
3995
3996 // start of ":try" block
3997 case ISN_TRY:
3998 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003999 trycmd_T *trycmd = NULL;
4000
Bram Moolenaar35578162021-08-02 19:10:38 +02004001 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004002 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004003 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4004 + ectx->ec_trystack.ga_len;
4005 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004007 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004008 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4009 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004010 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004011 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004012 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004013 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004014 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004015 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 }
4017 break;
4018
4019 case ISN_PUSHEXC:
4020 if (current_exception == NULL)
4021 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004022 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004024 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004026 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004027 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004029 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004031 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 tv->vval.v_string = vim_strsave(
4033 (char_u *)current_exception->value);
4034 break;
4035
4036 case ISN_CATCH:
4037 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004038 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004039 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040
Bram Moolenaar4c137212021-04-19 16:48:48 +02004041 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004042 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004044 trycmd->tcd_caught = TRUE;
4045 trycmd->tcd_did_throw = FALSE;
4046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004048 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 catch_exception(current_exception);
4050 }
4051 break;
4052
Bram Moolenaarc150c092021-02-13 15:02:46 +01004053 case ISN_TRYCONT:
4054 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004055 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004056 trycont_T *trycont = &iptr->isn_arg.trycont;
4057 int i;
4058 trycmd_T *trycmd;
4059 int iidx = trycont->tct_where;
4060
4061 if (trystack->ga_len < trycont->tct_levels)
4062 {
4063 siemsg("TRYCONT: expected %d levels, found %d",
4064 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004065 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004066 }
4067 // Make :endtry jump to any outer try block and the last
4068 // :endtry inside the loop to the loop start.
4069 for (i = trycont->tct_levels; i > 0; --i)
4070 {
4071 trycmd = ((trycmd_T *)trystack->ga_data)
4072 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004073 // Add one to tcd_cont to be able to jump to
4074 // instruction with index zero.
4075 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004076 iidx = trycmd->tcd_finally_idx == 0
4077 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004078 }
4079 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004080 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004081 }
4082 break;
4083
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004084 case ISN_FINALLY:
4085 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004086 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004087 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4088 + trystack->ga_len - 1;
4089
4090 // Reset the index to avoid a return statement jumps here
4091 // again.
4092 trycmd->tcd_finally_idx = 0;
4093 break;
4094 }
4095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 // end of ":try" block
4097 case ISN_ENDTRY:
4098 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004099 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004100 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101
Bram Moolenaar06651632022-04-27 17:54:25 +01004102 --trystack->ga_len;
4103 --trylevel;
4104 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4105 if (trycmd->tcd_did_throw)
4106 did_throw = TRUE;
4107 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004109 // discard the exception
4110 if (caught_stack == current_exception)
4111 caught_stack = caught_stack->caught;
4112 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004114
4115 if (trycmd->tcd_return)
4116 goto func_return;
4117
4118 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4119 {
4120 --ectx->ec_stack.ga_len;
4121 clear_tv(STACK_TV_BOT(0));
4122 }
4123 if (trycmd->tcd_cont != 0)
4124 // handling :continue: jump to outer try block or
4125 // start of the loop
4126 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 }
4128 break;
4129
4130 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004131 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004132 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004133
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004134 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4135 {
4136 // throwing an exception while using "silent!" causes
4137 // the function to abort but not display an error.
4138 tv = STACK_TV_BOT(-1);
4139 clear_tv(tv);
4140 tv->v_type = VAR_NUMBER;
4141 tv->vval.v_number = 0;
4142 goto done;
4143 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004144 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004145 tv = STACK_TV_BOT(0);
4146 if (tv->vval.v_string == NULL
4147 || *skipwhite(tv->vval.v_string) == NUL)
4148 {
4149 vim_free(tv->vval.v_string);
4150 SOURCING_LNUM = iptr->isn_lnum;
4151 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004152 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004153 }
4154
4155 // Inside a "catch" we need to first discard the caught
4156 // exception.
4157 if (trystack->ga_len > 0)
4158 {
4159 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4160 + trystack->ga_len - 1;
4161 if (trycmd->tcd_caught && current_exception != NULL)
4162 {
4163 // discard the exception
4164 if (caught_stack == current_exception)
4165 caught_stack = caught_stack->caught;
4166 discard_current_exception();
4167 trycmd->tcd_caught = FALSE;
4168 }
4169 }
4170
Bram Moolenaar90a57162022-02-12 14:23:17 +00004171 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004172 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4173 == FAIL)
4174 {
4175 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004176 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004177 }
4178 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 break;
4181
4182 // compare with special values
4183 case ISN_COMPAREBOOL:
4184 case ISN_COMPARESPECIAL:
4185 {
4186 typval_T *tv1 = STACK_TV_BOT(-2);
4187 typval_T *tv2 = STACK_TV_BOT(-1);
4188 varnumber_T arg1 = tv1->vval.v_number;
4189 varnumber_T arg2 = tv2->vval.v_number;
4190 int res;
4191
Bram Moolenaar06651632022-04-27 17:54:25 +01004192 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4193 res = arg1 == arg2;
4194 else
4195 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196
Bram Moolenaar4c137212021-04-19 16:48:48 +02004197 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 tv1->v_type = VAR_BOOL;
4199 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4200 }
4201 break;
4202
Bram Moolenaar7a222242022-03-01 19:23:24 +00004203 case ISN_COMPARENULL:
4204 {
4205 typval_T *tv1 = STACK_TV_BOT(-2);
4206 typval_T *tv2 = STACK_TV_BOT(-1);
4207 int res;
4208
4209 res = typval_compare_null(tv1, tv2);
4210 if (res == MAYBE)
4211 goto on_error;
4212 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4213 res = !res;
4214 clear_tv(tv1);
4215 clear_tv(tv2);
4216 --ectx->ec_stack.ga_len;
4217 tv1->v_type = VAR_BOOL;
4218 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4219 }
4220 break;
4221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 // Operation with two number arguments
4223 case ISN_OPNR:
4224 case ISN_COMPARENR:
4225 {
4226 typval_T *tv1 = STACK_TV_BOT(-2);
4227 typval_T *tv2 = STACK_TV_BOT(-1);
4228 varnumber_T arg1 = tv1->vval.v_number;
4229 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004230 varnumber_T res = 0;
4231 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004233 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4234 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4235 {
4236 if (arg2 < 0)
4237 {
4238 SOURCING_LNUM = iptr->isn_lnum;
4239 emsg(_(e_bitshift_ops_must_be_postive));
4240 goto on_error;
4241 }
4242 }
4243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 switch (iptr->isn_arg.op.op_type)
4245 {
4246 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004247 case EXPR_DIV: if (arg2 == 0)
4248 div_zero = TRUE;
4249 else
4250 res = arg1 / arg2;
4251 break;
4252 case EXPR_REM: if (arg2 == 0)
4253 div_zero = TRUE;
4254 else
4255 res = arg1 % arg2;
4256 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 case EXPR_SUB: res = arg1 - arg2; break;
4258 case EXPR_ADD: res = arg1 + arg2; break;
4259
4260 case EXPR_EQUAL: res = arg1 == arg2; break;
4261 case EXPR_NEQUAL: res = arg1 != arg2; break;
4262 case EXPR_GREATER: res = arg1 > arg2; break;
4263 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4264 case EXPR_SMALLER: res = arg1 < arg2; break;
4265 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004266 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4267 res = 0;
4268 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004269 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004270 break;
4271 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4272 res = 0;
4273 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004274 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004275 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004276 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 }
4278
Bram Moolenaar4c137212021-04-19 16:48:48 +02004279 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 if (iptr->isn_type == ISN_COMPARENR)
4281 {
4282 tv1->v_type = VAR_BOOL;
4283 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4284 }
4285 else
4286 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004287 if (div_zero)
4288 {
4289 SOURCING_LNUM = iptr->isn_lnum;
4290 emsg(_(e_divide_by_zero));
4291 goto on_error;
4292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 }
4294 break;
4295
4296 // Computation with two float arguments
4297 case ISN_OPFLOAT:
4298 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004299#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 {
4301 typval_T *tv1 = STACK_TV_BOT(-2);
4302 typval_T *tv2 = STACK_TV_BOT(-1);
4303 float_T arg1 = tv1->vval.v_float;
4304 float_T arg2 = tv2->vval.v_float;
4305 float_T res = 0;
4306 int cmp = FALSE;
4307
4308 switch (iptr->isn_arg.op.op_type)
4309 {
4310 case EXPR_MULT: res = arg1 * arg2; break;
4311 case EXPR_DIV: res = arg1 / arg2; break;
4312 case EXPR_SUB: res = arg1 - arg2; break;
4313 case EXPR_ADD: res = arg1 + arg2; break;
4314
4315 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4316 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4317 case EXPR_GREATER: cmp = arg1 > arg2; break;
4318 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4319 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4320 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4321 default: cmp = 0; break;
4322 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004323 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 if (iptr->isn_type == ISN_COMPAREFLOAT)
4325 {
4326 tv1->v_type = VAR_BOOL;
4327 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4328 }
4329 else
4330 tv1->vval.v_float = res;
4331 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004332#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 break;
4334
4335 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004336 case ISN_COMPAREDICT:
4337 case ISN_COMPAREFUNC:
4338 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 case ISN_COMPAREBLOB:
4340 {
4341 typval_T *tv1 = STACK_TV_BOT(-2);
4342 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004343 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4344 int ic = iptr->isn_arg.op.op_ic;
4345 int res = FALSE;
4346 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347
Bram Moolenaar265f8112021-12-19 12:33:05 +00004348 SOURCING_LNUM = iptr->isn_lnum;
4349 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004351 status = typval_compare_list(tv1, tv2,
4352 exprtype, ic, &res);
4353 }
4354 else if (iptr->isn_type == ISN_COMPAREDICT)
4355 {
4356 status = typval_compare_dict(tv1, tv2,
4357 exprtype, ic, &res);
4358 }
4359 else if (iptr->isn_type == ISN_COMPAREFUNC)
4360 {
4361 status = typval_compare_func(tv1, tv2,
4362 exprtype, ic, &res);
4363 }
4364 else if (iptr->isn_type == ISN_COMPARESTRING)
4365 {
4366 status = typval_compare_string(tv1, tv2,
4367 exprtype, ic, &res);
4368 }
4369 else
4370 {
4371 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004373 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 clear_tv(tv1);
4375 clear_tv(tv2);
4376 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004377 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4378 if (status == FAIL)
4379 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 }
4381 break;
4382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 case ISN_COMPAREANY:
4384 {
4385 typval_T *tv1 = STACK_TV_BOT(-2);
4386 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004387 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004389 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390
Bram Moolenaareb26f432020-09-14 16:50:05 +02004391 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004392 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004394 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004395 if (status == FAIL)
4396 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 }
4398 break;
4399
4400 case ISN_ADDLIST:
4401 case ISN_ADDBLOB:
4402 {
4403 typval_T *tv1 = STACK_TV_BOT(-2);
4404 typval_T *tv2 = STACK_TV_BOT(-1);
4405
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004406 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004408 {
4409 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4410 && tv1->vval.v_list != NULL)
4411 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4412 NULL);
4413 else
4414 eval_addlist(tv1, tv2);
4415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 else
4417 eval_addblob(tv1, tv2);
4418 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004419 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 }
4421 break;
4422
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004423 case ISN_LISTAPPEND:
4424 {
4425 typval_T *tv1 = STACK_TV_BOT(-2);
4426 typval_T *tv2 = STACK_TV_BOT(-1);
4427 list_T *l = tv1->vval.v_list;
4428
4429 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004430 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004431 if (l == NULL)
4432 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004433 emsg(_(e_cannot_add_to_null_list));
4434 goto on_error;
4435 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004436 if (value_check_lock(l->lv_lock, NULL, FALSE))
4437 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004438 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004439 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004440 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004441 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004442 }
4443 break;
4444
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004445 case ISN_BLOBAPPEND:
4446 {
4447 typval_T *tv1 = STACK_TV_BOT(-2);
4448 typval_T *tv2 = STACK_TV_BOT(-1);
4449 blob_T *b = tv1->vval.v_blob;
4450 int error = FALSE;
4451 varnumber_T n;
4452
4453 // add a number to a blob
4454 if (b == NULL)
4455 {
4456 SOURCING_LNUM = iptr->isn_lnum;
4457 emsg(_(e_cannot_add_to_null_blob));
4458 goto on_error;
4459 }
4460 n = tv_get_number_chk(tv2, &error);
4461 if (error)
4462 goto on_error;
4463 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004464 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004465 }
4466 break;
4467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 // Computation with two arguments of unknown type
4469 case ISN_OPANY:
4470 {
4471 typval_T *tv1 = STACK_TV_BOT(-2);
4472 typval_T *tv2 = STACK_TV_BOT(-1);
4473 varnumber_T n1, n2;
4474#ifdef FEAT_FLOAT
4475 float_T f1 = 0, f2 = 0;
4476#endif
4477 int error = FALSE;
4478
4479 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4480 {
4481 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4482 {
4483 eval_addlist(tv1, tv2);
4484 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004485 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 break;
4487 }
4488 else if (tv1->v_type == VAR_BLOB
4489 && tv2->v_type == VAR_BLOB)
4490 {
4491 eval_addblob(tv1, tv2);
4492 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004493 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 break;
4495 }
4496 }
4497#ifdef FEAT_FLOAT
4498 if (tv1->v_type == VAR_FLOAT)
4499 {
4500 f1 = tv1->vval.v_float;
4501 n1 = 0;
4502 }
4503 else
4504#endif
4505 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004506 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 n1 = tv_get_number_chk(tv1, &error);
4508 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004509 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510#ifdef FEAT_FLOAT
4511 if (tv2->v_type == VAR_FLOAT)
4512 f1 = n1;
4513#endif
4514 }
4515#ifdef FEAT_FLOAT
4516 if (tv2->v_type == VAR_FLOAT)
4517 {
4518 f2 = tv2->vval.v_float;
4519 n2 = 0;
4520 }
4521 else
4522#endif
4523 {
4524 n2 = tv_get_number_chk(tv2, &error);
4525 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004526 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527#ifdef FEAT_FLOAT
4528 if (tv1->v_type == VAR_FLOAT)
4529 f2 = n2;
4530#endif
4531 }
4532#ifdef FEAT_FLOAT
4533 // if there is a float on either side the result is a float
4534 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4535 {
4536 switch (iptr->isn_arg.op.op_type)
4537 {
4538 case EXPR_MULT: f1 = f1 * f2; break;
4539 case EXPR_DIV: f1 = f1 / f2; break;
4540 case EXPR_SUB: f1 = f1 - f2; break;
4541 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004542 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004543 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004544 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 }
4546 clear_tv(tv1);
4547 clear_tv(tv2);
4548 tv1->v_type = VAR_FLOAT;
4549 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004550 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 }
4552 else
4553#endif
4554 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004555 int failed = FALSE;
4556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 switch (iptr->isn_arg.op.op_type)
4558 {
4559 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004560 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4561 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004562 goto on_error;
4563 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 case EXPR_SUB: n1 = n1 - n2; break;
4565 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004566 default: n1 = num_modulus(n1, n2, &failed);
4567 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004568 goto on_error;
4569 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 }
4571 clear_tv(tv1);
4572 clear_tv(tv2);
4573 tv1->v_type = VAR_NUMBER;
4574 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004575 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 }
4577 }
4578 break;
4579
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004580 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004581 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004582 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004583 int is_slice = iptr->isn_type == ISN_STRSLICE;
4584 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004585 char_u *res;
4586
4587 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004588 // string slice: string is at stack-3, first index at
4589 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004590 if (is_slice)
4591 {
4592 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004593 n1 = tv->vval.v_number;
4594 }
4595
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004596 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004597 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004598
Bram Moolenaar4c137212021-04-19 16:48:48 +02004599 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004600 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004601 if (is_slice)
4602 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004603 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004604 else
4605 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004606 // single character (including composing characters).
4607 // If the index is too big or negative the result is
4608 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004609 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004610 vim_free(tv->vval.v_string);
4611 tv->vval.v_string = res;
4612 }
4613 break;
4614
4615 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004616 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004617 case ISN_BLOBINDEX:
4618 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004620 int is_slice = iptr->isn_type == ISN_LISTSLICE
4621 || iptr->isn_type == ISN_BLOBSLICE;
4622 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4623 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004624 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004625 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626
4627 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004628 // list slice: list is at stack-3, indexes at stack-2 and
4629 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004630 // Same for blob.
4631 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632
4633 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004634 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004636
4637 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 {
Bram Moolenaared591872020-08-15 22:14:53 +02004639 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004640 n1 = tv->vval.v_number;
4641 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 }
Bram Moolenaared591872020-08-15 22:14:53 +02004643
Bram Moolenaar4c137212021-04-19 16:48:48 +02004644 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004645 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004646 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004647 if (is_blob)
4648 {
4649 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4650 n1, n2, FALSE, tv) == FAIL)
4651 goto on_error;
4652 }
4653 else
4654 {
4655 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4656 n1, n2, FALSE, tv, TRUE) == FAIL)
4657 goto on_error;
4658 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 }
4660 break;
4661
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004662 case ISN_ANYINDEX:
4663 case ISN_ANYSLICE:
4664 {
4665 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4666 typval_T *var1, *var2;
4667 int res;
4668
4669 // index: composite is at stack-2, index at stack-1
4670 // slice: composite is at stack-3, indexes at stack-2 and
4671 // stack-1
4672 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004673 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004674 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4675 goto on_error;
4676 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4677 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004678 res = eval_index_inner(tv, is_slice, var1, var2,
4679 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004680 clear_tv(var1);
4681 if (is_slice)
4682 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004683 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004684 if (res == FAIL)
4685 goto on_error;
4686 }
4687 break;
4688
Bram Moolenaar9af78762020-06-16 11:34:42 +02004689 case ISN_SLICE:
4690 {
4691 list_T *list;
4692 int count = iptr->isn_arg.number;
4693
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004694 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004695 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004696 list = tv->vval.v_list;
4697
4698 // no error for short list, expect it to be checked earlier
4699 if (list != NULL && list->lv_len >= count)
4700 {
4701 list_T *newlist = list_slice(list,
4702 count, list->lv_len - 1);
4703
4704 if (newlist != NULL)
4705 {
4706 list_unref(list);
4707 tv->vval.v_list = newlist;
4708 ++newlist->lv_refcount;
4709 }
4710 }
4711 }
4712 break;
4713
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004714 case ISN_GETITEM:
4715 {
4716 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004717 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004718
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004719 // Get list item: list is at stack-1, push item.
4720 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004721 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4722 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004723
Bram Moolenaar35578162021-08-02 19:10:38 +02004724 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004725 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004726 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004727 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004728
4729 // Useful when used in unpack assignment. Reset at
4730 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004731 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004732 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004733 }
4734 break;
4735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 case ISN_MEMBER:
4737 {
4738 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004739 char_u *key;
4740 dictitem_T *di;
4741
4742 // dict member: dict is at stack-2, key at stack-1
4743 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004744 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004745 dict = tv->vval.v_dict;
4746
4747 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004748 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004749 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004750 if (key == NULL)
4751 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004752
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004753 if ((di = dict_find(dict, key, -1)) == NULL)
4754 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004755 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004756 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004757
4758 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004759 // stack contents makes sense and the dict stack is
4760 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004761 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004762 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004763 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004764 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004765 tv->v_type = VAR_NUMBER;
4766 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004767 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004768 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004769 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004770 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004771 // Put the dict used on the dict stack, it might be used by
4772 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004773 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004774 if (dict_stack_save(tv) == FAIL)
4775 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004776 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004777 }
4778 break;
4779
4780 // dict member with string key
4781 case ISN_STRINGMEMBER:
4782 {
4783 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 dictitem_T *di;
4785
4786 tv = STACK_TV_BOT(-1);
4787 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4788 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004789 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004790 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004791 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 }
4793 dict = tv->vval.v_dict;
4794
4795 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4796 == NULL)
4797 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004798 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004799 semsg(_(e_key_not_present_in_dictionary),
4800 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004801 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004803 // Put the dict used on the dict stack, it might be used by
4804 // a dict function later.
4805 if (dict_stack_save(tv) == FAIL)
4806 goto on_fatal_error;
4807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004809 }
4810 break;
4811
4812 case ISN_CLEARDICT:
4813 dict_stack_drop();
4814 break;
4815
4816 case ISN_USEDICT:
4817 {
4818 typval_T *dict_tv = dict_stack_get_tv();
4819
4820 // Turn "dict.Func" into a partial for "Func" bound to
4821 // "dict". Don't do this when "Func" is already a partial
4822 // that was bound explicitly (pt_auto is FALSE).
4823 tv = STACK_TV_BOT(-1);
4824 if (dict_tv != NULL
4825 && dict_tv->v_type == VAR_DICT
4826 && dict_tv->vval.v_dict != NULL
4827 && (tv->v_type == VAR_FUNC
4828 || (tv->v_type == VAR_PARTIAL
4829 && (tv->vval.v_partial->pt_auto
4830 || tv->vval.v_partial->pt_dict == NULL))))
4831 dict_tv->vval.v_dict =
4832 make_partial(dict_tv->vval.v_dict, tv);
4833 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 }
4835 break;
4836
4837 case ISN_NEGATENR:
4838 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004839 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004840#ifdef FEAT_FLOAT
4841 if (tv->v_type == VAR_FLOAT)
4842 tv->vval.v_float = -tv->vval.v_float;
4843 else
4844#endif
4845 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 break;
4847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 case ISN_CHECKTYPE:
4849 {
4850 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004851 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004852 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004854 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004855 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004856 if (!ectx->ec_where.wt_variable)
4857 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004858 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004859 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004860 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004861 if (r == FAIL)
4862 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004863 if (!ectx->ec_where.wt_variable)
4864 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004865
4866 // number 0 is FALSE, number 1 is TRUE
4867 if (tv->v_type == VAR_NUMBER
4868 && ct->ct_type->tt_type == VAR_BOOL
4869 && (tv->vval.v_number == 0
4870 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004872 tv->v_type = VAR_BOOL;
4873 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004874 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 }
4876 }
4877 break;
4878
Bram Moolenaar9af78762020-06-16 11:34:42 +02004879 case ISN_CHECKLEN:
4880 {
4881 int min_len = iptr->isn_arg.checklen.cl_min_len;
4882 list_T *list = NULL;
4883
4884 tv = STACK_TV_BOT(-1);
4885 if (tv->v_type == VAR_LIST)
4886 list = tv->vval.v_list;
4887 if (list == NULL || list->lv_len < min_len
4888 || (list->lv_len > min_len
4889 && !iptr->isn_arg.checklen.cl_more_OK))
4890 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004891 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004892 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004893 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004894 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004895 }
4896 }
4897 break;
4898
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004899 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004900 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004901 break;
4902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004904 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 {
4906 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004907 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004909 if (iptr->isn_type == ISN_2BOOL)
4910 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004911 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004912 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004913 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004914 n = !n;
4915 }
4916 else
4917 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004918 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004919 SOURCING_LNUM = iptr->isn_lnum;
4920 n = tv_get_bool_chk(tv, &error);
4921 if (error)
4922 goto on_error;
4923 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 clear_tv(tv);
4925 tv->v_type = VAR_BOOL;
4926 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4927 }
4928 break;
4929
4930 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004931 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004932 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004933 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4934 iptr->isn_type == ISN_2STRING_ANY,
4935 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004936 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 break;
4938
Bram Moolenaar08597872020-12-10 19:43:40 +01004939 case ISN_RANGE:
4940 {
4941 exarg_T ea;
4942 char *errormsg;
4943
Bram Moolenaarece0b872021-01-08 20:40:45 +01004944 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004945 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004946 ea.addr_type = ADDR_LINES;
4947 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004948 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004949 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004950 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004951
Bram Moolenaar35578162021-08-02 19:10:38 +02004952 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004953 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004954 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004955 tv = STACK_TV_BOT(-1);
4956 tv->v_type = VAR_NUMBER;
4957 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004958 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004959 }
4960 break;
4961
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004962 case ISN_PUT:
4963 {
4964 int regname = iptr->isn_arg.put.put_regname;
4965 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4966 char_u *expr = NULL;
4967 int dir = FORWARD;
4968
Bram Moolenaar08597872020-12-10 19:43:40 +01004969 if (lnum < -2)
4970 {
4971 // line number was put on the stack by ISN_RANGE
4972 tv = STACK_TV_BOT(-1);
4973 curwin->w_cursor.lnum = tv->vval.v_number;
4974 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4975 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004976 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004977 }
4978 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004979 // :put! above cursor
4980 dir = BACKWARD;
4981 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004982 {
4983 curwin->w_cursor.lnum = lnum;
4984 if (lnum == 0)
4985 // check_cursor() below will move to line 1
4986 dir = BACKWARD;
4987 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004988
4989 if (regname == '=')
4990 {
4991 tv = STACK_TV_BOT(-1);
4992 if (tv->v_type == VAR_STRING)
4993 expr = tv->vval.v_string;
4994 else
4995 {
4996 expr = typval2string(tv, TRUE); // allocates value
4997 clear_tv(tv);
4998 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004999 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005000 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005001 check_cursor();
5002 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5003 vim_free(expr);
5004 }
5005 break;
5006
Bram Moolenaar02194d22020-10-24 23:08:38 +02005007 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005008 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5009 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5010 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5011 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005012 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5013 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005014 break;
5015
Bram Moolenaar02194d22020-10-24 23:08:38 +02005016 case ISN_CMDMOD_REV:
5017 // filter regprog is owned by the instruction, don't free it
5018 cmdmod.cmod_filter_regmatch.regprog = NULL;
5019 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005020 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5021 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005022 break;
5023
Bram Moolenaar792f7862020-11-23 08:31:18 +01005024 case ISN_UNPACK:
5025 {
5026 int count = iptr->isn_arg.unpack.unp_count;
5027 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5028 list_T *l;
5029 listitem_T *li;
5030 int i;
5031
5032 // Check there is a valid list to unpack.
5033 tv = STACK_TV_BOT(-1);
5034 if (tv->v_type != VAR_LIST)
5035 {
5036 SOURCING_LNUM = iptr->isn_lnum;
5037 emsg(_(e_for_argument_must_be_sequence_of_lists));
5038 goto on_error;
5039 }
5040 l = tv->vval.v_list;
5041 if (l == NULL
5042 || l->lv_len < (semicolon ? count - 1 : count))
5043 {
5044 SOURCING_LNUM = iptr->isn_lnum;
5045 emsg(_(e_list_value_does_not_have_enough_items));
5046 goto on_error;
5047 }
5048 else if (!semicolon && l->lv_len > count)
5049 {
5050 SOURCING_LNUM = iptr->isn_lnum;
5051 emsg(_(e_list_value_has_more_items_than_targets));
5052 goto on_error;
5053 }
5054
5055 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005056 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005057 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005058 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005059
5060 // Variable after semicolon gets a list with the remaining
5061 // items.
5062 if (semicolon)
5063 {
5064 list_T *rem_list =
5065 list_alloc_with_items(l->lv_len - count + 1);
5066
5067 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005068 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005069 tv = STACK_TV_BOT(-count);
5070 tv->vval.v_list = rem_list;
5071 ++rem_list->lv_refcount;
5072 tv->v_lock = 0;
5073 li = l->lv_first;
5074 for (i = 0; i < count - 1; ++i)
5075 li = li->li_next;
5076 for (i = 0; li != NULL; ++i)
5077 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005078 typval_T tvcopy;
5079
5080 copy_tv(&li->li_tv, &tvcopy);
5081 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005082 li = li->li_next;
5083 }
5084 --count;
5085 }
5086
5087 // Produce the values in reverse order, first item last.
5088 li = l->lv_first;
5089 for (i = 0; i < count; ++i)
5090 {
5091 tv = STACK_TV_BOT(-i - 1);
5092 copy_tv(&li->li_tv, tv);
5093 li = li->li_next;
5094 }
5095
5096 list_unref(l);
5097 }
5098 break;
5099
Bram Moolenaarb2049902021-01-24 12:53:53 +01005100 case ISN_PROF_START:
5101 case ISN_PROF_END:
5102 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005103#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005104 funccall_T cookie;
5105 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005106 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005107 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005108
Bram Moolenaarca16c602022-09-06 18:57:08 +01005109 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005110 if (iptr->isn_type == ISN_PROF_START)
5111 {
5112 func_line_start(&cookie, iptr->isn_lnum);
5113 // if we get here the instruction is executed
5114 func_line_exec(&cookie);
5115 }
5116 else
5117 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005118#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005119 }
5120 break;
5121
Bram Moolenaare99d4222021-06-13 14:01:26 +02005122 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005123 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005124 break;
5125
Bram Moolenaar389df252020-07-09 21:20:47 +02005126 case ISN_SHUFFLE:
5127 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005128 typval_T tmp_tv;
5129 int item = iptr->isn_arg.shuffle.shfl_item;
5130 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005131
5132 tmp_tv = *STACK_TV_BOT(-item);
5133 for ( ; up > 0 && item > 1; --up)
5134 {
5135 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5136 --item;
5137 }
5138 *STACK_TV_BOT(-item) = tmp_tv;
5139 }
5140 break;
5141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005143 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005145 ectx->ec_where.wt_index = 0;
5146 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 break;
5148 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005149 continue;
5150
Bram Moolenaard032f342020-07-18 18:13:02 +02005151func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005152 // Restore previous function. If the frame pointer is where we started
5153 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005154 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005155 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005156
Bram Moolenaar4c137212021-04-19 16:48:48 +02005157 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005158 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005159 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005160 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005161
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005162on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005163 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005164 // If "emsg_silent" is set then ignore the error, unless it was set
5165 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005166 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005167 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005168 {
5169 // If a sequence of instructions causes an error while ":silent!"
5170 // was used, restore the stack length and jump ahead to restoring
5171 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005172 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005173 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005174 while (ectx->ec_stack.ga_len
5175 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005176 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005177 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005178 clear_tv(STACK_TV_BOT(0));
5179 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005180 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5181 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005182 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005183 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005184 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005185on_fatal_error:
5186 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005187 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005188 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005189 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 }
5191
5192done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005193 ret = OK;
5194theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005195 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005196
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005197 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005198 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5199 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005200}
5201
5202/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005203 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005204 * "rettv".
5205 * Return OK or FAIL.
5206 */
5207 int
5208exe_typval_instr(typval_T *tv, typval_T *rettv)
5209{
5210 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5211 isn_T *save_instr = ectx->ec_instr;
5212 int save_iidx = ectx->ec_iidx;
5213 int res;
5214
LemonBoyf3b48952022-05-05 13:53:03 +01005215 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5216 // even when the compilation fails.
5217 rettv->v_type = VAR_UNKNOWN;
5218
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005219 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5220 res = exec_instructions(ectx);
5221 if (res == OK)
5222 {
5223 *rettv = *STACK_TV_BOT(-1);
5224 --ectx->ec_stack.ga_len;
5225 }
5226
5227 ectx->ec_instr = save_instr;
5228 ectx->ec_iidx = save_iidx;
5229
5230 return res;
5231}
5232
5233/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005234 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5235 * "substitute_instr".
5236 */
5237 char_u *
5238exe_substitute_instr(void)
5239{
5240 ectx_T *ectx = substitute_instr->subs_ectx;
5241 isn_T *save_instr = ectx->ec_instr;
5242 int save_iidx = ectx->ec_iidx;
5243 char_u *res;
5244
5245 ectx->ec_instr = substitute_instr->subs_instr;
5246 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005247 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005248 typval_T *tv = STACK_TV_BOT(-1);
5249
Bram Moolenaar27523602021-06-05 21:36:19 +02005250 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005251 --ectx->ec_stack.ga_len;
5252 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005253 }
5254 else
5255 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005256 substitute_instr->subs_status = FAIL;
5257 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259
Bram Moolenaar4c137212021-04-19 16:48:48 +02005260 ectx->ec_instr = save_instr;
5261 ectx->ec_iidx = save_iidx;
5262
5263 return res;
5264}
5265
5266/*
5267 * Call a "def" function from old Vim script.
5268 * Return OK or FAIL.
5269 */
5270 int
5271call_def_function(
5272 ufunc_T *ufunc,
5273 int argc_arg, // nr of arguments
5274 typval_T *argv, // arguments
5275 partial_T *partial, // optional partial for context
Bram Moolenaar58779852022-09-06 18:31:14 +01005276 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005277 typval_T *rettv) // return value
5278{
5279 ectx_T ectx; // execution context
5280 int argc = argc_arg;
5281 typval_T *tv;
5282 int idx;
5283 int ret = FAIL;
5284 int defcount = ufunc->uf_args.ga_len - argc;
5285 sctx_T save_current_sctx = current_sctx;
5286 int did_emsg_before = did_emsg_cumul + did_emsg;
5287 int save_suppress_errthrow = suppress_errthrow;
5288 msglist_T **saved_msg_list = NULL;
5289 msglist_T *private_msg_list = NULL;
5290 int save_emsg_silent_def = emsg_silent_def;
5291 int save_did_emsg_def = did_emsg_def;
5292 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005293 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005294
5295// Get pointer to item in the stack.
5296#undef STACK_TV
5297#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5298
5299// Get pointer to item at the bottom of the stack, -1 is the bottom.
5300#undef STACK_TV_BOT
5301#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5302
5303// Get pointer to a local variable on the stack. Negative for arguments.
5304#undef STACK_TV_VAR
5305#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5306
5307 if (ufunc->uf_def_status == UF_NOT_COMPILED
5308 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005309 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5310 && compile_def_function(ufunc, FALSE,
5311 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005312 {
5313 if (did_emsg_cumul + did_emsg == did_emsg_before)
5314 semsg(_(e_function_is_not_compiled_str),
5315 printable_func_name(ufunc));
5316 return FAIL;
5317 }
5318
5319 {
5320 // Check the function was really compiled.
5321 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5322 + ufunc->uf_dfunc_idx;
5323 if (INSTRUCTIONS(dfunc) == NULL)
5324 {
5325 iemsg("using call_def_function() on not compiled function");
5326 return FAIL;
5327 }
5328 }
5329
5330 // If depth of calling is getting too high, don't execute the function.
5331 orig_funcdepth = funcdepth_get();
5332 if (funcdepth_increment() == FAIL)
5333 return FAIL;
5334
5335 CLEAR_FIELD(ectx);
5336 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5337 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005338 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005339 {
5340 funcdepth_decrement();
5341 return FAIL;
5342 }
5343 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5344 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5345 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005346 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005347
5348 idx = argc - ufunc->uf_args.ga_len;
5349 if (idx > 0 && ufunc->uf_va_name == NULL)
5350 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005351 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
5352 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005353 goto failed_early;
5354 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005355 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5356 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005357 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005358 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005359 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005360 goto failed_early;
5361 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005362
5363 // Put arguments on the stack, but no more than what the function expects.
5364 // A lambda can be called with more arguments than it uses.
5365 for (idx = 0; idx < argc
5366 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5367 ++idx)
5368 {
5369 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5370 && argv[idx].v_type == VAR_SPECIAL
5371 && argv[idx].vval.v_number == VVAL_NONE)
5372 {
5373 // Use the default value.
5374 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5375 }
5376 else
5377 {
5378 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5379 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005380 ufunc->uf_arg_types[idx], &argv[idx],
5381 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005382 goto failed_early;
5383 copy_tv(&argv[idx], STACK_TV_BOT(0));
5384 }
5385 ++ectx.ec_stack.ga_len;
5386 }
5387
5388 // Turn varargs into a list. Empty list if no args.
5389 if (ufunc->uf_va_name != NULL)
5390 {
5391 int vararg_count = argc - ufunc->uf_args.ga_len;
5392
5393 if (vararg_count < 0)
5394 vararg_count = 0;
5395 else
5396 argc -= vararg_count;
5397 if (exe_newlist(vararg_count, &ectx) == FAIL)
5398 goto failed_early;
5399
5400 // Check the type of the list items.
5401 tv = STACK_TV_BOT(-1);
5402 if (ufunc->uf_va_type != NULL
5403 && ufunc->uf_va_type != &t_list_any
5404 && ufunc->uf_va_type->tt_member != &t_any
5405 && tv->vval.v_list != NULL)
5406 {
5407 type_T *expected = ufunc->uf_va_type->tt_member;
5408 listitem_T *li = tv->vval.v_list->lv_first;
5409
5410 for (idx = 0; idx < vararg_count; ++idx)
5411 {
5412 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005413 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005414 goto failed_early;
5415 li = li->li_next;
5416 }
5417 }
5418
5419 if (defcount > 0)
5420 // Move varargs list to below missing default arguments.
5421 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5422 --ectx.ec_stack.ga_len;
5423 }
5424
5425 // Make space for omitted arguments, will store default value below.
5426 // Any varargs list goes after them.
5427 if (defcount > 0)
5428 for (idx = 0; idx < defcount; ++idx)
5429 {
5430 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5431 ++ectx.ec_stack.ga_len;
5432 }
5433 if (ufunc->uf_va_name != NULL)
5434 ++ectx.ec_stack.ga_len;
5435
5436 // Frame pointer points to just after arguments.
5437 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5438 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5439
5440 {
5441 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5442 + ufunc->uf_dfunc_idx;
5443 ufunc_T *base_ufunc = dfunc->df_ufunc;
5444
5445 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5446 // by copy_func().
5447 if (partial != NULL || base_ufunc->uf_partial != NULL)
5448 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005449 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5450 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005451 goto failed_early;
5452 if (partial != NULL)
5453 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005454 outer_T *outer = get_pt_outer(partial);
5455
5456 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005457 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005458 if (current_ectx != NULL)
5459 {
5460 if (current_ectx->ec_outer_ref != NULL
5461 && current_ectx->ec_outer_ref->or_outer != NULL)
5462 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005463 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005464 }
5465 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005466 }
5467 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005468 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005469 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005470 ++partial->pt_refcount;
5471 ectx.ec_outer_ref->or_partial = partial;
5472 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005473 }
5474 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005475 {
5476 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5477 ++base_ufunc->uf_partial->pt_refcount;
5478 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5479 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005480 }
5481 }
5482
5483 // dummy frame entries
5484 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5485 {
5486 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5487 ++ectx.ec_stack.ga_len;
5488 }
5489
5490 {
5491 // Reserve space for local variables and any closure reference count.
5492 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5493 + ufunc->uf_dfunc_idx;
5494
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005495 // Initialize variables to zero. That avoids having to generate
5496 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005497 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005498 {
5499 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5500 STACK_TV_VAR(idx)->vval.v_number = 0;
5501 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005502 ectx.ec_stack.ga_len += dfunc->df_varcount;
5503 if (dfunc->df_has_closure)
5504 {
5505 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5506 STACK_TV_VAR(idx)->vval.v_number = 0;
5507 ++ectx.ec_stack.ga_len;
5508 }
5509
5510 ectx.ec_instr = INSTRUCTIONS(dfunc);
5511 }
5512
Bram Moolenaar58779852022-09-06 18:31:14 +01005513 // Store the execution context in funccal, used by invoke_all_defer().
5514 if (funccal != NULL)
5515 funccal->fc_ectx = &ectx;
5516
Bram Moolenaar4c137212021-04-19 16:48:48 +02005517 // Following errors are in the function, not the caller.
5518 // Commands behave like vim9script.
5519 estack_push_ufunc(ufunc, 1);
5520 current_sctx = ufunc->uf_script_ctx;
5521 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5522
5523 // Use a specific location for storing error messages to be converted to an
5524 // exception.
5525 saved_msg_list = msg_list;
5526 msg_list = &private_msg_list;
5527
5528 // Do turn errors into exceptions.
5529 suppress_errthrow = FALSE;
5530
5531 // Do not delete the function while executing it.
5532 ++ufunc->uf_calls;
5533
5534 // When ":silent!" was used before calling then we still abort the
5535 // function. If ":silent!" is used in the function then we don't.
5536 emsg_silent_def = emsg_silent;
5537 did_emsg_def = 0;
5538
5539 ectx.ec_where.wt_index = 0;
5540 ectx.ec_where.wt_variable = FALSE;
5541
5542 // Execute the instructions until done.
5543 ret = exec_instructions(&ectx);
5544 if (ret == OK)
5545 {
5546 // function finished, get result from the stack.
5547 if (ufunc->uf_ret_type == &t_void)
5548 {
5549 rettv->v_type = VAR_VOID;
5550 }
5551 else
5552 {
5553 tv = STACK_TV_BOT(-1);
5554 *rettv = *tv;
5555 tv->v_type = VAR_UNKNOWN;
5556 }
5557 }
5558
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005559 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005560 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005561
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005562 // Deal with any remaining closures, they may be in use somewhere.
5563 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005564 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005565 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005566 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005567 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005568
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005569 estack_pop();
5570 current_sctx = save_current_sctx;
5571
Bram Moolenaar2336c372021-12-06 15:06:54 +00005572 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5573 // Function was unreferenced while being used, free it now.
5574 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005575
Bram Moolenaar352134b2020-10-17 22:04:08 +02005576 if (*msg_list != NULL && saved_msg_list != NULL)
5577 {
5578 msglist_T **plist = saved_msg_list;
5579
5580 // Append entries from the current msg_list (uncaught exceptions) to
5581 // the saved msg_list.
5582 while (*plist != NULL)
5583 plist = &(*plist)->next;
5584
5585 *plist = *msg_list;
5586 }
5587 msg_list = saved_msg_list;
5588
Bram Moolenaar4c137212021-04-19 16:48:48 +02005589 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005590 {
5591 cmdmod.cmod_filter_regmatch.regprog = NULL;
5592 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005593 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005594 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005595 emsg_silent_def = save_emsg_silent_def;
5596 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005597
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005598failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005599 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005600 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5601 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005602 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005604 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005605 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005606 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005607 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005608 if (ectx.ec_outer_ref->or_outer_allocated)
5609 vim_free(ectx.ec_outer_ref->or_outer);
5610 partial_unref(ectx.ec_outer_ref->or_partial);
5611 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005612 }
5613
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005614 // Not sure if this is necessary.
5615 suppress_errthrow = save_suppress_errthrow;
5616
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005617 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5618 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005619 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005620 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005621 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005622 return ret;
5623}
5624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625/*
Bram Moolenaar58779852022-09-06 18:31:14 +01005626 * Called when a def function has finished (possibly failed).
5627 * Invoke all the function returns to clean up and invoke deferred functions,
5628 * except the toplevel one.
5629 */
5630 void
5631unwind_def_callstack(ectx_T *ectx)
5632{
5633 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
5634 func_return(ectx);
5635}
5636
5637/*
5638 * Invoke any deffered functions for the top function in "ectx".
5639 */
5640 void
5641may_invoke_defer_funcs(ectx_T *ectx)
5642{
5643 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
5644
5645 if (dfunc->df_defer_var_idx > 0)
5646 invoke_defer_funcs(ectx);
5647}
5648
5649/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005650 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5651 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5652 * "pfx" is prefixed to every line.
5653 */
5654 static void
5655list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5656{
5657 int line_idx = 0;
5658 int prev_current = 0;
5659 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005660 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005661
5662 for (current = 0; current < instr_count; ++current)
5663 {
5664 isn_T *iptr = &instr[current];
5665 char *line;
5666
5667 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005668 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005669 while (line_idx < iptr->isn_lnum
5670 && line_idx < ufunc->uf_lines.ga_len)
5671 {
5672 if (current > prev_current)
5673 {
5674 msg_puts("\n\n");
5675 prev_current = current;
5676 }
5677 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5678 if (line != NULL)
5679 msg(line);
5680 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005681 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5682 {
5683 int first_def_arg = ufunc->uf_args.ga_len
5684 - ufunc->uf_def_args.ga_len;
5685
5686 if (def_arg_idx > 0)
5687 msg_puts("\n\n");
5688 msg_start();
5689 msg_puts(" ");
5690 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5691 first_def_arg + def_arg_idx]);
5692 msg_puts(" = ");
5693 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5694 msg_clr_eos();
5695 msg_end();
5696 }
5697 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005698
5699 switch (iptr->isn_type)
5700 {
5701 case ISN_EXEC:
5702 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5703 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005704 case ISN_EXEC_SPLIT:
5705 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5706 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005707 case ISN_EXECRANGE:
5708 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5709 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005710 case ISN_LEGACY_EVAL:
5711 smsg("%s%4d EVAL legacy %s", pfx, current,
5712 iptr->isn_arg.string);
5713 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005714 case ISN_REDIRSTART:
5715 smsg("%s%4d REDIR", pfx, current);
5716 break;
5717 case ISN_REDIREND:
5718 smsg("%s%4d REDIR END%s", pfx, current,
5719 iptr->isn_arg.number ? " append" : "");
5720 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005721 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005722#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005723 smsg("%s%4d CEXPR pre %s", pfx, current,
5724 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005725#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005726 break;
5727 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005728#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005729 {
5730 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5731
5732 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5733 cexpr_get_auname(cer->cer_cmdidx),
5734 cer->cer_forceit ? "!" : "",
5735 cer->cer_cmdline);
5736 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005737#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005738 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005739 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005740 smsg("%s%4d INSTR", pfx, current);
5741 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5742 msg(" -------------");
5743 break;
5744 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005745 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005746 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5747
5748 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005749 }
5750 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005751 case ISN_SUBSTITUTE:
5752 {
5753 subs_T *subs = &iptr->isn_arg.subs;
5754
5755 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5756 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5757 msg(" -------------");
5758 }
5759 break;
5760 case ISN_EXECCONCAT:
5761 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5762 (varnumber_T)iptr->isn_arg.number);
5763 break;
5764 case ISN_ECHO:
5765 {
5766 echo_T *echo = &iptr->isn_arg.echo;
5767
5768 smsg("%s%4d %s %d", pfx, current,
5769 echo->echo_with_white ? "ECHO" : "ECHON",
5770 echo->echo_count);
5771 }
5772 break;
5773 case ISN_EXECUTE:
5774 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005775 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005776 break;
5777 case ISN_ECHOMSG:
5778 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005779 (varnumber_T)(iptr->isn_arg.number));
5780 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01005781 case ISN_ECHOWINDOW:
5782 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
5783 (varnumber_T)(iptr->isn_arg.number));
5784 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02005785 case ISN_ECHOCONSOLE:
5786 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5787 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005788 break;
5789 case ISN_ECHOERR:
5790 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005791 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005792 break;
5793 case ISN_LOAD:
5794 {
5795 if (iptr->isn_arg.number < 0)
5796 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5797 (varnumber_T)(iptr->isn_arg.number
5798 + STACK_FRAME_SIZE));
5799 else
5800 smsg("%s%4d LOAD $%lld", pfx, current,
5801 (varnumber_T)(iptr->isn_arg.number));
5802 }
5803 break;
5804 case ISN_LOADOUTER:
5805 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005806 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005807 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5808 iptr->isn_arg.outer.outer_depth,
5809 iptr->isn_arg.outer.outer_idx
5810 + STACK_FRAME_SIZE);
5811 else
5812 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5813 iptr->isn_arg.outer.outer_depth,
5814 iptr->isn_arg.outer.outer_idx);
5815 }
5816 break;
5817 case ISN_LOADV:
5818 smsg("%s%4d LOADV v:%s", pfx, current,
5819 get_vim_var_name(iptr->isn_arg.number));
5820 break;
5821 case ISN_LOADSCRIPT:
5822 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005823 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5824 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5825 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005826
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005827 sv = get_script_svar(sref, -1);
5828 if (sv == NULL)
5829 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5830 pfx, current, si->sn_name);
5831 else
5832 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005833 sv->sv_name,
5834 sref->sref_idx,
5835 si->sn_name);
5836 }
5837 break;
5838 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005839 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005840 {
5841 scriptitem_T *si = SCRIPT_ITEM(
5842 iptr->isn_arg.loadstore.ls_sid);
5843
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005844 smsg("%s%4d %s s:%s from %s", pfx, current,
5845 iptr->isn_type == ISN_LOADS ? "LOADS"
5846 : "LOADEXPORT",
5847 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005848 }
5849 break;
5850 case ISN_LOADAUTO:
5851 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5852 break;
5853 case ISN_LOADG:
5854 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5855 break;
5856 case ISN_LOADB:
5857 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5858 break;
5859 case ISN_LOADW:
5860 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5861 break;
5862 case ISN_LOADT:
5863 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5864 break;
5865 case ISN_LOADGDICT:
5866 smsg("%s%4d LOAD g:", pfx, current);
5867 break;
5868 case ISN_LOADBDICT:
5869 smsg("%s%4d LOAD b:", pfx, current);
5870 break;
5871 case ISN_LOADWDICT:
5872 smsg("%s%4d LOAD w:", pfx, current);
5873 break;
5874 case ISN_LOADTDICT:
5875 smsg("%s%4d LOAD t:", pfx, current);
5876 break;
5877 case ISN_LOADOPT:
5878 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5879 break;
5880 case ISN_LOADENV:
5881 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5882 break;
5883 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005884 smsg("%s%4d LOADREG @%c", pfx, current,
5885 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005886 break;
5887
5888 case ISN_STORE:
5889 if (iptr->isn_arg.number < 0)
5890 smsg("%s%4d STORE arg[%lld]", pfx, current,
5891 iptr->isn_arg.number + STACK_FRAME_SIZE);
5892 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005893 smsg("%s%4d STORE $%lld", pfx, current,
5894 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005895 break;
5896 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005897 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5898 iptr->isn_arg.outer.outer_depth,
5899 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005900 break;
5901 case ISN_STOREV:
5902 smsg("%s%4d STOREV v:%s", pfx, current,
5903 get_vim_var_name(iptr->isn_arg.number));
5904 break;
5905 case ISN_STOREAUTO:
5906 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5907 break;
5908 case ISN_STOREG:
5909 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5910 break;
5911 case ISN_STOREB:
5912 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5913 break;
5914 case ISN_STOREW:
5915 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5916 break;
5917 case ISN_STORET:
5918 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5919 break;
5920 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005921 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005922 {
5923 scriptitem_T *si = SCRIPT_ITEM(
5924 iptr->isn_arg.loadstore.ls_sid);
5925
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005926 smsg("%s%4d %s %s in %s", pfx, current,
5927 iptr->isn_type == ISN_STORES
5928 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005929 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5930 }
5931 break;
5932 case ISN_STORESCRIPT:
5933 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005934 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5935 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5936 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005937
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005938 sv = get_script_svar(sref, -1);
5939 if (sv == NULL)
5940 smsg("%s%4d STORESCRIPT [deleted] in %s",
5941 pfx, current, si->sn_name);
5942 else
5943 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005944 sv->sv_name,
5945 sref->sref_idx,
5946 si->sn_name);
5947 }
5948 break;
5949 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005950 case ISN_STOREFUNCOPT:
5951 smsg("%s%4d %s &%s", pfx, current,
5952 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005953 iptr->isn_arg.storeopt.so_name);
5954 break;
5955 case ISN_STOREENV:
5956 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5957 break;
5958 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005959 smsg("%s%4d STOREREG @%c", pfx, current,
5960 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005961 break;
5962 case ISN_STORENR:
5963 smsg("%s%4d STORE %lld in $%d", pfx, current,
5964 iptr->isn_arg.storenr.stnr_val,
5965 iptr->isn_arg.storenr.stnr_idx);
5966 break;
5967
5968 case ISN_STOREINDEX:
5969 smsg("%s%4d STOREINDEX %s", pfx, current,
5970 vartype_name(iptr->isn_arg.vartype));
5971 break;
5972
5973 case ISN_STORERANGE:
5974 smsg("%s%4d STORERANGE", pfx, current);
5975 break;
5976
5977 // constants
5978 case ISN_PUSHNR:
5979 smsg("%s%4d PUSHNR %lld", pfx, current,
5980 (varnumber_T)(iptr->isn_arg.number));
5981 break;
5982 case ISN_PUSHBOOL:
5983 case ISN_PUSHSPEC:
5984 smsg("%s%4d PUSH %s", pfx, current,
5985 get_var_special_name(iptr->isn_arg.number));
5986 break;
5987 case ISN_PUSHF:
5988#ifdef FEAT_FLOAT
5989 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5990#endif
5991 break;
5992 case ISN_PUSHS:
5993 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5994 break;
5995 case ISN_PUSHBLOB:
5996 {
5997 char_u *r;
5998 char_u numbuf[NUMBUFLEN];
5999 char_u *tofree;
6000
6001 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6002 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6003 vim_free(tofree);
6004 }
6005 break;
6006 case ISN_PUSHFUNC:
6007 {
6008 char *name = (char *)iptr->isn_arg.string;
6009
6010 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6011 name == NULL ? "[none]" : name);
6012 }
6013 break;
6014 case ISN_PUSHCHANNEL:
6015#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006016 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006017#endif
6018 break;
6019 case ISN_PUSHJOB:
6020#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006021 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006022#endif
6023 break;
6024 case ISN_PUSHEXC:
6025 smsg("%s%4d PUSH v:exception", pfx, current);
6026 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006027 case ISN_AUTOLOAD:
6028 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6029 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006030 case ISN_UNLET:
6031 smsg("%s%4d UNLET%s %s", pfx, current,
6032 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6033 iptr->isn_arg.unlet.ul_name);
6034 break;
6035 case ISN_UNLETENV:
6036 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6037 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6038 iptr->isn_arg.unlet.ul_name);
6039 break;
6040 case ISN_UNLETINDEX:
6041 smsg("%s%4d UNLETINDEX", pfx, current);
6042 break;
6043 case ISN_UNLETRANGE:
6044 smsg("%s%4d UNLETRANGE", pfx, current);
6045 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006046 case ISN_LOCKUNLOCK:
6047 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6048 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006049 case ISN_LOCKCONST:
6050 smsg("%s%4d LOCKCONST", pfx, current);
6051 break;
6052 case ISN_NEWLIST:
6053 smsg("%s%4d NEWLIST size %lld", pfx, current,
6054 (varnumber_T)(iptr->isn_arg.number));
6055 break;
6056 case ISN_NEWDICT:
6057 smsg("%s%4d NEWDICT size %lld", pfx, current,
6058 (varnumber_T)(iptr->isn_arg.number));
6059 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006060 case ISN_NEWPARTIAL:
6061 smsg("%s%4d NEWPARTIAL", pfx, current);
6062 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006063
6064 // function call
6065 case ISN_BCALL:
6066 {
6067 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6068
6069 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6070 internal_func_name(cbfunc->cbf_idx),
6071 cbfunc->cbf_argcount);
6072 }
6073 break;
6074 case ISN_DCALL:
6075 {
6076 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6077 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6078 + cdfunc->cdf_idx;
6079
6080 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006081 printable_func_name(df->df_ufunc),
6082 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006083 }
6084 break;
6085 case ISN_UCALL:
6086 {
6087 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6088
6089 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6090 cufunc->cuf_name, cufunc->cuf_argcount);
6091 }
6092 break;
6093 case ISN_PCALL:
6094 {
6095 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6096
6097 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6098 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6099 }
6100 break;
6101 case ISN_PCALL_END:
6102 smsg("%s%4d PCALL end", pfx, current);
6103 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006104 case ISN_DEFER:
6105 smsg("%s%4d DEFER %d args", pfx, current,
6106 (int)iptr->isn_arg.defer.defer_argcount);
6107 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006108 case ISN_RETURN:
6109 smsg("%s%4d RETURN", pfx, current);
6110 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006111 case ISN_RETURN_VOID:
6112 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006113 break;
6114 case ISN_FUNCREF:
6115 {
6116 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00006117 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006118
Bram Moolenaar38453522021-11-28 22:00:12 +00006119 if (funcref->fr_func_name == NULL)
6120 {
6121 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6122 + funcref->fr_dfunc_idx;
6123 name = df->df_ufunc->uf_name;
6124 }
6125 else
6126 name = funcref->fr_func_name;
6127 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006128 }
6129 break;
6130
6131 case ISN_NEWFUNC:
6132 {
6133 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
6134
6135 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6136 newfunc->nf_lambda, newfunc->nf_global);
6137 }
6138 break;
6139
6140 case ISN_DEF:
6141 {
6142 char_u *name = iptr->isn_arg.string;
6143
6144 smsg("%s%4d DEF %s", pfx, current,
6145 name == NULL ? (char_u *)"" : name);
6146 }
6147 break;
6148
6149 case ISN_JUMP:
6150 {
6151 char *when = "?";
6152
6153 switch (iptr->isn_arg.jump.jump_when)
6154 {
6155 case JUMP_ALWAYS:
6156 when = "JUMP";
6157 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006158 case JUMP_NEVER:
6159 iemsg("JUMP_NEVER should not be used");
6160 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006161 case JUMP_AND_KEEP_IF_TRUE:
6162 when = "JUMP_AND_KEEP_IF_TRUE";
6163 break;
6164 case JUMP_IF_FALSE:
6165 when = "JUMP_IF_FALSE";
6166 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006167 case JUMP_IF_COND_FALSE:
6168 when = "JUMP_IF_COND_FALSE";
6169 break;
6170 case JUMP_IF_COND_TRUE:
6171 when = "JUMP_IF_COND_TRUE";
6172 break;
6173 }
6174 smsg("%s%4d %s -> %d", pfx, current, when,
6175 iptr->isn_arg.jump.jump_where);
6176 }
6177 break;
6178
6179 case ISN_JUMP_IF_ARG_SET:
6180 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6181 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6182 iptr->isn_arg.jump.jump_where);
6183 break;
6184
6185 case ISN_FOR:
6186 {
6187 forloop_T *forloop = &iptr->isn_arg.forloop;
6188
6189 smsg("%s%4d FOR $%d -> %d", pfx, current,
6190 forloop->for_idx, forloop->for_end);
6191 }
6192 break;
6193
6194 case ISN_TRY:
6195 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006196 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006197
6198 if (try->try_ref->try_finally == 0)
6199 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6200 pfx, current,
6201 try->try_ref->try_catch,
6202 try->try_ref->try_endtry);
6203 else
6204 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6205 pfx, current,
6206 try->try_ref->try_catch,
6207 try->try_ref->try_finally,
6208 try->try_ref->try_endtry);
6209 }
6210 break;
6211 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006212 smsg("%s%4d CATCH", pfx, current);
6213 break;
6214 case ISN_TRYCONT:
6215 {
6216 trycont_T *trycont = &iptr->isn_arg.trycont;
6217
6218 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6219 trycont->tct_levels,
6220 trycont->tct_levels == 1 ? "" : "s",
6221 trycont->tct_where);
6222 }
6223 break;
6224 case ISN_FINALLY:
6225 smsg("%s%4d FINALLY", pfx, current);
6226 break;
6227 case ISN_ENDTRY:
6228 smsg("%s%4d ENDTRY", pfx, current);
6229 break;
6230 case ISN_THROW:
6231 smsg("%s%4d THROW", pfx, current);
6232 break;
6233
6234 // expression operations on number
6235 case ISN_OPNR:
6236 case ISN_OPFLOAT:
6237 case ISN_OPANY:
6238 {
6239 char *what;
6240 char *ins;
6241
6242 switch (iptr->isn_arg.op.op_type)
6243 {
6244 case EXPR_MULT: what = "*"; break;
6245 case EXPR_DIV: what = "/"; break;
6246 case EXPR_REM: what = "%"; break;
6247 case EXPR_SUB: what = "-"; break;
6248 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006249 case EXPR_LSHIFT: what = "<<"; break;
6250 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006251 default: what = "???"; break;
6252 }
6253 switch (iptr->isn_type)
6254 {
6255 case ISN_OPNR: ins = "OPNR"; break;
6256 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6257 case ISN_OPANY: ins = "OPANY"; break;
6258 default: ins = "???"; break;
6259 }
6260 smsg("%s%4d %s %s", pfx, current, ins, what);
6261 }
6262 break;
6263
6264 case ISN_COMPAREBOOL:
6265 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006266 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006267 case ISN_COMPARENR:
6268 case ISN_COMPAREFLOAT:
6269 case ISN_COMPARESTRING:
6270 case ISN_COMPAREBLOB:
6271 case ISN_COMPARELIST:
6272 case ISN_COMPAREDICT:
6273 case ISN_COMPAREFUNC:
6274 case ISN_COMPAREANY:
6275 {
6276 char *p;
6277 char buf[10];
6278 char *type;
6279
6280 switch (iptr->isn_arg.op.op_type)
6281 {
6282 case EXPR_EQUAL: p = "=="; break;
6283 case EXPR_NEQUAL: p = "!="; break;
6284 case EXPR_GREATER: p = ">"; break;
6285 case EXPR_GEQUAL: p = ">="; break;
6286 case EXPR_SMALLER: p = "<"; break;
6287 case EXPR_SEQUAL: p = "<="; break;
6288 case EXPR_MATCH: p = "=~"; break;
6289 case EXPR_IS: p = "is"; break;
6290 case EXPR_ISNOT: p = "isnot"; break;
6291 case EXPR_NOMATCH: p = "!~"; break;
6292 default: p = "???"; break;
6293 }
6294 STRCPY(buf, p);
6295 if (iptr->isn_arg.op.op_ic == TRUE)
6296 strcat(buf, "?");
6297 switch(iptr->isn_type)
6298 {
6299 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6300 case ISN_COMPARESPECIAL:
6301 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006302 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006303 case ISN_COMPARENR: type = "COMPARENR"; break;
6304 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6305 case ISN_COMPARESTRING:
6306 type = "COMPARESTRING"; break;
6307 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6308 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6309 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6310 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6311 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6312 default: type = "???"; break;
6313 }
6314
6315 smsg("%s%4d %s %s", pfx, current, type, buf);
6316 }
6317 break;
6318
6319 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6320 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6321
6322 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006323 case ISN_CONCAT:
6324 smsg("%s%4d CONCAT size %lld", pfx, current,
6325 (varnumber_T)(iptr->isn_arg.number));
6326 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006327 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6328 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6329 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6330 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6331 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6332 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6333 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6334 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6335 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6336 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6337 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006338 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006339 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6340 iptr->isn_arg.getitem.gi_index,
6341 iptr->isn_arg.getitem.gi_with_op ?
6342 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006343 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6344 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6345 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006346 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6347 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6348
Bram Moolenaar4c137212021-04-19 16:48:48 +02006349 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6350
Bram Moolenaar4c137212021-04-19 16:48:48 +02006351 case ISN_CHECKTYPE:
6352 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006353 checktype_T *ct = &iptr->isn_arg.type;
6354 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006355
6356 if (ct->ct_arg_idx == 0)
6357 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6358 type_name(ct->ct_type, &tofree),
6359 (int)ct->ct_off);
6360 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006361 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006362 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006363 type_name(ct->ct_type, &tofree),
6364 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006365 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006366 (int)ct->ct_arg_idx);
6367 vim_free(tofree);
6368 break;
6369 }
6370 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6371 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6372 iptr->isn_arg.checklen.cl_min_len);
6373 break;
6374 case ISN_SETTYPE:
6375 {
6376 char *tofree;
6377
6378 smsg("%s%4d SETTYPE %s", pfx, current,
6379 type_name(iptr->isn_arg.type.ct_type, &tofree));
6380 vim_free(tofree);
6381 break;
6382 }
6383 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006384 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6385 smsg("%s%4d INVERT %d (!val)", pfx, current,
6386 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006387 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006388 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6389 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006390 break;
6391 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006392 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006393 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006394 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6395 pfx, current,
6396 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006397 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006398 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6399 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006400 break;
6401 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006402 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006403 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006404 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006405 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6406 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006407 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006408 else
6409 smsg("%s%4d PUT %c %ld", pfx, current,
6410 iptr->isn_arg.put.put_regname,
6411 (long)iptr->isn_arg.put.put_lnum);
6412 break;
6413
Bram Moolenaar4c137212021-04-19 16:48:48 +02006414 case ISN_CMDMOD:
6415 {
6416 char_u *buf;
6417 size_t len = produce_cmdmods(
6418 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6419
6420 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006421 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006422 {
6423 (void)produce_cmdmods(
6424 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6425 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6426 vim_free(buf);
6427 }
6428 break;
6429 }
6430 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6431
6432 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006433 smsg("%s%4d PROFILE START line %d", pfx, current,
6434 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006435 break;
6436
6437 case ISN_PROF_END:
6438 smsg("%s%4d PROFILE END", pfx, current);
6439 break;
6440
Bram Moolenaare99d4222021-06-13 14:01:26 +02006441 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006442 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6443 iptr->isn_arg.debug.dbg_break_lnum + 1,
6444 iptr->isn_lnum,
6445 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006446 break;
6447
Bram Moolenaar4c137212021-04-19 16:48:48 +02006448 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6449 iptr->isn_arg.unpack.unp_count,
6450 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6451 break;
6452 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6453 iptr->isn_arg.shuffle.shfl_item,
6454 iptr->isn_arg.shuffle.shfl_up);
6455 break;
6456 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6457
6458 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6459 return;
6460 }
6461
6462 out_flush(); // output one line at a time
6463 ui_breakcheck();
6464 if (got_int)
6465 break;
6466 }
6467}
6468
6469/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006470 * Handle command line completion for the :disassemble command.
6471 */
6472 void
6473set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6474{
6475 char_u *p;
6476
6477 // Default: expand user functions, "debug" and "profile"
6478 xp->xp_context = EXPAND_DISASSEMBLE;
6479 xp->xp_pattern = arg;
6480
6481 // first argument already typed: only user function names
6482 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6483 {
6484 xp->xp_context = EXPAND_USER_FUNC;
6485 xp->xp_pattern = skipwhite(p);
6486 }
6487}
6488
6489/*
6490 * Function given to ExpandGeneric() to obtain the list of :disassemble
6491 * arguments.
6492 */
6493 char_u *
6494get_disassemble_argument(expand_T *xp, int idx)
6495{
6496 if (idx == 0)
6497 return (char_u *)"debug";
6498 if (idx == 1)
6499 return (char_u *)"profile";
6500 return get_user_func_name(xp, idx - 2);
6501}
6502
6503/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006504 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006505 * We don't really need this at runtime, but we do have tests that require it,
6506 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006507 */
6508 void
6509ex_disassemble(exarg_T *eap)
6510{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006511 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006512 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006514 isn_T *instr = NULL; // init to shut up gcc warning
6515 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006516 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006517
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006518 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006519 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006521 if (func_needs_compiling(ufunc, compile_type)
6522 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006523 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006524 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006526 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527 return;
6528 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006529 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006530
6531 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006532 switch (compile_type)
6533 {
6534 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006535#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006536 instr = dfunc->df_instr_prof;
6537 instr_count = dfunc->df_instr_prof_count;
6538 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006539#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006540 // FALLTHROUGH
6541 case CT_NONE:
6542 instr = dfunc->df_instr;
6543 instr_count = dfunc->df_instr_count;
6544 break;
6545 case CT_DEBUG:
6546 instr = dfunc->df_instr_debug;
6547 instr_count = dfunc->df_instr_debug_count;
6548 break;
6549 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550
Bram Moolenaar4c137212021-04-19 16:48:48 +02006551 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552}
6553
6554/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006555 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556 * list, etc. Mostly like what JavaScript does, except that empty list and
6557 * empty dictionary are FALSE.
6558 */
6559 int
6560tv2bool(typval_T *tv)
6561{
6562 switch (tv->v_type)
6563 {
6564 case VAR_NUMBER:
6565 return tv->vval.v_number != 0;
6566 case VAR_FLOAT:
6567#ifdef FEAT_FLOAT
6568 return tv->vval.v_float != 0.0;
6569#else
6570 break;
6571#endif
6572 case VAR_PARTIAL:
6573 return tv->vval.v_partial != NULL;
6574 case VAR_FUNC:
6575 case VAR_STRING:
6576 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6577 case VAR_LIST:
6578 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6579 case VAR_DICT:
6580 return tv->vval.v_dict != NULL
6581 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6582 case VAR_BOOL:
6583 case VAR_SPECIAL:
6584 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6585 case VAR_JOB:
6586#ifdef FEAT_JOB_CHANNEL
6587 return tv->vval.v_job != NULL;
6588#else
6589 break;
6590#endif
6591 case VAR_CHANNEL:
6592#ifdef FEAT_JOB_CHANNEL
6593 return tv->vval.v_channel != NULL;
6594#else
6595 break;
6596#endif
6597 case VAR_BLOB:
6598 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6599 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006600 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006602 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 break;
6604 }
6605 return FALSE;
6606}
6607
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006608 void
6609emsg_using_string_as(typval_T *tv, int as_number)
6610{
6611 semsg(_(as_number ? e_using_string_as_number_str
6612 : e_using_string_as_bool_str),
6613 tv->vval.v_string == NULL
6614 ? (char_u *)"" : tv->vval.v_string);
6615}
6616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617/*
6618 * If "tv" is a string give an error and return FAIL.
6619 */
6620 int
6621check_not_string(typval_T *tv)
6622{
6623 if (tv->v_type == VAR_STRING)
6624 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006625 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626 clear_tv(tv);
6627 return FAIL;
6628 }
6629 return OK;
6630}
6631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632
6633#endif // FEAT_EVAL