blob: a1c2f8b27ca70b6f18bdc29fc68ef441df488447 [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 Moolenaarb46c0832022-09-15 17:19:37 +0100507 if (GA_GROW_FAILS(&ectx->ec_stack,
508 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509 return FAIL;
510
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100511 // If depth of calling is getting too high, don't execute the function.
512 if (funcdepth_increment() == FAIL)
513 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200514 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100515
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100516 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200517 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100518 {
519 floc = ALLOC_ONE(funclocal_T);
520 if (floc == NULL)
521 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200522 *floc = ectx->ec_funclocal;
523 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100524 }
525
Bram Moolenaarfe270812020-04-11 22:31:27 +0200526 // Move the vararg-list to below the missing optional arguments.
527 if (vararg_count > 0 && arg_to_add > 0)
528 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100529
530 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200531 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200532 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200533 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534
535 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100536 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
537 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200538 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200539 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
540 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100541 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100542 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200543 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544
545 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200546 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000547 {
548 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
549
550 tv->v_type = VAR_NUMBER;
551 tv->vval.v_number = 0;
552 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200553 if (dfunc->df_has_closure)
554 {
555 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
556
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100557 // Initialize the variable that counts how many closures were created.
558 // This is used in handle_closure_in_use().
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200559 tv->v_type = VAR_NUMBER;
560 tv->vval.v_number = 0;
561 }
562 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100564 if (pt != NULL || ufunc->uf_partial != NULL
565 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100566 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200567 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100568
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200569 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100570 return FAIL;
571 if (pt != NULL)
572 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000573 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200574 ++pt->pt_refcount;
575 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100576 }
577 else if (ufunc->uf_partial != NULL)
578 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000579 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200580 ++ufunc->uf_partial->pt_refcount;
581 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100582 }
583 else
584 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200585 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200586 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200587 {
588 vim_free(ref);
589 return FAIL;
590 }
591 ref->or_outer_allocated = TRUE;
592 ref->or_outer->out_stack = &ectx->ec_stack;
593 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
594 if (ectx->ec_outer_ref != NULL)
595 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100596 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200597 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100598 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100599 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200600 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100601
Bram Moolenaarc970e422021-03-17 15:03:04 +0100602 ++ufunc->uf_calls;
603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604 // Set execution state to the start of the called function.
605 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100606 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100607 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200608 if (entry != NULL)
609 {
610 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200611 // Save the current context so it can be restored on return.
612 entry->es_save_sctx = current_sctx;
613 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200614 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100615
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200616 // Start execution at the first instruction.
617 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618
619 return OK;
620}
621
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000622// Double linked list of funcstack_T in use.
623static funcstack_T *first_funcstack = NULL;
624
625 static void
626add_funcstack_to_list(funcstack_T *funcstack)
627{
628 // Link in list of funcstacks.
629 if (first_funcstack != NULL)
630 first_funcstack->fs_prev = funcstack;
631 funcstack->fs_next = first_funcstack;
632 funcstack->fs_prev = NULL;
633 first_funcstack = funcstack;
634}
635
636 static void
637remove_funcstack_from_list(funcstack_T *funcstack)
638{
639 if (funcstack->fs_prev == NULL)
640 first_funcstack = funcstack->fs_next;
641 else
642 funcstack->fs_prev->fs_next = funcstack->fs_next;
643 if (funcstack->fs_next != NULL)
644 funcstack->fs_next->fs_prev = funcstack->fs_prev;
645}
646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200648 * Used when returning from a function: Check if any closure is still
649 * referenced. If so then move the arguments and variables to a separate piece
650 * of stack to be used when the closure is called.
651 * When "free_arguments" is TRUE the arguments are to be freed.
652 * Returns FAIL when out of memory.
653 */
654 static int
655handle_closure_in_use(ectx_T *ectx, int free_arguments)
656{
657 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
658 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200659 int argcount;
660 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200661 int idx;
662 typval_T *tv;
663 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200664 garray_T *gap = &ectx->ec_funcrefs;
665 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200666
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200667 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200668 return OK; // function was freed
669 if (dfunc->df_has_closure == 0)
670 return OK; // no closures
671 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
672 closure_count = tv->vval.v_number;
673 if (closure_count == 0)
674 return OK; // no funcrefs created
675
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200676 argcount = ufunc_argcount(dfunc->df_ufunc);
677 top = ectx->ec_frame_idx - argcount;
678
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200679 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200680 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200681 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200682 partial_T *pt;
683 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200684
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200685 if (off < 0)
686 continue; // count is off or already done
687 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200688 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200689 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200690 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200691 int i;
692
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000693 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200694 // unreferenced on return.
695 for (i = 0; i < dfunc->df_varcount; ++i)
696 {
697 typval_T *stv = STACK_TV(ectx->ec_frame_idx
698 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200699 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200700 --refcount;
701 }
702 if (refcount > 1)
703 {
704 closure_in_use = TRUE;
705 break;
706 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200707 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200708 }
709
710 if (closure_in_use)
711 {
712 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
713 typval_T *stack;
714
715 // A closure is using the arguments and/or local variables.
716 // Move them to the called function.
717 if (funcstack == NULL)
718 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000719
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200720 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
721 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200722 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
723 funcstack->fs_ga.ga_data = stack;
724 if (stack == NULL)
725 {
726 vim_free(funcstack);
727 return FAIL;
728 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000729 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200730
731 // Move or copy the arguments.
732 for (idx = 0; idx < argcount; ++idx)
733 {
734 tv = STACK_TV(top + idx);
735 if (free_arguments)
736 {
737 *(stack + idx) = *tv;
738 tv->v_type = VAR_UNKNOWN;
739 }
740 else
741 copy_tv(tv, stack + idx);
742 }
743 // Move the local variables.
744 for (idx = 0; idx < dfunc->df_varcount; ++idx)
745 {
746 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200747
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200748 // A partial created for a local function, that is also used as a
749 // local variable, has a reference count for the variable, thus
750 // will never go down to zero. When all these refcounts are one
751 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000752 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200753 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
754 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200755 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200756
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200757 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200758 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
759 gap->ga_len - closure_count + i])
760 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200761 }
762
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200763 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200764 tv->v_type = VAR_UNKNOWN;
765 }
766
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200767 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200768 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200769 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
770 - closure_count + idx];
771 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200772 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200773 ++funcstack->fs_refcount;
774 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100775 pt->pt_outer.out_stack = &funcstack->fs_ga;
776 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200777 }
778 }
779 }
780
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200781 for (idx = 0; idx < closure_count; ++idx)
782 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
783 - closure_count + idx]);
784 gap->ga_len -= closure_count;
785 if (gap->ga_len == 0)
786 ga_clear(gap);
787
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200788 return OK;
789}
790
791/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200792 * Called when a partial is freed or its reference count goes down to one. The
793 * funcstack may be the only reference to the partials in the local variables.
794 * Go over all of them, the funcref and can be freed if all partials
795 * referencing the funcstack have a reference count of one.
796 */
797 void
798funcstack_check_refcount(funcstack_T *funcstack)
799{
800 int i;
801 garray_T *gap = &funcstack->fs_ga;
802 int done = 0;
803
804 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
805 return;
806 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
807 {
808 typval_T *tv = ((typval_T *)gap->ga_data) + i;
809
810 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
811 && tv->vval.v_partial->pt_funcstack == funcstack
812 && tv->vval.v_partial->pt_refcount == 1)
813 ++done;
814 }
815 if (done == funcstack->fs_min_refcount)
816 {
817 typval_T *stack = gap->ga_data;
818
819 // All partials referencing the funcstack have a reference count of
820 // one, thus the funcstack is no longer of use.
821 for (i = 0; i < gap->ga_len; ++i)
822 clear_tv(stack + i);
823 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000824 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200825 vim_free(funcstack);
826 }
827}
828
829/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000830 * For garbage collecting: set references in all variables referenced by
831 * all funcstacks.
832 */
833 int
834set_ref_in_funcstacks(int copyID)
835{
836 funcstack_T *funcstack;
837
838 for (funcstack = first_funcstack; funcstack != NULL;
839 funcstack = funcstack->fs_next)
840 {
841 typval_T *stack = funcstack->fs_ga.ga_data;
842 int i;
843
844 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
845 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
846 return TRUE; // abort
847 }
848 return FALSE;
849}
850
Bram Moolenaar806a2732022-09-04 15:40:36 +0100851// Ugly static to avoid passing the execution context around through many
852// layers.
853static ectx_T *current_ectx = NULL;
854
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000855/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100856 * Return TRUE if currently executing a :def function.
857 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100858 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100859 int
860in_def_function(void)
861{
862 return current_ectx != NULL;
863}
864
865/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100866 * Clear "current_ectx" and return the previous value. To be used when calling
867 * a user function.
868 */
869 ectx_T *
870clear_currrent_ectx(void)
871{
872 ectx_T *r = current_ectx;
873
874 current_ectx = NULL;
875 return r;
876}
877
878 void
879restore_current_ectx(ectx_T *ectx)
880{
881 if (current_ectx != NULL)
882 iemsg("Restoring current_ectx while it is not NULL");
883 current_ectx = ectx;
884}
885
886/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100887 * Add an entry for a deferred function call to the currently executing
888 * function.
889 * Return the list or NULL when failed.
890 */
891 static list_T *
892add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100893{
894 typval_T *defer_tv = STACK_TV_VAR(var_idx);
895 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100896 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100897 typval_T listval;
898
899 if (defer_tv->v_type != VAR_LIST)
900 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100901 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100902 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100903 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100904 }
905 defer_l = defer_tv->vval.v_list;
906
907 l = list_alloc_with_items(argcount + 1);
908 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100909 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100910 listval.v_type = VAR_LIST;
911 listval.vval.v_list = l;
912 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100913 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100914 {
915 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100916 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100917 }
918
Bram Moolenaar806a2732022-09-04 15:40:36 +0100919 return l;
920}
921
922/*
923 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
924 * The local variable that lists deferred functions is "var_idx".
925 * Returns OK or FAIL.
926 */
927 static int
928defer_command(int var_idx, int argcount, ectx_T *ectx)
929{
930 list_T *l = add_defer_item(var_idx, argcount, ectx);
931 int i;
932 typval_T *func_tv;
933
934 if (l == NULL)
935 return FAIL;
936
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100937 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100938 if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL)
939 {
940 semsg(_(e_expected_str_but_got_str),
941 "function or partial",
942 vartype_name(func_tv->v_type));
943 return FAIL;
944 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100945 list_set_item(l, 0, func_tv);
946
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100947 for (i = 0; i < argcount; ++i)
948 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100949 ectx->ec_stack.ga_len -= argcount + 1;
950 return OK;
951}
952
953/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100954 * Add a deferred function "name" with one argument "arg_tv".
955 * Consumes "name", also on failure.
956 * Only to be called when in_def_function() returns TRUE.
957 */
958 int
959add_defer_function(char_u *name, int argcount, typval_T *argvars)
960{
961 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
962 + current_ectx->ec_dfunc_idx;
963 list_T *l;
964 typval_T func_tv;
965 int i;
966
967 if (dfunc->df_defer_var_idx == 0)
968 {
969 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100970 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100971 return FAIL;
972 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100973
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100974 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100975 if (l == NULL)
976 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100977 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100978 return FAIL;
979 }
980
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100981 func_tv.v_type = VAR_FUNC;
982 func_tv.v_lock = 0;
983 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100984 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100985
Bram Moolenaar806a2732022-09-04 15:40:36 +0100986 for (i = 0; i < argcount; ++i)
987 list_set_item(l, i + 1, argvars + i);
988 return OK;
989}
990
991/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100992 * Invoked when returning from a function: Invoke any deferred calls.
993 */
994 static void
995invoke_defer_funcs(ectx_T *ectx)
996{
997 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
998 + ectx->ec_dfunc_idx;
999 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1000 listitem_T *li;
1001
1002 if (defer_tv->v_type != VAR_LIST)
1003 return; // no function added
1004 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1005 {
1006 list_T *l = li->li_tv.vval.v_list;
1007 typval_T rettv;
1008 typval_T argvars[MAX_FUNC_ARGS];
1009 int i;
1010 listitem_T *arg_li = l->lv_first;
1011 funcexe_T funcexe;
1012
1013 for (i = 0; i < l->lv_len - 1; ++i)
1014 {
1015 arg_li = arg_li->li_next;
1016 argvars[i] = arg_li->li_tv;
1017 }
1018
1019 CLEAR_FIELD(funcexe);
1020 funcexe.fe_evaluate = TRUE;
1021 rettv.v_type = VAR_UNKNOWN;
1022 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1023 &rettv, l->lv_len - 1, argvars, &funcexe);
1024 clear_tv(&rettv);
1025 }
1026}
1027
1028/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 * Return from the current function.
1030 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001031 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001032func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001035 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001036 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1037 + ectx->ec_dfunc_idx;
1038 int argcount = ufunc_argcount(dfunc->df_ufunc);
1039 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001040 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001041 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1042 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001043 funclocal_T *floc;
1044#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001045 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1046 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047
Bram Moolenaar12d26532021-02-19 19:13:21 +01001048 if (do_profiling == PROF_YES)
1049 {
1050 ufunc_T *caller = prev_dfunc->df_ufunc;
1051
1052 if (dfunc->df_ufunc->uf_profiling
1053 || (caller != NULL && caller->uf_profiling))
1054 {
1055 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1056 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1057 --profile_info_ga.ga_len;
1058 }
1059 }
1060#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001061
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001062 if (dfunc->df_defer_var_idx > 0)
1063 invoke_defer_funcs(ectx);
1064
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001065 // No check for uf_refcount being zero, cannot think of a way that would
1066 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001067 --dfunc->df_ufunc->uf_calls;
1068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001070 entry = estack_pop();
1071 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001072 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001074 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1075 return FAIL;
1076
1077 // Clear the arguments.
1078 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1079 clear_tv(STACK_TV(idx));
1080
1081 // Clear local variables and temp values, but not the return value.
1082 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001083 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001085
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001086 // The return value should be on top of the stack. However, when aborting
1087 // it may not be there and ec_frame_idx is the top of the stack.
1088 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001089 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001090 ret_idx = 0;
1091
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001092 if (ectx->ec_outer_ref != NULL)
1093 {
1094 if (ectx->ec_outer_ref->or_outer_allocated)
1095 vim_free(ectx->ec_outer_ref->or_outer);
1096 partial_unref(ectx->ec_outer_ref->or_partial);
1097 vim_free(ectx->ec_outer_ref);
1098 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001099
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001100 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001101 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001102 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1103 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001104 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1105 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001106 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001107 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001108 floc = (void *)STACK_TV(ectx->ec_frame_idx
1109 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001110 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001111 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1112 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001113
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001114 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001115 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001116 else
1117 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001118 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001119 vim_free(floc);
1120 }
1121
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001122 if (ret_idx > 0)
1123 {
1124 // Reset the stack to the position before the call, with a spot for the
1125 // return value, moved there from above the frame.
1126 ectx->ec_stack.ga_len = top + 1;
1127 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1128 }
1129 else
1130 // Reset the stack to the position before the call.
1131 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001132
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001133 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001134 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001135 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136}
1137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138/*
1139 * Prepare arguments and rettv for calling a builtin or user function.
1140 */
1141 static int
1142call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1143{
1144 int idx;
1145 typval_T *tv;
1146
1147 // Move arguments from bottom of the stack to argvars[] and add terminator.
1148 for (idx = 0; idx < argcount; ++idx)
1149 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1150 argvars[argcount].v_type = VAR_UNKNOWN;
1151
1152 // Result replaces the arguments on the stack.
1153 if (argcount > 0)
1154 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001155 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 return FAIL;
1157 else
1158 ++ectx->ec_stack.ga_len;
1159
1160 // Default return value is zero.
1161 tv = STACK_TV_BOT(-1);
1162 tv->v_type = VAR_NUMBER;
1163 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001164 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165
1166 return OK;
1167}
1168
1169/*
1170 * Call a builtin function by index.
1171 */
1172 static int
1173call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1174{
1175 typval_T argvars[MAX_FUNC_ARGS];
1176 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001177 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001178 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001179 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180
1181 if (call_prepare(argcount, argvars, ectx) == FAIL)
1182 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001183 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001185 // Call the builtin function. Set "current_ectx" so that when it
1186 // recursively invokes call_def_function() a closure context can be set.
1187 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001189 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001190 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191
1192 // Clear the arguments.
1193 for (idx = 0; idx < argcount; ++idx)
1194 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001195
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001196 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001197 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 return OK;
1199}
1200
1201/*
1202 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001203 * If the function is compiled this will add a stack frame and set the
1204 * instruction pointer at the start of the function.
1205 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001206 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001207 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 */
1209 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001210call_ufunc(
1211 ufunc_T *ufunc,
1212 partial_T *pt,
1213 int argcount,
1214 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001215 isn_T *iptr,
1216 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217{
1218 typval_T argvars[MAX_FUNC_ARGS];
1219 funcexe_T funcexe;
1220 int error;
1221 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001222 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001223 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224
Bram Moolenaare99d4222021-06-13 14:01:26 +02001225 if (func_needs_compiling(ufunc, compile_type)
1226 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1227 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001228 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001229 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001230 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001231 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001232 if (error != FCERR_UNKNOWN)
1233 {
1234 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001235 semsg(_(e_too_many_arguments_for_function_str),
1236 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001237 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001238 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001239 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001240 return FAIL;
1241 }
1242
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001243 // The function has been compiled, can call it quickly. For a function
1244 // that was defined later: we can call it directly next time.
1245 if (iptr != NULL)
1246 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001247 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001248 iptr->isn_type = ISN_DCALL;
1249 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1250 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1251 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001252 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254
1255 if (call_prepare(argcount, argvars, ectx) == FAIL)
1256 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001257 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001258 funcexe.fe_evaluate = TRUE;
1259 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260
1261 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001263 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264
1265 // Clear the arguments.
1266 for (idx = 0; idx < argcount; ++idx)
1267 clear_tv(&argvars[idx]);
1268
1269 if (error != FCERR_NONE)
1270 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001271 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 return FAIL;
1273 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001274 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001275 // Error other than from calling the function itself.
1276 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 return OK;
1278}
1279
1280/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001281 * If command modifiers were applied restore them.
1282 */
1283 static void
1284may_restore_cmdmod(funclocal_T *funclocal)
1285{
1286 if (funclocal->floc_restore_cmdmod)
1287 {
1288 cmdmod.cmod_filter_regmatch.regprog = NULL;
1289 undo_cmdmod(&cmdmod);
1290 cmdmod = funclocal->floc_save_cmdmod;
1291 funclocal->floc_restore_cmdmod = FALSE;
1292 }
1293}
1294
1295/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001296 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1297 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001298 */
1299 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001300vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001301{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001302 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001303}
1304
1305/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 * Execute a function by "name".
1307 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001308 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 * Returns FAIL if not found without an error message.
1310 */
1311 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001312call_by_name(
1313 char_u *name,
1314 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001315 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001316 isn_T *iptr,
1317 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318{
1319 ufunc_T *ufunc;
1320
1321 if (builtin_function(name, -1))
1322 {
1323 int func_idx = find_internal_func(name);
1324
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001325 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001327 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 return FAIL;
1329 return call_bfunc(func_idx, argcount, ectx);
1330 }
1331
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001332 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001333
1334 if (ufunc == NULL)
1335 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001336 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001337
1338 if (script_autoload(name, TRUE))
1339 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001340 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001341
1342 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001343 return FAIL; // bail out if loading the script caused an error
1344 }
1345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001347 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001348 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001349 {
1350 int i;
1351 typval_T *argv = STACK_TV_BOT(0) - argcount;
1352
1353 // The function can change at runtime, check that the argument
1354 // types are correct.
1355 for (i = 0; i < argcount; ++i)
1356 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001357 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001358
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001359 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001360 type = ufunc->uf_arg_types[i];
1361 else if (ufunc->uf_va_type != NULL)
1362 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001363 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001364 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001365 return FAIL;
1366 }
1367 }
1368
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001369 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371
1372 return FAIL;
1373}
1374
1375 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001376call_partial(
1377 typval_T *tv,
1378 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001379 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001381 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001382 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001384 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001385 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386
1387 if (tv->v_type == VAR_PARTIAL)
1388 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001389 partial_T *pt = tv->vval.v_partial;
1390 int i;
1391
1392 if (pt->pt_argc > 0)
1393 {
1394 // Make space for arguments from the partial, shift the "argcount"
1395 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001396 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001397 return FAIL;
1398 for (i = 1; i <= argcount; ++i)
1399 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1400 ectx->ec_stack.ga_len += pt->pt_argc;
1401 argcount += pt->pt_argc;
1402
1403 // copy the arguments from the partial onto the stack
1404 for (i = 0; i < pt->pt_argc; ++i)
1405 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1406 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001407 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408
1409 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001410 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 name = pt->pt_name;
1413 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001414 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001416 if (name != NULL)
1417 {
1418 char_u fname_buf[FLEN_FIXED + 1];
1419 char_u *tofree = NULL;
1420 int error = FCERR_NONE;
1421 char_u *fname;
1422
1423 // May need to translate <SNR>123_ to K_SNR.
1424 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1425 if (error != FCERR_NONE)
1426 res = FAIL;
1427 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001428 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001429 vim_free(tofree);
1430 }
1431
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001432 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 {
1434 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001435 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001436 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 return FAIL;
1438 }
1439 return OK;
1440}
1441
1442/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001443 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1444 * TRUE.
1445 */
1446 static int
1447error_if_locked(int lock, char *error)
1448{
1449 if (lock & (VAR_LOCKED | VAR_FIXED))
1450 {
1451 emsg(_(error));
1452 return TRUE;
1453 }
1454 return FALSE;
1455}
1456
1457/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001458 * Give an error if "tv" is not a number and return FAIL.
1459 */
1460 static int
1461check_for_number(typval_T *tv)
1462{
1463 if (tv->v_type != VAR_NUMBER)
1464 {
1465 semsg(_(e_expected_str_but_got_str),
1466 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1467 return FAIL;
1468 }
1469 return OK;
1470}
1471
1472/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001473 * Store "tv" in variable "name".
1474 * This is for s: and g: variables.
1475 */
1476 static void
1477store_var(char_u *name, typval_T *tv)
1478{
1479 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001480 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001481
Bram Moolenaard877a572021-04-01 19:42:48 +02001482 if (tv->v_lock)
1483 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001484 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001485 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001486 restore_funccal();
1487}
1488
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001489/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001490 * Convert "tv" to a string.
1491 * Return FAIL if not allowed.
1492 */
1493 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001494do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001495{
1496 if (tv->v_type != VAR_STRING)
1497 {
1498 char_u *str;
1499
1500 if (is_2string_any)
1501 {
1502 switch (tv->v_type)
1503 {
1504 case VAR_SPECIAL:
1505 case VAR_BOOL:
1506 case VAR_NUMBER:
1507 case VAR_FLOAT:
1508 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001509
1510 case VAR_LIST:
1511 if (tolerant)
1512 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001513 char_u *s, *e, *p;
1514 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001515
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001516 ga_init2(&ga, sizeof(char_u *), 1);
1517
1518 // Convert to NL separated items, then
1519 // escape the items and replace the NL with
1520 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001521 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001522 if (str == NULL)
1523 return FAIL;
1524 s = str;
1525 while ((e = vim_strchr(s, '\n')) != NULL)
1526 {
1527 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001528 p = vim_strsave_fnameescape(s,
1529 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001530 if (p != NULL)
1531 {
1532 ga_concat(&ga, p);
1533 ga_concat(&ga, (char_u *)" ");
1534 vim_free(p);
1535 }
1536 s = e + 1;
1537 }
1538 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001539 clear_tv(tv);
1540 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001541 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001542 return OK;
1543 }
1544 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001545 default: to_string_error(tv->v_type);
1546 return FAIL;
1547 }
1548 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001549 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001550 clear_tv(tv);
1551 tv->v_type = VAR_STRING;
1552 tv->vval.v_string = str;
1553 }
1554 return OK;
1555}
1556
1557/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001558 * When the value of "sv" is a null list of dict, allocate it.
1559 */
1560 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001561allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001562{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001563 typval_T *tv = sv->sv_tv;
1564
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001565 switch (tv->v_type)
1566 {
1567 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001568 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001569 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001570 break;
1571 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001572 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001573 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001574 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001575 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001576 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001577 (void)rettv_blob_alloc(tv);
1578 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001579 default:
1580 break;
1581 }
1582}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001583
Bram Moolenaare7525c52021-01-09 13:20:37 +01001584/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001585 * Return the character "str[index]" where "index" is the character index,
1586 * including composing characters.
1587 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001588 */
1589 char_u *
1590char_from_string(char_u *str, varnumber_T index)
1591{
1592 size_t nbyte = 0;
1593 varnumber_T nchar = index;
1594 size_t slen;
1595
1596 if (str == NULL)
1597 return NULL;
1598 slen = STRLEN(str);
1599
Bram Moolenaarff871402021-03-26 13:34:05 +01001600 // Do the same as for a list: a negative index counts from the end.
1601 // Optimization to check the first byte to be below 0x80 (and no composing
1602 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001603 if (index < 0)
1604 {
1605 int clen = 0;
1606
1607 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001608 {
1609 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1610 ++nbyte;
1611 else if (enc_utf8)
1612 nbyte += utfc_ptr2len(str + nbyte);
1613 else
1614 nbyte += mb_ptr2len(str + nbyte);
1615 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001616 nchar = clen + index;
1617 if (nchar < 0)
1618 // unlike list: index out of range results in empty string
1619 return NULL;
1620 }
1621
1622 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001623 {
1624 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1625 ++nbyte;
1626 else if (enc_utf8)
1627 nbyte += utfc_ptr2len(str + nbyte);
1628 else
1629 nbyte += mb_ptr2len(str + nbyte);
1630 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001631 if (nbyte >= slen)
1632 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001633 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001634}
1635
1636/*
1637 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001638 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001639 * If going over the end return "str_len".
1640 * If "idx" is negative count from the end, -1 is the last character.
1641 * When going over the start return -1.
1642 */
1643 static long
1644char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1645{
1646 varnumber_T nchar = idx;
1647 size_t nbyte = 0;
1648
1649 if (nchar >= 0)
1650 {
1651 while (nchar > 0 && nbyte < str_len)
1652 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001653 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001654 --nchar;
1655 }
1656 }
1657 else
1658 {
1659 nbyte = str_len;
1660 while (nchar < 0 && nbyte > 0)
1661 {
1662 --nbyte;
1663 nbyte -= mb_head_off(str, str + nbyte);
1664 ++nchar;
1665 }
1666 if (nchar < 0)
1667 return -1;
1668 }
1669 return (long)nbyte;
1670}
1671
1672/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001673 * Return the slice "str[first : last]" using character indexes. Composing
1674 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001675 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001676 * Return NULL when the result is empty.
1677 */
1678 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001679string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001680{
1681 long start_byte, end_byte;
1682 size_t slen;
1683
1684 if (str == NULL)
1685 return NULL;
1686 slen = STRLEN(str);
1687 start_byte = char_idx2byte(str, slen, first);
1688 if (start_byte < 0)
1689 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001690 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001691 end_byte = (long)slen;
1692 else
1693 {
1694 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001695 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001696 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001697 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001698 }
1699
1700 if (start_byte >= (long)slen || end_byte <= start_byte)
1701 return NULL;
1702 return vim_strnsave(str + start_byte, end_byte - start_byte);
1703}
1704
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001705/*
1706 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1707 * When "dfunc_idx" is negative don't give an error.
1708 * Returns NULL for an error.
1709 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001710 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001711get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001712{
1713 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001714 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1715 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001716 svar_T *sv;
1717
1718 if (sref->sref_seq != si->sn_script_seq)
1719 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001720 // The script was reloaded after the function was compiled, the
1721 // script_idx may not be valid.
1722 if (dfunc != NULL)
1723 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1724 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001725 return NULL;
1726 }
1727 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001728 if (sv->sv_name == NULL)
1729 {
1730 if (dfunc != NULL)
1731 emsg(_(e_script_variable_was_deleted));
1732 return NULL;
1733 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001734 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001735 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001736 if (dfunc != NULL)
1737 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001738 return NULL;
1739 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001740
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001741 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1742 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001743 {
1744 if (dfunc != NULL)
1745 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1746 return NULL;
1747 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001748 return sv;
1749}
1750
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001751/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001752 * Function passed to do_cmdline() for splitting a script joined by NL
1753 * characters.
1754 */
1755 static char_u *
1756get_split_sourceline(
1757 int c UNUSED,
1758 void *cookie,
1759 int indent UNUSED,
1760 getline_opt_T options UNUSED)
1761{
1762 source_cookie_T *sp = (source_cookie_T *)cookie;
1763 char_u *p;
1764 char_u *line;
1765
Bram Moolenaar20677332021-06-06 17:02:53 +02001766 p = vim_strchr(sp->nextline, '\n');
1767 if (p == NULL)
1768 {
1769 line = vim_strsave(sp->nextline);
1770 sp->nextline += STRLEN(sp->nextline);
1771 }
1772 else
1773 {
1774 line = vim_strnsave(sp->nextline, p - sp->nextline);
1775 sp->nextline = p + 1;
1776 }
1777 return line;
1778}
1779
1780/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 * Execute a function by "name".
1782 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001783 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 */
1785 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001786call_eval_func(
1787 char_u *name,
1788 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001789 ectx_T *ectx,
1790 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791{
Bram Moolenaared677f52020-08-12 16:38:10 +02001792 int called_emsg_before = called_emsg;
1793 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001795 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001796 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001798 dictitem_T *v;
1799
1800 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001801 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1802 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001803 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001804 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001805 return FAIL;
1806 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001807 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001809 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810}
1811
1812/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001813 * When a function reference is used, fill a partial with the information
1814 * needed, especially when it is used as a closure.
1815 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001816 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001817fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1818{
1819 pt->pt_func = ufunc;
1820 pt->pt_refcount = 1;
1821
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001822 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001823 {
1824 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1825 + ectx->ec_dfunc_idx;
1826
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001827 // The closure may need to find arguments and local variables of the
1828 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001829 pt->pt_outer.out_stack = &ectx->ec_stack;
1830 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001831 if (ectx->ec_outer_ref != NULL)
1832 {
1833 // The current context already has a context, link to that one.
1834 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1835 if (ectx->ec_outer_ref->or_partial != NULL)
1836 {
1837 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1838 ++pt->pt_outer.out_up_partial->pt_refcount;
1839 }
1840 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001841
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001842 // If the function currently executing returns and the closure is still
1843 // being referenced, we need to make a copy of the context (arguments
1844 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001845 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001846 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001847 {
1848 vim_free(pt);
1849 return FAIL;
1850 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001851 // Extra variable keeps the count of closures created in the current
1852 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001853 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1854 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1855
1856 ((partial_T **)ectx->ec_funcrefs.ga_data)
1857 [ectx->ec_funcrefs.ga_len] = pt;
1858 ++pt->pt_refcount;
1859 ++ectx->ec_funcrefs.ga_len;
1860 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001861 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001862 return OK;
1863}
1864
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001865/*
1866 * Execute iptr->isn_arg.string as an Ex command.
1867 */
1868 static int
1869exec_command(isn_T *iptr)
1870{
1871 source_cookie_T cookie;
1872
1873 SOURCING_LNUM = iptr->isn_lnum;
1874 // Pass getsourceline to get an error for a missing ":end"
1875 // command.
1876 CLEAR_FIELD(cookie);
1877 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1878 if (do_cmdline(iptr->isn_arg.string,
1879 getsourceline, &cookie,
1880 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1881 || did_emsg)
1882 return FAIL;
1883 return OK;
1884}
1885
Bram Moolenaar89445512022-04-14 12:58:23 +01001886/*
1887 * If script "sid" is not loaded yet then load it now.
1888 * Caller must make sure "sid" is a valid script ID.
1889 * "loaded" is set to TRUE if the script had to be loaded.
1890 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1891 */
1892 int
1893may_load_script(int sid, int *loaded)
1894{
1895 scriptitem_T *si = SCRIPT_ITEM(sid);
1896
1897 if (si->sn_state == SN_STATE_NOT_LOADED)
1898 {
1899 *loaded = TRUE;
1900 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1901 {
1902 semsg(_(e_cant_open_file_str), si->sn_name);
1903 return FAIL;
1904 }
1905 }
1906 return OK;
1907}
1908
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001909// used for v_instr of typval of VAR_INSTR
1910struct instr_S {
1911 ectx_T *instr_ectx;
1912 isn_T *instr_instr;
1913};
1914
Bram Moolenaar4c137212021-04-19 16:48:48 +02001915// used for substitute_instr
1916typedef struct subs_expr_S {
1917 ectx_T *subs_ectx;
1918 isn_T *subs_instr;
1919 int subs_status;
1920} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001922// Set when calling do_debug().
1923static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001924static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001925
1926/*
1927 * When debugging lookup "name" and return the typeval.
1928 * When not found return NULL.
1929 */
1930 typval_T *
1931lookup_debug_var(char_u *name)
1932{
1933 int idx;
1934 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001935 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001936 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001937 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001938
1939 if (ectx == NULL)
1940 return NULL;
1941 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1942
1943 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001944 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001945 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001946 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1947
1948 // the variable name may be NULL when not available in this block
1949 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001950 return STACK_TV_VAR(idx);
1951 }
1952
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001953 // Go through argument names.
1954 ufunc = dfunc->df_ufunc;
1955 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1956 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1957 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1958 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1959 - varargs_off + idx);
1960 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1961 return STACK_TV(ectx->ec_frame_idx - 1);
1962
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001963 return NULL;
1964}
1965
Bram Moolenaar26a44842021-09-02 18:49:06 +02001966/*
1967 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1968 * breakpoint was set in that function or when there is any expression.
1969 */
1970 int
1971may_break_in_function(ufunc_T *ufunc)
1972{
1973 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1974}
1975
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001976 static void
1977handle_debug(isn_T *iptr, ectx_T *ectx)
1978{
1979 char_u *line;
1980 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1981 + ectx->ec_dfunc_idx)->df_ufunc;
1982 isn_T *ni;
1983 int end_lnum = iptr->isn_lnum;
1984 garray_T ga;
1985 int lnum;
1986
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001987 if (ex_nesting_level > debug_break_level)
1988 {
1989 linenr_T breakpoint;
1990
Bram Moolenaar26a44842021-09-02 18:49:06 +02001991 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001992 return;
1993
1994 // check for the next breakpoint if needed
1995 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001996 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001997 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1998 return;
1999 }
2000
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002001 SOURCING_LNUM = iptr->isn_lnum;
2002 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002003 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002004
2005 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2006 if (ni->isn_type == ISN_DEBUG
2007 || ni->isn_type == ISN_RETURN
2008 || ni->isn_type == ISN_RETURN_VOID)
2009 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002010 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002011 break;
2012 }
2013
2014 if (end_lnum > iptr->isn_lnum)
2015 {
2016 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002017 for (lnum = iptr->isn_lnum; lnum < end_lnum
2018 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002019 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002020 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002021
Bram Moolenaar303215d2021-07-07 20:10:43 +02002022 if (p == NULL)
2023 continue; // left over from continuation line
2024 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002025 if (*p == '#')
2026 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002027 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002028 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2029 if (STRNCMP(p, "def ", 4) == 0)
2030 break;
2031 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002032 line = ga_concat_strings(&ga, " ");
2033 vim_free(ga.ga_data);
2034 }
2035 else
2036 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002037
Dominique Pelle6e969552021-06-17 13:53:41 +02002038 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002039 debug_context = NULL;
2040
2041 if (end_lnum > iptr->isn_lnum)
2042 vim_free(line);
2043}
2044
Bram Moolenaar4c137212021-04-19 16:48:48 +02002045/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002046 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002047 * Returns OK, FAIL or NOTDONE (uncatchable error).
2048 */
2049 static int
2050execute_storeindex(isn_T *iptr, ectx_T *ectx)
2051{
2052 vartype_T dest_type = iptr->isn_arg.vartype;
2053 typval_T *tv;
2054 typval_T *tv_idx = STACK_TV_BOT(-2);
2055 typval_T *tv_dest = STACK_TV_BOT(-1);
2056 int status = OK;
2057
2058 // Stack contains:
2059 // -3 value to be stored
2060 // -2 index
2061 // -1 dict or list
2062 tv = STACK_TV_BOT(-3);
2063 SOURCING_LNUM = iptr->isn_lnum;
2064 if (dest_type == VAR_ANY)
2065 {
2066 dest_type = tv_dest->v_type;
2067 if (dest_type == VAR_DICT)
2068 status = do_2string(tv_idx, TRUE, FALSE);
2069 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2070 {
2071 emsg(_(e_number_expected));
2072 status = FAIL;
2073 }
2074 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002075
2076 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002077 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002078 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002079 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002080 long lidx = (long)tv_idx->vval.v_number;
2081 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002082
Bram Moolenaare08be092022-02-17 13:08:26 +00002083 if (list == NULL)
2084 {
2085 emsg(_(e_list_not_set));
2086 return FAIL;
2087 }
2088 if (lidx < 0 && list->lv_len + lidx >= 0)
2089 // negative index is relative to the end
2090 lidx = list->lv_len + lidx;
2091 if (lidx < 0 || lidx > list->lv_len)
2092 {
2093 semsg(_(e_list_index_out_of_range_nr), lidx);
2094 return FAIL;
2095 }
2096 if (lidx < list->lv_len)
2097 {
2098 listitem_T *li = list_find(list, lidx);
2099
2100 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002101 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002102 return FAIL;
2103 // overwrite existing list item
2104 clear_tv(&li->li_tv);
2105 li->li_tv = *tv;
2106 }
2107 else
2108 {
2109 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2110 return FAIL;
2111 // append to list, only fails when out of memory
2112 if (list_append_tv(list, tv) == FAIL)
2113 return NOTDONE;
2114 clear_tv(tv);
2115 }
2116 }
2117 else if (dest_type == VAR_DICT)
2118 {
2119 char_u *key = tv_idx->vval.v_string;
2120 dict_T *dict = tv_dest->vval.v_dict;
2121 dictitem_T *di;
2122
2123 SOURCING_LNUM = iptr->isn_lnum;
2124 if (dict == NULL)
2125 {
2126 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002127 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002128 }
2129 if (key == NULL)
2130 key = (char_u *)"";
2131 di = dict_find(dict, key, -1);
2132 if (di != NULL)
2133 {
2134 if (error_if_locked(di->di_tv.v_lock,
2135 e_cannot_change_dict_item))
2136 return FAIL;
2137 // overwrite existing value
2138 clear_tv(&di->di_tv);
2139 di->di_tv = *tv;
2140 }
2141 else
2142 {
2143 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2144 return FAIL;
2145 // add to dict, only fails when out of memory
2146 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2147 return NOTDONE;
2148 clear_tv(tv);
2149 }
2150 }
2151 else if (dest_type == VAR_BLOB)
2152 {
2153 long lidx = (long)tv_idx->vval.v_number;
2154 blob_T *blob = tv_dest->vval.v_blob;
2155 varnumber_T nr;
2156 int error = FALSE;
2157 int len;
2158
2159 if (blob == NULL)
2160 {
2161 emsg(_(e_blob_not_set));
2162 return FAIL;
2163 }
2164 len = blob_len(blob);
2165 if (lidx < 0 && len + lidx >= 0)
2166 // negative index is relative to the end
2167 lidx = len + lidx;
2168
2169 // Can add one byte at the end.
2170 if (lidx < 0 || lidx > len)
2171 {
2172 semsg(_(e_blob_index_out_of_range_nr), lidx);
2173 return FAIL;
2174 }
2175 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2176 return FAIL;
2177 nr = tv_get_number_chk(tv, &error);
2178 if (error)
2179 return FAIL;
2180 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002181 }
2182 else
2183 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002184 status = FAIL;
2185 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002186 }
2187 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002188
2189 clear_tv(tv_idx);
2190 clear_tv(tv_dest);
2191 ectx->ec_stack.ga_len -= 3;
2192 if (status == FAIL)
2193 {
2194 clear_tv(tv);
2195 return FAIL;
2196 }
2197 return OK;
2198}
2199
2200/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002201 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002202 */
2203 static int
2204execute_storerange(isn_T *iptr, ectx_T *ectx)
2205{
2206 typval_T *tv;
2207 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2208 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2209 typval_T *tv_dest = STACK_TV_BOT(-1);
2210 int status = OK;
2211
2212 // Stack contains:
2213 // -4 value to be stored
2214 // -3 first index or "none"
2215 // -2 second index or "none"
2216 // -1 destination list or blob
2217 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002218 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002219 if (tv_dest->v_type == VAR_LIST)
2220 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002221 long n1;
2222 long n2;
2223 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002224
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002225 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2226 if (tv_idx2->v_type == VAR_SPECIAL
2227 && tv_idx2->vval.v_number == VVAL_NONE)
2228 n2 = list_len(tv_dest->vval.v_list) - 1;
2229 else
2230 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2231
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002232 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002233 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002234 status = FAIL;
2235 else
2236 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002237 status = check_range_index_two(tv_dest->vval.v_list,
2238 &n1, li1, &n2, FALSE);
2239 if (status != FAIL)
2240 status = list_assign_range(
2241 tv_dest->vval.v_list,
2242 tv->vval.v_list,
2243 n1,
2244 n2,
2245 tv_idx2->v_type == VAR_SPECIAL,
2246 (char_u *)"=",
2247 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002248 }
2249 }
2250 else if (tv_dest->v_type == VAR_BLOB)
2251 {
2252 varnumber_T n1;
2253 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002254 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002255
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002256 n1 = tv_get_number_chk(tv_idx1, NULL);
2257 if (tv_idx2->v_type == VAR_SPECIAL
2258 && tv_idx2->vval.v_number == VVAL_NONE)
2259 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2260 else
2261 n2 = tv_get_number_chk(tv_idx2, NULL);
2262 bloblen = blob_len(tv_dest->vval.v_blob);
2263
2264 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2265 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002266 status = FAIL;
2267 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002268 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002269 }
2270 else
2271 {
2272 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002273 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002274 }
2275
2276 clear_tv(tv_idx1);
2277 clear_tv(tv_idx2);
2278 clear_tv(tv_dest);
2279 ectx->ec_stack.ga_len -= 4;
2280 clear_tv(tv);
2281
2282 return status;
2283}
2284
2285/*
2286 * Unlet item in list or dict variable.
2287 */
2288 static int
2289execute_unletindex(isn_T *iptr, ectx_T *ectx)
2290{
2291 typval_T *tv_idx = STACK_TV_BOT(-2);
2292 typval_T *tv_dest = STACK_TV_BOT(-1);
2293 int status = OK;
2294
2295 // Stack contains:
2296 // -2 index
2297 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002298 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002299 if (tv_dest->v_type == VAR_DICT)
2300 {
2301 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002302 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002303 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002304 semsg(_(e_expected_str_but_got_str),
2305 vartype_name(VAR_STRING),
2306 vartype_name(tv_idx->v_type));
2307 status = FAIL;
2308 }
2309 else
2310 {
2311 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002312 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002313 dictitem_T *di = NULL;
2314
2315 if (d != NULL && value_check_lock(
2316 d->dv_lock, NULL, FALSE))
2317 status = FAIL;
2318 else
2319 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002320 if (tv_idx->v_type == VAR_STRING)
2321 {
2322 key = tv_idx->vval.v_string;
2323 if (key == NULL)
2324 key = (char_u *)"";
2325 }
2326 else
2327 {
2328 key = tv_get_string(tv_idx);
2329 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002330 if (d != NULL)
2331 di = dict_find(d, key, (int)STRLEN(key));
2332 if (di == NULL)
2333 {
2334 // NULL dict is equivalent to empty dict
2335 semsg(_(e_key_not_present_in_dictionary),
2336 key);
2337 status = FAIL;
2338 }
2339 else if (var_check_fixed(di->di_flags,
2340 NULL, FALSE)
2341 || var_check_ro(di->di_flags,
2342 NULL, FALSE))
2343 status = FAIL;
2344 else
2345 dictitem_remove(d, di);
2346 }
2347 }
2348 }
2349 else if (tv_dest->v_type == VAR_LIST)
2350 {
2351 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002352 if (check_for_number(tv_idx) == FAIL)
2353 {
2354 status = FAIL;
2355 }
2356 else
2357 {
2358 list_T *l = tv_dest->vval.v_list;
2359 long n = (long)tv_idx->vval.v_number;
2360
2361 if (l != NULL && value_check_lock(
2362 l->lv_lock, NULL, FALSE))
2363 status = FAIL;
2364 else
2365 {
2366 listitem_T *li = list_find(l, n);
2367
2368 if (li == NULL)
2369 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002370 semsg(_(e_list_index_out_of_range_nr), n);
2371 status = FAIL;
2372 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002373 else
2374 listitem_remove(l, li);
2375 }
2376 }
2377 }
2378 else
2379 {
2380 status = FAIL;
2381 semsg(_(e_cannot_index_str),
2382 vartype_name(tv_dest->v_type));
2383 }
2384
2385 clear_tv(tv_idx);
2386 clear_tv(tv_dest);
2387 ectx->ec_stack.ga_len -= 2;
2388
2389 return status;
2390}
2391
2392/*
2393 * Unlet a range of items in a list variable.
2394 */
2395 static int
2396execute_unletrange(isn_T *iptr, ectx_T *ectx)
2397{
2398 // Stack contains:
2399 // -3 index1
2400 // -2 index2
2401 // -1 dict or list
2402 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2403 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2404 typval_T *tv_dest = STACK_TV_BOT(-1);
2405 int status = OK;
2406
2407 if (tv_dest->v_type == VAR_LIST)
2408 {
2409 // indexes must be a number
2410 SOURCING_LNUM = iptr->isn_lnum;
2411 if (check_for_number(tv_idx1) == FAIL
2412 || (tv_idx2->v_type != VAR_SPECIAL
2413 && check_for_number(tv_idx2) == FAIL))
2414 {
2415 status = FAIL;
2416 }
2417 else
2418 {
2419 list_T *l = tv_dest->vval.v_list;
2420 long n1 = (long)tv_idx1->vval.v_number;
2421 long n2 = tv_idx2->v_type == VAR_SPECIAL
2422 ? 0 : (long)tv_idx2->vval.v_number;
2423 listitem_T *li;
2424
2425 li = list_find_index(l, &n1);
2426 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002427 {
2428 semsg(_(e_list_index_out_of_range_nr),
2429 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002430 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002431 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002432 else
2433 {
2434 if (n1 < 0)
2435 n1 = list_idx_of_item(l, li);
2436 if (n2 < 0)
2437 {
2438 listitem_T *li2 = list_find(l, n2);
2439
2440 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002441 {
2442 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002443 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002444 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002445 else
2446 n2 = list_idx_of_item(l, li2);
2447 }
2448 if (status != FAIL
2449 && tv_idx2->v_type != VAR_SPECIAL
2450 && n2 < n1)
2451 {
2452 semsg(_(e_list_index_out_of_range_nr), n2);
2453 status = FAIL;
2454 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002455 if (status != FAIL)
2456 list_unlet_range(l, li, n1,
2457 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002458 }
2459 }
2460 }
2461 else
2462 {
2463 status = FAIL;
2464 SOURCING_LNUM = iptr->isn_lnum;
2465 semsg(_(e_cannot_index_str),
2466 vartype_name(tv_dest->v_type));
2467 }
2468
2469 clear_tv(tv_idx1);
2470 clear_tv(tv_idx2);
2471 clear_tv(tv_dest);
2472 ectx->ec_stack.ga_len -= 3;
2473
2474 return status;
2475}
2476
2477/*
2478 * Top of a for loop.
2479 */
2480 static int
2481execute_for(isn_T *iptr, ectx_T *ectx)
2482{
2483 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002484 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002485 typval_T *ltv = STACK_TV_BOT(-1);
2486 typval_T *idxtv =
2487 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2488
2489 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2490 return FAIL;
2491 if (ltv->v_type == VAR_LIST)
2492 {
2493 list_T *list = ltv->vval.v_list;
2494
2495 // push the next item from the list
2496 ++idxtv->vval.v_number;
2497 if (list == NULL
2498 || idxtv->vval.v_number >= list->lv_len)
2499 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002500 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002501 }
2502 else if (list->lv_first == &range_list_item)
2503 {
2504 // non-materialized range() list
2505 tv = STACK_TV_BOT(0);
2506 tv->v_type = VAR_NUMBER;
2507 tv->v_lock = 0;
2508 tv->vval.v_number = list_find_nr(
2509 list, idxtv->vval.v_number, NULL);
2510 ++ectx->ec_stack.ga_len;
2511 }
2512 else
2513 {
2514 listitem_T *li = list_find(list,
2515 idxtv->vval.v_number);
2516
2517 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2518 ++ectx->ec_stack.ga_len;
2519 }
2520 }
2521 else if (ltv->v_type == VAR_STRING)
2522 {
2523 char_u *str = ltv->vval.v_string;
2524
2525 // The index is for the last byte of the previous
2526 // character.
2527 ++idxtv->vval.v_number;
2528 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2529 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002530 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002531 }
2532 else
2533 {
2534 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2535
2536 // Push the next character from the string.
2537 tv = STACK_TV_BOT(0);
2538 tv->v_type = VAR_STRING;
2539 tv->vval.v_string = vim_strnsave(
2540 str + idxtv->vval.v_number, clen);
2541 ++ectx->ec_stack.ga_len;
2542 idxtv->vval.v_number += clen - 1;
2543 }
2544 }
2545 else if (ltv->v_type == VAR_BLOB)
2546 {
2547 blob_T *blob = ltv->vval.v_blob;
2548
2549 // When we get here the first time make a copy of the
2550 // blob, so that the iteration still works when it is
2551 // changed.
2552 if (idxtv->vval.v_number == -1 && blob != NULL)
2553 {
2554 blob_copy(blob, ltv);
2555 blob_unref(blob);
2556 blob = ltv->vval.v_blob;
2557 }
2558
2559 // The index is for the previous byte.
2560 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002561 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002562 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002563 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002564 }
2565 else
2566 {
2567 // Push the next byte from the blob.
2568 tv = STACK_TV_BOT(0);
2569 tv->v_type = VAR_NUMBER;
2570 tv->vval.v_number = blob_get(blob,
2571 idxtv->vval.v_number);
2572 ++ectx->ec_stack.ga_len;
2573 }
2574 }
2575 else
2576 {
2577 semsg(_(e_for_loop_on_str_not_supported),
2578 vartype_name(ltv->v_type));
2579 return FAIL;
2580 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002581
2582 if (jump)
2583 {
2584 // past the end of the list/string/blob, jump to "endfor"
2585 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2586 may_restore_cmdmod(&ectx->ec_funclocal);
2587 }
2588 else
2589 {
2590 // Store the current number of funcrefs, this may be used in
2591 // ISN_LOOPEND. The variable index is always one more than the loop
2592 // variable index.
2593 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_idx + 1);
2594 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2595 }
2596
2597 return OK;
2598}
2599
2600/*
2601 * End of a for or while loop: Handle any variables used by a closure.
2602 */
2603 static int
2604execute_endloop(isn_T *iptr UNUSED, ectx_T *ectx UNUSED)
2605{
2606 // TODO
2607
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002608 return OK;
2609}
2610
2611/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002612 * Load instruction for w:/b:/g:/t: variable.
2613 * "isn_type" is used instead of "iptr->isn_type".
2614 */
2615 static int
2616load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2617{
2618 dictitem_T *di = NULL;
2619 hashtab_T *ht = NULL;
2620 char namespace;
2621
2622 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2623 return NOTDONE;
2624 switch (isn_type)
2625 {
2626 case ISN_LOADG:
2627 ht = get_globvar_ht();
2628 namespace = 'g';
2629 break;
2630 case ISN_LOADB:
2631 ht = &curbuf->b_vars->dv_hashtab;
2632 namespace = 'b';
2633 break;
2634 case ISN_LOADW:
2635 ht = &curwin->w_vars->dv_hashtab;
2636 namespace = 'w';
2637 break;
2638 case ISN_LOADT:
2639 ht = &curtab->tp_vars->dv_hashtab;
2640 namespace = 't';
2641 break;
2642 default: // Cannot reach here
2643 return NOTDONE;
2644 }
2645 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2646
Bram Moolenaar06b77222022-01-25 15:51:56 +00002647 if (di == NULL)
2648 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002649 if (isn_type == ISN_LOADG)
2650 {
2651 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2652
2653 // g:Something could be a function
2654 if (ufunc != NULL)
2655 {
2656 typval_T *tv = STACK_TV_BOT(0);
2657
2658 ++ectx->ec_stack.ga_len;
2659 tv->v_type = VAR_FUNC;
2660 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2661 if (tv->vval.v_string == NULL)
2662 return FAIL;
2663 STRCPY(tv->vval.v_string, "g:");
2664 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2665 return OK;
2666 }
2667 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002668 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002669 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002670 // no check if the item exists in the script but
2671 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002672 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002673 else
2674 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002675 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002676 return FAIL;
2677 }
2678 else
2679 {
2680 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2681 ++ectx->ec_stack.ga_len;
2682 }
2683 return OK;
2684}
2685
2686/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002687 * Execute instructions in execution context "ectx".
2688 * Return OK or FAIL;
2689 */
2690 static int
2691exec_instructions(ectx_T *ectx)
2692{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002693 int ret = FAIL;
2694 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002695 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002696
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002697 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002698 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002699
Bram Moolenaarff652882021-05-16 15:24:49 +02002700 // Only catch exceptions in this instruction list.
2701 ectx->ec_trylevel_at_start = trylevel;
2702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 for (;;)
2704 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002705 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002707 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002708
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002709 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002710 {
2711 line_breakcheck();
2712 breakcheck_count = 0;
2713 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002714 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002715 {
2716 // Turn CTRL-C into an exception.
2717 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002718 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002719 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002720 did_throw = TRUE;
2721 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002723 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002724 {
2725 // Turn an error message into an exception.
2726 did_emsg = FALSE;
2727 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002728 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002729 did_throw = TRUE;
2730 *msg_list = NULL;
2731 }
2732
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002733 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002735 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002736 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002737 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738
2739 // An exception jumps to the first catch, finally, or returns from
2740 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002741 while (index > 0)
2742 {
2743 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002744 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002745 break;
2746 // In the catch and finally block of this try we have to go up
2747 // one level.
2748 --index;
2749 trycmd = NULL;
2750 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002751 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002753 if (trycmd->tcd_in_catch)
2754 {
2755 // exception inside ":catch", jump to ":finally" once
2756 ectx->ec_iidx = trycmd->tcd_finally_idx;
2757 trycmd->tcd_finally_idx = 0;
2758 }
2759 else
2760 // jump to first ":catch"
2761 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002762 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002763 did_throw = FALSE; // don't come back here until :endtry
2764 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 }
2766 else
2767 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002768 // Not inside try or need to return from current functions.
2769 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002770 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002771 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002772 tv = STACK_TV_BOT(0);
2773 tv->v_type = VAR_NUMBER;
2774 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002775 ++ectx->ec_stack.ga_len;
2776 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002778 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002779 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002780 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002781 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 goto done;
2783 }
2784
Bram Moolenaar4c137212021-04-19 16:48:48 +02002785 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002786 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 }
2788 continue;
2789 }
2790
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002791 /*
2792 * Big switch on the instruction. Most compilers will be turning this
2793 * into an efficient lookup table, since the "case" values are an enum
2794 * with sequential numbers. It may look ugly, but it should be the
2795 * most efficient way.
2796 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002797 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 switch (iptr->isn_type)
2799 {
2800 // execute Ex command line
2801 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002802 if (exec_command(iptr) == FAIL)
2803 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 break;
2805
Bram Moolenaar20677332021-06-06 17:02:53 +02002806 // execute Ex command line split at NL characters.
2807 case ISN_EXEC_SPLIT:
2808 {
2809 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002810 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002811
2812 SOURCING_LNUM = iptr->isn_lnum;
2813 CLEAR_FIELD(cookie);
2814 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2815 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002816 line = get_split_sourceline(0, &cookie, 0, 0);
2817 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002818 get_split_sourceline, &cookie,
2819 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2820 == FAIL
2821 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002822 {
2823 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002824 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002825 }
2826 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002827 }
2828 break;
2829
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002830 // execute Ex command line that is only a range
2831 case ISN_EXECRANGE:
2832 {
2833 exarg_T ea;
2834 char *error = NULL;
2835
2836 CLEAR_FIELD(ea);
2837 ea.cmdidx = CMD_SIZE;
2838 ea.addr_type = ADDR_LINES;
2839 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002840 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002841 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002842 if (ea.cmd == NULL)
2843 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002844 // error is always NULL when using ADDR_LINES
2845 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002846 if (error != NULL)
2847 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002848 emsg(error);
2849 goto on_error;
2850 }
2851 }
2852 break;
2853
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002854 // Evaluate an expression with legacy syntax, push it onto the
2855 // stack.
2856 case ISN_LEGACY_EVAL:
2857 {
2858 char_u *arg = iptr->isn_arg.string;
2859 int res;
2860 int save_flags = cmdmod.cmod_flags;
2861
Bram Moolenaar35578162021-08-02 19:10:38 +02002862 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002863 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002864 tv = STACK_TV_BOT(0);
2865 init_tv(tv);
2866 cmdmod.cmod_flags |= CMOD_LEGACY;
2867 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2868 cmdmod.cmod_flags = save_flags;
2869 if (res == FAIL)
2870 goto on_error;
2871 ++ectx->ec_stack.ga_len;
2872 }
2873 break;
2874
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002875 // push typeval VAR_INSTR with instructions to be executed
2876 case ISN_INSTR:
2877 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002878 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002879 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002880 tv = STACK_TV_BOT(0);
2881 tv->vval.v_instr = ALLOC_ONE(instr_T);
2882 if (tv->vval.v_instr == NULL)
2883 goto on_error;
2884 ++ectx->ec_stack.ga_len;
2885
2886 tv->v_type = VAR_INSTR;
2887 tv->vval.v_instr->instr_ectx = ectx;
2888 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2889 }
2890 break;
2891
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002892 case ISN_SOURCE:
2893 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002894 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002895
Bram Moolenaar89445512022-04-14 12:58:23 +01002896 SOURCING_LNUM = iptr->isn_lnum;
2897 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002898 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002899 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002900 }
2901 break;
2902
Bram Moolenaar4c137212021-04-19 16:48:48 +02002903 // execute :substitute with an expression
2904 case ISN_SUBSTITUTE:
2905 {
2906 subs_T *subs = &iptr->isn_arg.subs;
2907 source_cookie_T cookie;
2908 struct subs_expr_S *save_instr = substitute_instr;
2909 struct subs_expr_S subs_instr;
2910 int res;
2911
2912 subs_instr.subs_ectx = ectx;
2913 subs_instr.subs_instr = subs->subs_instr;
2914 subs_instr.subs_status = OK;
2915 substitute_instr = &subs_instr;
2916
2917 SOURCING_LNUM = iptr->isn_lnum;
2918 // This is very much like ISN_EXEC
2919 CLEAR_FIELD(cookie);
2920 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2921 res = do_cmdline(subs->subs_cmd,
2922 getsourceline, &cookie,
2923 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2924 substitute_instr = save_instr;
2925
2926 if (res == FAIL || did_emsg
2927 || subs_instr.subs_status == FAIL)
2928 goto on_error;
2929 }
2930 break;
2931
2932 case ISN_FINISH:
2933 goto done;
2934
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002935 case ISN_REDIRSTART:
2936 // create a dummy entry for var_redir_str()
2937 if (alloc_redir_lval() == FAIL)
2938 goto on_error;
2939
2940 // The output is stored in growarray "redir_ga" until
2941 // redirection ends.
2942 init_redir_ga();
2943 redir_vname = 1;
2944 break;
2945
2946 case ISN_REDIREND:
2947 {
2948 char_u *res = get_clear_redir_ga();
2949
2950 // End redirection, put redirected text on the stack.
2951 clear_redir_lval();
2952 redir_vname = 0;
2953
Bram Moolenaar35578162021-08-02 19:10:38 +02002954 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002955 {
2956 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002957 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002958 }
2959 tv = STACK_TV_BOT(0);
2960 tv->v_type = VAR_STRING;
2961 tv->vval.v_string = res;
2962 ++ectx->ec_stack.ga_len;
2963 }
2964 break;
2965
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002966 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002967#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002968 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002969 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2970 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002971 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002972#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002973 break;
2974
2975 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002976#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002977 {
2978 exarg_T ea;
2979 int res;
2980
2981 CLEAR_FIELD(ea);
2982 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2983 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2984 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2985 --ectx->ec_stack.ga_len;
2986 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01002987 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002988 res = cexpr_core(&ea, tv);
2989 clear_tv(tv);
2990 if (res == FAIL)
2991 goto on_error;
2992 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002993#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002994 break;
2995
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002996 // execute Ex command from pieces on the stack
2997 case ISN_EXECCONCAT:
2998 {
2999 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003000 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003001 int pass;
3002 int i;
3003 char_u *cmd = NULL;
3004 char_u *str;
3005
3006 for (pass = 1; pass <= 2; ++pass)
3007 {
3008 for (i = 0; i < count; ++i)
3009 {
3010 tv = STACK_TV_BOT(i - count);
3011 str = tv->vval.v_string;
3012 if (str != NULL && *str != NUL)
3013 {
3014 if (pass == 2)
3015 STRCPY(cmd + len, str);
3016 len += STRLEN(str);
3017 }
3018 if (pass == 2)
3019 clear_tv(tv);
3020 }
3021 if (pass == 1)
3022 {
3023 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003024 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003025 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003026 len = 0;
3027 }
3028 }
3029
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003030 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003031 do_cmdline_cmd(cmd);
3032 vim_free(cmd);
3033 }
3034 break;
3035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 // execute :echo {string} ...
3037 case ISN_ECHO:
3038 {
3039 int count = iptr->isn_arg.echo.echo_count;
3040 int atstart = TRUE;
3041 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003042 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043
3044 for (idx = 0; idx < count; ++idx)
3045 {
3046 tv = STACK_TV_BOT(idx - count);
3047 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3048 &atstart, &needclr);
3049 clear_tv(tv);
3050 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003051 if (needclr)
3052 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003053 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 }
3055 break;
3056
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003057 // :execute {string} ...
3058 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003059 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003060 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003061 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003062 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003063 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003064 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003065 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003066 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003067 {
3068 int count = iptr->isn_arg.number;
3069 garray_T ga;
3070 char_u buf[NUMBUFLEN];
3071 char_u *p;
3072 int len;
3073 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003074 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003075
3076 ga_init2(&ga, 1, 80);
3077 for (idx = 0; idx < count; ++idx)
3078 {
3079 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003080 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003081 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003082 if (tv->v_type == VAR_CHANNEL
3083 || tv->v_type == VAR_JOB)
3084 {
3085 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003086 semsg(_(e_using_invalid_value_as_string_str),
3087 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003088 break;
3089 }
3090 else
3091 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003092 }
3093 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003094 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003095
3096 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003097 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003098 failed = TRUE;
3099 else
3100 {
3101 if (ga.ga_len > 0)
3102 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3103 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3104 ga.ga_len += len;
3105 }
3106 clear_tv(tv);
3107 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003108 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003109 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003110 {
3111 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003112 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003113 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003114
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003115 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003116 {
3117 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003118 {
3119 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003120 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003121 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003122 {
3123 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003124 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003125 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003126 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003127 else
3128 {
3129 msg_sb_eol();
3130 if (iptr->isn_type == ISN_ECHOMSG)
3131 {
3132 msg_attr(ga.ga_data, echo_attr);
3133 out_flush();
3134 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003135#ifdef HAS_MESSAGE_WINDOW
3136 else if (iptr->isn_type == ISN_ECHOWINDOW)
3137 {
3138 start_echowindow();
3139 msg_attr(ga.ga_data, echo_attr);
3140 end_echowindow();
3141 }
3142#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003143 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3144 {
3145 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3146 TRUE);
3147 ui_write((char_u *)"\r\n", 2, TRUE);
3148 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003149 else
3150 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003151 SOURCING_LNUM = iptr->isn_lnum;
3152 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003153 }
3154 }
3155 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003156 ga_clear(&ga);
3157 }
3158 break;
3159
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 // load local variable or argument
3161 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003162 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003163 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003165 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 break;
3167
3168 // load v: variable
3169 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003170 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003171 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003173 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 break;
3175
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003176 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 case ISN_LOADSCRIPT:
3178 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003179 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 svar_T *sv;
3181
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003182 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003183 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003184 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003185 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003186 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003187 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003189 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 }
3191 break;
3192
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003193 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003195 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003197 int sid = iptr->isn_arg.loadstore.ls_sid;
3198 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003199 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003201
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 if (di == NULL)
3203 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003204 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003205 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003206 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 }
3208 else
3209 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003210 if (iptr->isn_type == ISN_LOADEXPORT)
3211 {
3212 int idx = get_script_item_idx(sid, name, 0,
3213 NULL, NULL);
3214 svar_T *sv;
3215
3216 if (idx >= 0)
3217 {
3218 sv = ((svar_T *)SCRIPT_ITEM(sid)
3219 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003220 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003221 {
3222 SOURCING_LNUM = iptr->isn_lnum;
3223 semsg(_(e_item_not_exported_in_script_str),
3224 name);
3225 goto on_error;
3226 }
3227 }
3228 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003229 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003230 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003232 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 }
3234 }
3235 break;
3236
Bram Moolenaard3aac292020-04-19 14:32:17 +02003237 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003239 case ISN_LOADB:
3240 case ISN_LOADW:
3241 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003243 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003244
Bram Moolenaar06b77222022-01-25 15:51:56 +00003245 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003246 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003247 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003248 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 break;
3252
Bram Moolenaar03290b82020-12-19 16:30:44 +01003253 // load autoload variable
3254 case ISN_LOADAUTO:
3255 {
3256 char_u *name = iptr->isn_arg.string;
3257
Bram Moolenaar35578162021-08-02 19:10:38 +02003258 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003259 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003260 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003261 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003262 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003263 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003264 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003265 }
3266 break;
3267
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003268 // load g:/b:/w:/t: namespace
3269 case ISN_LOADGDICT:
3270 case ISN_LOADBDICT:
3271 case ISN_LOADWDICT:
3272 case ISN_LOADTDICT:
3273 {
3274 dict_T *d = NULL;
3275
3276 switch (iptr->isn_type)
3277 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003278 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3279 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3280 case ISN_LOADWDICT: d = curwin->w_vars; break;
3281 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003282 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003283 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003284 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003285 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003286 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003287 tv = STACK_TV_BOT(0);
3288 tv->v_type = VAR_DICT;
3289 tv->v_lock = 0;
3290 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003291 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003292 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003293 }
3294 break;
3295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 // load &option
3297 case ISN_LOADOPT:
3298 {
3299 typval_T optval;
3300 char_u *name = iptr->isn_arg.string;
3301
Bram Moolenaara8c17702020-04-01 21:17:24 +02003302 // This is not expected to fail, name is checked during
3303 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003304 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003305 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003306 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003307 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003309 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 }
3311 break;
3312
3313 // load $ENV
3314 case ISN_LOADENV:
3315 {
3316 typval_T optval;
3317 char_u *name = iptr->isn_arg.string;
3318
Bram Moolenaar35578162021-08-02 19:10:38 +02003319 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003320 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003321 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003322 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003324 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 }
3326 break;
3327
3328 // load @register
3329 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003330 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003331 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 tv = STACK_TV_BOT(0);
3333 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003334 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003335 // This may result in NULL, which should be equivalent to an
3336 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 tv->vval.v_string = get_reg_contents(
3338 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003339 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 break;
3341
3342 // store local variable
3343 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003344 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 tv = STACK_TV_VAR(iptr->isn_arg.number);
3346 clear_tv(tv);
3347 *tv = *STACK_TV_BOT(0);
3348 break;
3349
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003350 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003351 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003352 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003353 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003354 int sid = iptr->isn_arg.loadstore.ls_sid;
3355 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003356 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003357 dictitem_T *di = find_var_in_ht(ht, 0,
3358 iptr->isn_type == ISN_STORES
3359 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003360
Bram Moolenaar4c137212021-04-19 16:48:48 +02003361 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003362 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003363 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003364 {
3365 if (iptr->isn_type == ISN_STOREEXPORT)
3366 {
3367 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003368 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003369 goto on_error;
3370 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003371 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003372 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003373 else
3374 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003375 if (iptr->isn_type == ISN_STOREEXPORT)
3376 {
3377 int idx = get_script_item_idx(sid, name, 0,
3378 NULL, NULL);
3379
Bram Moolenaar06651632022-04-27 17:54:25 +01003380 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003381 if (idx >= 0)
3382 {
3383 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3384 ->sn_var_vals.ga_data) + idx;
3385
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003386 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003387 {
3388 semsg(_(e_item_not_exported_in_script_str),
3389 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003390 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003391 goto on_error;
3392 }
3393 }
3394 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003395 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003396 {
3397 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003398 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003399 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003400 clear_tv(&di->di_tv);
3401 di->di_tv = *STACK_TV_BOT(0);
3402 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003403 }
3404 break;
3405
3406 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 case ISN_STORESCRIPT:
3408 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003409 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3410 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003412 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003413 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003414 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003415 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003416
3417 // "const" and "final" are checked at compile time, locking
3418 // the value needs to be checked here.
3419 SOURCING_LNUM = iptr->isn_lnum;
3420 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003421 {
3422 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003423 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003424 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003425
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 clear_tv(sv->sv_tv);
3427 *sv->sv_tv = *STACK_TV_BOT(0);
3428 }
3429 break;
3430
3431 // store option
3432 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003433 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003435 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3436 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 long n = 0;
3438 char_u *s = NULL;
3439 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003440 char_u numbuf[NUMBUFLEN];
3441 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442
Bram Moolenaar4c137212021-04-19 16:48:48 +02003443 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 tv = STACK_TV_BOT(0);
3445 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003446 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003448 if (s == NULL)
3449 s = (char_u *)"";
3450 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003451 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3452 {
3453 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003454 // If the option can be set to a function reference or
3455 // a lambda and the passed value is a function
3456 // reference, then convert it to the name (string) of
3457 // the function reference.
3458 s = tv2string(tv, &tofree, numbuf, 0);
3459 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003460 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003461 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003462 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003463 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003464 goto on_error;
3465 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003468 // must be VAR_NUMBER, CHECKTYPE makes sure
3469 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003470 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003471 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003472 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 if (msg != NULL)
3474 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003475 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003477 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 }
3480 break;
3481
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003482 // store $ENV
3483 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003484 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003485 tv = STACK_TV_BOT(0);
3486 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3487 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003488 break;
3489
3490 // store @r
3491 case ISN_STOREREG:
3492 {
3493 int reg = iptr->isn_arg.number;
3494
Bram Moolenaar4c137212021-04-19 16:48:48 +02003495 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003496 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003497 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003498 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003499 }
3500 break;
3501
3502 // store v: variable
3503 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003504 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003505 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3506 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003507 // should not happen, type is checked when compiling
3508 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003509 break;
3510
Bram Moolenaard3aac292020-04-19 14:32:17 +02003511 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003513 case ISN_STOREB:
3514 case ISN_STOREW:
3515 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003517 dictitem_T *di;
3518 hashtab_T *ht;
3519 char_u *name = iptr->isn_arg.string + 2;
3520
Bram Moolenaard3aac292020-04-19 14:32:17 +02003521 switch (iptr->isn_type)
3522 {
3523 case ISN_STOREG:
3524 ht = get_globvar_ht();
3525 break;
3526 case ISN_STOREB:
3527 ht = &curbuf->b_vars->dv_hashtab;
3528 break;
3529 case ISN_STOREW:
3530 ht = &curwin->w_vars->dv_hashtab;
3531 break;
3532 case ISN_STORET:
3533 ht = &curtab->tp_vars->dv_hashtab;
3534 break;
3535 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003536 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538
Bram Moolenaar4c137212021-04-19 16:48:48 +02003539 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003540 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003542 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 else
3544 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003545 SOURCING_LNUM = iptr->isn_lnum;
3546 if (var_check_permission(di, name) == FAIL)
3547 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 clear_tv(&di->di_tv);
3549 di->di_tv = *STACK_TV_BOT(0);
3550 }
3551 }
3552 break;
3553
Bram Moolenaar03290b82020-12-19 16:30:44 +01003554 // store an autoload variable
3555 case ISN_STOREAUTO:
3556 SOURCING_LNUM = iptr->isn_lnum;
3557 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3558 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003559 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003560 break;
3561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 // store number in local variable
3563 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003564 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 clear_tv(tv);
3566 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003567 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 break;
3569
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003570 // store value in list or dict variable
3571 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003572 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003573 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003574
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003575 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003576 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003577 if (res == NOTDONE)
3578 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003579 }
3580 break;
3581
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003582 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003583 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003584 if (execute_storerange(iptr, ectx) == FAIL)
3585 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003586 break;
3587
Bram Moolenaar0186e582021-01-10 18:33:11 +01003588 // load or store variable or argument from outer scope
3589 case ISN_LOADOUTER:
3590 case ISN_STOREOUTER:
3591 {
3592 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003593 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3594 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003595
3596 while (depth > 1 && outer != NULL)
3597 {
3598 outer = outer->out_up;
3599 --depth;
3600 }
3601 if (outer == NULL)
3602 {
3603 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003604 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3605 || ectx->ec_outer_ref == NULL)
3606 // Possibly :def function called from legacy
3607 // context.
3608 emsg(_(e_closure_called_from_invalid_context));
3609 else
3610 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003611 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003612 }
3613 tv = ((typval_T *)outer->out_stack->ga_data)
3614 + outer->out_frame_idx + STACK_FRAME_SIZE
3615 + iptr->isn_arg.outer.outer_idx;
3616 if (iptr->isn_type == ISN_LOADOUTER)
3617 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003618 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003619 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003620 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003621 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003622 }
3623 else
3624 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003625 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003626 clear_tv(tv);
3627 *tv = *STACK_TV_BOT(0);
3628 }
3629 }
3630 break;
3631
Bram Moolenaar752fc692021-01-04 21:57:11 +01003632 // unlet item in list or dict variable
3633 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003634 if (execute_unletindex(iptr, ectx) == FAIL)
3635 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003636 break;
3637
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003638 // unlet range of items in list variable
3639 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003640 if (execute_unletrange(iptr, ectx) == FAIL)
3641 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003642 break;
3643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 // push constant
3645 case ISN_PUSHNR:
3646 case ISN_PUSHBOOL:
3647 case ISN_PUSHSPEC:
3648 case ISN_PUSHF:
3649 case ISN_PUSHS:
3650 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003651 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003652 case ISN_PUSHCHANNEL:
3653 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003654 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003655 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003657 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003658 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 switch (iptr->isn_type)
3660 {
3661 case ISN_PUSHNR:
3662 tv->v_type = VAR_NUMBER;
3663 tv->vval.v_number = iptr->isn_arg.number;
3664 break;
3665 case ISN_PUSHBOOL:
3666 tv->v_type = VAR_BOOL;
3667 tv->vval.v_number = iptr->isn_arg.number;
3668 break;
3669 case ISN_PUSHSPEC:
3670 tv->v_type = VAR_SPECIAL;
3671 tv->vval.v_number = iptr->isn_arg.number;
3672 break;
3673#ifdef FEAT_FLOAT
3674 case ISN_PUSHF:
3675 tv->v_type = VAR_FLOAT;
3676 tv->vval.v_float = iptr->isn_arg.fnumber;
3677 break;
3678#endif
3679 case ISN_PUSHBLOB:
3680 blob_copy(iptr->isn_arg.blob, tv);
3681 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003682 case ISN_PUSHFUNC:
3683 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003684 if (iptr->isn_arg.string == NULL)
3685 tv->vval.v_string = NULL;
3686 else
3687 tv->vval.v_string =
3688 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003689 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003690 case ISN_PUSHCHANNEL:
3691#ifdef FEAT_JOB_CHANNEL
3692 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003693 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003694#endif
3695 break;
3696 case ISN_PUSHJOB:
3697#ifdef FEAT_JOB_CHANNEL
3698 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003699 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003700#endif
3701 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 default:
3703 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003704 tv->vval.v_string = iptr->isn_arg.string == NULL
3705 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 }
3707 break;
3708
Bram Moolenaar06b77222022-01-25 15:51:56 +00003709 case ISN_AUTOLOAD:
3710 {
3711 char_u *name = iptr->isn_arg.string;
3712
3713 (void)script_autoload(name, FALSE);
3714 if (find_func(name, TRUE))
3715 {
3716 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3717 goto theend;
3718 tv = STACK_TV_BOT(0);
3719 tv->v_lock = 0;
3720 ++ectx->ec_stack.ga_len;
3721 tv->v_type = VAR_FUNC;
3722 tv->vval.v_string = vim_strsave(name);
3723 }
3724 else
3725 {
3726 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3727
3728 if (res == NOTDONE)
3729 goto theend;
3730 if (res == FAIL)
3731 goto on_error;
3732 }
3733 }
3734 break;
3735
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003736 case ISN_UNLET:
3737 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3738 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003739 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003740 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003741 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003742 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003743 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003744
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003745 case ISN_LOCKUNLOCK:
3746 {
3747 typval_T *lval_root_save = lval_root;
3748 int res;
3749
3750 // Stack has the local variable, argument the whole :lock
3751 // or :unlock command, like ISN_EXEC.
3752 --ectx->ec_stack.ga_len;
3753 lval_root = STACK_TV_BOT(0);
3754 res = exec_command(iptr);
3755 clear_tv(lval_root);
3756 lval_root = lval_root_save;
3757 if (res == FAIL)
3758 goto on_error;
3759 }
3760 break;
3761
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003762 case ISN_LOCKCONST:
3763 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3764 break;
3765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 // create a list from items on the stack; uses a single allocation
3767 // for the list header and the items
3768 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003769 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003770 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 break;
3772
3773 // create a dict from items on the stack
3774 case ISN_NEWDICT:
3775 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003776 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003778 SOURCING_LNUM = iptr->isn_lnum;
3779 res = exe_newdict(iptr->isn_arg.number, ectx);
3780 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003781 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003782 if (res == MAYBE)
3783 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 }
3785 break;
3786
LemonBoy372bcce2022-04-25 12:43:20 +01003787 case ISN_CONCAT:
3788 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3789 goto theend;
3790 break;
3791
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003792 // create a partial with NULL value
3793 case ISN_NEWPARTIAL:
3794 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3795 goto theend;
3796 ++ectx->ec_stack.ga_len;
3797 tv = STACK_TV_BOT(-1);
3798 tv->v_type = VAR_PARTIAL;
3799 tv->v_lock = 0;
3800 tv->vval.v_partial = NULL;
3801 break;
3802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 // call a :def function
3804 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003805 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003806 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3807 NULL,
3808 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003809 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003810 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 break;
3812
3813 // call a builtin function
3814 case ISN_BCALL:
3815 SOURCING_LNUM = iptr->isn_lnum;
3816 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3817 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003818 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003819 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 break;
3821
3822 // call a funcref or partial
3823 case ISN_PCALL:
3824 {
3825 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3826 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003827 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828
3829 SOURCING_LNUM = iptr->isn_lnum;
3830 if (pfunc->cpf_top)
3831 {
3832 // funcref is above the arguments
3833 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3834 }
3835 else
3836 {
3837 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003838 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003839 partial_tv = *STACK_TV_BOT(0);
3840 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003842 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003843 if (tv == &partial_tv)
3844 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003846 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 }
3848 break;
3849
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003850 case ISN_PCALL_END:
3851 // PCALL finished, arguments have been consumed and replaced by
3852 // the return value. Now clear the funcref from the stack,
3853 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003854 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003855 clear_tv(STACK_TV_BOT(-1));
3856 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3857 break;
3858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 // call a user defined function or funcref/partial
3860 case ISN_UCALL:
3861 {
3862 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3863
3864 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003865 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003866 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003867 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 }
3869 break;
3870
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003871 // :defer func(arg)
3872 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01003873 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003874 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
3875 goto on_error;
3876 break;
3877
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003878 // return from a :def function call without a value
3879 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003880 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003881 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003882 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003883 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003884 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003885 tv->vval.v_number = 0;
3886 tv->v_lock = 0;
3887 // FALLTHROUGH
3888
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003889 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 case ISN_RETURN:
3891 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003892 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003893 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003894
3895 if (trystack->ga_len > 0)
3896 trycmd = ((trycmd_T *)trystack->ga_data)
3897 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003898 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003899 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003901 // jump to ":finally" or ":endtry"
3902 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003903 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003904 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003905 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 trycmd->tcd_return = TRUE;
3907 }
3908 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003909 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 }
3911 break;
3912
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003913 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 case ISN_FUNCREF:
3915 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003916 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003917 ufunc_T *ufunc;
3918 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003921 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003922 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003923 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003924 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003925 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003926 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003927 if (funcref->fr_func_name == NULL)
3928 {
3929 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3930 + funcref->fr_dfunc_idx;
3931
3932 ufunc = pt_dfunc->df_ufunc;
3933 }
3934 else
3935 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003936 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003937 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003938 if (ufunc == NULL)
3939 {
3940 SOURCING_LNUM = iptr->isn_lnum;
3941 iemsg("ufunc unexpectedly NULL for FUNCREF");
3942 goto theend;
3943 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003944 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003945 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003947 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 tv->vval.v_partial = pt;
3949 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003950 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 }
3952 break;
3953
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003954 // Create a global function from a lambda.
3955 case ISN_NEWFUNC:
3956 {
3957 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3958
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003959 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003960 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003961 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003962 }
3963 break;
3964
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003965 // List functions
3966 case ISN_DEF:
3967 if (iptr->isn_arg.string == NULL)
3968 list_functions(NULL);
3969 else
3970 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003971 exarg_T ea;
3972 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003973
3974 CLEAR_FIELD(ea);
3975 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003976 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3977 define_function(&ea, NULL, &lines_to_free);
3978 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003979 }
3980 break;
3981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 // jump if a condition is met
3983 case ISN_JUMP:
3984 {
3985 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003986 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 int jump = TRUE;
3988
3989 if (when != JUMP_ALWAYS)
3990 {
3991 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003992 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003993 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003994 || when == JUMP_IF_COND_TRUE)
3995 {
3996 SOURCING_LNUM = iptr->isn_lnum;
3997 jump = tv_get_bool_chk(tv, &error);
3998 if (error)
3999 goto on_error;
4000 }
4001 else
4002 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004003 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004005 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 {
4007 // drop the value from the stack
4008 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004009 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 }
4011 }
4012 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004013 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 }
4015 break;
4016
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004017 // "while": jump to end if a condition is false
4018 case ISN_WHILE:
4019 {
4020 int error = FALSE;
4021 int jump = TRUE;
4022
4023 tv = STACK_TV_BOT(-1);
4024 SOURCING_LNUM = iptr->isn_lnum;
4025 jump = !tv_get_bool_chk(tv, &error);
4026 if (error)
4027 goto on_error;
4028 // drop the value from the stack
4029 clear_tv(tv);
4030 --ectx->ec_stack.ga_len;
4031 if (jump)
4032 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4033
4034 // Store the current funccal count, may be used by
4035 // ISN_LOOPEND later
4036 tv = STACK_TV_VAR(
4037 iptr->isn_arg.whileloop.while_funcref_idx);
4038 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4039 }
4040 break;
4041
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004042 // Jump if an argument with a default value was already set and not
4043 // v:none.
4044 case ISN_JUMP_IF_ARG_SET:
4045 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
4046 if (tv->v_type != VAR_UNKNOWN
4047 && !(tv->v_type == VAR_SPECIAL
4048 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004049 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004050 break;
4051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 // top of a for loop
4053 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004054 if (execute_for(iptr, ectx) == FAIL)
4055 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 break;
4057
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004058 // end of a for or while loop
4059 case ISN_ENDLOOP:
4060 if (execute_endloop(iptr, ectx) == FAIL)
4061 goto theend;
4062 break;
4063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 // start of ":try" block
4065 case ISN_TRY:
4066 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004067 trycmd_T *trycmd = NULL;
4068
Bram Moolenaar35578162021-08-02 19:10:38 +02004069 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004070 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004071 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4072 + ectx->ec_trystack.ga_len;
4073 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004075 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004076 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4077 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004078 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004079 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004080 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004081 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004082 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004083 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 }
4085 break;
4086
4087 case ISN_PUSHEXC:
4088 if (current_exception == NULL)
4089 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004090 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004092 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004094 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004095 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004097 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004099 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 tv->vval.v_string = vim_strsave(
4101 (char_u *)current_exception->value);
4102 break;
4103
4104 case ISN_CATCH:
4105 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004106 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004107 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108
Bram Moolenaar4c137212021-04-19 16:48:48 +02004109 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004110 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004112 trycmd->tcd_caught = TRUE;
4113 trycmd->tcd_did_throw = FALSE;
4114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004116 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 catch_exception(current_exception);
4118 }
4119 break;
4120
Bram Moolenaarc150c092021-02-13 15:02:46 +01004121 case ISN_TRYCONT:
4122 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004123 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004124 trycont_T *trycont = &iptr->isn_arg.trycont;
4125 int i;
4126 trycmd_T *trycmd;
4127 int iidx = trycont->tct_where;
4128
4129 if (trystack->ga_len < trycont->tct_levels)
4130 {
4131 siemsg("TRYCONT: expected %d levels, found %d",
4132 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004133 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004134 }
4135 // Make :endtry jump to any outer try block and the last
4136 // :endtry inside the loop to the loop start.
4137 for (i = trycont->tct_levels; i > 0; --i)
4138 {
4139 trycmd = ((trycmd_T *)trystack->ga_data)
4140 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004141 // Add one to tcd_cont to be able to jump to
4142 // instruction with index zero.
4143 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004144 iidx = trycmd->tcd_finally_idx == 0
4145 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004146 }
4147 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004148 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004149 }
4150 break;
4151
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004152 case ISN_FINALLY:
4153 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004154 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004155 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4156 + trystack->ga_len - 1;
4157
4158 // Reset the index to avoid a return statement jumps here
4159 // again.
4160 trycmd->tcd_finally_idx = 0;
4161 break;
4162 }
4163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 // end of ":try" block
4165 case ISN_ENDTRY:
4166 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004167 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004168 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169
Bram Moolenaar06651632022-04-27 17:54:25 +01004170 --trystack->ga_len;
4171 --trylevel;
4172 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4173 if (trycmd->tcd_did_throw)
4174 did_throw = TRUE;
4175 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004177 // discard the exception
4178 if (caught_stack == current_exception)
4179 caught_stack = caught_stack->caught;
4180 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004182
4183 if (trycmd->tcd_return)
4184 goto func_return;
4185
4186 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4187 {
4188 --ectx->ec_stack.ga_len;
4189 clear_tv(STACK_TV_BOT(0));
4190 }
4191 if (trycmd->tcd_cont != 0)
4192 // handling :continue: jump to outer try block or
4193 // start of the loop
4194 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 }
4196 break;
4197
4198 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004199 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004200 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004201
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004202 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4203 {
4204 // throwing an exception while using "silent!" causes
4205 // the function to abort but not display an error.
4206 tv = STACK_TV_BOT(-1);
4207 clear_tv(tv);
4208 tv->v_type = VAR_NUMBER;
4209 tv->vval.v_number = 0;
4210 goto done;
4211 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004212 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004213 tv = STACK_TV_BOT(0);
4214 if (tv->vval.v_string == NULL
4215 || *skipwhite(tv->vval.v_string) == NUL)
4216 {
4217 vim_free(tv->vval.v_string);
4218 SOURCING_LNUM = iptr->isn_lnum;
4219 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004220 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004221 }
4222
4223 // Inside a "catch" we need to first discard the caught
4224 // exception.
4225 if (trystack->ga_len > 0)
4226 {
4227 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4228 + trystack->ga_len - 1;
4229 if (trycmd->tcd_caught && current_exception != NULL)
4230 {
4231 // discard the exception
4232 if (caught_stack == current_exception)
4233 caught_stack = caught_stack->caught;
4234 discard_current_exception();
4235 trycmd->tcd_caught = FALSE;
4236 }
4237 }
4238
Bram Moolenaar90a57162022-02-12 14:23:17 +00004239 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004240 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4241 == FAIL)
4242 {
4243 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004244 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004245 }
4246 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 break;
4249
4250 // compare with special values
4251 case ISN_COMPAREBOOL:
4252 case ISN_COMPARESPECIAL:
4253 {
4254 typval_T *tv1 = STACK_TV_BOT(-2);
4255 typval_T *tv2 = STACK_TV_BOT(-1);
4256 varnumber_T arg1 = tv1->vval.v_number;
4257 varnumber_T arg2 = tv2->vval.v_number;
4258 int res;
4259
Bram Moolenaar06651632022-04-27 17:54:25 +01004260 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4261 res = arg1 == arg2;
4262 else
4263 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264
Bram Moolenaar4c137212021-04-19 16:48:48 +02004265 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 tv1->v_type = VAR_BOOL;
4267 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4268 }
4269 break;
4270
Bram Moolenaar7a222242022-03-01 19:23:24 +00004271 case ISN_COMPARENULL:
4272 {
4273 typval_T *tv1 = STACK_TV_BOT(-2);
4274 typval_T *tv2 = STACK_TV_BOT(-1);
4275 int res;
4276
4277 res = typval_compare_null(tv1, tv2);
4278 if (res == MAYBE)
4279 goto on_error;
4280 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4281 res = !res;
4282 clear_tv(tv1);
4283 clear_tv(tv2);
4284 --ectx->ec_stack.ga_len;
4285 tv1->v_type = VAR_BOOL;
4286 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4287 }
4288 break;
4289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 // Operation with two number arguments
4291 case ISN_OPNR:
4292 case ISN_COMPARENR:
4293 {
4294 typval_T *tv1 = STACK_TV_BOT(-2);
4295 typval_T *tv2 = STACK_TV_BOT(-1);
4296 varnumber_T arg1 = tv1->vval.v_number;
4297 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004298 varnumber_T res = 0;
4299 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004301 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4302 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4303 {
4304 if (arg2 < 0)
4305 {
4306 SOURCING_LNUM = iptr->isn_lnum;
4307 emsg(_(e_bitshift_ops_must_be_postive));
4308 goto on_error;
4309 }
4310 }
4311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 switch (iptr->isn_arg.op.op_type)
4313 {
4314 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004315 case EXPR_DIV: if (arg2 == 0)
4316 div_zero = TRUE;
4317 else
4318 res = arg1 / arg2;
4319 break;
4320 case EXPR_REM: if (arg2 == 0)
4321 div_zero = TRUE;
4322 else
4323 res = arg1 % arg2;
4324 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 case EXPR_SUB: res = arg1 - arg2; break;
4326 case EXPR_ADD: res = arg1 + arg2; break;
4327
4328 case EXPR_EQUAL: res = arg1 == arg2; break;
4329 case EXPR_NEQUAL: res = arg1 != arg2; break;
4330 case EXPR_GREATER: res = arg1 > arg2; break;
4331 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4332 case EXPR_SMALLER: res = arg1 < arg2; break;
4333 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004334 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4335 res = 0;
4336 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004337 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004338 break;
4339 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4340 res = 0;
4341 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004342 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004343 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004344 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 }
4346
Bram Moolenaar4c137212021-04-19 16:48:48 +02004347 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 if (iptr->isn_type == ISN_COMPARENR)
4349 {
4350 tv1->v_type = VAR_BOOL;
4351 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4352 }
4353 else
4354 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004355 if (div_zero)
4356 {
4357 SOURCING_LNUM = iptr->isn_lnum;
4358 emsg(_(e_divide_by_zero));
4359 goto on_error;
4360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 }
4362 break;
4363
4364 // Computation with two float arguments
4365 case ISN_OPFLOAT:
4366 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004367#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 {
4369 typval_T *tv1 = STACK_TV_BOT(-2);
4370 typval_T *tv2 = STACK_TV_BOT(-1);
4371 float_T arg1 = tv1->vval.v_float;
4372 float_T arg2 = tv2->vval.v_float;
4373 float_T res = 0;
4374 int cmp = FALSE;
4375
4376 switch (iptr->isn_arg.op.op_type)
4377 {
4378 case EXPR_MULT: res = arg1 * arg2; break;
4379 case EXPR_DIV: res = arg1 / arg2; break;
4380 case EXPR_SUB: res = arg1 - arg2; break;
4381 case EXPR_ADD: res = arg1 + arg2; break;
4382
4383 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4384 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4385 case EXPR_GREATER: cmp = arg1 > arg2; break;
4386 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4387 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4388 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4389 default: cmp = 0; break;
4390 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004391 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 if (iptr->isn_type == ISN_COMPAREFLOAT)
4393 {
4394 tv1->v_type = VAR_BOOL;
4395 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4396 }
4397 else
4398 tv1->vval.v_float = res;
4399 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004400#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 break;
4402
4403 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004404 case ISN_COMPAREDICT:
4405 case ISN_COMPAREFUNC:
4406 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 case ISN_COMPAREBLOB:
4408 {
4409 typval_T *tv1 = STACK_TV_BOT(-2);
4410 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004411 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4412 int ic = iptr->isn_arg.op.op_ic;
4413 int res = FALSE;
4414 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415
Bram Moolenaar265f8112021-12-19 12:33:05 +00004416 SOURCING_LNUM = iptr->isn_lnum;
4417 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004419 status = typval_compare_list(tv1, tv2,
4420 exprtype, ic, &res);
4421 }
4422 else if (iptr->isn_type == ISN_COMPAREDICT)
4423 {
4424 status = typval_compare_dict(tv1, tv2,
4425 exprtype, ic, &res);
4426 }
4427 else if (iptr->isn_type == ISN_COMPAREFUNC)
4428 {
4429 status = typval_compare_func(tv1, tv2,
4430 exprtype, ic, &res);
4431 }
4432 else if (iptr->isn_type == ISN_COMPARESTRING)
4433 {
4434 status = typval_compare_string(tv1, tv2,
4435 exprtype, ic, &res);
4436 }
4437 else
4438 {
4439 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004441 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 clear_tv(tv1);
4443 clear_tv(tv2);
4444 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004445 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4446 if (status == FAIL)
4447 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 }
4449 break;
4450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 case ISN_COMPAREANY:
4452 {
4453 typval_T *tv1 = STACK_TV_BOT(-2);
4454 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004455 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004457 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458
Bram Moolenaareb26f432020-09-14 16:50:05 +02004459 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004460 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004462 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004463 if (status == FAIL)
4464 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 }
4466 break;
4467
4468 case ISN_ADDLIST:
4469 case ISN_ADDBLOB:
4470 {
4471 typval_T *tv1 = STACK_TV_BOT(-2);
4472 typval_T *tv2 = STACK_TV_BOT(-1);
4473
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004474 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004476 {
4477 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4478 && tv1->vval.v_list != NULL)
4479 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4480 NULL);
4481 else
4482 eval_addlist(tv1, tv2);
4483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 else
4485 eval_addblob(tv1, tv2);
4486 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004487 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 }
4489 break;
4490
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004491 case ISN_LISTAPPEND:
4492 {
4493 typval_T *tv1 = STACK_TV_BOT(-2);
4494 typval_T *tv2 = STACK_TV_BOT(-1);
4495 list_T *l = tv1->vval.v_list;
4496
4497 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004498 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004499 if (l == NULL)
4500 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004501 emsg(_(e_cannot_add_to_null_list));
4502 goto on_error;
4503 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004504 if (value_check_lock(l->lv_lock, NULL, FALSE))
4505 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004506 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004507 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004508 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004509 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004510 }
4511 break;
4512
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004513 case ISN_BLOBAPPEND:
4514 {
4515 typval_T *tv1 = STACK_TV_BOT(-2);
4516 typval_T *tv2 = STACK_TV_BOT(-1);
4517 blob_T *b = tv1->vval.v_blob;
4518 int error = FALSE;
4519 varnumber_T n;
4520
4521 // add a number to a blob
4522 if (b == NULL)
4523 {
4524 SOURCING_LNUM = iptr->isn_lnum;
4525 emsg(_(e_cannot_add_to_null_blob));
4526 goto on_error;
4527 }
4528 n = tv_get_number_chk(tv2, &error);
4529 if (error)
4530 goto on_error;
4531 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004532 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004533 }
4534 break;
4535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 // Computation with two arguments of unknown type
4537 case ISN_OPANY:
4538 {
4539 typval_T *tv1 = STACK_TV_BOT(-2);
4540 typval_T *tv2 = STACK_TV_BOT(-1);
4541 varnumber_T n1, n2;
4542#ifdef FEAT_FLOAT
4543 float_T f1 = 0, f2 = 0;
4544#endif
4545 int error = FALSE;
4546
4547 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4548 {
4549 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4550 {
4551 eval_addlist(tv1, tv2);
4552 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004553 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 break;
4555 }
4556 else if (tv1->v_type == VAR_BLOB
4557 && tv2->v_type == VAR_BLOB)
4558 {
4559 eval_addblob(tv1, tv2);
4560 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004561 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 break;
4563 }
4564 }
4565#ifdef FEAT_FLOAT
4566 if (tv1->v_type == VAR_FLOAT)
4567 {
4568 f1 = tv1->vval.v_float;
4569 n1 = 0;
4570 }
4571 else
4572#endif
4573 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004574 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 n1 = tv_get_number_chk(tv1, &error);
4576 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004577 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578#ifdef FEAT_FLOAT
4579 if (tv2->v_type == VAR_FLOAT)
4580 f1 = n1;
4581#endif
4582 }
4583#ifdef FEAT_FLOAT
4584 if (tv2->v_type == VAR_FLOAT)
4585 {
4586 f2 = tv2->vval.v_float;
4587 n2 = 0;
4588 }
4589 else
4590#endif
4591 {
4592 n2 = tv_get_number_chk(tv2, &error);
4593 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004594 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595#ifdef FEAT_FLOAT
4596 if (tv1->v_type == VAR_FLOAT)
4597 f2 = n2;
4598#endif
4599 }
4600#ifdef FEAT_FLOAT
4601 // if there is a float on either side the result is a float
4602 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4603 {
4604 switch (iptr->isn_arg.op.op_type)
4605 {
4606 case EXPR_MULT: f1 = f1 * f2; break;
4607 case EXPR_DIV: f1 = f1 / f2; break;
4608 case EXPR_SUB: f1 = f1 - f2; break;
4609 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004610 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004611 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004612 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 }
4614 clear_tv(tv1);
4615 clear_tv(tv2);
4616 tv1->v_type = VAR_FLOAT;
4617 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004618 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 }
4620 else
4621#endif
4622 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004623 int failed = FALSE;
4624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 switch (iptr->isn_arg.op.op_type)
4626 {
4627 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004628 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4629 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004630 goto on_error;
4631 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 case EXPR_SUB: n1 = n1 - n2; break;
4633 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004634 default: n1 = num_modulus(n1, n2, &failed);
4635 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004636 goto on_error;
4637 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 }
4639 clear_tv(tv1);
4640 clear_tv(tv2);
4641 tv1->v_type = VAR_NUMBER;
4642 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004643 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 }
4645 }
4646 break;
4647
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004648 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004649 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004650 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004651 int is_slice = iptr->isn_type == ISN_STRSLICE;
4652 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004653 char_u *res;
4654
4655 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004656 // string slice: string is at stack-3, first index at
4657 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004658 if (is_slice)
4659 {
4660 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004661 n1 = tv->vval.v_number;
4662 }
4663
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004664 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004665 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004666
Bram Moolenaar4c137212021-04-19 16:48:48 +02004667 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004668 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004669 if (is_slice)
4670 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004671 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004672 else
4673 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004674 // single character (including composing characters).
4675 // If the index is too big or negative the result is
4676 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004677 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004678 vim_free(tv->vval.v_string);
4679 tv->vval.v_string = res;
4680 }
4681 break;
4682
4683 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004684 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004685 case ISN_BLOBINDEX:
4686 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004688 int is_slice = iptr->isn_type == ISN_LISTSLICE
4689 || iptr->isn_type == ISN_BLOBSLICE;
4690 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4691 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004692 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004693 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694
4695 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004696 // list slice: list is at stack-3, indexes at stack-2 and
4697 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004698 // Same for blob.
4699 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700
4701 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004702 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004704
4705 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 {
Bram Moolenaared591872020-08-15 22:14:53 +02004707 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004708 n1 = tv->vval.v_number;
4709 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 }
Bram Moolenaared591872020-08-15 22:14:53 +02004711
Bram Moolenaar4c137212021-04-19 16:48:48 +02004712 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004713 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004714 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004715 if (is_blob)
4716 {
4717 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4718 n1, n2, FALSE, tv) == FAIL)
4719 goto on_error;
4720 }
4721 else
4722 {
4723 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4724 n1, n2, FALSE, tv, TRUE) == FAIL)
4725 goto on_error;
4726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 }
4728 break;
4729
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004730 case ISN_ANYINDEX:
4731 case ISN_ANYSLICE:
4732 {
4733 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4734 typval_T *var1, *var2;
4735 int res;
4736
4737 // index: composite is at stack-2, index at stack-1
4738 // slice: composite is at stack-3, indexes at stack-2 and
4739 // stack-1
4740 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004741 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004742 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4743 goto on_error;
4744 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4745 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004746 res = eval_index_inner(tv, is_slice, var1, var2,
4747 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004748 clear_tv(var1);
4749 if (is_slice)
4750 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004751 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004752 if (res == FAIL)
4753 goto on_error;
4754 }
4755 break;
4756
Bram Moolenaar9af78762020-06-16 11:34:42 +02004757 case ISN_SLICE:
4758 {
4759 list_T *list;
4760 int count = iptr->isn_arg.number;
4761
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004762 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004763 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004764 list = tv->vval.v_list;
4765
4766 // no error for short list, expect it to be checked earlier
4767 if (list != NULL && list->lv_len >= count)
4768 {
4769 list_T *newlist = list_slice(list,
4770 count, list->lv_len - 1);
4771
4772 if (newlist != NULL)
4773 {
4774 list_unref(list);
4775 tv->vval.v_list = newlist;
4776 ++newlist->lv_refcount;
4777 }
4778 }
4779 }
4780 break;
4781
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004782 case ISN_GETITEM:
4783 {
4784 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004785 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004786
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004787 // Get list item: list is at stack-1, push item.
4788 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004789 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4790 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004791
Bram Moolenaar35578162021-08-02 19:10:38 +02004792 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004793 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004794 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004795 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004796
4797 // Useful when used in unpack assignment. Reset at
4798 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004799 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004800 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004801 }
4802 break;
4803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 case ISN_MEMBER:
4805 {
4806 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004807 char_u *key;
4808 dictitem_T *di;
4809
4810 // dict member: dict is at stack-2, key at stack-1
4811 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004812 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004813 dict = tv->vval.v_dict;
4814
4815 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004816 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004817 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004818 if (key == NULL)
4819 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004820
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004821 if ((di = dict_find(dict, key, -1)) == NULL)
4822 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004823 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004824 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004825
4826 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004827 // stack contents makes sense and the dict stack is
4828 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004829 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004830 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004831 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004832 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004833 tv->v_type = VAR_NUMBER;
4834 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004835 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004836 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004837 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004838 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004839 // Put the dict used on the dict stack, it might be used by
4840 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004841 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004842 if (dict_stack_save(tv) == FAIL)
4843 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004844 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004845 }
4846 break;
4847
4848 // dict member with string key
4849 case ISN_STRINGMEMBER:
4850 {
4851 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 dictitem_T *di;
4853
4854 tv = STACK_TV_BOT(-1);
4855 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4856 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004857 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004858 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004859 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 }
4861 dict = tv->vval.v_dict;
4862
4863 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4864 == NULL)
4865 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004866 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004867 semsg(_(e_key_not_present_in_dictionary),
4868 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004869 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004871 // Put the dict used on the dict stack, it might be used by
4872 // a dict function later.
4873 if (dict_stack_save(tv) == FAIL)
4874 goto on_fatal_error;
4875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004877 }
4878 break;
4879
4880 case ISN_CLEARDICT:
4881 dict_stack_drop();
4882 break;
4883
4884 case ISN_USEDICT:
4885 {
4886 typval_T *dict_tv = dict_stack_get_tv();
4887
4888 // Turn "dict.Func" into a partial for "Func" bound to
4889 // "dict". Don't do this when "Func" is already a partial
4890 // that was bound explicitly (pt_auto is FALSE).
4891 tv = STACK_TV_BOT(-1);
4892 if (dict_tv != NULL
4893 && dict_tv->v_type == VAR_DICT
4894 && dict_tv->vval.v_dict != NULL
4895 && (tv->v_type == VAR_FUNC
4896 || (tv->v_type == VAR_PARTIAL
4897 && (tv->vval.v_partial->pt_auto
4898 || tv->vval.v_partial->pt_dict == NULL))))
4899 dict_tv->vval.v_dict =
4900 make_partial(dict_tv->vval.v_dict, tv);
4901 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 }
4903 break;
4904
4905 case ISN_NEGATENR:
4906 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004907 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004908#ifdef FEAT_FLOAT
4909 if (tv->v_type == VAR_FLOAT)
4910 tv->vval.v_float = -tv->vval.v_float;
4911 else
4912#endif
4913 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 break;
4915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 case ISN_CHECKTYPE:
4917 {
4918 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004919 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004920 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004922 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004923 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004924 if (!ectx->ec_where.wt_variable)
4925 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004926 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004927 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004928 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004929 if (r == FAIL)
4930 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004931 if (!ectx->ec_where.wt_variable)
4932 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004933
4934 // number 0 is FALSE, number 1 is TRUE
4935 if (tv->v_type == VAR_NUMBER
4936 && ct->ct_type->tt_type == VAR_BOOL
4937 && (tv->vval.v_number == 0
4938 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004940 tv->v_type = VAR_BOOL;
4941 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004942 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 }
4944 }
4945 break;
4946
Bram Moolenaar9af78762020-06-16 11:34:42 +02004947 case ISN_CHECKLEN:
4948 {
4949 int min_len = iptr->isn_arg.checklen.cl_min_len;
4950 list_T *list = NULL;
4951
4952 tv = STACK_TV_BOT(-1);
4953 if (tv->v_type == VAR_LIST)
4954 list = tv->vval.v_list;
4955 if (list == NULL || list->lv_len < min_len
4956 || (list->lv_len > min_len
4957 && !iptr->isn_arg.checklen.cl_more_OK))
4958 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004959 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004960 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004961 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004962 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004963 }
4964 }
4965 break;
4966
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004967 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004968 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004969 break;
4970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004972 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 {
4974 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004975 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004977 if (iptr->isn_type == ISN_2BOOL)
4978 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004979 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004980 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004981 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004982 n = !n;
4983 }
4984 else
4985 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004986 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004987 SOURCING_LNUM = iptr->isn_lnum;
4988 n = tv_get_bool_chk(tv, &error);
4989 if (error)
4990 goto on_error;
4991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 clear_tv(tv);
4993 tv->v_type = VAR_BOOL;
4994 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4995 }
4996 break;
4997
4998 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004999 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005000 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005001 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5002 iptr->isn_type == ISN_2STRING_ANY,
5003 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005004 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 break;
5006
Bram Moolenaar08597872020-12-10 19:43:40 +01005007 case ISN_RANGE:
5008 {
5009 exarg_T ea;
5010 char *errormsg;
5011
Bram Moolenaarece0b872021-01-08 20:40:45 +01005012 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005013 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005014 ea.addr_type = ADDR_LINES;
5015 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005016 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005017 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005018 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005019
Bram Moolenaar35578162021-08-02 19:10:38 +02005020 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005021 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005022 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005023 tv = STACK_TV_BOT(-1);
5024 tv->v_type = VAR_NUMBER;
5025 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005026 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005027 }
5028 break;
5029
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005030 case ISN_PUT:
5031 {
5032 int regname = iptr->isn_arg.put.put_regname;
5033 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5034 char_u *expr = NULL;
5035 int dir = FORWARD;
5036
Bram Moolenaar08597872020-12-10 19:43:40 +01005037 if (lnum < -2)
5038 {
5039 // line number was put on the stack by ISN_RANGE
5040 tv = STACK_TV_BOT(-1);
5041 curwin->w_cursor.lnum = tv->vval.v_number;
5042 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5043 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005044 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005045 }
5046 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005047 // :put! above cursor
5048 dir = BACKWARD;
5049 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005050 {
5051 curwin->w_cursor.lnum = lnum;
5052 if (lnum == 0)
5053 // check_cursor() below will move to line 1
5054 dir = BACKWARD;
5055 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005056
5057 if (regname == '=')
5058 {
5059 tv = STACK_TV_BOT(-1);
5060 if (tv->v_type == VAR_STRING)
5061 expr = tv->vval.v_string;
5062 else
5063 {
5064 expr = typval2string(tv, TRUE); // allocates value
5065 clear_tv(tv);
5066 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005067 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005068 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005069 check_cursor();
5070 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5071 vim_free(expr);
5072 }
5073 break;
5074
Bram Moolenaar02194d22020-10-24 23:08:38 +02005075 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005076 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5077 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5078 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5079 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005080 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5081 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005082 break;
5083
Bram Moolenaar02194d22020-10-24 23:08:38 +02005084 case ISN_CMDMOD_REV:
5085 // filter regprog is owned by the instruction, don't free it
5086 cmdmod.cmod_filter_regmatch.regprog = NULL;
5087 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005088 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5089 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005090 break;
5091
Bram Moolenaar792f7862020-11-23 08:31:18 +01005092 case ISN_UNPACK:
5093 {
5094 int count = iptr->isn_arg.unpack.unp_count;
5095 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5096 list_T *l;
5097 listitem_T *li;
5098 int i;
5099
5100 // Check there is a valid list to unpack.
5101 tv = STACK_TV_BOT(-1);
5102 if (tv->v_type != VAR_LIST)
5103 {
5104 SOURCING_LNUM = iptr->isn_lnum;
5105 emsg(_(e_for_argument_must_be_sequence_of_lists));
5106 goto on_error;
5107 }
5108 l = tv->vval.v_list;
5109 if (l == NULL
5110 || l->lv_len < (semicolon ? count - 1 : count))
5111 {
5112 SOURCING_LNUM = iptr->isn_lnum;
5113 emsg(_(e_list_value_does_not_have_enough_items));
5114 goto on_error;
5115 }
5116 else if (!semicolon && l->lv_len > count)
5117 {
5118 SOURCING_LNUM = iptr->isn_lnum;
5119 emsg(_(e_list_value_has_more_items_than_targets));
5120 goto on_error;
5121 }
5122
5123 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005124 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005125 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005126 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005127
5128 // Variable after semicolon gets a list with the remaining
5129 // items.
5130 if (semicolon)
5131 {
5132 list_T *rem_list =
5133 list_alloc_with_items(l->lv_len - count + 1);
5134
5135 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005136 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005137 tv = STACK_TV_BOT(-count);
5138 tv->vval.v_list = rem_list;
5139 ++rem_list->lv_refcount;
5140 tv->v_lock = 0;
5141 li = l->lv_first;
5142 for (i = 0; i < count - 1; ++i)
5143 li = li->li_next;
5144 for (i = 0; li != NULL; ++i)
5145 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005146 typval_T tvcopy;
5147
5148 copy_tv(&li->li_tv, &tvcopy);
5149 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005150 li = li->li_next;
5151 }
5152 --count;
5153 }
5154
5155 // Produce the values in reverse order, first item last.
5156 li = l->lv_first;
5157 for (i = 0; i < count; ++i)
5158 {
5159 tv = STACK_TV_BOT(-i - 1);
5160 copy_tv(&li->li_tv, tv);
5161 li = li->li_next;
5162 }
5163
5164 list_unref(l);
5165 }
5166 break;
5167
Bram Moolenaarb2049902021-01-24 12:53:53 +01005168 case ISN_PROF_START:
5169 case ISN_PROF_END:
5170 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005171#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005172 funccall_T cookie;
5173 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005174 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005175 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005176
Bram Moolenaarca16c602022-09-06 18:57:08 +01005177 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005178 if (iptr->isn_type == ISN_PROF_START)
5179 {
5180 func_line_start(&cookie, iptr->isn_lnum);
5181 // if we get here the instruction is executed
5182 func_line_exec(&cookie);
5183 }
5184 else
5185 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005186#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005187 }
5188 break;
5189
Bram Moolenaare99d4222021-06-13 14:01:26 +02005190 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005191 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005192 break;
5193
Bram Moolenaar389df252020-07-09 21:20:47 +02005194 case ISN_SHUFFLE:
5195 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005196 typval_T tmp_tv;
5197 int item = iptr->isn_arg.shuffle.shfl_item;
5198 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005199
5200 tmp_tv = *STACK_TV_BOT(-item);
5201 for ( ; up > 0 && item > 1; --up)
5202 {
5203 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5204 --item;
5205 }
5206 *STACK_TV_BOT(-item) = tmp_tv;
5207 }
5208 break;
5209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005211 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005213 ectx->ec_where.wt_index = 0;
5214 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215 break;
5216 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005217 continue;
5218
Bram Moolenaard032f342020-07-18 18:13:02 +02005219func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005220 // Restore previous function. If the frame pointer is where we started
5221 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005222 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005223 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005224
Bram Moolenaar4c137212021-04-19 16:48:48 +02005225 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005226 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005227 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005228 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005229
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005230on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005231 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005232 // If "emsg_silent" is set then ignore the error, unless it was set
5233 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005234 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005235 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005236 {
5237 // If a sequence of instructions causes an error while ":silent!"
5238 // was used, restore the stack length and jump ahead to restoring
5239 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005240 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005241 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005242 while (ectx->ec_stack.ga_len
5243 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005244 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005245 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005246 clear_tv(STACK_TV_BOT(0));
5247 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005248 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5249 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005250 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005251 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005252 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005253on_fatal_error:
5254 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005255 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005256 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005257 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258 }
5259
5260done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005261 ret = OK;
5262theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005263 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005264
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005265 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005266 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5267 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005268}
5269
5270/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005271 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005272 * "rettv".
5273 * Return OK or FAIL.
5274 */
5275 int
5276exe_typval_instr(typval_T *tv, typval_T *rettv)
5277{
5278 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5279 isn_T *save_instr = ectx->ec_instr;
5280 int save_iidx = ectx->ec_iidx;
5281 int res;
5282
LemonBoyf3b48952022-05-05 13:53:03 +01005283 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5284 // even when the compilation fails.
5285 rettv->v_type = VAR_UNKNOWN;
5286
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005287 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5288 res = exec_instructions(ectx);
5289 if (res == OK)
5290 {
5291 *rettv = *STACK_TV_BOT(-1);
5292 --ectx->ec_stack.ga_len;
5293 }
5294
5295 ectx->ec_instr = save_instr;
5296 ectx->ec_iidx = save_iidx;
5297
5298 return res;
5299}
5300
5301/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005302 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5303 * "substitute_instr".
5304 */
5305 char_u *
5306exe_substitute_instr(void)
5307{
5308 ectx_T *ectx = substitute_instr->subs_ectx;
5309 isn_T *save_instr = ectx->ec_instr;
5310 int save_iidx = ectx->ec_iidx;
5311 char_u *res;
5312
5313 ectx->ec_instr = substitute_instr->subs_instr;
5314 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005315 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005316 typval_T *tv = STACK_TV_BOT(-1);
5317
Bram Moolenaar27523602021-06-05 21:36:19 +02005318 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005319 --ectx->ec_stack.ga_len;
5320 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005321 }
5322 else
5323 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005324 substitute_instr->subs_status = FAIL;
5325 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005327
Bram Moolenaar4c137212021-04-19 16:48:48 +02005328 ectx->ec_instr = save_instr;
5329 ectx->ec_iidx = save_iidx;
5330
5331 return res;
5332}
5333
5334/*
5335 * Call a "def" function from old Vim script.
5336 * Return OK or FAIL.
5337 */
5338 int
5339call_def_function(
5340 ufunc_T *ufunc,
5341 int argc_arg, // nr of arguments
5342 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005343 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005344 partial_T *partial, // optional partial for context
Bram Moolenaar58779852022-09-06 18:31:14 +01005345 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005346 typval_T *rettv) // return value
5347{
5348 ectx_T ectx; // execution context
5349 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005350 int partial_argc = partial == NULL
5351 || (flags & DEF_USE_PT_ARGV) == 0
5352 ? 0 : partial->pt_argc;
5353 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005354 typval_T *tv;
5355 int idx;
5356 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005357 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005358 sctx_T save_current_sctx = current_sctx;
5359 int did_emsg_before = did_emsg_cumul + did_emsg;
5360 int save_suppress_errthrow = suppress_errthrow;
5361 msglist_T **saved_msg_list = NULL;
5362 msglist_T *private_msg_list = NULL;
5363 int save_emsg_silent_def = emsg_silent_def;
5364 int save_did_emsg_def = did_emsg_def;
5365 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005366 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005367
5368// Get pointer to item in the stack.
5369#undef STACK_TV
5370#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5371
5372// Get pointer to item at the bottom of the stack, -1 is the bottom.
5373#undef STACK_TV_BOT
5374#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5375
5376// Get pointer to a local variable on the stack. Negative for arguments.
5377#undef STACK_TV_VAR
5378#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5379
5380 if (ufunc->uf_def_status == UF_NOT_COMPILED
5381 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005382 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5383 && compile_def_function(ufunc, FALSE,
5384 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005385 {
5386 if (did_emsg_cumul + did_emsg == did_emsg_before)
5387 semsg(_(e_function_is_not_compiled_str),
5388 printable_func_name(ufunc));
5389 return FAIL;
5390 }
5391
5392 {
5393 // Check the function was really compiled.
5394 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5395 + ufunc->uf_dfunc_idx;
5396 if (INSTRUCTIONS(dfunc) == NULL)
5397 {
5398 iemsg("using call_def_function() on not compiled function");
5399 return FAIL;
5400 }
5401 }
5402
5403 // If depth of calling is getting too high, don't execute the function.
5404 orig_funcdepth = funcdepth_get();
5405 if (funcdepth_increment() == FAIL)
5406 return FAIL;
5407
5408 CLEAR_FIELD(ectx);
5409 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5410 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005411 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005412 {
5413 funcdepth_decrement();
5414 return FAIL;
5415 }
5416 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5417 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5418 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005419 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005420
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005421 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005422 if (idx > 0 && ufunc->uf_va_name == NULL)
5423 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005424 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005425 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005426 goto failed_early;
5427 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005428 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005429 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005430 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005431 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005432 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005433 goto failed_early;
5434 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005435
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005436 // Put values from the partial and arguments on the stack, but no more than
5437 // what the function expects. A lambda can be called with more arguments
5438 // than it uses.
5439 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005440 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5441 ++idx)
5442 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005443 int argv_idx = idx - partial_argc;
5444
5445 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005446 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005447 && tv->v_type == VAR_SPECIAL
5448 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005449 {
5450 // Use the default value.
5451 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5452 }
5453 else
5454 {
5455 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5456 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005457 ufunc->uf_arg_types[idx], tv,
5458 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005459 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005460 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005461 }
5462 ++ectx.ec_stack.ga_len;
5463 }
5464
5465 // Turn varargs into a list. Empty list if no args.
5466 if (ufunc->uf_va_name != NULL)
5467 {
5468 int vararg_count = argc - ufunc->uf_args.ga_len;
5469
5470 if (vararg_count < 0)
5471 vararg_count = 0;
5472 else
5473 argc -= vararg_count;
5474 if (exe_newlist(vararg_count, &ectx) == FAIL)
5475 goto failed_early;
5476
5477 // Check the type of the list items.
5478 tv = STACK_TV_BOT(-1);
5479 if (ufunc->uf_va_type != NULL
5480 && ufunc->uf_va_type != &t_list_any
5481 && ufunc->uf_va_type->tt_member != &t_any
5482 && tv->vval.v_list != NULL)
5483 {
5484 type_T *expected = ufunc->uf_va_type->tt_member;
5485 listitem_T *li = tv->vval.v_list->lv_first;
5486
5487 for (idx = 0; idx < vararg_count; ++idx)
5488 {
5489 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005490 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005491 goto failed_early;
5492 li = li->li_next;
5493 }
5494 }
5495
5496 if (defcount > 0)
5497 // Move varargs list to below missing default arguments.
5498 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5499 --ectx.ec_stack.ga_len;
5500 }
5501
5502 // Make space for omitted arguments, will store default value below.
5503 // Any varargs list goes after them.
5504 if (defcount > 0)
5505 for (idx = 0; idx < defcount; ++idx)
5506 {
5507 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5508 ++ectx.ec_stack.ga_len;
5509 }
5510 if (ufunc->uf_va_name != NULL)
5511 ++ectx.ec_stack.ga_len;
5512
5513 // Frame pointer points to just after arguments.
5514 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5515 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5516
5517 {
5518 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5519 + ufunc->uf_dfunc_idx;
5520 ufunc_T *base_ufunc = dfunc->df_ufunc;
5521
5522 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5523 // by copy_func().
5524 if (partial != NULL || base_ufunc->uf_partial != NULL)
5525 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005526 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5527 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005528 goto failed_early;
5529 if (partial != NULL)
5530 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005531 outer_T *outer = get_pt_outer(partial);
5532
5533 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005534 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005535 if (current_ectx != NULL)
5536 {
5537 if (current_ectx->ec_outer_ref != NULL
5538 && current_ectx->ec_outer_ref->or_outer != NULL)
5539 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005540 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005541 }
5542 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005543 }
5544 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005545 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005546 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005547 ++partial->pt_refcount;
5548 ectx.ec_outer_ref->or_partial = partial;
5549 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005550 }
5551 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005552 {
5553 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5554 ++base_ufunc->uf_partial->pt_refcount;
5555 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5556 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005557 }
5558 }
5559
5560 // dummy frame entries
5561 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5562 {
5563 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5564 ++ectx.ec_stack.ga_len;
5565 }
5566
5567 {
5568 // Reserve space for local variables and any closure reference count.
5569 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5570 + ufunc->uf_dfunc_idx;
5571
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005572 // Initialize variables to zero. That avoids having to generate
5573 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005574 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005575 {
5576 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5577 STACK_TV_VAR(idx)->vval.v_number = 0;
5578 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005579 ectx.ec_stack.ga_len += dfunc->df_varcount;
5580 if (dfunc->df_has_closure)
5581 {
5582 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5583 STACK_TV_VAR(idx)->vval.v_number = 0;
5584 ++ectx.ec_stack.ga_len;
5585 }
5586
5587 ectx.ec_instr = INSTRUCTIONS(dfunc);
5588 }
5589
Bram Moolenaar58779852022-09-06 18:31:14 +01005590 // Store the execution context in funccal, used by invoke_all_defer().
5591 if (funccal != NULL)
5592 funccal->fc_ectx = &ectx;
5593
Bram Moolenaar4c137212021-04-19 16:48:48 +02005594 // Following errors are in the function, not the caller.
5595 // Commands behave like vim9script.
5596 estack_push_ufunc(ufunc, 1);
5597 current_sctx = ufunc->uf_script_ctx;
5598 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5599
5600 // Use a specific location for storing error messages to be converted to an
5601 // exception.
5602 saved_msg_list = msg_list;
5603 msg_list = &private_msg_list;
5604
5605 // Do turn errors into exceptions.
5606 suppress_errthrow = FALSE;
5607
5608 // Do not delete the function while executing it.
5609 ++ufunc->uf_calls;
5610
5611 // When ":silent!" was used before calling then we still abort the
5612 // function. If ":silent!" is used in the function then we don't.
5613 emsg_silent_def = emsg_silent;
5614 did_emsg_def = 0;
5615
5616 ectx.ec_where.wt_index = 0;
5617 ectx.ec_where.wt_variable = FALSE;
5618
5619 // Execute the instructions until done.
5620 ret = exec_instructions(&ectx);
5621 if (ret == OK)
5622 {
5623 // function finished, get result from the stack.
5624 if (ufunc->uf_ret_type == &t_void)
5625 {
5626 rettv->v_type = VAR_VOID;
5627 }
5628 else
5629 {
5630 tv = STACK_TV_BOT(-1);
5631 *rettv = *tv;
5632 tv->v_type = VAR_UNKNOWN;
5633 }
5634 }
5635
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005636 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005637 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005638
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005639 // Deal with any remaining closures, they may be in use somewhere.
5640 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005641 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005642 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005643 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005644 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005645
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005646 estack_pop();
5647 current_sctx = save_current_sctx;
5648
Bram Moolenaar2336c372021-12-06 15:06:54 +00005649 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5650 // Function was unreferenced while being used, free it now.
5651 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005652
Bram Moolenaar352134b2020-10-17 22:04:08 +02005653 if (*msg_list != NULL && saved_msg_list != NULL)
5654 {
5655 msglist_T **plist = saved_msg_list;
5656
5657 // Append entries from the current msg_list (uncaught exceptions) to
5658 // the saved msg_list.
5659 while (*plist != NULL)
5660 plist = &(*plist)->next;
5661
5662 *plist = *msg_list;
5663 }
5664 msg_list = saved_msg_list;
5665
Bram Moolenaar4c137212021-04-19 16:48:48 +02005666 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005667 {
5668 cmdmod.cmod_filter_regmatch.regprog = NULL;
5669 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005670 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005671 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005672 emsg_silent_def = save_emsg_silent_def;
5673 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005674
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005675failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005676 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005677 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5678 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005679 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005682 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005683 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005684 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005685 if (ectx.ec_outer_ref->or_outer_allocated)
5686 vim_free(ectx.ec_outer_ref->or_outer);
5687 partial_unref(ectx.ec_outer_ref->or_partial);
5688 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005689 }
5690
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005691 // Not sure if this is necessary.
5692 suppress_errthrow = save_suppress_errthrow;
5693
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005694 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5695 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005696 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005697 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005698 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005699 return ret;
5700}
5701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005702/*
Bram Moolenaar58779852022-09-06 18:31:14 +01005703 * Called when a def function has finished (possibly failed).
5704 * Invoke all the function returns to clean up and invoke deferred functions,
5705 * except the toplevel one.
5706 */
5707 void
5708unwind_def_callstack(ectx_T *ectx)
5709{
5710 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
5711 func_return(ectx);
5712}
5713
5714/*
5715 * Invoke any deffered functions for the top function in "ectx".
5716 */
5717 void
5718may_invoke_defer_funcs(ectx_T *ectx)
5719{
5720 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
5721
5722 if (dfunc->df_defer_var_idx > 0)
5723 invoke_defer_funcs(ectx);
5724}
5725
5726/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005727 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5728 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5729 * "pfx" is prefixed to every line.
5730 */
5731 static void
5732list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5733{
5734 int line_idx = 0;
5735 int prev_current = 0;
5736 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005737 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005738
5739 for (current = 0; current < instr_count; ++current)
5740 {
5741 isn_T *iptr = &instr[current];
5742 char *line;
5743
5744 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005745 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005746 while (line_idx < iptr->isn_lnum
5747 && line_idx < ufunc->uf_lines.ga_len)
5748 {
5749 if (current > prev_current)
5750 {
5751 msg_puts("\n\n");
5752 prev_current = current;
5753 }
5754 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5755 if (line != NULL)
5756 msg(line);
5757 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005758 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5759 {
5760 int first_def_arg = ufunc->uf_args.ga_len
5761 - ufunc->uf_def_args.ga_len;
5762
5763 if (def_arg_idx > 0)
5764 msg_puts("\n\n");
5765 msg_start();
5766 msg_puts(" ");
5767 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5768 first_def_arg + def_arg_idx]);
5769 msg_puts(" = ");
5770 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5771 msg_clr_eos();
5772 msg_end();
5773 }
5774 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005775
5776 switch (iptr->isn_type)
5777 {
5778 case ISN_EXEC:
5779 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5780 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005781 case ISN_EXEC_SPLIT:
5782 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5783 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005784 case ISN_EXECRANGE:
5785 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5786 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005787 case ISN_LEGACY_EVAL:
5788 smsg("%s%4d EVAL legacy %s", pfx, current,
5789 iptr->isn_arg.string);
5790 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005791 case ISN_REDIRSTART:
5792 smsg("%s%4d REDIR", pfx, current);
5793 break;
5794 case ISN_REDIREND:
5795 smsg("%s%4d REDIR END%s", pfx, current,
5796 iptr->isn_arg.number ? " append" : "");
5797 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005798 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005799#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005800 smsg("%s%4d CEXPR pre %s", pfx, current,
5801 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005802#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005803 break;
5804 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005805#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005806 {
5807 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5808
5809 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5810 cexpr_get_auname(cer->cer_cmdidx),
5811 cer->cer_forceit ? "!" : "",
5812 cer->cer_cmdline);
5813 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005814#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005815 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005816 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005817 smsg("%s%4d INSTR", pfx, current);
5818 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5819 msg(" -------------");
5820 break;
5821 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005822 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005823 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5824
5825 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005826 }
5827 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005828 case ISN_SUBSTITUTE:
5829 {
5830 subs_T *subs = &iptr->isn_arg.subs;
5831
5832 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5833 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5834 msg(" -------------");
5835 }
5836 break;
5837 case ISN_EXECCONCAT:
5838 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5839 (varnumber_T)iptr->isn_arg.number);
5840 break;
5841 case ISN_ECHO:
5842 {
5843 echo_T *echo = &iptr->isn_arg.echo;
5844
5845 smsg("%s%4d %s %d", pfx, current,
5846 echo->echo_with_white ? "ECHO" : "ECHON",
5847 echo->echo_count);
5848 }
5849 break;
5850 case ISN_EXECUTE:
5851 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005852 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005853 break;
5854 case ISN_ECHOMSG:
5855 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005856 (varnumber_T)(iptr->isn_arg.number));
5857 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01005858 case ISN_ECHOWINDOW:
5859 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
5860 (varnumber_T)(iptr->isn_arg.number));
5861 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02005862 case ISN_ECHOCONSOLE:
5863 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5864 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005865 break;
5866 case ISN_ECHOERR:
5867 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005868 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005869 break;
5870 case ISN_LOAD:
5871 {
5872 if (iptr->isn_arg.number < 0)
5873 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5874 (varnumber_T)(iptr->isn_arg.number
5875 + STACK_FRAME_SIZE));
5876 else
5877 smsg("%s%4d LOAD $%lld", pfx, current,
5878 (varnumber_T)(iptr->isn_arg.number));
5879 }
5880 break;
5881 case ISN_LOADOUTER:
5882 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005883 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005884 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5885 iptr->isn_arg.outer.outer_depth,
5886 iptr->isn_arg.outer.outer_idx
5887 + STACK_FRAME_SIZE);
5888 else
5889 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5890 iptr->isn_arg.outer.outer_depth,
5891 iptr->isn_arg.outer.outer_idx);
5892 }
5893 break;
5894 case ISN_LOADV:
5895 smsg("%s%4d LOADV v:%s", pfx, current,
5896 get_vim_var_name(iptr->isn_arg.number));
5897 break;
5898 case ISN_LOADSCRIPT:
5899 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005900 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5901 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5902 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005903
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005904 sv = get_script_svar(sref, -1);
5905 if (sv == NULL)
5906 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5907 pfx, current, si->sn_name);
5908 else
5909 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005910 sv->sv_name,
5911 sref->sref_idx,
5912 si->sn_name);
5913 }
5914 break;
5915 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005916 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005917 {
5918 scriptitem_T *si = SCRIPT_ITEM(
5919 iptr->isn_arg.loadstore.ls_sid);
5920
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005921 smsg("%s%4d %s s:%s from %s", pfx, current,
5922 iptr->isn_type == ISN_LOADS ? "LOADS"
5923 : "LOADEXPORT",
5924 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005925 }
5926 break;
5927 case ISN_LOADAUTO:
5928 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5929 break;
5930 case ISN_LOADG:
5931 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5932 break;
5933 case ISN_LOADB:
5934 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5935 break;
5936 case ISN_LOADW:
5937 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5938 break;
5939 case ISN_LOADT:
5940 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5941 break;
5942 case ISN_LOADGDICT:
5943 smsg("%s%4d LOAD g:", pfx, current);
5944 break;
5945 case ISN_LOADBDICT:
5946 smsg("%s%4d LOAD b:", pfx, current);
5947 break;
5948 case ISN_LOADWDICT:
5949 smsg("%s%4d LOAD w:", pfx, current);
5950 break;
5951 case ISN_LOADTDICT:
5952 smsg("%s%4d LOAD t:", pfx, current);
5953 break;
5954 case ISN_LOADOPT:
5955 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5956 break;
5957 case ISN_LOADENV:
5958 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5959 break;
5960 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005961 smsg("%s%4d LOADREG @%c", pfx, current,
5962 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005963 break;
5964
5965 case ISN_STORE:
5966 if (iptr->isn_arg.number < 0)
5967 smsg("%s%4d STORE arg[%lld]", pfx, current,
5968 iptr->isn_arg.number + STACK_FRAME_SIZE);
5969 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005970 smsg("%s%4d STORE $%lld", pfx, current,
5971 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005972 break;
5973 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005974 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5975 iptr->isn_arg.outer.outer_depth,
5976 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005977 break;
5978 case ISN_STOREV:
5979 smsg("%s%4d STOREV v:%s", pfx, current,
5980 get_vim_var_name(iptr->isn_arg.number));
5981 break;
5982 case ISN_STOREAUTO:
5983 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5984 break;
5985 case ISN_STOREG:
5986 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5987 break;
5988 case ISN_STOREB:
5989 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5990 break;
5991 case ISN_STOREW:
5992 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5993 break;
5994 case ISN_STORET:
5995 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5996 break;
5997 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005998 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005999 {
6000 scriptitem_T *si = SCRIPT_ITEM(
6001 iptr->isn_arg.loadstore.ls_sid);
6002
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006003 smsg("%s%4d %s %s in %s", pfx, current,
6004 iptr->isn_type == ISN_STORES
6005 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006006 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6007 }
6008 break;
6009 case ISN_STORESCRIPT:
6010 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006011 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6012 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6013 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006014
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006015 sv = get_script_svar(sref, -1);
6016 if (sv == NULL)
6017 smsg("%s%4d STORESCRIPT [deleted] in %s",
6018 pfx, current, si->sn_name);
6019 else
6020 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006021 sv->sv_name,
6022 sref->sref_idx,
6023 si->sn_name);
6024 }
6025 break;
6026 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006027 case ISN_STOREFUNCOPT:
6028 smsg("%s%4d %s &%s", pfx, current,
6029 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006030 iptr->isn_arg.storeopt.so_name);
6031 break;
6032 case ISN_STOREENV:
6033 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6034 break;
6035 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006036 smsg("%s%4d STOREREG @%c", pfx, current,
6037 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006038 break;
6039 case ISN_STORENR:
6040 smsg("%s%4d STORE %lld in $%d", pfx, current,
6041 iptr->isn_arg.storenr.stnr_val,
6042 iptr->isn_arg.storenr.stnr_idx);
6043 break;
6044
6045 case ISN_STOREINDEX:
6046 smsg("%s%4d STOREINDEX %s", pfx, current,
6047 vartype_name(iptr->isn_arg.vartype));
6048 break;
6049
6050 case ISN_STORERANGE:
6051 smsg("%s%4d STORERANGE", pfx, current);
6052 break;
6053
6054 // constants
6055 case ISN_PUSHNR:
6056 smsg("%s%4d PUSHNR %lld", pfx, current,
6057 (varnumber_T)(iptr->isn_arg.number));
6058 break;
6059 case ISN_PUSHBOOL:
6060 case ISN_PUSHSPEC:
6061 smsg("%s%4d PUSH %s", pfx, current,
6062 get_var_special_name(iptr->isn_arg.number));
6063 break;
6064 case ISN_PUSHF:
6065#ifdef FEAT_FLOAT
6066 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
6067#endif
6068 break;
6069 case ISN_PUSHS:
6070 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6071 break;
6072 case ISN_PUSHBLOB:
6073 {
6074 char_u *r;
6075 char_u numbuf[NUMBUFLEN];
6076 char_u *tofree;
6077
6078 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6079 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6080 vim_free(tofree);
6081 }
6082 break;
6083 case ISN_PUSHFUNC:
6084 {
6085 char *name = (char *)iptr->isn_arg.string;
6086
6087 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6088 name == NULL ? "[none]" : name);
6089 }
6090 break;
6091 case ISN_PUSHCHANNEL:
6092#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006093 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006094#endif
6095 break;
6096 case ISN_PUSHJOB:
6097#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006098 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006099#endif
6100 break;
6101 case ISN_PUSHEXC:
6102 smsg("%s%4d PUSH v:exception", pfx, current);
6103 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006104 case ISN_AUTOLOAD:
6105 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6106 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006107 case ISN_UNLET:
6108 smsg("%s%4d UNLET%s %s", pfx, current,
6109 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6110 iptr->isn_arg.unlet.ul_name);
6111 break;
6112 case ISN_UNLETENV:
6113 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6114 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6115 iptr->isn_arg.unlet.ul_name);
6116 break;
6117 case ISN_UNLETINDEX:
6118 smsg("%s%4d UNLETINDEX", pfx, current);
6119 break;
6120 case ISN_UNLETRANGE:
6121 smsg("%s%4d UNLETRANGE", pfx, current);
6122 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006123 case ISN_LOCKUNLOCK:
6124 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6125 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006126 case ISN_LOCKCONST:
6127 smsg("%s%4d LOCKCONST", pfx, current);
6128 break;
6129 case ISN_NEWLIST:
6130 smsg("%s%4d NEWLIST size %lld", pfx, current,
6131 (varnumber_T)(iptr->isn_arg.number));
6132 break;
6133 case ISN_NEWDICT:
6134 smsg("%s%4d NEWDICT size %lld", pfx, current,
6135 (varnumber_T)(iptr->isn_arg.number));
6136 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006137 case ISN_NEWPARTIAL:
6138 smsg("%s%4d NEWPARTIAL", pfx, current);
6139 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006140
6141 // function call
6142 case ISN_BCALL:
6143 {
6144 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6145
6146 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6147 internal_func_name(cbfunc->cbf_idx),
6148 cbfunc->cbf_argcount);
6149 }
6150 break;
6151 case ISN_DCALL:
6152 {
6153 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6154 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6155 + cdfunc->cdf_idx;
6156
6157 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006158 printable_func_name(df->df_ufunc),
6159 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006160 }
6161 break;
6162 case ISN_UCALL:
6163 {
6164 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6165
6166 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6167 cufunc->cuf_name, cufunc->cuf_argcount);
6168 }
6169 break;
6170 case ISN_PCALL:
6171 {
6172 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6173
6174 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6175 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6176 }
6177 break;
6178 case ISN_PCALL_END:
6179 smsg("%s%4d PCALL end", pfx, current);
6180 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006181 case ISN_DEFER:
6182 smsg("%s%4d DEFER %d args", pfx, current,
6183 (int)iptr->isn_arg.defer.defer_argcount);
6184 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006185 case ISN_RETURN:
6186 smsg("%s%4d RETURN", pfx, current);
6187 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006188 case ISN_RETURN_VOID:
6189 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006190 break;
6191 case ISN_FUNCREF:
6192 {
6193 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00006194 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006195
Bram Moolenaar38453522021-11-28 22:00:12 +00006196 if (funcref->fr_func_name == NULL)
6197 {
6198 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6199 + funcref->fr_dfunc_idx;
6200 name = df->df_ufunc->uf_name;
6201 }
6202 else
6203 name = funcref->fr_func_name;
6204 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006205 }
6206 break;
6207
6208 case ISN_NEWFUNC:
6209 {
6210 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
6211
6212 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6213 newfunc->nf_lambda, newfunc->nf_global);
6214 }
6215 break;
6216
6217 case ISN_DEF:
6218 {
6219 char_u *name = iptr->isn_arg.string;
6220
6221 smsg("%s%4d DEF %s", pfx, current,
6222 name == NULL ? (char_u *)"" : name);
6223 }
6224 break;
6225
6226 case ISN_JUMP:
6227 {
6228 char *when = "?";
6229
6230 switch (iptr->isn_arg.jump.jump_when)
6231 {
6232 case JUMP_ALWAYS:
6233 when = "JUMP";
6234 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006235 case JUMP_NEVER:
6236 iemsg("JUMP_NEVER should not be used");
6237 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006238 case JUMP_AND_KEEP_IF_TRUE:
6239 when = "JUMP_AND_KEEP_IF_TRUE";
6240 break;
6241 case JUMP_IF_FALSE:
6242 when = "JUMP_IF_FALSE";
6243 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006244 case JUMP_WHILE_FALSE:
6245 when = "JUMP_WHILE_FALSE"; // unused
6246 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006247 case JUMP_IF_COND_FALSE:
6248 when = "JUMP_IF_COND_FALSE";
6249 break;
6250 case JUMP_IF_COND_TRUE:
6251 when = "JUMP_IF_COND_TRUE";
6252 break;
6253 }
6254 smsg("%s%4d %s -> %d", pfx, current, when,
6255 iptr->isn_arg.jump.jump_where);
6256 }
6257 break;
6258
6259 case ISN_JUMP_IF_ARG_SET:
6260 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6261 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6262 iptr->isn_arg.jump.jump_where);
6263 break;
6264
6265 case ISN_FOR:
6266 {
6267 forloop_T *forloop = &iptr->isn_arg.forloop;
6268
6269 smsg("%s%4d FOR $%d -> %d", pfx, current,
6270 forloop->for_idx, forloop->for_end);
6271 }
6272 break;
6273
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006274 case ISN_ENDLOOP:
6275 {
6276 endloop_T *endloop = &iptr->isn_arg.endloop;
6277
6278 smsg("%s%4d ENDLOOP $%d save $%d - $%d", pfx, current,
6279 endloop->end_funcref_idx,
6280 endloop->end_var_idx,
6281 endloop->end_var_idx + endloop->end_var_count - 1);
6282 }
6283 break;
6284
6285 case ISN_WHILE:
6286 {
6287 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6288
6289 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6290 whileloop->while_funcref_idx,
6291 whileloop->while_end);
6292 }
6293 break;
6294
Bram Moolenaar4c137212021-04-19 16:48:48 +02006295 case ISN_TRY:
6296 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006297 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006298
6299 if (try->try_ref->try_finally == 0)
6300 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6301 pfx, current,
6302 try->try_ref->try_catch,
6303 try->try_ref->try_endtry);
6304 else
6305 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6306 pfx, current,
6307 try->try_ref->try_catch,
6308 try->try_ref->try_finally,
6309 try->try_ref->try_endtry);
6310 }
6311 break;
6312 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006313 smsg("%s%4d CATCH", pfx, current);
6314 break;
6315 case ISN_TRYCONT:
6316 {
6317 trycont_T *trycont = &iptr->isn_arg.trycont;
6318
6319 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6320 trycont->tct_levels,
6321 trycont->tct_levels == 1 ? "" : "s",
6322 trycont->tct_where);
6323 }
6324 break;
6325 case ISN_FINALLY:
6326 smsg("%s%4d FINALLY", pfx, current);
6327 break;
6328 case ISN_ENDTRY:
6329 smsg("%s%4d ENDTRY", pfx, current);
6330 break;
6331 case ISN_THROW:
6332 smsg("%s%4d THROW", pfx, current);
6333 break;
6334
6335 // expression operations on number
6336 case ISN_OPNR:
6337 case ISN_OPFLOAT:
6338 case ISN_OPANY:
6339 {
6340 char *what;
6341 char *ins;
6342
6343 switch (iptr->isn_arg.op.op_type)
6344 {
6345 case EXPR_MULT: what = "*"; break;
6346 case EXPR_DIV: what = "/"; break;
6347 case EXPR_REM: what = "%"; break;
6348 case EXPR_SUB: what = "-"; break;
6349 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006350 case EXPR_LSHIFT: what = "<<"; break;
6351 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006352 default: what = "???"; break;
6353 }
6354 switch (iptr->isn_type)
6355 {
6356 case ISN_OPNR: ins = "OPNR"; break;
6357 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6358 case ISN_OPANY: ins = "OPANY"; break;
6359 default: ins = "???"; break;
6360 }
6361 smsg("%s%4d %s %s", pfx, current, ins, what);
6362 }
6363 break;
6364
6365 case ISN_COMPAREBOOL:
6366 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006367 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006368 case ISN_COMPARENR:
6369 case ISN_COMPAREFLOAT:
6370 case ISN_COMPARESTRING:
6371 case ISN_COMPAREBLOB:
6372 case ISN_COMPARELIST:
6373 case ISN_COMPAREDICT:
6374 case ISN_COMPAREFUNC:
6375 case ISN_COMPAREANY:
6376 {
6377 char *p;
6378 char buf[10];
6379 char *type;
6380
6381 switch (iptr->isn_arg.op.op_type)
6382 {
6383 case EXPR_EQUAL: p = "=="; break;
6384 case EXPR_NEQUAL: p = "!="; break;
6385 case EXPR_GREATER: p = ">"; break;
6386 case EXPR_GEQUAL: p = ">="; break;
6387 case EXPR_SMALLER: p = "<"; break;
6388 case EXPR_SEQUAL: p = "<="; break;
6389 case EXPR_MATCH: p = "=~"; break;
6390 case EXPR_IS: p = "is"; break;
6391 case EXPR_ISNOT: p = "isnot"; break;
6392 case EXPR_NOMATCH: p = "!~"; break;
6393 default: p = "???"; break;
6394 }
6395 STRCPY(buf, p);
6396 if (iptr->isn_arg.op.op_ic == TRUE)
6397 strcat(buf, "?");
6398 switch(iptr->isn_type)
6399 {
6400 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6401 case ISN_COMPARESPECIAL:
6402 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006403 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006404 case ISN_COMPARENR: type = "COMPARENR"; break;
6405 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6406 case ISN_COMPARESTRING:
6407 type = "COMPARESTRING"; break;
6408 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6409 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6410 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6411 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6412 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6413 default: type = "???"; break;
6414 }
6415
6416 smsg("%s%4d %s %s", pfx, current, type, buf);
6417 }
6418 break;
6419
6420 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6421 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6422
6423 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006424 case ISN_CONCAT:
6425 smsg("%s%4d CONCAT size %lld", pfx, current,
6426 (varnumber_T)(iptr->isn_arg.number));
6427 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006428 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6429 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6430 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6431 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6432 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6433 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6434 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6435 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6436 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6437 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6438 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006439 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006440 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6441 iptr->isn_arg.getitem.gi_index,
6442 iptr->isn_arg.getitem.gi_with_op ?
6443 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006444 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6445 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6446 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006447 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6448 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6449
Bram Moolenaar4c137212021-04-19 16:48:48 +02006450 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6451
Bram Moolenaar4c137212021-04-19 16:48:48 +02006452 case ISN_CHECKTYPE:
6453 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006454 checktype_T *ct = &iptr->isn_arg.type;
6455 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006456
6457 if (ct->ct_arg_idx == 0)
6458 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6459 type_name(ct->ct_type, &tofree),
6460 (int)ct->ct_off);
6461 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006462 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006463 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006464 type_name(ct->ct_type, &tofree),
6465 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006466 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006467 (int)ct->ct_arg_idx);
6468 vim_free(tofree);
6469 break;
6470 }
6471 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6472 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6473 iptr->isn_arg.checklen.cl_min_len);
6474 break;
6475 case ISN_SETTYPE:
6476 {
6477 char *tofree;
6478
6479 smsg("%s%4d SETTYPE %s", pfx, current,
6480 type_name(iptr->isn_arg.type.ct_type, &tofree));
6481 vim_free(tofree);
6482 break;
6483 }
6484 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006485 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6486 smsg("%s%4d INVERT %d (!val)", pfx, current,
6487 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006488 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006489 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6490 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006491 break;
6492 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006493 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006494 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006495 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6496 pfx, current,
6497 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006498 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006499 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6500 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006501 break;
6502 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006503 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006504 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006505 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006506 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6507 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006508 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006509 else
6510 smsg("%s%4d PUT %c %ld", pfx, current,
6511 iptr->isn_arg.put.put_regname,
6512 (long)iptr->isn_arg.put.put_lnum);
6513 break;
6514
Bram Moolenaar4c137212021-04-19 16:48:48 +02006515 case ISN_CMDMOD:
6516 {
6517 char_u *buf;
6518 size_t len = produce_cmdmods(
6519 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6520
6521 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006522 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006523 {
6524 (void)produce_cmdmods(
6525 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6526 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6527 vim_free(buf);
6528 }
6529 break;
6530 }
6531 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6532
6533 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006534 smsg("%s%4d PROFILE START line %d", pfx, current,
6535 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006536 break;
6537
6538 case ISN_PROF_END:
6539 smsg("%s%4d PROFILE END", pfx, current);
6540 break;
6541
Bram Moolenaare99d4222021-06-13 14:01:26 +02006542 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006543 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6544 iptr->isn_arg.debug.dbg_break_lnum + 1,
6545 iptr->isn_lnum,
6546 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006547 break;
6548
Bram Moolenaar4c137212021-04-19 16:48:48 +02006549 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6550 iptr->isn_arg.unpack.unp_count,
6551 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6552 break;
6553 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6554 iptr->isn_arg.shuffle.shfl_item,
6555 iptr->isn_arg.shuffle.shfl_up);
6556 break;
6557 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6558
6559 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6560 return;
6561 }
6562
6563 out_flush(); // output one line at a time
6564 ui_breakcheck();
6565 if (got_int)
6566 break;
6567 }
6568}
6569
6570/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006571 * Handle command line completion for the :disassemble command.
6572 */
6573 void
6574set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6575{
6576 char_u *p;
6577
6578 // Default: expand user functions, "debug" and "profile"
6579 xp->xp_context = EXPAND_DISASSEMBLE;
6580 xp->xp_pattern = arg;
6581
6582 // first argument already typed: only user function names
6583 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6584 {
6585 xp->xp_context = EXPAND_USER_FUNC;
6586 xp->xp_pattern = skipwhite(p);
6587 }
6588}
6589
6590/*
6591 * Function given to ExpandGeneric() to obtain the list of :disassemble
6592 * arguments.
6593 */
6594 char_u *
6595get_disassemble_argument(expand_T *xp, int idx)
6596{
6597 if (idx == 0)
6598 return (char_u *)"debug";
6599 if (idx == 1)
6600 return (char_u *)"profile";
6601 return get_user_func_name(xp, idx - 2);
6602}
6603
6604/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006605 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006606 * We don't really need this at runtime, but we do have tests that require it,
6607 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 */
6609 void
6610ex_disassemble(exarg_T *eap)
6611{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006612 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006613 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006614 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006615 isn_T *instr = NULL; // init to shut up gcc warning
6616 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006617 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006618
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006619 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006620 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006622 if (func_needs_compiling(ufunc, compile_type)
6623 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006624 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006625 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006627 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628 return;
6629 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006630 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631
6632 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006633 switch (compile_type)
6634 {
6635 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006636#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006637 instr = dfunc->df_instr_prof;
6638 instr_count = dfunc->df_instr_prof_count;
6639 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006640#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006641 // FALLTHROUGH
6642 case CT_NONE:
6643 instr = dfunc->df_instr;
6644 instr_count = dfunc->df_instr_count;
6645 break;
6646 case CT_DEBUG:
6647 instr = dfunc->df_instr_debug;
6648 instr_count = dfunc->df_instr_debug_count;
6649 break;
6650 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651
Bram Moolenaar4c137212021-04-19 16:48:48 +02006652 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006653}
6654
6655/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006656 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006657 * list, etc. Mostly like what JavaScript does, except that empty list and
6658 * empty dictionary are FALSE.
6659 */
6660 int
6661tv2bool(typval_T *tv)
6662{
6663 switch (tv->v_type)
6664 {
6665 case VAR_NUMBER:
6666 return tv->vval.v_number != 0;
6667 case VAR_FLOAT:
6668#ifdef FEAT_FLOAT
6669 return tv->vval.v_float != 0.0;
6670#else
6671 break;
6672#endif
6673 case VAR_PARTIAL:
6674 return tv->vval.v_partial != NULL;
6675 case VAR_FUNC:
6676 case VAR_STRING:
6677 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6678 case VAR_LIST:
6679 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6680 case VAR_DICT:
6681 return tv->vval.v_dict != NULL
6682 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6683 case VAR_BOOL:
6684 case VAR_SPECIAL:
6685 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6686 case VAR_JOB:
6687#ifdef FEAT_JOB_CHANNEL
6688 return tv->vval.v_job != NULL;
6689#else
6690 break;
6691#endif
6692 case VAR_CHANNEL:
6693#ifdef FEAT_JOB_CHANNEL
6694 return tv->vval.v_channel != NULL;
6695#else
6696 break;
6697#endif
6698 case VAR_BLOB:
6699 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6700 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006701 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006703 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 break;
6705 }
6706 return FALSE;
6707}
6708
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006709 void
6710emsg_using_string_as(typval_T *tv, int as_number)
6711{
6712 semsg(_(as_number ? e_using_string_as_number_str
6713 : e_using_string_as_bool_str),
6714 tv->vval.v_string == NULL
6715 ? (char_u *)"" : tv->vval.v_string);
6716}
6717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718/*
6719 * If "tv" is a string give an error and return FAIL.
6720 */
6721 int
6722check_not_string(typval_T *tv)
6723{
6724 if (tv->v_type == VAR_STRING)
6725 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006726 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006727 clear_tv(tv);
6728 return FAIL;
6729 }
6730 return OK;
6731}
6732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006733
6734#endif // FEAT_EVAL