blob: c1c2e124a88a285e6c89c82cc38409b37b381dc7 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010067// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100104// Get pointer to item in the stack.
105#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200108#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100110// Get pointer to a local variable on the stack. Negative for arguments.
111#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
112
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200113 void
114to_string_error(vartype_T vartype)
115{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200116 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200117}
118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100120 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 */
122 static int
123ufunc_argcount(ufunc_T *ufunc)
124{
125 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
126}
127
128/*
LemonBoy372bcce2022-04-25 12:43:20 +0100129 * Create a new string from "count" items at the bottom of the stack.
130 * A trailing NUL is appended.
131 * When "count" is zero an empty string is added to the stack.
132 */
133 static int
134exe_concat(int count, ectx_T *ectx)
135{
136 int idx;
137 int len = 0;
138 typval_T *tv;
139 garray_T ga;
140
141 ga_init2(&ga, sizeof(char), 1);
142 // Preallocate enough space for the whole string to avoid having to grow
143 // and copy.
144 for (idx = 0; idx < count; ++idx)
145 {
146 tv = STACK_TV_BOT(idx - count);
147 if (tv->vval.v_string != NULL)
148 len += (int)STRLEN(tv->vval.v_string);
149 }
150
151 if (ga_grow(&ga, len + 1) == FAIL)
152 return FAIL;
153
154 for (idx = 0; idx < count; ++idx)
155 {
156 tv = STACK_TV_BOT(idx - count);
157 ga_concat(&ga, tv->vval.v_string);
158 clear_tv(tv);
159 }
160
161 // add a terminating NUL
162 ga_append(&ga, NUL);
163
164 ectx->ec_stack.ga_len -= count - 1;
165 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
166
167 return OK;
168}
169
170/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200171 * Create a new list from "count" items at the bottom of the stack.
172 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100173 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200174 */
175 static int
176exe_newlist(int count, ectx_T *ectx)
177{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 int idx;
180 typval_T *tv;
181
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100182 if (count >= 0)
183 {
184 list = list_alloc_with_items(count);
185 if (list == NULL)
186 return FAIL;
187 for (idx = 0; idx < count; ++idx)
188 list_set_item(list, idx, STACK_TV_BOT(idx - count));
189 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190
191 if (count > 0)
192 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200193 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100194 {
195 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 else
199 ++ectx->ec_stack.ga_len;
200 tv = STACK_TV_BOT(-1);
201 tv->v_type = VAR_LIST;
202 tv->vval.v_list = list;
Bram Moolenaar566badc2022-09-18 13:46:08 +0100203 tv->v_lock = 0;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100204 if (list != NULL)
205 ++list->lv_refcount;
206 return OK;
207}
208
209/*
210 * Implementation of ISN_NEWDICT.
211 * Returns FAIL on total failure, MAYBE on error.
212 */
213 static int
214exe_newdict(int count, ectx_T *ectx)
215{
216 dict_T *dict = NULL;
217 dictitem_T *item;
218 char_u *key;
219 int idx;
220 typval_T *tv;
221
222 if (count >= 0)
223 {
224 dict = dict_alloc();
225 if (unlikely(dict == NULL))
226 return FAIL;
227 for (idx = 0; idx < count; ++idx)
228 {
229 // have already checked key type is VAR_STRING
230 tv = STACK_TV_BOT(2 * (idx - count));
231 // check key is unique
232 key = tv->vval.v_string == NULL
233 ? (char_u *)"" : tv->vval.v_string;
234 item = dict_find(dict, key, -1);
235 if (item != NULL)
236 {
237 semsg(_(e_duplicate_key_in_dicitonary), key);
238 dict_unref(dict);
239 return MAYBE;
240 }
241 item = dictitem_alloc(key);
242 clear_tv(tv);
243 if (unlikely(item == NULL))
244 {
245 dict_unref(dict);
246 return FAIL;
247 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100248 tv = STACK_TV_BOT(2 * (idx - count) + 1);
249 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100250 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100251 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100252 if (dict_add(dict, item) == FAIL)
253 {
254 // can this ever happen?
255 dict_unref(dict);
256 return FAIL;
257 }
258 }
259 }
260
261 if (count > 0)
262 ectx->ec_stack.ga_len -= 2 * count - 1;
263 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
264 return FAIL;
265 else
266 ++ectx->ec_stack.ga_len;
267 tv = STACK_TV_BOT(-1);
268 tv->v_type = VAR_DICT;
269 tv->v_lock = 0;
270 tv->vval.v_dict = dict;
271 if (dict != NULL)
272 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200273 return OK;
274}
275
276/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200277 * If debug_tick changed check if "ufunc" has a breakpoint and update
278 * "uf_has_breakpoint".
279 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000280 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200281update_has_breakpoint(ufunc_T *ufunc)
282{
283 if (ufunc->uf_debug_tick != debug_tick)
284 {
285 linenr_T breakpoint;
286
287 ufunc->uf_debug_tick = debug_tick;
288 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
289 ufunc->uf_has_breakpoint = breakpoint > 0;
290 }
291}
292
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200293static garray_T dict_stack = GA_EMPTY;
294
295/*
296 * Put a value on the dict stack. This consumes "tv".
297 */
298 static int
299dict_stack_save(typval_T *tv)
300{
301 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000302 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200303 if (ga_grow(&dict_stack, 1) == FAIL)
304 return FAIL;
305 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
306 ++dict_stack.ga_len;
307 return OK;
308}
309
310/*
311 * Get the typval at top of the dict stack.
312 */
313 static typval_T *
314dict_stack_get_tv(void)
315{
316 if (dict_stack.ga_len == 0)
317 return NULL;
318 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
319}
320
321/*
322 * Get the dict at top of the dict stack.
323 */
324 static dict_T *
325dict_stack_get_dict(void)
326{
327 typval_T *tv;
328
329 if (dict_stack.ga_len == 0)
330 return NULL;
331 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
332 if (tv->v_type == VAR_DICT)
333 return tv->vval.v_dict;
334 return NULL;
335}
336
337/*
338 * Drop an item from the dict stack.
339 */
340 static void
341dict_stack_drop(void)
342{
343 if (dict_stack.ga_len == 0)
344 {
345 iemsg("Dict stack underflow");
346 return;
347 }
348 --dict_stack.ga_len;
349 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
350}
351
352/*
353 * Drop items from the dict stack until the length is equal to "len".
354 */
355 static void
356dict_stack_clear(int len)
357{
358 while (dict_stack.ga_len > len)
359 dict_stack_drop();
360}
361
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200362/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000363 * Get a pointer to useful "pt_outer" of "pt".
364 */
365 static outer_T *
366get_pt_outer(partial_T *pt)
367{
368 partial_T *ptref = pt->pt_outer_partial;
369
370 if (ptref == NULL)
371 return &pt->pt_outer;
372
373 // partial using partial (recursively)
374 while (ptref->pt_outer_partial != NULL)
375 ptref = ptref->pt_outer_partial;
376 return &ptref->pt_outer;
377}
378
379/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100380 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100381 * This adds a stack frame and sets the instruction pointer to the start of the
382 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200383 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 *
385 * Stack has:
386 * - current arguments (already there)
387 * - omitted optional argument (default values) added here
388 * - stack frame:
389 * - pointer to calling function
390 * - Index of next instruction in calling function
391 * - previous frame pointer
392 * - reserved space for local variables
393 */
394 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100395call_dfunc(
396 int cdf_idx,
397 partial_T *pt,
398 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100399 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100401 int argcount = argcount_arg;
402 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
403 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200404 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100405 int arg_to_add;
406 int vararg_count = 0;
407 int varcount;
408 int idx;
409 estack_T *entry;
410 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200411 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000412 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413
414 if (dfunc->df_deleted)
415 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100416 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000417 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100418 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 return FAIL;
420 }
421
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100422#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100423 if (do_profiling == PROF_YES)
424 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200425 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100426 {
427 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
428 + profile_info_ga.ga_len;
429 ++profile_info_ga.ga_len;
430 CLEAR_POINTER(info);
431 profile_may_start_func(info, ufunc,
432 (((dfunc_T *)def_functions.ga_data)
433 + ectx->ec_dfunc_idx)->df_ufunc);
434 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100435 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100436#endif
437
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200438 // When debugging and using "cont" switches to the not-debugged
439 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000440 compile_type = get_compile_type(ufunc);
441 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200442 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000443 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200444
445 // compile_def_function() may cause def_functions.ga_data to change
446 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
447 }
448 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200449 {
450 if (did_emsg_cumul + did_emsg == did_emsg_before)
451 semsg(_(e_function_is_not_compiled_str),
452 printable_func_name(ufunc));
453 return FAIL;
454 }
455
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200456 if (ufunc->uf_va_name != NULL)
457 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200458 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200459 // Stack at time of call with 2 varargs:
460 // normal_arg
461 // optional_arg
462 // vararg_1
463 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200464 // After creating the list:
465 // normal_arg
466 // optional_arg
467 // vararg-list
468 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200469 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200470 // After creating the list
471 // normal_arg
472 // (space for optional_arg)
473 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200474 vararg_count = argcount - ufunc->uf_args.ga_len;
475 if (vararg_count < 0)
476 vararg_count = 0;
477 else
478 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200479 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200480 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200481
482 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200483 }
484
Bram Moolenaarfe270812020-04-11 22:31:27 +0200485 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200486 if (arg_to_add < 0)
487 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100488 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
489 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200490 return FAIL;
491 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000492 else if (arg_to_add > ufunc->uf_def_args.ga_len)
493 {
494 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
495
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100496 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
497 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000498 return FAIL;
499 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200500
501 // Reserve space for:
502 // - missing arguments
503 // - stack frame
504 // - local variables
505 // - if needed: a counter for number of closures created in
506 // ectx->ec_funcrefs.
507 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100508 if (GA_GROW_FAILS(&ectx->ec_stack,
509 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100510 return FAIL;
511
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100512 // If depth of calling is getting too high, don't execute the function.
513 if (funcdepth_increment() == FAIL)
514 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200515 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100516
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100517 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200518 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100519 {
520 floc = ALLOC_ONE(funclocal_T);
521 if (floc == NULL)
522 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200523 *floc = ectx->ec_funclocal;
524 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100525 }
526
Bram Moolenaarfe270812020-04-11 22:31:27 +0200527 // Move the vararg-list to below the missing optional arguments.
528 if (vararg_count > 0 && arg_to_add > 0)
529 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100530
531 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200532 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200533 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200534 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535
536 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100537 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
538 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200539 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200540 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
541 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100542 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100543 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200544 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545
546 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200547 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000548 {
549 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
550
551 tv->v_type = VAR_NUMBER;
552 tv->vval.v_number = 0;
553 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200554 if (dfunc->df_has_closure)
555 {
556 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
557
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100558 // Initialize the variable that counts how many closures were created.
559 // This is used in handle_closure_in_use().
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200560 tv->v_type = VAR_NUMBER;
561 tv->vval.v_number = 0;
562 }
563 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100564
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100565 if (pt != NULL || ufunc->uf_partial != NULL
566 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100567 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200568 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100569
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200570 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100571 return FAIL;
572 if (pt != NULL)
573 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000574 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200575 ++pt->pt_refcount;
576 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100577 }
578 else if (ufunc->uf_partial != NULL)
579 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000580 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200581 ++ufunc->uf_partial->pt_refcount;
582 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100583 }
584 else
585 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200586 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200587 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200588 {
589 vim_free(ref);
590 return FAIL;
591 }
592 ref->or_outer_allocated = TRUE;
593 ref->or_outer->out_stack = &ectx->ec_stack;
594 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
595 if (ectx->ec_outer_ref != NULL)
596 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100597 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200598 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100599 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100600 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200601 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100602
Bram Moolenaarc970e422021-03-17 15:03:04 +0100603 ++ufunc->uf_calls;
604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605 // Set execution state to the start of the called function.
606 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100607 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100608 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200609 if (entry != NULL)
610 {
611 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200612 // Save the current context so it can be restored on return.
613 entry->es_save_sctx = current_sctx;
614 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200615 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100616
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200617 // Start execution at the first instruction.
618 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619
620 return OK;
621}
622
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000623// Double linked list of funcstack_T in use.
624static funcstack_T *first_funcstack = NULL;
625
626 static void
627add_funcstack_to_list(funcstack_T *funcstack)
628{
629 // Link in list of funcstacks.
630 if (first_funcstack != NULL)
631 first_funcstack->fs_prev = funcstack;
632 funcstack->fs_next = first_funcstack;
633 funcstack->fs_prev = NULL;
634 first_funcstack = funcstack;
635}
636
637 static void
638remove_funcstack_from_list(funcstack_T *funcstack)
639{
640 if (funcstack->fs_prev == NULL)
641 first_funcstack = funcstack->fs_next;
642 else
643 funcstack->fs_prev->fs_next = funcstack->fs_next;
644 if (funcstack->fs_next != NULL)
645 funcstack->fs_next->fs_prev = funcstack->fs_prev;
646}
647
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100648/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200649 * Used when returning from a function: Check if any closure is still
650 * referenced. If so then move the arguments and variables to a separate piece
651 * of stack to be used when the closure is called.
652 * When "free_arguments" is TRUE the arguments are to be freed.
653 * Returns FAIL when out of memory.
654 */
655 static int
656handle_closure_in_use(ectx_T *ectx, int free_arguments)
657{
658 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
659 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200660 int argcount;
661 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200662 int idx;
663 typval_T *tv;
664 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200665 garray_T *gap = &ectx->ec_funcrefs;
666 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200667
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200668 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200669 return OK; // function was freed
670 if (dfunc->df_has_closure == 0)
671 return OK; // no closures
672 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
673 closure_count = tv->vval.v_number;
674 if (closure_count == 0)
675 return OK; // no funcrefs created
676
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100677 // Compute "top": the first entry in the stack used by the function.
678 // This is the first argument (after that comes the stack frame and then
679 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200680 argcount = ufunc_argcount(dfunc->df_ufunc);
681 top = ectx->ec_frame_idx - argcount;
682
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200683 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200684 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200685 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200686 partial_T *pt;
687 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200688
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200689 if (off < 0)
690 continue; // count is off or already done
691 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200692 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200693 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200694 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200695 int i;
696
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000697 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200698 // unreferenced on return.
699 for (i = 0; i < dfunc->df_varcount; ++i)
700 {
701 typval_T *stv = STACK_TV(ectx->ec_frame_idx
702 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200703 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200704 --refcount;
705 }
706 if (refcount > 1)
707 {
708 closure_in_use = TRUE;
709 break;
710 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200711 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200712 }
713
714 if (closure_in_use)
715 {
716 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
717 typval_T *stack;
718
719 // A closure is using the arguments and/or local variables.
720 // Move them to the called function.
721 if (funcstack == NULL)
722 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000723
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200724 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
725 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200726 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
727 funcstack->fs_ga.ga_data = stack;
728 if (stack == NULL)
729 {
730 vim_free(funcstack);
731 return FAIL;
732 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000733 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200734
735 // Move or copy the arguments.
736 for (idx = 0; idx < argcount; ++idx)
737 {
738 tv = STACK_TV(top + idx);
739 if (free_arguments)
740 {
741 *(stack + idx) = *tv;
742 tv->v_type = VAR_UNKNOWN;
743 }
744 else
745 copy_tv(tv, stack + idx);
746 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100747 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200748 // Move the local variables.
749 for (idx = 0; idx < dfunc->df_varcount; ++idx)
750 {
751 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200752
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200753 // A partial created for a local function, that is also used as a
754 // local variable, has a reference count for the variable, thus
755 // will never go down to zero. When all these refcounts are one
756 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000757 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200758 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
759 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200760 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200761
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200762 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200763 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
764 gap->ga_len - closure_count + i])
765 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200766 }
767
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200768 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200769 tv->v_type = VAR_UNKNOWN;
770 }
771
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200772 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200773 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200774 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
775 - closure_count + idx];
776 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200777 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200778 ++funcstack->fs_refcount;
779 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100780 pt->pt_outer.out_stack = &funcstack->fs_ga;
781 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200782 }
783 }
784 }
785
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200786 for (idx = 0; idx < closure_count; ++idx)
787 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
788 - closure_count + idx]);
789 gap->ga_len -= closure_count;
790 if (gap->ga_len == 0)
791 ga_clear(gap);
792
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200793 return OK;
794}
795
796/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200797 * Called when a partial is freed or its reference count goes down to one. The
798 * funcstack may be the only reference to the partials in the local variables.
799 * Go over all of them, the funcref and can be freed if all partials
800 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100801 * Returns TRUE if the funcstack is freed, the partial referencing it will then
802 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200803 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100804 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200805funcstack_check_refcount(funcstack_T *funcstack)
806{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100807 int i;
808 garray_T *gap = &funcstack->fs_ga;
809 int done = 0;
810 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200811
812 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100813 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200814 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
815 {
816 typval_T *tv = ((typval_T *)gap->ga_data) + i;
817
818 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
819 && tv->vval.v_partial->pt_funcstack == funcstack
820 && tv->vval.v_partial->pt_refcount == 1)
821 ++done;
822 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100823 if (done != funcstack->fs_min_refcount)
824 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200825
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100826 stack = gap->ga_data;
827
828 // All partials referencing the funcstack have a reference count of
829 // one, thus the funcstack is no longer of use.
830 for (i = 0; i < gap->ga_len; ++i)
831 clear_tv(stack + i);
832 vim_free(stack);
833 remove_funcstack_from_list(funcstack);
834 vim_free(funcstack);
835
836 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200837}
838
839/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000840 * For garbage collecting: set references in all variables referenced by
841 * all funcstacks.
842 */
843 int
844set_ref_in_funcstacks(int copyID)
845{
846 funcstack_T *funcstack;
847
848 for (funcstack = first_funcstack; funcstack != NULL;
849 funcstack = funcstack->fs_next)
850 {
851 typval_T *stack = funcstack->fs_ga.ga_data;
852 int i;
853
854 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
855 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
856 return TRUE; // abort
857 }
858 return FALSE;
859}
860
Bram Moolenaar806a2732022-09-04 15:40:36 +0100861// Ugly static to avoid passing the execution context around through many
862// layers.
863static ectx_T *current_ectx = NULL;
864
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000865/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100866 * Return TRUE if currently executing a :def function.
867 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100868 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100869 int
870in_def_function(void)
871{
872 return current_ectx != NULL;
873}
874
875/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100876 * Clear "current_ectx" and return the previous value. To be used when calling
877 * a user function.
878 */
879 ectx_T *
880clear_currrent_ectx(void)
881{
882 ectx_T *r = current_ectx;
883
884 current_ectx = NULL;
885 return r;
886}
887
888 void
889restore_current_ectx(ectx_T *ectx)
890{
891 if (current_ectx != NULL)
892 iemsg("Restoring current_ectx while it is not NULL");
893 current_ectx = ectx;
894}
895
896/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100897 * Add an entry for a deferred function call to the currently executing
898 * function.
899 * Return the list or NULL when failed.
900 */
901 static list_T *
902add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100903{
904 typval_T *defer_tv = STACK_TV_VAR(var_idx);
905 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100906 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100907 typval_T listval;
908
909 if (defer_tv->v_type != VAR_LIST)
910 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100911 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100912 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100913 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100914 }
915 defer_l = defer_tv->vval.v_list;
916
917 l = list_alloc_with_items(argcount + 1);
918 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100919 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100920 listval.v_type = VAR_LIST;
921 listval.vval.v_list = l;
922 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100923 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100924 {
925 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100926 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100927 }
928
Bram Moolenaar806a2732022-09-04 15:40:36 +0100929 return l;
930}
931
932/*
933 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
934 * The local variable that lists deferred functions is "var_idx".
935 * Returns OK or FAIL.
936 */
937 static int
938defer_command(int var_idx, int argcount, ectx_T *ectx)
939{
940 list_T *l = add_defer_item(var_idx, argcount, ectx);
941 int i;
942 typval_T *func_tv;
943
944 if (l == NULL)
945 return FAIL;
946
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100947 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100948 if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL)
949 {
950 semsg(_(e_expected_str_but_got_str),
951 "function or partial",
952 vartype_name(func_tv->v_type));
953 return FAIL;
954 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100955 list_set_item(l, 0, func_tv);
956
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100957 for (i = 0; i < argcount; ++i)
958 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100959 ectx->ec_stack.ga_len -= argcount + 1;
960 return OK;
961}
962
963/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100964 * Add a deferred function "name" with one argument "arg_tv".
965 * Consumes "name", also on failure.
966 * Only to be called when in_def_function() returns TRUE.
967 */
968 int
969add_defer_function(char_u *name, int argcount, typval_T *argvars)
970{
971 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
972 + current_ectx->ec_dfunc_idx;
973 list_T *l;
974 typval_T func_tv;
975 int i;
976
977 if (dfunc->df_defer_var_idx == 0)
978 {
979 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100980 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100981 return FAIL;
982 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100983
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100984 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100985 if (l == NULL)
986 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100987 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100988 return FAIL;
989 }
990
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100991 func_tv.v_type = VAR_FUNC;
992 func_tv.v_lock = 0;
993 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100994 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100995
Bram Moolenaar806a2732022-09-04 15:40:36 +0100996 for (i = 0; i < argcount; ++i)
997 list_set_item(l, i + 1, argvars + i);
998 return OK;
999}
1000
1001/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001002 * Invoked when returning from a function: Invoke any deferred calls.
1003 */
1004 static void
1005invoke_defer_funcs(ectx_T *ectx)
1006{
1007 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1008 + ectx->ec_dfunc_idx;
1009 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1010 listitem_T *li;
1011
1012 if (defer_tv->v_type != VAR_LIST)
1013 return; // no function added
1014 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1015 {
1016 list_T *l = li->li_tv.vval.v_list;
1017 typval_T rettv;
1018 typval_T argvars[MAX_FUNC_ARGS];
1019 int i;
1020 listitem_T *arg_li = l->lv_first;
1021 funcexe_T funcexe;
1022
1023 for (i = 0; i < l->lv_len - 1; ++i)
1024 {
1025 arg_li = arg_li->li_next;
1026 argvars[i] = arg_li->li_tv;
1027 }
1028
1029 CLEAR_FIELD(funcexe);
1030 funcexe.fe_evaluate = TRUE;
1031 rettv.v_type = VAR_UNKNOWN;
1032 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1033 &rettv, l->lv_len - 1, argvars, &funcexe);
1034 clear_tv(&rettv);
1035 }
1036}
1037
1038/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 * Return from the current function.
1040 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001041 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001042func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001045 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001046 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1047 + ectx->ec_dfunc_idx;
1048 int argcount = ufunc_argcount(dfunc->df_ufunc);
1049 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001050 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001051 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1052 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001053 funclocal_T *floc;
1054#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001055 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1056 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057
Bram Moolenaar12d26532021-02-19 19:13:21 +01001058 if (do_profiling == PROF_YES)
1059 {
1060 ufunc_T *caller = prev_dfunc->df_ufunc;
1061
1062 if (dfunc->df_ufunc->uf_profiling
1063 || (caller != NULL && caller->uf_profiling))
1064 {
1065 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1066 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1067 --profile_info_ga.ga_len;
1068 }
1069 }
1070#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001071
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001072 if (dfunc->df_defer_var_idx > 0)
1073 invoke_defer_funcs(ectx);
1074
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001075 // No check for uf_refcount being zero, cannot think of a way that would
1076 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001077 --dfunc->df_ufunc->uf_calls;
1078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001080 entry = estack_pop();
1081 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001082 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001084 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1085 return FAIL;
1086
1087 // Clear the arguments.
1088 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1089 clear_tv(STACK_TV(idx));
1090
1091 // Clear local variables and temp values, but not the return value.
1092 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001093 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001095
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001096 // The return value should be on top of the stack. However, when aborting
1097 // it may not be there and ec_frame_idx is the top of the stack.
1098 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001099 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001100 ret_idx = 0;
1101
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001102 if (ectx->ec_outer_ref != NULL)
1103 {
1104 if (ectx->ec_outer_ref->or_outer_allocated)
1105 vim_free(ectx->ec_outer_ref->or_outer);
1106 partial_unref(ectx->ec_outer_ref->or_partial);
1107 vim_free(ectx->ec_outer_ref);
1108 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001109
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001110 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001111 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001112 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1113 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001114 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1115 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001116 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001117 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001118 floc = (void *)STACK_TV(ectx->ec_frame_idx
1119 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001120 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001121 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1122 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001123
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001124 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001125 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001126 else
1127 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001128 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001129 vim_free(floc);
1130 }
1131
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001132 if (ret_idx > 0)
1133 {
1134 // Reset the stack to the position before the call, with a spot for the
1135 // return value, moved there from above the frame.
1136 ectx->ec_stack.ga_len = top + 1;
1137 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1138 }
1139 else
1140 // Reset the stack to the position before the call.
1141 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001142
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001143 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001144 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001145 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146}
1147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148/*
1149 * Prepare arguments and rettv for calling a builtin or user function.
1150 */
1151 static int
1152call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1153{
1154 int idx;
1155 typval_T *tv;
1156
1157 // Move arguments from bottom of the stack to argvars[] and add terminator.
1158 for (idx = 0; idx < argcount; ++idx)
1159 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1160 argvars[argcount].v_type = VAR_UNKNOWN;
1161
1162 // Result replaces the arguments on the stack.
1163 if (argcount > 0)
1164 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001165 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 return FAIL;
1167 else
1168 ++ectx->ec_stack.ga_len;
1169
1170 // Default return value is zero.
1171 tv = STACK_TV_BOT(-1);
1172 tv->v_type = VAR_NUMBER;
1173 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001174 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175
1176 return OK;
1177}
1178
1179/*
1180 * Call a builtin function by index.
1181 */
1182 static int
1183call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1184{
1185 typval_T argvars[MAX_FUNC_ARGS];
1186 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001187 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001188 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001189 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190
1191 if (call_prepare(argcount, argvars, ectx) == FAIL)
1192 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001193 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001195 // Call the builtin function. Set "current_ectx" so that when it
1196 // recursively invokes call_def_function() a closure context can be set.
1197 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001199 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001200 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201
1202 // Clear the arguments.
1203 for (idx = 0; idx < argcount; ++idx)
1204 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001205
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001206 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001207 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 return OK;
1209}
1210
1211/*
1212 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001213 * If the function is compiled this will add a stack frame and set the
1214 * instruction pointer at the start of the function.
1215 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001216 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001217 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 */
1219 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001220call_ufunc(
1221 ufunc_T *ufunc,
1222 partial_T *pt,
1223 int argcount,
1224 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001225 isn_T *iptr,
1226 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227{
1228 typval_T argvars[MAX_FUNC_ARGS];
1229 funcexe_T funcexe;
1230 int error;
1231 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001232 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001233 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234
Bram Moolenaare99d4222021-06-13 14:01:26 +02001235 if (func_needs_compiling(ufunc, compile_type)
1236 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1237 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001238 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001239 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001240 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001241 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001242 if (error != FCERR_UNKNOWN)
1243 {
1244 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001245 semsg(_(e_too_many_arguments_for_function_str),
1246 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001247 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001248 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001249 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001250 return FAIL;
1251 }
1252
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001253 // The function has been compiled, can call it quickly. For a function
1254 // that was defined later: we can call it directly next time.
1255 if (iptr != NULL)
1256 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001257 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001258 iptr->isn_type = ISN_DCALL;
1259 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1260 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1261 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001262 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264
1265 if (call_prepare(argcount, argvars, ectx) == FAIL)
1266 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001267 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001268 funcexe.fe_evaluate = TRUE;
1269 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270
1271 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001273 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274
1275 // Clear the arguments.
1276 for (idx = 0; idx < argcount; ++idx)
1277 clear_tv(&argvars[idx]);
1278
1279 if (error != FCERR_NONE)
1280 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001281 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 return FAIL;
1283 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001284 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001285 // Error other than from calling the function itself.
1286 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 return OK;
1288}
1289
1290/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001291 * If command modifiers were applied restore them.
1292 */
1293 static void
1294may_restore_cmdmod(funclocal_T *funclocal)
1295{
1296 if (funclocal->floc_restore_cmdmod)
1297 {
1298 cmdmod.cmod_filter_regmatch.regprog = NULL;
1299 undo_cmdmod(&cmdmod);
1300 cmdmod = funclocal->floc_save_cmdmod;
1301 funclocal->floc_restore_cmdmod = FALSE;
1302 }
1303}
1304
1305/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001306 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1307 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001308 */
1309 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001310vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001311{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001312 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001313}
1314
1315/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 * Execute a function by "name".
1317 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001318 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 * Returns FAIL if not found without an error message.
1320 */
1321 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001322call_by_name(
1323 char_u *name,
1324 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001325 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001326 isn_T *iptr,
1327 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328{
1329 ufunc_T *ufunc;
1330
1331 if (builtin_function(name, -1))
1332 {
1333 int func_idx = find_internal_func(name);
1334
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001335 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001337 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 return FAIL;
1339 return call_bfunc(func_idx, argcount, ectx);
1340 }
1341
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001342 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001343
1344 if (ufunc == NULL)
1345 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001346 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001347
1348 if (script_autoload(name, TRUE))
1349 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001350 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001351
1352 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001353 return FAIL; // bail out if loading the script caused an error
1354 }
1355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001357 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001358 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001359 {
1360 int i;
1361 typval_T *argv = STACK_TV_BOT(0) - argcount;
1362
1363 // The function can change at runtime, check that the argument
1364 // types are correct.
1365 for (i = 0; i < argcount; ++i)
1366 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001367 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001368
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001369 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001370 type = ufunc->uf_arg_types[i];
1371 else if (ufunc->uf_va_type != NULL)
1372 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001373 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001374 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001375 return FAIL;
1376 }
1377 }
1378
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001379 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001380 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381
1382 return FAIL;
1383}
1384
1385 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001386call_partial(
1387 typval_T *tv,
1388 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001389 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001391 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001392 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001394 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001395 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396
1397 if (tv->v_type == VAR_PARTIAL)
1398 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001399 partial_T *pt = tv->vval.v_partial;
1400 int i;
1401
1402 if (pt->pt_argc > 0)
1403 {
1404 // Make space for arguments from the partial, shift the "argcount"
1405 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001406 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001407 return FAIL;
1408 for (i = 1; i <= argcount; ++i)
1409 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1410 ectx->ec_stack.ga_len += pt->pt_argc;
1411 argcount += pt->pt_argc;
1412
1413 // copy the arguments from the partial onto the stack
1414 for (i = 0; i < pt->pt_argc; ++i)
1415 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1416 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001417 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418
1419 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001420 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422 name = pt->pt_name;
1423 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001424 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001426 if (name != NULL)
1427 {
1428 char_u fname_buf[FLEN_FIXED + 1];
1429 char_u *tofree = NULL;
1430 int error = FCERR_NONE;
1431 char_u *fname;
1432
1433 // May need to translate <SNR>123_ to K_SNR.
1434 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1435 if (error != FCERR_NONE)
1436 res = FAIL;
1437 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001438 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001439 vim_free(tofree);
1440 }
1441
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001442 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 {
1444 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001445 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001446 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 return FAIL;
1448 }
1449 return OK;
1450}
1451
1452/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001453 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1454 * TRUE.
1455 */
1456 static int
1457error_if_locked(int lock, char *error)
1458{
1459 if (lock & (VAR_LOCKED | VAR_FIXED))
1460 {
1461 emsg(_(error));
1462 return TRUE;
1463 }
1464 return FALSE;
1465}
1466
1467/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001468 * Give an error if "tv" is not a number and return FAIL.
1469 */
1470 static int
1471check_for_number(typval_T *tv)
1472{
1473 if (tv->v_type != VAR_NUMBER)
1474 {
1475 semsg(_(e_expected_str_but_got_str),
1476 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1477 return FAIL;
1478 }
1479 return OK;
1480}
1481
1482/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001483 * Store "tv" in variable "name".
1484 * This is for s: and g: variables.
1485 */
1486 static void
1487store_var(char_u *name, typval_T *tv)
1488{
1489 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001490 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001491
Bram Moolenaard877a572021-04-01 19:42:48 +02001492 if (tv->v_lock)
1493 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001494 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001495 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001496 restore_funccal();
1497}
1498
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001499/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001500 * Convert "tv" to a string.
1501 * Return FAIL if not allowed.
1502 */
1503 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001504do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001505{
1506 if (tv->v_type != VAR_STRING)
1507 {
1508 char_u *str;
1509
1510 if (is_2string_any)
1511 {
1512 switch (tv->v_type)
1513 {
1514 case VAR_SPECIAL:
1515 case VAR_BOOL:
1516 case VAR_NUMBER:
1517 case VAR_FLOAT:
1518 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001519
1520 case VAR_LIST:
1521 if (tolerant)
1522 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001523 char_u *s, *e, *p;
1524 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001525
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001526 ga_init2(&ga, sizeof(char_u *), 1);
1527
1528 // Convert to NL separated items, then
1529 // escape the items and replace the NL with
1530 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001531 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001532 if (str == NULL)
1533 return FAIL;
1534 s = str;
1535 while ((e = vim_strchr(s, '\n')) != NULL)
1536 {
1537 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001538 p = vim_strsave_fnameescape(s,
1539 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001540 if (p != NULL)
1541 {
1542 ga_concat(&ga, p);
1543 ga_concat(&ga, (char_u *)" ");
1544 vim_free(p);
1545 }
1546 s = e + 1;
1547 }
1548 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001549 clear_tv(tv);
1550 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001551 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001552 return OK;
1553 }
1554 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001555 default: to_string_error(tv->v_type);
1556 return FAIL;
1557 }
1558 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001559 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001560 clear_tv(tv);
1561 tv->v_type = VAR_STRING;
1562 tv->vval.v_string = str;
1563 }
1564 return OK;
1565}
1566
1567/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001568 * When the value of "sv" is a null list of dict, allocate it.
1569 */
1570 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001571allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001572{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001573 typval_T *tv = sv->sv_tv;
1574
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001575 switch (tv->v_type)
1576 {
1577 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001578 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001579 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001580 break;
1581 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001582 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001583 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001584 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001585 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001586 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001587 (void)rettv_blob_alloc(tv);
1588 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001589 default:
1590 break;
1591 }
1592}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001593
Bram Moolenaare7525c52021-01-09 13:20:37 +01001594/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001595 * Return the character "str[index]" where "index" is the character index,
1596 * including composing characters.
1597 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001598 */
1599 char_u *
1600char_from_string(char_u *str, varnumber_T index)
1601{
1602 size_t nbyte = 0;
1603 varnumber_T nchar = index;
1604 size_t slen;
1605
1606 if (str == NULL)
1607 return NULL;
1608 slen = STRLEN(str);
1609
Bram Moolenaarff871402021-03-26 13:34:05 +01001610 // Do the same as for a list: a negative index counts from the end.
1611 // Optimization to check the first byte to be below 0x80 (and no composing
1612 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001613 if (index < 0)
1614 {
1615 int clen = 0;
1616
1617 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001618 {
1619 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1620 ++nbyte;
1621 else if (enc_utf8)
1622 nbyte += utfc_ptr2len(str + nbyte);
1623 else
1624 nbyte += mb_ptr2len(str + nbyte);
1625 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001626 nchar = clen + index;
1627 if (nchar < 0)
1628 // unlike list: index out of range results in empty string
1629 return NULL;
1630 }
1631
1632 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001633 {
1634 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1635 ++nbyte;
1636 else if (enc_utf8)
1637 nbyte += utfc_ptr2len(str + nbyte);
1638 else
1639 nbyte += mb_ptr2len(str + nbyte);
1640 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001641 if (nbyte >= slen)
1642 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001643 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001644}
1645
1646/*
1647 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001648 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001649 * If going over the end return "str_len".
1650 * If "idx" is negative count from the end, -1 is the last character.
1651 * When going over the start return -1.
1652 */
1653 static long
1654char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1655{
1656 varnumber_T nchar = idx;
1657 size_t nbyte = 0;
1658
1659 if (nchar >= 0)
1660 {
1661 while (nchar > 0 && nbyte < str_len)
1662 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001663 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001664 --nchar;
1665 }
1666 }
1667 else
1668 {
1669 nbyte = str_len;
1670 while (nchar < 0 && nbyte > 0)
1671 {
1672 --nbyte;
1673 nbyte -= mb_head_off(str, str + nbyte);
1674 ++nchar;
1675 }
1676 if (nchar < 0)
1677 return -1;
1678 }
1679 return (long)nbyte;
1680}
1681
1682/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001683 * Return the slice "str[first : last]" using character indexes. Composing
1684 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001685 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001686 * Return NULL when the result is empty.
1687 */
1688 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001689string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001690{
1691 long start_byte, end_byte;
1692 size_t slen;
1693
1694 if (str == NULL)
1695 return NULL;
1696 slen = STRLEN(str);
1697 start_byte = char_idx2byte(str, slen, first);
1698 if (start_byte < 0)
1699 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001700 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001701 end_byte = (long)slen;
1702 else
1703 {
1704 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001705 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001706 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001707 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001708 }
1709
1710 if (start_byte >= (long)slen || end_byte <= start_byte)
1711 return NULL;
1712 return vim_strnsave(str + start_byte, end_byte - start_byte);
1713}
1714
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001715/*
1716 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1717 * When "dfunc_idx" is negative don't give an error.
1718 * Returns NULL for an error.
1719 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001720 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001721get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001722{
1723 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001724 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1725 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001726 svar_T *sv;
1727
1728 if (sref->sref_seq != si->sn_script_seq)
1729 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001730 // The script was reloaded after the function was compiled, the
1731 // script_idx may not be valid.
1732 if (dfunc != NULL)
1733 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1734 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001735 return NULL;
1736 }
1737 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001738 if (sv->sv_name == NULL)
1739 {
1740 if (dfunc != NULL)
1741 emsg(_(e_script_variable_was_deleted));
1742 return NULL;
1743 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001744 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001745 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001746 if (dfunc != NULL)
1747 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001748 return NULL;
1749 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001750
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001751 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1752 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001753 {
1754 if (dfunc != NULL)
1755 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1756 return NULL;
1757 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001758 return sv;
1759}
1760
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001761/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001762 * Function passed to do_cmdline() for splitting a script joined by NL
1763 * characters.
1764 */
1765 static char_u *
1766get_split_sourceline(
1767 int c UNUSED,
1768 void *cookie,
1769 int indent UNUSED,
1770 getline_opt_T options UNUSED)
1771{
1772 source_cookie_T *sp = (source_cookie_T *)cookie;
1773 char_u *p;
1774 char_u *line;
1775
Bram Moolenaar20677332021-06-06 17:02:53 +02001776 p = vim_strchr(sp->nextline, '\n');
1777 if (p == NULL)
1778 {
1779 line = vim_strsave(sp->nextline);
1780 sp->nextline += STRLEN(sp->nextline);
1781 }
1782 else
1783 {
1784 line = vim_strnsave(sp->nextline, p - sp->nextline);
1785 sp->nextline = p + 1;
1786 }
1787 return line;
1788}
1789
1790/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 * Execute a function by "name".
1792 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001793 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 */
1795 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001796call_eval_func(
1797 char_u *name,
1798 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001799 ectx_T *ectx,
1800 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801{
Bram Moolenaared677f52020-08-12 16:38:10 +02001802 int called_emsg_before = called_emsg;
1803 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001805 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001806 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001808 dictitem_T *v;
1809
1810 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001811 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1812 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001813 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001814 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001815 return FAIL;
1816 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001817 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001819 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820}
1821
1822/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001823 * When a function reference is used, fill a partial with the information
1824 * needed, especially when it is used as a closure.
1825 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001826 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001827fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001828 partial_T *pt,
1829 ufunc_T *ufunc,
1830 loopvarinfo_T *lvi,
1831 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001832{
1833 pt->pt_func = ufunc;
1834 pt->pt_refcount = 1;
1835
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001836 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001837 {
1838 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1839 + ectx->ec_dfunc_idx;
1840
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001841 // The closure may need to find arguments and local variables of the
1842 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001843 pt->pt_outer.out_stack = &ectx->ec_stack;
1844 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001845 if (ectx->ec_outer_ref != NULL)
1846 {
1847 // The current context already has a context, link to that one.
1848 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1849 if (ectx->ec_outer_ref->or_partial != NULL)
1850 {
1851 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1852 ++pt->pt_outer.out_up_partial->pt_refcount;
1853 }
1854 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001855
Bram Moolenaarcc341812022-09-19 15:54:34 +01001856 if (lvi != NULL)
1857 {
1858 int depth;
1859
1860 // The closure may need to find variables defined inside a loop,
1861 // for every nested loop. A new reference is made every time,
1862 // ISN_ENDLOOP will check if they are actually used.
1863 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1864 {
1865 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1866 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1867 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1868 pt->pt_outer.out_loop[depth].var_count =
1869 lvi->lvi_loop[depth].var_count;
1870 }
1871 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001872
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001873 // If the function currently executing returns and the closure is still
1874 // being referenced, we need to make a copy of the context (arguments
1875 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001876 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001877 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001878 {
1879 vim_free(pt);
1880 return FAIL;
1881 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001882 // Extra variable keeps the count of closures created in the current
1883 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001884 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1885 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1886
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001887 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1888 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001889 ++pt->pt_refcount;
1890 ++ectx->ec_funcrefs.ga_len;
1891 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001892 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001893 return OK;
1894}
1895
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001896/*
1897 * Execute iptr->isn_arg.string as an Ex command.
1898 */
1899 static int
1900exec_command(isn_T *iptr)
1901{
1902 source_cookie_T cookie;
1903
1904 SOURCING_LNUM = iptr->isn_lnum;
1905 // Pass getsourceline to get an error for a missing ":end"
1906 // command.
1907 CLEAR_FIELD(cookie);
1908 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1909 if (do_cmdline(iptr->isn_arg.string,
1910 getsourceline, &cookie,
1911 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1912 || did_emsg)
1913 return FAIL;
1914 return OK;
1915}
1916
Bram Moolenaar89445512022-04-14 12:58:23 +01001917/*
1918 * If script "sid" is not loaded yet then load it now.
1919 * Caller must make sure "sid" is a valid script ID.
1920 * "loaded" is set to TRUE if the script had to be loaded.
1921 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1922 */
1923 int
1924may_load_script(int sid, int *loaded)
1925{
1926 scriptitem_T *si = SCRIPT_ITEM(sid);
1927
1928 if (si->sn_state == SN_STATE_NOT_LOADED)
1929 {
1930 *loaded = TRUE;
1931 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1932 {
1933 semsg(_(e_cant_open_file_str), si->sn_name);
1934 return FAIL;
1935 }
1936 }
1937 return OK;
1938}
1939
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001940// used for v_instr of typval of VAR_INSTR
1941struct instr_S {
1942 ectx_T *instr_ectx;
1943 isn_T *instr_instr;
1944};
1945
Bram Moolenaar4c137212021-04-19 16:48:48 +02001946// used for substitute_instr
1947typedef struct subs_expr_S {
1948 ectx_T *subs_ectx;
1949 isn_T *subs_instr;
1950 int subs_status;
1951} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001953// Set when calling do_debug().
1954static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001955static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001956
1957/*
1958 * When debugging lookup "name" and return the typeval.
1959 * When not found return NULL.
1960 */
1961 typval_T *
1962lookup_debug_var(char_u *name)
1963{
1964 int idx;
1965 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001966 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001967 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001968 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001969
1970 if (ectx == NULL)
1971 return NULL;
1972 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1973
1974 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001975 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001976 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001977 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1978
1979 // the variable name may be NULL when not available in this block
1980 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001981 return STACK_TV_VAR(idx);
1982 }
1983
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001984 // Go through argument names.
1985 ufunc = dfunc->df_ufunc;
1986 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1987 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1988 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1989 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1990 - varargs_off + idx);
1991 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1992 return STACK_TV(ectx->ec_frame_idx - 1);
1993
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001994 return NULL;
1995}
1996
Bram Moolenaar26a44842021-09-02 18:49:06 +02001997/*
1998 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1999 * breakpoint was set in that function or when there is any expression.
2000 */
2001 int
2002may_break_in_function(ufunc_T *ufunc)
2003{
2004 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2005}
2006
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002007 static void
2008handle_debug(isn_T *iptr, ectx_T *ectx)
2009{
2010 char_u *line;
2011 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2012 + ectx->ec_dfunc_idx)->df_ufunc;
2013 isn_T *ni;
2014 int end_lnum = iptr->isn_lnum;
2015 garray_T ga;
2016 int lnum;
2017
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002018 if (ex_nesting_level > debug_break_level)
2019 {
2020 linenr_T breakpoint;
2021
Bram Moolenaar26a44842021-09-02 18:49:06 +02002022 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002023 return;
2024
2025 // check for the next breakpoint if needed
2026 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002027 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002028 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2029 return;
2030 }
2031
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002032 SOURCING_LNUM = iptr->isn_lnum;
2033 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002034 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002035
2036 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2037 if (ni->isn_type == ISN_DEBUG
2038 || ni->isn_type == ISN_RETURN
2039 || ni->isn_type == ISN_RETURN_VOID)
2040 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002041 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002042 break;
2043 }
2044
2045 if (end_lnum > iptr->isn_lnum)
2046 {
2047 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002048 for (lnum = iptr->isn_lnum; lnum < end_lnum
2049 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002050 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002051 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002052
Bram Moolenaar303215d2021-07-07 20:10:43 +02002053 if (p == NULL)
2054 continue; // left over from continuation line
2055 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002056 if (*p == '#')
2057 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002058 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002059 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2060 if (STRNCMP(p, "def ", 4) == 0)
2061 break;
2062 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002063 line = ga_concat_strings(&ga, " ");
2064 vim_free(ga.ga_data);
2065 }
2066 else
2067 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002068
Dominique Pelle6e969552021-06-17 13:53:41 +02002069 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002070 debug_context = NULL;
2071
2072 if (end_lnum > iptr->isn_lnum)
2073 vim_free(line);
2074}
2075
Bram Moolenaar4c137212021-04-19 16:48:48 +02002076/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002077 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002078 * Returns OK, FAIL or NOTDONE (uncatchable error).
2079 */
2080 static int
2081execute_storeindex(isn_T *iptr, ectx_T *ectx)
2082{
2083 vartype_T dest_type = iptr->isn_arg.vartype;
2084 typval_T *tv;
2085 typval_T *tv_idx = STACK_TV_BOT(-2);
2086 typval_T *tv_dest = STACK_TV_BOT(-1);
2087 int status = OK;
2088
2089 // Stack contains:
2090 // -3 value to be stored
2091 // -2 index
2092 // -1 dict or list
2093 tv = STACK_TV_BOT(-3);
2094 SOURCING_LNUM = iptr->isn_lnum;
2095 if (dest_type == VAR_ANY)
2096 {
2097 dest_type = tv_dest->v_type;
2098 if (dest_type == VAR_DICT)
2099 status = do_2string(tv_idx, TRUE, FALSE);
2100 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2101 {
2102 emsg(_(e_number_expected));
2103 status = FAIL;
2104 }
2105 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002106
2107 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002108 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002109 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002110 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002111 long lidx = (long)tv_idx->vval.v_number;
2112 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002113
Bram Moolenaare08be092022-02-17 13:08:26 +00002114 if (list == NULL)
2115 {
2116 emsg(_(e_list_not_set));
2117 return FAIL;
2118 }
2119 if (lidx < 0 && list->lv_len + lidx >= 0)
2120 // negative index is relative to the end
2121 lidx = list->lv_len + lidx;
2122 if (lidx < 0 || lidx > list->lv_len)
2123 {
2124 semsg(_(e_list_index_out_of_range_nr), lidx);
2125 return FAIL;
2126 }
2127 if (lidx < list->lv_len)
2128 {
2129 listitem_T *li = list_find(list, lidx);
2130
2131 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002132 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002133 return FAIL;
2134 // overwrite existing list item
2135 clear_tv(&li->li_tv);
2136 li->li_tv = *tv;
2137 }
2138 else
2139 {
2140 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2141 return FAIL;
2142 // append to list, only fails when out of memory
2143 if (list_append_tv(list, tv) == FAIL)
2144 return NOTDONE;
2145 clear_tv(tv);
2146 }
2147 }
2148 else if (dest_type == VAR_DICT)
2149 {
2150 char_u *key = tv_idx->vval.v_string;
2151 dict_T *dict = tv_dest->vval.v_dict;
2152 dictitem_T *di;
2153
2154 SOURCING_LNUM = iptr->isn_lnum;
2155 if (dict == NULL)
2156 {
2157 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002158 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002159 }
2160 if (key == NULL)
2161 key = (char_u *)"";
2162 di = dict_find(dict, key, -1);
2163 if (di != NULL)
2164 {
2165 if (error_if_locked(di->di_tv.v_lock,
2166 e_cannot_change_dict_item))
2167 return FAIL;
2168 // overwrite existing value
2169 clear_tv(&di->di_tv);
2170 di->di_tv = *tv;
2171 }
2172 else
2173 {
2174 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2175 return FAIL;
2176 // add to dict, only fails when out of memory
2177 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2178 return NOTDONE;
2179 clear_tv(tv);
2180 }
2181 }
2182 else if (dest_type == VAR_BLOB)
2183 {
2184 long lidx = (long)tv_idx->vval.v_number;
2185 blob_T *blob = tv_dest->vval.v_blob;
2186 varnumber_T nr;
2187 int error = FALSE;
2188 int len;
2189
2190 if (blob == NULL)
2191 {
2192 emsg(_(e_blob_not_set));
2193 return FAIL;
2194 }
2195 len = blob_len(blob);
2196 if (lidx < 0 && len + lidx >= 0)
2197 // negative index is relative to the end
2198 lidx = len + lidx;
2199
2200 // Can add one byte at the end.
2201 if (lidx < 0 || lidx > len)
2202 {
2203 semsg(_(e_blob_index_out_of_range_nr), lidx);
2204 return FAIL;
2205 }
2206 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2207 return FAIL;
2208 nr = tv_get_number_chk(tv, &error);
2209 if (error)
2210 return FAIL;
2211 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002212 }
2213 else
2214 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002215 status = FAIL;
2216 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002217 }
2218 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002219
2220 clear_tv(tv_idx);
2221 clear_tv(tv_dest);
2222 ectx->ec_stack.ga_len -= 3;
2223 if (status == FAIL)
2224 {
2225 clear_tv(tv);
2226 return FAIL;
2227 }
2228 return OK;
2229}
2230
2231/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002232 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002233 */
2234 static int
2235execute_storerange(isn_T *iptr, ectx_T *ectx)
2236{
2237 typval_T *tv;
2238 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2239 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2240 typval_T *tv_dest = STACK_TV_BOT(-1);
2241 int status = OK;
2242
2243 // Stack contains:
2244 // -4 value to be stored
2245 // -3 first index or "none"
2246 // -2 second index or "none"
2247 // -1 destination list or blob
2248 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002249 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002250 if (tv_dest->v_type == VAR_LIST)
2251 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002252 long n1;
2253 long n2;
2254 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002255
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002256 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2257 if (tv_idx2->v_type == VAR_SPECIAL
2258 && tv_idx2->vval.v_number == VVAL_NONE)
2259 n2 = list_len(tv_dest->vval.v_list) - 1;
2260 else
2261 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2262
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002263 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002264 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002265 status = FAIL;
2266 else
2267 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002268 status = check_range_index_two(tv_dest->vval.v_list,
2269 &n1, li1, &n2, FALSE);
2270 if (status != FAIL)
2271 status = list_assign_range(
2272 tv_dest->vval.v_list,
2273 tv->vval.v_list,
2274 n1,
2275 n2,
2276 tv_idx2->v_type == VAR_SPECIAL,
2277 (char_u *)"=",
2278 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002279 }
2280 }
2281 else if (tv_dest->v_type == VAR_BLOB)
2282 {
2283 varnumber_T n1;
2284 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002285 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002286
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002287 n1 = tv_get_number_chk(tv_idx1, NULL);
2288 if (tv_idx2->v_type == VAR_SPECIAL
2289 && tv_idx2->vval.v_number == VVAL_NONE)
2290 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2291 else
2292 n2 = tv_get_number_chk(tv_idx2, NULL);
2293 bloblen = blob_len(tv_dest->vval.v_blob);
2294
2295 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2296 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002297 status = FAIL;
2298 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002299 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002300 }
2301 else
2302 {
2303 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002304 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002305 }
2306
2307 clear_tv(tv_idx1);
2308 clear_tv(tv_idx2);
2309 clear_tv(tv_dest);
2310 ectx->ec_stack.ga_len -= 4;
2311 clear_tv(tv);
2312
2313 return status;
2314}
2315
2316/*
2317 * Unlet item in list or dict variable.
2318 */
2319 static int
2320execute_unletindex(isn_T *iptr, ectx_T *ectx)
2321{
2322 typval_T *tv_idx = STACK_TV_BOT(-2);
2323 typval_T *tv_dest = STACK_TV_BOT(-1);
2324 int status = OK;
2325
2326 // Stack contains:
2327 // -2 index
2328 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002329 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002330 if (tv_dest->v_type == VAR_DICT)
2331 {
2332 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002333 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002334 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002335 semsg(_(e_expected_str_but_got_str),
2336 vartype_name(VAR_STRING),
2337 vartype_name(tv_idx->v_type));
2338 status = FAIL;
2339 }
2340 else
2341 {
2342 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002343 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002344 dictitem_T *di = NULL;
2345
2346 if (d != NULL && value_check_lock(
2347 d->dv_lock, NULL, FALSE))
2348 status = FAIL;
2349 else
2350 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002351 if (tv_idx->v_type == VAR_STRING)
2352 {
2353 key = tv_idx->vval.v_string;
2354 if (key == NULL)
2355 key = (char_u *)"";
2356 }
2357 else
2358 {
2359 key = tv_get_string(tv_idx);
2360 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002361 if (d != NULL)
2362 di = dict_find(d, key, (int)STRLEN(key));
2363 if (di == NULL)
2364 {
2365 // NULL dict is equivalent to empty dict
2366 semsg(_(e_key_not_present_in_dictionary),
2367 key);
2368 status = FAIL;
2369 }
2370 else if (var_check_fixed(di->di_flags,
2371 NULL, FALSE)
2372 || var_check_ro(di->di_flags,
2373 NULL, FALSE))
2374 status = FAIL;
2375 else
2376 dictitem_remove(d, di);
2377 }
2378 }
2379 }
2380 else if (tv_dest->v_type == VAR_LIST)
2381 {
2382 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002383 if (check_for_number(tv_idx) == FAIL)
2384 {
2385 status = FAIL;
2386 }
2387 else
2388 {
2389 list_T *l = tv_dest->vval.v_list;
2390 long n = (long)tv_idx->vval.v_number;
2391
2392 if (l != NULL && value_check_lock(
2393 l->lv_lock, NULL, FALSE))
2394 status = FAIL;
2395 else
2396 {
2397 listitem_T *li = list_find(l, n);
2398
2399 if (li == NULL)
2400 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002401 semsg(_(e_list_index_out_of_range_nr), n);
2402 status = FAIL;
2403 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002404 else
2405 listitem_remove(l, li);
2406 }
2407 }
2408 }
2409 else
2410 {
2411 status = FAIL;
2412 semsg(_(e_cannot_index_str),
2413 vartype_name(tv_dest->v_type));
2414 }
2415
2416 clear_tv(tv_idx);
2417 clear_tv(tv_dest);
2418 ectx->ec_stack.ga_len -= 2;
2419
2420 return status;
2421}
2422
2423/*
2424 * Unlet a range of items in a list variable.
2425 */
2426 static int
2427execute_unletrange(isn_T *iptr, ectx_T *ectx)
2428{
2429 // Stack contains:
2430 // -3 index1
2431 // -2 index2
2432 // -1 dict or list
2433 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2434 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2435 typval_T *tv_dest = STACK_TV_BOT(-1);
2436 int status = OK;
2437
2438 if (tv_dest->v_type == VAR_LIST)
2439 {
2440 // indexes must be a number
2441 SOURCING_LNUM = iptr->isn_lnum;
2442 if (check_for_number(tv_idx1) == FAIL
2443 || (tv_idx2->v_type != VAR_SPECIAL
2444 && check_for_number(tv_idx2) == FAIL))
2445 {
2446 status = FAIL;
2447 }
2448 else
2449 {
2450 list_T *l = tv_dest->vval.v_list;
2451 long n1 = (long)tv_idx1->vval.v_number;
2452 long n2 = tv_idx2->v_type == VAR_SPECIAL
2453 ? 0 : (long)tv_idx2->vval.v_number;
2454 listitem_T *li;
2455
2456 li = list_find_index(l, &n1);
2457 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002458 {
2459 semsg(_(e_list_index_out_of_range_nr),
2460 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002461 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002462 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002463 else
2464 {
2465 if (n1 < 0)
2466 n1 = list_idx_of_item(l, li);
2467 if (n2 < 0)
2468 {
2469 listitem_T *li2 = list_find(l, n2);
2470
2471 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002472 {
2473 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002474 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002475 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002476 else
2477 n2 = list_idx_of_item(l, li2);
2478 }
2479 if (status != FAIL
2480 && tv_idx2->v_type != VAR_SPECIAL
2481 && n2 < n1)
2482 {
2483 semsg(_(e_list_index_out_of_range_nr), n2);
2484 status = FAIL;
2485 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002486 if (status != FAIL)
2487 list_unlet_range(l, li, n1,
2488 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002489 }
2490 }
2491 }
2492 else
2493 {
2494 status = FAIL;
2495 SOURCING_LNUM = iptr->isn_lnum;
2496 semsg(_(e_cannot_index_str),
2497 vartype_name(tv_dest->v_type));
2498 }
2499
2500 clear_tv(tv_idx1);
2501 clear_tv(tv_idx2);
2502 clear_tv(tv_dest);
2503 ectx->ec_stack.ga_len -= 3;
2504
2505 return status;
2506}
2507
2508/*
2509 * Top of a for loop.
2510 */
2511 static int
2512execute_for(isn_T *iptr, ectx_T *ectx)
2513{
2514 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002515 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002516 typval_T *ltv = STACK_TV_BOT(-1);
2517 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002518 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002519
2520 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2521 return FAIL;
2522 if (ltv->v_type == VAR_LIST)
2523 {
2524 list_T *list = ltv->vval.v_list;
2525
2526 // push the next item from the list
2527 ++idxtv->vval.v_number;
2528 if (list == NULL
2529 || idxtv->vval.v_number >= list->lv_len)
2530 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002531 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002532 }
2533 else if (list->lv_first == &range_list_item)
2534 {
2535 // non-materialized range() list
2536 tv = STACK_TV_BOT(0);
2537 tv->v_type = VAR_NUMBER;
2538 tv->v_lock = 0;
2539 tv->vval.v_number = list_find_nr(
2540 list, idxtv->vval.v_number, NULL);
2541 ++ectx->ec_stack.ga_len;
2542 }
2543 else
2544 {
2545 listitem_T *li = list_find(list,
2546 idxtv->vval.v_number);
2547
2548 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2549 ++ectx->ec_stack.ga_len;
2550 }
2551 }
2552 else if (ltv->v_type == VAR_STRING)
2553 {
2554 char_u *str = ltv->vval.v_string;
2555
2556 // The index is for the last byte of the previous
2557 // character.
2558 ++idxtv->vval.v_number;
2559 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2560 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002561 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002562 }
2563 else
2564 {
2565 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2566
2567 // Push the next character from the string.
2568 tv = STACK_TV_BOT(0);
2569 tv->v_type = VAR_STRING;
2570 tv->vval.v_string = vim_strnsave(
2571 str + idxtv->vval.v_number, clen);
2572 ++ectx->ec_stack.ga_len;
2573 idxtv->vval.v_number += clen - 1;
2574 }
2575 }
2576 else if (ltv->v_type == VAR_BLOB)
2577 {
2578 blob_T *blob = ltv->vval.v_blob;
2579
2580 // When we get here the first time make a copy of the
2581 // blob, so that the iteration still works when it is
2582 // changed.
2583 if (idxtv->vval.v_number == -1 && blob != NULL)
2584 {
2585 blob_copy(blob, ltv);
2586 blob_unref(blob);
2587 blob = ltv->vval.v_blob;
2588 }
2589
2590 // The index is for the previous byte.
2591 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002592 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002593 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002594 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002595 }
2596 else
2597 {
2598 // Push the next byte from the blob.
2599 tv = STACK_TV_BOT(0);
2600 tv->v_type = VAR_NUMBER;
2601 tv->vval.v_number = blob_get(blob,
2602 idxtv->vval.v_number);
2603 ++ectx->ec_stack.ga_len;
2604 }
2605 }
2606 else
2607 {
2608 semsg(_(e_for_loop_on_str_not_supported),
2609 vartype_name(ltv->v_type));
2610 return FAIL;
2611 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002612
2613 if (jump)
2614 {
2615 // past the end of the list/string/blob, jump to "endfor"
2616 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2617 may_restore_cmdmod(&ectx->ec_funclocal);
2618 }
2619 else
2620 {
2621 // Store the current number of funcrefs, this may be used in
2622 // ISN_LOOPEND. The variable index is always one more than the loop
2623 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002624 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002625 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2626 }
2627
2628 return OK;
2629}
2630
2631/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002632 * Code for handling variables declared inside a loop and used in a closure.
2633 * This is very similar to what is done with funcstack_T. The difference is
2634 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2635 * scope of the block inside a loop and each loop may have its own.
2636 */
2637
2638// Double linked list of loopvars_T in use.
2639static loopvars_T *first_loopvars = NULL;
2640
2641 static void
2642add_loopvars_to_list(loopvars_T *loopvars)
2643{
2644 // Link in list of loopvarss.
2645 if (first_loopvars != NULL)
2646 first_loopvars->lvs_prev = loopvars;
2647 loopvars->lvs_next = first_loopvars;
2648 loopvars->lvs_prev = NULL;
2649 first_loopvars = loopvars;
2650}
2651
2652 static void
2653remove_loopvars_from_list(loopvars_T *loopvars)
2654{
2655 if (loopvars->lvs_prev == NULL)
2656 first_loopvars = loopvars->lvs_next;
2657 else
2658 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2659 if (loopvars->lvs_next != NULL)
2660 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2661}
2662
2663/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002664 * End of a for or while loop: Handle any variables used by a closure.
2665 */
2666 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002667execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002668{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002669 endloop_T *endloop = &iptr->isn_arg.endloop;
2670 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2671 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002672 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002673 garray_T *gap = &ectx->ec_funcrefs;
2674 int closure_in_use = FALSE;
2675 loopvars_T *loopvars;
2676 typval_T *stack;
2677 int idx;
2678
Bram Moolenaarcc341812022-09-19 15:54:34 +01002679 // Check if any created closure is still being referenced and loopvars have
2680 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002681 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2682 {
2683 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2684
Bram Moolenaarcc341812022-09-19 15:54:34 +01002685 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002686 {
2687 int refcount = pt->pt_refcount;
2688 int i;
2689
2690 // A Reference in a variable inside the loop doesn't count, it gets
2691 // unreferenced at the end of the loop.
2692 for (i = 0; i < endloop->end_var_count; ++i)
2693 {
2694 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2695
2696 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2697 --refcount;
2698 }
2699 if (refcount > 1)
2700 {
2701 closure_in_use = TRUE;
2702 break;
2703 }
2704 }
2705 }
2706
2707 // If no function reference were created since the start of the loop block
2708 // or it is no longer referenced there is nothing to do.
2709 if (!closure_in_use)
2710 return OK;
2711
2712 // A closure is using variables declared inside the loop.
2713 // Move them to the called function.
2714 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2715 if (loopvars == NULL)
2716 return FAIL;
2717
2718 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2719 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2720 loopvars->lvs_ga.ga_data = stack;
2721 if (stack == NULL)
2722 {
2723 vim_free(loopvars);
2724 return FAIL;
2725 }
2726 add_loopvars_to_list(loopvars);
2727
2728 // Move the variable values.
2729 for (idx = 0; idx < endloop->end_var_count; ++idx)
2730 {
2731 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2732
2733 *(stack + idx) = *tv;
2734 tv->v_type = VAR_UNKNOWN;
2735 }
2736
2737 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2738 {
2739 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2740
Bram Moolenaarcc341812022-09-19 15:54:34 +01002741 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002742 {
2743 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002744 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002745
Bram Moolenaarcc341812022-09-19 15:54:34 +01002746 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2747 pt->pt_outer.out_loop[depth].var_idx -=
2748 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002749 }
2750 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002751
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002752 return OK;
2753}
2754
2755/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002756 * Called when a partial is freed or its reference count goes down to one. The
2757 * loopvars may be the only reference to the partials in the local variables.
2758 * Go over all of them, the funcref and can be freed if all partials
2759 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002760 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002761 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002762 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002763loopvars_check_refcount(loopvars_T *loopvars)
2764{
2765 int i;
2766 garray_T *gap = &loopvars->lvs_ga;
2767 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002768 typval_T *stack = gap->ga_data;
2769
Bram Moolenaarcc341812022-09-19 15:54:34 +01002770 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2771 return FALSE;
2772 for (i = 0; i < gap->ga_len; ++i)
2773 {
2774 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2775
2776 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2777 && tv->vval.v_partial->pt_refcount == 1)
2778 {
2779 int depth;
2780
2781 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2782 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2783 ++done;
2784 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002785 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002786 if (done != loopvars->lvs_min_refcount)
2787 return FALSE;
2788
2789 // All partials referencing the loopvars have a reference count of
2790 // one, thus the loopvars is no longer of use.
2791 stack = gap->ga_data;
2792 for (i = 0; i < gap->ga_len; ++i)
2793 clear_tv(stack + i);
2794 vim_free(stack);
2795 remove_loopvars_from_list(loopvars);
2796 vim_free(loopvars);
2797 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002798}
2799
2800/*
2801 * For garbage collecting: set references in all variables referenced by
2802 * all loopvars.
2803 */
2804 int
2805set_ref_in_loopvars(int copyID)
2806{
2807 loopvars_T *loopvars;
2808
2809 for (loopvars = first_loopvars; loopvars != NULL;
2810 loopvars = loopvars->lvs_next)
2811 {
2812 typval_T *stack = loopvars->lvs_ga.ga_data;
2813 int i;
2814
2815 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2816 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2817 return TRUE; // abort
2818 }
2819 return FALSE;
2820}
2821
2822/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002823 * Load instruction for w:/b:/g:/t: variable.
2824 * "isn_type" is used instead of "iptr->isn_type".
2825 */
2826 static int
2827load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2828{
2829 dictitem_T *di = NULL;
2830 hashtab_T *ht = NULL;
2831 char namespace;
2832
2833 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2834 return NOTDONE;
2835 switch (isn_type)
2836 {
2837 case ISN_LOADG:
2838 ht = get_globvar_ht();
2839 namespace = 'g';
2840 break;
2841 case ISN_LOADB:
2842 ht = &curbuf->b_vars->dv_hashtab;
2843 namespace = 'b';
2844 break;
2845 case ISN_LOADW:
2846 ht = &curwin->w_vars->dv_hashtab;
2847 namespace = 'w';
2848 break;
2849 case ISN_LOADT:
2850 ht = &curtab->tp_vars->dv_hashtab;
2851 namespace = 't';
2852 break;
2853 default: // Cannot reach here
2854 return NOTDONE;
2855 }
2856 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2857
Bram Moolenaar06b77222022-01-25 15:51:56 +00002858 if (di == NULL)
2859 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002860 if (isn_type == ISN_LOADG)
2861 {
2862 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2863
2864 // g:Something could be a function
2865 if (ufunc != NULL)
2866 {
2867 typval_T *tv = STACK_TV_BOT(0);
2868
2869 ++ectx->ec_stack.ga_len;
2870 tv->v_type = VAR_FUNC;
2871 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2872 if (tv->vval.v_string == NULL)
2873 return FAIL;
2874 STRCPY(tv->vval.v_string, "g:");
2875 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2876 return OK;
2877 }
2878 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002879 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002880 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002881 // no check if the item exists in the script but
2882 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002883 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002884 else
2885 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002886 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002887 return FAIL;
2888 }
2889 else
2890 {
2891 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2892 ++ectx->ec_stack.ga_len;
2893 }
2894 return OK;
2895}
2896
2897/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002898 * Execute instructions in execution context "ectx".
2899 * Return OK or FAIL;
2900 */
2901 static int
2902exec_instructions(ectx_T *ectx)
2903{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002904 int ret = FAIL;
2905 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002906 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002907
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002908 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002909 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002910
Bram Moolenaarff652882021-05-16 15:24:49 +02002911 // Only catch exceptions in this instruction list.
2912 ectx->ec_trylevel_at_start = trylevel;
2913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 for (;;)
2915 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002916 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002918 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002919
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002920 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002921 {
2922 line_breakcheck();
2923 breakcheck_count = 0;
2924 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002925 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002926 {
2927 // Turn CTRL-C into an exception.
2928 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002929 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002930 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002931 did_throw = TRUE;
2932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002934 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002935 {
2936 // Turn an error message into an exception.
2937 did_emsg = FALSE;
2938 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002939 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002940 did_throw = TRUE;
2941 *msg_list = NULL;
2942 }
2943
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002944 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002946 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002947 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002948 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949
2950 // An exception jumps to the first catch, finally, or returns from
2951 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002952 while (index > 0)
2953 {
2954 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002955 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002956 break;
2957 // In the catch and finally block of this try we have to go up
2958 // one level.
2959 --index;
2960 trycmd = NULL;
2961 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002962 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002964 if (trycmd->tcd_in_catch)
2965 {
2966 // exception inside ":catch", jump to ":finally" once
2967 ectx->ec_iidx = trycmd->tcd_finally_idx;
2968 trycmd->tcd_finally_idx = 0;
2969 }
2970 else
2971 // jump to first ":catch"
2972 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002973 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002974 did_throw = FALSE; // don't come back here until :endtry
2975 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 }
2977 else
2978 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002979 // Not inside try or need to return from current functions.
2980 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002981 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002982 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002983 tv = STACK_TV_BOT(0);
2984 tv->v_type = VAR_NUMBER;
2985 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002986 ++ectx->ec_stack.ga_len;
2987 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002989 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002990 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002991 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002992 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 goto done;
2994 }
2995
Bram Moolenaar4c137212021-04-19 16:48:48 +02002996 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002997 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 }
2999 continue;
3000 }
3001
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003002 /*
3003 * Big switch on the instruction. Most compilers will be turning this
3004 * into an efficient lookup table, since the "case" values are an enum
3005 * with sequential numbers. It may look ugly, but it should be the
3006 * most efficient way.
3007 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003008 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 switch (iptr->isn_type)
3010 {
3011 // execute Ex command line
3012 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003013 if (exec_command(iptr) == FAIL)
3014 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 break;
3016
Bram Moolenaar20677332021-06-06 17:02:53 +02003017 // execute Ex command line split at NL characters.
3018 case ISN_EXEC_SPLIT:
3019 {
3020 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003021 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003022
3023 SOURCING_LNUM = iptr->isn_lnum;
3024 CLEAR_FIELD(cookie);
3025 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3026 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003027 line = get_split_sourceline(0, &cookie, 0, 0);
3028 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003029 get_split_sourceline, &cookie,
3030 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3031 == FAIL
3032 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003033 {
3034 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003035 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003036 }
3037 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003038 }
3039 break;
3040
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003041 // execute Ex command line that is only a range
3042 case ISN_EXECRANGE:
3043 {
3044 exarg_T ea;
3045 char *error = NULL;
3046
3047 CLEAR_FIELD(ea);
3048 ea.cmdidx = CMD_SIZE;
3049 ea.addr_type = ADDR_LINES;
3050 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003051 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003052 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003053 if (ea.cmd == NULL)
3054 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003055 // error is always NULL when using ADDR_LINES
3056 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003057 if (error != NULL)
3058 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003059 emsg(error);
3060 goto on_error;
3061 }
3062 }
3063 break;
3064
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003065 // Evaluate an expression with legacy syntax, push it onto the
3066 // stack.
3067 case ISN_LEGACY_EVAL:
3068 {
3069 char_u *arg = iptr->isn_arg.string;
3070 int res;
3071 int save_flags = cmdmod.cmod_flags;
3072
Bram Moolenaar35578162021-08-02 19:10:38 +02003073 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003074 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003075 tv = STACK_TV_BOT(0);
3076 init_tv(tv);
3077 cmdmod.cmod_flags |= CMOD_LEGACY;
3078 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3079 cmdmod.cmod_flags = save_flags;
3080 if (res == FAIL)
3081 goto on_error;
3082 ++ectx->ec_stack.ga_len;
3083 }
3084 break;
3085
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003086 // push typeval VAR_INSTR with instructions to be executed
3087 case ISN_INSTR:
3088 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003089 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003090 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003091 tv = STACK_TV_BOT(0);
3092 tv->vval.v_instr = ALLOC_ONE(instr_T);
3093 if (tv->vval.v_instr == NULL)
3094 goto on_error;
3095 ++ectx->ec_stack.ga_len;
3096
3097 tv->v_type = VAR_INSTR;
3098 tv->vval.v_instr->instr_ectx = ectx;
3099 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3100 }
3101 break;
3102
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003103 case ISN_SOURCE:
3104 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003105 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003106
Bram Moolenaar89445512022-04-14 12:58:23 +01003107 SOURCING_LNUM = iptr->isn_lnum;
3108 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003109 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003110 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003111 }
3112 break;
3113
Bram Moolenaar4c137212021-04-19 16:48:48 +02003114 // execute :substitute with an expression
3115 case ISN_SUBSTITUTE:
3116 {
3117 subs_T *subs = &iptr->isn_arg.subs;
3118 source_cookie_T cookie;
3119 struct subs_expr_S *save_instr = substitute_instr;
3120 struct subs_expr_S subs_instr;
3121 int res;
3122
3123 subs_instr.subs_ectx = ectx;
3124 subs_instr.subs_instr = subs->subs_instr;
3125 subs_instr.subs_status = OK;
3126 substitute_instr = &subs_instr;
3127
3128 SOURCING_LNUM = iptr->isn_lnum;
3129 // This is very much like ISN_EXEC
3130 CLEAR_FIELD(cookie);
3131 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3132 res = do_cmdline(subs->subs_cmd,
3133 getsourceline, &cookie,
3134 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3135 substitute_instr = save_instr;
3136
3137 if (res == FAIL || did_emsg
3138 || subs_instr.subs_status == FAIL)
3139 goto on_error;
3140 }
3141 break;
3142
3143 case ISN_FINISH:
3144 goto done;
3145
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003146 case ISN_REDIRSTART:
3147 // create a dummy entry for var_redir_str()
3148 if (alloc_redir_lval() == FAIL)
3149 goto on_error;
3150
3151 // The output is stored in growarray "redir_ga" until
3152 // redirection ends.
3153 init_redir_ga();
3154 redir_vname = 1;
3155 break;
3156
3157 case ISN_REDIREND:
3158 {
3159 char_u *res = get_clear_redir_ga();
3160
3161 // End redirection, put redirected text on the stack.
3162 clear_redir_lval();
3163 redir_vname = 0;
3164
Bram Moolenaar35578162021-08-02 19:10:38 +02003165 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003166 {
3167 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003168 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003169 }
3170 tv = STACK_TV_BOT(0);
3171 tv->v_type = VAR_STRING;
3172 tv->vval.v_string = res;
3173 ++ectx->ec_stack.ga_len;
3174 }
3175 break;
3176
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003177 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003178#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003179 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003180 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3181 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003182 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003183#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003184 break;
3185
3186 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003187#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003188 {
3189 exarg_T ea;
3190 int res;
3191
3192 CLEAR_FIELD(ea);
3193 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3194 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3195 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3196 --ectx->ec_stack.ga_len;
3197 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003198 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003199 res = cexpr_core(&ea, tv);
3200 clear_tv(tv);
3201 if (res == FAIL)
3202 goto on_error;
3203 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003204#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003205 break;
3206
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003207 // execute Ex command from pieces on the stack
3208 case ISN_EXECCONCAT:
3209 {
3210 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003211 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003212 int pass;
3213 int i;
3214 char_u *cmd = NULL;
3215 char_u *str;
3216
3217 for (pass = 1; pass <= 2; ++pass)
3218 {
3219 for (i = 0; i < count; ++i)
3220 {
3221 tv = STACK_TV_BOT(i - count);
3222 str = tv->vval.v_string;
3223 if (str != NULL && *str != NUL)
3224 {
3225 if (pass == 2)
3226 STRCPY(cmd + len, str);
3227 len += STRLEN(str);
3228 }
3229 if (pass == 2)
3230 clear_tv(tv);
3231 }
3232 if (pass == 1)
3233 {
3234 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003235 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003236 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003237 len = 0;
3238 }
3239 }
3240
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003241 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003242 do_cmdline_cmd(cmd);
3243 vim_free(cmd);
3244 }
3245 break;
3246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 // execute :echo {string} ...
3248 case ISN_ECHO:
3249 {
3250 int count = iptr->isn_arg.echo.echo_count;
3251 int atstart = TRUE;
3252 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003253 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254
3255 for (idx = 0; idx < count; ++idx)
3256 {
3257 tv = STACK_TV_BOT(idx - count);
3258 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3259 &atstart, &needclr);
3260 clear_tv(tv);
3261 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003262 if (needclr)
3263 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003264 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 }
3266 break;
3267
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003268 // :execute {string} ...
3269 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003270 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003271 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003272 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003273 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003274 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003275 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003276 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003277 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003278 {
3279 int count = iptr->isn_arg.number;
3280 garray_T ga;
3281 char_u buf[NUMBUFLEN];
3282 char_u *p;
3283 int len;
3284 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003285 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003286
3287 ga_init2(&ga, 1, 80);
3288 for (idx = 0; idx < count; ++idx)
3289 {
3290 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003291 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003292 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003293 if (tv->v_type == VAR_CHANNEL
3294 || tv->v_type == VAR_JOB)
3295 {
3296 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003297 semsg(_(e_using_invalid_value_as_string_str),
3298 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003299 break;
3300 }
3301 else
3302 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003303 }
3304 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003305 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003306
3307 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003308 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003309 failed = TRUE;
3310 else
3311 {
3312 if (ga.ga_len > 0)
3313 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3314 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3315 ga.ga_len += len;
3316 }
3317 clear_tv(tv);
3318 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003319 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003320 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003321 {
3322 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003323 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003324 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003325
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003326 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003327 {
3328 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003329 {
3330 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003331 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003332 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003333 {
3334 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003335 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003336 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003337 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003338 else
3339 {
3340 msg_sb_eol();
3341 if (iptr->isn_type == ISN_ECHOMSG)
3342 {
3343 msg_attr(ga.ga_data, echo_attr);
3344 out_flush();
3345 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003346#ifdef HAS_MESSAGE_WINDOW
3347 else if (iptr->isn_type == ISN_ECHOWINDOW)
3348 {
3349 start_echowindow();
3350 msg_attr(ga.ga_data, echo_attr);
3351 end_echowindow();
3352 }
3353#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003354 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3355 {
3356 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3357 TRUE);
3358 ui_write((char_u *)"\r\n", 2, TRUE);
3359 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003360 else
3361 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003362 SOURCING_LNUM = iptr->isn_lnum;
3363 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003364 }
3365 }
3366 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003367 ga_clear(&ga);
3368 }
3369 break;
3370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 // load local variable or argument
3372 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003373 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003374 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003376 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 break;
3378
3379 // load v: variable
3380 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003381 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003382 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003384 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 break;
3386
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003387 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 case ISN_LOADSCRIPT:
3389 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003390 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 svar_T *sv;
3392
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003393 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003394 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003395 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003396 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003397 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003398 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003400 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 }
3402 break;
3403
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003404 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003406 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003408 int sid = iptr->isn_arg.loadstore.ls_sid;
3409 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003410 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 if (di == NULL)
3414 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003415 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003416 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003417 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 }
3419 else
3420 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003421 if (iptr->isn_type == ISN_LOADEXPORT)
3422 {
3423 int idx = get_script_item_idx(sid, name, 0,
3424 NULL, NULL);
3425 svar_T *sv;
3426
3427 if (idx >= 0)
3428 {
3429 sv = ((svar_T *)SCRIPT_ITEM(sid)
3430 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003431 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003432 {
3433 SOURCING_LNUM = iptr->isn_lnum;
3434 semsg(_(e_item_not_exported_in_script_str),
3435 name);
3436 goto on_error;
3437 }
3438 }
3439 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003440 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003441 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003443 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 }
3445 }
3446 break;
3447
Bram Moolenaard3aac292020-04-19 14:32:17 +02003448 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003450 case ISN_LOADB:
3451 case ISN_LOADW:
3452 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003454 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003455
Bram Moolenaar06b77222022-01-25 15:51:56 +00003456 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003457 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003458 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003459 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 break;
3463
Bram Moolenaar03290b82020-12-19 16:30:44 +01003464 // load autoload variable
3465 case ISN_LOADAUTO:
3466 {
3467 char_u *name = iptr->isn_arg.string;
3468
Bram Moolenaar35578162021-08-02 19:10:38 +02003469 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003470 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003471 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003472 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003473 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003474 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003475 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003476 }
3477 break;
3478
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003479 // load g:/b:/w:/t: namespace
3480 case ISN_LOADGDICT:
3481 case ISN_LOADBDICT:
3482 case ISN_LOADWDICT:
3483 case ISN_LOADTDICT:
3484 {
3485 dict_T *d = NULL;
3486
3487 switch (iptr->isn_type)
3488 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003489 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3490 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3491 case ISN_LOADWDICT: d = curwin->w_vars; break;
3492 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003493 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003494 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003495 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003496 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003497 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003498 tv = STACK_TV_BOT(0);
3499 tv->v_type = VAR_DICT;
3500 tv->v_lock = 0;
3501 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003502 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003503 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003504 }
3505 break;
3506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 // load &option
3508 case ISN_LOADOPT:
3509 {
3510 typval_T optval;
3511 char_u *name = iptr->isn_arg.string;
3512
Bram Moolenaara8c17702020-04-01 21:17:24 +02003513 // This is not expected to fail, name is checked during
3514 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003515 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003516 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003517 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003518 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003520 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 }
3522 break;
3523
3524 // load $ENV
3525 case ISN_LOADENV:
3526 {
3527 typval_T optval;
3528 char_u *name = iptr->isn_arg.string;
3529
Bram Moolenaar35578162021-08-02 19:10:38 +02003530 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003531 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003532 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003533 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003535 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 }
3537 break;
3538
3539 // load @register
3540 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003541 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003542 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 tv = STACK_TV_BOT(0);
3544 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003545 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003546 // This may result in NULL, which should be equivalent to an
3547 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 tv->vval.v_string = get_reg_contents(
3549 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003550 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 break;
3552
3553 // store local variable
3554 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003555 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 tv = STACK_TV_VAR(iptr->isn_arg.number);
3557 clear_tv(tv);
3558 *tv = *STACK_TV_BOT(0);
3559 break;
3560
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003561 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003562 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003563 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003564 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003565 int sid = iptr->isn_arg.loadstore.ls_sid;
3566 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003567 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003568 dictitem_T *di = find_var_in_ht(ht, 0,
3569 iptr->isn_type == ISN_STORES
3570 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003571
Bram Moolenaar4c137212021-04-19 16:48:48 +02003572 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003573 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003574 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003575 {
3576 if (iptr->isn_type == ISN_STOREEXPORT)
3577 {
3578 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003579 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003580 goto on_error;
3581 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003582 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003583 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003584 else
3585 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003586 if (iptr->isn_type == ISN_STOREEXPORT)
3587 {
3588 int idx = get_script_item_idx(sid, name, 0,
3589 NULL, NULL);
3590
Bram Moolenaar06651632022-04-27 17:54:25 +01003591 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003592 if (idx >= 0)
3593 {
3594 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3595 ->sn_var_vals.ga_data) + idx;
3596
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003597 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003598 {
3599 semsg(_(e_item_not_exported_in_script_str),
3600 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003601 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003602 goto on_error;
3603 }
3604 }
3605 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003606 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003607 {
3608 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003609 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003610 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003611 clear_tv(&di->di_tv);
3612 di->di_tv = *STACK_TV_BOT(0);
3613 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003614 }
3615 break;
3616
3617 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 case ISN_STORESCRIPT:
3619 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003620 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3621 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003623 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003624 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003625 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003626 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003627
3628 // "const" and "final" are checked at compile time, locking
3629 // the value needs to be checked here.
3630 SOURCING_LNUM = iptr->isn_lnum;
3631 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003632 {
3633 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003634 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003635 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 clear_tv(sv->sv_tv);
3638 *sv->sv_tv = *STACK_TV_BOT(0);
3639 }
3640 break;
3641
3642 // store option
3643 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003644 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003646 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3647 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 long n = 0;
3649 char_u *s = NULL;
3650 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003651 char_u numbuf[NUMBUFLEN];
3652 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653
Bram Moolenaar4c137212021-04-19 16:48:48 +02003654 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 tv = STACK_TV_BOT(0);
3656 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003657 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003659 if (s == NULL)
3660 s = (char_u *)"";
3661 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003662 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3663 {
3664 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003665 // If the option can be set to a function reference or
3666 // a lambda and the passed value is a function
3667 // reference, then convert it to the name (string) of
3668 // the function reference.
3669 s = tv2string(tv, &tofree, numbuf, 0);
3670 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003671 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003672 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003673 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003674 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003675 goto on_error;
3676 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003677 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003679 // must be VAR_NUMBER, CHECKTYPE makes sure
3680 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003681 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003682 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003683 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 if (msg != NULL)
3685 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003686 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003688 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 }
3691 break;
3692
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003693 // store $ENV
3694 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003695 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003696 tv = STACK_TV_BOT(0);
3697 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3698 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003699 break;
3700
3701 // store @r
3702 case ISN_STOREREG:
3703 {
3704 int reg = iptr->isn_arg.number;
3705
Bram Moolenaar4c137212021-04-19 16:48:48 +02003706 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003707 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003708 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003709 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003710 }
3711 break;
3712
3713 // store v: variable
3714 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003715 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003716 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3717 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003718 // should not happen, type is checked when compiling
3719 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003720 break;
3721
Bram Moolenaard3aac292020-04-19 14:32:17 +02003722 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003724 case ISN_STOREB:
3725 case ISN_STOREW:
3726 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003728 dictitem_T *di;
3729 hashtab_T *ht;
3730 char_u *name = iptr->isn_arg.string + 2;
3731
Bram Moolenaard3aac292020-04-19 14:32:17 +02003732 switch (iptr->isn_type)
3733 {
3734 case ISN_STOREG:
3735 ht = get_globvar_ht();
3736 break;
3737 case ISN_STOREB:
3738 ht = &curbuf->b_vars->dv_hashtab;
3739 break;
3740 case ISN_STOREW:
3741 ht = &curwin->w_vars->dv_hashtab;
3742 break;
3743 case ISN_STORET:
3744 ht = &curtab->tp_vars->dv_hashtab;
3745 break;
3746 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003747 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749
Bram Moolenaar4c137212021-04-19 16:48:48 +02003750 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003751 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003753 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 else
3755 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003756 SOURCING_LNUM = iptr->isn_lnum;
3757 if (var_check_permission(di, name) == FAIL)
3758 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 clear_tv(&di->di_tv);
3760 di->di_tv = *STACK_TV_BOT(0);
3761 }
3762 }
3763 break;
3764
Bram Moolenaar03290b82020-12-19 16:30:44 +01003765 // store an autoload variable
3766 case ISN_STOREAUTO:
3767 SOURCING_LNUM = iptr->isn_lnum;
3768 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3769 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003770 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003771 break;
3772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 // store number in local variable
3774 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003775 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 clear_tv(tv);
3777 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003778 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 break;
3780
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003781 // store value in list or dict variable
3782 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003783 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003784 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003785
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003786 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003787 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003788 if (res == NOTDONE)
3789 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003790 }
3791 break;
3792
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003793 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003794 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003795 if (execute_storerange(iptr, ectx) == FAIL)
3796 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003797 break;
3798
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003799 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003800 case ISN_LOADOUTER:
3801 case ISN_STOREOUTER:
3802 {
3803 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003804 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3805 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003806
3807 while (depth > 1 && outer != NULL)
3808 {
3809 outer = outer->out_up;
3810 --depth;
3811 }
3812 if (outer == NULL)
3813 {
3814 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003815 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3816 || ectx->ec_outer_ref == NULL)
3817 // Possibly :def function called from legacy
3818 // context.
3819 emsg(_(e_closure_called_from_invalid_context));
3820 else
3821 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003822 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003823 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003824 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003825 // Variable declared in loop. May be copied if the
3826 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01003827 tv = ((typval_T *)outer->out_loop[-depth - 1]
3828 .stack->ga_data)
3829 + outer->out_loop[-depth - 1].var_idx
3830 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003831 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003832 // Variable declared in a function. May be copied if
3833 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003834 tv = ((typval_T *)outer->out_stack->ga_data)
3835 + outer->out_frame_idx + STACK_FRAME_SIZE
3836 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003837 if (iptr->isn_type == ISN_LOADOUTER)
3838 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003839 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003840 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003841 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003842 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003843 }
3844 else
3845 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003846 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003847 clear_tv(tv);
3848 *tv = *STACK_TV_BOT(0);
3849 }
3850 }
3851 break;
3852
Bram Moolenaar752fc692021-01-04 21:57:11 +01003853 // unlet item in list or dict variable
3854 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003855 if (execute_unletindex(iptr, ectx) == FAIL)
3856 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003857 break;
3858
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003859 // unlet range of items in list variable
3860 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003861 if (execute_unletrange(iptr, ectx) == FAIL)
3862 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003863 break;
3864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 // push constant
3866 case ISN_PUSHNR:
3867 case ISN_PUSHBOOL:
3868 case ISN_PUSHSPEC:
3869 case ISN_PUSHF:
3870 case ISN_PUSHS:
3871 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003872 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003873 case ISN_PUSHCHANNEL:
3874 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003875 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003876 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003878 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003879 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 switch (iptr->isn_type)
3881 {
3882 case ISN_PUSHNR:
3883 tv->v_type = VAR_NUMBER;
3884 tv->vval.v_number = iptr->isn_arg.number;
3885 break;
3886 case ISN_PUSHBOOL:
3887 tv->v_type = VAR_BOOL;
3888 tv->vval.v_number = iptr->isn_arg.number;
3889 break;
3890 case ISN_PUSHSPEC:
3891 tv->v_type = VAR_SPECIAL;
3892 tv->vval.v_number = iptr->isn_arg.number;
3893 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 case ISN_PUSHF:
3895 tv->v_type = VAR_FLOAT;
3896 tv->vval.v_float = iptr->isn_arg.fnumber;
3897 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 case ISN_PUSHBLOB:
3899 blob_copy(iptr->isn_arg.blob, tv);
3900 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003901 case ISN_PUSHFUNC:
3902 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003903 if (iptr->isn_arg.string == NULL)
3904 tv->vval.v_string = NULL;
3905 else
3906 tv->vval.v_string =
3907 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003908 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003909 case ISN_PUSHCHANNEL:
3910#ifdef FEAT_JOB_CHANNEL
3911 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003912 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003913#endif
3914 break;
3915 case ISN_PUSHJOB:
3916#ifdef FEAT_JOB_CHANNEL
3917 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003918 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003919#endif
3920 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 default:
3922 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003923 tv->vval.v_string = iptr->isn_arg.string == NULL
3924 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 }
3926 break;
3927
Bram Moolenaar06b77222022-01-25 15:51:56 +00003928 case ISN_AUTOLOAD:
3929 {
3930 char_u *name = iptr->isn_arg.string;
3931
3932 (void)script_autoload(name, FALSE);
3933 if (find_func(name, TRUE))
3934 {
3935 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3936 goto theend;
3937 tv = STACK_TV_BOT(0);
3938 tv->v_lock = 0;
3939 ++ectx->ec_stack.ga_len;
3940 tv->v_type = VAR_FUNC;
3941 tv->vval.v_string = vim_strsave(name);
3942 }
3943 else
3944 {
3945 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3946
3947 if (res == NOTDONE)
3948 goto theend;
3949 if (res == FAIL)
3950 goto on_error;
3951 }
3952 }
3953 break;
3954
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003955 case ISN_UNLET:
3956 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3957 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003958 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003959 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003960 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003961 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003962 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003963
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003964 case ISN_LOCKUNLOCK:
3965 {
3966 typval_T *lval_root_save = lval_root;
3967 int res;
3968
3969 // Stack has the local variable, argument the whole :lock
3970 // or :unlock command, like ISN_EXEC.
3971 --ectx->ec_stack.ga_len;
3972 lval_root = STACK_TV_BOT(0);
3973 res = exec_command(iptr);
3974 clear_tv(lval_root);
3975 lval_root = lval_root_save;
3976 if (res == FAIL)
3977 goto on_error;
3978 }
3979 break;
3980
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003981 case ISN_LOCKCONST:
3982 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3983 break;
3984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 // create a list from items on the stack; uses a single allocation
3986 // for the list header and the items
3987 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003988 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003989 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 break;
3991
3992 // create a dict from items on the stack
3993 case ISN_NEWDICT:
3994 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003995 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003997 SOURCING_LNUM = iptr->isn_lnum;
3998 res = exe_newdict(iptr->isn_arg.number, ectx);
3999 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004000 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004001 if (res == MAYBE)
4002 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 }
4004 break;
4005
LemonBoy372bcce2022-04-25 12:43:20 +01004006 case ISN_CONCAT:
4007 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4008 goto theend;
4009 break;
4010
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004011 // create a partial with NULL value
4012 case ISN_NEWPARTIAL:
4013 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4014 goto theend;
4015 ++ectx->ec_stack.ga_len;
4016 tv = STACK_TV_BOT(-1);
4017 tv->v_type = VAR_PARTIAL;
4018 tv->v_lock = 0;
4019 tv->vval.v_partial = NULL;
4020 break;
4021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 // call a :def function
4023 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004024 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004025 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4026 NULL,
4027 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004028 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004029 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 break;
4031
4032 // call a builtin function
4033 case ISN_BCALL:
4034 SOURCING_LNUM = iptr->isn_lnum;
4035 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4036 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004037 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004038 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 break;
4040
4041 // call a funcref or partial
4042 case ISN_PCALL:
4043 {
4044 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4045 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004046 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047
4048 SOURCING_LNUM = iptr->isn_lnum;
4049 if (pfunc->cpf_top)
4050 {
4051 // funcref is above the arguments
4052 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4053 }
4054 else
4055 {
4056 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004057 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004058 partial_tv = *STACK_TV_BOT(0);
4059 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004061 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004062 if (tv == &partial_tv)
4063 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004065 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 }
4067 break;
4068
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004069 case ISN_PCALL_END:
4070 // PCALL finished, arguments have been consumed and replaced by
4071 // the return value. Now clear the funcref from the stack,
4072 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004073 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004074 clear_tv(STACK_TV_BOT(-1));
4075 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4076 break;
4077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 // call a user defined function or funcref/partial
4079 case ISN_UCALL:
4080 {
4081 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4082
4083 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004084 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004085 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004086 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 }
4088 break;
4089
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004090 // :defer func(arg)
4091 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004092 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004093 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4094 goto on_error;
4095 break;
4096
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004097 // return from a :def function call without a value
4098 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02004099 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004100 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004101 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004102 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004103 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004104 tv->vval.v_number = 0;
4105 tv->v_lock = 0;
4106 // FALLTHROUGH
4107
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004108 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 case ISN_RETURN:
4110 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004111 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004112 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004113
4114 if (trystack->ga_len > 0)
4115 trycmd = ((trycmd_T *)trystack->ga_data)
4116 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004117 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004118 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004120 // jump to ":finally" or ":endtry"
4121 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004122 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004123 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004124 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 trycmd->tcd_return = TRUE;
4126 }
4127 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004128 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 }
4130 break;
4131
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004132 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 case ISN_FUNCREF:
4134 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004135 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4136 ufunc_T *ufunc;
4137 funcref_T *funcref = &iptr->isn_arg.funcref;
4138 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004141 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004142 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004143 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004144 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004145 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004146 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004147 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004148 {
4149 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4150 + funcref->fr_dfunc_idx;
4151
4152 ufunc = pt_dfunc->df_ufunc;
4153 }
4154 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004155 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004156 if (ufunc == NULL)
4157 {
4158 SOURCING_LNUM = iptr->isn_lnum;
4159 iemsg("ufunc unexpectedly NULL for FUNCREF");
4160 goto theend;
4161 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004162 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004163 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004164 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004165 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004167 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 tv->vval.v_partial = pt;
4169 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004170 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 }
4172 break;
4173
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004174 // Create a global function from a lambda.
4175 case ISN_NEWFUNC:
4176 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004177 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004178
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004179 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004180 arg->nfa_global, &arg->nfa_loopvar_info,
4181 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004182 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004183 }
4184 break;
4185
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004186 // List functions
4187 case ISN_DEF:
4188 if (iptr->isn_arg.string == NULL)
4189 list_functions(NULL);
4190 else
4191 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004192 exarg_T ea;
4193 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004194
4195 CLEAR_FIELD(ea);
4196 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004197 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4198 define_function(&ea, NULL, &lines_to_free);
4199 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004200 }
4201 break;
4202
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 // jump if a condition is met
4204 case ISN_JUMP:
4205 {
4206 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004207 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 int jump = TRUE;
4209
4210 if (when != JUMP_ALWAYS)
4211 {
4212 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004213 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004214 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004215 || when == JUMP_IF_COND_TRUE)
4216 {
4217 SOURCING_LNUM = iptr->isn_lnum;
4218 jump = tv_get_bool_chk(tv, &error);
4219 if (error)
4220 goto on_error;
4221 }
4222 else
4223 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004224 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004226 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 {
4228 // drop the value from the stack
4229 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004230 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 }
4232 }
4233 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004234 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 }
4236 break;
4237
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004238 // "while": jump to end if a condition is false
4239 case ISN_WHILE:
4240 {
4241 int error = FALSE;
4242 int jump = TRUE;
4243
4244 tv = STACK_TV_BOT(-1);
4245 SOURCING_LNUM = iptr->isn_lnum;
4246 jump = !tv_get_bool_chk(tv, &error);
4247 if (error)
4248 goto on_error;
4249 // drop the value from the stack
4250 clear_tv(tv);
4251 --ectx->ec_stack.ga_len;
4252 if (jump)
4253 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4254
4255 // Store the current funccal count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004256 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004257 tv = STACK_TV_VAR(
4258 iptr->isn_arg.whileloop.while_funcref_idx);
4259 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4260 }
4261 break;
4262
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004263 // Jump if an argument with a default value was already set and not
4264 // v:none.
4265 case ISN_JUMP_IF_ARG_SET:
4266 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
4267 if (tv->v_type != VAR_UNKNOWN
4268 && !(tv->v_type == VAR_SPECIAL
4269 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004270 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004271 break;
4272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 // top of a for loop
4274 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004275 if (execute_for(iptr, ectx) == FAIL)
4276 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 break;
4278
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004279 // end of a for or while loop
4280 case ISN_ENDLOOP:
4281 if (execute_endloop(iptr, ectx) == FAIL)
4282 goto theend;
4283 break;
4284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 // start of ":try" block
4286 case ISN_TRY:
4287 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004288 trycmd_T *trycmd = NULL;
4289
Bram Moolenaar35578162021-08-02 19:10:38 +02004290 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004291 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004292 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4293 + ectx->ec_trystack.ga_len;
4294 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004296 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004297 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4298 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004299 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004300 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004301 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004302 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004303 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004304 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 }
4306 break;
4307
4308 case ISN_PUSHEXC:
4309 if (current_exception == NULL)
4310 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004311 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004313 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004315 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004316 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004318 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004320 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 tv->vval.v_string = vim_strsave(
4322 (char_u *)current_exception->value);
4323 break;
4324
4325 case ISN_CATCH:
4326 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004327 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004328 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329
Bram Moolenaar4c137212021-04-19 16:48:48 +02004330 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004331 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004333 trycmd->tcd_caught = TRUE;
4334 trycmd->tcd_did_throw = FALSE;
4335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004337 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 catch_exception(current_exception);
4339 }
4340 break;
4341
Bram Moolenaarc150c092021-02-13 15:02:46 +01004342 case ISN_TRYCONT:
4343 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004344 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004345 trycont_T *trycont = &iptr->isn_arg.trycont;
4346 int i;
4347 trycmd_T *trycmd;
4348 int iidx = trycont->tct_where;
4349
4350 if (trystack->ga_len < trycont->tct_levels)
4351 {
4352 siemsg("TRYCONT: expected %d levels, found %d",
4353 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004354 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004355 }
4356 // Make :endtry jump to any outer try block and the last
4357 // :endtry inside the loop to the loop start.
4358 for (i = trycont->tct_levels; i > 0; --i)
4359 {
4360 trycmd = ((trycmd_T *)trystack->ga_data)
4361 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004362 // Add one to tcd_cont to be able to jump to
4363 // instruction with index zero.
4364 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004365 iidx = trycmd->tcd_finally_idx == 0
4366 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004367 }
4368 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004369 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004370 }
4371 break;
4372
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004373 case ISN_FINALLY:
4374 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004375 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004376 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4377 + trystack->ga_len - 1;
4378
4379 // Reset the index to avoid a return statement jumps here
4380 // again.
4381 trycmd->tcd_finally_idx = 0;
4382 break;
4383 }
4384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 // end of ":try" block
4386 case ISN_ENDTRY:
4387 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004388 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004389 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390
Bram Moolenaar06651632022-04-27 17:54:25 +01004391 --trystack->ga_len;
4392 --trylevel;
4393 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4394 if (trycmd->tcd_did_throw)
4395 did_throw = TRUE;
4396 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004398 // discard the exception
4399 if (caught_stack == current_exception)
4400 caught_stack = caught_stack->caught;
4401 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004403
4404 if (trycmd->tcd_return)
4405 goto func_return;
4406
4407 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4408 {
4409 --ectx->ec_stack.ga_len;
4410 clear_tv(STACK_TV_BOT(0));
4411 }
4412 if (trycmd->tcd_cont != 0)
4413 // handling :continue: jump to outer try block or
4414 // start of the loop
4415 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 }
4417 break;
4418
4419 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004420 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004421 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004422
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004423 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4424 {
4425 // throwing an exception while using "silent!" causes
4426 // the function to abort but not display an error.
4427 tv = STACK_TV_BOT(-1);
4428 clear_tv(tv);
4429 tv->v_type = VAR_NUMBER;
4430 tv->vval.v_number = 0;
4431 goto done;
4432 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004433 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004434 tv = STACK_TV_BOT(0);
4435 if (tv->vval.v_string == NULL
4436 || *skipwhite(tv->vval.v_string) == NUL)
4437 {
4438 vim_free(tv->vval.v_string);
4439 SOURCING_LNUM = iptr->isn_lnum;
4440 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004441 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004442 }
4443
4444 // Inside a "catch" we need to first discard the caught
4445 // exception.
4446 if (trystack->ga_len > 0)
4447 {
4448 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4449 + trystack->ga_len - 1;
4450 if (trycmd->tcd_caught && current_exception != NULL)
4451 {
4452 // discard the exception
4453 if (caught_stack == current_exception)
4454 caught_stack = caught_stack->caught;
4455 discard_current_exception();
4456 trycmd->tcd_caught = FALSE;
4457 }
4458 }
4459
Bram Moolenaar90a57162022-02-12 14:23:17 +00004460 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004461 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4462 == FAIL)
4463 {
4464 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004465 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004466 }
4467 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 break;
4470
4471 // compare with special values
4472 case ISN_COMPAREBOOL:
4473 case ISN_COMPARESPECIAL:
4474 {
4475 typval_T *tv1 = STACK_TV_BOT(-2);
4476 typval_T *tv2 = STACK_TV_BOT(-1);
4477 varnumber_T arg1 = tv1->vval.v_number;
4478 varnumber_T arg2 = tv2->vval.v_number;
4479 int res;
4480
Bram Moolenaar06651632022-04-27 17:54:25 +01004481 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4482 res = arg1 == arg2;
4483 else
4484 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485
Bram Moolenaar4c137212021-04-19 16:48:48 +02004486 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 tv1->v_type = VAR_BOOL;
4488 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4489 }
4490 break;
4491
Bram Moolenaar7a222242022-03-01 19:23:24 +00004492 case ISN_COMPARENULL:
4493 {
4494 typval_T *tv1 = STACK_TV_BOT(-2);
4495 typval_T *tv2 = STACK_TV_BOT(-1);
4496 int res;
4497
4498 res = typval_compare_null(tv1, tv2);
4499 if (res == MAYBE)
4500 goto on_error;
4501 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4502 res = !res;
4503 clear_tv(tv1);
4504 clear_tv(tv2);
4505 --ectx->ec_stack.ga_len;
4506 tv1->v_type = VAR_BOOL;
4507 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4508 }
4509 break;
4510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 // Operation with two number arguments
4512 case ISN_OPNR:
4513 case ISN_COMPARENR:
4514 {
4515 typval_T *tv1 = STACK_TV_BOT(-2);
4516 typval_T *tv2 = STACK_TV_BOT(-1);
4517 varnumber_T arg1 = tv1->vval.v_number;
4518 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004519 varnumber_T res = 0;
4520 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004522 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4523 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4524 {
4525 if (arg2 < 0)
4526 {
4527 SOURCING_LNUM = iptr->isn_lnum;
4528 emsg(_(e_bitshift_ops_must_be_postive));
4529 goto on_error;
4530 }
4531 }
4532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 switch (iptr->isn_arg.op.op_type)
4534 {
4535 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004536 case EXPR_DIV: if (arg2 == 0)
4537 div_zero = TRUE;
4538 else
4539 res = arg1 / arg2;
4540 break;
4541 case EXPR_REM: if (arg2 == 0)
4542 div_zero = TRUE;
4543 else
4544 res = arg1 % arg2;
4545 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 case EXPR_SUB: res = arg1 - arg2; break;
4547 case EXPR_ADD: res = arg1 + arg2; break;
4548
4549 case EXPR_EQUAL: res = arg1 == arg2; break;
4550 case EXPR_NEQUAL: res = arg1 != arg2; break;
4551 case EXPR_GREATER: res = arg1 > arg2; break;
4552 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4553 case EXPR_SMALLER: res = arg1 < arg2; break;
4554 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004555 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4556 res = 0;
4557 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004558 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004559 break;
4560 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4561 res = 0;
4562 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004563 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004564 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004565 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 }
4567
Bram Moolenaar4c137212021-04-19 16:48:48 +02004568 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 if (iptr->isn_type == ISN_COMPARENR)
4570 {
4571 tv1->v_type = VAR_BOOL;
4572 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4573 }
4574 else
4575 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004576 if (div_zero)
4577 {
4578 SOURCING_LNUM = iptr->isn_lnum;
4579 emsg(_(e_divide_by_zero));
4580 goto on_error;
4581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 }
4583 break;
4584
4585 // Computation with two float arguments
4586 case ISN_OPFLOAT:
4587 case ISN_COMPAREFLOAT:
4588 {
4589 typval_T *tv1 = STACK_TV_BOT(-2);
4590 typval_T *tv2 = STACK_TV_BOT(-1);
4591 float_T arg1 = tv1->vval.v_float;
4592 float_T arg2 = tv2->vval.v_float;
4593 float_T res = 0;
4594 int cmp = FALSE;
4595
4596 switch (iptr->isn_arg.op.op_type)
4597 {
4598 case EXPR_MULT: res = arg1 * arg2; break;
4599 case EXPR_DIV: res = arg1 / arg2; break;
4600 case EXPR_SUB: res = arg1 - arg2; break;
4601 case EXPR_ADD: res = arg1 + arg2; break;
4602
4603 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4604 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4605 case EXPR_GREATER: cmp = arg1 > arg2; break;
4606 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4607 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4608 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4609 default: cmp = 0; break;
4610 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004611 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 if (iptr->isn_type == ISN_COMPAREFLOAT)
4613 {
4614 tv1->v_type = VAR_BOOL;
4615 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4616 }
4617 else
4618 tv1->vval.v_float = res;
4619 }
4620 break;
4621
4622 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004623 case ISN_COMPAREDICT:
4624 case ISN_COMPAREFUNC:
4625 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 case ISN_COMPAREBLOB:
4627 {
4628 typval_T *tv1 = STACK_TV_BOT(-2);
4629 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004630 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4631 int ic = iptr->isn_arg.op.op_ic;
4632 int res = FALSE;
4633 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634
Bram Moolenaar265f8112021-12-19 12:33:05 +00004635 SOURCING_LNUM = iptr->isn_lnum;
4636 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004638 status = typval_compare_list(tv1, tv2,
4639 exprtype, ic, &res);
4640 }
4641 else if (iptr->isn_type == ISN_COMPAREDICT)
4642 {
4643 status = typval_compare_dict(tv1, tv2,
4644 exprtype, ic, &res);
4645 }
4646 else if (iptr->isn_type == ISN_COMPAREFUNC)
4647 {
4648 status = typval_compare_func(tv1, tv2,
4649 exprtype, ic, &res);
4650 }
4651 else if (iptr->isn_type == ISN_COMPARESTRING)
4652 {
4653 status = typval_compare_string(tv1, tv2,
4654 exprtype, ic, &res);
4655 }
4656 else
4657 {
4658 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004660 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 clear_tv(tv1);
4662 clear_tv(tv2);
4663 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004664 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4665 if (status == FAIL)
4666 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 }
4668 break;
4669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 case ISN_COMPAREANY:
4671 {
4672 typval_T *tv1 = STACK_TV_BOT(-2);
4673 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004674 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004676 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677
Bram Moolenaareb26f432020-09-14 16:50:05 +02004678 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004679 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004681 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004682 if (status == FAIL)
4683 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 }
4685 break;
4686
4687 case ISN_ADDLIST:
4688 case ISN_ADDBLOB:
4689 {
4690 typval_T *tv1 = STACK_TV_BOT(-2);
4691 typval_T *tv2 = STACK_TV_BOT(-1);
4692
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004693 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004695 {
4696 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4697 && tv1->vval.v_list != NULL)
4698 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4699 NULL);
4700 else
4701 eval_addlist(tv1, tv2);
4702 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 else
4704 eval_addblob(tv1, tv2);
4705 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004706 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 }
4708 break;
4709
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004710 case ISN_LISTAPPEND:
4711 {
4712 typval_T *tv1 = STACK_TV_BOT(-2);
4713 typval_T *tv2 = STACK_TV_BOT(-1);
4714 list_T *l = tv1->vval.v_list;
4715
4716 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004717 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004718 if (l == NULL)
4719 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004720 emsg(_(e_cannot_add_to_null_list));
4721 goto on_error;
4722 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004723 if (value_check_lock(l->lv_lock, NULL, FALSE))
4724 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004725 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004726 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004727 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004728 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004729 }
4730 break;
4731
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004732 case ISN_BLOBAPPEND:
4733 {
4734 typval_T *tv1 = STACK_TV_BOT(-2);
4735 typval_T *tv2 = STACK_TV_BOT(-1);
4736 blob_T *b = tv1->vval.v_blob;
4737 int error = FALSE;
4738 varnumber_T n;
4739
4740 // add a number to a blob
4741 if (b == NULL)
4742 {
4743 SOURCING_LNUM = iptr->isn_lnum;
4744 emsg(_(e_cannot_add_to_null_blob));
4745 goto on_error;
4746 }
4747 n = tv_get_number_chk(tv2, &error);
4748 if (error)
4749 goto on_error;
4750 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004751 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004752 }
4753 break;
4754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 // Computation with two arguments of unknown type
4756 case ISN_OPANY:
4757 {
4758 typval_T *tv1 = STACK_TV_BOT(-2);
4759 typval_T *tv2 = STACK_TV_BOT(-1);
4760 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 int error = FALSE;
4763
4764 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4765 {
4766 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4767 {
4768 eval_addlist(tv1, tv2);
4769 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004770 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 break;
4772 }
4773 else if (tv1->v_type == VAR_BLOB
4774 && tv2->v_type == VAR_BLOB)
4775 {
4776 eval_addblob(tv1, tv2);
4777 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004778 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 break;
4780 }
4781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782 if (tv1->v_type == VAR_FLOAT)
4783 {
4784 f1 = tv1->vval.v_float;
4785 n1 = 0;
4786 }
4787 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004789 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 n1 = tv_get_number_chk(tv1, &error);
4791 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004792 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 if (tv2->v_type == VAR_FLOAT)
4794 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 if (tv2->v_type == VAR_FLOAT)
4797 {
4798 f2 = tv2->vval.v_float;
4799 n2 = 0;
4800 }
4801 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 {
4803 n2 = tv_get_number_chk(tv2, &error);
4804 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004805 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 if (tv1->v_type == VAR_FLOAT)
4807 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 // if there is a float on either side the result is a float
4810 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4811 {
4812 switch (iptr->isn_arg.op.op_type)
4813 {
4814 case EXPR_MULT: f1 = f1 * f2; break;
4815 case EXPR_DIV: f1 = f1 / f2; break;
4816 case EXPR_SUB: f1 = f1 - f2; break;
4817 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004818 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004819 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004820 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 }
4822 clear_tv(tv1);
4823 clear_tv(tv2);
4824 tv1->v_type = VAR_FLOAT;
4825 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004826 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827 }
4828 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004830 int failed = FALSE;
4831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 switch (iptr->isn_arg.op.op_type)
4833 {
4834 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004835 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4836 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004837 goto on_error;
4838 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 case EXPR_SUB: n1 = n1 - n2; break;
4840 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004841 default: n1 = num_modulus(n1, n2, &failed);
4842 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004843 goto on_error;
4844 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 }
4846 clear_tv(tv1);
4847 clear_tv(tv2);
4848 tv1->v_type = VAR_NUMBER;
4849 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004850 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 }
4852 }
4853 break;
4854
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004855 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004856 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004857 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004858 int is_slice = iptr->isn_type == ISN_STRSLICE;
4859 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004860 char_u *res;
4861
4862 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004863 // string slice: string is at stack-3, first index at
4864 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004865 if (is_slice)
4866 {
4867 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004868 n1 = tv->vval.v_number;
4869 }
4870
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004871 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004872 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004873
Bram Moolenaar4c137212021-04-19 16:48:48 +02004874 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004875 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004876 if (is_slice)
4877 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004878 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004879 else
4880 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004881 // single character (including composing characters).
4882 // If the index is too big or negative the result is
4883 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004884 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004885 vim_free(tv->vval.v_string);
4886 tv->vval.v_string = res;
4887 }
4888 break;
4889
4890 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004891 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004892 case ISN_BLOBINDEX:
4893 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004895 int is_slice = iptr->isn_type == ISN_LISTSLICE
4896 || iptr->isn_type == ISN_BLOBSLICE;
4897 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4898 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004899 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004900 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901
4902 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004903 // list slice: list is at stack-3, indexes at stack-2 and
4904 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004905 // Same for blob.
4906 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907
4908 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004909 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004911
4912 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913 {
Bram Moolenaared591872020-08-15 22:14:53 +02004914 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004915 n1 = tv->vval.v_number;
4916 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 }
Bram Moolenaared591872020-08-15 22:14:53 +02004918
Bram Moolenaar4c137212021-04-19 16:48:48 +02004919 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004920 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004921 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004922 if (is_blob)
4923 {
4924 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4925 n1, n2, FALSE, tv) == FAIL)
4926 goto on_error;
4927 }
4928 else
4929 {
4930 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4931 n1, n2, FALSE, tv, TRUE) == FAIL)
4932 goto on_error;
4933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 }
4935 break;
4936
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004937 case ISN_ANYINDEX:
4938 case ISN_ANYSLICE:
4939 {
4940 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4941 typval_T *var1, *var2;
4942 int res;
4943
4944 // index: composite is at stack-2, index at stack-1
4945 // slice: composite is at stack-3, indexes at stack-2 and
4946 // stack-1
4947 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004948 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004949 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4950 goto on_error;
4951 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4952 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004953 res = eval_index_inner(tv, is_slice, var1, var2,
4954 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004955 clear_tv(var1);
4956 if (is_slice)
4957 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004958 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004959 if (res == FAIL)
4960 goto on_error;
4961 }
4962 break;
4963
Bram Moolenaar9af78762020-06-16 11:34:42 +02004964 case ISN_SLICE:
4965 {
4966 list_T *list;
4967 int count = iptr->isn_arg.number;
4968
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004969 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004970 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004971 list = tv->vval.v_list;
4972
4973 // no error for short list, expect it to be checked earlier
4974 if (list != NULL && list->lv_len >= count)
4975 {
4976 list_T *newlist = list_slice(list,
4977 count, list->lv_len - 1);
4978
4979 if (newlist != NULL)
4980 {
4981 list_unref(list);
4982 tv->vval.v_list = newlist;
4983 ++newlist->lv_refcount;
4984 }
4985 }
4986 }
4987 break;
4988
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004989 case ISN_GETITEM:
4990 {
4991 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004992 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004993
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004994 // Get list item: list is at stack-1, push item.
4995 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004996 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4997 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004998
Bram Moolenaar35578162021-08-02 19:10:38 +02004999 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005000 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005001 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005002 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005003
5004 // Useful when used in unpack assignment. Reset at
5005 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005006 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005007 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005008 }
5009 break;
5010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 case ISN_MEMBER:
5012 {
5013 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005014 char_u *key;
5015 dictitem_T *di;
5016
5017 // dict member: dict is at stack-2, key at stack-1
5018 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005019 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005020 dict = tv->vval.v_dict;
5021
5022 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005023 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005024 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005025 if (key == NULL)
5026 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005027
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005028 if ((di = dict_find(dict, key, -1)) == NULL)
5029 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005030 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005031 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005032
5033 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005034 // stack contents makes sense and the dict stack is
5035 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005036 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005037 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005038 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005039 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005040 tv->v_type = VAR_NUMBER;
5041 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005042 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005043 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005044 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005045 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005046 // Put the dict used on the dict stack, it might be used by
5047 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005048 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005049 if (dict_stack_save(tv) == FAIL)
5050 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005051 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005052 }
5053 break;
5054
5055 // dict member with string key
5056 case ISN_STRINGMEMBER:
5057 {
5058 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 dictitem_T *di;
5060
5061 tv = STACK_TV_BOT(-1);
5062 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5063 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005064 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005065 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005066 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 }
5068 dict = tv->vval.v_dict;
5069
5070 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5071 == NULL)
5072 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005073 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00005074 semsg(_(e_key_not_present_in_dictionary),
5075 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005076 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005078 // Put the dict used on the dict stack, it might be used by
5079 // a dict function later.
5080 if (dict_stack_save(tv) == FAIL)
5081 goto on_fatal_error;
5082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005084 }
5085 break;
5086
5087 case ISN_CLEARDICT:
5088 dict_stack_drop();
5089 break;
5090
5091 case ISN_USEDICT:
5092 {
5093 typval_T *dict_tv = dict_stack_get_tv();
5094
5095 // Turn "dict.Func" into a partial for "Func" bound to
5096 // "dict". Don't do this when "Func" is already a partial
5097 // that was bound explicitly (pt_auto is FALSE).
5098 tv = STACK_TV_BOT(-1);
5099 if (dict_tv != NULL
5100 && dict_tv->v_type == VAR_DICT
5101 && dict_tv->vval.v_dict != NULL
5102 && (tv->v_type == VAR_FUNC
5103 || (tv->v_type == VAR_PARTIAL
5104 && (tv->vval.v_partial->pt_auto
5105 || tv->vval.v_partial->pt_dict == NULL))))
5106 dict_tv->vval.v_dict =
5107 make_partial(dict_tv->vval.v_dict, tv);
5108 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 }
5110 break;
5111
5112 case ISN_NEGATENR:
5113 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005114 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005115 if (tv->v_type == VAR_FLOAT)
5116 tv->vval.v_float = -tv->vval.v_float;
5117 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005118 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 break;
5120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 case ISN_CHECKTYPE:
5122 {
5123 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005124 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005125 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005127 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005128 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005129 if (!ectx->ec_where.wt_variable)
5130 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005131 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005132 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005133 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005134 if (r == FAIL)
5135 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005136 if (!ectx->ec_where.wt_variable)
5137 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005138
5139 // number 0 is FALSE, number 1 is TRUE
5140 if (tv->v_type == VAR_NUMBER
5141 && ct->ct_type->tt_type == VAR_BOOL
5142 && (tv->vval.v_number == 0
5143 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005145 tv->v_type = VAR_BOOL;
5146 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005147 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 }
5149 }
5150 break;
5151
Bram Moolenaar9af78762020-06-16 11:34:42 +02005152 case ISN_CHECKLEN:
5153 {
5154 int min_len = iptr->isn_arg.checklen.cl_min_len;
5155 list_T *list = NULL;
5156
5157 tv = STACK_TV_BOT(-1);
5158 if (tv->v_type == VAR_LIST)
5159 list = tv->vval.v_list;
5160 if (list == NULL || list->lv_len < min_len
5161 || (list->lv_len > min_len
5162 && !iptr->isn_arg.checklen.cl_more_OK))
5163 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005164 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005165 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005166 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005167 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005168 }
5169 }
5170 break;
5171
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005172 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005173 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005174 break;
5175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005177 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178 {
5179 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005180 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005182 if (iptr->isn_type == ISN_2BOOL)
5183 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005184 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005185 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005186 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005187 n = !n;
5188 }
5189 else
5190 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005191 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005192 SOURCING_LNUM = iptr->isn_lnum;
5193 n = tv_get_bool_chk(tv, &error);
5194 if (error)
5195 goto on_error;
5196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 clear_tv(tv);
5198 tv->v_type = VAR_BOOL;
5199 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5200 }
5201 break;
5202
5203 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005204 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005205 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005206 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5207 iptr->isn_type == ISN_2STRING_ANY,
5208 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005209 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 break;
5211
Bram Moolenaar08597872020-12-10 19:43:40 +01005212 case ISN_RANGE:
5213 {
5214 exarg_T ea;
5215 char *errormsg;
5216
Bram Moolenaarece0b872021-01-08 20:40:45 +01005217 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005218 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005219 ea.addr_type = ADDR_LINES;
5220 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005221 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005222 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005223 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005224
Bram Moolenaar35578162021-08-02 19:10:38 +02005225 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005226 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005227 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005228 tv = STACK_TV_BOT(-1);
5229 tv->v_type = VAR_NUMBER;
5230 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005231 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005232 }
5233 break;
5234
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005235 case ISN_PUT:
5236 {
5237 int regname = iptr->isn_arg.put.put_regname;
5238 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5239 char_u *expr = NULL;
5240 int dir = FORWARD;
5241
Bram Moolenaar08597872020-12-10 19:43:40 +01005242 if (lnum < -2)
5243 {
5244 // line number was put on the stack by ISN_RANGE
5245 tv = STACK_TV_BOT(-1);
5246 curwin->w_cursor.lnum = tv->vval.v_number;
5247 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5248 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005249 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005250 }
5251 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005252 // :put! above cursor
5253 dir = BACKWARD;
5254 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005255 {
5256 curwin->w_cursor.lnum = lnum;
5257 if (lnum == 0)
5258 // check_cursor() below will move to line 1
5259 dir = BACKWARD;
5260 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005261
5262 if (regname == '=')
5263 {
5264 tv = STACK_TV_BOT(-1);
5265 if (tv->v_type == VAR_STRING)
5266 expr = tv->vval.v_string;
5267 else
5268 {
5269 expr = typval2string(tv, TRUE); // allocates value
5270 clear_tv(tv);
5271 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005272 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005273 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005274 check_cursor();
5275 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5276 vim_free(expr);
5277 }
5278 break;
5279
Bram Moolenaar02194d22020-10-24 23:08:38 +02005280 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005281 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5282 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5283 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5284 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005285 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5286 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005287 break;
5288
Bram Moolenaar02194d22020-10-24 23:08:38 +02005289 case ISN_CMDMOD_REV:
5290 // filter regprog is owned by the instruction, don't free it
5291 cmdmod.cmod_filter_regmatch.regprog = NULL;
5292 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005293 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5294 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005295 break;
5296
Bram Moolenaar792f7862020-11-23 08:31:18 +01005297 case ISN_UNPACK:
5298 {
5299 int count = iptr->isn_arg.unpack.unp_count;
5300 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5301 list_T *l;
5302 listitem_T *li;
5303 int i;
5304
5305 // Check there is a valid list to unpack.
5306 tv = STACK_TV_BOT(-1);
5307 if (tv->v_type != VAR_LIST)
5308 {
5309 SOURCING_LNUM = iptr->isn_lnum;
5310 emsg(_(e_for_argument_must_be_sequence_of_lists));
5311 goto on_error;
5312 }
5313 l = tv->vval.v_list;
5314 if (l == NULL
5315 || l->lv_len < (semicolon ? count - 1 : count))
5316 {
5317 SOURCING_LNUM = iptr->isn_lnum;
5318 emsg(_(e_list_value_does_not_have_enough_items));
5319 goto on_error;
5320 }
5321 else if (!semicolon && l->lv_len > count)
5322 {
5323 SOURCING_LNUM = iptr->isn_lnum;
5324 emsg(_(e_list_value_has_more_items_than_targets));
5325 goto on_error;
5326 }
5327
5328 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005329 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005330 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005331 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005332
5333 // Variable after semicolon gets a list with the remaining
5334 // items.
5335 if (semicolon)
5336 {
5337 list_T *rem_list =
5338 list_alloc_with_items(l->lv_len - count + 1);
5339
5340 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005341 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005342 tv = STACK_TV_BOT(-count);
5343 tv->vval.v_list = rem_list;
5344 ++rem_list->lv_refcount;
5345 tv->v_lock = 0;
5346 li = l->lv_first;
5347 for (i = 0; i < count - 1; ++i)
5348 li = li->li_next;
5349 for (i = 0; li != NULL; ++i)
5350 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005351 typval_T tvcopy;
5352
5353 copy_tv(&li->li_tv, &tvcopy);
5354 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005355 li = li->li_next;
5356 }
5357 --count;
5358 }
5359
5360 // Produce the values in reverse order, first item last.
5361 li = l->lv_first;
5362 for (i = 0; i < count; ++i)
5363 {
5364 tv = STACK_TV_BOT(-i - 1);
5365 copy_tv(&li->li_tv, tv);
5366 li = li->li_next;
5367 }
5368
5369 list_unref(l);
5370 }
5371 break;
5372
Bram Moolenaarb2049902021-01-24 12:53:53 +01005373 case ISN_PROF_START:
5374 case ISN_PROF_END:
5375 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005376#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005377 funccall_T cookie;
5378 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005379 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005380 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005381
Bram Moolenaarca16c602022-09-06 18:57:08 +01005382 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005383 if (iptr->isn_type == ISN_PROF_START)
5384 {
5385 func_line_start(&cookie, iptr->isn_lnum);
5386 // if we get here the instruction is executed
5387 func_line_exec(&cookie);
5388 }
5389 else
5390 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005391#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005392 }
5393 break;
5394
Bram Moolenaare99d4222021-06-13 14:01:26 +02005395 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005396 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005397 break;
5398
Bram Moolenaar389df252020-07-09 21:20:47 +02005399 case ISN_SHUFFLE:
5400 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005401 typval_T tmp_tv;
5402 int item = iptr->isn_arg.shuffle.shfl_item;
5403 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005404
5405 tmp_tv = *STACK_TV_BOT(-item);
5406 for ( ; up > 0 && item > 1; --up)
5407 {
5408 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5409 --item;
5410 }
5411 *STACK_TV_BOT(-item) = tmp_tv;
5412 }
5413 break;
5414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005415 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005416 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005417 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005418 ectx->ec_where.wt_index = 0;
5419 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005420 break;
5421 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005422 continue;
5423
Bram Moolenaard032f342020-07-18 18:13:02 +02005424func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005425 // Restore previous function. If the frame pointer is where we started
5426 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005427 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005428 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005429
Bram Moolenaar4c137212021-04-19 16:48:48 +02005430 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005431 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005432 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005433 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005434
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005435on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005436 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005437 // If "emsg_silent" is set then ignore the error, unless it was set
5438 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005439 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005440 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005441 {
5442 // If a sequence of instructions causes an error while ":silent!"
5443 // was used, restore the stack length and jump ahead to restoring
5444 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005445 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005446 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005447 while (ectx->ec_stack.ga_len
5448 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005449 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005450 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005451 clear_tv(STACK_TV_BOT(0));
5452 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005453 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5454 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005455 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005456 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005457 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005458on_fatal_error:
5459 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005460 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005461 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005462 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463 }
5464
5465done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005466 ret = OK;
5467theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005468 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005469
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005470 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005471 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5472 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005473}
5474
5475/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005476 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005477 * "rettv".
5478 * Return OK or FAIL.
5479 */
5480 int
5481exe_typval_instr(typval_T *tv, typval_T *rettv)
5482{
5483 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5484 isn_T *save_instr = ectx->ec_instr;
5485 int save_iidx = ectx->ec_iidx;
5486 int res;
5487
LemonBoyf3b48952022-05-05 13:53:03 +01005488 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5489 // even when the compilation fails.
5490 rettv->v_type = VAR_UNKNOWN;
5491
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005492 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5493 res = exec_instructions(ectx);
5494 if (res == OK)
5495 {
5496 *rettv = *STACK_TV_BOT(-1);
5497 --ectx->ec_stack.ga_len;
5498 }
5499
5500 ectx->ec_instr = save_instr;
5501 ectx->ec_iidx = save_iidx;
5502
5503 return res;
5504}
5505
5506/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005507 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5508 * "substitute_instr".
5509 */
5510 char_u *
5511exe_substitute_instr(void)
5512{
5513 ectx_T *ectx = substitute_instr->subs_ectx;
5514 isn_T *save_instr = ectx->ec_instr;
5515 int save_iidx = ectx->ec_iidx;
5516 char_u *res;
5517
5518 ectx->ec_instr = substitute_instr->subs_instr;
5519 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005520 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005521 typval_T *tv = STACK_TV_BOT(-1);
5522
Bram Moolenaar27523602021-06-05 21:36:19 +02005523 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005524 --ectx->ec_stack.ga_len;
5525 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005526 }
5527 else
5528 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005529 substitute_instr->subs_status = FAIL;
5530 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005532
Bram Moolenaar4c137212021-04-19 16:48:48 +02005533 ectx->ec_instr = save_instr;
5534 ectx->ec_iidx = save_iidx;
5535
5536 return res;
5537}
5538
5539/*
5540 * Call a "def" function from old Vim script.
5541 * Return OK or FAIL.
5542 */
5543 int
5544call_def_function(
5545 ufunc_T *ufunc,
5546 int argc_arg, // nr of arguments
5547 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005548 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005549 partial_T *partial, // optional partial for context
Bram Moolenaar58779852022-09-06 18:31:14 +01005550 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005551 typval_T *rettv) // return value
5552{
5553 ectx_T ectx; // execution context
5554 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005555 int partial_argc = partial == NULL
5556 || (flags & DEF_USE_PT_ARGV) == 0
5557 ? 0 : partial->pt_argc;
5558 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005559 typval_T *tv;
5560 int idx;
5561 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005562 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005563 sctx_T save_current_sctx = current_sctx;
5564 int did_emsg_before = did_emsg_cumul + did_emsg;
5565 int save_suppress_errthrow = suppress_errthrow;
5566 msglist_T **saved_msg_list = NULL;
5567 msglist_T *private_msg_list = NULL;
5568 int save_emsg_silent_def = emsg_silent_def;
5569 int save_did_emsg_def = did_emsg_def;
5570 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005571 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005572
5573// Get pointer to item in the stack.
5574#undef STACK_TV
5575#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5576
5577// Get pointer to item at the bottom of the stack, -1 is the bottom.
5578#undef STACK_TV_BOT
5579#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5580
5581// Get pointer to a local variable on the stack. Negative for arguments.
5582#undef STACK_TV_VAR
5583#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5584
5585 if (ufunc->uf_def_status == UF_NOT_COMPILED
5586 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005587 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5588 && compile_def_function(ufunc, FALSE,
5589 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005590 {
5591 if (did_emsg_cumul + did_emsg == did_emsg_before)
5592 semsg(_(e_function_is_not_compiled_str),
5593 printable_func_name(ufunc));
5594 return FAIL;
5595 }
5596
5597 {
5598 // Check the function was really compiled.
5599 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5600 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005601 if (dfunc->df_ufunc == NULL)
5602 {
5603 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5604 return FAIL;
5605 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005606 if (INSTRUCTIONS(dfunc) == NULL)
5607 {
5608 iemsg("using call_def_function() on not compiled function");
5609 return FAIL;
5610 }
5611 }
5612
5613 // If depth of calling is getting too high, don't execute the function.
5614 orig_funcdepth = funcdepth_get();
5615 if (funcdepth_increment() == FAIL)
5616 return FAIL;
5617
5618 CLEAR_FIELD(ectx);
5619 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5620 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005621 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005622 {
5623 funcdepth_decrement();
5624 return FAIL;
5625 }
5626 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5627 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5628 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005629 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005630
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005631 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005632 if (idx > 0 && ufunc->uf_va_name == NULL)
5633 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005634 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005635 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005636 goto failed_early;
5637 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005638 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005639 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005640 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005641 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005642 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005643 goto failed_early;
5644 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005645
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005646 // Put values from the partial and arguments on the stack, but no more than
5647 // what the function expects. A lambda can be called with more arguments
5648 // than it uses.
5649 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005650 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5651 ++idx)
5652 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005653 int argv_idx = idx - partial_argc;
5654
5655 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005656 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005657 && tv->v_type == VAR_SPECIAL
5658 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005659 {
5660 // Use the default value.
5661 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5662 }
5663 else
5664 {
5665 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5666 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005667 ufunc->uf_arg_types[idx], tv,
5668 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005669 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005670 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005671 }
5672 ++ectx.ec_stack.ga_len;
5673 }
5674
5675 // Turn varargs into a list. Empty list if no args.
5676 if (ufunc->uf_va_name != NULL)
5677 {
5678 int vararg_count = argc - ufunc->uf_args.ga_len;
5679
5680 if (vararg_count < 0)
5681 vararg_count = 0;
5682 else
5683 argc -= vararg_count;
5684 if (exe_newlist(vararg_count, &ectx) == FAIL)
5685 goto failed_early;
5686
5687 // Check the type of the list items.
5688 tv = STACK_TV_BOT(-1);
5689 if (ufunc->uf_va_type != NULL
5690 && ufunc->uf_va_type != &t_list_any
5691 && ufunc->uf_va_type->tt_member != &t_any
5692 && tv->vval.v_list != NULL)
5693 {
5694 type_T *expected = ufunc->uf_va_type->tt_member;
5695 listitem_T *li = tv->vval.v_list->lv_first;
5696
5697 for (idx = 0; idx < vararg_count; ++idx)
5698 {
5699 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005700 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005701 goto failed_early;
5702 li = li->li_next;
5703 }
5704 }
5705
5706 if (defcount > 0)
5707 // Move varargs list to below missing default arguments.
5708 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5709 --ectx.ec_stack.ga_len;
5710 }
5711
5712 // Make space for omitted arguments, will store default value below.
5713 // Any varargs list goes after them.
5714 if (defcount > 0)
5715 for (idx = 0; idx < defcount; ++idx)
5716 {
5717 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5718 ++ectx.ec_stack.ga_len;
5719 }
5720 if (ufunc->uf_va_name != NULL)
5721 ++ectx.ec_stack.ga_len;
5722
5723 // Frame pointer points to just after arguments.
5724 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5725 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5726
5727 {
5728 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5729 + ufunc->uf_dfunc_idx;
5730 ufunc_T *base_ufunc = dfunc->df_ufunc;
5731
5732 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005733 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005734 if (partial != NULL || base_ufunc->uf_partial != NULL)
5735 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005736 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5737 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005738 goto failed_early;
5739 if (partial != NULL)
5740 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005741 outer_T *outer = get_pt_outer(partial);
Bram Moolenaarcc341812022-09-19 15:54:34 +01005742 int depth;
5743 void *ptr = outer->out_stack;
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005744
Bram Moolenaarcc341812022-09-19 15:54:34 +01005745 // see if any stack was set
5746 for (depth = 0; ptr == NULL && depth < MAX_LOOP_DEPTH; ++depth)
5747 ptr = outer->out_loop[depth].stack;
5748 if (ptr == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005749 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005750 if (current_ectx != NULL)
5751 {
5752 if (current_ectx->ec_outer_ref != NULL
5753 && current_ectx->ec_outer_ref->or_outer != NULL)
5754 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005755 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005756 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005757 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005758 }
5759 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005760 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005761 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005762 ++partial->pt_refcount;
5763 ectx.ec_outer_ref->or_partial = partial;
5764 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005765 }
5766 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005767 {
5768 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5769 ++base_ufunc->uf_partial->pt_refcount;
5770 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5771 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005772 }
5773 }
5774
5775 // dummy frame entries
5776 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5777 {
5778 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5779 ++ectx.ec_stack.ga_len;
5780 }
5781
5782 {
5783 // Reserve space for local variables and any closure reference count.
5784 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5785 + ufunc->uf_dfunc_idx;
5786
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005787 // Initialize variables to zero. That avoids having to generate
5788 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005789 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005790 {
5791 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5792 STACK_TV_VAR(idx)->vval.v_number = 0;
5793 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005794 ectx.ec_stack.ga_len += dfunc->df_varcount;
5795 if (dfunc->df_has_closure)
5796 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01005797 // Initialize the variable that counts how many closures were
5798 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005799 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5800 STACK_TV_VAR(idx)->vval.v_number = 0;
5801 ++ectx.ec_stack.ga_len;
5802 }
5803
5804 ectx.ec_instr = INSTRUCTIONS(dfunc);
5805 }
5806
Bram Moolenaar58779852022-09-06 18:31:14 +01005807 // Store the execution context in funccal, used by invoke_all_defer().
5808 if (funccal != NULL)
5809 funccal->fc_ectx = &ectx;
5810
Bram Moolenaar4c137212021-04-19 16:48:48 +02005811 // Following errors are in the function, not the caller.
5812 // Commands behave like vim9script.
5813 estack_push_ufunc(ufunc, 1);
5814 current_sctx = ufunc->uf_script_ctx;
5815 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5816
5817 // Use a specific location for storing error messages to be converted to an
5818 // exception.
5819 saved_msg_list = msg_list;
5820 msg_list = &private_msg_list;
5821
5822 // Do turn errors into exceptions.
5823 suppress_errthrow = FALSE;
5824
5825 // Do not delete the function while executing it.
5826 ++ufunc->uf_calls;
5827
5828 // When ":silent!" was used before calling then we still abort the
5829 // function. If ":silent!" is used in the function then we don't.
5830 emsg_silent_def = emsg_silent;
5831 did_emsg_def = 0;
5832
5833 ectx.ec_where.wt_index = 0;
5834 ectx.ec_where.wt_variable = FALSE;
5835
5836 // Execute the instructions until done.
5837 ret = exec_instructions(&ectx);
5838 if (ret == OK)
5839 {
5840 // function finished, get result from the stack.
5841 if (ufunc->uf_ret_type == &t_void)
5842 {
5843 rettv->v_type = VAR_VOID;
5844 }
5845 else
5846 {
5847 tv = STACK_TV_BOT(-1);
5848 *rettv = *tv;
5849 tv->v_type = VAR_UNKNOWN;
5850 }
5851 }
5852
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005853 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005854 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005855
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005856 // Deal with any remaining closures, they may be in use somewhere.
5857 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005858 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005859 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005860 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005861 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005862
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005863 estack_pop();
5864 current_sctx = save_current_sctx;
5865
Bram Moolenaar2336c372021-12-06 15:06:54 +00005866 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5867 // Function was unreferenced while being used, free it now.
5868 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005869
Bram Moolenaar352134b2020-10-17 22:04:08 +02005870 if (*msg_list != NULL && saved_msg_list != NULL)
5871 {
5872 msglist_T **plist = saved_msg_list;
5873
5874 // Append entries from the current msg_list (uncaught exceptions) to
5875 // the saved msg_list.
5876 while (*plist != NULL)
5877 plist = &(*plist)->next;
5878
5879 *plist = *msg_list;
5880 }
5881 msg_list = saved_msg_list;
5882
Bram Moolenaar4c137212021-04-19 16:48:48 +02005883 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005884 {
5885 cmdmod.cmod_filter_regmatch.regprog = NULL;
5886 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005887 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005888 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005889 emsg_silent_def = save_emsg_silent_def;
5890 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005891
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005892failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005893 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005894 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5895 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005896 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005899 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005900 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005901 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005902 if (ectx.ec_outer_ref->or_outer_allocated)
5903 vim_free(ectx.ec_outer_ref->or_outer);
5904 partial_unref(ectx.ec_outer_ref->or_partial);
5905 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005906 }
5907
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005908 // Not sure if this is necessary.
5909 suppress_errthrow = save_suppress_errthrow;
5910
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005911 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5912 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005913 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005914 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005915 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916 return ret;
5917}
5918
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005919/*
Bram Moolenaar58779852022-09-06 18:31:14 +01005920 * Called when a def function has finished (possibly failed).
5921 * Invoke all the function returns to clean up and invoke deferred functions,
5922 * except the toplevel one.
5923 */
5924 void
5925unwind_def_callstack(ectx_T *ectx)
5926{
5927 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
5928 func_return(ectx);
5929}
5930
5931/*
5932 * Invoke any deffered functions for the top function in "ectx".
5933 */
5934 void
5935may_invoke_defer_funcs(ectx_T *ectx)
5936{
5937 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
5938
5939 if (dfunc->df_defer_var_idx > 0)
5940 invoke_defer_funcs(ectx);
5941}
5942
5943/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01005944 * Return loopvarinfo in a printable form in allocated memory.
5945 */
5946 static char_u *
5947printable_loopvarinfo(loopvarinfo_T *lvi)
5948{
5949 garray_T ga;
5950 int depth;
5951
5952 ga_init2(&ga, 1, 100);
5953 for (depth = 0; depth < lvi->lvi_depth; ++depth)
5954 {
5955 if (ga_grow(&ga, 50) == FAIL)
5956 break;
5957 if (lvi->lvi_loop[depth].var_idx == 0)
5958 STRCPY(ga.ga_data + ga.ga_len, " -");
5959 else
5960 vim_snprintf(ga.ga_data + ga.ga_len, 50, " $%d-$%d",
5961 lvi->lvi_loop[depth].var_idx,
5962 lvi->lvi_loop[depth].var_idx
5963 + lvi->lvi_loop[depth].var_count - 1);
5964 ga.ga_len = STRLEN(ga.ga_data);
5965 }
5966 return ga.ga_data;
5967}
5968
5969/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005970 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5971 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5972 * "pfx" is prefixed to every line.
5973 */
5974 static void
5975list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5976{
5977 int line_idx = 0;
5978 int prev_current = 0;
5979 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005980 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005981
5982 for (current = 0; current < instr_count; ++current)
5983 {
5984 isn_T *iptr = &instr[current];
5985 char *line;
5986
5987 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005988 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005989 while (line_idx < iptr->isn_lnum
5990 && line_idx < ufunc->uf_lines.ga_len)
5991 {
5992 if (current > prev_current)
5993 {
5994 msg_puts("\n\n");
5995 prev_current = current;
5996 }
5997 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5998 if (line != NULL)
5999 msg(line);
6000 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006001 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6002 {
6003 int first_def_arg = ufunc->uf_args.ga_len
6004 - ufunc->uf_def_args.ga_len;
6005
6006 if (def_arg_idx > 0)
6007 msg_puts("\n\n");
6008 msg_start();
6009 msg_puts(" ");
6010 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6011 first_def_arg + def_arg_idx]);
6012 msg_puts(" = ");
6013 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6014 msg_clr_eos();
6015 msg_end();
6016 }
6017 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006018
6019 switch (iptr->isn_type)
6020 {
6021 case ISN_EXEC:
6022 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6023 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006024 case ISN_EXEC_SPLIT:
6025 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6026 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006027 case ISN_EXECRANGE:
6028 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6029 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006030 case ISN_LEGACY_EVAL:
6031 smsg("%s%4d EVAL legacy %s", pfx, current,
6032 iptr->isn_arg.string);
6033 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006034 case ISN_REDIRSTART:
6035 smsg("%s%4d REDIR", pfx, current);
6036 break;
6037 case ISN_REDIREND:
6038 smsg("%s%4d REDIR END%s", pfx, current,
6039 iptr->isn_arg.number ? " append" : "");
6040 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006041 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006042#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006043 smsg("%s%4d CEXPR pre %s", pfx, current,
6044 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006045#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006046 break;
6047 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006048#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006049 {
6050 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6051
6052 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6053 cexpr_get_auname(cer->cer_cmdidx),
6054 cer->cer_forceit ? "!" : "",
6055 cer->cer_cmdline);
6056 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006057#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006058 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006059 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006060 smsg("%s%4d INSTR", pfx, current);
6061 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6062 msg(" -------------");
6063 break;
6064 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006065 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006066 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6067
6068 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006069 }
6070 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006071 case ISN_SUBSTITUTE:
6072 {
6073 subs_T *subs = &iptr->isn_arg.subs;
6074
6075 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6076 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6077 msg(" -------------");
6078 }
6079 break;
6080 case ISN_EXECCONCAT:
6081 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6082 (varnumber_T)iptr->isn_arg.number);
6083 break;
6084 case ISN_ECHO:
6085 {
6086 echo_T *echo = &iptr->isn_arg.echo;
6087
6088 smsg("%s%4d %s %d", pfx, current,
6089 echo->echo_with_white ? "ECHO" : "ECHON",
6090 echo->echo_count);
6091 }
6092 break;
6093 case ISN_EXECUTE:
6094 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006095 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006096 break;
6097 case ISN_ECHOMSG:
6098 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006099 (varnumber_T)(iptr->isn_arg.number));
6100 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006101 case ISN_ECHOWINDOW:
6102 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
6103 (varnumber_T)(iptr->isn_arg.number));
6104 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006105 case ISN_ECHOCONSOLE:
6106 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6107 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006108 break;
6109 case ISN_ECHOERR:
6110 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006111 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006112 break;
6113 case ISN_LOAD:
6114 {
6115 if (iptr->isn_arg.number < 0)
6116 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6117 (varnumber_T)(iptr->isn_arg.number
6118 + STACK_FRAME_SIZE));
6119 else
6120 smsg("%s%4d LOAD $%lld", pfx, current,
6121 (varnumber_T)(iptr->isn_arg.number));
6122 }
6123 break;
6124 case ISN_LOADOUTER:
6125 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006126 isn_outer_T *outer = &iptr->isn_arg.outer;
6127
6128 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006129 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006130 outer->outer_depth,
6131 outer->outer_idx + STACK_FRAME_SIZE);
6132 else if (outer->outer_depth < 0)
6133 smsg("%s%4d LOADOUTER $%d in loop level %d",
6134 pfx, current,
6135 outer->outer_idx,
6136 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006137 else
6138 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006139 outer->outer_depth,
6140 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006141 }
6142 break;
6143 case ISN_LOADV:
6144 smsg("%s%4d LOADV v:%s", pfx, current,
6145 get_vim_var_name(iptr->isn_arg.number));
6146 break;
6147 case ISN_LOADSCRIPT:
6148 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006149 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6150 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6151 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006152
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006153 sv = get_script_svar(sref, -1);
6154 if (sv == NULL)
6155 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6156 pfx, current, si->sn_name);
6157 else
6158 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006159 sv->sv_name,
6160 sref->sref_idx,
6161 si->sn_name);
6162 }
6163 break;
6164 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006165 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006166 {
6167 scriptitem_T *si = SCRIPT_ITEM(
6168 iptr->isn_arg.loadstore.ls_sid);
6169
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006170 smsg("%s%4d %s s:%s from %s", pfx, current,
6171 iptr->isn_type == ISN_LOADS ? "LOADS"
6172 : "LOADEXPORT",
6173 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006174 }
6175 break;
6176 case ISN_LOADAUTO:
6177 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6178 break;
6179 case ISN_LOADG:
6180 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6181 break;
6182 case ISN_LOADB:
6183 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6184 break;
6185 case ISN_LOADW:
6186 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6187 break;
6188 case ISN_LOADT:
6189 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6190 break;
6191 case ISN_LOADGDICT:
6192 smsg("%s%4d LOAD g:", pfx, current);
6193 break;
6194 case ISN_LOADBDICT:
6195 smsg("%s%4d LOAD b:", pfx, current);
6196 break;
6197 case ISN_LOADWDICT:
6198 smsg("%s%4d LOAD w:", pfx, current);
6199 break;
6200 case ISN_LOADTDICT:
6201 smsg("%s%4d LOAD t:", pfx, current);
6202 break;
6203 case ISN_LOADOPT:
6204 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6205 break;
6206 case ISN_LOADENV:
6207 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6208 break;
6209 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006210 smsg("%s%4d LOADREG @%c", pfx, current,
6211 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006212 break;
6213
6214 case ISN_STORE:
6215 if (iptr->isn_arg.number < 0)
6216 smsg("%s%4d STORE arg[%lld]", pfx, current,
6217 iptr->isn_arg.number + STACK_FRAME_SIZE);
6218 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006219 smsg("%s%4d STORE $%lld", pfx, current,
6220 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006221 break;
6222 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006223 {
6224 isn_outer_T *outer = &iptr->isn_arg.outer;
6225
6226 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6227 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6228 pfx, current, outer->outer_idx);
6229 else
6230 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6231 outer->outer_depth, outer->outer_idx);
6232 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006233 break;
6234 case ISN_STOREV:
6235 smsg("%s%4d STOREV v:%s", pfx, current,
6236 get_vim_var_name(iptr->isn_arg.number));
6237 break;
6238 case ISN_STOREAUTO:
6239 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6240 break;
6241 case ISN_STOREG:
6242 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6243 break;
6244 case ISN_STOREB:
6245 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6246 break;
6247 case ISN_STOREW:
6248 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6249 break;
6250 case ISN_STORET:
6251 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6252 break;
6253 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006254 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006255 {
6256 scriptitem_T *si = SCRIPT_ITEM(
6257 iptr->isn_arg.loadstore.ls_sid);
6258
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006259 smsg("%s%4d %s %s in %s", pfx, current,
6260 iptr->isn_type == ISN_STORES
6261 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006262 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6263 }
6264 break;
6265 case ISN_STORESCRIPT:
6266 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006267 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6268 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6269 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006270
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006271 sv = get_script_svar(sref, -1);
6272 if (sv == NULL)
6273 smsg("%s%4d STORESCRIPT [deleted] in %s",
6274 pfx, current, si->sn_name);
6275 else
6276 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006277 sv->sv_name,
6278 sref->sref_idx,
6279 si->sn_name);
6280 }
6281 break;
6282 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006283 case ISN_STOREFUNCOPT:
6284 smsg("%s%4d %s &%s", pfx, current,
6285 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006286 iptr->isn_arg.storeopt.so_name);
6287 break;
6288 case ISN_STOREENV:
6289 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6290 break;
6291 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006292 smsg("%s%4d STOREREG @%c", pfx, current,
6293 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006294 break;
6295 case ISN_STORENR:
6296 smsg("%s%4d STORE %lld in $%d", pfx, current,
6297 iptr->isn_arg.storenr.stnr_val,
6298 iptr->isn_arg.storenr.stnr_idx);
6299 break;
6300
6301 case ISN_STOREINDEX:
6302 smsg("%s%4d STOREINDEX %s", pfx, current,
6303 vartype_name(iptr->isn_arg.vartype));
6304 break;
6305
6306 case ISN_STORERANGE:
6307 smsg("%s%4d STORERANGE", pfx, current);
6308 break;
6309
6310 // constants
6311 case ISN_PUSHNR:
6312 smsg("%s%4d PUSHNR %lld", pfx, current,
6313 (varnumber_T)(iptr->isn_arg.number));
6314 break;
6315 case ISN_PUSHBOOL:
6316 case ISN_PUSHSPEC:
6317 smsg("%s%4d PUSH %s", pfx, current,
6318 get_var_special_name(iptr->isn_arg.number));
6319 break;
6320 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006321 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006322 break;
6323 case ISN_PUSHS:
6324 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6325 break;
6326 case ISN_PUSHBLOB:
6327 {
6328 char_u *r;
6329 char_u numbuf[NUMBUFLEN];
6330 char_u *tofree;
6331
6332 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6333 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6334 vim_free(tofree);
6335 }
6336 break;
6337 case ISN_PUSHFUNC:
6338 {
6339 char *name = (char *)iptr->isn_arg.string;
6340
6341 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6342 name == NULL ? "[none]" : name);
6343 }
6344 break;
6345 case ISN_PUSHCHANNEL:
6346#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006347 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006348#endif
6349 break;
6350 case ISN_PUSHJOB:
6351#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006352 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006353#endif
6354 break;
6355 case ISN_PUSHEXC:
6356 smsg("%s%4d PUSH v:exception", pfx, current);
6357 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006358 case ISN_AUTOLOAD:
6359 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6360 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006361 case ISN_UNLET:
6362 smsg("%s%4d UNLET%s %s", pfx, current,
6363 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6364 iptr->isn_arg.unlet.ul_name);
6365 break;
6366 case ISN_UNLETENV:
6367 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6368 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6369 iptr->isn_arg.unlet.ul_name);
6370 break;
6371 case ISN_UNLETINDEX:
6372 smsg("%s%4d UNLETINDEX", pfx, current);
6373 break;
6374 case ISN_UNLETRANGE:
6375 smsg("%s%4d UNLETRANGE", pfx, current);
6376 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006377 case ISN_LOCKUNLOCK:
6378 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6379 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006380 case ISN_LOCKCONST:
6381 smsg("%s%4d LOCKCONST", pfx, current);
6382 break;
6383 case ISN_NEWLIST:
6384 smsg("%s%4d NEWLIST size %lld", pfx, current,
6385 (varnumber_T)(iptr->isn_arg.number));
6386 break;
6387 case ISN_NEWDICT:
6388 smsg("%s%4d NEWDICT size %lld", pfx, current,
6389 (varnumber_T)(iptr->isn_arg.number));
6390 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006391 case ISN_NEWPARTIAL:
6392 smsg("%s%4d NEWPARTIAL", pfx, current);
6393 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006394
6395 // function call
6396 case ISN_BCALL:
6397 {
6398 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6399
6400 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6401 internal_func_name(cbfunc->cbf_idx),
6402 cbfunc->cbf_argcount);
6403 }
6404 break;
6405 case ISN_DCALL:
6406 {
6407 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6408 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6409 + cdfunc->cdf_idx;
6410
6411 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006412 printable_func_name(df->df_ufunc),
6413 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006414 }
6415 break;
6416 case ISN_UCALL:
6417 {
6418 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6419
6420 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6421 cufunc->cuf_name, cufunc->cuf_argcount);
6422 }
6423 break;
6424 case ISN_PCALL:
6425 {
6426 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6427
6428 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6429 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6430 }
6431 break;
6432 case ISN_PCALL_END:
6433 smsg("%s%4d PCALL end", pfx, current);
6434 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006435 case ISN_DEFER:
6436 smsg("%s%4d DEFER %d args", pfx, current,
6437 (int)iptr->isn_arg.defer.defer_argcount);
6438 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006439 case ISN_RETURN:
6440 smsg("%s%4d RETURN", pfx, current);
6441 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006442 case ISN_RETURN_VOID:
6443 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006444 break;
6445 case ISN_FUNCREF:
6446 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006447 funcref_T *funcref = &iptr->isn_arg.funcref;
6448 funcref_extra_T *extra = funcref->fr_extra;
6449 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006450
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006451 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006452 {
6453 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6454 + funcref->fr_dfunc_idx;
6455 name = df->df_ufunc->uf_name;
6456 }
6457 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006458 name = extra->fre_func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006459 if (extra == NULL || extra->fre_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006460 smsg("%s%4d FUNCREF %s", pfx, current, name);
6461 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006462 {
6463 char_u *info = printable_loopvarinfo(
6464 &extra->fre_loopvar_info);
6465
6466 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6467 name, info);
6468 vim_free(info);
6469 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006470 }
6471 break;
6472
6473 case ISN_NEWFUNC:
6474 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006475 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006476
Bram Moolenaarcc341812022-09-19 15:54:34 +01006477 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006478 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6479 arg->nfa_lambda, arg->nfa_global);
6480 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006481 {
6482 char_u *info = printable_loopvarinfo(
6483 &arg->nfa_loopvar_info);
6484
6485 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6486 arg->nfa_lambda, arg->nfa_global, info);
6487 vim_free(info);
6488 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006489 }
6490 break;
6491
6492 case ISN_DEF:
6493 {
6494 char_u *name = iptr->isn_arg.string;
6495
6496 smsg("%s%4d DEF %s", pfx, current,
6497 name == NULL ? (char_u *)"" : name);
6498 }
6499 break;
6500
6501 case ISN_JUMP:
6502 {
6503 char *when = "?";
6504
6505 switch (iptr->isn_arg.jump.jump_when)
6506 {
6507 case JUMP_ALWAYS:
6508 when = "JUMP";
6509 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006510 case JUMP_NEVER:
6511 iemsg("JUMP_NEVER should not be used");
6512 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006513 case JUMP_AND_KEEP_IF_TRUE:
6514 when = "JUMP_AND_KEEP_IF_TRUE";
6515 break;
6516 case JUMP_IF_FALSE:
6517 when = "JUMP_IF_FALSE";
6518 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006519 case JUMP_WHILE_FALSE:
6520 when = "JUMP_WHILE_FALSE"; // unused
6521 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006522 case JUMP_IF_COND_FALSE:
6523 when = "JUMP_IF_COND_FALSE";
6524 break;
6525 case JUMP_IF_COND_TRUE:
6526 when = "JUMP_IF_COND_TRUE";
6527 break;
6528 }
6529 smsg("%s%4d %s -> %d", pfx, current, when,
6530 iptr->isn_arg.jump.jump_where);
6531 }
6532 break;
6533
6534 case ISN_JUMP_IF_ARG_SET:
6535 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6536 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6537 iptr->isn_arg.jump.jump_where);
6538 break;
6539
6540 case ISN_FOR:
6541 {
6542 forloop_T *forloop = &iptr->isn_arg.forloop;
6543
6544 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006545 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006546 }
6547 break;
6548
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006549 case ISN_ENDLOOP:
6550 {
6551 endloop_T *endloop = &iptr->isn_arg.endloop;
6552
Bram Moolenaarcc341812022-09-19 15:54:34 +01006553 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6554 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006555 endloop->end_funcref_idx,
6556 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006557 endloop->end_var_idx + endloop->end_var_count - 1,
6558 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006559 }
6560 break;
6561
6562 case ISN_WHILE:
6563 {
6564 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6565
6566 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6567 whileloop->while_funcref_idx,
6568 whileloop->while_end);
6569 }
6570 break;
6571
Bram Moolenaar4c137212021-04-19 16:48:48 +02006572 case ISN_TRY:
6573 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006574 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006575
6576 if (try->try_ref->try_finally == 0)
6577 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6578 pfx, current,
6579 try->try_ref->try_catch,
6580 try->try_ref->try_endtry);
6581 else
6582 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6583 pfx, current,
6584 try->try_ref->try_catch,
6585 try->try_ref->try_finally,
6586 try->try_ref->try_endtry);
6587 }
6588 break;
6589 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006590 smsg("%s%4d CATCH", pfx, current);
6591 break;
6592 case ISN_TRYCONT:
6593 {
6594 trycont_T *trycont = &iptr->isn_arg.trycont;
6595
6596 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6597 trycont->tct_levels,
6598 trycont->tct_levels == 1 ? "" : "s",
6599 trycont->tct_where);
6600 }
6601 break;
6602 case ISN_FINALLY:
6603 smsg("%s%4d FINALLY", pfx, current);
6604 break;
6605 case ISN_ENDTRY:
6606 smsg("%s%4d ENDTRY", pfx, current);
6607 break;
6608 case ISN_THROW:
6609 smsg("%s%4d THROW", pfx, current);
6610 break;
6611
6612 // expression operations on number
6613 case ISN_OPNR:
6614 case ISN_OPFLOAT:
6615 case ISN_OPANY:
6616 {
6617 char *what;
6618 char *ins;
6619
6620 switch (iptr->isn_arg.op.op_type)
6621 {
6622 case EXPR_MULT: what = "*"; break;
6623 case EXPR_DIV: what = "/"; break;
6624 case EXPR_REM: what = "%"; break;
6625 case EXPR_SUB: what = "-"; break;
6626 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006627 case EXPR_LSHIFT: what = "<<"; break;
6628 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006629 default: what = "???"; break;
6630 }
6631 switch (iptr->isn_type)
6632 {
6633 case ISN_OPNR: ins = "OPNR"; break;
6634 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6635 case ISN_OPANY: ins = "OPANY"; break;
6636 default: ins = "???"; break;
6637 }
6638 smsg("%s%4d %s %s", pfx, current, ins, what);
6639 }
6640 break;
6641
6642 case ISN_COMPAREBOOL:
6643 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006644 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006645 case ISN_COMPARENR:
6646 case ISN_COMPAREFLOAT:
6647 case ISN_COMPARESTRING:
6648 case ISN_COMPAREBLOB:
6649 case ISN_COMPARELIST:
6650 case ISN_COMPAREDICT:
6651 case ISN_COMPAREFUNC:
6652 case ISN_COMPAREANY:
6653 {
6654 char *p;
6655 char buf[10];
6656 char *type;
6657
6658 switch (iptr->isn_arg.op.op_type)
6659 {
6660 case EXPR_EQUAL: p = "=="; break;
6661 case EXPR_NEQUAL: p = "!="; break;
6662 case EXPR_GREATER: p = ">"; break;
6663 case EXPR_GEQUAL: p = ">="; break;
6664 case EXPR_SMALLER: p = "<"; break;
6665 case EXPR_SEQUAL: p = "<="; break;
6666 case EXPR_MATCH: p = "=~"; break;
6667 case EXPR_IS: p = "is"; break;
6668 case EXPR_ISNOT: p = "isnot"; break;
6669 case EXPR_NOMATCH: p = "!~"; break;
6670 default: p = "???"; break;
6671 }
6672 STRCPY(buf, p);
6673 if (iptr->isn_arg.op.op_ic == TRUE)
6674 strcat(buf, "?");
6675 switch(iptr->isn_type)
6676 {
6677 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6678 case ISN_COMPARESPECIAL:
6679 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006680 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006681 case ISN_COMPARENR: type = "COMPARENR"; break;
6682 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6683 case ISN_COMPARESTRING:
6684 type = "COMPARESTRING"; break;
6685 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6686 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6687 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6688 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6689 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6690 default: type = "???"; break;
6691 }
6692
6693 smsg("%s%4d %s %s", pfx, current, type, buf);
6694 }
6695 break;
6696
6697 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6698 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6699
6700 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006701 case ISN_CONCAT:
6702 smsg("%s%4d CONCAT size %lld", pfx, current,
6703 (varnumber_T)(iptr->isn_arg.number));
6704 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006705 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6706 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6707 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6708 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6709 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6710 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6711 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6712 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6713 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6714 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6715 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006716 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006717 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6718 iptr->isn_arg.getitem.gi_index,
6719 iptr->isn_arg.getitem.gi_with_op ?
6720 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006721 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6722 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6723 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006724 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6725 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6726
Bram Moolenaar4c137212021-04-19 16:48:48 +02006727 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6728
Bram Moolenaar4c137212021-04-19 16:48:48 +02006729 case ISN_CHECKTYPE:
6730 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006731 checktype_T *ct = &iptr->isn_arg.type;
6732 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006733
6734 if (ct->ct_arg_idx == 0)
6735 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6736 type_name(ct->ct_type, &tofree),
6737 (int)ct->ct_off);
6738 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006739 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006740 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006741 type_name(ct->ct_type, &tofree),
6742 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006743 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006744 (int)ct->ct_arg_idx);
6745 vim_free(tofree);
6746 break;
6747 }
6748 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6749 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6750 iptr->isn_arg.checklen.cl_min_len);
6751 break;
6752 case ISN_SETTYPE:
6753 {
6754 char *tofree;
6755
6756 smsg("%s%4d SETTYPE %s", pfx, current,
6757 type_name(iptr->isn_arg.type.ct_type, &tofree));
6758 vim_free(tofree);
6759 break;
6760 }
6761 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006762 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6763 smsg("%s%4d INVERT %d (!val)", pfx, current,
6764 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006765 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006766 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6767 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006768 break;
6769 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006770 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006771 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006772 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6773 pfx, current,
6774 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006775 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006776 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6777 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006778 break;
6779 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006780 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006781 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006782 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006783 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6784 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006785 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006786 else
6787 smsg("%s%4d PUT %c %ld", pfx, current,
6788 iptr->isn_arg.put.put_regname,
6789 (long)iptr->isn_arg.put.put_lnum);
6790 break;
6791
Bram Moolenaar4c137212021-04-19 16:48:48 +02006792 case ISN_CMDMOD:
6793 {
6794 char_u *buf;
6795 size_t len = produce_cmdmods(
6796 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6797
6798 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006799 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006800 {
6801 (void)produce_cmdmods(
6802 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6803 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6804 vim_free(buf);
6805 }
6806 break;
6807 }
6808 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6809
6810 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006811 smsg("%s%4d PROFILE START line %d", pfx, current,
6812 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006813 break;
6814
6815 case ISN_PROF_END:
6816 smsg("%s%4d PROFILE END", pfx, current);
6817 break;
6818
Bram Moolenaare99d4222021-06-13 14:01:26 +02006819 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006820 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6821 iptr->isn_arg.debug.dbg_break_lnum + 1,
6822 iptr->isn_lnum,
6823 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006824 break;
6825
Bram Moolenaar4c137212021-04-19 16:48:48 +02006826 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6827 iptr->isn_arg.unpack.unp_count,
6828 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6829 break;
6830 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6831 iptr->isn_arg.shuffle.shfl_item,
6832 iptr->isn_arg.shuffle.shfl_up);
6833 break;
6834 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6835
6836 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6837 return;
6838 }
6839
6840 out_flush(); // output one line at a time
6841 ui_breakcheck();
6842 if (got_int)
6843 break;
6844 }
6845}
6846
6847/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006848 * Handle command line completion for the :disassemble command.
6849 */
6850 void
6851set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6852{
6853 char_u *p;
6854
6855 // Default: expand user functions, "debug" and "profile"
6856 xp->xp_context = EXPAND_DISASSEMBLE;
6857 xp->xp_pattern = arg;
6858
6859 // first argument already typed: only user function names
6860 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6861 {
6862 xp->xp_context = EXPAND_USER_FUNC;
6863 xp->xp_pattern = skipwhite(p);
6864 }
6865}
6866
6867/*
6868 * Function given to ExpandGeneric() to obtain the list of :disassemble
6869 * arguments.
6870 */
6871 char_u *
6872get_disassemble_argument(expand_T *xp, int idx)
6873{
6874 if (idx == 0)
6875 return (char_u *)"debug";
6876 if (idx == 1)
6877 return (char_u *)"profile";
6878 return get_user_func_name(xp, idx - 2);
6879}
6880
6881/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006882 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006883 * We don't really need this at runtime, but we do have tests that require it,
6884 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885 */
6886 void
6887ex_disassemble(exarg_T *eap)
6888{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006889 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006890 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006891 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006892 isn_T *instr = NULL; // init to shut up gcc warning
6893 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006894 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006895
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006896 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006897 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006898 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006899 if (func_needs_compiling(ufunc, compile_type)
6900 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006901 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006902 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006904 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006905 return;
6906 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006907 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908
6909 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006910 switch (compile_type)
6911 {
6912 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006913#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006914 instr = dfunc->df_instr_prof;
6915 instr_count = dfunc->df_instr_prof_count;
6916 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006917#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006918 // FALLTHROUGH
6919 case CT_NONE:
6920 instr = dfunc->df_instr;
6921 instr_count = dfunc->df_instr_count;
6922 break;
6923 case CT_DEBUG:
6924 instr = dfunc->df_instr_debug;
6925 instr_count = dfunc->df_instr_debug_count;
6926 break;
6927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006928
Bram Moolenaar4c137212021-04-19 16:48:48 +02006929 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930}
6931
6932/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006933 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 * list, etc. Mostly like what JavaScript does, except that empty list and
6935 * empty dictionary are FALSE.
6936 */
6937 int
6938tv2bool(typval_T *tv)
6939{
6940 switch (tv->v_type)
6941 {
6942 case VAR_NUMBER:
6943 return tv->vval.v_number != 0;
6944 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006946 case VAR_PARTIAL:
6947 return tv->vval.v_partial != NULL;
6948 case VAR_FUNC:
6949 case VAR_STRING:
6950 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6951 case VAR_LIST:
6952 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6953 case VAR_DICT:
6954 return tv->vval.v_dict != NULL
6955 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6956 case VAR_BOOL:
6957 case VAR_SPECIAL:
6958 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6959 case VAR_JOB:
6960#ifdef FEAT_JOB_CHANNEL
6961 return tv->vval.v_job != NULL;
6962#else
6963 break;
6964#endif
6965 case VAR_CHANNEL:
6966#ifdef FEAT_JOB_CHANNEL
6967 return tv->vval.v_channel != NULL;
6968#else
6969 break;
6970#endif
6971 case VAR_BLOB:
6972 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6973 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006974 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006976 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 break;
6978 }
6979 return FALSE;
6980}
6981
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006982 void
6983emsg_using_string_as(typval_T *tv, int as_number)
6984{
6985 semsg(_(as_number ? e_using_string_as_number_str
6986 : e_using_string_as_bool_str),
6987 tv->vval.v_string == NULL
6988 ? (char_u *)"" : tv->vval.v_string);
6989}
6990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991/*
6992 * If "tv" is a string give an error and return FAIL.
6993 */
6994 int
6995check_not_string(typval_T *tv)
6996{
6997 if (tv->v_type == VAR_STRING)
6998 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006999 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 clear_tv(tv);
7001 return FAIL;
7002 }
7003 return OK;
7004}
7005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007006
7007#endif // FEAT_EVAL