blob: ddcbe2c76b68b1815e7c5c2f5eb74b8a28d6922a [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010067// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100104// Get pointer to item in the stack.
105#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200108#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100110// Get pointer to a local variable on the stack. Negative for arguments.
111#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
112
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200113 void
114to_string_error(vartype_T vartype)
115{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200116 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200117}
118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100120 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 */
122 static int
123ufunc_argcount(ufunc_T *ufunc)
124{
125 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
126}
127
128/*
LemonBoy372bcce2022-04-25 12:43:20 +0100129 * Create a new string from "count" items at the bottom of the stack.
130 * A trailing NUL is appended.
131 * When "count" is zero an empty string is added to the stack.
132 */
133 static int
134exe_concat(int count, ectx_T *ectx)
135{
136 int idx;
137 int len = 0;
138 typval_T *tv;
139 garray_T ga;
140
141 ga_init2(&ga, sizeof(char), 1);
142 // Preallocate enough space for the whole string to avoid having to grow
143 // and copy.
144 for (idx = 0; idx < count; ++idx)
145 {
146 tv = STACK_TV_BOT(idx - count);
147 if (tv->vval.v_string != NULL)
148 len += (int)STRLEN(tv->vval.v_string);
149 }
150
151 if (ga_grow(&ga, len + 1) == FAIL)
152 return FAIL;
153
154 for (idx = 0; idx < count; ++idx)
155 {
156 tv = STACK_TV_BOT(idx - count);
157 ga_concat(&ga, tv->vval.v_string);
158 clear_tv(tv);
159 }
160
161 // add a terminating NUL
162 ga_append(&ga, NUL);
163
164 ectx->ec_stack.ga_len -= count - 1;
165 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
166
167 return OK;
168}
169
170/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200171 * Create a new list from "count" items at the bottom of the stack.
172 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100173 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200174 */
175 static int
176exe_newlist(int count, ectx_T *ectx)
177{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 int idx;
180 typval_T *tv;
181
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100182 if (count >= 0)
183 {
184 list = list_alloc_with_items(count);
185 if (list == NULL)
186 return FAIL;
187 for (idx = 0; idx < count; ++idx)
188 list_set_item(list, idx, STACK_TV_BOT(idx - count));
189 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190
191 if (count > 0)
192 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200193 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100194 {
195 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 else
199 ++ectx->ec_stack.ga_len;
200 tv = STACK_TV_BOT(-1);
201 tv->v_type = VAR_LIST;
202 tv->vval.v_list = list;
Bram Moolenaar566badc2022-09-18 13:46:08 +0100203 tv->v_lock = 0;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100204 if (list != NULL)
205 ++list->lv_refcount;
206 return OK;
207}
208
209/*
210 * Implementation of ISN_NEWDICT.
211 * Returns FAIL on total failure, MAYBE on error.
212 */
213 static int
214exe_newdict(int count, ectx_T *ectx)
215{
216 dict_T *dict = NULL;
217 dictitem_T *item;
218 char_u *key;
219 int idx;
220 typval_T *tv;
221
222 if (count >= 0)
223 {
224 dict = dict_alloc();
225 if (unlikely(dict == NULL))
226 return FAIL;
227 for (idx = 0; idx < count; ++idx)
228 {
229 // have already checked key type is VAR_STRING
230 tv = STACK_TV_BOT(2 * (idx - count));
231 // check key is unique
232 key = tv->vval.v_string == NULL
233 ? (char_u *)"" : tv->vval.v_string;
234 item = dict_find(dict, key, -1);
235 if (item != NULL)
236 {
237 semsg(_(e_duplicate_key_in_dicitonary), key);
238 dict_unref(dict);
239 return MAYBE;
240 }
241 item = dictitem_alloc(key);
242 clear_tv(tv);
243 if (unlikely(item == NULL))
244 {
245 dict_unref(dict);
246 return FAIL;
247 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100248 tv = STACK_TV_BOT(2 * (idx - count) + 1);
249 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100250 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100251 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100252 if (dict_add(dict, item) == FAIL)
253 {
254 // can this ever happen?
255 dict_unref(dict);
256 return FAIL;
257 }
258 }
259 }
260
261 if (count > 0)
262 ectx->ec_stack.ga_len -= 2 * count - 1;
263 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
264 return FAIL;
265 else
266 ++ectx->ec_stack.ga_len;
267 tv = STACK_TV_BOT(-1);
268 tv->v_type = VAR_DICT;
269 tv->v_lock = 0;
270 tv->vval.v_dict = dict;
271 if (dict != NULL)
272 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200273 return OK;
274}
275
276/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200277 * If debug_tick changed check if "ufunc" has a breakpoint and update
278 * "uf_has_breakpoint".
279 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000280 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200281update_has_breakpoint(ufunc_T *ufunc)
282{
283 if (ufunc->uf_debug_tick != debug_tick)
284 {
285 linenr_T breakpoint;
286
287 ufunc->uf_debug_tick = debug_tick;
288 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
289 ufunc->uf_has_breakpoint = breakpoint > 0;
290 }
291}
292
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200293static garray_T dict_stack = GA_EMPTY;
294
295/*
296 * Put a value on the dict stack. This consumes "tv".
297 */
298 static int
299dict_stack_save(typval_T *tv)
300{
301 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000302 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200303 if (ga_grow(&dict_stack, 1) == FAIL)
304 return FAIL;
305 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
306 ++dict_stack.ga_len;
307 return OK;
308}
309
310/*
311 * Get the typval at top of the dict stack.
312 */
313 static typval_T *
314dict_stack_get_tv(void)
315{
316 if (dict_stack.ga_len == 0)
317 return NULL;
318 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
319}
320
321/*
322 * Get the dict at top of the dict stack.
323 */
324 static dict_T *
325dict_stack_get_dict(void)
326{
327 typval_T *tv;
328
329 if (dict_stack.ga_len == 0)
330 return NULL;
331 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
332 if (tv->v_type == VAR_DICT)
333 return tv->vval.v_dict;
334 return NULL;
335}
336
337/*
338 * Drop an item from the dict stack.
339 */
340 static void
341dict_stack_drop(void)
342{
343 if (dict_stack.ga_len == 0)
344 {
345 iemsg("Dict stack underflow");
346 return;
347 }
348 --dict_stack.ga_len;
349 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
350}
351
352/*
353 * Drop items from the dict stack until the length is equal to "len".
354 */
355 static void
356dict_stack_clear(int len)
357{
358 while (dict_stack.ga_len > len)
359 dict_stack_drop();
360}
361
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200362/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000363 * Get a pointer to useful "pt_outer" of "pt".
364 */
365 static outer_T *
366get_pt_outer(partial_T *pt)
367{
368 partial_T *ptref = pt->pt_outer_partial;
369
370 if (ptref == NULL)
371 return &pt->pt_outer;
372
373 // partial using partial (recursively)
374 while (ptref->pt_outer_partial != NULL)
375 ptref = ptref->pt_outer_partial;
376 return &ptref->pt_outer;
377}
378
379/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100380 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100381 * This adds a stack frame and sets the instruction pointer to the start of the
382 * called function.
Bram Moolenaar5800c792022-09-22 17:34:01 +0100383 * If "pt_arg" is not NULL use "pt_arg->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 *
385 * Stack has:
386 * - current arguments (already there)
387 * - omitted optional argument (default values) added here
388 * - stack frame:
389 * - pointer to calling function
390 * - Index of next instruction in calling function
391 * - previous frame pointer
392 * - reserved space for local variables
393 */
394 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100395call_dfunc(
396 int cdf_idx,
Bram Moolenaar5800c792022-09-22 17:34:01 +0100397 partial_T *pt_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100399 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100401 int argcount = argcount_arg;
402 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
403 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200404 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100405 int arg_to_add;
406 int vararg_count = 0;
407 int varcount;
408 int idx;
409 estack_T *entry;
410 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200411 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000412 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413
414 if (dfunc->df_deleted)
415 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100416 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000417 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100418 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 return FAIL;
420 }
421
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100422#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100423 if (do_profiling == PROF_YES)
424 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200425 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100426 {
427 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
428 + profile_info_ga.ga_len;
429 ++profile_info_ga.ga_len;
430 CLEAR_POINTER(info);
431 profile_may_start_func(info, ufunc,
432 (((dfunc_T *)def_functions.ga_data)
433 + ectx->ec_dfunc_idx)->df_ufunc);
434 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100435 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100436#endif
437
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200438 // When debugging and using "cont" switches to the not-debugged
439 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000440 compile_type = get_compile_type(ufunc);
441 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200442 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000443 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200444
445 // compile_def_function() may cause def_functions.ga_data to change
446 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
447 }
448 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200449 {
450 if (did_emsg_cumul + did_emsg == did_emsg_before)
451 semsg(_(e_function_is_not_compiled_str),
452 printable_func_name(ufunc));
453 return FAIL;
454 }
455
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200456 if (ufunc->uf_va_name != NULL)
457 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200458 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200459 // Stack at time of call with 2 varargs:
460 // normal_arg
461 // optional_arg
462 // vararg_1
463 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200464 // After creating the list:
465 // normal_arg
466 // optional_arg
467 // vararg-list
468 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200469 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200470 // After creating the list
471 // normal_arg
472 // (space for optional_arg)
473 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200474 vararg_count = argcount - ufunc->uf_args.ga_len;
475 if (vararg_count < 0)
476 vararg_count = 0;
477 else
478 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200479 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200480 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200481
482 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200483 }
484
Bram Moolenaarfe270812020-04-11 22:31:27 +0200485 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200486 if (arg_to_add < 0)
487 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100488 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
489 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200490 return FAIL;
491 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000492 else if (arg_to_add > ufunc->uf_def_args.ga_len)
493 {
494 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
495
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100496 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
497 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000498 return FAIL;
499 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200500
501 // Reserve space for:
502 // - missing arguments
503 // - stack frame
504 // - local variables
505 // - if needed: a counter for number of closures created in
506 // ectx->ec_funcrefs.
507 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100508 if (GA_GROW_FAILS(&ectx->ec_stack,
509 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100510 return FAIL;
511
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100512 // If depth of calling is getting too high, don't execute the function.
513 if (funcdepth_increment() == FAIL)
514 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200515 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100516
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100517 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200518 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100519 {
520 floc = ALLOC_ONE(funclocal_T);
521 if (floc == NULL)
522 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200523 *floc = ectx->ec_funclocal;
524 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100525 }
526
Bram Moolenaarfe270812020-04-11 22:31:27 +0200527 // Move the vararg-list to below the missing optional arguments.
528 if (vararg_count > 0 && arg_to_add > 0)
529 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100530
531 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200532 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200533 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200534 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535
536 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100537 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
538 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200539 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200540 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
541 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100542 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100543 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200544 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545
Bram Moolenaar5800c792022-09-22 17:34:01 +0100546 // Initialize all local variables to number zero. Also initialize the
547 // variable that counts how many closures were created. This is used in
548 // handle_closure_in_use().
549 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
550 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000551 {
552 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
553
554 tv->v_type = VAR_NUMBER;
555 tv->vval.v_number = 0;
556 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200557 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558
Bram Moolenaar5800c792022-09-22 17:34:01 +0100559 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
560 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100561 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200562 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100563
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200564 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100565 return FAIL;
566 if (pt != NULL)
567 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000568 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200569 ++pt->pt_refcount;
570 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100571 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100572 else
573 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200574 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200575 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200576 {
577 vim_free(ref);
578 return FAIL;
579 }
580 ref->or_outer_allocated = TRUE;
581 ref->or_outer->out_stack = &ectx->ec_stack;
582 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
583 if (ectx->ec_outer_ref != NULL)
584 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100585 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200586 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100587 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100588 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200589 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100590
Bram Moolenaarc970e422021-03-17 15:03:04 +0100591 ++ufunc->uf_calls;
592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 // Set execution state to the start of the called function.
594 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100595 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100596 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200597 if (entry != NULL)
598 {
599 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200600 // Save the current context so it can be restored on return.
601 entry->es_save_sctx = current_sctx;
602 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200603 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100604
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200605 // Start execution at the first instruction.
606 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607
608 return OK;
609}
610
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000611// Double linked list of funcstack_T in use.
612static funcstack_T *first_funcstack = NULL;
613
614 static void
615add_funcstack_to_list(funcstack_T *funcstack)
616{
617 // Link in list of funcstacks.
618 if (first_funcstack != NULL)
619 first_funcstack->fs_prev = funcstack;
620 funcstack->fs_next = first_funcstack;
621 funcstack->fs_prev = NULL;
622 first_funcstack = funcstack;
623}
624
625 static void
626remove_funcstack_from_list(funcstack_T *funcstack)
627{
628 if (funcstack->fs_prev == NULL)
629 first_funcstack = funcstack->fs_next;
630 else
631 funcstack->fs_prev->fs_next = funcstack->fs_next;
632 if (funcstack->fs_next != NULL)
633 funcstack->fs_next->fs_prev = funcstack->fs_prev;
634}
635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200637 * Used when returning from a function: Check if any closure is still
638 * referenced. If so then move the arguments and variables to a separate piece
639 * of stack to be used when the closure is called.
640 * When "free_arguments" is TRUE the arguments are to be freed.
641 * Returns FAIL when out of memory.
642 */
643 static int
644handle_closure_in_use(ectx_T *ectx, int free_arguments)
645{
646 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
647 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200648 int argcount;
649 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200650 int idx;
651 typval_T *tv;
652 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200653 garray_T *gap = &ectx->ec_funcrefs;
654 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200655
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200656 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200657 return OK; // function was freed
658 if (dfunc->df_has_closure == 0)
659 return OK; // no closures
660 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
661 closure_count = tv->vval.v_number;
662 if (closure_count == 0)
663 return OK; // no funcrefs created
664
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100665 // Compute "top": the first entry in the stack used by the function.
666 // This is the first argument (after that comes the stack frame and then
667 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200668 argcount = ufunc_argcount(dfunc->df_ufunc);
669 top = ectx->ec_frame_idx - argcount;
670
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200671 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200672 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200673 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200674 partial_T *pt;
675 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200676
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200677 if (off < 0)
678 continue; // count is off or already done
679 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200680 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200681 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200682 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200683 int i;
684
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000685 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200686 // unreferenced on return.
687 for (i = 0; i < dfunc->df_varcount; ++i)
688 {
689 typval_T *stv = STACK_TV(ectx->ec_frame_idx
690 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200691 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200692 --refcount;
693 }
694 if (refcount > 1)
695 {
696 closure_in_use = TRUE;
697 break;
698 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200699 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200700 }
701
702 if (closure_in_use)
703 {
704 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
705 typval_T *stack;
706
707 // A closure is using the arguments and/or local variables.
708 // Move them to the called function.
709 if (funcstack == NULL)
710 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000711
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200712 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100713 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
714 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200715 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
716 funcstack->fs_ga.ga_data = stack;
717 if (stack == NULL)
718 {
719 vim_free(funcstack);
720 return FAIL;
721 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000722 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200723
724 // Move or copy the arguments.
725 for (idx = 0; idx < argcount; ++idx)
726 {
727 tv = STACK_TV(top + idx);
728 if (free_arguments)
729 {
730 *(stack + idx) = *tv;
731 tv->v_type = VAR_UNKNOWN;
732 }
733 else
734 copy_tv(tv, stack + idx);
735 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100736 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200737 // Move the local variables.
738 for (idx = 0; idx < dfunc->df_varcount; ++idx)
739 {
740 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200741
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200742 // A partial created for a local function, that is also used as a
743 // local variable, has a reference count for the variable, thus
744 // will never go down to zero. When all these refcounts are one
745 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000746 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200747 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
748 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200749 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200750
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200751 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200752 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
753 gap->ga_len - closure_count + i])
754 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200755 }
756
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200757 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200758 tv->v_type = VAR_UNKNOWN;
759 }
760
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200761 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200762 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200763 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
764 - closure_count + idx];
765 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200766 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200767 ++funcstack->fs_refcount;
768 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100769 pt->pt_outer.out_stack = &funcstack->fs_ga;
770 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200771 }
772 }
773 }
774
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200775 for (idx = 0; idx < closure_count; ++idx)
776 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
777 - closure_count + idx]);
778 gap->ga_len -= closure_count;
779 if (gap->ga_len == 0)
780 ga_clear(gap);
781
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200782 return OK;
783}
784
785/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200786 * Called when a partial is freed or its reference count goes down to one. The
787 * funcstack may be the only reference to the partials in the local variables.
788 * Go over all of them, the funcref and can be freed if all partials
789 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100790 * Returns TRUE if the funcstack is freed, the partial referencing it will then
791 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200792 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100793 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200794funcstack_check_refcount(funcstack_T *funcstack)
795{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100796 int i;
797 garray_T *gap = &funcstack->fs_ga;
798 int done = 0;
799 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200800
801 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100802 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200803 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
804 {
805 typval_T *tv = ((typval_T *)gap->ga_data) + i;
806
807 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
808 && tv->vval.v_partial->pt_funcstack == funcstack
809 && tv->vval.v_partial->pt_refcount == 1)
810 ++done;
811 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100812 if (done != funcstack->fs_min_refcount)
813 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200814
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100815 stack = gap->ga_data;
816
817 // All partials referencing the funcstack have a reference count of
818 // one, thus the funcstack is no longer of use.
819 for (i = 0; i < gap->ga_len; ++i)
820 clear_tv(stack + i);
821 vim_free(stack);
822 remove_funcstack_from_list(funcstack);
823 vim_free(funcstack);
824
825 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200826}
827
828/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000829 * For garbage collecting: set references in all variables referenced by
830 * all funcstacks.
831 */
832 int
833set_ref_in_funcstacks(int copyID)
834{
835 funcstack_T *funcstack;
836
837 for (funcstack = first_funcstack; funcstack != NULL;
838 funcstack = funcstack->fs_next)
839 {
840 typval_T *stack = funcstack->fs_ga.ga_data;
841 int i;
842
843 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
844 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
845 return TRUE; // abort
846 }
847 return FALSE;
848}
849
Bram Moolenaar806a2732022-09-04 15:40:36 +0100850// Ugly static to avoid passing the execution context around through many
851// layers.
852static ectx_T *current_ectx = NULL;
853
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000854/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100855 * Return TRUE if currently executing a :def function.
856 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100857 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100858 int
859in_def_function(void)
860{
861 return current_ectx != NULL;
862}
863
864/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100865 * Clear "current_ectx" and return the previous value. To be used when calling
866 * a user function.
867 */
868 ectx_T *
869clear_currrent_ectx(void)
870{
871 ectx_T *r = current_ectx;
872
873 current_ectx = NULL;
874 return r;
875}
876
877 void
878restore_current_ectx(ectx_T *ectx)
879{
880 if (current_ectx != NULL)
881 iemsg("Restoring current_ectx while it is not NULL");
882 current_ectx = ectx;
883}
884
885/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100886 * Add an entry for a deferred function call to the currently executing
887 * function.
888 * Return the list or NULL when failed.
889 */
890 static list_T *
891add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100892{
893 typval_T *defer_tv = STACK_TV_VAR(var_idx);
894 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100895 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100896 typval_T listval;
897
898 if (defer_tv->v_type != VAR_LIST)
899 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100900 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100901 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100902 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100903 }
904 defer_l = defer_tv->vval.v_list;
905
906 l = list_alloc_with_items(argcount + 1);
907 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100908 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100909 listval.v_type = VAR_LIST;
910 listval.vval.v_list = l;
911 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100912 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100913 {
914 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100915 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100916 }
917
Bram Moolenaar806a2732022-09-04 15:40:36 +0100918 return l;
919}
920
921/*
922 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
923 * The local variable that lists deferred functions is "var_idx".
924 * Returns OK or FAIL.
925 */
926 static int
927defer_command(int var_idx, int argcount, ectx_T *ectx)
928{
929 list_T *l = add_defer_item(var_idx, argcount, ectx);
930 int i;
931 typval_T *func_tv;
932
933 if (l == NULL)
934 return FAIL;
935
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100936 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100937 if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL)
938 {
939 semsg(_(e_expected_str_but_got_str),
940 "function or partial",
941 vartype_name(func_tv->v_type));
942 return FAIL;
943 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100944 list_set_item(l, 0, func_tv);
945
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100946 for (i = 0; i < argcount; ++i)
947 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100948 ectx->ec_stack.ga_len -= argcount + 1;
949 return OK;
950}
951
952/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100953 * Add a deferred function "name" with one argument "arg_tv".
954 * Consumes "name", also on failure.
955 * Only to be called when in_def_function() returns TRUE.
956 */
957 int
958add_defer_function(char_u *name, int argcount, typval_T *argvars)
959{
960 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
961 + current_ectx->ec_dfunc_idx;
962 list_T *l;
963 typval_T func_tv;
964 int i;
965
966 if (dfunc->df_defer_var_idx == 0)
967 {
968 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100969 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100970 return FAIL;
971 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100972
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100973 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100974 if (l == NULL)
975 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100976 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100977 return FAIL;
978 }
979
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100980 func_tv.v_type = VAR_FUNC;
981 func_tv.v_lock = 0;
982 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100983 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100984
Bram Moolenaar806a2732022-09-04 15:40:36 +0100985 for (i = 0; i < argcount; ++i)
986 list_set_item(l, i + 1, argvars + i);
987 return OK;
988}
989
990/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100991 * Invoked when returning from a function: Invoke any deferred calls.
992 */
993 static void
994invoke_defer_funcs(ectx_T *ectx)
995{
996 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
997 + ectx->ec_dfunc_idx;
998 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
999 listitem_T *li;
1000
1001 if (defer_tv->v_type != VAR_LIST)
1002 return; // no function added
1003 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1004 {
1005 list_T *l = li->li_tv.vval.v_list;
1006 typval_T rettv;
1007 typval_T argvars[MAX_FUNC_ARGS];
1008 int i;
1009 listitem_T *arg_li = l->lv_first;
1010 funcexe_T funcexe;
1011
1012 for (i = 0; i < l->lv_len - 1; ++i)
1013 {
1014 arg_li = arg_li->li_next;
1015 argvars[i] = arg_li->li_tv;
1016 }
1017
1018 CLEAR_FIELD(funcexe);
1019 funcexe.fe_evaluate = TRUE;
1020 rettv.v_type = VAR_UNKNOWN;
1021 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1022 &rettv, l->lv_len - 1, argvars, &funcexe);
1023 clear_tv(&rettv);
1024 }
1025}
1026
1027/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 * Return from the current function.
1029 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001030 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001031func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001034 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001035 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1036 + ectx->ec_dfunc_idx;
1037 int argcount = ufunc_argcount(dfunc->df_ufunc);
1038 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001039 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001040 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1041 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001042 funclocal_T *floc;
1043#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001044 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1045 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046
Bram Moolenaar12d26532021-02-19 19:13:21 +01001047 if (do_profiling == PROF_YES)
1048 {
1049 ufunc_T *caller = prev_dfunc->df_ufunc;
1050
1051 if (dfunc->df_ufunc->uf_profiling
1052 || (caller != NULL && caller->uf_profiling))
1053 {
1054 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1055 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1056 --profile_info_ga.ga_len;
1057 }
1058 }
1059#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001060
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001061 if (dfunc->df_defer_var_idx > 0)
1062 invoke_defer_funcs(ectx);
1063
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001064 // No check for uf_refcount being zero, cannot think of a way that would
1065 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001066 --dfunc->df_ufunc->uf_calls;
1067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001069 entry = estack_pop();
1070 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001071 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001073 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1074 return FAIL;
1075
1076 // Clear the arguments.
1077 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1078 clear_tv(STACK_TV(idx));
1079
1080 // Clear local variables and temp values, but not the return value.
1081 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001082 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001084
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001085 // The return value should be on top of the stack. However, when aborting
1086 // it may not be there and ec_frame_idx is the top of the stack.
1087 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001088 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001089 ret_idx = 0;
1090
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001091 if (ectx->ec_outer_ref != NULL)
1092 {
1093 if (ectx->ec_outer_ref->or_outer_allocated)
1094 vim_free(ectx->ec_outer_ref->or_outer);
1095 partial_unref(ectx->ec_outer_ref->or_partial);
1096 vim_free(ectx->ec_outer_ref);
1097 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001098
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001099 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001100 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001101 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1102 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001103 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1104 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001105 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001106 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001107 floc = (void *)STACK_TV(ectx->ec_frame_idx
1108 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001109 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001110 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1111 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001112
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001113 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001114 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001115 else
1116 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001117 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001118 vim_free(floc);
1119 }
1120
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001121 if (ret_idx > 0)
1122 {
1123 // Reset the stack to the position before the call, with a spot for the
1124 // return value, moved there from above the frame.
1125 ectx->ec_stack.ga_len = top + 1;
1126 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1127 }
1128 else
1129 // Reset the stack to the position before the call.
1130 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001131
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001132 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001133 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001134 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135}
1136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137/*
1138 * Prepare arguments and rettv for calling a builtin or user function.
1139 */
1140 static int
1141call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1142{
1143 int idx;
1144 typval_T *tv;
1145
1146 // Move arguments from bottom of the stack to argvars[] and add terminator.
1147 for (idx = 0; idx < argcount; ++idx)
1148 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1149 argvars[argcount].v_type = VAR_UNKNOWN;
1150
1151 // Result replaces the arguments on the stack.
1152 if (argcount > 0)
1153 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001154 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 return FAIL;
1156 else
1157 ++ectx->ec_stack.ga_len;
1158
1159 // Default return value is zero.
1160 tv = STACK_TV_BOT(-1);
1161 tv->v_type = VAR_NUMBER;
1162 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001163 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164
1165 return OK;
1166}
1167
1168/*
1169 * Call a builtin function by index.
1170 */
1171 static int
1172call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1173{
1174 typval_T argvars[MAX_FUNC_ARGS];
1175 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001176 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001177 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001178 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179
1180 if (call_prepare(argcount, argvars, ectx) == FAIL)
1181 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001182 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001184 // Call the builtin function. Set "current_ectx" so that when it
1185 // recursively invokes call_def_function() a closure context can be set.
1186 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001188 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001189 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190
1191 // Clear the arguments.
1192 for (idx = 0; idx < argcount; ++idx)
1193 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001194
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001195 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001196 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 return OK;
1198}
1199
1200/*
1201 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001202 * If the function is compiled this will add a stack frame and set the
1203 * instruction pointer at the start of the function.
1204 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001205 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001206 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 */
1208 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001209call_ufunc(
1210 ufunc_T *ufunc,
1211 partial_T *pt,
1212 int argcount,
1213 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001214 isn_T *iptr,
1215 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216{
1217 typval_T argvars[MAX_FUNC_ARGS];
1218 funcexe_T funcexe;
1219 int error;
1220 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001221 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001222 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223
Bram Moolenaare99d4222021-06-13 14:01:26 +02001224 if (func_needs_compiling(ufunc, compile_type)
1225 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1226 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001227 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001228 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001229 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001230 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001231 if (error != FCERR_UNKNOWN)
1232 {
1233 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001234 semsg(_(e_too_many_arguments_for_function_str),
1235 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001236 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001237 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001238 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001239 return FAIL;
1240 }
1241
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001242 // The function has been compiled, can call it quickly. For a function
1243 // that was defined later: we can call it directly next time.
1244 if (iptr != NULL)
1245 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001246 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001247 iptr->isn_type = ISN_DCALL;
1248 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1249 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1250 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001251 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253
1254 if (call_prepare(argcount, argvars, ectx) == FAIL)
1255 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001256 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001257 funcexe.fe_evaluate = TRUE;
1258 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259
1260 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001262 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263
1264 // Clear the arguments.
1265 for (idx = 0; idx < argcount; ++idx)
1266 clear_tv(&argvars[idx]);
1267
1268 if (error != FCERR_NONE)
1269 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001270 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 return FAIL;
1272 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001273 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001274 // Error other than from calling the function itself.
1275 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 return OK;
1277}
1278
1279/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001280 * If command modifiers were applied restore them.
1281 */
1282 static void
1283may_restore_cmdmod(funclocal_T *funclocal)
1284{
1285 if (funclocal->floc_restore_cmdmod)
1286 {
1287 cmdmod.cmod_filter_regmatch.regprog = NULL;
1288 undo_cmdmod(&cmdmod);
1289 cmdmod = funclocal->floc_save_cmdmod;
1290 funclocal->floc_restore_cmdmod = FALSE;
1291 }
1292}
1293
1294/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001295 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1296 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001297 */
1298 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001299vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001300{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001301 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001302}
1303
1304/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 * Execute a function by "name".
1306 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001307 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 * Returns FAIL if not found without an error message.
1309 */
1310 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001311call_by_name(
1312 char_u *name,
1313 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001314 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001315 isn_T *iptr,
1316 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317{
1318 ufunc_T *ufunc;
1319
1320 if (builtin_function(name, -1))
1321 {
1322 int func_idx = find_internal_func(name);
1323
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001324 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001326 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327 return FAIL;
1328 return call_bfunc(func_idx, argcount, ectx);
1329 }
1330
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001331 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001332
1333 if (ufunc == NULL)
1334 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001335 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001336
1337 if (script_autoload(name, TRUE))
1338 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001339 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001340
1341 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001342 return FAIL; // bail out if loading the script caused an error
1343 }
1344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001346 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001347 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001348 {
1349 int i;
1350 typval_T *argv = STACK_TV_BOT(0) - argcount;
1351
1352 // The function can change at runtime, check that the argument
1353 // types are correct.
1354 for (i = 0; i < argcount; ++i)
1355 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001356 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001357
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001358 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001359 type = ufunc->uf_arg_types[i];
1360 else if (ufunc->uf_va_type != NULL)
1361 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001362 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001363 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001364 return FAIL;
1365 }
1366 }
1367
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001368 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370
1371 return FAIL;
1372}
1373
1374 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001375call_partial(
1376 typval_T *tv,
1377 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001378 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001380 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001381 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001383 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001384 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385
1386 if (tv->v_type == VAR_PARTIAL)
1387 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001388 partial_T *pt = tv->vval.v_partial;
1389 int i;
1390
1391 if (pt->pt_argc > 0)
1392 {
1393 // Make space for arguments from the partial, shift the "argcount"
1394 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001395 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001396 return FAIL;
1397 for (i = 1; i <= argcount; ++i)
1398 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1399 ectx->ec_stack.ga_len += pt->pt_argc;
1400 argcount += pt->pt_argc;
1401
1402 // copy the arguments from the partial onto the stack
1403 for (i = 0; i < pt->pt_argc; ++i)
1404 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1405 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001406 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407
1408 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001409 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 name = pt->pt_name;
1412 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001413 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001415 if (name != NULL)
1416 {
1417 char_u fname_buf[FLEN_FIXED + 1];
1418 char_u *tofree = NULL;
1419 int error = FCERR_NONE;
1420 char_u *fname;
1421
1422 // May need to translate <SNR>123_ to K_SNR.
1423 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1424 if (error != FCERR_NONE)
1425 res = FAIL;
1426 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001427 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001428 vim_free(tofree);
1429 }
1430
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001431 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 {
1433 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001434 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001435 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436 return FAIL;
1437 }
1438 return OK;
1439}
1440
1441/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001442 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1443 * TRUE.
1444 */
1445 static int
1446error_if_locked(int lock, char *error)
1447{
1448 if (lock & (VAR_LOCKED | VAR_FIXED))
1449 {
1450 emsg(_(error));
1451 return TRUE;
1452 }
1453 return FALSE;
1454}
1455
1456/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001457 * Give an error if "tv" is not a number and return FAIL.
1458 */
1459 static int
1460check_for_number(typval_T *tv)
1461{
1462 if (tv->v_type != VAR_NUMBER)
1463 {
1464 semsg(_(e_expected_str_but_got_str),
1465 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1466 return FAIL;
1467 }
1468 return OK;
1469}
1470
1471/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001472 * Store "tv" in variable "name".
1473 * This is for s: and g: variables.
1474 */
1475 static void
1476store_var(char_u *name, typval_T *tv)
1477{
1478 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001479 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001480
Bram Moolenaard877a572021-04-01 19:42:48 +02001481 if (tv->v_lock)
1482 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001483 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001484 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001485 restore_funccal();
1486}
1487
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001488/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001489 * Convert "tv" to a string.
1490 * Return FAIL if not allowed.
1491 */
1492 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001493do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001494{
1495 if (tv->v_type != VAR_STRING)
1496 {
1497 char_u *str;
1498
1499 if (is_2string_any)
1500 {
1501 switch (tv->v_type)
1502 {
1503 case VAR_SPECIAL:
1504 case VAR_BOOL:
1505 case VAR_NUMBER:
1506 case VAR_FLOAT:
1507 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001508
1509 case VAR_LIST:
1510 if (tolerant)
1511 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001512 char_u *s, *e, *p;
1513 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001514
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001515 ga_init2(&ga, sizeof(char_u *), 1);
1516
1517 // Convert to NL separated items, then
1518 // escape the items and replace the NL with
1519 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001520 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001521 if (str == NULL)
1522 return FAIL;
1523 s = str;
1524 while ((e = vim_strchr(s, '\n')) != NULL)
1525 {
1526 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001527 p = vim_strsave_fnameescape(s,
1528 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001529 if (p != NULL)
1530 {
1531 ga_concat(&ga, p);
1532 ga_concat(&ga, (char_u *)" ");
1533 vim_free(p);
1534 }
1535 s = e + 1;
1536 }
1537 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001538 clear_tv(tv);
1539 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001540 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001541 return OK;
1542 }
1543 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001544 default: to_string_error(tv->v_type);
1545 return FAIL;
1546 }
1547 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001548 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001549 clear_tv(tv);
1550 tv->v_type = VAR_STRING;
1551 tv->vval.v_string = str;
1552 }
1553 return OK;
1554}
1555
1556/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001557 * When the value of "sv" is a null list of dict, allocate it.
1558 */
1559 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001560allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001561{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001562 typval_T *tv = sv->sv_tv;
1563
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001564 switch (tv->v_type)
1565 {
1566 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001567 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001568 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001569 break;
1570 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001571 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001572 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001573 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001574 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001575 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001576 (void)rettv_blob_alloc(tv);
1577 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001578 default:
1579 break;
1580 }
1581}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001582
Bram Moolenaare7525c52021-01-09 13:20:37 +01001583/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001584 * Return the character "str[index]" where "index" is the character index,
1585 * including composing characters.
1586 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001587 */
1588 char_u *
1589char_from_string(char_u *str, varnumber_T index)
1590{
1591 size_t nbyte = 0;
1592 varnumber_T nchar = index;
1593 size_t slen;
1594
1595 if (str == NULL)
1596 return NULL;
1597 slen = STRLEN(str);
1598
Bram Moolenaarff871402021-03-26 13:34:05 +01001599 // Do the same as for a list: a negative index counts from the end.
1600 // Optimization to check the first byte to be below 0x80 (and no composing
1601 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001602 if (index < 0)
1603 {
1604 int clen = 0;
1605
1606 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001607 {
1608 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1609 ++nbyte;
1610 else if (enc_utf8)
1611 nbyte += utfc_ptr2len(str + nbyte);
1612 else
1613 nbyte += mb_ptr2len(str + nbyte);
1614 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001615 nchar = clen + index;
1616 if (nchar < 0)
1617 // unlike list: index out of range results in empty string
1618 return NULL;
1619 }
1620
1621 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001622 {
1623 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1624 ++nbyte;
1625 else if (enc_utf8)
1626 nbyte += utfc_ptr2len(str + nbyte);
1627 else
1628 nbyte += mb_ptr2len(str + nbyte);
1629 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001630 if (nbyte >= slen)
1631 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001632 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001633}
1634
1635/*
1636 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001637 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001638 * If going over the end return "str_len".
1639 * If "idx" is negative count from the end, -1 is the last character.
1640 * When going over the start return -1.
1641 */
1642 static long
1643char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1644{
1645 varnumber_T nchar = idx;
1646 size_t nbyte = 0;
1647
1648 if (nchar >= 0)
1649 {
1650 while (nchar > 0 && nbyte < str_len)
1651 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001652 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001653 --nchar;
1654 }
1655 }
1656 else
1657 {
1658 nbyte = str_len;
1659 while (nchar < 0 && nbyte > 0)
1660 {
1661 --nbyte;
1662 nbyte -= mb_head_off(str, str + nbyte);
1663 ++nchar;
1664 }
1665 if (nchar < 0)
1666 return -1;
1667 }
1668 return (long)nbyte;
1669}
1670
1671/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001672 * Return the slice "str[first : last]" using character indexes. Composing
1673 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001674 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001675 * Return NULL when the result is empty.
1676 */
1677 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001678string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001679{
1680 long start_byte, end_byte;
1681 size_t slen;
1682
1683 if (str == NULL)
1684 return NULL;
1685 slen = STRLEN(str);
1686 start_byte = char_idx2byte(str, slen, first);
1687 if (start_byte < 0)
1688 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001689 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001690 end_byte = (long)slen;
1691 else
1692 {
1693 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001694 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001695 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001696 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001697 }
1698
1699 if (start_byte >= (long)slen || end_byte <= start_byte)
1700 return NULL;
1701 return vim_strnsave(str + start_byte, end_byte - start_byte);
1702}
1703
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001704/*
1705 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1706 * When "dfunc_idx" is negative don't give an error.
1707 * Returns NULL for an error.
1708 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001709 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001710get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001711{
1712 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001713 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1714 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001715 svar_T *sv;
1716
1717 if (sref->sref_seq != si->sn_script_seq)
1718 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001719 // The script was reloaded after the function was compiled, the
1720 // script_idx may not be valid.
1721 if (dfunc != NULL)
1722 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1723 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001724 return NULL;
1725 }
1726 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001727 if (sv->sv_name == NULL)
1728 {
1729 if (dfunc != NULL)
1730 emsg(_(e_script_variable_was_deleted));
1731 return NULL;
1732 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001733 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001734 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001735 if (dfunc != NULL)
1736 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001737 return NULL;
1738 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001739
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001740 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1741 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001742 {
1743 if (dfunc != NULL)
1744 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1745 return NULL;
1746 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001747 return sv;
1748}
1749
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001750/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001751 * Function passed to do_cmdline() for splitting a script joined by NL
1752 * characters.
1753 */
1754 static char_u *
1755get_split_sourceline(
1756 int c UNUSED,
1757 void *cookie,
1758 int indent UNUSED,
1759 getline_opt_T options UNUSED)
1760{
1761 source_cookie_T *sp = (source_cookie_T *)cookie;
1762 char_u *p;
1763 char_u *line;
1764
Bram Moolenaar20677332021-06-06 17:02:53 +02001765 p = vim_strchr(sp->nextline, '\n');
1766 if (p == NULL)
1767 {
1768 line = vim_strsave(sp->nextline);
1769 sp->nextline += STRLEN(sp->nextline);
1770 }
1771 else
1772 {
1773 line = vim_strnsave(sp->nextline, p - sp->nextline);
1774 sp->nextline = p + 1;
1775 }
1776 return line;
1777}
1778
1779/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 * Execute a function by "name".
1781 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001782 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 */
1784 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001785call_eval_func(
1786 char_u *name,
1787 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001788 ectx_T *ectx,
1789 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790{
Bram Moolenaared677f52020-08-12 16:38:10 +02001791 int called_emsg_before = called_emsg;
1792 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001794 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001795 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001797 dictitem_T *v;
1798
1799 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001800 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1801 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001802 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001803 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001804 return FAIL;
1805 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001806 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001808 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809}
1810
1811/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001812 * When a function reference is used, fill a partial with the information
1813 * needed, especially when it is used as a closure.
1814 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001815 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001816fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001817 partial_T *pt,
1818 ufunc_T *ufunc,
1819 loopvarinfo_T *lvi,
1820 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001821{
1822 pt->pt_func = ufunc;
1823 pt->pt_refcount = 1;
1824
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001825 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001826 {
1827 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1828 + ectx->ec_dfunc_idx;
1829
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001830 // The closure may need to find arguments and local variables of the
1831 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001832 pt->pt_outer.out_stack = &ectx->ec_stack;
1833 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001834 if (ectx->ec_outer_ref != NULL)
1835 {
1836 // The current context already has a context, link to that one.
1837 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1838 if (ectx->ec_outer_ref->or_partial != NULL)
1839 {
1840 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1841 ++pt->pt_outer.out_up_partial->pt_refcount;
1842 }
1843 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001844
Bram Moolenaarcc341812022-09-19 15:54:34 +01001845 if (lvi != NULL)
1846 {
1847 int depth;
1848
1849 // The closure may need to find variables defined inside a loop,
1850 // for every nested loop. A new reference is made every time,
1851 // ISN_ENDLOOP will check if they are actually used.
1852 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1853 {
1854 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1855 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1856 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1857 pt->pt_outer.out_loop[depth].var_count =
1858 lvi->lvi_loop[depth].var_count;
1859 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001860 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001861 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001862 else
1863 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001864
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001865 // If the function currently executing returns and the closure is still
1866 // being referenced, we need to make a copy of the context (arguments
1867 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001868 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001869 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001870 {
1871 vim_free(pt);
1872 return FAIL;
1873 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001874 // Extra variable keeps the count of closures created in the current
1875 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001876 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1877 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1878
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001879 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1880 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001881 ++pt->pt_refcount;
1882 ++ectx->ec_funcrefs.ga_len;
1883 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001884 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001885 return OK;
1886}
1887
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001888/*
1889 * Execute iptr->isn_arg.string as an Ex command.
1890 */
1891 static int
1892exec_command(isn_T *iptr)
1893{
1894 source_cookie_T cookie;
1895
1896 SOURCING_LNUM = iptr->isn_lnum;
1897 // Pass getsourceline to get an error for a missing ":end"
1898 // command.
1899 CLEAR_FIELD(cookie);
1900 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1901 if (do_cmdline(iptr->isn_arg.string,
1902 getsourceline, &cookie,
1903 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1904 || did_emsg)
1905 return FAIL;
1906 return OK;
1907}
1908
Bram Moolenaar89445512022-04-14 12:58:23 +01001909/*
1910 * If script "sid" is not loaded yet then load it now.
1911 * Caller must make sure "sid" is a valid script ID.
1912 * "loaded" is set to TRUE if the script had to be loaded.
1913 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1914 */
1915 int
1916may_load_script(int sid, int *loaded)
1917{
1918 scriptitem_T *si = SCRIPT_ITEM(sid);
1919
1920 if (si->sn_state == SN_STATE_NOT_LOADED)
1921 {
1922 *loaded = TRUE;
1923 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1924 {
1925 semsg(_(e_cant_open_file_str), si->sn_name);
1926 return FAIL;
1927 }
1928 }
1929 return OK;
1930}
1931
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001932// used for v_instr of typval of VAR_INSTR
1933struct instr_S {
1934 ectx_T *instr_ectx;
1935 isn_T *instr_instr;
1936};
1937
Bram Moolenaar4c137212021-04-19 16:48:48 +02001938// used for substitute_instr
1939typedef struct subs_expr_S {
1940 ectx_T *subs_ectx;
1941 isn_T *subs_instr;
1942 int subs_status;
1943} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001945// Set when calling do_debug().
1946static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001947static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001948
1949/*
1950 * When debugging lookup "name" and return the typeval.
1951 * When not found return NULL.
1952 */
1953 typval_T *
1954lookup_debug_var(char_u *name)
1955{
1956 int idx;
1957 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001958 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001959 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001960 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001961
1962 if (ectx == NULL)
1963 return NULL;
1964 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1965
1966 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001967 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001968 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001969 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1970
1971 // the variable name may be NULL when not available in this block
1972 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001973 return STACK_TV_VAR(idx);
1974 }
1975
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001976 // Go through argument names.
1977 ufunc = dfunc->df_ufunc;
1978 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1979 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1980 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1981 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1982 - varargs_off + idx);
1983 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1984 return STACK_TV(ectx->ec_frame_idx - 1);
1985
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001986 return NULL;
1987}
1988
Bram Moolenaar26a44842021-09-02 18:49:06 +02001989/*
1990 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1991 * breakpoint was set in that function or when there is any expression.
1992 */
1993 int
1994may_break_in_function(ufunc_T *ufunc)
1995{
1996 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1997}
1998
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001999 static void
2000handle_debug(isn_T *iptr, ectx_T *ectx)
2001{
2002 char_u *line;
2003 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2004 + ectx->ec_dfunc_idx)->df_ufunc;
2005 isn_T *ni;
2006 int end_lnum = iptr->isn_lnum;
2007 garray_T ga;
2008 int lnum;
2009
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002010 if (ex_nesting_level > debug_break_level)
2011 {
2012 linenr_T breakpoint;
2013
Bram Moolenaar26a44842021-09-02 18:49:06 +02002014 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002015 return;
2016
2017 // check for the next breakpoint if needed
2018 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002019 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002020 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2021 return;
2022 }
2023
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002024 SOURCING_LNUM = iptr->isn_lnum;
2025 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002026 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002027
2028 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2029 if (ni->isn_type == ISN_DEBUG
2030 || ni->isn_type == ISN_RETURN
2031 || ni->isn_type == ISN_RETURN_VOID)
2032 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002033 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002034 break;
2035 }
2036
2037 if (end_lnum > iptr->isn_lnum)
2038 {
2039 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002040 for (lnum = iptr->isn_lnum; lnum < end_lnum
2041 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002042 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002043 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002044
Bram Moolenaar303215d2021-07-07 20:10:43 +02002045 if (p == NULL)
2046 continue; // left over from continuation line
2047 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002048 if (*p == '#')
2049 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002050 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002051 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2052 if (STRNCMP(p, "def ", 4) == 0)
2053 break;
2054 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002055 line = ga_concat_strings(&ga, " ");
2056 vim_free(ga.ga_data);
2057 }
2058 else
2059 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002060
Dominique Pelle6e969552021-06-17 13:53:41 +02002061 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002062 debug_context = NULL;
2063
2064 if (end_lnum > iptr->isn_lnum)
2065 vim_free(line);
2066}
2067
Bram Moolenaar4c137212021-04-19 16:48:48 +02002068/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002069 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002070 * Returns OK, FAIL or NOTDONE (uncatchable error).
2071 */
2072 static int
2073execute_storeindex(isn_T *iptr, ectx_T *ectx)
2074{
2075 vartype_T dest_type = iptr->isn_arg.vartype;
2076 typval_T *tv;
2077 typval_T *tv_idx = STACK_TV_BOT(-2);
2078 typval_T *tv_dest = STACK_TV_BOT(-1);
2079 int status = OK;
2080
2081 // Stack contains:
2082 // -3 value to be stored
2083 // -2 index
2084 // -1 dict or list
2085 tv = STACK_TV_BOT(-3);
2086 SOURCING_LNUM = iptr->isn_lnum;
2087 if (dest_type == VAR_ANY)
2088 {
2089 dest_type = tv_dest->v_type;
2090 if (dest_type == VAR_DICT)
2091 status = do_2string(tv_idx, TRUE, FALSE);
2092 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2093 {
2094 emsg(_(e_number_expected));
2095 status = FAIL;
2096 }
2097 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002098
2099 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002100 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002101 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002102 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002103 long lidx = (long)tv_idx->vval.v_number;
2104 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002105
Bram Moolenaare08be092022-02-17 13:08:26 +00002106 if (list == NULL)
2107 {
2108 emsg(_(e_list_not_set));
2109 return FAIL;
2110 }
2111 if (lidx < 0 && list->lv_len + lidx >= 0)
2112 // negative index is relative to the end
2113 lidx = list->lv_len + lidx;
2114 if (lidx < 0 || lidx > list->lv_len)
2115 {
2116 semsg(_(e_list_index_out_of_range_nr), lidx);
2117 return FAIL;
2118 }
2119 if (lidx < list->lv_len)
2120 {
2121 listitem_T *li = list_find(list, lidx);
2122
2123 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002124 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002125 return FAIL;
2126 // overwrite existing list item
2127 clear_tv(&li->li_tv);
2128 li->li_tv = *tv;
2129 }
2130 else
2131 {
2132 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2133 return FAIL;
2134 // append to list, only fails when out of memory
2135 if (list_append_tv(list, tv) == FAIL)
2136 return NOTDONE;
2137 clear_tv(tv);
2138 }
2139 }
2140 else if (dest_type == VAR_DICT)
2141 {
2142 char_u *key = tv_idx->vval.v_string;
2143 dict_T *dict = tv_dest->vval.v_dict;
2144 dictitem_T *di;
2145
2146 SOURCING_LNUM = iptr->isn_lnum;
2147 if (dict == NULL)
2148 {
2149 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002150 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002151 }
2152 if (key == NULL)
2153 key = (char_u *)"";
2154 di = dict_find(dict, key, -1);
2155 if (di != NULL)
2156 {
2157 if (error_if_locked(di->di_tv.v_lock,
2158 e_cannot_change_dict_item))
2159 return FAIL;
2160 // overwrite existing value
2161 clear_tv(&di->di_tv);
2162 di->di_tv = *tv;
2163 }
2164 else
2165 {
2166 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2167 return FAIL;
2168 // add to dict, only fails when out of memory
2169 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2170 return NOTDONE;
2171 clear_tv(tv);
2172 }
2173 }
2174 else if (dest_type == VAR_BLOB)
2175 {
2176 long lidx = (long)tv_idx->vval.v_number;
2177 blob_T *blob = tv_dest->vval.v_blob;
2178 varnumber_T nr;
2179 int error = FALSE;
2180 int len;
2181
2182 if (blob == NULL)
2183 {
2184 emsg(_(e_blob_not_set));
2185 return FAIL;
2186 }
2187 len = blob_len(blob);
2188 if (lidx < 0 && len + lidx >= 0)
2189 // negative index is relative to the end
2190 lidx = len + lidx;
2191
2192 // Can add one byte at the end.
2193 if (lidx < 0 || lidx > len)
2194 {
2195 semsg(_(e_blob_index_out_of_range_nr), lidx);
2196 return FAIL;
2197 }
2198 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2199 return FAIL;
2200 nr = tv_get_number_chk(tv, &error);
2201 if (error)
2202 return FAIL;
2203 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002204 }
2205 else
2206 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002207 status = FAIL;
2208 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002209 }
2210 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002211
2212 clear_tv(tv_idx);
2213 clear_tv(tv_dest);
2214 ectx->ec_stack.ga_len -= 3;
2215 if (status == FAIL)
2216 {
2217 clear_tv(tv);
2218 return FAIL;
2219 }
2220 return OK;
2221}
2222
2223/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002224 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002225 */
2226 static int
2227execute_storerange(isn_T *iptr, ectx_T *ectx)
2228{
2229 typval_T *tv;
2230 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2231 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2232 typval_T *tv_dest = STACK_TV_BOT(-1);
2233 int status = OK;
2234
2235 // Stack contains:
2236 // -4 value to be stored
2237 // -3 first index or "none"
2238 // -2 second index or "none"
2239 // -1 destination list or blob
2240 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002241 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002242 if (tv_dest->v_type == VAR_LIST)
2243 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002244 long n1;
2245 long n2;
2246 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002247
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002248 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2249 if (tv_idx2->v_type == VAR_SPECIAL
2250 && tv_idx2->vval.v_number == VVAL_NONE)
2251 n2 = list_len(tv_dest->vval.v_list) - 1;
2252 else
2253 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2254
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002255 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002256 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002257 status = FAIL;
2258 else
2259 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002260 status = check_range_index_two(tv_dest->vval.v_list,
2261 &n1, li1, &n2, FALSE);
2262 if (status != FAIL)
2263 status = list_assign_range(
2264 tv_dest->vval.v_list,
2265 tv->vval.v_list,
2266 n1,
2267 n2,
2268 tv_idx2->v_type == VAR_SPECIAL,
2269 (char_u *)"=",
2270 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002271 }
2272 }
2273 else if (tv_dest->v_type == VAR_BLOB)
2274 {
2275 varnumber_T n1;
2276 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002277 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002278
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002279 n1 = tv_get_number_chk(tv_idx1, NULL);
2280 if (tv_idx2->v_type == VAR_SPECIAL
2281 && tv_idx2->vval.v_number == VVAL_NONE)
2282 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2283 else
2284 n2 = tv_get_number_chk(tv_idx2, NULL);
2285 bloblen = blob_len(tv_dest->vval.v_blob);
2286
2287 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2288 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002289 status = FAIL;
2290 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002291 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002292 }
2293 else
2294 {
2295 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002296 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002297 }
2298
2299 clear_tv(tv_idx1);
2300 clear_tv(tv_idx2);
2301 clear_tv(tv_dest);
2302 ectx->ec_stack.ga_len -= 4;
2303 clear_tv(tv);
2304
2305 return status;
2306}
2307
2308/*
2309 * Unlet item in list or dict variable.
2310 */
2311 static int
2312execute_unletindex(isn_T *iptr, ectx_T *ectx)
2313{
2314 typval_T *tv_idx = STACK_TV_BOT(-2);
2315 typval_T *tv_dest = STACK_TV_BOT(-1);
2316 int status = OK;
2317
2318 // Stack contains:
2319 // -2 index
2320 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002321 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002322 if (tv_dest->v_type == VAR_DICT)
2323 {
2324 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002325 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002326 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002327 semsg(_(e_expected_str_but_got_str),
2328 vartype_name(VAR_STRING),
2329 vartype_name(tv_idx->v_type));
2330 status = FAIL;
2331 }
2332 else
2333 {
2334 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002335 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002336 dictitem_T *di = NULL;
2337
2338 if (d != NULL && value_check_lock(
2339 d->dv_lock, NULL, FALSE))
2340 status = FAIL;
2341 else
2342 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002343 if (tv_idx->v_type == VAR_STRING)
2344 {
2345 key = tv_idx->vval.v_string;
2346 if (key == NULL)
2347 key = (char_u *)"";
2348 }
2349 else
2350 {
2351 key = tv_get_string(tv_idx);
2352 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002353 if (d != NULL)
2354 di = dict_find(d, key, (int)STRLEN(key));
2355 if (di == NULL)
2356 {
2357 // NULL dict is equivalent to empty dict
2358 semsg(_(e_key_not_present_in_dictionary),
2359 key);
2360 status = FAIL;
2361 }
2362 else if (var_check_fixed(di->di_flags,
2363 NULL, FALSE)
2364 || var_check_ro(di->di_flags,
2365 NULL, FALSE))
2366 status = FAIL;
2367 else
2368 dictitem_remove(d, di);
2369 }
2370 }
2371 }
2372 else if (tv_dest->v_type == VAR_LIST)
2373 {
2374 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002375 if (check_for_number(tv_idx) == FAIL)
2376 {
2377 status = FAIL;
2378 }
2379 else
2380 {
2381 list_T *l = tv_dest->vval.v_list;
2382 long n = (long)tv_idx->vval.v_number;
2383
2384 if (l != NULL && value_check_lock(
2385 l->lv_lock, NULL, FALSE))
2386 status = FAIL;
2387 else
2388 {
2389 listitem_T *li = list_find(l, n);
2390
2391 if (li == NULL)
2392 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002393 semsg(_(e_list_index_out_of_range_nr), n);
2394 status = FAIL;
2395 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002396 else
2397 listitem_remove(l, li);
2398 }
2399 }
2400 }
2401 else
2402 {
2403 status = FAIL;
2404 semsg(_(e_cannot_index_str),
2405 vartype_name(tv_dest->v_type));
2406 }
2407
2408 clear_tv(tv_idx);
2409 clear_tv(tv_dest);
2410 ectx->ec_stack.ga_len -= 2;
2411
2412 return status;
2413}
2414
2415/*
2416 * Unlet a range of items in a list variable.
2417 */
2418 static int
2419execute_unletrange(isn_T *iptr, ectx_T *ectx)
2420{
2421 // Stack contains:
2422 // -3 index1
2423 // -2 index2
2424 // -1 dict or list
2425 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2426 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2427 typval_T *tv_dest = STACK_TV_BOT(-1);
2428 int status = OK;
2429
2430 if (tv_dest->v_type == VAR_LIST)
2431 {
2432 // indexes must be a number
2433 SOURCING_LNUM = iptr->isn_lnum;
2434 if (check_for_number(tv_idx1) == FAIL
2435 || (tv_idx2->v_type != VAR_SPECIAL
2436 && check_for_number(tv_idx2) == FAIL))
2437 {
2438 status = FAIL;
2439 }
2440 else
2441 {
2442 list_T *l = tv_dest->vval.v_list;
2443 long n1 = (long)tv_idx1->vval.v_number;
2444 long n2 = tv_idx2->v_type == VAR_SPECIAL
2445 ? 0 : (long)tv_idx2->vval.v_number;
2446 listitem_T *li;
2447
2448 li = list_find_index(l, &n1);
2449 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002450 {
2451 semsg(_(e_list_index_out_of_range_nr),
2452 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002453 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002454 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002455 else
2456 {
2457 if (n1 < 0)
2458 n1 = list_idx_of_item(l, li);
2459 if (n2 < 0)
2460 {
2461 listitem_T *li2 = list_find(l, n2);
2462
2463 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002464 {
2465 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002466 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002467 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002468 else
2469 n2 = list_idx_of_item(l, li2);
2470 }
2471 if (status != FAIL
2472 && tv_idx2->v_type != VAR_SPECIAL
2473 && n2 < n1)
2474 {
2475 semsg(_(e_list_index_out_of_range_nr), n2);
2476 status = FAIL;
2477 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002478 if (status != FAIL)
2479 list_unlet_range(l, li, n1,
2480 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002481 }
2482 }
2483 }
2484 else
2485 {
2486 status = FAIL;
2487 SOURCING_LNUM = iptr->isn_lnum;
2488 semsg(_(e_cannot_index_str),
2489 vartype_name(tv_dest->v_type));
2490 }
2491
2492 clear_tv(tv_idx1);
2493 clear_tv(tv_idx2);
2494 clear_tv(tv_dest);
2495 ectx->ec_stack.ga_len -= 3;
2496
2497 return status;
2498}
2499
2500/*
2501 * Top of a for loop.
2502 */
2503 static int
2504execute_for(isn_T *iptr, ectx_T *ectx)
2505{
2506 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002507 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002508 typval_T *ltv = STACK_TV_BOT(-1);
2509 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002510 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002511
2512 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2513 return FAIL;
2514 if (ltv->v_type == VAR_LIST)
2515 {
2516 list_T *list = ltv->vval.v_list;
2517
2518 // push the next item from the list
2519 ++idxtv->vval.v_number;
2520 if (list == NULL
2521 || idxtv->vval.v_number >= list->lv_len)
2522 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002523 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002524 }
2525 else if (list->lv_first == &range_list_item)
2526 {
2527 // non-materialized range() list
2528 tv = STACK_TV_BOT(0);
2529 tv->v_type = VAR_NUMBER;
2530 tv->v_lock = 0;
2531 tv->vval.v_number = list_find_nr(
2532 list, idxtv->vval.v_number, NULL);
2533 ++ectx->ec_stack.ga_len;
2534 }
2535 else
2536 {
2537 listitem_T *li = list_find(list,
2538 idxtv->vval.v_number);
2539
2540 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2541 ++ectx->ec_stack.ga_len;
2542 }
2543 }
2544 else if (ltv->v_type == VAR_STRING)
2545 {
2546 char_u *str = ltv->vval.v_string;
2547
2548 // The index is for the last byte of the previous
2549 // character.
2550 ++idxtv->vval.v_number;
2551 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2552 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002553 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002554 }
2555 else
2556 {
2557 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2558
2559 // Push the next character from the string.
2560 tv = STACK_TV_BOT(0);
2561 tv->v_type = VAR_STRING;
2562 tv->vval.v_string = vim_strnsave(
2563 str + idxtv->vval.v_number, clen);
2564 ++ectx->ec_stack.ga_len;
2565 idxtv->vval.v_number += clen - 1;
2566 }
2567 }
2568 else if (ltv->v_type == VAR_BLOB)
2569 {
2570 blob_T *blob = ltv->vval.v_blob;
2571
2572 // When we get here the first time make a copy of the
2573 // blob, so that the iteration still works when it is
2574 // changed.
2575 if (idxtv->vval.v_number == -1 && blob != NULL)
2576 {
2577 blob_copy(blob, ltv);
2578 blob_unref(blob);
2579 blob = ltv->vval.v_blob;
2580 }
2581
2582 // The index is for the previous byte.
2583 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002584 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002585 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002586 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002587 }
2588 else
2589 {
2590 // Push the next byte from the blob.
2591 tv = STACK_TV_BOT(0);
2592 tv->v_type = VAR_NUMBER;
2593 tv->vval.v_number = blob_get(blob,
2594 idxtv->vval.v_number);
2595 ++ectx->ec_stack.ga_len;
2596 }
2597 }
2598 else
2599 {
2600 semsg(_(e_for_loop_on_str_not_supported),
2601 vartype_name(ltv->v_type));
2602 return FAIL;
2603 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002604
2605 if (jump)
2606 {
2607 // past the end of the list/string/blob, jump to "endfor"
2608 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2609 may_restore_cmdmod(&ectx->ec_funclocal);
2610 }
2611 else
2612 {
2613 // Store the current number of funcrefs, this may be used in
2614 // ISN_LOOPEND. The variable index is always one more than the loop
2615 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002616 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002617 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2618 }
2619
2620 return OK;
2621}
2622
2623/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002624 * Code for handling variables declared inside a loop and used in a closure.
2625 * This is very similar to what is done with funcstack_T. The difference is
2626 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2627 * scope of the block inside a loop and each loop may have its own.
2628 */
2629
2630// Double linked list of loopvars_T in use.
2631static loopvars_T *first_loopvars = NULL;
2632
2633 static void
2634add_loopvars_to_list(loopvars_T *loopvars)
2635{
2636 // Link in list of loopvarss.
2637 if (first_loopvars != NULL)
2638 first_loopvars->lvs_prev = loopvars;
2639 loopvars->lvs_next = first_loopvars;
2640 loopvars->lvs_prev = NULL;
2641 first_loopvars = loopvars;
2642}
2643
2644 static void
2645remove_loopvars_from_list(loopvars_T *loopvars)
2646{
2647 if (loopvars->lvs_prev == NULL)
2648 first_loopvars = loopvars->lvs_next;
2649 else
2650 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2651 if (loopvars->lvs_next != NULL)
2652 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2653}
2654
2655/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002656 * End of a for or while loop: Handle any variables used by a closure.
2657 */
2658 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002659execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002660{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002661 endloop_T *endloop = &iptr->isn_arg.endloop;
2662 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2663 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002664 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002665 garray_T *gap = &ectx->ec_funcrefs;
2666 int closure_in_use = FALSE;
2667 loopvars_T *loopvars;
2668 typval_T *stack;
2669 int idx;
2670
Bram Moolenaarcc341812022-09-19 15:54:34 +01002671 // Check if any created closure is still being referenced and loopvars have
2672 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002673 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2674 {
2675 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2676
Bram Moolenaarcc341812022-09-19 15:54:34 +01002677 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002678 {
2679 int refcount = pt->pt_refcount;
2680 int i;
2681
2682 // A Reference in a variable inside the loop doesn't count, it gets
2683 // unreferenced at the end of the loop.
2684 for (i = 0; i < endloop->end_var_count; ++i)
2685 {
2686 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2687
2688 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2689 --refcount;
2690 }
2691 if (refcount > 1)
2692 {
2693 closure_in_use = TRUE;
2694 break;
2695 }
2696 }
2697 }
2698
2699 // If no function reference were created since the start of the loop block
2700 // or it is no longer referenced there is nothing to do.
2701 if (!closure_in_use)
2702 return OK;
2703
2704 // A closure is using variables declared inside the loop.
2705 // Move them to the called function.
2706 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2707 if (loopvars == NULL)
2708 return FAIL;
2709
2710 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2711 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2712 loopvars->lvs_ga.ga_data = stack;
2713 if (stack == NULL)
2714 {
2715 vim_free(loopvars);
2716 return FAIL;
2717 }
2718 add_loopvars_to_list(loopvars);
2719
2720 // Move the variable values.
2721 for (idx = 0; idx < endloop->end_var_count; ++idx)
2722 {
2723 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2724
2725 *(stack + idx) = *tv;
2726 tv->v_type = VAR_UNKNOWN;
2727 }
2728
2729 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2730 {
2731 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2732
Bram Moolenaarcc341812022-09-19 15:54:34 +01002733 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002734 {
2735 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002736 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002737
Bram Moolenaarcc341812022-09-19 15:54:34 +01002738 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2739 pt->pt_outer.out_loop[depth].var_idx -=
2740 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002741 }
2742 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002743
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002744 return OK;
2745}
2746
2747/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002748 * Called when a partial is freed or its reference count goes down to one. The
2749 * loopvars may be the only reference to the partials in the local variables.
2750 * Go over all of them, the funcref and can be freed if all partials
2751 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002752 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002753 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002754 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002755loopvars_check_refcount(loopvars_T *loopvars)
2756{
2757 int i;
2758 garray_T *gap = &loopvars->lvs_ga;
2759 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002760 typval_T *stack = gap->ga_data;
2761
Bram Moolenaarcc341812022-09-19 15:54:34 +01002762 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2763 return FALSE;
2764 for (i = 0; i < gap->ga_len; ++i)
2765 {
2766 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2767
2768 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2769 && tv->vval.v_partial->pt_refcount == 1)
2770 {
2771 int depth;
2772
2773 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2774 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2775 ++done;
2776 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002777 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002778 if (done != loopvars->lvs_min_refcount)
2779 return FALSE;
2780
2781 // All partials referencing the loopvars have a reference count of
2782 // one, thus the loopvars is no longer of use.
2783 stack = gap->ga_data;
2784 for (i = 0; i < gap->ga_len; ++i)
2785 clear_tv(stack + i);
2786 vim_free(stack);
2787 remove_loopvars_from_list(loopvars);
2788 vim_free(loopvars);
2789 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002790}
2791
2792/*
2793 * For garbage collecting: set references in all variables referenced by
2794 * all loopvars.
2795 */
2796 int
2797set_ref_in_loopvars(int copyID)
2798{
2799 loopvars_T *loopvars;
2800
2801 for (loopvars = first_loopvars; loopvars != NULL;
2802 loopvars = loopvars->lvs_next)
2803 {
2804 typval_T *stack = loopvars->lvs_ga.ga_data;
2805 int i;
2806
2807 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2808 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2809 return TRUE; // abort
2810 }
2811 return FALSE;
2812}
2813
2814/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002815 * Load instruction for w:/b:/g:/t: variable.
2816 * "isn_type" is used instead of "iptr->isn_type".
2817 */
2818 static int
2819load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2820{
2821 dictitem_T *di = NULL;
2822 hashtab_T *ht = NULL;
2823 char namespace;
2824
2825 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2826 return NOTDONE;
2827 switch (isn_type)
2828 {
2829 case ISN_LOADG:
2830 ht = get_globvar_ht();
2831 namespace = 'g';
2832 break;
2833 case ISN_LOADB:
2834 ht = &curbuf->b_vars->dv_hashtab;
2835 namespace = 'b';
2836 break;
2837 case ISN_LOADW:
2838 ht = &curwin->w_vars->dv_hashtab;
2839 namespace = 'w';
2840 break;
2841 case ISN_LOADT:
2842 ht = &curtab->tp_vars->dv_hashtab;
2843 namespace = 't';
2844 break;
2845 default: // Cannot reach here
2846 return NOTDONE;
2847 }
2848 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2849
Bram Moolenaar06b77222022-01-25 15:51:56 +00002850 if (di == NULL)
2851 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002852 if (isn_type == ISN_LOADG)
2853 {
2854 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2855
2856 // g:Something could be a function
2857 if (ufunc != NULL)
2858 {
2859 typval_T *tv = STACK_TV_BOT(0);
2860
2861 ++ectx->ec_stack.ga_len;
2862 tv->v_type = VAR_FUNC;
2863 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2864 if (tv->vval.v_string == NULL)
2865 return FAIL;
2866 STRCPY(tv->vval.v_string, "g:");
2867 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2868 return OK;
2869 }
2870 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002871 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002872 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002873 // no check if the item exists in the script but
2874 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002875 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002876 else
2877 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002878 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002879 return FAIL;
2880 }
2881 else
2882 {
2883 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2884 ++ectx->ec_stack.ga_len;
2885 }
2886 return OK;
2887}
2888
2889/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002890 * Execute instructions in execution context "ectx".
2891 * Return OK or FAIL;
2892 */
2893 static int
2894exec_instructions(ectx_T *ectx)
2895{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002896 int ret = FAIL;
2897 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002898 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002899
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002900 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002901 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002902
Bram Moolenaarff652882021-05-16 15:24:49 +02002903 // Only catch exceptions in this instruction list.
2904 ectx->ec_trylevel_at_start = trylevel;
2905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 for (;;)
2907 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002908 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002910 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002911
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002912 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002913 {
2914 line_breakcheck();
2915 breakcheck_count = 0;
2916 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002917 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002918 {
2919 // Turn CTRL-C into an exception.
2920 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002921 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002922 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002923 did_throw = TRUE;
2924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002926 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002927 {
2928 // Turn an error message into an exception.
2929 did_emsg = FALSE;
2930 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002931 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002932 did_throw = TRUE;
2933 *msg_list = NULL;
2934 }
2935
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002936 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002938 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002939 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002940 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941
2942 // An exception jumps to the first catch, finally, or returns from
2943 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002944 while (index > 0)
2945 {
2946 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002947 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002948 break;
2949 // In the catch and finally block of this try we have to go up
2950 // one level.
2951 --index;
2952 trycmd = NULL;
2953 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002954 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002956 if (trycmd->tcd_in_catch)
2957 {
2958 // exception inside ":catch", jump to ":finally" once
2959 ectx->ec_iidx = trycmd->tcd_finally_idx;
2960 trycmd->tcd_finally_idx = 0;
2961 }
2962 else
2963 // jump to first ":catch"
2964 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002965 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002966 did_throw = FALSE; // don't come back here until :endtry
2967 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 }
2969 else
2970 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002971 // Not inside try or need to return from current functions.
2972 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002973 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002974 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002975 tv = STACK_TV_BOT(0);
2976 tv->v_type = VAR_NUMBER;
2977 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002978 ++ectx->ec_stack.ga_len;
2979 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002981 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002982 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002983 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002984 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 goto done;
2986 }
2987
Bram Moolenaar4c137212021-04-19 16:48:48 +02002988 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002989 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 }
2991 continue;
2992 }
2993
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002994 /*
2995 * Big switch on the instruction. Most compilers will be turning this
2996 * into an efficient lookup table, since the "case" values are an enum
2997 * with sequential numbers. It may look ugly, but it should be the
2998 * most efficient way.
2999 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003000 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 switch (iptr->isn_type)
3002 {
3003 // execute Ex command line
3004 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003005 if (exec_command(iptr) == FAIL)
3006 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 break;
3008
Bram Moolenaar20677332021-06-06 17:02:53 +02003009 // execute Ex command line split at NL characters.
3010 case ISN_EXEC_SPLIT:
3011 {
3012 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003013 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003014
3015 SOURCING_LNUM = iptr->isn_lnum;
3016 CLEAR_FIELD(cookie);
3017 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3018 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003019 line = get_split_sourceline(0, &cookie, 0, 0);
3020 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003021 get_split_sourceline, &cookie,
3022 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3023 == FAIL
3024 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003025 {
3026 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003027 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003028 }
3029 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003030 }
3031 break;
3032
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003033 // execute Ex command line that is only a range
3034 case ISN_EXECRANGE:
3035 {
3036 exarg_T ea;
3037 char *error = NULL;
3038
3039 CLEAR_FIELD(ea);
3040 ea.cmdidx = CMD_SIZE;
3041 ea.addr_type = ADDR_LINES;
3042 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003043 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003044 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003045 if (ea.cmd == NULL)
3046 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003047 // error is always NULL when using ADDR_LINES
3048 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003049 if (error != NULL)
3050 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003051 emsg(error);
3052 goto on_error;
3053 }
3054 }
3055 break;
3056
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003057 // Evaluate an expression with legacy syntax, push it onto the
3058 // stack.
3059 case ISN_LEGACY_EVAL:
3060 {
3061 char_u *arg = iptr->isn_arg.string;
3062 int res;
3063 int save_flags = cmdmod.cmod_flags;
3064
Bram Moolenaar35578162021-08-02 19:10:38 +02003065 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003066 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003067 tv = STACK_TV_BOT(0);
3068 init_tv(tv);
3069 cmdmod.cmod_flags |= CMOD_LEGACY;
3070 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3071 cmdmod.cmod_flags = save_flags;
3072 if (res == FAIL)
3073 goto on_error;
3074 ++ectx->ec_stack.ga_len;
3075 }
3076 break;
3077
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003078 // push typeval VAR_INSTR with instructions to be executed
3079 case ISN_INSTR:
3080 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003081 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003082 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003083 tv = STACK_TV_BOT(0);
3084 tv->vval.v_instr = ALLOC_ONE(instr_T);
3085 if (tv->vval.v_instr == NULL)
3086 goto on_error;
3087 ++ectx->ec_stack.ga_len;
3088
3089 tv->v_type = VAR_INSTR;
3090 tv->vval.v_instr->instr_ectx = ectx;
3091 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3092 }
3093 break;
3094
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003095 case ISN_SOURCE:
3096 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003097 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003098
Bram Moolenaar89445512022-04-14 12:58:23 +01003099 SOURCING_LNUM = iptr->isn_lnum;
3100 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003101 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003102 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003103 }
3104 break;
3105
Bram Moolenaar4c137212021-04-19 16:48:48 +02003106 // execute :substitute with an expression
3107 case ISN_SUBSTITUTE:
3108 {
3109 subs_T *subs = &iptr->isn_arg.subs;
3110 source_cookie_T cookie;
3111 struct subs_expr_S *save_instr = substitute_instr;
3112 struct subs_expr_S subs_instr;
3113 int res;
3114
3115 subs_instr.subs_ectx = ectx;
3116 subs_instr.subs_instr = subs->subs_instr;
3117 subs_instr.subs_status = OK;
3118 substitute_instr = &subs_instr;
3119
3120 SOURCING_LNUM = iptr->isn_lnum;
3121 // This is very much like ISN_EXEC
3122 CLEAR_FIELD(cookie);
3123 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3124 res = do_cmdline(subs->subs_cmd,
3125 getsourceline, &cookie,
3126 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3127 substitute_instr = save_instr;
3128
3129 if (res == FAIL || did_emsg
3130 || subs_instr.subs_status == FAIL)
3131 goto on_error;
3132 }
3133 break;
3134
3135 case ISN_FINISH:
3136 goto done;
3137
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003138 case ISN_REDIRSTART:
3139 // create a dummy entry for var_redir_str()
3140 if (alloc_redir_lval() == FAIL)
3141 goto on_error;
3142
3143 // The output is stored in growarray "redir_ga" until
3144 // redirection ends.
3145 init_redir_ga();
3146 redir_vname = 1;
3147 break;
3148
3149 case ISN_REDIREND:
3150 {
3151 char_u *res = get_clear_redir_ga();
3152
3153 // End redirection, put redirected text on the stack.
3154 clear_redir_lval();
3155 redir_vname = 0;
3156
Bram Moolenaar35578162021-08-02 19:10:38 +02003157 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003158 {
3159 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003160 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003161 }
3162 tv = STACK_TV_BOT(0);
3163 tv->v_type = VAR_STRING;
3164 tv->vval.v_string = res;
3165 ++ectx->ec_stack.ga_len;
3166 }
3167 break;
3168
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003169 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003170#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003171 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003172 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3173 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003174 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003175#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003176 break;
3177
3178 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003179#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003180 {
3181 exarg_T ea;
3182 int res;
3183
3184 CLEAR_FIELD(ea);
3185 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3186 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3187 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3188 --ectx->ec_stack.ga_len;
3189 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003190 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003191 res = cexpr_core(&ea, tv);
3192 clear_tv(tv);
3193 if (res == FAIL)
3194 goto on_error;
3195 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003196#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003197 break;
3198
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003199 // execute Ex command from pieces on the stack
3200 case ISN_EXECCONCAT:
3201 {
3202 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003203 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003204 int pass;
3205 int i;
3206 char_u *cmd = NULL;
3207 char_u *str;
3208
3209 for (pass = 1; pass <= 2; ++pass)
3210 {
3211 for (i = 0; i < count; ++i)
3212 {
3213 tv = STACK_TV_BOT(i - count);
3214 str = tv->vval.v_string;
3215 if (str != NULL && *str != NUL)
3216 {
3217 if (pass == 2)
3218 STRCPY(cmd + len, str);
3219 len += STRLEN(str);
3220 }
3221 if (pass == 2)
3222 clear_tv(tv);
3223 }
3224 if (pass == 1)
3225 {
3226 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003227 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003228 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003229 len = 0;
3230 }
3231 }
3232
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003233 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003234 do_cmdline_cmd(cmd);
3235 vim_free(cmd);
3236 }
3237 break;
3238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 // execute :echo {string} ...
3240 case ISN_ECHO:
3241 {
3242 int count = iptr->isn_arg.echo.echo_count;
3243 int atstart = TRUE;
3244 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003245 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246
3247 for (idx = 0; idx < count; ++idx)
3248 {
3249 tv = STACK_TV_BOT(idx - count);
3250 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3251 &atstart, &needclr);
3252 clear_tv(tv);
3253 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003254 if (needclr)
3255 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003256 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 }
3258 break;
3259
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003260 // :execute {string} ...
3261 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003262 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003263 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003264 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003265 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003266 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003267 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003268 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003269 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003270 {
3271 int count = iptr->isn_arg.number;
3272 garray_T ga;
3273 char_u buf[NUMBUFLEN];
3274 char_u *p;
3275 int len;
3276 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003277 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003278
3279 ga_init2(&ga, 1, 80);
3280 for (idx = 0; idx < count; ++idx)
3281 {
3282 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003283 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003284 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003285 if (tv->v_type == VAR_CHANNEL
3286 || tv->v_type == VAR_JOB)
3287 {
3288 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003289 semsg(_(e_using_invalid_value_as_string_str),
3290 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003291 break;
3292 }
3293 else
3294 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003295 }
3296 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003297 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003298
3299 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003300 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003301 failed = TRUE;
3302 else
3303 {
3304 if (ga.ga_len > 0)
3305 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3306 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3307 ga.ga_len += len;
3308 }
3309 clear_tv(tv);
3310 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003311 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003312 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003313 {
3314 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003315 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003316 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003317
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003318 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003319 {
3320 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003321 {
3322 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003323 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003324 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003325 {
3326 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003327 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003328 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003329 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003330 else
3331 {
3332 msg_sb_eol();
3333 if (iptr->isn_type == ISN_ECHOMSG)
3334 {
3335 msg_attr(ga.ga_data, echo_attr);
3336 out_flush();
3337 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003338#ifdef HAS_MESSAGE_WINDOW
3339 else if (iptr->isn_type == ISN_ECHOWINDOW)
3340 {
3341 start_echowindow();
3342 msg_attr(ga.ga_data, echo_attr);
3343 end_echowindow();
3344 }
3345#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003346 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3347 {
3348 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3349 TRUE);
3350 ui_write((char_u *)"\r\n", 2, TRUE);
3351 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003352 else
3353 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003354 SOURCING_LNUM = iptr->isn_lnum;
3355 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003356 }
3357 }
3358 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003359 ga_clear(&ga);
3360 }
3361 break;
3362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 // load local variable or argument
3364 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003365 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003366 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003368 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 break;
3370
3371 // load v: variable
3372 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003373 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003374 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003376 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 break;
3378
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003379 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 case ISN_LOADSCRIPT:
3381 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003382 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 svar_T *sv;
3384
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003385 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003386 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003387 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003388 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003389 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003390 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003392 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 }
3394 break;
3395
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003396 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003398 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003400 int sid = iptr->isn_arg.loadstore.ls_sid;
3401 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003402 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 if (di == NULL)
3406 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003407 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003408 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003409 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 }
3411 else
3412 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003413 if (iptr->isn_type == ISN_LOADEXPORT)
3414 {
3415 int idx = get_script_item_idx(sid, name, 0,
3416 NULL, NULL);
3417 svar_T *sv;
3418
3419 if (idx >= 0)
3420 {
3421 sv = ((svar_T *)SCRIPT_ITEM(sid)
3422 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003423 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003424 {
3425 SOURCING_LNUM = iptr->isn_lnum;
3426 semsg(_(e_item_not_exported_in_script_str),
3427 name);
3428 goto on_error;
3429 }
3430 }
3431 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003432 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003433 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003435 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 }
3437 }
3438 break;
3439
Bram Moolenaard3aac292020-04-19 14:32:17 +02003440 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003442 case ISN_LOADB:
3443 case ISN_LOADW:
3444 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003446 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003447
Bram Moolenaar06b77222022-01-25 15:51:56 +00003448 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003449 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003450 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003451 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 break;
3455
Bram Moolenaar03290b82020-12-19 16:30:44 +01003456 // load autoload variable
3457 case ISN_LOADAUTO:
3458 {
3459 char_u *name = iptr->isn_arg.string;
3460
Bram Moolenaar35578162021-08-02 19:10:38 +02003461 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003462 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003463 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003464 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003465 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003466 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003467 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003468 }
3469 break;
3470
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003471 // load g:/b:/w:/t: namespace
3472 case ISN_LOADGDICT:
3473 case ISN_LOADBDICT:
3474 case ISN_LOADWDICT:
3475 case ISN_LOADTDICT:
3476 {
3477 dict_T *d = NULL;
3478
3479 switch (iptr->isn_type)
3480 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003481 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3482 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3483 case ISN_LOADWDICT: d = curwin->w_vars; break;
3484 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003485 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003486 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003487 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003488 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003489 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003490 tv = STACK_TV_BOT(0);
3491 tv->v_type = VAR_DICT;
3492 tv->v_lock = 0;
3493 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003494 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003495 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003496 }
3497 break;
3498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 // load &option
3500 case ISN_LOADOPT:
3501 {
3502 typval_T optval;
3503 char_u *name = iptr->isn_arg.string;
3504
Bram Moolenaara8c17702020-04-01 21:17:24 +02003505 // This is not expected to fail, name is checked during
3506 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003507 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003508 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003509 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003510 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003512 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 }
3514 break;
3515
3516 // load $ENV
3517 case ISN_LOADENV:
3518 {
3519 typval_T optval;
3520 char_u *name = iptr->isn_arg.string;
3521
Bram Moolenaar35578162021-08-02 19:10:38 +02003522 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003523 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003524 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003525 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003527 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 }
3529 break;
3530
3531 // load @register
3532 case ISN_LOADREG:
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 Moolenaar8a7d6542020-01-26 15:56:19 +01003535 tv = STACK_TV_BOT(0);
3536 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003537 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003538 // This may result in NULL, which should be equivalent to an
3539 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 tv->vval.v_string = get_reg_contents(
3541 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003542 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 break;
3544
3545 // store local variable
3546 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003547 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 tv = STACK_TV_VAR(iptr->isn_arg.number);
3549 clear_tv(tv);
3550 *tv = *STACK_TV_BOT(0);
3551 break;
3552
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003553 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003554 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003555 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003556 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003557 int sid = iptr->isn_arg.loadstore.ls_sid;
3558 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003559 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003560 dictitem_T *di = find_var_in_ht(ht, 0,
3561 iptr->isn_type == ISN_STORES
3562 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003563
Bram Moolenaar4c137212021-04-19 16:48:48 +02003564 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003565 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003566 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003567 {
3568 if (iptr->isn_type == ISN_STOREEXPORT)
3569 {
3570 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003571 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003572 goto on_error;
3573 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003574 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003575 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003576 else
3577 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003578 if (iptr->isn_type == ISN_STOREEXPORT)
3579 {
3580 int idx = get_script_item_idx(sid, name, 0,
3581 NULL, NULL);
3582
Bram Moolenaar06651632022-04-27 17:54:25 +01003583 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003584 if (idx >= 0)
3585 {
3586 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3587 ->sn_var_vals.ga_data) + idx;
3588
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003589 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003590 {
3591 semsg(_(e_item_not_exported_in_script_str),
3592 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003593 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003594 goto on_error;
3595 }
3596 }
3597 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003598 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003599 {
3600 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003601 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003602 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003603 clear_tv(&di->di_tv);
3604 di->di_tv = *STACK_TV_BOT(0);
3605 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003606 }
3607 break;
3608
3609 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 case ISN_STORESCRIPT:
3611 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003612 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3613 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003615 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003616 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003617 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003618 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003619
3620 // "const" and "final" are checked at compile time, locking
3621 // the value needs to be checked here.
3622 SOURCING_LNUM = iptr->isn_lnum;
3623 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003624 {
3625 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003626 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003627 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 clear_tv(sv->sv_tv);
3630 *sv->sv_tv = *STACK_TV_BOT(0);
3631 }
3632 break;
3633
3634 // store option
3635 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003636 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003638 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3639 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 long n = 0;
3641 char_u *s = NULL;
3642 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003643 char_u numbuf[NUMBUFLEN];
3644 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645
Bram Moolenaar4c137212021-04-19 16:48:48 +02003646 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 tv = STACK_TV_BOT(0);
3648 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003649 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003651 if (s == NULL)
3652 s = (char_u *)"";
3653 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003654 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3655 {
3656 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003657 // If the option can be set to a function reference or
3658 // a lambda and the passed value is a function
3659 // reference, then convert it to the name (string) of
3660 // the function reference.
3661 s = tv2string(tv, &tofree, numbuf, 0);
3662 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003663 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003664 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003665 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003666 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003667 goto on_error;
3668 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003669 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003671 // must be VAR_NUMBER, CHECKTYPE makes sure
3672 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003673 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003674 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003675 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 if (msg != NULL)
3677 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003678 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003680 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 }
3683 break;
3684
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003685 // store $ENV
3686 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003687 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003688 tv = STACK_TV_BOT(0);
3689 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3690 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003691 break;
3692
3693 // store @r
3694 case ISN_STOREREG:
3695 {
3696 int reg = iptr->isn_arg.number;
3697
Bram Moolenaar4c137212021-04-19 16:48:48 +02003698 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003699 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003700 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003701 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003702 }
3703 break;
3704
3705 // store v: variable
3706 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003707 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003708 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3709 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003710 // should not happen, type is checked when compiling
3711 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003712 break;
3713
Bram Moolenaard3aac292020-04-19 14:32:17 +02003714 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003716 case ISN_STOREB:
3717 case ISN_STOREW:
3718 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003720 dictitem_T *di;
3721 hashtab_T *ht;
3722 char_u *name = iptr->isn_arg.string + 2;
3723
Bram Moolenaard3aac292020-04-19 14:32:17 +02003724 switch (iptr->isn_type)
3725 {
3726 case ISN_STOREG:
3727 ht = get_globvar_ht();
3728 break;
3729 case ISN_STOREB:
3730 ht = &curbuf->b_vars->dv_hashtab;
3731 break;
3732 case ISN_STOREW:
3733 ht = &curwin->w_vars->dv_hashtab;
3734 break;
3735 case ISN_STORET:
3736 ht = &curtab->tp_vars->dv_hashtab;
3737 break;
3738 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003739 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003740 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741
Bram Moolenaar4c137212021-04-19 16:48:48 +02003742 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003743 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003745 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 else
3747 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003748 SOURCING_LNUM = iptr->isn_lnum;
3749 if (var_check_permission(di, name) == FAIL)
3750 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 clear_tv(&di->di_tv);
3752 di->di_tv = *STACK_TV_BOT(0);
3753 }
3754 }
3755 break;
3756
Bram Moolenaar03290b82020-12-19 16:30:44 +01003757 // store an autoload variable
3758 case ISN_STOREAUTO:
3759 SOURCING_LNUM = iptr->isn_lnum;
3760 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3761 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003762 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003763 break;
3764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 // store number in local variable
3766 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003767 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 clear_tv(tv);
3769 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003770 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 break;
3772
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003773 // store value in list or dict variable
3774 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003775 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003776 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003777
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003778 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003779 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003780 if (res == NOTDONE)
3781 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003782 }
3783 break;
3784
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003785 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003786 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003787 if (execute_storerange(iptr, ectx) == FAIL)
3788 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003789 break;
3790
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003791 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003792 case ISN_LOADOUTER:
3793 case ISN_STOREOUTER:
3794 {
3795 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003796 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3797 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003798
3799 while (depth > 1 && outer != NULL)
3800 {
3801 outer = outer->out_up;
3802 --depth;
3803 }
3804 if (outer == NULL)
3805 {
3806 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003807 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3808 || ectx->ec_outer_ref == NULL)
3809 // Possibly :def function called from legacy
3810 // context.
3811 emsg(_(e_closure_called_from_invalid_context));
3812 else
3813 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003814 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003815 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003816 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003817 // Variable declared in loop. May be copied if the
3818 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01003819 tv = ((typval_T *)outer->out_loop[-depth - 1]
3820 .stack->ga_data)
3821 + outer->out_loop[-depth - 1].var_idx
3822 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003823 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003824 // Variable declared in a function. May be copied if
3825 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003826 tv = ((typval_T *)outer->out_stack->ga_data)
3827 + outer->out_frame_idx + STACK_FRAME_SIZE
3828 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003829 if (iptr->isn_type == ISN_LOADOUTER)
3830 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003831 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003832 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003833 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003834 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003835 }
3836 else
3837 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003838 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003839 clear_tv(tv);
3840 *tv = *STACK_TV_BOT(0);
3841 }
3842 }
3843 break;
3844
Bram Moolenaar752fc692021-01-04 21:57:11 +01003845 // unlet item in list or dict variable
3846 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003847 if (execute_unletindex(iptr, ectx) == FAIL)
3848 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003849 break;
3850
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003851 // unlet range of items in list variable
3852 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003853 if (execute_unletrange(iptr, ectx) == FAIL)
3854 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003855 break;
3856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 // push constant
3858 case ISN_PUSHNR:
3859 case ISN_PUSHBOOL:
3860 case ISN_PUSHSPEC:
3861 case ISN_PUSHF:
3862 case ISN_PUSHS:
3863 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003864 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003865 case ISN_PUSHCHANNEL:
3866 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003867 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003868 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003870 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003871 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 switch (iptr->isn_type)
3873 {
3874 case ISN_PUSHNR:
3875 tv->v_type = VAR_NUMBER;
3876 tv->vval.v_number = iptr->isn_arg.number;
3877 break;
3878 case ISN_PUSHBOOL:
3879 tv->v_type = VAR_BOOL;
3880 tv->vval.v_number = iptr->isn_arg.number;
3881 break;
3882 case ISN_PUSHSPEC:
3883 tv->v_type = VAR_SPECIAL;
3884 tv->vval.v_number = iptr->isn_arg.number;
3885 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 case ISN_PUSHF:
3887 tv->v_type = VAR_FLOAT;
3888 tv->vval.v_float = iptr->isn_arg.fnumber;
3889 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 case ISN_PUSHBLOB:
3891 blob_copy(iptr->isn_arg.blob, tv);
3892 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003893 case ISN_PUSHFUNC:
3894 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003895 if (iptr->isn_arg.string == NULL)
3896 tv->vval.v_string = NULL;
3897 else
3898 tv->vval.v_string =
3899 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003900 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003901 case ISN_PUSHCHANNEL:
3902#ifdef FEAT_JOB_CHANNEL
3903 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003904 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003905#endif
3906 break;
3907 case ISN_PUSHJOB:
3908#ifdef FEAT_JOB_CHANNEL
3909 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003910 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003911#endif
3912 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 default:
3914 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003915 tv->vval.v_string = iptr->isn_arg.string == NULL
3916 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 }
3918 break;
3919
Bram Moolenaar06b77222022-01-25 15:51:56 +00003920 case ISN_AUTOLOAD:
3921 {
3922 char_u *name = iptr->isn_arg.string;
3923
3924 (void)script_autoload(name, FALSE);
3925 if (find_func(name, TRUE))
3926 {
3927 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3928 goto theend;
3929 tv = STACK_TV_BOT(0);
3930 tv->v_lock = 0;
3931 ++ectx->ec_stack.ga_len;
3932 tv->v_type = VAR_FUNC;
3933 tv->vval.v_string = vim_strsave(name);
3934 }
3935 else
3936 {
3937 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3938
3939 if (res == NOTDONE)
3940 goto theend;
3941 if (res == FAIL)
3942 goto on_error;
3943 }
3944 }
3945 break;
3946
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003947 case ISN_UNLET:
3948 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3949 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003950 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003951 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003952 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003953 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003954 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003955
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003956 case ISN_LOCKUNLOCK:
3957 {
3958 typval_T *lval_root_save = lval_root;
3959 int res;
3960
3961 // Stack has the local variable, argument the whole :lock
3962 // or :unlock command, like ISN_EXEC.
3963 --ectx->ec_stack.ga_len;
3964 lval_root = STACK_TV_BOT(0);
3965 res = exec_command(iptr);
3966 clear_tv(lval_root);
3967 lval_root = lval_root_save;
3968 if (res == FAIL)
3969 goto on_error;
3970 }
3971 break;
3972
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003973 case ISN_LOCKCONST:
3974 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3975 break;
3976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 // create a list from items on the stack; uses a single allocation
3978 // for the list header and the items
3979 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003980 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003981 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 break;
3983
3984 // create a dict from items on the stack
3985 case ISN_NEWDICT:
3986 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003987 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003989 SOURCING_LNUM = iptr->isn_lnum;
3990 res = exe_newdict(iptr->isn_arg.number, ectx);
3991 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003992 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003993 if (res == MAYBE)
3994 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 }
3996 break;
3997
LemonBoy372bcce2022-04-25 12:43:20 +01003998 case ISN_CONCAT:
3999 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4000 goto theend;
4001 break;
4002
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004003 // create a partial with NULL value
4004 case ISN_NEWPARTIAL:
4005 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4006 goto theend;
4007 ++ectx->ec_stack.ga_len;
4008 tv = STACK_TV_BOT(-1);
4009 tv->v_type = VAR_PARTIAL;
4010 tv->v_lock = 0;
4011 tv->vval.v_partial = NULL;
4012 break;
4013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 // call a :def function
4015 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004016 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004017 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4018 NULL,
4019 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004020 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004021 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 break;
4023
4024 // call a builtin function
4025 case ISN_BCALL:
4026 SOURCING_LNUM = iptr->isn_lnum;
4027 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4028 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004029 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004030 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 break;
4032
4033 // call a funcref or partial
4034 case ISN_PCALL:
4035 {
4036 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4037 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004038 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039
4040 SOURCING_LNUM = iptr->isn_lnum;
4041 if (pfunc->cpf_top)
4042 {
4043 // funcref is above the arguments
4044 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4045 }
4046 else
4047 {
4048 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004049 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004050 partial_tv = *STACK_TV_BOT(0);
4051 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004053 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004054 if (tv == &partial_tv)
4055 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004057 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 }
4059 break;
4060
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004061 case ISN_PCALL_END:
4062 // PCALL finished, arguments have been consumed and replaced by
4063 // the return value. Now clear the funcref from the stack,
4064 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004065 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004066 clear_tv(STACK_TV_BOT(-1));
4067 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4068 break;
4069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 // call a user defined function or funcref/partial
4071 case ISN_UCALL:
4072 {
4073 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4074
4075 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004076 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004077 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004078 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 }
4080 break;
4081
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004082 // :defer func(arg)
4083 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004084 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004085 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4086 goto on_error;
4087 break;
4088
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004089 // return from a :def function call without a value
4090 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02004091 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004092 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004093 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004094 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004095 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004096 tv->vval.v_number = 0;
4097 tv->v_lock = 0;
4098 // FALLTHROUGH
4099
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004100 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 case ISN_RETURN:
4102 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004103 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004104 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004105
4106 if (trystack->ga_len > 0)
4107 trycmd = ((trycmd_T *)trystack->ga_data)
4108 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004109 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004110 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004112 // jump to ":finally" or ":endtry"
4113 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004114 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004115 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004116 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 trycmd->tcd_return = TRUE;
4118 }
4119 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004120 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 }
4122 break;
4123
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004124 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 case ISN_FUNCREF:
4126 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004127 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4128 ufunc_T *ufunc;
4129 funcref_T *funcref = &iptr->isn_arg.funcref;
4130 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004133 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004134 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004135 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004136 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004137 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004138 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004139 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004140 {
4141 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4142 + funcref->fr_dfunc_idx;
4143
4144 ufunc = pt_dfunc->df_ufunc;
4145 }
4146 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004147 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004148 if (ufunc == NULL)
4149 {
4150 SOURCING_LNUM = iptr->isn_lnum;
4151 iemsg("ufunc unexpectedly NULL for FUNCREF");
4152 goto theend;
4153 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004154 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004155 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004156 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004157 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004159 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 tv->vval.v_partial = pt;
4161 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004162 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 }
4164 break;
4165
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004166 // Create a global function from a lambda.
4167 case ISN_NEWFUNC:
4168 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004169 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004170
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004171 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004172 arg->nfa_global, &arg->nfa_loopvar_info,
4173 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004174 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004175 }
4176 break;
4177
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004178 // List functions
4179 case ISN_DEF:
4180 if (iptr->isn_arg.string == NULL)
4181 list_functions(NULL);
4182 else
4183 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004184 exarg_T ea;
4185 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004186
4187 CLEAR_FIELD(ea);
4188 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004189 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4190 define_function(&ea, NULL, &lines_to_free);
4191 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004192 }
4193 break;
4194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 // jump if a condition is met
4196 case ISN_JUMP:
4197 {
4198 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004199 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 int jump = TRUE;
4201
4202 if (when != JUMP_ALWAYS)
4203 {
4204 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004205 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004206 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004207 || when == JUMP_IF_COND_TRUE)
4208 {
4209 SOURCING_LNUM = iptr->isn_lnum;
4210 jump = tv_get_bool_chk(tv, &error);
4211 if (error)
4212 goto on_error;
4213 }
4214 else
4215 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004216 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004218 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 {
4220 // drop the value from the stack
4221 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004222 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 }
4224 }
4225 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004226 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 }
4228 break;
4229
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004230 // "while": jump to end if a condition is false
4231 case ISN_WHILE:
4232 {
4233 int error = FALSE;
4234 int jump = TRUE;
4235
4236 tv = STACK_TV_BOT(-1);
4237 SOURCING_LNUM = iptr->isn_lnum;
4238 jump = !tv_get_bool_chk(tv, &error);
4239 if (error)
4240 goto on_error;
4241 // drop the value from the stack
4242 clear_tv(tv);
4243 --ectx->ec_stack.ga_len;
4244 if (jump)
4245 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4246
4247 // Store the current funccal count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004248 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004249 tv = STACK_TV_VAR(
4250 iptr->isn_arg.whileloop.while_funcref_idx);
4251 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4252 }
4253 break;
4254
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004255 // Jump if an argument with a default value was already set and not
4256 // v:none.
4257 case ISN_JUMP_IF_ARG_SET:
4258 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
4259 if (tv->v_type != VAR_UNKNOWN
4260 && !(tv->v_type == VAR_SPECIAL
4261 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004262 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004263 break;
4264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 // top of a for loop
4266 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004267 if (execute_for(iptr, ectx) == FAIL)
4268 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 break;
4270
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004271 // end of a for or while loop
4272 case ISN_ENDLOOP:
4273 if (execute_endloop(iptr, ectx) == FAIL)
4274 goto theend;
4275 break;
4276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 // start of ":try" block
4278 case ISN_TRY:
4279 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004280 trycmd_T *trycmd = NULL;
4281
Bram Moolenaar35578162021-08-02 19:10:38 +02004282 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004283 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004284 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4285 + ectx->ec_trystack.ga_len;
4286 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004288 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004289 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4290 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004291 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004292 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004293 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004294 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004295 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004296 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 }
4298 break;
4299
4300 case ISN_PUSHEXC:
4301 if (current_exception == NULL)
4302 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004303 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004305 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004307 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004308 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004310 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004312 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 tv->vval.v_string = vim_strsave(
4314 (char_u *)current_exception->value);
4315 break;
4316
4317 case ISN_CATCH:
4318 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004319 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004320 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321
Bram Moolenaar4c137212021-04-19 16:48:48 +02004322 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004323 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004325 trycmd->tcd_caught = TRUE;
4326 trycmd->tcd_did_throw = FALSE;
4327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004329 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 catch_exception(current_exception);
4331 }
4332 break;
4333
Bram Moolenaarc150c092021-02-13 15:02:46 +01004334 case ISN_TRYCONT:
4335 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004336 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004337 trycont_T *trycont = &iptr->isn_arg.trycont;
4338 int i;
4339 trycmd_T *trycmd;
4340 int iidx = trycont->tct_where;
4341
4342 if (trystack->ga_len < trycont->tct_levels)
4343 {
4344 siemsg("TRYCONT: expected %d levels, found %d",
4345 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004346 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004347 }
4348 // Make :endtry jump to any outer try block and the last
4349 // :endtry inside the loop to the loop start.
4350 for (i = trycont->tct_levels; i > 0; --i)
4351 {
4352 trycmd = ((trycmd_T *)trystack->ga_data)
4353 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004354 // Add one to tcd_cont to be able to jump to
4355 // instruction with index zero.
4356 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004357 iidx = trycmd->tcd_finally_idx == 0
4358 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004359 }
4360 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004361 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004362 }
4363 break;
4364
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004365 case ISN_FINALLY:
4366 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004367 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004368 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4369 + trystack->ga_len - 1;
4370
4371 // Reset the index to avoid a return statement jumps here
4372 // again.
4373 trycmd->tcd_finally_idx = 0;
4374 break;
4375 }
4376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 // end of ":try" block
4378 case ISN_ENDTRY:
4379 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004380 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004381 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382
Bram Moolenaar06651632022-04-27 17:54:25 +01004383 --trystack->ga_len;
4384 --trylevel;
4385 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4386 if (trycmd->tcd_did_throw)
4387 did_throw = TRUE;
4388 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004390 // discard the exception
4391 if (caught_stack == current_exception)
4392 caught_stack = caught_stack->caught;
4393 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004395
4396 if (trycmd->tcd_return)
4397 goto func_return;
4398
4399 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4400 {
4401 --ectx->ec_stack.ga_len;
4402 clear_tv(STACK_TV_BOT(0));
4403 }
4404 if (trycmd->tcd_cont != 0)
4405 // handling :continue: jump to outer try block or
4406 // start of the loop
4407 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 }
4409 break;
4410
4411 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004412 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004413 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004414
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004415 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4416 {
4417 // throwing an exception while using "silent!" causes
4418 // the function to abort but not display an error.
4419 tv = STACK_TV_BOT(-1);
4420 clear_tv(tv);
4421 tv->v_type = VAR_NUMBER;
4422 tv->vval.v_number = 0;
4423 goto done;
4424 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004425 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004426 tv = STACK_TV_BOT(0);
4427 if (tv->vval.v_string == NULL
4428 || *skipwhite(tv->vval.v_string) == NUL)
4429 {
4430 vim_free(tv->vval.v_string);
4431 SOURCING_LNUM = iptr->isn_lnum;
4432 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004433 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004434 }
4435
4436 // Inside a "catch" we need to first discard the caught
4437 // exception.
4438 if (trystack->ga_len > 0)
4439 {
4440 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4441 + trystack->ga_len - 1;
4442 if (trycmd->tcd_caught && current_exception != NULL)
4443 {
4444 // discard the exception
4445 if (caught_stack == current_exception)
4446 caught_stack = caught_stack->caught;
4447 discard_current_exception();
4448 trycmd->tcd_caught = FALSE;
4449 }
4450 }
4451
Bram Moolenaar90a57162022-02-12 14:23:17 +00004452 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004453 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4454 == FAIL)
4455 {
4456 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004457 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004458 }
4459 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 break;
4462
4463 // compare with special values
4464 case ISN_COMPAREBOOL:
4465 case ISN_COMPARESPECIAL:
4466 {
4467 typval_T *tv1 = STACK_TV_BOT(-2);
4468 typval_T *tv2 = STACK_TV_BOT(-1);
4469 varnumber_T arg1 = tv1->vval.v_number;
4470 varnumber_T arg2 = tv2->vval.v_number;
4471 int res;
4472
Bram Moolenaar06651632022-04-27 17:54:25 +01004473 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4474 res = arg1 == arg2;
4475 else
4476 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477
Bram Moolenaar4c137212021-04-19 16:48:48 +02004478 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 tv1->v_type = VAR_BOOL;
4480 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4481 }
4482 break;
4483
Bram Moolenaar7a222242022-03-01 19:23:24 +00004484 case ISN_COMPARENULL:
4485 {
4486 typval_T *tv1 = STACK_TV_BOT(-2);
4487 typval_T *tv2 = STACK_TV_BOT(-1);
4488 int res;
4489
4490 res = typval_compare_null(tv1, tv2);
4491 if (res == MAYBE)
4492 goto on_error;
4493 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4494 res = !res;
4495 clear_tv(tv1);
4496 clear_tv(tv2);
4497 --ectx->ec_stack.ga_len;
4498 tv1->v_type = VAR_BOOL;
4499 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4500 }
4501 break;
4502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 // Operation with two number arguments
4504 case ISN_OPNR:
4505 case ISN_COMPARENR:
4506 {
4507 typval_T *tv1 = STACK_TV_BOT(-2);
4508 typval_T *tv2 = STACK_TV_BOT(-1);
4509 varnumber_T arg1 = tv1->vval.v_number;
4510 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004511 varnumber_T res = 0;
4512 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004514 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4515 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4516 {
4517 if (arg2 < 0)
4518 {
4519 SOURCING_LNUM = iptr->isn_lnum;
4520 emsg(_(e_bitshift_ops_must_be_postive));
4521 goto on_error;
4522 }
4523 }
4524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 switch (iptr->isn_arg.op.op_type)
4526 {
4527 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004528 case EXPR_DIV: if (arg2 == 0)
4529 div_zero = TRUE;
4530 else
4531 res = arg1 / arg2;
4532 break;
4533 case EXPR_REM: if (arg2 == 0)
4534 div_zero = TRUE;
4535 else
4536 res = arg1 % arg2;
4537 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 case EXPR_SUB: res = arg1 - arg2; break;
4539 case EXPR_ADD: res = arg1 + arg2; break;
4540
4541 case EXPR_EQUAL: res = arg1 == arg2; break;
4542 case EXPR_NEQUAL: res = arg1 != arg2; break;
4543 case EXPR_GREATER: res = arg1 > arg2; break;
4544 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4545 case EXPR_SMALLER: res = arg1 < arg2; break;
4546 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004547 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4548 res = 0;
4549 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004550 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004551 break;
4552 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4553 res = 0;
4554 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004555 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004556 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004557 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 }
4559
Bram Moolenaar4c137212021-04-19 16:48:48 +02004560 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 if (iptr->isn_type == ISN_COMPARENR)
4562 {
4563 tv1->v_type = VAR_BOOL;
4564 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4565 }
4566 else
4567 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004568 if (div_zero)
4569 {
4570 SOURCING_LNUM = iptr->isn_lnum;
4571 emsg(_(e_divide_by_zero));
4572 goto on_error;
4573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 }
4575 break;
4576
4577 // Computation with two float arguments
4578 case ISN_OPFLOAT:
4579 case ISN_COMPAREFLOAT:
4580 {
4581 typval_T *tv1 = STACK_TV_BOT(-2);
4582 typval_T *tv2 = STACK_TV_BOT(-1);
4583 float_T arg1 = tv1->vval.v_float;
4584 float_T arg2 = tv2->vval.v_float;
4585 float_T res = 0;
4586 int cmp = FALSE;
4587
4588 switch (iptr->isn_arg.op.op_type)
4589 {
4590 case EXPR_MULT: res = arg1 * arg2; break;
4591 case EXPR_DIV: res = arg1 / arg2; break;
4592 case EXPR_SUB: res = arg1 - arg2; break;
4593 case EXPR_ADD: res = arg1 + arg2; break;
4594
4595 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4596 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4597 case EXPR_GREATER: cmp = arg1 > arg2; break;
4598 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4599 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4600 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4601 default: cmp = 0; break;
4602 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004603 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 if (iptr->isn_type == ISN_COMPAREFLOAT)
4605 {
4606 tv1->v_type = VAR_BOOL;
4607 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4608 }
4609 else
4610 tv1->vval.v_float = res;
4611 }
4612 break;
4613
4614 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004615 case ISN_COMPAREDICT:
4616 case ISN_COMPAREFUNC:
4617 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 case ISN_COMPAREBLOB:
4619 {
4620 typval_T *tv1 = STACK_TV_BOT(-2);
4621 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004622 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4623 int ic = iptr->isn_arg.op.op_ic;
4624 int res = FALSE;
4625 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626
Bram Moolenaar265f8112021-12-19 12:33:05 +00004627 SOURCING_LNUM = iptr->isn_lnum;
4628 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004630 status = typval_compare_list(tv1, tv2,
4631 exprtype, ic, &res);
4632 }
4633 else if (iptr->isn_type == ISN_COMPAREDICT)
4634 {
4635 status = typval_compare_dict(tv1, tv2,
4636 exprtype, ic, &res);
4637 }
4638 else if (iptr->isn_type == ISN_COMPAREFUNC)
4639 {
4640 status = typval_compare_func(tv1, tv2,
4641 exprtype, ic, &res);
4642 }
4643 else if (iptr->isn_type == ISN_COMPARESTRING)
4644 {
4645 status = typval_compare_string(tv1, tv2,
4646 exprtype, ic, &res);
4647 }
4648 else
4649 {
4650 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004652 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 clear_tv(tv1);
4654 clear_tv(tv2);
4655 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004656 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4657 if (status == FAIL)
4658 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 }
4660 break;
4661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 case ISN_COMPAREANY:
4663 {
4664 typval_T *tv1 = STACK_TV_BOT(-2);
4665 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004666 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004668 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669
Bram Moolenaareb26f432020-09-14 16:50:05 +02004670 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004671 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004673 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004674 if (status == FAIL)
4675 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 }
4677 break;
4678
4679 case ISN_ADDLIST:
4680 case ISN_ADDBLOB:
4681 {
4682 typval_T *tv1 = STACK_TV_BOT(-2);
4683 typval_T *tv2 = STACK_TV_BOT(-1);
4684
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004685 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004687 {
4688 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4689 && tv1->vval.v_list != NULL)
4690 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4691 NULL);
4692 else
4693 eval_addlist(tv1, tv2);
4694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 else
4696 eval_addblob(tv1, tv2);
4697 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004698 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 }
4700 break;
4701
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004702 case ISN_LISTAPPEND:
4703 {
4704 typval_T *tv1 = STACK_TV_BOT(-2);
4705 typval_T *tv2 = STACK_TV_BOT(-1);
4706 list_T *l = tv1->vval.v_list;
4707
4708 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004709 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004710 if (l == NULL)
4711 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004712 emsg(_(e_cannot_add_to_null_list));
4713 goto on_error;
4714 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004715 if (value_check_lock(l->lv_lock, NULL, FALSE))
4716 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004717 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004718 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004719 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004720 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004721 }
4722 break;
4723
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004724 case ISN_BLOBAPPEND:
4725 {
4726 typval_T *tv1 = STACK_TV_BOT(-2);
4727 typval_T *tv2 = STACK_TV_BOT(-1);
4728 blob_T *b = tv1->vval.v_blob;
4729 int error = FALSE;
4730 varnumber_T n;
4731
4732 // add a number to a blob
4733 if (b == NULL)
4734 {
4735 SOURCING_LNUM = iptr->isn_lnum;
4736 emsg(_(e_cannot_add_to_null_blob));
4737 goto on_error;
4738 }
4739 n = tv_get_number_chk(tv2, &error);
4740 if (error)
4741 goto on_error;
4742 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004743 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004744 }
4745 break;
4746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 // Computation with two arguments of unknown type
4748 case ISN_OPANY:
4749 {
4750 typval_T *tv1 = STACK_TV_BOT(-2);
4751 typval_T *tv2 = STACK_TV_BOT(-1);
4752 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754 int error = FALSE;
4755
4756 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4757 {
4758 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4759 {
4760 eval_addlist(tv1, tv2);
4761 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004762 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 break;
4764 }
4765 else if (tv1->v_type == VAR_BLOB
4766 && tv2->v_type == VAR_BLOB)
4767 {
4768 eval_addblob(tv1, tv2);
4769 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004770 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 break;
4772 }
4773 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774 if (tv1->v_type == VAR_FLOAT)
4775 {
4776 f1 = tv1->vval.v_float;
4777 n1 = 0;
4778 }
4779 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004781 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782 n1 = tv_get_number_chk(tv1, &error);
4783 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004784 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 if (tv2->v_type == VAR_FLOAT)
4786 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 if (tv2->v_type == VAR_FLOAT)
4789 {
4790 f2 = tv2->vval.v_float;
4791 n2 = 0;
4792 }
4793 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 {
4795 n2 = tv_get_number_chk(tv2, &error);
4796 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004797 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 if (tv1->v_type == VAR_FLOAT)
4799 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 // if there is a float on either side the result is a float
4802 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4803 {
4804 switch (iptr->isn_arg.op.op_type)
4805 {
4806 case EXPR_MULT: f1 = f1 * f2; break;
4807 case EXPR_DIV: f1 = f1 / f2; break;
4808 case EXPR_SUB: f1 = f1 - f2; break;
4809 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004810 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004811 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004812 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004813 }
4814 clear_tv(tv1);
4815 clear_tv(tv2);
4816 tv1->v_type = VAR_FLOAT;
4817 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004818 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819 }
4820 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004822 int failed = FALSE;
4823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 switch (iptr->isn_arg.op.op_type)
4825 {
4826 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004827 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4828 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004829 goto on_error;
4830 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831 case EXPR_SUB: n1 = n1 - n2; break;
4832 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004833 default: n1 = num_modulus(n1, n2, &failed);
4834 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004835 goto on_error;
4836 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 }
4838 clear_tv(tv1);
4839 clear_tv(tv2);
4840 tv1->v_type = VAR_NUMBER;
4841 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004842 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 }
4844 }
4845 break;
4846
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004847 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004848 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004849 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004850 int is_slice = iptr->isn_type == ISN_STRSLICE;
4851 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004852 char_u *res;
4853
4854 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004855 // string slice: string is at stack-3, first index at
4856 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004857 if (is_slice)
4858 {
4859 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004860 n1 = tv->vval.v_number;
4861 }
4862
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004863 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004864 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004865
Bram Moolenaar4c137212021-04-19 16:48:48 +02004866 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004867 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004868 if (is_slice)
4869 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004870 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004871 else
4872 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004873 // single character (including composing characters).
4874 // If the index is too big or negative the result is
4875 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004876 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004877 vim_free(tv->vval.v_string);
4878 tv->vval.v_string = res;
4879 }
4880 break;
4881
4882 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004883 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004884 case ISN_BLOBINDEX:
4885 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004887 int is_slice = iptr->isn_type == ISN_LISTSLICE
4888 || iptr->isn_type == ISN_BLOBSLICE;
4889 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4890 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004891 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004892 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893
4894 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004895 // list slice: list is at stack-3, indexes at stack-2 and
4896 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004897 // Same for blob.
4898 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899
4900 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004901 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004903
4904 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 {
Bram Moolenaared591872020-08-15 22:14:53 +02004906 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004907 n1 = tv->vval.v_number;
4908 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 }
Bram Moolenaared591872020-08-15 22:14:53 +02004910
Bram Moolenaar4c137212021-04-19 16:48:48 +02004911 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004912 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004913 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004914 if (is_blob)
4915 {
4916 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4917 n1, n2, FALSE, tv) == FAIL)
4918 goto on_error;
4919 }
4920 else
4921 {
4922 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4923 n1, n2, FALSE, tv, TRUE) == FAIL)
4924 goto on_error;
4925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 }
4927 break;
4928
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004929 case ISN_ANYINDEX:
4930 case ISN_ANYSLICE:
4931 {
4932 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4933 typval_T *var1, *var2;
4934 int res;
4935
4936 // index: composite is at stack-2, index at stack-1
4937 // slice: composite is at stack-3, indexes at stack-2 and
4938 // stack-1
4939 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004940 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004941 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4942 goto on_error;
4943 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4944 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004945 res = eval_index_inner(tv, is_slice, var1, var2,
4946 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004947 clear_tv(var1);
4948 if (is_slice)
4949 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004950 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004951 if (res == FAIL)
4952 goto on_error;
4953 }
4954 break;
4955
Bram Moolenaar9af78762020-06-16 11:34:42 +02004956 case ISN_SLICE:
4957 {
4958 list_T *list;
4959 int count = iptr->isn_arg.number;
4960
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004961 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004962 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004963 list = tv->vval.v_list;
4964
4965 // no error for short list, expect it to be checked earlier
4966 if (list != NULL && list->lv_len >= count)
4967 {
4968 list_T *newlist = list_slice(list,
4969 count, list->lv_len - 1);
4970
4971 if (newlist != NULL)
4972 {
4973 list_unref(list);
4974 tv->vval.v_list = newlist;
4975 ++newlist->lv_refcount;
4976 }
4977 }
4978 }
4979 break;
4980
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004981 case ISN_GETITEM:
4982 {
4983 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004984 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004985
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004986 // Get list item: list is at stack-1, push item.
4987 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004988 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4989 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004990
Bram Moolenaar35578162021-08-02 19:10:38 +02004991 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004992 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004993 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004994 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004995
4996 // Useful when used in unpack assignment. Reset at
4997 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004998 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004999 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005000 }
5001 break;
5002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 case ISN_MEMBER:
5004 {
5005 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005006 char_u *key;
5007 dictitem_T *di;
5008
5009 // dict member: dict is at stack-2, key at stack-1
5010 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005011 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005012 dict = tv->vval.v_dict;
5013
5014 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005015 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005016 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005017 if (key == NULL)
5018 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005019
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005020 if ((di = dict_find(dict, key, -1)) == NULL)
5021 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005022 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005023 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005024
5025 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005026 // stack contents makes sense and the dict stack is
5027 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005028 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005029 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005030 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005031 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005032 tv->v_type = VAR_NUMBER;
5033 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005034 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005035 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005036 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005037 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005038 // Put the dict used on the dict stack, it might be used by
5039 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005040 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005041 if (dict_stack_save(tv) == FAIL)
5042 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005043 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005044 }
5045 break;
5046
5047 // dict member with string key
5048 case ISN_STRINGMEMBER:
5049 {
5050 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 dictitem_T *di;
5052
5053 tv = STACK_TV_BOT(-1);
5054 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5055 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005056 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005057 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005058 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 }
5060 dict = tv->vval.v_dict;
5061
5062 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5063 == NULL)
5064 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005065 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00005066 semsg(_(e_key_not_present_in_dictionary),
5067 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005068 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005070 // Put the dict used on the dict stack, it might be used by
5071 // a dict function later.
5072 if (dict_stack_save(tv) == FAIL)
5073 goto on_fatal_error;
5074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005076 }
5077 break;
5078
5079 case ISN_CLEARDICT:
5080 dict_stack_drop();
5081 break;
5082
5083 case ISN_USEDICT:
5084 {
5085 typval_T *dict_tv = dict_stack_get_tv();
5086
5087 // Turn "dict.Func" into a partial for "Func" bound to
5088 // "dict". Don't do this when "Func" is already a partial
5089 // that was bound explicitly (pt_auto is FALSE).
5090 tv = STACK_TV_BOT(-1);
5091 if (dict_tv != NULL
5092 && dict_tv->v_type == VAR_DICT
5093 && dict_tv->vval.v_dict != NULL
5094 && (tv->v_type == VAR_FUNC
5095 || (tv->v_type == VAR_PARTIAL
5096 && (tv->vval.v_partial->pt_auto
5097 || tv->vval.v_partial->pt_dict == NULL))))
5098 dict_tv->vval.v_dict =
5099 make_partial(dict_tv->vval.v_dict, tv);
5100 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 }
5102 break;
5103
5104 case ISN_NEGATENR:
5105 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005106 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005107 if (tv->v_type == VAR_FLOAT)
5108 tv->vval.v_float = -tv->vval.v_float;
5109 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005110 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 break;
5112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 case ISN_CHECKTYPE:
5114 {
5115 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005116 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005117 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005119 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005120 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005121 if (!ectx->ec_where.wt_variable)
5122 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005123 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005124 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005125 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005126 if (r == FAIL)
5127 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005128 if (!ectx->ec_where.wt_variable)
5129 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005130
5131 // number 0 is FALSE, number 1 is TRUE
5132 if (tv->v_type == VAR_NUMBER
5133 && ct->ct_type->tt_type == VAR_BOOL
5134 && (tv->vval.v_number == 0
5135 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005136 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005137 tv->v_type = VAR_BOOL;
5138 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005139 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005140 }
5141 }
5142 break;
5143
Bram Moolenaar9af78762020-06-16 11:34:42 +02005144 case ISN_CHECKLEN:
5145 {
5146 int min_len = iptr->isn_arg.checklen.cl_min_len;
5147 list_T *list = NULL;
5148
5149 tv = STACK_TV_BOT(-1);
5150 if (tv->v_type == VAR_LIST)
5151 list = tv->vval.v_list;
5152 if (list == NULL || list->lv_len < min_len
5153 || (list->lv_len > min_len
5154 && !iptr->isn_arg.checklen.cl_more_OK))
5155 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005156 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005157 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005158 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005159 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005160 }
5161 }
5162 break;
5163
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005164 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005165 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005166 break;
5167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005169 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 {
5171 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005172 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005174 if (iptr->isn_type == ISN_2BOOL)
5175 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005176 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005177 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005178 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005179 n = !n;
5180 }
5181 else
5182 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005183 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005184 SOURCING_LNUM = iptr->isn_lnum;
5185 n = tv_get_bool_chk(tv, &error);
5186 if (error)
5187 goto on_error;
5188 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 clear_tv(tv);
5190 tv->v_type = VAR_BOOL;
5191 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5192 }
5193 break;
5194
5195 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005196 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005197 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005198 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5199 iptr->isn_type == ISN_2STRING_ANY,
5200 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005201 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 break;
5203
Bram Moolenaar08597872020-12-10 19:43:40 +01005204 case ISN_RANGE:
5205 {
5206 exarg_T ea;
5207 char *errormsg;
5208
Bram Moolenaarece0b872021-01-08 20:40:45 +01005209 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005210 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005211 ea.addr_type = ADDR_LINES;
5212 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005213 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005214 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005215 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005216
Bram Moolenaar35578162021-08-02 19:10:38 +02005217 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005218 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005219 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005220 tv = STACK_TV_BOT(-1);
5221 tv->v_type = VAR_NUMBER;
5222 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005223 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005224 }
5225 break;
5226
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005227 case ISN_PUT:
5228 {
5229 int regname = iptr->isn_arg.put.put_regname;
5230 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5231 char_u *expr = NULL;
5232 int dir = FORWARD;
5233
Bram Moolenaar08597872020-12-10 19:43:40 +01005234 if (lnum < -2)
5235 {
5236 // line number was put on the stack by ISN_RANGE
5237 tv = STACK_TV_BOT(-1);
5238 curwin->w_cursor.lnum = tv->vval.v_number;
5239 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5240 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005241 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005242 }
5243 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005244 // :put! above cursor
5245 dir = BACKWARD;
5246 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005247 {
5248 curwin->w_cursor.lnum = lnum;
5249 if (lnum == 0)
5250 // check_cursor() below will move to line 1
5251 dir = BACKWARD;
5252 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005253
5254 if (regname == '=')
5255 {
5256 tv = STACK_TV_BOT(-1);
5257 if (tv->v_type == VAR_STRING)
5258 expr = tv->vval.v_string;
5259 else
5260 {
5261 expr = typval2string(tv, TRUE); // allocates value
5262 clear_tv(tv);
5263 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005264 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005265 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005266 check_cursor();
5267 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5268 vim_free(expr);
5269 }
5270 break;
5271
Bram Moolenaar02194d22020-10-24 23:08:38 +02005272 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005273 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5274 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5275 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5276 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005277 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5278 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005279 break;
5280
Bram Moolenaar02194d22020-10-24 23:08:38 +02005281 case ISN_CMDMOD_REV:
5282 // filter regprog is owned by the instruction, don't free it
5283 cmdmod.cmod_filter_regmatch.regprog = NULL;
5284 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005285 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5286 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005287 break;
5288
Bram Moolenaar792f7862020-11-23 08:31:18 +01005289 case ISN_UNPACK:
5290 {
5291 int count = iptr->isn_arg.unpack.unp_count;
5292 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5293 list_T *l;
5294 listitem_T *li;
5295 int i;
5296
5297 // Check there is a valid list to unpack.
5298 tv = STACK_TV_BOT(-1);
5299 if (tv->v_type != VAR_LIST)
5300 {
5301 SOURCING_LNUM = iptr->isn_lnum;
5302 emsg(_(e_for_argument_must_be_sequence_of_lists));
5303 goto on_error;
5304 }
5305 l = tv->vval.v_list;
5306 if (l == NULL
5307 || l->lv_len < (semicolon ? count - 1 : count))
5308 {
5309 SOURCING_LNUM = iptr->isn_lnum;
5310 emsg(_(e_list_value_does_not_have_enough_items));
5311 goto on_error;
5312 }
5313 else if (!semicolon && l->lv_len > count)
5314 {
5315 SOURCING_LNUM = iptr->isn_lnum;
5316 emsg(_(e_list_value_has_more_items_than_targets));
5317 goto on_error;
5318 }
5319
5320 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005321 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005322 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005323 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005324
5325 // Variable after semicolon gets a list with the remaining
5326 // items.
5327 if (semicolon)
5328 {
5329 list_T *rem_list =
5330 list_alloc_with_items(l->lv_len - count + 1);
5331
5332 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005333 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005334 tv = STACK_TV_BOT(-count);
5335 tv->vval.v_list = rem_list;
5336 ++rem_list->lv_refcount;
5337 tv->v_lock = 0;
5338 li = l->lv_first;
5339 for (i = 0; i < count - 1; ++i)
5340 li = li->li_next;
5341 for (i = 0; li != NULL; ++i)
5342 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005343 typval_T tvcopy;
5344
5345 copy_tv(&li->li_tv, &tvcopy);
5346 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005347 li = li->li_next;
5348 }
5349 --count;
5350 }
5351
5352 // Produce the values in reverse order, first item last.
5353 li = l->lv_first;
5354 for (i = 0; i < count; ++i)
5355 {
5356 tv = STACK_TV_BOT(-i - 1);
5357 copy_tv(&li->li_tv, tv);
5358 li = li->li_next;
5359 }
5360
5361 list_unref(l);
5362 }
5363 break;
5364
Bram Moolenaarb2049902021-01-24 12:53:53 +01005365 case ISN_PROF_START:
5366 case ISN_PROF_END:
5367 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005368#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005369 funccall_T cookie;
5370 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005371 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005372 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005373
Bram Moolenaarca16c602022-09-06 18:57:08 +01005374 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005375 if (iptr->isn_type == ISN_PROF_START)
5376 {
5377 func_line_start(&cookie, iptr->isn_lnum);
5378 // if we get here the instruction is executed
5379 func_line_exec(&cookie);
5380 }
5381 else
5382 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005383#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005384 }
5385 break;
5386
Bram Moolenaare99d4222021-06-13 14:01:26 +02005387 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005388 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005389 break;
5390
Bram Moolenaar389df252020-07-09 21:20:47 +02005391 case ISN_SHUFFLE:
5392 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005393 typval_T tmp_tv;
5394 int item = iptr->isn_arg.shuffle.shfl_item;
5395 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005396
5397 tmp_tv = *STACK_TV_BOT(-item);
5398 for ( ; up > 0 && item > 1; --up)
5399 {
5400 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5401 --item;
5402 }
5403 *STACK_TV_BOT(-item) = tmp_tv;
5404 }
5405 break;
5406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005407 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005408 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005410 ectx->ec_where.wt_index = 0;
5411 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005412 break;
5413 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005414 continue;
5415
Bram Moolenaard032f342020-07-18 18:13:02 +02005416func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005417 // Restore previous function. If the frame pointer is where we started
5418 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005419 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005420 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005421
Bram Moolenaar4c137212021-04-19 16:48:48 +02005422 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005423 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005424 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005425 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005426
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005427on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005428 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005429 // If "emsg_silent" is set then ignore the error, unless it was set
5430 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005431 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005432 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005433 {
5434 // If a sequence of instructions causes an error while ":silent!"
5435 // was used, restore the stack length and jump ahead to restoring
5436 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005437 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005438 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005439 while (ectx->ec_stack.ga_len
5440 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005441 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005442 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005443 clear_tv(STACK_TV_BOT(0));
5444 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005445 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5446 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005447 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005448 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005449 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005450on_fatal_error:
5451 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005452 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005453 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005454 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455 }
5456
5457done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005458 ret = OK;
5459theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005460 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005461
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005462 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005463 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5464 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005465}
5466
5467/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005468 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005469 * "rettv".
5470 * Return OK or FAIL.
5471 */
5472 int
5473exe_typval_instr(typval_T *tv, typval_T *rettv)
5474{
5475 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5476 isn_T *save_instr = ectx->ec_instr;
5477 int save_iidx = ectx->ec_iidx;
5478 int res;
5479
LemonBoyf3b48952022-05-05 13:53:03 +01005480 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5481 // even when the compilation fails.
5482 rettv->v_type = VAR_UNKNOWN;
5483
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005484 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5485 res = exec_instructions(ectx);
5486 if (res == OK)
5487 {
5488 *rettv = *STACK_TV_BOT(-1);
5489 --ectx->ec_stack.ga_len;
5490 }
5491
5492 ectx->ec_instr = save_instr;
5493 ectx->ec_iidx = save_iidx;
5494
5495 return res;
5496}
5497
5498/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005499 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5500 * "substitute_instr".
5501 */
5502 char_u *
5503exe_substitute_instr(void)
5504{
5505 ectx_T *ectx = substitute_instr->subs_ectx;
5506 isn_T *save_instr = ectx->ec_instr;
5507 int save_iidx = ectx->ec_iidx;
5508 char_u *res;
5509
5510 ectx->ec_instr = substitute_instr->subs_instr;
5511 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005512 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005513 typval_T *tv = STACK_TV_BOT(-1);
5514
Bram Moolenaar27523602021-06-05 21:36:19 +02005515 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005516 --ectx->ec_stack.ga_len;
5517 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005518 }
5519 else
5520 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005521 substitute_instr->subs_status = FAIL;
5522 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005524
Bram Moolenaar4c137212021-04-19 16:48:48 +02005525 ectx->ec_instr = save_instr;
5526 ectx->ec_iidx = save_iidx;
5527
5528 return res;
5529}
5530
5531/*
5532 * Call a "def" function from old Vim script.
5533 * Return OK or FAIL.
5534 */
5535 int
5536call_def_function(
5537 ufunc_T *ufunc,
5538 int argc_arg, // nr of arguments
5539 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005540 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005541 partial_T *partial, // optional partial for context
Bram Moolenaar58779852022-09-06 18:31:14 +01005542 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005543 typval_T *rettv) // return value
5544{
5545 ectx_T ectx; // execution context
5546 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005547 int partial_argc = partial == NULL
5548 || (flags & DEF_USE_PT_ARGV) == 0
5549 ? 0 : partial->pt_argc;
5550 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005551 typval_T *tv;
5552 int idx;
5553 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005554 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005555 sctx_T save_current_sctx = current_sctx;
5556 int did_emsg_before = did_emsg_cumul + did_emsg;
5557 int save_suppress_errthrow = suppress_errthrow;
5558 msglist_T **saved_msg_list = NULL;
5559 msglist_T *private_msg_list = NULL;
5560 int save_emsg_silent_def = emsg_silent_def;
5561 int save_did_emsg_def = did_emsg_def;
5562 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005563 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005564
5565// Get pointer to item in the stack.
5566#undef STACK_TV
5567#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5568
5569// Get pointer to item at the bottom of the stack, -1 is the bottom.
5570#undef STACK_TV_BOT
5571#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5572
5573// Get pointer to a local variable on the stack. Negative for arguments.
5574#undef STACK_TV_VAR
5575#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5576
5577 if (ufunc->uf_def_status == UF_NOT_COMPILED
5578 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005579 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5580 && compile_def_function(ufunc, FALSE,
5581 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005582 {
5583 if (did_emsg_cumul + did_emsg == did_emsg_before)
5584 semsg(_(e_function_is_not_compiled_str),
5585 printable_func_name(ufunc));
5586 return FAIL;
5587 }
5588
5589 {
5590 // Check the function was really compiled.
5591 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5592 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005593 if (dfunc->df_ufunc == NULL)
5594 {
5595 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5596 return FAIL;
5597 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005598 if (INSTRUCTIONS(dfunc) == NULL)
5599 {
5600 iemsg("using call_def_function() on not compiled function");
5601 return FAIL;
5602 }
5603 }
5604
5605 // If depth of calling is getting too high, don't execute the function.
5606 orig_funcdepth = funcdepth_get();
5607 if (funcdepth_increment() == FAIL)
5608 return FAIL;
5609
5610 CLEAR_FIELD(ectx);
5611 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5612 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005613 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005614 {
5615 funcdepth_decrement();
5616 return FAIL;
5617 }
5618 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5619 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5620 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005621 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005622
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005623 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005624 if (idx > 0 && ufunc->uf_va_name == NULL)
5625 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005626 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005627 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005628 goto failed_early;
5629 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005630 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005631 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005632 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005633 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005634 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005635 goto failed_early;
5636 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005637
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005638 // Put values from the partial and arguments on the stack, but no more than
5639 // what the function expects. A lambda can be called with more arguments
5640 // than it uses.
5641 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005642 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5643 ++idx)
5644 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005645 int argv_idx = idx - partial_argc;
5646
5647 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005648 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005649 && tv->v_type == VAR_SPECIAL
5650 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005651 {
5652 // Use the default value.
5653 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5654 }
5655 else
5656 {
5657 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5658 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005659 ufunc->uf_arg_types[idx], tv,
5660 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005661 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005662 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005663 }
5664 ++ectx.ec_stack.ga_len;
5665 }
5666
5667 // Turn varargs into a list. Empty list if no args.
5668 if (ufunc->uf_va_name != NULL)
5669 {
5670 int vararg_count = argc - ufunc->uf_args.ga_len;
5671
5672 if (vararg_count < 0)
5673 vararg_count = 0;
5674 else
5675 argc -= vararg_count;
5676 if (exe_newlist(vararg_count, &ectx) == FAIL)
5677 goto failed_early;
5678
5679 // Check the type of the list items.
5680 tv = STACK_TV_BOT(-1);
5681 if (ufunc->uf_va_type != NULL
5682 && ufunc->uf_va_type != &t_list_any
5683 && ufunc->uf_va_type->tt_member != &t_any
5684 && tv->vval.v_list != NULL)
5685 {
5686 type_T *expected = ufunc->uf_va_type->tt_member;
5687 listitem_T *li = tv->vval.v_list->lv_first;
5688
5689 for (idx = 0; idx < vararg_count; ++idx)
5690 {
5691 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005692 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005693 goto failed_early;
5694 li = li->li_next;
5695 }
5696 }
5697
5698 if (defcount > 0)
5699 // Move varargs list to below missing default arguments.
5700 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5701 --ectx.ec_stack.ga_len;
5702 }
5703
5704 // Make space for omitted arguments, will store default value below.
5705 // Any varargs list goes after them.
5706 if (defcount > 0)
5707 for (idx = 0; idx < defcount; ++idx)
5708 {
5709 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5710 ++ectx.ec_stack.ga_len;
5711 }
5712 if (ufunc->uf_va_name != NULL)
5713 ++ectx.ec_stack.ga_len;
5714
5715 // Frame pointer points to just after arguments.
5716 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5717 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5718
5719 {
5720 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5721 + ufunc->uf_dfunc_idx;
5722 ufunc_T *base_ufunc = dfunc->df_ufunc;
5723
5724 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005725 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005726 if (partial != NULL || base_ufunc->uf_partial != NULL)
5727 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005728 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5729 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005730 goto failed_early;
5731 if (partial != NULL)
5732 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005733 outer_T *outer = get_pt_outer(partial);
5734
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005735 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005736 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005737 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005738 if (current_ectx != NULL)
5739 {
5740 if (current_ectx->ec_outer_ref != NULL
5741 && current_ectx->ec_outer_ref->or_outer != NULL)
5742 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005743 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005744 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005745 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005746 }
5747 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005748 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005749 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005750 ++partial->pt_refcount;
5751 ectx.ec_outer_ref->or_partial = partial;
5752 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005753 }
5754 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005755 {
5756 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5757 ++base_ufunc->uf_partial->pt_refcount;
5758 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5759 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005760 }
5761 }
5762
5763 // dummy frame entries
5764 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5765 {
5766 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5767 ++ectx.ec_stack.ga_len;
5768 }
5769
5770 {
5771 // Reserve space for local variables and any closure reference count.
5772 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5773 + ufunc->uf_dfunc_idx;
5774
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005775 // Initialize variables to zero. That avoids having to generate
5776 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005777 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005778 {
5779 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5780 STACK_TV_VAR(idx)->vval.v_number = 0;
5781 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005782 ectx.ec_stack.ga_len += dfunc->df_varcount;
5783 if (dfunc->df_has_closure)
5784 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01005785 // Initialize the variable that counts how many closures were
5786 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005787 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5788 STACK_TV_VAR(idx)->vval.v_number = 0;
5789 ++ectx.ec_stack.ga_len;
5790 }
5791
5792 ectx.ec_instr = INSTRUCTIONS(dfunc);
5793 }
5794
Bram Moolenaar58779852022-09-06 18:31:14 +01005795 // Store the execution context in funccal, used by invoke_all_defer().
5796 if (funccal != NULL)
5797 funccal->fc_ectx = &ectx;
5798
Bram Moolenaar4c137212021-04-19 16:48:48 +02005799 // Following errors are in the function, not the caller.
5800 // Commands behave like vim9script.
5801 estack_push_ufunc(ufunc, 1);
5802 current_sctx = ufunc->uf_script_ctx;
5803 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5804
5805 // Use a specific location for storing error messages to be converted to an
5806 // exception.
5807 saved_msg_list = msg_list;
5808 msg_list = &private_msg_list;
5809
5810 // Do turn errors into exceptions.
5811 suppress_errthrow = FALSE;
5812
5813 // Do not delete the function while executing it.
5814 ++ufunc->uf_calls;
5815
5816 // When ":silent!" was used before calling then we still abort the
5817 // function. If ":silent!" is used in the function then we don't.
5818 emsg_silent_def = emsg_silent;
5819 did_emsg_def = 0;
5820
5821 ectx.ec_where.wt_index = 0;
5822 ectx.ec_where.wt_variable = FALSE;
5823
Bram Moolenaar5800c792022-09-22 17:34:01 +01005824 /*
5825 * Execute the instructions until done.
5826 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02005827 ret = exec_instructions(&ectx);
5828 if (ret == OK)
5829 {
5830 // function finished, get result from the stack.
5831 if (ufunc->uf_ret_type == &t_void)
5832 {
5833 rettv->v_type = VAR_VOID;
5834 }
5835 else
5836 {
5837 tv = STACK_TV_BOT(-1);
5838 *rettv = *tv;
5839 tv->v_type = VAR_UNKNOWN;
5840 }
5841 }
5842
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005843 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005844 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005845
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005846 // Deal with any remaining closures, they may be in use somewhere.
5847 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005848 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005849 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005850 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005851 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005852
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005853 estack_pop();
5854 current_sctx = save_current_sctx;
5855
Bram Moolenaar2336c372021-12-06 15:06:54 +00005856 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5857 // Function was unreferenced while being used, free it now.
5858 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005859
Bram Moolenaar352134b2020-10-17 22:04:08 +02005860 if (*msg_list != NULL && saved_msg_list != NULL)
5861 {
5862 msglist_T **plist = saved_msg_list;
5863
5864 // Append entries from the current msg_list (uncaught exceptions) to
5865 // the saved msg_list.
5866 while (*plist != NULL)
5867 plist = &(*plist)->next;
5868
5869 *plist = *msg_list;
5870 }
5871 msg_list = saved_msg_list;
5872
Bram Moolenaar4c137212021-04-19 16:48:48 +02005873 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005874 {
5875 cmdmod.cmod_filter_regmatch.regprog = NULL;
5876 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005877 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005878 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005879 emsg_silent_def = save_emsg_silent_def;
5880 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005881
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005882failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005883 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005884 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01005885 {
5886 tv = STACK_TV(idx);
5887 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
5888 clear_tv(tv);
5889 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005890 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005893 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005894 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005895 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005896 if (ectx.ec_outer_ref->or_outer_allocated)
5897 vim_free(ectx.ec_outer_ref->or_outer);
5898 partial_unref(ectx.ec_outer_ref->or_partial);
5899 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005900 }
5901
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005902 // Not sure if this is necessary.
5903 suppress_errthrow = save_suppress_errthrow;
5904
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005905 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5906 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005907 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005908 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005909 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005910 return ret;
5911}
5912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913/*
Bram Moolenaar58779852022-09-06 18:31:14 +01005914 * Called when a def function has finished (possibly failed).
5915 * Invoke all the function returns to clean up and invoke deferred functions,
5916 * except the toplevel one.
5917 */
5918 void
5919unwind_def_callstack(ectx_T *ectx)
5920{
5921 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
5922 func_return(ectx);
5923}
5924
5925/*
5926 * Invoke any deffered functions for the top function in "ectx".
5927 */
5928 void
5929may_invoke_defer_funcs(ectx_T *ectx)
5930{
5931 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
5932
5933 if (dfunc->df_defer_var_idx > 0)
5934 invoke_defer_funcs(ectx);
5935}
5936
5937/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01005938 * Return loopvarinfo in a printable form in allocated memory.
5939 */
5940 static char_u *
5941printable_loopvarinfo(loopvarinfo_T *lvi)
5942{
5943 garray_T ga;
5944 int depth;
5945
5946 ga_init2(&ga, 1, 100);
5947 for (depth = 0; depth < lvi->lvi_depth; ++depth)
5948 {
5949 if (ga_grow(&ga, 50) == FAIL)
5950 break;
5951 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01005952 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01005953 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01005954 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01005955 lvi->lvi_loop[depth].var_idx,
5956 lvi->lvi_loop[depth].var_idx
5957 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01005958 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01005959 }
5960 return ga.ga_data;
5961}
5962
5963/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005964 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5965 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5966 * "pfx" is prefixed to every line.
5967 */
5968 static void
5969list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5970{
5971 int line_idx = 0;
5972 int prev_current = 0;
5973 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005974 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005975
5976 for (current = 0; current < instr_count; ++current)
5977 {
5978 isn_T *iptr = &instr[current];
5979 char *line;
5980
5981 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005982 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005983 while (line_idx < iptr->isn_lnum
5984 && line_idx < ufunc->uf_lines.ga_len)
5985 {
5986 if (current > prev_current)
5987 {
5988 msg_puts("\n\n");
5989 prev_current = current;
5990 }
5991 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5992 if (line != NULL)
5993 msg(line);
5994 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005995 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5996 {
5997 int first_def_arg = ufunc->uf_args.ga_len
5998 - ufunc->uf_def_args.ga_len;
5999
6000 if (def_arg_idx > 0)
6001 msg_puts("\n\n");
6002 msg_start();
6003 msg_puts(" ");
6004 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6005 first_def_arg + def_arg_idx]);
6006 msg_puts(" = ");
6007 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6008 msg_clr_eos();
6009 msg_end();
6010 }
6011 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006012
6013 switch (iptr->isn_type)
6014 {
6015 case ISN_EXEC:
6016 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6017 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006018 case ISN_EXEC_SPLIT:
6019 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6020 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006021 case ISN_EXECRANGE:
6022 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6023 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006024 case ISN_LEGACY_EVAL:
6025 smsg("%s%4d EVAL legacy %s", pfx, current,
6026 iptr->isn_arg.string);
6027 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006028 case ISN_REDIRSTART:
6029 smsg("%s%4d REDIR", pfx, current);
6030 break;
6031 case ISN_REDIREND:
6032 smsg("%s%4d REDIR END%s", pfx, current,
6033 iptr->isn_arg.number ? " append" : "");
6034 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006035 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006036#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006037 smsg("%s%4d CEXPR pre %s", pfx, current,
6038 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006039#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006040 break;
6041 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006042#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006043 {
6044 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6045
6046 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6047 cexpr_get_auname(cer->cer_cmdidx),
6048 cer->cer_forceit ? "!" : "",
6049 cer->cer_cmdline);
6050 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006051#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006052 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006053 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006054 smsg("%s%4d INSTR", pfx, current);
6055 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6056 msg(" -------------");
6057 break;
6058 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006059 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006060 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6061
6062 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006063 }
6064 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006065 case ISN_SUBSTITUTE:
6066 {
6067 subs_T *subs = &iptr->isn_arg.subs;
6068
6069 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6070 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6071 msg(" -------------");
6072 }
6073 break;
6074 case ISN_EXECCONCAT:
6075 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6076 (varnumber_T)iptr->isn_arg.number);
6077 break;
6078 case ISN_ECHO:
6079 {
6080 echo_T *echo = &iptr->isn_arg.echo;
6081
6082 smsg("%s%4d %s %d", pfx, current,
6083 echo->echo_with_white ? "ECHO" : "ECHON",
6084 echo->echo_count);
6085 }
6086 break;
6087 case ISN_EXECUTE:
6088 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006089 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006090 break;
6091 case ISN_ECHOMSG:
6092 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006093 (varnumber_T)(iptr->isn_arg.number));
6094 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006095 case ISN_ECHOWINDOW:
6096 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
6097 (varnumber_T)(iptr->isn_arg.number));
6098 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006099 case ISN_ECHOCONSOLE:
6100 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6101 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006102 break;
6103 case ISN_ECHOERR:
6104 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006105 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006106 break;
6107 case ISN_LOAD:
6108 {
6109 if (iptr->isn_arg.number < 0)
6110 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6111 (varnumber_T)(iptr->isn_arg.number
6112 + STACK_FRAME_SIZE));
6113 else
6114 smsg("%s%4d LOAD $%lld", pfx, current,
6115 (varnumber_T)(iptr->isn_arg.number));
6116 }
6117 break;
6118 case ISN_LOADOUTER:
6119 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006120 isn_outer_T *outer = &iptr->isn_arg.outer;
6121
6122 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006123 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006124 outer->outer_depth,
6125 outer->outer_idx + STACK_FRAME_SIZE);
6126 else if (outer->outer_depth < 0)
6127 smsg("%s%4d LOADOUTER $%d in loop level %d",
6128 pfx, current,
6129 outer->outer_idx,
6130 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006131 else
6132 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006133 outer->outer_depth,
6134 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006135 }
6136 break;
6137 case ISN_LOADV:
6138 smsg("%s%4d LOADV v:%s", pfx, current,
6139 get_vim_var_name(iptr->isn_arg.number));
6140 break;
6141 case ISN_LOADSCRIPT:
6142 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006143 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6144 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6145 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006146
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006147 sv = get_script_svar(sref, -1);
6148 if (sv == NULL)
6149 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6150 pfx, current, si->sn_name);
6151 else
6152 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006153 sv->sv_name,
6154 sref->sref_idx,
6155 si->sn_name);
6156 }
6157 break;
6158 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006159 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006160 {
6161 scriptitem_T *si = SCRIPT_ITEM(
6162 iptr->isn_arg.loadstore.ls_sid);
6163
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006164 smsg("%s%4d %s s:%s from %s", pfx, current,
6165 iptr->isn_type == ISN_LOADS ? "LOADS"
6166 : "LOADEXPORT",
6167 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006168 }
6169 break;
6170 case ISN_LOADAUTO:
6171 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6172 break;
6173 case ISN_LOADG:
6174 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6175 break;
6176 case ISN_LOADB:
6177 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6178 break;
6179 case ISN_LOADW:
6180 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6181 break;
6182 case ISN_LOADT:
6183 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6184 break;
6185 case ISN_LOADGDICT:
6186 smsg("%s%4d LOAD g:", pfx, current);
6187 break;
6188 case ISN_LOADBDICT:
6189 smsg("%s%4d LOAD b:", pfx, current);
6190 break;
6191 case ISN_LOADWDICT:
6192 smsg("%s%4d LOAD w:", pfx, current);
6193 break;
6194 case ISN_LOADTDICT:
6195 smsg("%s%4d LOAD t:", pfx, current);
6196 break;
6197 case ISN_LOADOPT:
6198 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6199 break;
6200 case ISN_LOADENV:
6201 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6202 break;
6203 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006204 smsg("%s%4d LOADREG @%c", pfx, current,
6205 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006206 break;
6207
6208 case ISN_STORE:
6209 if (iptr->isn_arg.number < 0)
6210 smsg("%s%4d STORE arg[%lld]", pfx, current,
6211 iptr->isn_arg.number + STACK_FRAME_SIZE);
6212 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006213 smsg("%s%4d STORE $%lld", pfx, current,
6214 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006215 break;
6216 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006217 {
6218 isn_outer_T *outer = &iptr->isn_arg.outer;
6219
6220 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6221 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6222 pfx, current, outer->outer_idx);
6223 else
6224 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6225 outer->outer_depth, outer->outer_idx);
6226 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006227 break;
6228 case ISN_STOREV:
6229 smsg("%s%4d STOREV v:%s", pfx, current,
6230 get_vim_var_name(iptr->isn_arg.number));
6231 break;
6232 case ISN_STOREAUTO:
6233 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6234 break;
6235 case ISN_STOREG:
6236 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6237 break;
6238 case ISN_STOREB:
6239 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6240 break;
6241 case ISN_STOREW:
6242 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6243 break;
6244 case ISN_STORET:
6245 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6246 break;
6247 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006248 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006249 {
6250 scriptitem_T *si = SCRIPT_ITEM(
6251 iptr->isn_arg.loadstore.ls_sid);
6252
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006253 smsg("%s%4d %s %s in %s", pfx, current,
6254 iptr->isn_type == ISN_STORES
6255 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006256 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6257 }
6258 break;
6259 case ISN_STORESCRIPT:
6260 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006261 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6262 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6263 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006264
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006265 sv = get_script_svar(sref, -1);
6266 if (sv == NULL)
6267 smsg("%s%4d STORESCRIPT [deleted] in %s",
6268 pfx, current, si->sn_name);
6269 else
6270 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006271 sv->sv_name,
6272 sref->sref_idx,
6273 si->sn_name);
6274 }
6275 break;
6276 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006277 case ISN_STOREFUNCOPT:
6278 smsg("%s%4d %s &%s", pfx, current,
6279 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006280 iptr->isn_arg.storeopt.so_name);
6281 break;
6282 case ISN_STOREENV:
6283 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6284 break;
6285 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006286 smsg("%s%4d STOREREG @%c", pfx, current,
6287 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006288 break;
6289 case ISN_STORENR:
6290 smsg("%s%4d STORE %lld in $%d", pfx, current,
6291 iptr->isn_arg.storenr.stnr_val,
6292 iptr->isn_arg.storenr.stnr_idx);
6293 break;
6294
6295 case ISN_STOREINDEX:
6296 smsg("%s%4d STOREINDEX %s", pfx, current,
6297 vartype_name(iptr->isn_arg.vartype));
6298 break;
6299
6300 case ISN_STORERANGE:
6301 smsg("%s%4d STORERANGE", pfx, current);
6302 break;
6303
6304 // constants
6305 case ISN_PUSHNR:
6306 smsg("%s%4d PUSHNR %lld", pfx, current,
6307 (varnumber_T)(iptr->isn_arg.number));
6308 break;
6309 case ISN_PUSHBOOL:
6310 case ISN_PUSHSPEC:
6311 smsg("%s%4d PUSH %s", pfx, current,
6312 get_var_special_name(iptr->isn_arg.number));
6313 break;
6314 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006315 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006316 break;
6317 case ISN_PUSHS:
6318 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6319 break;
6320 case ISN_PUSHBLOB:
6321 {
6322 char_u *r;
6323 char_u numbuf[NUMBUFLEN];
6324 char_u *tofree;
6325
6326 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6327 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6328 vim_free(tofree);
6329 }
6330 break;
6331 case ISN_PUSHFUNC:
6332 {
6333 char *name = (char *)iptr->isn_arg.string;
6334
6335 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6336 name == NULL ? "[none]" : name);
6337 }
6338 break;
6339 case ISN_PUSHCHANNEL:
6340#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006341 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006342#endif
6343 break;
6344 case ISN_PUSHJOB:
6345#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006346 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006347#endif
6348 break;
6349 case ISN_PUSHEXC:
6350 smsg("%s%4d PUSH v:exception", pfx, current);
6351 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006352 case ISN_AUTOLOAD:
6353 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6354 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006355 case ISN_UNLET:
6356 smsg("%s%4d UNLET%s %s", pfx, current,
6357 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6358 iptr->isn_arg.unlet.ul_name);
6359 break;
6360 case ISN_UNLETENV:
6361 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6362 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6363 iptr->isn_arg.unlet.ul_name);
6364 break;
6365 case ISN_UNLETINDEX:
6366 smsg("%s%4d UNLETINDEX", pfx, current);
6367 break;
6368 case ISN_UNLETRANGE:
6369 smsg("%s%4d UNLETRANGE", pfx, current);
6370 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006371 case ISN_LOCKUNLOCK:
6372 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6373 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006374 case ISN_LOCKCONST:
6375 smsg("%s%4d LOCKCONST", pfx, current);
6376 break;
6377 case ISN_NEWLIST:
6378 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006379 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006380 break;
6381 case ISN_NEWDICT:
6382 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006383 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006384 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006385 case ISN_NEWPARTIAL:
6386 smsg("%s%4d NEWPARTIAL", pfx, current);
6387 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006388
6389 // function call
6390 case ISN_BCALL:
6391 {
6392 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6393
6394 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6395 internal_func_name(cbfunc->cbf_idx),
6396 cbfunc->cbf_argcount);
6397 }
6398 break;
6399 case ISN_DCALL:
6400 {
6401 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6402 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6403 + cdfunc->cdf_idx;
6404
6405 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006406 printable_func_name(df->df_ufunc),
6407 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006408 }
6409 break;
6410 case ISN_UCALL:
6411 {
6412 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6413
6414 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6415 cufunc->cuf_name, cufunc->cuf_argcount);
6416 }
6417 break;
6418 case ISN_PCALL:
6419 {
6420 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6421
6422 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6423 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6424 }
6425 break;
6426 case ISN_PCALL_END:
6427 smsg("%s%4d PCALL end", pfx, current);
6428 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006429 case ISN_DEFER:
6430 smsg("%s%4d DEFER %d args", pfx, current,
6431 (int)iptr->isn_arg.defer.defer_argcount);
6432 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006433 case ISN_RETURN:
6434 smsg("%s%4d RETURN", pfx, current);
6435 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006436 case ISN_RETURN_VOID:
6437 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006438 break;
6439 case ISN_FUNCREF:
6440 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006441 funcref_T *funcref = &iptr->isn_arg.funcref;
6442 funcref_extra_T *extra = funcref->fr_extra;
6443 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006444
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006445 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006446 {
6447 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6448 + funcref->fr_dfunc_idx;
6449 name = df->df_ufunc->uf_name;
6450 }
6451 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006452 name = extra->fre_func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006453 if (extra == NULL || extra->fre_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006454 smsg("%s%4d FUNCREF %s", pfx, current, name);
6455 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006456 {
6457 char_u *info = printable_loopvarinfo(
6458 &extra->fre_loopvar_info);
6459
6460 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6461 name, info);
6462 vim_free(info);
6463 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006464 }
6465 break;
6466
6467 case ISN_NEWFUNC:
6468 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006469 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006470
Bram Moolenaarcc341812022-09-19 15:54:34 +01006471 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006472 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6473 arg->nfa_lambda, arg->nfa_global);
6474 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006475 {
6476 char_u *info = printable_loopvarinfo(
6477 &arg->nfa_loopvar_info);
6478
6479 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6480 arg->nfa_lambda, arg->nfa_global, info);
6481 vim_free(info);
6482 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006483 }
6484 break;
6485
6486 case ISN_DEF:
6487 {
6488 char_u *name = iptr->isn_arg.string;
6489
6490 smsg("%s%4d DEF %s", pfx, current,
6491 name == NULL ? (char_u *)"" : name);
6492 }
6493 break;
6494
6495 case ISN_JUMP:
6496 {
6497 char *when = "?";
6498
6499 switch (iptr->isn_arg.jump.jump_when)
6500 {
6501 case JUMP_ALWAYS:
6502 when = "JUMP";
6503 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006504 case JUMP_NEVER:
6505 iemsg("JUMP_NEVER should not be used");
6506 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006507 case JUMP_AND_KEEP_IF_TRUE:
6508 when = "JUMP_AND_KEEP_IF_TRUE";
6509 break;
6510 case JUMP_IF_FALSE:
6511 when = "JUMP_IF_FALSE";
6512 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006513 case JUMP_WHILE_FALSE:
6514 when = "JUMP_WHILE_FALSE"; // unused
6515 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006516 case JUMP_IF_COND_FALSE:
6517 when = "JUMP_IF_COND_FALSE";
6518 break;
6519 case JUMP_IF_COND_TRUE:
6520 when = "JUMP_IF_COND_TRUE";
6521 break;
6522 }
6523 smsg("%s%4d %s -> %d", pfx, current, when,
6524 iptr->isn_arg.jump.jump_where);
6525 }
6526 break;
6527
6528 case ISN_JUMP_IF_ARG_SET:
6529 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6530 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6531 iptr->isn_arg.jump.jump_where);
6532 break;
6533
6534 case ISN_FOR:
6535 {
6536 forloop_T *forloop = &iptr->isn_arg.forloop;
6537
6538 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006539 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006540 }
6541 break;
6542
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006543 case ISN_ENDLOOP:
6544 {
6545 endloop_T *endloop = &iptr->isn_arg.endloop;
6546
Bram Moolenaarcc341812022-09-19 15:54:34 +01006547 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6548 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006549 endloop->end_funcref_idx,
6550 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006551 endloop->end_var_idx + endloop->end_var_count - 1,
6552 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006553 }
6554 break;
6555
6556 case ISN_WHILE:
6557 {
6558 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6559
6560 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6561 whileloop->while_funcref_idx,
6562 whileloop->while_end);
6563 }
6564 break;
6565
Bram Moolenaar4c137212021-04-19 16:48:48 +02006566 case ISN_TRY:
6567 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006568 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006569
6570 if (try->try_ref->try_finally == 0)
6571 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6572 pfx, current,
6573 try->try_ref->try_catch,
6574 try->try_ref->try_endtry);
6575 else
6576 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6577 pfx, current,
6578 try->try_ref->try_catch,
6579 try->try_ref->try_finally,
6580 try->try_ref->try_endtry);
6581 }
6582 break;
6583 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006584 smsg("%s%4d CATCH", pfx, current);
6585 break;
6586 case ISN_TRYCONT:
6587 {
6588 trycont_T *trycont = &iptr->isn_arg.trycont;
6589
6590 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6591 trycont->tct_levels,
6592 trycont->tct_levels == 1 ? "" : "s",
6593 trycont->tct_where);
6594 }
6595 break;
6596 case ISN_FINALLY:
6597 smsg("%s%4d FINALLY", pfx, current);
6598 break;
6599 case ISN_ENDTRY:
6600 smsg("%s%4d ENDTRY", pfx, current);
6601 break;
6602 case ISN_THROW:
6603 smsg("%s%4d THROW", pfx, current);
6604 break;
6605
6606 // expression operations on number
6607 case ISN_OPNR:
6608 case ISN_OPFLOAT:
6609 case ISN_OPANY:
6610 {
6611 char *what;
6612 char *ins;
6613
6614 switch (iptr->isn_arg.op.op_type)
6615 {
6616 case EXPR_MULT: what = "*"; break;
6617 case EXPR_DIV: what = "/"; break;
6618 case EXPR_REM: what = "%"; break;
6619 case EXPR_SUB: what = "-"; break;
6620 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006621 case EXPR_LSHIFT: what = "<<"; break;
6622 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006623 default: what = "???"; break;
6624 }
6625 switch (iptr->isn_type)
6626 {
6627 case ISN_OPNR: ins = "OPNR"; break;
6628 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6629 case ISN_OPANY: ins = "OPANY"; break;
6630 default: ins = "???"; break;
6631 }
6632 smsg("%s%4d %s %s", pfx, current, ins, what);
6633 }
6634 break;
6635
6636 case ISN_COMPAREBOOL:
6637 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006638 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006639 case ISN_COMPARENR:
6640 case ISN_COMPAREFLOAT:
6641 case ISN_COMPARESTRING:
6642 case ISN_COMPAREBLOB:
6643 case ISN_COMPARELIST:
6644 case ISN_COMPAREDICT:
6645 case ISN_COMPAREFUNC:
6646 case ISN_COMPAREANY:
6647 {
6648 char *p;
6649 char buf[10];
6650 char *type;
6651
6652 switch (iptr->isn_arg.op.op_type)
6653 {
6654 case EXPR_EQUAL: p = "=="; break;
6655 case EXPR_NEQUAL: p = "!="; break;
6656 case EXPR_GREATER: p = ">"; break;
6657 case EXPR_GEQUAL: p = ">="; break;
6658 case EXPR_SMALLER: p = "<"; break;
6659 case EXPR_SEQUAL: p = "<="; break;
6660 case EXPR_MATCH: p = "=~"; break;
6661 case EXPR_IS: p = "is"; break;
6662 case EXPR_ISNOT: p = "isnot"; break;
6663 case EXPR_NOMATCH: p = "!~"; break;
6664 default: p = "???"; break;
6665 }
6666 STRCPY(buf, p);
6667 if (iptr->isn_arg.op.op_ic == TRUE)
6668 strcat(buf, "?");
6669 switch(iptr->isn_type)
6670 {
6671 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6672 case ISN_COMPARESPECIAL:
6673 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006674 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006675 case ISN_COMPARENR: type = "COMPARENR"; break;
6676 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6677 case ISN_COMPARESTRING:
6678 type = "COMPARESTRING"; break;
6679 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6680 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6681 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6682 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6683 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6684 default: type = "???"; break;
6685 }
6686
6687 smsg("%s%4d %s %s", pfx, current, type, buf);
6688 }
6689 break;
6690
6691 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6692 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6693
6694 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006695 case ISN_CONCAT:
6696 smsg("%s%4d CONCAT size %lld", pfx, current,
6697 (varnumber_T)(iptr->isn_arg.number));
6698 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006699 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6700 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6701 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6702 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6703 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6704 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6705 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6706 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6707 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6708 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6709 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006710 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006711 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6712 iptr->isn_arg.getitem.gi_index,
6713 iptr->isn_arg.getitem.gi_with_op ?
6714 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006715 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6716 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6717 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006718 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6719 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6720
Bram Moolenaar4c137212021-04-19 16:48:48 +02006721 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6722
Bram Moolenaar4c137212021-04-19 16:48:48 +02006723 case ISN_CHECKTYPE:
6724 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006725 checktype_T *ct = &iptr->isn_arg.type;
6726 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006727
6728 if (ct->ct_arg_idx == 0)
6729 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6730 type_name(ct->ct_type, &tofree),
6731 (int)ct->ct_off);
6732 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006733 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006734 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006735 type_name(ct->ct_type, &tofree),
6736 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006737 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006738 (int)ct->ct_arg_idx);
6739 vim_free(tofree);
6740 break;
6741 }
6742 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6743 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6744 iptr->isn_arg.checklen.cl_min_len);
6745 break;
6746 case ISN_SETTYPE:
6747 {
6748 char *tofree;
6749
6750 smsg("%s%4d SETTYPE %s", pfx, current,
6751 type_name(iptr->isn_arg.type.ct_type, &tofree));
6752 vim_free(tofree);
6753 break;
6754 }
6755 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006756 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6757 smsg("%s%4d INVERT %d (!val)", pfx, current,
6758 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006759 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006760 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6761 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006762 break;
6763 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006764 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006765 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006766 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6767 pfx, current,
6768 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006769 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006770 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6771 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006772 break;
6773 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006774 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006775 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006776 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006777 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6778 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006779 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006780 else
6781 smsg("%s%4d PUT %c %ld", pfx, current,
6782 iptr->isn_arg.put.put_regname,
6783 (long)iptr->isn_arg.put.put_lnum);
6784 break;
6785
Bram Moolenaar4c137212021-04-19 16:48:48 +02006786 case ISN_CMDMOD:
6787 {
6788 char_u *buf;
6789 size_t len = produce_cmdmods(
6790 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6791
6792 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006793 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006794 {
6795 (void)produce_cmdmods(
6796 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6797 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6798 vim_free(buf);
6799 }
6800 break;
6801 }
6802 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6803
6804 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006805 smsg("%s%4d PROFILE START line %d", pfx, current,
6806 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006807 break;
6808
6809 case ISN_PROF_END:
6810 smsg("%s%4d PROFILE END", pfx, current);
6811 break;
6812
Bram Moolenaare99d4222021-06-13 14:01:26 +02006813 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006814 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6815 iptr->isn_arg.debug.dbg_break_lnum + 1,
6816 iptr->isn_lnum,
6817 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006818 break;
6819
Bram Moolenaar4c137212021-04-19 16:48:48 +02006820 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6821 iptr->isn_arg.unpack.unp_count,
6822 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6823 break;
6824 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6825 iptr->isn_arg.shuffle.shfl_item,
6826 iptr->isn_arg.shuffle.shfl_up);
6827 break;
6828 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6829
6830 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6831 return;
6832 }
6833
6834 out_flush(); // output one line at a time
6835 ui_breakcheck();
6836 if (got_int)
6837 break;
6838 }
6839}
6840
6841/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006842 * Handle command line completion for the :disassemble command.
6843 */
6844 void
6845set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6846{
6847 char_u *p;
6848
6849 // Default: expand user functions, "debug" and "profile"
6850 xp->xp_context = EXPAND_DISASSEMBLE;
6851 xp->xp_pattern = arg;
6852
6853 // first argument already typed: only user function names
6854 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6855 {
6856 xp->xp_context = EXPAND_USER_FUNC;
6857 xp->xp_pattern = skipwhite(p);
6858 }
6859}
6860
6861/*
6862 * Function given to ExpandGeneric() to obtain the list of :disassemble
6863 * arguments.
6864 */
6865 char_u *
6866get_disassemble_argument(expand_T *xp, int idx)
6867{
6868 if (idx == 0)
6869 return (char_u *)"debug";
6870 if (idx == 1)
6871 return (char_u *)"profile";
6872 return get_user_func_name(xp, idx - 2);
6873}
6874
6875/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006876 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006877 * We don't really need this at runtime, but we do have tests that require it,
6878 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 */
6880 void
6881ex_disassemble(exarg_T *eap)
6882{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006883 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006884 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006886 isn_T *instr = NULL; // init to shut up gcc warning
6887 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006888 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006889
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006890 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006891 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006893 if (func_needs_compiling(ufunc, compile_type)
6894 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006895 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006896 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006898 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006899 return;
6900 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006901 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006902
6903 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006904 switch (compile_type)
6905 {
6906 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006907#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006908 instr = dfunc->df_instr_prof;
6909 instr_count = dfunc->df_instr_prof_count;
6910 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006911#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006912 // FALLTHROUGH
6913 case CT_NONE:
6914 instr = dfunc->df_instr;
6915 instr_count = dfunc->df_instr_count;
6916 break;
6917 case CT_DEBUG:
6918 instr = dfunc->df_instr_debug;
6919 instr_count = dfunc->df_instr_debug_count;
6920 break;
6921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922
Bram Moolenaar4c137212021-04-19 16:48:48 +02006923 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924}
6925
6926/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006927 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006928 * list, etc. Mostly like what JavaScript does, except that empty list and
6929 * empty dictionary are FALSE.
6930 */
6931 int
6932tv2bool(typval_T *tv)
6933{
6934 switch (tv->v_type)
6935 {
6936 case VAR_NUMBER:
6937 return tv->vval.v_number != 0;
6938 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006939 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940 case VAR_PARTIAL:
6941 return tv->vval.v_partial != NULL;
6942 case VAR_FUNC:
6943 case VAR_STRING:
6944 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6945 case VAR_LIST:
6946 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6947 case VAR_DICT:
6948 return tv->vval.v_dict != NULL
6949 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6950 case VAR_BOOL:
6951 case VAR_SPECIAL:
6952 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6953 case VAR_JOB:
6954#ifdef FEAT_JOB_CHANNEL
6955 return tv->vval.v_job != NULL;
6956#else
6957 break;
6958#endif
6959 case VAR_CHANNEL:
6960#ifdef FEAT_JOB_CHANNEL
6961 return tv->vval.v_channel != NULL;
6962#else
6963 break;
6964#endif
6965 case VAR_BLOB:
6966 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6967 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006968 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006970 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 break;
6972 }
6973 return FALSE;
6974}
6975
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006976 void
6977emsg_using_string_as(typval_T *tv, int as_number)
6978{
6979 semsg(_(as_number ? e_using_string_as_number_str
6980 : e_using_string_as_bool_str),
6981 tv->vval.v_string == NULL
6982 ? (char_u *)"" : tv->vval.v_string);
6983}
6984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006985/*
6986 * If "tv" is a string give an error and return FAIL.
6987 */
6988 int
6989check_not_string(typval_T *tv)
6990{
6991 if (tv->v_type == VAR_STRING)
6992 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006993 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006994 clear_tv(tv);
6995 return FAIL;
6996 }
6997 return OK;
6998}
6999
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000
7001#endif // FEAT_EVAL