blob: c2a3310813094512185dbaa2a61816864058bb5a [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(
1828 partial_T *pt,
1829 ufunc_T *ufunc,
1830 short loop_var_idx,
1831 short loop_var_count,
1832 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001833{
1834 pt->pt_func = ufunc;
1835 pt->pt_refcount = 1;
1836
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001837 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001838 {
1839 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1840 + ectx->ec_dfunc_idx;
1841
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001842 // The closure may need to find arguments and local variables of the
1843 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001844 pt->pt_outer.out_stack = &ectx->ec_stack;
1845 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001846 if (ectx->ec_outer_ref != NULL)
1847 {
1848 // The current context already has a context, link to that one.
1849 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1850 if (ectx->ec_outer_ref->or_partial != NULL)
1851 {
1852 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1853 ++pt->pt_outer.out_up_partial->pt_refcount;
1854 }
1855 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001856
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001857 // The closure may need to find variables defined inside a loop. A
1858 // new reference is made every time, ISN_ENDLOOP will check if they
1859 // are actually used.
1860 pt->pt_outer.out_loop_stack = &ectx->ec_stack;
1861 pt->pt_outer.out_loop_var_idx = ectx->ec_frame_idx + STACK_FRAME_SIZE
1862 + loop_var_idx;
1863 pt->pt_outer.out_loop_var_count = loop_var_count;
1864
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001865 // If the function currently executing returns and the closure is still
1866 // being referenced, we need to make a copy of the context (arguments
1867 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001868 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001869 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001870 {
1871 vim_free(pt);
1872 return FAIL;
1873 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001874 // Extra variable keeps the count of closures created in the current
1875 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001876 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1877 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1878
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001879 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1880 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001881 ++pt->pt_refcount;
1882 ++ectx->ec_funcrefs.ga_len;
1883 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001884 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001885 return OK;
1886}
1887
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001888/*
1889 * Execute iptr->isn_arg.string as an Ex command.
1890 */
1891 static int
1892exec_command(isn_T *iptr)
1893{
1894 source_cookie_T cookie;
1895
1896 SOURCING_LNUM = iptr->isn_lnum;
1897 // Pass getsourceline to get an error for a missing ":end"
1898 // command.
1899 CLEAR_FIELD(cookie);
1900 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1901 if (do_cmdline(iptr->isn_arg.string,
1902 getsourceline, &cookie,
1903 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1904 || did_emsg)
1905 return FAIL;
1906 return OK;
1907}
1908
Bram Moolenaar89445512022-04-14 12:58:23 +01001909/*
1910 * If script "sid" is not loaded yet then load it now.
1911 * Caller must make sure "sid" is a valid script ID.
1912 * "loaded" is set to TRUE if the script had to be loaded.
1913 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1914 */
1915 int
1916may_load_script(int sid, int *loaded)
1917{
1918 scriptitem_T *si = SCRIPT_ITEM(sid);
1919
1920 if (si->sn_state == SN_STATE_NOT_LOADED)
1921 {
1922 *loaded = TRUE;
1923 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1924 {
1925 semsg(_(e_cant_open_file_str), si->sn_name);
1926 return FAIL;
1927 }
1928 }
1929 return OK;
1930}
1931
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001932// used for v_instr of typval of VAR_INSTR
1933struct instr_S {
1934 ectx_T *instr_ectx;
1935 isn_T *instr_instr;
1936};
1937
Bram Moolenaar4c137212021-04-19 16:48:48 +02001938// used for substitute_instr
1939typedef struct subs_expr_S {
1940 ectx_T *subs_ectx;
1941 isn_T *subs_instr;
1942 int subs_status;
1943} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001945// Set when calling do_debug().
1946static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001947static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001948
1949/*
1950 * When debugging lookup "name" and return the typeval.
1951 * When not found return NULL.
1952 */
1953 typval_T *
1954lookup_debug_var(char_u *name)
1955{
1956 int idx;
1957 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001958 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001959 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001960 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001961
1962 if (ectx == NULL)
1963 return NULL;
1964 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1965
1966 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001967 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001968 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001969 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1970
1971 // the variable name may be NULL when not available in this block
1972 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001973 return STACK_TV_VAR(idx);
1974 }
1975
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001976 // Go through argument names.
1977 ufunc = dfunc->df_ufunc;
1978 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1979 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1980 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1981 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1982 - varargs_off + idx);
1983 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1984 return STACK_TV(ectx->ec_frame_idx - 1);
1985
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001986 return NULL;
1987}
1988
Bram Moolenaar26a44842021-09-02 18:49:06 +02001989/*
1990 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1991 * breakpoint was set in that function or when there is any expression.
1992 */
1993 int
1994may_break_in_function(ufunc_T *ufunc)
1995{
1996 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1997}
1998
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001999 static void
2000handle_debug(isn_T *iptr, ectx_T *ectx)
2001{
2002 char_u *line;
2003 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2004 + ectx->ec_dfunc_idx)->df_ufunc;
2005 isn_T *ni;
2006 int end_lnum = iptr->isn_lnum;
2007 garray_T ga;
2008 int lnum;
2009
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002010 if (ex_nesting_level > debug_break_level)
2011 {
2012 linenr_T breakpoint;
2013
Bram Moolenaar26a44842021-09-02 18:49:06 +02002014 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002015 return;
2016
2017 // check for the next breakpoint if needed
2018 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002019 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002020 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2021 return;
2022 }
2023
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002024 SOURCING_LNUM = iptr->isn_lnum;
2025 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002026 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002027
2028 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2029 if (ni->isn_type == ISN_DEBUG
2030 || ni->isn_type == ISN_RETURN
2031 || ni->isn_type == ISN_RETURN_VOID)
2032 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002033 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002034 break;
2035 }
2036
2037 if (end_lnum > iptr->isn_lnum)
2038 {
2039 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002040 for (lnum = iptr->isn_lnum; lnum < end_lnum
2041 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002042 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002043 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002044
Bram Moolenaar303215d2021-07-07 20:10:43 +02002045 if (p == NULL)
2046 continue; // left over from continuation line
2047 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002048 if (*p == '#')
2049 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002050 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002051 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2052 if (STRNCMP(p, "def ", 4) == 0)
2053 break;
2054 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002055 line = ga_concat_strings(&ga, " ");
2056 vim_free(ga.ga_data);
2057 }
2058 else
2059 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002060
Dominique Pelle6e969552021-06-17 13:53:41 +02002061 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002062 debug_context = NULL;
2063
2064 if (end_lnum > iptr->isn_lnum)
2065 vim_free(line);
2066}
2067
Bram Moolenaar4c137212021-04-19 16:48:48 +02002068/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002069 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002070 * Returns OK, FAIL or NOTDONE (uncatchable error).
2071 */
2072 static int
2073execute_storeindex(isn_T *iptr, ectx_T *ectx)
2074{
2075 vartype_T dest_type = iptr->isn_arg.vartype;
2076 typval_T *tv;
2077 typval_T *tv_idx = STACK_TV_BOT(-2);
2078 typval_T *tv_dest = STACK_TV_BOT(-1);
2079 int status = OK;
2080
2081 // Stack contains:
2082 // -3 value to be stored
2083 // -2 index
2084 // -1 dict or list
2085 tv = STACK_TV_BOT(-3);
2086 SOURCING_LNUM = iptr->isn_lnum;
2087 if (dest_type == VAR_ANY)
2088 {
2089 dest_type = tv_dest->v_type;
2090 if (dest_type == VAR_DICT)
2091 status = do_2string(tv_idx, TRUE, FALSE);
2092 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2093 {
2094 emsg(_(e_number_expected));
2095 status = FAIL;
2096 }
2097 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002098
2099 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002100 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002101 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002102 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002103 long lidx = (long)tv_idx->vval.v_number;
2104 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002105
Bram Moolenaare08be092022-02-17 13:08:26 +00002106 if (list == NULL)
2107 {
2108 emsg(_(e_list_not_set));
2109 return FAIL;
2110 }
2111 if (lidx < 0 && list->lv_len + lidx >= 0)
2112 // negative index is relative to the end
2113 lidx = list->lv_len + lidx;
2114 if (lidx < 0 || lidx > list->lv_len)
2115 {
2116 semsg(_(e_list_index_out_of_range_nr), lidx);
2117 return FAIL;
2118 }
2119 if (lidx < list->lv_len)
2120 {
2121 listitem_T *li = list_find(list, lidx);
2122
2123 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002124 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002125 return FAIL;
2126 // overwrite existing list item
2127 clear_tv(&li->li_tv);
2128 li->li_tv = *tv;
2129 }
2130 else
2131 {
2132 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2133 return FAIL;
2134 // append to list, only fails when out of memory
2135 if (list_append_tv(list, tv) == FAIL)
2136 return NOTDONE;
2137 clear_tv(tv);
2138 }
2139 }
2140 else if (dest_type == VAR_DICT)
2141 {
2142 char_u *key = tv_idx->vval.v_string;
2143 dict_T *dict = tv_dest->vval.v_dict;
2144 dictitem_T *di;
2145
2146 SOURCING_LNUM = iptr->isn_lnum;
2147 if (dict == NULL)
2148 {
2149 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002150 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002151 }
2152 if (key == NULL)
2153 key = (char_u *)"";
2154 di = dict_find(dict, key, -1);
2155 if (di != NULL)
2156 {
2157 if (error_if_locked(di->di_tv.v_lock,
2158 e_cannot_change_dict_item))
2159 return FAIL;
2160 // overwrite existing value
2161 clear_tv(&di->di_tv);
2162 di->di_tv = *tv;
2163 }
2164 else
2165 {
2166 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2167 return FAIL;
2168 // add to dict, only fails when out of memory
2169 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2170 return NOTDONE;
2171 clear_tv(tv);
2172 }
2173 }
2174 else if (dest_type == VAR_BLOB)
2175 {
2176 long lidx = (long)tv_idx->vval.v_number;
2177 blob_T *blob = tv_dest->vval.v_blob;
2178 varnumber_T nr;
2179 int error = FALSE;
2180 int len;
2181
2182 if (blob == NULL)
2183 {
2184 emsg(_(e_blob_not_set));
2185 return FAIL;
2186 }
2187 len = blob_len(blob);
2188 if (lidx < 0 && len + lidx >= 0)
2189 // negative index is relative to the end
2190 lidx = len + lidx;
2191
2192 // Can add one byte at the end.
2193 if (lidx < 0 || lidx > len)
2194 {
2195 semsg(_(e_blob_index_out_of_range_nr), lidx);
2196 return FAIL;
2197 }
2198 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2199 return FAIL;
2200 nr = tv_get_number_chk(tv, &error);
2201 if (error)
2202 return FAIL;
2203 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002204 }
2205 else
2206 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002207 status = FAIL;
2208 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002209 }
2210 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002211
2212 clear_tv(tv_idx);
2213 clear_tv(tv_dest);
2214 ectx->ec_stack.ga_len -= 3;
2215 if (status == FAIL)
2216 {
2217 clear_tv(tv);
2218 return FAIL;
2219 }
2220 return OK;
2221}
2222
2223/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002224 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002225 */
2226 static int
2227execute_storerange(isn_T *iptr, ectx_T *ectx)
2228{
2229 typval_T *tv;
2230 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2231 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2232 typval_T *tv_dest = STACK_TV_BOT(-1);
2233 int status = OK;
2234
2235 // Stack contains:
2236 // -4 value to be stored
2237 // -3 first index or "none"
2238 // -2 second index or "none"
2239 // -1 destination list or blob
2240 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002241 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002242 if (tv_dest->v_type == VAR_LIST)
2243 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002244 long n1;
2245 long n2;
2246 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002247
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002248 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2249 if (tv_idx2->v_type == VAR_SPECIAL
2250 && tv_idx2->vval.v_number == VVAL_NONE)
2251 n2 = list_len(tv_dest->vval.v_list) - 1;
2252 else
2253 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2254
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002255 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002256 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002257 status = FAIL;
2258 else
2259 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002260 status = check_range_index_two(tv_dest->vval.v_list,
2261 &n1, li1, &n2, FALSE);
2262 if (status != FAIL)
2263 status = list_assign_range(
2264 tv_dest->vval.v_list,
2265 tv->vval.v_list,
2266 n1,
2267 n2,
2268 tv_idx2->v_type == VAR_SPECIAL,
2269 (char_u *)"=",
2270 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002271 }
2272 }
2273 else if (tv_dest->v_type == VAR_BLOB)
2274 {
2275 varnumber_T n1;
2276 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002277 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002278
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002279 n1 = tv_get_number_chk(tv_idx1, NULL);
2280 if (tv_idx2->v_type == VAR_SPECIAL
2281 && tv_idx2->vval.v_number == VVAL_NONE)
2282 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2283 else
2284 n2 = tv_get_number_chk(tv_idx2, NULL);
2285 bloblen = blob_len(tv_dest->vval.v_blob);
2286
2287 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2288 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002289 status = FAIL;
2290 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002291 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002292 }
2293 else
2294 {
2295 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002296 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002297 }
2298
2299 clear_tv(tv_idx1);
2300 clear_tv(tv_idx2);
2301 clear_tv(tv_dest);
2302 ectx->ec_stack.ga_len -= 4;
2303 clear_tv(tv);
2304
2305 return status;
2306}
2307
2308/*
2309 * Unlet item in list or dict variable.
2310 */
2311 static int
2312execute_unletindex(isn_T *iptr, ectx_T *ectx)
2313{
2314 typval_T *tv_idx = STACK_TV_BOT(-2);
2315 typval_T *tv_dest = STACK_TV_BOT(-1);
2316 int status = OK;
2317
2318 // Stack contains:
2319 // -2 index
2320 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002321 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002322 if (tv_dest->v_type == VAR_DICT)
2323 {
2324 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002325 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002326 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002327 semsg(_(e_expected_str_but_got_str),
2328 vartype_name(VAR_STRING),
2329 vartype_name(tv_idx->v_type));
2330 status = FAIL;
2331 }
2332 else
2333 {
2334 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002335 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002336 dictitem_T *di = NULL;
2337
2338 if (d != NULL && value_check_lock(
2339 d->dv_lock, NULL, FALSE))
2340 status = FAIL;
2341 else
2342 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002343 if (tv_idx->v_type == VAR_STRING)
2344 {
2345 key = tv_idx->vval.v_string;
2346 if (key == NULL)
2347 key = (char_u *)"";
2348 }
2349 else
2350 {
2351 key = tv_get_string(tv_idx);
2352 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002353 if (d != NULL)
2354 di = dict_find(d, key, (int)STRLEN(key));
2355 if (di == NULL)
2356 {
2357 // NULL dict is equivalent to empty dict
2358 semsg(_(e_key_not_present_in_dictionary),
2359 key);
2360 status = FAIL;
2361 }
2362 else if (var_check_fixed(di->di_flags,
2363 NULL, FALSE)
2364 || var_check_ro(di->di_flags,
2365 NULL, FALSE))
2366 status = FAIL;
2367 else
2368 dictitem_remove(d, di);
2369 }
2370 }
2371 }
2372 else if (tv_dest->v_type == VAR_LIST)
2373 {
2374 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002375 if (check_for_number(tv_idx) == FAIL)
2376 {
2377 status = FAIL;
2378 }
2379 else
2380 {
2381 list_T *l = tv_dest->vval.v_list;
2382 long n = (long)tv_idx->vval.v_number;
2383
2384 if (l != NULL && value_check_lock(
2385 l->lv_lock, NULL, FALSE))
2386 status = FAIL;
2387 else
2388 {
2389 listitem_T *li = list_find(l, n);
2390
2391 if (li == NULL)
2392 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002393 semsg(_(e_list_index_out_of_range_nr), n);
2394 status = FAIL;
2395 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002396 else
2397 listitem_remove(l, li);
2398 }
2399 }
2400 }
2401 else
2402 {
2403 status = FAIL;
2404 semsg(_(e_cannot_index_str),
2405 vartype_name(tv_dest->v_type));
2406 }
2407
2408 clear_tv(tv_idx);
2409 clear_tv(tv_dest);
2410 ectx->ec_stack.ga_len -= 2;
2411
2412 return status;
2413}
2414
2415/*
2416 * Unlet a range of items in a list variable.
2417 */
2418 static int
2419execute_unletrange(isn_T *iptr, ectx_T *ectx)
2420{
2421 // Stack contains:
2422 // -3 index1
2423 // -2 index2
2424 // -1 dict or list
2425 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2426 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2427 typval_T *tv_dest = STACK_TV_BOT(-1);
2428 int status = OK;
2429
2430 if (tv_dest->v_type == VAR_LIST)
2431 {
2432 // indexes must be a number
2433 SOURCING_LNUM = iptr->isn_lnum;
2434 if (check_for_number(tv_idx1) == FAIL
2435 || (tv_idx2->v_type != VAR_SPECIAL
2436 && check_for_number(tv_idx2) == FAIL))
2437 {
2438 status = FAIL;
2439 }
2440 else
2441 {
2442 list_T *l = tv_dest->vval.v_list;
2443 long n1 = (long)tv_idx1->vval.v_number;
2444 long n2 = tv_idx2->v_type == VAR_SPECIAL
2445 ? 0 : (long)tv_idx2->vval.v_number;
2446 listitem_T *li;
2447
2448 li = list_find_index(l, &n1);
2449 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002450 {
2451 semsg(_(e_list_index_out_of_range_nr),
2452 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002453 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002454 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002455 else
2456 {
2457 if (n1 < 0)
2458 n1 = list_idx_of_item(l, li);
2459 if (n2 < 0)
2460 {
2461 listitem_T *li2 = list_find(l, n2);
2462
2463 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002464 {
2465 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002466 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002467 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002468 else
2469 n2 = list_idx_of_item(l, li2);
2470 }
2471 if (status != FAIL
2472 && tv_idx2->v_type != VAR_SPECIAL
2473 && n2 < n1)
2474 {
2475 semsg(_(e_list_index_out_of_range_nr), n2);
2476 status = FAIL;
2477 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002478 if (status != FAIL)
2479 list_unlet_range(l, li, n1,
2480 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002481 }
2482 }
2483 }
2484 else
2485 {
2486 status = FAIL;
2487 SOURCING_LNUM = iptr->isn_lnum;
2488 semsg(_(e_cannot_index_str),
2489 vartype_name(tv_dest->v_type));
2490 }
2491
2492 clear_tv(tv_idx1);
2493 clear_tv(tv_idx2);
2494 clear_tv(tv_dest);
2495 ectx->ec_stack.ga_len -= 3;
2496
2497 return status;
2498}
2499
2500/*
2501 * Top of a for loop.
2502 */
2503 static int
2504execute_for(isn_T *iptr, ectx_T *ectx)
2505{
2506 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002507 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002508 typval_T *ltv = STACK_TV_BOT(-1);
2509 typval_T *idxtv =
2510 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2511
2512 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2513 return FAIL;
2514 if (ltv->v_type == VAR_LIST)
2515 {
2516 list_T *list = ltv->vval.v_list;
2517
2518 // push the next item from the list
2519 ++idxtv->vval.v_number;
2520 if (list == NULL
2521 || idxtv->vval.v_number >= list->lv_len)
2522 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002523 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002524 }
2525 else if (list->lv_first == &range_list_item)
2526 {
2527 // non-materialized range() list
2528 tv = STACK_TV_BOT(0);
2529 tv->v_type = VAR_NUMBER;
2530 tv->v_lock = 0;
2531 tv->vval.v_number = list_find_nr(
2532 list, idxtv->vval.v_number, NULL);
2533 ++ectx->ec_stack.ga_len;
2534 }
2535 else
2536 {
2537 listitem_T *li = list_find(list,
2538 idxtv->vval.v_number);
2539
2540 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2541 ++ectx->ec_stack.ga_len;
2542 }
2543 }
2544 else if (ltv->v_type == VAR_STRING)
2545 {
2546 char_u *str = ltv->vval.v_string;
2547
2548 // The index is for the last byte of the previous
2549 // character.
2550 ++idxtv->vval.v_number;
2551 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2552 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002553 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002554 }
2555 else
2556 {
2557 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2558
2559 // Push the next character from the string.
2560 tv = STACK_TV_BOT(0);
2561 tv->v_type = VAR_STRING;
2562 tv->vval.v_string = vim_strnsave(
2563 str + idxtv->vval.v_number, clen);
2564 ++ectx->ec_stack.ga_len;
2565 idxtv->vval.v_number += clen - 1;
2566 }
2567 }
2568 else if (ltv->v_type == VAR_BLOB)
2569 {
2570 blob_T *blob = ltv->vval.v_blob;
2571
2572 // When we get here the first time make a copy of the
2573 // blob, so that the iteration still works when it is
2574 // changed.
2575 if (idxtv->vval.v_number == -1 && blob != NULL)
2576 {
2577 blob_copy(blob, ltv);
2578 blob_unref(blob);
2579 blob = ltv->vval.v_blob;
2580 }
2581
2582 // The index is for the previous byte.
2583 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002584 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002585 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002586 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002587 }
2588 else
2589 {
2590 // Push the next byte from the blob.
2591 tv = STACK_TV_BOT(0);
2592 tv->v_type = VAR_NUMBER;
2593 tv->vval.v_number = blob_get(blob,
2594 idxtv->vval.v_number);
2595 ++ectx->ec_stack.ga_len;
2596 }
2597 }
2598 else
2599 {
2600 semsg(_(e_for_loop_on_str_not_supported),
2601 vartype_name(ltv->v_type));
2602 return FAIL;
2603 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002604
2605 if (jump)
2606 {
2607 // past the end of the list/string/blob, jump to "endfor"
2608 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2609 may_restore_cmdmod(&ectx->ec_funclocal);
2610 }
2611 else
2612 {
2613 // Store the current number of funcrefs, this may be used in
2614 // ISN_LOOPEND. The variable index is always one more than the loop
2615 // variable index.
2616 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_idx + 1);
2617 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2618 }
2619
2620 return OK;
2621}
2622
2623/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002624 * Code for handling variables declared inside a loop and used in a closure.
2625 * This is very similar to what is done with funcstack_T. The difference is
2626 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2627 * scope of the block inside a loop and each loop may have its own.
2628 */
2629
2630// Double linked list of loopvars_T in use.
2631static loopvars_T *first_loopvars = NULL;
2632
2633 static void
2634add_loopvars_to_list(loopvars_T *loopvars)
2635{
2636 // Link in list of loopvarss.
2637 if (first_loopvars != NULL)
2638 first_loopvars->lvs_prev = loopvars;
2639 loopvars->lvs_next = first_loopvars;
2640 loopvars->lvs_prev = NULL;
2641 first_loopvars = loopvars;
2642}
2643
2644 static void
2645remove_loopvars_from_list(loopvars_T *loopvars)
2646{
2647 if (loopvars->lvs_prev == NULL)
2648 first_loopvars = loopvars->lvs_next;
2649 else
2650 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2651 if (loopvars->lvs_next != NULL)
2652 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2653}
2654
2655/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002656 * End of a for or while loop: Handle any variables used by a closure.
2657 */
2658 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002659execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002660{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002661 endloop_T *endloop = &iptr->isn_arg.endloop;
2662 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2663 int prev_closure_count = tv_refcount->vval.v_number;
2664 garray_T *gap = &ectx->ec_funcrefs;
2665 int closure_in_use = FALSE;
2666 loopvars_T *loopvars;
2667 typval_T *stack;
2668 int idx;
2669
2670 // Check if any created closure is still being referenced.
2671 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2672 {
2673 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2674
Bram Moolenaardbbb02b2022-09-18 12:00:21 +01002675 if (pt->pt_refcount > 1 && pt->pt_loopvars == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002676 {
2677 int refcount = pt->pt_refcount;
2678 int i;
2679
2680 // A Reference in a variable inside the loop doesn't count, it gets
2681 // unreferenced at the end of the loop.
2682 for (i = 0; i < endloop->end_var_count; ++i)
2683 {
2684 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2685
2686 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2687 --refcount;
2688 }
2689 if (refcount > 1)
2690 {
2691 closure_in_use = TRUE;
2692 break;
2693 }
2694 }
2695 }
2696
2697 // If no function reference were created since the start of the loop block
2698 // or it is no longer referenced there is nothing to do.
2699 if (!closure_in_use)
2700 return OK;
2701
2702 // A closure is using variables declared inside the loop.
2703 // Move them to the called function.
2704 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2705 if (loopvars == NULL)
2706 return FAIL;
2707
2708 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2709 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2710 loopvars->lvs_ga.ga_data = stack;
2711 if (stack == NULL)
2712 {
2713 vim_free(loopvars);
2714 return FAIL;
2715 }
2716 add_loopvars_to_list(loopvars);
2717
2718 // Move the variable values.
2719 for (idx = 0; idx < endloop->end_var_count; ++idx)
2720 {
2721 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2722
2723 *(stack + idx) = *tv;
2724 tv->v_type = VAR_UNKNOWN;
2725 }
2726
2727 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2728 {
2729 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2730
Bram Moolenaardbbb02b2022-09-18 12:00:21 +01002731 if (pt->pt_refcount > 1 && pt->pt_loopvars == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002732 {
2733 ++loopvars->lvs_refcount;
2734 pt->pt_loopvars = loopvars;
2735
2736 pt->pt_outer.out_loop_stack = &loopvars->lvs_ga;
2737 pt->pt_outer.out_loop_var_idx -= ectx->ec_frame_idx
2738 + STACK_FRAME_SIZE + endloop->end_var_idx;
2739 }
2740 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002741
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002742 return OK;
2743}
2744
2745/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002746 * Called when a partial is freed or its reference count goes down to one. The
2747 * loopvars may be the only reference to the partials in the local variables.
2748 * Go over all of them, the funcref and can be freed if all partials
2749 * referencing the loopvars have a reference count of one.
2750 */
2751 void
2752loopvars_check_refcount(loopvars_T *loopvars)
2753{
2754 int i;
2755 garray_T *gap = &loopvars->lvs_ga;
2756 int done = 0;
2757
2758 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2759 return;
2760 for (i = 0; i < gap->ga_len; ++i)
2761 {
2762 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2763
2764 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2765 && tv->vval.v_partial->pt_loopvars == loopvars
2766 && tv->vval.v_partial->pt_refcount == 1)
2767 ++done;
2768 }
2769 if (done == loopvars->lvs_min_refcount)
2770 {
2771 typval_T *stack = gap->ga_data;
2772
2773 // All partials referencing the loopvars have a reference count of
2774 // one, thus the loopvars is no longer of use.
2775 for (i = 0; i < gap->ga_len; ++i)
2776 clear_tv(stack + i);
2777 vim_free(stack);
2778 remove_loopvars_from_list(loopvars);
2779 vim_free(loopvars);
2780 }
2781}
2782
2783/*
2784 * For garbage collecting: set references in all variables referenced by
2785 * all loopvars.
2786 */
2787 int
2788set_ref_in_loopvars(int copyID)
2789{
2790 loopvars_T *loopvars;
2791
2792 for (loopvars = first_loopvars; loopvars != NULL;
2793 loopvars = loopvars->lvs_next)
2794 {
2795 typval_T *stack = loopvars->lvs_ga.ga_data;
2796 int i;
2797
2798 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2799 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2800 return TRUE; // abort
2801 }
2802 return FALSE;
2803}
2804
2805/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002806 * Load instruction for w:/b:/g:/t: variable.
2807 * "isn_type" is used instead of "iptr->isn_type".
2808 */
2809 static int
2810load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2811{
2812 dictitem_T *di = NULL;
2813 hashtab_T *ht = NULL;
2814 char namespace;
2815
2816 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2817 return NOTDONE;
2818 switch (isn_type)
2819 {
2820 case ISN_LOADG:
2821 ht = get_globvar_ht();
2822 namespace = 'g';
2823 break;
2824 case ISN_LOADB:
2825 ht = &curbuf->b_vars->dv_hashtab;
2826 namespace = 'b';
2827 break;
2828 case ISN_LOADW:
2829 ht = &curwin->w_vars->dv_hashtab;
2830 namespace = 'w';
2831 break;
2832 case ISN_LOADT:
2833 ht = &curtab->tp_vars->dv_hashtab;
2834 namespace = 't';
2835 break;
2836 default: // Cannot reach here
2837 return NOTDONE;
2838 }
2839 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2840
Bram Moolenaar06b77222022-01-25 15:51:56 +00002841 if (di == NULL)
2842 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002843 if (isn_type == ISN_LOADG)
2844 {
2845 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2846
2847 // g:Something could be a function
2848 if (ufunc != NULL)
2849 {
2850 typval_T *tv = STACK_TV_BOT(0);
2851
2852 ++ectx->ec_stack.ga_len;
2853 tv->v_type = VAR_FUNC;
2854 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2855 if (tv->vval.v_string == NULL)
2856 return FAIL;
2857 STRCPY(tv->vval.v_string, "g:");
2858 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2859 return OK;
2860 }
2861 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002862 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002863 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002864 // no check if the item exists in the script but
2865 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002866 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002867 else
2868 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002869 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002870 return FAIL;
2871 }
2872 else
2873 {
2874 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2875 ++ectx->ec_stack.ga_len;
2876 }
2877 return OK;
2878}
2879
2880/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002881 * Execute instructions in execution context "ectx".
2882 * Return OK or FAIL;
2883 */
2884 static int
2885exec_instructions(ectx_T *ectx)
2886{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002887 int ret = FAIL;
2888 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002889 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002890
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002891 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002892 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002893
Bram Moolenaarff652882021-05-16 15:24:49 +02002894 // Only catch exceptions in this instruction list.
2895 ectx->ec_trylevel_at_start = trylevel;
2896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 for (;;)
2898 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002899 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002901 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002902
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002903 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002904 {
2905 line_breakcheck();
2906 breakcheck_count = 0;
2907 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002908 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002909 {
2910 // Turn CTRL-C into an exception.
2911 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002912 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002913 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002914 did_throw = TRUE;
2915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002917 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002918 {
2919 // Turn an error message into an exception.
2920 did_emsg = FALSE;
2921 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002922 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002923 did_throw = TRUE;
2924 *msg_list = NULL;
2925 }
2926
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002927 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002929 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002930 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002931 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932
2933 // An exception jumps to the first catch, finally, or returns from
2934 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002935 while (index > 0)
2936 {
2937 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002938 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002939 break;
2940 // In the catch and finally block of this try we have to go up
2941 // one level.
2942 --index;
2943 trycmd = NULL;
2944 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002945 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002947 if (trycmd->tcd_in_catch)
2948 {
2949 // exception inside ":catch", jump to ":finally" once
2950 ectx->ec_iidx = trycmd->tcd_finally_idx;
2951 trycmd->tcd_finally_idx = 0;
2952 }
2953 else
2954 // jump to first ":catch"
2955 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002956 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002957 did_throw = FALSE; // don't come back here until :endtry
2958 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 }
2960 else
2961 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002962 // Not inside try or need to return from current functions.
2963 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002964 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002965 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002966 tv = STACK_TV_BOT(0);
2967 tv->v_type = VAR_NUMBER;
2968 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002969 ++ectx->ec_stack.ga_len;
2970 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002972 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002973 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002974 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002975 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 goto done;
2977 }
2978
Bram Moolenaar4c137212021-04-19 16:48:48 +02002979 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002980 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 }
2982 continue;
2983 }
2984
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002985 /*
2986 * Big switch on the instruction. Most compilers will be turning this
2987 * into an efficient lookup table, since the "case" values are an enum
2988 * with sequential numbers. It may look ugly, but it should be the
2989 * most efficient way.
2990 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002991 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 switch (iptr->isn_type)
2993 {
2994 // execute Ex command line
2995 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002996 if (exec_command(iptr) == FAIL)
2997 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 break;
2999
Bram Moolenaar20677332021-06-06 17:02:53 +02003000 // execute Ex command line split at NL characters.
3001 case ISN_EXEC_SPLIT:
3002 {
3003 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003004 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003005
3006 SOURCING_LNUM = iptr->isn_lnum;
3007 CLEAR_FIELD(cookie);
3008 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3009 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003010 line = get_split_sourceline(0, &cookie, 0, 0);
3011 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003012 get_split_sourceline, &cookie,
3013 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3014 == FAIL
3015 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003016 {
3017 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003018 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003019 }
3020 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003021 }
3022 break;
3023
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003024 // execute Ex command line that is only a range
3025 case ISN_EXECRANGE:
3026 {
3027 exarg_T ea;
3028 char *error = NULL;
3029
3030 CLEAR_FIELD(ea);
3031 ea.cmdidx = CMD_SIZE;
3032 ea.addr_type = ADDR_LINES;
3033 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003034 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003035 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003036 if (ea.cmd == NULL)
3037 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003038 // error is always NULL when using ADDR_LINES
3039 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003040 if (error != NULL)
3041 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003042 emsg(error);
3043 goto on_error;
3044 }
3045 }
3046 break;
3047
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003048 // Evaluate an expression with legacy syntax, push it onto the
3049 // stack.
3050 case ISN_LEGACY_EVAL:
3051 {
3052 char_u *arg = iptr->isn_arg.string;
3053 int res;
3054 int save_flags = cmdmod.cmod_flags;
3055
Bram Moolenaar35578162021-08-02 19:10:38 +02003056 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003057 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003058 tv = STACK_TV_BOT(0);
3059 init_tv(tv);
3060 cmdmod.cmod_flags |= CMOD_LEGACY;
3061 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3062 cmdmod.cmod_flags = save_flags;
3063 if (res == FAIL)
3064 goto on_error;
3065 ++ectx->ec_stack.ga_len;
3066 }
3067 break;
3068
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003069 // push typeval VAR_INSTR with instructions to be executed
3070 case ISN_INSTR:
3071 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003072 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003073 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003074 tv = STACK_TV_BOT(0);
3075 tv->vval.v_instr = ALLOC_ONE(instr_T);
3076 if (tv->vval.v_instr == NULL)
3077 goto on_error;
3078 ++ectx->ec_stack.ga_len;
3079
3080 tv->v_type = VAR_INSTR;
3081 tv->vval.v_instr->instr_ectx = ectx;
3082 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3083 }
3084 break;
3085
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003086 case ISN_SOURCE:
3087 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003088 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003089
Bram Moolenaar89445512022-04-14 12:58:23 +01003090 SOURCING_LNUM = iptr->isn_lnum;
3091 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003092 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003093 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003094 }
3095 break;
3096
Bram Moolenaar4c137212021-04-19 16:48:48 +02003097 // execute :substitute with an expression
3098 case ISN_SUBSTITUTE:
3099 {
3100 subs_T *subs = &iptr->isn_arg.subs;
3101 source_cookie_T cookie;
3102 struct subs_expr_S *save_instr = substitute_instr;
3103 struct subs_expr_S subs_instr;
3104 int res;
3105
3106 subs_instr.subs_ectx = ectx;
3107 subs_instr.subs_instr = subs->subs_instr;
3108 subs_instr.subs_status = OK;
3109 substitute_instr = &subs_instr;
3110
3111 SOURCING_LNUM = iptr->isn_lnum;
3112 // This is very much like ISN_EXEC
3113 CLEAR_FIELD(cookie);
3114 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3115 res = do_cmdline(subs->subs_cmd,
3116 getsourceline, &cookie,
3117 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3118 substitute_instr = save_instr;
3119
3120 if (res == FAIL || did_emsg
3121 || subs_instr.subs_status == FAIL)
3122 goto on_error;
3123 }
3124 break;
3125
3126 case ISN_FINISH:
3127 goto done;
3128
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003129 case ISN_REDIRSTART:
3130 // create a dummy entry for var_redir_str()
3131 if (alloc_redir_lval() == FAIL)
3132 goto on_error;
3133
3134 // The output is stored in growarray "redir_ga" until
3135 // redirection ends.
3136 init_redir_ga();
3137 redir_vname = 1;
3138 break;
3139
3140 case ISN_REDIREND:
3141 {
3142 char_u *res = get_clear_redir_ga();
3143
3144 // End redirection, put redirected text on the stack.
3145 clear_redir_lval();
3146 redir_vname = 0;
3147
Bram Moolenaar35578162021-08-02 19:10:38 +02003148 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003149 {
3150 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003151 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003152 }
3153 tv = STACK_TV_BOT(0);
3154 tv->v_type = VAR_STRING;
3155 tv->vval.v_string = res;
3156 ++ectx->ec_stack.ga_len;
3157 }
3158 break;
3159
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003160 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003161#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003162 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003163 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3164 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003165 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003166#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003167 break;
3168
3169 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003170#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003171 {
3172 exarg_T ea;
3173 int res;
3174
3175 CLEAR_FIELD(ea);
3176 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3177 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3178 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3179 --ectx->ec_stack.ga_len;
3180 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003181 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003182 res = cexpr_core(&ea, tv);
3183 clear_tv(tv);
3184 if (res == FAIL)
3185 goto on_error;
3186 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003187#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003188 break;
3189
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003190 // execute Ex command from pieces on the stack
3191 case ISN_EXECCONCAT:
3192 {
3193 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003194 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003195 int pass;
3196 int i;
3197 char_u *cmd = NULL;
3198 char_u *str;
3199
3200 for (pass = 1; pass <= 2; ++pass)
3201 {
3202 for (i = 0; i < count; ++i)
3203 {
3204 tv = STACK_TV_BOT(i - count);
3205 str = tv->vval.v_string;
3206 if (str != NULL && *str != NUL)
3207 {
3208 if (pass == 2)
3209 STRCPY(cmd + len, str);
3210 len += STRLEN(str);
3211 }
3212 if (pass == 2)
3213 clear_tv(tv);
3214 }
3215 if (pass == 1)
3216 {
3217 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003218 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003219 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003220 len = 0;
3221 }
3222 }
3223
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003224 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003225 do_cmdline_cmd(cmd);
3226 vim_free(cmd);
3227 }
3228 break;
3229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 // execute :echo {string} ...
3231 case ISN_ECHO:
3232 {
3233 int count = iptr->isn_arg.echo.echo_count;
3234 int atstart = TRUE;
3235 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003236 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237
3238 for (idx = 0; idx < count; ++idx)
3239 {
3240 tv = STACK_TV_BOT(idx - count);
3241 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3242 &atstart, &needclr);
3243 clear_tv(tv);
3244 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003245 if (needclr)
3246 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003247 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 }
3249 break;
3250
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003251 // :execute {string} ...
3252 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003253 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003254 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003255 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003256 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003257 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003258 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003259 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003260 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003261 {
3262 int count = iptr->isn_arg.number;
3263 garray_T ga;
3264 char_u buf[NUMBUFLEN];
3265 char_u *p;
3266 int len;
3267 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003268 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003269
3270 ga_init2(&ga, 1, 80);
3271 for (idx = 0; idx < count; ++idx)
3272 {
3273 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003274 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003275 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003276 if (tv->v_type == VAR_CHANNEL
3277 || tv->v_type == VAR_JOB)
3278 {
3279 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003280 semsg(_(e_using_invalid_value_as_string_str),
3281 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003282 break;
3283 }
3284 else
3285 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003286 }
3287 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003288 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003289
3290 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003291 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003292 failed = TRUE;
3293 else
3294 {
3295 if (ga.ga_len > 0)
3296 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3297 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3298 ga.ga_len += len;
3299 }
3300 clear_tv(tv);
3301 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003302 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003303 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003304 {
3305 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003306 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003307 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003308
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003309 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003310 {
3311 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003312 {
3313 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003314 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003315 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003316 {
3317 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003318 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003319 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003320 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003321 else
3322 {
3323 msg_sb_eol();
3324 if (iptr->isn_type == ISN_ECHOMSG)
3325 {
3326 msg_attr(ga.ga_data, echo_attr);
3327 out_flush();
3328 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003329#ifdef HAS_MESSAGE_WINDOW
3330 else if (iptr->isn_type == ISN_ECHOWINDOW)
3331 {
3332 start_echowindow();
3333 msg_attr(ga.ga_data, echo_attr);
3334 end_echowindow();
3335 }
3336#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003337 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3338 {
3339 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3340 TRUE);
3341 ui_write((char_u *)"\r\n", 2, TRUE);
3342 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003343 else
3344 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003345 SOURCING_LNUM = iptr->isn_lnum;
3346 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003347 }
3348 }
3349 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003350 ga_clear(&ga);
3351 }
3352 break;
3353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 // load local variable or argument
3355 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003356 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003357 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003359 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 break;
3361
3362 // load v: variable
3363 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003364 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003365 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003367 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 break;
3369
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003370 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 case ISN_LOADSCRIPT:
3372 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003373 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 svar_T *sv;
3375
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003376 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003377 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003378 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003379 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003380 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003381 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003383 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 }
3385 break;
3386
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003387 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003389 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003391 int sid = iptr->isn_arg.loadstore.ls_sid;
3392 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003393 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 if (di == NULL)
3397 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003398 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003399 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003400 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 }
3402 else
3403 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003404 if (iptr->isn_type == ISN_LOADEXPORT)
3405 {
3406 int idx = get_script_item_idx(sid, name, 0,
3407 NULL, NULL);
3408 svar_T *sv;
3409
3410 if (idx >= 0)
3411 {
3412 sv = ((svar_T *)SCRIPT_ITEM(sid)
3413 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003414 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003415 {
3416 SOURCING_LNUM = iptr->isn_lnum;
3417 semsg(_(e_item_not_exported_in_script_str),
3418 name);
3419 goto on_error;
3420 }
3421 }
3422 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003423 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003424 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003426 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 }
3428 }
3429 break;
3430
Bram Moolenaard3aac292020-04-19 14:32:17 +02003431 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003433 case ISN_LOADB:
3434 case ISN_LOADW:
3435 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003437 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003438
Bram Moolenaar06b77222022-01-25 15:51:56 +00003439 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003440 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003441 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003442 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 break;
3446
Bram Moolenaar03290b82020-12-19 16:30:44 +01003447 // load autoload variable
3448 case ISN_LOADAUTO:
3449 {
3450 char_u *name = iptr->isn_arg.string;
3451
Bram Moolenaar35578162021-08-02 19:10:38 +02003452 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003453 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003454 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003455 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003456 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003457 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003458 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003459 }
3460 break;
3461
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003462 // load g:/b:/w:/t: namespace
3463 case ISN_LOADGDICT:
3464 case ISN_LOADBDICT:
3465 case ISN_LOADWDICT:
3466 case ISN_LOADTDICT:
3467 {
3468 dict_T *d = NULL;
3469
3470 switch (iptr->isn_type)
3471 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003472 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3473 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3474 case ISN_LOADWDICT: d = curwin->w_vars; break;
3475 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003476 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003477 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003478 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003479 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003480 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003481 tv = STACK_TV_BOT(0);
3482 tv->v_type = VAR_DICT;
3483 tv->v_lock = 0;
3484 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003485 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003486 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003487 }
3488 break;
3489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490 // load &option
3491 case ISN_LOADOPT:
3492 {
3493 typval_T optval;
3494 char_u *name = iptr->isn_arg.string;
3495
Bram Moolenaara8c17702020-04-01 21:17:24 +02003496 // This is not expected to fail, name is checked during
3497 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003498 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003499 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003500 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003501 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003503 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 }
3505 break;
3506
3507 // load $ENV
3508 case ISN_LOADENV:
3509 {
3510 typval_T optval;
3511 char_u *name = iptr->isn_arg.string;
3512
Bram Moolenaar35578162021-08-02 19:10:38 +02003513 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003514 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003515 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003516 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003518 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 }
3520 break;
3521
3522 // load @register
3523 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003524 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003525 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 tv = STACK_TV_BOT(0);
3527 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003528 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003529 // This may result in NULL, which should be equivalent to an
3530 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 tv->vval.v_string = get_reg_contents(
3532 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003533 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 break;
3535
3536 // store local variable
3537 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003538 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 tv = STACK_TV_VAR(iptr->isn_arg.number);
3540 clear_tv(tv);
3541 *tv = *STACK_TV_BOT(0);
3542 break;
3543
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003544 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003545 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003546 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003547 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003548 int sid = iptr->isn_arg.loadstore.ls_sid;
3549 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003550 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003551 dictitem_T *di = find_var_in_ht(ht, 0,
3552 iptr->isn_type == ISN_STORES
3553 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003554
Bram Moolenaar4c137212021-04-19 16:48:48 +02003555 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003556 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003557 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003558 {
3559 if (iptr->isn_type == ISN_STOREEXPORT)
3560 {
3561 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003562 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003563 goto on_error;
3564 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003565 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003566 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003567 else
3568 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003569 if (iptr->isn_type == ISN_STOREEXPORT)
3570 {
3571 int idx = get_script_item_idx(sid, name, 0,
3572 NULL, NULL);
3573
Bram Moolenaar06651632022-04-27 17:54:25 +01003574 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003575 if (idx >= 0)
3576 {
3577 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3578 ->sn_var_vals.ga_data) + idx;
3579
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003580 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003581 {
3582 semsg(_(e_item_not_exported_in_script_str),
3583 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003584 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003585 goto on_error;
3586 }
3587 }
3588 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003589 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003590 {
3591 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003592 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003593 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003594 clear_tv(&di->di_tv);
3595 di->di_tv = *STACK_TV_BOT(0);
3596 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003597 }
3598 break;
3599
3600 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 case ISN_STORESCRIPT:
3602 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003603 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3604 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003606 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003607 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003608 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003609 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003610
3611 // "const" and "final" are checked at compile time, locking
3612 // the value needs to be checked here.
3613 SOURCING_LNUM = iptr->isn_lnum;
3614 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003615 {
3616 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003617 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003618 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 clear_tv(sv->sv_tv);
3621 *sv->sv_tv = *STACK_TV_BOT(0);
3622 }
3623 break;
3624
3625 // store option
3626 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003627 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003629 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3630 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 long n = 0;
3632 char_u *s = NULL;
3633 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003634 char_u numbuf[NUMBUFLEN];
3635 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636
Bram Moolenaar4c137212021-04-19 16:48:48 +02003637 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 tv = STACK_TV_BOT(0);
3639 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003640 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003642 if (s == NULL)
3643 s = (char_u *)"";
3644 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003645 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3646 {
3647 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003648 // If the option can be set to a function reference or
3649 // a lambda and the passed value is a function
3650 // reference, then convert it to the name (string) of
3651 // the function reference.
3652 s = tv2string(tv, &tofree, numbuf, 0);
3653 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003654 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003655 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003656 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003657 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003658 goto on_error;
3659 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003660 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003662 // must be VAR_NUMBER, CHECKTYPE makes sure
3663 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003664 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003665 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003666 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 if (msg != NULL)
3668 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003669 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003671 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 }
3674 break;
3675
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003676 // store $ENV
3677 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003678 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003679 tv = STACK_TV_BOT(0);
3680 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3681 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003682 break;
3683
3684 // store @r
3685 case ISN_STOREREG:
3686 {
3687 int reg = iptr->isn_arg.number;
3688
Bram Moolenaar4c137212021-04-19 16:48:48 +02003689 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003690 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003691 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003692 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003693 }
3694 break;
3695
3696 // store v: variable
3697 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003698 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003699 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3700 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003701 // should not happen, type is checked when compiling
3702 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003703 break;
3704
Bram Moolenaard3aac292020-04-19 14:32:17 +02003705 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003707 case ISN_STOREB:
3708 case ISN_STOREW:
3709 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003711 dictitem_T *di;
3712 hashtab_T *ht;
3713 char_u *name = iptr->isn_arg.string + 2;
3714
Bram Moolenaard3aac292020-04-19 14:32:17 +02003715 switch (iptr->isn_type)
3716 {
3717 case ISN_STOREG:
3718 ht = get_globvar_ht();
3719 break;
3720 case ISN_STOREB:
3721 ht = &curbuf->b_vars->dv_hashtab;
3722 break;
3723 case ISN_STOREW:
3724 ht = &curwin->w_vars->dv_hashtab;
3725 break;
3726 case ISN_STORET:
3727 ht = &curtab->tp_vars->dv_hashtab;
3728 break;
3729 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003730 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003731 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732
Bram Moolenaar4c137212021-04-19 16:48:48 +02003733 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003734 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003736 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 else
3738 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003739 SOURCING_LNUM = iptr->isn_lnum;
3740 if (var_check_permission(di, name) == FAIL)
3741 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 clear_tv(&di->di_tv);
3743 di->di_tv = *STACK_TV_BOT(0);
3744 }
3745 }
3746 break;
3747
Bram Moolenaar03290b82020-12-19 16:30:44 +01003748 // store an autoload variable
3749 case ISN_STOREAUTO:
3750 SOURCING_LNUM = iptr->isn_lnum;
3751 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3752 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003753 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003754 break;
3755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 // store number in local variable
3757 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003758 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 clear_tv(tv);
3760 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003761 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 break;
3763
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003764 // store value in list or dict variable
3765 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003766 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003767 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003768
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003769 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003770 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003771 if (res == NOTDONE)
3772 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003773 }
3774 break;
3775
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003776 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003777 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003778 if (execute_storerange(iptr, ectx) == FAIL)
3779 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003780 break;
3781
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003782 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003783 case ISN_LOADOUTER:
3784 case ISN_STOREOUTER:
3785 {
3786 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003787 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3788 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003789
3790 while (depth > 1 && outer != NULL)
3791 {
3792 outer = outer->out_up;
3793 --depth;
3794 }
3795 if (outer == NULL)
3796 {
3797 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003798 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3799 || ectx->ec_outer_ref == NULL)
3800 // Possibly :def function called from legacy
3801 // context.
3802 emsg(_(e_closure_called_from_invalid_context));
3803 else
3804 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003805 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003806 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003807 if (depth == OUTER_LOOP_DEPTH)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003808 // Variable declared in loop. May be copied if the
3809 // loop block has already ended.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003810 tv = ((typval_T *)outer->out_loop_stack->ga_data)
3811 + outer->out_loop_var_idx
3812 + iptr->isn_arg.outer.outer_idx;
3813 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003814 // Variable declared in a function. May be copied if
3815 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003816 tv = ((typval_T *)outer->out_stack->ga_data)
3817 + outer->out_frame_idx + STACK_FRAME_SIZE
3818 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003819 if (iptr->isn_type == ISN_LOADOUTER)
3820 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003821 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003822 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003823 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003824 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003825 }
3826 else
3827 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003828 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003829 clear_tv(tv);
3830 *tv = *STACK_TV_BOT(0);
3831 }
3832 }
3833 break;
3834
Bram Moolenaar752fc692021-01-04 21:57:11 +01003835 // unlet item in list or dict variable
3836 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003837 if (execute_unletindex(iptr, ectx) == FAIL)
3838 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003839 break;
3840
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003841 // unlet range of items in list variable
3842 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003843 if (execute_unletrange(iptr, ectx) == FAIL)
3844 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003845 break;
3846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 // push constant
3848 case ISN_PUSHNR:
3849 case ISN_PUSHBOOL:
3850 case ISN_PUSHSPEC:
3851 case ISN_PUSHF:
3852 case ISN_PUSHS:
3853 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003854 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003855 case ISN_PUSHCHANNEL:
3856 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003857 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003858 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003860 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003861 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 switch (iptr->isn_type)
3863 {
3864 case ISN_PUSHNR:
3865 tv->v_type = VAR_NUMBER;
3866 tv->vval.v_number = iptr->isn_arg.number;
3867 break;
3868 case ISN_PUSHBOOL:
3869 tv->v_type = VAR_BOOL;
3870 tv->vval.v_number = iptr->isn_arg.number;
3871 break;
3872 case ISN_PUSHSPEC:
3873 tv->v_type = VAR_SPECIAL;
3874 tv->vval.v_number = iptr->isn_arg.number;
3875 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 case ISN_PUSHF:
3877 tv->v_type = VAR_FLOAT;
3878 tv->vval.v_float = iptr->isn_arg.fnumber;
3879 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 case ISN_PUSHBLOB:
3881 blob_copy(iptr->isn_arg.blob, tv);
3882 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003883 case ISN_PUSHFUNC:
3884 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003885 if (iptr->isn_arg.string == NULL)
3886 tv->vval.v_string = NULL;
3887 else
3888 tv->vval.v_string =
3889 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003890 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003891 case ISN_PUSHCHANNEL:
3892#ifdef FEAT_JOB_CHANNEL
3893 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003894 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003895#endif
3896 break;
3897 case ISN_PUSHJOB:
3898#ifdef FEAT_JOB_CHANNEL
3899 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003900 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003901#endif
3902 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 default:
3904 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003905 tv->vval.v_string = iptr->isn_arg.string == NULL
3906 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 }
3908 break;
3909
Bram Moolenaar06b77222022-01-25 15:51:56 +00003910 case ISN_AUTOLOAD:
3911 {
3912 char_u *name = iptr->isn_arg.string;
3913
3914 (void)script_autoload(name, FALSE);
3915 if (find_func(name, TRUE))
3916 {
3917 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3918 goto theend;
3919 tv = STACK_TV_BOT(0);
3920 tv->v_lock = 0;
3921 ++ectx->ec_stack.ga_len;
3922 tv->v_type = VAR_FUNC;
3923 tv->vval.v_string = vim_strsave(name);
3924 }
3925 else
3926 {
3927 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3928
3929 if (res == NOTDONE)
3930 goto theend;
3931 if (res == FAIL)
3932 goto on_error;
3933 }
3934 }
3935 break;
3936
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003937 case ISN_UNLET:
3938 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3939 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003940 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003941 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003942 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003943 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003944 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003945
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003946 case ISN_LOCKUNLOCK:
3947 {
3948 typval_T *lval_root_save = lval_root;
3949 int res;
3950
3951 // Stack has the local variable, argument the whole :lock
3952 // or :unlock command, like ISN_EXEC.
3953 --ectx->ec_stack.ga_len;
3954 lval_root = STACK_TV_BOT(0);
3955 res = exec_command(iptr);
3956 clear_tv(lval_root);
3957 lval_root = lval_root_save;
3958 if (res == FAIL)
3959 goto on_error;
3960 }
3961 break;
3962
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003963 case ISN_LOCKCONST:
3964 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3965 break;
3966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 // create a list from items on the stack; uses a single allocation
3968 // for the list header and the items
3969 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003970 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003971 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 break;
3973
3974 // create a dict from items on the stack
3975 case ISN_NEWDICT:
3976 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003977 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003979 SOURCING_LNUM = iptr->isn_lnum;
3980 res = exe_newdict(iptr->isn_arg.number, ectx);
3981 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003982 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003983 if (res == MAYBE)
3984 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 }
3986 break;
3987
LemonBoy372bcce2022-04-25 12:43:20 +01003988 case ISN_CONCAT:
3989 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3990 goto theend;
3991 break;
3992
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003993 // create a partial with NULL value
3994 case ISN_NEWPARTIAL:
3995 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3996 goto theend;
3997 ++ectx->ec_stack.ga_len;
3998 tv = STACK_TV_BOT(-1);
3999 tv->v_type = VAR_PARTIAL;
4000 tv->v_lock = 0;
4001 tv->vval.v_partial = NULL;
4002 break;
4003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 // call a :def function
4005 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004006 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004007 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4008 NULL,
4009 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004010 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004011 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 break;
4013
4014 // call a builtin function
4015 case ISN_BCALL:
4016 SOURCING_LNUM = iptr->isn_lnum;
4017 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4018 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004019 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004020 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 break;
4022
4023 // call a funcref or partial
4024 case ISN_PCALL:
4025 {
4026 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4027 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004028 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029
4030 SOURCING_LNUM = iptr->isn_lnum;
4031 if (pfunc->cpf_top)
4032 {
4033 // funcref is above the arguments
4034 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4035 }
4036 else
4037 {
4038 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004039 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004040 partial_tv = *STACK_TV_BOT(0);
4041 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004043 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004044 if (tv == &partial_tv)
4045 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004047 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 }
4049 break;
4050
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004051 case ISN_PCALL_END:
4052 // PCALL finished, arguments have been consumed and replaced by
4053 // the return value. Now clear the funcref from the stack,
4054 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004055 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004056 clear_tv(STACK_TV_BOT(-1));
4057 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4058 break;
4059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 // call a user defined function or funcref/partial
4061 case ISN_UCALL:
4062 {
4063 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4064
4065 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004066 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004067 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004068 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 }
4070 break;
4071
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004072 // :defer func(arg)
4073 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004074 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004075 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4076 goto on_error;
4077 break;
4078
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004079 // return from a :def function call without a value
4080 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02004081 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004082 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004083 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004084 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004085 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004086 tv->vval.v_number = 0;
4087 tv->v_lock = 0;
4088 // FALLTHROUGH
4089
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004090 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 case ISN_RETURN:
4092 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004093 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004094 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004095
4096 if (trystack->ga_len > 0)
4097 trycmd = ((trycmd_T *)trystack->ga_data)
4098 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004099 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004100 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004102 // jump to ":finally" or ":endtry"
4103 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004104 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004105 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004106 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 trycmd->tcd_return = TRUE;
4108 }
4109 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004110 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 }
4112 break;
4113
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004114 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 case ISN_FUNCREF:
4116 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004117 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4118 ufunc_T *ufunc;
4119 funcref_T *funcref = &iptr->isn_arg.funcref;
4120 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004123 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004124 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004125 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004126 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004127 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004128 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004129 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004130 {
4131 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4132 + funcref->fr_dfunc_idx;
4133
4134 ufunc = pt_dfunc->df_ufunc;
4135 }
4136 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004137 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004138 if (ufunc == NULL)
4139 {
4140 SOURCING_LNUM = iptr->isn_lnum;
4141 iemsg("ufunc unexpectedly NULL for FUNCREF");
4142 goto theend;
4143 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004144 if (fill_partial_and_closure(pt, ufunc,
4145 extra == NULL ? 0 : extra->fre_loop_var_idx,
4146 extra == NULL ? 0 : extra->fre_loop_var_count,
4147 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004148 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004150 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 tv->vval.v_partial = pt;
4152 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004153 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 }
4155 break;
4156
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004157 // Create a global function from a lambda.
4158 case ISN_NEWFUNC:
4159 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004160 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004161
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004162 if (copy_lambda_to_global_func(arg->nfa_lambda,
4163 arg->nfa_global, arg->nfa_loop_var_idx,
4164 arg->nfa_loop_var_count, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004165 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004166 }
4167 break;
4168
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004169 // List functions
4170 case ISN_DEF:
4171 if (iptr->isn_arg.string == NULL)
4172 list_functions(NULL);
4173 else
4174 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004175 exarg_T ea;
4176 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004177
4178 CLEAR_FIELD(ea);
4179 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004180 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4181 define_function(&ea, NULL, &lines_to_free);
4182 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004183 }
4184 break;
4185
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 // jump if a condition is met
4187 case ISN_JUMP:
4188 {
4189 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004190 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 int jump = TRUE;
4192
4193 if (when != JUMP_ALWAYS)
4194 {
4195 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004196 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004197 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004198 || when == JUMP_IF_COND_TRUE)
4199 {
4200 SOURCING_LNUM = iptr->isn_lnum;
4201 jump = tv_get_bool_chk(tv, &error);
4202 if (error)
4203 goto on_error;
4204 }
4205 else
4206 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004207 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004209 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 {
4211 // drop the value from the stack
4212 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004213 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 }
4215 }
4216 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004217 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 }
4219 break;
4220
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004221 // "while": jump to end if a condition is false
4222 case ISN_WHILE:
4223 {
4224 int error = FALSE;
4225 int jump = TRUE;
4226
4227 tv = STACK_TV_BOT(-1);
4228 SOURCING_LNUM = iptr->isn_lnum;
4229 jump = !tv_get_bool_chk(tv, &error);
4230 if (error)
4231 goto on_error;
4232 // drop the value from the stack
4233 clear_tv(tv);
4234 --ectx->ec_stack.ga_len;
4235 if (jump)
4236 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4237
4238 // Store the current funccal count, may be used by
4239 // ISN_LOOPEND later
4240 tv = STACK_TV_VAR(
4241 iptr->isn_arg.whileloop.while_funcref_idx);
4242 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4243 }
4244 break;
4245
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004246 // Jump if an argument with a default value was already set and not
4247 // v:none.
4248 case ISN_JUMP_IF_ARG_SET:
4249 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
4250 if (tv->v_type != VAR_UNKNOWN
4251 && !(tv->v_type == VAR_SPECIAL
4252 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004253 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004254 break;
4255
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 // top of a for loop
4257 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004258 if (execute_for(iptr, ectx) == FAIL)
4259 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 break;
4261
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004262 // end of a for or while loop
4263 case ISN_ENDLOOP:
4264 if (execute_endloop(iptr, ectx) == FAIL)
4265 goto theend;
4266 break;
4267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 // start of ":try" block
4269 case ISN_TRY:
4270 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004271 trycmd_T *trycmd = NULL;
4272
Bram Moolenaar35578162021-08-02 19:10:38 +02004273 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004274 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004275 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4276 + ectx->ec_trystack.ga_len;
4277 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004279 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004280 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4281 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004282 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004283 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004284 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004285 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004286 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004287 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 }
4289 break;
4290
4291 case ISN_PUSHEXC:
4292 if (current_exception == NULL)
4293 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004294 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004296 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004298 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004299 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004301 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004303 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 tv->vval.v_string = vim_strsave(
4305 (char_u *)current_exception->value);
4306 break;
4307
4308 case ISN_CATCH:
4309 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004310 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004311 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312
Bram Moolenaar4c137212021-04-19 16:48:48 +02004313 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004314 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004316 trycmd->tcd_caught = TRUE;
4317 trycmd->tcd_did_throw = FALSE;
4318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004320 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 catch_exception(current_exception);
4322 }
4323 break;
4324
Bram Moolenaarc150c092021-02-13 15:02:46 +01004325 case ISN_TRYCONT:
4326 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004327 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004328 trycont_T *trycont = &iptr->isn_arg.trycont;
4329 int i;
4330 trycmd_T *trycmd;
4331 int iidx = trycont->tct_where;
4332
4333 if (trystack->ga_len < trycont->tct_levels)
4334 {
4335 siemsg("TRYCONT: expected %d levels, found %d",
4336 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004337 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004338 }
4339 // Make :endtry jump to any outer try block and the last
4340 // :endtry inside the loop to the loop start.
4341 for (i = trycont->tct_levels; i > 0; --i)
4342 {
4343 trycmd = ((trycmd_T *)trystack->ga_data)
4344 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004345 // Add one to tcd_cont to be able to jump to
4346 // instruction with index zero.
4347 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004348 iidx = trycmd->tcd_finally_idx == 0
4349 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004350 }
4351 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004352 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004353 }
4354 break;
4355
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004356 case ISN_FINALLY:
4357 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004358 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004359 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4360 + trystack->ga_len - 1;
4361
4362 // Reset the index to avoid a return statement jumps here
4363 // again.
4364 trycmd->tcd_finally_idx = 0;
4365 break;
4366 }
4367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 // end of ":try" block
4369 case ISN_ENDTRY:
4370 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004371 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004372 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373
Bram Moolenaar06651632022-04-27 17:54:25 +01004374 --trystack->ga_len;
4375 --trylevel;
4376 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4377 if (trycmd->tcd_did_throw)
4378 did_throw = TRUE;
4379 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004381 // discard the exception
4382 if (caught_stack == current_exception)
4383 caught_stack = caught_stack->caught;
4384 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004386
4387 if (trycmd->tcd_return)
4388 goto func_return;
4389
4390 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4391 {
4392 --ectx->ec_stack.ga_len;
4393 clear_tv(STACK_TV_BOT(0));
4394 }
4395 if (trycmd->tcd_cont != 0)
4396 // handling :continue: jump to outer try block or
4397 // start of the loop
4398 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 }
4400 break;
4401
4402 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004403 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004404 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004405
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004406 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4407 {
4408 // throwing an exception while using "silent!" causes
4409 // the function to abort but not display an error.
4410 tv = STACK_TV_BOT(-1);
4411 clear_tv(tv);
4412 tv->v_type = VAR_NUMBER;
4413 tv->vval.v_number = 0;
4414 goto done;
4415 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004416 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004417 tv = STACK_TV_BOT(0);
4418 if (tv->vval.v_string == NULL
4419 || *skipwhite(tv->vval.v_string) == NUL)
4420 {
4421 vim_free(tv->vval.v_string);
4422 SOURCING_LNUM = iptr->isn_lnum;
4423 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004424 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004425 }
4426
4427 // Inside a "catch" we need to first discard the caught
4428 // exception.
4429 if (trystack->ga_len > 0)
4430 {
4431 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4432 + trystack->ga_len - 1;
4433 if (trycmd->tcd_caught && current_exception != NULL)
4434 {
4435 // discard the exception
4436 if (caught_stack == current_exception)
4437 caught_stack = caught_stack->caught;
4438 discard_current_exception();
4439 trycmd->tcd_caught = FALSE;
4440 }
4441 }
4442
Bram Moolenaar90a57162022-02-12 14:23:17 +00004443 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004444 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4445 == FAIL)
4446 {
4447 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004448 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004449 }
4450 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 break;
4453
4454 // compare with special values
4455 case ISN_COMPAREBOOL:
4456 case ISN_COMPARESPECIAL:
4457 {
4458 typval_T *tv1 = STACK_TV_BOT(-2);
4459 typval_T *tv2 = STACK_TV_BOT(-1);
4460 varnumber_T arg1 = tv1->vval.v_number;
4461 varnumber_T arg2 = tv2->vval.v_number;
4462 int res;
4463
Bram Moolenaar06651632022-04-27 17:54:25 +01004464 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4465 res = arg1 == arg2;
4466 else
4467 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468
Bram Moolenaar4c137212021-04-19 16:48:48 +02004469 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 tv1->v_type = VAR_BOOL;
4471 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4472 }
4473 break;
4474
Bram Moolenaar7a222242022-03-01 19:23:24 +00004475 case ISN_COMPARENULL:
4476 {
4477 typval_T *tv1 = STACK_TV_BOT(-2);
4478 typval_T *tv2 = STACK_TV_BOT(-1);
4479 int res;
4480
4481 res = typval_compare_null(tv1, tv2);
4482 if (res == MAYBE)
4483 goto on_error;
4484 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4485 res = !res;
4486 clear_tv(tv1);
4487 clear_tv(tv2);
4488 --ectx->ec_stack.ga_len;
4489 tv1->v_type = VAR_BOOL;
4490 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4491 }
4492 break;
4493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 // Operation with two number arguments
4495 case ISN_OPNR:
4496 case ISN_COMPARENR:
4497 {
4498 typval_T *tv1 = STACK_TV_BOT(-2);
4499 typval_T *tv2 = STACK_TV_BOT(-1);
4500 varnumber_T arg1 = tv1->vval.v_number;
4501 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004502 varnumber_T res = 0;
4503 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004505 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4506 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4507 {
4508 if (arg2 < 0)
4509 {
4510 SOURCING_LNUM = iptr->isn_lnum;
4511 emsg(_(e_bitshift_ops_must_be_postive));
4512 goto on_error;
4513 }
4514 }
4515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 switch (iptr->isn_arg.op.op_type)
4517 {
4518 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004519 case EXPR_DIV: if (arg2 == 0)
4520 div_zero = TRUE;
4521 else
4522 res = arg1 / arg2;
4523 break;
4524 case EXPR_REM: if (arg2 == 0)
4525 div_zero = TRUE;
4526 else
4527 res = arg1 % arg2;
4528 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 case EXPR_SUB: res = arg1 - arg2; break;
4530 case EXPR_ADD: res = arg1 + arg2; break;
4531
4532 case EXPR_EQUAL: res = arg1 == arg2; break;
4533 case EXPR_NEQUAL: res = arg1 != arg2; break;
4534 case EXPR_GREATER: res = arg1 > arg2; break;
4535 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4536 case EXPR_SMALLER: res = arg1 < arg2; break;
4537 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004538 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4539 res = 0;
4540 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004541 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004542 break;
4543 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4544 res = 0;
4545 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004546 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004547 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004548 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 }
4550
Bram Moolenaar4c137212021-04-19 16:48:48 +02004551 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 if (iptr->isn_type == ISN_COMPARENR)
4553 {
4554 tv1->v_type = VAR_BOOL;
4555 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4556 }
4557 else
4558 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004559 if (div_zero)
4560 {
4561 SOURCING_LNUM = iptr->isn_lnum;
4562 emsg(_(e_divide_by_zero));
4563 goto on_error;
4564 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 }
4566 break;
4567
4568 // Computation with two float arguments
4569 case ISN_OPFLOAT:
4570 case ISN_COMPAREFLOAT:
4571 {
4572 typval_T *tv1 = STACK_TV_BOT(-2);
4573 typval_T *tv2 = STACK_TV_BOT(-1);
4574 float_T arg1 = tv1->vval.v_float;
4575 float_T arg2 = tv2->vval.v_float;
4576 float_T res = 0;
4577 int cmp = FALSE;
4578
4579 switch (iptr->isn_arg.op.op_type)
4580 {
4581 case EXPR_MULT: res = arg1 * arg2; break;
4582 case EXPR_DIV: res = arg1 / arg2; break;
4583 case EXPR_SUB: res = arg1 - arg2; break;
4584 case EXPR_ADD: res = arg1 + arg2; break;
4585
4586 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4587 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4588 case EXPR_GREATER: cmp = arg1 > arg2; break;
4589 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4590 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4591 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4592 default: cmp = 0; break;
4593 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004594 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 if (iptr->isn_type == ISN_COMPAREFLOAT)
4596 {
4597 tv1->v_type = VAR_BOOL;
4598 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4599 }
4600 else
4601 tv1->vval.v_float = res;
4602 }
4603 break;
4604
4605 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004606 case ISN_COMPAREDICT:
4607 case ISN_COMPAREFUNC:
4608 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 case ISN_COMPAREBLOB:
4610 {
4611 typval_T *tv1 = STACK_TV_BOT(-2);
4612 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004613 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4614 int ic = iptr->isn_arg.op.op_ic;
4615 int res = FALSE;
4616 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617
Bram Moolenaar265f8112021-12-19 12:33:05 +00004618 SOURCING_LNUM = iptr->isn_lnum;
4619 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004621 status = typval_compare_list(tv1, tv2,
4622 exprtype, ic, &res);
4623 }
4624 else if (iptr->isn_type == ISN_COMPAREDICT)
4625 {
4626 status = typval_compare_dict(tv1, tv2,
4627 exprtype, ic, &res);
4628 }
4629 else if (iptr->isn_type == ISN_COMPAREFUNC)
4630 {
4631 status = typval_compare_func(tv1, tv2,
4632 exprtype, ic, &res);
4633 }
4634 else if (iptr->isn_type == ISN_COMPARESTRING)
4635 {
4636 status = typval_compare_string(tv1, tv2,
4637 exprtype, ic, &res);
4638 }
4639 else
4640 {
4641 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004643 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 clear_tv(tv1);
4645 clear_tv(tv2);
4646 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004647 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4648 if (status == FAIL)
4649 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 }
4651 break;
4652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 case ISN_COMPAREANY:
4654 {
4655 typval_T *tv1 = STACK_TV_BOT(-2);
4656 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004657 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004659 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660
Bram Moolenaareb26f432020-09-14 16:50:05 +02004661 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004662 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004664 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004665 if (status == FAIL)
4666 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 }
4668 break;
4669
4670 case ISN_ADDLIST:
4671 case ISN_ADDBLOB:
4672 {
4673 typval_T *tv1 = STACK_TV_BOT(-2);
4674 typval_T *tv2 = STACK_TV_BOT(-1);
4675
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004676 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004678 {
4679 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4680 && tv1->vval.v_list != NULL)
4681 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4682 NULL);
4683 else
4684 eval_addlist(tv1, tv2);
4685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 else
4687 eval_addblob(tv1, tv2);
4688 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004689 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 }
4691 break;
4692
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004693 case ISN_LISTAPPEND:
4694 {
4695 typval_T *tv1 = STACK_TV_BOT(-2);
4696 typval_T *tv2 = STACK_TV_BOT(-1);
4697 list_T *l = tv1->vval.v_list;
4698
4699 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004700 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004701 if (l == NULL)
4702 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004703 emsg(_(e_cannot_add_to_null_list));
4704 goto on_error;
4705 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004706 if (value_check_lock(l->lv_lock, NULL, FALSE))
4707 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004708 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004709 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004710 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004711 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004712 }
4713 break;
4714
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004715 case ISN_BLOBAPPEND:
4716 {
4717 typval_T *tv1 = STACK_TV_BOT(-2);
4718 typval_T *tv2 = STACK_TV_BOT(-1);
4719 blob_T *b = tv1->vval.v_blob;
4720 int error = FALSE;
4721 varnumber_T n;
4722
4723 // add a number to a blob
4724 if (b == NULL)
4725 {
4726 SOURCING_LNUM = iptr->isn_lnum;
4727 emsg(_(e_cannot_add_to_null_blob));
4728 goto on_error;
4729 }
4730 n = tv_get_number_chk(tv2, &error);
4731 if (error)
4732 goto on_error;
4733 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004734 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004735 }
4736 break;
4737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 // Computation with two arguments of unknown type
4739 case ISN_OPANY:
4740 {
4741 typval_T *tv1 = STACK_TV_BOT(-2);
4742 typval_T *tv2 = STACK_TV_BOT(-1);
4743 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 int error = FALSE;
4746
4747 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4748 {
4749 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4750 {
4751 eval_addlist(tv1, tv2);
4752 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004753 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754 break;
4755 }
4756 else if (tv1->v_type == VAR_BLOB
4757 && tv2->v_type == VAR_BLOB)
4758 {
4759 eval_addblob(tv1, tv2);
4760 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004761 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 break;
4763 }
4764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 if (tv1->v_type == VAR_FLOAT)
4766 {
4767 f1 = tv1->vval.v_float;
4768 n1 = 0;
4769 }
4770 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004772 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 n1 = tv_get_number_chk(tv1, &error);
4774 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004775 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 if (tv2->v_type == VAR_FLOAT)
4777 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 if (tv2->v_type == VAR_FLOAT)
4780 {
4781 f2 = tv2->vval.v_float;
4782 n2 = 0;
4783 }
4784 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 {
4786 n2 = tv_get_number_chk(tv2, &error);
4787 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004788 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 if (tv1->v_type == VAR_FLOAT)
4790 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 // if there is a float on either side the result is a float
4793 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4794 {
4795 switch (iptr->isn_arg.op.op_type)
4796 {
4797 case EXPR_MULT: f1 = f1 * f2; break;
4798 case EXPR_DIV: f1 = f1 / f2; break;
4799 case EXPR_SUB: f1 = f1 - f2; break;
4800 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004801 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004802 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004803 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 }
4805 clear_tv(tv1);
4806 clear_tv(tv2);
4807 tv1->v_type = VAR_FLOAT;
4808 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004809 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810 }
4811 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004813 int failed = FALSE;
4814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 switch (iptr->isn_arg.op.op_type)
4816 {
4817 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004818 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4819 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004820 goto on_error;
4821 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 case EXPR_SUB: n1 = n1 - n2; break;
4823 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004824 default: n1 = num_modulus(n1, n2, &failed);
4825 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004826 goto on_error;
4827 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 }
4829 clear_tv(tv1);
4830 clear_tv(tv2);
4831 tv1->v_type = VAR_NUMBER;
4832 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004833 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 }
4835 }
4836 break;
4837
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004838 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004839 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004840 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004841 int is_slice = iptr->isn_type == ISN_STRSLICE;
4842 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004843 char_u *res;
4844
4845 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004846 // string slice: string is at stack-3, first index at
4847 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004848 if (is_slice)
4849 {
4850 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004851 n1 = tv->vval.v_number;
4852 }
4853
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004854 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004855 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004856
Bram Moolenaar4c137212021-04-19 16:48:48 +02004857 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004858 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004859 if (is_slice)
4860 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004861 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004862 else
4863 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004864 // single character (including composing characters).
4865 // If the index is too big or negative the result is
4866 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004867 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004868 vim_free(tv->vval.v_string);
4869 tv->vval.v_string = res;
4870 }
4871 break;
4872
4873 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004874 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004875 case ISN_BLOBINDEX:
4876 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004878 int is_slice = iptr->isn_type == ISN_LISTSLICE
4879 || iptr->isn_type == ISN_BLOBSLICE;
4880 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4881 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004882 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004883 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884
4885 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004886 // list slice: list is at stack-3, indexes at stack-2 and
4887 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004888 // Same for blob.
4889 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890
4891 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004892 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004894
4895 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 {
Bram Moolenaared591872020-08-15 22:14:53 +02004897 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004898 n1 = tv->vval.v_number;
4899 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 }
Bram Moolenaared591872020-08-15 22:14:53 +02004901
Bram Moolenaar4c137212021-04-19 16:48:48 +02004902 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004903 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004904 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004905 if (is_blob)
4906 {
4907 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4908 n1, n2, FALSE, tv) == FAIL)
4909 goto on_error;
4910 }
4911 else
4912 {
4913 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4914 n1, n2, FALSE, tv, TRUE) == FAIL)
4915 goto on_error;
4916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 }
4918 break;
4919
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004920 case ISN_ANYINDEX:
4921 case ISN_ANYSLICE:
4922 {
4923 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4924 typval_T *var1, *var2;
4925 int res;
4926
4927 // index: composite is at stack-2, index at stack-1
4928 // slice: composite is at stack-3, indexes at stack-2 and
4929 // stack-1
4930 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004931 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004932 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4933 goto on_error;
4934 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4935 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004936 res = eval_index_inner(tv, is_slice, var1, var2,
4937 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004938 clear_tv(var1);
4939 if (is_slice)
4940 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004941 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004942 if (res == FAIL)
4943 goto on_error;
4944 }
4945 break;
4946
Bram Moolenaar9af78762020-06-16 11:34:42 +02004947 case ISN_SLICE:
4948 {
4949 list_T *list;
4950 int count = iptr->isn_arg.number;
4951
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004952 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004953 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004954 list = tv->vval.v_list;
4955
4956 // no error for short list, expect it to be checked earlier
4957 if (list != NULL && list->lv_len >= count)
4958 {
4959 list_T *newlist = list_slice(list,
4960 count, list->lv_len - 1);
4961
4962 if (newlist != NULL)
4963 {
4964 list_unref(list);
4965 tv->vval.v_list = newlist;
4966 ++newlist->lv_refcount;
4967 }
4968 }
4969 }
4970 break;
4971
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004972 case ISN_GETITEM:
4973 {
4974 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004975 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004976
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004977 // Get list item: list is at stack-1, push item.
4978 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004979 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4980 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004981
Bram Moolenaar35578162021-08-02 19:10:38 +02004982 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004983 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004984 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004985 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004986
4987 // Useful when used in unpack assignment. Reset at
4988 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004989 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004990 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004991 }
4992 break;
4993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 case ISN_MEMBER:
4995 {
4996 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004997 char_u *key;
4998 dictitem_T *di;
4999
5000 // dict member: dict is at stack-2, key at stack-1
5001 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005002 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005003 dict = tv->vval.v_dict;
5004
5005 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005006 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005007 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005008 if (key == NULL)
5009 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005010
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005011 if ((di = dict_find(dict, key, -1)) == NULL)
5012 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005013 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005014 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005015
5016 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005017 // stack contents makes sense and the dict stack is
5018 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005019 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005020 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005021 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005022 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005023 tv->v_type = VAR_NUMBER;
5024 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005025 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005026 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005027 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005028 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005029 // Put the dict used on the dict stack, it might be used by
5030 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005031 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005032 if (dict_stack_save(tv) == FAIL)
5033 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005034 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005035 }
5036 break;
5037
5038 // dict member with string key
5039 case ISN_STRINGMEMBER:
5040 {
5041 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 dictitem_T *di;
5043
5044 tv = STACK_TV_BOT(-1);
5045 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5046 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005047 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005048 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005049 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 }
5051 dict = tv->vval.v_dict;
5052
5053 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5054 == NULL)
5055 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005056 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00005057 semsg(_(e_key_not_present_in_dictionary),
5058 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005059 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005061 // Put the dict used on the dict stack, it might be used by
5062 // a dict function later.
5063 if (dict_stack_save(tv) == FAIL)
5064 goto on_fatal_error;
5065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005067 }
5068 break;
5069
5070 case ISN_CLEARDICT:
5071 dict_stack_drop();
5072 break;
5073
5074 case ISN_USEDICT:
5075 {
5076 typval_T *dict_tv = dict_stack_get_tv();
5077
5078 // Turn "dict.Func" into a partial for "Func" bound to
5079 // "dict". Don't do this when "Func" is already a partial
5080 // that was bound explicitly (pt_auto is FALSE).
5081 tv = STACK_TV_BOT(-1);
5082 if (dict_tv != NULL
5083 && dict_tv->v_type == VAR_DICT
5084 && dict_tv->vval.v_dict != NULL
5085 && (tv->v_type == VAR_FUNC
5086 || (tv->v_type == VAR_PARTIAL
5087 && (tv->vval.v_partial->pt_auto
5088 || tv->vval.v_partial->pt_dict == NULL))))
5089 dict_tv->vval.v_dict =
5090 make_partial(dict_tv->vval.v_dict, tv);
5091 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092 }
5093 break;
5094
5095 case ISN_NEGATENR:
5096 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005097 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005098 if (tv->v_type == VAR_FLOAT)
5099 tv->vval.v_float = -tv->vval.v_float;
5100 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005101 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 break;
5103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 case ISN_CHECKTYPE:
5105 {
5106 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005107 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005108 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005110 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005111 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005112 if (!ectx->ec_where.wt_variable)
5113 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005114 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005115 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005116 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005117 if (r == FAIL)
5118 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005119 if (!ectx->ec_where.wt_variable)
5120 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005121
5122 // number 0 is FALSE, number 1 is TRUE
5123 if (tv->v_type == VAR_NUMBER
5124 && ct->ct_type->tt_type == VAR_BOOL
5125 && (tv->vval.v_number == 0
5126 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005128 tv->v_type = VAR_BOOL;
5129 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005130 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 }
5132 }
5133 break;
5134
Bram Moolenaar9af78762020-06-16 11:34:42 +02005135 case ISN_CHECKLEN:
5136 {
5137 int min_len = iptr->isn_arg.checklen.cl_min_len;
5138 list_T *list = NULL;
5139
5140 tv = STACK_TV_BOT(-1);
5141 if (tv->v_type == VAR_LIST)
5142 list = tv->vval.v_list;
5143 if (list == NULL || list->lv_len < min_len
5144 || (list->lv_len > min_len
5145 && !iptr->isn_arg.checklen.cl_more_OK))
5146 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005147 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005148 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005149 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005150 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005151 }
5152 }
5153 break;
5154
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005155 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005156 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005157 break;
5158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005159 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005160 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161 {
5162 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005163 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005165 if (iptr->isn_type == ISN_2BOOL)
5166 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005167 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005168 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005169 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005170 n = !n;
5171 }
5172 else
5173 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005174 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005175 SOURCING_LNUM = iptr->isn_lnum;
5176 n = tv_get_bool_chk(tv, &error);
5177 if (error)
5178 goto on_error;
5179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 clear_tv(tv);
5181 tv->v_type = VAR_BOOL;
5182 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5183 }
5184 break;
5185
5186 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005187 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005188 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005189 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5190 iptr->isn_type == ISN_2STRING_ANY,
5191 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005192 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 break;
5194
Bram Moolenaar08597872020-12-10 19:43:40 +01005195 case ISN_RANGE:
5196 {
5197 exarg_T ea;
5198 char *errormsg;
5199
Bram Moolenaarece0b872021-01-08 20:40:45 +01005200 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005201 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005202 ea.addr_type = ADDR_LINES;
5203 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005204 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005205 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005206 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005207
Bram Moolenaar35578162021-08-02 19:10:38 +02005208 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005209 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005210 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005211 tv = STACK_TV_BOT(-1);
5212 tv->v_type = VAR_NUMBER;
5213 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005214 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005215 }
5216 break;
5217
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005218 case ISN_PUT:
5219 {
5220 int regname = iptr->isn_arg.put.put_regname;
5221 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5222 char_u *expr = NULL;
5223 int dir = FORWARD;
5224
Bram Moolenaar08597872020-12-10 19:43:40 +01005225 if (lnum < -2)
5226 {
5227 // line number was put on the stack by ISN_RANGE
5228 tv = STACK_TV_BOT(-1);
5229 curwin->w_cursor.lnum = tv->vval.v_number;
5230 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5231 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005232 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005233 }
5234 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005235 // :put! above cursor
5236 dir = BACKWARD;
5237 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005238 {
5239 curwin->w_cursor.lnum = lnum;
5240 if (lnum == 0)
5241 // check_cursor() below will move to line 1
5242 dir = BACKWARD;
5243 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005244
5245 if (regname == '=')
5246 {
5247 tv = STACK_TV_BOT(-1);
5248 if (tv->v_type == VAR_STRING)
5249 expr = tv->vval.v_string;
5250 else
5251 {
5252 expr = typval2string(tv, TRUE); // allocates value
5253 clear_tv(tv);
5254 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005255 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005256 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005257 check_cursor();
5258 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5259 vim_free(expr);
5260 }
5261 break;
5262
Bram Moolenaar02194d22020-10-24 23:08:38 +02005263 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005264 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5265 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5266 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5267 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005268 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5269 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005270 break;
5271
Bram Moolenaar02194d22020-10-24 23:08:38 +02005272 case ISN_CMDMOD_REV:
5273 // filter regprog is owned by the instruction, don't free it
5274 cmdmod.cmod_filter_regmatch.regprog = NULL;
5275 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005276 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5277 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005278 break;
5279
Bram Moolenaar792f7862020-11-23 08:31:18 +01005280 case ISN_UNPACK:
5281 {
5282 int count = iptr->isn_arg.unpack.unp_count;
5283 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5284 list_T *l;
5285 listitem_T *li;
5286 int i;
5287
5288 // Check there is a valid list to unpack.
5289 tv = STACK_TV_BOT(-1);
5290 if (tv->v_type != VAR_LIST)
5291 {
5292 SOURCING_LNUM = iptr->isn_lnum;
5293 emsg(_(e_for_argument_must_be_sequence_of_lists));
5294 goto on_error;
5295 }
5296 l = tv->vval.v_list;
5297 if (l == NULL
5298 || l->lv_len < (semicolon ? count - 1 : count))
5299 {
5300 SOURCING_LNUM = iptr->isn_lnum;
5301 emsg(_(e_list_value_does_not_have_enough_items));
5302 goto on_error;
5303 }
5304 else if (!semicolon && l->lv_len > count)
5305 {
5306 SOURCING_LNUM = iptr->isn_lnum;
5307 emsg(_(e_list_value_has_more_items_than_targets));
5308 goto on_error;
5309 }
5310
5311 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005312 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005313 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005314 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005315
5316 // Variable after semicolon gets a list with the remaining
5317 // items.
5318 if (semicolon)
5319 {
5320 list_T *rem_list =
5321 list_alloc_with_items(l->lv_len - count + 1);
5322
5323 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005324 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005325 tv = STACK_TV_BOT(-count);
5326 tv->vval.v_list = rem_list;
5327 ++rem_list->lv_refcount;
5328 tv->v_lock = 0;
5329 li = l->lv_first;
5330 for (i = 0; i < count - 1; ++i)
5331 li = li->li_next;
5332 for (i = 0; li != NULL; ++i)
5333 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005334 typval_T tvcopy;
5335
5336 copy_tv(&li->li_tv, &tvcopy);
5337 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005338 li = li->li_next;
5339 }
5340 --count;
5341 }
5342
5343 // Produce the values in reverse order, first item last.
5344 li = l->lv_first;
5345 for (i = 0; i < count; ++i)
5346 {
5347 tv = STACK_TV_BOT(-i - 1);
5348 copy_tv(&li->li_tv, tv);
5349 li = li->li_next;
5350 }
5351
5352 list_unref(l);
5353 }
5354 break;
5355
Bram Moolenaarb2049902021-01-24 12:53:53 +01005356 case ISN_PROF_START:
5357 case ISN_PROF_END:
5358 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005359#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005360 funccall_T cookie;
5361 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005362 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005363 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005364
Bram Moolenaarca16c602022-09-06 18:57:08 +01005365 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005366 if (iptr->isn_type == ISN_PROF_START)
5367 {
5368 func_line_start(&cookie, iptr->isn_lnum);
5369 // if we get here the instruction is executed
5370 func_line_exec(&cookie);
5371 }
5372 else
5373 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005374#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005375 }
5376 break;
5377
Bram Moolenaare99d4222021-06-13 14:01:26 +02005378 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005379 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005380 break;
5381
Bram Moolenaar389df252020-07-09 21:20:47 +02005382 case ISN_SHUFFLE:
5383 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005384 typval_T tmp_tv;
5385 int item = iptr->isn_arg.shuffle.shfl_item;
5386 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005387
5388 tmp_tv = *STACK_TV_BOT(-item);
5389 for ( ; up > 0 && item > 1; --up)
5390 {
5391 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5392 --item;
5393 }
5394 *STACK_TV_BOT(-item) = tmp_tv;
5395 }
5396 break;
5397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005399 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005401 ectx->ec_where.wt_index = 0;
5402 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403 break;
5404 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005405 continue;
5406
Bram Moolenaard032f342020-07-18 18:13:02 +02005407func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005408 // Restore previous function. If the frame pointer is where we started
5409 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005410 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005411 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005412
Bram Moolenaar4c137212021-04-19 16:48:48 +02005413 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005414 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005415 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005416 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005417
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005418on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005419 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005420 // If "emsg_silent" is set then ignore the error, unless it was set
5421 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005422 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005423 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005424 {
5425 // If a sequence of instructions causes an error while ":silent!"
5426 // was used, restore the stack length and jump ahead to restoring
5427 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005428 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005429 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005430 while (ectx->ec_stack.ga_len
5431 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005432 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005433 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005434 clear_tv(STACK_TV_BOT(0));
5435 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005436 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5437 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005438 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005439 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005440 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005441on_fatal_error:
5442 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005443 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005444 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005445 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005446 }
5447
5448done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005449 ret = OK;
5450theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005451 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005452
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005453 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005454 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5455 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005456}
5457
5458/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005459 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005460 * "rettv".
5461 * Return OK or FAIL.
5462 */
5463 int
5464exe_typval_instr(typval_T *tv, typval_T *rettv)
5465{
5466 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5467 isn_T *save_instr = ectx->ec_instr;
5468 int save_iidx = ectx->ec_iidx;
5469 int res;
5470
LemonBoyf3b48952022-05-05 13:53:03 +01005471 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5472 // even when the compilation fails.
5473 rettv->v_type = VAR_UNKNOWN;
5474
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005475 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5476 res = exec_instructions(ectx);
5477 if (res == OK)
5478 {
5479 *rettv = *STACK_TV_BOT(-1);
5480 --ectx->ec_stack.ga_len;
5481 }
5482
5483 ectx->ec_instr = save_instr;
5484 ectx->ec_iidx = save_iidx;
5485
5486 return res;
5487}
5488
5489/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005490 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5491 * "substitute_instr".
5492 */
5493 char_u *
5494exe_substitute_instr(void)
5495{
5496 ectx_T *ectx = substitute_instr->subs_ectx;
5497 isn_T *save_instr = ectx->ec_instr;
5498 int save_iidx = ectx->ec_iidx;
5499 char_u *res;
5500
5501 ectx->ec_instr = substitute_instr->subs_instr;
5502 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005503 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005504 typval_T *tv = STACK_TV_BOT(-1);
5505
Bram Moolenaar27523602021-06-05 21:36:19 +02005506 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005507 --ectx->ec_stack.ga_len;
5508 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005509 }
5510 else
5511 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005512 substitute_instr->subs_status = FAIL;
5513 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515
Bram Moolenaar4c137212021-04-19 16:48:48 +02005516 ectx->ec_instr = save_instr;
5517 ectx->ec_iidx = save_iidx;
5518
5519 return res;
5520}
5521
5522/*
5523 * Call a "def" function from old Vim script.
5524 * Return OK or FAIL.
5525 */
5526 int
5527call_def_function(
5528 ufunc_T *ufunc,
5529 int argc_arg, // nr of arguments
5530 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005531 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005532 partial_T *partial, // optional partial for context
Bram Moolenaar58779852022-09-06 18:31:14 +01005533 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005534 typval_T *rettv) // return value
5535{
5536 ectx_T ectx; // execution context
5537 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005538 int partial_argc = partial == NULL
5539 || (flags & DEF_USE_PT_ARGV) == 0
5540 ? 0 : partial->pt_argc;
5541 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005542 typval_T *tv;
5543 int idx;
5544 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005545 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005546 sctx_T save_current_sctx = current_sctx;
5547 int did_emsg_before = did_emsg_cumul + did_emsg;
5548 int save_suppress_errthrow = suppress_errthrow;
5549 msglist_T **saved_msg_list = NULL;
5550 msglist_T *private_msg_list = NULL;
5551 int save_emsg_silent_def = emsg_silent_def;
5552 int save_did_emsg_def = did_emsg_def;
5553 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005554 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005555
5556// Get pointer to item in the stack.
5557#undef STACK_TV
5558#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5559
5560// Get pointer to item at the bottom of the stack, -1 is the bottom.
5561#undef STACK_TV_BOT
5562#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5563
5564// Get pointer to a local variable on the stack. Negative for arguments.
5565#undef STACK_TV_VAR
5566#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5567
5568 if (ufunc->uf_def_status == UF_NOT_COMPILED
5569 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005570 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5571 && compile_def_function(ufunc, FALSE,
5572 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005573 {
5574 if (did_emsg_cumul + did_emsg == did_emsg_before)
5575 semsg(_(e_function_is_not_compiled_str),
5576 printable_func_name(ufunc));
5577 return FAIL;
5578 }
5579
5580 {
5581 // Check the function was really compiled.
5582 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5583 + ufunc->uf_dfunc_idx;
5584 if (INSTRUCTIONS(dfunc) == NULL)
5585 {
5586 iemsg("using call_def_function() on not compiled function");
5587 return FAIL;
5588 }
5589 }
5590
5591 // If depth of calling is getting too high, don't execute the function.
5592 orig_funcdepth = funcdepth_get();
5593 if (funcdepth_increment() == FAIL)
5594 return FAIL;
5595
5596 CLEAR_FIELD(ectx);
5597 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5598 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005599 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005600 {
5601 funcdepth_decrement();
5602 return FAIL;
5603 }
5604 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5605 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5606 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005607 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005608
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005609 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005610 if (idx > 0 && ufunc->uf_va_name == NULL)
5611 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005612 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005613 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005614 goto failed_early;
5615 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005616 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005617 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005618 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005619 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005620 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005621 goto failed_early;
5622 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005623
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005624 // Put values from the partial and arguments on the stack, but no more than
5625 // what the function expects. A lambda can be called with more arguments
5626 // than it uses.
5627 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005628 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5629 ++idx)
5630 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005631 int argv_idx = idx - partial_argc;
5632
5633 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005634 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005635 && tv->v_type == VAR_SPECIAL
5636 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005637 {
5638 // Use the default value.
5639 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5640 }
5641 else
5642 {
5643 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5644 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005645 ufunc->uf_arg_types[idx], tv,
5646 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005647 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005648 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005649 }
5650 ++ectx.ec_stack.ga_len;
5651 }
5652
5653 // Turn varargs into a list. Empty list if no args.
5654 if (ufunc->uf_va_name != NULL)
5655 {
5656 int vararg_count = argc - ufunc->uf_args.ga_len;
5657
5658 if (vararg_count < 0)
5659 vararg_count = 0;
5660 else
5661 argc -= vararg_count;
5662 if (exe_newlist(vararg_count, &ectx) == FAIL)
5663 goto failed_early;
5664
5665 // Check the type of the list items.
5666 tv = STACK_TV_BOT(-1);
5667 if (ufunc->uf_va_type != NULL
5668 && ufunc->uf_va_type != &t_list_any
5669 && ufunc->uf_va_type->tt_member != &t_any
5670 && tv->vval.v_list != NULL)
5671 {
5672 type_T *expected = ufunc->uf_va_type->tt_member;
5673 listitem_T *li = tv->vval.v_list->lv_first;
5674
5675 for (idx = 0; idx < vararg_count; ++idx)
5676 {
5677 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005678 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005679 goto failed_early;
5680 li = li->li_next;
5681 }
5682 }
5683
5684 if (defcount > 0)
5685 // Move varargs list to below missing default arguments.
5686 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5687 --ectx.ec_stack.ga_len;
5688 }
5689
5690 // Make space for omitted arguments, will store default value below.
5691 // Any varargs list goes after them.
5692 if (defcount > 0)
5693 for (idx = 0; idx < defcount; ++idx)
5694 {
5695 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5696 ++ectx.ec_stack.ga_len;
5697 }
5698 if (ufunc->uf_va_name != NULL)
5699 ++ectx.ec_stack.ga_len;
5700
5701 // Frame pointer points to just after arguments.
5702 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5703 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5704
5705 {
5706 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5707 + ufunc->uf_dfunc_idx;
5708 ufunc_T *base_ufunc = dfunc->df_ufunc;
5709
5710 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005711 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005712 if (partial != NULL || base_ufunc->uf_partial != NULL)
5713 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005714 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5715 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005716 goto failed_early;
5717 if (partial != NULL)
5718 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005719 outer_T *outer = get_pt_outer(partial);
5720
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005721 if (outer->out_stack == NULL && outer->out_loop_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005722 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005723 if (current_ectx != NULL)
5724 {
5725 if (current_ectx->ec_outer_ref != NULL
5726 && current_ectx->ec_outer_ref->or_outer != NULL)
5727 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005728 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005729 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005730 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005731 }
5732 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005733 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005734 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005735 ++partial->pt_refcount;
5736 ectx.ec_outer_ref->or_partial = partial;
5737 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005738 }
5739 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005740 {
5741 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5742 ++base_ufunc->uf_partial->pt_refcount;
5743 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5744 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005745 }
5746 }
5747
5748 // dummy frame entries
5749 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5750 {
5751 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5752 ++ectx.ec_stack.ga_len;
5753 }
5754
5755 {
5756 // Reserve space for local variables and any closure reference count.
5757 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5758 + ufunc->uf_dfunc_idx;
5759
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005760 // Initialize variables to zero. That avoids having to generate
5761 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005762 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005763 {
5764 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5765 STACK_TV_VAR(idx)->vval.v_number = 0;
5766 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005767 ectx.ec_stack.ga_len += dfunc->df_varcount;
5768 if (dfunc->df_has_closure)
5769 {
5770 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5771 STACK_TV_VAR(idx)->vval.v_number = 0;
5772 ++ectx.ec_stack.ga_len;
5773 }
5774
5775 ectx.ec_instr = INSTRUCTIONS(dfunc);
5776 }
5777
Bram Moolenaar58779852022-09-06 18:31:14 +01005778 // Store the execution context in funccal, used by invoke_all_defer().
5779 if (funccal != NULL)
5780 funccal->fc_ectx = &ectx;
5781
Bram Moolenaar4c137212021-04-19 16:48:48 +02005782 // Following errors are in the function, not the caller.
5783 // Commands behave like vim9script.
5784 estack_push_ufunc(ufunc, 1);
5785 current_sctx = ufunc->uf_script_ctx;
5786 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5787
5788 // Use a specific location for storing error messages to be converted to an
5789 // exception.
5790 saved_msg_list = msg_list;
5791 msg_list = &private_msg_list;
5792
5793 // Do turn errors into exceptions.
5794 suppress_errthrow = FALSE;
5795
5796 // Do not delete the function while executing it.
5797 ++ufunc->uf_calls;
5798
5799 // When ":silent!" was used before calling then we still abort the
5800 // function. If ":silent!" is used in the function then we don't.
5801 emsg_silent_def = emsg_silent;
5802 did_emsg_def = 0;
5803
5804 ectx.ec_where.wt_index = 0;
5805 ectx.ec_where.wt_variable = FALSE;
5806
5807 // Execute the instructions until done.
5808 ret = exec_instructions(&ectx);
5809 if (ret == OK)
5810 {
5811 // function finished, get result from the stack.
5812 if (ufunc->uf_ret_type == &t_void)
5813 {
5814 rettv->v_type = VAR_VOID;
5815 }
5816 else
5817 {
5818 tv = STACK_TV_BOT(-1);
5819 *rettv = *tv;
5820 tv->v_type = VAR_UNKNOWN;
5821 }
5822 }
5823
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005824 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005825 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005826
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005827 // Deal with any remaining closures, they may be in use somewhere.
5828 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005829 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005830 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005831 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005832 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005833
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005834 estack_pop();
5835 current_sctx = save_current_sctx;
5836
Bram Moolenaar2336c372021-12-06 15:06:54 +00005837 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5838 // Function was unreferenced while being used, free it now.
5839 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005840
Bram Moolenaar352134b2020-10-17 22:04:08 +02005841 if (*msg_list != NULL && saved_msg_list != NULL)
5842 {
5843 msglist_T **plist = saved_msg_list;
5844
5845 // Append entries from the current msg_list (uncaught exceptions) to
5846 // the saved msg_list.
5847 while (*plist != NULL)
5848 plist = &(*plist)->next;
5849
5850 *plist = *msg_list;
5851 }
5852 msg_list = saved_msg_list;
5853
Bram Moolenaar4c137212021-04-19 16:48:48 +02005854 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005855 {
5856 cmdmod.cmod_filter_regmatch.regprog = NULL;
5857 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005858 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005859 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005860 emsg_silent_def = save_emsg_silent_def;
5861 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005862
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005863failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005864 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005865 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5866 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005867 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005870 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005871 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005872 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005873 if (ectx.ec_outer_ref->or_outer_allocated)
5874 vim_free(ectx.ec_outer_ref->or_outer);
5875 partial_unref(ectx.ec_outer_ref->or_partial);
5876 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005877 }
5878
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005879 // Not sure if this is necessary.
5880 suppress_errthrow = save_suppress_errthrow;
5881
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005882 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5883 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005884 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005885 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005886 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005887 return ret;
5888}
5889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890/*
Bram Moolenaar58779852022-09-06 18:31:14 +01005891 * Called when a def function has finished (possibly failed).
5892 * Invoke all the function returns to clean up and invoke deferred functions,
5893 * except the toplevel one.
5894 */
5895 void
5896unwind_def_callstack(ectx_T *ectx)
5897{
5898 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
5899 func_return(ectx);
5900}
5901
5902/*
5903 * Invoke any deffered functions for the top function in "ectx".
5904 */
5905 void
5906may_invoke_defer_funcs(ectx_T *ectx)
5907{
5908 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
5909
5910 if (dfunc->df_defer_var_idx > 0)
5911 invoke_defer_funcs(ectx);
5912}
5913
5914/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005915 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5916 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5917 * "pfx" is prefixed to every line.
5918 */
5919 static void
5920list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5921{
5922 int line_idx = 0;
5923 int prev_current = 0;
5924 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005925 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005926
5927 for (current = 0; current < instr_count; ++current)
5928 {
5929 isn_T *iptr = &instr[current];
5930 char *line;
5931
5932 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005933 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005934 while (line_idx < iptr->isn_lnum
5935 && line_idx < ufunc->uf_lines.ga_len)
5936 {
5937 if (current > prev_current)
5938 {
5939 msg_puts("\n\n");
5940 prev_current = current;
5941 }
5942 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5943 if (line != NULL)
5944 msg(line);
5945 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005946 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5947 {
5948 int first_def_arg = ufunc->uf_args.ga_len
5949 - ufunc->uf_def_args.ga_len;
5950
5951 if (def_arg_idx > 0)
5952 msg_puts("\n\n");
5953 msg_start();
5954 msg_puts(" ");
5955 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5956 first_def_arg + def_arg_idx]);
5957 msg_puts(" = ");
5958 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5959 msg_clr_eos();
5960 msg_end();
5961 }
5962 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005963
5964 switch (iptr->isn_type)
5965 {
5966 case ISN_EXEC:
5967 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5968 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005969 case ISN_EXEC_SPLIT:
5970 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5971 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005972 case ISN_EXECRANGE:
5973 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5974 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005975 case ISN_LEGACY_EVAL:
5976 smsg("%s%4d EVAL legacy %s", pfx, current,
5977 iptr->isn_arg.string);
5978 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005979 case ISN_REDIRSTART:
5980 smsg("%s%4d REDIR", pfx, current);
5981 break;
5982 case ISN_REDIREND:
5983 smsg("%s%4d REDIR END%s", pfx, current,
5984 iptr->isn_arg.number ? " append" : "");
5985 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005986 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005987#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005988 smsg("%s%4d CEXPR pre %s", pfx, current,
5989 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005990#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005991 break;
5992 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005993#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005994 {
5995 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5996
5997 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5998 cexpr_get_auname(cer->cer_cmdidx),
5999 cer->cer_forceit ? "!" : "",
6000 cer->cer_cmdline);
6001 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006002#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006003 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006004 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006005 smsg("%s%4d INSTR", pfx, current);
6006 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6007 msg(" -------------");
6008 break;
6009 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006010 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006011 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6012
6013 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006014 }
6015 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006016 case ISN_SUBSTITUTE:
6017 {
6018 subs_T *subs = &iptr->isn_arg.subs;
6019
6020 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6021 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6022 msg(" -------------");
6023 }
6024 break;
6025 case ISN_EXECCONCAT:
6026 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6027 (varnumber_T)iptr->isn_arg.number);
6028 break;
6029 case ISN_ECHO:
6030 {
6031 echo_T *echo = &iptr->isn_arg.echo;
6032
6033 smsg("%s%4d %s %d", pfx, current,
6034 echo->echo_with_white ? "ECHO" : "ECHON",
6035 echo->echo_count);
6036 }
6037 break;
6038 case ISN_EXECUTE:
6039 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006040 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006041 break;
6042 case ISN_ECHOMSG:
6043 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006044 (varnumber_T)(iptr->isn_arg.number));
6045 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006046 case ISN_ECHOWINDOW:
6047 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
6048 (varnumber_T)(iptr->isn_arg.number));
6049 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006050 case ISN_ECHOCONSOLE:
6051 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6052 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006053 break;
6054 case ISN_ECHOERR:
6055 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006056 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006057 break;
6058 case ISN_LOAD:
6059 {
6060 if (iptr->isn_arg.number < 0)
6061 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6062 (varnumber_T)(iptr->isn_arg.number
6063 + STACK_FRAME_SIZE));
6064 else
6065 smsg("%s%4d LOAD $%lld", pfx, current,
6066 (varnumber_T)(iptr->isn_arg.number));
6067 }
6068 break;
6069 case ISN_LOADOUTER:
6070 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006071 isn_outer_T *outer = &iptr->isn_arg.outer;
6072
6073 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006074 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006075 outer->outer_depth,
6076 outer->outer_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +02006077 + STACK_FRAME_SIZE);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006078 else if (outer->outer_depth == OUTER_LOOP_DEPTH)
6079 smsg("%s%4d LOADOUTER level 1 $%d in loop",
6080 pfx, current, outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006081 else
6082 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006083 outer->outer_depth,
6084 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006085 }
6086 break;
6087 case ISN_LOADV:
6088 smsg("%s%4d LOADV v:%s", pfx, current,
6089 get_vim_var_name(iptr->isn_arg.number));
6090 break;
6091 case ISN_LOADSCRIPT:
6092 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006093 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6094 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6095 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006096
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006097 sv = get_script_svar(sref, -1);
6098 if (sv == NULL)
6099 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6100 pfx, current, si->sn_name);
6101 else
6102 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006103 sv->sv_name,
6104 sref->sref_idx,
6105 si->sn_name);
6106 }
6107 break;
6108 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006109 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006110 {
6111 scriptitem_T *si = SCRIPT_ITEM(
6112 iptr->isn_arg.loadstore.ls_sid);
6113
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006114 smsg("%s%4d %s s:%s from %s", pfx, current,
6115 iptr->isn_type == ISN_LOADS ? "LOADS"
6116 : "LOADEXPORT",
6117 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006118 }
6119 break;
6120 case ISN_LOADAUTO:
6121 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6122 break;
6123 case ISN_LOADG:
6124 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6125 break;
6126 case ISN_LOADB:
6127 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6128 break;
6129 case ISN_LOADW:
6130 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6131 break;
6132 case ISN_LOADT:
6133 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6134 break;
6135 case ISN_LOADGDICT:
6136 smsg("%s%4d LOAD g:", pfx, current);
6137 break;
6138 case ISN_LOADBDICT:
6139 smsg("%s%4d LOAD b:", pfx, current);
6140 break;
6141 case ISN_LOADWDICT:
6142 smsg("%s%4d LOAD w:", pfx, current);
6143 break;
6144 case ISN_LOADTDICT:
6145 smsg("%s%4d LOAD t:", pfx, current);
6146 break;
6147 case ISN_LOADOPT:
6148 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6149 break;
6150 case ISN_LOADENV:
6151 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6152 break;
6153 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006154 smsg("%s%4d LOADREG @%c", pfx, current,
6155 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006156 break;
6157
6158 case ISN_STORE:
6159 if (iptr->isn_arg.number < 0)
6160 smsg("%s%4d STORE arg[%lld]", pfx, current,
6161 iptr->isn_arg.number + STACK_FRAME_SIZE);
6162 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006163 smsg("%s%4d STORE $%lld", pfx, current,
6164 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006165 break;
6166 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006167 {
6168 isn_outer_T *outer = &iptr->isn_arg.outer;
6169
6170 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6171 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6172 pfx, current, outer->outer_idx);
6173 else
6174 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6175 outer->outer_depth, outer->outer_idx);
6176 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006177 break;
6178 case ISN_STOREV:
6179 smsg("%s%4d STOREV v:%s", pfx, current,
6180 get_vim_var_name(iptr->isn_arg.number));
6181 break;
6182 case ISN_STOREAUTO:
6183 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6184 break;
6185 case ISN_STOREG:
6186 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6187 break;
6188 case ISN_STOREB:
6189 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6190 break;
6191 case ISN_STOREW:
6192 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6193 break;
6194 case ISN_STORET:
6195 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6196 break;
6197 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006198 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006199 {
6200 scriptitem_T *si = SCRIPT_ITEM(
6201 iptr->isn_arg.loadstore.ls_sid);
6202
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006203 smsg("%s%4d %s %s in %s", pfx, current,
6204 iptr->isn_type == ISN_STORES
6205 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006206 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6207 }
6208 break;
6209 case ISN_STORESCRIPT:
6210 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006211 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6212 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6213 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006214
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006215 sv = get_script_svar(sref, -1);
6216 if (sv == NULL)
6217 smsg("%s%4d STORESCRIPT [deleted] in %s",
6218 pfx, current, si->sn_name);
6219 else
6220 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006221 sv->sv_name,
6222 sref->sref_idx,
6223 si->sn_name);
6224 }
6225 break;
6226 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006227 case ISN_STOREFUNCOPT:
6228 smsg("%s%4d %s &%s", pfx, current,
6229 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006230 iptr->isn_arg.storeopt.so_name);
6231 break;
6232 case ISN_STOREENV:
6233 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6234 break;
6235 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006236 smsg("%s%4d STOREREG @%c", pfx, current,
6237 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006238 break;
6239 case ISN_STORENR:
6240 smsg("%s%4d STORE %lld in $%d", pfx, current,
6241 iptr->isn_arg.storenr.stnr_val,
6242 iptr->isn_arg.storenr.stnr_idx);
6243 break;
6244
6245 case ISN_STOREINDEX:
6246 smsg("%s%4d STOREINDEX %s", pfx, current,
6247 vartype_name(iptr->isn_arg.vartype));
6248 break;
6249
6250 case ISN_STORERANGE:
6251 smsg("%s%4d STORERANGE", pfx, current);
6252 break;
6253
6254 // constants
6255 case ISN_PUSHNR:
6256 smsg("%s%4d PUSHNR %lld", pfx, current,
6257 (varnumber_T)(iptr->isn_arg.number));
6258 break;
6259 case ISN_PUSHBOOL:
6260 case ISN_PUSHSPEC:
6261 smsg("%s%4d PUSH %s", pfx, current,
6262 get_var_special_name(iptr->isn_arg.number));
6263 break;
6264 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006265 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006266 break;
6267 case ISN_PUSHS:
6268 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6269 break;
6270 case ISN_PUSHBLOB:
6271 {
6272 char_u *r;
6273 char_u numbuf[NUMBUFLEN];
6274 char_u *tofree;
6275
6276 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6277 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6278 vim_free(tofree);
6279 }
6280 break;
6281 case ISN_PUSHFUNC:
6282 {
6283 char *name = (char *)iptr->isn_arg.string;
6284
6285 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6286 name == NULL ? "[none]" : name);
6287 }
6288 break;
6289 case ISN_PUSHCHANNEL:
6290#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006291 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006292#endif
6293 break;
6294 case ISN_PUSHJOB:
6295#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006296 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006297#endif
6298 break;
6299 case ISN_PUSHEXC:
6300 smsg("%s%4d PUSH v:exception", pfx, current);
6301 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006302 case ISN_AUTOLOAD:
6303 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6304 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006305 case ISN_UNLET:
6306 smsg("%s%4d UNLET%s %s", pfx, current,
6307 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6308 iptr->isn_arg.unlet.ul_name);
6309 break;
6310 case ISN_UNLETENV:
6311 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6312 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6313 iptr->isn_arg.unlet.ul_name);
6314 break;
6315 case ISN_UNLETINDEX:
6316 smsg("%s%4d UNLETINDEX", pfx, current);
6317 break;
6318 case ISN_UNLETRANGE:
6319 smsg("%s%4d UNLETRANGE", pfx, current);
6320 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006321 case ISN_LOCKUNLOCK:
6322 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6323 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006324 case ISN_LOCKCONST:
6325 smsg("%s%4d LOCKCONST", pfx, current);
6326 break;
6327 case ISN_NEWLIST:
6328 smsg("%s%4d NEWLIST size %lld", pfx, current,
6329 (varnumber_T)(iptr->isn_arg.number));
6330 break;
6331 case ISN_NEWDICT:
6332 smsg("%s%4d NEWDICT size %lld", pfx, current,
6333 (varnumber_T)(iptr->isn_arg.number));
6334 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006335 case ISN_NEWPARTIAL:
6336 smsg("%s%4d NEWPARTIAL", pfx, current);
6337 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006338
6339 // function call
6340 case ISN_BCALL:
6341 {
6342 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6343
6344 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6345 internal_func_name(cbfunc->cbf_idx),
6346 cbfunc->cbf_argcount);
6347 }
6348 break;
6349 case ISN_DCALL:
6350 {
6351 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6352 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6353 + cdfunc->cdf_idx;
6354
6355 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006356 printable_func_name(df->df_ufunc),
6357 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006358 }
6359 break;
6360 case ISN_UCALL:
6361 {
6362 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6363
6364 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6365 cufunc->cuf_name, cufunc->cuf_argcount);
6366 }
6367 break;
6368 case ISN_PCALL:
6369 {
6370 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6371
6372 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6373 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6374 }
6375 break;
6376 case ISN_PCALL_END:
6377 smsg("%s%4d PCALL end", pfx, current);
6378 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006379 case ISN_DEFER:
6380 smsg("%s%4d DEFER %d args", pfx, current,
6381 (int)iptr->isn_arg.defer.defer_argcount);
6382 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006383 case ISN_RETURN:
6384 smsg("%s%4d RETURN", pfx, current);
6385 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006386 case ISN_RETURN_VOID:
6387 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006388 break;
6389 case ISN_FUNCREF:
6390 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006391 funcref_T *funcref = &iptr->isn_arg.funcref;
6392 funcref_extra_T *extra = funcref->fr_extra;
6393 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006394
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006395 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006396 {
6397 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6398 + funcref->fr_dfunc_idx;
6399 name = df->df_ufunc->uf_name;
6400 }
6401 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006402 name = extra->fre_func_name;
6403 if (extra == NULL || extra->fre_loop_var_count == 0)
6404 smsg("%s%4d FUNCREF %s", pfx, current, name);
6405 else
6406 smsg("%s%4d FUNCREF %s var $%d - $%d", pfx, current,
6407 name,
6408 extra->fre_loop_var_idx,
6409 extra->fre_loop_var_idx
6410 + extra->fre_loop_var_count - 1);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006411 }
6412 break;
6413
6414 case ISN_NEWFUNC:
6415 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006416 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006417
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006418 if (arg->nfa_loop_var_count == 0)
6419 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6420 arg->nfa_lambda, arg->nfa_global);
6421 else
6422 smsg("%s%4d NEWFUNC %s %s var $%d - $%d", pfx, current,
6423 arg->nfa_lambda, arg->nfa_global,
6424 arg->nfa_loop_var_idx,
6425 arg->nfa_loop_var_idx + arg->nfa_loop_var_count - 1);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006426 }
6427 break;
6428
6429 case ISN_DEF:
6430 {
6431 char_u *name = iptr->isn_arg.string;
6432
6433 smsg("%s%4d DEF %s", pfx, current,
6434 name == NULL ? (char_u *)"" : name);
6435 }
6436 break;
6437
6438 case ISN_JUMP:
6439 {
6440 char *when = "?";
6441
6442 switch (iptr->isn_arg.jump.jump_when)
6443 {
6444 case JUMP_ALWAYS:
6445 when = "JUMP";
6446 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006447 case JUMP_NEVER:
6448 iemsg("JUMP_NEVER should not be used");
6449 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006450 case JUMP_AND_KEEP_IF_TRUE:
6451 when = "JUMP_AND_KEEP_IF_TRUE";
6452 break;
6453 case JUMP_IF_FALSE:
6454 when = "JUMP_IF_FALSE";
6455 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006456 case JUMP_WHILE_FALSE:
6457 when = "JUMP_WHILE_FALSE"; // unused
6458 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006459 case JUMP_IF_COND_FALSE:
6460 when = "JUMP_IF_COND_FALSE";
6461 break;
6462 case JUMP_IF_COND_TRUE:
6463 when = "JUMP_IF_COND_TRUE";
6464 break;
6465 }
6466 smsg("%s%4d %s -> %d", pfx, current, when,
6467 iptr->isn_arg.jump.jump_where);
6468 }
6469 break;
6470
6471 case ISN_JUMP_IF_ARG_SET:
6472 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6473 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6474 iptr->isn_arg.jump.jump_where);
6475 break;
6476
6477 case ISN_FOR:
6478 {
6479 forloop_T *forloop = &iptr->isn_arg.forloop;
6480
6481 smsg("%s%4d FOR $%d -> %d", pfx, current,
6482 forloop->for_idx, forloop->for_end);
6483 }
6484 break;
6485
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006486 case ISN_ENDLOOP:
6487 {
6488 endloop_T *endloop = &iptr->isn_arg.endloop;
6489
6490 smsg("%s%4d ENDLOOP $%d save $%d - $%d", pfx, current,
6491 endloop->end_funcref_idx,
6492 endloop->end_var_idx,
6493 endloop->end_var_idx + endloop->end_var_count - 1);
6494 }
6495 break;
6496
6497 case ISN_WHILE:
6498 {
6499 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6500
6501 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6502 whileloop->while_funcref_idx,
6503 whileloop->while_end);
6504 }
6505 break;
6506
Bram Moolenaar4c137212021-04-19 16:48:48 +02006507 case ISN_TRY:
6508 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006509 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006510
6511 if (try->try_ref->try_finally == 0)
6512 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6513 pfx, current,
6514 try->try_ref->try_catch,
6515 try->try_ref->try_endtry);
6516 else
6517 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6518 pfx, current,
6519 try->try_ref->try_catch,
6520 try->try_ref->try_finally,
6521 try->try_ref->try_endtry);
6522 }
6523 break;
6524 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006525 smsg("%s%4d CATCH", pfx, current);
6526 break;
6527 case ISN_TRYCONT:
6528 {
6529 trycont_T *trycont = &iptr->isn_arg.trycont;
6530
6531 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6532 trycont->tct_levels,
6533 trycont->tct_levels == 1 ? "" : "s",
6534 trycont->tct_where);
6535 }
6536 break;
6537 case ISN_FINALLY:
6538 smsg("%s%4d FINALLY", pfx, current);
6539 break;
6540 case ISN_ENDTRY:
6541 smsg("%s%4d ENDTRY", pfx, current);
6542 break;
6543 case ISN_THROW:
6544 smsg("%s%4d THROW", pfx, current);
6545 break;
6546
6547 // expression operations on number
6548 case ISN_OPNR:
6549 case ISN_OPFLOAT:
6550 case ISN_OPANY:
6551 {
6552 char *what;
6553 char *ins;
6554
6555 switch (iptr->isn_arg.op.op_type)
6556 {
6557 case EXPR_MULT: what = "*"; break;
6558 case EXPR_DIV: what = "/"; break;
6559 case EXPR_REM: what = "%"; break;
6560 case EXPR_SUB: what = "-"; break;
6561 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006562 case EXPR_LSHIFT: what = "<<"; break;
6563 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006564 default: what = "???"; break;
6565 }
6566 switch (iptr->isn_type)
6567 {
6568 case ISN_OPNR: ins = "OPNR"; break;
6569 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6570 case ISN_OPANY: ins = "OPANY"; break;
6571 default: ins = "???"; break;
6572 }
6573 smsg("%s%4d %s %s", pfx, current, ins, what);
6574 }
6575 break;
6576
6577 case ISN_COMPAREBOOL:
6578 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006579 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006580 case ISN_COMPARENR:
6581 case ISN_COMPAREFLOAT:
6582 case ISN_COMPARESTRING:
6583 case ISN_COMPAREBLOB:
6584 case ISN_COMPARELIST:
6585 case ISN_COMPAREDICT:
6586 case ISN_COMPAREFUNC:
6587 case ISN_COMPAREANY:
6588 {
6589 char *p;
6590 char buf[10];
6591 char *type;
6592
6593 switch (iptr->isn_arg.op.op_type)
6594 {
6595 case EXPR_EQUAL: p = "=="; break;
6596 case EXPR_NEQUAL: p = "!="; break;
6597 case EXPR_GREATER: p = ">"; break;
6598 case EXPR_GEQUAL: p = ">="; break;
6599 case EXPR_SMALLER: p = "<"; break;
6600 case EXPR_SEQUAL: p = "<="; break;
6601 case EXPR_MATCH: p = "=~"; break;
6602 case EXPR_IS: p = "is"; break;
6603 case EXPR_ISNOT: p = "isnot"; break;
6604 case EXPR_NOMATCH: p = "!~"; break;
6605 default: p = "???"; break;
6606 }
6607 STRCPY(buf, p);
6608 if (iptr->isn_arg.op.op_ic == TRUE)
6609 strcat(buf, "?");
6610 switch(iptr->isn_type)
6611 {
6612 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6613 case ISN_COMPARESPECIAL:
6614 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006615 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006616 case ISN_COMPARENR: type = "COMPARENR"; break;
6617 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6618 case ISN_COMPARESTRING:
6619 type = "COMPARESTRING"; break;
6620 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6621 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6622 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6623 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6624 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6625 default: type = "???"; break;
6626 }
6627
6628 smsg("%s%4d %s %s", pfx, current, type, buf);
6629 }
6630 break;
6631
6632 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6633 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6634
6635 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006636 case ISN_CONCAT:
6637 smsg("%s%4d CONCAT size %lld", pfx, current,
6638 (varnumber_T)(iptr->isn_arg.number));
6639 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006640 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6641 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6642 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6643 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6644 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6645 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6646 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6647 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6648 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6649 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6650 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006651 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006652 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6653 iptr->isn_arg.getitem.gi_index,
6654 iptr->isn_arg.getitem.gi_with_op ?
6655 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006656 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6657 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6658 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006659 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6660 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6661
Bram Moolenaar4c137212021-04-19 16:48:48 +02006662 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6663
Bram Moolenaar4c137212021-04-19 16:48:48 +02006664 case ISN_CHECKTYPE:
6665 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006666 checktype_T *ct = &iptr->isn_arg.type;
6667 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006668
6669 if (ct->ct_arg_idx == 0)
6670 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6671 type_name(ct->ct_type, &tofree),
6672 (int)ct->ct_off);
6673 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006674 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006675 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006676 type_name(ct->ct_type, &tofree),
6677 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006678 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006679 (int)ct->ct_arg_idx);
6680 vim_free(tofree);
6681 break;
6682 }
6683 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6684 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6685 iptr->isn_arg.checklen.cl_min_len);
6686 break;
6687 case ISN_SETTYPE:
6688 {
6689 char *tofree;
6690
6691 smsg("%s%4d SETTYPE %s", pfx, current,
6692 type_name(iptr->isn_arg.type.ct_type, &tofree));
6693 vim_free(tofree);
6694 break;
6695 }
6696 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006697 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6698 smsg("%s%4d INVERT %d (!val)", pfx, current,
6699 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006700 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006701 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6702 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006703 break;
6704 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006705 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006706 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006707 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6708 pfx, current,
6709 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006710 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006711 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6712 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006713 break;
6714 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006715 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006716 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006717 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006718 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6719 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006720 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006721 else
6722 smsg("%s%4d PUT %c %ld", pfx, current,
6723 iptr->isn_arg.put.put_regname,
6724 (long)iptr->isn_arg.put.put_lnum);
6725 break;
6726
Bram Moolenaar4c137212021-04-19 16:48:48 +02006727 case ISN_CMDMOD:
6728 {
6729 char_u *buf;
6730 size_t len = produce_cmdmods(
6731 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6732
6733 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006734 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006735 {
6736 (void)produce_cmdmods(
6737 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6738 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6739 vim_free(buf);
6740 }
6741 break;
6742 }
6743 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6744
6745 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006746 smsg("%s%4d PROFILE START line %d", pfx, current,
6747 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006748 break;
6749
6750 case ISN_PROF_END:
6751 smsg("%s%4d PROFILE END", pfx, current);
6752 break;
6753
Bram Moolenaare99d4222021-06-13 14:01:26 +02006754 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006755 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6756 iptr->isn_arg.debug.dbg_break_lnum + 1,
6757 iptr->isn_lnum,
6758 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006759 break;
6760
Bram Moolenaar4c137212021-04-19 16:48:48 +02006761 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6762 iptr->isn_arg.unpack.unp_count,
6763 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6764 break;
6765 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6766 iptr->isn_arg.shuffle.shfl_item,
6767 iptr->isn_arg.shuffle.shfl_up);
6768 break;
6769 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6770
6771 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6772 return;
6773 }
6774
6775 out_flush(); // output one line at a time
6776 ui_breakcheck();
6777 if (got_int)
6778 break;
6779 }
6780}
6781
6782/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006783 * Handle command line completion for the :disassemble command.
6784 */
6785 void
6786set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6787{
6788 char_u *p;
6789
6790 // Default: expand user functions, "debug" and "profile"
6791 xp->xp_context = EXPAND_DISASSEMBLE;
6792 xp->xp_pattern = arg;
6793
6794 // first argument already typed: only user function names
6795 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6796 {
6797 xp->xp_context = EXPAND_USER_FUNC;
6798 xp->xp_pattern = skipwhite(p);
6799 }
6800}
6801
6802/*
6803 * Function given to ExpandGeneric() to obtain the list of :disassemble
6804 * arguments.
6805 */
6806 char_u *
6807get_disassemble_argument(expand_T *xp, int idx)
6808{
6809 if (idx == 0)
6810 return (char_u *)"debug";
6811 if (idx == 1)
6812 return (char_u *)"profile";
6813 return get_user_func_name(xp, idx - 2);
6814}
6815
6816/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006817 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006818 * We don't really need this at runtime, but we do have tests that require it,
6819 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820 */
6821 void
6822ex_disassemble(exarg_T *eap)
6823{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006824 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006825 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006827 isn_T *instr = NULL; // init to shut up gcc warning
6828 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006829 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006830
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006831 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006832 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006833 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006834 if (func_needs_compiling(ufunc, compile_type)
6835 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006836 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006837 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006838 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006839 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 return;
6841 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006842 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843
6844 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006845 switch (compile_type)
6846 {
6847 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006848#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006849 instr = dfunc->df_instr_prof;
6850 instr_count = dfunc->df_instr_prof_count;
6851 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006852#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006853 // FALLTHROUGH
6854 case CT_NONE:
6855 instr = dfunc->df_instr;
6856 instr_count = dfunc->df_instr_count;
6857 break;
6858 case CT_DEBUG:
6859 instr = dfunc->df_instr_debug;
6860 instr_count = dfunc->df_instr_debug_count;
6861 break;
6862 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006863
Bram Moolenaar4c137212021-04-19 16:48:48 +02006864 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006865}
6866
6867/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006868 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869 * list, etc. Mostly like what JavaScript does, except that empty list and
6870 * empty dictionary are FALSE.
6871 */
6872 int
6873tv2bool(typval_T *tv)
6874{
6875 switch (tv->v_type)
6876 {
6877 case VAR_NUMBER:
6878 return tv->vval.v_number != 0;
6879 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 case VAR_PARTIAL:
6882 return tv->vval.v_partial != NULL;
6883 case VAR_FUNC:
6884 case VAR_STRING:
6885 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6886 case VAR_LIST:
6887 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6888 case VAR_DICT:
6889 return tv->vval.v_dict != NULL
6890 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6891 case VAR_BOOL:
6892 case VAR_SPECIAL:
6893 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6894 case VAR_JOB:
6895#ifdef FEAT_JOB_CHANNEL
6896 return tv->vval.v_job != NULL;
6897#else
6898 break;
6899#endif
6900 case VAR_CHANNEL:
6901#ifdef FEAT_JOB_CHANNEL
6902 return tv->vval.v_channel != NULL;
6903#else
6904 break;
6905#endif
6906 case VAR_BLOB:
6907 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6908 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006909 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006910 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006911 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912 break;
6913 }
6914 return FALSE;
6915}
6916
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006917 void
6918emsg_using_string_as(typval_T *tv, int as_number)
6919{
6920 semsg(_(as_number ? e_using_string_as_number_str
6921 : e_using_string_as_bool_str),
6922 tv->vval.v_string == NULL
6923 ? (char_u *)"" : tv->vval.v_string);
6924}
6925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006926/*
6927 * If "tv" is a string give an error and return FAIL.
6928 */
6929 int
6930check_not_string(typval_T *tv)
6931{
6932 if (tv->v_type == VAR_STRING)
6933 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006934 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 clear_tv(tv);
6936 return FAIL;
6937 }
6938 return OK;
6939}
6940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941
6942#endif // FEAT_EVAL