blob: 9416cf3a35ad13fd378c7e3096ddabc1af62b3ba [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010067// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100104// Get pointer to item in the stack.
105#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200108#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100110// Get pointer to a local variable on the stack. Negative for arguments.
111#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
112
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200113 void
114to_string_error(vartype_T vartype)
115{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200116 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200117}
118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100120 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 */
122 static int
123ufunc_argcount(ufunc_T *ufunc)
124{
125 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
126}
127
128/*
LemonBoy372bcce2022-04-25 12:43:20 +0100129 * Create a new string from "count" items at the bottom of the stack.
130 * A trailing NUL is appended.
131 * When "count" is zero an empty string is added to the stack.
132 */
133 static int
134exe_concat(int count, ectx_T *ectx)
135{
136 int idx;
137 int len = 0;
138 typval_T *tv;
139 garray_T ga;
140
141 ga_init2(&ga, sizeof(char), 1);
142 // Preallocate enough space for the whole string to avoid having to grow
143 // and copy.
144 for (idx = 0; idx < count; ++idx)
145 {
146 tv = STACK_TV_BOT(idx - count);
147 if (tv->vval.v_string != NULL)
148 len += (int)STRLEN(tv->vval.v_string);
149 }
150
151 if (ga_grow(&ga, len + 1) == FAIL)
152 return FAIL;
153
154 for (idx = 0; idx < count; ++idx)
155 {
156 tv = STACK_TV_BOT(idx - count);
157 ga_concat(&ga, tv->vval.v_string);
158 clear_tv(tv);
159 }
160
161 // add a terminating NUL
162 ga_append(&ga, NUL);
163
164 ectx->ec_stack.ga_len -= count - 1;
165 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
166
167 return OK;
168}
169
170/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200171 * Create a new list from "count" items at the bottom of the stack.
172 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100173 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200174 */
175 static int
176exe_newlist(int count, ectx_T *ectx)
177{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 int idx;
180 typval_T *tv;
181
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100182 if (count >= 0)
183 {
184 list = list_alloc_with_items(count);
185 if (list == NULL)
186 return FAIL;
187 for (idx = 0; idx < count; ++idx)
188 list_set_item(list, idx, STACK_TV_BOT(idx - count));
189 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190
191 if (count > 0)
192 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200193 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100194 {
195 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 else
199 ++ectx->ec_stack.ga_len;
200 tv = STACK_TV_BOT(-1);
201 tv->v_type = VAR_LIST;
202 tv->vval.v_list = list;
Bram Moolenaar566badc2022-09-18 13:46:08 +0100203 tv->v_lock = 0;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100204 if (list != NULL)
205 ++list->lv_refcount;
206 return OK;
207}
208
209/*
210 * Implementation of ISN_NEWDICT.
211 * Returns FAIL on total failure, MAYBE on error.
212 */
213 static int
214exe_newdict(int count, ectx_T *ectx)
215{
216 dict_T *dict = NULL;
217 dictitem_T *item;
218 char_u *key;
219 int idx;
220 typval_T *tv;
221
222 if (count >= 0)
223 {
224 dict = dict_alloc();
225 if (unlikely(dict == NULL))
226 return FAIL;
227 for (idx = 0; idx < count; ++idx)
228 {
229 // have already checked key type is VAR_STRING
230 tv = STACK_TV_BOT(2 * (idx - count));
231 // check key is unique
232 key = tv->vval.v_string == NULL
233 ? (char_u *)"" : tv->vval.v_string;
234 item = dict_find(dict, key, -1);
235 if (item != NULL)
236 {
237 semsg(_(e_duplicate_key_in_dicitonary), key);
238 dict_unref(dict);
239 return MAYBE;
240 }
241 item = dictitem_alloc(key);
242 clear_tv(tv);
243 if (unlikely(item == NULL))
244 {
245 dict_unref(dict);
246 return FAIL;
247 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100248 tv = STACK_TV_BOT(2 * (idx - count) + 1);
249 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100250 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100251 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100252 if (dict_add(dict, item) == FAIL)
253 {
254 // can this ever happen?
255 dict_unref(dict);
256 return FAIL;
257 }
258 }
259 }
260
261 if (count > 0)
262 ectx->ec_stack.ga_len -= 2 * count - 1;
263 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
264 return FAIL;
265 else
266 ++ectx->ec_stack.ga_len;
267 tv = STACK_TV_BOT(-1);
268 tv->v_type = VAR_DICT;
269 tv->v_lock = 0;
270 tv->vval.v_dict = dict;
271 if (dict != NULL)
272 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200273 return OK;
274}
275
276/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200277 * If debug_tick changed check if "ufunc" has a breakpoint and update
278 * "uf_has_breakpoint".
279 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000280 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200281update_has_breakpoint(ufunc_T *ufunc)
282{
283 if (ufunc->uf_debug_tick != debug_tick)
284 {
285 linenr_T breakpoint;
286
287 ufunc->uf_debug_tick = debug_tick;
288 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
289 ufunc->uf_has_breakpoint = breakpoint > 0;
290 }
291}
292
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200293static garray_T dict_stack = GA_EMPTY;
294
295/*
296 * Put a value on the dict stack. This consumes "tv".
297 */
298 static int
299dict_stack_save(typval_T *tv)
300{
301 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000302 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200303 if (ga_grow(&dict_stack, 1) == FAIL)
304 return FAIL;
305 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
306 ++dict_stack.ga_len;
307 return OK;
308}
309
310/*
311 * Get the typval at top of the dict stack.
312 */
313 static typval_T *
314dict_stack_get_tv(void)
315{
316 if (dict_stack.ga_len == 0)
317 return NULL;
318 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
319}
320
321/*
322 * Get the dict at top of the dict stack.
323 */
324 static dict_T *
325dict_stack_get_dict(void)
326{
327 typval_T *tv;
328
329 if (dict_stack.ga_len == 0)
330 return NULL;
331 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
332 if (tv->v_type == VAR_DICT)
333 return tv->vval.v_dict;
334 return NULL;
335}
336
337/*
338 * Drop an item from the dict stack.
339 */
340 static void
341dict_stack_drop(void)
342{
343 if (dict_stack.ga_len == 0)
344 {
345 iemsg("Dict stack underflow");
346 return;
347 }
348 --dict_stack.ga_len;
349 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
350}
351
352/*
353 * Drop items from the dict stack until the length is equal to "len".
354 */
355 static void
356dict_stack_clear(int len)
357{
358 while (dict_stack.ga_len > len)
359 dict_stack_drop();
360}
361
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200362/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000363 * Get a pointer to useful "pt_outer" of "pt".
364 */
365 static outer_T *
366get_pt_outer(partial_T *pt)
367{
368 partial_T *ptref = pt->pt_outer_partial;
369
370 if (ptref == NULL)
371 return &pt->pt_outer;
372
373 // partial using partial (recursively)
374 while (ptref->pt_outer_partial != NULL)
375 ptref = ptref->pt_outer_partial;
376 return &ptref->pt_outer;
377}
378
379/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100380 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100381 * This adds a stack frame and sets the instruction pointer to the start of the
382 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200383 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 *
385 * Stack has:
386 * - current arguments (already there)
387 * - omitted optional argument (default values) added here
388 * - stack frame:
389 * - pointer to calling function
390 * - Index of next instruction in calling function
391 * - previous frame pointer
392 * - reserved space for local variables
393 */
394 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100395call_dfunc(
396 int cdf_idx,
397 partial_T *pt,
398 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100399 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100401 int argcount = argcount_arg;
402 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
403 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200404 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100405 int arg_to_add;
406 int vararg_count = 0;
407 int varcount;
408 int idx;
409 estack_T *entry;
410 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200411 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000412 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413
414 if (dfunc->df_deleted)
415 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100416 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000417 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100418 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 return FAIL;
420 }
421
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100422#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100423 if (do_profiling == PROF_YES)
424 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200425 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100426 {
427 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
428 + profile_info_ga.ga_len;
429 ++profile_info_ga.ga_len;
430 CLEAR_POINTER(info);
431 profile_may_start_func(info, ufunc,
432 (((dfunc_T *)def_functions.ga_data)
433 + ectx->ec_dfunc_idx)->df_ufunc);
434 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100435 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100436#endif
437
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200438 // When debugging and using "cont" switches to the not-debugged
439 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000440 compile_type = get_compile_type(ufunc);
441 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200442 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000443 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200444
445 // compile_def_function() may cause def_functions.ga_data to change
446 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
447 }
448 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200449 {
450 if (did_emsg_cumul + did_emsg == did_emsg_before)
451 semsg(_(e_function_is_not_compiled_str),
452 printable_func_name(ufunc));
453 return FAIL;
454 }
455
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200456 if (ufunc->uf_va_name != NULL)
457 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200458 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200459 // Stack at time of call with 2 varargs:
460 // normal_arg
461 // optional_arg
462 // vararg_1
463 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200464 // After creating the list:
465 // normal_arg
466 // optional_arg
467 // vararg-list
468 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200469 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200470 // After creating the list
471 // normal_arg
472 // (space for optional_arg)
473 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200474 vararg_count = argcount - ufunc->uf_args.ga_len;
475 if (vararg_count < 0)
476 vararg_count = 0;
477 else
478 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200479 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200480 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200481
482 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200483 }
484
Bram Moolenaarfe270812020-04-11 22:31:27 +0200485 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200486 if (arg_to_add < 0)
487 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100488 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
489 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200490 return FAIL;
491 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000492 else if (arg_to_add > ufunc->uf_def_args.ga_len)
493 {
494 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
495
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100496 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
497 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000498 return FAIL;
499 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200500
501 // Reserve space for:
502 // - missing arguments
503 // - stack frame
504 // - local variables
505 // - if needed: a counter for number of closures created in
506 // ectx->ec_funcrefs.
507 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100508 if (GA_GROW_FAILS(&ectx->ec_stack,
509 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100510 return FAIL;
511
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100512 // If depth of calling is getting too high, don't execute the function.
513 if (funcdepth_increment() == FAIL)
514 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200515 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100516
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100517 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200518 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100519 {
520 floc = ALLOC_ONE(funclocal_T);
521 if (floc == NULL)
522 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200523 *floc = ectx->ec_funclocal;
524 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100525 }
526
Bram Moolenaarfe270812020-04-11 22:31:27 +0200527 // Move the vararg-list to below the missing optional arguments.
528 if (vararg_count > 0 && arg_to_add > 0)
529 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100530
531 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200532 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200533 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200534 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535
536 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100537 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
538 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200539 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200540 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
541 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100542 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100543 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200544 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545
546 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200547 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000548 {
549 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
550
551 tv->v_type = VAR_NUMBER;
552 tv->vval.v_number = 0;
553 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200554 if (dfunc->df_has_closure)
555 {
556 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
557
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100558 // Initialize the variable that counts how many closures were created.
559 // This is used in handle_closure_in_use().
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200560 tv->v_type = VAR_NUMBER;
561 tv->vval.v_number = 0;
562 }
563 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100564
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100565 if (pt != NULL || ufunc->uf_partial != NULL
566 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100567 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200568 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100569
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200570 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100571 return FAIL;
572 if (pt != NULL)
573 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000574 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200575 ++pt->pt_refcount;
576 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100577 }
578 else if (ufunc->uf_partial != NULL)
579 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000580 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200581 ++ufunc->uf_partial->pt_refcount;
582 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100583 }
584 else
585 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200586 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200587 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200588 {
589 vim_free(ref);
590 return FAIL;
591 }
592 ref->or_outer_allocated = TRUE;
593 ref->or_outer->out_stack = &ectx->ec_stack;
594 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
595 if (ectx->ec_outer_ref != NULL)
596 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100597 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200598 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100599 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100600 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200601 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100602
Bram Moolenaarc970e422021-03-17 15:03:04 +0100603 ++ufunc->uf_calls;
604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605 // Set execution state to the start of the called function.
606 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100607 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100608 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200609 if (entry != NULL)
610 {
611 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200612 // Save the current context so it can be restored on return.
613 entry->es_save_sctx = current_sctx;
614 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200615 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100616
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200617 // Start execution at the first instruction.
618 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619
620 return OK;
621}
622
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000623// Double linked list of funcstack_T in use.
624static funcstack_T *first_funcstack = NULL;
625
626 static void
627add_funcstack_to_list(funcstack_T *funcstack)
628{
629 // Link in list of funcstacks.
630 if (first_funcstack != NULL)
631 first_funcstack->fs_prev = funcstack;
632 funcstack->fs_next = first_funcstack;
633 funcstack->fs_prev = NULL;
634 first_funcstack = funcstack;
635}
636
637 static void
638remove_funcstack_from_list(funcstack_T *funcstack)
639{
640 if (funcstack->fs_prev == NULL)
641 first_funcstack = funcstack->fs_next;
642 else
643 funcstack->fs_prev->fs_next = funcstack->fs_next;
644 if (funcstack->fs_next != NULL)
645 funcstack->fs_next->fs_prev = funcstack->fs_prev;
646}
647
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100648/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200649 * Used when returning from a function: Check if any closure is still
650 * referenced. If so then move the arguments and variables to a separate piece
651 * of stack to be used when the closure is called.
652 * When "free_arguments" is TRUE the arguments are to be freed.
653 * Returns FAIL when out of memory.
654 */
655 static int
656handle_closure_in_use(ectx_T *ectx, int free_arguments)
657{
658 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
659 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200660 int argcount;
661 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200662 int idx;
663 typval_T *tv;
664 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200665 garray_T *gap = &ectx->ec_funcrefs;
666 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200667
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200668 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200669 return OK; // function was freed
670 if (dfunc->df_has_closure == 0)
671 return OK; // no closures
672 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
673 closure_count = tv->vval.v_number;
674 if (closure_count == 0)
675 return OK; // no funcrefs created
676
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100677 // Compute "top": the first entry in the stack used by the function.
678 // This is the first argument (after that comes the stack frame and then
679 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200680 argcount = ufunc_argcount(dfunc->df_ufunc);
681 top = ectx->ec_frame_idx - argcount;
682
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200683 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200684 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200685 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200686 partial_T *pt;
687 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200688
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200689 if (off < 0)
690 continue; // count is off or already done
691 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200692 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200693 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200694 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200695 int i;
696
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000697 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200698 // unreferenced on return.
699 for (i = 0; i < dfunc->df_varcount; ++i)
700 {
701 typval_T *stv = STACK_TV(ectx->ec_frame_idx
702 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200703 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200704 --refcount;
705 }
706 if (refcount > 1)
707 {
708 closure_in_use = TRUE;
709 break;
710 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200711 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200712 }
713
714 if (closure_in_use)
715 {
716 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
717 typval_T *stack;
718
719 // A closure is using the arguments and/or local variables.
720 // Move them to the called function.
721 if (funcstack == NULL)
722 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000723
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200724 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
725 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200726 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
727 funcstack->fs_ga.ga_data = stack;
728 if (stack == NULL)
729 {
730 vim_free(funcstack);
731 return FAIL;
732 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000733 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200734
735 // Move or copy the arguments.
736 for (idx = 0; idx < argcount; ++idx)
737 {
738 tv = STACK_TV(top + idx);
739 if (free_arguments)
740 {
741 *(stack + idx) = *tv;
742 tv->v_type = VAR_UNKNOWN;
743 }
744 else
745 copy_tv(tv, stack + idx);
746 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100747 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200748 // Move the local variables.
749 for (idx = 0; idx < dfunc->df_varcount; ++idx)
750 {
751 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200752
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200753 // A partial created for a local function, that is also used as a
754 // local variable, has a reference count for the variable, thus
755 // will never go down to zero. When all these refcounts are one
756 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000757 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200758 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
759 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200760 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200761
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200762 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200763 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
764 gap->ga_len - closure_count + i])
765 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200766 }
767
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200768 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200769 tv->v_type = VAR_UNKNOWN;
770 }
771
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200772 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200773 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200774 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
775 - closure_count + idx];
776 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200777 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200778 ++funcstack->fs_refcount;
779 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100780 pt->pt_outer.out_stack = &funcstack->fs_ga;
781 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200782 }
783 }
784 }
785
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200786 for (idx = 0; idx < closure_count; ++idx)
787 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
788 - closure_count + idx]);
789 gap->ga_len -= closure_count;
790 if (gap->ga_len == 0)
791 ga_clear(gap);
792
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200793 return OK;
794}
795
796/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200797 * Called when a partial is freed or its reference count goes down to one. The
798 * funcstack may be the only reference to the partials in the local variables.
799 * Go over all of them, the funcref and can be freed if all partials
800 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100801 * Returns TRUE if the funcstack is freed, the partial referencing it will then
802 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200803 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100804 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200805funcstack_check_refcount(funcstack_T *funcstack)
806{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100807 int i;
808 garray_T *gap = &funcstack->fs_ga;
809 int done = 0;
810 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200811
812 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100813 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200814 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
815 {
816 typval_T *tv = ((typval_T *)gap->ga_data) + i;
817
818 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
819 && tv->vval.v_partial->pt_funcstack == funcstack
820 && tv->vval.v_partial->pt_refcount == 1)
821 ++done;
822 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100823 if (done != funcstack->fs_min_refcount)
824 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200825
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100826 stack = gap->ga_data;
827
828 // All partials referencing the funcstack have a reference count of
829 // one, thus the funcstack is no longer of use.
830 for (i = 0; i < gap->ga_len; ++i)
831 clear_tv(stack + i);
832 vim_free(stack);
833 remove_funcstack_from_list(funcstack);
834 vim_free(funcstack);
835
836 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200837}
838
839/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000840 * For garbage collecting: set references in all variables referenced by
841 * all funcstacks.
842 */
843 int
844set_ref_in_funcstacks(int copyID)
845{
846 funcstack_T *funcstack;
847
848 for (funcstack = first_funcstack; funcstack != NULL;
849 funcstack = funcstack->fs_next)
850 {
851 typval_T *stack = funcstack->fs_ga.ga_data;
852 int i;
853
854 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
855 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
856 return TRUE; // abort
857 }
858 return FALSE;
859}
860
Bram Moolenaar806a2732022-09-04 15:40:36 +0100861// Ugly static to avoid passing the execution context around through many
862// layers.
863static ectx_T *current_ectx = NULL;
864
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000865/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100866 * Return TRUE if currently executing a :def function.
867 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100868 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100869 int
870in_def_function(void)
871{
872 return current_ectx != NULL;
873}
874
875/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100876 * Clear "current_ectx" and return the previous value. To be used when calling
877 * a user function.
878 */
879 ectx_T *
880clear_currrent_ectx(void)
881{
882 ectx_T *r = current_ectx;
883
884 current_ectx = NULL;
885 return r;
886}
887
888 void
889restore_current_ectx(ectx_T *ectx)
890{
891 if (current_ectx != NULL)
892 iemsg("Restoring current_ectx while it is not NULL");
893 current_ectx = ectx;
894}
895
896/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100897 * Add an entry for a deferred function call to the currently executing
898 * function.
899 * Return the list or NULL when failed.
900 */
901 static list_T *
902add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100903{
904 typval_T *defer_tv = STACK_TV_VAR(var_idx);
905 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100906 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100907 typval_T listval;
908
909 if (defer_tv->v_type != VAR_LIST)
910 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100911 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100912 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100913 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100914 }
915 defer_l = defer_tv->vval.v_list;
916
917 l = list_alloc_with_items(argcount + 1);
918 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100919 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100920 listval.v_type = VAR_LIST;
921 listval.vval.v_list = l;
922 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100923 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100924 {
925 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100926 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100927 }
928
Bram Moolenaar806a2732022-09-04 15:40:36 +0100929 return l;
930}
931
932/*
933 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
934 * The local variable that lists deferred functions is "var_idx".
935 * Returns OK or FAIL.
936 */
937 static int
938defer_command(int var_idx, int argcount, ectx_T *ectx)
939{
940 list_T *l = add_defer_item(var_idx, argcount, ectx);
941 int i;
942 typval_T *func_tv;
943
944 if (l == NULL)
945 return FAIL;
946
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100947 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100948 if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL)
949 {
950 semsg(_(e_expected_str_but_got_str),
951 "function or partial",
952 vartype_name(func_tv->v_type));
953 return FAIL;
954 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100955 list_set_item(l, 0, func_tv);
956
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100957 for (i = 0; i < argcount; ++i)
958 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100959 ectx->ec_stack.ga_len -= argcount + 1;
960 return OK;
961}
962
963/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100964 * Add a deferred function "name" with one argument "arg_tv".
965 * Consumes "name", also on failure.
966 * Only to be called when in_def_function() returns TRUE.
967 */
968 int
969add_defer_function(char_u *name, int argcount, typval_T *argvars)
970{
971 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
972 + current_ectx->ec_dfunc_idx;
973 list_T *l;
974 typval_T func_tv;
975 int i;
976
977 if (dfunc->df_defer_var_idx == 0)
978 {
979 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100980 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100981 return FAIL;
982 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100983
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100984 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100985 if (l == NULL)
986 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100987 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100988 return FAIL;
989 }
990
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100991 func_tv.v_type = VAR_FUNC;
992 func_tv.v_lock = 0;
993 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100994 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100995
Bram Moolenaar806a2732022-09-04 15:40:36 +0100996 for (i = 0; i < argcount; ++i)
997 list_set_item(l, i + 1, argvars + i);
998 return OK;
999}
1000
1001/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001002 * Invoked when returning from a function: Invoke any deferred calls.
1003 */
1004 static void
1005invoke_defer_funcs(ectx_T *ectx)
1006{
1007 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1008 + ectx->ec_dfunc_idx;
1009 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1010 listitem_T *li;
1011
1012 if (defer_tv->v_type != VAR_LIST)
1013 return; // no function added
1014 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1015 {
1016 list_T *l = li->li_tv.vval.v_list;
1017 typval_T rettv;
1018 typval_T argvars[MAX_FUNC_ARGS];
1019 int i;
1020 listitem_T *arg_li = l->lv_first;
1021 funcexe_T funcexe;
1022
1023 for (i = 0; i < l->lv_len - 1; ++i)
1024 {
1025 arg_li = arg_li->li_next;
1026 argvars[i] = arg_li->li_tv;
1027 }
1028
1029 CLEAR_FIELD(funcexe);
1030 funcexe.fe_evaluate = TRUE;
1031 rettv.v_type = VAR_UNKNOWN;
1032 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1033 &rettv, l->lv_len - 1, argvars, &funcexe);
1034 clear_tv(&rettv);
1035 }
1036}
1037
1038/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 * Return from the current function.
1040 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001041 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001042func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001045 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001046 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1047 + ectx->ec_dfunc_idx;
1048 int argcount = ufunc_argcount(dfunc->df_ufunc);
1049 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001050 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001051 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1052 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001053 funclocal_T *floc;
1054#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001055 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1056 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057
Bram Moolenaar12d26532021-02-19 19:13:21 +01001058 if (do_profiling == PROF_YES)
1059 {
1060 ufunc_T *caller = prev_dfunc->df_ufunc;
1061
1062 if (dfunc->df_ufunc->uf_profiling
1063 || (caller != NULL && caller->uf_profiling))
1064 {
1065 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1066 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1067 --profile_info_ga.ga_len;
1068 }
1069 }
1070#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001071
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001072 if (dfunc->df_defer_var_idx > 0)
1073 invoke_defer_funcs(ectx);
1074
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001075 // No check for uf_refcount being zero, cannot think of a way that would
1076 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001077 --dfunc->df_ufunc->uf_calls;
1078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001080 entry = estack_pop();
1081 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001082 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001084 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1085 return FAIL;
1086
1087 // Clear the arguments.
1088 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1089 clear_tv(STACK_TV(idx));
1090
1091 // Clear local variables and temp values, but not the return value.
1092 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001093 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001095
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001096 // The return value should be on top of the stack. However, when aborting
1097 // it may not be there and ec_frame_idx is the top of the stack.
1098 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001099 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001100 ret_idx = 0;
1101
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001102 if (ectx->ec_outer_ref != NULL)
1103 {
1104 if (ectx->ec_outer_ref->or_outer_allocated)
1105 vim_free(ectx->ec_outer_ref->or_outer);
1106 partial_unref(ectx->ec_outer_ref->or_partial);
1107 vim_free(ectx->ec_outer_ref);
1108 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001109
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001110 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001111 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001112 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1113 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001114 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1115 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001116 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001117 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001118 floc = (void *)STACK_TV(ectx->ec_frame_idx
1119 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001120 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001121 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1122 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001123
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001124 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001125 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001126 else
1127 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001128 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001129 vim_free(floc);
1130 }
1131
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001132 if (ret_idx > 0)
1133 {
1134 // Reset the stack to the position before the call, with a spot for the
1135 // return value, moved there from above the frame.
1136 ectx->ec_stack.ga_len = top + 1;
1137 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1138 }
1139 else
1140 // Reset the stack to the position before the call.
1141 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001142
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001143 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001144 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001145 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146}
1147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148/*
1149 * Prepare arguments and rettv for calling a builtin or user function.
1150 */
1151 static int
1152call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1153{
1154 int idx;
1155 typval_T *tv;
1156
1157 // Move arguments from bottom of the stack to argvars[] and add terminator.
1158 for (idx = 0; idx < argcount; ++idx)
1159 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1160 argvars[argcount].v_type = VAR_UNKNOWN;
1161
1162 // Result replaces the arguments on the stack.
1163 if (argcount > 0)
1164 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001165 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 return FAIL;
1167 else
1168 ++ectx->ec_stack.ga_len;
1169
1170 // Default return value is zero.
1171 tv = STACK_TV_BOT(-1);
1172 tv->v_type = VAR_NUMBER;
1173 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001174 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175
1176 return OK;
1177}
1178
1179/*
1180 * Call a builtin function by index.
1181 */
1182 static int
1183call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1184{
1185 typval_T argvars[MAX_FUNC_ARGS];
1186 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001187 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001188 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001189 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190
1191 if (call_prepare(argcount, argvars, ectx) == FAIL)
1192 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001193 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001195 // Call the builtin function. Set "current_ectx" so that when it
1196 // recursively invokes call_def_function() a closure context can be set.
1197 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001199 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001200 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201
1202 // Clear the arguments.
1203 for (idx = 0; idx < argcount; ++idx)
1204 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001205
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001206 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001207 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 return OK;
1209}
1210
1211/*
1212 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001213 * If the function is compiled this will add a stack frame and set the
1214 * instruction pointer at the start of the function.
1215 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001216 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001217 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 */
1219 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001220call_ufunc(
1221 ufunc_T *ufunc,
1222 partial_T *pt,
1223 int argcount,
1224 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001225 isn_T *iptr,
1226 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227{
1228 typval_T argvars[MAX_FUNC_ARGS];
1229 funcexe_T funcexe;
1230 int error;
1231 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001232 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001233 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234
Bram Moolenaare99d4222021-06-13 14:01:26 +02001235 if (func_needs_compiling(ufunc, compile_type)
1236 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1237 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001238 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001239 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001240 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001241 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001242 if (error != FCERR_UNKNOWN)
1243 {
1244 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001245 semsg(_(e_too_many_arguments_for_function_str),
1246 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001247 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001248 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001249 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001250 return FAIL;
1251 }
1252
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001253 // The function has been compiled, can call it quickly. For a function
1254 // that was defined later: we can call it directly next time.
1255 if (iptr != NULL)
1256 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001257 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001258 iptr->isn_type = ISN_DCALL;
1259 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1260 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1261 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001262 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264
1265 if (call_prepare(argcount, argvars, ectx) == FAIL)
1266 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001267 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001268 funcexe.fe_evaluate = TRUE;
1269 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270
1271 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001273 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274
1275 // Clear the arguments.
1276 for (idx = 0; idx < argcount; ++idx)
1277 clear_tv(&argvars[idx]);
1278
1279 if (error != FCERR_NONE)
1280 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001281 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 return FAIL;
1283 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001284 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001285 // Error other than from calling the function itself.
1286 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 return OK;
1288}
1289
1290/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001291 * If command modifiers were applied restore them.
1292 */
1293 static void
1294may_restore_cmdmod(funclocal_T *funclocal)
1295{
1296 if (funclocal->floc_restore_cmdmod)
1297 {
1298 cmdmod.cmod_filter_regmatch.regprog = NULL;
1299 undo_cmdmod(&cmdmod);
1300 cmdmod = funclocal->floc_save_cmdmod;
1301 funclocal->floc_restore_cmdmod = FALSE;
1302 }
1303}
1304
1305/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001306 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1307 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001308 */
1309 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001310vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001311{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001312 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001313}
1314
1315/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 * Execute a function by "name".
1317 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001318 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 * Returns FAIL if not found without an error message.
1320 */
1321 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001322call_by_name(
1323 char_u *name,
1324 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001325 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001326 isn_T *iptr,
1327 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328{
1329 ufunc_T *ufunc;
1330
1331 if (builtin_function(name, -1))
1332 {
1333 int func_idx = find_internal_func(name);
1334
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001335 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001337 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 return FAIL;
1339 return call_bfunc(func_idx, argcount, ectx);
1340 }
1341
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001342 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001343
1344 if (ufunc == NULL)
1345 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001346 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001347
1348 if (script_autoload(name, TRUE))
1349 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001350 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001351
1352 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001353 return FAIL; // bail out if loading the script caused an error
1354 }
1355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001357 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001358 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001359 {
1360 int i;
1361 typval_T *argv = STACK_TV_BOT(0) - argcount;
1362
1363 // The function can change at runtime, check that the argument
1364 // types are correct.
1365 for (i = 0; i < argcount; ++i)
1366 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001367 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001368
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001369 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001370 type = ufunc->uf_arg_types[i];
1371 else if (ufunc->uf_va_type != NULL)
1372 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001373 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001374 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001375 return FAIL;
1376 }
1377 }
1378
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001379 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001380 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381
1382 return FAIL;
1383}
1384
1385 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001386call_partial(
1387 typval_T *tv,
1388 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001389 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001391 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001392 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001394 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001395 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396
1397 if (tv->v_type == VAR_PARTIAL)
1398 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001399 partial_T *pt = tv->vval.v_partial;
1400 int i;
1401
1402 if (pt->pt_argc > 0)
1403 {
1404 // Make space for arguments from the partial, shift the "argcount"
1405 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001406 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001407 return FAIL;
1408 for (i = 1; i <= argcount; ++i)
1409 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1410 ectx->ec_stack.ga_len += pt->pt_argc;
1411 argcount += pt->pt_argc;
1412
1413 // copy the arguments from the partial onto the stack
1414 for (i = 0; i < pt->pt_argc; ++i)
1415 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1416 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001417 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418
1419 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001420 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422 name = pt->pt_name;
1423 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001424 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001426 if (name != NULL)
1427 {
1428 char_u fname_buf[FLEN_FIXED + 1];
1429 char_u *tofree = NULL;
1430 int error = FCERR_NONE;
1431 char_u *fname;
1432
1433 // May need to translate <SNR>123_ to K_SNR.
1434 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1435 if (error != FCERR_NONE)
1436 res = FAIL;
1437 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001438 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001439 vim_free(tofree);
1440 }
1441
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001442 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 {
1444 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001445 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001446 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 return FAIL;
1448 }
1449 return OK;
1450}
1451
1452/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001453 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1454 * TRUE.
1455 */
1456 static int
1457error_if_locked(int lock, char *error)
1458{
1459 if (lock & (VAR_LOCKED | VAR_FIXED))
1460 {
1461 emsg(_(error));
1462 return TRUE;
1463 }
1464 return FALSE;
1465}
1466
1467/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001468 * Give an error if "tv" is not a number and return FAIL.
1469 */
1470 static int
1471check_for_number(typval_T *tv)
1472{
1473 if (tv->v_type != VAR_NUMBER)
1474 {
1475 semsg(_(e_expected_str_but_got_str),
1476 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1477 return FAIL;
1478 }
1479 return OK;
1480}
1481
1482/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001483 * Store "tv" in variable "name".
1484 * This is for s: and g: variables.
1485 */
1486 static void
1487store_var(char_u *name, typval_T *tv)
1488{
1489 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001490 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001491
Bram Moolenaard877a572021-04-01 19:42:48 +02001492 if (tv->v_lock)
1493 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001494 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001495 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001496 restore_funccal();
1497}
1498
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001499/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001500 * Convert "tv" to a string.
1501 * Return FAIL if not allowed.
1502 */
1503 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001504do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001505{
1506 if (tv->v_type != VAR_STRING)
1507 {
1508 char_u *str;
1509
1510 if (is_2string_any)
1511 {
1512 switch (tv->v_type)
1513 {
1514 case VAR_SPECIAL:
1515 case VAR_BOOL:
1516 case VAR_NUMBER:
1517 case VAR_FLOAT:
1518 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001519
1520 case VAR_LIST:
1521 if (tolerant)
1522 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001523 char_u *s, *e, *p;
1524 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001525
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001526 ga_init2(&ga, sizeof(char_u *), 1);
1527
1528 // Convert to NL separated items, then
1529 // escape the items and replace the NL with
1530 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001531 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001532 if (str == NULL)
1533 return FAIL;
1534 s = str;
1535 while ((e = vim_strchr(s, '\n')) != NULL)
1536 {
1537 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001538 p = vim_strsave_fnameescape(s,
1539 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001540 if (p != NULL)
1541 {
1542 ga_concat(&ga, p);
1543 ga_concat(&ga, (char_u *)" ");
1544 vim_free(p);
1545 }
1546 s = e + 1;
1547 }
1548 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001549 clear_tv(tv);
1550 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001551 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001552 return OK;
1553 }
1554 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001555 default: to_string_error(tv->v_type);
1556 return FAIL;
1557 }
1558 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001559 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001560 clear_tv(tv);
1561 tv->v_type = VAR_STRING;
1562 tv->vval.v_string = str;
1563 }
1564 return OK;
1565}
1566
1567/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001568 * When the value of "sv" is a null list of dict, allocate it.
1569 */
1570 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001571allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001572{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001573 typval_T *tv = sv->sv_tv;
1574
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001575 switch (tv->v_type)
1576 {
1577 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001578 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001579 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001580 break;
1581 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001582 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001583 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001584 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001585 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001586 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001587 (void)rettv_blob_alloc(tv);
1588 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001589 default:
1590 break;
1591 }
1592}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001593
Bram Moolenaare7525c52021-01-09 13:20:37 +01001594/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001595 * Return the character "str[index]" where "index" is the character index,
1596 * including composing characters.
1597 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001598 */
1599 char_u *
1600char_from_string(char_u *str, varnumber_T index)
1601{
1602 size_t nbyte = 0;
1603 varnumber_T nchar = index;
1604 size_t slen;
1605
1606 if (str == NULL)
1607 return NULL;
1608 slen = STRLEN(str);
1609
Bram Moolenaarff871402021-03-26 13:34:05 +01001610 // Do the same as for a list: a negative index counts from the end.
1611 // Optimization to check the first byte to be below 0x80 (and no composing
1612 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001613 if (index < 0)
1614 {
1615 int clen = 0;
1616
1617 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001618 {
1619 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1620 ++nbyte;
1621 else if (enc_utf8)
1622 nbyte += utfc_ptr2len(str + nbyte);
1623 else
1624 nbyte += mb_ptr2len(str + nbyte);
1625 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001626 nchar = clen + index;
1627 if (nchar < 0)
1628 // unlike list: index out of range results in empty string
1629 return NULL;
1630 }
1631
1632 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001633 {
1634 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1635 ++nbyte;
1636 else if (enc_utf8)
1637 nbyte += utfc_ptr2len(str + nbyte);
1638 else
1639 nbyte += mb_ptr2len(str + nbyte);
1640 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001641 if (nbyte >= slen)
1642 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001643 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001644}
1645
1646/*
1647 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001648 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001649 * If going over the end return "str_len".
1650 * If "idx" is negative count from the end, -1 is the last character.
1651 * When going over the start return -1.
1652 */
1653 static long
1654char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1655{
1656 varnumber_T nchar = idx;
1657 size_t nbyte = 0;
1658
1659 if (nchar >= 0)
1660 {
1661 while (nchar > 0 && nbyte < str_len)
1662 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001663 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001664 --nchar;
1665 }
1666 }
1667 else
1668 {
1669 nbyte = str_len;
1670 while (nchar < 0 && nbyte > 0)
1671 {
1672 --nbyte;
1673 nbyte -= mb_head_off(str, str + nbyte);
1674 ++nchar;
1675 }
1676 if (nchar < 0)
1677 return -1;
1678 }
1679 return (long)nbyte;
1680}
1681
1682/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001683 * Return the slice "str[first : last]" using character indexes. Composing
1684 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001685 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001686 * Return NULL when the result is empty.
1687 */
1688 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001689string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001690{
1691 long start_byte, end_byte;
1692 size_t slen;
1693
1694 if (str == NULL)
1695 return NULL;
1696 slen = STRLEN(str);
1697 start_byte = char_idx2byte(str, slen, first);
1698 if (start_byte < 0)
1699 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001700 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001701 end_byte = (long)slen;
1702 else
1703 {
1704 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001705 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001706 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001707 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001708 }
1709
1710 if (start_byte >= (long)slen || end_byte <= start_byte)
1711 return NULL;
1712 return vim_strnsave(str + start_byte, end_byte - start_byte);
1713}
1714
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001715/*
1716 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1717 * When "dfunc_idx" is negative don't give an error.
1718 * Returns NULL for an error.
1719 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001720 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001721get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001722{
1723 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001724 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1725 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001726 svar_T *sv;
1727
1728 if (sref->sref_seq != si->sn_script_seq)
1729 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001730 // The script was reloaded after the function was compiled, the
1731 // script_idx may not be valid.
1732 if (dfunc != NULL)
1733 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1734 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001735 return NULL;
1736 }
1737 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001738 if (sv->sv_name == NULL)
1739 {
1740 if (dfunc != NULL)
1741 emsg(_(e_script_variable_was_deleted));
1742 return NULL;
1743 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001744 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001745 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001746 if (dfunc != NULL)
1747 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001748 return NULL;
1749 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001750
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001751 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1752 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001753 {
1754 if (dfunc != NULL)
1755 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1756 return NULL;
1757 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001758 return sv;
1759}
1760
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001761/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001762 * Function passed to do_cmdline() for splitting a script joined by NL
1763 * characters.
1764 */
1765 static char_u *
1766get_split_sourceline(
1767 int c UNUSED,
1768 void *cookie,
1769 int indent UNUSED,
1770 getline_opt_T options UNUSED)
1771{
1772 source_cookie_T *sp = (source_cookie_T *)cookie;
1773 char_u *p;
1774 char_u *line;
1775
Bram Moolenaar20677332021-06-06 17:02:53 +02001776 p = vim_strchr(sp->nextline, '\n');
1777 if (p == NULL)
1778 {
1779 line = vim_strsave(sp->nextline);
1780 sp->nextline += STRLEN(sp->nextline);
1781 }
1782 else
1783 {
1784 line = vim_strnsave(sp->nextline, p - sp->nextline);
1785 sp->nextline = p + 1;
1786 }
1787 return line;
1788}
1789
1790/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 * Execute a function by "name".
1792 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001793 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 */
1795 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001796call_eval_func(
1797 char_u *name,
1798 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001799 ectx_T *ectx,
1800 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801{
Bram Moolenaared677f52020-08-12 16:38:10 +02001802 int called_emsg_before = called_emsg;
1803 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001805 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001806 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001808 dictitem_T *v;
1809
1810 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001811 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1812 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001813 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001814 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001815 return FAIL;
1816 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001817 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001819 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820}
1821
1822/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001823 * When a function reference is used, fill a partial with the information
1824 * needed, especially when it is used as a closure.
1825 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001826 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001827fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001828 partial_T *pt,
1829 ufunc_T *ufunc,
1830 loopvarinfo_T *lvi,
1831 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001832{
1833 pt->pt_func = ufunc;
1834 pt->pt_refcount = 1;
1835
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001836 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001837 {
1838 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1839 + ectx->ec_dfunc_idx;
1840
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001841 // The closure may need to find arguments and local variables of the
1842 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001843 pt->pt_outer.out_stack = &ectx->ec_stack;
1844 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001845 if (ectx->ec_outer_ref != NULL)
1846 {
1847 // The current context already has a context, link to that one.
1848 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1849 if (ectx->ec_outer_ref->or_partial != NULL)
1850 {
1851 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1852 ++pt->pt_outer.out_up_partial->pt_refcount;
1853 }
1854 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001855
Bram Moolenaarcc341812022-09-19 15:54:34 +01001856 if (lvi != NULL)
1857 {
1858 int depth;
1859
1860 // The closure may need to find variables defined inside a loop,
1861 // for every nested loop. A new reference is made every time,
1862 // ISN_ENDLOOP will check if they are actually used.
1863 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1864 {
1865 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1866 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1867 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1868 pt->pt_outer.out_loop[depth].var_count =
1869 lvi->lvi_loop[depth].var_count;
1870 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001871 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001872 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001873 else
1874 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001875
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001876 // If the function currently executing returns and the closure is still
1877 // being referenced, we need to make a copy of the context (arguments
1878 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001879 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001880 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001881 {
1882 vim_free(pt);
1883 return FAIL;
1884 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001885 // Extra variable keeps the count of closures created in the current
1886 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001887 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1888 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1889
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001890 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1891 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001892 ++pt->pt_refcount;
1893 ++ectx->ec_funcrefs.ga_len;
1894 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001895 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001896 return OK;
1897}
1898
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001899/*
1900 * Execute iptr->isn_arg.string as an Ex command.
1901 */
1902 static int
1903exec_command(isn_T *iptr)
1904{
1905 source_cookie_T cookie;
1906
1907 SOURCING_LNUM = iptr->isn_lnum;
1908 // Pass getsourceline to get an error for a missing ":end"
1909 // command.
1910 CLEAR_FIELD(cookie);
1911 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1912 if (do_cmdline(iptr->isn_arg.string,
1913 getsourceline, &cookie,
1914 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1915 || did_emsg)
1916 return FAIL;
1917 return OK;
1918}
1919
Bram Moolenaar89445512022-04-14 12:58:23 +01001920/*
1921 * If script "sid" is not loaded yet then load it now.
1922 * Caller must make sure "sid" is a valid script ID.
1923 * "loaded" is set to TRUE if the script had to be loaded.
1924 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1925 */
1926 int
1927may_load_script(int sid, int *loaded)
1928{
1929 scriptitem_T *si = SCRIPT_ITEM(sid);
1930
1931 if (si->sn_state == SN_STATE_NOT_LOADED)
1932 {
1933 *loaded = TRUE;
1934 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1935 {
1936 semsg(_(e_cant_open_file_str), si->sn_name);
1937 return FAIL;
1938 }
1939 }
1940 return OK;
1941}
1942
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001943// used for v_instr of typval of VAR_INSTR
1944struct instr_S {
1945 ectx_T *instr_ectx;
1946 isn_T *instr_instr;
1947};
1948
Bram Moolenaar4c137212021-04-19 16:48:48 +02001949// used for substitute_instr
1950typedef struct subs_expr_S {
1951 ectx_T *subs_ectx;
1952 isn_T *subs_instr;
1953 int subs_status;
1954} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001956// Set when calling do_debug().
1957static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001958static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001959
1960/*
1961 * When debugging lookup "name" and return the typeval.
1962 * When not found return NULL.
1963 */
1964 typval_T *
1965lookup_debug_var(char_u *name)
1966{
1967 int idx;
1968 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001969 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001970 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001971 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001972
1973 if (ectx == NULL)
1974 return NULL;
1975 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1976
1977 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001978 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001979 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001980 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1981
1982 // the variable name may be NULL when not available in this block
1983 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001984 return STACK_TV_VAR(idx);
1985 }
1986
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001987 // Go through argument names.
1988 ufunc = dfunc->df_ufunc;
1989 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1990 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1991 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1992 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1993 - varargs_off + idx);
1994 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1995 return STACK_TV(ectx->ec_frame_idx - 1);
1996
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001997 return NULL;
1998}
1999
Bram Moolenaar26a44842021-09-02 18:49:06 +02002000/*
2001 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2002 * breakpoint was set in that function or when there is any expression.
2003 */
2004 int
2005may_break_in_function(ufunc_T *ufunc)
2006{
2007 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2008}
2009
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002010 static void
2011handle_debug(isn_T *iptr, ectx_T *ectx)
2012{
2013 char_u *line;
2014 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2015 + ectx->ec_dfunc_idx)->df_ufunc;
2016 isn_T *ni;
2017 int end_lnum = iptr->isn_lnum;
2018 garray_T ga;
2019 int lnum;
2020
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002021 if (ex_nesting_level > debug_break_level)
2022 {
2023 linenr_T breakpoint;
2024
Bram Moolenaar26a44842021-09-02 18:49:06 +02002025 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002026 return;
2027
2028 // check for the next breakpoint if needed
2029 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002030 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002031 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2032 return;
2033 }
2034
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002035 SOURCING_LNUM = iptr->isn_lnum;
2036 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002037 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002038
2039 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2040 if (ni->isn_type == ISN_DEBUG
2041 || ni->isn_type == ISN_RETURN
2042 || ni->isn_type == ISN_RETURN_VOID)
2043 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002044 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002045 break;
2046 }
2047
2048 if (end_lnum > iptr->isn_lnum)
2049 {
2050 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002051 for (lnum = iptr->isn_lnum; lnum < end_lnum
2052 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002053 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002054 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002055
Bram Moolenaar303215d2021-07-07 20:10:43 +02002056 if (p == NULL)
2057 continue; // left over from continuation line
2058 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002059 if (*p == '#')
2060 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002061 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002062 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2063 if (STRNCMP(p, "def ", 4) == 0)
2064 break;
2065 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002066 line = ga_concat_strings(&ga, " ");
2067 vim_free(ga.ga_data);
2068 }
2069 else
2070 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002071
Dominique Pelle6e969552021-06-17 13:53:41 +02002072 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002073 debug_context = NULL;
2074
2075 if (end_lnum > iptr->isn_lnum)
2076 vim_free(line);
2077}
2078
Bram Moolenaar4c137212021-04-19 16:48:48 +02002079/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002080 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002081 * Returns OK, FAIL or NOTDONE (uncatchable error).
2082 */
2083 static int
2084execute_storeindex(isn_T *iptr, ectx_T *ectx)
2085{
2086 vartype_T dest_type = iptr->isn_arg.vartype;
2087 typval_T *tv;
2088 typval_T *tv_idx = STACK_TV_BOT(-2);
2089 typval_T *tv_dest = STACK_TV_BOT(-1);
2090 int status = OK;
2091
2092 // Stack contains:
2093 // -3 value to be stored
2094 // -2 index
2095 // -1 dict or list
2096 tv = STACK_TV_BOT(-3);
2097 SOURCING_LNUM = iptr->isn_lnum;
2098 if (dest_type == VAR_ANY)
2099 {
2100 dest_type = tv_dest->v_type;
2101 if (dest_type == VAR_DICT)
2102 status = do_2string(tv_idx, TRUE, FALSE);
2103 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2104 {
2105 emsg(_(e_number_expected));
2106 status = FAIL;
2107 }
2108 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002109
2110 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002111 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002112 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002113 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002114 long lidx = (long)tv_idx->vval.v_number;
2115 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002116
Bram Moolenaare08be092022-02-17 13:08:26 +00002117 if (list == NULL)
2118 {
2119 emsg(_(e_list_not_set));
2120 return FAIL;
2121 }
2122 if (lidx < 0 && list->lv_len + lidx >= 0)
2123 // negative index is relative to the end
2124 lidx = list->lv_len + lidx;
2125 if (lidx < 0 || lidx > list->lv_len)
2126 {
2127 semsg(_(e_list_index_out_of_range_nr), lidx);
2128 return FAIL;
2129 }
2130 if (lidx < list->lv_len)
2131 {
2132 listitem_T *li = list_find(list, lidx);
2133
2134 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002135 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002136 return FAIL;
2137 // overwrite existing list item
2138 clear_tv(&li->li_tv);
2139 li->li_tv = *tv;
2140 }
2141 else
2142 {
2143 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2144 return FAIL;
2145 // append to list, only fails when out of memory
2146 if (list_append_tv(list, tv) == FAIL)
2147 return NOTDONE;
2148 clear_tv(tv);
2149 }
2150 }
2151 else if (dest_type == VAR_DICT)
2152 {
2153 char_u *key = tv_idx->vval.v_string;
2154 dict_T *dict = tv_dest->vval.v_dict;
2155 dictitem_T *di;
2156
2157 SOURCING_LNUM = iptr->isn_lnum;
2158 if (dict == NULL)
2159 {
2160 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002161 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002162 }
2163 if (key == NULL)
2164 key = (char_u *)"";
2165 di = dict_find(dict, key, -1);
2166 if (di != NULL)
2167 {
2168 if (error_if_locked(di->di_tv.v_lock,
2169 e_cannot_change_dict_item))
2170 return FAIL;
2171 // overwrite existing value
2172 clear_tv(&di->di_tv);
2173 di->di_tv = *tv;
2174 }
2175 else
2176 {
2177 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2178 return FAIL;
2179 // add to dict, only fails when out of memory
2180 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2181 return NOTDONE;
2182 clear_tv(tv);
2183 }
2184 }
2185 else if (dest_type == VAR_BLOB)
2186 {
2187 long lidx = (long)tv_idx->vval.v_number;
2188 blob_T *blob = tv_dest->vval.v_blob;
2189 varnumber_T nr;
2190 int error = FALSE;
2191 int len;
2192
2193 if (blob == NULL)
2194 {
2195 emsg(_(e_blob_not_set));
2196 return FAIL;
2197 }
2198 len = blob_len(blob);
2199 if (lidx < 0 && len + lidx >= 0)
2200 // negative index is relative to the end
2201 lidx = len + lidx;
2202
2203 // Can add one byte at the end.
2204 if (lidx < 0 || lidx > len)
2205 {
2206 semsg(_(e_blob_index_out_of_range_nr), lidx);
2207 return FAIL;
2208 }
2209 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2210 return FAIL;
2211 nr = tv_get_number_chk(tv, &error);
2212 if (error)
2213 return FAIL;
2214 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002215 }
2216 else
2217 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002218 status = FAIL;
2219 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002220 }
2221 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002222
2223 clear_tv(tv_idx);
2224 clear_tv(tv_dest);
2225 ectx->ec_stack.ga_len -= 3;
2226 if (status == FAIL)
2227 {
2228 clear_tv(tv);
2229 return FAIL;
2230 }
2231 return OK;
2232}
2233
2234/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002235 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002236 */
2237 static int
2238execute_storerange(isn_T *iptr, ectx_T *ectx)
2239{
2240 typval_T *tv;
2241 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2242 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2243 typval_T *tv_dest = STACK_TV_BOT(-1);
2244 int status = OK;
2245
2246 // Stack contains:
2247 // -4 value to be stored
2248 // -3 first index or "none"
2249 // -2 second index or "none"
2250 // -1 destination list or blob
2251 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002252 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002253 if (tv_dest->v_type == VAR_LIST)
2254 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002255 long n1;
2256 long n2;
2257 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002258
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002259 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2260 if (tv_idx2->v_type == VAR_SPECIAL
2261 && tv_idx2->vval.v_number == VVAL_NONE)
2262 n2 = list_len(tv_dest->vval.v_list) - 1;
2263 else
2264 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2265
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002266 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002267 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002268 status = FAIL;
2269 else
2270 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002271 status = check_range_index_two(tv_dest->vval.v_list,
2272 &n1, li1, &n2, FALSE);
2273 if (status != FAIL)
2274 status = list_assign_range(
2275 tv_dest->vval.v_list,
2276 tv->vval.v_list,
2277 n1,
2278 n2,
2279 tv_idx2->v_type == VAR_SPECIAL,
2280 (char_u *)"=",
2281 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002282 }
2283 }
2284 else if (tv_dest->v_type == VAR_BLOB)
2285 {
2286 varnumber_T n1;
2287 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002288 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002289
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002290 n1 = tv_get_number_chk(tv_idx1, NULL);
2291 if (tv_idx2->v_type == VAR_SPECIAL
2292 && tv_idx2->vval.v_number == VVAL_NONE)
2293 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2294 else
2295 n2 = tv_get_number_chk(tv_idx2, NULL);
2296 bloblen = blob_len(tv_dest->vval.v_blob);
2297
2298 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2299 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002300 status = FAIL;
2301 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002302 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002303 }
2304 else
2305 {
2306 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002307 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002308 }
2309
2310 clear_tv(tv_idx1);
2311 clear_tv(tv_idx2);
2312 clear_tv(tv_dest);
2313 ectx->ec_stack.ga_len -= 4;
2314 clear_tv(tv);
2315
2316 return status;
2317}
2318
2319/*
2320 * Unlet item in list or dict variable.
2321 */
2322 static int
2323execute_unletindex(isn_T *iptr, ectx_T *ectx)
2324{
2325 typval_T *tv_idx = STACK_TV_BOT(-2);
2326 typval_T *tv_dest = STACK_TV_BOT(-1);
2327 int status = OK;
2328
2329 // Stack contains:
2330 // -2 index
2331 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002332 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002333 if (tv_dest->v_type == VAR_DICT)
2334 {
2335 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002336 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002337 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002338 semsg(_(e_expected_str_but_got_str),
2339 vartype_name(VAR_STRING),
2340 vartype_name(tv_idx->v_type));
2341 status = FAIL;
2342 }
2343 else
2344 {
2345 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002346 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002347 dictitem_T *di = NULL;
2348
2349 if (d != NULL && value_check_lock(
2350 d->dv_lock, NULL, FALSE))
2351 status = FAIL;
2352 else
2353 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002354 if (tv_idx->v_type == VAR_STRING)
2355 {
2356 key = tv_idx->vval.v_string;
2357 if (key == NULL)
2358 key = (char_u *)"";
2359 }
2360 else
2361 {
2362 key = tv_get_string(tv_idx);
2363 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002364 if (d != NULL)
2365 di = dict_find(d, key, (int)STRLEN(key));
2366 if (di == NULL)
2367 {
2368 // NULL dict is equivalent to empty dict
2369 semsg(_(e_key_not_present_in_dictionary),
2370 key);
2371 status = FAIL;
2372 }
2373 else if (var_check_fixed(di->di_flags,
2374 NULL, FALSE)
2375 || var_check_ro(di->di_flags,
2376 NULL, FALSE))
2377 status = FAIL;
2378 else
2379 dictitem_remove(d, di);
2380 }
2381 }
2382 }
2383 else if (tv_dest->v_type == VAR_LIST)
2384 {
2385 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002386 if (check_for_number(tv_idx) == FAIL)
2387 {
2388 status = FAIL;
2389 }
2390 else
2391 {
2392 list_T *l = tv_dest->vval.v_list;
2393 long n = (long)tv_idx->vval.v_number;
2394
2395 if (l != NULL && value_check_lock(
2396 l->lv_lock, NULL, FALSE))
2397 status = FAIL;
2398 else
2399 {
2400 listitem_T *li = list_find(l, n);
2401
2402 if (li == NULL)
2403 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002404 semsg(_(e_list_index_out_of_range_nr), n);
2405 status = FAIL;
2406 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002407 else
2408 listitem_remove(l, li);
2409 }
2410 }
2411 }
2412 else
2413 {
2414 status = FAIL;
2415 semsg(_(e_cannot_index_str),
2416 vartype_name(tv_dest->v_type));
2417 }
2418
2419 clear_tv(tv_idx);
2420 clear_tv(tv_dest);
2421 ectx->ec_stack.ga_len -= 2;
2422
2423 return status;
2424}
2425
2426/*
2427 * Unlet a range of items in a list variable.
2428 */
2429 static int
2430execute_unletrange(isn_T *iptr, ectx_T *ectx)
2431{
2432 // Stack contains:
2433 // -3 index1
2434 // -2 index2
2435 // -1 dict or list
2436 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2437 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2438 typval_T *tv_dest = STACK_TV_BOT(-1);
2439 int status = OK;
2440
2441 if (tv_dest->v_type == VAR_LIST)
2442 {
2443 // indexes must be a number
2444 SOURCING_LNUM = iptr->isn_lnum;
2445 if (check_for_number(tv_idx1) == FAIL
2446 || (tv_idx2->v_type != VAR_SPECIAL
2447 && check_for_number(tv_idx2) == FAIL))
2448 {
2449 status = FAIL;
2450 }
2451 else
2452 {
2453 list_T *l = tv_dest->vval.v_list;
2454 long n1 = (long)tv_idx1->vval.v_number;
2455 long n2 = tv_idx2->v_type == VAR_SPECIAL
2456 ? 0 : (long)tv_idx2->vval.v_number;
2457 listitem_T *li;
2458
2459 li = list_find_index(l, &n1);
2460 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002461 {
2462 semsg(_(e_list_index_out_of_range_nr),
2463 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002464 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002465 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002466 else
2467 {
2468 if (n1 < 0)
2469 n1 = list_idx_of_item(l, li);
2470 if (n2 < 0)
2471 {
2472 listitem_T *li2 = list_find(l, n2);
2473
2474 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002475 {
2476 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002477 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002478 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002479 else
2480 n2 = list_idx_of_item(l, li2);
2481 }
2482 if (status != FAIL
2483 && tv_idx2->v_type != VAR_SPECIAL
2484 && n2 < n1)
2485 {
2486 semsg(_(e_list_index_out_of_range_nr), n2);
2487 status = FAIL;
2488 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002489 if (status != FAIL)
2490 list_unlet_range(l, li, n1,
2491 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002492 }
2493 }
2494 }
2495 else
2496 {
2497 status = FAIL;
2498 SOURCING_LNUM = iptr->isn_lnum;
2499 semsg(_(e_cannot_index_str),
2500 vartype_name(tv_dest->v_type));
2501 }
2502
2503 clear_tv(tv_idx1);
2504 clear_tv(tv_idx2);
2505 clear_tv(tv_dest);
2506 ectx->ec_stack.ga_len -= 3;
2507
2508 return status;
2509}
2510
2511/*
2512 * Top of a for loop.
2513 */
2514 static int
2515execute_for(isn_T *iptr, ectx_T *ectx)
2516{
2517 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002518 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002519 typval_T *ltv = STACK_TV_BOT(-1);
2520 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002521 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002522
2523 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2524 return FAIL;
2525 if (ltv->v_type == VAR_LIST)
2526 {
2527 list_T *list = ltv->vval.v_list;
2528
2529 // push the next item from the list
2530 ++idxtv->vval.v_number;
2531 if (list == NULL
2532 || idxtv->vval.v_number >= list->lv_len)
2533 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002534 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002535 }
2536 else if (list->lv_first == &range_list_item)
2537 {
2538 // non-materialized range() list
2539 tv = STACK_TV_BOT(0);
2540 tv->v_type = VAR_NUMBER;
2541 tv->v_lock = 0;
2542 tv->vval.v_number = list_find_nr(
2543 list, idxtv->vval.v_number, NULL);
2544 ++ectx->ec_stack.ga_len;
2545 }
2546 else
2547 {
2548 listitem_T *li = list_find(list,
2549 idxtv->vval.v_number);
2550
2551 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2552 ++ectx->ec_stack.ga_len;
2553 }
2554 }
2555 else if (ltv->v_type == VAR_STRING)
2556 {
2557 char_u *str = ltv->vval.v_string;
2558
2559 // The index is for the last byte of the previous
2560 // character.
2561 ++idxtv->vval.v_number;
2562 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2563 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002564 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002565 }
2566 else
2567 {
2568 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2569
2570 // Push the next character from the string.
2571 tv = STACK_TV_BOT(0);
2572 tv->v_type = VAR_STRING;
2573 tv->vval.v_string = vim_strnsave(
2574 str + idxtv->vval.v_number, clen);
2575 ++ectx->ec_stack.ga_len;
2576 idxtv->vval.v_number += clen - 1;
2577 }
2578 }
2579 else if (ltv->v_type == VAR_BLOB)
2580 {
2581 blob_T *blob = ltv->vval.v_blob;
2582
2583 // When we get here the first time make a copy of the
2584 // blob, so that the iteration still works when it is
2585 // changed.
2586 if (idxtv->vval.v_number == -1 && blob != NULL)
2587 {
2588 blob_copy(blob, ltv);
2589 blob_unref(blob);
2590 blob = ltv->vval.v_blob;
2591 }
2592
2593 // The index is for the previous byte.
2594 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002595 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002596 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002597 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002598 }
2599 else
2600 {
2601 // Push the next byte from the blob.
2602 tv = STACK_TV_BOT(0);
2603 tv->v_type = VAR_NUMBER;
2604 tv->vval.v_number = blob_get(blob,
2605 idxtv->vval.v_number);
2606 ++ectx->ec_stack.ga_len;
2607 }
2608 }
2609 else
2610 {
2611 semsg(_(e_for_loop_on_str_not_supported),
2612 vartype_name(ltv->v_type));
2613 return FAIL;
2614 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002615
2616 if (jump)
2617 {
2618 // past the end of the list/string/blob, jump to "endfor"
2619 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2620 may_restore_cmdmod(&ectx->ec_funclocal);
2621 }
2622 else
2623 {
2624 // Store the current number of funcrefs, this may be used in
2625 // ISN_LOOPEND. The variable index is always one more than the loop
2626 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002627 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002628 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2629 }
2630
2631 return OK;
2632}
2633
2634/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002635 * Code for handling variables declared inside a loop and used in a closure.
2636 * This is very similar to what is done with funcstack_T. The difference is
2637 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2638 * scope of the block inside a loop and each loop may have its own.
2639 */
2640
2641// Double linked list of loopvars_T in use.
2642static loopvars_T *first_loopvars = NULL;
2643
2644 static void
2645add_loopvars_to_list(loopvars_T *loopvars)
2646{
2647 // Link in list of loopvarss.
2648 if (first_loopvars != NULL)
2649 first_loopvars->lvs_prev = loopvars;
2650 loopvars->lvs_next = first_loopvars;
2651 loopvars->lvs_prev = NULL;
2652 first_loopvars = loopvars;
2653}
2654
2655 static void
2656remove_loopvars_from_list(loopvars_T *loopvars)
2657{
2658 if (loopvars->lvs_prev == NULL)
2659 first_loopvars = loopvars->lvs_next;
2660 else
2661 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2662 if (loopvars->lvs_next != NULL)
2663 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2664}
2665
2666/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002667 * End of a for or while loop: Handle any variables used by a closure.
2668 */
2669 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002670execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002671{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002672 endloop_T *endloop = &iptr->isn_arg.endloop;
2673 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2674 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002675 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002676 garray_T *gap = &ectx->ec_funcrefs;
2677 int closure_in_use = FALSE;
2678 loopvars_T *loopvars;
2679 typval_T *stack;
2680 int idx;
2681
Bram Moolenaarcc341812022-09-19 15:54:34 +01002682 // Check if any created closure is still being referenced and loopvars have
2683 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002684 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2685 {
2686 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2687
Bram Moolenaarcc341812022-09-19 15:54:34 +01002688 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002689 {
2690 int refcount = pt->pt_refcount;
2691 int i;
2692
2693 // A Reference in a variable inside the loop doesn't count, it gets
2694 // unreferenced at the end of the loop.
2695 for (i = 0; i < endloop->end_var_count; ++i)
2696 {
2697 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2698
2699 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2700 --refcount;
2701 }
2702 if (refcount > 1)
2703 {
2704 closure_in_use = TRUE;
2705 break;
2706 }
2707 }
2708 }
2709
2710 // If no function reference were created since the start of the loop block
2711 // or it is no longer referenced there is nothing to do.
2712 if (!closure_in_use)
2713 return OK;
2714
2715 // A closure is using variables declared inside the loop.
2716 // Move them to the called function.
2717 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2718 if (loopvars == NULL)
2719 return FAIL;
2720
2721 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2722 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2723 loopvars->lvs_ga.ga_data = stack;
2724 if (stack == NULL)
2725 {
2726 vim_free(loopvars);
2727 return FAIL;
2728 }
2729 add_loopvars_to_list(loopvars);
2730
2731 // Move the variable values.
2732 for (idx = 0; idx < endloop->end_var_count; ++idx)
2733 {
2734 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2735
2736 *(stack + idx) = *tv;
2737 tv->v_type = VAR_UNKNOWN;
2738 }
2739
2740 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2741 {
2742 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2743
Bram Moolenaarcc341812022-09-19 15:54:34 +01002744 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002745 {
2746 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002747 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002748
Bram Moolenaarcc341812022-09-19 15:54:34 +01002749 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2750 pt->pt_outer.out_loop[depth].var_idx -=
2751 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002752 }
2753 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002754
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002755 return OK;
2756}
2757
2758/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002759 * Called when a partial is freed or its reference count goes down to one. The
2760 * loopvars may be the only reference to the partials in the local variables.
2761 * Go over all of them, the funcref and can be freed if all partials
2762 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002763 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002764 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002765 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002766loopvars_check_refcount(loopvars_T *loopvars)
2767{
2768 int i;
2769 garray_T *gap = &loopvars->lvs_ga;
2770 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002771 typval_T *stack = gap->ga_data;
2772
Bram Moolenaarcc341812022-09-19 15:54:34 +01002773 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2774 return FALSE;
2775 for (i = 0; i < gap->ga_len; ++i)
2776 {
2777 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2778
2779 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2780 && tv->vval.v_partial->pt_refcount == 1)
2781 {
2782 int depth;
2783
2784 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2785 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2786 ++done;
2787 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002788 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002789 if (done != loopvars->lvs_min_refcount)
2790 return FALSE;
2791
2792 // All partials referencing the loopvars have a reference count of
2793 // one, thus the loopvars is no longer of use.
2794 stack = gap->ga_data;
2795 for (i = 0; i < gap->ga_len; ++i)
2796 clear_tv(stack + i);
2797 vim_free(stack);
2798 remove_loopvars_from_list(loopvars);
2799 vim_free(loopvars);
2800 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002801}
2802
2803/*
2804 * For garbage collecting: set references in all variables referenced by
2805 * all loopvars.
2806 */
2807 int
2808set_ref_in_loopvars(int copyID)
2809{
2810 loopvars_T *loopvars;
2811
2812 for (loopvars = first_loopvars; loopvars != NULL;
2813 loopvars = loopvars->lvs_next)
2814 {
2815 typval_T *stack = loopvars->lvs_ga.ga_data;
2816 int i;
2817
2818 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2819 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2820 return TRUE; // abort
2821 }
2822 return FALSE;
2823}
2824
2825/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002826 * Load instruction for w:/b:/g:/t: variable.
2827 * "isn_type" is used instead of "iptr->isn_type".
2828 */
2829 static int
2830load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2831{
2832 dictitem_T *di = NULL;
2833 hashtab_T *ht = NULL;
2834 char namespace;
2835
2836 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2837 return NOTDONE;
2838 switch (isn_type)
2839 {
2840 case ISN_LOADG:
2841 ht = get_globvar_ht();
2842 namespace = 'g';
2843 break;
2844 case ISN_LOADB:
2845 ht = &curbuf->b_vars->dv_hashtab;
2846 namespace = 'b';
2847 break;
2848 case ISN_LOADW:
2849 ht = &curwin->w_vars->dv_hashtab;
2850 namespace = 'w';
2851 break;
2852 case ISN_LOADT:
2853 ht = &curtab->tp_vars->dv_hashtab;
2854 namespace = 't';
2855 break;
2856 default: // Cannot reach here
2857 return NOTDONE;
2858 }
2859 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2860
Bram Moolenaar06b77222022-01-25 15:51:56 +00002861 if (di == NULL)
2862 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002863 if (isn_type == ISN_LOADG)
2864 {
2865 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2866
2867 // g:Something could be a function
2868 if (ufunc != NULL)
2869 {
2870 typval_T *tv = STACK_TV_BOT(0);
2871
2872 ++ectx->ec_stack.ga_len;
2873 tv->v_type = VAR_FUNC;
2874 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2875 if (tv->vval.v_string == NULL)
2876 return FAIL;
2877 STRCPY(tv->vval.v_string, "g:");
2878 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2879 return OK;
2880 }
2881 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002882 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002883 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002884 // no check if the item exists in the script but
2885 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002886 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002887 else
2888 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002889 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002890 return FAIL;
2891 }
2892 else
2893 {
2894 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2895 ++ectx->ec_stack.ga_len;
2896 }
2897 return OK;
2898}
2899
2900/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002901 * Execute instructions in execution context "ectx".
2902 * Return OK or FAIL;
2903 */
2904 static int
2905exec_instructions(ectx_T *ectx)
2906{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002907 int ret = FAIL;
2908 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002909 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002910
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002911 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002912 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002913
Bram Moolenaarff652882021-05-16 15:24:49 +02002914 // Only catch exceptions in this instruction list.
2915 ectx->ec_trylevel_at_start = trylevel;
2916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 for (;;)
2918 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002919 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002921 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002922
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002923 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002924 {
2925 line_breakcheck();
2926 breakcheck_count = 0;
2927 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002928 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002929 {
2930 // Turn CTRL-C into an exception.
2931 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002932 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002933 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002934 did_throw = TRUE;
2935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002937 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002938 {
2939 // Turn an error message into an exception.
2940 did_emsg = FALSE;
2941 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002942 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002943 did_throw = TRUE;
2944 *msg_list = NULL;
2945 }
2946
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002947 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002949 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002950 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002951 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952
2953 // An exception jumps to the first catch, finally, or returns from
2954 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002955 while (index > 0)
2956 {
2957 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002958 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002959 break;
2960 // In the catch and finally block of this try we have to go up
2961 // one level.
2962 --index;
2963 trycmd = NULL;
2964 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002965 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002967 if (trycmd->tcd_in_catch)
2968 {
2969 // exception inside ":catch", jump to ":finally" once
2970 ectx->ec_iidx = trycmd->tcd_finally_idx;
2971 trycmd->tcd_finally_idx = 0;
2972 }
2973 else
2974 // jump to first ":catch"
2975 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002976 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002977 did_throw = FALSE; // don't come back here until :endtry
2978 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 }
2980 else
2981 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002982 // Not inside try or need to return from current functions.
2983 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002984 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002985 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002986 tv = STACK_TV_BOT(0);
2987 tv->v_type = VAR_NUMBER;
2988 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002989 ++ectx->ec_stack.ga_len;
2990 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002992 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002993 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002994 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002995 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 goto done;
2997 }
2998
Bram Moolenaar4c137212021-04-19 16:48:48 +02002999 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003000 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 }
3002 continue;
3003 }
3004
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003005 /*
3006 * Big switch on the instruction. Most compilers will be turning this
3007 * into an efficient lookup table, since the "case" values are an enum
3008 * with sequential numbers. It may look ugly, but it should be the
3009 * most efficient way.
3010 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003011 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 switch (iptr->isn_type)
3013 {
3014 // execute Ex command line
3015 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003016 if (exec_command(iptr) == FAIL)
3017 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 break;
3019
Bram Moolenaar20677332021-06-06 17:02:53 +02003020 // execute Ex command line split at NL characters.
3021 case ISN_EXEC_SPLIT:
3022 {
3023 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003024 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003025
3026 SOURCING_LNUM = iptr->isn_lnum;
3027 CLEAR_FIELD(cookie);
3028 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3029 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003030 line = get_split_sourceline(0, &cookie, 0, 0);
3031 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003032 get_split_sourceline, &cookie,
3033 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3034 == FAIL
3035 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003036 {
3037 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003038 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003039 }
3040 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003041 }
3042 break;
3043
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003044 // execute Ex command line that is only a range
3045 case ISN_EXECRANGE:
3046 {
3047 exarg_T ea;
3048 char *error = NULL;
3049
3050 CLEAR_FIELD(ea);
3051 ea.cmdidx = CMD_SIZE;
3052 ea.addr_type = ADDR_LINES;
3053 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003054 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003055 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003056 if (ea.cmd == NULL)
3057 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003058 // error is always NULL when using ADDR_LINES
3059 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003060 if (error != NULL)
3061 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003062 emsg(error);
3063 goto on_error;
3064 }
3065 }
3066 break;
3067
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003068 // Evaluate an expression with legacy syntax, push it onto the
3069 // stack.
3070 case ISN_LEGACY_EVAL:
3071 {
3072 char_u *arg = iptr->isn_arg.string;
3073 int res;
3074 int save_flags = cmdmod.cmod_flags;
3075
Bram Moolenaar35578162021-08-02 19:10:38 +02003076 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003077 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003078 tv = STACK_TV_BOT(0);
3079 init_tv(tv);
3080 cmdmod.cmod_flags |= CMOD_LEGACY;
3081 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3082 cmdmod.cmod_flags = save_flags;
3083 if (res == FAIL)
3084 goto on_error;
3085 ++ectx->ec_stack.ga_len;
3086 }
3087 break;
3088
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003089 // push typeval VAR_INSTR with instructions to be executed
3090 case ISN_INSTR:
3091 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003092 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003093 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003094 tv = STACK_TV_BOT(0);
3095 tv->vval.v_instr = ALLOC_ONE(instr_T);
3096 if (tv->vval.v_instr == NULL)
3097 goto on_error;
3098 ++ectx->ec_stack.ga_len;
3099
3100 tv->v_type = VAR_INSTR;
3101 tv->vval.v_instr->instr_ectx = ectx;
3102 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3103 }
3104 break;
3105
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003106 case ISN_SOURCE:
3107 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003108 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003109
Bram Moolenaar89445512022-04-14 12:58:23 +01003110 SOURCING_LNUM = iptr->isn_lnum;
3111 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003112 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003113 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003114 }
3115 break;
3116
Bram Moolenaar4c137212021-04-19 16:48:48 +02003117 // execute :substitute with an expression
3118 case ISN_SUBSTITUTE:
3119 {
3120 subs_T *subs = &iptr->isn_arg.subs;
3121 source_cookie_T cookie;
3122 struct subs_expr_S *save_instr = substitute_instr;
3123 struct subs_expr_S subs_instr;
3124 int res;
3125
3126 subs_instr.subs_ectx = ectx;
3127 subs_instr.subs_instr = subs->subs_instr;
3128 subs_instr.subs_status = OK;
3129 substitute_instr = &subs_instr;
3130
3131 SOURCING_LNUM = iptr->isn_lnum;
3132 // This is very much like ISN_EXEC
3133 CLEAR_FIELD(cookie);
3134 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3135 res = do_cmdline(subs->subs_cmd,
3136 getsourceline, &cookie,
3137 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3138 substitute_instr = save_instr;
3139
3140 if (res == FAIL || did_emsg
3141 || subs_instr.subs_status == FAIL)
3142 goto on_error;
3143 }
3144 break;
3145
3146 case ISN_FINISH:
3147 goto done;
3148
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003149 case ISN_REDIRSTART:
3150 // create a dummy entry for var_redir_str()
3151 if (alloc_redir_lval() == FAIL)
3152 goto on_error;
3153
3154 // The output is stored in growarray "redir_ga" until
3155 // redirection ends.
3156 init_redir_ga();
3157 redir_vname = 1;
3158 break;
3159
3160 case ISN_REDIREND:
3161 {
3162 char_u *res = get_clear_redir_ga();
3163
3164 // End redirection, put redirected text on the stack.
3165 clear_redir_lval();
3166 redir_vname = 0;
3167
Bram Moolenaar35578162021-08-02 19:10:38 +02003168 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003169 {
3170 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003171 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003172 }
3173 tv = STACK_TV_BOT(0);
3174 tv->v_type = VAR_STRING;
3175 tv->vval.v_string = res;
3176 ++ectx->ec_stack.ga_len;
3177 }
3178 break;
3179
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003180 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003181#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003182 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003183 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3184 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003185 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003186#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003187 break;
3188
3189 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003190#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003191 {
3192 exarg_T ea;
3193 int res;
3194
3195 CLEAR_FIELD(ea);
3196 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3197 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3198 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3199 --ectx->ec_stack.ga_len;
3200 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003201 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003202 res = cexpr_core(&ea, tv);
3203 clear_tv(tv);
3204 if (res == FAIL)
3205 goto on_error;
3206 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003207#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003208 break;
3209
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003210 // execute Ex command from pieces on the stack
3211 case ISN_EXECCONCAT:
3212 {
3213 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003214 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003215 int pass;
3216 int i;
3217 char_u *cmd = NULL;
3218 char_u *str;
3219
3220 for (pass = 1; pass <= 2; ++pass)
3221 {
3222 for (i = 0; i < count; ++i)
3223 {
3224 tv = STACK_TV_BOT(i - count);
3225 str = tv->vval.v_string;
3226 if (str != NULL && *str != NUL)
3227 {
3228 if (pass == 2)
3229 STRCPY(cmd + len, str);
3230 len += STRLEN(str);
3231 }
3232 if (pass == 2)
3233 clear_tv(tv);
3234 }
3235 if (pass == 1)
3236 {
3237 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003238 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003239 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003240 len = 0;
3241 }
3242 }
3243
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003244 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003245 do_cmdline_cmd(cmd);
3246 vim_free(cmd);
3247 }
3248 break;
3249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 // execute :echo {string} ...
3251 case ISN_ECHO:
3252 {
3253 int count = iptr->isn_arg.echo.echo_count;
3254 int atstart = TRUE;
3255 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003256 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257
3258 for (idx = 0; idx < count; ++idx)
3259 {
3260 tv = STACK_TV_BOT(idx - count);
3261 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3262 &atstart, &needclr);
3263 clear_tv(tv);
3264 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003265 if (needclr)
3266 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003267 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 }
3269 break;
3270
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003271 // :execute {string} ...
3272 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003273 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003274 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003275 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003276 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003277 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003278 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003279 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003280 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003281 {
3282 int count = iptr->isn_arg.number;
3283 garray_T ga;
3284 char_u buf[NUMBUFLEN];
3285 char_u *p;
3286 int len;
3287 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003288 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003289
3290 ga_init2(&ga, 1, 80);
3291 for (idx = 0; idx < count; ++idx)
3292 {
3293 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003294 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003295 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003296 if (tv->v_type == VAR_CHANNEL
3297 || tv->v_type == VAR_JOB)
3298 {
3299 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003300 semsg(_(e_using_invalid_value_as_string_str),
3301 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003302 break;
3303 }
3304 else
3305 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003306 }
3307 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003308 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003309
3310 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003311 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003312 failed = TRUE;
3313 else
3314 {
3315 if (ga.ga_len > 0)
3316 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3317 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3318 ga.ga_len += len;
3319 }
3320 clear_tv(tv);
3321 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003322 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003323 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003324 {
3325 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003326 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003327 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003328
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003329 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003330 {
3331 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003332 {
3333 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003334 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003335 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003336 {
3337 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003338 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003339 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003340 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003341 else
3342 {
3343 msg_sb_eol();
3344 if (iptr->isn_type == ISN_ECHOMSG)
3345 {
3346 msg_attr(ga.ga_data, echo_attr);
3347 out_flush();
3348 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003349#ifdef HAS_MESSAGE_WINDOW
3350 else if (iptr->isn_type == ISN_ECHOWINDOW)
3351 {
3352 start_echowindow();
3353 msg_attr(ga.ga_data, echo_attr);
3354 end_echowindow();
3355 }
3356#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003357 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3358 {
3359 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3360 TRUE);
3361 ui_write((char_u *)"\r\n", 2, TRUE);
3362 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003363 else
3364 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003365 SOURCING_LNUM = iptr->isn_lnum;
3366 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003367 }
3368 }
3369 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003370 ga_clear(&ga);
3371 }
3372 break;
3373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 // load local variable or argument
3375 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003376 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003377 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003379 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 break;
3381
3382 // load v: variable
3383 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003384 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003385 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003387 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 break;
3389
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003390 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 case ISN_LOADSCRIPT:
3392 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003393 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 svar_T *sv;
3395
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003396 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003397 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003398 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003399 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003400 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003401 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003403 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 }
3405 break;
3406
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003407 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003409 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003411 int sid = iptr->isn_arg.loadstore.ls_sid;
3412 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003413 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 if (di == NULL)
3417 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003418 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003419 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003420 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 }
3422 else
3423 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003424 if (iptr->isn_type == ISN_LOADEXPORT)
3425 {
3426 int idx = get_script_item_idx(sid, name, 0,
3427 NULL, NULL);
3428 svar_T *sv;
3429
3430 if (idx >= 0)
3431 {
3432 sv = ((svar_T *)SCRIPT_ITEM(sid)
3433 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003434 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003435 {
3436 SOURCING_LNUM = iptr->isn_lnum;
3437 semsg(_(e_item_not_exported_in_script_str),
3438 name);
3439 goto on_error;
3440 }
3441 }
3442 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003443 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003444 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003446 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 }
3448 }
3449 break;
3450
Bram Moolenaard3aac292020-04-19 14:32:17 +02003451 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003453 case ISN_LOADB:
3454 case ISN_LOADW:
3455 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003457 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003458
Bram Moolenaar06b77222022-01-25 15:51:56 +00003459 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003460 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003461 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003462 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 break;
3466
Bram Moolenaar03290b82020-12-19 16:30:44 +01003467 // load autoload variable
3468 case ISN_LOADAUTO:
3469 {
3470 char_u *name = iptr->isn_arg.string;
3471
Bram Moolenaar35578162021-08-02 19:10:38 +02003472 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003473 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003474 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003475 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003476 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003477 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003478 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003479 }
3480 break;
3481
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003482 // load g:/b:/w:/t: namespace
3483 case ISN_LOADGDICT:
3484 case ISN_LOADBDICT:
3485 case ISN_LOADWDICT:
3486 case ISN_LOADTDICT:
3487 {
3488 dict_T *d = NULL;
3489
3490 switch (iptr->isn_type)
3491 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003492 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3493 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3494 case ISN_LOADWDICT: d = curwin->w_vars; break;
3495 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003496 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003497 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003498 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003499 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003500 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003501 tv = STACK_TV_BOT(0);
3502 tv->v_type = VAR_DICT;
3503 tv->v_lock = 0;
3504 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003505 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003506 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003507 }
3508 break;
3509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 // load &option
3511 case ISN_LOADOPT:
3512 {
3513 typval_T optval;
3514 char_u *name = iptr->isn_arg.string;
3515
Bram Moolenaara8c17702020-04-01 21:17:24 +02003516 // This is not expected to fail, name is checked during
3517 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003518 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003519 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003520 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003521 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003523 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 }
3525 break;
3526
3527 // load $ENV
3528 case ISN_LOADENV:
3529 {
3530 typval_T optval;
3531 char_u *name = iptr->isn_arg.string;
3532
Bram Moolenaar35578162021-08-02 19:10:38 +02003533 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003534 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003535 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003536 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003538 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 }
3540 break;
3541
3542 // load @register
3543 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003544 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003545 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 tv = STACK_TV_BOT(0);
3547 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003548 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003549 // This may result in NULL, which should be equivalent to an
3550 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 tv->vval.v_string = get_reg_contents(
3552 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003553 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 break;
3555
3556 // store local variable
3557 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003558 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 tv = STACK_TV_VAR(iptr->isn_arg.number);
3560 clear_tv(tv);
3561 *tv = *STACK_TV_BOT(0);
3562 break;
3563
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003564 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003565 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003566 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003567 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003568 int sid = iptr->isn_arg.loadstore.ls_sid;
3569 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003570 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003571 dictitem_T *di = find_var_in_ht(ht, 0,
3572 iptr->isn_type == ISN_STORES
3573 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003574
Bram Moolenaar4c137212021-04-19 16:48:48 +02003575 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003576 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003577 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003578 {
3579 if (iptr->isn_type == ISN_STOREEXPORT)
3580 {
3581 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003582 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003583 goto on_error;
3584 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003585 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003586 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003587 else
3588 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003589 if (iptr->isn_type == ISN_STOREEXPORT)
3590 {
3591 int idx = get_script_item_idx(sid, name, 0,
3592 NULL, NULL);
3593
Bram Moolenaar06651632022-04-27 17:54:25 +01003594 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003595 if (idx >= 0)
3596 {
3597 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3598 ->sn_var_vals.ga_data) + idx;
3599
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003600 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003601 {
3602 semsg(_(e_item_not_exported_in_script_str),
3603 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003604 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003605 goto on_error;
3606 }
3607 }
3608 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003609 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003610 {
3611 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003612 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003613 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003614 clear_tv(&di->di_tv);
3615 di->di_tv = *STACK_TV_BOT(0);
3616 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003617 }
3618 break;
3619
3620 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 case ISN_STORESCRIPT:
3622 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003623 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3624 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003626 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003627 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003628 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003629 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003630
3631 // "const" and "final" are checked at compile time, locking
3632 // the value needs to be checked here.
3633 SOURCING_LNUM = iptr->isn_lnum;
3634 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003635 {
3636 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003637 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003638 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 clear_tv(sv->sv_tv);
3641 *sv->sv_tv = *STACK_TV_BOT(0);
3642 }
3643 break;
3644
3645 // store option
3646 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003647 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003649 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3650 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 long n = 0;
3652 char_u *s = NULL;
3653 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003654 char_u numbuf[NUMBUFLEN];
3655 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656
Bram Moolenaar4c137212021-04-19 16:48:48 +02003657 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 tv = STACK_TV_BOT(0);
3659 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003660 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003662 if (s == NULL)
3663 s = (char_u *)"";
3664 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003665 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3666 {
3667 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003668 // If the option can be set to a function reference or
3669 // a lambda and the passed value is a function
3670 // reference, then convert it to the name (string) of
3671 // the function reference.
3672 s = tv2string(tv, &tofree, numbuf, 0);
3673 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003674 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003675 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003676 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003677 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003678 goto on_error;
3679 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003682 // must be VAR_NUMBER, CHECKTYPE makes sure
3683 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003684 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003685 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003686 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 if (msg != NULL)
3688 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003689 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003691 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 }
3694 break;
3695
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003696 // store $ENV
3697 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003698 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003699 tv = STACK_TV_BOT(0);
3700 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3701 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003702 break;
3703
3704 // store @r
3705 case ISN_STOREREG:
3706 {
3707 int reg = iptr->isn_arg.number;
3708
Bram Moolenaar4c137212021-04-19 16:48:48 +02003709 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003710 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003711 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003712 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003713 }
3714 break;
3715
3716 // store v: variable
3717 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003718 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003719 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3720 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003721 // should not happen, type is checked when compiling
3722 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003723 break;
3724
Bram Moolenaard3aac292020-04-19 14:32:17 +02003725 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003727 case ISN_STOREB:
3728 case ISN_STOREW:
3729 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003731 dictitem_T *di;
3732 hashtab_T *ht;
3733 char_u *name = iptr->isn_arg.string + 2;
3734
Bram Moolenaard3aac292020-04-19 14:32:17 +02003735 switch (iptr->isn_type)
3736 {
3737 case ISN_STOREG:
3738 ht = get_globvar_ht();
3739 break;
3740 case ISN_STOREB:
3741 ht = &curbuf->b_vars->dv_hashtab;
3742 break;
3743 case ISN_STOREW:
3744 ht = &curwin->w_vars->dv_hashtab;
3745 break;
3746 case ISN_STORET:
3747 ht = &curtab->tp_vars->dv_hashtab;
3748 break;
3749 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003750 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752
Bram Moolenaar4c137212021-04-19 16:48:48 +02003753 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003754 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003756 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 else
3758 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003759 SOURCING_LNUM = iptr->isn_lnum;
3760 if (var_check_permission(di, name) == FAIL)
3761 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 clear_tv(&di->di_tv);
3763 di->di_tv = *STACK_TV_BOT(0);
3764 }
3765 }
3766 break;
3767
Bram Moolenaar03290b82020-12-19 16:30:44 +01003768 // store an autoload variable
3769 case ISN_STOREAUTO:
3770 SOURCING_LNUM = iptr->isn_lnum;
3771 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3772 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003773 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003774 break;
3775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 // store number in local variable
3777 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003778 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 clear_tv(tv);
3780 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003781 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 break;
3783
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003784 // store value in list or dict variable
3785 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003786 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003787 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003788
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003789 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003790 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003791 if (res == NOTDONE)
3792 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003793 }
3794 break;
3795
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003796 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003797 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003798 if (execute_storerange(iptr, ectx) == FAIL)
3799 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003800 break;
3801
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003802 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003803 case ISN_LOADOUTER:
3804 case ISN_STOREOUTER:
3805 {
3806 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003807 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3808 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003809
3810 while (depth > 1 && outer != NULL)
3811 {
3812 outer = outer->out_up;
3813 --depth;
3814 }
3815 if (outer == NULL)
3816 {
3817 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003818 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3819 || ectx->ec_outer_ref == NULL)
3820 // Possibly :def function called from legacy
3821 // context.
3822 emsg(_(e_closure_called_from_invalid_context));
3823 else
3824 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003825 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003826 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003827 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003828 // Variable declared in loop. May be copied if the
3829 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01003830 tv = ((typval_T *)outer->out_loop[-depth - 1]
3831 .stack->ga_data)
3832 + outer->out_loop[-depth - 1].var_idx
3833 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003834 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003835 // Variable declared in a function. May be copied if
3836 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003837 tv = ((typval_T *)outer->out_stack->ga_data)
3838 + outer->out_frame_idx + STACK_FRAME_SIZE
3839 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003840 if (iptr->isn_type == ISN_LOADOUTER)
3841 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003842 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003843 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003844 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003845 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003846 }
3847 else
3848 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003849 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003850 clear_tv(tv);
3851 *tv = *STACK_TV_BOT(0);
3852 }
3853 }
3854 break;
3855
Bram Moolenaar752fc692021-01-04 21:57:11 +01003856 // unlet item in list or dict variable
3857 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003858 if (execute_unletindex(iptr, ectx) == FAIL)
3859 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003860 break;
3861
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003862 // unlet range of items in list variable
3863 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003864 if (execute_unletrange(iptr, ectx) == FAIL)
3865 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003866 break;
3867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 // push constant
3869 case ISN_PUSHNR:
3870 case ISN_PUSHBOOL:
3871 case ISN_PUSHSPEC:
3872 case ISN_PUSHF:
3873 case ISN_PUSHS:
3874 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003875 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003876 case ISN_PUSHCHANNEL:
3877 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003878 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003879 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003881 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003882 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 switch (iptr->isn_type)
3884 {
3885 case ISN_PUSHNR:
3886 tv->v_type = VAR_NUMBER;
3887 tv->vval.v_number = iptr->isn_arg.number;
3888 break;
3889 case ISN_PUSHBOOL:
3890 tv->v_type = VAR_BOOL;
3891 tv->vval.v_number = iptr->isn_arg.number;
3892 break;
3893 case ISN_PUSHSPEC:
3894 tv->v_type = VAR_SPECIAL;
3895 tv->vval.v_number = iptr->isn_arg.number;
3896 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 case ISN_PUSHF:
3898 tv->v_type = VAR_FLOAT;
3899 tv->vval.v_float = iptr->isn_arg.fnumber;
3900 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 case ISN_PUSHBLOB:
3902 blob_copy(iptr->isn_arg.blob, tv);
3903 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003904 case ISN_PUSHFUNC:
3905 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003906 if (iptr->isn_arg.string == NULL)
3907 tv->vval.v_string = NULL;
3908 else
3909 tv->vval.v_string =
3910 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003911 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003912 case ISN_PUSHCHANNEL:
3913#ifdef FEAT_JOB_CHANNEL
3914 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003915 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003916#endif
3917 break;
3918 case ISN_PUSHJOB:
3919#ifdef FEAT_JOB_CHANNEL
3920 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003921 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003922#endif
3923 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 default:
3925 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003926 tv->vval.v_string = iptr->isn_arg.string == NULL
3927 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 }
3929 break;
3930
Bram Moolenaar06b77222022-01-25 15:51:56 +00003931 case ISN_AUTOLOAD:
3932 {
3933 char_u *name = iptr->isn_arg.string;
3934
3935 (void)script_autoload(name, FALSE);
3936 if (find_func(name, TRUE))
3937 {
3938 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3939 goto theend;
3940 tv = STACK_TV_BOT(0);
3941 tv->v_lock = 0;
3942 ++ectx->ec_stack.ga_len;
3943 tv->v_type = VAR_FUNC;
3944 tv->vval.v_string = vim_strsave(name);
3945 }
3946 else
3947 {
3948 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3949
3950 if (res == NOTDONE)
3951 goto theend;
3952 if (res == FAIL)
3953 goto on_error;
3954 }
3955 }
3956 break;
3957
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003958 case ISN_UNLET:
3959 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3960 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003961 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003962 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003963 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003964 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003965 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003966
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003967 case ISN_LOCKUNLOCK:
3968 {
3969 typval_T *lval_root_save = lval_root;
3970 int res;
3971
3972 // Stack has the local variable, argument the whole :lock
3973 // or :unlock command, like ISN_EXEC.
3974 --ectx->ec_stack.ga_len;
3975 lval_root = STACK_TV_BOT(0);
3976 res = exec_command(iptr);
3977 clear_tv(lval_root);
3978 lval_root = lval_root_save;
3979 if (res == FAIL)
3980 goto on_error;
3981 }
3982 break;
3983
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003984 case ISN_LOCKCONST:
3985 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3986 break;
3987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 // create a list from items on the stack; uses a single allocation
3989 // for the list header and the items
3990 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003991 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003992 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 break;
3994
3995 // create a dict from items on the stack
3996 case ISN_NEWDICT:
3997 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003998 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004000 SOURCING_LNUM = iptr->isn_lnum;
4001 res = exe_newdict(iptr->isn_arg.number, ectx);
4002 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004003 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004004 if (res == MAYBE)
4005 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 }
4007 break;
4008
LemonBoy372bcce2022-04-25 12:43:20 +01004009 case ISN_CONCAT:
4010 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4011 goto theend;
4012 break;
4013
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004014 // create a partial with NULL value
4015 case ISN_NEWPARTIAL:
4016 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4017 goto theend;
4018 ++ectx->ec_stack.ga_len;
4019 tv = STACK_TV_BOT(-1);
4020 tv->v_type = VAR_PARTIAL;
4021 tv->v_lock = 0;
4022 tv->vval.v_partial = NULL;
4023 break;
4024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 // call a :def function
4026 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004027 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004028 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4029 NULL,
4030 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004031 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004032 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 break;
4034
4035 // call a builtin function
4036 case ISN_BCALL:
4037 SOURCING_LNUM = iptr->isn_lnum;
4038 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4039 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004040 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004041 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 break;
4043
4044 // call a funcref or partial
4045 case ISN_PCALL:
4046 {
4047 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4048 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004049 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050
4051 SOURCING_LNUM = iptr->isn_lnum;
4052 if (pfunc->cpf_top)
4053 {
4054 // funcref is above the arguments
4055 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4056 }
4057 else
4058 {
4059 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004060 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004061 partial_tv = *STACK_TV_BOT(0);
4062 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004064 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004065 if (tv == &partial_tv)
4066 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004068 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 }
4070 break;
4071
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004072 case ISN_PCALL_END:
4073 // PCALL finished, arguments have been consumed and replaced by
4074 // the return value. Now clear the funcref from the stack,
4075 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004076 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004077 clear_tv(STACK_TV_BOT(-1));
4078 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4079 break;
4080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 // call a user defined function or funcref/partial
4082 case ISN_UCALL:
4083 {
4084 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4085
4086 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004087 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004088 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004089 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 }
4091 break;
4092
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004093 // :defer func(arg)
4094 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004095 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004096 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4097 goto on_error;
4098 break;
4099
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004100 // return from a :def function call without a value
4101 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02004102 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004103 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004104 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004105 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004106 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004107 tv->vval.v_number = 0;
4108 tv->v_lock = 0;
4109 // FALLTHROUGH
4110
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004111 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 case ISN_RETURN:
4113 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004114 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004115 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004116
4117 if (trystack->ga_len > 0)
4118 trycmd = ((trycmd_T *)trystack->ga_data)
4119 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004120 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004121 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004123 // jump to ":finally" or ":endtry"
4124 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004125 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004126 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004127 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 trycmd->tcd_return = TRUE;
4129 }
4130 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004131 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 }
4133 break;
4134
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004135 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 case ISN_FUNCREF:
4137 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004138 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4139 ufunc_T *ufunc;
4140 funcref_T *funcref = &iptr->isn_arg.funcref;
4141 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004144 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004145 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004146 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004147 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004148 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004149 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004150 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004151 {
4152 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4153 + funcref->fr_dfunc_idx;
4154
4155 ufunc = pt_dfunc->df_ufunc;
4156 }
4157 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004158 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004159 if (ufunc == NULL)
4160 {
4161 SOURCING_LNUM = iptr->isn_lnum;
4162 iemsg("ufunc unexpectedly NULL for FUNCREF");
4163 goto theend;
4164 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004165 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004166 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004167 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004168 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004170 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 tv->vval.v_partial = pt;
4172 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004173 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 }
4175 break;
4176
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004177 // Create a global function from a lambda.
4178 case ISN_NEWFUNC:
4179 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004180 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004181
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004182 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004183 arg->nfa_global, &arg->nfa_loopvar_info,
4184 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004185 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004186 }
4187 break;
4188
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004189 // List functions
4190 case ISN_DEF:
4191 if (iptr->isn_arg.string == NULL)
4192 list_functions(NULL);
4193 else
4194 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004195 exarg_T ea;
4196 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004197
4198 CLEAR_FIELD(ea);
4199 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004200 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4201 define_function(&ea, NULL, &lines_to_free);
4202 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004203 }
4204 break;
4205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 // jump if a condition is met
4207 case ISN_JUMP:
4208 {
4209 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004210 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 int jump = TRUE;
4212
4213 if (when != JUMP_ALWAYS)
4214 {
4215 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004216 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004217 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004218 || when == JUMP_IF_COND_TRUE)
4219 {
4220 SOURCING_LNUM = iptr->isn_lnum;
4221 jump = tv_get_bool_chk(tv, &error);
4222 if (error)
4223 goto on_error;
4224 }
4225 else
4226 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004227 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004229 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 {
4231 // drop the value from the stack
4232 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004233 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 }
4235 }
4236 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004237 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 }
4239 break;
4240
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004241 // "while": jump to end if a condition is false
4242 case ISN_WHILE:
4243 {
4244 int error = FALSE;
4245 int jump = TRUE;
4246
4247 tv = STACK_TV_BOT(-1);
4248 SOURCING_LNUM = iptr->isn_lnum;
4249 jump = !tv_get_bool_chk(tv, &error);
4250 if (error)
4251 goto on_error;
4252 // drop the value from the stack
4253 clear_tv(tv);
4254 --ectx->ec_stack.ga_len;
4255 if (jump)
4256 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4257
4258 // Store the current funccal count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004259 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004260 tv = STACK_TV_VAR(
4261 iptr->isn_arg.whileloop.while_funcref_idx);
4262 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4263 }
4264 break;
4265
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004266 // Jump if an argument with a default value was already set and not
4267 // v:none.
4268 case ISN_JUMP_IF_ARG_SET:
4269 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
4270 if (tv->v_type != VAR_UNKNOWN
4271 && !(tv->v_type == VAR_SPECIAL
4272 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004273 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004274 break;
4275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 // top of a for loop
4277 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004278 if (execute_for(iptr, ectx) == FAIL)
4279 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 break;
4281
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004282 // end of a for or while loop
4283 case ISN_ENDLOOP:
4284 if (execute_endloop(iptr, ectx) == FAIL)
4285 goto theend;
4286 break;
4287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 // start of ":try" block
4289 case ISN_TRY:
4290 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004291 trycmd_T *trycmd = NULL;
4292
Bram Moolenaar35578162021-08-02 19:10:38 +02004293 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004294 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004295 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4296 + ectx->ec_trystack.ga_len;
4297 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004299 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004300 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4301 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004302 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004303 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004304 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004305 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004306 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004307 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 }
4309 break;
4310
4311 case ISN_PUSHEXC:
4312 if (current_exception == NULL)
4313 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004314 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004316 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004318 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004319 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004321 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004323 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 tv->vval.v_string = vim_strsave(
4325 (char_u *)current_exception->value);
4326 break;
4327
4328 case ISN_CATCH:
4329 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004330 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004331 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332
Bram Moolenaar4c137212021-04-19 16:48:48 +02004333 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004334 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004336 trycmd->tcd_caught = TRUE;
4337 trycmd->tcd_did_throw = FALSE;
4338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004340 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 catch_exception(current_exception);
4342 }
4343 break;
4344
Bram Moolenaarc150c092021-02-13 15:02:46 +01004345 case ISN_TRYCONT:
4346 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004347 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004348 trycont_T *trycont = &iptr->isn_arg.trycont;
4349 int i;
4350 trycmd_T *trycmd;
4351 int iidx = trycont->tct_where;
4352
4353 if (trystack->ga_len < trycont->tct_levels)
4354 {
4355 siemsg("TRYCONT: expected %d levels, found %d",
4356 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004357 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004358 }
4359 // Make :endtry jump to any outer try block and the last
4360 // :endtry inside the loop to the loop start.
4361 for (i = trycont->tct_levels; i > 0; --i)
4362 {
4363 trycmd = ((trycmd_T *)trystack->ga_data)
4364 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004365 // Add one to tcd_cont to be able to jump to
4366 // instruction with index zero.
4367 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004368 iidx = trycmd->tcd_finally_idx == 0
4369 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004370 }
4371 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004372 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004373 }
4374 break;
4375
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004376 case ISN_FINALLY:
4377 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004378 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004379 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4380 + trystack->ga_len - 1;
4381
4382 // Reset the index to avoid a return statement jumps here
4383 // again.
4384 trycmd->tcd_finally_idx = 0;
4385 break;
4386 }
4387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 // end of ":try" block
4389 case ISN_ENDTRY:
4390 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004391 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004392 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393
Bram Moolenaar06651632022-04-27 17:54:25 +01004394 --trystack->ga_len;
4395 --trylevel;
4396 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4397 if (trycmd->tcd_did_throw)
4398 did_throw = TRUE;
4399 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004401 // discard the exception
4402 if (caught_stack == current_exception)
4403 caught_stack = caught_stack->caught;
4404 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004406
4407 if (trycmd->tcd_return)
4408 goto func_return;
4409
4410 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4411 {
4412 --ectx->ec_stack.ga_len;
4413 clear_tv(STACK_TV_BOT(0));
4414 }
4415 if (trycmd->tcd_cont != 0)
4416 // handling :continue: jump to outer try block or
4417 // start of the loop
4418 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 }
4420 break;
4421
4422 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004423 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004424 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004425
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004426 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4427 {
4428 // throwing an exception while using "silent!" causes
4429 // the function to abort but not display an error.
4430 tv = STACK_TV_BOT(-1);
4431 clear_tv(tv);
4432 tv->v_type = VAR_NUMBER;
4433 tv->vval.v_number = 0;
4434 goto done;
4435 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004436 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004437 tv = STACK_TV_BOT(0);
4438 if (tv->vval.v_string == NULL
4439 || *skipwhite(tv->vval.v_string) == NUL)
4440 {
4441 vim_free(tv->vval.v_string);
4442 SOURCING_LNUM = iptr->isn_lnum;
4443 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004444 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004445 }
4446
4447 // Inside a "catch" we need to first discard the caught
4448 // exception.
4449 if (trystack->ga_len > 0)
4450 {
4451 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4452 + trystack->ga_len - 1;
4453 if (trycmd->tcd_caught && current_exception != NULL)
4454 {
4455 // discard the exception
4456 if (caught_stack == current_exception)
4457 caught_stack = caught_stack->caught;
4458 discard_current_exception();
4459 trycmd->tcd_caught = FALSE;
4460 }
4461 }
4462
Bram Moolenaar90a57162022-02-12 14:23:17 +00004463 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004464 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4465 == FAIL)
4466 {
4467 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004468 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004469 }
4470 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 break;
4473
4474 // compare with special values
4475 case ISN_COMPAREBOOL:
4476 case ISN_COMPARESPECIAL:
4477 {
4478 typval_T *tv1 = STACK_TV_BOT(-2);
4479 typval_T *tv2 = STACK_TV_BOT(-1);
4480 varnumber_T arg1 = tv1->vval.v_number;
4481 varnumber_T arg2 = tv2->vval.v_number;
4482 int res;
4483
Bram Moolenaar06651632022-04-27 17:54:25 +01004484 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4485 res = arg1 == arg2;
4486 else
4487 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488
Bram Moolenaar4c137212021-04-19 16:48:48 +02004489 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 tv1->v_type = VAR_BOOL;
4491 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4492 }
4493 break;
4494
Bram Moolenaar7a222242022-03-01 19:23:24 +00004495 case ISN_COMPARENULL:
4496 {
4497 typval_T *tv1 = STACK_TV_BOT(-2);
4498 typval_T *tv2 = STACK_TV_BOT(-1);
4499 int res;
4500
4501 res = typval_compare_null(tv1, tv2);
4502 if (res == MAYBE)
4503 goto on_error;
4504 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4505 res = !res;
4506 clear_tv(tv1);
4507 clear_tv(tv2);
4508 --ectx->ec_stack.ga_len;
4509 tv1->v_type = VAR_BOOL;
4510 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4511 }
4512 break;
4513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 // Operation with two number arguments
4515 case ISN_OPNR:
4516 case ISN_COMPARENR:
4517 {
4518 typval_T *tv1 = STACK_TV_BOT(-2);
4519 typval_T *tv2 = STACK_TV_BOT(-1);
4520 varnumber_T arg1 = tv1->vval.v_number;
4521 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004522 varnumber_T res = 0;
4523 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004525 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4526 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4527 {
4528 if (arg2 < 0)
4529 {
4530 SOURCING_LNUM = iptr->isn_lnum;
4531 emsg(_(e_bitshift_ops_must_be_postive));
4532 goto on_error;
4533 }
4534 }
4535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 switch (iptr->isn_arg.op.op_type)
4537 {
4538 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004539 case EXPR_DIV: if (arg2 == 0)
4540 div_zero = TRUE;
4541 else
4542 res = arg1 / arg2;
4543 break;
4544 case EXPR_REM: if (arg2 == 0)
4545 div_zero = TRUE;
4546 else
4547 res = arg1 % arg2;
4548 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 case EXPR_SUB: res = arg1 - arg2; break;
4550 case EXPR_ADD: res = arg1 + arg2; break;
4551
4552 case EXPR_EQUAL: res = arg1 == arg2; break;
4553 case EXPR_NEQUAL: res = arg1 != arg2; break;
4554 case EXPR_GREATER: res = arg1 > arg2; break;
4555 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4556 case EXPR_SMALLER: res = arg1 < arg2; break;
4557 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004558 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4559 res = 0;
4560 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004561 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004562 break;
4563 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4564 res = 0;
4565 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004566 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004567 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004568 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 }
4570
Bram Moolenaar4c137212021-04-19 16:48:48 +02004571 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 if (iptr->isn_type == ISN_COMPARENR)
4573 {
4574 tv1->v_type = VAR_BOOL;
4575 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4576 }
4577 else
4578 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004579 if (div_zero)
4580 {
4581 SOURCING_LNUM = iptr->isn_lnum;
4582 emsg(_(e_divide_by_zero));
4583 goto on_error;
4584 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 }
4586 break;
4587
4588 // Computation with two float arguments
4589 case ISN_OPFLOAT:
4590 case ISN_COMPAREFLOAT:
4591 {
4592 typval_T *tv1 = STACK_TV_BOT(-2);
4593 typval_T *tv2 = STACK_TV_BOT(-1);
4594 float_T arg1 = tv1->vval.v_float;
4595 float_T arg2 = tv2->vval.v_float;
4596 float_T res = 0;
4597 int cmp = FALSE;
4598
4599 switch (iptr->isn_arg.op.op_type)
4600 {
4601 case EXPR_MULT: res = arg1 * arg2; break;
4602 case EXPR_DIV: res = arg1 / arg2; break;
4603 case EXPR_SUB: res = arg1 - arg2; break;
4604 case EXPR_ADD: res = arg1 + arg2; break;
4605
4606 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4607 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4608 case EXPR_GREATER: cmp = arg1 > arg2; break;
4609 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4610 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4611 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4612 default: cmp = 0; break;
4613 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004614 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615 if (iptr->isn_type == ISN_COMPAREFLOAT)
4616 {
4617 tv1->v_type = VAR_BOOL;
4618 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4619 }
4620 else
4621 tv1->vval.v_float = res;
4622 }
4623 break;
4624
4625 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004626 case ISN_COMPAREDICT:
4627 case ISN_COMPAREFUNC:
4628 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 case ISN_COMPAREBLOB:
4630 {
4631 typval_T *tv1 = STACK_TV_BOT(-2);
4632 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004633 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4634 int ic = iptr->isn_arg.op.op_ic;
4635 int res = FALSE;
4636 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637
Bram Moolenaar265f8112021-12-19 12:33:05 +00004638 SOURCING_LNUM = iptr->isn_lnum;
4639 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004641 status = typval_compare_list(tv1, tv2,
4642 exprtype, ic, &res);
4643 }
4644 else if (iptr->isn_type == ISN_COMPAREDICT)
4645 {
4646 status = typval_compare_dict(tv1, tv2,
4647 exprtype, ic, &res);
4648 }
4649 else if (iptr->isn_type == ISN_COMPAREFUNC)
4650 {
4651 status = typval_compare_func(tv1, tv2,
4652 exprtype, ic, &res);
4653 }
4654 else if (iptr->isn_type == ISN_COMPARESTRING)
4655 {
4656 status = typval_compare_string(tv1, tv2,
4657 exprtype, ic, &res);
4658 }
4659 else
4660 {
4661 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004663 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 clear_tv(tv1);
4665 clear_tv(tv2);
4666 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004667 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4668 if (status == FAIL)
4669 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 }
4671 break;
4672
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 case ISN_COMPAREANY:
4674 {
4675 typval_T *tv1 = STACK_TV_BOT(-2);
4676 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004677 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004679 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680
Bram Moolenaareb26f432020-09-14 16:50:05 +02004681 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004682 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004684 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004685 if (status == FAIL)
4686 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 }
4688 break;
4689
4690 case ISN_ADDLIST:
4691 case ISN_ADDBLOB:
4692 {
4693 typval_T *tv1 = STACK_TV_BOT(-2);
4694 typval_T *tv2 = STACK_TV_BOT(-1);
4695
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004696 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004698 {
4699 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4700 && tv1->vval.v_list != NULL)
4701 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4702 NULL);
4703 else
4704 eval_addlist(tv1, tv2);
4705 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 else
4707 eval_addblob(tv1, tv2);
4708 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004709 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 }
4711 break;
4712
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004713 case ISN_LISTAPPEND:
4714 {
4715 typval_T *tv1 = STACK_TV_BOT(-2);
4716 typval_T *tv2 = STACK_TV_BOT(-1);
4717 list_T *l = tv1->vval.v_list;
4718
4719 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004720 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004721 if (l == NULL)
4722 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004723 emsg(_(e_cannot_add_to_null_list));
4724 goto on_error;
4725 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004726 if (value_check_lock(l->lv_lock, NULL, FALSE))
4727 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004728 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004729 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004730 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004731 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004732 }
4733 break;
4734
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004735 case ISN_BLOBAPPEND:
4736 {
4737 typval_T *tv1 = STACK_TV_BOT(-2);
4738 typval_T *tv2 = STACK_TV_BOT(-1);
4739 blob_T *b = tv1->vval.v_blob;
4740 int error = FALSE;
4741 varnumber_T n;
4742
4743 // add a number to a blob
4744 if (b == NULL)
4745 {
4746 SOURCING_LNUM = iptr->isn_lnum;
4747 emsg(_(e_cannot_add_to_null_blob));
4748 goto on_error;
4749 }
4750 n = tv_get_number_chk(tv2, &error);
4751 if (error)
4752 goto on_error;
4753 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004754 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004755 }
4756 break;
4757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 // Computation with two arguments of unknown type
4759 case ISN_OPANY:
4760 {
4761 typval_T *tv1 = STACK_TV_BOT(-2);
4762 typval_T *tv2 = STACK_TV_BOT(-1);
4763 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 int error = FALSE;
4766
4767 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4768 {
4769 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4770 {
4771 eval_addlist(tv1, tv2);
4772 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004773 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774 break;
4775 }
4776 else if (tv1->v_type == VAR_BLOB
4777 && tv2->v_type == VAR_BLOB)
4778 {
4779 eval_addblob(tv1, tv2);
4780 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004781 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782 break;
4783 }
4784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 if (tv1->v_type == VAR_FLOAT)
4786 {
4787 f1 = tv1->vval.v_float;
4788 n1 = 0;
4789 }
4790 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004792 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 n1 = tv_get_number_chk(tv1, &error);
4794 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004795 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 if (tv2->v_type == VAR_FLOAT)
4797 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799 if (tv2->v_type == VAR_FLOAT)
4800 {
4801 f2 = tv2->vval.v_float;
4802 n2 = 0;
4803 }
4804 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 {
4806 n2 = tv_get_number_chk(tv2, &error);
4807 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004808 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 if (tv1->v_type == VAR_FLOAT)
4810 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 // if there is a float on either side the result is a float
4813 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4814 {
4815 switch (iptr->isn_arg.op.op_type)
4816 {
4817 case EXPR_MULT: f1 = f1 * f2; break;
4818 case EXPR_DIV: f1 = f1 / f2; break;
4819 case EXPR_SUB: f1 = f1 - f2; break;
4820 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004821 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004822 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004823 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 }
4825 clear_tv(tv1);
4826 clear_tv(tv2);
4827 tv1->v_type = VAR_FLOAT;
4828 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004829 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 }
4831 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004833 int failed = FALSE;
4834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 switch (iptr->isn_arg.op.op_type)
4836 {
4837 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004838 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4839 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004840 goto on_error;
4841 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 case EXPR_SUB: n1 = n1 - n2; break;
4843 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004844 default: n1 = num_modulus(n1, n2, &failed);
4845 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004846 goto on_error;
4847 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 }
4849 clear_tv(tv1);
4850 clear_tv(tv2);
4851 tv1->v_type = VAR_NUMBER;
4852 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004853 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854 }
4855 }
4856 break;
4857
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004858 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004859 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004860 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004861 int is_slice = iptr->isn_type == ISN_STRSLICE;
4862 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004863 char_u *res;
4864
4865 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004866 // string slice: string is at stack-3, first index at
4867 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004868 if (is_slice)
4869 {
4870 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004871 n1 = tv->vval.v_number;
4872 }
4873
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004874 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004875 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004876
Bram Moolenaar4c137212021-04-19 16:48:48 +02004877 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004878 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004879 if (is_slice)
4880 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004881 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004882 else
4883 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004884 // single character (including composing characters).
4885 // If the index is too big or negative the result is
4886 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004887 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004888 vim_free(tv->vval.v_string);
4889 tv->vval.v_string = res;
4890 }
4891 break;
4892
4893 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004894 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004895 case ISN_BLOBINDEX:
4896 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004898 int is_slice = iptr->isn_type == ISN_LISTSLICE
4899 || iptr->isn_type == ISN_BLOBSLICE;
4900 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4901 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004902 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004903 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904
4905 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004906 // list slice: list is at stack-3, indexes at stack-2 and
4907 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004908 // Same for blob.
4909 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910
4911 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004912 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004914
4915 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 {
Bram Moolenaared591872020-08-15 22:14:53 +02004917 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004918 n1 = tv->vval.v_number;
4919 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 }
Bram Moolenaared591872020-08-15 22:14:53 +02004921
Bram Moolenaar4c137212021-04-19 16:48:48 +02004922 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004923 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004924 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004925 if (is_blob)
4926 {
4927 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4928 n1, n2, FALSE, tv) == FAIL)
4929 goto on_error;
4930 }
4931 else
4932 {
4933 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4934 n1, n2, FALSE, tv, TRUE) == FAIL)
4935 goto on_error;
4936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 }
4938 break;
4939
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004940 case ISN_ANYINDEX:
4941 case ISN_ANYSLICE:
4942 {
4943 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4944 typval_T *var1, *var2;
4945 int res;
4946
4947 // index: composite is at stack-2, index at stack-1
4948 // slice: composite is at stack-3, indexes at stack-2 and
4949 // stack-1
4950 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004951 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004952 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4953 goto on_error;
4954 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4955 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004956 res = eval_index_inner(tv, is_slice, var1, var2,
4957 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004958 clear_tv(var1);
4959 if (is_slice)
4960 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004961 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004962 if (res == FAIL)
4963 goto on_error;
4964 }
4965 break;
4966
Bram Moolenaar9af78762020-06-16 11:34:42 +02004967 case ISN_SLICE:
4968 {
4969 list_T *list;
4970 int count = iptr->isn_arg.number;
4971
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004972 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004973 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004974 list = tv->vval.v_list;
4975
4976 // no error for short list, expect it to be checked earlier
4977 if (list != NULL && list->lv_len >= count)
4978 {
4979 list_T *newlist = list_slice(list,
4980 count, list->lv_len - 1);
4981
4982 if (newlist != NULL)
4983 {
4984 list_unref(list);
4985 tv->vval.v_list = newlist;
4986 ++newlist->lv_refcount;
4987 }
4988 }
4989 }
4990 break;
4991
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004992 case ISN_GETITEM:
4993 {
4994 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004995 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004996
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004997 // Get list item: list is at stack-1, push item.
4998 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004999 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5000 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005001
Bram Moolenaar35578162021-08-02 19:10:38 +02005002 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005003 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005004 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005005 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005006
5007 // Useful when used in unpack assignment. Reset at
5008 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005009 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005010 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005011 }
5012 break;
5013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 case ISN_MEMBER:
5015 {
5016 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005017 char_u *key;
5018 dictitem_T *di;
5019
5020 // dict member: dict is at stack-2, key at stack-1
5021 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005022 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005023 dict = tv->vval.v_dict;
5024
5025 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005026 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005027 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005028 if (key == NULL)
5029 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005030
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005031 if ((di = dict_find(dict, key, -1)) == NULL)
5032 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005033 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005034 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005035
5036 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005037 // stack contents makes sense and the dict stack is
5038 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005039 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005040 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005041 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005042 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005043 tv->v_type = VAR_NUMBER;
5044 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005045 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005046 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005047 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005048 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005049 // Put the dict used on the dict stack, it might be used by
5050 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005051 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005052 if (dict_stack_save(tv) == FAIL)
5053 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005054 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005055 }
5056 break;
5057
5058 // dict member with string key
5059 case ISN_STRINGMEMBER:
5060 {
5061 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 dictitem_T *di;
5063
5064 tv = STACK_TV_BOT(-1);
5065 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5066 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005067 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005068 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005069 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 }
5071 dict = tv->vval.v_dict;
5072
5073 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5074 == NULL)
5075 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005076 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00005077 semsg(_(e_key_not_present_in_dictionary),
5078 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005079 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005081 // Put the dict used on the dict stack, it might be used by
5082 // a dict function later.
5083 if (dict_stack_save(tv) == FAIL)
5084 goto on_fatal_error;
5085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005087 }
5088 break;
5089
5090 case ISN_CLEARDICT:
5091 dict_stack_drop();
5092 break;
5093
5094 case ISN_USEDICT:
5095 {
5096 typval_T *dict_tv = dict_stack_get_tv();
5097
5098 // Turn "dict.Func" into a partial for "Func" bound to
5099 // "dict". Don't do this when "Func" is already a partial
5100 // that was bound explicitly (pt_auto is FALSE).
5101 tv = STACK_TV_BOT(-1);
5102 if (dict_tv != NULL
5103 && dict_tv->v_type == VAR_DICT
5104 && dict_tv->vval.v_dict != NULL
5105 && (tv->v_type == VAR_FUNC
5106 || (tv->v_type == VAR_PARTIAL
5107 && (tv->vval.v_partial->pt_auto
5108 || tv->vval.v_partial->pt_dict == NULL))))
5109 dict_tv->vval.v_dict =
5110 make_partial(dict_tv->vval.v_dict, tv);
5111 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 }
5113 break;
5114
5115 case ISN_NEGATENR:
5116 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005117 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005118 if (tv->v_type == VAR_FLOAT)
5119 tv->vval.v_float = -tv->vval.v_float;
5120 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005121 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122 break;
5123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 case ISN_CHECKTYPE:
5125 {
5126 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005127 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005128 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005130 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005131 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005132 if (!ectx->ec_where.wt_variable)
5133 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005134 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005135 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005136 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005137 if (r == FAIL)
5138 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005139 if (!ectx->ec_where.wt_variable)
5140 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005141
5142 // number 0 is FALSE, number 1 is TRUE
5143 if (tv->v_type == VAR_NUMBER
5144 && ct->ct_type->tt_type == VAR_BOOL
5145 && (tv->vval.v_number == 0
5146 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005148 tv->v_type = VAR_BOOL;
5149 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005150 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151 }
5152 }
5153 break;
5154
Bram Moolenaar9af78762020-06-16 11:34:42 +02005155 case ISN_CHECKLEN:
5156 {
5157 int min_len = iptr->isn_arg.checklen.cl_min_len;
5158 list_T *list = NULL;
5159
5160 tv = STACK_TV_BOT(-1);
5161 if (tv->v_type == VAR_LIST)
5162 list = tv->vval.v_list;
5163 if (list == NULL || list->lv_len < min_len
5164 || (list->lv_len > min_len
5165 && !iptr->isn_arg.checklen.cl_more_OK))
5166 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005167 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005168 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005169 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005170 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005171 }
5172 }
5173 break;
5174
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005175 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005176 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005177 break;
5178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005180 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 {
5182 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005183 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005185 if (iptr->isn_type == ISN_2BOOL)
5186 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005187 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005188 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005189 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005190 n = !n;
5191 }
5192 else
5193 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005194 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005195 SOURCING_LNUM = iptr->isn_lnum;
5196 n = tv_get_bool_chk(tv, &error);
5197 if (error)
5198 goto on_error;
5199 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 clear_tv(tv);
5201 tv->v_type = VAR_BOOL;
5202 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5203 }
5204 break;
5205
5206 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005207 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005208 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005209 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5210 iptr->isn_type == ISN_2STRING_ANY,
5211 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005212 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213 break;
5214
Bram Moolenaar08597872020-12-10 19:43:40 +01005215 case ISN_RANGE:
5216 {
5217 exarg_T ea;
5218 char *errormsg;
5219
Bram Moolenaarece0b872021-01-08 20:40:45 +01005220 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005221 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005222 ea.addr_type = ADDR_LINES;
5223 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005224 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005225 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005226 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005227
Bram Moolenaar35578162021-08-02 19:10:38 +02005228 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005229 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005230 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005231 tv = STACK_TV_BOT(-1);
5232 tv->v_type = VAR_NUMBER;
5233 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005234 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005235 }
5236 break;
5237
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005238 case ISN_PUT:
5239 {
5240 int regname = iptr->isn_arg.put.put_regname;
5241 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5242 char_u *expr = NULL;
5243 int dir = FORWARD;
5244
Bram Moolenaar08597872020-12-10 19:43:40 +01005245 if (lnum < -2)
5246 {
5247 // line number was put on the stack by ISN_RANGE
5248 tv = STACK_TV_BOT(-1);
5249 curwin->w_cursor.lnum = tv->vval.v_number;
5250 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5251 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005252 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005253 }
5254 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005255 // :put! above cursor
5256 dir = BACKWARD;
5257 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005258 {
5259 curwin->w_cursor.lnum = lnum;
5260 if (lnum == 0)
5261 // check_cursor() below will move to line 1
5262 dir = BACKWARD;
5263 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005264
5265 if (regname == '=')
5266 {
5267 tv = STACK_TV_BOT(-1);
5268 if (tv->v_type == VAR_STRING)
5269 expr = tv->vval.v_string;
5270 else
5271 {
5272 expr = typval2string(tv, TRUE); // allocates value
5273 clear_tv(tv);
5274 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005275 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005276 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005277 check_cursor();
5278 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5279 vim_free(expr);
5280 }
5281 break;
5282
Bram Moolenaar02194d22020-10-24 23:08:38 +02005283 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005284 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5285 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5286 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5287 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005288 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5289 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005290 break;
5291
Bram Moolenaar02194d22020-10-24 23:08:38 +02005292 case ISN_CMDMOD_REV:
5293 // filter regprog is owned by the instruction, don't free it
5294 cmdmod.cmod_filter_regmatch.regprog = NULL;
5295 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005296 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5297 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005298 break;
5299
Bram Moolenaar792f7862020-11-23 08:31:18 +01005300 case ISN_UNPACK:
5301 {
5302 int count = iptr->isn_arg.unpack.unp_count;
5303 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5304 list_T *l;
5305 listitem_T *li;
5306 int i;
5307
5308 // Check there is a valid list to unpack.
5309 tv = STACK_TV_BOT(-1);
5310 if (tv->v_type != VAR_LIST)
5311 {
5312 SOURCING_LNUM = iptr->isn_lnum;
5313 emsg(_(e_for_argument_must_be_sequence_of_lists));
5314 goto on_error;
5315 }
5316 l = tv->vval.v_list;
5317 if (l == NULL
5318 || l->lv_len < (semicolon ? count - 1 : count))
5319 {
5320 SOURCING_LNUM = iptr->isn_lnum;
5321 emsg(_(e_list_value_does_not_have_enough_items));
5322 goto on_error;
5323 }
5324 else if (!semicolon && l->lv_len > count)
5325 {
5326 SOURCING_LNUM = iptr->isn_lnum;
5327 emsg(_(e_list_value_has_more_items_than_targets));
5328 goto on_error;
5329 }
5330
5331 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005332 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005333 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005334 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005335
5336 // Variable after semicolon gets a list with the remaining
5337 // items.
5338 if (semicolon)
5339 {
5340 list_T *rem_list =
5341 list_alloc_with_items(l->lv_len - count + 1);
5342
5343 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005344 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005345 tv = STACK_TV_BOT(-count);
5346 tv->vval.v_list = rem_list;
5347 ++rem_list->lv_refcount;
5348 tv->v_lock = 0;
5349 li = l->lv_first;
5350 for (i = 0; i < count - 1; ++i)
5351 li = li->li_next;
5352 for (i = 0; li != NULL; ++i)
5353 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005354 typval_T tvcopy;
5355
5356 copy_tv(&li->li_tv, &tvcopy);
5357 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005358 li = li->li_next;
5359 }
5360 --count;
5361 }
5362
5363 // Produce the values in reverse order, first item last.
5364 li = l->lv_first;
5365 for (i = 0; i < count; ++i)
5366 {
5367 tv = STACK_TV_BOT(-i - 1);
5368 copy_tv(&li->li_tv, tv);
5369 li = li->li_next;
5370 }
5371
5372 list_unref(l);
5373 }
5374 break;
5375
Bram Moolenaarb2049902021-01-24 12:53:53 +01005376 case ISN_PROF_START:
5377 case ISN_PROF_END:
5378 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005379#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005380 funccall_T cookie;
5381 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005382 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005383 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005384
Bram Moolenaarca16c602022-09-06 18:57:08 +01005385 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005386 if (iptr->isn_type == ISN_PROF_START)
5387 {
5388 func_line_start(&cookie, iptr->isn_lnum);
5389 // if we get here the instruction is executed
5390 func_line_exec(&cookie);
5391 }
5392 else
5393 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005394#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005395 }
5396 break;
5397
Bram Moolenaare99d4222021-06-13 14:01:26 +02005398 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005399 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005400 break;
5401
Bram Moolenaar389df252020-07-09 21:20:47 +02005402 case ISN_SHUFFLE:
5403 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005404 typval_T tmp_tv;
5405 int item = iptr->isn_arg.shuffle.shfl_item;
5406 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005407
5408 tmp_tv = *STACK_TV_BOT(-item);
5409 for ( ; up > 0 && item > 1; --up)
5410 {
5411 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5412 --item;
5413 }
5414 *STACK_TV_BOT(-item) = tmp_tv;
5415 }
5416 break;
5417
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005419 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005420 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005421 ectx->ec_where.wt_index = 0;
5422 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 break;
5424 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005425 continue;
5426
Bram Moolenaard032f342020-07-18 18:13:02 +02005427func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005428 // Restore previous function. If the frame pointer is where we started
5429 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005430 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005431 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005432
Bram Moolenaar4c137212021-04-19 16:48:48 +02005433 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005434 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005435 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005436 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005437
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005438on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005439 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005440 // If "emsg_silent" is set then ignore the error, unless it was set
5441 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005442 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005443 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005444 {
5445 // If a sequence of instructions causes an error while ":silent!"
5446 // was used, restore the stack length and jump ahead to restoring
5447 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005448 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005449 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005450 while (ectx->ec_stack.ga_len
5451 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005452 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005453 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005454 clear_tv(STACK_TV_BOT(0));
5455 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005456 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5457 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005458 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005459 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005460 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005461on_fatal_error:
5462 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005463 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005464 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005465 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466 }
5467
5468done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005469 ret = OK;
5470theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005471 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005472
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005473 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005474 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5475 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005476}
5477
5478/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005479 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005480 * "rettv".
5481 * Return OK or FAIL.
5482 */
5483 int
5484exe_typval_instr(typval_T *tv, typval_T *rettv)
5485{
5486 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5487 isn_T *save_instr = ectx->ec_instr;
5488 int save_iidx = ectx->ec_iidx;
5489 int res;
5490
LemonBoyf3b48952022-05-05 13:53:03 +01005491 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5492 // even when the compilation fails.
5493 rettv->v_type = VAR_UNKNOWN;
5494
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005495 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5496 res = exec_instructions(ectx);
5497 if (res == OK)
5498 {
5499 *rettv = *STACK_TV_BOT(-1);
5500 --ectx->ec_stack.ga_len;
5501 }
5502
5503 ectx->ec_instr = save_instr;
5504 ectx->ec_iidx = save_iidx;
5505
5506 return res;
5507}
5508
5509/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005510 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5511 * "substitute_instr".
5512 */
5513 char_u *
5514exe_substitute_instr(void)
5515{
5516 ectx_T *ectx = substitute_instr->subs_ectx;
5517 isn_T *save_instr = ectx->ec_instr;
5518 int save_iidx = ectx->ec_iidx;
5519 char_u *res;
5520
5521 ectx->ec_instr = substitute_instr->subs_instr;
5522 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005523 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005524 typval_T *tv = STACK_TV_BOT(-1);
5525
Bram Moolenaar27523602021-06-05 21:36:19 +02005526 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005527 --ectx->ec_stack.ga_len;
5528 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005529 }
5530 else
5531 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005532 substitute_instr->subs_status = FAIL;
5533 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535
Bram Moolenaar4c137212021-04-19 16:48:48 +02005536 ectx->ec_instr = save_instr;
5537 ectx->ec_iidx = save_iidx;
5538
5539 return res;
5540}
5541
5542/*
5543 * Call a "def" function from old Vim script.
5544 * Return OK or FAIL.
5545 */
5546 int
5547call_def_function(
5548 ufunc_T *ufunc,
5549 int argc_arg, // nr of arguments
5550 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005551 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005552 partial_T *partial, // optional partial for context
Bram Moolenaar58779852022-09-06 18:31:14 +01005553 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005554 typval_T *rettv) // return value
5555{
5556 ectx_T ectx; // execution context
5557 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005558 int partial_argc = partial == NULL
5559 || (flags & DEF_USE_PT_ARGV) == 0
5560 ? 0 : partial->pt_argc;
5561 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005562 typval_T *tv;
5563 int idx;
5564 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005565 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005566 sctx_T save_current_sctx = current_sctx;
5567 int did_emsg_before = did_emsg_cumul + did_emsg;
5568 int save_suppress_errthrow = suppress_errthrow;
5569 msglist_T **saved_msg_list = NULL;
5570 msglist_T *private_msg_list = NULL;
5571 int save_emsg_silent_def = emsg_silent_def;
5572 int save_did_emsg_def = did_emsg_def;
5573 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005574 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005575
5576// Get pointer to item in the stack.
5577#undef STACK_TV
5578#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5579
5580// Get pointer to item at the bottom of the stack, -1 is the bottom.
5581#undef STACK_TV_BOT
5582#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5583
5584// Get pointer to a local variable on the stack. Negative for arguments.
5585#undef STACK_TV_VAR
5586#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5587
5588 if (ufunc->uf_def_status == UF_NOT_COMPILED
5589 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005590 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5591 && compile_def_function(ufunc, FALSE,
5592 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005593 {
5594 if (did_emsg_cumul + did_emsg == did_emsg_before)
5595 semsg(_(e_function_is_not_compiled_str),
5596 printable_func_name(ufunc));
5597 return FAIL;
5598 }
5599
5600 {
5601 // Check the function was really compiled.
5602 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5603 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005604 if (dfunc->df_ufunc == NULL)
5605 {
5606 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5607 return FAIL;
5608 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005609 if (INSTRUCTIONS(dfunc) == NULL)
5610 {
5611 iemsg("using call_def_function() on not compiled function");
5612 return FAIL;
5613 }
5614 }
5615
5616 // If depth of calling is getting too high, don't execute the function.
5617 orig_funcdepth = funcdepth_get();
5618 if (funcdepth_increment() == FAIL)
5619 return FAIL;
5620
5621 CLEAR_FIELD(ectx);
5622 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5623 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005624 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005625 {
5626 funcdepth_decrement();
5627 return FAIL;
5628 }
5629 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5630 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5631 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005632 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005633
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005634 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005635 if (idx > 0 && ufunc->uf_va_name == NULL)
5636 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005637 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005638 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005639 goto failed_early;
5640 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005641 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005642 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005643 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005644 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005645 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005646 goto failed_early;
5647 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005648
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005649 // Put values from the partial and arguments on the stack, but no more than
5650 // what the function expects. A lambda can be called with more arguments
5651 // than it uses.
5652 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005653 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5654 ++idx)
5655 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005656 int argv_idx = idx - partial_argc;
5657
5658 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005659 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005660 && tv->v_type == VAR_SPECIAL
5661 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005662 {
5663 // Use the default value.
5664 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5665 }
5666 else
5667 {
5668 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5669 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005670 ufunc->uf_arg_types[idx], tv,
5671 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005672 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005673 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005674 }
5675 ++ectx.ec_stack.ga_len;
5676 }
5677
5678 // Turn varargs into a list. Empty list if no args.
5679 if (ufunc->uf_va_name != NULL)
5680 {
5681 int vararg_count = argc - ufunc->uf_args.ga_len;
5682
5683 if (vararg_count < 0)
5684 vararg_count = 0;
5685 else
5686 argc -= vararg_count;
5687 if (exe_newlist(vararg_count, &ectx) == FAIL)
5688 goto failed_early;
5689
5690 // Check the type of the list items.
5691 tv = STACK_TV_BOT(-1);
5692 if (ufunc->uf_va_type != NULL
5693 && ufunc->uf_va_type != &t_list_any
5694 && ufunc->uf_va_type->tt_member != &t_any
5695 && tv->vval.v_list != NULL)
5696 {
5697 type_T *expected = ufunc->uf_va_type->tt_member;
5698 listitem_T *li = tv->vval.v_list->lv_first;
5699
5700 for (idx = 0; idx < vararg_count; ++idx)
5701 {
5702 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005703 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005704 goto failed_early;
5705 li = li->li_next;
5706 }
5707 }
5708
5709 if (defcount > 0)
5710 // Move varargs list to below missing default arguments.
5711 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5712 --ectx.ec_stack.ga_len;
5713 }
5714
5715 // Make space for omitted arguments, will store default value below.
5716 // Any varargs list goes after them.
5717 if (defcount > 0)
5718 for (idx = 0; idx < defcount; ++idx)
5719 {
5720 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5721 ++ectx.ec_stack.ga_len;
5722 }
5723 if (ufunc->uf_va_name != NULL)
5724 ++ectx.ec_stack.ga_len;
5725
5726 // Frame pointer points to just after arguments.
5727 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5728 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5729
5730 {
5731 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5732 + ufunc->uf_dfunc_idx;
5733 ufunc_T *base_ufunc = dfunc->df_ufunc;
5734
5735 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005736 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005737 if (partial != NULL || base_ufunc->uf_partial != NULL)
5738 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005739 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5740 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005741 goto failed_early;
5742 if (partial != NULL)
5743 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005744 outer_T *outer = get_pt_outer(partial);
5745
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005746 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005747 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005748 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005749 if (current_ectx != NULL)
5750 {
5751 if (current_ectx->ec_outer_ref != NULL
5752 && current_ectx->ec_outer_ref->or_outer != NULL)
5753 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005754 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005755 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005756 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005757 }
5758 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005759 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005760 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005761 ++partial->pt_refcount;
5762 ectx.ec_outer_ref->or_partial = partial;
5763 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005764 }
5765 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005766 {
5767 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5768 ++base_ufunc->uf_partial->pt_refcount;
5769 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5770 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005771 }
5772 }
5773
5774 // dummy frame entries
5775 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5776 {
5777 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5778 ++ectx.ec_stack.ga_len;
5779 }
5780
5781 {
5782 // Reserve space for local variables and any closure reference count.
5783 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5784 + ufunc->uf_dfunc_idx;
5785
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005786 // Initialize variables to zero. That avoids having to generate
5787 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005788 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005789 {
5790 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5791 STACK_TV_VAR(idx)->vval.v_number = 0;
5792 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005793 ectx.ec_stack.ga_len += dfunc->df_varcount;
5794 if (dfunc->df_has_closure)
5795 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01005796 // Initialize the variable that counts how many closures were
5797 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005798 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5799 STACK_TV_VAR(idx)->vval.v_number = 0;
5800 ++ectx.ec_stack.ga_len;
5801 }
5802
5803 ectx.ec_instr = INSTRUCTIONS(dfunc);
5804 }
5805
Bram Moolenaar58779852022-09-06 18:31:14 +01005806 // Store the execution context in funccal, used by invoke_all_defer().
5807 if (funccal != NULL)
5808 funccal->fc_ectx = &ectx;
5809
Bram Moolenaar4c137212021-04-19 16:48:48 +02005810 // Following errors are in the function, not the caller.
5811 // Commands behave like vim9script.
5812 estack_push_ufunc(ufunc, 1);
5813 current_sctx = ufunc->uf_script_ctx;
5814 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5815
5816 // Use a specific location for storing error messages to be converted to an
5817 // exception.
5818 saved_msg_list = msg_list;
5819 msg_list = &private_msg_list;
5820
5821 // Do turn errors into exceptions.
5822 suppress_errthrow = FALSE;
5823
5824 // Do not delete the function while executing it.
5825 ++ufunc->uf_calls;
5826
5827 // When ":silent!" was used before calling then we still abort the
5828 // function. If ":silent!" is used in the function then we don't.
5829 emsg_silent_def = emsg_silent;
5830 did_emsg_def = 0;
5831
5832 ectx.ec_where.wt_index = 0;
5833 ectx.ec_where.wt_variable = FALSE;
5834
5835 // Execute the instructions until done.
5836 ret = exec_instructions(&ectx);
5837 if (ret == OK)
5838 {
5839 // function finished, get result from the stack.
5840 if (ufunc->uf_ret_type == &t_void)
5841 {
5842 rettv->v_type = VAR_VOID;
5843 }
5844 else
5845 {
5846 tv = STACK_TV_BOT(-1);
5847 *rettv = *tv;
5848 tv->v_type = VAR_UNKNOWN;
5849 }
5850 }
5851
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005852 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005853 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005854
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005855 // Deal with any remaining closures, they may be in use somewhere.
5856 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005857 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005858 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005859 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005860 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005861
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005862 estack_pop();
5863 current_sctx = save_current_sctx;
5864
Bram Moolenaar2336c372021-12-06 15:06:54 +00005865 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5866 // Function was unreferenced while being used, free it now.
5867 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005868
Bram Moolenaar352134b2020-10-17 22:04:08 +02005869 if (*msg_list != NULL && saved_msg_list != NULL)
5870 {
5871 msglist_T **plist = saved_msg_list;
5872
5873 // Append entries from the current msg_list (uncaught exceptions) to
5874 // the saved msg_list.
5875 while (*plist != NULL)
5876 plist = &(*plist)->next;
5877
5878 *plist = *msg_list;
5879 }
5880 msg_list = saved_msg_list;
5881
Bram Moolenaar4c137212021-04-19 16:48:48 +02005882 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005883 {
5884 cmdmod.cmod_filter_regmatch.regprog = NULL;
5885 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005886 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005887 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005888 emsg_silent_def = save_emsg_silent_def;
5889 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005890
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005891failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005892 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005893 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5894 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005895 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005898 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005899 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005900 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005901 if (ectx.ec_outer_ref->or_outer_allocated)
5902 vim_free(ectx.ec_outer_ref->or_outer);
5903 partial_unref(ectx.ec_outer_ref->or_partial);
5904 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005905 }
5906
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005907 // Not sure if this is necessary.
5908 suppress_errthrow = save_suppress_errthrow;
5909
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005910 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5911 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005912 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005913 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005914 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915 return ret;
5916}
5917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005918/*
Bram Moolenaar58779852022-09-06 18:31:14 +01005919 * Called when a def function has finished (possibly failed).
5920 * Invoke all the function returns to clean up and invoke deferred functions,
5921 * except the toplevel one.
5922 */
5923 void
5924unwind_def_callstack(ectx_T *ectx)
5925{
5926 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
5927 func_return(ectx);
5928}
5929
5930/*
5931 * Invoke any deffered functions for the top function in "ectx".
5932 */
5933 void
5934may_invoke_defer_funcs(ectx_T *ectx)
5935{
5936 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
5937
5938 if (dfunc->df_defer_var_idx > 0)
5939 invoke_defer_funcs(ectx);
5940}
5941
5942/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01005943 * Return loopvarinfo in a printable form in allocated memory.
5944 */
5945 static char_u *
5946printable_loopvarinfo(loopvarinfo_T *lvi)
5947{
5948 garray_T ga;
5949 int depth;
5950
5951 ga_init2(&ga, 1, 100);
5952 for (depth = 0; depth < lvi->lvi_depth; ++depth)
5953 {
5954 if (ga_grow(&ga, 50) == FAIL)
5955 break;
5956 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01005957 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01005958 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01005959 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01005960 lvi->lvi_loop[depth].var_idx,
5961 lvi->lvi_loop[depth].var_idx
5962 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01005963 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01005964 }
5965 return ga.ga_data;
5966}
5967
5968/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005969 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5970 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5971 * "pfx" is prefixed to every line.
5972 */
5973 static void
5974list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5975{
5976 int line_idx = 0;
5977 int prev_current = 0;
5978 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005979 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005980
5981 for (current = 0; current < instr_count; ++current)
5982 {
5983 isn_T *iptr = &instr[current];
5984 char *line;
5985
5986 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005987 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005988 while (line_idx < iptr->isn_lnum
5989 && line_idx < ufunc->uf_lines.ga_len)
5990 {
5991 if (current > prev_current)
5992 {
5993 msg_puts("\n\n");
5994 prev_current = current;
5995 }
5996 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5997 if (line != NULL)
5998 msg(line);
5999 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006000 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6001 {
6002 int first_def_arg = ufunc->uf_args.ga_len
6003 - ufunc->uf_def_args.ga_len;
6004
6005 if (def_arg_idx > 0)
6006 msg_puts("\n\n");
6007 msg_start();
6008 msg_puts(" ");
6009 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6010 first_def_arg + def_arg_idx]);
6011 msg_puts(" = ");
6012 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6013 msg_clr_eos();
6014 msg_end();
6015 }
6016 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006017
6018 switch (iptr->isn_type)
6019 {
6020 case ISN_EXEC:
6021 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6022 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006023 case ISN_EXEC_SPLIT:
6024 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6025 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006026 case ISN_EXECRANGE:
6027 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6028 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006029 case ISN_LEGACY_EVAL:
6030 smsg("%s%4d EVAL legacy %s", pfx, current,
6031 iptr->isn_arg.string);
6032 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006033 case ISN_REDIRSTART:
6034 smsg("%s%4d REDIR", pfx, current);
6035 break;
6036 case ISN_REDIREND:
6037 smsg("%s%4d REDIR END%s", pfx, current,
6038 iptr->isn_arg.number ? " append" : "");
6039 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006040 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006041#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006042 smsg("%s%4d CEXPR pre %s", pfx, current,
6043 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006044#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006045 break;
6046 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006047#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006048 {
6049 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6050
6051 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6052 cexpr_get_auname(cer->cer_cmdidx),
6053 cer->cer_forceit ? "!" : "",
6054 cer->cer_cmdline);
6055 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006056#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006057 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006058 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006059 smsg("%s%4d INSTR", pfx, current);
6060 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6061 msg(" -------------");
6062 break;
6063 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006064 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006065 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6066
6067 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006068 }
6069 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006070 case ISN_SUBSTITUTE:
6071 {
6072 subs_T *subs = &iptr->isn_arg.subs;
6073
6074 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6075 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6076 msg(" -------------");
6077 }
6078 break;
6079 case ISN_EXECCONCAT:
6080 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6081 (varnumber_T)iptr->isn_arg.number);
6082 break;
6083 case ISN_ECHO:
6084 {
6085 echo_T *echo = &iptr->isn_arg.echo;
6086
6087 smsg("%s%4d %s %d", pfx, current,
6088 echo->echo_with_white ? "ECHO" : "ECHON",
6089 echo->echo_count);
6090 }
6091 break;
6092 case ISN_EXECUTE:
6093 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006094 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006095 break;
6096 case ISN_ECHOMSG:
6097 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006098 (varnumber_T)(iptr->isn_arg.number));
6099 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006100 case ISN_ECHOWINDOW:
6101 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
6102 (varnumber_T)(iptr->isn_arg.number));
6103 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006104 case ISN_ECHOCONSOLE:
6105 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6106 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006107 break;
6108 case ISN_ECHOERR:
6109 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006110 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006111 break;
6112 case ISN_LOAD:
6113 {
6114 if (iptr->isn_arg.number < 0)
6115 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6116 (varnumber_T)(iptr->isn_arg.number
6117 + STACK_FRAME_SIZE));
6118 else
6119 smsg("%s%4d LOAD $%lld", pfx, current,
6120 (varnumber_T)(iptr->isn_arg.number));
6121 }
6122 break;
6123 case ISN_LOADOUTER:
6124 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006125 isn_outer_T *outer = &iptr->isn_arg.outer;
6126
6127 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006128 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006129 outer->outer_depth,
6130 outer->outer_idx + STACK_FRAME_SIZE);
6131 else if (outer->outer_depth < 0)
6132 smsg("%s%4d LOADOUTER $%d in loop level %d",
6133 pfx, current,
6134 outer->outer_idx,
6135 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006136 else
6137 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006138 outer->outer_depth,
6139 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006140 }
6141 break;
6142 case ISN_LOADV:
6143 smsg("%s%4d LOADV v:%s", pfx, current,
6144 get_vim_var_name(iptr->isn_arg.number));
6145 break;
6146 case ISN_LOADSCRIPT:
6147 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006148 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6149 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6150 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006151
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006152 sv = get_script_svar(sref, -1);
6153 if (sv == NULL)
6154 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6155 pfx, current, si->sn_name);
6156 else
6157 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006158 sv->sv_name,
6159 sref->sref_idx,
6160 si->sn_name);
6161 }
6162 break;
6163 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006164 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006165 {
6166 scriptitem_T *si = SCRIPT_ITEM(
6167 iptr->isn_arg.loadstore.ls_sid);
6168
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006169 smsg("%s%4d %s s:%s from %s", pfx, current,
6170 iptr->isn_type == ISN_LOADS ? "LOADS"
6171 : "LOADEXPORT",
6172 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006173 }
6174 break;
6175 case ISN_LOADAUTO:
6176 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6177 break;
6178 case ISN_LOADG:
6179 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6180 break;
6181 case ISN_LOADB:
6182 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6183 break;
6184 case ISN_LOADW:
6185 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6186 break;
6187 case ISN_LOADT:
6188 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6189 break;
6190 case ISN_LOADGDICT:
6191 smsg("%s%4d LOAD g:", pfx, current);
6192 break;
6193 case ISN_LOADBDICT:
6194 smsg("%s%4d LOAD b:", pfx, current);
6195 break;
6196 case ISN_LOADWDICT:
6197 smsg("%s%4d LOAD w:", pfx, current);
6198 break;
6199 case ISN_LOADTDICT:
6200 smsg("%s%4d LOAD t:", pfx, current);
6201 break;
6202 case ISN_LOADOPT:
6203 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6204 break;
6205 case ISN_LOADENV:
6206 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6207 break;
6208 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006209 smsg("%s%4d LOADREG @%c", pfx, current,
6210 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006211 break;
6212
6213 case ISN_STORE:
6214 if (iptr->isn_arg.number < 0)
6215 smsg("%s%4d STORE arg[%lld]", pfx, current,
6216 iptr->isn_arg.number + STACK_FRAME_SIZE);
6217 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006218 smsg("%s%4d STORE $%lld", pfx, current,
6219 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006220 break;
6221 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006222 {
6223 isn_outer_T *outer = &iptr->isn_arg.outer;
6224
6225 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6226 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6227 pfx, current, outer->outer_idx);
6228 else
6229 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6230 outer->outer_depth, outer->outer_idx);
6231 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006232 break;
6233 case ISN_STOREV:
6234 smsg("%s%4d STOREV v:%s", pfx, current,
6235 get_vim_var_name(iptr->isn_arg.number));
6236 break;
6237 case ISN_STOREAUTO:
6238 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6239 break;
6240 case ISN_STOREG:
6241 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6242 break;
6243 case ISN_STOREB:
6244 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6245 break;
6246 case ISN_STOREW:
6247 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6248 break;
6249 case ISN_STORET:
6250 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6251 break;
6252 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006253 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006254 {
6255 scriptitem_T *si = SCRIPT_ITEM(
6256 iptr->isn_arg.loadstore.ls_sid);
6257
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006258 smsg("%s%4d %s %s in %s", pfx, current,
6259 iptr->isn_type == ISN_STORES
6260 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006261 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6262 }
6263 break;
6264 case ISN_STORESCRIPT:
6265 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006266 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6267 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6268 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006269
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006270 sv = get_script_svar(sref, -1);
6271 if (sv == NULL)
6272 smsg("%s%4d STORESCRIPT [deleted] in %s",
6273 pfx, current, si->sn_name);
6274 else
6275 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006276 sv->sv_name,
6277 sref->sref_idx,
6278 si->sn_name);
6279 }
6280 break;
6281 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006282 case ISN_STOREFUNCOPT:
6283 smsg("%s%4d %s &%s", pfx, current,
6284 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006285 iptr->isn_arg.storeopt.so_name);
6286 break;
6287 case ISN_STOREENV:
6288 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6289 break;
6290 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006291 smsg("%s%4d STOREREG @%c", pfx, current,
6292 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006293 break;
6294 case ISN_STORENR:
6295 smsg("%s%4d STORE %lld in $%d", pfx, current,
6296 iptr->isn_arg.storenr.stnr_val,
6297 iptr->isn_arg.storenr.stnr_idx);
6298 break;
6299
6300 case ISN_STOREINDEX:
6301 smsg("%s%4d STOREINDEX %s", pfx, current,
6302 vartype_name(iptr->isn_arg.vartype));
6303 break;
6304
6305 case ISN_STORERANGE:
6306 smsg("%s%4d STORERANGE", pfx, current);
6307 break;
6308
6309 // constants
6310 case ISN_PUSHNR:
6311 smsg("%s%4d PUSHNR %lld", pfx, current,
6312 (varnumber_T)(iptr->isn_arg.number));
6313 break;
6314 case ISN_PUSHBOOL:
6315 case ISN_PUSHSPEC:
6316 smsg("%s%4d PUSH %s", pfx, current,
6317 get_var_special_name(iptr->isn_arg.number));
6318 break;
6319 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006320 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006321 break;
6322 case ISN_PUSHS:
6323 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6324 break;
6325 case ISN_PUSHBLOB:
6326 {
6327 char_u *r;
6328 char_u numbuf[NUMBUFLEN];
6329 char_u *tofree;
6330
6331 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6332 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6333 vim_free(tofree);
6334 }
6335 break;
6336 case ISN_PUSHFUNC:
6337 {
6338 char *name = (char *)iptr->isn_arg.string;
6339
6340 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6341 name == NULL ? "[none]" : name);
6342 }
6343 break;
6344 case ISN_PUSHCHANNEL:
6345#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006346 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006347#endif
6348 break;
6349 case ISN_PUSHJOB:
6350#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006351 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006352#endif
6353 break;
6354 case ISN_PUSHEXC:
6355 smsg("%s%4d PUSH v:exception", pfx, current);
6356 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006357 case ISN_AUTOLOAD:
6358 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6359 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006360 case ISN_UNLET:
6361 smsg("%s%4d UNLET%s %s", pfx, current,
6362 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6363 iptr->isn_arg.unlet.ul_name);
6364 break;
6365 case ISN_UNLETENV:
6366 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6367 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6368 iptr->isn_arg.unlet.ul_name);
6369 break;
6370 case ISN_UNLETINDEX:
6371 smsg("%s%4d UNLETINDEX", pfx, current);
6372 break;
6373 case ISN_UNLETRANGE:
6374 smsg("%s%4d UNLETRANGE", pfx, current);
6375 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006376 case ISN_LOCKUNLOCK:
6377 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6378 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006379 case ISN_LOCKCONST:
6380 smsg("%s%4d LOCKCONST", pfx, current);
6381 break;
6382 case ISN_NEWLIST:
6383 smsg("%s%4d NEWLIST size %lld", pfx, current,
6384 (varnumber_T)(iptr->isn_arg.number));
6385 break;
6386 case ISN_NEWDICT:
6387 smsg("%s%4d NEWDICT size %lld", pfx, current,
6388 (varnumber_T)(iptr->isn_arg.number));
6389 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006390 case ISN_NEWPARTIAL:
6391 smsg("%s%4d NEWPARTIAL", pfx, current);
6392 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006393
6394 // function call
6395 case ISN_BCALL:
6396 {
6397 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6398
6399 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6400 internal_func_name(cbfunc->cbf_idx),
6401 cbfunc->cbf_argcount);
6402 }
6403 break;
6404 case ISN_DCALL:
6405 {
6406 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6407 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6408 + cdfunc->cdf_idx;
6409
6410 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006411 printable_func_name(df->df_ufunc),
6412 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006413 }
6414 break;
6415 case ISN_UCALL:
6416 {
6417 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6418
6419 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6420 cufunc->cuf_name, cufunc->cuf_argcount);
6421 }
6422 break;
6423 case ISN_PCALL:
6424 {
6425 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6426
6427 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6428 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6429 }
6430 break;
6431 case ISN_PCALL_END:
6432 smsg("%s%4d PCALL end", pfx, current);
6433 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006434 case ISN_DEFER:
6435 smsg("%s%4d DEFER %d args", pfx, current,
6436 (int)iptr->isn_arg.defer.defer_argcount);
6437 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006438 case ISN_RETURN:
6439 smsg("%s%4d RETURN", pfx, current);
6440 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006441 case ISN_RETURN_VOID:
6442 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006443 break;
6444 case ISN_FUNCREF:
6445 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006446 funcref_T *funcref = &iptr->isn_arg.funcref;
6447 funcref_extra_T *extra = funcref->fr_extra;
6448 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006449
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006450 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006451 {
6452 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6453 + funcref->fr_dfunc_idx;
6454 name = df->df_ufunc->uf_name;
6455 }
6456 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006457 name = extra->fre_func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006458 if (extra == NULL || extra->fre_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006459 smsg("%s%4d FUNCREF %s", pfx, current, name);
6460 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006461 {
6462 char_u *info = printable_loopvarinfo(
6463 &extra->fre_loopvar_info);
6464
6465 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6466 name, info);
6467 vim_free(info);
6468 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006469 }
6470 break;
6471
6472 case ISN_NEWFUNC:
6473 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006474 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006475
Bram Moolenaarcc341812022-09-19 15:54:34 +01006476 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006477 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6478 arg->nfa_lambda, arg->nfa_global);
6479 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006480 {
6481 char_u *info = printable_loopvarinfo(
6482 &arg->nfa_loopvar_info);
6483
6484 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6485 arg->nfa_lambda, arg->nfa_global, info);
6486 vim_free(info);
6487 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006488 }
6489 break;
6490
6491 case ISN_DEF:
6492 {
6493 char_u *name = iptr->isn_arg.string;
6494
6495 smsg("%s%4d DEF %s", pfx, current,
6496 name == NULL ? (char_u *)"" : name);
6497 }
6498 break;
6499
6500 case ISN_JUMP:
6501 {
6502 char *when = "?";
6503
6504 switch (iptr->isn_arg.jump.jump_when)
6505 {
6506 case JUMP_ALWAYS:
6507 when = "JUMP";
6508 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006509 case JUMP_NEVER:
6510 iemsg("JUMP_NEVER should not be used");
6511 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006512 case JUMP_AND_KEEP_IF_TRUE:
6513 when = "JUMP_AND_KEEP_IF_TRUE";
6514 break;
6515 case JUMP_IF_FALSE:
6516 when = "JUMP_IF_FALSE";
6517 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006518 case JUMP_WHILE_FALSE:
6519 when = "JUMP_WHILE_FALSE"; // unused
6520 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006521 case JUMP_IF_COND_FALSE:
6522 when = "JUMP_IF_COND_FALSE";
6523 break;
6524 case JUMP_IF_COND_TRUE:
6525 when = "JUMP_IF_COND_TRUE";
6526 break;
6527 }
6528 smsg("%s%4d %s -> %d", pfx, current, when,
6529 iptr->isn_arg.jump.jump_where);
6530 }
6531 break;
6532
6533 case ISN_JUMP_IF_ARG_SET:
6534 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6535 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6536 iptr->isn_arg.jump.jump_where);
6537 break;
6538
6539 case ISN_FOR:
6540 {
6541 forloop_T *forloop = &iptr->isn_arg.forloop;
6542
6543 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006544 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006545 }
6546 break;
6547
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006548 case ISN_ENDLOOP:
6549 {
6550 endloop_T *endloop = &iptr->isn_arg.endloop;
6551
Bram Moolenaarcc341812022-09-19 15:54:34 +01006552 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6553 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006554 endloop->end_funcref_idx,
6555 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006556 endloop->end_var_idx + endloop->end_var_count - 1,
6557 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006558 }
6559 break;
6560
6561 case ISN_WHILE:
6562 {
6563 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6564
6565 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6566 whileloop->while_funcref_idx,
6567 whileloop->while_end);
6568 }
6569 break;
6570
Bram Moolenaar4c137212021-04-19 16:48:48 +02006571 case ISN_TRY:
6572 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006573 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006574
6575 if (try->try_ref->try_finally == 0)
6576 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6577 pfx, current,
6578 try->try_ref->try_catch,
6579 try->try_ref->try_endtry);
6580 else
6581 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6582 pfx, current,
6583 try->try_ref->try_catch,
6584 try->try_ref->try_finally,
6585 try->try_ref->try_endtry);
6586 }
6587 break;
6588 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006589 smsg("%s%4d CATCH", pfx, current);
6590 break;
6591 case ISN_TRYCONT:
6592 {
6593 trycont_T *trycont = &iptr->isn_arg.trycont;
6594
6595 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6596 trycont->tct_levels,
6597 trycont->tct_levels == 1 ? "" : "s",
6598 trycont->tct_where);
6599 }
6600 break;
6601 case ISN_FINALLY:
6602 smsg("%s%4d FINALLY", pfx, current);
6603 break;
6604 case ISN_ENDTRY:
6605 smsg("%s%4d ENDTRY", pfx, current);
6606 break;
6607 case ISN_THROW:
6608 smsg("%s%4d THROW", pfx, current);
6609 break;
6610
6611 // expression operations on number
6612 case ISN_OPNR:
6613 case ISN_OPFLOAT:
6614 case ISN_OPANY:
6615 {
6616 char *what;
6617 char *ins;
6618
6619 switch (iptr->isn_arg.op.op_type)
6620 {
6621 case EXPR_MULT: what = "*"; break;
6622 case EXPR_DIV: what = "/"; break;
6623 case EXPR_REM: what = "%"; break;
6624 case EXPR_SUB: what = "-"; break;
6625 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006626 case EXPR_LSHIFT: what = "<<"; break;
6627 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006628 default: what = "???"; break;
6629 }
6630 switch (iptr->isn_type)
6631 {
6632 case ISN_OPNR: ins = "OPNR"; break;
6633 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6634 case ISN_OPANY: ins = "OPANY"; break;
6635 default: ins = "???"; break;
6636 }
6637 smsg("%s%4d %s %s", pfx, current, ins, what);
6638 }
6639 break;
6640
6641 case ISN_COMPAREBOOL:
6642 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006643 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006644 case ISN_COMPARENR:
6645 case ISN_COMPAREFLOAT:
6646 case ISN_COMPARESTRING:
6647 case ISN_COMPAREBLOB:
6648 case ISN_COMPARELIST:
6649 case ISN_COMPAREDICT:
6650 case ISN_COMPAREFUNC:
6651 case ISN_COMPAREANY:
6652 {
6653 char *p;
6654 char buf[10];
6655 char *type;
6656
6657 switch (iptr->isn_arg.op.op_type)
6658 {
6659 case EXPR_EQUAL: p = "=="; break;
6660 case EXPR_NEQUAL: p = "!="; break;
6661 case EXPR_GREATER: p = ">"; break;
6662 case EXPR_GEQUAL: p = ">="; break;
6663 case EXPR_SMALLER: p = "<"; break;
6664 case EXPR_SEQUAL: p = "<="; break;
6665 case EXPR_MATCH: p = "=~"; break;
6666 case EXPR_IS: p = "is"; break;
6667 case EXPR_ISNOT: p = "isnot"; break;
6668 case EXPR_NOMATCH: p = "!~"; break;
6669 default: p = "???"; break;
6670 }
6671 STRCPY(buf, p);
6672 if (iptr->isn_arg.op.op_ic == TRUE)
6673 strcat(buf, "?");
6674 switch(iptr->isn_type)
6675 {
6676 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6677 case ISN_COMPARESPECIAL:
6678 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006679 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006680 case ISN_COMPARENR: type = "COMPARENR"; break;
6681 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6682 case ISN_COMPARESTRING:
6683 type = "COMPARESTRING"; break;
6684 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6685 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6686 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6687 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6688 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6689 default: type = "???"; break;
6690 }
6691
6692 smsg("%s%4d %s %s", pfx, current, type, buf);
6693 }
6694 break;
6695
6696 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6697 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6698
6699 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006700 case ISN_CONCAT:
6701 smsg("%s%4d CONCAT size %lld", pfx, current,
6702 (varnumber_T)(iptr->isn_arg.number));
6703 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006704 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6705 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6706 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6707 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6708 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6709 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6710 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6711 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6712 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6713 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6714 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006715 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006716 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6717 iptr->isn_arg.getitem.gi_index,
6718 iptr->isn_arg.getitem.gi_with_op ?
6719 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006720 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6721 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6722 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006723 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6724 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6725
Bram Moolenaar4c137212021-04-19 16:48:48 +02006726 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6727
Bram Moolenaar4c137212021-04-19 16:48:48 +02006728 case ISN_CHECKTYPE:
6729 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006730 checktype_T *ct = &iptr->isn_arg.type;
6731 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006732
6733 if (ct->ct_arg_idx == 0)
6734 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6735 type_name(ct->ct_type, &tofree),
6736 (int)ct->ct_off);
6737 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006738 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006739 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006740 type_name(ct->ct_type, &tofree),
6741 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006742 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006743 (int)ct->ct_arg_idx);
6744 vim_free(tofree);
6745 break;
6746 }
6747 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6748 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6749 iptr->isn_arg.checklen.cl_min_len);
6750 break;
6751 case ISN_SETTYPE:
6752 {
6753 char *tofree;
6754
6755 smsg("%s%4d SETTYPE %s", pfx, current,
6756 type_name(iptr->isn_arg.type.ct_type, &tofree));
6757 vim_free(tofree);
6758 break;
6759 }
6760 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006761 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6762 smsg("%s%4d INVERT %d (!val)", pfx, current,
6763 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006764 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006765 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6766 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006767 break;
6768 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006769 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006770 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006771 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6772 pfx, current,
6773 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006774 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006775 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6776 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006777 break;
6778 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006779 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006780 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006781 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006782 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6783 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006784 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006785 else
6786 smsg("%s%4d PUT %c %ld", pfx, current,
6787 iptr->isn_arg.put.put_regname,
6788 (long)iptr->isn_arg.put.put_lnum);
6789 break;
6790
Bram Moolenaar4c137212021-04-19 16:48:48 +02006791 case ISN_CMDMOD:
6792 {
6793 char_u *buf;
6794 size_t len = produce_cmdmods(
6795 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6796
6797 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006798 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006799 {
6800 (void)produce_cmdmods(
6801 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6802 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6803 vim_free(buf);
6804 }
6805 break;
6806 }
6807 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6808
6809 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006810 smsg("%s%4d PROFILE START line %d", pfx, current,
6811 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006812 break;
6813
6814 case ISN_PROF_END:
6815 smsg("%s%4d PROFILE END", pfx, current);
6816 break;
6817
Bram Moolenaare99d4222021-06-13 14:01:26 +02006818 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006819 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6820 iptr->isn_arg.debug.dbg_break_lnum + 1,
6821 iptr->isn_lnum,
6822 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006823 break;
6824
Bram Moolenaar4c137212021-04-19 16:48:48 +02006825 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6826 iptr->isn_arg.unpack.unp_count,
6827 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6828 break;
6829 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6830 iptr->isn_arg.shuffle.shfl_item,
6831 iptr->isn_arg.shuffle.shfl_up);
6832 break;
6833 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6834
6835 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6836 return;
6837 }
6838
6839 out_flush(); // output one line at a time
6840 ui_breakcheck();
6841 if (got_int)
6842 break;
6843 }
6844}
6845
6846/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006847 * Handle command line completion for the :disassemble command.
6848 */
6849 void
6850set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6851{
6852 char_u *p;
6853
6854 // Default: expand user functions, "debug" and "profile"
6855 xp->xp_context = EXPAND_DISASSEMBLE;
6856 xp->xp_pattern = arg;
6857
6858 // first argument already typed: only user function names
6859 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6860 {
6861 xp->xp_context = EXPAND_USER_FUNC;
6862 xp->xp_pattern = skipwhite(p);
6863 }
6864}
6865
6866/*
6867 * Function given to ExpandGeneric() to obtain the list of :disassemble
6868 * arguments.
6869 */
6870 char_u *
6871get_disassemble_argument(expand_T *xp, int idx)
6872{
6873 if (idx == 0)
6874 return (char_u *)"debug";
6875 if (idx == 1)
6876 return (char_u *)"profile";
6877 return get_user_func_name(xp, idx - 2);
6878}
6879
6880/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006881 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006882 * We don't really need this at runtime, but we do have tests that require it,
6883 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884 */
6885 void
6886ex_disassemble(exarg_T *eap)
6887{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006888 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006889 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006891 isn_T *instr = NULL; // init to shut up gcc warning
6892 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006893 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006894
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006895 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006896 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006898 if (func_needs_compiling(ufunc, compile_type)
6899 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006900 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006901 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006902 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006903 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006904 return;
6905 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006906 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006907
6908 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006909 switch (compile_type)
6910 {
6911 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006912#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006913 instr = dfunc->df_instr_prof;
6914 instr_count = dfunc->df_instr_prof_count;
6915 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006916#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006917 // FALLTHROUGH
6918 case CT_NONE:
6919 instr = dfunc->df_instr;
6920 instr_count = dfunc->df_instr_count;
6921 break;
6922 case CT_DEBUG:
6923 instr = dfunc->df_instr_debug;
6924 instr_count = dfunc->df_instr_debug_count;
6925 break;
6926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927
Bram Moolenaar4c137212021-04-19 16:48:48 +02006928 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929}
6930
6931/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006932 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 * list, etc. Mostly like what JavaScript does, except that empty list and
6934 * empty dictionary are FALSE.
6935 */
6936 int
6937tv2bool(typval_T *tv)
6938{
6939 switch (tv->v_type)
6940 {
6941 case VAR_NUMBER:
6942 return tv->vval.v_number != 0;
6943 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 case VAR_PARTIAL:
6946 return tv->vval.v_partial != NULL;
6947 case VAR_FUNC:
6948 case VAR_STRING:
6949 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6950 case VAR_LIST:
6951 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6952 case VAR_DICT:
6953 return tv->vval.v_dict != NULL
6954 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6955 case VAR_BOOL:
6956 case VAR_SPECIAL:
6957 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6958 case VAR_JOB:
6959#ifdef FEAT_JOB_CHANNEL
6960 return tv->vval.v_job != NULL;
6961#else
6962 break;
6963#endif
6964 case VAR_CHANNEL:
6965#ifdef FEAT_JOB_CHANNEL
6966 return tv->vval.v_channel != NULL;
6967#else
6968 break;
6969#endif
6970 case VAR_BLOB:
6971 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6972 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006973 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006975 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976 break;
6977 }
6978 return FALSE;
6979}
6980
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006981 void
6982emsg_using_string_as(typval_T *tv, int as_number)
6983{
6984 semsg(_(as_number ? e_using_string_as_number_str
6985 : e_using_string_as_bool_str),
6986 tv->vval.v_string == NULL
6987 ? (char_u *)"" : tv->vval.v_string);
6988}
6989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990/*
6991 * If "tv" is a string give an error and return FAIL.
6992 */
6993 int
6994check_not_string(typval_T *tv)
6995{
6996 if (tv->v_type == VAR_STRING)
6997 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006998 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 clear_tv(tv);
7000 return FAIL;
7001 }
7002 return OK;
7003}
7004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005
7006#endif // FEAT_EVAL