blob: 1699caf49640418fe2d9784b9585be578808343b [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 Moolenaar5800c792022-09-22 17:34:01 +0100383 * If "pt_arg" is not NULL use "pt_arg->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,
Bram Moolenaar5800c792022-09-22 17:34:01 +0100397 partial_T *pt_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 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
Bram Moolenaar5800c792022-09-22 17:34:01 +0100546 // Initialize all local variables to number zero. Also initialize the
547 // variable that counts how many closures were created. This is used in
548 // handle_closure_in_use().
549 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
550 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000551 {
552 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
553
554 tv->v_type = VAR_NUMBER;
555 tv->vval.v_number = 0;
556 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200557 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558
Bram Moolenaar5800c792022-09-22 17:34:01 +0100559 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
560 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100561 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200562 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100563
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200564 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100565 return FAIL;
566 if (pt != NULL)
567 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000568 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200569 ++pt->pt_refcount;
570 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100571 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100572 else
573 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200574 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200575 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200576 {
577 vim_free(ref);
578 return FAIL;
579 }
580 ref->or_outer_allocated = TRUE;
581 ref->or_outer->out_stack = &ectx->ec_stack;
582 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
583 if (ectx->ec_outer_ref != NULL)
584 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100585 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200586 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100587 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100588 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200589 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100590
Bram Moolenaarc970e422021-03-17 15:03:04 +0100591 ++ufunc->uf_calls;
592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 // Set execution state to the start of the called function.
594 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100595 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100596 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200597 if (entry != NULL)
598 {
599 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200600 // Save the current context so it can be restored on return.
601 entry->es_save_sctx = current_sctx;
602 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200603 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100604
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200605 // Start execution at the first instruction.
606 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607
608 return OK;
609}
610
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000611// Double linked list of funcstack_T in use.
612static funcstack_T *first_funcstack = NULL;
613
614 static void
615add_funcstack_to_list(funcstack_T *funcstack)
616{
617 // Link in list of funcstacks.
618 if (first_funcstack != NULL)
619 first_funcstack->fs_prev = funcstack;
620 funcstack->fs_next = first_funcstack;
621 funcstack->fs_prev = NULL;
622 first_funcstack = funcstack;
623}
624
625 static void
626remove_funcstack_from_list(funcstack_T *funcstack)
627{
628 if (funcstack->fs_prev == NULL)
629 first_funcstack = funcstack->fs_next;
630 else
631 funcstack->fs_prev->fs_next = funcstack->fs_next;
632 if (funcstack->fs_next != NULL)
633 funcstack->fs_next->fs_prev = funcstack->fs_prev;
634}
635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200637 * Used when returning from a function: Check if any closure is still
638 * referenced. If so then move the arguments and variables to a separate piece
639 * of stack to be used when the closure is called.
640 * When "free_arguments" is TRUE the arguments are to be freed.
641 * Returns FAIL when out of memory.
642 */
643 static int
644handle_closure_in_use(ectx_T *ectx, int free_arguments)
645{
646 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
647 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200648 int argcount;
649 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200650 int idx;
651 typval_T *tv;
652 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200653 garray_T *gap = &ectx->ec_funcrefs;
654 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200655
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200656 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200657 return OK; // function was freed
658 if (dfunc->df_has_closure == 0)
659 return OK; // no closures
660 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
661 closure_count = tv->vval.v_number;
662 if (closure_count == 0)
663 return OK; // no funcrefs created
664
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100665 // Compute "top": the first entry in the stack used by the function.
666 // This is the first argument (after that comes the stack frame and then
667 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200668 argcount = ufunc_argcount(dfunc->df_ufunc);
669 top = ectx->ec_frame_idx - argcount;
670
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200671 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200672 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200673 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200674 partial_T *pt;
675 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200676
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200677 if (off < 0)
678 continue; // count is off or already done
679 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200680 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200681 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200682 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200683 int i;
684
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000685 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200686 // unreferenced on return.
687 for (i = 0; i < dfunc->df_varcount; ++i)
688 {
689 typval_T *stv = STACK_TV(ectx->ec_frame_idx
690 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200691 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200692 --refcount;
693 }
694 if (refcount > 1)
695 {
696 closure_in_use = TRUE;
697 break;
698 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200699 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200700 }
701
702 if (closure_in_use)
703 {
704 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
705 typval_T *stack;
706
707 // A closure is using the arguments and/or local variables.
708 // Move them to the called function.
709 if (funcstack == NULL)
710 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000711
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200712 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
713 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200714 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
715 funcstack->fs_ga.ga_data = stack;
716 if (stack == NULL)
717 {
718 vim_free(funcstack);
719 return FAIL;
720 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000721 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200722
723 // Move or copy the arguments.
724 for (idx = 0; idx < argcount; ++idx)
725 {
726 tv = STACK_TV(top + idx);
727 if (free_arguments)
728 {
729 *(stack + idx) = *tv;
730 tv->v_type = VAR_UNKNOWN;
731 }
732 else
733 copy_tv(tv, stack + idx);
734 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100735 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200736 // Move the local variables.
737 for (idx = 0; idx < dfunc->df_varcount; ++idx)
738 {
739 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200740
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200741 // A partial created for a local function, that is also used as a
742 // local variable, has a reference count for the variable, thus
743 // will never go down to zero. When all these refcounts are one
744 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000745 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200746 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
747 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200748 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200749
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200750 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200751 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
752 gap->ga_len - closure_count + i])
753 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200754 }
755
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200756 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200757 tv->v_type = VAR_UNKNOWN;
758 }
759
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200760 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200761 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200762 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
763 - closure_count + idx];
764 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200765 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200766 ++funcstack->fs_refcount;
767 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100768 pt->pt_outer.out_stack = &funcstack->fs_ga;
769 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200770 }
771 }
772 }
773
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200774 for (idx = 0; idx < closure_count; ++idx)
775 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
776 - closure_count + idx]);
777 gap->ga_len -= closure_count;
778 if (gap->ga_len == 0)
779 ga_clear(gap);
780
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200781 return OK;
782}
783
784/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200785 * Called when a partial is freed or its reference count goes down to one. The
786 * funcstack may be the only reference to the partials in the local variables.
787 * Go over all of them, the funcref and can be freed if all partials
788 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100789 * Returns TRUE if the funcstack is freed, the partial referencing it will then
790 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200791 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100792 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200793funcstack_check_refcount(funcstack_T *funcstack)
794{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100795 int i;
796 garray_T *gap = &funcstack->fs_ga;
797 int done = 0;
798 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200799
800 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100801 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200802 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
803 {
804 typval_T *tv = ((typval_T *)gap->ga_data) + i;
805
806 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
807 && tv->vval.v_partial->pt_funcstack == funcstack
808 && tv->vval.v_partial->pt_refcount == 1)
809 ++done;
810 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100811 if (done != funcstack->fs_min_refcount)
812 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200813
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100814 stack = gap->ga_data;
815
816 // All partials referencing the funcstack have a reference count of
817 // one, thus the funcstack is no longer of use.
818 for (i = 0; i < gap->ga_len; ++i)
819 clear_tv(stack + i);
820 vim_free(stack);
821 remove_funcstack_from_list(funcstack);
822 vim_free(funcstack);
823
824 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200825}
826
827/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000828 * For garbage collecting: set references in all variables referenced by
829 * all funcstacks.
830 */
831 int
832set_ref_in_funcstacks(int copyID)
833{
834 funcstack_T *funcstack;
835
836 for (funcstack = first_funcstack; funcstack != NULL;
837 funcstack = funcstack->fs_next)
838 {
839 typval_T *stack = funcstack->fs_ga.ga_data;
840 int i;
841
842 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
843 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
844 return TRUE; // abort
845 }
846 return FALSE;
847}
848
Bram Moolenaar806a2732022-09-04 15:40:36 +0100849// Ugly static to avoid passing the execution context around through many
850// layers.
851static ectx_T *current_ectx = NULL;
852
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000853/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100854 * Return TRUE if currently executing a :def function.
855 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100856 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100857 int
858in_def_function(void)
859{
860 return current_ectx != NULL;
861}
862
863/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100864 * Clear "current_ectx" and return the previous value. To be used when calling
865 * a user function.
866 */
867 ectx_T *
868clear_currrent_ectx(void)
869{
870 ectx_T *r = current_ectx;
871
872 current_ectx = NULL;
873 return r;
874}
875
876 void
877restore_current_ectx(ectx_T *ectx)
878{
879 if (current_ectx != NULL)
880 iemsg("Restoring current_ectx while it is not NULL");
881 current_ectx = ectx;
882}
883
884/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100885 * Add an entry for a deferred function call to the currently executing
886 * function.
887 * Return the list or NULL when failed.
888 */
889 static list_T *
890add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100891{
892 typval_T *defer_tv = STACK_TV_VAR(var_idx);
893 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100894 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100895 typval_T listval;
896
897 if (defer_tv->v_type != VAR_LIST)
898 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100899 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100900 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100901 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100902 }
903 defer_l = defer_tv->vval.v_list;
904
905 l = list_alloc_with_items(argcount + 1);
906 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100907 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100908 listval.v_type = VAR_LIST;
909 listval.vval.v_list = l;
910 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100911 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100912 {
913 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100914 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100915 }
916
Bram Moolenaar806a2732022-09-04 15:40:36 +0100917 return l;
918}
919
920/*
921 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
922 * The local variable that lists deferred functions is "var_idx".
923 * Returns OK or FAIL.
924 */
925 static int
926defer_command(int var_idx, int argcount, ectx_T *ectx)
927{
928 list_T *l = add_defer_item(var_idx, argcount, ectx);
929 int i;
930 typval_T *func_tv;
931
932 if (l == NULL)
933 return FAIL;
934
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100935 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100936 if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL)
937 {
938 semsg(_(e_expected_str_but_got_str),
939 "function or partial",
940 vartype_name(func_tv->v_type));
941 return FAIL;
942 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100943 list_set_item(l, 0, func_tv);
944
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100945 for (i = 0; i < argcount; ++i)
946 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100947 ectx->ec_stack.ga_len -= argcount + 1;
948 return OK;
949}
950
951/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100952 * Add a deferred function "name" with one argument "arg_tv".
953 * Consumes "name", also on failure.
954 * Only to be called when in_def_function() returns TRUE.
955 */
956 int
957add_defer_function(char_u *name, int argcount, typval_T *argvars)
958{
959 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
960 + current_ectx->ec_dfunc_idx;
961 list_T *l;
962 typval_T func_tv;
963 int i;
964
965 if (dfunc->df_defer_var_idx == 0)
966 {
967 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100968 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100969 return FAIL;
970 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100971
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100972 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100973 if (l == NULL)
974 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100975 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100976 return FAIL;
977 }
978
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100979 func_tv.v_type = VAR_FUNC;
980 func_tv.v_lock = 0;
981 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100982 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100983
Bram Moolenaar806a2732022-09-04 15:40:36 +0100984 for (i = 0; i < argcount; ++i)
985 list_set_item(l, i + 1, argvars + i);
986 return OK;
987}
988
989/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100990 * Invoked when returning from a function: Invoke any deferred calls.
991 */
992 static void
993invoke_defer_funcs(ectx_T *ectx)
994{
995 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
996 + ectx->ec_dfunc_idx;
997 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
998 listitem_T *li;
999
1000 if (defer_tv->v_type != VAR_LIST)
1001 return; // no function added
1002 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1003 {
1004 list_T *l = li->li_tv.vval.v_list;
1005 typval_T rettv;
1006 typval_T argvars[MAX_FUNC_ARGS];
1007 int i;
1008 listitem_T *arg_li = l->lv_first;
1009 funcexe_T funcexe;
1010
1011 for (i = 0; i < l->lv_len - 1; ++i)
1012 {
1013 arg_li = arg_li->li_next;
1014 argvars[i] = arg_li->li_tv;
1015 }
1016
1017 CLEAR_FIELD(funcexe);
1018 funcexe.fe_evaluate = TRUE;
1019 rettv.v_type = VAR_UNKNOWN;
1020 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1021 &rettv, l->lv_len - 1, argvars, &funcexe);
1022 clear_tv(&rettv);
1023 }
1024}
1025
1026/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 * Return from the current function.
1028 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001029 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001030func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001033 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001034 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1035 + ectx->ec_dfunc_idx;
1036 int argcount = ufunc_argcount(dfunc->df_ufunc);
1037 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001038 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001039 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1040 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001041 funclocal_T *floc;
1042#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001043 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1044 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045
Bram Moolenaar12d26532021-02-19 19:13:21 +01001046 if (do_profiling == PROF_YES)
1047 {
1048 ufunc_T *caller = prev_dfunc->df_ufunc;
1049
1050 if (dfunc->df_ufunc->uf_profiling
1051 || (caller != NULL && caller->uf_profiling))
1052 {
1053 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1054 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1055 --profile_info_ga.ga_len;
1056 }
1057 }
1058#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001059
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001060 if (dfunc->df_defer_var_idx > 0)
1061 invoke_defer_funcs(ectx);
1062
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001063 // No check for uf_refcount being zero, cannot think of a way that would
1064 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001065 --dfunc->df_ufunc->uf_calls;
1066
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001068 entry = estack_pop();
1069 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001070 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001072 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1073 return FAIL;
1074
1075 // Clear the arguments.
1076 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1077 clear_tv(STACK_TV(idx));
1078
1079 // Clear local variables and temp values, but not the return value.
1080 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001081 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001083
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001084 // The return value should be on top of the stack. However, when aborting
1085 // it may not be there and ec_frame_idx is the top of the stack.
1086 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001087 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001088 ret_idx = 0;
1089
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001090 if (ectx->ec_outer_ref != NULL)
1091 {
1092 if (ectx->ec_outer_ref->or_outer_allocated)
1093 vim_free(ectx->ec_outer_ref->or_outer);
1094 partial_unref(ectx->ec_outer_ref->or_partial);
1095 vim_free(ectx->ec_outer_ref);
1096 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001097
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001098 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001099 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001100 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1101 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001102 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1103 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001104 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001105 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001106 floc = (void *)STACK_TV(ectx->ec_frame_idx
1107 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001108 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001109 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1110 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001111
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001112 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001113 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001114 else
1115 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001116 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001117 vim_free(floc);
1118 }
1119
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001120 if (ret_idx > 0)
1121 {
1122 // Reset the stack to the position before the call, with a spot for the
1123 // return value, moved there from above the frame.
1124 ectx->ec_stack.ga_len = top + 1;
1125 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1126 }
1127 else
1128 // Reset the stack to the position before the call.
1129 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001130
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001131 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001132 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001133 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134}
1135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136/*
1137 * Prepare arguments and rettv for calling a builtin or user function.
1138 */
1139 static int
1140call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1141{
1142 int idx;
1143 typval_T *tv;
1144
1145 // Move arguments from bottom of the stack to argvars[] and add terminator.
1146 for (idx = 0; idx < argcount; ++idx)
1147 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1148 argvars[argcount].v_type = VAR_UNKNOWN;
1149
1150 // Result replaces the arguments on the stack.
1151 if (argcount > 0)
1152 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001153 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 return FAIL;
1155 else
1156 ++ectx->ec_stack.ga_len;
1157
1158 // Default return value is zero.
1159 tv = STACK_TV_BOT(-1);
1160 tv->v_type = VAR_NUMBER;
1161 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001162 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163
1164 return OK;
1165}
1166
1167/*
1168 * Call a builtin function by index.
1169 */
1170 static int
1171call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1172{
1173 typval_T argvars[MAX_FUNC_ARGS];
1174 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001175 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001176 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001177 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178
1179 if (call_prepare(argcount, argvars, ectx) == FAIL)
1180 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001181 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001183 // Call the builtin function. Set "current_ectx" so that when it
1184 // recursively invokes call_def_function() a closure context can be set.
1185 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001187 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001188 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189
1190 // Clear the arguments.
1191 for (idx = 0; idx < argcount; ++idx)
1192 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001193
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001194 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001195 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 return OK;
1197}
1198
1199/*
1200 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001201 * If the function is compiled this will add a stack frame and set the
1202 * instruction pointer at the start of the function.
1203 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001204 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001205 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 */
1207 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001208call_ufunc(
1209 ufunc_T *ufunc,
1210 partial_T *pt,
1211 int argcount,
1212 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001213 isn_T *iptr,
1214 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215{
1216 typval_T argvars[MAX_FUNC_ARGS];
1217 funcexe_T funcexe;
1218 int error;
1219 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001220 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001221 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222
Bram Moolenaare99d4222021-06-13 14:01:26 +02001223 if (func_needs_compiling(ufunc, compile_type)
1224 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1225 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001226 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001227 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001228 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001229 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001230 if (error != FCERR_UNKNOWN)
1231 {
1232 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001233 semsg(_(e_too_many_arguments_for_function_str),
1234 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001235 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001236 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001237 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001238 return FAIL;
1239 }
1240
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001241 // The function has been compiled, can call it quickly. For a function
1242 // that was defined later: we can call it directly next time.
1243 if (iptr != NULL)
1244 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001245 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001246 iptr->isn_type = ISN_DCALL;
1247 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1248 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1249 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001250 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252
1253 if (call_prepare(argcount, argvars, ectx) == FAIL)
1254 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001255 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001256 funcexe.fe_evaluate = TRUE;
1257 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258
1259 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001261 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262
1263 // Clear the arguments.
1264 for (idx = 0; idx < argcount; ++idx)
1265 clear_tv(&argvars[idx]);
1266
1267 if (error != FCERR_NONE)
1268 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001269 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 return FAIL;
1271 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001272 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001273 // Error other than from calling the function itself.
1274 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 return OK;
1276}
1277
1278/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001279 * If command modifiers were applied restore them.
1280 */
1281 static void
1282may_restore_cmdmod(funclocal_T *funclocal)
1283{
1284 if (funclocal->floc_restore_cmdmod)
1285 {
1286 cmdmod.cmod_filter_regmatch.regprog = NULL;
1287 undo_cmdmod(&cmdmod);
1288 cmdmod = funclocal->floc_save_cmdmod;
1289 funclocal->floc_restore_cmdmod = FALSE;
1290 }
1291}
1292
1293/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001294 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1295 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001296 */
1297 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001298vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001299{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001300 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001301}
1302
1303/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304 * Execute a function by "name".
1305 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001306 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 * Returns FAIL if not found without an error message.
1308 */
1309 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001310call_by_name(
1311 char_u *name,
1312 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001313 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001314 isn_T *iptr,
1315 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316{
1317 ufunc_T *ufunc;
1318
1319 if (builtin_function(name, -1))
1320 {
1321 int func_idx = find_internal_func(name);
1322
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001323 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001325 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326 return FAIL;
1327 return call_bfunc(func_idx, argcount, ectx);
1328 }
1329
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001330 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001331
1332 if (ufunc == NULL)
1333 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001334 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001335
1336 if (script_autoload(name, TRUE))
1337 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001338 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001339
1340 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001341 return FAIL; // bail out if loading the script caused an error
1342 }
1343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001345 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001346 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001347 {
1348 int i;
1349 typval_T *argv = STACK_TV_BOT(0) - argcount;
1350
1351 // The function can change at runtime, check that the argument
1352 // types are correct.
1353 for (i = 0; i < argcount; ++i)
1354 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001355 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001356
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001357 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001358 type = ufunc->uf_arg_types[i];
1359 else if (ufunc->uf_va_type != NULL)
1360 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001361 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001362 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001363 return FAIL;
1364 }
1365 }
1366
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001367 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369
1370 return FAIL;
1371}
1372
1373 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001374call_partial(
1375 typval_T *tv,
1376 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001377 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001379 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001380 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001382 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001383 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384
1385 if (tv->v_type == VAR_PARTIAL)
1386 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001387 partial_T *pt = tv->vval.v_partial;
1388 int i;
1389
1390 if (pt->pt_argc > 0)
1391 {
1392 // Make space for arguments from the partial, shift the "argcount"
1393 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001394 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001395 return FAIL;
1396 for (i = 1; i <= argcount; ++i)
1397 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1398 ectx->ec_stack.ga_len += pt->pt_argc;
1399 argcount += pt->pt_argc;
1400
1401 // copy the arguments from the partial onto the stack
1402 for (i = 0; i < pt->pt_argc; ++i)
1403 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1404 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001405 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406
1407 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001408 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 name = pt->pt_name;
1411 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001412 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001414 if (name != NULL)
1415 {
1416 char_u fname_buf[FLEN_FIXED + 1];
1417 char_u *tofree = NULL;
1418 int error = FCERR_NONE;
1419 char_u *fname;
1420
1421 // May need to translate <SNR>123_ to K_SNR.
1422 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1423 if (error != FCERR_NONE)
1424 res = FAIL;
1425 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001426 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001427 vim_free(tofree);
1428 }
1429
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001430 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 {
1432 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001433 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001434 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 return FAIL;
1436 }
1437 return OK;
1438}
1439
1440/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001441 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1442 * TRUE.
1443 */
1444 static int
1445error_if_locked(int lock, char *error)
1446{
1447 if (lock & (VAR_LOCKED | VAR_FIXED))
1448 {
1449 emsg(_(error));
1450 return TRUE;
1451 }
1452 return FALSE;
1453}
1454
1455/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001456 * Give an error if "tv" is not a number and return FAIL.
1457 */
1458 static int
1459check_for_number(typval_T *tv)
1460{
1461 if (tv->v_type != VAR_NUMBER)
1462 {
1463 semsg(_(e_expected_str_but_got_str),
1464 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1465 return FAIL;
1466 }
1467 return OK;
1468}
1469
1470/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001471 * Store "tv" in variable "name".
1472 * This is for s: and g: variables.
1473 */
1474 static void
1475store_var(char_u *name, typval_T *tv)
1476{
1477 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001478 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001479
Bram Moolenaard877a572021-04-01 19:42:48 +02001480 if (tv->v_lock)
1481 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001482 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001483 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001484 restore_funccal();
1485}
1486
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001487/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001488 * Convert "tv" to a string.
1489 * Return FAIL if not allowed.
1490 */
1491 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001492do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001493{
1494 if (tv->v_type != VAR_STRING)
1495 {
1496 char_u *str;
1497
1498 if (is_2string_any)
1499 {
1500 switch (tv->v_type)
1501 {
1502 case VAR_SPECIAL:
1503 case VAR_BOOL:
1504 case VAR_NUMBER:
1505 case VAR_FLOAT:
1506 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001507
1508 case VAR_LIST:
1509 if (tolerant)
1510 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001511 char_u *s, *e, *p;
1512 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001513
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001514 ga_init2(&ga, sizeof(char_u *), 1);
1515
1516 // Convert to NL separated items, then
1517 // escape the items and replace the NL with
1518 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001519 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001520 if (str == NULL)
1521 return FAIL;
1522 s = str;
1523 while ((e = vim_strchr(s, '\n')) != NULL)
1524 {
1525 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001526 p = vim_strsave_fnameescape(s,
1527 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001528 if (p != NULL)
1529 {
1530 ga_concat(&ga, p);
1531 ga_concat(&ga, (char_u *)" ");
1532 vim_free(p);
1533 }
1534 s = e + 1;
1535 }
1536 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001537 clear_tv(tv);
1538 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001539 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001540 return OK;
1541 }
1542 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001543 default: to_string_error(tv->v_type);
1544 return FAIL;
1545 }
1546 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001547 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001548 clear_tv(tv);
1549 tv->v_type = VAR_STRING;
1550 tv->vval.v_string = str;
1551 }
1552 return OK;
1553}
1554
1555/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001556 * When the value of "sv" is a null list of dict, allocate it.
1557 */
1558 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001559allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001560{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001561 typval_T *tv = sv->sv_tv;
1562
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001563 switch (tv->v_type)
1564 {
1565 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001566 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001567 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001568 break;
1569 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001570 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001571 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001572 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001573 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001574 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001575 (void)rettv_blob_alloc(tv);
1576 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001577 default:
1578 break;
1579 }
1580}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001581
Bram Moolenaare7525c52021-01-09 13:20:37 +01001582/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001583 * Return the character "str[index]" where "index" is the character index,
1584 * including composing characters.
1585 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001586 */
1587 char_u *
1588char_from_string(char_u *str, varnumber_T index)
1589{
1590 size_t nbyte = 0;
1591 varnumber_T nchar = index;
1592 size_t slen;
1593
1594 if (str == NULL)
1595 return NULL;
1596 slen = STRLEN(str);
1597
Bram Moolenaarff871402021-03-26 13:34:05 +01001598 // Do the same as for a list: a negative index counts from the end.
1599 // Optimization to check the first byte to be below 0x80 (and no composing
1600 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001601 if (index < 0)
1602 {
1603 int clen = 0;
1604
1605 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001606 {
1607 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1608 ++nbyte;
1609 else if (enc_utf8)
1610 nbyte += utfc_ptr2len(str + nbyte);
1611 else
1612 nbyte += mb_ptr2len(str + nbyte);
1613 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001614 nchar = clen + index;
1615 if (nchar < 0)
1616 // unlike list: index out of range results in empty string
1617 return NULL;
1618 }
1619
1620 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001621 {
1622 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1623 ++nbyte;
1624 else if (enc_utf8)
1625 nbyte += utfc_ptr2len(str + nbyte);
1626 else
1627 nbyte += mb_ptr2len(str + nbyte);
1628 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001629 if (nbyte >= slen)
1630 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001631 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001632}
1633
1634/*
1635 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001636 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001637 * If going over the end return "str_len".
1638 * If "idx" is negative count from the end, -1 is the last character.
1639 * When going over the start return -1.
1640 */
1641 static long
1642char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1643{
1644 varnumber_T nchar = idx;
1645 size_t nbyte = 0;
1646
1647 if (nchar >= 0)
1648 {
1649 while (nchar > 0 && nbyte < str_len)
1650 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001651 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001652 --nchar;
1653 }
1654 }
1655 else
1656 {
1657 nbyte = str_len;
1658 while (nchar < 0 && nbyte > 0)
1659 {
1660 --nbyte;
1661 nbyte -= mb_head_off(str, str + nbyte);
1662 ++nchar;
1663 }
1664 if (nchar < 0)
1665 return -1;
1666 }
1667 return (long)nbyte;
1668}
1669
1670/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001671 * Return the slice "str[first : last]" using character indexes. Composing
1672 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001673 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001674 * Return NULL when the result is empty.
1675 */
1676 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001677string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001678{
1679 long start_byte, end_byte;
1680 size_t slen;
1681
1682 if (str == NULL)
1683 return NULL;
1684 slen = STRLEN(str);
1685 start_byte = char_idx2byte(str, slen, first);
1686 if (start_byte < 0)
1687 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001688 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001689 end_byte = (long)slen;
1690 else
1691 {
1692 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001693 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001694 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001695 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001696 }
1697
1698 if (start_byte >= (long)slen || end_byte <= start_byte)
1699 return NULL;
1700 return vim_strnsave(str + start_byte, end_byte - start_byte);
1701}
1702
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001703/*
1704 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1705 * When "dfunc_idx" is negative don't give an error.
1706 * Returns NULL for an error.
1707 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001708 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001709get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001710{
1711 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001712 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1713 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001714 svar_T *sv;
1715
1716 if (sref->sref_seq != si->sn_script_seq)
1717 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001718 // The script was reloaded after the function was compiled, the
1719 // script_idx may not be valid.
1720 if (dfunc != NULL)
1721 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1722 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001723 return NULL;
1724 }
1725 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001726 if (sv->sv_name == NULL)
1727 {
1728 if (dfunc != NULL)
1729 emsg(_(e_script_variable_was_deleted));
1730 return NULL;
1731 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001732 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001733 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001734 if (dfunc != NULL)
1735 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001736 return NULL;
1737 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001738
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001739 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1740 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001741 {
1742 if (dfunc != NULL)
1743 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1744 return NULL;
1745 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001746 return sv;
1747}
1748
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001749/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001750 * Function passed to do_cmdline() for splitting a script joined by NL
1751 * characters.
1752 */
1753 static char_u *
1754get_split_sourceline(
1755 int c UNUSED,
1756 void *cookie,
1757 int indent UNUSED,
1758 getline_opt_T options UNUSED)
1759{
1760 source_cookie_T *sp = (source_cookie_T *)cookie;
1761 char_u *p;
1762 char_u *line;
1763
Bram Moolenaar20677332021-06-06 17:02:53 +02001764 p = vim_strchr(sp->nextline, '\n');
1765 if (p == NULL)
1766 {
1767 line = vim_strsave(sp->nextline);
1768 sp->nextline += STRLEN(sp->nextline);
1769 }
1770 else
1771 {
1772 line = vim_strnsave(sp->nextline, p - sp->nextline);
1773 sp->nextline = p + 1;
1774 }
1775 return line;
1776}
1777
1778/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 * Execute a function by "name".
1780 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001781 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 */
1783 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001784call_eval_func(
1785 char_u *name,
1786 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001787 ectx_T *ectx,
1788 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789{
Bram Moolenaared677f52020-08-12 16:38:10 +02001790 int called_emsg_before = called_emsg;
1791 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001793 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001794 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001796 dictitem_T *v;
1797
1798 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001799 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1800 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001801 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001802 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001803 return FAIL;
1804 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001805 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001807 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808}
1809
1810/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001811 * When a function reference is used, fill a partial with the information
1812 * needed, especially when it is used as a closure.
1813 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001814 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001815fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001816 partial_T *pt,
1817 ufunc_T *ufunc,
1818 loopvarinfo_T *lvi,
1819 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001820{
1821 pt->pt_func = ufunc;
1822 pt->pt_refcount = 1;
1823
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001824 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001825 {
1826 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1827 + ectx->ec_dfunc_idx;
1828
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001829 // The closure may need to find arguments and local variables of the
1830 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001831 pt->pt_outer.out_stack = &ectx->ec_stack;
1832 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001833 if (ectx->ec_outer_ref != NULL)
1834 {
1835 // The current context already has a context, link to that one.
1836 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1837 if (ectx->ec_outer_ref->or_partial != NULL)
1838 {
1839 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1840 ++pt->pt_outer.out_up_partial->pt_refcount;
1841 }
1842 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001843
Bram Moolenaarcc341812022-09-19 15:54:34 +01001844 if (lvi != NULL)
1845 {
1846 int depth;
1847
1848 // The closure may need to find variables defined inside a loop,
1849 // for every nested loop. A new reference is made every time,
1850 // ISN_ENDLOOP will check if they are actually used.
1851 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1852 {
1853 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1854 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1855 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1856 pt->pt_outer.out_loop[depth].var_count =
1857 lvi->lvi_loop[depth].var_count;
1858 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001859 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001860 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001861 else
1862 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001863
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001864 // If the function currently executing returns and the closure is still
1865 // being referenced, we need to make a copy of the context (arguments
1866 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001867 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001868 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001869 {
1870 vim_free(pt);
1871 return FAIL;
1872 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001873 // Extra variable keeps the count of closures created in the current
1874 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001875 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1876 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1877
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001878 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1879 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001880 ++pt->pt_refcount;
1881 ++ectx->ec_funcrefs.ga_len;
1882 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001883 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001884 return OK;
1885}
1886
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001887/*
1888 * Execute iptr->isn_arg.string as an Ex command.
1889 */
1890 static int
1891exec_command(isn_T *iptr)
1892{
1893 source_cookie_T cookie;
1894
1895 SOURCING_LNUM = iptr->isn_lnum;
1896 // Pass getsourceline to get an error for a missing ":end"
1897 // command.
1898 CLEAR_FIELD(cookie);
1899 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1900 if (do_cmdline(iptr->isn_arg.string,
1901 getsourceline, &cookie,
1902 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1903 || did_emsg)
1904 return FAIL;
1905 return OK;
1906}
1907
Bram Moolenaar89445512022-04-14 12:58:23 +01001908/*
1909 * If script "sid" is not loaded yet then load it now.
1910 * Caller must make sure "sid" is a valid script ID.
1911 * "loaded" is set to TRUE if the script had to be loaded.
1912 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1913 */
1914 int
1915may_load_script(int sid, int *loaded)
1916{
1917 scriptitem_T *si = SCRIPT_ITEM(sid);
1918
1919 if (si->sn_state == SN_STATE_NOT_LOADED)
1920 {
1921 *loaded = TRUE;
1922 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1923 {
1924 semsg(_(e_cant_open_file_str), si->sn_name);
1925 return FAIL;
1926 }
1927 }
1928 return OK;
1929}
1930
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001931// used for v_instr of typval of VAR_INSTR
1932struct instr_S {
1933 ectx_T *instr_ectx;
1934 isn_T *instr_instr;
1935};
1936
Bram Moolenaar4c137212021-04-19 16:48:48 +02001937// used for substitute_instr
1938typedef struct subs_expr_S {
1939 ectx_T *subs_ectx;
1940 isn_T *subs_instr;
1941 int subs_status;
1942} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001944// Set when calling do_debug().
1945static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001946static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001947
1948/*
1949 * When debugging lookup "name" and return the typeval.
1950 * When not found return NULL.
1951 */
1952 typval_T *
1953lookup_debug_var(char_u *name)
1954{
1955 int idx;
1956 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001957 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001958 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001959 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001960
1961 if (ectx == NULL)
1962 return NULL;
1963 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1964
1965 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001966 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001967 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001968 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1969
1970 // the variable name may be NULL when not available in this block
1971 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001972 return STACK_TV_VAR(idx);
1973 }
1974
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001975 // Go through argument names.
1976 ufunc = dfunc->df_ufunc;
1977 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1978 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1979 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1980 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1981 - varargs_off + idx);
1982 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1983 return STACK_TV(ectx->ec_frame_idx - 1);
1984
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001985 return NULL;
1986}
1987
Bram Moolenaar26a44842021-09-02 18:49:06 +02001988/*
1989 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1990 * breakpoint was set in that function or when there is any expression.
1991 */
1992 int
1993may_break_in_function(ufunc_T *ufunc)
1994{
1995 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1996}
1997
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001998 static void
1999handle_debug(isn_T *iptr, ectx_T *ectx)
2000{
2001 char_u *line;
2002 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2003 + ectx->ec_dfunc_idx)->df_ufunc;
2004 isn_T *ni;
2005 int end_lnum = iptr->isn_lnum;
2006 garray_T ga;
2007 int lnum;
2008
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002009 if (ex_nesting_level > debug_break_level)
2010 {
2011 linenr_T breakpoint;
2012
Bram Moolenaar26a44842021-09-02 18:49:06 +02002013 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002014 return;
2015
2016 // check for the next breakpoint if needed
2017 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002018 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002019 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2020 return;
2021 }
2022
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002023 SOURCING_LNUM = iptr->isn_lnum;
2024 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002025 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002026
2027 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2028 if (ni->isn_type == ISN_DEBUG
2029 || ni->isn_type == ISN_RETURN
2030 || ni->isn_type == ISN_RETURN_VOID)
2031 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002032 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002033 break;
2034 }
2035
2036 if (end_lnum > iptr->isn_lnum)
2037 {
2038 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002039 for (lnum = iptr->isn_lnum; lnum < end_lnum
2040 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002041 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002042 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002043
Bram Moolenaar303215d2021-07-07 20:10:43 +02002044 if (p == NULL)
2045 continue; // left over from continuation line
2046 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002047 if (*p == '#')
2048 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002049 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002050 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2051 if (STRNCMP(p, "def ", 4) == 0)
2052 break;
2053 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002054 line = ga_concat_strings(&ga, " ");
2055 vim_free(ga.ga_data);
2056 }
2057 else
2058 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002059
Dominique Pelle6e969552021-06-17 13:53:41 +02002060 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002061 debug_context = NULL;
2062
2063 if (end_lnum > iptr->isn_lnum)
2064 vim_free(line);
2065}
2066
Bram Moolenaar4c137212021-04-19 16:48:48 +02002067/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002068 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002069 * Returns OK, FAIL or NOTDONE (uncatchable error).
2070 */
2071 static int
2072execute_storeindex(isn_T *iptr, ectx_T *ectx)
2073{
2074 vartype_T dest_type = iptr->isn_arg.vartype;
2075 typval_T *tv;
2076 typval_T *tv_idx = STACK_TV_BOT(-2);
2077 typval_T *tv_dest = STACK_TV_BOT(-1);
2078 int status = OK;
2079
2080 // Stack contains:
2081 // -3 value to be stored
2082 // -2 index
2083 // -1 dict or list
2084 tv = STACK_TV_BOT(-3);
2085 SOURCING_LNUM = iptr->isn_lnum;
2086 if (dest_type == VAR_ANY)
2087 {
2088 dest_type = tv_dest->v_type;
2089 if (dest_type == VAR_DICT)
2090 status = do_2string(tv_idx, TRUE, FALSE);
2091 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2092 {
2093 emsg(_(e_number_expected));
2094 status = FAIL;
2095 }
2096 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002097
2098 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002099 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002100 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002101 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002102 long lidx = (long)tv_idx->vval.v_number;
2103 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002104
Bram Moolenaare08be092022-02-17 13:08:26 +00002105 if (list == NULL)
2106 {
2107 emsg(_(e_list_not_set));
2108 return FAIL;
2109 }
2110 if (lidx < 0 && list->lv_len + lidx >= 0)
2111 // negative index is relative to the end
2112 lidx = list->lv_len + lidx;
2113 if (lidx < 0 || lidx > list->lv_len)
2114 {
2115 semsg(_(e_list_index_out_of_range_nr), lidx);
2116 return FAIL;
2117 }
2118 if (lidx < list->lv_len)
2119 {
2120 listitem_T *li = list_find(list, lidx);
2121
2122 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002123 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002124 return FAIL;
2125 // overwrite existing list item
2126 clear_tv(&li->li_tv);
2127 li->li_tv = *tv;
2128 }
2129 else
2130 {
2131 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2132 return FAIL;
2133 // append to list, only fails when out of memory
2134 if (list_append_tv(list, tv) == FAIL)
2135 return NOTDONE;
2136 clear_tv(tv);
2137 }
2138 }
2139 else if (dest_type == VAR_DICT)
2140 {
2141 char_u *key = tv_idx->vval.v_string;
2142 dict_T *dict = tv_dest->vval.v_dict;
2143 dictitem_T *di;
2144
2145 SOURCING_LNUM = iptr->isn_lnum;
2146 if (dict == NULL)
2147 {
2148 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002149 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002150 }
2151 if (key == NULL)
2152 key = (char_u *)"";
2153 di = dict_find(dict, key, -1);
2154 if (di != NULL)
2155 {
2156 if (error_if_locked(di->di_tv.v_lock,
2157 e_cannot_change_dict_item))
2158 return FAIL;
2159 // overwrite existing value
2160 clear_tv(&di->di_tv);
2161 di->di_tv = *tv;
2162 }
2163 else
2164 {
2165 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2166 return FAIL;
2167 // add to dict, only fails when out of memory
2168 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2169 return NOTDONE;
2170 clear_tv(tv);
2171 }
2172 }
2173 else if (dest_type == VAR_BLOB)
2174 {
2175 long lidx = (long)tv_idx->vval.v_number;
2176 blob_T *blob = tv_dest->vval.v_blob;
2177 varnumber_T nr;
2178 int error = FALSE;
2179 int len;
2180
2181 if (blob == NULL)
2182 {
2183 emsg(_(e_blob_not_set));
2184 return FAIL;
2185 }
2186 len = blob_len(blob);
2187 if (lidx < 0 && len + lidx >= 0)
2188 // negative index is relative to the end
2189 lidx = len + lidx;
2190
2191 // Can add one byte at the end.
2192 if (lidx < 0 || lidx > len)
2193 {
2194 semsg(_(e_blob_index_out_of_range_nr), lidx);
2195 return FAIL;
2196 }
2197 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2198 return FAIL;
2199 nr = tv_get_number_chk(tv, &error);
2200 if (error)
2201 return FAIL;
2202 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002203 }
2204 else
2205 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002206 status = FAIL;
2207 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002208 }
2209 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002210
2211 clear_tv(tv_idx);
2212 clear_tv(tv_dest);
2213 ectx->ec_stack.ga_len -= 3;
2214 if (status == FAIL)
2215 {
2216 clear_tv(tv);
2217 return FAIL;
2218 }
2219 return OK;
2220}
2221
2222/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002223 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002224 */
2225 static int
2226execute_storerange(isn_T *iptr, ectx_T *ectx)
2227{
2228 typval_T *tv;
2229 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2230 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2231 typval_T *tv_dest = STACK_TV_BOT(-1);
2232 int status = OK;
2233
2234 // Stack contains:
2235 // -4 value to be stored
2236 // -3 first index or "none"
2237 // -2 second index or "none"
2238 // -1 destination list or blob
2239 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002240 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002241 if (tv_dest->v_type == VAR_LIST)
2242 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002243 long n1;
2244 long n2;
2245 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002246
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002247 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2248 if (tv_idx2->v_type == VAR_SPECIAL
2249 && tv_idx2->vval.v_number == VVAL_NONE)
2250 n2 = list_len(tv_dest->vval.v_list) - 1;
2251 else
2252 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2253
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002254 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002255 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002256 status = FAIL;
2257 else
2258 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002259 status = check_range_index_two(tv_dest->vval.v_list,
2260 &n1, li1, &n2, FALSE);
2261 if (status != FAIL)
2262 status = list_assign_range(
2263 tv_dest->vval.v_list,
2264 tv->vval.v_list,
2265 n1,
2266 n2,
2267 tv_idx2->v_type == VAR_SPECIAL,
2268 (char_u *)"=",
2269 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002270 }
2271 }
2272 else if (tv_dest->v_type == VAR_BLOB)
2273 {
2274 varnumber_T n1;
2275 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002276 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002277
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002278 n1 = tv_get_number_chk(tv_idx1, NULL);
2279 if (tv_idx2->v_type == VAR_SPECIAL
2280 && tv_idx2->vval.v_number == VVAL_NONE)
2281 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2282 else
2283 n2 = tv_get_number_chk(tv_idx2, NULL);
2284 bloblen = blob_len(tv_dest->vval.v_blob);
2285
2286 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2287 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002288 status = FAIL;
2289 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002290 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002291 }
2292 else
2293 {
2294 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002295 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002296 }
2297
2298 clear_tv(tv_idx1);
2299 clear_tv(tv_idx2);
2300 clear_tv(tv_dest);
2301 ectx->ec_stack.ga_len -= 4;
2302 clear_tv(tv);
2303
2304 return status;
2305}
2306
2307/*
2308 * Unlet item in list or dict variable.
2309 */
2310 static int
2311execute_unletindex(isn_T *iptr, ectx_T *ectx)
2312{
2313 typval_T *tv_idx = STACK_TV_BOT(-2);
2314 typval_T *tv_dest = STACK_TV_BOT(-1);
2315 int status = OK;
2316
2317 // Stack contains:
2318 // -2 index
2319 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002320 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002321 if (tv_dest->v_type == VAR_DICT)
2322 {
2323 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002324 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002325 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002326 semsg(_(e_expected_str_but_got_str),
2327 vartype_name(VAR_STRING),
2328 vartype_name(tv_idx->v_type));
2329 status = FAIL;
2330 }
2331 else
2332 {
2333 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002334 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002335 dictitem_T *di = NULL;
2336
2337 if (d != NULL && value_check_lock(
2338 d->dv_lock, NULL, FALSE))
2339 status = FAIL;
2340 else
2341 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002342 if (tv_idx->v_type == VAR_STRING)
2343 {
2344 key = tv_idx->vval.v_string;
2345 if (key == NULL)
2346 key = (char_u *)"";
2347 }
2348 else
2349 {
2350 key = tv_get_string(tv_idx);
2351 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002352 if (d != NULL)
2353 di = dict_find(d, key, (int)STRLEN(key));
2354 if (di == NULL)
2355 {
2356 // NULL dict is equivalent to empty dict
2357 semsg(_(e_key_not_present_in_dictionary),
2358 key);
2359 status = FAIL;
2360 }
2361 else if (var_check_fixed(di->di_flags,
2362 NULL, FALSE)
2363 || var_check_ro(di->di_flags,
2364 NULL, FALSE))
2365 status = FAIL;
2366 else
2367 dictitem_remove(d, di);
2368 }
2369 }
2370 }
2371 else if (tv_dest->v_type == VAR_LIST)
2372 {
2373 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002374 if (check_for_number(tv_idx) == FAIL)
2375 {
2376 status = FAIL;
2377 }
2378 else
2379 {
2380 list_T *l = tv_dest->vval.v_list;
2381 long n = (long)tv_idx->vval.v_number;
2382
2383 if (l != NULL && value_check_lock(
2384 l->lv_lock, NULL, FALSE))
2385 status = FAIL;
2386 else
2387 {
2388 listitem_T *li = list_find(l, n);
2389
2390 if (li == NULL)
2391 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002392 semsg(_(e_list_index_out_of_range_nr), n);
2393 status = FAIL;
2394 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002395 else
2396 listitem_remove(l, li);
2397 }
2398 }
2399 }
2400 else
2401 {
2402 status = FAIL;
2403 semsg(_(e_cannot_index_str),
2404 vartype_name(tv_dest->v_type));
2405 }
2406
2407 clear_tv(tv_idx);
2408 clear_tv(tv_dest);
2409 ectx->ec_stack.ga_len -= 2;
2410
2411 return status;
2412}
2413
2414/*
2415 * Unlet a range of items in a list variable.
2416 */
2417 static int
2418execute_unletrange(isn_T *iptr, ectx_T *ectx)
2419{
2420 // Stack contains:
2421 // -3 index1
2422 // -2 index2
2423 // -1 dict or list
2424 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2425 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2426 typval_T *tv_dest = STACK_TV_BOT(-1);
2427 int status = OK;
2428
2429 if (tv_dest->v_type == VAR_LIST)
2430 {
2431 // indexes must be a number
2432 SOURCING_LNUM = iptr->isn_lnum;
2433 if (check_for_number(tv_idx1) == FAIL
2434 || (tv_idx2->v_type != VAR_SPECIAL
2435 && check_for_number(tv_idx2) == FAIL))
2436 {
2437 status = FAIL;
2438 }
2439 else
2440 {
2441 list_T *l = tv_dest->vval.v_list;
2442 long n1 = (long)tv_idx1->vval.v_number;
2443 long n2 = tv_idx2->v_type == VAR_SPECIAL
2444 ? 0 : (long)tv_idx2->vval.v_number;
2445 listitem_T *li;
2446
2447 li = list_find_index(l, &n1);
2448 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002449 {
2450 semsg(_(e_list_index_out_of_range_nr),
2451 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002452 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002453 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002454 else
2455 {
2456 if (n1 < 0)
2457 n1 = list_idx_of_item(l, li);
2458 if (n2 < 0)
2459 {
2460 listitem_T *li2 = list_find(l, n2);
2461
2462 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002463 {
2464 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002465 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002466 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002467 else
2468 n2 = list_idx_of_item(l, li2);
2469 }
2470 if (status != FAIL
2471 && tv_idx2->v_type != VAR_SPECIAL
2472 && n2 < n1)
2473 {
2474 semsg(_(e_list_index_out_of_range_nr), n2);
2475 status = FAIL;
2476 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002477 if (status != FAIL)
2478 list_unlet_range(l, li, n1,
2479 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002480 }
2481 }
2482 }
2483 else
2484 {
2485 status = FAIL;
2486 SOURCING_LNUM = iptr->isn_lnum;
2487 semsg(_(e_cannot_index_str),
2488 vartype_name(tv_dest->v_type));
2489 }
2490
2491 clear_tv(tv_idx1);
2492 clear_tv(tv_idx2);
2493 clear_tv(tv_dest);
2494 ectx->ec_stack.ga_len -= 3;
2495
2496 return status;
2497}
2498
2499/*
2500 * Top of a for loop.
2501 */
2502 static int
2503execute_for(isn_T *iptr, ectx_T *ectx)
2504{
2505 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002506 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002507 typval_T *ltv = STACK_TV_BOT(-1);
2508 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002509 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002510
2511 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2512 return FAIL;
2513 if (ltv->v_type == VAR_LIST)
2514 {
2515 list_T *list = ltv->vval.v_list;
2516
2517 // push the next item from the list
2518 ++idxtv->vval.v_number;
2519 if (list == NULL
2520 || idxtv->vval.v_number >= list->lv_len)
2521 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002522 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002523 }
2524 else if (list->lv_first == &range_list_item)
2525 {
2526 // non-materialized range() list
2527 tv = STACK_TV_BOT(0);
2528 tv->v_type = VAR_NUMBER;
2529 tv->v_lock = 0;
2530 tv->vval.v_number = list_find_nr(
2531 list, idxtv->vval.v_number, NULL);
2532 ++ectx->ec_stack.ga_len;
2533 }
2534 else
2535 {
2536 listitem_T *li = list_find(list,
2537 idxtv->vval.v_number);
2538
2539 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2540 ++ectx->ec_stack.ga_len;
2541 }
2542 }
2543 else if (ltv->v_type == VAR_STRING)
2544 {
2545 char_u *str = ltv->vval.v_string;
2546
2547 // The index is for the last byte of the previous
2548 // character.
2549 ++idxtv->vval.v_number;
2550 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2551 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002552 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002553 }
2554 else
2555 {
2556 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2557
2558 // Push the next character from the string.
2559 tv = STACK_TV_BOT(0);
2560 tv->v_type = VAR_STRING;
2561 tv->vval.v_string = vim_strnsave(
2562 str + idxtv->vval.v_number, clen);
2563 ++ectx->ec_stack.ga_len;
2564 idxtv->vval.v_number += clen - 1;
2565 }
2566 }
2567 else if (ltv->v_type == VAR_BLOB)
2568 {
2569 blob_T *blob = ltv->vval.v_blob;
2570
2571 // When we get here the first time make a copy of the
2572 // blob, so that the iteration still works when it is
2573 // changed.
2574 if (idxtv->vval.v_number == -1 && blob != NULL)
2575 {
2576 blob_copy(blob, ltv);
2577 blob_unref(blob);
2578 blob = ltv->vval.v_blob;
2579 }
2580
2581 // The index is for the previous byte.
2582 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002583 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002584 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002585 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002586 }
2587 else
2588 {
2589 // Push the next byte from the blob.
2590 tv = STACK_TV_BOT(0);
2591 tv->v_type = VAR_NUMBER;
2592 tv->vval.v_number = blob_get(blob,
2593 idxtv->vval.v_number);
2594 ++ectx->ec_stack.ga_len;
2595 }
2596 }
2597 else
2598 {
2599 semsg(_(e_for_loop_on_str_not_supported),
2600 vartype_name(ltv->v_type));
2601 return FAIL;
2602 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002603
2604 if (jump)
2605 {
2606 // past the end of the list/string/blob, jump to "endfor"
2607 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2608 may_restore_cmdmod(&ectx->ec_funclocal);
2609 }
2610 else
2611 {
2612 // Store the current number of funcrefs, this may be used in
2613 // ISN_LOOPEND. The variable index is always one more than the loop
2614 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002615 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002616 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2617 }
2618
2619 return OK;
2620}
2621
2622/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002623 * Code for handling variables declared inside a loop and used in a closure.
2624 * This is very similar to what is done with funcstack_T. The difference is
2625 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2626 * scope of the block inside a loop and each loop may have its own.
2627 */
2628
2629// Double linked list of loopvars_T in use.
2630static loopvars_T *first_loopvars = NULL;
2631
2632 static void
2633add_loopvars_to_list(loopvars_T *loopvars)
2634{
2635 // Link in list of loopvarss.
2636 if (first_loopvars != NULL)
2637 first_loopvars->lvs_prev = loopvars;
2638 loopvars->lvs_next = first_loopvars;
2639 loopvars->lvs_prev = NULL;
2640 first_loopvars = loopvars;
2641}
2642
2643 static void
2644remove_loopvars_from_list(loopvars_T *loopvars)
2645{
2646 if (loopvars->lvs_prev == NULL)
2647 first_loopvars = loopvars->lvs_next;
2648 else
2649 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2650 if (loopvars->lvs_next != NULL)
2651 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2652}
2653
2654/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002655 * End of a for or while loop: Handle any variables used by a closure.
2656 */
2657 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002658execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002659{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002660 endloop_T *endloop = &iptr->isn_arg.endloop;
2661 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2662 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002663 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002664 garray_T *gap = &ectx->ec_funcrefs;
2665 int closure_in_use = FALSE;
2666 loopvars_T *loopvars;
2667 typval_T *stack;
2668 int idx;
2669
Bram Moolenaarcc341812022-09-19 15:54:34 +01002670 // Check if any created closure is still being referenced and loopvars have
2671 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002672 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2673 {
2674 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2675
Bram Moolenaarcc341812022-09-19 15:54:34 +01002676 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002677 {
2678 int refcount = pt->pt_refcount;
2679 int i;
2680
2681 // A Reference in a variable inside the loop doesn't count, it gets
2682 // unreferenced at the end of the loop.
2683 for (i = 0; i < endloop->end_var_count; ++i)
2684 {
2685 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2686
2687 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2688 --refcount;
2689 }
2690 if (refcount > 1)
2691 {
2692 closure_in_use = TRUE;
2693 break;
2694 }
2695 }
2696 }
2697
2698 // If no function reference were created since the start of the loop block
2699 // or it is no longer referenced there is nothing to do.
2700 if (!closure_in_use)
2701 return OK;
2702
2703 // A closure is using variables declared inside the loop.
2704 // Move them to the called function.
2705 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2706 if (loopvars == NULL)
2707 return FAIL;
2708
2709 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2710 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2711 loopvars->lvs_ga.ga_data = stack;
2712 if (stack == NULL)
2713 {
2714 vim_free(loopvars);
2715 return FAIL;
2716 }
2717 add_loopvars_to_list(loopvars);
2718
2719 // Move the variable values.
2720 for (idx = 0; idx < endloop->end_var_count; ++idx)
2721 {
2722 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2723
2724 *(stack + idx) = *tv;
2725 tv->v_type = VAR_UNKNOWN;
2726 }
2727
2728 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2729 {
2730 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2731
Bram Moolenaarcc341812022-09-19 15:54:34 +01002732 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002733 {
2734 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002735 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002736
Bram Moolenaarcc341812022-09-19 15:54:34 +01002737 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2738 pt->pt_outer.out_loop[depth].var_idx -=
2739 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002740 }
2741 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002742
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002743 return OK;
2744}
2745
2746/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002747 * Called when a partial is freed or its reference count goes down to one. The
2748 * loopvars may be the only reference to the partials in the local variables.
2749 * Go over all of them, the funcref and can be freed if all partials
2750 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002751 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002752 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002753 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002754loopvars_check_refcount(loopvars_T *loopvars)
2755{
2756 int i;
2757 garray_T *gap = &loopvars->lvs_ga;
2758 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002759 typval_T *stack = gap->ga_data;
2760
Bram Moolenaarcc341812022-09-19 15:54:34 +01002761 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2762 return FALSE;
2763 for (i = 0; i < gap->ga_len; ++i)
2764 {
2765 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2766
2767 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2768 && tv->vval.v_partial->pt_refcount == 1)
2769 {
2770 int depth;
2771
2772 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2773 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2774 ++done;
2775 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002776 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002777 if (done != loopvars->lvs_min_refcount)
2778 return FALSE;
2779
2780 // All partials referencing the loopvars have a reference count of
2781 // one, thus the loopvars is no longer of use.
2782 stack = gap->ga_data;
2783 for (i = 0; i < gap->ga_len; ++i)
2784 clear_tv(stack + i);
2785 vim_free(stack);
2786 remove_loopvars_from_list(loopvars);
2787 vim_free(loopvars);
2788 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002789}
2790
2791/*
2792 * For garbage collecting: set references in all variables referenced by
2793 * all loopvars.
2794 */
2795 int
2796set_ref_in_loopvars(int copyID)
2797{
2798 loopvars_T *loopvars;
2799
2800 for (loopvars = first_loopvars; loopvars != NULL;
2801 loopvars = loopvars->lvs_next)
2802 {
2803 typval_T *stack = loopvars->lvs_ga.ga_data;
2804 int i;
2805
2806 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2807 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2808 return TRUE; // abort
2809 }
2810 return FALSE;
2811}
2812
2813/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002814 * Load instruction for w:/b:/g:/t: variable.
2815 * "isn_type" is used instead of "iptr->isn_type".
2816 */
2817 static int
2818load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2819{
2820 dictitem_T *di = NULL;
2821 hashtab_T *ht = NULL;
2822 char namespace;
2823
2824 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2825 return NOTDONE;
2826 switch (isn_type)
2827 {
2828 case ISN_LOADG:
2829 ht = get_globvar_ht();
2830 namespace = 'g';
2831 break;
2832 case ISN_LOADB:
2833 ht = &curbuf->b_vars->dv_hashtab;
2834 namespace = 'b';
2835 break;
2836 case ISN_LOADW:
2837 ht = &curwin->w_vars->dv_hashtab;
2838 namespace = 'w';
2839 break;
2840 case ISN_LOADT:
2841 ht = &curtab->tp_vars->dv_hashtab;
2842 namespace = 't';
2843 break;
2844 default: // Cannot reach here
2845 return NOTDONE;
2846 }
2847 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2848
Bram Moolenaar06b77222022-01-25 15:51:56 +00002849 if (di == NULL)
2850 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002851 if (isn_type == ISN_LOADG)
2852 {
2853 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2854
2855 // g:Something could be a function
2856 if (ufunc != NULL)
2857 {
2858 typval_T *tv = STACK_TV_BOT(0);
2859
2860 ++ectx->ec_stack.ga_len;
2861 tv->v_type = VAR_FUNC;
2862 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2863 if (tv->vval.v_string == NULL)
2864 return FAIL;
2865 STRCPY(tv->vval.v_string, "g:");
2866 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2867 return OK;
2868 }
2869 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002870 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002871 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002872 // no check if the item exists in the script but
2873 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002874 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002875 else
2876 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002877 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002878 return FAIL;
2879 }
2880 else
2881 {
2882 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2883 ++ectx->ec_stack.ga_len;
2884 }
2885 return OK;
2886}
2887
2888/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002889 * Execute instructions in execution context "ectx".
2890 * Return OK or FAIL;
2891 */
2892 static int
2893exec_instructions(ectx_T *ectx)
2894{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002895 int ret = FAIL;
2896 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002897 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002898
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002899 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002900 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002901
Bram Moolenaarff652882021-05-16 15:24:49 +02002902 // Only catch exceptions in this instruction list.
2903 ectx->ec_trylevel_at_start = trylevel;
2904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 for (;;)
2906 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002907 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002909 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002910
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002911 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002912 {
2913 line_breakcheck();
2914 breakcheck_count = 0;
2915 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002916 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002917 {
2918 // Turn CTRL-C into an exception.
2919 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002920 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002921 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002922 did_throw = TRUE;
2923 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002925 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002926 {
2927 // Turn an error message into an exception.
2928 did_emsg = FALSE;
2929 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002930 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002931 did_throw = TRUE;
2932 *msg_list = NULL;
2933 }
2934
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002935 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002937 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002938 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002939 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940
2941 // An exception jumps to the first catch, finally, or returns from
2942 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002943 while (index > 0)
2944 {
2945 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002946 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002947 break;
2948 // In the catch and finally block of this try we have to go up
2949 // one level.
2950 --index;
2951 trycmd = NULL;
2952 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002953 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002955 if (trycmd->tcd_in_catch)
2956 {
2957 // exception inside ":catch", jump to ":finally" once
2958 ectx->ec_iidx = trycmd->tcd_finally_idx;
2959 trycmd->tcd_finally_idx = 0;
2960 }
2961 else
2962 // jump to first ":catch"
2963 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002964 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002965 did_throw = FALSE; // don't come back here until :endtry
2966 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 }
2968 else
2969 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002970 // Not inside try or need to return from current functions.
2971 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002972 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002973 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002974 tv = STACK_TV_BOT(0);
2975 tv->v_type = VAR_NUMBER;
2976 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002977 ++ectx->ec_stack.ga_len;
2978 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002980 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002981 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002982 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002983 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 goto done;
2985 }
2986
Bram Moolenaar4c137212021-04-19 16:48:48 +02002987 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002988 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 }
2990 continue;
2991 }
2992
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002993 /*
2994 * Big switch on the instruction. Most compilers will be turning this
2995 * into an efficient lookup table, since the "case" values are an enum
2996 * with sequential numbers. It may look ugly, but it should be the
2997 * most efficient way.
2998 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002999 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 switch (iptr->isn_type)
3001 {
3002 // execute Ex command line
3003 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003004 if (exec_command(iptr) == FAIL)
3005 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 break;
3007
Bram Moolenaar20677332021-06-06 17:02:53 +02003008 // execute Ex command line split at NL characters.
3009 case ISN_EXEC_SPLIT:
3010 {
3011 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003012 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003013
3014 SOURCING_LNUM = iptr->isn_lnum;
3015 CLEAR_FIELD(cookie);
3016 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3017 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003018 line = get_split_sourceline(0, &cookie, 0, 0);
3019 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003020 get_split_sourceline, &cookie,
3021 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3022 == FAIL
3023 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003024 {
3025 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003026 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003027 }
3028 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003029 }
3030 break;
3031
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003032 // execute Ex command line that is only a range
3033 case ISN_EXECRANGE:
3034 {
3035 exarg_T ea;
3036 char *error = NULL;
3037
3038 CLEAR_FIELD(ea);
3039 ea.cmdidx = CMD_SIZE;
3040 ea.addr_type = ADDR_LINES;
3041 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003042 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003043 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003044 if (ea.cmd == NULL)
3045 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003046 // error is always NULL when using ADDR_LINES
3047 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003048 if (error != NULL)
3049 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003050 emsg(error);
3051 goto on_error;
3052 }
3053 }
3054 break;
3055
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003056 // Evaluate an expression with legacy syntax, push it onto the
3057 // stack.
3058 case ISN_LEGACY_EVAL:
3059 {
3060 char_u *arg = iptr->isn_arg.string;
3061 int res;
3062 int save_flags = cmdmod.cmod_flags;
3063
Bram Moolenaar35578162021-08-02 19:10:38 +02003064 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003065 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003066 tv = STACK_TV_BOT(0);
3067 init_tv(tv);
3068 cmdmod.cmod_flags |= CMOD_LEGACY;
3069 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3070 cmdmod.cmod_flags = save_flags;
3071 if (res == FAIL)
3072 goto on_error;
3073 ++ectx->ec_stack.ga_len;
3074 }
3075 break;
3076
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003077 // push typeval VAR_INSTR with instructions to be executed
3078 case ISN_INSTR:
3079 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003080 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003081 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003082 tv = STACK_TV_BOT(0);
3083 tv->vval.v_instr = ALLOC_ONE(instr_T);
3084 if (tv->vval.v_instr == NULL)
3085 goto on_error;
3086 ++ectx->ec_stack.ga_len;
3087
3088 tv->v_type = VAR_INSTR;
3089 tv->vval.v_instr->instr_ectx = ectx;
3090 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3091 }
3092 break;
3093
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003094 case ISN_SOURCE:
3095 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003096 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003097
Bram Moolenaar89445512022-04-14 12:58:23 +01003098 SOURCING_LNUM = iptr->isn_lnum;
3099 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003100 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003101 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003102 }
3103 break;
3104
Bram Moolenaar4c137212021-04-19 16:48:48 +02003105 // execute :substitute with an expression
3106 case ISN_SUBSTITUTE:
3107 {
3108 subs_T *subs = &iptr->isn_arg.subs;
3109 source_cookie_T cookie;
3110 struct subs_expr_S *save_instr = substitute_instr;
3111 struct subs_expr_S subs_instr;
3112 int res;
3113
3114 subs_instr.subs_ectx = ectx;
3115 subs_instr.subs_instr = subs->subs_instr;
3116 subs_instr.subs_status = OK;
3117 substitute_instr = &subs_instr;
3118
3119 SOURCING_LNUM = iptr->isn_lnum;
3120 // This is very much like ISN_EXEC
3121 CLEAR_FIELD(cookie);
3122 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3123 res = do_cmdline(subs->subs_cmd,
3124 getsourceline, &cookie,
3125 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3126 substitute_instr = save_instr;
3127
3128 if (res == FAIL || did_emsg
3129 || subs_instr.subs_status == FAIL)
3130 goto on_error;
3131 }
3132 break;
3133
3134 case ISN_FINISH:
3135 goto done;
3136
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003137 case ISN_REDIRSTART:
3138 // create a dummy entry for var_redir_str()
3139 if (alloc_redir_lval() == FAIL)
3140 goto on_error;
3141
3142 // The output is stored in growarray "redir_ga" until
3143 // redirection ends.
3144 init_redir_ga();
3145 redir_vname = 1;
3146 break;
3147
3148 case ISN_REDIREND:
3149 {
3150 char_u *res = get_clear_redir_ga();
3151
3152 // End redirection, put redirected text on the stack.
3153 clear_redir_lval();
3154 redir_vname = 0;
3155
Bram Moolenaar35578162021-08-02 19:10:38 +02003156 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003157 {
3158 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003159 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003160 }
3161 tv = STACK_TV_BOT(0);
3162 tv->v_type = VAR_STRING;
3163 tv->vval.v_string = res;
3164 ++ectx->ec_stack.ga_len;
3165 }
3166 break;
3167
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003168 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003169#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003170 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003171 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3172 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003173 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003174#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003175 break;
3176
3177 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003178#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003179 {
3180 exarg_T ea;
3181 int res;
3182
3183 CLEAR_FIELD(ea);
3184 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3185 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3186 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3187 --ectx->ec_stack.ga_len;
3188 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003189 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003190 res = cexpr_core(&ea, tv);
3191 clear_tv(tv);
3192 if (res == FAIL)
3193 goto on_error;
3194 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003195#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003196 break;
3197
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003198 // execute Ex command from pieces on the stack
3199 case ISN_EXECCONCAT:
3200 {
3201 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003202 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003203 int pass;
3204 int i;
3205 char_u *cmd = NULL;
3206 char_u *str;
3207
3208 for (pass = 1; pass <= 2; ++pass)
3209 {
3210 for (i = 0; i < count; ++i)
3211 {
3212 tv = STACK_TV_BOT(i - count);
3213 str = tv->vval.v_string;
3214 if (str != NULL && *str != NUL)
3215 {
3216 if (pass == 2)
3217 STRCPY(cmd + len, str);
3218 len += STRLEN(str);
3219 }
3220 if (pass == 2)
3221 clear_tv(tv);
3222 }
3223 if (pass == 1)
3224 {
3225 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003226 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003227 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003228 len = 0;
3229 }
3230 }
3231
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003232 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003233 do_cmdline_cmd(cmd);
3234 vim_free(cmd);
3235 }
3236 break;
3237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 // execute :echo {string} ...
3239 case ISN_ECHO:
3240 {
3241 int count = iptr->isn_arg.echo.echo_count;
3242 int atstart = TRUE;
3243 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003244 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245
3246 for (idx = 0; idx < count; ++idx)
3247 {
3248 tv = STACK_TV_BOT(idx - count);
3249 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3250 &atstart, &needclr);
3251 clear_tv(tv);
3252 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003253 if (needclr)
3254 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003255 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 }
3257 break;
3258
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003259 // :execute {string} ...
3260 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003261 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003262 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003263 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003264 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003265 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003266 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003267 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003268 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003269 {
3270 int count = iptr->isn_arg.number;
3271 garray_T ga;
3272 char_u buf[NUMBUFLEN];
3273 char_u *p;
3274 int len;
3275 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003276 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003277
3278 ga_init2(&ga, 1, 80);
3279 for (idx = 0; idx < count; ++idx)
3280 {
3281 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003282 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003283 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003284 if (tv->v_type == VAR_CHANNEL
3285 || tv->v_type == VAR_JOB)
3286 {
3287 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003288 semsg(_(e_using_invalid_value_as_string_str),
3289 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003290 break;
3291 }
3292 else
3293 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003294 }
3295 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003296 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003297
3298 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003299 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003300 failed = TRUE;
3301 else
3302 {
3303 if (ga.ga_len > 0)
3304 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3305 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3306 ga.ga_len += len;
3307 }
3308 clear_tv(tv);
3309 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003310 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003311 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003312 {
3313 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003314 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003315 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003316
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003317 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003318 {
3319 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003320 {
3321 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003322 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003323 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003324 {
3325 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003326 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003327 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003328 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003329 else
3330 {
3331 msg_sb_eol();
3332 if (iptr->isn_type == ISN_ECHOMSG)
3333 {
3334 msg_attr(ga.ga_data, echo_attr);
3335 out_flush();
3336 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003337#ifdef HAS_MESSAGE_WINDOW
3338 else if (iptr->isn_type == ISN_ECHOWINDOW)
3339 {
3340 start_echowindow();
3341 msg_attr(ga.ga_data, echo_attr);
3342 end_echowindow();
3343 }
3344#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003345 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3346 {
3347 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3348 TRUE);
3349 ui_write((char_u *)"\r\n", 2, TRUE);
3350 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003351 else
3352 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003353 SOURCING_LNUM = iptr->isn_lnum;
3354 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003355 }
3356 }
3357 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003358 ga_clear(&ga);
3359 }
3360 break;
3361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 // load local variable or argument
3363 case ISN_LOAD:
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(STACK_TV_VAR(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
3370 // load v: variable
3371 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003372 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003373 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003375 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 break;
3377
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003378 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 case ISN_LOADSCRIPT:
3380 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003381 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 svar_T *sv;
3383
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003384 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003385 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003386 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003387 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003388 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003389 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003391 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 }
3393 break;
3394
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003395 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003397 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003399 int sid = iptr->isn_arg.loadstore.ls_sid;
3400 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003401 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003403
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 if (di == NULL)
3405 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003406 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003407 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003408 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 }
3410 else
3411 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003412 if (iptr->isn_type == ISN_LOADEXPORT)
3413 {
3414 int idx = get_script_item_idx(sid, name, 0,
3415 NULL, NULL);
3416 svar_T *sv;
3417
3418 if (idx >= 0)
3419 {
3420 sv = ((svar_T *)SCRIPT_ITEM(sid)
3421 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003422 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003423 {
3424 SOURCING_LNUM = iptr->isn_lnum;
3425 semsg(_(e_item_not_exported_in_script_str),
3426 name);
3427 goto on_error;
3428 }
3429 }
3430 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003431 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003432 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003434 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 }
3436 }
3437 break;
3438
Bram Moolenaard3aac292020-04-19 14:32:17 +02003439 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003441 case ISN_LOADB:
3442 case ISN_LOADW:
3443 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003445 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003446
Bram Moolenaar06b77222022-01-25 15:51:56 +00003447 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003448 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003449 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003450 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 break;
3454
Bram Moolenaar03290b82020-12-19 16:30:44 +01003455 // load autoload variable
3456 case ISN_LOADAUTO:
3457 {
3458 char_u *name = iptr->isn_arg.string;
3459
Bram Moolenaar35578162021-08-02 19:10:38 +02003460 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003461 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003462 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003463 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003464 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003465 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003466 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003467 }
3468 break;
3469
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003470 // load g:/b:/w:/t: namespace
3471 case ISN_LOADGDICT:
3472 case ISN_LOADBDICT:
3473 case ISN_LOADWDICT:
3474 case ISN_LOADTDICT:
3475 {
3476 dict_T *d = NULL;
3477
3478 switch (iptr->isn_type)
3479 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003480 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3481 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3482 case ISN_LOADWDICT: d = curwin->w_vars; break;
3483 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003484 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003485 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003486 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003487 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003488 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003489 tv = STACK_TV_BOT(0);
3490 tv->v_type = VAR_DICT;
3491 tv->v_lock = 0;
3492 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003493 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003494 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003495 }
3496 break;
3497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 // load &option
3499 case ISN_LOADOPT:
3500 {
3501 typval_T optval;
3502 char_u *name = iptr->isn_arg.string;
3503
Bram Moolenaara8c17702020-04-01 21:17:24 +02003504 // This is not expected to fail, name is checked during
3505 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003506 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003507 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003508 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003509 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003511 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 }
3513 break;
3514
3515 // load $ENV
3516 case ISN_LOADENV:
3517 {
3518 typval_T optval;
3519 char_u *name = iptr->isn_arg.string;
3520
Bram Moolenaar35578162021-08-02 19:10:38 +02003521 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003522 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003523 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003524 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003526 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 }
3528 break;
3529
3530 // load @register
3531 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003532 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003533 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 tv = STACK_TV_BOT(0);
3535 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003536 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003537 // This may result in NULL, which should be equivalent to an
3538 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 tv->vval.v_string = get_reg_contents(
3540 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003541 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 break;
3543
3544 // store local variable
3545 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003546 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 tv = STACK_TV_VAR(iptr->isn_arg.number);
3548 clear_tv(tv);
3549 *tv = *STACK_TV_BOT(0);
3550 break;
3551
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003552 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003553 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003554 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003555 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003556 int sid = iptr->isn_arg.loadstore.ls_sid;
3557 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003558 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003559 dictitem_T *di = find_var_in_ht(ht, 0,
3560 iptr->isn_type == ISN_STORES
3561 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003562
Bram Moolenaar4c137212021-04-19 16:48:48 +02003563 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003564 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003565 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003566 {
3567 if (iptr->isn_type == ISN_STOREEXPORT)
3568 {
3569 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003570 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003571 goto on_error;
3572 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003573 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003574 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003575 else
3576 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003577 if (iptr->isn_type == ISN_STOREEXPORT)
3578 {
3579 int idx = get_script_item_idx(sid, name, 0,
3580 NULL, NULL);
3581
Bram Moolenaar06651632022-04-27 17:54:25 +01003582 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003583 if (idx >= 0)
3584 {
3585 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3586 ->sn_var_vals.ga_data) + idx;
3587
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003588 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003589 {
3590 semsg(_(e_item_not_exported_in_script_str),
3591 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003592 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003593 goto on_error;
3594 }
3595 }
3596 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003597 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003598 {
3599 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003600 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003601 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003602 clear_tv(&di->di_tv);
3603 di->di_tv = *STACK_TV_BOT(0);
3604 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003605 }
3606 break;
3607
3608 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 case ISN_STORESCRIPT:
3610 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003611 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3612 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003614 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003615 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003616 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003617 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003618
3619 // "const" and "final" are checked at compile time, locking
3620 // the value needs to be checked here.
3621 SOURCING_LNUM = iptr->isn_lnum;
3622 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003623 {
3624 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003625 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003626 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 clear_tv(sv->sv_tv);
3629 *sv->sv_tv = *STACK_TV_BOT(0);
3630 }
3631 break;
3632
3633 // store option
3634 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003635 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003637 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3638 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 long n = 0;
3640 char_u *s = NULL;
3641 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003642 char_u numbuf[NUMBUFLEN];
3643 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644
Bram Moolenaar4c137212021-04-19 16:48:48 +02003645 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 tv = STACK_TV_BOT(0);
3647 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003648 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003650 if (s == NULL)
3651 s = (char_u *)"";
3652 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003653 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3654 {
3655 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003656 // If the option can be set to a function reference or
3657 // a lambda and the passed value is a function
3658 // reference, then convert it to the name (string) of
3659 // the function reference.
3660 s = tv2string(tv, &tofree, numbuf, 0);
3661 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003662 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003663 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003664 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003665 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003666 goto on_error;
3667 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003668 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003670 // must be VAR_NUMBER, CHECKTYPE makes sure
3671 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003672 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003673 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003674 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 if (msg != NULL)
3676 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003677 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003679 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 }
3682 break;
3683
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003684 // store $ENV
3685 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003686 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003687 tv = STACK_TV_BOT(0);
3688 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3689 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003690 break;
3691
3692 // store @r
3693 case ISN_STOREREG:
3694 {
3695 int reg = iptr->isn_arg.number;
3696
Bram Moolenaar4c137212021-04-19 16:48:48 +02003697 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003698 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003699 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003700 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003701 }
3702 break;
3703
3704 // store v: variable
3705 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003706 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003707 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3708 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003709 // should not happen, type is checked when compiling
3710 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003711 break;
3712
Bram Moolenaard3aac292020-04-19 14:32:17 +02003713 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003715 case ISN_STOREB:
3716 case ISN_STOREW:
3717 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003719 dictitem_T *di;
3720 hashtab_T *ht;
3721 char_u *name = iptr->isn_arg.string + 2;
3722
Bram Moolenaard3aac292020-04-19 14:32:17 +02003723 switch (iptr->isn_type)
3724 {
3725 case ISN_STOREG:
3726 ht = get_globvar_ht();
3727 break;
3728 case ISN_STOREB:
3729 ht = &curbuf->b_vars->dv_hashtab;
3730 break;
3731 case ISN_STOREW:
3732 ht = &curwin->w_vars->dv_hashtab;
3733 break;
3734 case ISN_STORET:
3735 ht = &curtab->tp_vars->dv_hashtab;
3736 break;
3737 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003738 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003739 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740
Bram Moolenaar4c137212021-04-19 16:48:48 +02003741 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003742 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003744 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 else
3746 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003747 SOURCING_LNUM = iptr->isn_lnum;
3748 if (var_check_permission(di, name) == FAIL)
3749 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 clear_tv(&di->di_tv);
3751 di->di_tv = *STACK_TV_BOT(0);
3752 }
3753 }
3754 break;
3755
Bram Moolenaar03290b82020-12-19 16:30:44 +01003756 // store an autoload variable
3757 case ISN_STOREAUTO:
3758 SOURCING_LNUM = iptr->isn_lnum;
3759 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3760 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003761 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003762 break;
3763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 // store number in local variable
3765 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003766 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 clear_tv(tv);
3768 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003769 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 break;
3771
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003772 // store value in list or dict variable
3773 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003774 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003775 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003776
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003777 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003778 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003779 if (res == NOTDONE)
3780 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003781 }
3782 break;
3783
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003784 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003785 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003786 if (execute_storerange(iptr, ectx) == FAIL)
3787 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003788 break;
3789
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003790 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003791 case ISN_LOADOUTER:
3792 case ISN_STOREOUTER:
3793 {
3794 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003795 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3796 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003797
3798 while (depth > 1 && outer != NULL)
3799 {
3800 outer = outer->out_up;
3801 --depth;
3802 }
3803 if (outer == NULL)
3804 {
3805 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003806 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3807 || ectx->ec_outer_ref == NULL)
3808 // Possibly :def function called from legacy
3809 // context.
3810 emsg(_(e_closure_called_from_invalid_context));
3811 else
3812 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003813 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003814 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003815 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003816 // Variable declared in loop. May be copied if the
3817 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01003818 tv = ((typval_T *)outer->out_loop[-depth - 1]
3819 .stack->ga_data)
3820 + outer->out_loop[-depth - 1].var_idx
3821 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003822 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003823 // Variable declared in a function. May be copied if
3824 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003825 tv = ((typval_T *)outer->out_stack->ga_data)
3826 + outer->out_frame_idx + STACK_FRAME_SIZE
3827 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003828 if (iptr->isn_type == ISN_LOADOUTER)
3829 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003830 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003831 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003832 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003833 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003834 }
3835 else
3836 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003837 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003838 clear_tv(tv);
3839 *tv = *STACK_TV_BOT(0);
3840 }
3841 }
3842 break;
3843
Bram Moolenaar752fc692021-01-04 21:57:11 +01003844 // unlet item in list or dict variable
3845 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003846 if (execute_unletindex(iptr, ectx) == FAIL)
3847 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003848 break;
3849
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003850 // unlet range of items in list variable
3851 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003852 if (execute_unletrange(iptr, ectx) == FAIL)
3853 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003854 break;
3855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 // push constant
3857 case ISN_PUSHNR:
3858 case ISN_PUSHBOOL:
3859 case ISN_PUSHSPEC:
3860 case ISN_PUSHF:
3861 case ISN_PUSHS:
3862 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003863 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003864 case ISN_PUSHCHANNEL:
3865 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003866 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003867 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003869 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003870 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 switch (iptr->isn_type)
3872 {
3873 case ISN_PUSHNR:
3874 tv->v_type = VAR_NUMBER;
3875 tv->vval.v_number = iptr->isn_arg.number;
3876 break;
3877 case ISN_PUSHBOOL:
3878 tv->v_type = VAR_BOOL;
3879 tv->vval.v_number = iptr->isn_arg.number;
3880 break;
3881 case ISN_PUSHSPEC:
3882 tv->v_type = VAR_SPECIAL;
3883 tv->vval.v_number = iptr->isn_arg.number;
3884 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 case ISN_PUSHF:
3886 tv->v_type = VAR_FLOAT;
3887 tv->vval.v_float = iptr->isn_arg.fnumber;
3888 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 case ISN_PUSHBLOB:
3890 blob_copy(iptr->isn_arg.blob, tv);
3891 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003892 case ISN_PUSHFUNC:
3893 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003894 if (iptr->isn_arg.string == NULL)
3895 tv->vval.v_string = NULL;
3896 else
3897 tv->vval.v_string =
3898 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003899 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003900 case ISN_PUSHCHANNEL:
3901#ifdef FEAT_JOB_CHANNEL
3902 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003903 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003904#endif
3905 break;
3906 case ISN_PUSHJOB:
3907#ifdef FEAT_JOB_CHANNEL
3908 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003909 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003910#endif
3911 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 default:
3913 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003914 tv->vval.v_string = iptr->isn_arg.string == NULL
3915 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 }
3917 break;
3918
Bram Moolenaar06b77222022-01-25 15:51:56 +00003919 case ISN_AUTOLOAD:
3920 {
3921 char_u *name = iptr->isn_arg.string;
3922
3923 (void)script_autoload(name, FALSE);
3924 if (find_func(name, TRUE))
3925 {
3926 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3927 goto theend;
3928 tv = STACK_TV_BOT(0);
3929 tv->v_lock = 0;
3930 ++ectx->ec_stack.ga_len;
3931 tv->v_type = VAR_FUNC;
3932 tv->vval.v_string = vim_strsave(name);
3933 }
3934 else
3935 {
3936 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3937
3938 if (res == NOTDONE)
3939 goto theend;
3940 if (res == FAIL)
3941 goto on_error;
3942 }
3943 }
3944 break;
3945
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003946 case ISN_UNLET:
3947 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3948 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003949 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003950 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003951 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003952 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003953 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003954
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003955 case ISN_LOCKUNLOCK:
3956 {
3957 typval_T *lval_root_save = lval_root;
3958 int res;
3959
3960 // Stack has the local variable, argument the whole :lock
3961 // or :unlock command, like ISN_EXEC.
3962 --ectx->ec_stack.ga_len;
3963 lval_root = STACK_TV_BOT(0);
3964 res = exec_command(iptr);
3965 clear_tv(lval_root);
3966 lval_root = lval_root_save;
3967 if (res == FAIL)
3968 goto on_error;
3969 }
3970 break;
3971
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003972 case ISN_LOCKCONST:
3973 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3974 break;
3975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 // create a list from items on the stack; uses a single allocation
3977 // for the list header and the items
3978 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003979 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003980 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 break;
3982
3983 // create a dict from items on the stack
3984 case ISN_NEWDICT:
3985 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003986 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003988 SOURCING_LNUM = iptr->isn_lnum;
3989 res = exe_newdict(iptr->isn_arg.number, ectx);
3990 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003991 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003992 if (res == MAYBE)
3993 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 }
3995 break;
3996
LemonBoy372bcce2022-04-25 12:43:20 +01003997 case ISN_CONCAT:
3998 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3999 goto theend;
4000 break;
4001
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004002 // create a partial with NULL value
4003 case ISN_NEWPARTIAL:
4004 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4005 goto theend;
4006 ++ectx->ec_stack.ga_len;
4007 tv = STACK_TV_BOT(-1);
4008 tv->v_type = VAR_PARTIAL;
4009 tv->v_lock = 0;
4010 tv->vval.v_partial = NULL;
4011 break;
4012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 // call a :def function
4014 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004015 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004016 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4017 NULL,
4018 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004019 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004020 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 break;
4022
4023 // call a builtin function
4024 case ISN_BCALL:
4025 SOURCING_LNUM = iptr->isn_lnum;
4026 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4027 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004028 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004029 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 break;
4031
4032 // call a funcref or partial
4033 case ISN_PCALL:
4034 {
4035 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4036 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004037 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038
4039 SOURCING_LNUM = iptr->isn_lnum;
4040 if (pfunc->cpf_top)
4041 {
4042 // funcref is above the arguments
4043 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4044 }
4045 else
4046 {
4047 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004048 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004049 partial_tv = *STACK_TV_BOT(0);
4050 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004052 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004053 if (tv == &partial_tv)
4054 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004056 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 }
4058 break;
4059
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004060 case ISN_PCALL_END:
4061 // PCALL finished, arguments have been consumed and replaced by
4062 // the return value. Now clear the funcref from the stack,
4063 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004064 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004065 clear_tv(STACK_TV_BOT(-1));
4066 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4067 break;
4068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 // call a user defined function or funcref/partial
4070 case ISN_UCALL:
4071 {
4072 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4073
4074 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004075 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004076 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004077 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 }
4079 break;
4080
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004081 // :defer func(arg)
4082 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004083 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004084 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4085 goto on_error;
4086 break;
4087
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004088 // return from a :def function call without a value
4089 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02004090 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004091 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004092 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004093 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004094 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004095 tv->vval.v_number = 0;
4096 tv->v_lock = 0;
4097 // FALLTHROUGH
4098
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004099 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 case ISN_RETURN:
4101 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004102 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004103 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004104
4105 if (trystack->ga_len > 0)
4106 trycmd = ((trycmd_T *)trystack->ga_data)
4107 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004108 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004109 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004111 // jump to ":finally" or ":endtry"
4112 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004113 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004114 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004115 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 trycmd->tcd_return = TRUE;
4117 }
4118 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004119 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 }
4121 break;
4122
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004123 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 case ISN_FUNCREF:
4125 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004126 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4127 ufunc_T *ufunc;
4128 funcref_T *funcref = &iptr->isn_arg.funcref;
4129 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004132 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004133 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004134 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004135 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004136 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004137 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004138 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004139 {
4140 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4141 + funcref->fr_dfunc_idx;
4142
4143 ufunc = pt_dfunc->df_ufunc;
4144 }
4145 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004146 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004147 if (ufunc == NULL)
4148 {
4149 SOURCING_LNUM = iptr->isn_lnum;
4150 iemsg("ufunc unexpectedly NULL for FUNCREF");
4151 goto theend;
4152 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004153 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004154 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004155 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004156 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004158 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 tv->vval.v_partial = pt;
4160 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004161 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 }
4163 break;
4164
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004165 // Create a global function from a lambda.
4166 case ISN_NEWFUNC:
4167 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004168 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004169
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004170 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004171 arg->nfa_global, &arg->nfa_loopvar_info,
4172 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004173 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004174 }
4175 break;
4176
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004177 // List functions
4178 case ISN_DEF:
4179 if (iptr->isn_arg.string == NULL)
4180 list_functions(NULL);
4181 else
4182 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004183 exarg_T ea;
4184 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004185
4186 CLEAR_FIELD(ea);
4187 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004188 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4189 define_function(&ea, NULL, &lines_to_free);
4190 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004191 }
4192 break;
4193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 // jump if a condition is met
4195 case ISN_JUMP:
4196 {
4197 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004198 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 int jump = TRUE;
4200
4201 if (when != JUMP_ALWAYS)
4202 {
4203 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004204 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004205 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004206 || when == JUMP_IF_COND_TRUE)
4207 {
4208 SOURCING_LNUM = iptr->isn_lnum;
4209 jump = tv_get_bool_chk(tv, &error);
4210 if (error)
4211 goto on_error;
4212 }
4213 else
4214 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004215 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004217 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 {
4219 // drop the value from the stack
4220 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004221 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 }
4223 }
4224 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004225 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 }
4227 break;
4228
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004229 // "while": jump to end if a condition is false
4230 case ISN_WHILE:
4231 {
4232 int error = FALSE;
4233 int jump = TRUE;
4234
4235 tv = STACK_TV_BOT(-1);
4236 SOURCING_LNUM = iptr->isn_lnum;
4237 jump = !tv_get_bool_chk(tv, &error);
4238 if (error)
4239 goto on_error;
4240 // drop the value from the stack
4241 clear_tv(tv);
4242 --ectx->ec_stack.ga_len;
4243 if (jump)
4244 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4245
4246 // Store the current funccal count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004247 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004248 tv = STACK_TV_VAR(
4249 iptr->isn_arg.whileloop.while_funcref_idx);
4250 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4251 }
4252 break;
4253
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004254 // Jump if an argument with a default value was already set and not
4255 // v:none.
4256 case ISN_JUMP_IF_ARG_SET:
4257 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
4258 if (tv->v_type != VAR_UNKNOWN
4259 && !(tv->v_type == VAR_SPECIAL
4260 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004261 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004262 break;
4263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 // top of a for loop
4265 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004266 if (execute_for(iptr, ectx) == FAIL)
4267 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 break;
4269
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004270 // end of a for or while loop
4271 case ISN_ENDLOOP:
4272 if (execute_endloop(iptr, ectx) == FAIL)
4273 goto theend;
4274 break;
4275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 // start of ":try" block
4277 case ISN_TRY:
4278 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004279 trycmd_T *trycmd = NULL;
4280
Bram Moolenaar35578162021-08-02 19:10:38 +02004281 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004282 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004283 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4284 + ectx->ec_trystack.ga_len;
4285 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004287 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004288 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4289 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004290 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004291 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004292 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004293 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004294 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004295 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 }
4297 break;
4298
4299 case ISN_PUSHEXC:
4300 if (current_exception == NULL)
4301 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004302 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004304 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004306 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004307 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004309 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004311 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 tv->vval.v_string = vim_strsave(
4313 (char_u *)current_exception->value);
4314 break;
4315
4316 case ISN_CATCH:
4317 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004318 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004319 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320
Bram Moolenaar4c137212021-04-19 16:48:48 +02004321 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004322 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004324 trycmd->tcd_caught = TRUE;
4325 trycmd->tcd_did_throw = FALSE;
4326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004328 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 catch_exception(current_exception);
4330 }
4331 break;
4332
Bram Moolenaarc150c092021-02-13 15:02:46 +01004333 case ISN_TRYCONT:
4334 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004335 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004336 trycont_T *trycont = &iptr->isn_arg.trycont;
4337 int i;
4338 trycmd_T *trycmd;
4339 int iidx = trycont->tct_where;
4340
4341 if (trystack->ga_len < trycont->tct_levels)
4342 {
4343 siemsg("TRYCONT: expected %d levels, found %d",
4344 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004345 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004346 }
4347 // Make :endtry jump to any outer try block and the last
4348 // :endtry inside the loop to the loop start.
4349 for (i = trycont->tct_levels; i > 0; --i)
4350 {
4351 trycmd = ((trycmd_T *)trystack->ga_data)
4352 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004353 // Add one to tcd_cont to be able to jump to
4354 // instruction with index zero.
4355 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004356 iidx = trycmd->tcd_finally_idx == 0
4357 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004358 }
4359 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004360 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004361 }
4362 break;
4363
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004364 case ISN_FINALLY:
4365 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004366 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004367 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4368 + trystack->ga_len - 1;
4369
4370 // Reset the index to avoid a return statement jumps here
4371 // again.
4372 trycmd->tcd_finally_idx = 0;
4373 break;
4374 }
4375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 // end of ":try" block
4377 case ISN_ENDTRY:
4378 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004379 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004380 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381
Bram Moolenaar06651632022-04-27 17:54:25 +01004382 --trystack->ga_len;
4383 --trylevel;
4384 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4385 if (trycmd->tcd_did_throw)
4386 did_throw = TRUE;
4387 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004389 // discard the exception
4390 if (caught_stack == current_exception)
4391 caught_stack = caught_stack->caught;
4392 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004394
4395 if (trycmd->tcd_return)
4396 goto func_return;
4397
4398 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4399 {
4400 --ectx->ec_stack.ga_len;
4401 clear_tv(STACK_TV_BOT(0));
4402 }
4403 if (trycmd->tcd_cont != 0)
4404 // handling :continue: jump to outer try block or
4405 // start of the loop
4406 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 }
4408 break;
4409
4410 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004411 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004412 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004413
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004414 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4415 {
4416 // throwing an exception while using "silent!" causes
4417 // the function to abort but not display an error.
4418 tv = STACK_TV_BOT(-1);
4419 clear_tv(tv);
4420 tv->v_type = VAR_NUMBER;
4421 tv->vval.v_number = 0;
4422 goto done;
4423 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004424 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004425 tv = STACK_TV_BOT(0);
4426 if (tv->vval.v_string == NULL
4427 || *skipwhite(tv->vval.v_string) == NUL)
4428 {
4429 vim_free(tv->vval.v_string);
4430 SOURCING_LNUM = iptr->isn_lnum;
4431 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004432 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004433 }
4434
4435 // Inside a "catch" we need to first discard the caught
4436 // exception.
4437 if (trystack->ga_len > 0)
4438 {
4439 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4440 + trystack->ga_len - 1;
4441 if (trycmd->tcd_caught && current_exception != NULL)
4442 {
4443 // discard the exception
4444 if (caught_stack == current_exception)
4445 caught_stack = caught_stack->caught;
4446 discard_current_exception();
4447 trycmd->tcd_caught = FALSE;
4448 }
4449 }
4450
Bram Moolenaar90a57162022-02-12 14:23:17 +00004451 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004452 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4453 == FAIL)
4454 {
4455 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004456 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004457 }
4458 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 break;
4461
4462 // compare with special values
4463 case ISN_COMPAREBOOL:
4464 case ISN_COMPARESPECIAL:
4465 {
4466 typval_T *tv1 = STACK_TV_BOT(-2);
4467 typval_T *tv2 = STACK_TV_BOT(-1);
4468 varnumber_T arg1 = tv1->vval.v_number;
4469 varnumber_T arg2 = tv2->vval.v_number;
4470 int res;
4471
Bram Moolenaar06651632022-04-27 17:54:25 +01004472 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4473 res = arg1 == arg2;
4474 else
4475 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476
Bram Moolenaar4c137212021-04-19 16:48:48 +02004477 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 tv1->v_type = VAR_BOOL;
4479 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4480 }
4481 break;
4482
Bram Moolenaar7a222242022-03-01 19:23:24 +00004483 case ISN_COMPARENULL:
4484 {
4485 typval_T *tv1 = STACK_TV_BOT(-2);
4486 typval_T *tv2 = STACK_TV_BOT(-1);
4487 int res;
4488
4489 res = typval_compare_null(tv1, tv2);
4490 if (res == MAYBE)
4491 goto on_error;
4492 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4493 res = !res;
4494 clear_tv(tv1);
4495 clear_tv(tv2);
4496 --ectx->ec_stack.ga_len;
4497 tv1->v_type = VAR_BOOL;
4498 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4499 }
4500 break;
4501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 // Operation with two number arguments
4503 case ISN_OPNR:
4504 case ISN_COMPARENR:
4505 {
4506 typval_T *tv1 = STACK_TV_BOT(-2);
4507 typval_T *tv2 = STACK_TV_BOT(-1);
4508 varnumber_T arg1 = tv1->vval.v_number;
4509 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004510 varnumber_T res = 0;
4511 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004513 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4514 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4515 {
4516 if (arg2 < 0)
4517 {
4518 SOURCING_LNUM = iptr->isn_lnum;
4519 emsg(_(e_bitshift_ops_must_be_postive));
4520 goto on_error;
4521 }
4522 }
4523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 switch (iptr->isn_arg.op.op_type)
4525 {
4526 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004527 case EXPR_DIV: if (arg2 == 0)
4528 div_zero = TRUE;
4529 else
4530 res = arg1 / arg2;
4531 break;
4532 case EXPR_REM: if (arg2 == 0)
4533 div_zero = TRUE;
4534 else
4535 res = arg1 % arg2;
4536 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 case EXPR_SUB: res = arg1 - arg2; break;
4538 case EXPR_ADD: res = arg1 + arg2; break;
4539
4540 case EXPR_EQUAL: res = arg1 == arg2; break;
4541 case EXPR_NEQUAL: res = arg1 != arg2; break;
4542 case EXPR_GREATER: res = arg1 > arg2; break;
4543 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4544 case EXPR_SMALLER: res = arg1 < arg2; break;
4545 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004546 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4547 res = 0;
4548 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004549 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004550 break;
4551 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4552 res = 0;
4553 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004554 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004555 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004556 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 }
4558
Bram Moolenaar4c137212021-04-19 16:48:48 +02004559 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 if (iptr->isn_type == ISN_COMPARENR)
4561 {
4562 tv1->v_type = VAR_BOOL;
4563 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4564 }
4565 else
4566 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004567 if (div_zero)
4568 {
4569 SOURCING_LNUM = iptr->isn_lnum;
4570 emsg(_(e_divide_by_zero));
4571 goto on_error;
4572 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 }
4574 break;
4575
4576 // Computation with two float arguments
4577 case ISN_OPFLOAT:
4578 case ISN_COMPAREFLOAT:
4579 {
4580 typval_T *tv1 = STACK_TV_BOT(-2);
4581 typval_T *tv2 = STACK_TV_BOT(-1);
4582 float_T arg1 = tv1->vval.v_float;
4583 float_T arg2 = tv2->vval.v_float;
4584 float_T res = 0;
4585 int cmp = FALSE;
4586
4587 switch (iptr->isn_arg.op.op_type)
4588 {
4589 case EXPR_MULT: res = arg1 * arg2; break;
4590 case EXPR_DIV: res = arg1 / arg2; break;
4591 case EXPR_SUB: res = arg1 - arg2; break;
4592 case EXPR_ADD: res = arg1 + arg2; break;
4593
4594 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4595 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4596 case EXPR_GREATER: cmp = arg1 > arg2; break;
4597 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4598 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4599 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4600 default: cmp = 0; break;
4601 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004602 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 if (iptr->isn_type == ISN_COMPAREFLOAT)
4604 {
4605 tv1->v_type = VAR_BOOL;
4606 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4607 }
4608 else
4609 tv1->vval.v_float = res;
4610 }
4611 break;
4612
4613 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004614 case ISN_COMPAREDICT:
4615 case ISN_COMPAREFUNC:
4616 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 case ISN_COMPAREBLOB:
4618 {
4619 typval_T *tv1 = STACK_TV_BOT(-2);
4620 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004621 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4622 int ic = iptr->isn_arg.op.op_ic;
4623 int res = FALSE;
4624 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625
Bram Moolenaar265f8112021-12-19 12:33:05 +00004626 SOURCING_LNUM = iptr->isn_lnum;
4627 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004629 status = typval_compare_list(tv1, tv2,
4630 exprtype, ic, &res);
4631 }
4632 else if (iptr->isn_type == ISN_COMPAREDICT)
4633 {
4634 status = typval_compare_dict(tv1, tv2,
4635 exprtype, ic, &res);
4636 }
4637 else if (iptr->isn_type == ISN_COMPAREFUNC)
4638 {
4639 status = typval_compare_func(tv1, tv2,
4640 exprtype, ic, &res);
4641 }
4642 else if (iptr->isn_type == ISN_COMPARESTRING)
4643 {
4644 status = typval_compare_string(tv1, tv2,
4645 exprtype, ic, &res);
4646 }
4647 else
4648 {
4649 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004651 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 clear_tv(tv1);
4653 clear_tv(tv2);
4654 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004655 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4656 if (status == FAIL)
4657 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 }
4659 break;
4660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 case ISN_COMPAREANY:
4662 {
4663 typval_T *tv1 = STACK_TV_BOT(-2);
4664 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004665 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004667 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668
Bram Moolenaareb26f432020-09-14 16:50:05 +02004669 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004670 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004672 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004673 if (status == FAIL)
4674 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 }
4676 break;
4677
4678 case ISN_ADDLIST:
4679 case ISN_ADDBLOB:
4680 {
4681 typval_T *tv1 = STACK_TV_BOT(-2);
4682 typval_T *tv2 = STACK_TV_BOT(-1);
4683
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004684 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004686 {
4687 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4688 && tv1->vval.v_list != NULL)
4689 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4690 NULL);
4691 else
4692 eval_addlist(tv1, tv2);
4693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 else
4695 eval_addblob(tv1, tv2);
4696 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004697 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 }
4699 break;
4700
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004701 case ISN_LISTAPPEND:
4702 {
4703 typval_T *tv1 = STACK_TV_BOT(-2);
4704 typval_T *tv2 = STACK_TV_BOT(-1);
4705 list_T *l = tv1->vval.v_list;
4706
4707 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004708 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004709 if (l == NULL)
4710 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004711 emsg(_(e_cannot_add_to_null_list));
4712 goto on_error;
4713 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004714 if (value_check_lock(l->lv_lock, NULL, FALSE))
4715 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004716 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004717 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004718 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004719 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004720 }
4721 break;
4722
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004723 case ISN_BLOBAPPEND:
4724 {
4725 typval_T *tv1 = STACK_TV_BOT(-2);
4726 typval_T *tv2 = STACK_TV_BOT(-1);
4727 blob_T *b = tv1->vval.v_blob;
4728 int error = FALSE;
4729 varnumber_T n;
4730
4731 // add a number to a blob
4732 if (b == NULL)
4733 {
4734 SOURCING_LNUM = iptr->isn_lnum;
4735 emsg(_(e_cannot_add_to_null_blob));
4736 goto on_error;
4737 }
4738 n = tv_get_number_chk(tv2, &error);
4739 if (error)
4740 goto on_error;
4741 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004742 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004743 }
4744 break;
4745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 // Computation with two arguments of unknown type
4747 case ISN_OPANY:
4748 {
4749 typval_T *tv1 = STACK_TV_BOT(-2);
4750 typval_T *tv2 = STACK_TV_BOT(-1);
4751 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 int error = FALSE;
4754
4755 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4756 {
4757 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4758 {
4759 eval_addlist(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 else if (tv1->v_type == VAR_BLOB
4765 && tv2->v_type == VAR_BLOB)
4766 {
4767 eval_addblob(tv1, tv2);
4768 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004769 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 break;
4771 }
4772 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 if (tv1->v_type == VAR_FLOAT)
4774 {
4775 f1 = tv1->vval.v_float;
4776 n1 = 0;
4777 }
4778 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004780 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781 n1 = tv_get_number_chk(tv1, &error);
4782 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004783 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 if (tv2->v_type == VAR_FLOAT)
4785 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 if (tv2->v_type == VAR_FLOAT)
4788 {
4789 f2 = tv2->vval.v_float;
4790 n2 = 0;
4791 }
4792 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 {
4794 n2 = tv_get_number_chk(tv2, &error);
4795 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004796 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 if (tv1->v_type == VAR_FLOAT)
4798 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 // if there is a float on either side the result is a float
4801 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4802 {
4803 switch (iptr->isn_arg.op.op_type)
4804 {
4805 case EXPR_MULT: f1 = f1 * f2; break;
4806 case EXPR_DIV: f1 = f1 / f2; break;
4807 case EXPR_SUB: f1 = f1 - f2; break;
4808 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004809 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004810 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004811 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 }
4813 clear_tv(tv1);
4814 clear_tv(tv2);
4815 tv1->v_type = VAR_FLOAT;
4816 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004817 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004818 }
4819 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004821 int failed = FALSE;
4822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 switch (iptr->isn_arg.op.op_type)
4824 {
4825 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004826 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4827 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004828 goto on_error;
4829 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 case EXPR_SUB: n1 = n1 - n2; break;
4831 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004832 default: n1 = num_modulus(n1, n2, &failed);
4833 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004834 goto on_error;
4835 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836 }
4837 clear_tv(tv1);
4838 clear_tv(tv2);
4839 tv1->v_type = VAR_NUMBER;
4840 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004841 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 }
4843 }
4844 break;
4845
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004846 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004847 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004848 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004849 int is_slice = iptr->isn_type == ISN_STRSLICE;
4850 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004851 char_u *res;
4852
4853 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004854 // string slice: string is at stack-3, first index at
4855 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004856 if (is_slice)
4857 {
4858 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004859 n1 = tv->vval.v_number;
4860 }
4861
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004862 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004863 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004864
Bram Moolenaar4c137212021-04-19 16:48:48 +02004865 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004866 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004867 if (is_slice)
4868 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004869 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004870 else
4871 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004872 // single character (including composing characters).
4873 // If the index is too big or negative the result is
4874 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004875 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004876 vim_free(tv->vval.v_string);
4877 tv->vval.v_string = res;
4878 }
4879 break;
4880
4881 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004882 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004883 case ISN_BLOBINDEX:
4884 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004886 int is_slice = iptr->isn_type == ISN_LISTSLICE
4887 || iptr->isn_type == ISN_BLOBSLICE;
4888 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4889 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004890 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004891 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892
4893 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004894 // list slice: list is at stack-3, indexes at stack-2 and
4895 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004896 // Same for blob.
4897 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898
4899 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004900 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004902
4903 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 {
Bram Moolenaared591872020-08-15 22:14:53 +02004905 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004906 n1 = tv->vval.v_number;
4907 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 }
Bram Moolenaared591872020-08-15 22:14:53 +02004909
Bram Moolenaar4c137212021-04-19 16:48:48 +02004910 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004911 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004912 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004913 if (is_blob)
4914 {
4915 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4916 n1, n2, FALSE, tv) == FAIL)
4917 goto on_error;
4918 }
4919 else
4920 {
4921 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4922 n1, n2, FALSE, tv, TRUE) == FAIL)
4923 goto on_error;
4924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 }
4926 break;
4927
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004928 case ISN_ANYINDEX:
4929 case ISN_ANYSLICE:
4930 {
4931 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4932 typval_T *var1, *var2;
4933 int res;
4934
4935 // index: composite is at stack-2, index at stack-1
4936 // slice: composite is at stack-3, indexes at stack-2 and
4937 // stack-1
4938 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004939 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004940 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4941 goto on_error;
4942 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4943 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004944 res = eval_index_inner(tv, is_slice, var1, var2,
4945 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004946 clear_tv(var1);
4947 if (is_slice)
4948 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004949 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004950 if (res == FAIL)
4951 goto on_error;
4952 }
4953 break;
4954
Bram Moolenaar9af78762020-06-16 11:34:42 +02004955 case ISN_SLICE:
4956 {
4957 list_T *list;
4958 int count = iptr->isn_arg.number;
4959
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004960 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004961 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004962 list = tv->vval.v_list;
4963
4964 // no error for short list, expect it to be checked earlier
4965 if (list != NULL && list->lv_len >= count)
4966 {
4967 list_T *newlist = list_slice(list,
4968 count, list->lv_len - 1);
4969
4970 if (newlist != NULL)
4971 {
4972 list_unref(list);
4973 tv->vval.v_list = newlist;
4974 ++newlist->lv_refcount;
4975 }
4976 }
4977 }
4978 break;
4979
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004980 case ISN_GETITEM:
4981 {
4982 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004983 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004984
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004985 // Get list item: list is at stack-1, push item.
4986 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004987 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4988 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004989
Bram Moolenaar35578162021-08-02 19:10:38 +02004990 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004991 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004992 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004993 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004994
4995 // Useful when used in unpack assignment. Reset at
4996 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004997 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004998 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004999 }
5000 break;
5001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 case ISN_MEMBER:
5003 {
5004 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005005 char_u *key;
5006 dictitem_T *di;
5007
5008 // dict member: dict is at stack-2, key at stack-1
5009 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005010 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005011 dict = tv->vval.v_dict;
5012
5013 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005014 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005015 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005016 if (key == NULL)
5017 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005018
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005019 if ((di = dict_find(dict, key, -1)) == NULL)
5020 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005021 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005022 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005023
5024 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005025 // stack contents makes sense and the dict stack is
5026 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005027 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005028 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005029 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005030 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005031 tv->v_type = VAR_NUMBER;
5032 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005033 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005034 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005035 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005036 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005037 // Put the dict used on the dict stack, it might be used by
5038 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005039 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005040 if (dict_stack_save(tv) == FAIL)
5041 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005042 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005043 }
5044 break;
5045
5046 // dict member with string key
5047 case ISN_STRINGMEMBER:
5048 {
5049 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 dictitem_T *di;
5051
5052 tv = STACK_TV_BOT(-1);
5053 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5054 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005055 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005056 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005057 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 }
5059 dict = tv->vval.v_dict;
5060
5061 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5062 == NULL)
5063 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005064 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00005065 semsg(_(e_key_not_present_in_dictionary),
5066 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005067 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005069 // Put the dict used on the dict stack, it might be used by
5070 // a dict function later.
5071 if (dict_stack_save(tv) == FAIL)
5072 goto on_fatal_error;
5073
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005074 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005075 }
5076 break;
5077
5078 case ISN_CLEARDICT:
5079 dict_stack_drop();
5080 break;
5081
5082 case ISN_USEDICT:
5083 {
5084 typval_T *dict_tv = dict_stack_get_tv();
5085
5086 // Turn "dict.Func" into a partial for "Func" bound to
5087 // "dict". Don't do this when "Func" is already a partial
5088 // that was bound explicitly (pt_auto is FALSE).
5089 tv = STACK_TV_BOT(-1);
5090 if (dict_tv != NULL
5091 && dict_tv->v_type == VAR_DICT
5092 && dict_tv->vval.v_dict != NULL
5093 && (tv->v_type == VAR_FUNC
5094 || (tv->v_type == VAR_PARTIAL
5095 && (tv->vval.v_partial->pt_auto
5096 || tv->vval.v_partial->pt_dict == NULL))))
5097 dict_tv->vval.v_dict =
5098 make_partial(dict_tv->vval.v_dict, tv);
5099 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 }
5101 break;
5102
5103 case ISN_NEGATENR:
5104 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005105 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005106 if (tv->v_type == VAR_FLOAT)
5107 tv->vval.v_float = -tv->vval.v_float;
5108 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005109 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 break;
5111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 case ISN_CHECKTYPE:
5113 {
5114 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005115 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005116 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005118 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005119 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005120 if (!ectx->ec_where.wt_variable)
5121 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005122 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005123 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005124 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005125 if (r == FAIL)
5126 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005127 if (!ectx->ec_where.wt_variable)
5128 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005129
5130 // number 0 is FALSE, number 1 is TRUE
5131 if (tv->v_type == VAR_NUMBER
5132 && ct->ct_type->tt_type == VAR_BOOL
5133 && (tv->vval.v_number == 0
5134 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005135 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005136 tv->v_type = VAR_BOOL;
5137 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005138 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 }
5140 }
5141 break;
5142
Bram Moolenaar9af78762020-06-16 11:34:42 +02005143 case ISN_CHECKLEN:
5144 {
5145 int min_len = iptr->isn_arg.checklen.cl_min_len;
5146 list_T *list = NULL;
5147
5148 tv = STACK_TV_BOT(-1);
5149 if (tv->v_type == VAR_LIST)
5150 list = tv->vval.v_list;
5151 if (list == NULL || list->lv_len < min_len
5152 || (list->lv_len > min_len
5153 && !iptr->isn_arg.checklen.cl_more_OK))
5154 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005155 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005156 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005157 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005158 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005159 }
5160 }
5161 break;
5162
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005163 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005164 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005165 break;
5166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005168 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005169 {
5170 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005171 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005173 if (iptr->isn_type == ISN_2BOOL)
5174 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005175 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005176 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005177 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005178 n = !n;
5179 }
5180 else
5181 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005182 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005183 SOURCING_LNUM = iptr->isn_lnum;
5184 n = tv_get_bool_chk(tv, &error);
5185 if (error)
5186 goto on_error;
5187 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005188 clear_tv(tv);
5189 tv->v_type = VAR_BOOL;
5190 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5191 }
5192 break;
5193
5194 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005195 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005196 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005197 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5198 iptr->isn_type == ISN_2STRING_ANY,
5199 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005200 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201 break;
5202
Bram Moolenaar08597872020-12-10 19:43:40 +01005203 case ISN_RANGE:
5204 {
5205 exarg_T ea;
5206 char *errormsg;
5207
Bram Moolenaarece0b872021-01-08 20:40:45 +01005208 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005209 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005210 ea.addr_type = ADDR_LINES;
5211 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005212 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005213 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005214 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005215
Bram Moolenaar35578162021-08-02 19:10:38 +02005216 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005217 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005218 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005219 tv = STACK_TV_BOT(-1);
5220 tv->v_type = VAR_NUMBER;
5221 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005222 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005223 }
5224 break;
5225
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005226 case ISN_PUT:
5227 {
5228 int regname = iptr->isn_arg.put.put_regname;
5229 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5230 char_u *expr = NULL;
5231 int dir = FORWARD;
5232
Bram Moolenaar08597872020-12-10 19:43:40 +01005233 if (lnum < -2)
5234 {
5235 // line number was put on the stack by ISN_RANGE
5236 tv = STACK_TV_BOT(-1);
5237 curwin->w_cursor.lnum = tv->vval.v_number;
5238 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5239 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005240 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005241 }
5242 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005243 // :put! above cursor
5244 dir = BACKWARD;
5245 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005246 {
5247 curwin->w_cursor.lnum = lnum;
5248 if (lnum == 0)
5249 // check_cursor() below will move to line 1
5250 dir = BACKWARD;
5251 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005252
5253 if (regname == '=')
5254 {
5255 tv = STACK_TV_BOT(-1);
5256 if (tv->v_type == VAR_STRING)
5257 expr = tv->vval.v_string;
5258 else
5259 {
5260 expr = typval2string(tv, TRUE); // allocates value
5261 clear_tv(tv);
5262 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005263 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005264 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005265 check_cursor();
5266 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5267 vim_free(expr);
5268 }
5269 break;
5270
Bram Moolenaar02194d22020-10-24 23:08:38 +02005271 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005272 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5273 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5274 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5275 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005276 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5277 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005278 break;
5279
Bram Moolenaar02194d22020-10-24 23:08:38 +02005280 case ISN_CMDMOD_REV:
5281 // filter regprog is owned by the instruction, don't free it
5282 cmdmod.cmod_filter_regmatch.regprog = NULL;
5283 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005284 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5285 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005286 break;
5287
Bram Moolenaar792f7862020-11-23 08:31:18 +01005288 case ISN_UNPACK:
5289 {
5290 int count = iptr->isn_arg.unpack.unp_count;
5291 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5292 list_T *l;
5293 listitem_T *li;
5294 int i;
5295
5296 // Check there is a valid list to unpack.
5297 tv = STACK_TV_BOT(-1);
5298 if (tv->v_type != VAR_LIST)
5299 {
5300 SOURCING_LNUM = iptr->isn_lnum;
5301 emsg(_(e_for_argument_must_be_sequence_of_lists));
5302 goto on_error;
5303 }
5304 l = tv->vval.v_list;
5305 if (l == NULL
5306 || l->lv_len < (semicolon ? count - 1 : count))
5307 {
5308 SOURCING_LNUM = iptr->isn_lnum;
5309 emsg(_(e_list_value_does_not_have_enough_items));
5310 goto on_error;
5311 }
5312 else if (!semicolon && l->lv_len > count)
5313 {
5314 SOURCING_LNUM = iptr->isn_lnum;
5315 emsg(_(e_list_value_has_more_items_than_targets));
5316 goto on_error;
5317 }
5318
5319 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005320 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005321 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005322 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005323
5324 // Variable after semicolon gets a list with the remaining
5325 // items.
5326 if (semicolon)
5327 {
5328 list_T *rem_list =
5329 list_alloc_with_items(l->lv_len - count + 1);
5330
5331 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005332 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005333 tv = STACK_TV_BOT(-count);
5334 tv->vval.v_list = rem_list;
5335 ++rem_list->lv_refcount;
5336 tv->v_lock = 0;
5337 li = l->lv_first;
5338 for (i = 0; i < count - 1; ++i)
5339 li = li->li_next;
5340 for (i = 0; li != NULL; ++i)
5341 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005342 typval_T tvcopy;
5343
5344 copy_tv(&li->li_tv, &tvcopy);
5345 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005346 li = li->li_next;
5347 }
5348 --count;
5349 }
5350
5351 // Produce the values in reverse order, first item last.
5352 li = l->lv_first;
5353 for (i = 0; i < count; ++i)
5354 {
5355 tv = STACK_TV_BOT(-i - 1);
5356 copy_tv(&li->li_tv, tv);
5357 li = li->li_next;
5358 }
5359
5360 list_unref(l);
5361 }
5362 break;
5363
Bram Moolenaarb2049902021-01-24 12:53:53 +01005364 case ISN_PROF_START:
5365 case ISN_PROF_END:
5366 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005367#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005368 funccall_T cookie;
5369 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005370 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005371 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005372
Bram Moolenaarca16c602022-09-06 18:57:08 +01005373 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005374 if (iptr->isn_type == ISN_PROF_START)
5375 {
5376 func_line_start(&cookie, iptr->isn_lnum);
5377 // if we get here the instruction is executed
5378 func_line_exec(&cookie);
5379 }
5380 else
5381 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005382#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005383 }
5384 break;
5385
Bram Moolenaare99d4222021-06-13 14:01:26 +02005386 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005387 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005388 break;
5389
Bram Moolenaar389df252020-07-09 21:20:47 +02005390 case ISN_SHUFFLE:
5391 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005392 typval_T tmp_tv;
5393 int item = iptr->isn_arg.shuffle.shfl_item;
5394 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005395
5396 tmp_tv = *STACK_TV_BOT(-item);
5397 for ( ; up > 0 && item > 1; --up)
5398 {
5399 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5400 --item;
5401 }
5402 *STACK_TV_BOT(-item) = tmp_tv;
5403 }
5404 break;
5405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005406 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005407 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005408 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005409 ectx->ec_where.wt_index = 0;
5410 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005411 break;
5412 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005413 continue;
5414
Bram Moolenaard032f342020-07-18 18:13:02 +02005415func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005416 // Restore previous function. If the frame pointer is where we started
5417 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005418 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005419 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005420
Bram Moolenaar4c137212021-04-19 16:48:48 +02005421 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005422 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005423 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005424 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005425
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005426on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005427 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005428 // If "emsg_silent" is set then ignore the error, unless it was set
5429 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005430 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005431 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005432 {
5433 // If a sequence of instructions causes an error while ":silent!"
5434 // was used, restore the stack length and jump ahead to restoring
5435 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005436 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005437 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005438 while (ectx->ec_stack.ga_len
5439 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005440 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005441 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005442 clear_tv(STACK_TV_BOT(0));
5443 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005444 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5445 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005446 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005447 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005448 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005449on_fatal_error:
5450 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005451 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005452 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005453 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454 }
5455
5456done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005457 ret = OK;
5458theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005459 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005460
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005461 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005462 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5463 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005464}
5465
5466/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005467 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005468 * "rettv".
5469 * Return OK or FAIL.
5470 */
5471 int
5472exe_typval_instr(typval_T *tv, typval_T *rettv)
5473{
5474 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5475 isn_T *save_instr = ectx->ec_instr;
5476 int save_iidx = ectx->ec_iidx;
5477 int res;
5478
LemonBoyf3b48952022-05-05 13:53:03 +01005479 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5480 // even when the compilation fails.
5481 rettv->v_type = VAR_UNKNOWN;
5482
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005483 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5484 res = exec_instructions(ectx);
5485 if (res == OK)
5486 {
5487 *rettv = *STACK_TV_BOT(-1);
5488 --ectx->ec_stack.ga_len;
5489 }
5490
5491 ectx->ec_instr = save_instr;
5492 ectx->ec_iidx = save_iidx;
5493
5494 return res;
5495}
5496
5497/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005498 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5499 * "substitute_instr".
5500 */
5501 char_u *
5502exe_substitute_instr(void)
5503{
5504 ectx_T *ectx = substitute_instr->subs_ectx;
5505 isn_T *save_instr = ectx->ec_instr;
5506 int save_iidx = ectx->ec_iidx;
5507 char_u *res;
5508
5509 ectx->ec_instr = substitute_instr->subs_instr;
5510 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005511 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005512 typval_T *tv = STACK_TV_BOT(-1);
5513
Bram Moolenaar27523602021-06-05 21:36:19 +02005514 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005515 --ectx->ec_stack.ga_len;
5516 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005517 }
5518 else
5519 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005520 substitute_instr->subs_status = FAIL;
5521 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005523
Bram Moolenaar4c137212021-04-19 16:48:48 +02005524 ectx->ec_instr = save_instr;
5525 ectx->ec_iidx = save_iidx;
5526
5527 return res;
5528}
5529
5530/*
5531 * Call a "def" function from old Vim script.
5532 * Return OK or FAIL.
5533 */
5534 int
5535call_def_function(
5536 ufunc_T *ufunc,
5537 int argc_arg, // nr of arguments
5538 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005539 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005540 partial_T *partial, // optional partial for context
Bram Moolenaar58779852022-09-06 18:31:14 +01005541 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005542 typval_T *rettv) // return value
5543{
5544 ectx_T ectx; // execution context
5545 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005546 int partial_argc = partial == NULL
5547 || (flags & DEF_USE_PT_ARGV) == 0
5548 ? 0 : partial->pt_argc;
5549 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005550 typval_T *tv;
5551 int idx;
5552 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005553 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005554 sctx_T save_current_sctx = current_sctx;
5555 int did_emsg_before = did_emsg_cumul + did_emsg;
5556 int save_suppress_errthrow = suppress_errthrow;
5557 msglist_T **saved_msg_list = NULL;
5558 msglist_T *private_msg_list = NULL;
5559 int save_emsg_silent_def = emsg_silent_def;
5560 int save_did_emsg_def = did_emsg_def;
5561 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005562 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005563
5564// Get pointer to item in the stack.
5565#undef STACK_TV
5566#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5567
5568// Get pointer to item at the bottom of the stack, -1 is the bottom.
5569#undef STACK_TV_BOT
5570#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5571
5572// Get pointer to a local variable on the stack. Negative for arguments.
5573#undef STACK_TV_VAR
5574#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5575
5576 if (ufunc->uf_def_status == UF_NOT_COMPILED
5577 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005578 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5579 && compile_def_function(ufunc, FALSE,
5580 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005581 {
5582 if (did_emsg_cumul + did_emsg == did_emsg_before)
5583 semsg(_(e_function_is_not_compiled_str),
5584 printable_func_name(ufunc));
5585 return FAIL;
5586 }
5587
5588 {
5589 // Check the function was really compiled.
5590 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5591 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005592 if (dfunc->df_ufunc == NULL)
5593 {
5594 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5595 return FAIL;
5596 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005597 if (INSTRUCTIONS(dfunc) == NULL)
5598 {
5599 iemsg("using call_def_function() on not compiled function");
5600 return FAIL;
5601 }
5602 }
5603
5604 // If depth of calling is getting too high, don't execute the function.
5605 orig_funcdepth = funcdepth_get();
5606 if (funcdepth_increment() == FAIL)
5607 return FAIL;
5608
5609 CLEAR_FIELD(ectx);
5610 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5611 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005612 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005613 {
5614 funcdepth_decrement();
5615 return FAIL;
5616 }
5617 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5618 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5619 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005620 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005621
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005622 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005623 if (idx > 0 && ufunc->uf_va_name == NULL)
5624 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005625 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005626 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005627 goto failed_early;
5628 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005629 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005630 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005631 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005632 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005633 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005634 goto failed_early;
5635 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005636
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005637 // Put values from the partial and arguments on the stack, but no more than
5638 // what the function expects. A lambda can be called with more arguments
5639 // than it uses.
5640 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005641 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5642 ++idx)
5643 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005644 int argv_idx = idx - partial_argc;
5645
5646 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005647 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005648 && tv->v_type == VAR_SPECIAL
5649 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005650 {
5651 // Use the default value.
5652 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5653 }
5654 else
5655 {
5656 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5657 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005658 ufunc->uf_arg_types[idx], tv,
5659 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005660 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005661 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005662 }
5663 ++ectx.ec_stack.ga_len;
5664 }
5665
5666 // Turn varargs into a list. Empty list if no args.
5667 if (ufunc->uf_va_name != NULL)
5668 {
5669 int vararg_count = argc - ufunc->uf_args.ga_len;
5670
5671 if (vararg_count < 0)
5672 vararg_count = 0;
5673 else
5674 argc -= vararg_count;
5675 if (exe_newlist(vararg_count, &ectx) == FAIL)
5676 goto failed_early;
5677
5678 // Check the type of the list items.
5679 tv = STACK_TV_BOT(-1);
5680 if (ufunc->uf_va_type != NULL
5681 && ufunc->uf_va_type != &t_list_any
5682 && ufunc->uf_va_type->tt_member != &t_any
5683 && tv->vval.v_list != NULL)
5684 {
5685 type_T *expected = ufunc->uf_va_type->tt_member;
5686 listitem_T *li = tv->vval.v_list->lv_first;
5687
5688 for (idx = 0; idx < vararg_count; ++idx)
5689 {
5690 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005691 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005692 goto failed_early;
5693 li = li->li_next;
5694 }
5695 }
5696
5697 if (defcount > 0)
5698 // Move varargs list to below missing default arguments.
5699 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5700 --ectx.ec_stack.ga_len;
5701 }
5702
5703 // Make space for omitted arguments, will store default value below.
5704 // Any varargs list goes after them.
5705 if (defcount > 0)
5706 for (idx = 0; idx < defcount; ++idx)
5707 {
5708 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5709 ++ectx.ec_stack.ga_len;
5710 }
5711 if (ufunc->uf_va_name != NULL)
5712 ++ectx.ec_stack.ga_len;
5713
5714 // Frame pointer points to just after arguments.
5715 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5716 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5717
5718 {
5719 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5720 + ufunc->uf_dfunc_idx;
5721 ufunc_T *base_ufunc = dfunc->df_ufunc;
5722
5723 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005724 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005725 if (partial != NULL || base_ufunc->uf_partial != NULL)
5726 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005727 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5728 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005729 goto failed_early;
5730 if (partial != NULL)
5731 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005732 outer_T *outer = get_pt_outer(partial);
5733
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005734 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005735 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005736 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005737 if (current_ectx != NULL)
5738 {
5739 if (current_ectx->ec_outer_ref != NULL
5740 && current_ectx->ec_outer_ref->or_outer != NULL)
5741 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005742 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005743 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005744 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005745 }
5746 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005747 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005748 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005749 ++partial->pt_refcount;
5750 ectx.ec_outer_ref->or_partial = partial;
5751 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005752 }
5753 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005754 {
5755 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5756 ++base_ufunc->uf_partial->pt_refcount;
5757 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5758 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005759 }
5760 }
5761
5762 // dummy frame entries
5763 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5764 {
5765 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5766 ++ectx.ec_stack.ga_len;
5767 }
5768
5769 {
5770 // Reserve space for local variables and any closure reference count.
5771 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5772 + ufunc->uf_dfunc_idx;
5773
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005774 // Initialize variables to zero. That avoids having to generate
5775 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005776 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005777 {
5778 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5779 STACK_TV_VAR(idx)->vval.v_number = 0;
5780 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005781 ectx.ec_stack.ga_len += dfunc->df_varcount;
5782 if (dfunc->df_has_closure)
5783 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01005784 // Initialize the variable that counts how many closures were
5785 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005786 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5787 STACK_TV_VAR(idx)->vval.v_number = 0;
5788 ++ectx.ec_stack.ga_len;
5789 }
5790
5791 ectx.ec_instr = INSTRUCTIONS(dfunc);
5792 }
5793
Bram Moolenaar58779852022-09-06 18:31:14 +01005794 // Store the execution context in funccal, used by invoke_all_defer().
5795 if (funccal != NULL)
5796 funccal->fc_ectx = &ectx;
5797
Bram Moolenaar4c137212021-04-19 16:48:48 +02005798 // Following errors are in the function, not the caller.
5799 // Commands behave like vim9script.
5800 estack_push_ufunc(ufunc, 1);
5801 current_sctx = ufunc->uf_script_ctx;
5802 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5803
5804 // Use a specific location for storing error messages to be converted to an
5805 // exception.
5806 saved_msg_list = msg_list;
5807 msg_list = &private_msg_list;
5808
5809 // Do turn errors into exceptions.
5810 suppress_errthrow = FALSE;
5811
5812 // Do not delete the function while executing it.
5813 ++ufunc->uf_calls;
5814
5815 // When ":silent!" was used before calling then we still abort the
5816 // function. If ":silent!" is used in the function then we don't.
5817 emsg_silent_def = emsg_silent;
5818 did_emsg_def = 0;
5819
5820 ectx.ec_where.wt_index = 0;
5821 ectx.ec_where.wt_variable = FALSE;
5822
Bram Moolenaar5800c792022-09-22 17:34:01 +01005823 /*
5824 * Execute the instructions until done.
5825 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02005826 ret = exec_instructions(&ectx);
5827 if (ret == OK)
5828 {
5829 // function finished, get result from the stack.
5830 if (ufunc->uf_ret_type == &t_void)
5831 {
5832 rettv->v_type = VAR_VOID;
5833 }
5834 else
5835 {
5836 tv = STACK_TV_BOT(-1);
5837 *rettv = *tv;
5838 tv->v_type = VAR_UNKNOWN;
5839 }
5840 }
5841
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005842 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005843 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005844
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005845 // Deal with any remaining closures, they may be in use somewhere.
5846 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005847 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005848 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005849 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005850 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005851
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005852 estack_pop();
5853 current_sctx = save_current_sctx;
5854
Bram Moolenaar2336c372021-12-06 15:06:54 +00005855 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5856 // Function was unreferenced while being used, free it now.
5857 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005858
Bram Moolenaar352134b2020-10-17 22:04:08 +02005859 if (*msg_list != NULL && saved_msg_list != NULL)
5860 {
5861 msglist_T **plist = saved_msg_list;
5862
5863 // Append entries from the current msg_list (uncaught exceptions) to
5864 // the saved msg_list.
5865 while (*plist != NULL)
5866 plist = &(*plist)->next;
5867
5868 *plist = *msg_list;
5869 }
5870 msg_list = saved_msg_list;
5871
Bram Moolenaar4c137212021-04-19 16:48:48 +02005872 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005873 {
5874 cmdmod.cmod_filter_regmatch.regprog = NULL;
5875 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005876 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005877 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005878 emsg_silent_def = save_emsg_silent_def;
5879 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005880
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005881failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005882 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5884 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005885 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005887 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005888 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005889 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005890 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005891 if (ectx.ec_outer_ref->or_outer_allocated)
5892 vim_free(ectx.ec_outer_ref->or_outer);
5893 partial_unref(ectx.ec_outer_ref->or_partial);
5894 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005895 }
5896
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005897 // Not sure if this is necessary.
5898 suppress_errthrow = save_suppress_errthrow;
5899
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005900 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5901 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005902 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005903 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005904 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905 return ret;
5906}
5907
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005908/*
Bram Moolenaar58779852022-09-06 18:31:14 +01005909 * Called when a def function has finished (possibly failed).
5910 * Invoke all the function returns to clean up and invoke deferred functions,
5911 * except the toplevel one.
5912 */
5913 void
5914unwind_def_callstack(ectx_T *ectx)
5915{
5916 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
5917 func_return(ectx);
5918}
5919
5920/*
5921 * Invoke any deffered functions for the top function in "ectx".
5922 */
5923 void
5924may_invoke_defer_funcs(ectx_T *ectx)
5925{
5926 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
5927
5928 if (dfunc->df_defer_var_idx > 0)
5929 invoke_defer_funcs(ectx);
5930}
5931
5932/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01005933 * Return loopvarinfo in a printable form in allocated memory.
5934 */
5935 static char_u *
5936printable_loopvarinfo(loopvarinfo_T *lvi)
5937{
5938 garray_T ga;
5939 int depth;
5940
5941 ga_init2(&ga, 1, 100);
5942 for (depth = 0; depth < lvi->lvi_depth; ++depth)
5943 {
5944 if (ga_grow(&ga, 50) == FAIL)
5945 break;
5946 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01005947 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01005948 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01005949 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01005950 lvi->lvi_loop[depth].var_idx,
5951 lvi->lvi_loop[depth].var_idx
5952 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01005953 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01005954 }
5955 return ga.ga_data;
5956}
5957
5958/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005959 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5960 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5961 * "pfx" is prefixed to every line.
5962 */
5963 static void
5964list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5965{
5966 int line_idx = 0;
5967 int prev_current = 0;
5968 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005969 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005970
5971 for (current = 0; current < instr_count; ++current)
5972 {
5973 isn_T *iptr = &instr[current];
5974 char *line;
5975
5976 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005977 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005978 while (line_idx < iptr->isn_lnum
5979 && line_idx < ufunc->uf_lines.ga_len)
5980 {
5981 if (current > prev_current)
5982 {
5983 msg_puts("\n\n");
5984 prev_current = current;
5985 }
5986 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5987 if (line != NULL)
5988 msg(line);
5989 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005990 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5991 {
5992 int first_def_arg = ufunc->uf_args.ga_len
5993 - ufunc->uf_def_args.ga_len;
5994
5995 if (def_arg_idx > 0)
5996 msg_puts("\n\n");
5997 msg_start();
5998 msg_puts(" ");
5999 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6000 first_def_arg + def_arg_idx]);
6001 msg_puts(" = ");
6002 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6003 msg_clr_eos();
6004 msg_end();
6005 }
6006 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006007
6008 switch (iptr->isn_type)
6009 {
6010 case ISN_EXEC:
6011 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6012 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006013 case ISN_EXEC_SPLIT:
6014 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6015 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006016 case ISN_EXECRANGE:
6017 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6018 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006019 case ISN_LEGACY_EVAL:
6020 smsg("%s%4d EVAL legacy %s", pfx, current,
6021 iptr->isn_arg.string);
6022 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006023 case ISN_REDIRSTART:
6024 smsg("%s%4d REDIR", pfx, current);
6025 break;
6026 case ISN_REDIREND:
6027 smsg("%s%4d REDIR END%s", pfx, current,
6028 iptr->isn_arg.number ? " append" : "");
6029 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006030 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006031#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006032 smsg("%s%4d CEXPR pre %s", pfx, current,
6033 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006034#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006035 break;
6036 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006037#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006038 {
6039 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6040
6041 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6042 cexpr_get_auname(cer->cer_cmdidx),
6043 cer->cer_forceit ? "!" : "",
6044 cer->cer_cmdline);
6045 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006046#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006047 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006048 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006049 smsg("%s%4d INSTR", pfx, current);
6050 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6051 msg(" -------------");
6052 break;
6053 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006054 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006055 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6056
6057 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006058 }
6059 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006060 case ISN_SUBSTITUTE:
6061 {
6062 subs_T *subs = &iptr->isn_arg.subs;
6063
6064 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6065 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6066 msg(" -------------");
6067 }
6068 break;
6069 case ISN_EXECCONCAT:
6070 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6071 (varnumber_T)iptr->isn_arg.number);
6072 break;
6073 case ISN_ECHO:
6074 {
6075 echo_T *echo = &iptr->isn_arg.echo;
6076
6077 smsg("%s%4d %s %d", pfx, current,
6078 echo->echo_with_white ? "ECHO" : "ECHON",
6079 echo->echo_count);
6080 }
6081 break;
6082 case ISN_EXECUTE:
6083 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006084 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006085 break;
6086 case ISN_ECHOMSG:
6087 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006088 (varnumber_T)(iptr->isn_arg.number));
6089 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006090 case ISN_ECHOWINDOW:
6091 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
6092 (varnumber_T)(iptr->isn_arg.number));
6093 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006094 case ISN_ECHOCONSOLE:
6095 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6096 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006097 break;
6098 case ISN_ECHOERR:
6099 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006100 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006101 break;
6102 case ISN_LOAD:
6103 {
6104 if (iptr->isn_arg.number < 0)
6105 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6106 (varnumber_T)(iptr->isn_arg.number
6107 + STACK_FRAME_SIZE));
6108 else
6109 smsg("%s%4d LOAD $%lld", pfx, current,
6110 (varnumber_T)(iptr->isn_arg.number));
6111 }
6112 break;
6113 case ISN_LOADOUTER:
6114 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006115 isn_outer_T *outer = &iptr->isn_arg.outer;
6116
6117 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006118 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006119 outer->outer_depth,
6120 outer->outer_idx + STACK_FRAME_SIZE);
6121 else if (outer->outer_depth < 0)
6122 smsg("%s%4d LOADOUTER $%d in loop level %d",
6123 pfx, current,
6124 outer->outer_idx,
6125 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006126 else
6127 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006128 outer->outer_depth,
6129 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006130 }
6131 break;
6132 case ISN_LOADV:
6133 smsg("%s%4d LOADV v:%s", pfx, current,
6134 get_vim_var_name(iptr->isn_arg.number));
6135 break;
6136 case ISN_LOADSCRIPT:
6137 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006138 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6139 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6140 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006141
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006142 sv = get_script_svar(sref, -1);
6143 if (sv == NULL)
6144 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6145 pfx, current, si->sn_name);
6146 else
6147 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006148 sv->sv_name,
6149 sref->sref_idx,
6150 si->sn_name);
6151 }
6152 break;
6153 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006154 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006155 {
6156 scriptitem_T *si = SCRIPT_ITEM(
6157 iptr->isn_arg.loadstore.ls_sid);
6158
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006159 smsg("%s%4d %s s:%s from %s", pfx, current,
6160 iptr->isn_type == ISN_LOADS ? "LOADS"
6161 : "LOADEXPORT",
6162 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006163 }
6164 break;
6165 case ISN_LOADAUTO:
6166 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6167 break;
6168 case ISN_LOADG:
6169 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6170 break;
6171 case ISN_LOADB:
6172 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6173 break;
6174 case ISN_LOADW:
6175 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6176 break;
6177 case ISN_LOADT:
6178 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6179 break;
6180 case ISN_LOADGDICT:
6181 smsg("%s%4d LOAD g:", pfx, current);
6182 break;
6183 case ISN_LOADBDICT:
6184 smsg("%s%4d LOAD b:", pfx, current);
6185 break;
6186 case ISN_LOADWDICT:
6187 smsg("%s%4d LOAD w:", pfx, current);
6188 break;
6189 case ISN_LOADTDICT:
6190 smsg("%s%4d LOAD t:", pfx, current);
6191 break;
6192 case ISN_LOADOPT:
6193 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6194 break;
6195 case ISN_LOADENV:
6196 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6197 break;
6198 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006199 smsg("%s%4d LOADREG @%c", pfx, current,
6200 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006201 break;
6202
6203 case ISN_STORE:
6204 if (iptr->isn_arg.number < 0)
6205 smsg("%s%4d STORE arg[%lld]", pfx, current,
6206 iptr->isn_arg.number + STACK_FRAME_SIZE);
6207 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006208 smsg("%s%4d STORE $%lld", pfx, current,
6209 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006210 break;
6211 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006212 {
6213 isn_outer_T *outer = &iptr->isn_arg.outer;
6214
6215 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6216 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6217 pfx, current, outer->outer_idx);
6218 else
6219 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6220 outer->outer_depth, outer->outer_idx);
6221 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006222 break;
6223 case ISN_STOREV:
6224 smsg("%s%4d STOREV v:%s", pfx, current,
6225 get_vim_var_name(iptr->isn_arg.number));
6226 break;
6227 case ISN_STOREAUTO:
6228 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6229 break;
6230 case ISN_STOREG:
6231 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6232 break;
6233 case ISN_STOREB:
6234 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6235 break;
6236 case ISN_STOREW:
6237 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6238 break;
6239 case ISN_STORET:
6240 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6241 break;
6242 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006243 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006244 {
6245 scriptitem_T *si = SCRIPT_ITEM(
6246 iptr->isn_arg.loadstore.ls_sid);
6247
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006248 smsg("%s%4d %s %s in %s", pfx, current,
6249 iptr->isn_type == ISN_STORES
6250 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006251 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6252 }
6253 break;
6254 case ISN_STORESCRIPT:
6255 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006256 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6257 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6258 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006259
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006260 sv = get_script_svar(sref, -1);
6261 if (sv == NULL)
6262 smsg("%s%4d STORESCRIPT [deleted] in %s",
6263 pfx, current, si->sn_name);
6264 else
6265 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006266 sv->sv_name,
6267 sref->sref_idx,
6268 si->sn_name);
6269 }
6270 break;
6271 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006272 case ISN_STOREFUNCOPT:
6273 smsg("%s%4d %s &%s", pfx, current,
6274 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006275 iptr->isn_arg.storeopt.so_name);
6276 break;
6277 case ISN_STOREENV:
6278 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6279 break;
6280 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006281 smsg("%s%4d STOREREG @%c", pfx, current,
6282 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006283 break;
6284 case ISN_STORENR:
6285 smsg("%s%4d STORE %lld in $%d", pfx, current,
6286 iptr->isn_arg.storenr.stnr_val,
6287 iptr->isn_arg.storenr.stnr_idx);
6288 break;
6289
6290 case ISN_STOREINDEX:
6291 smsg("%s%4d STOREINDEX %s", pfx, current,
6292 vartype_name(iptr->isn_arg.vartype));
6293 break;
6294
6295 case ISN_STORERANGE:
6296 smsg("%s%4d STORERANGE", pfx, current);
6297 break;
6298
6299 // constants
6300 case ISN_PUSHNR:
6301 smsg("%s%4d PUSHNR %lld", pfx, current,
6302 (varnumber_T)(iptr->isn_arg.number));
6303 break;
6304 case ISN_PUSHBOOL:
6305 case ISN_PUSHSPEC:
6306 smsg("%s%4d PUSH %s", pfx, current,
6307 get_var_special_name(iptr->isn_arg.number));
6308 break;
6309 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006310 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006311 break;
6312 case ISN_PUSHS:
6313 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6314 break;
6315 case ISN_PUSHBLOB:
6316 {
6317 char_u *r;
6318 char_u numbuf[NUMBUFLEN];
6319 char_u *tofree;
6320
6321 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6322 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6323 vim_free(tofree);
6324 }
6325 break;
6326 case ISN_PUSHFUNC:
6327 {
6328 char *name = (char *)iptr->isn_arg.string;
6329
6330 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6331 name == NULL ? "[none]" : name);
6332 }
6333 break;
6334 case ISN_PUSHCHANNEL:
6335#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006336 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006337#endif
6338 break;
6339 case ISN_PUSHJOB:
6340#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006341 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006342#endif
6343 break;
6344 case ISN_PUSHEXC:
6345 smsg("%s%4d PUSH v:exception", pfx, current);
6346 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006347 case ISN_AUTOLOAD:
6348 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6349 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006350 case ISN_UNLET:
6351 smsg("%s%4d UNLET%s %s", pfx, current,
6352 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6353 iptr->isn_arg.unlet.ul_name);
6354 break;
6355 case ISN_UNLETENV:
6356 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6357 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6358 iptr->isn_arg.unlet.ul_name);
6359 break;
6360 case ISN_UNLETINDEX:
6361 smsg("%s%4d UNLETINDEX", pfx, current);
6362 break;
6363 case ISN_UNLETRANGE:
6364 smsg("%s%4d UNLETRANGE", pfx, current);
6365 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006366 case ISN_LOCKUNLOCK:
6367 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6368 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006369 case ISN_LOCKCONST:
6370 smsg("%s%4d LOCKCONST", pfx, current);
6371 break;
6372 case ISN_NEWLIST:
6373 smsg("%s%4d NEWLIST size %lld", pfx, current,
6374 (varnumber_T)(iptr->isn_arg.number));
6375 break;
6376 case ISN_NEWDICT:
6377 smsg("%s%4d NEWDICT size %lld", pfx, current,
6378 (varnumber_T)(iptr->isn_arg.number));
6379 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006380 case ISN_NEWPARTIAL:
6381 smsg("%s%4d NEWPARTIAL", pfx, current);
6382 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006383
6384 // function call
6385 case ISN_BCALL:
6386 {
6387 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6388
6389 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6390 internal_func_name(cbfunc->cbf_idx),
6391 cbfunc->cbf_argcount);
6392 }
6393 break;
6394 case ISN_DCALL:
6395 {
6396 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6397 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6398 + cdfunc->cdf_idx;
6399
6400 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006401 printable_func_name(df->df_ufunc),
6402 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006403 }
6404 break;
6405 case ISN_UCALL:
6406 {
6407 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6408
6409 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6410 cufunc->cuf_name, cufunc->cuf_argcount);
6411 }
6412 break;
6413 case ISN_PCALL:
6414 {
6415 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6416
6417 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6418 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6419 }
6420 break;
6421 case ISN_PCALL_END:
6422 smsg("%s%4d PCALL end", pfx, current);
6423 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006424 case ISN_DEFER:
6425 smsg("%s%4d DEFER %d args", pfx, current,
6426 (int)iptr->isn_arg.defer.defer_argcount);
6427 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006428 case ISN_RETURN:
6429 smsg("%s%4d RETURN", pfx, current);
6430 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006431 case ISN_RETURN_VOID:
6432 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006433 break;
6434 case ISN_FUNCREF:
6435 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006436 funcref_T *funcref = &iptr->isn_arg.funcref;
6437 funcref_extra_T *extra = funcref->fr_extra;
6438 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006439
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006440 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006441 {
6442 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6443 + funcref->fr_dfunc_idx;
6444 name = df->df_ufunc->uf_name;
6445 }
6446 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006447 name = extra->fre_func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006448 if (extra == NULL || extra->fre_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006449 smsg("%s%4d FUNCREF %s", pfx, current, name);
6450 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006451 {
6452 char_u *info = printable_loopvarinfo(
6453 &extra->fre_loopvar_info);
6454
6455 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6456 name, info);
6457 vim_free(info);
6458 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006459 }
6460 break;
6461
6462 case ISN_NEWFUNC:
6463 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006464 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006465
Bram Moolenaarcc341812022-09-19 15:54:34 +01006466 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006467 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6468 arg->nfa_lambda, arg->nfa_global);
6469 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006470 {
6471 char_u *info = printable_loopvarinfo(
6472 &arg->nfa_loopvar_info);
6473
6474 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6475 arg->nfa_lambda, arg->nfa_global, info);
6476 vim_free(info);
6477 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006478 }
6479 break;
6480
6481 case ISN_DEF:
6482 {
6483 char_u *name = iptr->isn_arg.string;
6484
6485 smsg("%s%4d DEF %s", pfx, current,
6486 name == NULL ? (char_u *)"" : name);
6487 }
6488 break;
6489
6490 case ISN_JUMP:
6491 {
6492 char *when = "?";
6493
6494 switch (iptr->isn_arg.jump.jump_when)
6495 {
6496 case JUMP_ALWAYS:
6497 when = "JUMP";
6498 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006499 case JUMP_NEVER:
6500 iemsg("JUMP_NEVER should not be used");
6501 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006502 case JUMP_AND_KEEP_IF_TRUE:
6503 when = "JUMP_AND_KEEP_IF_TRUE";
6504 break;
6505 case JUMP_IF_FALSE:
6506 when = "JUMP_IF_FALSE";
6507 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006508 case JUMP_WHILE_FALSE:
6509 when = "JUMP_WHILE_FALSE"; // unused
6510 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006511 case JUMP_IF_COND_FALSE:
6512 when = "JUMP_IF_COND_FALSE";
6513 break;
6514 case JUMP_IF_COND_TRUE:
6515 when = "JUMP_IF_COND_TRUE";
6516 break;
6517 }
6518 smsg("%s%4d %s -> %d", pfx, current, when,
6519 iptr->isn_arg.jump.jump_where);
6520 }
6521 break;
6522
6523 case ISN_JUMP_IF_ARG_SET:
6524 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6525 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6526 iptr->isn_arg.jump.jump_where);
6527 break;
6528
6529 case ISN_FOR:
6530 {
6531 forloop_T *forloop = &iptr->isn_arg.forloop;
6532
6533 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006534 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006535 }
6536 break;
6537
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006538 case ISN_ENDLOOP:
6539 {
6540 endloop_T *endloop = &iptr->isn_arg.endloop;
6541
Bram Moolenaarcc341812022-09-19 15:54:34 +01006542 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6543 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006544 endloop->end_funcref_idx,
6545 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006546 endloop->end_var_idx + endloop->end_var_count - 1,
6547 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006548 }
6549 break;
6550
6551 case ISN_WHILE:
6552 {
6553 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6554
6555 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6556 whileloop->while_funcref_idx,
6557 whileloop->while_end);
6558 }
6559 break;
6560
Bram Moolenaar4c137212021-04-19 16:48:48 +02006561 case ISN_TRY:
6562 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006563 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006564
6565 if (try->try_ref->try_finally == 0)
6566 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6567 pfx, current,
6568 try->try_ref->try_catch,
6569 try->try_ref->try_endtry);
6570 else
6571 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6572 pfx, current,
6573 try->try_ref->try_catch,
6574 try->try_ref->try_finally,
6575 try->try_ref->try_endtry);
6576 }
6577 break;
6578 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006579 smsg("%s%4d CATCH", pfx, current);
6580 break;
6581 case ISN_TRYCONT:
6582 {
6583 trycont_T *trycont = &iptr->isn_arg.trycont;
6584
6585 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6586 trycont->tct_levels,
6587 trycont->tct_levels == 1 ? "" : "s",
6588 trycont->tct_where);
6589 }
6590 break;
6591 case ISN_FINALLY:
6592 smsg("%s%4d FINALLY", pfx, current);
6593 break;
6594 case ISN_ENDTRY:
6595 smsg("%s%4d ENDTRY", pfx, current);
6596 break;
6597 case ISN_THROW:
6598 smsg("%s%4d THROW", pfx, current);
6599 break;
6600
6601 // expression operations on number
6602 case ISN_OPNR:
6603 case ISN_OPFLOAT:
6604 case ISN_OPANY:
6605 {
6606 char *what;
6607 char *ins;
6608
6609 switch (iptr->isn_arg.op.op_type)
6610 {
6611 case EXPR_MULT: what = "*"; break;
6612 case EXPR_DIV: what = "/"; break;
6613 case EXPR_REM: what = "%"; break;
6614 case EXPR_SUB: what = "-"; break;
6615 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006616 case EXPR_LSHIFT: what = "<<"; break;
6617 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006618 default: what = "???"; break;
6619 }
6620 switch (iptr->isn_type)
6621 {
6622 case ISN_OPNR: ins = "OPNR"; break;
6623 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6624 case ISN_OPANY: ins = "OPANY"; break;
6625 default: ins = "???"; break;
6626 }
6627 smsg("%s%4d %s %s", pfx, current, ins, what);
6628 }
6629 break;
6630
6631 case ISN_COMPAREBOOL:
6632 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006633 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006634 case ISN_COMPARENR:
6635 case ISN_COMPAREFLOAT:
6636 case ISN_COMPARESTRING:
6637 case ISN_COMPAREBLOB:
6638 case ISN_COMPARELIST:
6639 case ISN_COMPAREDICT:
6640 case ISN_COMPAREFUNC:
6641 case ISN_COMPAREANY:
6642 {
6643 char *p;
6644 char buf[10];
6645 char *type;
6646
6647 switch (iptr->isn_arg.op.op_type)
6648 {
6649 case EXPR_EQUAL: p = "=="; break;
6650 case EXPR_NEQUAL: p = "!="; break;
6651 case EXPR_GREATER: p = ">"; break;
6652 case EXPR_GEQUAL: p = ">="; break;
6653 case EXPR_SMALLER: p = "<"; break;
6654 case EXPR_SEQUAL: p = "<="; break;
6655 case EXPR_MATCH: p = "=~"; break;
6656 case EXPR_IS: p = "is"; break;
6657 case EXPR_ISNOT: p = "isnot"; break;
6658 case EXPR_NOMATCH: p = "!~"; break;
6659 default: p = "???"; break;
6660 }
6661 STRCPY(buf, p);
6662 if (iptr->isn_arg.op.op_ic == TRUE)
6663 strcat(buf, "?");
6664 switch(iptr->isn_type)
6665 {
6666 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6667 case ISN_COMPARESPECIAL:
6668 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006669 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006670 case ISN_COMPARENR: type = "COMPARENR"; break;
6671 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6672 case ISN_COMPARESTRING:
6673 type = "COMPARESTRING"; break;
6674 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6675 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6676 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6677 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6678 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6679 default: type = "???"; break;
6680 }
6681
6682 smsg("%s%4d %s %s", pfx, current, type, buf);
6683 }
6684 break;
6685
6686 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6687 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6688
6689 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006690 case ISN_CONCAT:
6691 smsg("%s%4d CONCAT size %lld", pfx, current,
6692 (varnumber_T)(iptr->isn_arg.number));
6693 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006694 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6695 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6696 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6697 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6698 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6699 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6700 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6701 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6702 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6703 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6704 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006705 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006706 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6707 iptr->isn_arg.getitem.gi_index,
6708 iptr->isn_arg.getitem.gi_with_op ?
6709 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006710 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6711 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6712 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006713 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6714 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6715
Bram Moolenaar4c137212021-04-19 16:48:48 +02006716 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6717
Bram Moolenaar4c137212021-04-19 16:48:48 +02006718 case ISN_CHECKTYPE:
6719 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006720 checktype_T *ct = &iptr->isn_arg.type;
6721 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006722
6723 if (ct->ct_arg_idx == 0)
6724 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6725 type_name(ct->ct_type, &tofree),
6726 (int)ct->ct_off);
6727 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006728 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006729 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006730 type_name(ct->ct_type, &tofree),
6731 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006732 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006733 (int)ct->ct_arg_idx);
6734 vim_free(tofree);
6735 break;
6736 }
6737 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6738 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6739 iptr->isn_arg.checklen.cl_min_len);
6740 break;
6741 case ISN_SETTYPE:
6742 {
6743 char *tofree;
6744
6745 smsg("%s%4d SETTYPE %s", pfx, current,
6746 type_name(iptr->isn_arg.type.ct_type, &tofree));
6747 vim_free(tofree);
6748 break;
6749 }
6750 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006751 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6752 smsg("%s%4d INVERT %d (!val)", pfx, current,
6753 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006754 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006755 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6756 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006757 break;
6758 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006759 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006760 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006761 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6762 pfx, current,
6763 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006764 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006765 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6766 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006767 break;
6768 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006769 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006770 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006771 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006772 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6773 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006774 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006775 else
6776 smsg("%s%4d PUT %c %ld", pfx, current,
6777 iptr->isn_arg.put.put_regname,
6778 (long)iptr->isn_arg.put.put_lnum);
6779 break;
6780
Bram Moolenaar4c137212021-04-19 16:48:48 +02006781 case ISN_CMDMOD:
6782 {
6783 char_u *buf;
6784 size_t len = produce_cmdmods(
6785 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6786
6787 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006788 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006789 {
6790 (void)produce_cmdmods(
6791 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6792 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6793 vim_free(buf);
6794 }
6795 break;
6796 }
6797 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6798
6799 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006800 smsg("%s%4d PROFILE START line %d", pfx, current,
6801 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006802 break;
6803
6804 case ISN_PROF_END:
6805 smsg("%s%4d PROFILE END", pfx, current);
6806 break;
6807
Bram Moolenaare99d4222021-06-13 14:01:26 +02006808 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006809 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6810 iptr->isn_arg.debug.dbg_break_lnum + 1,
6811 iptr->isn_lnum,
6812 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006813 break;
6814
Bram Moolenaar4c137212021-04-19 16:48:48 +02006815 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6816 iptr->isn_arg.unpack.unp_count,
6817 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6818 break;
6819 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6820 iptr->isn_arg.shuffle.shfl_item,
6821 iptr->isn_arg.shuffle.shfl_up);
6822 break;
6823 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6824
6825 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6826 return;
6827 }
6828
6829 out_flush(); // output one line at a time
6830 ui_breakcheck();
6831 if (got_int)
6832 break;
6833 }
6834}
6835
6836/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006837 * Handle command line completion for the :disassemble command.
6838 */
6839 void
6840set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6841{
6842 char_u *p;
6843
6844 // Default: expand user functions, "debug" and "profile"
6845 xp->xp_context = EXPAND_DISASSEMBLE;
6846 xp->xp_pattern = arg;
6847
6848 // first argument already typed: only user function names
6849 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6850 {
6851 xp->xp_context = EXPAND_USER_FUNC;
6852 xp->xp_pattern = skipwhite(p);
6853 }
6854}
6855
6856/*
6857 * Function given to ExpandGeneric() to obtain the list of :disassemble
6858 * arguments.
6859 */
6860 char_u *
6861get_disassemble_argument(expand_T *xp, int idx)
6862{
6863 if (idx == 0)
6864 return (char_u *)"debug";
6865 if (idx == 1)
6866 return (char_u *)"profile";
6867 return get_user_func_name(xp, idx - 2);
6868}
6869
6870/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006871 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006872 * We don't really need this at runtime, but we do have tests that require it,
6873 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874 */
6875 void
6876ex_disassemble(exarg_T *eap)
6877{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006878 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006879 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006881 isn_T *instr = NULL; // init to shut up gcc warning
6882 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006883 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006884
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006885 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006886 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006888 if (func_needs_compiling(ufunc, compile_type)
6889 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006890 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006891 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006893 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006894 return;
6895 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006896 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897
6898 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006899 switch (compile_type)
6900 {
6901 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006902#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006903 instr = dfunc->df_instr_prof;
6904 instr_count = dfunc->df_instr_prof_count;
6905 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006906#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006907 // FALLTHROUGH
6908 case CT_NONE:
6909 instr = dfunc->df_instr;
6910 instr_count = dfunc->df_instr_count;
6911 break;
6912 case CT_DEBUG:
6913 instr = dfunc->df_instr_debug;
6914 instr_count = dfunc->df_instr_debug_count;
6915 break;
6916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006917
Bram Moolenaar4c137212021-04-19 16:48:48 +02006918 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919}
6920
6921/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006922 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923 * list, etc. Mostly like what JavaScript does, except that empty list and
6924 * empty dictionary are FALSE.
6925 */
6926 int
6927tv2bool(typval_T *tv)
6928{
6929 switch (tv->v_type)
6930 {
6931 case VAR_NUMBER:
6932 return tv->vval.v_number != 0;
6933 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 case VAR_PARTIAL:
6936 return tv->vval.v_partial != NULL;
6937 case VAR_FUNC:
6938 case VAR_STRING:
6939 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6940 case VAR_LIST:
6941 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6942 case VAR_DICT:
6943 return tv->vval.v_dict != NULL
6944 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6945 case VAR_BOOL:
6946 case VAR_SPECIAL:
6947 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6948 case VAR_JOB:
6949#ifdef FEAT_JOB_CHANNEL
6950 return tv->vval.v_job != NULL;
6951#else
6952 break;
6953#endif
6954 case VAR_CHANNEL:
6955#ifdef FEAT_JOB_CHANNEL
6956 return tv->vval.v_channel != NULL;
6957#else
6958 break;
6959#endif
6960 case VAR_BLOB:
6961 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6962 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006963 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006964 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006965 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966 break;
6967 }
6968 return FALSE;
6969}
6970
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006971 void
6972emsg_using_string_as(typval_T *tv, int as_number)
6973{
6974 semsg(_(as_number ? e_using_string_as_number_str
6975 : e_using_string_as_bool_str),
6976 tv->vval.v_string == NULL
6977 ? (char_u *)"" : tv->vval.v_string);
6978}
6979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006980/*
6981 * If "tv" is a string give an error and return FAIL.
6982 */
6983 int
6984check_not_string(typval_T *tv)
6985{
6986 if (tv->v_type == VAR_STRING)
6987 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006988 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006989 clear_tv(tv);
6990 return FAIL;
6991 }
6992 return OK;
6993}
6994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006995
6996#endif // FEAT_EVAL