blob: 22dfaaa52deab00e81f48a1a87d60732ba038fb2 [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 Moolenaar8fa745e2022-09-16 19:04:24 +0100676 // Compute "top": the first entry in the stack used by the function.
677 // This is the first argument (after that comes the stack frame and then
678 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200679 argcount = ufunc_argcount(dfunc->df_ufunc);
680 top = ectx->ec_frame_idx - argcount;
681
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200682 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200683 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200684 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200685 partial_T *pt;
686 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200687
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200688 if (off < 0)
689 continue; // count is off or already done
690 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200691 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200692 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200693 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200694 int i;
695
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000696 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200697 // unreferenced on return.
698 for (i = 0; i < dfunc->df_varcount; ++i)
699 {
700 typval_T *stv = STACK_TV(ectx->ec_frame_idx
701 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200702 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200703 --refcount;
704 }
705 if (refcount > 1)
706 {
707 closure_in_use = TRUE;
708 break;
709 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200710 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200711 }
712
713 if (closure_in_use)
714 {
715 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
716 typval_T *stack;
717
718 // A closure is using the arguments and/or local variables.
719 // Move them to the called function.
720 if (funcstack == NULL)
721 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000722
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200723 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
724 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200725 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
726 funcstack->fs_ga.ga_data = stack;
727 if (stack == NULL)
728 {
729 vim_free(funcstack);
730 return FAIL;
731 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000732 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200733
734 // Move or copy the arguments.
735 for (idx = 0; idx < argcount; ++idx)
736 {
737 tv = STACK_TV(top + idx);
738 if (free_arguments)
739 {
740 *(stack + idx) = *tv;
741 tv->v_type = VAR_UNKNOWN;
742 }
743 else
744 copy_tv(tv, stack + idx);
745 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100746 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200747 // Move the local variables.
748 for (idx = 0; idx < dfunc->df_varcount; ++idx)
749 {
750 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200751
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200752 // A partial created for a local function, that is also used as a
753 // local variable, has a reference count for the variable, thus
754 // will never go down to zero. When all these refcounts are one
755 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000756 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200757 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
758 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200759 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200760
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200761 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200762 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
763 gap->ga_len - closure_count + i])
764 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200765 }
766
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200767 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200768 tv->v_type = VAR_UNKNOWN;
769 }
770
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200771 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200772 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200773 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
774 - closure_count + idx];
775 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200776 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200777 ++funcstack->fs_refcount;
778 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100779 pt->pt_outer.out_stack = &funcstack->fs_ga;
780 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200781 }
782 }
783 }
784
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200785 for (idx = 0; idx < closure_count; ++idx)
786 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
787 - closure_count + idx]);
788 gap->ga_len -= closure_count;
789 if (gap->ga_len == 0)
790 ga_clear(gap);
791
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200792 return OK;
793}
794
795/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200796 * Called when a partial is freed or its reference count goes down to one. The
797 * funcstack may be the only reference to the partials in the local variables.
798 * Go over all of them, the funcref and can be freed if all partials
799 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100800 * Returns TRUE if the funcstack is freed, the partial referencing it will then
801 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200802 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100803 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200804funcstack_check_refcount(funcstack_T *funcstack)
805{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100806 int i;
807 garray_T *gap = &funcstack->fs_ga;
808 int done = 0;
809 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200810
811 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100812 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200813 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
814 {
815 typval_T *tv = ((typval_T *)gap->ga_data) + i;
816
817 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
818 && tv->vval.v_partial->pt_funcstack == funcstack
819 && tv->vval.v_partial->pt_refcount == 1)
820 ++done;
821 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100822 if (done != funcstack->fs_min_refcount)
823 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200824
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100825 stack = gap->ga_data;
826
827 // All partials referencing the funcstack have a reference count of
828 // one, thus the funcstack is no longer of use.
829 for (i = 0; i < gap->ga_len; ++i)
830 clear_tv(stack + i);
831 vim_free(stack);
832 remove_funcstack_from_list(funcstack);
833 vim_free(funcstack);
834
835 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200836}
837
838/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000839 * For garbage collecting: set references in all variables referenced by
840 * all funcstacks.
841 */
842 int
843set_ref_in_funcstacks(int copyID)
844{
845 funcstack_T *funcstack;
846
847 for (funcstack = first_funcstack; funcstack != NULL;
848 funcstack = funcstack->fs_next)
849 {
850 typval_T *stack = funcstack->fs_ga.ga_data;
851 int i;
852
853 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
854 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
855 return TRUE; // abort
856 }
857 return FALSE;
858}
859
Bram Moolenaar806a2732022-09-04 15:40:36 +0100860// Ugly static to avoid passing the execution context around through many
861// layers.
862static ectx_T *current_ectx = NULL;
863
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000864/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100865 * Return TRUE if currently executing a :def function.
866 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100867 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100868 int
869in_def_function(void)
870{
871 return current_ectx != NULL;
872}
873
874/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100875 * Clear "current_ectx" and return the previous value. To be used when calling
876 * a user function.
877 */
878 ectx_T *
879clear_currrent_ectx(void)
880{
881 ectx_T *r = current_ectx;
882
883 current_ectx = NULL;
884 return r;
885}
886
887 void
888restore_current_ectx(ectx_T *ectx)
889{
890 if (current_ectx != NULL)
891 iemsg("Restoring current_ectx while it is not NULL");
892 current_ectx = ectx;
893}
894
895/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100896 * Add an entry for a deferred function call to the currently executing
897 * function.
898 * Return the list or NULL when failed.
899 */
900 static list_T *
901add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100902{
903 typval_T *defer_tv = STACK_TV_VAR(var_idx);
904 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100905 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100906 typval_T listval;
907
908 if (defer_tv->v_type != VAR_LIST)
909 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100910 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100911 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100912 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100913 }
914 defer_l = defer_tv->vval.v_list;
915
916 l = list_alloc_with_items(argcount + 1);
917 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100918 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100919 listval.v_type = VAR_LIST;
920 listval.vval.v_list = l;
921 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100922 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100923 {
924 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100925 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100926 }
927
Bram Moolenaar806a2732022-09-04 15:40:36 +0100928 return l;
929}
930
931/*
932 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
933 * The local variable that lists deferred functions is "var_idx".
934 * Returns OK or FAIL.
935 */
936 static int
937defer_command(int var_idx, int argcount, ectx_T *ectx)
938{
939 list_T *l = add_defer_item(var_idx, argcount, ectx);
940 int i;
941 typval_T *func_tv;
942
943 if (l == NULL)
944 return FAIL;
945
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100946 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100947 if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL)
948 {
949 semsg(_(e_expected_str_but_got_str),
950 "function or partial",
951 vartype_name(func_tv->v_type));
952 return FAIL;
953 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100954 list_set_item(l, 0, func_tv);
955
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100956 for (i = 0; i < argcount; ++i)
957 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100958 ectx->ec_stack.ga_len -= argcount + 1;
959 return OK;
960}
961
962/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100963 * Add a deferred function "name" with one argument "arg_tv".
964 * Consumes "name", also on failure.
965 * Only to be called when in_def_function() returns TRUE.
966 */
967 int
968add_defer_function(char_u *name, int argcount, typval_T *argvars)
969{
970 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
971 + current_ectx->ec_dfunc_idx;
972 list_T *l;
973 typval_T func_tv;
974 int i;
975
976 if (dfunc->df_defer_var_idx == 0)
977 {
978 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100979 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100980 return FAIL;
981 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100982
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100983 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100984 if (l == NULL)
985 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100986 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100987 return FAIL;
988 }
989
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100990 func_tv.v_type = VAR_FUNC;
991 func_tv.v_lock = 0;
992 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100993 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100994
Bram Moolenaar806a2732022-09-04 15:40:36 +0100995 for (i = 0; i < argcount; ++i)
996 list_set_item(l, i + 1, argvars + i);
997 return OK;
998}
999
1000/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001001 * Invoked when returning from a function: Invoke any deferred calls.
1002 */
1003 static void
1004invoke_defer_funcs(ectx_T *ectx)
1005{
1006 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1007 + ectx->ec_dfunc_idx;
1008 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1009 listitem_T *li;
1010
1011 if (defer_tv->v_type != VAR_LIST)
1012 return; // no function added
1013 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1014 {
1015 list_T *l = li->li_tv.vval.v_list;
1016 typval_T rettv;
1017 typval_T argvars[MAX_FUNC_ARGS];
1018 int i;
1019 listitem_T *arg_li = l->lv_first;
1020 funcexe_T funcexe;
1021
1022 for (i = 0; i < l->lv_len - 1; ++i)
1023 {
1024 arg_li = arg_li->li_next;
1025 argvars[i] = arg_li->li_tv;
1026 }
1027
1028 CLEAR_FIELD(funcexe);
1029 funcexe.fe_evaluate = TRUE;
1030 rettv.v_type = VAR_UNKNOWN;
1031 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1032 &rettv, l->lv_len - 1, argvars, &funcexe);
1033 clear_tv(&rettv);
1034 }
1035}
1036
1037/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038 * Return from the current function.
1039 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001040 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001041func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001044 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001045 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1046 + ectx->ec_dfunc_idx;
1047 int argcount = ufunc_argcount(dfunc->df_ufunc);
1048 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001049 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001050 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1051 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001052 funclocal_T *floc;
1053#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001054 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1055 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056
Bram Moolenaar12d26532021-02-19 19:13:21 +01001057 if (do_profiling == PROF_YES)
1058 {
1059 ufunc_T *caller = prev_dfunc->df_ufunc;
1060
1061 if (dfunc->df_ufunc->uf_profiling
1062 || (caller != NULL && caller->uf_profiling))
1063 {
1064 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1065 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1066 --profile_info_ga.ga_len;
1067 }
1068 }
1069#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001070
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001071 if (dfunc->df_defer_var_idx > 0)
1072 invoke_defer_funcs(ectx);
1073
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001074 // No check for uf_refcount being zero, cannot think of a way that would
1075 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001076 --dfunc->df_ufunc->uf_calls;
1077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001079 entry = estack_pop();
1080 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001081 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001083 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1084 return FAIL;
1085
1086 // Clear the arguments.
1087 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1088 clear_tv(STACK_TV(idx));
1089
1090 // Clear local variables and temp values, but not the return value.
1091 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001092 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001094
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001095 // The return value should be on top of the stack. However, when aborting
1096 // it may not be there and ec_frame_idx is the top of the stack.
1097 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001098 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001099 ret_idx = 0;
1100
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001101 if (ectx->ec_outer_ref != NULL)
1102 {
1103 if (ectx->ec_outer_ref->or_outer_allocated)
1104 vim_free(ectx->ec_outer_ref->or_outer);
1105 partial_unref(ectx->ec_outer_ref->or_partial);
1106 vim_free(ectx->ec_outer_ref);
1107 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001108
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001109 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001110 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001111 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1112 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001113 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1114 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001115 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001116 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001117 floc = (void *)STACK_TV(ectx->ec_frame_idx
1118 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001119 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001120 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1121 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001122
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001123 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001124 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001125 else
1126 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001127 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001128 vim_free(floc);
1129 }
1130
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001131 if (ret_idx > 0)
1132 {
1133 // Reset the stack to the position before the call, with a spot for the
1134 // return value, moved there from above the frame.
1135 ectx->ec_stack.ga_len = top + 1;
1136 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1137 }
1138 else
1139 // Reset the stack to the position before the call.
1140 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001141
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001142 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001143 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001144 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145}
1146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147/*
1148 * Prepare arguments and rettv for calling a builtin or user function.
1149 */
1150 static int
1151call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1152{
1153 int idx;
1154 typval_T *tv;
1155
1156 // Move arguments from bottom of the stack to argvars[] and add terminator.
1157 for (idx = 0; idx < argcount; ++idx)
1158 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1159 argvars[argcount].v_type = VAR_UNKNOWN;
1160
1161 // Result replaces the arguments on the stack.
1162 if (argcount > 0)
1163 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001164 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 return FAIL;
1166 else
1167 ++ectx->ec_stack.ga_len;
1168
1169 // Default return value is zero.
1170 tv = STACK_TV_BOT(-1);
1171 tv->v_type = VAR_NUMBER;
1172 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001173 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174
1175 return OK;
1176}
1177
1178/*
1179 * Call a builtin function by index.
1180 */
1181 static int
1182call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1183{
1184 typval_T argvars[MAX_FUNC_ARGS];
1185 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001186 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001187 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001188 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189
1190 if (call_prepare(argcount, argvars, ectx) == FAIL)
1191 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001192 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001194 // Call the builtin function. Set "current_ectx" so that when it
1195 // recursively invokes call_def_function() a closure context can be set.
1196 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001198 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001199 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200
1201 // Clear the arguments.
1202 for (idx = 0; idx < argcount; ++idx)
1203 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001204
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001205 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001206 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 return OK;
1208}
1209
1210/*
1211 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001212 * If the function is compiled this will add a stack frame and set the
1213 * instruction pointer at the start of the function.
1214 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001215 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001216 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 */
1218 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001219call_ufunc(
1220 ufunc_T *ufunc,
1221 partial_T *pt,
1222 int argcount,
1223 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001224 isn_T *iptr,
1225 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226{
1227 typval_T argvars[MAX_FUNC_ARGS];
1228 funcexe_T funcexe;
1229 int error;
1230 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001231 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001232 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233
Bram Moolenaare99d4222021-06-13 14:01:26 +02001234 if (func_needs_compiling(ufunc, compile_type)
1235 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1236 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001237 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001238 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001239 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001240 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001241 if (error != FCERR_UNKNOWN)
1242 {
1243 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001244 semsg(_(e_too_many_arguments_for_function_str),
1245 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001246 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001247 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001248 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001249 return FAIL;
1250 }
1251
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001252 // The function has been compiled, can call it quickly. For a function
1253 // that was defined later: we can call it directly next time.
1254 if (iptr != NULL)
1255 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001256 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001257 iptr->isn_type = ISN_DCALL;
1258 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1259 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1260 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001261 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001262 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263
1264 if (call_prepare(argcount, argvars, ectx) == FAIL)
1265 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001266 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001267 funcexe.fe_evaluate = TRUE;
1268 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269
1270 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001272 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273
1274 // Clear the arguments.
1275 for (idx = 0; idx < argcount; ++idx)
1276 clear_tv(&argvars[idx]);
1277
1278 if (error != FCERR_NONE)
1279 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001280 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 return FAIL;
1282 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001283 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001284 // Error other than from calling the function itself.
1285 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286 return OK;
1287}
1288
1289/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001290 * If command modifiers were applied restore them.
1291 */
1292 static void
1293may_restore_cmdmod(funclocal_T *funclocal)
1294{
1295 if (funclocal->floc_restore_cmdmod)
1296 {
1297 cmdmod.cmod_filter_regmatch.regprog = NULL;
1298 undo_cmdmod(&cmdmod);
1299 cmdmod = funclocal->floc_save_cmdmod;
1300 funclocal->floc_restore_cmdmod = FALSE;
1301 }
1302}
1303
1304/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001305 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1306 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001307 */
1308 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001309vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001310{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001311 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001312}
1313
1314/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 * Execute a function by "name".
1316 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001317 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 * Returns FAIL if not found without an error message.
1319 */
1320 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001321call_by_name(
1322 char_u *name,
1323 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001324 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001325 isn_T *iptr,
1326 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327{
1328 ufunc_T *ufunc;
1329
1330 if (builtin_function(name, -1))
1331 {
1332 int func_idx = find_internal_func(name);
1333
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001334 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001336 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 return FAIL;
1338 return call_bfunc(func_idx, argcount, ectx);
1339 }
1340
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001341 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001342
1343 if (ufunc == NULL)
1344 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001345 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001346
1347 if (script_autoload(name, TRUE))
1348 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001349 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001350
1351 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001352 return FAIL; // bail out if loading the script caused an error
1353 }
1354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001356 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001357 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001358 {
1359 int i;
1360 typval_T *argv = STACK_TV_BOT(0) - argcount;
1361
1362 // The function can change at runtime, check that the argument
1363 // types are correct.
1364 for (i = 0; i < argcount; ++i)
1365 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001366 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001367
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001368 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001369 type = ufunc->uf_arg_types[i];
1370 else if (ufunc->uf_va_type != NULL)
1371 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001372 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001373 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001374 return FAIL;
1375 }
1376 }
1377
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001378 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001379 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380
1381 return FAIL;
1382}
1383
1384 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001385call_partial(
1386 typval_T *tv,
1387 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001388 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001390 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001391 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001393 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001394 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395
1396 if (tv->v_type == VAR_PARTIAL)
1397 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001398 partial_T *pt = tv->vval.v_partial;
1399 int i;
1400
1401 if (pt->pt_argc > 0)
1402 {
1403 // Make space for arguments from the partial, shift the "argcount"
1404 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001405 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001406 return FAIL;
1407 for (i = 1; i <= argcount; ++i)
1408 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1409 ectx->ec_stack.ga_len += pt->pt_argc;
1410 argcount += pt->pt_argc;
1411
1412 // copy the arguments from the partial onto the stack
1413 for (i = 0; i < pt->pt_argc; ++i)
1414 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1415 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001416 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417
1418 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001419 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 name = pt->pt_name;
1422 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001423 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001425 if (name != NULL)
1426 {
1427 char_u fname_buf[FLEN_FIXED + 1];
1428 char_u *tofree = NULL;
1429 int error = FCERR_NONE;
1430 char_u *fname;
1431
1432 // May need to translate <SNR>123_ to K_SNR.
1433 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1434 if (error != FCERR_NONE)
1435 res = FAIL;
1436 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001437 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001438 vim_free(tofree);
1439 }
1440
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001441 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 {
1443 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001444 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001445 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 return FAIL;
1447 }
1448 return OK;
1449}
1450
1451/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001452 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1453 * TRUE.
1454 */
1455 static int
1456error_if_locked(int lock, char *error)
1457{
1458 if (lock & (VAR_LOCKED | VAR_FIXED))
1459 {
1460 emsg(_(error));
1461 return TRUE;
1462 }
1463 return FALSE;
1464}
1465
1466/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001467 * Give an error if "tv" is not a number and return FAIL.
1468 */
1469 static int
1470check_for_number(typval_T *tv)
1471{
1472 if (tv->v_type != VAR_NUMBER)
1473 {
1474 semsg(_(e_expected_str_but_got_str),
1475 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1476 return FAIL;
1477 }
1478 return OK;
1479}
1480
1481/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001482 * Store "tv" in variable "name".
1483 * This is for s: and g: variables.
1484 */
1485 static void
1486store_var(char_u *name, typval_T *tv)
1487{
1488 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001489 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001490
Bram Moolenaard877a572021-04-01 19:42:48 +02001491 if (tv->v_lock)
1492 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001493 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001494 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001495 restore_funccal();
1496}
1497
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001498/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001499 * Convert "tv" to a string.
1500 * Return FAIL if not allowed.
1501 */
1502 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001503do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001504{
1505 if (tv->v_type != VAR_STRING)
1506 {
1507 char_u *str;
1508
1509 if (is_2string_any)
1510 {
1511 switch (tv->v_type)
1512 {
1513 case VAR_SPECIAL:
1514 case VAR_BOOL:
1515 case VAR_NUMBER:
1516 case VAR_FLOAT:
1517 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001518
1519 case VAR_LIST:
1520 if (tolerant)
1521 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001522 char_u *s, *e, *p;
1523 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001524
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001525 ga_init2(&ga, sizeof(char_u *), 1);
1526
1527 // Convert to NL separated items, then
1528 // escape the items and replace the NL with
1529 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001530 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001531 if (str == NULL)
1532 return FAIL;
1533 s = str;
1534 while ((e = vim_strchr(s, '\n')) != NULL)
1535 {
1536 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001537 p = vim_strsave_fnameescape(s,
1538 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001539 if (p != NULL)
1540 {
1541 ga_concat(&ga, p);
1542 ga_concat(&ga, (char_u *)" ");
1543 vim_free(p);
1544 }
1545 s = e + 1;
1546 }
1547 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001548 clear_tv(tv);
1549 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001550 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001551 return OK;
1552 }
1553 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001554 default: to_string_error(tv->v_type);
1555 return FAIL;
1556 }
1557 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001558 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001559 clear_tv(tv);
1560 tv->v_type = VAR_STRING;
1561 tv->vval.v_string = str;
1562 }
1563 return OK;
1564}
1565
1566/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001567 * When the value of "sv" is a null list of dict, allocate it.
1568 */
1569 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001570allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001571{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001572 typval_T *tv = sv->sv_tv;
1573
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001574 switch (tv->v_type)
1575 {
1576 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001577 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001578 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001579 break;
1580 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001581 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001582 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001583 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001584 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001585 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001586 (void)rettv_blob_alloc(tv);
1587 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001588 default:
1589 break;
1590 }
1591}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001592
Bram Moolenaare7525c52021-01-09 13:20:37 +01001593/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001594 * Return the character "str[index]" where "index" is the character index,
1595 * including composing characters.
1596 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001597 */
1598 char_u *
1599char_from_string(char_u *str, varnumber_T index)
1600{
1601 size_t nbyte = 0;
1602 varnumber_T nchar = index;
1603 size_t slen;
1604
1605 if (str == NULL)
1606 return NULL;
1607 slen = STRLEN(str);
1608
Bram Moolenaarff871402021-03-26 13:34:05 +01001609 // Do the same as for a list: a negative index counts from the end.
1610 // Optimization to check the first byte to be below 0x80 (and no composing
1611 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001612 if (index < 0)
1613 {
1614 int clen = 0;
1615
1616 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001617 {
1618 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1619 ++nbyte;
1620 else if (enc_utf8)
1621 nbyte += utfc_ptr2len(str + nbyte);
1622 else
1623 nbyte += mb_ptr2len(str + nbyte);
1624 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001625 nchar = clen + index;
1626 if (nchar < 0)
1627 // unlike list: index out of range results in empty string
1628 return NULL;
1629 }
1630
1631 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001632 {
1633 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1634 ++nbyte;
1635 else if (enc_utf8)
1636 nbyte += utfc_ptr2len(str + nbyte);
1637 else
1638 nbyte += mb_ptr2len(str + nbyte);
1639 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001640 if (nbyte >= slen)
1641 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001642 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001643}
1644
1645/*
1646 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001647 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001648 * If going over the end return "str_len".
1649 * If "idx" is negative count from the end, -1 is the last character.
1650 * When going over the start return -1.
1651 */
1652 static long
1653char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1654{
1655 varnumber_T nchar = idx;
1656 size_t nbyte = 0;
1657
1658 if (nchar >= 0)
1659 {
1660 while (nchar > 0 && nbyte < str_len)
1661 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001662 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001663 --nchar;
1664 }
1665 }
1666 else
1667 {
1668 nbyte = str_len;
1669 while (nchar < 0 && nbyte > 0)
1670 {
1671 --nbyte;
1672 nbyte -= mb_head_off(str, str + nbyte);
1673 ++nchar;
1674 }
1675 if (nchar < 0)
1676 return -1;
1677 }
1678 return (long)nbyte;
1679}
1680
1681/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001682 * Return the slice "str[first : last]" using character indexes. Composing
1683 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001684 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001685 * Return NULL when the result is empty.
1686 */
1687 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001688string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001689{
1690 long start_byte, end_byte;
1691 size_t slen;
1692
1693 if (str == NULL)
1694 return NULL;
1695 slen = STRLEN(str);
1696 start_byte = char_idx2byte(str, slen, first);
1697 if (start_byte < 0)
1698 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001699 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001700 end_byte = (long)slen;
1701 else
1702 {
1703 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001704 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001705 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001706 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001707 }
1708
1709 if (start_byte >= (long)slen || end_byte <= start_byte)
1710 return NULL;
1711 return vim_strnsave(str + start_byte, end_byte - start_byte);
1712}
1713
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001714/*
1715 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1716 * When "dfunc_idx" is negative don't give an error.
1717 * Returns NULL for an error.
1718 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001719 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001720get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001721{
1722 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001723 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1724 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001725 svar_T *sv;
1726
1727 if (sref->sref_seq != si->sn_script_seq)
1728 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001729 // The script was reloaded after the function was compiled, the
1730 // script_idx may not be valid.
1731 if (dfunc != NULL)
1732 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1733 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001734 return NULL;
1735 }
1736 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001737 if (sv->sv_name == NULL)
1738 {
1739 if (dfunc != NULL)
1740 emsg(_(e_script_variable_was_deleted));
1741 return NULL;
1742 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001743 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001744 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001745 if (dfunc != NULL)
1746 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001747 return NULL;
1748 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001749
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001750 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1751 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001752 {
1753 if (dfunc != NULL)
1754 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1755 return NULL;
1756 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001757 return sv;
1758}
1759
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001760/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001761 * Function passed to do_cmdline() for splitting a script joined by NL
1762 * characters.
1763 */
1764 static char_u *
1765get_split_sourceline(
1766 int c UNUSED,
1767 void *cookie,
1768 int indent UNUSED,
1769 getline_opt_T options UNUSED)
1770{
1771 source_cookie_T *sp = (source_cookie_T *)cookie;
1772 char_u *p;
1773 char_u *line;
1774
Bram Moolenaar20677332021-06-06 17:02:53 +02001775 p = vim_strchr(sp->nextline, '\n');
1776 if (p == NULL)
1777 {
1778 line = vim_strsave(sp->nextline);
1779 sp->nextline += STRLEN(sp->nextline);
1780 }
1781 else
1782 {
1783 line = vim_strnsave(sp->nextline, p - sp->nextline);
1784 sp->nextline = p + 1;
1785 }
1786 return line;
1787}
1788
1789/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 * Execute a function by "name".
1791 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001792 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 */
1794 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001795call_eval_func(
1796 char_u *name,
1797 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001798 ectx_T *ectx,
1799 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800{
Bram Moolenaared677f52020-08-12 16:38:10 +02001801 int called_emsg_before = called_emsg;
1802 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001804 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001805 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001807 dictitem_T *v;
1808
1809 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001810 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1811 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001812 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001813 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001814 return FAIL;
1815 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001816 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001818 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819}
1820
1821/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001822 * When a function reference is used, fill a partial with the information
1823 * needed, especially when it is used as a closure.
1824 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001825 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001826fill_partial_and_closure(
1827 partial_T *pt,
1828 ufunc_T *ufunc,
1829 short loop_var_idx,
1830 short loop_var_count,
1831 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001832{
1833 pt->pt_func = ufunc;
1834 pt->pt_refcount = 1;
1835
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001836 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001837 {
1838 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1839 + ectx->ec_dfunc_idx;
1840
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001841 // The closure may need to find arguments and local variables of the
1842 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001843 pt->pt_outer.out_stack = &ectx->ec_stack;
1844 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001845 if (ectx->ec_outer_ref != NULL)
1846 {
1847 // The current context already has a context, link to that one.
1848 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1849 if (ectx->ec_outer_ref->or_partial != NULL)
1850 {
1851 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1852 ++pt->pt_outer.out_up_partial->pt_refcount;
1853 }
1854 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001855
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001856 // The closure may need to find variables defined inside a loop. A
1857 // new reference is made every time, ISN_ENDLOOP will check if they
1858 // are actually used.
1859 pt->pt_outer.out_loop_stack = &ectx->ec_stack;
1860 pt->pt_outer.out_loop_var_idx = ectx->ec_frame_idx + STACK_FRAME_SIZE
1861 + loop_var_idx;
1862 pt->pt_outer.out_loop_var_count = loop_var_count;
1863
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001864 // If the function currently executing returns and the closure is still
1865 // being referenced, we need to make a copy of the context (arguments
1866 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001867 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001868 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001869 {
1870 vim_free(pt);
1871 return FAIL;
1872 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001873 // Extra variable keeps the count of closures created in the current
1874 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001875 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1876 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1877
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001878 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1879 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001880 ++pt->pt_refcount;
1881 ++ectx->ec_funcrefs.ga_len;
1882 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001883 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001884 return OK;
1885}
1886
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001887/*
1888 * Execute iptr->isn_arg.string as an Ex command.
1889 */
1890 static int
1891exec_command(isn_T *iptr)
1892{
1893 source_cookie_T cookie;
1894
1895 SOURCING_LNUM = iptr->isn_lnum;
1896 // Pass getsourceline to get an error for a missing ":end"
1897 // command.
1898 CLEAR_FIELD(cookie);
1899 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1900 if (do_cmdline(iptr->isn_arg.string,
1901 getsourceline, &cookie,
1902 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1903 || did_emsg)
1904 return FAIL;
1905 return OK;
1906}
1907
Bram Moolenaar89445512022-04-14 12:58:23 +01001908/*
1909 * If script "sid" is not loaded yet then load it now.
1910 * Caller must make sure "sid" is a valid script ID.
1911 * "loaded" is set to TRUE if the script had to be loaded.
1912 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1913 */
1914 int
1915may_load_script(int sid, int *loaded)
1916{
1917 scriptitem_T *si = SCRIPT_ITEM(sid);
1918
1919 if (si->sn_state == SN_STATE_NOT_LOADED)
1920 {
1921 *loaded = TRUE;
1922 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1923 {
1924 semsg(_(e_cant_open_file_str), si->sn_name);
1925 return FAIL;
1926 }
1927 }
1928 return OK;
1929}
1930
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001931// used for v_instr of typval of VAR_INSTR
1932struct instr_S {
1933 ectx_T *instr_ectx;
1934 isn_T *instr_instr;
1935};
1936
Bram Moolenaar4c137212021-04-19 16:48:48 +02001937// used for substitute_instr
1938typedef struct subs_expr_S {
1939 ectx_T *subs_ectx;
1940 isn_T *subs_instr;
1941 int subs_status;
1942} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001944// Set when calling do_debug().
1945static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001946static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001947
1948/*
1949 * When debugging lookup "name" and return the typeval.
1950 * When not found return NULL.
1951 */
1952 typval_T *
1953lookup_debug_var(char_u *name)
1954{
1955 int idx;
1956 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001957 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001958 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001959 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001960
1961 if (ectx == NULL)
1962 return NULL;
1963 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1964
1965 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001966 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001967 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001968 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1969
1970 // the variable name may be NULL when not available in this block
1971 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001972 return STACK_TV_VAR(idx);
1973 }
1974
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001975 // Go through argument names.
1976 ufunc = dfunc->df_ufunc;
1977 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1978 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1979 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1980 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1981 - varargs_off + idx);
1982 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1983 return STACK_TV(ectx->ec_frame_idx - 1);
1984
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001985 return NULL;
1986}
1987
Bram Moolenaar26a44842021-09-02 18:49:06 +02001988/*
1989 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1990 * breakpoint was set in that function or when there is any expression.
1991 */
1992 int
1993may_break_in_function(ufunc_T *ufunc)
1994{
1995 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1996}
1997
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001998 static void
1999handle_debug(isn_T *iptr, ectx_T *ectx)
2000{
2001 char_u *line;
2002 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2003 + ectx->ec_dfunc_idx)->df_ufunc;
2004 isn_T *ni;
2005 int end_lnum = iptr->isn_lnum;
2006 garray_T ga;
2007 int lnum;
2008
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002009 if (ex_nesting_level > debug_break_level)
2010 {
2011 linenr_T breakpoint;
2012
Bram Moolenaar26a44842021-09-02 18:49:06 +02002013 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002014 return;
2015
2016 // check for the next breakpoint if needed
2017 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002018 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002019 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2020 return;
2021 }
2022
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002023 SOURCING_LNUM = iptr->isn_lnum;
2024 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002025 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002026
2027 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2028 if (ni->isn_type == ISN_DEBUG
2029 || ni->isn_type == ISN_RETURN
2030 || ni->isn_type == ISN_RETURN_VOID)
2031 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002032 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002033 break;
2034 }
2035
2036 if (end_lnum > iptr->isn_lnum)
2037 {
2038 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002039 for (lnum = iptr->isn_lnum; lnum < end_lnum
2040 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002041 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002042 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002043
Bram Moolenaar303215d2021-07-07 20:10:43 +02002044 if (p == NULL)
2045 continue; // left over from continuation line
2046 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002047 if (*p == '#')
2048 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002049 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002050 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2051 if (STRNCMP(p, "def ", 4) == 0)
2052 break;
2053 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002054 line = ga_concat_strings(&ga, " ");
2055 vim_free(ga.ga_data);
2056 }
2057 else
2058 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002059
Dominique Pelle6e969552021-06-17 13:53:41 +02002060 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002061 debug_context = NULL;
2062
2063 if (end_lnum > iptr->isn_lnum)
2064 vim_free(line);
2065}
2066
Bram Moolenaar4c137212021-04-19 16:48:48 +02002067/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002068 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002069 * Returns OK, FAIL or NOTDONE (uncatchable error).
2070 */
2071 static int
2072execute_storeindex(isn_T *iptr, ectx_T *ectx)
2073{
2074 vartype_T dest_type = iptr->isn_arg.vartype;
2075 typval_T *tv;
2076 typval_T *tv_idx = STACK_TV_BOT(-2);
2077 typval_T *tv_dest = STACK_TV_BOT(-1);
2078 int status = OK;
2079
2080 // Stack contains:
2081 // -3 value to be stored
2082 // -2 index
2083 // -1 dict or list
2084 tv = STACK_TV_BOT(-3);
2085 SOURCING_LNUM = iptr->isn_lnum;
2086 if (dest_type == VAR_ANY)
2087 {
2088 dest_type = tv_dest->v_type;
2089 if (dest_type == VAR_DICT)
2090 status = do_2string(tv_idx, TRUE, FALSE);
2091 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2092 {
2093 emsg(_(e_number_expected));
2094 status = FAIL;
2095 }
2096 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002097
2098 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002099 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002100 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002101 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002102 long lidx = (long)tv_idx->vval.v_number;
2103 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002104
Bram Moolenaare08be092022-02-17 13:08:26 +00002105 if (list == NULL)
2106 {
2107 emsg(_(e_list_not_set));
2108 return FAIL;
2109 }
2110 if (lidx < 0 && list->lv_len + lidx >= 0)
2111 // negative index is relative to the end
2112 lidx = list->lv_len + lidx;
2113 if (lidx < 0 || lidx > list->lv_len)
2114 {
2115 semsg(_(e_list_index_out_of_range_nr), lidx);
2116 return FAIL;
2117 }
2118 if (lidx < list->lv_len)
2119 {
2120 listitem_T *li = list_find(list, lidx);
2121
2122 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002123 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002124 return FAIL;
2125 // overwrite existing list item
2126 clear_tv(&li->li_tv);
2127 li->li_tv = *tv;
2128 }
2129 else
2130 {
2131 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2132 return FAIL;
2133 // append to list, only fails when out of memory
2134 if (list_append_tv(list, tv) == FAIL)
2135 return NOTDONE;
2136 clear_tv(tv);
2137 }
2138 }
2139 else if (dest_type == VAR_DICT)
2140 {
2141 char_u *key = tv_idx->vval.v_string;
2142 dict_T *dict = tv_dest->vval.v_dict;
2143 dictitem_T *di;
2144
2145 SOURCING_LNUM = iptr->isn_lnum;
2146 if (dict == NULL)
2147 {
2148 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002149 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002150 }
2151 if (key == NULL)
2152 key = (char_u *)"";
2153 di = dict_find(dict, key, -1);
2154 if (di != NULL)
2155 {
2156 if (error_if_locked(di->di_tv.v_lock,
2157 e_cannot_change_dict_item))
2158 return FAIL;
2159 // overwrite existing value
2160 clear_tv(&di->di_tv);
2161 di->di_tv = *tv;
2162 }
2163 else
2164 {
2165 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2166 return FAIL;
2167 // add to dict, only fails when out of memory
2168 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2169 return NOTDONE;
2170 clear_tv(tv);
2171 }
2172 }
2173 else if (dest_type == VAR_BLOB)
2174 {
2175 long lidx = (long)tv_idx->vval.v_number;
2176 blob_T *blob = tv_dest->vval.v_blob;
2177 varnumber_T nr;
2178 int error = FALSE;
2179 int len;
2180
2181 if (blob == NULL)
2182 {
2183 emsg(_(e_blob_not_set));
2184 return FAIL;
2185 }
2186 len = blob_len(blob);
2187 if (lidx < 0 && len + lidx >= 0)
2188 // negative index is relative to the end
2189 lidx = len + lidx;
2190
2191 // Can add one byte at the end.
2192 if (lidx < 0 || lidx > len)
2193 {
2194 semsg(_(e_blob_index_out_of_range_nr), lidx);
2195 return FAIL;
2196 }
2197 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2198 return FAIL;
2199 nr = tv_get_number_chk(tv, &error);
2200 if (error)
2201 return FAIL;
2202 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002203 }
2204 else
2205 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002206 status = FAIL;
2207 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002208 }
2209 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002210
2211 clear_tv(tv_idx);
2212 clear_tv(tv_dest);
2213 ectx->ec_stack.ga_len -= 3;
2214 if (status == FAIL)
2215 {
2216 clear_tv(tv);
2217 return FAIL;
2218 }
2219 return OK;
2220}
2221
2222/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002223 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002224 */
2225 static int
2226execute_storerange(isn_T *iptr, ectx_T *ectx)
2227{
2228 typval_T *tv;
2229 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2230 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2231 typval_T *tv_dest = STACK_TV_BOT(-1);
2232 int status = OK;
2233
2234 // Stack contains:
2235 // -4 value to be stored
2236 // -3 first index or "none"
2237 // -2 second index or "none"
2238 // -1 destination list or blob
2239 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002240 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002241 if (tv_dest->v_type == VAR_LIST)
2242 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002243 long n1;
2244 long n2;
2245 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002246
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002247 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2248 if (tv_idx2->v_type == VAR_SPECIAL
2249 && tv_idx2->vval.v_number == VVAL_NONE)
2250 n2 = list_len(tv_dest->vval.v_list) - 1;
2251 else
2252 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2253
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002254 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002255 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002256 status = FAIL;
2257 else
2258 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002259 status = check_range_index_two(tv_dest->vval.v_list,
2260 &n1, li1, &n2, FALSE);
2261 if (status != FAIL)
2262 status = list_assign_range(
2263 tv_dest->vval.v_list,
2264 tv->vval.v_list,
2265 n1,
2266 n2,
2267 tv_idx2->v_type == VAR_SPECIAL,
2268 (char_u *)"=",
2269 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002270 }
2271 }
2272 else if (tv_dest->v_type == VAR_BLOB)
2273 {
2274 varnumber_T n1;
2275 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002276 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002277
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002278 n1 = tv_get_number_chk(tv_idx1, NULL);
2279 if (tv_idx2->v_type == VAR_SPECIAL
2280 && tv_idx2->vval.v_number == VVAL_NONE)
2281 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2282 else
2283 n2 = tv_get_number_chk(tv_idx2, NULL);
2284 bloblen = blob_len(tv_dest->vval.v_blob);
2285
2286 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2287 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002288 status = FAIL;
2289 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002290 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002291 }
2292 else
2293 {
2294 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002295 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002296 }
2297
2298 clear_tv(tv_idx1);
2299 clear_tv(tv_idx2);
2300 clear_tv(tv_dest);
2301 ectx->ec_stack.ga_len -= 4;
2302 clear_tv(tv);
2303
2304 return status;
2305}
2306
2307/*
2308 * Unlet item in list or dict variable.
2309 */
2310 static int
2311execute_unletindex(isn_T *iptr, ectx_T *ectx)
2312{
2313 typval_T *tv_idx = STACK_TV_BOT(-2);
2314 typval_T *tv_dest = STACK_TV_BOT(-1);
2315 int status = OK;
2316
2317 // Stack contains:
2318 // -2 index
2319 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002320 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002321 if (tv_dest->v_type == VAR_DICT)
2322 {
2323 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002324 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002325 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002326 semsg(_(e_expected_str_but_got_str),
2327 vartype_name(VAR_STRING),
2328 vartype_name(tv_idx->v_type));
2329 status = FAIL;
2330 }
2331 else
2332 {
2333 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002334 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002335 dictitem_T *di = NULL;
2336
2337 if (d != NULL && value_check_lock(
2338 d->dv_lock, NULL, FALSE))
2339 status = FAIL;
2340 else
2341 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002342 if (tv_idx->v_type == VAR_STRING)
2343 {
2344 key = tv_idx->vval.v_string;
2345 if (key == NULL)
2346 key = (char_u *)"";
2347 }
2348 else
2349 {
2350 key = tv_get_string(tv_idx);
2351 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002352 if (d != NULL)
2353 di = dict_find(d, key, (int)STRLEN(key));
2354 if (di == NULL)
2355 {
2356 // NULL dict is equivalent to empty dict
2357 semsg(_(e_key_not_present_in_dictionary),
2358 key);
2359 status = FAIL;
2360 }
2361 else if (var_check_fixed(di->di_flags,
2362 NULL, FALSE)
2363 || var_check_ro(di->di_flags,
2364 NULL, FALSE))
2365 status = FAIL;
2366 else
2367 dictitem_remove(d, di);
2368 }
2369 }
2370 }
2371 else if (tv_dest->v_type == VAR_LIST)
2372 {
2373 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002374 if (check_for_number(tv_idx) == FAIL)
2375 {
2376 status = FAIL;
2377 }
2378 else
2379 {
2380 list_T *l = tv_dest->vval.v_list;
2381 long n = (long)tv_idx->vval.v_number;
2382
2383 if (l != NULL && value_check_lock(
2384 l->lv_lock, NULL, FALSE))
2385 status = FAIL;
2386 else
2387 {
2388 listitem_T *li = list_find(l, n);
2389
2390 if (li == NULL)
2391 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002392 semsg(_(e_list_index_out_of_range_nr), n);
2393 status = FAIL;
2394 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002395 else
2396 listitem_remove(l, li);
2397 }
2398 }
2399 }
2400 else
2401 {
2402 status = FAIL;
2403 semsg(_(e_cannot_index_str),
2404 vartype_name(tv_dest->v_type));
2405 }
2406
2407 clear_tv(tv_idx);
2408 clear_tv(tv_dest);
2409 ectx->ec_stack.ga_len -= 2;
2410
2411 return status;
2412}
2413
2414/*
2415 * Unlet a range of items in a list variable.
2416 */
2417 static int
2418execute_unletrange(isn_T *iptr, ectx_T *ectx)
2419{
2420 // Stack contains:
2421 // -3 index1
2422 // -2 index2
2423 // -1 dict or list
2424 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2425 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2426 typval_T *tv_dest = STACK_TV_BOT(-1);
2427 int status = OK;
2428
2429 if (tv_dest->v_type == VAR_LIST)
2430 {
2431 // indexes must be a number
2432 SOURCING_LNUM = iptr->isn_lnum;
2433 if (check_for_number(tv_idx1) == FAIL
2434 || (tv_idx2->v_type != VAR_SPECIAL
2435 && check_for_number(tv_idx2) == FAIL))
2436 {
2437 status = FAIL;
2438 }
2439 else
2440 {
2441 list_T *l = tv_dest->vval.v_list;
2442 long n1 = (long)tv_idx1->vval.v_number;
2443 long n2 = tv_idx2->v_type == VAR_SPECIAL
2444 ? 0 : (long)tv_idx2->vval.v_number;
2445 listitem_T *li;
2446
2447 li = list_find_index(l, &n1);
2448 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002449 {
2450 semsg(_(e_list_index_out_of_range_nr),
2451 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002452 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002453 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002454 else
2455 {
2456 if (n1 < 0)
2457 n1 = list_idx_of_item(l, li);
2458 if (n2 < 0)
2459 {
2460 listitem_T *li2 = list_find(l, n2);
2461
2462 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002463 {
2464 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002465 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002466 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002467 else
2468 n2 = list_idx_of_item(l, li2);
2469 }
2470 if (status != FAIL
2471 && tv_idx2->v_type != VAR_SPECIAL
2472 && n2 < n1)
2473 {
2474 semsg(_(e_list_index_out_of_range_nr), n2);
2475 status = FAIL;
2476 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002477 if (status != FAIL)
2478 list_unlet_range(l, li, n1,
2479 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002480 }
2481 }
2482 }
2483 else
2484 {
2485 status = FAIL;
2486 SOURCING_LNUM = iptr->isn_lnum;
2487 semsg(_(e_cannot_index_str),
2488 vartype_name(tv_dest->v_type));
2489 }
2490
2491 clear_tv(tv_idx1);
2492 clear_tv(tv_idx2);
2493 clear_tv(tv_dest);
2494 ectx->ec_stack.ga_len -= 3;
2495
2496 return status;
2497}
2498
2499/*
2500 * Top of a for loop.
2501 */
2502 static int
2503execute_for(isn_T *iptr, ectx_T *ectx)
2504{
2505 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002506 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002507 typval_T *ltv = STACK_TV_BOT(-1);
2508 typval_T *idxtv =
2509 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2510
2511 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2512 return FAIL;
2513 if (ltv->v_type == VAR_LIST)
2514 {
2515 list_T *list = ltv->vval.v_list;
2516
2517 // push the next item from the list
2518 ++idxtv->vval.v_number;
2519 if (list == NULL
2520 || idxtv->vval.v_number >= list->lv_len)
2521 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002522 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002523 }
2524 else if (list->lv_first == &range_list_item)
2525 {
2526 // non-materialized range() list
2527 tv = STACK_TV_BOT(0);
2528 tv->v_type = VAR_NUMBER;
2529 tv->v_lock = 0;
2530 tv->vval.v_number = list_find_nr(
2531 list, idxtv->vval.v_number, NULL);
2532 ++ectx->ec_stack.ga_len;
2533 }
2534 else
2535 {
2536 listitem_T *li = list_find(list,
2537 idxtv->vval.v_number);
2538
2539 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2540 ++ectx->ec_stack.ga_len;
2541 }
2542 }
2543 else if (ltv->v_type == VAR_STRING)
2544 {
2545 char_u *str = ltv->vval.v_string;
2546
2547 // The index is for the last byte of the previous
2548 // character.
2549 ++idxtv->vval.v_number;
2550 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2551 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002552 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002553 }
2554 else
2555 {
2556 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2557
2558 // Push the next character from the string.
2559 tv = STACK_TV_BOT(0);
2560 tv->v_type = VAR_STRING;
2561 tv->vval.v_string = vim_strnsave(
2562 str + idxtv->vval.v_number, clen);
2563 ++ectx->ec_stack.ga_len;
2564 idxtv->vval.v_number += clen - 1;
2565 }
2566 }
2567 else if (ltv->v_type == VAR_BLOB)
2568 {
2569 blob_T *blob = ltv->vval.v_blob;
2570
2571 // When we get here the first time make a copy of the
2572 // blob, so that the iteration still works when it is
2573 // changed.
2574 if (idxtv->vval.v_number == -1 && blob != NULL)
2575 {
2576 blob_copy(blob, ltv);
2577 blob_unref(blob);
2578 blob = ltv->vval.v_blob;
2579 }
2580
2581 // The index is for the previous byte.
2582 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002583 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002584 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002585 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002586 }
2587 else
2588 {
2589 // Push the next byte from the blob.
2590 tv = STACK_TV_BOT(0);
2591 tv->v_type = VAR_NUMBER;
2592 tv->vval.v_number = blob_get(blob,
2593 idxtv->vval.v_number);
2594 ++ectx->ec_stack.ga_len;
2595 }
2596 }
2597 else
2598 {
2599 semsg(_(e_for_loop_on_str_not_supported),
2600 vartype_name(ltv->v_type));
2601 return FAIL;
2602 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002603
2604 if (jump)
2605 {
2606 // past the end of the list/string/blob, jump to "endfor"
2607 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2608 may_restore_cmdmod(&ectx->ec_funclocal);
2609 }
2610 else
2611 {
2612 // Store the current number of funcrefs, this may be used in
2613 // ISN_LOOPEND. The variable index is always one more than the loop
2614 // variable index.
2615 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_idx + 1);
2616 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2617 }
2618
2619 return OK;
2620}
2621
2622/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002623 * Code for handling variables declared inside a loop and used in a closure.
2624 * This is very similar to what is done with funcstack_T. The difference is
2625 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2626 * scope of the block inside a loop and each loop may have its own.
2627 */
2628
2629// Double linked list of loopvars_T in use.
2630static loopvars_T *first_loopvars = NULL;
2631
2632 static void
2633add_loopvars_to_list(loopvars_T *loopvars)
2634{
2635 // Link in list of loopvarss.
2636 if (first_loopvars != NULL)
2637 first_loopvars->lvs_prev = loopvars;
2638 loopvars->lvs_next = first_loopvars;
2639 loopvars->lvs_prev = NULL;
2640 first_loopvars = loopvars;
2641}
2642
2643 static void
2644remove_loopvars_from_list(loopvars_T *loopvars)
2645{
2646 if (loopvars->lvs_prev == NULL)
2647 first_loopvars = loopvars->lvs_next;
2648 else
2649 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2650 if (loopvars->lvs_next != NULL)
2651 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2652}
2653
2654/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002655 * End of a for or while loop: Handle any variables used by a closure.
2656 */
2657 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002658execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002659{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002660 endloop_T *endloop = &iptr->isn_arg.endloop;
2661 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2662 int prev_closure_count = tv_refcount->vval.v_number;
2663 garray_T *gap = &ectx->ec_funcrefs;
2664 int closure_in_use = FALSE;
2665 loopvars_T *loopvars;
2666 typval_T *stack;
2667 int idx;
2668
2669 // Check if any created closure is still being referenced.
2670 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2671 {
2672 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2673
Bram Moolenaardbbb02b2022-09-18 12:00:21 +01002674 if (pt->pt_refcount > 1 && pt->pt_loopvars == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002675 {
2676 int refcount = pt->pt_refcount;
2677 int i;
2678
2679 // A Reference in a variable inside the loop doesn't count, it gets
2680 // unreferenced at the end of the loop.
2681 for (i = 0; i < endloop->end_var_count; ++i)
2682 {
2683 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2684
2685 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2686 --refcount;
2687 }
2688 if (refcount > 1)
2689 {
2690 closure_in_use = TRUE;
2691 break;
2692 }
2693 }
2694 }
2695
2696 // If no function reference were created since the start of the loop block
2697 // or it is no longer referenced there is nothing to do.
2698 if (!closure_in_use)
2699 return OK;
2700
2701 // A closure is using variables declared inside the loop.
2702 // Move them to the called function.
2703 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2704 if (loopvars == NULL)
2705 return FAIL;
2706
2707 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2708 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2709 loopvars->lvs_ga.ga_data = stack;
2710 if (stack == NULL)
2711 {
2712 vim_free(loopvars);
2713 return FAIL;
2714 }
2715 add_loopvars_to_list(loopvars);
2716
2717 // Move the variable values.
2718 for (idx = 0; idx < endloop->end_var_count; ++idx)
2719 {
2720 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2721
2722 *(stack + idx) = *tv;
2723 tv->v_type = VAR_UNKNOWN;
2724 }
2725
2726 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2727 {
2728 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2729
Bram Moolenaardbbb02b2022-09-18 12:00:21 +01002730 if (pt->pt_refcount > 1 && pt->pt_loopvars == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002731 {
2732 ++loopvars->lvs_refcount;
2733 pt->pt_loopvars = loopvars;
2734
2735 pt->pt_outer.out_loop_stack = &loopvars->lvs_ga;
2736 pt->pt_outer.out_loop_var_idx -= ectx->ec_frame_idx
2737 + STACK_FRAME_SIZE + endloop->end_var_idx;
2738 }
2739 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002740
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002741 return OK;
2742}
2743
2744/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002745 * Called when a partial is freed or its reference count goes down to one. The
2746 * loopvars may be the only reference to the partials in the local variables.
2747 * Go over all of them, the funcref and can be freed if all partials
2748 * referencing the loopvars have a reference count of one.
2749 */
2750 void
2751loopvars_check_refcount(loopvars_T *loopvars)
2752{
2753 int i;
2754 garray_T *gap = &loopvars->lvs_ga;
2755 int done = 0;
2756
2757 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2758 return;
2759 for (i = 0; i < gap->ga_len; ++i)
2760 {
2761 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2762
2763 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2764 && tv->vval.v_partial->pt_loopvars == loopvars
2765 && tv->vval.v_partial->pt_refcount == 1)
2766 ++done;
2767 }
2768 if (done == loopvars->lvs_min_refcount)
2769 {
2770 typval_T *stack = gap->ga_data;
2771
2772 // All partials referencing the loopvars have a reference count of
2773 // one, thus the loopvars is no longer of use.
2774 for (i = 0; i < gap->ga_len; ++i)
2775 clear_tv(stack + i);
2776 vim_free(stack);
2777 remove_loopvars_from_list(loopvars);
2778 vim_free(loopvars);
2779 }
2780}
2781
2782/*
2783 * For garbage collecting: set references in all variables referenced by
2784 * all loopvars.
2785 */
2786 int
2787set_ref_in_loopvars(int copyID)
2788{
2789 loopvars_T *loopvars;
2790
2791 for (loopvars = first_loopvars; loopvars != NULL;
2792 loopvars = loopvars->lvs_next)
2793 {
2794 typval_T *stack = loopvars->lvs_ga.ga_data;
2795 int i;
2796
2797 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2798 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2799 return TRUE; // abort
2800 }
2801 return FALSE;
2802}
2803
2804/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002805 * Load instruction for w:/b:/g:/t: variable.
2806 * "isn_type" is used instead of "iptr->isn_type".
2807 */
2808 static int
2809load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2810{
2811 dictitem_T *di = NULL;
2812 hashtab_T *ht = NULL;
2813 char namespace;
2814
2815 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2816 return NOTDONE;
2817 switch (isn_type)
2818 {
2819 case ISN_LOADG:
2820 ht = get_globvar_ht();
2821 namespace = 'g';
2822 break;
2823 case ISN_LOADB:
2824 ht = &curbuf->b_vars->dv_hashtab;
2825 namespace = 'b';
2826 break;
2827 case ISN_LOADW:
2828 ht = &curwin->w_vars->dv_hashtab;
2829 namespace = 'w';
2830 break;
2831 case ISN_LOADT:
2832 ht = &curtab->tp_vars->dv_hashtab;
2833 namespace = 't';
2834 break;
2835 default: // Cannot reach here
2836 return NOTDONE;
2837 }
2838 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2839
Bram Moolenaar06b77222022-01-25 15:51:56 +00002840 if (di == NULL)
2841 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002842 if (isn_type == ISN_LOADG)
2843 {
2844 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2845
2846 // g:Something could be a function
2847 if (ufunc != NULL)
2848 {
2849 typval_T *tv = STACK_TV_BOT(0);
2850
2851 ++ectx->ec_stack.ga_len;
2852 tv->v_type = VAR_FUNC;
2853 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2854 if (tv->vval.v_string == NULL)
2855 return FAIL;
2856 STRCPY(tv->vval.v_string, "g:");
2857 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2858 return OK;
2859 }
2860 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002861 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002862 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002863 // no check if the item exists in the script but
2864 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002865 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002866 else
2867 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002868 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002869 return FAIL;
2870 }
2871 else
2872 {
2873 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2874 ++ectx->ec_stack.ga_len;
2875 }
2876 return OK;
2877}
2878
2879/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002880 * Execute instructions in execution context "ectx".
2881 * Return OK or FAIL;
2882 */
2883 static int
2884exec_instructions(ectx_T *ectx)
2885{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002886 int ret = FAIL;
2887 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002888 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002889
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002890 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002891 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002892
Bram Moolenaarff652882021-05-16 15:24:49 +02002893 // Only catch exceptions in this instruction list.
2894 ectx->ec_trylevel_at_start = trylevel;
2895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 for (;;)
2897 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002898 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002900 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002901
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002902 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002903 {
2904 line_breakcheck();
2905 breakcheck_count = 0;
2906 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002907 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002908 {
2909 // Turn CTRL-C into an exception.
2910 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002911 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002912 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002913 did_throw = TRUE;
2914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002916 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002917 {
2918 // Turn an error message into an exception.
2919 did_emsg = FALSE;
2920 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002921 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002922 did_throw = TRUE;
2923 *msg_list = NULL;
2924 }
2925
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002926 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002928 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002929 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002930 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931
2932 // An exception jumps to the first catch, finally, or returns from
2933 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002934 while (index > 0)
2935 {
2936 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002937 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002938 break;
2939 // In the catch and finally block of this try we have to go up
2940 // one level.
2941 --index;
2942 trycmd = NULL;
2943 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002944 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002946 if (trycmd->tcd_in_catch)
2947 {
2948 // exception inside ":catch", jump to ":finally" once
2949 ectx->ec_iidx = trycmd->tcd_finally_idx;
2950 trycmd->tcd_finally_idx = 0;
2951 }
2952 else
2953 // jump to first ":catch"
2954 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002955 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002956 did_throw = FALSE; // don't come back here until :endtry
2957 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 }
2959 else
2960 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002961 // Not inside try or need to return from current functions.
2962 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002963 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002964 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002965 tv = STACK_TV_BOT(0);
2966 tv->v_type = VAR_NUMBER;
2967 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002968 ++ectx->ec_stack.ga_len;
2969 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002971 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002972 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002973 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002974 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 goto done;
2976 }
2977
Bram Moolenaar4c137212021-04-19 16:48:48 +02002978 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002979 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 }
2981 continue;
2982 }
2983
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002984 /*
2985 * Big switch on the instruction. Most compilers will be turning this
2986 * into an efficient lookup table, since the "case" values are an enum
2987 * with sequential numbers. It may look ugly, but it should be the
2988 * most efficient way.
2989 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002990 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 switch (iptr->isn_type)
2992 {
2993 // execute Ex command line
2994 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002995 if (exec_command(iptr) == FAIL)
2996 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 break;
2998
Bram Moolenaar20677332021-06-06 17:02:53 +02002999 // execute Ex command line split at NL characters.
3000 case ISN_EXEC_SPLIT:
3001 {
3002 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003003 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003004
3005 SOURCING_LNUM = iptr->isn_lnum;
3006 CLEAR_FIELD(cookie);
3007 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3008 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003009 line = get_split_sourceline(0, &cookie, 0, 0);
3010 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003011 get_split_sourceline, &cookie,
3012 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3013 == FAIL
3014 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003015 {
3016 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003017 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003018 }
3019 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003020 }
3021 break;
3022
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003023 // execute Ex command line that is only a range
3024 case ISN_EXECRANGE:
3025 {
3026 exarg_T ea;
3027 char *error = NULL;
3028
3029 CLEAR_FIELD(ea);
3030 ea.cmdidx = CMD_SIZE;
3031 ea.addr_type = ADDR_LINES;
3032 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003033 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003034 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003035 if (ea.cmd == NULL)
3036 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003037 // error is always NULL when using ADDR_LINES
3038 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003039 if (error != NULL)
3040 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003041 emsg(error);
3042 goto on_error;
3043 }
3044 }
3045 break;
3046
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003047 // Evaluate an expression with legacy syntax, push it onto the
3048 // stack.
3049 case ISN_LEGACY_EVAL:
3050 {
3051 char_u *arg = iptr->isn_arg.string;
3052 int res;
3053 int save_flags = cmdmod.cmod_flags;
3054
Bram Moolenaar35578162021-08-02 19:10:38 +02003055 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003056 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003057 tv = STACK_TV_BOT(0);
3058 init_tv(tv);
3059 cmdmod.cmod_flags |= CMOD_LEGACY;
3060 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3061 cmdmod.cmod_flags = save_flags;
3062 if (res == FAIL)
3063 goto on_error;
3064 ++ectx->ec_stack.ga_len;
3065 }
3066 break;
3067
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003068 // push typeval VAR_INSTR with instructions to be executed
3069 case ISN_INSTR:
3070 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003071 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003072 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003073 tv = STACK_TV_BOT(0);
3074 tv->vval.v_instr = ALLOC_ONE(instr_T);
3075 if (tv->vval.v_instr == NULL)
3076 goto on_error;
3077 ++ectx->ec_stack.ga_len;
3078
3079 tv->v_type = VAR_INSTR;
3080 tv->vval.v_instr->instr_ectx = ectx;
3081 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3082 }
3083 break;
3084
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003085 case ISN_SOURCE:
3086 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003087 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003088
Bram Moolenaar89445512022-04-14 12:58:23 +01003089 SOURCING_LNUM = iptr->isn_lnum;
3090 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003091 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003092 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003093 }
3094 break;
3095
Bram Moolenaar4c137212021-04-19 16:48:48 +02003096 // execute :substitute with an expression
3097 case ISN_SUBSTITUTE:
3098 {
3099 subs_T *subs = &iptr->isn_arg.subs;
3100 source_cookie_T cookie;
3101 struct subs_expr_S *save_instr = substitute_instr;
3102 struct subs_expr_S subs_instr;
3103 int res;
3104
3105 subs_instr.subs_ectx = ectx;
3106 subs_instr.subs_instr = subs->subs_instr;
3107 subs_instr.subs_status = OK;
3108 substitute_instr = &subs_instr;
3109
3110 SOURCING_LNUM = iptr->isn_lnum;
3111 // This is very much like ISN_EXEC
3112 CLEAR_FIELD(cookie);
3113 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3114 res = do_cmdline(subs->subs_cmd,
3115 getsourceline, &cookie,
3116 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3117 substitute_instr = save_instr;
3118
3119 if (res == FAIL || did_emsg
3120 || subs_instr.subs_status == FAIL)
3121 goto on_error;
3122 }
3123 break;
3124
3125 case ISN_FINISH:
3126 goto done;
3127
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003128 case ISN_REDIRSTART:
3129 // create a dummy entry for var_redir_str()
3130 if (alloc_redir_lval() == FAIL)
3131 goto on_error;
3132
3133 // The output is stored in growarray "redir_ga" until
3134 // redirection ends.
3135 init_redir_ga();
3136 redir_vname = 1;
3137 break;
3138
3139 case ISN_REDIREND:
3140 {
3141 char_u *res = get_clear_redir_ga();
3142
3143 // End redirection, put redirected text on the stack.
3144 clear_redir_lval();
3145 redir_vname = 0;
3146
Bram Moolenaar35578162021-08-02 19:10:38 +02003147 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003148 {
3149 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003150 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003151 }
3152 tv = STACK_TV_BOT(0);
3153 tv->v_type = VAR_STRING;
3154 tv->vval.v_string = res;
3155 ++ectx->ec_stack.ga_len;
3156 }
3157 break;
3158
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003159 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003160#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003161 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003162 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3163 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003164 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003165#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003166 break;
3167
3168 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003169#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003170 {
3171 exarg_T ea;
3172 int res;
3173
3174 CLEAR_FIELD(ea);
3175 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3176 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3177 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3178 --ectx->ec_stack.ga_len;
3179 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003180 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003181 res = cexpr_core(&ea, tv);
3182 clear_tv(tv);
3183 if (res == FAIL)
3184 goto on_error;
3185 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003186#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003187 break;
3188
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003189 // execute Ex command from pieces on the stack
3190 case ISN_EXECCONCAT:
3191 {
3192 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003193 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003194 int pass;
3195 int i;
3196 char_u *cmd = NULL;
3197 char_u *str;
3198
3199 for (pass = 1; pass <= 2; ++pass)
3200 {
3201 for (i = 0; i < count; ++i)
3202 {
3203 tv = STACK_TV_BOT(i - count);
3204 str = tv->vval.v_string;
3205 if (str != NULL && *str != NUL)
3206 {
3207 if (pass == 2)
3208 STRCPY(cmd + len, str);
3209 len += STRLEN(str);
3210 }
3211 if (pass == 2)
3212 clear_tv(tv);
3213 }
3214 if (pass == 1)
3215 {
3216 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003217 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003218 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003219 len = 0;
3220 }
3221 }
3222
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003223 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003224 do_cmdline_cmd(cmd);
3225 vim_free(cmd);
3226 }
3227 break;
3228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 // execute :echo {string} ...
3230 case ISN_ECHO:
3231 {
3232 int count = iptr->isn_arg.echo.echo_count;
3233 int atstart = TRUE;
3234 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003235 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236
3237 for (idx = 0; idx < count; ++idx)
3238 {
3239 tv = STACK_TV_BOT(idx - count);
3240 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3241 &atstart, &needclr);
3242 clear_tv(tv);
3243 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003244 if (needclr)
3245 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003246 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 }
3248 break;
3249
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003250 // :execute {string} ...
3251 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003252 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003253 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003254 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003255 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003256 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003257 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003258 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003259 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003260 {
3261 int count = iptr->isn_arg.number;
3262 garray_T ga;
3263 char_u buf[NUMBUFLEN];
3264 char_u *p;
3265 int len;
3266 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003267 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003268
3269 ga_init2(&ga, 1, 80);
3270 for (idx = 0; idx < count; ++idx)
3271 {
3272 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003273 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003274 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003275 if (tv->v_type == VAR_CHANNEL
3276 || tv->v_type == VAR_JOB)
3277 {
3278 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003279 semsg(_(e_using_invalid_value_as_string_str),
3280 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003281 break;
3282 }
3283 else
3284 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003285 }
3286 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003287 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003288
3289 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003290 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003291 failed = TRUE;
3292 else
3293 {
3294 if (ga.ga_len > 0)
3295 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3296 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3297 ga.ga_len += len;
3298 }
3299 clear_tv(tv);
3300 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003301 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003302 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003303 {
3304 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003305 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003306 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003307
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003308 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003309 {
3310 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003311 {
3312 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003313 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003314 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003315 {
3316 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003317 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003318 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003319 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003320 else
3321 {
3322 msg_sb_eol();
3323 if (iptr->isn_type == ISN_ECHOMSG)
3324 {
3325 msg_attr(ga.ga_data, echo_attr);
3326 out_flush();
3327 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003328#ifdef HAS_MESSAGE_WINDOW
3329 else if (iptr->isn_type == ISN_ECHOWINDOW)
3330 {
3331 start_echowindow();
3332 msg_attr(ga.ga_data, echo_attr);
3333 end_echowindow();
3334 }
3335#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003336 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3337 {
3338 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3339 TRUE);
3340 ui_write((char_u *)"\r\n", 2, TRUE);
3341 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003342 else
3343 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003344 SOURCING_LNUM = iptr->isn_lnum;
3345 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003346 }
3347 }
3348 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003349 ga_clear(&ga);
3350 }
3351 break;
3352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 // load local variable or argument
3354 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003355 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003356 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003358 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 break;
3360
3361 // load v: variable
3362 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003363 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003364 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003366 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 break;
3368
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003369 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 case ISN_LOADSCRIPT:
3371 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003372 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 svar_T *sv;
3374
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003375 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003376 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003377 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003378 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003379 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003380 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003382 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 }
3384 break;
3385
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003386 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003388 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003390 int sid = iptr->isn_arg.loadstore.ls_sid;
3391 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003392 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 if (di == NULL)
3396 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003397 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003398 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003399 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 }
3401 else
3402 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003403 if (iptr->isn_type == ISN_LOADEXPORT)
3404 {
3405 int idx = get_script_item_idx(sid, name, 0,
3406 NULL, NULL);
3407 svar_T *sv;
3408
3409 if (idx >= 0)
3410 {
3411 sv = ((svar_T *)SCRIPT_ITEM(sid)
3412 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003413 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003414 {
3415 SOURCING_LNUM = iptr->isn_lnum;
3416 semsg(_(e_item_not_exported_in_script_str),
3417 name);
3418 goto on_error;
3419 }
3420 }
3421 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003422 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003423 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003425 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 }
3427 }
3428 break;
3429
Bram Moolenaard3aac292020-04-19 14:32:17 +02003430 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003432 case ISN_LOADB:
3433 case ISN_LOADW:
3434 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003436 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003437
Bram Moolenaar06b77222022-01-25 15:51:56 +00003438 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003439 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003440 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003441 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003443
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 break;
3445
Bram Moolenaar03290b82020-12-19 16:30:44 +01003446 // load autoload variable
3447 case ISN_LOADAUTO:
3448 {
3449 char_u *name = iptr->isn_arg.string;
3450
Bram Moolenaar35578162021-08-02 19:10:38 +02003451 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003452 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003453 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003454 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003455 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003456 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003457 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003458 }
3459 break;
3460
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003461 // load g:/b:/w:/t: namespace
3462 case ISN_LOADGDICT:
3463 case ISN_LOADBDICT:
3464 case ISN_LOADWDICT:
3465 case ISN_LOADTDICT:
3466 {
3467 dict_T *d = NULL;
3468
3469 switch (iptr->isn_type)
3470 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003471 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3472 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3473 case ISN_LOADWDICT: d = curwin->w_vars; break;
3474 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003475 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003476 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003477 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003478 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003479 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003480 tv = STACK_TV_BOT(0);
3481 tv->v_type = VAR_DICT;
3482 tv->v_lock = 0;
3483 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003484 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003485 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003486 }
3487 break;
3488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 // load &option
3490 case ISN_LOADOPT:
3491 {
3492 typval_T optval;
3493 char_u *name = iptr->isn_arg.string;
3494
Bram Moolenaara8c17702020-04-01 21:17:24 +02003495 // This is not expected to fail, name is checked during
3496 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003497 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003498 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003499 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003500 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003502 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 }
3504 break;
3505
3506 // load $ENV
3507 case ISN_LOADENV:
3508 {
3509 typval_T optval;
3510 char_u *name = iptr->isn_arg.string;
3511
Bram Moolenaar35578162021-08-02 19:10:38 +02003512 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003513 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003514 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003515 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003517 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 }
3519 break;
3520
3521 // load @register
3522 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003523 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003524 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 tv = STACK_TV_BOT(0);
3526 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003527 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003528 // This may result in NULL, which should be equivalent to an
3529 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 tv->vval.v_string = get_reg_contents(
3531 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003532 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 break;
3534
3535 // store local variable
3536 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003537 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 tv = STACK_TV_VAR(iptr->isn_arg.number);
3539 clear_tv(tv);
3540 *tv = *STACK_TV_BOT(0);
3541 break;
3542
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003543 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003544 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003545 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003546 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003547 int sid = iptr->isn_arg.loadstore.ls_sid;
3548 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003549 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003550 dictitem_T *di = find_var_in_ht(ht, 0,
3551 iptr->isn_type == ISN_STORES
3552 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003553
Bram Moolenaar4c137212021-04-19 16:48:48 +02003554 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003555 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003556 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003557 {
3558 if (iptr->isn_type == ISN_STOREEXPORT)
3559 {
3560 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003561 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003562 goto on_error;
3563 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003564 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003565 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003566 else
3567 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003568 if (iptr->isn_type == ISN_STOREEXPORT)
3569 {
3570 int idx = get_script_item_idx(sid, name, 0,
3571 NULL, NULL);
3572
Bram Moolenaar06651632022-04-27 17:54:25 +01003573 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003574 if (idx >= 0)
3575 {
3576 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3577 ->sn_var_vals.ga_data) + idx;
3578
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003579 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003580 {
3581 semsg(_(e_item_not_exported_in_script_str),
3582 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003583 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003584 goto on_error;
3585 }
3586 }
3587 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003588 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003589 {
3590 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003591 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003592 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003593 clear_tv(&di->di_tv);
3594 di->di_tv = *STACK_TV_BOT(0);
3595 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003596 }
3597 break;
3598
3599 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 case ISN_STORESCRIPT:
3601 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003602 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3603 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003605 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003606 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003607 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003608 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003609
3610 // "const" and "final" are checked at compile time, locking
3611 // the value needs to be checked here.
3612 SOURCING_LNUM = iptr->isn_lnum;
3613 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003614 {
3615 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003616 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003617 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 clear_tv(sv->sv_tv);
3620 *sv->sv_tv = *STACK_TV_BOT(0);
3621 }
3622 break;
3623
3624 // store option
3625 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003626 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003628 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3629 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 long n = 0;
3631 char_u *s = NULL;
3632 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003633 char_u numbuf[NUMBUFLEN];
3634 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635
Bram Moolenaar4c137212021-04-19 16:48:48 +02003636 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 tv = STACK_TV_BOT(0);
3638 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003639 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003641 if (s == NULL)
3642 s = (char_u *)"";
3643 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003644 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3645 {
3646 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003647 // If the option can be set to a function reference or
3648 // a lambda and the passed value is a function
3649 // reference, then convert it to the name (string) of
3650 // the function reference.
3651 s = tv2string(tv, &tofree, numbuf, 0);
3652 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003653 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003654 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003655 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003656 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003657 goto on_error;
3658 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003659 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003661 // must be VAR_NUMBER, CHECKTYPE makes sure
3662 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003663 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003664 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003665 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 if (msg != NULL)
3667 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003668 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003670 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 }
3673 break;
3674
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003675 // store $ENV
3676 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003677 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003678 tv = STACK_TV_BOT(0);
3679 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3680 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003681 break;
3682
3683 // store @r
3684 case ISN_STOREREG:
3685 {
3686 int reg = iptr->isn_arg.number;
3687
Bram Moolenaar4c137212021-04-19 16:48:48 +02003688 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003689 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003690 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003691 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003692 }
3693 break;
3694
3695 // store v: variable
3696 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003697 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003698 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3699 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003700 // should not happen, type is checked when compiling
3701 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003702 break;
3703
Bram Moolenaard3aac292020-04-19 14:32:17 +02003704 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003706 case ISN_STOREB:
3707 case ISN_STOREW:
3708 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003710 dictitem_T *di;
3711 hashtab_T *ht;
3712 char_u *name = iptr->isn_arg.string + 2;
3713
Bram Moolenaard3aac292020-04-19 14:32:17 +02003714 switch (iptr->isn_type)
3715 {
3716 case ISN_STOREG:
3717 ht = get_globvar_ht();
3718 break;
3719 case ISN_STOREB:
3720 ht = &curbuf->b_vars->dv_hashtab;
3721 break;
3722 case ISN_STOREW:
3723 ht = &curwin->w_vars->dv_hashtab;
3724 break;
3725 case ISN_STORET:
3726 ht = &curtab->tp_vars->dv_hashtab;
3727 break;
3728 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003729 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003730 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731
Bram Moolenaar4c137212021-04-19 16:48:48 +02003732 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003733 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003735 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 else
3737 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003738 SOURCING_LNUM = iptr->isn_lnum;
3739 if (var_check_permission(di, name) == FAIL)
3740 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 clear_tv(&di->di_tv);
3742 di->di_tv = *STACK_TV_BOT(0);
3743 }
3744 }
3745 break;
3746
Bram Moolenaar03290b82020-12-19 16:30:44 +01003747 // store an autoload variable
3748 case ISN_STOREAUTO:
3749 SOURCING_LNUM = iptr->isn_lnum;
3750 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3751 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003752 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003753 break;
3754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 // store number in local variable
3756 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003757 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 clear_tv(tv);
3759 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003760 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 break;
3762
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003763 // store value in list or dict variable
3764 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003765 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003766 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003767
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003768 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003769 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003770 if (res == NOTDONE)
3771 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003772 }
3773 break;
3774
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003775 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003776 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003777 if (execute_storerange(iptr, ectx) == FAIL)
3778 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003779 break;
3780
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003781 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003782 case ISN_LOADOUTER:
3783 case ISN_STOREOUTER:
3784 {
3785 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003786 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3787 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003788
3789 while (depth > 1 && outer != NULL)
3790 {
3791 outer = outer->out_up;
3792 --depth;
3793 }
3794 if (outer == NULL)
3795 {
3796 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003797 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3798 || ectx->ec_outer_ref == NULL)
3799 // Possibly :def function called from legacy
3800 // context.
3801 emsg(_(e_closure_called_from_invalid_context));
3802 else
3803 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003804 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003805 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003806 if (depth == OUTER_LOOP_DEPTH)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003807 // Variable declared in loop. May be copied if the
3808 // loop block has already ended.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003809 tv = ((typval_T *)outer->out_loop_stack->ga_data)
3810 + outer->out_loop_var_idx
3811 + iptr->isn_arg.outer.outer_idx;
3812 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003813 // Variable declared in a function. May be copied if
3814 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003815 tv = ((typval_T *)outer->out_stack->ga_data)
3816 + outer->out_frame_idx + STACK_FRAME_SIZE
3817 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003818 if (iptr->isn_type == ISN_LOADOUTER)
3819 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003820 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003821 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003822 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003823 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003824 }
3825 else
3826 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003827 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003828 clear_tv(tv);
3829 *tv = *STACK_TV_BOT(0);
3830 }
3831 }
3832 break;
3833
Bram Moolenaar752fc692021-01-04 21:57:11 +01003834 // unlet item in list or dict variable
3835 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003836 if (execute_unletindex(iptr, ectx) == FAIL)
3837 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003838 break;
3839
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003840 // unlet range of items in list variable
3841 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003842 if (execute_unletrange(iptr, ectx) == FAIL)
3843 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003844 break;
3845
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 // push constant
3847 case ISN_PUSHNR:
3848 case ISN_PUSHBOOL:
3849 case ISN_PUSHSPEC:
3850 case ISN_PUSHF:
3851 case ISN_PUSHS:
3852 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003853 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003854 case ISN_PUSHCHANNEL:
3855 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003856 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003857 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003859 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003860 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 switch (iptr->isn_type)
3862 {
3863 case ISN_PUSHNR:
3864 tv->v_type = VAR_NUMBER;
3865 tv->vval.v_number = iptr->isn_arg.number;
3866 break;
3867 case ISN_PUSHBOOL:
3868 tv->v_type = VAR_BOOL;
3869 tv->vval.v_number = iptr->isn_arg.number;
3870 break;
3871 case ISN_PUSHSPEC:
3872 tv->v_type = VAR_SPECIAL;
3873 tv->vval.v_number = iptr->isn_arg.number;
3874 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 case ISN_PUSHF:
3876 tv->v_type = VAR_FLOAT;
3877 tv->vval.v_float = iptr->isn_arg.fnumber;
3878 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 case ISN_PUSHBLOB:
3880 blob_copy(iptr->isn_arg.blob, tv);
3881 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003882 case ISN_PUSHFUNC:
3883 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003884 if (iptr->isn_arg.string == NULL)
3885 tv->vval.v_string = NULL;
3886 else
3887 tv->vval.v_string =
3888 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003889 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003890 case ISN_PUSHCHANNEL:
3891#ifdef FEAT_JOB_CHANNEL
3892 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003893 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003894#endif
3895 break;
3896 case ISN_PUSHJOB:
3897#ifdef FEAT_JOB_CHANNEL
3898 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003899 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003900#endif
3901 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 default:
3903 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003904 tv->vval.v_string = iptr->isn_arg.string == NULL
3905 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 }
3907 break;
3908
Bram Moolenaar06b77222022-01-25 15:51:56 +00003909 case ISN_AUTOLOAD:
3910 {
3911 char_u *name = iptr->isn_arg.string;
3912
3913 (void)script_autoload(name, FALSE);
3914 if (find_func(name, TRUE))
3915 {
3916 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3917 goto theend;
3918 tv = STACK_TV_BOT(0);
3919 tv->v_lock = 0;
3920 ++ectx->ec_stack.ga_len;
3921 tv->v_type = VAR_FUNC;
3922 tv->vval.v_string = vim_strsave(name);
3923 }
3924 else
3925 {
3926 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3927
3928 if (res == NOTDONE)
3929 goto theend;
3930 if (res == FAIL)
3931 goto on_error;
3932 }
3933 }
3934 break;
3935
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003936 case ISN_UNLET:
3937 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3938 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003939 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003940 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003941 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003942 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003943 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003944
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003945 case ISN_LOCKUNLOCK:
3946 {
3947 typval_T *lval_root_save = lval_root;
3948 int res;
3949
3950 // Stack has the local variable, argument the whole :lock
3951 // or :unlock command, like ISN_EXEC.
3952 --ectx->ec_stack.ga_len;
3953 lval_root = STACK_TV_BOT(0);
3954 res = exec_command(iptr);
3955 clear_tv(lval_root);
3956 lval_root = lval_root_save;
3957 if (res == FAIL)
3958 goto on_error;
3959 }
3960 break;
3961
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003962 case ISN_LOCKCONST:
3963 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3964 break;
3965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 // create a list from items on the stack; uses a single allocation
3967 // for the list header and the items
3968 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003969 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003970 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 break;
3972
3973 // create a dict from items on the stack
3974 case ISN_NEWDICT:
3975 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003976 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003978 SOURCING_LNUM = iptr->isn_lnum;
3979 res = exe_newdict(iptr->isn_arg.number, ectx);
3980 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003981 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003982 if (res == MAYBE)
3983 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 }
3985 break;
3986
LemonBoy372bcce2022-04-25 12:43:20 +01003987 case ISN_CONCAT:
3988 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3989 goto theend;
3990 break;
3991
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003992 // create a partial with NULL value
3993 case ISN_NEWPARTIAL:
3994 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3995 goto theend;
3996 ++ectx->ec_stack.ga_len;
3997 tv = STACK_TV_BOT(-1);
3998 tv->v_type = VAR_PARTIAL;
3999 tv->v_lock = 0;
4000 tv->vval.v_partial = NULL;
4001 break;
4002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 // call a :def function
4004 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004005 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004006 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4007 NULL,
4008 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004009 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004010 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 break;
4012
4013 // call a builtin function
4014 case ISN_BCALL:
4015 SOURCING_LNUM = iptr->isn_lnum;
4016 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4017 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004018 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004019 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 break;
4021
4022 // call a funcref or partial
4023 case ISN_PCALL:
4024 {
4025 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4026 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004027 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028
4029 SOURCING_LNUM = iptr->isn_lnum;
4030 if (pfunc->cpf_top)
4031 {
4032 // funcref is above the arguments
4033 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4034 }
4035 else
4036 {
4037 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004038 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004039 partial_tv = *STACK_TV_BOT(0);
4040 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004042 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004043 if (tv == &partial_tv)
4044 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004046 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 }
4048 break;
4049
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004050 case ISN_PCALL_END:
4051 // PCALL finished, arguments have been consumed and replaced by
4052 // the return value. Now clear the funcref from the stack,
4053 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004054 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004055 clear_tv(STACK_TV_BOT(-1));
4056 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4057 break;
4058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 // call a user defined function or funcref/partial
4060 case ISN_UCALL:
4061 {
4062 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4063
4064 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004065 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004066 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004067 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 }
4069 break;
4070
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004071 // :defer func(arg)
4072 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004073 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004074 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4075 goto on_error;
4076 break;
4077
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004078 // return from a :def function call without a value
4079 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02004080 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004081 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004082 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004083 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004084 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004085 tv->vval.v_number = 0;
4086 tv->v_lock = 0;
4087 // FALLTHROUGH
4088
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004089 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 case ISN_RETURN:
4091 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004092 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004093 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004094
4095 if (trystack->ga_len > 0)
4096 trycmd = ((trycmd_T *)trystack->ga_data)
4097 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004098 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004099 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004101 // jump to ":finally" or ":endtry"
4102 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004103 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004104 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004105 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 trycmd->tcd_return = TRUE;
4107 }
4108 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004109 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 }
4111 break;
4112
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004113 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 case ISN_FUNCREF:
4115 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004116 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4117 ufunc_T *ufunc;
4118 funcref_T *funcref = &iptr->isn_arg.funcref;
4119 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004122 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004123 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004124 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004125 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004126 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004127 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004128 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004129 {
4130 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4131 + funcref->fr_dfunc_idx;
4132
4133 ufunc = pt_dfunc->df_ufunc;
4134 }
4135 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004136 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004137 if (ufunc == NULL)
4138 {
4139 SOURCING_LNUM = iptr->isn_lnum;
4140 iemsg("ufunc unexpectedly NULL for FUNCREF");
4141 goto theend;
4142 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004143 if (fill_partial_and_closure(pt, ufunc,
4144 extra == NULL ? 0 : extra->fre_loop_var_idx,
4145 extra == NULL ? 0 : extra->fre_loop_var_count,
4146 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004147 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004149 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 tv->vval.v_partial = pt;
4151 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004152 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 }
4154 break;
4155
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004156 // Create a global function from a lambda.
4157 case ISN_NEWFUNC:
4158 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004159 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004160
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004161 if (copy_lambda_to_global_func(arg->nfa_lambda,
4162 arg->nfa_global, arg->nfa_loop_var_idx,
4163 arg->nfa_loop_var_count, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004164 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004165 }
4166 break;
4167
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004168 // List functions
4169 case ISN_DEF:
4170 if (iptr->isn_arg.string == NULL)
4171 list_functions(NULL);
4172 else
4173 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004174 exarg_T ea;
4175 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004176
4177 CLEAR_FIELD(ea);
4178 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004179 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4180 define_function(&ea, NULL, &lines_to_free);
4181 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004182 }
4183 break;
4184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 // jump if a condition is met
4186 case ISN_JUMP:
4187 {
4188 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004189 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 int jump = TRUE;
4191
4192 if (when != JUMP_ALWAYS)
4193 {
4194 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004195 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004196 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004197 || when == JUMP_IF_COND_TRUE)
4198 {
4199 SOURCING_LNUM = iptr->isn_lnum;
4200 jump = tv_get_bool_chk(tv, &error);
4201 if (error)
4202 goto on_error;
4203 }
4204 else
4205 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004206 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004208 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 {
4210 // drop the value from the stack
4211 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004212 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 }
4214 }
4215 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004216 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 }
4218 break;
4219
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004220 // "while": jump to end if a condition is false
4221 case ISN_WHILE:
4222 {
4223 int error = FALSE;
4224 int jump = TRUE;
4225
4226 tv = STACK_TV_BOT(-1);
4227 SOURCING_LNUM = iptr->isn_lnum;
4228 jump = !tv_get_bool_chk(tv, &error);
4229 if (error)
4230 goto on_error;
4231 // drop the value from the stack
4232 clear_tv(tv);
4233 --ectx->ec_stack.ga_len;
4234 if (jump)
4235 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4236
4237 // Store the current funccal count, may be used by
4238 // ISN_LOOPEND later
4239 tv = STACK_TV_VAR(
4240 iptr->isn_arg.whileloop.while_funcref_idx);
4241 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4242 }
4243 break;
4244
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004245 // Jump if an argument with a default value was already set and not
4246 // v:none.
4247 case ISN_JUMP_IF_ARG_SET:
4248 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
4249 if (tv->v_type != VAR_UNKNOWN
4250 && !(tv->v_type == VAR_SPECIAL
4251 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004252 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004253 break;
4254
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 // top of a for loop
4256 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004257 if (execute_for(iptr, ectx) == FAIL)
4258 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 break;
4260
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004261 // end of a for or while loop
4262 case ISN_ENDLOOP:
4263 if (execute_endloop(iptr, ectx) == FAIL)
4264 goto theend;
4265 break;
4266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 // start of ":try" block
4268 case ISN_TRY:
4269 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004270 trycmd_T *trycmd = NULL;
4271
Bram Moolenaar35578162021-08-02 19:10:38 +02004272 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004273 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004274 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4275 + ectx->ec_trystack.ga_len;
4276 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004278 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004279 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4280 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004281 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004282 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004283 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004284 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004285 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004286 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 }
4288 break;
4289
4290 case ISN_PUSHEXC:
4291 if (current_exception == NULL)
4292 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004293 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004295 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004297 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004298 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004300 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004302 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 tv->vval.v_string = vim_strsave(
4304 (char_u *)current_exception->value);
4305 break;
4306
4307 case ISN_CATCH:
4308 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004309 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004310 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311
Bram Moolenaar4c137212021-04-19 16:48:48 +02004312 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004313 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004315 trycmd->tcd_caught = TRUE;
4316 trycmd->tcd_did_throw = FALSE;
4317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004319 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 catch_exception(current_exception);
4321 }
4322 break;
4323
Bram Moolenaarc150c092021-02-13 15:02:46 +01004324 case ISN_TRYCONT:
4325 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004326 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004327 trycont_T *trycont = &iptr->isn_arg.trycont;
4328 int i;
4329 trycmd_T *trycmd;
4330 int iidx = trycont->tct_where;
4331
4332 if (trystack->ga_len < trycont->tct_levels)
4333 {
4334 siemsg("TRYCONT: expected %d levels, found %d",
4335 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004336 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004337 }
4338 // Make :endtry jump to any outer try block and the last
4339 // :endtry inside the loop to the loop start.
4340 for (i = trycont->tct_levels; i > 0; --i)
4341 {
4342 trycmd = ((trycmd_T *)trystack->ga_data)
4343 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004344 // Add one to tcd_cont to be able to jump to
4345 // instruction with index zero.
4346 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004347 iidx = trycmd->tcd_finally_idx == 0
4348 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004349 }
4350 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004351 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004352 }
4353 break;
4354
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004355 case ISN_FINALLY:
4356 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004357 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004358 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4359 + trystack->ga_len - 1;
4360
4361 // Reset the index to avoid a return statement jumps here
4362 // again.
4363 trycmd->tcd_finally_idx = 0;
4364 break;
4365 }
4366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 // end of ":try" block
4368 case ISN_ENDTRY:
4369 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004370 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004371 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372
Bram Moolenaar06651632022-04-27 17:54:25 +01004373 --trystack->ga_len;
4374 --trylevel;
4375 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4376 if (trycmd->tcd_did_throw)
4377 did_throw = TRUE;
4378 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004380 // discard the exception
4381 if (caught_stack == current_exception)
4382 caught_stack = caught_stack->caught;
4383 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004385
4386 if (trycmd->tcd_return)
4387 goto func_return;
4388
4389 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4390 {
4391 --ectx->ec_stack.ga_len;
4392 clear_tv(STACK_TV_BOT(0));
4393 }
4394 if (trycmd->tcd_cont != 0)
4395 // handling :continue: jump to outer try block or
4396 // start of the loop
4397 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 }
4399 break;
4400
4401 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004402 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004403 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004404
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004405 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4406 {
4407 // throwing an exception while using "silent!" causes
4408 // the function to abort but not display an error.
4409 tv = STACK_TV_BOT(-1);
4410 clear_tv(tv);
4411 tv->v_type = VAR_NUMBER;
4412 tv->vval.v_number = 0;
4413 goto done;
4414 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004415 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004416 tv = STACK_TV_BOT(0);
4417 if (tv->vval.v_string == NULL
4418 || *skipwhite(tv->vval.v_string) == NUL)
4419 {
4420 vim_free(tv->vval.v_string);
4421 SOURCING_LNUM = iptr->isn_lnum;
4422 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004423 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004424 }
4425
4426 // Inside a "catch" we need to first discard the caught
4427 // exception.
4428 if (trystack->ga_len > 0)
4429 {
4430 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4431 + trystack->ga_len - 1;
4432 if (trycmd->tcd_caught && current_exception != NULL)
4433 {
4434 // discard the exception
4435 if (caught_stack == current_exception)
4436 caught_stack = caught_stack->caught;
4437 discard_current_exception();
4438 trycmd->tcd_caught = FALSE;
4439 }
4440 }
4441
Bram Moolenaar90a57162022-02-12 14:23:17 +00004442 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004443 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4444 == FAIL)
4445 {
4446 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004447 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004448 }
4449 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 break;
4452
4453 // compare with special values
4454 case ISN_COMPAREBOOL:
4455 case ISN_COMPARESPECIAL:
4456 {
4457 typval_T *tv1 = STACK_TV_BOT(-2);
4458 typval_T *tv2 = STACK_TV_BOT(-1);
4459 varnumber_T arg1 = tv1->vval.v_number;
4460 varnumber_T arg2 = tv2->vval.v_number;
4461 int res;
4462
Bram Moolenaar06651632022-04-27 17:54:25 +01004463 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4464 res = arg1 == arg2;
4465 else
4466 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467
Bram Moolenaar4c137212021-04-19 16:48:48 +02004468 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 tv1->v_type = VAR_BOOL;
4470 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4471 }
4472 break;
4473
Bram Moolenaar7a222242022-03-01 19:23:24 +00004474 case ISN_COMPARENULL:
4475 {
4476 typval_T *tv1 = STACK_TV_BOT(-2);
4477 typval_T *tv2 = STACK_TV_BOT(-1);
4478 int res;
4479
4480 res = typval_compare_null(tv1, tv2);
4481 if (res == MAYBE)
4482 goto on_error;
4483 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4484 res = !res;
4485 clear_tv(tv1);
4486 clear_tv(tv2);
4487 --ectx->ec_stack.ga_len;
4488 tv1->v_type = VAR_BOOL;
4489 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4490 }
4491 break;
4492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 // Operation with two number arguments
4494 case ISN_OPNR:
4495 case ISN_COMPARENR:
4496 {
4497 typval_T *tv1 = STACK_TV_BOT(-2);
4498 typval_T *tv2 = STACK_TV_BOT(-1);
4499 varnumber_T arg1 = tv1->vval.v_number;
4500 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004501 varnumber_T res = 0;
4502 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004504 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4505 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4506 {
4507 if (arg2 < 0)
4508 {
4509 SOURCING_LNUM = iptr->isn_lnum;
4510 emsg(_(e_bitshift_ops_must_be_postive));
4511 goto on_error;
4512 }
4513 }
4514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 switch (iptr->isn_arg.op.op_type)
4516 {
4517 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004518 case EXPR_DIV: if (arg2 == 0)
4519 div_zero = TRUE;
4520 else
4521 res = arg1 / arg2;
4522 break;
4523 case EXPR_REM: if (arg2 == 0)
4524 div_zero = TRUE;
4525 else
4526 res = arg1 % arg2;
4527 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 case EXPR_SUB: res = arg1 - arg2; break;
4529 case EXPR_ADD: res = arg1 + arg2; break;
4530
4531 case EXPR_EQUAL: res = arg1 == arg2; break;
4532 case EXPR_NEQUAL: res = arg1 != arg2; break;
4533 case EXPR_GREATER: res = arg1 > arg2; break;
4534 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4535 case EXPR_SMALLER: res = arg1 < arg2; break;
4536 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004537 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4538 res = 0;
4539 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004540 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004541 break;
4542 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4543 res = 0;
4544 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004545 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004546 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004547 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 }
4549
Bram Moolenaar4c137212021-04-19 16:48:48 +02004550 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 if (iptr->isn_type == ISN_COMPARENR)
4552 {
4553 tv1->v_type = VAR_BOOL;
4554 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4555 }
4556 else
4557 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004558 if (div_zero)
4559 {
4560 SOURCING_LNUM = iptr->isn_lnum;
4561 emsg(_(e_divide_by_zero));
4562 goto on_error;
4563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 }
4565 break;
4566
4567 // Computation with two float arguments
4568 case ISN_OPFLOAT:
4569 case ISN_COMPAREFLOAT:
4570 {
4571 typval_T *tv1 = STACK_TV_BOT(-2);
4572 typval_T *tv2 = STACK_TV_BOT(-1);
4573 float_T arg1 = tv1->vval.v_float;
4574 float_T arg2 = tv2->vval.v_float;
4575 float_T res = 0;
4576 int cmp = FALSE;
4577
4578 switch (iptr->isn_arg.op.op_type)
4579 {
4580 case EXPR_MULT: res = arg1 * arg2; break;
4581 case EXPR_DIV: res = arg1 / arg2; break;
4582 case EXPR_SUB: res = arg1 - arg2; break;
4583 case EXPR_ADD: res = arg1 + arg2; break;
4584
4585 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4586 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4587 case EXPR_GREATER: cmp = arg1 > arg2; break;
4588 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4589 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4590 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4591 default: cmp = 0; break;
4592 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004593 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 if (iptr->isn_type == ISN_COMPAREFLOAT)
4595 {
4596 tv1->v_type = VAR_BOOL;
4597 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4598 }
4599 else
4600 tv1->vval.v_float = res;
4601 }
4602 break;
4603
4604 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004605 case ISN_COMPAREDICT:
4606 case ISN_COMPAREFUNC:
4607 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 case ISN_COMPAREBLOB:
4609 {
4610 typval_T *tv1 = STACK_TV_BOT(-2);
4611 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004612 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4613 int ic = iptr->isn_arg.op.op_ic;
4614 int res = FALSE;
4615 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616
Bram Moolenaar265f8112021-12-19 12:33:05 +00004617 SOURCING_LNUM = iptr->isn_lnum;
4618 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004620 status = typval_compare_list(tv1, tv2,
4621 exprtype, ic, &res);
4622 }
4623 else if (iptr->isn_type == ISN_COMPAREDICT)
4624 {
4625 status = typval_compare_dict(tv1, tv2,
4626 exprtype, ic, &res);
4627 }
4628 else if (iptr->isn_type == ISN_COMPAREFUNC)
4629 {
4630 status = typval_compare_func(tv1, tv2,
4631 exprtype, ic, &res);
4632 }
4633 else if (iptr->isn_type == ISN_COMPARESTRING)
4634 {
4635 status = typval_compare_string(tv1, tv2,
4636 exprtype, ic, &res);
4637 }
4638 else
4639 {
4640 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004642 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 clear_tv(tv1);
4644 clear_tv(tv2);
4645 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004646 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4647 if (status == FAIL)
4648 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 }
4650 break;
4651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 case ISN_COMPAREANY:
4653 {
4654 typval_T *tv1 = STACK_TV_BOT(-2);
4655 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004656 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004658 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659
Bram Moolenaareb26f432020-09-14 16:50:05 +02004660 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004661 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004663 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004664 if (status == FAIL)
4665 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 }
4667 break;
4668
4669 case ISN_ADDLIST:
4670 case ISN_ADDBLOB:
4671 {
4672 typval_T *tv1 = STACK_TV_BOT(-2);
4673 typval_T *tv2 = STACK_TV_BOT(-1);
4674
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004675 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004677 {
4678 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4679 && tv1->vval.v_list != NULL)
4680 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4681 NULL);
4682 else
4683 eval_addlist(tv1, tv2);
4684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 else
4686 eval_addblob(tv1, tv2);
4687 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004688 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 }
4690 break;
4691
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004692 case ISN_LISTAPPEND:
4693 {
4694 typval_T *tv1 = STACK_TV_BOT(-2);
4695 typval_T *tv2 = STACK_TV_BOT(-1);
4696 list_T *l = tv1->vval.v_list;
4697
4698 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004699 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004700 if (l == NULL)
4701 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004702 emsg(_(e_cannot_add_to_null_list));
4703 goto on_error;
4704 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004705 if (value_check_lock(l->lv_lock, NULL, FALSE))
4706 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004707 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004708 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004709 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004710 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004711 }
4712 break;
4713
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004714 case ISN_BLOBAPPEND:
4715 {
4716 typval_T *tv1 = STACK_TV_BOT(-2);
4717 typval_T *tv2 = STACK_TV_BOT(-1);
4718 blob_T *b = tv1->vval.v_blob;
4719 int error = FALSE;
4720 varnumber_T n;
4721
4722 // add a number to a blob
4723 if (b == NULL)
4724 {
4725 SOURCING_LNUM = iptr->isn_lnum;
4726 emsg(_(e_cannot_add_to_null_blob));
4727 goto on_error;
4728 }
4729 n = tv_get_number_chk(tv2, &error);
4730 if (error)
4731 goto on_error;
4732 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004733 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004734 }
4735 break;
4736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 // Computation with two arguments of unknown type
4738 case ISN_OPANY:
4739 {
4740 typval_T *tv1 = STACK_TV_BOT(-2);
4741 typval_T *tv2 = STACK_TV_BOT(-1);
4742 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 int error = FALSE;
4745
4746 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4747 {
4748 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4749 {
4750 eval_addlist(tv1, tv2);
4751 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004752 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 break;
4754 }
4755 else if (tv1->v_type == VAR_BLOB
4756 && tv2->v_type == VAR_BLOB)
4757 {
4758 eval_addblob(tv1, tv2);
4759 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004760 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 break;
4762 }
4763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 if (tv1->v_type == VAR_FLOAT)
4765 {
4766 f1 = tv1->vval.v_float;
4767 n1 = 0;
4768 }
4769 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004771 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 n1 = tv_get_number_chk(tv1, &error);
4773 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004774 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 if (tv2->v_type == VAR_FLOAT)
4776 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 if (tv2->v_type == VAR_FLOAT)
4779 {
4780 f2 = tv2->vval.v_float;
4781 n2 = 0;
4782 }
4783 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 {
4785 n2 = tv_get_number_chk(tv2, &error);
4786 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004787 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 if (tv1->v_type == VAR_FLOAT)
4789 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 // if there is a float on either side the result is a float
4792 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4793 {
4794 switch (iptr->isn_arg.op.op_type)
4795 {
4796 case EXPR_MULT: f1 = f1 * f2; break;
4797 case EXPR_DIV: f1 = f1 / f2; break;
4798 case EXPR_SUB: f1 = f1 - f2; break;
4799 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004800 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004801 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004802 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803 }
4804 clear_tv(tv1);
4805 clear_tv(tv2);
4806 tv1->v_type = VAR_FLOAT;
4807 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004808 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 }
4810 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004812 int failed = FALSE;
4813
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 switch (iptr->isn_arg.op.op_type)
4815 {
4816 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004817 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4818 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004819 goto on_error;
4820 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 case EXPR_SUB: n1 = n1 - n2; break;
4822 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004823 default: n1 = num_modulus(n1, n2, &failed);
4824 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004825 goto on_error;
4826 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827 }
4828 clear_tv(tv1);
4829 clear_tv(tv2);
4830 tv1->v_type = VAR_NUMBER;
4831 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004832 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 }
4834 }
4835 break;
4836
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004837 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004838 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004839 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004840 int is_slice = iptr->isn_type == ISN_STRSLICE;
4841 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004842 char_u *res;
4843
4844 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004845 // string slice: string is at stack-3, first index at
4846 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004847 if (is_slice)
4848 {
4849 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004850 n1 = tv->vval.v_number;
4851 }
4852
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004853 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004854 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004855
Bram Moolenaar4c137212021-04-19 16:48:48 +02004856 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004857 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004858 if (is_slice)
4859 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004860 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004861 else
4862 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004863 // single character (including composing characters).
4864 // If the index is too big or negative the result is
4865 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004866 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004867 vim_free(tv->vval.v_string);
4868 tv->vval.v_string = res;
4869 }
4870 break;
4871
4872 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004873 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004874 case ISN_BLOBINDEX:
4875 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004877 int is_slice = iptr->isn_type == ISN_LISTSLICE
4878 || iptr->isn_type == ISN_BLOBSLICE;
4879 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4880 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004881 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004882 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883
4884 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004885 // list slice: list is at stack-3, indexes at stack-2 and
4886 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004887 // Same for blob.
4888 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889
4890 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004891 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004893
4894 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 {
Bram Moolenaared591872020-08-15 22:14:53 +02004896 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004897 n1 = tv->vval.v_number;
4898 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 }
Bram Moolenaared591872020-08-15 22:14:53 +02004900
Bram Moolenaar4c137212021-04-19 16:48:48 +02004901 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004902 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004903 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004904 if (is_blob)
4905 {
4906 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4907 n1, n2, FALSE, tv) == FAIL)
4908 goto on_error;
4909 }
4910 else
4911 {
4912 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4913 n1, n2, FALSE, tv, TRUE) == FAIL)
4914 goto on_error;
4915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 }
4917 break;
4918
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004919 case ISN_ANYINDEX:
4920 case ISN_ANYSLICE:
4921 {
4922 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4923 typval_T *var1, *var2;
4924 int res;
4925
4926 // index: composite is at stack-2, index at stack-1
4927 // slice: composite is at stack-3, indexes at stack-2 and
4928 // stack-1
4929 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004930 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004931 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4932 goto on_error;
4933 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4934 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004935 res = eval_index_inner(tv, is_slice, var1, var2,
4936 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004937 clear_tv(var1);
4938 if (is_slice)
4939 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004940 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004941 if (res == FAIL)
4942 goto on_error;
4943 }
4944 break;
4945
Bram Moolenaar9af78762020-06-16 11:34:42 +02004946 case ISN_SLICE:
4947 {
4948 list_T *list;
4949 int count = iptr->isn_arg.number;
4950
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004951 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004952 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004953 list = tv->vval.v_list;
4954
4955 // no error for short list, expect it to be checked earlier
4956 if (list != NULL && list->lv_len >= count)
4957 {
4958 list_T *newlist = list_slice(list,
4959 count, list->lv_len - 1);
4960
4961 if (newlist != NULL)
4962 {
4963 list_unref(list);
4964 tv->vval.v_list = newlist;
4965 ++newlist->lv_refcount;
4966 }
4967 }
4968 }
4969 break;
4970
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004971 case ISN_GETITEM:
4972 {
4973 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004974 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004975
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004976 // Get list item: list is at stack-1, push item.
4977 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004978 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4979 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004980
Bram Moolenaar35578162021-08-02 19:10:38 +02004981 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004982 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004983 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004984 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004985
4986 // Useful when used in unpack assignment. Reset at
4987 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004988 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004989 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004990 }
4991 break;
4992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 case ISN_MEMBER:
4994 {
4995 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004996 char_u *key;
4997 dictitem_T *di;
4998
4999 // dict member: dict is at stack-2, key at stack-1
5000 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005001 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005002 dict = tv->vval.v_dict;
5003
5004 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005005 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005006 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005007 if (key == NULL)
5008 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005009
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005010 if ((di = dict_find(dict, key, -1)) == NULL)
5011 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005012 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005013 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005014
5015 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005016 // stack contents makes sense and the dict stack is
5017 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005018 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005019 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005020 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005021 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005022 tv->v_type = VAR_NUMBER;
5023 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005024 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005025 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005026 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005027 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005028 // Put the dict used on the dict stack, it might be used by
5029 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005030 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005031 if (dict_stack_save(tv) == FAIL)
5032 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005033 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005034 }
5035 break;
5036
5037 // dict member with string key
5038 case ISN_STRINGMEMBER:
5039 {
5040 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041 dictitem_T *di;
5042
5043 tv = STACK_TV_BOT(-1);
5044 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5045 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005046 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005047 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005048 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 }
5050 dict = tv->vval.v_dict;
5051
5052 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5053 == NULL)
5054 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005055 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00005056 semsg(_(e_key_not_present_in_dictionary),
5057 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005058 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005060 // Put the dict used on the dict stack, it might be used by
5061 // a dict function later.
5062 if (dict_stack_save(tv) == FAIL)
5063 goto on_fatal_error;
5064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005066 }
5067 break;
5068
5069 case ISN_CLEARDICT:
5070 dict_stack_drop();
5071 break;
5072
5073 case ISN_USEDICT:
5074 {
5075 typval_T *dict_tv = dict_stack_get_tv();
5076
5077 // Turn "dict.Func" into a partial for "Func" bound to
5078 // "dict". Don't do this when "Func" is already a partial
5079 // that was bound explicitly (pt_auto is FALSE).
5080 tv = STACK_TV_BOT(-1);
5081 if (dict_tv != NULL
5082 && dict_tv->v_type == VAR_DICT
5083 && dict_tv->vval.v_dict != NULL
5084 && (tv->v_type == VAR_FUNC
5085 || (tv->v_type == VAR_PARTIAL
5086 && (tv->vval.v_partial->pt_auto
5087 || tv->vval.v_partial->pt_dict == NULL))))
5088 dict_tv->vval.v_dict =
5089 make_partial(dict_tv->vval.v_dict, tv);
5090 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 }
5092 break;
5093
5094 case ISN_NEGATENR:
5095 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005096 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005097 if (tv->v_type == VAR_FLOAT)
5098 tv->vval.v_float = -tv->vval.v_float;
5099 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005100 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 break;
5102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 case ISN_CHECKTYPE:
5104 {
5105 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005106 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005107 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005109 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005110 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005111 if (!ectx->ec_where.wt_variable)
5112 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005113 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005114 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005115 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005116 if (r == FAIL)
5117 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005118 if (!ectx->ec_where.wt_variable)
5119 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005120
5121 // number 0 is FALSE, number 1 is TRUE
5122 if (tv->v_type == VAR_NUMBER
5123 && ct->ct_type->tt_type == VAR_BOOL
5124 && (tv->vval.v_number == 0
5125 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005127 tv->v_type = VAR_BOOL;
5128 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005129 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 }
5131 }
5132 break;
5133
Bram Moolenaar9af78762020-06-16 11:34:42 +02005134 case ISN_CHECKLEN:
5135 {
5136 int min_len = iptr->isn_arg.checklen.cl_min_len;
5137 list_T *list = NULL;
5138
5139 tv = STACK_TV_BOT(-1);
5140 if (tv->v_type == VAR_LIST)
5141 list = tv->vval.v_list;
5142 if (list == NULL || list->lv_len < min_len
5143 || (list->lv_len > min_len
5144 && !iptr->isn_arg.checklen.cl_more_OK))
5145 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005146 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005147 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005148 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005149 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005150 }
5151 }
5152 break;
5153
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005154 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005155 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005156 break;
5157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005158 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005159 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005160 {
5161 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005162 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005164 if (iptr->isn_type == ISN_2BOOL)
5165 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005166 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005167 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005168 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005169 n = !n;
5170 }
5171 else
5172 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005173 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005174 SOURCING_LNUM = iptr->isn_lnum;
5175 n = tv_get_bool_chk(tv, &error);
5176 if (error)
5177 goto on_error;
5178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 clear_tv(tv);
5180 tv->v_type = VAR_BOOL;
5181 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5182 }
5183 break;
5184
5185 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005186 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005187 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005188 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5189 iptr->isn_type == ISN_2STRING_ANY,
5190 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005191 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 break;
5193
Bram Moolenaar08597872020-12-10 19:43:40 +01005194 case ISN_RANGE:
5195 {
5196 exarg_T ea;
5197 char *errormsg;
5198
Bram Moolenaarece0b872021-01-08 20:40:45 +01005199 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005200 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005201 ea.addr_type = ADDR_LINES;
5202 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005203 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005204 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005205 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005206
Bram Moolenaar35578162021-08-02 19:10:38 +02005207 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005208 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005209 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005210 tv = STACK_TV_BOT(-1);
5211 tv->v_type = VAR_NUMBER;
5212 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005213 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005214 }
5215 break;
5216
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005217 case ISN_PUT:
5218 {
5219 int regname = iptr->isn_arg.put.put_regname;
5220 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5221 char_u *expr = NULL;
5222 int dir = FORWARD;
5223
Bram Moolenaar08597872020-12-10 19:43:40 +01005224 if (lnum < -2)
5225 {
5226 // line number was put on the stack by ISN_RANGE
5227 tv = STACK_TV_BOT(-1);
5228 curwin->w_cursor.lnum = tv->vval.v_number;
5229 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5230 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005231 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005232 }
5233 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005234 // :put! above cursor
5235 dir = BACKWARD;
5236 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005237 {
5238 curwin->w_cursor.lnum = lnum;
5239 if (lnum == 0)
5240 // check_cursor() below will move to line 1
5241 dir = BACKWARD;
5242 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005243
5244 if (regname == '=')
5245 {
5246 tv = STACK_TV_BOT(-1);
5247 if (tv->v_type == VAR_STRING)
5248 expr = tv->vval.v_string;
5249 else
5250 {
5251 expr = typval2string(tv, TRUE); // allocates value
5252 clear_tv(tv);
5253 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005254 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005255 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005256 check_cursor();
5257 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5258 vim_free(expr);
5259 }
5260 break;
5261
Bram Moolenaar02194d22020-10-24 23:08:38 +02005262 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005263 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5264 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5265 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5266 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005267 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5268 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005269 break;
5270
Bram Moolenaar02194d22020-10-24 23:08:38 +02005271 case ISN_CMDMOD_REV:
5272 // filter regprog is owned by the instruction, don't free it
5273 cmdmod.cmod_filter_regmatch.regprog = NULL;
5274 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005275 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5276 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005277 break;
5278
Bram Moolenaar792f7862020-11-23 08:31:18 +01005279 case ISN_UNPACK:
5280 {
5281 int count = iptr->isn_arg.unpack.unp_count;
5282 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5283 list_T *l;
5284 listitem_T *li;
5285 int i;
5286
5287 // Check there is a valid list to unpack.
5288 tv = STACK_TV_BOT(-1);
5289 if (tv->v_type != VAR_LIST)
5290 {
5291 SOURCING_LNUM = iptr->isn_lnum;
5292 emsg(_(e_for_argument_must_be_sequence_of_lists));
5293 goto on_error;
5294 }
5295 l = tv->vval.v_list;
5296 if (l == NULL
5297 || l->lv_len < (semicolon ? count - 1 : count))
5298 {
5299 SOURCING_LNUM = iptr->isn_lnum;
5300 emsg(_(e_list_value_does_not_have_enough_items));
5301 goto on_error;
5302 }
5303 else if (!semicolon && l->lv_len > count)
5304 {
5305 SOURCING_LNUM = iptr->isn_lnum;
5306 emsg(_(e_list_value_has_more_items_than_targets));
5307 goto on_error;
5308 }
5309
5310 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005311 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005312 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005313 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005314
5315 // Variable after semicolon gets a list with the remaining
5316 // items.
5317 if (semicolon)
5318 {
5319 list_T *rem_list =
5320 list_alloc_with_items(l->lv_len - count + 1);
5321
5322 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005323 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005324 tv = STACK_TV_BOT(-count);
5325 tv->vval.v_list = rem_list;
5326 ++rem_list->lv_refcount;
5327 tv->v_lock = 0;
5328 li = l->lv_first;
5329 for (i = 0; i < count - 1; ++i)
5330 li = li->li_next;
5331 for (i = 0; li != NULL; ++i)
5332 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005333 typval_T tvcopy;
5334
5335 copy_tv(&li->li_tv, &tvcopy);
5336 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005337 li = li->li_next;
5338 }
5339 --count;
5340 }
5341
5342 // Produce the values in reverse order, first item last.
5343 li = l->lv_first;
5344 for (i = 0; i < count; ++i)
5345 {
5346 tv = STACK_TV_BOT(-i - 1);
5347 copy_tv(&li->li_tv, tv);
5348 li = li->li_next;
5349 }
5350
5351 list_unref(l);
5352 }
5353 break;
5354
Bram Moolenaarb2049902021-01-24 12:53:53 +01005355 case ISN_PROF_START:
5356 case ISN_PROF_END:
5357 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005358#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005359 funccall_T cookie;
5360 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005361 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005362 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005363
Bram Moolenaarca16c602022-09-06 18:57:08 +01005364 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005365 if (iptr->isn_type == ISN_PROF_START)
5366 {
5367 func_line_start(&cookie, iptr->isn_lnum);
5368 // if we get here the instruction is executed
5369 func_line_exec(&cookie);
5370 }
5371 else
5372 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005373#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005374 }
5375 break;
5376
Bram Moolenaare99d4222021-06-13 14:01:26 +02005377 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005378 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005379 break;
5380
Bram Moolenaar389df252020-07-09 21:20:47 +02005381 case ISN_SHUFFLE:
5382 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005383 typval_T tmp_tv;
5384 int item = iptr->isn_arg.shuffle.shfl_item;
5385 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005386
5387 tmp_tv = *STACK_TV_BOT(-item);
5388 for ( ; up > 0 && item > 1; --up)
5389 {
5390 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5391 --item;
5392 }
5393 *STACK_TV_BOT(-item) = tmp_tv;
5394 }
5395 break;
5396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005397 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005398 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005399 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005400 ectx->ec_where.wt_index = 0;
5401 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402 break;
5403 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005404 continue;
5405
Bram Moolenaard032f342020-07-18 18:13:02 +02005406func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005407 // Restore previous function. If the frame pointer is where we started
5408 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005409 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005410 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005411
Bram Moolenaar4c137212021-04-19 16:48:48 +02005412 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005413 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005414 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005415 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005416
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005417on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005418 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005419 // If "emsg_silent" is set then ignore the error, unless it was set
5420 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005421 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005422 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005423 {
5424 // If a sequence of instructions causes an error while ":silent!"
5425 // was used, restore the stack length and jump ahead to restoring
5426 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005427 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005428 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005429 while (ectx->ec_stack.ga_len
5430 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005431 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005432 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005433 clear_tv(STACK_TV_BOT(0));
5434 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005435 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5436 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005437 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005438 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005439 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005440on_fatal_error:
5441 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005442 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005443 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005444 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005445 }
5446
5447done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005448 ret = OK;
5449theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005450 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005451
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005452 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005453 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5454 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005455}
5456
5457/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005458 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005459 * "rettv".
5460 * Return OK or FAIL.
5461 */
5462 int
5463exe_typval_instr(typval_T *tv, typval_T *rettv)
5464{
5465 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5466 isn_T *save_instr = ectx->ec_instr;
5467 int save_iidx = ectx->ec_iidx;
5468 int res;
5469
LemonBoyf3b48952022-05-05 13:53:03 +01005470 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5471 // even when the compilation fails.
5472 rettv->v_type = VAR_UNKNOWN;
5473
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005474 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5475 res = exec_instructions(ectx);
5476 if (res == OK)
5477 {
5478 *rettv = *STACK_TV_BOT(-1);
5479 --ectx->ec_stack.ga_len;
5480 }
5481
5482 ectx->ec_instr = save_instr;
5483 ectx->ec_iidx = save_iidx;
5484
5485 return res;
5486}
5487
5488/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005489 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5490 * "substitute_instr".
5491 */
5492 char_u *
5493exe_substitute_instr(void)
5494{
5495 ectx_T *ectx = substitute_instr->subs_ectx;
5496 isn_T *save_instr = ectx->ec_instr;
5497 int save_iidx = ectx->ec_iidx;
5498 char_u *res;
5499
5500 ectx->ec_instr = substitute_instr->subs_instr;
5501 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005502 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005503 typval_T *tv = STACK_TV_BOT(-1);
5504
Bram Moolenaar27523602021-06-05 21:36:19 +02005505 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005506 --ectx->ec_stack.ga_len;
5507 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005508 }
5509 else
5510 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005511 substitute_instr->subs_status = FAIL;
5512 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005514
Bram Moolenaar4c137212021-04-19 16:48:48 +02005515 ectx->ec_instr = save_instr;
5516 ectx->ec_iidx = save_iidx;
5517
5518 return res;
5519}
5520
5521/*
5522 * Call a "def" function from old Vim script.
5523 * Return OK or FAIL.
5524 */
5525 int
5526call_def_function(
5527 ufunc_T *ufunc,
5528 int argc_arg, // nr of arguments
5529 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005530 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005531 partial_T *partial, // optional partial for context
Bram Moolenaar58779852022-09-06 18:31:14 +01005532 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005533 typval_T *rettv) // return value
5534{
5535 ectx_T ectx; // execution context
5536 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005537 int partial_argc = partial == NULL
5538 || (flags & DEF_USE_PT_ARGV) == 0
5539 ? 0 : partial->pt_argc;
5540 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005541 typval_T *tv;
5542 int idx;
5543 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005544 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005545 sctx_T save_current_sctx = current_sctx;
5546 int did_emsg_before = did_emsg_cumul + did_emsg;
5547 int save_suppress_errthrow = suppress_errthrow;
5548 msglist_T **saved_msg_list = NULL;
5549 msglist_T *private_msg_list = NULL;
5550 int save_emsg_silent_def = emsg_silent_def;
5551 int save_did_emsg_def = did_emsg_def;
5552 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005553 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005554
5555// Get pointer to item in the stack.
5556#undef STACK_TV
5557#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5558
5559// Get pointer to item at the bottom of the stack, -1 is the bottom.
5560#undef STACK_TV_BOT
5561#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5562
5563// Get pointer to a local variable on the stack. Negative for arguments.
5564#undef STACK_TV_VAR
5565#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5566
5567 if (ufunc->uf_def_status == UF_NOT_COMPILED
5568 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005569 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5570 && compile_def_function(ufunc, FALSE,
5571 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005572 {
5573 if (did_emsg_cumul + did_emsg == did_emsg_before)
5574 semsg(_(e_function_is_not_compiled_str),
5575 printable_func_name(ufunc));
5576 return FAIL;
5577 }
5578
5579 {
5580 // Check the function was really compiled.
5581 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5582 + ufunc->uf_dfunc_idx;
5583 if (INSTRUCTIONS(dfunc) == NULL)
5584 {
5585 iemsg("using call_def_function() on not compiled function");
5586 return FAIL;
5587 }
5588 }
5589
5590 // If depth of calling is getting too high, don't execute the function.
5591 orig_funcdepth = funcdepth_get();
5592 if (funcdepth_increment() == FAIL)
5593 return FAIL;
5594
5595 CLEAR_FIELD(ectx);
5596 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5597 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005598 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005599 {
5600 funcdepth_decrement();
5601 return FAIL;
5602 }
5603 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5604 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5605 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005606 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005607
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005608 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005609 if (idx > 0 && ufunc->uf_va_name == NULL)
5610 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005611 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005612 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005613 goto failed_early;
5614 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005615 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005616 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005617 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005618 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005619 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005620 goto failed_early;
5621 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005622
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005623 // Put values from the partial and arguments on the stack, but no more than
5624 // what the function expects. A lambda can be called with more arguments
5625 // than it uses.
5626 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005627 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5628 ++idx)
5629 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005630 int argv_idx = idx - partial_argc;
5631
5632 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005633 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005634 && tv->v_type == VAR_SPECIAL
5635 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005636 {
5637 // Use the default value.
5638 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5639 }
5640 else
5641 {
5642 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5643 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005644 ufunc->uf_arg_types[idx], tv,
5645 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005646 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005647 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005648 }
5649 ++ectx.ec_stack.ga_len;
5650 }
5651
5652 // Turn varargs into a list. Empty list if no args.
5653 if (ufunc->uf_va_name != NULL)
5654 {
5655 int vararg_count = argc - ufunc->uf_args.ga_len;
5656
5657 if (vararg_count < 0)
5658 vararg_count = 0;
5659 else
5660 argc -= vararg_count;
5661 if (exe_newlist(vararg_count, &ectx) == FAIL)
5662 goto failed_early;
5663
5664 // Check the type of the list items.
5665 tv = STACK_TV_BOT(-1);
5666 if (ufunc->uf_va_type != NULL
5667 && ufunc->uf_va_type != &t_list_any
5668 && ufunc->uf_va_type->tt_member != &t_any
5669 && tv->vval.v_list != NULL)
5670 {
5671 type_T *expected = ufunc->uf_va_type->tt_member;
5672 listitem_T *li = tv->vval.v_list->lv_first;
5673
5674 for (idx = 0; idx < vararg_count; ++idx)
5675 {
5676 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005677 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005678 goto failed_early;
5679 li = li->li_next;
5680 }
5681 }
5682
5683 if (defcount > 0)
5684 // Move varargs list to below missing default arguments.
5685 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5686 --ectx.ec_stack.ga_len;
5687 }
5688
5689 // Make space for omitted arguments, will store default value below.
5690 // Any varargs list goes after them.
5691 if (defcount > 0)
5692 for (idx = 0; idx < defcount; ++idx)
5693 {
5694 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5695 ++ectx.ec_stack.ga_len;
5696 }
5697 if (ufunc->uf_va_name != NULL)
5698 ++ectx.ec_stack.ga_len;
5699
5700 // Frame pointer points to just after arguments.
5701 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5702 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5703
5704 {
5705 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5706 + ufunc->uf_dfunc_idx;
5707 ufunc_T *base_ufunc = dfunc->df_ufunc;
5708
5709 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005710 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005711 if (partial != NULL || base_ufunc->uf_partial != NULL)
5712 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005713 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5714 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005715 goto failed_early;
5716 if (partial != NULL)
5717 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005718 outer_T *outer = get_pt_outer(partial);
5719
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005720 if (outer->out_stack == NULL && outer->out_loop_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005721 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005722 if (current_ectx != NULL)
5723 {
5724 if (current_ectx->ec_outer_ref != NULL
5725 && current_ectx->ec_outer_ref->or_outer != NULL)
5726 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005727 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005728 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005729 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005730 }
5731 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005732 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005733 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005734 ++partial->pt_refcount;
5735 ectx.ec_outer_ref->or_partial = partial;
5736 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005737 }
5738 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005739 {
5740 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5741 ++base_ufunc->uf_partial->pt_refcount;
5742 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5743 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005744 }
5745 }
5746
5747 // dummy frame entries
5748 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5749 {
5750 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5751 ++ectx.ec_stack.ga_len;
5752 }
5753
5754 {
5755 // Reserve space for local variables and any closure reference count.
5756 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5757 + ufunc->uf_dfunc_idx;
5758
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005759 // Initialize variables to zero. That avoids having to generate
5760 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005761 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005762 {
5763 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5764 STACK_TV_VAR(idx)->vval.v_number = 0;
5765 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005766 ectx.ec_stack.ga_len += dfunc->df_varcount;
5767 if (dfunc->df_has_closure)
5768 {
5769 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5770 STACK_TV_VAR(idx)->vval.v_number = 0;
5771 ++ectx.ec_stack.ga_len;
5772 }
5773
5774 ectx.ec_instr = INSTRUCTIONS(dfunc);
5775 }
5776
Bram Moolenaar58779852022-09-06 18:31:14 +01005777 // Store the execution context in funccal, used by invoke_all_defer().
5778 if (funccal != NULL)
5779 funccal->fc_ectx = &ectx;
5780
Bram Moolenaar4c137212021-04-19 16:48:48 +02005781 // Following errors are in the function, not the caller.
5782 // Commands behave like vim9script.
5783 estack_push_ufunc(ufunc, 1);
5784 current_sctx = ufunc->uf_script_ctx;
5785 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5786
5787 // Use a specific location for storing error messages to be converted to an
5788 // exception.
5789 saved_msg_list = msg_list;
5790 msg_list = &private_msg_list;
5791
5792 // Do turn errors into exceptions.
5793 suppress_errthrow = FALSE;
5794
5795 // Do not delete the function while executing it.
5796 ++ufunc->uf_calls;
5797
5798 // When ":silent!" was used before calling then we still abort the
5799 // function. If ":silent!" is used in the function then we don't.
5800 emsg_silent_def = emsg_silent;
5801 did_emsg_def = 0;
5802
5803 ectx.ec_where.wt_index = 0;
5804 ectx.ec_where.wt_variable = FALSE;
5805
5806 // Execute the instructions until done.
5807 ret = exec_instructions(&ectx);
5808 if (ret == OK)
5809 {
5810 // function finished, get result from the stack.
5811 if (ufunc->uf_ret_type == &t_void)
5812 {
5813 rettv->v_type = VAR_VOID;
5814 }
5815 else
5816 {
5817 tv = STACK_TV_BOT(-1);
5818 *rettv = *tv;
5819 tv->v_type = VAR_UNKNOWN;
5820 }
5821 }
5822
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005823 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005824 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005825
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005826 // Deal with any remaining closures, they may be in use somewhere.
5827 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005828 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005829 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005830 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005831 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005832
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005833 estack_pop();
5834 current_sctx = save_current_sctx;
5835
Bram Moolenaar2336c372021-12-06 15:06:54 +00005836 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5837 // Function was unreferenced while being used, free it now.
5838 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005839
Bram Moolenaar352134b2020-10-17 22:04:08 +02005840 if (*msg_list != NULL && saved_msg_list != NULL)
5841 {
5842 msglist_T **plist = saved_msg_list;
5843
5844 // Append entries from the current msg_list (uncaught exceptions) to
5845 // the saved msg_list.
5846 while (*plist != NULL)
5847 plist = &(*plist)->next;
5848
5849 *plist = *msg_list;
5850 }
5851 msg_list = saved_msg_list;
5852
Bram Moolenaar4c137212021-04-19 16:48:48 +02005853 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005854 {
5855 cmdmod.cmod_filter_regmatch.regprog = NULL;
5856 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005857 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005858 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005859 emsg_silent_def = save_emsg_silent_def;
5860 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005861
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005862failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005863 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005864 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5865 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005866 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005868 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005869 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005870 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005871 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005872 if (ectx.ec_outer_ref->or_outer_allocated)
5873 vim_free(ectx.ec_outer_ref->or_outer);
5874 partial_unref(ectx.ec_outer_ref->or_partial);
5875 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005876 }
5877
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005878 // Not sure if this is necessary.
5879 suppress_errthrow = save_suppress_errthrow;
5880
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005881 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5882 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005883 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005884 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005885 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886 return ret;
5887}
5888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005889/*
Bram Moolenaar58779852022-09-06 18:31:14 +01005890 * Called when a def function has finished (possibly failed).
5891 * Invoke all the function returns to clean up and invoke deferred functions,
5892 * except the toplevel one.
5893 */
5894 void
5895unwind_def_callstack(ectx_T *ectx)
5896{
5897 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
5898 func_return(ectx);
5899}
5900
5901/*
5902 * Invoke any deffered functions for the top function in "ectx".
5903 */
5904 void
5905may_invoke_defer_funcs(ectx_T *ectx)
5906{
5907 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
5908
5909 if (dfunc->df_defer_var_idx > 0)
5910 invoke_defer_funcs(ectx);
5911}
5912
5913/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005914 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5915 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5916 * "pfx" is prefixed to every line.
5917 */
5918 static void
5919list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5920{
5921 int line_idx = 0;
5922 int prev_current = 0;
5923 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005924 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005925
5926 for (current = 0; current < instr_count; ++current)
5927 {
5928 isn_T *iptr = &instr[current];
5929 char *line;
5930
5931 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005932 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005933 while (line_idx < iptr->isn_lnum
5934 && line_idx < ufunc->uf_lines.ga_len)
5935 {
5936 if (current > prev_current)
5937 {
5938 msg_puts("\n\n");
5939 prev_current = current;
5940 }
5941 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5942 if (line != NULL)
5943 msg(line);
5944 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005945 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5946 {
5947 int first_def_arg = ufunc->uf_args.ga_len
5948 - ufunc->uf_def_args.ga_len;
5949
5950 if (def_arg_idx > 0)
5951 msg_puts("\n\n");
5952 msg_start();
5953 msg_puts(" ");
5954 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5955 first_def_arg + def_arg_idx]);
5956 msg_puts(" = ");
5957 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5958 msg_clr_eos();
5959 msg_end();
5960 }
5961 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005962
5963 switch (iptr->isn_type)
5964 {
5965 case ISN_EXEC:
5966 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5967 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005968 case ISN_EXEC_SPLIT:
5969 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5970 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005971 case ISN_EXECRANGE:
5972 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5973 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005974 case ISN_LEGACY_EVAL:
5975 smsg("%s%4d EVAL legacy %s", pfx, current,
5976 iptr->isn_arg.string);
5977 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005978 case ISN_REDIRSTART:
5979 smsg("%s%4d REDIR", pfx, current);
5980 break;
5981 case ISN_REDIREND:
5982 smsg("%s%4d REDIR END%s", pfx, current,
5983 iptr->isn_arg.number ? " append" : "");
5984 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005985 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005986#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005987 smsg("%s%4d CEXPR pre %s", pfx, current,
5988 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005989#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005990 break;
5991 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005992#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005993 {
5994 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5995
5996 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5997 cexpr_get_auname(cer->cer_cmdidx),
5998 cer->cer_forceit ? "!" : "",
5999 cer->cer_cmdline);
6000 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006001#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006002 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006003 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006004 smsg("%s%4d INSTR", pfx, current);
6005 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6006 msg(" -------------");
6007 break;
6008 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006009 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006010 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6011
6012 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006013 }
6014 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006015 case ISN_SUBSTITUTE:
6016 {
6017 subs_T *subs = &iptr->isn_arg.subs;
6018
6019 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6020 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6021 msg(" -------------");
6022 }
6023 break;
6024 case ISN_EXECCONCAT:
6025 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6026 (varnumber_T)iptr->isn_arg.number);
6027 break;
6028 case ISN_ECHO:
6029 {
6030 echo_T *echo = &iptr->isn_arg.echo;
6031
6032 smsg("%s%4d %s %d", pfx, current,
6033 echo->echo_with_white ? "ECHO" : "ECHON",
6034 echo->echo_count);
6035 }
6036 break;
6037 case ISN_EXECUTE:
6038 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006039 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006040 break;
6041 case ISN_ECHOMSG:
6042 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006043 (varnumber_T)(iptr->isn_arg.number));
6044 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006045 case ISN_ECHOWINDOW:
6046 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
6047 (varnumber_T)(iptr->isn_arg.number));
6048 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006049 case ISN_ECHOCONSOLE:
6050 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6051 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006052 break;
6053 case ISN_ECHOERR:
6054 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006055 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006056 break;
6057 case ISN_LOAD:
6058 {
6059 if (iptr->isn_arg.number < 0)
6060 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6061 (varnumber_T)(iptr->isn_arg.number
6062 + STACK_FRAME_SIZE));
6063 else
6064 smsg("%s%4d LOAD $%lld", pfx, current,
6065 (varnumber_T)(iptr->isn_arg.number));
6066 }
6067 break;
6068 case ISN_LOADOUTER:
6069 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006070 isn_outer_T *outer = &iptr->isn_arg.outer;
6071
6072 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006073 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006074 outer->outer_depth,
6075 outer->outer_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +02006076 + STACK_FRAME_SIZE);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006077 else if (outer->outer_depth == OUTER_LOOP_DEPTH)
6078 smsg("%s%4d LOADOUTER level 1 $%d in loop",
6079 pfx, current, outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006080 else
6081 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006082 outer->outer_depth,
6083 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006084 }
6085 break;
6086 case ISN_LOADV:
6087 smsg("%s%4d LOADV v:%s", pfx, current,
6088 get_vim_var_name(iptr->isn_arg.number));
6089 break;
6090 case ISN_LOADSCRIPT:
6091 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006092 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6093 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6094 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006095
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006096 sv = get_script_svar(sref, -1);
6097 if (sv == NULL)
6098 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6099 pfx, current, si->sn_name);
6100 else
6101 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006102 sv->sv_name,
6103 sref->sref_idx,
6104 si->sn_name);
6105 }
6106 break;
6107 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006108 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006109 {
6110 scriptitem_T *si = SCRIPT_ITEM(
6111 iptr->isn_arg.loadstore.ls_sid);
6112
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006113 smsg("%s%4d %s s:%s from %s", pfx, current,
6114 iptr->isn_type == ISN_LOADS ? "LOADS"
6115 : "LOADEXPORT",
6116 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006117 }
6118 break;
6119 case ISN_LOADAUTO:
6120 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6121 break;
6122 case ISN_LOADG:
6123 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6124 break;
6125 case ISN_LOADB:
6126 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6127 break;
6128 case ISN_LOADW:
6129 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6130 break;
6131 case ISN_LOADT:
6132 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6133 break;
6134 case ISN_LOADGDICT:
6135 smsg("%s%4d LOAD g:", pfx, current);
6136 break;
6137 case ISN_LOADBDICT:
6138 smsg("%s%4d LOAD b:", pfx, current);
6139 break;
6140 case ISN_LOADWDICT:
6141 smsg("%s%4d LOAD w:", pfx, current);
6142 break;
6143 case ISN_LOADTDICT:
6144 smsg("%s%4d LOAD t:", pfx, current);
6145 break;
6146 case ISN_LOADOPT:
6147 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6148 break;
6149 case ISN_LOADENV:
6150 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6151 break;
6152 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006153 smsg("%s%4d LOADREG @%c", pfx, current,
6154 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006155 break;
6156
6157 case ISN_STORE:
6158 if (iptr->isn_arg.number < 0)
6159 smsg("%s%4d STORE arg[%lld]", pfx, current,
6160 iptr->isn_arg.number + STACK_FRAME_SIZE);
6161 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006162 smsg("%s%4d STORE $%lld", pfx, current,
6163 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006164 break;
6165 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006166 {
6167 isn_outer_T *outer = &iptr->isn_arg.outer;
6168
6169 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6170 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6171 pfx, current, outer->outer_idx);
6172 else
6173 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6174 outer->outer_depth, outer->outer_idx);
6175 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006176 break;
6177 case ISN_STOREV:
6178 smsg("%s%4d STOREV v:%s", pfx, current,
6179 get_vim_var_name(iptr->isn_arg.number));
6180 break;
6181 case ISN_STOREAUTO:
6182 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6183 break;
6184 case ISN_STOREG:
6185 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6186 break;
6187 case ISN_STOREB:
6188 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6189 break;
6190 case ISN_STOREW:
6191 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6192 break;
6193 case ISN_STORET:
6194 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6195 break;
6196 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006197 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006198 {
6199 scriptitem_T *si = SCRIPT_ITEM(
6200 iptr->isn_arg.loadstore.ls_sid);
6201
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006202 smsg("%s%4d %s %s in %s", pfx, current,
6203 iptr->isn_type == ISN_STORES
6204 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006205 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6206 }
6207 break;
6208 case ISN_STORESCRIPT:
6209 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006210 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6211 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6212 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006213
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006214 sv = get_script_svar(sref, -1);
6215 if (sv == NULL)
6216 smsg("%s%4d STORESCRIPT [deleted] in %s",
6217 pfx, current, si->sn_name);
6218 else
6219 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006220 sv->sv_name,
6221 sref->sref_idx,
6222 si->sn_name);
6223 }
6224 break;
6225 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006226 case ISN_STOREFUNCOPT:
6227 smsg("%s%4d %s &%s", pfx, current,
6228 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006229 iptr->isn_arg.storeopt.so_name);
6230 break;
6231 case ISN_STOREENV:
6232 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6233 break;
6234 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006235 smsg("%s%4d STOREREG @%c", pfx, current,
6236 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006237 break;
6238 case ISN_STORENR:
6239 smsg("%s%4d STORE %lld in $%d", pfx, current,
6240 iptr->isn_arg.storenr.stnr_val,
6241 iptr->isn_arg.storenr.stnr_idx);
6242 break;
6243
6244 case ISN_STOREINDEX:
6245 smsg("%s%4d STOREINDEX %s", pfx, current,
6246 vartype_name(iptr->isn_arg.vartype));
6247 break;
6248
6249 case ISN_STORERANGE:
6250 smsg("%s%4d STORERANGE", pfx, current);
6251 break;
6252
6253 // constants
6254 case ISN_PUSHNR:
6255 smsg("%s%4d PUSHNR %lld", pfx, current,
6256 (varnumber_T)(iptr->isn_arg.number));
6257 break;
6258 case ISN_PUSHBOOL:
6259 case ISN_PUSHSPEC:
6260 smsg("%s%4d PUSH %s", pfx, current,
6261 get_var_special_name(iptr->isn_arg.number));
6262 break;
6263 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006264 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006265 break;
6266 case ISN_PUSHS:
6267 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6268 break;
6269 case ISN_PUSHBLOB:
6270 {
6271 char_u *r;
6272 char_u numbuf[NUMBUFLEN];
6273 char_u *tofree;
6274
6275 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6276 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6277 vim_free(tofree);
6278 }
6279 break;
6280 case ISN_PUSHFUNC:
6281 {
6282 char *name = (char *)iptr->isn_arg.string;
6283
6284 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6285 name == NULL ? "[none]" : name);
6286 }
6287 break;
6288 case ISN_PUSHCHANNEL:
6289#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006290 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006291#endif
6292 break;
6293 case ISN_PUSHJOB:
6294#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006295 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006296#endif
6297 break;
6298 case ISN_PUSHEXC:
6299 smsg("%s%4d PUSH v:exception", pfx, current);
6300 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006301 case ISN_AUTOLOAD:
6302 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6303 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006304 case ISN_UNLET:
6305 smsg("%s%4d UNLET%s %s", pfx, current,
6306 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6307 iptr->isn_arg.unlet.ul_name);
6308 break;
6309 case ISN_UNLETENV:
6310 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6311 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6312 iptr->isn_arg.unlet.ul_name);
6313 break;
6314 case ISN_UNLETINDEX:
6315 smsg("%s%4d UNLETINDEX", pfx, current);
6316 break;
6317 case ISN_UNLETRANGE:
6318 smsg("%s%4d UNLETRANGE", pfx, current);
6319 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006320 case ISN_LOCKUNLOCK:
6321 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6322 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006323 case ISN_LOCKCONST:
6324 smsg("%s%4d LOCKCONST", pfx, current);
6325 break;
6326 case ISN_NEWLIST:
6327 smsg("%s%4d NEWLIST size %lld", pfx, current,
6328 (varnumber_T)(iptr->isn_arg.number));
6329 break;
6330 case ISN_NEWDICT:
6331 smsg("%s%4d NEWDICT size %lld", pfx, current,
6332 (varnumber_T)(iptr->isn_arg.number));
6333 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006334 case ISN_NEWPARTIAL:
6335 smsg("%s%4d NEWPARTIAL", pfx, current);
6336 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006337
6338 // function call
6339 case ISN_BCALL:
6340 {
6341 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6342
6343 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6344 internal_func_name(cbfunc->cbf_idx),
6345 cbfunc->cbf_argcount);
6346 }
6347 break;
6348 case ISN_DCALL:
6349 {
6350 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6351 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6352 + cdfunc->cdf_idx;
6353
6354 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006355 printable_func_name(df->df_ufunc),
6356 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006357 }
6358 break;
6359 case ISN_UCALL:
6360 {
6361 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6362
6363 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6364 cufunc->cuf_name, cufunc->cuf_argcount);
6365 }
6366 break;
6367 case ISN_PCALL:
6368 {
6369 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6370
6371 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6372 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6373 }
6374 break;
6375 case ISN_PCALL_END:
6376 smsg("%s%4d PCALL end", pfx, current);
6377 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006378 case ISN_DEFER:
6379 smsg("%s%4d DEFER %d args", pfx, current,
6380 (int)iptr->isn_arg.defer.defer_argcount);
6381 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006382 case ISN_RETURN:
6383 smsg("%s%4d RETURN", pfx, current);
6384 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006385 case ISN_RETURN_VOID:
6386 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006387 break;
6388 case ISN_FUNCREF:
6389 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006390 funcref_T *funcref = &iptr->isn_arg.funcref;
6391 funcref_extra_T *extra = funcref->fr_extra;
6392 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006393
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006394 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006395 {
6396 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6397 + funcref->fr_dfunc_idx;
6398 name = df->df_ufunc->uf_name;
6399 }
6400 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006401 name = extra->fre_func_name;
6402 if (extra == NULL || extra->fre_loop_var_count == 0)
6403 smsg("%s%4d FUNCREF %s", pfx, current, name);
6404 else
6405 smsg("%s%4d FUNCREF %s var $%d - $%d", pfx, current,
6406 name,
6407 extra->fre_loop_var_idx,
6408 extra->fre_loop_var_idx
6409 + extra->fre_loop_var_count - 1);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006410 }
6411 break;
6412
6413 case ISN_NEWFUNC:
6414 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006415 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006416
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006417 if (arg->nfa_loop_var_count == 0)
6418 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6419 arg->nfa_lambda, arg->nfa_global);
6420 else
6421 smsg("%s%4d NEWFUNC %s %s var $%d - $%d", pfx, current,
6422 arg->nfa_lambda, arg->nfa_global,
6423 arg->nfa_loop_var_idx,
6424 arg->nfa_loop_var_idx + arg->nfa_loop_var_count - 1);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006425 }
6426 break;
6427
6428 case ISN_DEF:
6429 {
6430 char_u *name = iptr->isn_arg.string;
6431
6432 smsg("%s%4d DEF %s", pfx, current,
6433 name == NULL ? (char_u *)"" : name);
6434 }
6435 break;
6436
6437 case ISN_JUMP:
6438 {
6439 char *when = "?";
6440
6441 switch (iptr->isn_arg.jump.jump_when)
6442 {
6443 case JUMP_ALWAYS:
6444 when = "JUMP";
6445 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006446 case JUMP_NEVER:
6447 iemsg("JUMP_NEVER should not be used");
6448 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006449 case JUMP_AND_KEEP_IF_TRUE:
6450 when = "JUMP_AND_KEEP_IF_TRUE";
6451 break;
6452 case JUMP_IF_FALSE:
6453 when = "JUMP_IF_FALSE";
6454 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006455 case JUMP_WHILE_FALSE:
6456 when = "JUMP_WHILE_FALSE"; // unused
6457 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006458 case JUMP_IF_COND_FALSE:
6459 when = "JUMP_IF_COND_FALSE";
6460 break;
6461 case JUMP_IF_COND_TRUE:
6462 when = "JUMP_IF_COND_TRUE";
6463 break;
6464 }
6465 smsg("%s%4d %s -> %d", pfx, current, when,
6466 iptr->isn_arg.jump.jump_where);
6467 }
6468 break;
6469
6470 case ISN_JUMP_IF_ARG_SET:
6471 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6472 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6473 iptr->isn_arg.jump.jump_where);
6474 break;
6475
6476 case ISN_FOR:
6477 {
6478 forloop_T *forloop = &iptr->isn_arg.forloop;
6479
6480 smsg("%s%4d FOR $%d -> %d", pfx, current,
6481 forloop->for_idx, forloop->for_end);
6482 }
6483 break;
6484
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006485 case ISN_ENDLOOP:
6486 {
6487 endloop_T *endloop = &iptr->isn_arg.endloop;
6488
6489 smsg("%s%4d ENDLOOP $%d save $%d - $%d", pfx, current,
6490 endloop->end_funcref_idx,
6491 endloop->end_var_idx,
6492 endloop->end_var_idx + endloop->end_var_count - 1);
6493 }
6494 break;
6495
6496 case ISN_WHILE:
6497 {
6498 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6499
6500 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6501 whileloop->while_funcref_idx,
6502 whileloop->while_end);
6503 }
6504 break;
6505
Bram Moolenaar4c137212021-04-19 16:48:48 +02006506 case ISN_TRY:
6507 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006508 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006509
6510 if (try->try_ref->try_finally == 0)
6511 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6512 pfx, current,
6513 try->try_ref->try_catch,
6514 try->try_ref->try_endtry);
6515 else
6516 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6517 pfx, current,
6518 try->try_ref->try_catch,
6519 try->try_ref->try_finally,
6520 try->try_ref->try_endtry);
6521 }
6522 break;
6523 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006524 smsg("%s%4d CATCH", pfx, current);
6525 break;
6526 case ISN_TRYCONT:
6527 {
6528 trycont_T *trycont = &iptr->isn_arg.trycont;
6529
6530 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6531 trycont->tct_levels,
6532 trycont->tct_levels == 1 ? "" : "s",
6533 trycont->tct_where);
6534 }
6535 break;
6536 case ISN_FINALLY:
6537 smsg("%s%4d FINALLY", pfx, current);
6538 break;
6539 case ISN_ENDTRY:
6540 smsg("%s%4d ENDTRY", pfx, current);
6541 break;
6542 case ISN_THROW:
6543 smsg("%s%4d THROW", pfx, current);
6544 break;
6545
6546 // expression operations on number
6547 case ISN_OPNR:
6548 case ISN_OPFLOAT:
6549 case ISN_OPANY:
6550 {
6551 char *what;
6552 char *ins;
6553
6554 switch (iptr->isn_arg.op.op_type)
6555 {
6556 case EXPR_MULT: what = "*"; break;
6557 case EXPR_DIV: what = "/"; break;
6558 case EXPR_REM: what = "%"; break;
6559 case EXPR_SUB: what = "-"; break;
6560 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006561 case EXPR_LSHIFT: what = "<<"; break;
6562 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006563 default: what = "???"; break;
6564 }
6565 switch (iptr->isn_type)
6566 {
6567 case ISN_OPNR: ins = "OPNR"; break;
6568 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6569 case ISN_OPANY: ins = "OPANY"; break;
6570 default: ins = "???"; break;
6571 }
6572 smsg("%s%4d %s %s", pfx, current, ins, what);
6573 }
6574 break;
6575
6576 case ISN_COMPAREBOOL:
6577 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006578 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006579 case ISN_COMPARENR:
6580 case ISN_COMPAREFLOAT:
6581 case ISN_COMPARESTRING:
6582 case ISN_COMPAREBLOB:
6583 case ISN_COMPARELIST:
6584 case ISN_COMPAREDICT:
6585 case ISN_COMPAREFUNC:
6586 case ISN_COMPAREANY:
6587 {
6588 char *p;
6589 char buf[10];
6590 char *type;
6591
6592 switch (iptr->isn_arg.op.op_type)
6593 {
6594 case EXPR_EQUAL: p = "=="; break;
6595 case EXPR_NEQUAL: p = "!="; break;
6596 case EXPR_GREATER: p = ">"; break;
6597 case EXPR_GEQUAL: p = ">="; break;
6598 case EXPR_SMALLER: p = "<"; break;
6599 case EXPR_SEQUAL: p = "<="; break;
6600 case EXPR_MATCH: p = "=~"; break;
6601 case EXPR_IS: p = "is"; break;
6602 case EXPR_ISNOT: p = "isnot"; break;
6603 case EXPR_NOMATCH: p = "!~"; break;
6604 default: p = "???"; break;
6605 }
6606 STRCPY(buf, p);
6607 if (iptr->isn_arg.op.op_ic == TRUE)
6608 strcat(buf, "?");
6609 switch(iptr->isn_type)
6610 {
6611 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6612 case ISN_COMPARESPECIAL:
6613 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006614 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006615 case ISN_COMPARENR: type = "COMPARENR"; break;
6616 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6617 case ISN_COMPARESTRING:
6618 type = "COMPARESTRING"; break;
6619 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6620 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6621 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6622 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6623 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6624 default: type = "???"; break;
6625 }
6626
6627 smsg("%s%4d %s %s", pfx, current, type, buf);
6628 }
6629 break;
6630
6631 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6632 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6633
6634 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006635 case ISN_CONCAT:
6636 smsg("%s%4d CONCAT size %lld", pfx, current,
6637 (varnumber_T)(iptr->isn_arg.number));
6638 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006639 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6640 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6641 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6642 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6643 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6644 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6645 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6646 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6647 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6648 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6649 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006650 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006651 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6652 iptr->isn_arg.getitem.gi_index,
6653 iptr->isn_arg.getitem.gi_with_op ?
6654 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006655 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6656 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6657 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006658 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6659 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6660
Bram Moolenaar4c137212021-04-19 16:48:48 +02006661 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6662
Bram Moolenaar4c137212021-04-19 16:48:48 +02006663 case ISN_CHECKTYPE:
6664 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006665 checktype_T *ct = &iptr->isn_arg.type;
6666 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006667
6668 if (ct->ct_arg_idx == 0)
6669 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6670 type_name(ct->ct_type, &tofree),
6671 (int)ct->ct_off);
6672 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006673 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006674 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006675 type_name(ct->ct_type, &tofree),
6676 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006677 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006678 (int)ct->ct_arg_idx);
6679 vim_free(tofree);
6680 break;
6681 }
6682 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6683 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6684 iptr->isn_arg.checklen.cl_min_len);
6685 break;
6686 case ISN_SETTYPE:
6687 {
6688 char *tofree;
6689
6690 smsg("%s%4d SETTYPE %s", pfx, current,
6691 type_name(iptr->isn_arg.type.ct_type, &tofree));
6692 vim_free(tofree);
6693 break;
6694 }
6695 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006696 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6697 smsg("%s%4d INVERT %d (!val)", pfx, current,
6698 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006699 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006700 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6701 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006702 break;
6703 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006704 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006705 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006706 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6707 pfx, current,
6708 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006709 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006710 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6711 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006712 break;
6713 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006714 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006715 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006716 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006717 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6718 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006719 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006720 else
6721 smsg("%s%4d PUT %c %ld", pfx, current,
6722 iptr->isn_arg.put.put_regname,
6723 (long)iptr->isn_arg.put.put_lnum);
6724 break;
6725
Bram Moolenaar4c137212021-04-19 16:48:48 +02006726 case ISN_CMDMOD:
6727 {
6728 char_u *buf;
6729 size_t len = produce_cmdmods(
6730 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6731
6732 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006733 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006734 {
6735 (void)produce_cmdmods(
6736 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6737 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6738 vim_free(buf);
6739 }
6740 break;
6741 }
6742 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6743
6744 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006745 smsg("%s%4d PROFILE START line %d", pfx, current,
6746 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006747 break;
6748
6749 case ISN_PROF_END:
6750 smsg("%s%4d PROFILE END", pfx, current);
6751 break;
6752
Bram Moolenaare99d4222021-06-13 14:01:26 +02006753 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006754 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6755 iptr->isn_arg.debug.dbg_break_lnum + 1,
6756 iptr->isn_lnum,
6757 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006758 break;
6759
Bram Moolenaar4c137212021-04-19 16:48:48 +02006760 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6761 iptr->isn_arg.unpack.unp_count,
6762 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6763 break;
6764 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6765 iptr->isn_arg.shuffle.shfl_item,
6766 iptr->isn_arg.shuffle.shfl_up);
6767 break;
6768 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6769
6770 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6771 return;
6772 }
6773
6774 out_flush(); // output one line at a time
6775 ui_breakcheck();
6776 if (got_int)
6777 break;
6778 }
6779}
6780
6781/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006782 * Handle command line completion for the :disassemble command.
6783 */
6784 void
6785set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6786{
6787 char_u *p;
6788
6789 // Default: expand user functions, "debug" and "profile"
6790 xp->xp_context = EXPAND_DISASSEMBLE;
6791 xp->xp_pattern = arg;
6792
6793 // first argument already typed: only user function names
6794 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6795 {
6796 xp->xp_context = EXPAND_USER_FUNC;
6797 xp->xp_pattern = skipwhite(p);
6798 }
6799}
6800
6801/*
6802 * Function given to ExpandGeneric() to obtain the list of :disassemble
6803 * arguments.
6804 */
6805 char_u *
6806get_disassemble_argument(expand_T *xp, int idx)
6807{
6808 if (idx == 0)
6809 return (char_u *)"debug";
6810 if (idx == 1)
6811 return (char_u *)"profile";
6812 return get_user_func_name(xp, idx - 2);
6813}
6814
6815/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006816 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006817 * We don't really need this at runtime, but we do have tests that require it,
6818 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819 */
6820 void
6821ex_disassemble(exarg_T *eap)
6822{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006823 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006824 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006826 isn_T *instr = NULL; // init to shut up gcc warning
6827 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006828 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006829
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006830 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006831 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006832 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006833 if (func_needs_compiling(ufunc, compile_type)
6834 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006835 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006836 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006837 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006838 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839 return;
6840 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006841 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006842
6843 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006844 switch (compile_type)
6845 {
6846 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006847#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006848 instr = dfunc->df_instr_prof;
6849 instr_count = dfunc->df_instr_prof_count;
6850 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006851#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006852 // FALLTHROUGH
6853 case CT_NONE:
6854 instr = dfunc->df_instr;
6855 instr_count = dfunc->df_instr_count;
6856 break;
6857 case CT_DEBUG:
6858 instr = dfunc->df_instr_debug;
6859 instr_count = dfunc->df_instr_debug_count;
6860 break;
6861 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006862
Bram Moolenaar4c137212021-04-19 16:48:48 +02006863 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006864}
6865
6866/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006867 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 * list, etc. Mostly like what JavaScript does, except that empty list and
6869 * empty dictionary are FALSE.
6870 */
6871 int
6872tv2bool(typval_T *tv)
6873{
6874 switch (tv->v_type)
6875 {
6876 case VAR_NUMBER:
6877 return tv->vval.v_number != 0;
6878 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 case VAR_PARTIAL:
6881 return tv->vval.v_partial != NULL;
6882 case VAR_FUNC:
6883 case VAR_STRING:
6884 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6885 case VAR_LIST:
6886 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6887 case VAR_DICT:
6888 return tv->vval.v_dict != NULL
6889 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6890 case VAR_BOOL:
6891 case VAR_SPECIAL:
6892 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6893 case VAR_JOB:
6894#ifdef FEAT_JOB_CHANNEL
6895 return tv->vval.v_job != NULL;
6896#else
6897 break;
6898#endif
6899 case VAR_CHANNEL:
6900#ifdef FEAT_JOB_CHANNEL
6901 return tv->vval.v_channel != NULL;
6902#else
6903 break;
6904#endif
6905 case VAR_BLOB:
6906 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6907 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006908 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006910 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006911 break;
6912 }
6913 return FALSE;
6914}
6915
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006916 void
6917emsg_using_string_as(typval_T *tv, int as_number)
6918{
6919 semsg(_(as_number ? e_using_string_as_number_str
6920 : e_using_string_as_bool_str),
6921 tv->vval.v_string == NULL
6922 ? (char_u *)"" : tv->vval.v_string);
6923}
6924
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006925/*
6926 * If "tv" is a string give an error and return FAIL.
6927 */
6928 int
6929check_not_string(typval_T *tv)
6930{
6931 if (tv->v_type == VAR_STRING)
6932 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006933 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 clear_tv(tv);
6935 return FAIL;
6936 }
6937 return OK;
6938}
6939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940
6941#endif // FEAT_EVAL