blob: d4d1ad6995d70b5142febbfb49ffd933bed6c57b [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 Moolenaarec15b1c2022-03-27 16:29:53 +0100203 if (list != NULL)
204 ++list->lv_refcount;
205 return OK;
206}
207
208/*
209 * Implementation of ISN_NEWDICT.
210 * Returns FAIL on total failure, MAYBE on error.
211 */
212 static int
213exe_newdict(int count, ectx_T *ectx)
214{
215 dict_T *dict = NULL;
216 dictitem_T *item;
217 char_u *key;
218 int idx;
219 typval_T *tv;
220
221 if (count >= 0)
222 {
223 dict = dict_alloc();
224 if (unlikely(dict == NULL))
225 return FAIL;
226 for (idx = 0; idx < count; ++idx)
227 {
228 // have already checked key type is VAR_STRING
229 tv = STACK_TV_BOT(2 * (idx - count));
230 // check key is unique
231 key = tv->vval.v_string == NULL
232 ? (char_u *)"" : tv->vval.v_string;
233 item = dict_find(dict, key, -1);
234 if (item != NULL)
235 {
236 semsg(_(e_duplicate_key_in_dicitonary), key);
237 dict_unref(dict);
238 return MAYBE;
239 }
240 item = dictitem_alloc(key);
241 clear_tv(tv);
242 if (unlikely(item == NULL))
243 {
244 dict_unref(dict);
245 return FAIL;
246 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100247 tv = STACK_TV_BOT(2 * (idx - count) + 1);
248 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100249 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100250 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100251 if (dict_add(dict, item) == FAIL)
252 {
253 // can this ever happen?
254 dict_unref(dict);
255 return FAIL;
256 }
257 }
258 }
259
260 if (count > 0)
261 ectx->ec_stack.ga_len -= 2 * count - 1;
262 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
263 return FAIL;
264 else
265 ++ectx->ec_stack.ga_len;
266 tv = STACK_TV_BOT(-1);
267 tv->v_type = VAR_DICT;
268 tv->v_lock = 0;
269 tv->vval.v_dict = dict;
270 if (dict != NULL)
271 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200272 return OK;
273}
274
275/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200276 * If debug_tick changed check if "ufunc" has a breakpoint and update
277 * "uf_has_breakpoint".
278 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000279 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200280update_has_breakpoint(ufunc_T *ufunc)
281{
282 if (ufunc->uf_debug_tick != debug_tick)
283 {
284 linenr_T breakpoint;
285
286 ufunc->uf_debug_tick = debug_tick;
287 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
288 ufunc->uf_has_breakpoint = breakpoint > 0;
289 }
290}
291
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200292static garray_T dict_stack = GA_EMPTY;
293
294/*
295 * Put a value on the dict stack. This consumes "tv".
296 */
297 static int
298dict_stack_save(typval_T *tv)
299{
300 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000301 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200302 if (ga_grow(&dict_stack, 1) == FAIL)
303 return FAIL;
304 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
305 ++dict_stack.ga_len;
306 return OK;
307}
308
309/*
310 * Get the typval at top of the dict stack.
311 */
312 static typval_T *
313dict_stack_get_tv(void)
314{
315 if (dict_stack.ga_len == 0)
316 return NULL;
317 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
318}
319
320/*
321 * Get the dict at top of the dict stack.
322 */
323 static dict_T *
324dict_stack_get_dict(void)
325{
326 typval_T *tv;
327
328 if (dict_stack.ga_len == 0)
329 return NULL;
330 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
331 if (tv->v_type == VAR_DICT)
332 return tv->vval.v_dict;
333 return NULL;
334}
335
336/*
337 * Drop an item from the dict stack.
338 */
339 static void
340dict_stack_drop(void)
341{
342 if (dict_stack.ga_len == 0)
343 {
344 iemsg("Dict stack underflow");
345 return;
346 }
347 --dict_stack.ga_len;
348 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
349}
350
351/*
352 * Drop items from the dict stack until the length is equal to "len".
353 */
354 static void
355dict_stack_clear(int len)
356{
357 while (dict_stack.ga_len > len)
358 dict_stack_drop();
359}
360
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200361/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000362 * Get a pointer to useful "pt_outer" of "pt".
363 */
364 static outer_T *
365get_pt_outer(partial_T *pt)
366{
367 partial_T *ptref = pt->pt_outer_partial;
368
369 if (ptref == NULL)
370 return &pt->pt_outer;
371
372 // partial using partial (recursively)
373 while (ptref->pt_outer_partial != NULL)
374 ptref = ptref->pt_outer_partial;
375 return &ptref->pt_outer;
376}
377
378/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100380 * This adds a stack frame and sets the instruction pointer to the start of the
381 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200382 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 *
384 * Stack has:
385 * - current arguments (already there)
386 * - omitted optional argument (default values) added here
387 * - stack frame:
388 * - pointer to calling function
389 * - Index of next instruction in calling function
390 * - previous frame pointer
391 * - reserved space for local variables
392 */
393 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394call_dfunc(
395 int cdf_idx,
396 partial_T *pt,
397 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100400 int argcount = argcount_arg;
401 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
402 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200403 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100404 int arg_to_add;
405 int vararg_count = 0;
406 int varcount;
407 int idx;
408 estack_T *entry;
409 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200410 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000411 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412
413 if (dfunc->df_deleted)
414 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100415 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000416 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100417 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 return FAIL;
419 }
420
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100421#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100422 if (do_profiling == PROF_YES)
423 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200424 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100425 {
426 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
427 + profile_info_ga.ga_len;
428 ++profile_info_ga.ga_len;
429 CLEAR_POINTER(info);
430 profile_may_start_func(info, ufunc,
431 (((dfunc_T *)def_functions.ga_data)
432 + ectx->ec_dfunc_idx)->df_ufunc);
433 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100434 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100435#endif
436
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200437 // When debugging and using "cont" switches to the not-debugged
438 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000439 compile_type = get_compile_type(ufunc);
440 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200441 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000442 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200443
444 // compile_def_function() may cause def_functions.ga_data to change
445 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
446 }
447 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200448 {
449 if (did_emsg_cumul + did_emsg == did_emsg_before)
450 semsg(_(e_function_is_not_compiled_str),
451 printable_func_name(ufunc));
452 return FAIL;
453 }
454
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200455 if (ufunc->uf_va_name != NULL)
456 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200457 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200458 // Stack at time of call with 2 varargs:
459 // normal_arg
460 // optional_arg
461 // vararg_1
462 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200463 // After creating the list:
464 // normal_arg
465 // optional_arg
466 // vararg-list
467 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200468 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200469 // After creating the list
470 // normal_arg
471 // (space for optional_arg)
472 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200473 vararg_count = argcount - ufunc->uf_args.ga_len;
474 if (vararg_count < 0)
475 vararg_count = 0;
476 else
477 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200478 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200479 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200480
481 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200482 }
483
Bram Moolenaarfe270812020-04-11 22:31:27 +0200484 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200485 if (arg_to_add < 0)
486 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100487 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
488 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200489 return FAIL;
490 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000491 else if (arg_to_add > ufunc->uf_def_args.ga_len)
492 {
493 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
494
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100495 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
496 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000497 return FAIL;
498 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200499
500 // Reserve space for:
501 // - missing arguments
502 // - stack frame
503 // - local variables
504 // - if needed: a counter for number of closures created in
505 // ectx->ec_funcrefs.
506 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200507 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 return FAIL;
509
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100510 // If depth of calling is getting too high, don't execute the function.
511 if (funcdepth_increment() == FAIL)
512 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200513 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100514
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100515 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200516 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100517 {
518 floc = ALLOC_ONE(funclocal_T);
519 if (floc == NULL)
520 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200521 *floc = ectx->ec_funclocal;
522 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100523 }
524
Bram Moolenaarfe270812020-04-11 22:31:27 +0200525 // Move the vararg-list to below the missing optional arguments.
526 if (vararg_count > 0 && arg_to_add > 0)
527 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100528
529 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200530 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200531 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200532 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533
534 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100535 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
536 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200537 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200538 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
539 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100540 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100541 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200542 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543
544 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200545 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000546 {
547 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
548
549 tv->v_type = VAR_NUMBER;
550 tv->vval.v_number = 0;
551 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200552 if (dfunc->df_has_closure)
553 {
554 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
555
556 tv->v_type = VAR_NUMBER;
557 tv->vval.v_number = 0;
558 }
559 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100561 if (pt != NULL || ufunc->uf_partial != NULL
562 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100563 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200564 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100565
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200566 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100567 return FAIL;
568 if (pt != NULL)
569 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000570 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200571 ++pt->pt_refcount;
572 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100573 }
574 else if (ufunc->uf_partial != NULL)
575 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000576 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200577 ++ufunc->uf_partial->pt_refcount;
578 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100579 }
580 else
581 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200582 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200583 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200584 {
585 vim_free(ref);
586 return FAIL;
587 }
588 ref->or_outer_allocated = TRUE;
589 ref->or_outer->out_stack = &ectx->ec_stack;
590 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
591 if (ectx->ec_outer_ref != NULL)
592 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100593 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200594 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100595 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100596 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200597 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100598
Bram Moolenaarc970e422021-03-17 15:03:04 +0100599 ++ufunc->uf_calls;
600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 // Set execution state to the start of the called function.
602 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100603 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100604 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200605 if (entry != NULL)
606 {
607 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200608 // Save the current context so it can be restored on return.
609 entry->es_save_sctx = current_sctx;
610 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200611 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100612
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200613 // Start execution at the first instruction.
614 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615
616 return OK;
617}
618
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000619// Double linked list of funcstack_T in use.
620static funcstack_T *first_funcstack = NULL;
621
622 static void
623add_funcstack_to_list(funcstack_T *funcstack)
624{
625 // Link in list of funcstacks.
626 if (first_funcstack != NULL)
627 first_funcstack->fs_prev = funcstack;
628 funcstack->fs_next = first_funcstack;
629 funcstack->fs_prev = NULL;
630 first_funcstack = funcstack;
631}
632
633 static void
634remove_funcstack_from_list(funcstack_T *funcstack)
635{
636 if (funcstack->fs_prev == NULL)
637 first_funcstack = funcstack->fs_next;
638 else
639 funcstack->fs_prev->fs_next = funcstack->fs_next;
640 if (funcstack->fs_next != NULL)
641 funcstack->fs_next->fs_prev = funcstack->fs_prev;
642}
643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200645 * Used when returning from a function: Check if any closure is still
646 * referenced. If so then move the arguments and variables to a separate piece
647 * of stack to be used when the closure is called.
648 * When "free_arguments" is TRUE the arguments are to be freed.
649 * Returns FAIL when out of memory.
650 */
651 static int
652handle_closure_in_use(ectx_T *ectx, int free_arguments)
653{
654 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
655 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200656 int argcount;
657 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200658 int idx;
659 typval_T *tv;
660 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200661 garray_T *gap = &ectx->ec_funcrefs;
662 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200663
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200664 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200665 return OK; // function was freed
666 if (dfunc->df_has_closure == 0)
667 return OK; // no closures
668 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
669 closure_count = tv->vval.v_number;
670 if (closure_count == 0)
671 return OK; // no funcrefs created
672
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200673 argcount = ufunc_argcount(dfunc->df_ufunc);
674 top = ectx->ec_frame_idx - argcount;
675
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200676 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200677 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200678 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200679 partial_T *pt;
680 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200681
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200682 if (off < 0)
683 continue; // count is off or already done
684 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200685 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200686 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200687 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200688 int i;
689
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000690 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200691 // unreferenced on return.
692 for (i = 0; i < dfunc->df_varcount; ++i)
693 {
694 typval_T *stv = STACK_TV(ectx->ec_frame_idx
695 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200696 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200697 --refcount;
698 }
699 if (refcount > 1)
700 {
701 closure_in_use = TRUE;
702 break;
703 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200704 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200705 }
706
707 if (closure_in_use)
708 {
709 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
710 typval_T *stack;
711
712 // A closure is using the arguments and/or local variables.
713 // Move them to the called function.
714 if (funcstack == NULL)
715 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000716
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200717 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
718 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200719 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
720 funcstack->fs_ga.ga_data = stack;
721 if (stack == NULL)
722 {
723 vim_free(funcstack);
724 return FAIL;
725 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000726 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200727
728 // Move or copy the arguments.
729 for (idx = 0; idx < argcount; ++idx)
730 {
731 tv = STACK_TV(top + idx);
732 if (free_arguments)
733 {
734 *(stack + idx) = *tv;
735 tv->v_type = VAR_UNKNOWN;
736 }
737 else
738 copy_tv(tv, stack + idx);
739 }
740 // Move the local variables.
741 for (idx = 0; idx < dfunc->df_varcount; ++idx)
742 {
743 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200744
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200745 // A partial created for a local function, that is also used as a
746 // local variable, has a reference count for the variable, thus
747 // will never go down to zero. When all these refcounts are one
748 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000749 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200750 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
751 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200752 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200753
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200754 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200755 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
756 gap->ga_len - closure_count + i])
757 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200758 }
759
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200760 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200761 tv->v_type = VAR_UNKNOWN;
762 }
763
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200764 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200765 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200766 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
767 - closure_count + idx];
768 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200769 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200770 ++funcstack->fs_refcount;
771 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100772 pt->pt_outer.out_stack = &funcstack->fs_ga;
773 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200774 }
775 }
776 }
777
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200778 for (idx = 0; idx < closure_count; ++idx)
779 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
780 - closure_count + idx]);
781 gap->ga_len -= closure_count;
782 if (gap->ga_len == 0)
783 ga_clear(gap);
784
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200785 return OK;
786}
787
788/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200789 * Called when a partial is freed or its reference count goes down to one. The
790 * funcstack may be the only reference to the partials in the local variables.
791 * Go over all of them, the funcref and can be freed if all partials
792 * referencing the funcstack have a reference count of one.
793 */
794 void
795funcstack_check_refcount(funcstack_T *funcstack)
796{
797 int i;
798 garray_T *gap = &funcstack->fs_ga;
799 int done = 0;
800
801 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
802 return;
803 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 }
812 if (done == funcstack->fs_min_refcount)
813 {
814 typval_T *stack = gap->ga_data;
815
816 // All partials referencing the funcstack have a reference count of
817 // one, thus the funcstack is no longer of use.
818 for (i = 0; i < gap->ga_len; ++i)
819 clear_tv(stack + i);
820 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000821 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200822 vim_free(funcstack);
823 }
824}
825
826/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000827 * For garbage collecting: set references in all variables referenced by
828 * all funcstacks.
829 */
830 int
831set_ref_in_funcstacks(int copyID)
832{
833 funcstack_T *funcstack;
834
835 for (funcstack = first_funcstack; funcstack != NULL;
836 funcstack = funcstack->fs_next)
837 {
838 typval_T *stack = funcstack->fs_ga.ga_data;
839 int i;
840
841 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
842 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
843 return TRUE; // abort
844 }
845 return FALSE;
846}
847
Bram Moolenaar806a2732022-09-04 15:40:36 +0100848// Ugly static to avoid passing the execution context around through many
849// layers.
850static ectx_T *current_ectx = NULL;
851
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000852/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100853 * Return TRUE if currently executing a :def function.
854 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100855 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100856 int
857in_def_function(void)
858{
859 return current_ectx != NULL;
860}
861
862/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100863 * Clear "current_ectx" and return the previous value. To be used when calling
864 * a user function.
865 */
866 ectx_T *
867clear_currrent_ectx(void)
868{
869 ectx_T *r = current_ectx;
870
871 current_ectx = NULL;
872 return r;
873}
874
875 void
876restore_current_ectx(ectx_T *ectx)
877{
878 if (current_ectx != NULL)
879 iemsg("Restoring current_ectx while it is not NULL");
880 current_ectx = ectx;
881}
882
883/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100884 * Add an entry for a deferred function call to the currently executing
885 * function.
886 * Return the list or NULL when failed.
887 */
888 static list_T *
889add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100890{
891 typval_T *defer_tv = STACK_TV_VAR(var_idx);
892 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100893 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100894 typval_T listval;
895
896 if (defer_tv->v_type != VAR_LIST)
897 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100898 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100899 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100900 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100901 }
902 defer_l = defer_tv->vval.v_list;
903
904 l = list_alloc_with_items(argcount + 1);
905 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100906 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100907 listval.v_type = VAR_LIST;
908 listval.vval.v_list = l;
909 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100910 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100911 {
912 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100913 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100914 }
915
Bram Moolenaar806a2732022-09-04 15:40:36 +0100916 return l;
917}
918
919/*
920 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
921 * The local variable that lists deferred functions is "var_idx".
922 * Returns OK or FAIL.
923 */
924 static int
925defer_command(int var_idx, int argcount, ectx_T *ectx)
926{
927 list_T *l = add_defer_item(var_idx, argcount, ectx);
928 int i;
929 typval_T *func_tv;
930
931 if (l == NULL)
932 return FAIL;
933
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100934 func_tv = STACK_TV_BOT(-argcount - 1);
935 // TODO: check type is a funcref
936 list_set_item(l, 0, func_tv);
937
938 for (i = 1; i <= argcount; ++i)
939 list_set_item(l, i, STACK_TV_BOT(-argcount + i - 1));
940 ectx->ec_stack.ga_len -= argcount + 1;
941 return OK;
942}
943
944/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100945 * Add a deferred function "name" with one argument "arg_tv".
946 * Consumes "name", also on failure.
947 * Only to be called when in_def_function() returns TRUE.
948 */
949 int
950add_defer_function(char_u *name, int argcount, typval_T *argvars)
951{
952 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
953 + current_ectx->ec_dfunc_idx;
954 list_T *l;
955 typval_T func_tv;
956 int i;
957
958 if (dfunc->df_defer_var_idx == 0)
959 {
960 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100961 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100962 return FAIL;
963 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100964
965 l = add_defer_item(dfunc->df_defer_var_idx - 1, 1, current_ectx);
966 if (l == NULL)
967 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100968 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100969 return FAIL;
970 }
971
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100972 func_tv.v_type = VAR_FUNC;
973 func_tv.v_lock = 0;
974 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100975 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100976
Bram Moolenaar806a2732022-09-04 15:40:36 +0100977 for (i = 0; i < argcount; ++i)
978 list_set_item(l, i + 1, argvars + i);
979 return OK;
980}
981
982/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100983 * Invoked when returning from a function: Invoke any deferred calls.
984 */
985 static void
986invoke_defer_funcs(ectx_T *ectx)
987{
988 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
989 + ectx->ec_dfunc_idx;
990 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
991 listitem_T *li;
992
993 if (defer_tv->v_type != VAR_LIST)
994 return; // no function added
995 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
996 {
997 list_T *l = li->li_tv.vval.v_list;
998 typval_T rettv;
999 typval_T argvars[MAX_FUNC_ARGS];
1000 int i;
1001 listitem_T *arg_li = l->lv_first;
1002 funcexe_T funcexe;
1003
1004 for (i = 0; i < l->lv_len - 1; ++i)
1005 {
1006 arg_li = arg_li->li_next;
1007 argvars[i] = arg_li->li_tv;
1008 }
1009
1010 CLEAR_FIELD(funcexe);
1011 funcexe.fe_evaluate = TRUE;
1012 rettv.v_type = VAR_UNKNOWN;
1013 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1014 &rettv, l->lv_len - 1, argvars, &funcexe);
1015 clear_tv(&rettv);
1016 }
1017}
1018
1019/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 * Return from the current function.
1021 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001022 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001023func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001025 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001026 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001027 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1028 + ectx->ec_dfunc_idx;
1029 int argcount = ufunc_argcount(dfunc->df_ufunc);
1030 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001031 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001032 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1033 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001034 funclocal_T *floc;
1035#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001036 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1037 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038
Bram Moolenaar12d26532021-02-19 19:13:21 +01001039 if (do_profiling == PROF_YES)
1040 {
1041 ufunc_T *caller = prev_dfunc->df_ufunc;
1042
1043 if (dfunc->df_ufunc->uf_profiling
1044 || (caller != NULL && caller->uf_profiling))
1045 {
1046 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1047 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1048 --profile_info_ga.ga_len;
1049 }
1050 }
1051#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001052
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001053 if (dfunc->df_defer_var_idx > 0)
1054 invoke_defer_funcs(ectx);
1055
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001056 // No check for uf_refcount being zero, cannot think of a way that would
1057 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001058 --dfunc->df_ufunc->uf_calls;
1059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001061 entry = estack_pop();
1062 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001063 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001065 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1066 return FAIL;
1067
1068 // Clear the arguments.
1069 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1070 clear_tv(STACK_TV(idx));
1071
1072 // Clear local variables and temp values, but not the return value.
1073 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001074 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001076
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001077 // The return value should be on top of the stack. However, when aborting
1078 // it may not be there and ec_frame_idx is the top of the stack.
1079 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001080 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001081 ret_idx = 0;
1082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001083 if (ectx->ec_outer_ref != NULL)
1084 {
1085 if (ectx->ec_outer_ref->or_outer_allocated)
1086 vim_free(ectx->ec_outer_ref->or_outer);
1087 partial_unref(ectx->ec_outer_ref->or_partial);
1088 vim_free(ectx->ec_outer_ref);
1089 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001090
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001091 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001092 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001093 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1094 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001095 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1096 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001097 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001098 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001099 floc = (void *)STACK_TV(ectx->ec_frame_idx
1100 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001101 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001102 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1103 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001104
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001105 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001106 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001107 else
1108 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001109 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001110 vim_free(floc);
1111 }
1112
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001113 if (ret_idx > 0)
1114 {
1115 // Reset the stack to the position before the call, with a spot for the
1116 // return value, moved there from above the frame.
1117 ectx->ec_stack.ga_len = top + 1;
1118 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1119 }
1120 else
1121 // Reset the stack to the position before the call.
1122 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001123
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001124 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001125 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001126 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127}
1128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129/*
1130 * Prepare arguments and rettv for calling a builtin or user function.
1131 */
1132 static int
1133call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1134{
1135 int idx;
1136 typval_T *tv;
1137
1138 // Move arguments from bottom of the stack to argvars[] and add terminator.
1139 for (idx = 0; idx < argcount; ++idx)
1140 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1141 argvars[argcount].v_type = VAR_UNKNOWN;
1142
1143 // Result replaces the arguments on the stack.
1144 if (argcount > 0)
1145 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001146 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147 return FAIL;
1148 else
1149 ++ectx->ec_stack.ga_len;
1150
1151 // Default return value is zero.
1152 tv = STACK_TV_BOT(-1);
1153 tv->v_type = VAR_NUMBER;
1154 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001155 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156
1157 return OK;
1158}
1159
1160/*
1161 * Call a builtin function by index.
1162 */
1163 static int
1164call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1165{
1166 typval_T argvars[MAX_FUNC_ARGS];
1167 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001168 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001169 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001170 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171
1172 if (call_prepare(argcount, argvars, ectx) == FAIL)
1173 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001174 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001176 // Call the builtin function. Set "current_ectx" so that when it
1177 // recursively invokes call_def_function() a closure context can be set.
1178 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001180 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001181 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182
1183 // Clear the arguments.
1184 for (idx = 0; idx < argcount; ++idx)
1185 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001186
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001187 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001188 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 return OK;
1190}
1191
1192/*
1193 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001194 * If the function is compiled this will add a stack frame and set the
1195 * instruction pointer at the start of the function.
1196 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001197 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001198 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 */
1200 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001201call_ufunc(
1202 ufunc_T *ufunc,
1203 partial_T *pt,
1204 int argcount,
1205 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001206 isn_T *iptr,
1207 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208{
1209 typval_T argvars[MAX_FUNC_ARGS];
1210 funcexe_T funcexe;
1211 int error;
1212 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001213 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001214 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215
Bram Moolenaare99d4222021-06-13 14:01:26 +02001216 if (func_needs_compiling(ufunc, compile_type)
1217 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1218 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001219 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001220 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001221 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001222 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001223 if (error != FCERR_UNKNOWN)
1224 {
1225 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001226 semsg(_(e_too_many_arguments_for_function_str),
1227 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001228 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001229 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001230 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001231 return FAIL;
1232 }
1233
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001234 // The function has been compiled, can call it quickly. For a function
1235 // that was defined later: we can call it directly next time.
1236 if (iptr != NULL)
1237 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001238 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001239 iptr->isn_type = ISN_DCALL;
1240 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1241 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1242 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001243 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245
1246 if (call_prepare(argcount, argvars, ectx) == FAIL)
1247 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001248 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001249 funcexe.fe_evaluate = TRUE;
1250 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251
1252 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001254 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255
1256 // Clear the arguments.
1257 for (idx = 0; idx < argcount; ++idx)
1258 clear_tv(&argvars[idx]);
1259
1260 if (error != FCERR_NONE)
1261 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001262 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 return FAIL;
1264 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001265 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001266 // Error other than from calling the function itself.
1267 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 return OK;
1269}
1270
1271/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001272 * If command modifiers were applied restore them.
1273 */
1274 static void
1275may_restore_cmdmod(funclocal_T *funclocal)
1276{
1277 if (funclocal->floc_restore_cmdmod)
1278 {
1279 cmdmod.cmod_filter_regmatch.regprog = NULL;
1280 undo_cmdmod(&cmdmod);
1281 cmdmod = funclocal->floc_save_cmdmod;
1282 funclocal->floc_restore_cmdmod = FALSE;
1283 }
1284}
1285
1286/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001287 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1288 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001289 */
1290 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001291vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001292{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001293 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001294}
1295
1296/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 * Execute a function by "name".
1298 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001299 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 * Returns FAIL if not found without an error message.
1301 */
1302 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001303call_by_name(
1304 char_u *name,
1305 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001306 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001307 isn_T *iptr,
1308 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309{
1310 ufunc_T *ufunc;
1311
1312 if (builtin_function(name, -1))
1313 {
1314 int func_idx = find_internal_func(name);
1315
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001316 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001318 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 return FAIL;
1320 return call_bfunc(func_idx, argcount, ectx);
1321 }
1322
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001323 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001324
1325 if (ufunc == NULL)
1326 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001327 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001328
1329 if (script_autoload(name, TRUE))
1330 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001331 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001332
1333 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001334 return FAIL; // bail out if loading the script caused an error
1335 }
1336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001338 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001339 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001340 {
1341 int i;
1342 typval_T *argv = STACK_TV_BOT(0) - argcount;
1343
1344 // The function can change at runtime, check that the argument
1345 // types are correct.
1346 for (i = 0; i < argcount; ++i)
1347 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001348 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001349
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001350 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001351 type = ufunc->uf_arg_types[i];
1352 else if (ufunc->uf_va_type != NULL)
1353 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001354 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001355 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001356 return FAIL;
1357 }
1358 }
1359
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001360 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001361 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362
1363 return FAIL;
1364}
1365
1366 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001367call_partial(
1368 typval_T *tv,
1369 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001370 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001372 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001373 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001375 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001376 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377
1378 if (tv->v_type == VAR_PARTIAL)
1379 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001380 partial_T *pt = tv->vval.v_partial;
1381 int i;
1382
1383 if (pt->pt_argc > 0)
1384 {
1385 // Make space for arguments from the partial, shift the "argcount"
1386 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001387 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001388 return FAIL;
1389 for (i = 1; i <= argcount; ++i)
1390 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1391 ectx->ec_stack.ga_len += pt->pt_argc;
1392 argcount += pt->pt_argc;
1393
1394 // copy the arguments from the partial onto the stack
1395 for (i = 0; i < pt->pt_argc; ++i)
1396 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1397 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001398 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399
1400 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001401 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 name = pt->pt_name;
1404 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001405 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001407 if (name != NULL)
1408 {
1409 char_u fname_buf[FLEN_FIXED + 1];
1410 char_u *tofree = NULL;
1411 int error = FCERR_NONE;
1412 char_u *fname;
1413
1414 // May need to translate <SNR>123_ to K_SNR.
1415 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1416 if (error != FCERR_NONE)
1417 res = FAIL;
1418 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001419 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001420 vim_free(tofree);
1421 }
1422
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001423 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 {
1425 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001426 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001427 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428 return FAIL;
1429 }
1430 return OK;
1431}
1432
1433/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001434 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1435 * TRUE.
1436 */
1437 static int
1438error_if_locked(int lock, char *error)
1439{
1440 if (lock & (VAR_LOCKED | VAR_FIXED))
1441 {
1442 emsg(_(error));
1443 return TRUE;
1444 }
1445 return FALSE;
1446}
1447
1448/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001449 * Give an error if "tv" is not a number and return FAIL.
1450 */
1451 static int
1452check_for_number(typval_T *tv)
1453{
1454 if (tv->v_type != VAR_NUMBER)
1455 {
1456 semsg(_(e_expected_str_but_got_str),
1457 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1458 return FAIL;
1459 }
1460 return OK;
1461}
1462
1463/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001464 * Store "tv" in variable "name".
1465 * This is for s: and g: variables.
1466 */
1467 static void
1468store_var(char_u *name, typval_T *tv)
1469{
1470 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001471 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001472
Bram Moolenaard877a572021-04-01 19:42:48 +02001473 if (tv->v_lock)
1474 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001475 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001476 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001477 restore_funccal();
1478}
1479
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001480/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001481 * Convert "tv" to a string.
1482 * Return FAIL if not allowed.
1483 */
1484 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001485do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001486{
1487 if (tv->v_type != VAR_STRING)
1488 {
1489 char_u *str;
1490
1491 if (is_2string_any)
1492 {
1493 switch (tv->v_type)
1494 {
1495 case VAR_SPECIAL:
1496 case VAR_BOOL:
1497 case VAR_NUMBER:
1498 case VAR_FLOAT:
1499 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001500
1501 case VAR_LIST:
1502 if (tolerant)
1503 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001504 char_u *s, *e, *p;
1505 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001506
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001507 ga_init2(&ga, sizeof(char_u *), 1);
1508
1509 // Convert to NL separated items, then
1510 // escape the items and replace the NL with
1511 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001512 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001513 if (str == NULL)
1514 return FAIL;
1515 s = str;
1516 while ((e = vim_strchr(s, '\n')) != NULL)
1517 {
1518 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001519 p = vim_strsave_fnameescape(s,
1520 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001521 if (p != NULL)
1522 {
1523 ga_concat(&ga, p);
1524 ga_concat(&ga, (char_u *)" ");
1525 vim_free(p);
1526 }
1527 s = e + 1;
1528 }
1529 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001530 clear_tv(tv);
1531 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001532 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001533 return OK;
1534 }
1535 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001536 default: to_string_error(tv->v_type);
1537 return FAIL;
1538 }
1539 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001540 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001541 clear_tv(tv);
1542 tv->v_type = VAR_STRING;
1543 tv->vval.v_string = str;
1544 }
1545 return OK;
1546}
1547
1548/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001549 * When the value of "sv" is a null list of dict, allocate it.
1550 */
1551 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001552allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001553{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001554 typval_T *tv = sv->sv_tv;
1555
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001556 switch (tv->v_type)
1557 {
1558 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001559 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001560 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001561 break;
1562 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001563 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001564 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001565 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001566 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001567 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001568 (void)rettv_blob_alloc(tv);
1569 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001570 default:
1571 break;
1572 }
1573}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001574
Bram Moolenaare7525c52021-01-09 13:20:37 +01001575/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001576 * Return the character "str[index]" where "index" is the character index,
1577 * including composing characters.
1578 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001579 */
1580 char_u *
1581char_from_string(char_u *str, varnumber_T index)
1582{
1583 size_t nbyte = 0;
1584 varnumber_T nchar = index;
1585 size_t slen;
1586
1587 if (str == NULL)
1588 return NULL;
1589 slen = STRLEN(str);
1590
Bram Moolenaarff871402021-03-26 13:34:05 +01001591 // Do the same as for a list: a negative index counts from the end.
1592 // Optimization to check the first byte to be below 0x80 (and no composing
1593 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001594 if (index < 0)
1595 {
1596 int clen = 0;
1597
1598 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001599 {
1600 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1601 ++nbyte;
1602 else if (enc_utf8)
1603 nbyte += utfc_ptr2len(str + nbyte);
1604 else
1605 nbyte += mb_ptr2len(str + nbyte);
1606 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001607 nchar = clen + index;
1608 if (nchar < 0)
1609 // unlike list: index out of range results in empty string
1610 return NULL;
1611 }
1612
1613 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001614 {
1615 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1616 ++nbyte;
1617 else if (enc_utf8)
1618 nbyte += utfc_ptr2len(str + nbyte);
1619 else
1620 nbyte += mb_ptr2len(str + nbyte);
1621 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001622 if (nbyte >= slen)
1623 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001624 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001625}
1626
1627/*
1628 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001629 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001630 * If going over the end return "str_len".
1631 * If "idx" is negative count from the end, -1 is the last character.
1632 * When going over the start return -1.
1633 */
1634 static long
1635char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1636{
1637 varnumber_T nchar = idx;
1638 size_t nbyte = 0;
1639
1640 if (nchar >= 0)
1641 {
1642 while (nchar > 0 && nbyte < str_len)
1643 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001644 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001645 --nchar;
1646 }
1647 }
1648 else
1649 {
1650 nbyte = str_len;
1651 while (nchar < 0 && nbyte > 0)
1652 {
1653 --nbyte;
1654 nbyte -= mb_head_off(str, str + nbyte);
1655 ++nchar;
1656 }
1657 if (nchar < 0)
1658 return -1;
1659 }
1660 return (long)nbyte;
1661}
1662
1663/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001664 * Return the slice "str[first : last]" using character indexes. Composing
1665 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001666 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001667 * Return NULL when the result is empty.
1668 */
1669 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001670string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001671{
1672 long start_byte, end_byte;
1673 size_t slen;
1674
1675 if (str == NULL)
1676 return NULL;
1677 slen = STRLEN(str);
1678 start_byte = char_idx2byte(str, slen, first);
1679 if (start_byte < 0)
1680 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001681 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001682 end_byte = (long)slen;
1683 else
1684 {
1685 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001686 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001687 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001688 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001689 }
1690
1691 if (start_byte >= (long)slen || end_byte <= start_byte)
1692 return NULL;
1693 return vim_strnsave(str + start_byte, end_byte - start_byte);
1694}
1695
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001696/*
1697 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1698 * When "dfunc_idx" is negative don't give an error.
1699 * Returns NULL for an error.
1700 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001701 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001702get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001703{
1704 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001705 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1706 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001707 svar_T *sv;
1708
1709 if (sref->sref_seq != si->sn_script_seq)
1710 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001711 // The script was reloaded after the function was compiled, the
1712 // script_idx may not be valid.
1713 if (dfunc != NULL)
1714 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1715 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001716 return NULL;
1717 }
1718 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001719 if (sv->sv_name == NULL)
1720 {
1721 if (dfunc != NULL)
1722 emsg(_(e_script_variable_was_deleted));
1723 return NULL;
1724 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001725 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001726 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001727 if (dfunc != NULL)
1728 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001729 return NULL;
1730 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001731
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001732 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1733 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001734 {
1735 if (dfunc != NULL)
1736 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1737 return NULL;
1738 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001739 return sv;
1740}
1741
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001742/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001743 * Function passed to do_cmdline() for splitting a script joined by NL
1744 * characters.
1745 */
1746 static char_u *
1747get_split_sourceline(
1748 int c UNUSED,
1749 void *cookie,
1750 int indent UNUSED,
1751 getline_opt_T options UNUSED)
1752{
1753 source_cookie_T *sp = (source_cookie_T *)cookie;
1754 char_u *p;
1755 char_u *line;
1756
Bram Moolenaar20677332021-06-06 17:02:53 +02001757 p = vim_strchr(sp->nextline, '\n');
1758 if (p == NULL)
1759 {
1760 line = vim_strsave(sp->nextline);
1761 sp->nextline += STRLEN(sp->nextline);
1762 }
1763 else
1764 {
1765 line = vim_strnsave(sp->nextline, p - sp->nextline);
1766 sp->nextline = p + 1;
1767 }
1768 return line;
1769}
1770
1771/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 * Execute a function by "name".
1773 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001774 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 */
1776 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001777call_eval_func(
1778 char_u *name,
1779 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001780 ectx_T *ectx,
1781 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782{
Bram Moolenaared677f52020-08-12 16:38:10 +02001783 int called_emsg_before = called_emsg;
1784 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001786 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001787 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001789 dictitem_T *v;
1790
1791 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001792 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1793 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001794 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001795 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001796 return FAIL;
1797 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001798 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001800 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801}
1802
1803/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001804 * When a function reference is used, fill a partial with the information
1805 * needed, especially when it is used as a closure.
1806 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001807 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001808fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1809{
1810 pt->pt_func = ufunc;
1811 pt->pt_refcount = 1;
1812
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001813 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001814 {
1815 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1816 + ectx->ec_dfunc_idx;
1817
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001818 // The closure may need to find arguments and local variables in the
1819 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001820 pt->pt_outer.out_stack = &ectx->ec_stack;
1821 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001822 if (ectx->ec_outer_ref != NULL)
1823 {
1824 // The current context already has a context, link to that one.
1825 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1826 if (ectx->ec_outer_ref->or_partial != NULL)
1827 {
1828 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1829 ++pt->pt_outer.out_up_partial->pt_refcount;
1830 }
1831 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001832
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001833 // If this function returns and the closure is still being used, we
1834 // need to make a copy of the context (arguments and local variables).
1835 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001836 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001837 {
1838 vim_free(pt);
1839 return FAIL;
1840 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001841 // Extra variable keeps the count of closures created in the current
1842 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001843 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1844 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1845
1846 ((partial_T **)ectx->ec_funcrefs.ga_data)
1847 [ectx->ec_funcrefs.ga_len] = pt;
1848 ++pt->pt_refcount;
1849 ++ectx->ec_funcrefs.ga_len;
1850 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001851 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001852 return OK;
1853}
1854
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001855/*
1856 * Execute iptr->isn_arg.string as an Ex command.
1857 */
1858 static int
1859exec_command(isn_T *iptr)
1860{
1861 source_cookie_T cookie;
1862
1863 SOURCING_LNUM = iptr->isn_lnum;
1864 // Pass getsourceline to get an error for a missing ":end"
1865 // command.
1866 CLEAR_FIELD(cookie);
1867 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1868 if (do_cmdline(iptr->isn_arg.string,
1869 getsourceline, &cookie,
1870 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1871 || did_emsg)
1872 return FAIL;
1873 return OK;
1874}
1875
Bram Moolenaar89445512022-04-14 12:58:23 +01001876/*
1877 * If script "sid" is not loaded yet then load it now.
1878 * Caller must make sure "sid" is a valid script ID.
1879 * "loaded" is set to TRUE if the script had to be loaded.
1880 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1881 */
1882 int
1883may_load_script(int sid, int *loaded)
1884{
1885 scriptitem_T *si = SCRIPT_ITEM(sid);
1886
1887 if (si->sn_state == SN_STATE_NOT_LOADED)
1888 {
1889 *loaded = TRUE;
1890 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1891 {
1892 semsg(_(e_cant_open_file_str), si->sn_name);
1893 return FAIL;
1894 }
1895 }
1896 return OK;
1897}
1898
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001899// used for v_instr of typval of VAR_INSTR
1900struct instr_S {
1901 ectx_T *instr_ectx;
1902 isn_T *instr_instr;
1903};
1904
Bram Moolenaar4c137212021-04-19 16:48:48 +02001905// used for substitute_instr
1906typedef struct subs_expr_S {
1907 ectx_T *subs_ectx;
1908 isn_T *subs_instr;
1909 int subs_status;
1910} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001911
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001912// Set when calling do_debug().
1913static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001914static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001915
1916/*
1917 * When debugging lookup "name" and return the typeval.
1918 * When not found return NULL.
1919 */
1920 typval_T *
1921lookup_debug_var(char_u *name)
1922{
1923 int idx;
1924 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001925 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001926 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001927 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001928
1929 if (ectx == NULL)
1930 return NULL;
1931 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1932
1933 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001934 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001935 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001936 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1937
1938 // the variable name may be NULL when not available in this block
1939 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001940 return STACK_TV_VAR(idx);
1941 }
1942
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001943 // Go through argument names.
1944 ufunc = dfunc->df_ufunc;
1945 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1946 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1947 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1948 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1949 - varargs_off + idx);
1950 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1951 return STACK_TV(ectx->ec_frame_idx - 1);
1952
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001953 return NULL;
1954}
1955
Bram Moolenaar26a44842021-09-02 18:49:06 +02001956/*
1957 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1958 * breakpoint was set in that function or when there is any expression.
1959 */
1960 int
1961may_break_in_function(ufunc_T *ufunc)
1962{
1963 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1964}
1965
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001966 static void
1967handle_debug(isn_T *iptr, ectx_T *ectx)
1968{
1969 char_u *line;
1970 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1971 + ectx->ec_dfunc_idx)->df_ufunc;
1972 isn_T *ni;
1973 int end_lnum = iptr->isn_lnum;
1974 garray_T ga;
1975 int lnum;
1976
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001977 if (ex_nesting_level > debug_break_level)
1978 {
1979 linenr_T breakpoint;
1980
Bram Moolenaar26a44842021-09-02 18:49:06 +02001981 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001982 return;
1983
1984 // check for the next breakpoint if needed
1985 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001986 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001987 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1988 return;
1989 }
1990
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001991 SOURCING_LNUM = iptr->isn_lnum;
1992 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001993 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001994
1995 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1996 if (ni->isn_type == ISN_DEBUG
1997 || ni->isn_type == ISN_RETURN
1998 || ni->isn_type == ISN_RETURN_VOID)
1999 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002000 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002001 break;
2002 }
2003
2004 if (end_lnum > iptr->isn_lnum)
2005 {
2006 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002007 for (lnum = iptr->isn_lnum; lnum < end_lnum
2008 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002009 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002010 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002011
Bram Moolenaar303215d2021-07-07 20:10:43 +02002012 if (p == NULL)
2013 continue; // left over from continuation line
2014 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002015 if (*p == '#')
2016 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002017 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002018 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2019 if (STRNCMP(p, "def ", 4) == 0)
2020 break;
2021 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002022 line = ga_concat_strings(&ga, " ");
2023 vim_free(ga.ga_data);
2024 }
2025 else
2026 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002027
Dominique Pelle6e969552021-06-17 13:53:41 +02002028 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002029 debug_context = NULL;
2030
2031 if (end_lnum > iptr->isn_lnum)
2032 vim_free(line);
2033}
2034
Bram Moolenaar4c137212021-04-19 16:48:48 +02002035/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002036 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002037 * Returns OK, FAIL or NOTDONE (uncatchable error).
2038 */
2039 static int
2040execute_storeindex(isn_T *iptr, ectx_T *ectx)
2041{
2042 vartype_T dest_type = iptr->isn_arg.vartype;
2043 typval_T *tv;
2044 typval_T *tv_idx = STACK_TV_BOT(-2);
2045 typval_T *tv_dest = STACK_TV_BOT(-1);
2046 int status = OK;
2047
2048 // Stack contains:
2049 // -3 value to be stored
2050 // -2 index
2051 // -1 dict or list
2052 tv = STACK_TV_BOT(-3);
2053 SOURCING_LNUM = iptr->isn_lnum;
2054 if (dest_type == VAR_ANY)
2055 {
2056 dest_type = tv_dest->v_type;
2057 if (dest_type == VAR_DICT)
2058 status = do_2string(tv_idx, TRUE, FALSE);
2059 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2060 {
2061 emsg(_(e_number_expected));
2062 status = FAIL;
2063 }
2064 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002065
2066 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002067 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002068 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002069 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002070 long lidx = (long)tv_idx->vval.v_number;
2071 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002072
Bram Moolenaare08be092022-02-17 13:08:26 +00002073 if (list == NULL)
2074 {
2075 emsg(_(e_list_not_set));
2076 return FAIL;
2077 }
2078 if (lidx < 0 && list->lv_len + lidx >= 0)
2079 // negative index is relative to the end
2080 lidx = list->lv_len + lidx;
2081 if (lidx < 0 || lidx > list->lv_len)
2082 {
2083 semsg(_(e_list_index_out_of_range_nr), lidx);
2084 return FAIL;
2085 }
2086 if (lidx < list->lv_len)
2087 {
2088 listitem_T *li = list_find(list, lidx);
2089
2090 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002091 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002092 return FAIL;
2093 // overwrite existing list item
2094 clear_tv(&li->li_tv);
2095 li->li_tv = *tv;
2096 }
2097 else
2098 {
2099 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2100 return FAIL;
2101 // append to list, only fails when out of memory
2102 if (list_append_tv(list, tv) == FAIL)
2103 return NOTDONE;
2104 clear_tv(tv);
2105 }
2106 }
2107 else if (dest_type == VAR_DICT)
2108 {
2109 char_u *key = tv_idx->vval.v_string;
2110 dict_T *dict = tv_dest->vval.v_dict;
2111 dictitem_T *di;
2112
2113 SOURCING_LNUM = iptr->isn_lnum;
2114 if (dict == NULL)
2115 {
2116 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002117 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002118 }
2119 if (key == NULL)
2120 key = (char_u *)"";
2121 di = dict_find(dict, key, -1);
2122 if (di != NULL)
2123 {
2124 if (error_if_locked(di->di_tv.v_lock,
2125 e_cannot_change_dict_item))
2126 return FAIL;
2127 // overwrite existing value
2128 clear_tv(&di->di_tv);
2129 di->di_tv = *tv;
2130 }
2131 else
2132 {
2133 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2134 return FAIL;
2135 // add to dict, only fails when out of memory
2136 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2137 return NOTDONE;
2138 clear_tv(tv);
2139 }
2140 }
2141 else if (dest_type == VAR_BLOB)
2142 {
2143 long lidx = (long)tv_idx->vval.v_number;
2144 blob_T *blob = tv_dest->vval.v_blob;
2145 varnumber_T nr;
2146 int error = FALSE;
2147 int len;
2148
2149 if (blob == NULL)
2150 {
2151 emsg(_(e_blob_not_set));
2152 return FAIL;
2153 }
2154 len = blob_len(blob);
2155 if (lidx < 0 && len + lidx >= 0)
2156 // negative index is relative to the end
2157 lidx = len + lidx;
2158
2159 // Can add one byte at the end.
2160 if (lidx < 0 || lidx > len)
2161 {
2162 semsg(_(e_blob_index_out_of_range_nr), lidx);
2163 return FAIL;
2164 }
2165 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2166 return FAIL;
2167 nr = tv_get_number_chk(tv, &error);
2168 if (error)
2169 return FAIL;
2170 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002171 }
2172 else
2173 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002174 status = FAIL;
2175 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002176 }
2177 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002178
2179 clear_tv(tv_idx);
2180 clear_tv(tv_dest);
2181 ectx->ec_stack.ga_len -= 3;
2182 if (status == FAIL)
2183 {
2184 clear_tv(tv);
2185 return FAIL;
2186 }
2187 return OK;
2188}
2189
2190/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002191 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002192 */
2193 static int
2194execute_storerange(isn_T *iptr, ectx_T *ectx)
2195{
2196 typval_T *tv;
2197 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2198 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2199 typval_T *tv_dest = STACK_TV_BOT(-1);
2200 int status = OK;
2201
2202 // Stack contains:
2203 // -4 value to be stored
2204 // -3 first index or "none"
2205 // -2 second index or "none"
2206 // -1 destination list or blob
2207 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002208 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002209 if (tv_dest->v_type == VAR_LIST)
2210 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002211 long n1;
2212 long n2;
2213 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002214
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002215 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2216 if (tv_idx2->v_type == VAR_SPECIAL
2217 && tv_idx2->vval.v_number == VVAL_NONE)
2218 n2 = list_len(tv_dest->vval.v_list) - 1;
2219 else
2220 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2221
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002222 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002223 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002224 status = FAIL;
2225 else
2226 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002227 status = check_range_index_two(tv_dest->vval.v_list,
2228 &n1, li1, &n2, FALSE);
2229 if (status != FAIL)
2230 status = list_assign_range(
2231 tv_dest->vval.v_list,
2232 tv->vval.v_list,
2233 n1,
2234 n2,
2235 tv_idx2->v_type == VAR_SPECIAL,
2236 (char_u *)"=",
2237 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002238 }
2239 }
2240 else if (tv_dest->v_type == VAR_BLOB)
2241 {
2242 varnumber_T n1;
2243 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002244 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002245
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002246 n1 = tv_get_number_chk(tv_idx1, NULL);
2247 if (tv_idx2->v_type == VAR_SPECIAL
2248 && tv_idx2->vval.v_number == VVAL_NONE)
2249 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2250 else
2251 n2 = tv_get_number_chk(tv_idx2, NULL);
2252 bloblen = blob_len(tv_dest->vval.v_blob);
2253
2254 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2255 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002256 status = FAIL;
2257 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002258 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002259 }
2260 else
2261 {
2262 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002263 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002264 }
2265
2266 clear_tv(tv_idx1);
2267 clear_tv(tv_idx2);
2268 clear_tv(tv_dest);
2269 ectx->ec_stack.ga_len -= 4;
2270 clear_tv(tv);
2271
2272 return status;
2273}
2274
2275/*
2276 * Unlet item in list or dict variable.
2277 */
2278 static int
2279execute_unletindex(isn_T *iptr, ectx_T *ectx)
2280{
2281 typval_T *tv_idx = STACK_TV_BOT(-2);
2282 typval_T *tv_dest = STACK_TV_BOT(-1);
2283 int status = OK;
2284
2285 // Stack contains:
2286 // -2 index
2287 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002288 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002289 if (tv_dest->v_type == VAR_DICT)
2290 {
2291 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002292 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002293 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002294 semsg(_(e_expected_str_but_got_str),
2295 vartype_name(VAR_STRING),
2296 vartype_name(tv_idx->v_type));
2297 status = FAIL;
2298 }
2299 else
2300 {
2301 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002302 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002303 dictitem_T *di = NULL;
2304
2305 if (d != NULL && value_check_lock(
2306 d->dv_lock, NULL, FALSE))
2307 status = FAIL;
2308 else
2309 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002310 if (tv_idx->v_type == VAR_STRING)
2311 {
2312 key = tv_idx->vval.v_string;
2313 if (key == NULL)
2314 key = (char_u *)"";
2315 }
2316 else
2317 {
2318 key = tv_get_string(tv_idx);
2319 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002320 if (d != NULL)
2321 di = dict_find(d, key, (int)STRLEN(key));
2322 if (di == NULL)
2323 {
2324 // NULL dict is equivalent to empty dict
2325 semsg(_(e_key_not_present_in_dictionary),
2326 key);
2327 status = FAIL;
2328 }
2329 else if (var_check_fixed(di->di_flags,
2330 NULL, FALSE)
2331 || var_check_ro(di->di_flags,
2332 NULL, FALSE))
2333 status = FAIL;
2334 else
2335 dictitem_remove(d, di);
2336 }
2337 }
2338 }
2339 else if (tv_dest->v_type == VAR_LIST)
2340 {
2341 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002342 if (check_for_number(tv_idx) == FAIL)
2343 {
2344 status = FAIL;
2345 }
2346 else
2347 {
2348 list_T *l = tv_dest->vval.v_list;
2349 long n = (long)tv_idx->vval.v_number;
2350
2351 if (l != NULL && value_check_lock(
2352 l->lv_lock, NULL, FALSE))
2353 status = FAIL;
2354 else
2355 {
2356 listitem_T *li = list_find(l, n);
2357
2358 if (li == NULL)
2359 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002360 semsg(_(e_list_index_out_of_range_nr), n);
2361 status = FAIL;
2362 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002363 else
2364 listitem_remove(l, li);
2365 }
2366 }
2367 }
2368 else
2369 {
2370 status = FAIL;
2371 semsg(_(e_cannot_index_str),
2372 vartype_name(tv_dest->v_type));
2373 }
2374
2375 clear_tv(tv_idx);
2376 clear_tv(tv_dest);
2377 ectx->ec_stack.ga_len -= 2;
2378
2379 return status;
2380}
2381
2382/*
2383 * Unlet a range of items in a list variable.
2384 */
2385 static int
2386execute_unletrange(isn_T *iptr, ectx_T *ectx)
2387{
2388 // Stack contains:
2389 // -3 index1
2390 // -2 index2
2391 // -1 dict or list
2392 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2393 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2394 typval_T *tv_dest = STACK_TV_BOT(-1);
2395 int status = OK;
2396
2397 if (tv_dest->v_type == VAR_LIST)
2398 {
2399 // indexes must be a number
2400 SOURCING_LNUM = iptr->isn_lnum;
2401 if (check_for_number(tv_idx1) == FAIL
2402 || (tv_idx2->v_type != VAR_SPECIAL
2403 && check_for_number(tv_idx2) == FAIL))
2404 {
2405 status = FAIL;
2406 }
2407 else
2408 {
2409 list_T *l = tv_dest->vval.v_list;
2410 long n1 = (long)tv_idx1->vval.v_number;
2411 long n2 = tv_idx2->v_type == VAR_SPECIAL
2412 ? 0 : (long)tv_idx2->vval.v_number;
2413 listitem_T *li;
2414
2415 li = list_find_index(l, &n1);
2416 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002417 {
2418 semsg(_(e_list_index_out_of_range_nr),
2419 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002420 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002421 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002422 else
2423 {
2424 if (n1 < 0)
2425 n1 = list_idx_of_item(l, li);
2426 if (n2 < 0)
2427 {
2428 listitem_T *li2 = list_find(l, n2);
2429
2430 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002431 {
2432 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002433 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002434 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002435 else
2436 n2 = list_idx_of_item(l, li2);
2437 }
2438 if (status != FAIL
2439 && tv_idx2->v_type != VAR_SPECIAL
2440 && n2 < n1)
2441 {
2442 semsg(_(e_list_index_out_of_range_nr), n2);
2443 status = FAIL;
2444 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002445 if (status != FAIL)
2446 list_unlet_range(l, li, n1,
2447 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002448 }
2449 }
2450 }
2451 else
2452 {
2453 status = FAIL;
2454 SOURCING_LNUM = iptr->isn_lnum;
2455 semsg(_(e_cannot_index_str),
2456 vartype_name(tv_dest->v_type));
2457 }
2458
2459 clear_tv(tv_idx1);
2460 clear_tv(tv_idx2);
2461 clear_tv(tv_dest);
2462 ectx->ec_stack.ga_len -= 3;
2463
2464 return status;
2465}
2466
2467/*
2468 * Top of a for loop.
2469 */
2470 static int
2471execute_for(isn_T *iptr, ectx_T *ectx)
2472{
2473 typval_T *tv;
2474 typval_T *ltv = STACK_TV_BOT(-1);
2475 typval_T *idxtv =
2476 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2477
2478 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2479 return FAIL;
2480 if (ltv->v_type == VAR_LIST)
2481 {
2482 list_T *list = ltv->vval.v_list;
2483
2484 // push the next item from the list
2485 ++idxtv->vval.v_number;
2486 if (list == NULL
2487 || idxtv->vval.v_number >= list->lv_len)
2488 {
2489 // past the end of the list, jump to "endfor"
2490 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2491 may_restore_cmdmod(&ectx->ec_funclocal);
2492 }
2493 else if (list->lv_first == &range_list_item)
2494 {
2495 // non-materialized range() list
2496 tv = STACK_TV_BOT(0);
2497 tv->v_type = VAR_NUMBER;
2498 tv->v_lock = 0;
2499 tv->vval.v_number = list_find_nr(
2500 list, idxtv->vval.v_number, NULL);
2501 ++ectx->ec_stack.ga_len;
2502 }
2503 else
2504 {
2505 listitem_T *li = list_find(list,
2506 idxtv->vval.v_number);
2507
2508 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2509 ++ectx->ec_stack.ga_len;
2510 }
2511 }
2512 else if (ltv->v_type == VAR_STRING)
2513 {
2514 char_u *str = ltv->vval.v_string;
2515
2516 // The index is for the last byte of the previous
2517 // character.
2518 ++idxtv->vval.v_number;
2519 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2520 {
2521 // past the end of the string, jump to "endfor"
2522 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2523 may_restore_cmdmod(&ectx->ec_funclocal);
2524 }
2525 else
2526 {
2527 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2528
2529 // Push the next character from the string.
2530 tv = STACK_TV_BOT(0);
2531 tv->v_type = VAR_STRING;
2532 tv->vval.v_string = vim_strnsave(
2533 str + idxtv->vval.v_number, clen);
2534 ++ectx->ec_stack.ga_len;
2535 idxtv->vval.v_number += clen - 1;
2536 }
2537 }
2538 else if (ltv->v_type == VAR_BLOB)
2539 {
2540 blob_T *blob = ltv->vval.v_blob;
2541
2542 // When we get here the first time make a copy of the
2543 // blob, so that the iteration still works when it is
2544 // changed.
2545 if (idxtv->vval.v_number == -1 && blob != NULL)
2546 {
2547 blob_copy(blob, ltv);
2548 blob_unref(blob);
2549 blob = ltv->vval.v_blob;
2550 }
2551
2552 // The index is for the previous byte.
2553 ++idxtv->vval.v_number;
2554 if (blob == NULL
2555 || idxtv->vval.v_number >= blob_len(blob))
2556 {
2557 // past the end of the blob, jump to "endfor"
2558 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2559 may_restore_cmdmod(&ectx->ec_funclocal);
2560 }
2561 else
2562 {
2563 // Push the next byte from the blob.
2564 tv = STACK_TV_BOT(0);
2565 tv->v_type = VAR_NUMBER;
2566 tv->vval.v_number = blob_get(blob,
2567 idxtv->vval.v_number);
2568 ++ectx->ec_stack.ga_len;
2569 }
2570 }
2571 else
2572 {
2573 semsg(_(e_for_loop_on_str_not_supported),
2574 vartype_name(ltv->v_type));
2575 return FAIL;
2576 }
2577 return OK;
2578}
2579
2580/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002581 * Load instruction for w:/b:/g:/t: variable.
2582 * "isn_type" is used instead of "iptr->isn_type".
2583 */
2584 static int
2585load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2586{
2587 dictitem_T *di = NULL;
2588 hashtab_T *ht = NULL;
2589 char namespace;
2590
2591 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2592 return NOTDONE;
2593 switch (isn_type)
2594 {
2595 case ISN_LOADG:
2596 ht = get_globvar_ht();
2597 namespace = 'g';
2598 break;
2599 case ISN_LOADB:
2600 ht = &curbuf->b_vars->dv_hashtab;
2601 namespace = 'b';
2602 break;
2603 case ISN_LOADW:
2604 ht = &curwin->w_vars->dv_hashtab;
2605 namespace = 'w';
2606 break;
2607 case ISN_LOADT:
2608 ht = &curtab->tp_vars->dv_hashtab;
2609 namespace = 't';
2610 break;
2611 default: // Cannot reach here
2612 return NOTDONE;
2613 }
2614 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2615
Bram Moolenaar06b77222022-01-25 15:51:56 +00002616 if (di == NULL)
2617 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002618 if (isn_type == ISN_LOADG)
2619 {
2620 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2621
2622 // g:Something could be a function
2623 if (ufunc != NULL)
2624 {
2625 typval_T *tv = STACK_TV_BOT(0);
2626
2627 ++ectx->ec_stack.ga_len;
2628 tv->v_type = VAR_FUNC;
2629 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2630 if (tv->vval.v_string == NULL)
2631 return FAIL;
2632 STRCPY(tv->vval.v_string, "g:");
2633 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2634 return OK;
2635 }
2636 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002637 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002638 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002639 // no check if the item exists in the script but
2640 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002641 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002642 else
2643 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002644 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002645 return FAIL;
2646 }
2647 else
2648 {
2649 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2650 ++ectx->ec_stack.ga_len;
2651 }
2652 return OK;
2653}
2654
2655/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002656 * Execute instructions in execution context "ectx".
2657 * Return OK or FAIL;
2658 */
2659 static int
2660exec_instructions(ectx_T *ectx)
2661{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002662 int ret = FAIL;
2663 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002664 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002665
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002666 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002667 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002668
Bram Moolenaarff652882021-05-16 15:24:49 +02002669 // Only catch exceptions in this instruction list.
2670 ectx->ec_trylevel_at_start = trylevel;
2671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 for (;;)
2673 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002674 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002676 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002677
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002678 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002679 {
2680 line_breakcheck();
2681 breakcheck_count = 0;
2682 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002683 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002684 {
2685 // Turn CTRL-C into an exception.
2686 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002687 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002688 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002689 did_throw = TRUE;
2690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002692 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002693 {
2694 // Turn an error message into an exception.
2695 did_emsg = FALSE;
2696 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002697 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002698 did_throw = TRUE;
2699 *msg_list = NULL;
2700 }
2701
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002702 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002704 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002705 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002706 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707
2708 // An exception jumps to the first catch, finally, or returns from
2709 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002710 while (index > 0)
2711 {
2712 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002713 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002714 break;
2715 // In the catch and finally block of this try we have to go up
2716 // one level.
2717 --index;
2718 trycmd = NULL;
2719 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002720 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002722 if (trycmd->tcd_in_catch)
2723 {
2724 // exception inside ":catch", jump to ":finally" once
2725 ectx->ec_iidx = trycmd->tcd_finally_idx;
2726 trycmd->tcd_finally_idx = 0;
2727 }
2728 else
2729 // jump to first ":catch"
2730 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002731 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002732 did_throw = FALSE; // don't come back here until :endtry
2733 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 }
2735 else
2736 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002737 // Not inside try or need to return from current functions.
2738 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002739 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002740 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002741 tv = STACK_TV_BOT(0);
2742 tv->v_type = VAR_NUMBER;
2743 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002744 ++ectx->ec_stack.ga_len;
2745 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002747 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002748 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002749 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002750 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 goto done;
2752 }
2753
Bram Moolenaar4c137212021-04-19 16:48:48 +02002754 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002755 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 }
2757 continue;
2758 }
2759
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002760 /*
2761 * Big switch on the instruction. Most compilers will be turning this
2762 * into an efficient lookup table, since the "case" values are an enum
2763 * with sequential numbers. It may look ugly, but it should be the
2764 * most efficient way.
2765 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002766 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 switch (iptr->isn_type)
2768 {
2769 // execute Ex command line
2770 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002771 if (exec_command(iptr) == FAIL)
2772 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 break;
2774
Bram Moolenaar20677332021-06-06 17:02:53 +02002775 // execute Ex command line split at NL characters.
2776 case ISN_EXEC_SPLIT:
2777 {
2778 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002779 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002780
2781 SOURCING_LNUM = iptr->isn_lnum;
2782 CLEAR_FIELD(cookie);
2783 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2784 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002785 line = get_split_sourceline(0, &cookie, 0, 0);
2786 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002787 get_split_sourceline, &cookie,
2788 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2789 == FAIL
2790 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002791 {
2792 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002793 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002794 }
2795 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002796 }
2797 break;
2798
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002799 // execute Ex command line that is only a range
2800 case ISN_EXECRANGE:
2801 {
2802 exarg_T ea;
2803 char *error = NULL;
2804
2805 CLEAR_FIELD(ea);
2806 ea.cmdidx = CMD_SIZE;
2807 ea.addr_type = ADDR_LINES;
2808 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002809 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002810 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002811 if (ea.cmd == NULL)
2812 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002813 // error is always NULL when using ADDR_LINES
2814 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002815 if (error != NULL)
2816 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002817 emsg(error);
2818 goto on_error;
2819 }
2820 }
2821 break;
2822
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002823 // Evaluate an expression with legacy syntax, push it onto the
2824 // stack.
2825 case ISN_LEGACY_EVAL:
2826 {
2827 char_u *arg = iptr->isn_arg.string;
2828 int res;
2829 int save_flags = cmdmod.cmod_flags;
2830
Bram Moolenaar35578162021-08-02 19:10:38 +02002831 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002832 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002833 tv = STACK_TV_BOT(0);
2834 init_tv(tv);
2835 cmdmod.cmod_flags |= CMOD_LEGACY;
2836 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2837 cmdmod.cmod_flags = save_flags;
2838 if (res == FAIL)
2839 goto on_error;
2840 ++ectx->ec_stack.ga_len;
2841 }
2842 break;
2843
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002844 // push typeval VAR_INSTR with instructions to be executed
2845 case ISN_INSTR:
2846 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002847 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002848 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002849 tv = STACK_TV_BOT(0);
2850 tv->vval.v_instr = ALLOC_ONE(instr_T);
2851 if (tv->vval.v_instr == NULL)
2852 goto on_error;
2853 ++ectx->ec_stack.ga_len;
2854
2855 tv->v_type = VAR_INSTR;
2856 tv->vval.v_instr->instr_ectx = ectx;
2857 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2858 }
2859 break;
2860
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002861 case ISN_SOURCE:
2862 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002863 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002864
Bram Moolenaar89445512022-04-14 12:58:23 +01002865 SOURCING_LNUM = iptr->isn_lnum;
2866 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002867 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002868 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002869 }
2870 break;
2871
Bram Moolenaar4c137212021-04-19 16:48:48 +02002872 // execute :substitute with an expression
2873 case ISN_SUBSTITUTE:
2874 {
2875 subs_T *subs = &iptr->isn_arg.subs;
2876 source_cookie_T cookie;
2877 struct subs_expr_S *save_instr = substitute_instr;
2878 struct subs_expr_S subs_instr;
2879 int res;
2880
2881 subs_instr.subs_ectx = ectx;
2882 subs_instr.subs_instr = subs->subs_instr;
2883 subs_instr.subs_status = OK;
2884 substitute_instr = &subs_instr;
2885
2886 SOURCING_LNUM = iptr->isn_lnum;
2887 // This is very much like ISN_EXEC
2888 CLEAR_FIELD(cookie);
2889 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2890 res = do_cmdline(subs->subs_cmd,
2891 getsourceline, &cookie,
2892 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2893 substitute_instr = save_instr;
2894
2895 if (res == FAIL || did_emsg
2896 || subs_instr.subs_status == FAIL)
2897 goto on_error;
2898 }
2899 break;
2900
2901 case ISN_FINISH:
2902 goto done;
2903
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002904 case ISN_REDIRSTART:
2905 // create a dummy entry for var_redir_str()
2906 if (alloc_redir_lval() == FAIL)
2907 goto on_error;
2908
2909 // The output is stored in growarray "redir_ga" until
2910 // redirection ends.
2911 init_redir_ga();
2912 redir_vname = 1;
2913 break;
2914
2915 case ISN_REDIREND:
2916 {
2917 char_u *res = get_clear_redir_ga();
2918
2919 // End redirection, put redirected text on the stack.
2920 clear_redir_lval();
2921 redir_vname = 0;
2922
Bram Moolenaar35578162021-08-02 19:10:38 +02002923 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002924 {
2925 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002926 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002927 }
2928 tv = STACK_TV_BOT(0);
2929 tv->v_type = VAR_STRING;
2930 tv->vval.v_string = res;
2931 ++ectx->ec_stack.ga_len;
2932 }
2933 break;
2934
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002935 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002936#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002937 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002938 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2939 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002940 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002941#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002942 break;
2943
2944 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002945#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002946 {
2947 exarg_T ea;
2948 int res;
2949
2950 CLEAR_FIELD(ea);
2951 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2952 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2953 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2954 --ectx->ec_stack.ga_len;
2955 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01002956 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002957 res = cexpr_core(&ea, tv);
2958 clear_tv(tv);
2959 if (res == FAIL)
2960 goto on_error;
2961 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002962#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002963 break;
2964
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002965 // execute Ex command from pieces on the stack
2966 case ISN_EXECCONCAT:
2967 {
2968 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002969 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002970 int pass;
2971 int i;
2972 char_u *cmd = NULL;
2973 char_u *str;
2974
2975 for (pass = 1; pass <= 2; ++pass)
2976 {
2977 for (i = 0; i < count; ++i)
2978 {
2979 tv = STACK_TV_BOT(i - count);
2980 str = tv->vval.v_string;
2981 if (str != NULL && *str != NUL)
2982 {
2983 if (pass == 2)
2984 STRCPY(cmd + len, str);
2985 len += STRLEN(str);
2986 }
2987 if (pass == 2)
2988 clear_tv(tv);
2989 }
2990 if (pass == 1)
2991 {
2992 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002993 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002994 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002995 len = 0;
2996 }
2997 }
2998
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002999 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003000 do_cmdline_cmd(cmd);
3001 vim_free(cmd);
3002 }
3003 break;
3004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 // execute :echo {string} ...
3006 case ISN_ECHO:
3007 {
3008 int count = iptr->isn_arg.echo.echo_count;
3009 int atstart = TRUE;
3010 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003011 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012
3013 for (idx = 0; idx < count; ++idx)
3014 {
3015 tv = STACK_TV_BOT(idx - count);
3016 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3017 &atstart, &needclr);
3018 clear_tv(tv);
3019 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003020 if (needclr)
3021 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003022 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 }
3024 break;
3025
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003026 // :execute {string} ...
3027 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003028 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003029 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003030 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003031 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003032 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003033 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003034 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003035 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003036 {
3037 int count = iptr->isn_arg.number;
3038 garray_T ga;
3039 char_u buf[NUMBUFLEN];
3040 char_u *p;
3041 int len;
3042 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003043 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003044
3045 ga_init2(&ga, 1, 80);
3046 for (idx = 0; idx < count; ++idx)
3047 {
3048 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003049 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003050 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003051 if (tv->v_type == VAR_CHANNEL
3052 || tv->v_type == VAR_JOB)
3053 {
3054 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003055 semsg(_(e_using_invalid_value_as_string_str),
3056 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003057 break;
3058 }
3059 else
3060 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003061 }
3062 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003063 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003064
3065 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003066 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003067 failed = TRUE;
3068 else
3069 {
3070 if (ga.ga_len > 0)
3071 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3072 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3073 ga.ga_len += len;
3074 }
3075 clear_tv(tv);
3076 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003077 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003078 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003079 {
3080 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003081 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003082 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003083
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003084 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003085 {
3086 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003087 {
3088 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003089 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003090 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003091 {
3092 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003093 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003094 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003095 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003096 else
3097 {
3098 msg_sb_eol();
3099 if (iptr->isn_type == ISN_ECHOMSG)
3100 {
3101 msg_attr(ga.ga_data, echo_attr);
3102 out_flush();
3103 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003104#ifdef HAS_MESSAGE_WINDOW
3105 else if (iptr->isn_type == ISN_ECHOWINDOW)
3106 {
3107 start_echowindow();
3108 msg_attr(ga.ga_data, echo_attr);
3109 end_echowindow();
3110 }
3111#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003112 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3113 {
3114 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3115 TRUE);
3116 ui_write((char_u *)"\r\n", 2, TRUE);
3117 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003118 else
3119 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003120 SOURCING_LNUM = iptr->isn_lnum;
3121 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003122 }
3123 }
3124 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003125 ga_clear(&ga);
3126 }
3127 break;
3128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 // load local variable or argument
3130 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003131 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003132 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003134 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 break;
3136
3137 // load v: variable
3138 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003139 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003140 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003142 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 break;
3144
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003145 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 case ISN_LOADSCRIPT:
3147 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003148 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 svar_T *sv;
3150
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003151 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003152 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003153 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003154 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003155 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003156 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003158 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 }
3160 break;
3161
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003162 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003164 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003166 int sid = iptr->isn_arg.loadstore.ls_sid;
3167 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003168 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 if (di == NULL)
3172 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003173 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003174 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003175 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 }
3177 else
3178 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003179 if (iptr->isn_type == ISN_LOADEXPORT)
3180 {
3181 int idx = get_script_item_idx(sid, name, 0,
3182 NULL, NULL);
3183 svar_T *sv;
3184
3185 if (idx >= 0)
3186 {
3187 sv = ((svar_T *)SCRIPT_ITEM(sid)
3188 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003189 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003190 {
3191 SOURCING_LNUM = iptr->isn_lnum;
3192 semsg(_(e_item_not_exported_in_script_str),
3193 name);
3194 goto on_error;
3195 }
3196 }
3197 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003198 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003199 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003201 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 }
3203 }
3204 break;
3205
Bram Moolenaard3aac292020-04-19 14:32:17 +02003206 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003208 case ISN_LOADB:
3209 case ISN_LOADW:
3210 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003212 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003213
Bram Moolenaar06b77222022-01-25 15:51:56 +00003214 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003215 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003216 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003217 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 break;
3221
Bram Moolenaar03290b82020-12-19 16:30:44 +01003222 // load autoload variable
3223 case ISN_LOADAUTO:
3224 {
3225 char_u *name = iptr->isn_arg.string;
3226
Bram Moolenaar35578162021-08-02 19:10:38 +02003227 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003228 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003229 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003230 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003231 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003232 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003233 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003234 }
3235 break;
3236
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003237 // load g:/b:/w:/t: namespace
3238 case ISN_LOADGDICT:
3239 case ISN_LOADBDICT:
3240 case ISN_LOADWDICT:
3241 case ISN_LOADTDICT:
3242 {
3243 dict_T *d = NULL;
3244
3245 switch (iptr->isn_type)
3246 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003247 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3248 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3249 case ISN_LOADWDICT: d = curwin->w_vars; break;
3250 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003251 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003252 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003253 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003254 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003255 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003256 tv = STACK_TV_BOT(0);
3257 tv->v_type = VAR_DICT;
3258 tv->v_lock = 0;
3259 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003260 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003261 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003262 }
3263 break;
3264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 // load &option
3266 case ISN_LOADOPT:
3267 {
3268 typval_T optval;
3269 char_u *name = iptr->isn_arg.string;
3270
Bram Moolenaara8c17702020-04-01 21:17:24 +02003271 // This is not expected to fail, name is checked during
3272 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003273 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003274 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003275 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003276 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003278 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 }
3280 break;
3281
3282 // load $ENV
3283 case ISN_LOADENV:
3284 {
3285 typval_T optval;
3286 char_u *name = iptr->isn_arg.string;
3287
Bram Moolenaar35578162021-08-02 19:10:38 +02003288 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003289 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003290 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003291 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003293 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 }
3295 break;
3296
3297 // load @register
3298 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003299 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003300 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 tv = STACK_TV_BOT(0);
3302 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003303 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003304 // This may result in NULL, which should be equivalent to an
3305 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 tv->vval.v_string = get_reg_contents(
3307 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003308 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 break;
3310
3311 // store local variable
3312 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003313 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 tv = STACK_TV_VAR(iptr->isn_arg.number);
3315 clear_tv(tv);
3316 *tv = *STACK_TV_BOT(0);
3317 break;
3318
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003319 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003320 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003321 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003322 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003323 int sid = iptr->isn_arg.loadstore.ls_sid;
3324 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003325 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003326 dictitem_T *di = find_var_in_ht(ht, 0,
3327 iptr->isn_type == ISN_STORES
3328 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003329
Bram Moolenaar4c137212021-04-19 16:48:48 +02003330 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003331 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003332 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003333 {
3334 if (iptr->isn_type == ISN_STOREEXPORT)
3335 {
3336 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003337 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003338 goto on_error;
3339 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003340 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003341 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003342 else
3343 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003344 if (iptr->isn_type == ISN_STOREEXPORT)
3345 {
3346 int idx = get_script_item_idx(sid, name, 0,
3347 NULL, NULL);
3348
Bram Moolenaar06651632022-04-27 17:54:25 +01003349 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003350 if (idx >= 0)
3351 {
3352 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3353 ->sn_var_vals.ga_data) + idx;
3354
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003355 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003356 {
3357 semsg(_(e_item_not_exported_in_script_str),
3358 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003359 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003360 goto on_error;
3361 }
3362 }
3363 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003364 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003365 {
3366 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003367 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003368 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003369 clear_tv(&di->di_tv);
3370 di->di_tv = *STACK_TV_BOT(0);
3371 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003372 }
3373 break;
3374
3375 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 case ISN_STORESCRIPT:
3377 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003378 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3379 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003381 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003382 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003383 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003384 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003385
3386 // "const" and "final" are checked at compile time, locking
3387 // the value needs to be checked here.
3388 SOURCING_LNUM = iptr->isn_lnum;
3389 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003390 {
3391 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003392 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003393 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 clear_tv(sv->sv_tv);
3396 *sv->sv_tv = *STACK_TV_BOT(0);
3397 }
3398 break;
3399
3400 // store option
3401 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003402 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003404 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3405 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 long n = 0;
3407 char_u *s = NULL;
3408 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003409 char_u numbuf[NUMBUFLEN];
3410 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411
Bram Moolenaar4c137212021-04-19 16:48:48 +02003412 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 tv = STACK_TV_BOT(0);
3414 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003415 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003417 if (s == NULL)
3418 s = (char_u *)"";
3419 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003420 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3421 {
3422 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003423 // If the option can be set to a function reference or
3424 // a lambda and the passed value is a function
3425 // reference, then convert it to the name (string) of
3426 // the function reference.
3427 s = tv2string(tv, &tofree, numbuf, 0);
3428 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003429 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003430 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003431 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003432 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003433 goto on_error;
3434 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003435 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003437 // must be VAR_NUMBER, CHECKTYPE makes sure
3438 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003439 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003440 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003441 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 if (msg != NULL)
3443 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003444 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003446 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 }
3449 break;
3450
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003451 // store $ENV
3452 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003453 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003454 tv = STACK_TV_BOT(0);
3455 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3456 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003457 break;
3458
3459 // store @r
3460 case ISN_STOREREG:
3461 {
3462 int reg = iptr->isn_arg.number;
3463
Bram Moolenaar4c137212021-04-19 16:48:48 +02003464 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003465 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003466 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003467 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003468 }
3469 break;
3470
3471 // store v: variable
3472 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003473 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003474 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3475 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003476 // should not happen, type is checked when compiling
3477 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003478 break;
3479
Bram Moolenaard3aac292020-04-19 14:32:17 +02003480 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003482 case ISN_STOREB:
3483 case ISN_STOREW:
3484 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003486 dictitem_T *di;
3487 hashtab_T *ht;
3488 char_u *name = iptr->isn_arg.string + 2;
3489
Bram Moolenaard3aac292020-04-19 14:32:17 +02003490 switch (iptr->isn_type)
3491 {
3492 case ISN_STOREG:
3493 ht = get_globvar_ht();
3494 break;
3495 case ISN_STOREB:
3496 ht = &curbuf->b_vars->dv_hashtab;
3497 break;
3498 case ISN_STOREW:
3499 ht = &curwin->w_vars->dv_hashtab;
3500 break;
3501 case ISN_STORET:
3502 ht = &curtab->tp_vars->dv_hashtab;
3503 break;
3504 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003505 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003506 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507
Bram Moolenaar4c137212021-04-19 16:48:48 +02003508 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003509 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003511 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 else
3513 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003514 SOURCING_LNUM = iptr->isn_lnum;
3515 if (var_check_permission(di, name) == FAIL)
3516 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 clear_tv(&di->di_tv);
3518 di->di_tv = *STACK_TV_BOT(0);
3519 }
3520 }
3521 break;
3522
Bram Moolenaar03290b82020-12-19 16:30:44 +01003523 // store an autoload variable
3524 case ISN_STOREAUTO:
3525 SOURCING_LNUM = iptr->isn_lnum;
3526 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3527 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003528 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003529 break;
3530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 // store number in local variable
3532 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003533 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 clear_tv(tv);
3535 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003536 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 break;
3538
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003539 // store value in list or dict variable
3540 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003541 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003542 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003543
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003544 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003545 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003546 if (res == NOTDONE)
3547 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003548 }
3549 break;
3550
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003551 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003552 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003553 if (execute_storerange(iptr, ectx) == FAIL)
3554 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003555 break;
3556
Bram Moolenaar0186e582021-01-10 18:33:11 +01003557 // load or store variable or argument from outer scope
3558 case ISN_LOADOUTER:
3559 case ISN_STOREOUTER:
3560 {
3561 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003562 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3563 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003564
3565 while (depth > 1 && outer != NULL)
3566 {
3567 outer = outer->out_up;
3568 --depth;
3569 }
3570 if (outer == NULL)
3571 {
3572 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003573 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3574 || ectx->ec_outer_ref == NULL)
3575 // Possibly :def function called from legacy
3576 // context.
3577 emsg(_(e_closure_called_from_invalid_context));
3578 else
3579 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003580 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003581 }
3582 tv = ((typval_T *)outer->out_stack->ga_data)
3583 + outer->out_frame_idx + STACK_FRAME_SIZE
3584 + iptr->isn_arg.outer.outer_idx;
3585 if (iptr->isn_type == ISN_LOADOUTER)
3586 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003587 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003588 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003589 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003590 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003591 }
3592 else
3593 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003594 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003595 clear_tv(tv);
3596 *tv = *STACK_TV_BOT(0);
3597 }
3598 }
3599 break;
3600
Bram Moolenaar752fc692021-01-04 21:57:11 +01003601 // unlet item in list or dict variable
3602 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003603 if (execute_unletindex(iptr, ectx) == FAIL)
3604 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003605 break;
3606
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003607 // unlet range of items in list variable
3608 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003609 if (execute_unletrange(iptr, ectx) == FAIL)
3610 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003611 break;
3612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 // push constant
3614 case ISN_PUSHNR:
3615 case ISN_PUSHBOOL:
3616 case ISN_PUSHSPEC:
3617 case ISN_PUSHF:
3618 case ISN_PUSHS:
3619 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003620 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003621 case ISN_PUSHCHANNEL:
3622 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003623 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003624 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003626 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003627 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 switch (iptr->isn_type)
3629 {
3630 case ISN_PUSHNR:
3631 tv->v_type = VAR_NUMBER;
3632 tv->vval.v_number = iptr->isn_arg.number;
3633 break;
3634 case ISN_PUSHBOOL:
3635 tv->v_type = VAR_BOOL;
3636 tv->vval.v_number = iptr->isn_arg.number;
3637 break;
3638 case ISN_PUSHSPEC:
3639 tv->v_type = VAR_SPECIAL;
3640 tv->vval.v_number = iptr->isn_arg.number;
3641 break;
3642#ifdef FEAT_FLOAT
3643 case ISN_PUSHF:
3644 tv->v_type = VAR_FLOAT;
3645 tv->vval.v_float = iptr->isn_arg.fnumber;
3646 break;
3647#endif
3648 case ISN_PUSHBLOB:
3649 blob_copy(iptr->isn_arg.blob, tv);
3650 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003651 case ISN_PUSHFUNC:
3652 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003653 if (iptr->isn_arg.string == NULL)
3654 tv->vval.v_string = NULL;
3655 else
3656 tv->vval.v_string =
3657 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003658 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003659 case ISN_PUSHCHANNEL:
3660#ifdef FEAT_JOB_CHANNEL
3661 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003662 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003663#endif
3664 break;
3665 case ISN_PUSHJOB:
3666#ifdef FEAT_JOB_CHANNEL
3667 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003668 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003669#endif
3670 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 default:
3672 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003673 tv->vval.v_string = iptr->isn_arg.string == NULL
3674 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 }
3676 break;
3677
Bram Moolenaar06b77222022-01-25 15:51:56 +00003678 case ISN_AUTOLOAD:
3679 {
3680 char_u *name = iptr->isn_arg.string;
3681
3682 (void)script_autoload(name, FALSE);
3683 if (find_func(name, TRUE))
3684 {
3685 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3686 goto theend;
3687 tv = STACK_TV_BOT(0);
3688 tv->v_lock = 0;
3689 ++ectx->ec_stack.ga_len;
3690 tv->v_type = VAR_FUNC;
3691 tv->vval.v_string = vim_strsave(name);
3692 }
3693 else
3694 {
3695 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3696
3697 if (res == NOTDONE)
3698 goto theend;
3699 if (res == FAIL)
3700 goto on_error;
3701 }
3702 }
3703 break;
3704
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003705 case ISN_UNLET:
3706 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3707 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003708 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003709 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003710 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003711 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003712 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003713
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003714 case ISN_LOCKUNLOCK:
3715 {
3716 typval_T *lval_root_save = lval_root;
3717 int res;
3718
3719 // Stack has the local variable, argument the whole :lock
3720 // or :unlock command, like ISN_EXEC.
3721 --ectx->ec_stack.ga_len;
3722 lval_root = STACK_TV_BOT(0);
3723 res = exec_command(iptr);
3724 clear_tv(lval_root);
3725 lval_root = lval_root_save;
3726 if (res == FAIL)
3727 goto on_error;
3728 }
3729 break;
3730
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003731 case ISN_LOCKCONST:
3732 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3733 break;
3734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 // create a list from items on the stack; uses a single allocation
3736 // for the list header and the items
3737 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003738 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003739 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 break;
3741
3742 // create a dict from items on the stack
3743 case ISN_NEWDICT:
3744 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003745 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003747 SOURCING_LNUM = iptr->isn_lnum;
3748 res = exe_newdict(iptr->isn_arg.number, ectx);
3749 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003750 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003751 if (res == MAYBE)
3752 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 }
3754 break;
3755
LemonBoy372bcce2022-04-25 12:43:20 +01003756 case ISN_CONCAT:
3757 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3758 goto theend;
3759 break;
3760
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003761 // create a partial with NULL value
3762 case ISN_NEWPARTIAL:
3763 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3764 goto theend;
3765 ++ectx->ec_stack.ga_len;
3766 tv = STACK_TV_BOT(-1);
3767 tv->v_type = VAR_PARTIAL;
3768 tv->v_lock = 0;
3769 tv->vval.v_partial = NULL;
3770 break;
3771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 // call a :def function
3773 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003774 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003775 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3776 NULL,
3777 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003778 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003779 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 break;
3781
3782 // call a builtin function
3783 case ISN_BCALL:
3784 SOURCING_LNUM = iptr->isn_lnum;
3785 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3786 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003787 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003788 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 break;
3790
3791 // call a funcref or partial
3792 case ISN_PCALL:
3793 {
3794 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3795 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003796 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797
3798 SOURCING_LNUM = iptr->isn_lnum;
3799 if (pfunc->cpf_top)
3800 {
3801 // funcref is above the arguments
3802 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3803 }
3804 else
3805 {
3806 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003807 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003808 partial_tv = *STACK_TV_BOT(0);
3809 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003811 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003812 if (tv == &partial_tv)
3813 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003815 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 }
3817 break;
3818
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003819 case ISN_PCALL_END:
3820 // PCALL finished, arguments have been consumed and replaced by
3821 // the return value. Now clear the funcref from the stack,
3822 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003823 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003824 clear_tv(STACK_TV_BOT(-1));
3825 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3826 break;
3827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 // call a user defined function or funcref/partial
3829 case ISN_UCALL:
3830 {
3831 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3832
3833 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003834 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003835 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003836 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 }
3838 break;
3839
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003840 // :defer func(arg)
3841 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01003842 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003843 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
3844 goto on_error;
3845 break;
3846
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003847 // return from a :def function call without a value
3848 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003849 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003850 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003851 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003852 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003853 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003854 tv->vval.v_number = 0;
3855 tv->v_lock = 0;
3856 // FALLTHROUGH
3857
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003858 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 case ISN_RETURN:
3860 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003861 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003862 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003863
3864 if (trystack->ga_len > 0)
3865 trycmd = ((trycmd_T *)trystack->ga_data)
3866 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003867 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003868 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003870 // jump to ":finally" or ":endtry"
3871 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003872 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003873 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003874 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 trycmd->tcd_return = TRUE;
3876 }
3877 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003878 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 }
3880 break;
3881
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003882 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 case ISN_FUNCREF:
3884 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003885 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003886 ufunc_T *ufunc;
3887 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003890 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003891 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003892 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003893 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003894 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003895 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003896 if (funcref->fr_func_name == NULL)
3897 {
3898 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3899 + funcref->fr_dfunc_idx;
3900
3901 ufunc = pt_dfunc->df_ufunc;
3902 }
3903 else
3904 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003905 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003906 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003907 if (ufunc == NULL)
3908 {
3909 SOURCING_LNUM = iptr->isn_lnum;
3910 iemsg("ufunc unexpectedly NULL for FUNCREF");
3911 goto theend;
3912 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003913 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003914 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003916 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 tv->vval.v_partial = pt;
3918 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003919 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 }
3921 break;
3922
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003923 // Create a global function from a lambda.
3924 case ISN_NEWFUNC:
3925 {
3926 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3927
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003928 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003929 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003930 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003931 }
3932 break;
3933
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003934 // List functions
3935 case ISN_DEF:
3936 if (iptr->isn_arg.string == NULL)
3937 list_functions(NULL);
3938 else
3939 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003940 exarg_T ea;
3941 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003942
3943 CLEAR_FIELD(ea);
3944 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003945 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3946 define_function(&ea, NULL, &lines_to_free);
3947 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003948 }
3949 break;
3950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 // jump if a condition is met
3952 case ISN_JUMP:
3953 {
3954 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003955 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 int jump = TRUE;
3957
3958 if (when != JUMP_ALWAYS)
3959 {
3960 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003961 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003962 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003963 || when == JUMP_IF_COND_TRUE)
3964 {
3965 SOURCING_LNUM = iptr->isn_lnum;
3966 jump = tv_get_bool_chk(tv, &error);
3967 if (error)
3968 goto on_error;
3969 }
3970 else
3971 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01003972 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003974 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 {
3976 // drop the value from the stack
3977 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003978 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 }
3980 }
3981 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003982 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 }
3984 break;
3985
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003986 // Jump if an argument with a default value was already set and not
3987 // v:none.
3988 case ISN_JUMP_IF_ARG_SET:
3989 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3990 if (tv->v_type != VAR_UNKNOWN
3991 && !(tv->v_type == VAR_SPECIAL
3992 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003993 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003994 break;
3995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 // top of a for loop
3997 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003998 if (execute_for(iptr, ectx) == FAIL)
3999 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 break;
4001
4002 // start of ":try" block
4003 case ISN_TRY:
4004 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004005 trycmd_T *trycmd = NULL;
4006
Bram Moolenaar35578162021-08-02 19:10:38 +02004007 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004008 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004009 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4010 + ectx->ec_trystack.ga_len;
4011 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004013 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004014 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4015 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004016 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004017 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004018 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004019 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004020 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004021 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 }
4023 break;
4024
4025 case ISN_PUSHEXC:
4026 if (current_exception == NULL)
4027 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004028 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004030 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004032 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004033 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004035 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004037 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 tv->vval.v_string = vim_strsave(
4039 (char_u *)current_exception->value);
4040 break;
4041
4042 case ISN_CATCH:
4043 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004044 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004045 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046
Bram Moolenaar4c137212021-04-19 16:48:48 +02004047 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004048 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004050 trycmd->tcd_caught = TRUE;
4051 trycmd->tcd_did_throw = FALSE;
4052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004054 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 catch_exception(current_exception);
4056 }
4057 break;
4058
Bram Moolenaarc150c092021-02-13 15:02:46 +01004059 case ISN_TRYCONT:
4060 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004061 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004062 trycont_T *trycont = &iptr->isn_arg.trycont;
4063 int i;
4064 trycmd_T *trycmd;
4065 int iidx = trycont->tct_where;
4066
4067 if (trystack->ga_len < trycont->tct_levels)
4068 {
4069 siemsg("TRYCONT: expected %d levels, found %d",
4070 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004071 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004072 }
4073 // Make :endtry jump to any outer try block and the last
4074 // :endtry inside the loop to the loop start.
4075 for (i = trycont->tct_levels; i > 0; --i)
4076 {
4077 trycmd = ((trycmd_T *)trystack->ga_data)
4078 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004079 // Add one to tcd_cont to be able to jump to
4080 // instruction with index zero.
4081 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004082 iidx = trycmd->tcd_finally_idx == 0
4083 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004084 }
4085 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004086 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004087 }
4088 break;
4089
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004090 case ISN_FINALLY:
4091 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004092 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004093 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4094 + trystack->ga_len - 1;
4095
4096 // Reset the index to avoid a return statement jumps here
4097 // again.
4098 trycmd->tcd_finally_idx = 0;
4099 break;
4100 }
4101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 // end of ":try" block
4103 case ISN_ENDTRY:
4104 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004105 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004106 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107
Bram Moolenaar06651632022-04-27 17:54:25 +01004108 --trystack->ga_len;
4109 --trylevel;
4110 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4111 if (trycmd->tcd_did_throw)
4112 did_throw = TRUE;
4113 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004115 // discard the exception
4116 if (caught_stack == current_exception)
4117 caught_stack = caught_stack->caught;
4118 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004120
4121 if (trycmd->tcd_return)
4122 goto func_return;
4123
4124 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4125 {
4126 --ectx->ec_stack.ga_len;
4127 clear_tv(STACK_TV_BOT(0));
4128 }
4129 if (trycmd->tcd_cont != 0)
4130 // handling :continue: jump to outer try block or
4131 // start of the loop
4132 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 }
4134 break;
4135
4136 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004137 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004138 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004139
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004140 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4141 {
4142 // throwing an exception while using "silent!" causes
4143 // the function to abort but not display an error.
4144 tv = STACK_TV_BOT(-1);
4145 clear_tv(tv);
4146 tv->v_type = VAR_NUMBER;
4147 tv->vval.v_number = 0;
4148 goto done;
4149 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004150 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004151 tv = STACK_TV_BOT(0);
4152 if (tv->vval.v_string == NULL
4153 || *skipwhite(tv->vval.v_string) == NUL)
4154 {
4155 vim_free(tv->vval.v_string);
4156 SOURCING_LNUM = iptr->isn_lnum;
4157 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004158 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004159 }
4160
4161 // Inside a "catch" we need to first discard the caught
4162 // exception.
4163 if (trystack->ga_len > 0)
4164 {
4165 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4166 + trystack->ga_len - 1;
4167 if (trycmd->tcd_caught && current_exception != NULL)
4168 {
4169 // discard the exception
4170 if (caught_stack == current_exception)
4171 caught_stack = caught_stack->caught;
4172 discard_current_exception();
4173 trycmd->tcd_caught = FALSE;
4174 }
4175 }
4176
Bram Moolenaar90a57162022-02-12 14:23:17 +00004177 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004178 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4179 == FAIL)
4180 {
4181 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004182 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004183 }
4184 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 break;
4187
4188 // compare with special values
4189 case ISN_COMPAREBOOL:
4190 case ISN_COMPARESPECIAL:
4191 {
4192 typval_T *tv1 = STACK_TV_BOT(-2);
4193 typval_T *tv2 = STACK_TV_BOT(-1);
4194 varnumber_T arg1 = tv1->vval.v_number;
4195 varnumber_T arg2 = tv2->vval.v_number;
4196 int res;
4197
Bram Moolenaar06651632022-04-27 17:54:25 +01004198 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4199 res = arg1 == arg2;
4200 else
4201 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202
Bram Moolenaar4c137212021-04-19 16:48:48 +02004203 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 tv1->v_type = VAR_BOOL;
4205 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4206 }
4207 break;
4208
Bram Moolenaar7a222242022-03-01 19:23:24 +00004209 case ISN_COMPARENULL:
4210 {
4211 typval_T *tv1 = STACK_TV_BOT(-2);
4212 typval_T *tv2 = STACK_TV_BOT(-1);
4213 int res;
4214
4215 res = typval_compare_null(tv1, tv2);
4216 if (res == MAYBE)
4217 goto on_error;
4218 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4219 res = !res;
4220 clear_tv(tv1);
4221 clear_tv(tv2);
4222 --ectx->ec_stack.ga_len;
4223 tv1->v_type = VAR_BOOL;
4224 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4225 }
4226 break;
4227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 // Operation with two number arguments
4229 case ISN_OPNR:
4230 case ISN_COMPARENR:
4231 {
4232 typval_T *tv1 = STACK_TV_BOT(-2);
4233 typval_T *tv2 = STACK_TV_BOT(-1);
4234 varnumber_T arg1 = tv1->vval.v_number;
4235 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004236 varnumber_T res = 0;
4237 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004239 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4240 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4241 {
4242 if (arg2 < 0)
4243 {
4244 SOURCING_LNUM = iptr->isn_lnum;
4245 emsg(_(e_bitshift_ops_must_be_postive));
4246 goto on_error;
4247 }
4248 }
4249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 switch (iptr->isn_arg.op.op_type)
4251 {
4252 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004253 case EXPR_DIV: if (arg2 == 0)
4254 div_zero = TRUE;
4255 else
4256 res = arg1 / arg2;
4257 break;
4258 case EXPR_REM: if (arg2 == 0)
4259 div_zero = TRUE;
4260 else
4261 res = arg1 % arg2;
4262 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 case EXPR_SUB: res = arg1 - arg2; break;
4264 case EXPR_ADD: res = arg1 + arg2; break;
4265
4266 case EXPR_EQUAL: res = arg1 == arg2; break;
4267 case EXPR_NEQUAL: res = arg1 != arg2; break;
4268 case EXPR_GREATER: res = arg1 > arg2; break;
4269 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4270 case EXPR_SMALLER: res = arg1 < arg2; break;
4271 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004272 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4273 res = 0;
4274 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004275 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004276 break;
4277 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4278 res = 0;
4279 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004280 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004281 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004282 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 }
4284
Bram Moolenaar4c137212021-04-19 16:48:48 +02004285 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 if (iptr->isn_type == ISN_COMPARENR)
4287 {
4288 tv1->v_type = VAR_BOOL;
4289 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4290 }
4291 else
4292 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004293 if (div_zero)
4294 {
4295 SOURCING_LNUM = iptr->isn_lnum;
4296 emsg(_(e_divide_by_zero));
4297 goto on_error;
4298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 }
4300 break;
4301
4302 // Computation with two float arguments
4303 case ISN_OPFLOAT:
4304 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004305#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 {
4307 typval_T *tv1 = STACK_TV_BOT(-2);
4308 typval_T *tv2 = STACK_TV_BOT(-1);
4309 float_T arg1 = tv1->vval.v_float;
4310 float_T arg2 = tv2->vval.v_float;
4311 float_T res = 0;
4312 int cmp = FALSE;
4313
4314 switch (iptr->isn_arg.op.op_type)
4315 {
4316 case EXPR_MULT: res = arg1 * arg2; break;
4317 case EXPR_DIV: res = arg1 / arg2; break;
4318 case EXPR_SUB: res = arg1 - arg2; break;
4319 case EXPR_ADD: res = arg1 + arg2; break;
4320
4321 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4322 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4323 case EXPR_GREATER: cmp = arg1 > arg2; break;
4324 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4325 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4326 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4327 default: cmp = 0; break;
4328 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004329 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 if (iptr->isn_type == ISN_COMPAREFLOAT)
4331 {
4332 tv1->v_type = VAR_BOOL;
4333 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4334 }
4335 else
4336 tv1->vval.v_float = res;
4337 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004338#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 break;
4340
4341 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004342 case ISN_COMPAREDICT:
4343 case ISN_COMPAREFUNC:
4344 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 case ISN_COMPAREBLOB:
4346 {
4347 typval_T *tv1 = STACK_TV_BOT(-2);
4348 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004349 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4350 int ic = iptr->isn_arg.op.op_ic;
4351 int res = FALSE;
4352 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353
Bram Moolenaar265f8112021-12-19 12:33:05 +00004354 SOURCING_LNUM = iptr->isn_lnum;
4355 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004357 status = typval_compare_list(tv1, tv2,
4358 exprtype, ic, &res);
4359 }
4360 else if (iptr->isn_type == ISN_COMPAREDICT)
4361 {
4362 status = typval_compare_dict(tv1, tv2,
4363 exprtype, ic, &res);
4364 }
4365 else if (iptr->isn_type == ISN_COMPAREFUNC)
4366 {
4367 status = typval_compare_func(tv1, tv2,
4368 exprtype, ic, &res);
4369 }
4370 else if (iptr->isn_type == ISN_COMPARESTRING)
4371 {
4372 status = typval_compare_string(tv1, tv2,
4373 exprtype, ic, &res);
4374 }
4375 else
4376 {
4377 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004379 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 clear_tv(tv1);
4381 clear_tv(tv2);
4382 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004383 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4384 if (status == FAIL)
4385 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 }
4387 break;
4388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 case ISN_COMPAREANY:
4390 {
4391 typval_T *tv1 = STACK_TV_BOT(-2);
4392 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004393 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004395 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396
Bram Moolenaareb26f432020-09-14 16:50:05 +02004397 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004398 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004400 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004401 if (status == FAIL)
4402 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 }
4404 break;
4405
4406 case ISN_ADDLIST:
4407 case ISN_ADDBLOB:
4408 {
4409 typval_T *tv1 = STACK_TV_BOT(-2);
4410 typval_T *tv2 = STACK_TV_BOT(-1);
4411
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004412 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004414 {
4415 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4416 && tv1->vval.v_list != NULL)
4417 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4418 NULL);
4419 else
4420 eval_addlist(tv1, tv2);
4421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 else
4423 eval_addblob(tv1, tv2);
4424 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004425 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 }
4427 break;
4428
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004429 case ISN_LISTAPPEND:
4430 {
4431 typval_T *tv1 = STACK_TV_BOT(-2);
4432 typval_T *tv2 = STACK_TV_BOT(-1);
4433 list_T *l = tv1->vval.v_list;
4434
4435 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004436 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004437 if (l == NULL)
4438 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004439 emsg(_(e_cannot_add_to_null_list));
4440 goto on_error;
4441 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004442 if (value_check_lock(l->lv_lock, NULL, FALSE))
4443 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004444 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004445 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004446 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004447 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004448 }
4449 break;
4450
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004451 case ISN_BLOBAPPEND:
4452 {
4453 typval_T *tv1 = STACK_TV_BOT(-2);
4454 typval_T *tv2 = STACK_TV_BOT(-1);
4455 blob_T *b = tv1->vval.v_blob;
4456 int error = FALSE;
4457 varnumber_T n;
4458
4459 // add a number to a blob
4460 if (b == NULL)
4461 {
4462 SOURCING_LNUM = iptr->isn_lnum;
4463 emsg(_(e_cannot_add_to_null_blob));
4464 goto on_error;
4465 }
4466 n = tv_get_number_chk(tv2, &error);
4467 if (error)
4468 goto on_error;
4469 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004470 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004471 }
4472 break;
4473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 // Computation with two arguments of unknown type
4475 case ISN_OPANY:
4476 {
4477 typval_T *tv1 = STACK_TV_BOT(-2);
4478 typval_T *tv2 = STACK_TV_BOT(-1);
4479 varnumber_T n1, n2;
4480#ifdef FEAT_FLOAT
4481 float_T f1 = 0, f2 = 0;
4482#endif
4483 int error = FALSE;
4484
4485 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4486 {
4487 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4488 {
4489 eval_addlist(tv1, tv2);
4490 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004491 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 break;
4493 }
4494 else if (tv1->v_type == VAR_BLOB
4495 && tv2->v_type == VAR_BLOB)
4496 {
4497 eval_addblob(tv1, tv2);
4498 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004499 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 break;
4501 }
4502 }
4503#ifdef FEAT_FLOAT
4504 if (tv1->v_type == VAR_FLOAT)
4505 {
4506 f1 = tv1->vval.v_float;
4507 n1 = 0;
4508 }
4509 else
4510#endif
4511 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004512 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 n1 = tv_get_number_chk(tv1, &error);
4514 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004515 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516#ifdef FEAT_FLOAT
4517 if (tv2->v_type == VAR_FLOAT)
4518 f1 = n1;
4519#endif
4520 }
4521#ifdef FEAT_FLOAT
4522 if (tv2->v_type == VAR_FLOAT)
4523 {
4524 f2 = tv2->vval.v_float;
4525 n2 = 0;
4526 }
4527 else
4528#endif
4529 {
4530 n2 = tv_get_number_chk(tv2, &error);
4531 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004532 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533#ifdef FEAT_FLOAT
4534 if (tv1->v_type == VAR_FLOAT)
4535 f2 = n2;
4536#endif
4537 }
4538#ifdef FEAT_FLOAT
4539 // if there is a float on either side the result is a float
4540 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4541 {
4542 switch (iptr->isn_arg.op.op_type)
4543 {
4544 case EXPR_MULT: f1 = f1 * f2; break;
4545 case EXPR_DIV: f1 = f1 / f2; break;
4546 case EXPR_SUB: f1 = f1 - f2; break;
4547 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004548 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004549 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004550 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 }
4552 clear_tv(tv1);
4553 clear_tv(tv2);
4554 tv1->v_type = VAR_FLOAT;
4555 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004556 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 }
4558 else
4559#endif
4560 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004561 int failed = FALSE;
4562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 switch (iptr->isn_arg.op.op_type)
4564 {
4565 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004566 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4567 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004568 goto on_error;
4569 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 case EXPR_SUB: n1 = n1 - n2; break;
4571 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004572 default: n1 = num_modulus(n1, n2, &failed);
4573 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004574 goto on_error;
4575 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 }
4577 clear_tv(tv1);
4578 clear_tv(tv2);
4579 tv1->v_type = VAR_NUMBER;
4580 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004581 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 }
4583 }
4584 break;
4585
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004586 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004587 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004588 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004589 int is_slice = iptr->isn_type == ISN_STRSLICE;
4590 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004591 char_u *res;
4592
4593 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004594 // string slice: string is at stack-3, first index at
4595 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004596 if (is_slice)
4597 {
4598 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004599 n1 = tv->vval.v_number;
4600 }
4601
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004602 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004603 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004604
Bram Moolenaar4c137212021-04-19 16:48:48 +02004605 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004606 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004607 if (is_slice)
4608 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004609 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004610 else
4611 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004612 // single character (including composing characters).
4613 // If the index is too big or negative the result is
4614 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004615 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004616 vim_free(tv->vval.v_string);
4617 tv->vval.v_string = res;
4618 }
4619 break;
4620
4621 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004622 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004623 case ISN_BLOBINDEX:
4624 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004626 int is_slice = iptr->isn_type == ISN_LISTSLICE
4627 || iptr->isn_type == ISN_BLOBSLICE;
4628 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4629 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004630 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004631 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632
4633 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004634 // list slice: list is at stack-3, indexes at stack-2 and
4635 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004636 // Same for blob.
4637 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638
4639 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004640 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004642
4643 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 {
Bram Moolenaared591872020-08-15 22:14:53 +02004645 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004646 n1 = tv->vval.v_number;
4647 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 }
Bram Moolenaared591872020-08-15 22:14:53 +02004649
Bram Moolenaar4c137212021-04-19 16:48:48 +02004650 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004651 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004652 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004653 if (is_blob)
4654 {
4655 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4656 n1, n2, FALSE, tv) == FAIL)
4657 goto on_error;
4658 }
4659 else
4660 {
4661 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4662 n1, n2, FALSE, tv, TRUE) == FAIL)
4663 goto on_error;
4664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 }
4666 break;
4667
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004668 case ISN_ANYINDEX:
4669 case ISN_ANYSLICE:
4670 {
4671 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4672 typval_T *var1, *var2;
4673 int res;
4674
4675 // index: composite is at stack-2, index at stack-1
4676 // slice: composite is at stack-3, indexes at stack-2 and
4677 // stack-1
4678 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004679 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004680 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4681 goto on_error;
4682 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4683 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004684 res = eval_index_inner(tv, is_slice, var1, var2,
4685 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004686 clear_tv(var1);
4687 if (is_slice)
4688 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004689 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004690 if (res == FAIL)
4691 goto on_error;
4692 }
4693 break;
4694
Bram Moolenaar9af78762020-06-16 11:34:42 +02004695 case ISN_SLICE:
4696 {
4697 list_T *list;
4698 int count = iptr->isn_arg.number;
4699
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004700 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004701 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004702 list = tv->vval.v_list;
4703
4704 // no error for short list, expect it to be checked earlier
4705 if (list != NULL && list->lv_len >= count)
4706 {
4707 list_T *newlist = list_slice(list,
4708 count, list->lv_len - 1);
4709
4710 if (newlist != NULL)
4711 {
4712 list_unref(list);
4713 tv->vval.v_list = newlist;
4714 ++newlist->lv_refcount;
4715 }
4716 }
4717 }
4718 break;
4719
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004720 case ISN_GETITEM:
4721 {
4722 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004723 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004724
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004725 // Get list item: list is at stack-1, push item.
4726 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004727 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4728 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004729
Bram Moolenaar35578162021-08-02 19:10:38 +02004730 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004731 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004732 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004733 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004734
4735 // Useful when used in unpack assignment. Reset at
4736 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004737 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004738 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004739 }
4740 break;
4741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 case ISN_MEMBER:
4743 {
4744 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004745 char_u *key;
4746 dictitem_T *di;
4747
4748 // dict member: dict is at stack-2, key at stack-1
4749 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004750 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004751 dict = tv->vval.v_dict;
4752
4753 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004754 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004755 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004756 if (key == NULL)
4757 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004758
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004759 if ((di = dict_find(dict, key, -1)) == NULL)
4760 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004761 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004762 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004763
4764 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004765 // stack contents makes sense and the dict stack is
4766 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004767 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004768 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004769 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004770 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004771 tv->v_type = VAR_NUMBER;
4772 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004773 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004774 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004775 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004776 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004777 // Put the dict used on the dict stack, it might be used by
4778 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004779 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004780 if (dict_stack_save(tv) == FAIL)
4781 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004782 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004783 }
4784 break;
4785
4786 // dict member with string key
4787 case ISN_STRINGMEMBER:
4788 {
4789 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 dictitem_T *di;
4791
4792 tv = STACK_TV_BOT(-1);
4793 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4794 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004795 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004796 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004797 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 }
4799 dict = tv->vval.v_dict;
4800
4801 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4802 == NULL)
4803 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004804 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004805 semsg(_(e_key_not_present_in_dictionary),
4806 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004807 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004809 // Put the dict used on the dict stack, it might be used by
4810 // a dict function later.
4811 if (dict_stack_save(tv) == FAIL)
4812 goto on_fatal_error;
4813
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004815 }
4816 break;
4817
4818 case ISN_CLEARDICT:
4819 dict_stack_drop();
4820 break;
4821
4822 case ISN_USEDICT:
4823 {
4824 typval_T *dict_tv = dict_stack_get_tv();
4825
4826 // Turn "dict.Func" into a partial for "Func" bound to
4827 // "dict". Don't do this when "Func" is already a partial
4828 // that was bound explicitly (pt_auto is FALSE).
4829 tv = STACK_TV_BOT(-1);
4830 if (dict_tv != NULL
4831 && dict_tv->v_type == VAR_DICT
4832 && dict_tv->vval.v_dict != NULL
4833 && (tv->v_type == VAR_FUNC
4834 || (tv->v_type == VAR_PARTIAL
4835 && (tv->vval.v_partial->pt_auto
4836 || tv->vval.v_partial->pt_dict == NULL))))
4837 dict_tv->vval.v_dict =
4838 make_partial(dict_tv->vval.v_dict, tv);
4839 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 }
4841 break;
4842
4843 case ISN_NEGATENR:
4844 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004845 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004846#ifdef FEAT_FLOAT
4847 if (tv->v_type == VAR_FLOAT)
4848 tv->vval.v_float = -tv->vval.v_float;
4849 else
4850#endif
4851 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 break;
4853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854 case ISN_CHECKTYPE:
4855 {
4856 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004857 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004858 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004860 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004861 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004862 if (!ectx->ec_where.wt_variable)
4863 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004864 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004865 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004866 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004867 if (r == FAIL)
4868 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004869 if (!ectx->ec_where.wt_variable)
4870 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004871
4872 // number 0 is FALSE, number 1 is TRUE
4873 if (tv->v_type == VAR_NUMBER
4874 && ct->ct_type->tt_type == VAR_BOOL
4875 && (tv->vval.v_number == 0
4876 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004878 tv->v_type = VAR_BOOL;
4879 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004880 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 }
4882 }
4883 break;
4884
Bram Moolenaar9af78762020-06-16 11:34:42 +02004885 case ISN_CHECKLEN:
4886 {
4887 int min_len = iptr->isn_arg.checklen.cl_min_len;
4888 list_T *list = NULL;
4889
4890 tv = STACK_TV_BOT(-1);
4891 if (tv->v_type == VAR_LIST)
4892 list = tv->vval.v_list;
4893 if (list == NULL || list->lv_len < min_len
4894 || (list->lv_len > min_len
4895 && !iptr->isn_arg.checklen.cl_more_OK))
4896 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004897 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004898 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004899 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004900 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004901 }
4902 }
4903 break;
4904
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004905 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004906 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004907 break;
4908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004910 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 {
4912 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004913 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004915 if (iptr->isn_type == ISN_2BOOL)
4916 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004917 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004918 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004919 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004920 n = !n;
4921 }
4922 else
4923 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004924 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004925 SOURCING_LNUM = iptr->isn_lnum;
4926 n = tv_get_bool_chk(tv, &error);
4927 if (error)
4928 goto on_error;
4929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 clear_tv(tv);
4931 tv->v_type = VAR_BOOL;
4932 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4933 }
4934 break;
4935
4936 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004937 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004938 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004939 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4940 iptr->isn_type == ISN_2STRING_ANY,
4941 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004942 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 break;
4944
Bram Moolenaar08597872020-12-10 19:43:40 +01004945 case ISN_RANGE:
4946 {
4947 exarg_T ea;
4948 char *errormsg;
4949
Bram Moolenaarece0b872021-01-08 20:40:45 +01004950 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004951 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004952 ea.addr_type = ADDR_LINES;
4953 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004954 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004955 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004956 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004957
Bram Moolenaar35578162021-08-02 19:10:38 +02004958 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004959 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004960 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004961 tv = STACK_TV_BOT(-1);
4962 tv->v_type = VAR_NUMBER;
4963 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004964 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004965 }
4966 break;
4967
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004968 case ISN_PUT:
4969 {
4970 int regname = iptr->isn_arg.put.put_regname;
4971 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4972 char_u *expr = NULL;
4973 int dir = FORWARD;
4974
Bram Moolenaar08597872020-12-10 19:43:40 +01004975 if (lnum < -2)
4976 {
4977 // line number was put on the stack by ISN_RANGE
4978 tv = STACK_TV_BOT(-1);
4979 curwin->w_cursor.lnum = tv->vval.v_number;
4980 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4981 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004982 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004983 }
4984 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004985 // :put! above cursor
4986 dir = BACKWARD;
4987 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004988 {
4989 curwin->w_cursor.lnum = lnum;
4990 if (lnum == 0)
4991 // check_cursor() below will move to line 1
4992 dir = BACKWARD;
4993 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004994
4995 if (regname == '=')
4996 {
4997 tv = STACK_TV_BOT(-1);
4998 if (tv->v_type == VAR_STRING)
4999 expr = tv->vval.v_string;
5000 else
5001 {
5002 expr = typval2string(tv, TRUE); // allocates value
5003 clear_tv(tv);
5004 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005005 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005006 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005007 check_cursor();
5008 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5009 vim_free(expr);
5010 }
5011 break;
5012
Bram Moolenaar02194d22020-10-24 23:08:38 +02005013 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005014 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5015 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5016 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5017 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005018 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5019 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005020 break;
5021
Bram Moolenaar02194d22020-10-24 23:08:38 +02005022 case ISN_CMDMOD_REV:
5023 // filter regprog is owned by the instruction, don't free it
5024 cmdmod.cmod_filter_regmatch.regprog = NULL;
5025 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005026 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5027 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005028 break;
5029
Bram Moolenaar792f7862020-11-23 08:31:18 +01005030 case ISN_UNPACK:
5031 {
5032 int count = iptr->isn_arg.unpack.unp_count;
5033 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5034 list_T *l;
5035 listitem_T *li;
5036 int i;
5037
5038 // Check there is a valid list to unpack.
5039 tv = STACK_TV_BOT(-1);
5040 if (tv->v_type != VAR_LIST)
5041 {
5042 SOURCING_LNUM = iptr->isn_lnum;
5043 emsg(_(e_for_argument_must_be_sequence_of_lists));
5044 goto on_error;
5045 }
5046 l = tv->vval.v_list;
5047 if (l == NULL
5048 || l->lv_len < (semicolon ? count - 1 : count))
5049 {
5050 SOURCING_LNUM = iptr->isn_lnum;
5051 emsg(_(e_list_value_does_not_have_enough_items));
5052 goto on_error;
5053 }
5054 else if (!semicolon && l->lv_len > count)
5055 {
5056 SOURCING_LNUM = iptr->isn_lnum;
5057 emsg(_(e_list_value_has_more_items_than_targets));
5058 goto on_error;
5059 }
5060
5061 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005062 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005063 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005064 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005065
5066 // Variable after semicolon gets a list with the remaining
5067 // items.
5068 if (semicolon)
5069 {
5070 list_T *rem_list =
5071 list_alloc_with_items(l->lv_len - count + 1);
5072
5073 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005074 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005075 tv = STACK_TV_BOT(-count);
5076 tv->vval.v_list = rem_list;
5077 ++rem_list->lv_refcount;
5078 tv->v_lock = 0;
5079 li = l->lv_first;
5080 for (i = 0; i < count - 1; ++i)
5081 li = li->li_next;
5082 for (i = 0; li != NULL; ++i)
5083 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005084 typval_T tvcopy;
5085
5086 copy_tv(&li->li_tv, &tvcopy);
5087 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005088 li = li->li_next;
5089 }
5090 --count;
5091 }
5092
5093 // Produce the values in reverse order, first item last.
5094 li = l->lv_first;
5095 for (i = 0; i < count; ++i)
5096 {
5097 tv = STACK_TV_BOT(-i - 1);
5098 copy_tv(&li->li_tv, tv);
5099 li = li->li_next;
5100 }
5101
5102 list_unref(l);
5103 }
5104 break;
5105
Bram Moolenaarb2049902021-01-24 12:53:53 +01005106 case ISN_PROF_START:
5107 case ISN_PROF_END:
5108 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005109#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005110 funccall_T cookie;
5111 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005112 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005113 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005114
Bram Moolenaarca16c602022-09-06 18:57:08 +01005115 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005116 if (iptr->isn_type == ISN_PROF_START)
5117 {
5118 func_line_start(&cookie, iptr->isn_lnum);
5119 // if we get here the instruction is executed
5120 func_line_exec(&cookie);
5121 }
5122 else
5123 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005124#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005125 }
5126 break;
5127
Bram Moolenaare99d4222021-06-13 14:01:26 +02005128 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005129 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005130 break;
5131
Bram Moolenaar389df252020-07-09 21:20:47 +02005132 case ISN_SHUFFLE:
5133 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005134 typval_T tmp_tv;
5135 int item = iptr->isn_arg.shuffle.shfl_item;
5136 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005137
5138 tmp_tv = *STACK_TV_BOT(-item);
5139 for ( ; up > 0 && item > 1; --up)
5140 {
5141 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5142 --item;
5143 }
5144 *STACK_TV_BOT(-item) = tmp_tv;
5145 }
5146 break;
5147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005149 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005150 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005151 ectx->ec_where.wt_index = 0;
5152 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153 break;
5154 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005155 continue;
5156
Bram Moolenaard032f342020-07-18 18:13:02 +02005157func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005158 // Restore previous function. If the frame pointer is where we started
5159 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005160 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005161 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005162
Bram Moolenaar4c137212021-04-19 16:48:48 +02005163 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005164 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005165 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005166 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005167
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005168on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005169 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005170 // If "emsg_silent" is set then ignore the error, unless it was set
5171 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005172 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005173 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005174 {
5175 // If a sequence of instructions causes an error while ":silent!"
5176 // was used, restore the stack length and jump ahead to restoring
5177 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005178 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005179 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005180 while (ectx->ec_stack.ga_len
5181 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005182 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005183 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005184 clear_tv(STACK_TV_BOT(0));
5185 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005186 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5187 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005188 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005189 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005190 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005191on_fatal_error:
5192 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005193 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005194 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005195 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196 }
5197
5198done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005199 ret = OK;
5200theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005201 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005202
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005203 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005204 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5205 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005206}
5207
5208/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005209 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005210 * "rettv".
5211 * Return OK or FAIL.
5212 */
5213 int
5214exe_typval_instr(typval_T *tv, typval_T *rettv)
5215{
5216 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5217 isn_T *save_instr = ectx->ec_instr;
5218 int save_iidx = ectx->ec_iidx;
5219 int res;
5220
LemonBoyf3b48952022-05-05 13:53:03 +01005221 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5222 // even when the compilation fails.
5223 rettv->v_type = VAR_UNKNOWN;
5224
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005225 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5226 res = exec_instructions(ectx);
5227 if (res == OK)
5228 {
5229 *rettv = *STACK_TV_BOT(-1);
5230 --ectx->ec_stack.ga_len;
5231 }
5232
5233 ectx->ec_instr = save_instr;
5234 ectx->ec_iidx = save_iidx;
5235
5236 return res;
5237}
5238
5239/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005240 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5241 * "substitute_instr".
5242 */
5243 char_u *
5244exe_substitute_instr(void)
5245{
5246 ectx_T *ectx = substitute_instr->subs_ectx;
5247 isn_T *save_instr = ectx->ec_instr;
5248 int save_iidx = ectx->ec_iidx;
5249 char_u *res;
5250
5251 ectx->ec_instr = substitute_instr->subs_instr;
5252 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005253 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005254 typval_T *tv = STACK_TV_BOT(-1);
5255
Bram Moolenaar27523602021-06-05 21:36:19 +02005256 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005257 --ectx->ec_stack.ga_len;
5258 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005259 }
5260 else
5261 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005262 substitute_instr->subs_status = FAIL;
5263 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005264 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265
Bram Moolenaar4c137212021-04-19 16:48:48 +02005266 ectx->ec_instr = save_instr;
5267 ectx->ec_iidx = save_iidx;
5268
5269 return res;
5270}
5271
5272/*
5273 * Call a "def" function from old Vim script.
5274 * Return OK or FAIL.
5275 */
5276 int
5277call_def_function(
5278 ufunc_T *ufunc,
5279 int argc_arg, // nr of arguments
5280 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005281 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005282 partial_T *partial, // optional partial for context
Bram Moolenaar58779852022-09-06 18:31:14 +01005283 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005284 typval_T *rettv) // return value
5285{
5286 ectx_T ectx; // execution context
5287 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005288 int partial_argc = partial == NULL
5289 || (flags & DEF_USE_PT_ARGV) == 0
5290 ? 0 : partial->pt_argc;
5291 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005292 typval_T *tv;
5293 int idx;
5294 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005295 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005296 sctx_T save_current_sctx = current_sctx;
5297 int did_emsg_before = did_emsg_cumul + did_emsg;
5298 int save_suppress_errthrow = suppress_errthrow;
5299 msglist_T **saved_msg_list = NULL;
5300 msglist_T *private_msg_list = NULL;
5301 int save_emsg_silent_def = emsg_silent_def;
5302 int save_did_emsg_def = did_emsg_def;
5303 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005304 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005305
5306// Get pointer to item in the stack.
5307#undef STACK_TV
5308#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5309
5310// Get pointer to item at the bottom of the stack, -1 is the bottom.
5311#undef STACK_TV_BOT
5312#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5313
5314// Get pointer to a local variable on the stack. Negative for arguments.
5315#undef STACK_TV_VAR
5316#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5317
5318 if (ufunc->uf_def_status == UF_NOT_COMPILED
5319 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005320 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5321 && compile_def_function(ufunc, FALSE,
5322 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005323 {
5324 if (did_emsg_cumul + did_emsg == did_emsg_before)
5325 semsg(_(e_function_is_not_compiled_str),
5326 printable_func_name(ufunc));
5327 return FAIL;
5328 }
5329
5330 {
5331 // Check the function was really compiled.
5332 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5333 + ufunc->uf_dfunc_idx;
5334 if (INSTRUCTIONS(dfunc) == NULL)
5335 {
5336 iemsg("using call_def_function() on not compiled function");
5337 return FAIL;
5338 }
5339 }
5340
5341 // If depth of calling is getting too high, don't execute the function.
5342 orig_funcdepth = funcdepth_get();
5343 if (funcdepth_increment() == FAIL)
5344 return FAIL;
5345
5346 CLEAR_FIELD(ectx);
5347 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5348 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005349 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005350 {
5351 funcdepth_decrement();
5352 return FAIL;
5353 }
5354 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5355 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5356 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005357 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005358
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005359 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005360 if (idx > 0 && ufunc->uf_va_name == NULL)
5361 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005362 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005363 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005364 goto failed_early;
5365 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005366 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005367 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005368 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005369 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005370 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005371 goto failed_early;
5372 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005373
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005374 // Put values from the partial and arguments on the stack, but no more than
5375 // what the function expects. A lambda can be called with more arguments
5376 // than it uses.
5377 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005378 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5379 ++idx)
5380 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005381 int argv_idx = idx - partial_argc;
5382
5383 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005384 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005385 && tv->v_type == VAR_SPECIAL
5386 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005387 {
5388 // Use the default value.
5389 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5390 }
5391 else
5392 {
5393 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5394 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005395 ufunc->uf_arg_types[idx], tv,
5396 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005397 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005398 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005399 }
5400 ++ectx.ec_stack.ga_len;
5401 }
5402
5403 // Turn varargs into a list. Empty list if no args.
5404 if (ufunc->uf_va_name != NULL)
5405 {
5406 int vararg_count = argc - ufunc->uf_args.ga_len;
5407
5408 if (vararg_count < 0)
5409 vararg_count = 0;
5410 else
5411 argc -= vararg_count;
5412 if (exe_newlist(vararg_count, &ectx) == FAIL)
5413 goto failed_early;
5414
5415 // Check the type of the list items.
5416 tv = STACK_TV_BOT(-1);
5417 if (ufunc->uf_va_type != NULL
5418 && ufunc->uf_va_type != &t_list_any
5419 && ufunc->uf_va_type->tt_member != &t_any
5420 && tv->vval.v_list != NULL)
5421 {
5422 type_T *expected = ufunc->uf_va_type->tt_member;
5423 listitem_T *li = tv->vval.v_list->lv_first;
5424
5425 for (idx = 0; idx < vararg_count; ++idx)
5426 {
5427 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005428 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005429 goto failed_early;
5430 li = li->li_next;
5431 }
5432 }
5433
5434 if (defcount > 0)
5435 // Move varargs list to below missing default arguments.
5436 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5437 --ectx.ec_stack.ga_len;
5438 }
5439
5440 // Make space for omitted arguments, will store default value below.
5441 // Any varargs list goes after them.
5442 if (defcount > 0)
5443 for (idx = 0; idx < defcount; ++idx)
5444 {
5445 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5446 ++ectx.ec_stack.ga_len;
5447 }
5448 if (ufunc->uf_va_name != NULL)
5449 ++ectx.ec_stack.ga_len;
5450
5451 // Frame pointer points to just after arguments.
5452 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5453 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5454
5455 {
5456 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5457 + ufunc->uf_dfunc_idx;
5458 ufunc_T *base_ufunc = dfunc->df_ufunc;
5459
5460 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5461 // by copy_func().
5462 if (partial != NULL || base_ufunc->uf_partial != NULL)
5463 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005464 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5465 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005466 goto failed_early;
5467 if (partial != NULL)
5468 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005469 outer_T *outer = get_pt_outer(partial);
5470
5471 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005472 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005473 if (current_ectx != NULL)
5474 {
5475 if (current_ectx->ec_outer_ref != NULL
5476 && current_ectx->ec_outer_ref->or_outer != NULL)
5477 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005478 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005479 }
5480 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005481 }
5482 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005483 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005484 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005485 ++partial->pt_refcount;
5486 ectx.ec_outer_ref->or_partial = partial;
5487 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005488 }
5489 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005490 {
5491 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5492 ++base_ufunc->uf_partial->pt_refcount;
5493 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5494 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005495 }
5496 }
5497
5498 // dummy frame entries
5499 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5500 {
5501 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5502 ++ectx.ec_stack.ga_len;
5503 }
5504
5505 {
5506 // Reserve space for local variables and any closure reference count.
5507 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5508 + ufunc->uf_dfunc_idx;
5509
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005510 // Initialize variables to zero. That avoids having to generate
5511 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005512 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005513 {
5514 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5515 STACK_TV_VAR(idx)->vval.v_number = 0;
5516 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005517 ectx.ec_stack.ga_len += dfunc->df_varcount;
5518 if (dfunc->df_has_closure)
5519 {
5520 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5521 STACK_TV_VAR(idx)->vval.v_number = 0;
5522 ++ectx.ec_stack.ga_len;
5523 }
5524
5525 ectx.ec_instr = INSTRUCTIONS(dfunc);
5526 }
5527
Bram Moolenaar58779852022-09-06 18:31:14 +01005528 // Store the execution context in funccal, used by invoke_all_defer().
5529 if (funccal != NULL)
5530 funccal->fc_ectx = &ectx;
5531
Bram Moolenaar4c137212021-04-19 16:48:48 +02005532 // Following errors are in the function, not the caller.
5533 // Commands behave like vim9script.
5534 estack_push_ufunc(ufunc, 1);
5535 current_sctx = ufunc->uf_script_ctx;
5536 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5537
5538 // Use a specific location for storing error messages to be converted to an
5539 // exception.
5540 saved_msg_list = msg_list;
5541 msg_list = &private_msg_list;
5542
5543 // Do turn errors into exceptions.
5544 suppress_errthrow = FALSE;
5545
5546 // Do not delete the function while executing it.
5547 ++ufunc->uf_calls;
5548
5549 // When ":silent!" was used before calling then we still abort the
5550 // function. If ":silent!" is used in the function then we don't.
5551 emsg_silent_def = emsg_silent;
5552 did_emsg_def = 0;
5553
5554 ectx.ec_where.wt_index = 0;
5555 ectx.ec_where.wt_variable = FALSE;
5556
5557 // Execute the instructions until done.
5558 ret = exec_instructions(&ectx);
5559 if (ret == OK)
5560 {
5561 // function finished, get result from the stack.
5562 if (ufunc->uf_ret_type == &t_void)
5563 {
5564 rettv->v_type = VAR_VOID;
5565 }
5566 else
5567 {
5568 tv = STACK_TV_BOT(-1);
5569 *rettv = *tv;
5570 tv->v_type = VAR_UNKNOWN;
5571 }
5572 }
5573
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005574 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005575 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005576
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005577 // Deal with any remaining closures, they may be in use somewhere.
5578 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005579 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005580 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005581 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005582 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005583
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005584 estack_pop();
5585 current_sctx = save_current_sctx;
5586
Bram Moolenaar2336c372021-12-06 15:06:54 +00005587 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5588 // Function was unreferenced while being used, free it now.
5589 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005590
Bram Moolenaar352134b2020-10-17 22:04:08 +02005591 if (*msg_list != NULL && saved_msg_list != NULL)
5592 {
5593 msglist_T **plist = saved_msg_list;
5594
5595 // Append entries from the current msg_list (uncaught exceptions) to
5596 // the saved msg_list.
5597 while (*plist != NULL)
5598 plist = &(*plist)->next;
5599
5600 *plist = *msg_list;
5601 }
5602 msg_list = saved_msg_list;
5603
Bram Moolenaar4c137212021-04-19 16:48:48 +02005604 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005605 {
5606 cmdmod.cmod_filter_regmatch.regprog = NULL;
5607 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005608 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005609 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005610 emsg_silent_def = save_emsg_silent_def;
5611 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005612
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005613failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005614 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005615 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5616 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005617 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005619 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005620 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005621 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005622 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005623 if (ectx.ec_outer_ref->or_outer_allocated)
5624 vim_free(ectx.ec_outer_ref->or_outer);
5625 partial_unref(ectx.ec_outer_ref->or_partial);
5626 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005627 }
5628
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005629 // Not sure if this is necessary.
5630 suppress_errthrow = save_suppress_errthrow;
5631
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005632 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5633 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005634 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005635 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005636 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005637 return ret;
5638}
5639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640/*
Bram Moolenaar58779852022-09-06 18:31:14 +01005641 * Called when a def function has finished (possibly failed).
5642 * Invoke all the function returns to clean up and invoke deferred functions,
5643 * except the toplevel one.
5644 */
5645 void
5646unwind_def_callstack(ectx_T *ectx)
5647{
5648 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
5649 func_return(ectx);
5650}
5651
5652/*
5653 * Invoke any deffered functions for the top function in "ectx".
5654 */
5655 void
5656may_invoke_defer_funcs(ectx_T *ectx)
5657{
5658 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
5659
5660 if (dfunc->df_defer_var_idx > 0)
5661 invoke_defer_funcs(ectx);
5662}
5663
5664/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005665 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5666 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5667 * "pfx" is prefixed to every line.
5668 */
5669 static void
5670list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5671{
5672 int line_idx = 0;
5673 int prev_current = 0;
5674 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005675 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005676
5677 for (current = 0; current < instr_count; ++current)
5678 {
5679 isn_T *iptr = &instr[current];
5680 char *line;
5681
5682 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005683 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005684 while (line_idx < iptr->isn_lnum
5685 && line_idx < ufunc->uf_lines.ga_len)
5686 {
5687 if (current > prev_current)
5688 {
5689 msg_puts("\n\n");
5690 prev_current = current;
5691 }
5692 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5693 if (line != NULL)
5694 msg(line);
5695 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005696 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5697 {
5698 int first_def_arg = ufunc->uf_args.ga_len
5699 - ufunc->uf_def_args.ga_len;
5700
5701 if (def_arg_idx > 0)
5702 msg_puts("\n\n");
5703 msg_start();
5704 msg_puts(" ");
5705 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5706 first_def_arg + def_arg_idx]);
5707 msg_puts(" = ");
5708 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5709 msg_clr_eos();
5710 msg_end();
5711 }
5712 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005713
5714 switch (iptr->isn_type)
5715 {
5716 case ISN_EXEC:
5717 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5718 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005719 case ISN_EXEC_SPLIT:
5720 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5721 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005722 case ISN_EXECRANGE:
5723 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5724 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005725 case ISN_LEGACY_EVAL:
5726 smsg("%s%4d EVAL legacy %s", pfx, current,
5727 iptr->isn_arg.string);
5728 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005729 case ISN_REDIRSTART:
5730 smsg("%s%4d REDIR", pfx, current);
5731 break;
5732 case ISN_REDIREND:
5733 smsg("%s%4d REDIR END%s", pfx, current,
5734 iptr->isn_arg.number ? " append" : "");
5735 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005736 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005737#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005738 smsg("%s%4d CEXPR pre %s", pfx, current,
5739 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005740#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005741 break;
5742 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005743#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005744 {
5745 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5746
5747 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5748 cexpr_get_auname(cer->cer_cmdidx),
5749 cer->cer_forceit ? "!" : "",
5750 cer->cer_cmdline);
5751 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005752#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005753 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005754 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005755 smsg("%s%4d INSTR", pfx, current);
5756 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5757 msg(" -------------");
5758 break;
5759 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005760 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005761 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5762
5763 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005764 }
5765 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005766 case ISN_SUBSTITUTE:
5767 {
5768 subs_T *subs = &iptr->isn_arg.subs;
5769
5770 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5771 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5772 msg(" -------------");
5773 }
5774 break;
5775 case ISN_EXECCONCAT:
5776 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5777 (varnumber_T)iptr->isn_arg.number);
5778 break;
5779 case ISN_ECHO:
5780 {
5781 echo_T *echo = &iptr->isn_arg.echo;
5782
5783 smsg("%s%4d %s %d", pfx, current,
5784 echo->echo_with_white ? "ECHO" : "ECHON",
5785 echo->echo_count);
5786 }
5787 break;
5788 case ISN_EXECUTE:
5789 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005790 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005791 break;
5792 case ISN_ECHOMSG:
5793 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005794 (varnumber_T)(iptr->isn_arg.number));
5795 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01005796 case ISN_ECHOWINDOW:
5797 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
5798 (varnumber_T)(iptr->isn_arg.number));
5799 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02005800 case ISN_ECHOCONSOLE:
5801 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5802 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005803 break;
5804 case ISN_ECHOERR:
5805 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005806 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005807 break;
5808 case ISN_LOAD:
5809 {
5810 if (iptr->isn_arg.number < 0)
5811 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5812 (varnumber_T)(iptr->isn_arg.number
5813 + STACK_FRAME_SIZE));
5814 else
5815 smsg("%s%4d LOAD $%lld", pfx, current,
5816 (varnumber_T)(iptr->isn_arg.number));
5817 }
5818 break;
5819 case ISN_LOADOUTER:
5820 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005821 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005822 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5823 iptr->isn_arg.outer.outer_depth,
5824 iptr->isn_arg.outer.outer_idx
5825 + STACK_FRAME_SIZE);
5826 else
5827 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5828 iptr->isn_arg.outer.outer_depth,
5829 iptr->isn_arg.outer.outer_idx);
5830 }
5831 break;
5832 case ISN_LOADV:
5833 smsg("%s%4d LOADV v:%s", pfx, current,
5834 get_vim_var_name(iptr->isn_arg.number));
5835 break;
5836 case ISN_LOADSCRIPT:
5837 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005838 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5839 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5840 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005841
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005842 sv = get_script_svar(sref, -1);
5843 if (sv == NULL)
5844 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5845 pfx, current, si->sn_name);
5846 else
5847 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005848 sv->sv_name,
5849 sref->sref_idx,
5850 si->sn_name);
5851 }
5852 break;
5853 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005854 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005855 {
5856 scriptitem_T *si = SCRIPT_ITEM(
5857 iptr->isn_arg.loadstore.ls_sid);
5858
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005859 smsg("%s%4d %s s:%s from %s", pfx, current,
5860 iptr->isn_type == ISN_LOADS ? "LOADS"
5861 : "LOADEXPORT",
5862 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005863 }
5864 break;
5865 case ISN_LOADAUTO:
5866 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5867 break;
5868 case ISN_LOADG:
5869 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5870 break;
5871 case ISN_LOADB:
5872 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5873 break;
5874 case ISN_LOADW:
5875 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5876 break;
5877 case ISN_LOADT:
5878 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5879 break;
5880 case ISN_LOADGDICT:
5881 smsg("%s%4d LOAD g:", pfx, current);
5882 break;
5883 case ISN_LOADBDICT:
5884 smsg("%s%4d LOAD b:", pfx, current);
5885 break;
5886 case ISN_LOADWDICT:
5887 smsg("%s%4d LOAD w:", pfx, current);
5888 break;
5889 case ISN_LOADTDICT:
5890 smsg("%s%4d LOAD t:", pfx, current);
5891 break;
5892 case ISN_LOADOPT:
5893 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5894 break;
5895 case ISN_LOADENV:
5896 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5897 break;
5898 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005899 smsg("%s%4d LOADREG @%c", pfx, current,
5900 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005901 break;
5902
5903 case ISN_STORE:
5904 if (iptr->isn_arg.number < 0)
5905 smsg("%s%4d STORE arg[%lld]", pfx, current,
5906 iptr->isn_arg.number + STACK_FRAME_SIZE);
5907 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005908 smsg("%s%4d STORE $%lld", pfx, current,
5909 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005910 break;
5911 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005912 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5913 iptr->isn_arg.outer.outer_depth,
5914 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005915 break;
5916 case ISN_STOREV:
5917 smsg("%s%4d STOREV v:%s", pfx, current,
5918 get_vim_var_name(iptr->isn_arg.number));
5919 break;
5920 case ISN_STOREAUTO:
5921 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5922 break;
5923 case ISN_STOREG:
5924 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5925 break;
5926 case ISN_STOREB:
5927 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5928 break;
5929 case ISN_STOREW:
5930 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5931 break;
5932 case ISN_STORET:
5933 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5934 break;
5935 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005936 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005937 {
5938 scriptitem_T *si = SCRIPT_ITEM(
5939 iptr->isn_arg.loadstore.ls_sid);
5940
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005941 smsg("%s%4d %s %s in %s", pfx, current,
5942 iptr->isn_type == ISN_STORES
5943 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005944 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5945 }
5946 break;
5947 case ISN_STORESCRIPT:
5948 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005949 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5950 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5951 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005952
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005953 sv = get_script_svar(sref, -1);
5954 if (sv == NULL)
5955 smsg("%s%4d STORESCRIPT [deleted] in %s",
5956 pfx, current, si->sn_name);
5957 else
5958 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005959 sv->sv_name,
5960 sref->sref_idx,
5961 si->sn_name);
5962 }
5963 break;
5964 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005965 case ISN_STOREFUNCOPT:
5966 smsg("%s%4d %s &%s", pfx, current,
5967 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005968 iptr->isn_arg.storeopt.so_name);
5969 break;
5970 case ISN_STOREENV:
5971 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5972 break;
5973 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005974 smsg("%s%4d STOREREG @%c", pfx, current,
5975 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005976 break;
5977 case ISN_STORENR:
5978 smsg("%s%4d STORE %lld in $%d", pfx, current,
5979 iptr->isn_arg.storenr.stnr_val,
5980 iptr->isn_arg.storenr.stnr_idx);
5981 break;
5982
5983 case ISN_STOREINDEX:
5984 smsg("%s%4d STOREINDEX %s", pfx, current,
5985 vartype_name(iptr->isn_arg.vartype));
5986 break;
5987
5988 case ISN_STORERANGE:
5989 smsg("%s%4d STORERANGE", pfx, current);
5990 break;
5991
5992 // constants
5993 case ISN_PUSHNR:
5994 smsg("%s%4d PUSHNR %lld", pfx, current,
5995 (varnumber_T)(iptr->isn_arg.number));
5996 break;
5997 case ISN_PUSHBOOL:
5998 case ISN_PUSHSPEC:
5999 smsg("%s%4d PUSH %s", pfx, current,
6000 get_var_special_name(iptr->isn_arg.number));
6001 break;
6002 case ISN_PUSHF:
6003#ifdef FEAT_FLOAT
6004 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
6005#endif
6006 break;
6007 case ISN_PUSHS:
6008 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6009 break;
6010 case ISN_PUSHBLOB:
6011 {
6012 char_u *r;
6013 char_u numbuf[NUMBUFLEN];
6014 char_u *tofree;
6015
6016 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6017 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6018 vim_free(tofree);
6019 }
6020 break;
6021 case ISN_PUSHFUNC:
6022 {
6023 char *name = (char *)iptr->isn_arg.string;
6024
6025 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6026 name == NULL ? "[none]" : name);
6027 }
6028 break;
6029 case ISN_PUSHCHANNEL:
6030#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006031 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006032#endif
6033 break;
6034 case ISN_PUSHJOB:
6035#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006036 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006037#endif
6038 break;
6039 case ISN_PUSHEXC:
6040 smsg("%s%4d PUSH v:exception", pfx, current);
6041 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006042 case ISN_AUTOLOAD:
6043 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6044 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006045 case ISN_UNLET:
6046 smsg("%s%4d UNLET%s %s", pfx, current,
6047 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6048 iptr->isn_arg.unlet.ul_name);
6049 break;
6050 case ISN_UNLETENV:
6051 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6052 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6053 iptr->isn_arg.unlet.ul_name);
6054 break;
6055 case ISN_UNLETINDEX:
6056 smsg("%s%4d UNLETINDEX", pfx, current);
6057 break;
6058 case ISN_UNLETRANGE:
6059 smsg("%s%4d UNLETRANGE", pfx, current);
6060 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006061 case ISN_LOCKUNLOCK:
6062 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6063 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006064 case ISN_LOCKCONST:
6065 smsg("%s%4d LOCKCONST", pfx, current);
6066 break;
6067 case ISN_NEWLIST:
6068 smsg("%s%4d NEWLIST size %lld", pfx, current,
6069 (varnumber_T)(iptr->isn_arg.number));
6070 break;
6071 case ISN_NEWDICT:
6072 smsg("%s%4d NEWDICT size %lld", pfx, current,
6073 (varnumber_T)(iptr->isn_arg.number));
6074 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006075 case ISN_NEWPARTIAL:
6076 smsg("%s%4d NEWPARTIAL", pfx, current);
6077 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006078
6079 // function call
6080 case ISN_BCALL:
6081 {
6082 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6083
6084 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6085 internal_func_name(cbfunc->cbf_idx),
6086 cbfunc->cbf_argcount);
6087 }
6088 break;
6089 case ISN_DCALL:
6090 {
6091 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6092 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6093 + cdfunc->cdf_idx;
6094
6095 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006096 printable_func_name(df->df_ufunc),
6097 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006098 }
6099 break;
6100 case ISN_UCALL:
6101 {
6102 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6103
6104 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6105 cufunc->cuf_name, cufunc->cuf_argcount);
6106 }
6107 break;
6108 case ISN_PCALL:
6109 {
6110 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6111
6112 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6113 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6114 }
6115 break;
6116 case ISN_PCALL_END:
6117 smsg("%s%4d PCALL end", pfx, current);
6118 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006119 case ISN_DEFER:
6120 smsg("%s%4d DEFER %d args", pfx, current,
6121 (int)iptr->isn_arg.defer.defer_argcount);
6122 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006123 case ISN_RETURN:
6124 smsg("%s%4d RETURN", pfx, current);
6125 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006126 case ISN_RETURN_VOID:
6127 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006128 break;
6129 case ISN_FUNCREF:
6130 {
6131 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00006132 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006133
Bram Moolenaar38453522021-11-28 22:00:12 +00006134 if (funcref->fr_func_name == NULL)
6135 {
6136 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6137 + funcref->fr_dfunc_idx;
6138 name = df->df_ufunc->uf_name;
6139 }
6140 else
6141 name = funcref->fr_func_name;
6142 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006143 }
6144 break;
6145
6146 case ISN_NEWFUNC:
6147 {
6148 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
6149
6150 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6151 newfunc->nf_lambda, newfunc->nf_global);
6152 }
6153 break;
6154
6155 case ISN_DEF:
6156 {
6157 char_u *name = iptr->isn_arg.string;
6158
6159 smsg("%s%4d DEF %s", pfx, current,
6160 name == NULL ? (char_u *)"" : name);
6161 }
6162 break;
6163
6164 case ISN_JUMP:
6165 {
6166 char *when = "?";
6167
6168 switch (iptr->isn_arg.jump.jump_when)
6169 {
6170 case JUMP_ALWAYS:
6171 when = "JUMP";
6172 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006173 case JUMP_NEVER:
6174 iemsg("JUMP_NEVER should not be used");
6175 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006176 case JUMP_AND_KEEP_IF_TRUE:
6177 when = "JUMP_AND_KEEP_IF_TRUE";
6178 break;
6179 case JUMP_IF_FALSE:
6180 when = "JUMP_IF_FALSE";
6181 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006182 case JUMP_IF_COND_FALSE:
6183 when = "JUMP_IF_COND_FALSE";
6184 break;
6185 case JUMP_IF_COND_TRUE:
6186 when = "JUMP_IF_COND_TRUE";
6187 break;
6188 }
6189 smsg("%s%4d %s -> %d", pfx, current, when,
6190 iptr->isn_arg.jump.jump_where);
6191 }
6192 break;
6193
6194 case ISN_JUMP_IF_ARG_SET:
6195 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6196 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6197 iptr->isn_arg.jump.jump_where);
6198 break;
6199
6200 case ISN_FOR:
6201 {
6202 forloop_T *forloop = &iptr->isn_arg.forloop;
6203
6204 smsg("%s%4d FOR $%d -> %d", pfx, current,
6205 forloop->for_idx, forloop->for_end);
6206 }
6207 break;
6208
6209 case ISN_TRY:
6210 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006211 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006212
6213 if (try->try_ref->try_finally == 0)
6214 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6215 pfx, current,
6216 try->try_ref->try_catch,
6217 try->try_ref->try_endtry);
6218 else
6219 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6220 pfx, current,
6221 try->try_ref->try_catch,
6222 try->try_ref->try_finally,
6223 try->try_ref->try_endtry);
6224 }
6225 break;
6226 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006227 smsg("%s%4d CATCH", pfx, current);
6228 break;
6229 case ISN_TRYCONT:
6230 {
6231 trycont_T *trycont = &iptr->isn_arg.trycont;
6232
6233 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6234 trycont->tct_levels,
6235 trycont->tct_levels == 1 ? "" : "s",
6236 trycont->tct_where);
6237 }
6238 break;
6239 case ISN_FINALLY:
6240 smsg("%s%4d FINALLY", pfx, current);
6241 break;
6242 case ISN_ENDTRY:
6243 smsg("%s%4d ENDTRY", pfx, current);
6244 break;
6245 case ISN_THROW:
6246 smsg("%s%4d THROW", pfx, current);
6247 break;
6248
6249 // expression operations on number
6250 case ISN_OPNR:
6251 case ISN_OPFLOAT:
6252 case ISN_OPANY:
6253 {
6254 char *what;
6255 char *ins;
6256
6257 switch (iptr->isn_arg.op.op_type)
6258 {
6259 case EXPR_MULT: what = "*"; break;
6260 case EXPR_DIV: what = "/"; break;
6261 case EXPR_REM: what = "%"; break;
6262 case EXPR_SUB: what = "-"; break;
6263 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006264 case EXPR_LSHIFT: what = "<<"; break;
6265 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006266 default: what = "???"; break;
6267 }
6268 switch (iptr->isn_type)
6269 {
6270 case ISN_OPNR: ins = "OPNR"; break;
6271 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6272 case ISN_OPANY: ins = "OPANY"; break;
6273 default: ins = "???"; break;
6274 }
6275 smsg("%s%4d %s %s", pfx, current, ins, what);
6276 }
6277 break;
6278
6279 case ISN_COMPAREBOOL:
6280 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006281 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006282 case ISN_COMPARENR:
6283 case ISN_COMPAREFLOAT:
6284 case ISN_COMPARESTRING:
6285 case ISN_COMPAREBLOB:
6286 case ISN_COMPARELIST:
6287 case ISN_COMPAREDICT:
6288 case ISN_COMPAREFUNC:
6289 case ISN_COMPAREANY:
6290 {
6291 char *p;
6292 char buf[10];
6293 char *type;
6294
6295 switch (iptr->isn_arg.op.op_type)
6296 {
6297 case EXPR_EQUAL: p = "=="; break;
6298 case EXPR_NEQUAL: p = "!="; break;
6299 case EXPR_GREATER: p = ">"; break;
6300 case EXPR_GEQUAL: p = ">="; break;
6301 case EXPR_SMALLER: p = "<"; break;
6302 case EXPR_SEQUAL: p = "<="; break;
6303 case EXPR_MATCH: p = "=~"; break;
6304 case EXPR_IS: p = "is"; break;
6305 case EXPR_ISNOT: p = "isnot"; break;
6306 case EXPR_NOMATCH: p = "!~"; break;
6307 default: p = "???"; break;
6308 }
6309 STRCPY(buf, p);
6310 if (iptr->isn_arg.op.op_ic == TRUE)
6311 strcat(buf, "?");
6312 switch(iptr->isn_type)
6313 {
6314 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6315 case ISN_COMPARESPECIAL:
6316 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006317 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006318 case ISN_COMPARENR: type = "COMPARENR"; break;
6319 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6320 case ISN_COMPARESTRING:
6321 type = "COMPARESTRING"; break;
6322 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6323 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6324 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6325 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6326 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6327 default: type = "???"; break;
6328 }
6329
6330 smsg("%s%4d %s %s", pfx, current, type, buf);
6331 }
6332 break;
6333
6334 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6335 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6336
6337 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006338 case ISN_CONCAT:
6339 smsg("%s%4d CONCAT size %lld", pfx, current,
6340 (varnumber_T)(iptr->isn_arg.number));
6341 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006342 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6343 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6344 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6345 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6346 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6347 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6348 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6349 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6350 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6351 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6352 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006353 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006354 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6355 iptr->isn_arg.getitem.gi_index,
6356 iptr->isn_arg.getitem.gi_with_op ?
6357 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006358 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6359 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6360 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006361 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6362 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6363
Bram Moolenaar4c137212021-04-19 16:48:48 +02006364 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6365
Bram Moolenaar4c137212021-04-19 16:48:48 +02006366 case ISN_CHECKTYPE:
6367 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006368 checktype_T *ct = &iptr->isn_arg.type;
6369 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006370
6371 if (ct->ct_arg_idx == 0)
6372 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6373 type_name(ct->ct_type, &tofree),
6374 (int)ct->ct_off);
6375 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006376 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006377 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006378 type_name(ct->ct_type, &tofree),
6379 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006380 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006381 (int)ct->ct_arg_idx);
6382 vim_free(tofree);
6383 break;
6384 }
6385 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6386 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6387 iptr->isn_arg.checklen.cl_min_len);
6388 break;
6389 case ISN_SETTYPE:
6390 {
6391 char *tofree;
6392
6393 smsg("%s%4d SETTYPE %s", pfx, current,
6394 type_name(iptr->isn_arg.type.ct_type, &tofree));
6395 vim_free(tofree);
6396 break;
6397 }
6398 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006399 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6400 smsg("%s%4d INVERT %d (!val)", pfx, current,
6401 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006402 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006403 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6404 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006405 break;
6406 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006407 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006408 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006409 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6410 pfx, current,
6411 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006412 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006413 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6414 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006415 break;
6416 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006417 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006418 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006419 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006420 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6421 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006422 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006423 else
6424 smsg("%s%4d PUT %c %ld", pfx, current,
6425 iptr->isn_arg.put.put_regname,
6426 (long)iptr->isn_arg.put.put_lnum);
6427 break;
6428
Bram Moolenaar4c137212021-04-19 16:48:48 +02006429 case ISN_CMDMOD:
6430 {
6431 char_u *buf;
6432 size_t len = produce_cmdmods(
6433 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6434
6435 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006436 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006437 {
6438 (void)produce_cmdmods(
6439 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6440 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6441 vim_free(buf);
6442 }
6443 break;
6444 }
6445 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6446
6447 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006448 smsg("%s%4d PROFILE START line %d", pfx, current,
6449 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006450 break;
6451
6452 case ISN_PROF_END:
6453 smsg("%s%4d PROFILE END", pfx, current);
6454 break;
6455
Bram Moolenaare99d4222021-06-13 14:01:26 +02006456 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006457 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6458 iptr->isn_arg.debug.dbg_break_lnum + 1,
6459 iptr->isn_lnum,
6460 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006461 break;
6462
Bram Moolenaar4c137212021-04-19 16:48:48 +02006463 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6464 iptr->isn_arg.unpack.unp_count,
6465 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6466 break;
6467 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6468 iptr->isn_arg.shuffle.shfl_item,
6469 iptr->isn_arg.shuffle.shfl_up);
6470 break;
6471 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6472
6473 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6474 return;
6475 }
6476
6477 out_flush(); // output one line at a time
6478 ui_breakcheck();
6479 if (got_int)
6480 break;
6481 }
6482}
6483
6484/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006485 * Handle command line completion for the :disassemble command.
6486 */
6487 void
6488set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6489{
6490 char_u *p;
6491
6492 // Default: expand user functions, "debug" and "profile"
6493 xp->xp_context = EXPAND_DISASSEMBLE;
6494 xp->xp_pattern = arg;
6495
6496 // first argument already typed: only user function names
6497 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6498 {
6499 xp->xp_context = EXPAND_USER_FUNC;
6500 xp->xp_pattern = skipwhite(p);
6501 }
6502}
6503
6504/*
6505 * Function given to ExpandGeneric() to obtain the list of :disassemble
6506 * arguments.
6507 */
6508 char_u *
6509get_disassemble_argument(expand_T *xp, int idx)
6510{
6511 if (idx == 0)
6512 return (char_u *)"debug";
6513 if (idx == 1)
6514 return (char_u *)"profile";
6515 return get_user_func_name(xp, idx - 2);
6516}
6517
6518/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006519 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006520 * We don't really need this at runtime, but we do have tests that require it,
6521 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 */
6523 void
6524ex_disassemble(exarg_T *eap)
6525{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006526 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006527 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006529 isn_T *instr = NULL; // init to shut up gcc warning
6530 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006531 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006532
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006533 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006534 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006535 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006536 if (func_needs_compiling(ufunc, compile_type)
6537 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006538 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006539 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006541 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 return;
6543 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006544 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545
6546 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006547 switch (compile_type)
6548 {
6549 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006550#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006551 instr = dfunc->df_instr_prof;
6552 instr_count = dfunc->df_instr_prof_count;
6553 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006554#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006555 // FALLTHROUGH
6556 case CT_NONE:
6557 instr = dfunc->df_instr;
6558 instr_count = dfunc->df_instr_count;
6559 break;
6560 case CT_DEBUG:
6561 instr = dfunc->df_instr_debug;
6562 instr_count = dfunc->df_instr_debug_count;
6563 break;
6564 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006565
Bram Moolenaar4c137212021-04-19 16:48:48 +02006566 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006567}
6568
6569/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006570 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571 * list, etc. Mostly like what JavaScript does, except that empty list and
6572 * empty dictionary are FALSE.
6573 */
6574 int
6575tv2bool(typval_T *tv)
6576{
6577 switch (tv->v_type)
6578 {
6579 case VAR_NUMBER:
6580 return tv->vval.v_number != 0;
6581 case VAR_FLOAT:
6582#ifdef FEAT_FLOAT
6583 return tv->vval.v_float != 0.0;
6584#else
6585 break;
6586#endif
6587 case VAR_PARTIAL:
6588 return tv->vval.v_partial != NULL;
6589 case VAR_FUNC:
6590 case VAR_STRING:
6591 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6592 case VAR_LIST:
6593 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6594 case VAR_DICT:
6595 return tv->vval.v_dict != NULL
6596 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6597 case VAR_BOOL:
6598 case VAR_SPECIAL:
6599 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6600 case VAR_JOB:
6601#ifdef FEAT_JOB_CHANNEL
6602 return tv->vval.v_job != NULL;
6603#else
6604 break;
6605#endif
6606 case VAR_CHANNEL:
6607#ifdef FEAT_JOB_CHANNEL
6608 return tv->vval.v_channel != NULL;
6609#else
6610 break;
6611#endif
6612 case VAR_BLOB:
6613 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6614 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006615 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006617 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006618 break;
6619 }
6620 return FALSE;
6621}
6622
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006623 void
6624emsg_using_string_as(typval_T *tv, int as_number)
6625{
6626 semsg(_(as_number ? e_using_string_as_number_str
6627 : e_using_string_as_bool_str),
6628 tv->vval.v_string == NULL
6629 ? (char_u *)"" : tv->vval.v_string);
6630}
6631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632/*
6633 * If "tv" is a string give an error and return FAIL.
6634 */
6635 int
6636check_not_string(typval_T *tv)
6637{
6638 if (tv->v_type == VAR_STRING)
6639 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006640 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 clear_tv(tv);
6642 return FAIL;
6643 }
6644 return OK;
6645}
6646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006647
6648#endif // FEAT_EVAL