blob: 3e0a37773880976b27e4a619721aca6fbfd62e63 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010067// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100104// Get pointer to item in the stack.
105#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200108#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100110// Get pointer to a local variable on the stack. Negative for arguments.
111#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
112
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200113 void
114to_string_error(vartype_T vartype)
115{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200116 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200117}
118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100120 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 */
122 static int
123ufunc_argcount(ufunc_T *ufunc)
124{
125 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
126}
127
128/*
LemonBoy372bcce2022-04-25 12:43:20 +0100129 * Create a new string from "count" items at the bottom of the stack.
130 * A trailing NUL is appended.
131 * When "count" is zero an empty string is added to the stack.
132 */
133 static int
134exe_concat(int count, ectx_T *ectx)
135{
136 int idx;
137 int len = 0;
138 typval_T *tv;
139 garray_T ga;
140
141 ga_init2(&ga, sizeof(char), 1);
142 // Preallocate enough space for the whole string to avoid having to grow
143 // and copy.
144 for (idx = 0; idx < count; ++idx)
145 {
146 tv = STACK_TV_BOT(idx - count);
147 if (tv->vval.v_string != NULL)
148 len += (int)STRLEN(tv->vval.v_string);
149 }
150
151 if (ga_grow(&ga, len + 1) == FAIL)
152 return FAIL;
153
154 for (idx = 0; idx < count; ++idx)
155 {
156 tv = STACK_TV_BOT(idx - count);
157 ga_concat(&ga, tv->vval.v_string);
158 clear_tv(tv);
159 }
160
161 // add a terminating NUL
162 ga_append(&ga, NUL);
163
164 ectx->ec_stack.ga_len -= count - 1;
165 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
166
167 return OK;
168}
169
170/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200171 * Create a new list from "count" items at the bottom of the stack.
172 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100173 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200174 */
175 static int
176exe_newlist(int count, ectx_T *ectx)
177{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 int idx;
180 typval_T *tv;
181
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100182 if (count >= 0)
183 {
184 list = list_alloc_with_items(count);
185 if (list == NULL)
186 return FAIL;
187 for (idx = 0; idx < count; ++idx)
188 list_set_item(list, idx, STACK_TV_BOT(idx - count));
189 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190
191 if (count > 0)
192 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200193 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100194 {
195 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 else
199 ++ectx->ec_stack.ga_len;
200 tv = STACK_TV_BOT(-1);
201 tv->v_type = VAR_LIST;
202 tv->vval.v_list = list;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100203 if (list != NULL)
204 ++list->lv_refcount;
205 return OK;
206}
207
208/*
209 * Implementation of ISN_NEWDICT.
210 * Returns FAIL on total failure, MAYBE on error.
211 */
212 static int
213exe_newdict(int count, ectx_T *ectx)
214{
215 dict_T *dict = NULL;
216 dictitem_T *item;
217 char_u *key;
218 int idx;
219 typval_T *tv;
220
221 if (count >= 0)
222 {
223 dict = dict_alloc();
224 if (unlikely(dict == NULL))
225 return FAIL;
226 for (idx = 0; idx < count; ++idx)
227 {
228 // have already checked key type is VAR_STRING
229 tv = STACK_TV_BOT(2 * (idx - count));
230 // check key is unique
231 key = tv->vval.v_string == NULL
232 ? (char_u *)"" : tv->vval.v_string;
233 item = dict_find(dict, key, -1);
234 if (item != NULL)
235 {
236 semsg(_(e_duplicate_key_in_dicitonary), key);
237 dict_unref(dict);
238 return MAYBE;
239 }
240 item = dictitem_alloc(key);
241 clear_tv(tv);
242 if (unlikely(item == NULL))
243 {
244 dict_unref(dict);
245 return FAIL;
246 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100247 tv = STACK_TV_BOT(2 * (idx - count) + 1);
248 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100249 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100250 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100251 if (dict_add(dict, item) == FAIL)
252 {
253 // can this ever happen?
254 dict_unref(dict);
255 return FAIL;
256 }
257 }
258 }
259
260 if (count > 0)
261 ectx->ec_stack.ga_len -= 2 * count - 1;
262 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
263 return FAIL;
264 else
265 ++ectx->ec_stack.ga_len;
266 tv = STACK_TV_BOT(-1);
267 tv->v_type = VAR_DICT;
268 tv->v_lock = 0;
269 tv->vval.v_dict = dict;
270 if (dict != NULL)
271 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200272 return OK;
273}
274
275/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200276 * If debug_tick changed check if "ufunc" has a breakpoint and update
277 * "uf_has_breakpoint".
278 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000279 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200280update_has_breakpoint(ufunc_T *ufunc)
281{
282 if (ufunc->uf_debug_tick != debug_tick)
283 {
284 linenr_T breakpoint;
285
286 ufunc->uf_debug_tick = debug_tick;
287 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
288 ufunc->uf_has_breakpoint = breakpoint > 0;
289 }
290}
291
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200292static garray_T dict_stack = GA_EMPTY;
293
294/*
295 * Put a value on the dict stack. This consumes "tv".
296 */
297 static int
298dict_stack_save(typval_T *tv)
299{
300 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000301 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200302 if (ga_grow(&dict_stack, 1) == FAIL)
303 return FAIL;
304 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
305 ++dict_stack.ga_len;
306 return OK;
307}
308
309/*
310 * Get the typval at top of the dict stack.
311 */
312 static typval_T *
313dict_stack_get_tv(void)
314{
315 if (dict_stack.ga_len == 0)
316 return NULL;
317 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
318}
319
320/*
321 * Get the dict at top of the dict stack.
322 */
323 static dict_T *
324dict_stack_get_dict(void)
325{
326 typval_T *tv;
327
328 if (dict_stack.ga_len == 0)
329 return NULL;
330 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
331 if (tv->v_type == VAR_DICT)
332 return tv->vval.v_dict;
333 return NULL;
334}
335
336/*
337 * Drop an item from the dict stack.
338 */
339 static void
340dict_stack_drop(void)
341{
342 if (dict_stack.ga_len == 0)
343 {
344 iemsg("Dict stack underflow");
345 return;
346 }
347 --dict_stack.ga_len;
348 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
349}
350
351/*
352 * Drop items from the dict stack until the length is equal to "len".
353 */
354 static void
355dict_stack_clear(int len)
356{
357 while (dict_stack.ga_len > len)
358 dict_stack_drop();
359}
360
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200361/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000362 * Get a pointer to useful "pt_outer" of "pt".
363 */
364 static outer_T *
365get_pt_outer(partial_T *pt)
366{
367 partial_T *ptref = pt->pt_outer_partial;
368
369 if (ptref == NULL)
370 return &pt->pt_outer;
371
372 // partial using partial (recursively)
373 while (ptref->pt_outer_partial != NULL)
374 ptref = ptref->pt_outer_partial;
375 return &ptref->pt_outer;
376}
377
378/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100380 * This adds a stack frame and sets the instruction pointer to the start of the
381 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200382 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 *
384 * Stack has:
385 * - current arguments (already there)
386 * - omitted optional argument (default values) added here
387 * - stack frame:
388 * - pointer to calling function
389 * - Index of next instruction in calling function
390 * - previous frame pointer
391 * - reserved space for local variables
392 */
393 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394call_dfunc(
395 int cdf_idx,
396 partial_T *pt,
397 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100400 int argcount = argcount_arg;
401 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
402 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200403 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100404 int arg_to_add;
405 int vararg_count = 0;
406 int varcount;
407 int idx;
408 estack_T *entry;
409 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200410 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000411 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412
413 if (dfunc->df_deleted)
414 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100415 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000416 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100417 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 return FAIL;
419 }
420
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100421#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100422 if (do_profiling == PROF_YES)
423 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200424 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100425 {
426 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
427 + profile_info_ga.ga_len;
428 ++profile_info_ga.ga_len;
429 CLEAR_POINTER(info);
430 profile_may_start_func(info, ufunc,
431 (((dfunc_T *)def_functions.ga_data)
432 + ectx->ec_dfunc_idx)->df_ufunc);
433 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100434 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100435#endif
436
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200437 // When debugging and using "cont" switches to the not-debugged
438 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000439 compile_type = get_compile_type(ufunc);
440 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200441 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000442 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200443
444 // compile_def_function() may cause def_functions.ga_data to change
445 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
446 }
447 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200448 {
449 if (did_emsg_cumul + did_emsg == did_emsg_before)
450 semsg(_(e_function_is_not_compiled_str),
451 printable_func_name(ufunc));
452 return FAIL;
453 }
454
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200455 if (ufunc->uf_va_name != NULL)
456 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200457 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200458 // Stack at time of call with 2 varargs:
459 // normal_arg
460 // optional_arg
461 // vararg_1
462 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200463 // After creating the list:
464 // normal_arg
465 // optional_arg
466 // vararg-list
467 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200468 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200469 // After creating the list
470 // normal_arg
471 // (space for optional_arg)
472 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200473 vararg_count = argcount - ufunc->uf_args.ga_len;
474 if (vararg_count < 0)
475 vararg_count = 0;
476 else
477 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200478 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200479 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200480
481 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200482 }
483
Bram Moolenaarfe270812020-04-11 22:31:27 +0200484 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200485 if (arg_to_add < 0)
486 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100487 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
488 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200489 return FAIL;
490 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000491 else if (arg_to_add > ufunc->uf_def_args.ga_len)
492 {
493 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
494
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100495 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
496 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000497 return FAIL;
498 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200499
500 // Reserve space for:
501 // - missing arguments
502 // - stack frame
503 // - local variables
504 // - if needed: a counter for number of closures created in
505 // ectx->ec_funcrefs.
506 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200507 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 return FAIL;
509
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100510 // If depth of calling is getting too high, don't execute the function.
511 if (funcdepth_increment() == FAIL)
512 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200513 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100514
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100515 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200516 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100517 {
518 floc = ALLOC_ONE(funclocal_T);
519 if (floc == NULL)
520 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200521 *floc = ectx->ec_funclocal;
522 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100523 }
524
Bram Moolenaarfe270812020-04-11 22:31:27 +0200525 // Move the vararg-list to below the missing optional arguments.
526 if (vararg_count > 0 && arg_to_add > 0)
527 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100528
529 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200530 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200531 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200532 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533
534 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100535 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
536 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200537 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200538 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
539 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100540 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100541 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200542 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543
544 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200545 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000546 {
547 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
548
549 tv->v_type = VAR_NUMBER;
550 tv->vval.v_number = 0;
551 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200552 if (dfunc->df_has_closure)
553 {
554 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
555
556 tv->v_type = VAR_NUMBER;
557 tv->vval.v_number = 0;
558 }
559 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100561 if (pt != NULL || ufunc->uf_partial != NULL
562 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100563 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200564 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100565
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200566 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100567 return FAIL;
568 if (pt != NULL)
569 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000570 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200571 ++pt->pt_refcount;
572 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100573 }
574 else if (ufunc->uf_partial != NULL)
575 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000576 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200577 ++ufunc->uf_partial->pt_refcount;
578 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100579 }
580 else
581 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200582 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200583 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200584 {
585 vim_free(ref);
586 return FAIL;
587 }
588 ref->or_outer_allocated = TRUE;
589 ref->or_outer->out_stack = &ectx->ec_stack;
590 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
591 if (ectx->ec_outer_ref != NULL)
592 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100593 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200594 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100595 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100596 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200597 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100598
Bram Moolenaarc970e422021-03-17 15:03:04 +0100599 ++ufunc->uf_calls;
600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 // Set execution state to the start of the called function.
602 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100603 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100604 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200605 if (entry != NULL)
606 {
607 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200608 // Save the current context so it can be restored on return.
609 entry->es_save_sctx = current_sctx;
610 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200611 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100612
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200613 // Start execution at the first instruction.
614 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615
616 return OK;
617}
618
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000619// Double linked list of funcstack_T in use.
620static funcstack_T *first_funcstack = NULL;
621
622 static void
623add_funcstack_to_list(funcstack_T *funcstack)
624{
625 // Link in list of funcstacks.
626 if (first_funcstack != NULL)
627 first_funcstack->fs_prev = funcstack;
628 funcstack->fs_next = first_funcstack;
629 funcstack->fs_prev = NULL;
630 first_funcstack = funcstack;
631}
632
633 static void
634remove_funcstack_from_list(funcstack_T *funcstack)
635{
636 if (funcstack->fs_prev == NULL)
637 first_funcstack = funcstack->fs_next;
638 else
639 funcstack->fs_prev->fs_next = funcstack->fs_next;
640 if (funcstack->fs_next != NULL)
641 funcstack->fs_next->fs_prev = funcstack->fs_prev;
642}
643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200645 * Used when returning from a function: Check if any closure is still
646 * referenced. If so then move the arguments and variables to a separate piece
647 * of stack to be used when the closure is called.
648 * When "free_arguments" is TRUE the arguments are to be freed.
649 * Returns FAIL when out of memory.
650 */
651 static int
652handle_closure_in_use(ectx_T *ectx, int free_arguments)
653{
654 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
655 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200656 int argcount;
657 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200658 int idx;
659 typval_T *tv;
660 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200661 garray_T *gap = &ectx->ec_funcrefs;
662 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200663
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200664 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200665 return OK; // function was freed
666 if (dfunc->df_has_closure == 0)
667 return OK; // no closures
668 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
669 closure_count = tv->vval.v_number;
670 if (closure_count == 0)
671 return OK; // no funcrefs created
672
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200673 argcount = ufunc_argcount(dfunc->df_ufunc);
674 top = ectx->ec_frame_idx - argcount;
675
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200676 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200677 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200678 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200679 partial_T *pt;
680 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200681
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200682 if (off < 0)
683 continue; // count is off or already done
684 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200685 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200686 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200687 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200688 int i;
689
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000690 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200691 // unreferenced on return.
692 for (i = 0; i < dfunc->df_varcount; ++i)
693 {
694 typval_T *stv = STACK_TV(ectx->ec_frame_idx
695 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200696 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200697 --refcount;
698 }
699 if (refcount > 1)
700 {
701 closure_in_use = TRUE;
702 break;
703 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200704 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200705 }
706
707 if (closure_in_use)
708 {
709 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
710 typval_T *stack;
711
712 // A closure is using the arguments and/or local variables.
713 // Move them to the called function.
714 if (funcstack == NULL)
715 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000716
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200717 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
718 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200719 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
720 funcstack->fs_ga.ga_data = stack;
721 if (stack == NULL)
722 {
723 vim_free(funcstack);
724 return FAIL;
725 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000726 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200727
728 // Move or copy the arguments.
729 for (idx = 0; idx < argcount; ++idx)
730 {
731 tv = STACK_TV(top + idx);
732 if (free_arguments)
733 {
734 *(stack + idx) = *tv;
735 tv->v_type = VAR_UNKNOWN;
736 }
737 else
738 copy_tv(tv, stack + idx);
739 }
740 // Move the local variables.
741 for (idx = 0; idx < dfunc->df_varcount; ++idx)
742 {
743 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200744
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200745 // A partial created for a local function, that is also used as a
746 // local variable, has a reference count for the variable, thus
747 // will never go down to zero. When all these refcounts are one
748 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000749 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200750 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
751 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200752 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200753
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200754 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200755 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
756 gap->ga_len - closure_count + i])
757 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200758 }
759
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200760 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200761 tv->v_type = VAR_UNKNOWN;
762 }
763
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200764 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200765 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200766 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
767 - closure_count + idx];
768 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200769 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200770 ++funcstack->fs_refcount;
771 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100772 pt->pt_outer.out_stack = &funcstack->fs_ga;
773 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200774 }
775 }
776 }
777
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200778 for (idx = 0; idx < closure_count; ++idx)
779 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
780 - closure_count + idx]);
781 gap->ga_len -= closure_count;
782 if (gap->ga_len == 0)
783 ga_clear(gap);
784
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200785 return OK;
786}
787
788/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200789 * Called when a partial is freed or its reference count goes down to one. The
790 * funcstack may be the only reference to the partials in the local variables.
791 * Go over all of them, the funcref and can be freed if all partials
792 * referencing the funcstack have a reference count of one.
793 */
794 void
795funcstack_check_refcount(funcstack_T *funcstack)
796{
797 int i;
798 garray_T *gap = &funcstack->fs_ga;
799 int done = 0;
800
801 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
802 return;
803 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
804 {
805 typval_T *tv = ((typval_T *)gap->ga_data) + i;
806
807 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
808 && tv->vval.v_partial->pt_funcstack == funcstack
809 && tv->vval.v_partial->pt_refcount == 1)
810 ++done;
811 }
812 if (done == funcstack->fs_min_refcount)
813 {
814 typval_T *stack = gap->ga_data;
815
816 // All partials referencing the funcstack have a reference count of
817 // one, thus the funcstack is no longer of use.
818 for (i = 0; i < gap->ga_len; ++i)
819 clear_tv(stack + i);
820 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000821 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200822 vim_free(funcstack);
823 }
824}
825
826/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000827 * For garbage collecting: set references in all variables referenced by
828 * all funcstacks.
829 */
830 int
831set_ref_in_funcstacks(int copyID)
832{
833 funcstack_T *funcstack;
834
835 for (funcstack = first_funcstack; funcstack != NULL;
836 funcstack = funcstack->fs_next)
837 {
838 typval_T *stack = funcstack->fs_ga.ga_data;
839 int i;
840
841 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
842 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
843 return TRUE; // abort
844 }
845 return FALSE;
846}
847
848/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100849 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
850 * The local variable that lists deferred functions is "var_idx".
851 * Returns OK or FAIL.
852 */
853 static int
854add_defer_func(int var_idx, int argcount, ectx_T *ectx)
855{
856 typval_T *defer_tv = STACK_TV_VAR(var_idx);
857 list_T *defer_l;
858 typval_T *func_tv;
859 list_T *l;
860 int i;
861 typval_T listval;
862
863 if (defer_tv->v_type != VAR_LIST)
864 {
865 // first one, allocate the list
866 if (rettv_list_alloc(defer_tv) == FAIL)
867 return FAIL;
868 }
869 defer_l = defer_tv->vval.v_list;
870
871 l = list_alloc_with_items(argcount + 1);
872 if (l == NULL)
873 return FAIL;
874 listval.v_type = VAR_LIST;
875 listval.vval.v_list = l;
876 listval.v_lock = 0;
877 if (list_insert_tv(defer_l, &listval,
878 defer_l == NULL ? NULL : defer_l->lv_first) == FAIL)
879 {
880 vim_free(l);
881 return FAIL;
882 }
883
884 func_tv = STACK_TV_BOT(-argcount - 1);
885 // TODO: check type is a funcref
886 list_set_item(l, 0, func_tv);
887
888 for (i = 1; i <= argcount; ++i)
889 list_set_item(l, i, STACK_TV_BOT(-argcount + i - 1));
890 ectx->ec_stack.ga_len -= argcount + 1;
891 return OK;
892}
893
894/*
895 * Invoked when returning from a function: Invoke any deferred calls.
896 */
897 static void
898invoke_defer_funcs(ectx_T *ectx)
899{
900 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
901 + ectx->ec_dfunc_idx;
902 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
903 listitem_T *li;
904
905 if (defer_tv->v_type != VAR_LIST)
906 return; // no function added
907 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
908 {
909 list_T *l = li->li_tv.vval.v_list;
910 typval_T rettv;
911 typval_T argvars[MAX_FUNC_ARGS];
912 int i;
913 listitem_T *arg_li = l->lv_first;
914 funcexe_T funcexe;
915
916 for (i = 0; i < l->lv_len - 1; ++i)
917 {
918 arg_li = arg_li->li_next;
919 argvars[i] = arg_li->li_tv;
920 }
921
922 CLEAR_FIELD(funcexe);
923 funcexe.fe_evaluate = TRUE;
924 rettv.v_type = VAR_UNKNOWN;
925 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
926 &rettv, l->lv_len - 1, argvars, &funcexe);
927 clear_tv(&rettv);
928 }
929}
930
931/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 * Return from the current function.
933 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200934 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200935func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100938 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200939 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
940 + ectx->ec_dfunc_idx;
941 int argcount = ufunc_argcount(dfunc->df_ufunc);
942 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200943 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100944 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
945 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200946 funclocal_T *floc;
947#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100948 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
949 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950
Bram Moolenaar12d26532021-02-19 19:13:21 +0100951 if (do_profiling == PROF_YES)
952 {
953 ufunc_T *caller = prev_dfunc->df_ufunc;
954
955 if (dfunc->df_ufunc->uf_profiling
956 || (caller != NULL && caller->uf_profiling))
957 {
958 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
959 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
960 --profile_info_ga.ga_len;
961 }
962 }
963#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000964
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100965 if (dfunc->df_defer_var_idx > 0)
966 invoke_defer_funcs(ectx);
967
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000968 // No check for uf_refcount being zero, cannot think of a way that would
969 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100970 --dfunc->df_ufunc->uf_calls;
971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200973 entry = estack_pop();
974 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200975 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200977 if (handle_closure_in_use(ectx, TRUE) == FAIL)
978 return FAIL;
979
980 // Clear the arguments.
981 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
982 clear_tv(STACK_TV(idx));
983
984 // Clear local variables and temp values, but not the return value.
985 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100986 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100988
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100989 // The return value should be on top of the stack. However, when aborting
990 // it may not be there and ec_frame_idx is the top of the stack.
991 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100992 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100993 ret_idx = 0;
994
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200995 if (ectx->ec_outer_ref != NULL)
996 {
997 if (ectx->ec_outer_ref->or_outer_allocated)
998 vim_free(ectx->ec_outer_ref->or_outer);
999 partial_unref(ectx->ec_outer_ref->or_partial);
1000 vim_free(ectx->ec_outer_ref);
1001 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001002
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001003 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001004 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001005 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1006 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001007 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1008 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001009 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001010 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001011 floc = (void *)STACK_TV(ectx->ec_frame_idx
1012 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001013 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001014 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1015 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001016
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001017 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001018 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001019 else
1020 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001021 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001022 vim_free(floc);
1023 }
1024
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001025 if (ret_idx > 0)
1026 {
1027 // Reset the stack to the position before the call, with a spot for the
1028 // return value, moved there from above the frame.
1029 ectx->ec_stack.ga_len = top + 1;
1030 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1031 }
1032 else
1033 // Reset the stack to the position before the call.
1034 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001035
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001036 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001037 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001038 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039}
1040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041/*
1042 * Prepare arguments and rettv for calling a builtin or user function.
1043 */
1044 static int
1045call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1046{
1047 int idx;
1048 typval_T *tv;
1049
1050 // Move arguments from bottom of the stack to argvars[] and add terminator.
1051 for (idx = 0; idx < argcount; ++idx)
1052 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1053 argvars[argcount].v_type = VAR_UNKNOWN;
1054
1055 // Result replaces the arguments on the stack.
1056 if (argcount > 0)
1057 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001058 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 return FAIL;
1060 else
1061 ++ectx->ec_stack.ga_len;
1062
1063 // Default return value is zero.
1064 tv = STACK_TV_BOT(-1);
1065 tv->v_type = VAR_NUMBER;
1066 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001067 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068
1069 return OK;
1070}
1071
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001072// Ugly global to avoid passing the execution context around through many
1073// layers.
1074static ectx_T *current_ectx = NULL;
1075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076/*
1077 * Call a builtin function by index.
1078 */
1079 static int
1080call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1081{
1082 typval_T argvars[MAX_FUNC_ARGS];
1083 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001084 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001085 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001086 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087
1088 if (call_prepare(argcount, argvars, ectx) == FAIL)
1089 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001090 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001092 // Call the builtin function. Set "current_ectx" so that when it
1093 // recursively invokes call_def_function() a closure context can be set.
1094 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001096 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001097 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098
1099 // Clear the arguments.
1100 for (idx = 0; idx < argcount; ++idx)
1101 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001102
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001103 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001104 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 return OK;
1106}
1107
1108/*
1109 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001110 * If the function is compiled this will add a stack frame and set the
1111 * instruction pointer at the start of the function.
1112 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001113 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001114 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 */
1116 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001117call_ufunc(
1118 ufunc_T *ufunc,
1119 partial_T *pt,
1120 int argcount,
1121 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001122 isn_T *iptr,
1123 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001124{
1125 typval_T argvars[MAX_FUNC_ARGS];
1126 funcexe_T funcexe;
1127 int error;
1128 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001129 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001130 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131
Bram Moolenaare99d4222021-06-13 14:01:26 +02001132 if (func_needs_compiling(ufunc, compile_type)
1133 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1134 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001135 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001136 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001137 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001138 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001139 if (error != FCERR_UNKNOWN)
1140 {
1141 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001142 semsg(_(e_too_many_arguments_for_function_str),
1143 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001144 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001145 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001146 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001147 return FAIL;
1148 }
1149
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001150 // The function has been compiled, can call it quickly. For a function
1151 // that was defined later: we can call it directly next time.
1152 if (iptr != NULL)
1153 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001154 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001155 iptr->isn_type = ISN_DCALL;
1156 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1157 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1158 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001159 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161
1162 if (call_prepare(argcount, argvars, ectx) == FAIL)
1163 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001164 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001165 funcexe.fe_evaluate = TRUE;
1166 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167
1168 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001170 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171
1172 // Clear the arguments.
1173 for (idx = 0; idx < argcount; ++idx)
1174 clear_tv(&argvars[idx]);
1175
1176 if (error != FCERR_NONE)
1177 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001178 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 return FAIL;
1180 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001181 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001182 // Error other than from calling the function itself.
1183 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184 return OK;
1185}
1186
1187/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001188 * If command modifiers were applied restore them.
1189 */
1190 static void
1191may_restore_cmdmod(funclocal_T *funclocal)
1192{
1193 if (funclocal->floc_restore_cmdmod)
1194 {
1195 cmdmod.cmod_filter_regmatch.regprog = NULL;
1196 undo_cmdmod(&cmdmod);
1197 cmdmod = funclocal->floc_save_cmdmod;
1198 funclocal->floc_restore_cmdmod = FALSE;
1199 }
1200}
1201
1202/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001203 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1204 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001205 */
1206 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001207vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001208{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001209 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001210}
1211
1212/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 * Execute a function by "name".
1214 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001215 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 * Returns FAIL if not found without an error message.
1217 */
1218 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001219call_by_name(
1220 char_u *name,
1221 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001222 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001223 isn_T *iptr,
1224 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225{
1226 ufunc_T *ufunc;
1227
1228 if (builtin_function(name, -1))
1229 {
1230 int func_idx = find_internal_func(name);
1231
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001232 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001234 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 return FAIL;
1236 return call_bfunc(func_idx, argcount, ectx);
1237 }
1238
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001239 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001240
1241 if (ufunc == NULL)
1242 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001243 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001244
1245 if (script_autoload(name, TRUE))
1246 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001247 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001248
1249 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001250 return FAIL; // bail out if loading the script caused an error
1251 }
1252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001254 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001255 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001256 {
1257 int i;
1258 typval_T *argv = STACK_TV_BOT(0) - argcount;
1259
1260 // The function can change at runtime, check that the argument
1261 // types are correct.
1262 for (i = 0; i < argcount; ++i)
1263 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001264 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001265
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001266 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001267 type = ufunc->uf_arg_types[i];
1268 else if (ufunc->uf_va_type != NULL)
1269 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001270 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001271 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001272 return FAIL;
1273 }
1274 }
1275
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001276 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001277 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278
1279 return FAIL;
1280}
1281
1282 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001283call_partial(
1284 typval_T *tv,
1285 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001286 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001288 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001289 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001291 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001292 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293
1294 if (tv->v_type == VAR_PARTIAL)
1295 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001296 partial_T *pt = tv->vval.v_partial;
1297 int i;
1298
1299 if (pt->pt_argc > 0)
1300 {
1301 // Make space for arguments from the partial, shift the "argcount"
1302 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001303 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001304 return FAIL;
1305 for (i = 1; i <= argcount; ++i)
1306 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1307 ectx->ec_stack.ga_len += pt->pt_argc;
1308 argcount += pt->pt_argc;
1309
1310 // copy the arguments from the partial onto the stack
1311 for (i = 0; i < pt->pt_argc; ++i)
1312 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1313 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001314 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315
1316 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001317 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 name = pt->pt_name;
1320 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001321 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001323 if (name != NULL)
1324 {
1325 char_u fname_buf[FLEN_FIXED + 1];
1326 char_u *tofree = NULL;
1327 int error = FCERR_NONE;
1328 char_u *fname;
1329
1330 // May need to translate <SNR>123_ to K_SNR.
1331 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1332 if (error != FCERR_NONE)
1333 res = FAIL;
1334 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001335 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001336 vim_free(tofree);
1337 }
1338
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001339 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 {
1341 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001342 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001343 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 return FAIL;
1345 }
1346 return OK;
1347}
1348
1349/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001350 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1351 * TRUE.
1352 */
1353 static int
1354error_if_locked(int lock, char *error)
1355{
1356 if (lock & (VAR_LOCKED | VAR_FIXED))
1357 {
1358 emsg(_(error));
1359 return TRUE;
1360 }
1361 return FALSE;
1362}
1363
1364/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001365 * Give an error if "tv" is not a number and return FAIL.
1366 */
1367 static int
1368check_for_number(typval_T *tv)
1369{
1370 if (tv->v_type != VAR_NUMBER)
1371 {
1372 semsg(_(e_expected_str_but_got_str),
1373 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1374 return FAIL;
1375 }
1376 return OK;
1377}
1378
1379/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001380 * Store "tv" in variable "name".
1381 * This is for s: and g: variables.
1382 */
1383 static void
1384store_var(char_u *name, typval_T *tv)
1385{
1386 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001387 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001388
Bram Moolenaard877a572021-04-01 19:42:48 +02001389 if (tv->v_lock)
1390 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001391 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001392 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001393 restore_funccal();
1394}
1395
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001396/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001397 * Convert "tv" to a string.
1398 * Return FAIL if not allowed.
1399 */
1400 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001401do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001402{
1403 if (tv->v_type != VAR_STRING)
1404 {
1405 char_u *str;
1406
1407 if (is_2string_any)
1408 {
1409 switch (tv->v_type)
1410 {
1411 case VAR_SPECIAL:
1412 case VAR_BOOL:
1413 case VAR_NUMBER:
1414 case VAR_FLOAT:
1415 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001416
1417 case VAR_LIST:
1418 if (tolerant)
1419 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001420 char_u *s, *e, *p;
1421 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001422
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001423 ga_init2(&ga, sizeof(char_u *), 1);
1424
1425 // Convert to NL separated items, then
1426 // escape the items and replace the NL with
1427 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001428 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001429 if (str == NULL)
1430 return FAIL;
1431 s = str;
1432 while ((e = vim_strchr(s, '\n')) != NULL)
1433 {
1434 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001435 p = vim_strsave_fnameescape(s,
1436 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001437 if (p != NULL)
1438 {
1439 ga_concat(&ga, p);
1440 ga_concat(&ga, (char_u *)" ");
1441 vim_free(p);
1442 }
1443 s = e + 1;
1444 }
1445 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001446 clear_tv(tv);
1447 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001448 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001449 return OK;
1450 }
1451 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001452 default: to_string_error(tv->v_type);
1453 return FAIL;
1454 }
1455 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001456 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001457 clear_tv(tv);
1458 tv->v_type = VAR_STRING;
1459 tv->vval.v_string = str;
1460 }
1461 return OK;
1462}
1463
1464/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001465 * When the value of "sv" is a null list of dict, allocate it.
1466 */
1467 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001468allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001469{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001470 typval_T *tv = sv->sv_tv;
1471
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001472 switch (tv->v_type)
1473 {
1474 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001475 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001476 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001477 break;
1478 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001479 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001480 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001481 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001482 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001483 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001484 (void)rettv_blob_alloc(tv);
1485 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001486 default:
1487 break;
1488 }
1489}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001490
Bram Moolenaare7525c52021-01-09 13:20:37 +01001491/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001492 * Return the character "str[index]" where "index" is the character index,
1493 * including composing characters.
1494 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001495 */
1496 char_u *
1497char_from_string(char_u *str, varnumber_T index)
1498{
1499 size_t nbyte = 0;
1500 varnumber_T nchar = index;
1501 size_t slen;
1502
1503 if (str == NULL)
1504 return NULL;
1505 slen = STRLEN(str);
1506
Bram Moolenaarff871402021-03-26 13:34:05 +01001507 // Do the same as for a list: a negative index counts from the end.
1508 // Optimization to check the first byte to be below 0x80 (and no composing
1509 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001510 if (index < 0)
1511 {
1512 int clen = 0;
1513
1514 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001515 {
1516 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1517 ++nbyte;
1518 else if (enc_utf8)
1519 nbyte += utfc_ptr2len(str + nbyte);
1520 else
1521 nbyte += mb_ptr2len(str + nbyte);
1522 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001523 nchar = clen + index;
1524 if (nchar < 0)
1525 // unlike list: index out of range results in empty string
1526 return NULL;
1527 }
1528
1529 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001530 {
1531 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1532 ++nbyte;
1533 else if (enc_utf8)
1534 nbyte += utfc_ptr2len(str + nbyte);
1535 else
1536 nbyte += mb_ptr2len(str + nbyte);
1537 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001538 if (nbyte >= slen)
1539 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001540 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001541}
1542
1543/*
1544 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001545 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001546 * If going over the end return "str_len".
1547 * If "idx" is negative count from the end, -1 is the last character.
1548 * When going over the start return -1.
1549 */
1550 static long
1551char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1552{
1553 varnumber_T nchar = idx;
1554 size_t nbyte = 0;
1555
1556 if (nchar >= 0)
1557 {
1558 while (nchar > 0 && nbyte < str_len)
1559 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001560 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001561 --nchar;
1562 }
1563 }
1564 else
1565 {
1566 nbyte = str_len;
1567 while (nchar < 0 && nbyte > 0)
1568 {
1569 --nbyte;
1570 nbyte -= mb_head_off(str, str + nbyte);
1571 ++nchar;
1572 }
1573 if (nchar < 0)
1574 return -1;
1575 }
1576 return (long)nbyte;
1577}
1578
1579/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001580 * Return the slice "str[first : last]" using character indexes. Composing
1581 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001582 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001583 * Return NULL when the result is empty.
1584 */
1585 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001586string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001587{
1588 long start_byte, end_byte;
1589 size_t slen;
1590
1591 if (str == NULL)
1592 return NULL;
1593 slen = STRLEN(str);
1594 start_byte = char_idx2byte(str, slen, first);
1595 if (start_byte < 0)
1596 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001597 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001598 end_byte = (long)slen;
1599 else
1600 {
1601 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001602 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001603 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001604 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001605 }
1606
1607 if (start_byte >= (long)slen || end_byte <= start_byte)
1608 return NULL;
1609 return vim_strnsave(str + start_byte, end_byte - start_byte);
1610}
1611
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001612/*
1613 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1614 * When "dfunc_idx" is negative don't give an error.
1615 * Returns NULL for an error.
1616 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001617 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001618get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001619{
1620 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001621 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1622 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001623 svar_T *sv;
1624
1625 if (sref->sref_seq != si->sn_script_seq)
1626 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001627 // The script was reloaded after the function was compiled, the
1628 // script_idx may not be valid.
1629 if (dfunc != NULL)
1630 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1631 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001632 return NULL;
1633 }
1634 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001635 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001636 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001637 if (dfunc != NULL)
1638 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001639 return NULL;
1640 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001641
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001642 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1643 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001644 {
1645 if (dfunc != NULL)
1646 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1647 return NULL;
1648 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001649 return sv;
1650}
1651
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001652/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001653 * Function passed to do_cmdline() for splitting a script joined by NL
1654 * characters.
1655 */
1656 static char_u *
1657get_split_sourceline(
1658 int c UNUSED,
1659 void *cookie,
1660 int indent UNUSED,
1661 getline_opt_T options UNUSED)
1662{
1663 source_cookie_T *sp = (source_cookie_T *)cookie;
1664 char_u *p;
1665 char_u *line;
1666
Bram Moolenaar20677332021-06-06 17:02:53 +02001667 p = vim_strchr(sp->nextline, '\n');
1668 if (p == NULL)
1669 {
1670 line = vim_strsave(sp->nextline);
1671 sp->nextline += STRLEN(sp->nextline);
1672 }
1673 else
1674 {
1675 line = vim_strnsave(sp->nextline, p - sp->nextline);
1676 sp->nextline = p + 1;
1677 }
1678 return line;
1679}
1680
1681/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 * Execute a function by "name".
1683 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001684 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 */
1686 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001687call_eval_func(
1688 char_u *name,
1689 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001690 ectx_T *ectx,
1691 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692{
Bram Moolenaared677f52020-08-12 16:38:10 +02001693 int called_emsg_before = called_emsg;
1694 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001696 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001697 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001699 dictitem_T *v;
1700
1701 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001702 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1703 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001704 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001705 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001706 return FAIL;
1707 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001708 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001710 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711}
1712
1713/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001714 * When a function reference is used, fill a partial with the information
1715 * needed, especially when it is used as a closure.
1716 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001717 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001718fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1719{
1720 pt->pt_func = ufunc;
1721 pt->pt_refcount = 1;
1722
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001723 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001724 {
1725 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1726 + ectx->ec_dfunc_idx;
1727
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001728 // The closure may need to find arguments and local variables in the
1729 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001730 pt->pt_outer.out_stack = &ectx->ec_stack;
1731 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001732 if (ectx->ec_outer_ref != NULL)
1733 {
1734 // The current context already has a context, link to that one.
1735 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1736 if (ectx->ec_outer_ref->or_partial != NULL)
1737 {
1738 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1739 ++pt->pt_outer.out_up_partial->pt_refcount;
1740 }
1741 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001742
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001743 // If this function returns and the closure is still being used, we
1744 // need to make a copy of the context (arguments and local variables).
1745 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001746 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001747 {
1748 vim_free(pt);
1749 return FAIL;
1750 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001751 // Extra variable keeps the count of closures created in the current
1752 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001753 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1754 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1755
1756 ((partial_T **)ectx->ec_funcrefs.ga_data)
1757 [ectx->ec_funcrefs.ga_len] = pt;
1758 ++pt->pt_refcount;
1759 ++ectx->ec_funcrefs.ga_len;
1760 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001761 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001762 return OK;
1763}
1764
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001765/*
1766 * Execute iptr->isn_arg.string as an Ex command.
1767 */
1768 static int
1769exec_command(isn_T *iptr)
1770{
1771 source_cookie_T cookie;
1772
1773 SOURCING_LNUM = iptr->isn_lnum;
1774 // Pass getsourceline to get an error for a missing ":end"
1775 // command.
1776 CLEAR_FIELD(cookie);
1777 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1778 if (do_cmdline(iptr->isn_arg.string,
1779 getsourceline, &cookie,
1780 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1781 || did_emsg)
1782 return FAIL;
1783 return OK;
1784}
1785
Bram Moolenaar89445512022-04-14 12:58:23 +01001786/*
1787 * If script "sid" is not loaded yet then load it now.
1788 * Caller must make sure "sid" is a valid script ID.
1789 * "loaded" is set to TRUE if the script had to be loaded.
1790 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1791 */
1792 int
1793may_load_script(int sid, int *loaded)
1794{
1795 scriptitem_T *si = SCRIPT_ITEM(sid);
1796
1797 if (si->sn_state == SN_STATE_NOT_LOADED)
1798 {
1799 *loaded = TRUE;
1800 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1801 {
1802 semsg(_(e_cant_open_file_str), si->sn_name);
1803 return FAIL;
1804 }
1805 }
1806 return OK;
1807}
1808
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001809// used for v_instr of typval of VAR_INSTR
1810struct instr_S {
1811 ectx_T *instr_ectx;
1812 isn_T *instr_instr;
1813};
1814
Bram Moolenaar4c137212021-04-19 16:48:48 +02001815// used for substitute_instr
1816typedef struct subs_expr_S {
1817 ectx_T *subs_ectx;
1818 isn_T *subs_instr;
1819 int subs_status;
1820} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001822// Set when calling do_debug().
1823static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001824static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001825
1826/*
1827 * When debugging lookup "name" and return the typeval.
1828 * When not found return NULL.
1829 */
1830 typval_T *
1831lookup_debug_var(char_u *name)
1832{
1833 int idx;
1834 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001835 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001836 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001837 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001838
1839 if (ectx == NULL)
1840 return NULL;
1841 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1842
1843 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001844 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001845 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001846 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1847
1848 // the variable name may be NULL when not available in this block
1849 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001850 return STACK_TV_VAR(idx);
1851 }
1852
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001853 // Go through argument names.
1854 ufunc = dfunc->df_ufunc;
1855 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1856 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1857 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1858 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1859 - varargs_off + idx);
1860 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1861 return STACK_TV(ectx->ec_frame_idx - 1);
1862
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001863 return NULL;
1864}
1865
Bram Moolenaar26a44842021-09-02 18:49:06 +02001866/*
1867 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1868 * breakpoint was set in that function or when there is any expression.
1869 */
1870 int
1871may_break_in_function(ufunc_T *ufunc)
1872{
1873 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1874}
1875
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001876 static void
1877handle_debug(isn_T *iptr, ectx_T *ectx)
1878{
1879 char_u *line;
1880 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1881 + ectx->ec_dfunc_idx)->df_ufunc;
1882 isn_T *ni;
1883 int end_lnum = iptr->isn_lnum;
1884 garray_T ga;
1885 int lnum;
1886
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001887 if (ex_nesting_level > debug_break_level)
1888 {
1889 linenr_T breakpoint;
1890
Bram Moolenaar26a44842021-09-02 18:49:06 +02001891 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001892 return;
1893
1894 // check for the next breakpoint if needed
1895 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001896 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001897 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1898 return;
1899 }
1900
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001901 SOURCING_LNUM = iptr->isn_lnum;
1902 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001903 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001904
1905 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1906 if (ni->isn_type == ISN_DEBUG
1907 || ni->isn_type == ISN_RETURN
1908 || ni->isn_type == ISN_RETURN_VOID)
1909 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001910 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001911 break;
1912 }
1913
1914 if (end_lnum > iptr->isn_lnum)
1915 {
1916 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001917 for (lnum = iptr->isn_lnum; lnum < end_lnum
1918 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001919 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001920 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001921
Bram Moolenaar303215d2021-07-07 20:10:43 +02001922 if (p == NULL)
1923 continue; // left over from continuation line
1924 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001925 if (*p == '#')
1926 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001927 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001928 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1929 if (STRNCMP(p, "def ", 4) == 0)
1930 break;
1931 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001932 line = ga_concat_strings(&ga, " ");
1933 vim_free(ga.ga_data);
1934 }
1935 else
1936 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001937
Dominique Pelle6e969552021-06-17 13:53:41 +02001938 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001939 debug_context = NULL;
1940
1941 if (end_lnum > iptr->isn_lnum)
1942 vim_free(line);
1943}
1944
Bram Moolenaar4c137212021-04-19 16:48:48 +02001945/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001946 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001947 * Returns OK, FAIL or NOTDONE (uncatchable error).
1948 */
1949 static int
1950execute_storeindex(isn_T *iptr, ectx_T *ectx)
1951{
1952 vartype_T dest_type = iptr->isn_arg.vartype;
1953 typval_T *tv;
1954 typval_T *tv_idx = STACK_TV_BOT(-2);
1955 typval_T *tv_dest = STACK_TV_BOT(-1);
1956 int status = OK;
1957
1958 // Stack contains:
1959 // -3 value to be stored
1960 // -2 index
1961 // -1 dict or list
1962 tv = STACK_TV_BOT(-3);
1963 SOURCING_LNUM = iptr->isn_lnum;
1964 if (dest_type == VAR_ANY)
1965 {
1966 dest_type = tv_dest->v_type;
1967 if (dest_type == VAR_DICT)
1968 status = do_2string(tv_idx, TRUE, FALSE);
1969 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1970 {
1971 emsg(_(e_number_expected));
1972 status = FAIL;
1973 }
1974 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001975
1976 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001977 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001978 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001979 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001980 long lidx = (long)tv_idx->vval.v_number;
1981 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001982
Bram Moolenaare08be092022-02-17 13:08:26 +00001983 if (list == NULL)
1984 {
1985 emsg(_(e_list_not_set));
1986 return FAIL;
1987 }
1988 if (lidx < 0 && list->lv_len + lidx >= 0)
1989 // negative index is relative to the end
1990 lidx = list->lv_len + lidx;
1991 if (lidx < 0 || lidx > list->lv_len)
1992 {
1993 semsg(_(e_list_index_out_of_range_nr), lidx);
1994 return FAIL;
1995 }
1996 if (lidx < list->lv_len)
1997 {
1998 listitem_T *li = list_find(list, lidx);
1999
2000 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002001 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002002 return FAIL;
2003 // overwrite existing list item
2004 clear_tv(&li->li_tv);
2005 li->li_tv = *tv;
2006 }
2007 else
2008 {
2009 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2010 return FAIL;
2011 // append to list, only fails when out of memory
2012 if (list_append_tv(list, tv) == FAIL)
2013 return NOTDONE;
2014 clear_tv(tv);
2015 }
2016 }
2017 else if (dest_type == VAR_DICT)
2018 {
2019 char_u *key = tv_idx->vval.v_string;
2020 dict_T *dict = tv_dest->vval.v_dict;
2021 dictitem_T *di;
2022
2023 SOURCING_LNUM = iptr->isn_lnum;
2024 if (dict == NULL)
2025 {
2026 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002027 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002028 }
2029 if (key == NULL)
2030 key = (char_u *)"";
2031 di = dict_find(dict, key, -1);
2032 if (di != NULL)
2033 {
2034 if (error_if_locked(di->di_tv.v_lock,
2035 e_cannot_change_dict_item))
2036 return FAIL;
2037 // overwrite existing value
2038 clear_tv(&di->di_tv);
2039 di->di_tv = *tv;
2040 }
2041 else
2042 {
2043 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2044 return FAIL;
2045 // add to dict, only fails when out of memory
2046 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2047 return NOTDONE;
2048 clear_tv(tv);
2049 }
2050 }
2051 else if (dest_type == VAR_BLOB)
2052 {
2053 long lidx = (long)tv_idx->vval.v_number;
2054 blob_T *blob = tv_dest->vval.v_blob;
2055 varnumber_T nr;
2056 int error = FALSE;
2057 int len;
2058
2059 if (blob == NULL)
2060 {
2061 emsg(_(e_blob_not_set));
2062 return FAIL;
2063 }
2064 len = blob_len(blob);
2065 if (lidx < 0 && len + lidx >= 0)
2066 // negative index is relative to the end
2067 lidx = len + lidx;
2068
2069 // Can add one byte at the end.
2070 if (lidx < 0 || lidx > len)
2071 {
2072 semsg(_(e_blob_index_out_of_range_nr), lidx);
2073 return FAIL;
2074 }
2075 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2076 return FAIL;
2077 nr = tv_get_number_chk(tv, &error);
2078 if (error)
2079 return FAIL;
2080 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002081 }
2082 else
2083 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002084 status = FAIL;
2085 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002086 }
2087 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002088
2089 clear_tv(tv_idx);
2090 clear_tv(tv_dest);
2091 ectx->ec_stack.ga_len -= 3;
2092 if (status == FAIL)
2093 {
2094 clear_tv(tv);
2095 return FAIL;
2096 }
2097 return OK;
2098}
2099
2100/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002101 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002102 */
2103 static int
2104execute_storerange(isn_T *iptr, ectx_T *ectx)
2105{
2106 typval_T *tv;
2107 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2108 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2109 typval_T *tv_dest = STACK_TV_BOT(-1);
2110 int status = OK;
2111
2112 // Stack contains:
2113 // -4 value to be stored
2114 // -3 first index or "none"
2115 // -2 second index or "none"
2116 // -1 destination list or blob
2117 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002118 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002119 if (tv_dest->v_type == VAR_LIST)
2120 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002121 long n1;
2122 long n2;
2123 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002124
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002125 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2126 if (tv_idx2->v_type == VAR_SPECIAL
2127 && tv_idx2->vval.v_number == VVAL_NONE)
2128 n2 = list_len(tv_dest->vval.v_list) - 1;
2129 else
2130 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2131
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002132 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002133 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002134 status = FAIL;
2135 else
2136 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002137 status = check_range_index_two(tv_dest->vval.v_list,
2138 &n1, li1, &n2, FALSE);
2139 if (status != FAIL)
2140 status = list_assign_range(
2141 tv_dest->vval.v_list,
2142 tv->vval.v_list,
2143 n1,
2144 n2,
2145 tv_idx2->v_type == VAR_SPECIAL,
2146 (char_u *)"=",
2147 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002148 }
2149 }
2150 else if (tv_dest->v_type == VAR_BLOB)
2151 {
2152 varnumber_T n1;
2153 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002154 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002155
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002156 n1 = tv_get_number_chk(tv_idx1, NULL);
2157 if (tv_idx2->v_type == VAR_SPECIAL
2158 && tv_idx2->vval.v_number == VVAL_NONE)
2159 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2160 else
2161 n2 = tv_get_number_chk(tv_idx2, NULL);
2162 bloblen = blob_len(tv_dest->vval.v_blob);
2163
2164 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2165 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002166 status = FAIL;
2167 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002168 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002169 }
2170 else
2171 {
2172 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002173 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002174 }
2175
2176 clear_tv(tv_idx1);
2177 clear_tv(tv_idx2);
2178 clear_tv(tv_dest);
2179 ectx->ec_stack.ga_len -= 4;
2180 clear_tv(tv);
2181
2182 return status;
2183}
2184
2185/*
2186 * Unlet item in list or dict variable.
2187 */
2188 static int
2189execute_unletindex(isn_T *iptr, ectx_T *ectx)
2190{
2191 typval_T *tv_idx = STACK_TV_BOT(-2);
2192 typval_T *tv_dest = STACK_TV_BOT(-1);
2193 int status = OK;
2194
2195 // Stack contains:
2196 // -2 index
2197 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002198 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002199 if (tv_dest->v_type == VAR_DICT)
2200 {
2201 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002202 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002203 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002204 semsg(_(e_expected_str_but_got_str),
2205 vartype_name(VAR_STRING),
2206 vartype_name(tv_idx->v_type));
2207 status = FAIL;
2208 }
2209 else
2210 {
2211 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002212 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002213 dictitem_T *di = NULL;
2214
2215 if (d != NULL && value_check_lock(
2216 d->dv_lock, NULL, FALSE))
2217 status = FAIL;
2218 else
2219 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002220 if (tv_idx->v_type == VAR_STRING)
2221 {
2222 key = tv_idx->vval.v_string;
2223 if (key == NULL)
2224 key = (char_u *)"";
2225 }
2226 else
2227 {
2228 key = tv_get_string(tv_idx);
2229 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002230 if (d != NULL)
2231 di = dict_find(d, key, (int)STRLEN(key));
2232 if (di == NULL)
2233 {
2234 // NULL dict is equivalent to empty dict
2235 semsg(_(e_key_not_present_in_dictionary),
2236 key);
2237 status = FAIL;
2238 }
2239 else if (var_check_fixed(di->di_flags,
2240 NULL, FALSE)
2241 || var_check_ro(di->di_flags,
2242 NULL, FALSE))
2243 status = FAIL;
2244 else
2245 dictitem_remove(d, di);
2246 }
2247 }
2248 }
2249 else if (tv_dest->v_type == VAR_LIST)
2250 {
2251 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002252 if (check_for_number(tv_idx) == FAIL)
2253 {
2254 status = FAIL;
2255 }
2256 else
2257 {
2258 list_T *l = tv_dest->vval.v_list;
2259 long n = (long)tv_idx->vval.v_number;
2260
2261 if (l != NULL && value_check_lock(
2262 l->lv_lock, NULL, FALSE))
2263 status = FAIL;
2264 else
2265 {
2266 listitem_T *li = list_find(l, n);
2267
2268 if (li == NULL)
2269 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002270 semsg(_(e_list_index_out_of_range_nr), n);
2271 status = FAIL;
2272 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002273 else
2274 listitem_remove(l, li);
2275 }
2276 }
2277 }
2278 else
2279 {
2280 status = FAIL;
2281 semsg(_(e_cannot_index_str),
2282 vartype_name(tv_dest->v_type));
2283 }
2284
2285 clear_tv(tv_idx);
2286 clear_tv(tv_dest);
2287 ectx->ec_stack.ga_len -= 2;
2288
2289 return status;
2290}
2291
2292/*
2293 * Unlet a range of items in a list variable.
2294 */
2295 static int
2296execute_unletrange(isn_T *iptr, ectx_T *ectx)
2297{
2298 // Stack contains:
2299 // -3 index1
2300 // -2 index2
2301 // -1 dict or list
2302 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2303 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2304 typval_T *tv_dest = STACK_TV_BOT(-1);
2305 int status = OK;
2306
2307 if (tv_dest->v_type == VAR_LIST)
2308 {
2309 // indexes must be a number
2310 SOURCING_LNUM = iptr->isn_lnum;
2311 if (check_for_number(tv_idx1) == FAIL
2312 || (tv_idx2->v_type != VAR_SPECIAL
2313 && check_for_number(tv_idx2) == FAIL))
2314 {
2315 status = FAIL;
2316 }
2317 else
2318 {
2319 list_T *l = tv_dest->vval.v_list;
2320 long n1 = (long)tv_idx1->vval.v_number;
2321 long n2 = tv_idx2->v_type == VAR_SPECIAL
2322 ? 0 : (long)tv_idx2->vval.v_number;
2323 listitem_T *li;
2324
2325 li = list_find_index(l, &n1);
2326 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002327 {
2328 semsg(_(e_list_index_out_of_range_nr),
2329 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002330 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002331 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002332 else
2333 {
2334 if (n1 < 0)
2335 n1 = list_idx_of_item(l, li);
2336 if (n2 < 0)
2337 {
2338 listitem_T *li2 = list_find(l, n2);
2339
2340 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002341 {
2342 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002343 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002344 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002345 else
2346 n2 = list_idx_of_item(l, li2);
2347 }
2348 if (status != FAIL
2349 && tv_idx2->v_type != VAR_SPECIAL
2350 && n2 < n1)
2351 {
2352 semsg(_(e_list_index_out_of_range_nr), n2);
2353 status = FAIL;
2354 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002355 if (status != FAIL)
2356 list_unlet_range(l, li, n1,
2357 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002358 }
2359 }
2360 }
2361 else
2362 {
2363 status = FAIL;
2364 SOURCING_LNUM = iptr->isn_lnum;
2365 semsg(_(e_cannot_index_str),
2366 vartype_name(tv_dest->v_type));
2367 }
2368
2369 clear_tv(tv_idx1);
2370 clear_tv(tv_idx2);
2371 clear_tv(tv_dest);
2372 ectx->ec_stack.ga_len -= 3;
2373
2374 return status;
2375}
2376
2377/*
2378 * Top of a for loop.
2379 */
2380 static int
2381execute_for(isn_T *iptr, ectx_T *ectx)
2382{
2383 typval_T *tv;
2384 typval_T *ltv = STACK_TV_BOT(-1);
2385 typval_T *idxtv =
2386 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2387
2388 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2389 return FAIL;
2390 if (ltv->v_type == VAR_LIST)
2391 {
2392 list_T *list = ltv->vval.v_list;
2393
2394 // push the next item from the list
2395 ++idxtv->vval.v_number;
2396 if (list == NULL
2397 || idxtv->vval.v_number >= list->lv_len)
2398 {
2399 // past the end of the list, jump to "endfor"
2400 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2401 may_restore_cmdmod(&ectx->ec_funclocal);
2402 }
2403 else if (list->lv_first == &range_list_item)
2404 {
2405 // non-materialized range() list
2406 tv = STACK_TV_BOT(0);
2407 tv->v_type = VAR_NUMBER;
2408 tv->v_lock = 0;
2409 tv->vval.v_number = list_find_nr(
2410 list, idxtv->vval.v_number, NULL);
2411 ++ectx->ec_stack.ga_len;
2412 }
2413 else
2414 {
2415 listitem_T *li = list_find(list,
2416 idxtv->vval.v_number);
2417
2418 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2419 ++ectx->ec_stack.ga_len;
2420 }
2421 }
2422 else if (ltv->v_type == VAR_STRING)
2423 {
2424 char_u *str = ltv->vval.v_string;
2425
2426 // The index is for the last byte of the previous
2427 // character.
2428 ++idxtv->vval.v_number;
2429 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2430 {
2431 // past the end of the string, jump to "endfor"
2432 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2433 may_restore_cmdmod(&ectx->ec_funclocal);
2434 }
2435 else
2436 {
2437 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2438
2439 // Push the next character from the string.
2440 tv = STACK_TV_BOT(0);
2441 tv->v_type = VAR_STRING;
2442 tv->vval.v_string = vim_strnsave(
2443 str + idxtv->vval.v_number, clen);
2444 ++ectx->ec_stack.ga_len;
2445 idxtv->vval.v_number += clen - 1;
2446 }
2447 }
2448 else if (ltv->v_type == VAR_BLOB)
2449 {
2450 blob_T *blob = ltv->vval.v_blob;
2451
2452 // When we get here the first time make a copy of the
2453 // blob, so that the iteration still works when it is
2454 // changed.
2455 if (idxtv->vval.v_number == -1 && blob != NULL)
2456 {
2457 blob_copy(blob, ltv);
2458 blob_unref(blob);
2459 blob = ltv->vval.v_blob;
2460 }
2461
2462 // The index is for the previous byte.
2463 ++idxtv->vval.v_number;
2464 if (blob == NULL
2465 || idxtv->vval.v_number >= blob_len(blob))
2466 {
2467 // past the end of the blob, jump to "endfor"
2468 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2469 may_restore_cmdmod(&ectx->ec_funclocal);
2470 }
2471 else
2472 {
2473 // Push the next byte from the blob.
2474 tv = STACK_TV_BOT(0);
2475 tv->v_type = VAR_NUMBER;
2476 tv->vval.v_number = blob_get(blob,
2477 idxtv->vval.v_number);
2478 ++ectx->ec_stack.ga_len;
2479 }
2480 }
2481 else
2482 {
2483 semsg(_(e_for_loop_on_str_not_supported),
2484 vartype_name(ltv->v_type));
2485 return FAIL;
2486 }
2487 return OK;
2488}
2489
2490/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002491 * Load instruction for w:/b:/g:/t: variable.
2492 * "isn_type" is used instead of "iptr->isn_type".
2493 */
2494 static int
2495load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2496{
2497 dictitem_T *di = NULL;
2498 hashtab_T *ht = NULL;
2499 char namespace;
2500
2501 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2502 return NOTDONE;
2503 switch (isn_type)
2504 {
2505 case ISN_LOADG:
2506 ht = get_globvar_ht();
2507 namespace = 'g';
2508 break;
2509 case ISN_LOADB:
2510 ht = &curbuf->b_vars->dv_hashtab;
2511 namespace = 'b';
2512 break;
2513 case ISN_LOADW:
2514 ht = &curwin->w_vars->dv_hashtab;
2515 namespace = 'w';
2516 break;
2517 case ISN_LOADT:
2518 ht = &curtab->tp_vars->dv_hashtab;
2519 namespace = 't';
2520 break;
2521 default: // Cannot reach here
2522 return NOTDONE;
2523 }
2524 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2525
Bram Moolenaar06b77222022-01-25 15:51:56 +00002526 if (di == NULL)
2527 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002528 if (isn_type == ISN_LOADG)
2529 {
2530 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2531
2532 // g:Something could be a function
2533 if (ufunc != NULL)
2534 {
2535 typval_T *tv = STACK_TV_BOT(0);
2536
2537 ++ectx->ec_stack.ga_len;
2538 tv->v_type = VAR_FUNC;
2539 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2540 if (tv->vval.v_string == NULL)
2541 return FAIL;
2542 STRCPY(tv->vval.v_string, "g:");
2543 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2544 return OK;
2545 }
2546 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002547 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002548 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002549 // no check if the item exists in the script but
2550 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002551 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002552 else
2553 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002554 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002555 return FAIL;
2556 }
2557 else
2558 {
2559 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2560 ++ectx->ec_stack.ga_len;
2561 }
2562 return OK;
2563}
2564
2565/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002566 * Execute instructions in execution context "ectx".
2567 * Return OK or FAIL;
2568 */
2569 static int
2570exec_instructions(ectx_T *ectx)
2571{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002572 int ret = FAIL;
2573 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002574 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002575
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002576 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002577 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002578
Bram Moolenaarff652882021-05-16 15:24:49 +02002579 // Only catch exceptions in this instruction list.
2580 ectx->ec_trylevel_at_start = trylevel;
2581
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 for (;;)
2583 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002584 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002586 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002587
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002588 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002589 {
2590 line_breakcheck();
2591 breakcheck_count = 0;
2592 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002593 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002594 {
2595 // Turn CTRL-C into an exception.
2596 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002597 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002598 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002599 did_throw = TRUE;
2600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002602 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002603 {
2604 // Turn an error message into an exception.
2605 did_emsg = FALSE;
2606 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002607 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002608 did_throw = TRUE;
2609 *msg_list = NULL;
2610 }
2611
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002612 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002614 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002615 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002616 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617
2618 // An exception jumps to the first catch, finally, or returns from
2619 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002620 while (index > 0)
2621 {
2622 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002623 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002624 break;
2625 // In the catch and finally block of this try we have to go up
2626 // one level.
2627 --index;
2628 trycmd = NULL;
2629 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002630 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002632 if (trycmd->tcd_in_catch)
2633 {
2634 // exception inside ":catch", jump to ":finally" once
2635 ectx->ec_iidx = trycmd->tcd_finally_idx;
2636 trycmd->tcd_finally_idx = 0;
2637 }
2638 else
2639 // jump to first ":catch"
2640 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002641 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002642 did_throw = FALSE; // don't come back here until :endtry
2643 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 }
2645 else
2646 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002647 // Not inside try or need to return from current functions.
2648 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002649 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002650 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002651 tv = STACK_TV_BOT(0);
2652 tv->v_type = VAR_NUMBER;
2653 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002654 ++ectx->ec_stack.ga_len;
2655 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002657 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002658 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002659 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002660 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 goto done;
2662 }
2663
Bram Moolenaar4c137212021-04-19 16:48:48 +02002664 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002665 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 }
2667 continue;
2668 }
2669
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002670 /*
2671 * Big switch on the instruction. Most compilers will be turning this
2672 * into an efficient lookup table, since the "case" values are an enum
2673 * with sequential numbers. It may look ugly, but it should be the
2674 * most efficient way.
2675 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002676 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 switch (iptr->isn_type)
2678 {
2679 // execute Ex command line
2680 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002681 if (exec_command(iptr) == FAIL)
2682 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 break;
2684
Bram Moolenaar20677332021-06-06 17:02:53 +02002685 // execute Ex command line split at NL characters.
2686 case ISN_EXEC_SPLIT:
2687 {
2688 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002689 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002690
2691 SOURCING_LNUM = iptr->isn_lnum;
2692 CLEAR_FIELD(cookie);
2693 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2694 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002695 line = get_split_sourceline(0, &cookie, 0, 0);
2696 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002697 get_split_sourceline, &cookie,
2698 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2699 == FAIL
2700 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002701 {
2702 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002703 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002704 }
2705 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002706 }
2707 break;
2708
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002709 // execute Ex command line that is only a range
2710 case ISN_EXECRANGE:
2711 {
2712 exarg_T ea;
2713 char *error = NULL;
2714
2715 CLEAR_FIELD(ea);
2716 ea.cmdidx = CMD_SIZE;
2717 ea.addr_type = ADDR_LINES;
2718 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002719 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002720 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002721 if (ea.cmd == NULL)
2722 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002723 // error is always NULL when using ADDR_LINES
2724 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002725 if (error != NULL)
2726 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002727 emsg(error);
2728 goto on_error;
2729 }
2730 }
2731 break;
2732
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002733 // Evaluate an expression with legacy syntax, push it onto the
2734 // stack.
2735 case ISN_LEGACY_EVAL:
2736 {
2737 char_u *arg = iptr->isn_arg.string;
2738 int res;
2739 int save_flags = cmdmod.cmod_flags;
2740
Bram Moolenaar35578162021-08-02 19:10:38 +02002741 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002742 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002743 tv = STACK_TV_BOT(0);
2744 init_tv(tv);
2745 cmdmod.cmod_flags |= CMOD_LEGACY;
2746 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2747 cmdmod.cmod_flags = save_flags;
2748 if (res == FAIL)
2749 goto on_error;
2750 ++ectx->ec_stack.ga_len;
2751 }
2752 break;
2753
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002754 // push typeval VAR_INSTR with instructions to be executed
2755 case ISN_INSTR:
2756 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002757 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002758 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002759 tv = STACK_TV_BOT(0);
2760 tv->vval.v_instr = ALLOC_ONE(instr_T);
2761 if (tv->vval.v_instr == NULL)
2762 goto on_error;
2763 ++ectx->ec_stack.ga_len;
2764
2765 tv->v_type = VAR_INSTR;
2766 tv->vval.v_instr->instr_ectx = ectx;
2767 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2768 }
2769 break;
2770
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002771 case ISN_SOURCE:
2772 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002773 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002774
Bram Moolenaar89445512022-04-14 12:58:23 +01002775 SOURCING_LNUM = iptr->isn_lnum;
2776 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002777 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002778 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002779 }
2780 break;
2781
Bram Moolenaar4c137212021-04-19 16:48:48 +02002782 // execute :substitute with an expression
2783 case ISN_SUBSTITUTE:
2784 {
2785 subs_T *subs = &iptr->isn_arg.subs;
2786 source_cookie_T cookie;
2787 struct subs_expr_S *save_instr = substitute_instr;
2788 struct subs_expr_S subs_instr;
2789 int res;
2790
2791 subs_instr.subs_ectx = ectx;
2792 subs_instr.subs_instr = subs->subs_instr;
2793 subs_instr.subs_status = OK;
2794 substitute_instr = &subs_instr;
2795
2796 SOURCING_LNUM = iptr->isn_lnum;
2797 // This is very much like ISN_EXEC
2798 CLEAR_FIELD(cookie);
2799 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2800 res = do_cmdline(subs->subs_cmd,
2801 getsourceline, &cookie,
2802 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2803 substitute_instr = save_instr;
2804
2805 if (res == FAIL || did_emsg
2806 || subs_instr.subs_status == FAIL)
2807 goto on_error;
2808 }
2809 break;
2810
2811 case ISN_FINISH:
2812 goto done;
2813
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002814 case ISN_REDIRSTART:
2815 // create a dummy entry for var_redir_str()
2816 if (alloc_redir_lval() == FAIL)
2817 goto on_error;
2818
2819 // The output is stored in growarray "redir_ga" until
2820 // redirection ends.
2821 init_redir_ga();
2822 redir_vname = 1;
2823 break;
2824
2825 case ISN_REDIREND:
2826 {
2827 char_u *res = get_clear_redir_ga();
2828
2829 // End redirection, put redirected text on the stack.
2830 clear_redir_lval();
2831 redir_vname = 0;
2832
Bram Moolenaar35578162021-08-02 19:10:38 +02002833 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002834 {
2835 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002836 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002837 }
2838 tv = STACK_TV_BOT(0);
2839 tv->v_type = VAR_STRING;
2840 tv->vval.v_string = res;
2841 ++ectx->ec_stack.ga_len;
2842 }
2843 break;
2844
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002845 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002846#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002847 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002848 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2849 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002850 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002851#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002852 break;
2853
2854 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002855#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002856 {
2857 exarg_T ea;
2858 int res;
2859
2860 CLEAR_FIELD(ea);
2861 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2862 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2863 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2864 --ectx->ec_stack.ga_len;
2865 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01002866 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002867 res = cexpr_core(&ea, tv);
2868 clear_tv(tv);
2869 if (res == FAIL)
2870 goto on_error;
2871 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002872#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002873 break;
2874
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002875 // execute Ex command from pieces on the stack
2876 case ISN_EXECCONCAT:
2877 {
2878 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002879 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002880 int pass;
2881 int i;
2882 char_u *cmd = NULL;
2883 char_u *str;
2884
2885 for (pass = 1; pass <= 2; ++pass)
2886 {
2887 for (i = 0; i < count; ++i)
2888 {
2889 tv = STACK_TV_BOT(i - count);
2890 str = tv->vval.v_string;
2891 if (str != NULL && *str != NUL)
2892 {
2893 if (pass == 2)
2894 STRCPY(cmd + len, str);
2895 len += STRLEN(str);
2896 }
2897 if (pass == 2)
2898 clear_tv(tv);
2899 }
2900 if (pass == 1)
2901 {
2902 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002903 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002904 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002905 len = 0;
2906 }
2907 }
2908
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002909 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002910 do_cmdline_cmd(cmd);
2911 vim_free(cmd);
2912 }
2913 break;
2914
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 // execute :echo {string} ...
2916 case ISN_ECHO:
2917 {
2918 int count = iptr->isn_arg.echo.echo_count;
2919 int atstart = TRUE;
2920 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002921 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922
2923 for (idx = 0; idx < count; ++idx)
2924 {
2925 tv = STACK_TV_BOT(idx - count);
2926 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2927 &atstart, &needclr);
2928 clear_tv(tv);
2929 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002930 if (needclr)
2931 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002932 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 }
2934 break;
2935
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002936 // :execute {string} ...
2937 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002938 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002939 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002940 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002941 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002942 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002943 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002944 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002945 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002946 {
2947 int count = iptr->isn_arg.number;
2948 garray_T ga;
2949 char_u buf[NUMBUFLEN];
2950 char_u *p;
2951 int len;
2952 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002953 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002954
2955 ga_init2(&ga, 1, 80);
2956 for (idx = 0; idx < count; ++idx)
2957 {
2958 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002959 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002960 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002961 if (tv->v_type == VAR_CHANNEL
2962 || tv->v_type == VAR_JOB)
2963 {
2964 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002965 semsg(_(e_using_invalid_value_as_string_str),
2966 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002967 break;
2968 }
2969 else
2970 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002971 }
2972 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002973 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002974
2975 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002976 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002977 failed = TRUE;
2978 else
2979 {
2980 if (ga.ga_len > 0)
2981 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2982 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2983 ga.ga_len += len;
2984 }
2985 clear_tv(tv);
2986 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002987 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002988 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002989 {
2990 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002991 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002992 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002993
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002994 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002995 {
2996 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002997 {
2998 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002999 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003000 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003001 {
3002 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003003 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003004 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003005 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003006 else
3007 {
3008 msg_sb_eol();
3009 if (iptr->isn_type == ISN_ECHOMSG)
3010 {
3011 msg_attr(ga.ga_data, echo_attr);
3012 out_flush();
3013 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003014#ifdef HAS_MESSAGE_WINDOW
3015 else if (iptr->isn_type == ISN_ECHOWINDOW)
3016 {
3017 start_echowindow();
3018 msg_attr(ga.ga_data, echo_attr);
3019 end_echowindow();
3020 }
3021#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003022 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3023 {
3024 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3025 TRUE);
3026 ui_write((char_u *)"\r\n", 2, TRUE);
3027 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003028 else
3029 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003030 SOURCING_LNUM = iptr->isn_lnum;
3031 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003032 }
3033 }
3034 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003035 ga_clear(&ga);
3036 }
3037 break;
3038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 // load local variable or argument
3040 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003041 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003042 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003044 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 break;
3046
3047 // load v: variable
3048 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003049 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003050 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003052 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 break;
3054
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003055 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 case ISN_LOADSCRIPT:
3057 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003058 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 svar_T *sv;
3060
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003061 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003062 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003063 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003064 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003065 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003066 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003068 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 }
3070 break;
3071
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003072 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003074 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003076 int sid = iptr->isn_arg.loadstore.ls_sid;
3077 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003078 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 if (di == NULL)
3082 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003083 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003084 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003085 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 }
3087 else
3088 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003089 if (iptr->isn_type == ISN_LOADEXPORT)
3090 {
3091 int idx = get_script_item_idx(sid, name, 0,
3092 NULL, NULL);
3093 svar_T *sv;
3094
3095 if (idx >= 0)
3096 {
3097 sv = ((svar_T *)SCRIPT_ITEM(sid)
3098 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003099 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003100 {
3101 SOURCING_LNUM = iptr->isn_lnum;
3102 semsg(_(e_item_not_exported_in_script_str),
3103 name);
3104 goto on_error;
3105 }
3106 }
3107 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003108 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003109 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003111 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 }
3113 }
3114 break;
3115
Bram Moolenaard3aac292020-04-19 14:32:17 +02003116 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003118 case ISN_LOADB:
3119 case ISN_LOADW:
3120 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003122 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003123
Bram Moolenaar06b77222022-01-25 15:51:56 +00003124 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003125 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003126 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003127 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 break;
3131
Bram Moolenaar03290b82020-12-19 16:30:44 +01003132 // load autoload variable
3133 case ISN_LOADAUTO:
3134 {
3135 char_u *name = iptr->isn_arg.string;
3136
Bram Moolenaar35578162021-08-02 19:10:38 +02003137 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003138 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003139 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003140 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003141 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003142 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003143 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003144 }
3145 break;
3146
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003147 // load g:/b:/w:/t: namespace
3148 case ISN_LOADGDICT:
3149 case ISN_LOADBDICT:
3150 case ISN_LOADWDICT:
3151 case ISN_LOADTDICT:
3152 {
3153 dict_T *d = NULL;
3154
3155 switch (iptr->isn_type)
3156 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003157 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3158 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3159 case ISN_LOADWDICT: d = curwin->w_vars; break;
3160 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003161 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003162 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003163 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003164 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003165 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003166 tv = STACK_TV_BOT(0);
3167 tv->v_type = VAR_DICT;
3168 tv->v_lock = 0;
3169 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003170 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003171 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003172 }
3173 break;
3174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 // load &option
3176 case ISN_LOADOPT:
3177 {
3178 typval_T optval;
3179 char_u *name = iptr->isn_arg.string;
3180
Bram Moolenaara8c17702020-04-01 21:17:24 +02003181 // This is not expected to fail, name is checked during
3182 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003183 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003184 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003185 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003186 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003188 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 }
3190 break;
3191
3192 // load $ENV
3193 case ISN_LOADENV:
3194 {
3195 typval_T optval;
3196 char_u *name = iptr->isn_arg.string;
3197
Bram Moolenaar35578162021-08-02 19:10:38 +02003198 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003199 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003200 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003201 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003203 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 }
3205 break;
3206
3207 // load @register
3208 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003209 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003210 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 tv = STACK_TV_BOT(0);
3212 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003213 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003214 // This may result in NULL, which should be equivalent to an
3215 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 tv->vval.v_string = get_reg_contents(
3217 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003218 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 break;
3220
3221 // store local variable
3222 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003223 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 tv = STACK_TV_VAR(iptr->isn_arg.number);
3225 clear_tv(tv);
3226 *tv = *STACK_TV_BOT(0);
3227 break;
3228
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003229 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003230 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003231 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003232 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003233 int sid = iptr->isn_arg.loadstore.ls_sid;
3234 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003235 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003236 dictitem_T *di = find_var_in_ht(ht, 0,
3237 iptr->isn_type == ISN_STORES
3238 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003239
Bram Moolenaar4c137212021-04-19 16:48:48 +02003240 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003241 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003242 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003243 {
3244 if (iptr->isn_type == ISN_STOREEXPORT)
3245 {
3246 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003247 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003248 goto on_error;
3249 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003250 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003251 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003252 else
3253 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003254 if (iptr->isn_type == ISN_STOREEXPORT)
3255 {
3256 int idx = get_script_item_idx(sid, name, 0,
3257 NULL, NULL);
3258
Bram Moolenaar06651632022-04-27 17:54:25 +01003259 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003260 if (idx >= 0)
3261 {
3262 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3263 ->sn_var_vals.ga_data) + idx;
3264
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003265 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003266 {
3267 semsg(_(e_item_not_exported_in_script_str),
3268 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003269 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003270 goto on_error;
3271 }
3272 }
3273 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003274 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003275 {
3276 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003277 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003278 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003279 clear_tv(&di->di_tv);
3280 di->di_tv = *STACK_TV_BOT(0);
3281 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003282 }
3283 break;
3284
3285 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 case ISN_STORESCRIPT:
3287 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003288 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3289 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003291 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003292 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003293 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003294 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003295
3296 // "const" and "final" are checked at compile time, locking
3297 // the value needs to be checked here.
3298 SOURCING_LNUM = iptr->isn_lnum;
3299 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003300 {
3301 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003302 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003303 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 clear_tv(sv->sv_tv);
3306 *sv->sv_tv = *STACK_TV_BOT(0);
3307 }
3308 break;
3309
3310 // store option
3311 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003312 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003314 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3315 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 long n = 0;
3317 char_u *s = NULL;
3318 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003319 char_u numbuf[NUMBUFLEN];
3320 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321
Bram Moolenaar4c137212021-04-19 16:48:48 +02003322 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 tv = STACK_TV_BOT(0);
3324 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003325 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003327 if (s == NULL)
3328 s = (char_u *)"";
3329 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003330 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3331 {
3332 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003333 // If the option can be set to a function reference or
3334 // a lambda and the passed value is a function
3335 // reference, then convert it to the name (string) of
3336 // the function reference.
3337 s = tv2string(tv, &tofree, numbuf, 0);
3338 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003339 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003340 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003341 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003342 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003343 goto on_error;
3344 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003345 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003347 // must be VAR_NUMBER, CHECKTYPE makes sure
3348 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003349 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003350 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003351 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 if (msg != NULL)
3353 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003354 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003356 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 }
3359 break;
3360
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003361 // store $ENV
3362 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003363 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003364 tv = STACK_TV_BOT(0);
3365 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3366 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003367 break;
3368
3369 // store @r
3370 case ISN_STOREREG:
3371 {
3372 int reg = iptr->isn_arg.number;
3373
Bram Moolenaar4c137212021-04-19 16:48:48 +02003374 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003375 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003376 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003377 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003378 }
3379 break;
3380
3381 // store v: variable
3382 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003383 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003384 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3385 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003386 // should not happen, type is checked when compiling
3387 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003388 break;
3389
Bram Moolenaard3aac292020-04-19 14:32:17 +02003390 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003392 case ISN_STOREB:
3393 case ISN_STOREW:
3394 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003396 dictitem_T *di;
3397 hashtab_T *ht;
3398 char_u *name = iptr->isn_arg.string + 2;
3399
Bram Moolenaard3aac292020-04-19 14:32:17 +02003400 switch (iptr->isn_type)
3401 {
3402 case ISN_STOREG:
3403 ht = get_globvar_ht();
3404 break;
3405 case ISN_STOREB:
3406 ht = &curbuf->b_vars->dv_hashtab;
3407 break;
3408 case ISN_STOREW:
3409 ht = &curwin->w_vars->dv_hashtab;
3410 break;
3411 case ISN_STORET:
3412 ht = &curtab->tp_vars->dv_hashtab;
3413 break;
3414 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003415 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003416 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417
Bram Moolenaar4c137212021-04-19 16:48:48 +02003418 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003419 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003421 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 else
3423 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003424 SOURCING_LNUM = iptr->isn_lnum;
3425 if (var_check_permission(di, name) == FAIL)
3426 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 clear_tv(&di->di_tv);
3428 di->di_tv = *STACK_TV_BOT(0);
3429 }
3430 }
3431 break;
3432
Bram Moolenaar03290b82020-12-19 16:30:44 +01003433 // store an autoload variable
3434 case ISN_STOREAUTO:
3435 SOURCING_LNUM = iptr->isn_lnum;
3436 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3437 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003438 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003439 break;
3440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 // store number in local variable
3442 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003443 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 clear_tv(tv);
3445 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003446 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 break;
3448
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003449 // store value in list or dict variable
3450 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003451 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003452 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003453
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003454 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003455 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003456 if (res == NOTDONE)
3457 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003458 }
3459 break;
3460
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003461 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003462 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003463 if (execute_storerange(iptr, ectx) == FAIL)
3464 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003465 break;
3466
Bram Moolenaar0186e582021-01-10 18:33:11 +01003467 // load or store variable or argument from outer scope
3468 case ISN_LOADOUTER:
3469 case ISN_STOREOUTER:
3470 {
3471 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003472 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3473 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003474
3475 while (depth > 1 && outer != NULL)
3476 {
3477 outer = outer->out_up;
3478 --depth;
3479 }
3480 if (outer == NULL)
3481 {
3482 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003483 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3484 || ectx->ec_outer_ref == NULL)
3485 // Possibly :def function called from legacy
3486 // context.
3487 emsg(_(e_closure_called_from_invalid_context));
3488 else
3489 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003490 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003491 }
3492 tv = ((typval_T *)outer->out_stack->ga_data)
3493 + outer->out_frame_idx + STACK_FRAME_SIZE
3494 + iptr->isn_arg.outer.outer_idx;
3495 if (iptr->isn_type == ISN_LOADOUTER)
3496 {
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 Moolenaar0186e582021-01-10 18:33:11 +01003499 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003500 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003501 }
3502 else
3503 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003504 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003505 clear_tv(tv);
3506 *tv = *STACK_TV_BOT(0);
3507 }
3508 }
3509 break;
3510
Bram Moolenaar752fc692021-01-04 21:57:11 +01003511 // unlet item in list or dict variable
3512 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003513 if (execute_unletindex(iptr, ectx) == FAIL)
3514 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003515 break;
3516
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003517 // unlet range of items in list variable
3518 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003519 if (execute_unletrange(iptr, ectx) == FAIL)
3520 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003521 break;
3522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 // push constant
3524 case ISN_PUSHNR:
3525 case ISN_PUSHBOOL:
3526 case ISN_PUSHSPEC:
3527 case ISN_PUSHF:
3528 case ISN_PUSHS:
3529 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003530 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003531 case ISN_PUSHCHANNEL:
3532 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003533 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003534 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003536 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003537 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 switch (iptr->isn_type)
3539 {
3540 case ISN_PUSHNR:
3541 tv->v_type = VAR_NUMBER;
3542 tv->vval.v_number = iptr->isn_arg.number;
3543 break;
3544 case ISN_PUSHBOOL:
3545 tv->v_type = VAR_BOOL;
3546 tv->vval.v_number = iptr->isn_arg.number;
3547 break;
3548 case ISN_PUSHSPEC:
3549 tv->v_type = VAR_SPECIAL;
3550 tv->vval.v_number = iptr->isn_arg.number;
3551 break;
3552#ifdef FEAT_FLOAT
3553 case ISN_PUSHF:
3554 tv->v_type = VAR_FLOAT;
3555 tv->vval.v_float = iptr->isn_arg.fnumber;
3556 break;
3557#endif
3558 case ISN_PUSHBLOB:
3559 blob_copy(iptr->isn_arg.blob, tv);
3560 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003561 case ISN_PUSHFUNC:
3562 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003563 if (iptr->isn_arg.string == NULL)
3564 tv->vval.v_string = NULL;
3565 else
3566 tv->vval.v_string =
3567 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003568 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003569 case ISN_PUSHCHANNEL:
3570#ifdef FEAT_JOB_CHANNEL
3571 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003572 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003573#endif
3574 break;
3575 case ISN_PUSHJOB:
3576#ifdef FEAT_JOB_CHANNEL
3577 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003578 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003579#endif
3580 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 default:
3582 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003583 tv->vval.v_string = iptr->isn_arg.string == NULL
3584 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 }
3586 break;
3587
Bram Moolenaar06b77222022-01-25 15:51:56 +00003588 case ISN_AUTOLOAD:
3589 {
3590 char_u *name = iptr->isn_arg.string;
3591
3592 (void)script_autoload(name, FALSE);
3593 if (find_func(name, TRUE))
3594 {
3595 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3596 goto theend;
3597 tv = STACK_TV_BOT(0);
3598 tv->v_lock = 0;
3599 ++ectx->ec_stack.ga_len;
3600 tv->v_type = VAR_FUNC;
3601 tv->vval.v_string = vim_strsave(name);
3602 }
3603 else
3604 {
3605 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3606
3607 if (res == NOTDONE)
3608 goto theend;
3609 if (res == FAIL)
3610 goto on_error;
3611 }
3612 }
3613 break;
3614
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003615 case ISN_UNLET:
3616 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3617 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003618 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003619 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003620 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003621 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003622 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003623
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003624 case ISN_LOCKUNLOCK:
3625 {
3626 typval_T *lval_root_save = lval_root;
3627 int res;
3628
3629 // Stack has the local variable, argument the whole :lock
3630 // or :unlock command, like ISN_EXEC.
3631 --ectx->ec_stack.ga_len;
3632 lval_root = STACK_TV_BOT(0);
3633 res = exec_command(iptr);
3634 clear_tv(lval_root);
3635 lval_root = lval_root_save;
3636 if (res == FAIL)
3637 goto on_error;
3638 }
3639 break;
3640
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003641 case ISN_LOCKCONST:
3642 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3643 break;
3644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 // create a list from items on the stack; uses a single allocation
3646 // for the list header and the items
3647 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003648 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003649 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 break;
3651
3652 // create a dict from items on the stack
3653 case ISN_NEWDICT:
3654 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003655 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003657 SOURCING_LNUM = iptr->isn_lnum;
3658 res = exe_newdict(iptr->isn_arg.number, ectx);
3659 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003660 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003661 if (res == MAYBE)
3662 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 }
3664 break;
3665
LemonBoy372bcce2022-04-25 12:43:20 +01003666 case ISN_CONCAT:
3667 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3668 goto theend;
3669 break;
3670
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003671 // create a partial with NULL value
3672 case ISN_NEWPARTIAL:
3673 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3674 goto theend;
3675 ++ectx->ec_stack.ga_len;
3676 tv = STACK_TV_BOT(-1);
3677 tv->v_type = VAR_PARTIAL;
3678 tv->v_lock = 0;
3679 tv->vval.v_partial = NULL;
3680 break;
3681
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 // call a :def function
3683 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003684 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003685 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3686 NULL,
3687 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003688 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003689 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 break;
3691
3692 // call a builtin function
3693 case ISN_BCALL:
3694 SOURCING_LNUM = iptr->isn_lnum;
3695 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3696 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003697 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003698 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 break;
3700
3701 // call a funcref or partial
3702 case ISN_PCALL:
3703 {
3704 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3705 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003706 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707
3708 SOURCING_LNUM = iptr->isn_lnum;
3709 if (pfunc->cpf_top)
3710 {
3711 // funcref is above the arguments
3712 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3713 }
3714 else
3715 {
3716 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003717 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003718 partial_tv = *STACK_TV_BOT(0);
3719 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003721 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003722 if (tv == &partial_tv)
3723 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003725 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 }
3727 break;
3728
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003729 case ISN_PCALL_END:
3730 // PCALL finished, arguments have been consumed and replaced by
3731 // the return value. Now clear the funcref from the stack,
3732 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003733 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003734 clear_tv(STACK_TV_BOT(-1));
3735 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3736 break;
3737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 // call a user defined function or funcref/partial
3739 case ISN_UCALL:
3740 {
3741 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3742
3743 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003744 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003745 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003746 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 }
3748 break;
3749
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003750 // :defer func(arg)
3751 case ISN_DEFER:
3752 if (add_defer_func(iptr->isn_arg.defer.defer_var_idx,
3753 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
3754 goto on_error;
3755 break;
3756
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003757 // return from a :def function call without a value
3758 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003759 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003760 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003761 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003762 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003763 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003764 tv->vval.v_number = 0;
3765 tv->v_lock = 0;
3766 // FALLTHROUGH
3767
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003768 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 case ISN_RETURN:
3770 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003771 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003772 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003773
3774 if (trystack->ga_len > 0)
3775 trycmd = ((trycmd_T *)trystack->ga_data)
3776 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003777 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003778 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003780 // jump to ":finally" or ":endtry"
3781 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003782 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003783 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003784 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 trycmd->tcd_return = TRUE;
3786 }
3787 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003788 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 }
3790 break;
3791
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003792 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 case ISN_FUNCREF:
3794 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003795 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003796 ufunc_T *ufunc;
3797 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003800 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003801 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003802 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003803 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003804 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003805 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003806 if (funcref->fr_func_name == NULL)
3807 {
3808 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3809 + funcref->fr_dfunc_idx;
3810
3811 ufunc = pt_dfunc->df_ufunc;
3812 }
3813 else
3814 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003815 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003816 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003817 if (ufunc == NULL)
3818 {
3819 SOURCING_LNUM = iptr->isn_lnum;
3820 iemsg("ufunc unexpectedly NULL for FUNCREF");
3821 goto theend;
3822 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003823 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003824 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003826 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 tv->vval.v_partial = pt;
3828 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003829 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 }
3831 break;
3832
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003833 // Create a global function from a lambda.
3834 case ISN_NEWFUNC:
3835 {
3836 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3837
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003838 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003839 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003840 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003841 }
3842 break;
3843
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003844 // List functions
3845 case ISN_DEF:
3846 if (iptr->isn_arg.string == NULL)
3847 list_functions(NULL);
3848 else
3849 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003850 exarg_T ea;
3851 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003852
3853 CLEAR_FIELD(ea);
3854 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003855 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3856 define_function(&ea, NULL, &lines_to_free);
3857 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003858 }
3859 break;
3860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 // jump if a condition is met
3862 case ISN_JUMP:
3863 {
3864 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003865 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 int jump = TRUE;
3867
3868 if (when != JUMP_ALWAYS)
3869 {
3870 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003871 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003872 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003873 || when == JUMP_IF_COND_TRUE)
3874 {
3875 SOURCING_LNUM = iptr->isn_lnum;
3876 jump = tv_get_bool_chk(tv, &error);
3877 if (error)
3878 goto on_error;
3879 }
3880 else
3881 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01003882 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003884 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 {
3886 // drop the value from the stack
3887 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003888 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 }
3890 }
3891 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003892 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 }
3894 break;
3895
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003896 // Jump if an argument with a default value was already set and not
3897 // v:none.
3898 case ISN_JUMP_IF_ARG_SET:
3899 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3900 if (tv->v_type != VAR_UNKNOWN
3901 && !(tv->v_type == VAR_SPECIAL
3902 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003903 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003904 break;
3905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 // top of a for loop
3907 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003908 if (execute_for(iptr, ectx) == FAIL)
3909 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 break;
3911
3912 // start of ":try" block
3913 case ISN_TRY:
3914 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003915 trycmd_T *trycmd = NULL;
3916
Bram Moolenaar35578162021-08-02 19:10:38 +02003917 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003918 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003919 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3920 + ectx->ec_trystack.ga_len;
3921 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003923 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003924 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3925 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003926 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003927 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003928 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003929 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003930 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003931 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 }
3933 break;
3934
3935 case ISN_PUSHEXC:
3936 if (current_exception == NULL)
3937 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003938 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003940 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003942 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003943 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003945 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003947 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 tv->vval.v_string = vim_strsave(
3949 (char_u *)current_exception->value);
3950 break;
3951
3952 case ISN_CATCH:
3953 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003954 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01003955 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956
Bram Moolenaar4c137212021-04-19 16:48:48 +02003957 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01003958 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01003960 trycmd->tcd_caught = TRUE;
3961 trycmd->tcd_did_throw = FALSE;
3962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003964 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 catch_exception(current_exception);
3966 }
3967 break;
3968
Bram Moolenaarc150c092021-02-13 15:02:46 +01003969 case ISN_TRYCONT:
3970 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003971 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003972 trycont_T *trycont = &iptr->isn_arg.trycont;
3973 int i;
3974 trycmd_T *trycmd;
3975 int iidx = trycont->tct_where;
3976
3977 if (trystack->ga_len < trycont->tct_levels)
3978 {
3979 siemsg("TRYCONT: expected %d levels, found %d",
3980 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003981 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003982 }
3983 // Make :endtry jump to any outer try block and the last
3984 // :endtry inside the loop to the loop start.
3985 for (i = trycont->tct_levels; i > 0; --i)
3986 {
3987 trycmd = ((trycmd_T *)trystack->ga_data)
3988 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003989 // Add one to tcd_cont to be able to jump to
3990 // instruction with index zero.
3991 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003992 iidx = trycmd->tcd_finally_idx == 0
3993 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003994 }
3995 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003996 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003997 }
3998 break;
3999
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004000 case ISN_FINALLY:
4001 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004002 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004003 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4004 + trystack->ga_len - 1;
4005
4006 // Reset the index to avoid a return statement jumps here
4007 // again.
4008 trycmd->tcd_finally_idx = 0;
4009 break;
4010 }
4011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 // end of ":try" block
4013 case ISN_ENDTRY:
4014 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004015 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004016 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017
Bram Moolenaar06651632022-04-27 17:54:25 +01004018 --trystack->ga_len;
4019 --trylevel;
4020 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4021 if (trycmd->tcd_did_throw)
4022 did_throw = TRUE;
4023 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004025 // discard the exception
4026 if (caught_stack == current_exception)
4027 caught_stack = caught_stack->caught;
4028 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004030
4031 if (trycmd->tcd_return)
4032 goto func_return;
4033
4034 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4035 {
4036 --ectx->ec_stack.ga_len;
4037 clear_tv(STACK_TV_BOT(0));
4038 }
4039 if (trycmd->tcd_cont != 0)
4040 // handling :continue: jump to outer try block or
4041 // start of the loop
4042 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 }
4044 break;
4045
4046 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004047 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004048 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004049
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004050 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4051 {
4052 // throwing an exception while using "silent!" causes
4053 // the function to abort but not display an error.
4054 tv = STACK_TV_BOT(-1);
4055 clear_tv(tv);
4056 tv->v_type = VAR_NUMBER;
4057 tv->vval.v_number = 0;
4058 goto done;
4059 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004060 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004061 tv = STACK_TV_BOT(0);
4062 if (tv->vval.v_string == NULL
4063 || *skipwhite(tv->vval.v_string) == NUL)
4064 {
4065 vim_free(tv->vval.v_string);
4066 SOURCING_LNUM = iptr->isn_lnum;
4067 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004068 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004069 }
4070
4071 // Inside a "catch" we need to first discard the caught
4072 // exception.
4073 if (trystack->ga_len > 0)
4074 {
4075 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4076 + trystack->ga_len - 1;
4077 if (trycmd->tcd_caught && current_exception != NULL)
4078 {
4079 // discard the exception
4080 if (caught_stack == current_exception)
4081 caught_stack = caught_stack->caught;
4082 discard_current_exception();
4083 trycmd->tcd_caught = FALSE;
4084 }
4085 }
4086
Bram Moolenaar90a57162022-02-12 14:23:17 +00004087 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004088 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4089 == FAIL)
4090 {
4091 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004092 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004093 }
4094 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 break;
4097
4098 // compare with special values
4099 case ISN_COMPAREBOOL:
4100 case ISN_COMPARESPECIAL:
4101 {
4102 typval_T *tv1 = STACK_TV_BOT(-2);
4103 typval_T *tv2 = STACK_TV_BOT(-1);
4104 varnumber_T arg1 = tv1->vval.v_number;
4105 varnumber_T arg2 = tv2->vval.v_number;
4106 int res;
4107
Bram Moolenaar06651632022-04-27 17:54:25 +01004108 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4109 res = arg1 == arg2;
4110 else
4111 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112
Bram Moolenaar4c137212021-04-19 16:48:48 +02004113 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 tv1->v_type = VAR_BOOL;
4115 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4116 }
4117 break;
4118
Bram Moolenaar7a222242022-03-01 19:23:24 +00004119 case ISN_COMPARENULL:
4120 {
4121 typval_T *tv1 = STACK_TV_BOT(-2);
4122 typval_T *tv2 = STACK_TV_BOT(-1);
4123 int res;
4124
4125 res = typval_compare_null(tv1, tv2);
4126 if (res == MAYBE)
4127 goto on_error;
4128 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4129 res = !res;
4130 clear_tv(tv1);
4131 clear_tv(tv2);
4132 --ectx->ec_stack.ga_len;
4133 tv1->v_type = VAR_BOOL;
4134 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4135 }
4136 break;
4137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 // Operation with two number arguments
4139 case ISN_OPNR:
4140 case ISN_COMPARENR:
4141 {
4142 typval_T *tv1 = STACK_TV_BOT(-2);
4143 typval_T *tv2 = STACK_TV_BOT(-1);
4144 varnumber_T arg1 = tv1->vval.v_number;
4145 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004146 varnumber_T res = 0;
4147 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004149 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4150 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4151 {
4152 if (arg2 < 0)
4153 {
4154 SOURCING_LNUM = iptr->isn_lnum;
4155 emsg(_(e_bitshift_ops_must_be_postive));
4156 goto on_error;
4157 }
4158 }
4159
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 switch (iptr->isn_arg.op.op_type)
4161 {
4162 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004163 case EXPR_DIV: if (arg2 == 0)
4164 div_zero = TRUE;
4165 else
4166 res = arg1 / arg2;
4167 break;
4168 case EXPR_REM: if (arg2 == 0)
4169 div_zero = TRUE;
4170 else
4171 res = arg1 % arg2;
4172 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 case EXPR_SUB: res = arg1 - arg2; break;
4174 case EXPR_ADD: res = arg1 + arg2; break;
4175
4176 case EXPR_EQUAL: res = arg1 == arg2; break;
4177 case EXPR_NEQUAL: res = arg1 != arg2; break;
4178 case EXPR_GREATER: res = arg1 > arg2; break;
4179 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4180 case EXPR_SMALLER: res = arg1 < arg2; break;
4181 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004182 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4183 res = 0;
4184 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004185 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004186 break;
4187 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4188 res = 0;
4189 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004190 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004191 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004192 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 }
4194
Bram Moolenaar4c137212021-04-19 16:48:48 +02004195 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 if (iptr->isn_type == ISN_COMPARENR)
4197 {
4198 tv1->v_type = VAR_BOOL;
4199 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4200 }
4201 else
4202 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004203 if (div_zero)
4204 {
4205 SOURCING_LNUM = iptr->isn_lnum;
4206 emsg(_(e_divide_by_zero));
4207 goto on_error;
4208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 }
4210 break;
4211
4212 // Computation with two float arguments
4213 case ISN_OPFLOAT:
4214 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004215#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 {
4217 typval_T *tv1 = STACK_TV_BOT(-2);
4218 typval_T *tv2 = STACK_TV_BOT(-1);
4219 float_T arg1 = tv1->vval.v_float;
4220 float_T arg2 = tv2->vval.v_float;
4221 float_T res = 0;
4222 int cmp = FALSE;
4223
4224 switch (iptr->isn_arg.op.op_type)
4225 {
4226 case EXPR_MULT: res = arg1 * arg2; break;
4227 case EXPR_DIV: res = arg1 / arg2; break;
4228 case EXPR_SUB: res = arg1 - arg2; break;
4229 case EXPR_ADD: res = arg1 + arg2; break;
4230
4231 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4232 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4233 case EXPR_GREATER: cmp = arg1 > arg2; break;
4234 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4235 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4236 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4237 default: cmp = 0; break;
4238 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004239 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 if (iptr->isn_type == ISN_COMPAREFLOAT)
4241 {
4242 tv1->v_type = VAR_BOOL;
4243 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4244 }
4245 else
4246 tv1->vval.v_float = res;
4247 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004248#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 break;
4250
4251 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004252 case ISN_COMPAREDICT:
4253 case ISN_COMPAREFUNC:
4254 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 case ISN_COMPAREBLOB:
4256 {
4257 typval_T *tv1 = STACK_TV_BOT(-2);
4258 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004259 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4260 int ic = iptr->isn_arg.op.op_ic;
4261 int res = FALSE;
4262 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263
Bram Moolenaar265f8112021-12-19 12:33:05 +00004264 SOURCING_LNUM = iptr->isn_lnum;
4265 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004267 status = typval_compare_list(tv1, tv2,
4268 exprtype, ic, &res);
4269 }
4270 else if (iptr->isn_type == ISN_COMPAREDICT)
4271 {
4272 status = typval_compare_dict(tv1, tv2,
4273 exprtype, ic, &res);
4274 }
4275 else if (iptr->isn_type == ISN_COMPAREFUNC)
4276 {
4277 status = typval_compare_func(tv1, tv2,
4278 exprtype, ic, &res);
4279 }
4280 else if (iptr->isn_type == ISN_COMPARESTRING)
4281 {
4282 status = typval_compare_string(tv1, tv2,
4283 exprtype, ic, &res);
4284 }
4285 else
4286 {
4287 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004289 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 clear_tv(tv1);
4291 clear_tv(tv2);
4292 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004293 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4294 if (status == FAIL)
4295 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 }
4297 break;
4298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 case ISN_COMPAREANY:
4300 {
4301 typval_T *tv1 = STACK_TV_BOT(-2);
4302 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004303 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004305 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306
Bram Moolenaareb26f432020-09-14 16:50:05 +02004307 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004308 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004310 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004311 if (status == FAIL)
4312 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 }
4314 break;
4315
4316 case ISN_ADDLIST:
4317 case ISN_ADDBLOB:
4318 {
4319 typval_T *tv1 = STACK_TV_BOT(-2);
4320 typval_T *tv2 = STACK_TV_BOT(-1);
4321
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004322 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004324 {
4325 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4326 && tv1->vval.v_list != NULL)
4327 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4328 NULL);
4329 else
4330 eval_addlist(tv1, tv2);
4331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 else
4333 eval_addblob(tv1, tv2);
4334 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004335 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 }
4337 break;
4338
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004339 case ISN_LISTAPPEND:
4340 {
4341 typval_T *tv1 = STACK_TV_BOT(-2);
4342 typval_T *tv2 = STACK_TV_BOT(-1);
4343 list_T *l = tv1->vval.v_list;
4344
4345 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004346 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004347 if (l == NULL)
4348 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004349 emsg(_(e_cannot_add_to_null_list));
4350 goto on_error;
4351 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004352 if (value_check_lock(l->lv_lock, NULL, FALSE))
4353 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004354 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004355 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004356 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004357 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004358 }
4359 break;
4360
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004361 case ISN_BLOBAPPEND:
4362 {
4363 typval_T *tv1 = STACK_TV_BOT(-2);
4364 typval_T *tv2 = STACK_TV_BOT(-1);
4365 blob_T *b = tv1->vval.v_blob;
4366 int error = FALSE;
4367 varnumber_T n;
4368
4369 // add a number to a blob
4370 if (b == NULL)
4371 {
4372 SOURCING_LNUM = iptr->isn_lnum;
4373 emsg(_(e_cannot_add_to_null_blob));
4374 goto on_error;
4375 }
4376 n = tv_get_number_chk(tv2, &error);
4377 if (error)
4378 goto on_error;
4379 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004380 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004381 }
4382 break;
4383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 // Computation with two arguments of unknown type
4385 case ISN_OPANY:
4386 {
4387 typval_T *tv1 = STACK_TV_BOT(-2);
4388 typval_T *tv2 = STACK_TV_BOT(-1);
4389 varnumber_T n1, n2;
4390#ifdef FEAT_FLOAT
4391 float_T f1 = 0, f2 = 0;
4392#endif
4393 int error = FALSE;
4394
4395 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4396 {
4397 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4398 {
4399 eval_addlist(tv1, tv2);
4400 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004401 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 break;
4403 }
4404 else if (tv1->v_type == VAR_BLOB
4405 && tv2->v_type == VAR_BLOB)
4406 {
4407 eval_addblob(tv1, tv2);
4408 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004409 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 break;
4411 }
4412 }
4413#ifdef FEAT_FLOAT
4414 if (tv1->v_type == VAR_FLOAT)
4415 {
4416 f1 = tv1->vval.v_float;
4417 n1 = 0;
4418 }
4419 else
4420#endif
4421 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004422 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 n1 = tv_get_number_chk(tv1, &error);
4424 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004425 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426#ifdef FEAT_FLOAT
4427 if (tv2->v_type == VAR_FLOAT)
4428 f1 = n1;
4429#endif
4430 }
4431#ifdef FEAT_FLOAT
4432 if (tv2->v_type == VAR_FLOAT)
4433 {
4434 f2 = tv2->vval.v_float;
4435 n2 = 0;
4436 }
4437 else
4438#endif
4439 {
4440 n2 = tv_get_number_chk(tv2, &error);
4441 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004442 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443#ifdef FEAT_FLOAT
4444 if (tv1->v_type == VAR_FLOAT)
4445 f2 = n2;
4446#endif
4447 }
4448#ifdef FEAT_FLOAT
4449 // if there is a float on either side the result is a float
4450 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4451 {
4452 switch (iptr->isn_arg.op.op_type)
4453 {
4454 case EXPR_MULT: f1 = f1 * f2; break;
4455 case EXPR_DIV: f1 = f1 / f2; break;
4456 case EXPR_SUB: f1 = f1 - f2; break;
4457 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004458 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004459 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004460 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 }
4462 clear_tv(tv1);
4463 clear_tv(tv2);
4464 tv1->v_type = VAR_FLOAT;
4465 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004466 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 }
4468 else
4469#endif
4470 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004471 int failed = FALSE;
4472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 switch (iptr->isn_arg.op.op_type)
4474 {
4475 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004476 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4477 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004478 goto on_error;
4479 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 case EXPR_SUB: n1 = n1 - n2; break;
4481 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004482 default: n1 = num_modulus(n1, n2, &failed);
4483 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004484 goto on_error;
4485 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 }
4487 clear_tv(tv1);
4488 clear_tv(tv2);
4489 tv1->v_type = VAR_NUMBER;
4490 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004491 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 }
4493 }
4494 break;
4495
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004496 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004497 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004498 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004499 int is_slice = iptr->isn_type == ISN_STRSLICE;
4500 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004501 char_u *res;
4502
4503 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004504 // string slice: string is at stack-3, first index at
4505 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004506 if (is_slice)
4507 {
4508 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004509 n1 = tv->vval.v_number;
4510 }
4511
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004512 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004513 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004514
Bram Moolenaar4c137212021-04-19 16:48:48 +02004515 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004516 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004517 if (is_slice)
4518 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004519 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004520 else
4521 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004522 // single character (including composing characters).
4523 // If the index is too big or negative the result is
4524 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004525 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004526 vim_free(tv->vval.v_string);
4527 tv->vval.v_string = res;
4528 }
4529 break;
4530
4531 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004532 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004533 case ISN_BLOBINDEX:
4534 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004536 int is_slice = iptr->isn_type == ISN_LISTSLICE
4537 || iptr->isn_type == ISN_BLOBSLICE;
4538 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4539 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004540 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004541 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542
4543 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004544 // list slice: list is at stack-3, indexes at stack-2 and
4545 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004546 // Same for blob.
4547 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548
4549 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004550 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004552
4553 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 {
Bram Moolenaared591872020-08-15 22:14:53 +02004555 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004556 n1 = tv->vval.v_number;
4557 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 }
Bram Moolenaared591872020-08-15 22:14:53 +02004559
Bram Moolenaar4c137212021-04-19 16:48:48 +02004560 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004561 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004562 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004563 if (is_blob)
4564 {
4565 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4566 n1, n2, FALSE, tv) == FAIL)
4567 goto on_error;
4568 }
4569 else
4570 {
4571 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4572 n1, n2, FALSE, tv, TRUE) == FAIL)
4573 goto on_error;
4574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 }
4576 break;
4577
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004578 case ISN_ANYINDEX:
4579 case ISN_ANYSLICE:
4580 {
4581 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4582 typval_T *var1, *var2;
4583 int res;
4584
4585 // index: composite is at stack-2, index at stack-1
4586 // slice: composite is at stack-3, indexes at stack-2 and
4587 // stack-1
4588 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004589 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004590 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4591 goto on_error;
4592 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4593 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004594 res = eval_index_inner(tv, is_slice, var1, var2,
4595 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004596 clear_tv(var1);
4597 if (is_slice)
4598 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004599 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004600 if (res == FAIL)
4601 goto on_error;
4602 }
4603 break;
4604
Bram Moolenaar9af78762020-06-16 11:34:42 +02004605 case ISN_SLICE:
4606 {
4607 list_T *list;
4608 int count = iptr->isn_arg.number;
4609
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004610 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004611 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004612 list = tv->vval.v_list;
4613
4614 // no error for short list, expect it to be checked earlier
4615 if (list != NULL && list->lv_len >= count)
4616 {
4617 list_T *newlist = list_slice(list,
4618 count, list->lv_len - 1);
4619
4620 if (newlist != NULL)
4621 {
4622 list_unref(list);
4623 tv->vval.v_list = newlist;
4624 ++newlist->lv_refcount;
4625 }
4626 }
4627 }
4628 break;
4629
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004630 case ISN_GETITEM:
4631 {
4632 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004633 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004634
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004635 // Get list item: list is at stack-1, push item.
4636 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004637 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4638 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004639
Bram Moolenaar35578162021-08-02 19:10:38 +02004640 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004641 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004642 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004643 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004644
4645 // Useful when used in unpack assignment. Reset at
4646 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004647 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004648 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004649 }
4650 break;
4651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 case ISN_MEMBER:
4653 {
4654 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004655 char_u *key;
4656 dictitem_T *di;
4657
4658 // dict member: dict is at stack-2, key at stack-1
4659 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004660 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004661 dict = tv->vval.v_dict;
4662
4663 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004664 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004665 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004666 if (key == NULL)
4667 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004668
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004669 if ((di = dict_find(dict, key, -1)) == NULL)
4670 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004671 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004672 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004673
4674 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004675 // stack contents makes sense and the dict stack is
4676 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004677 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004678 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004679 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004680 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004681 tv->v_type = VAR_NUMBER;
4682 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004683 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004684 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004685 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004686 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004687 // Put the dict used on the dict stack, it might be used by
4688 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004689 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004690 if (dict_stack_save(tv) == FAIL)
4691 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004692 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004693 }
4694 break;
4695
4696 // dict member with string key
4697 case ISN_STRINGMEMBER:
4698 {
4699 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 dictitem_T *di;
4701
4702 tv = STACK_TV_BOT(-1);
4703 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4704 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004705 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004706 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004707 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 }
4709 dict = tv->vval.v_dict;
4710
4711 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4712 == NULL)
4713 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004714 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004715 semsg(_(e_key_not_present_in_dictionary),
4716 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004717 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004719 // Put the dict used on the dict stack, it might be used by
4720 // a dict function later.
4721 if (dict_stack_save(tv) == FAIL)
4722 goto on_fatal_error;
4723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004725 }
4726 break;
4727
4728 case ISN_CLEARDICT:
4729 dict_stack_drop();
4730 break;
4731
4732 case ISN_USEDICT:
4733 {
4734 typval_T *dict_tv = dict_stack_get_tv();
4735
4736 // Turn "dict.Func" into a partial for "Func" bound to
4737 // "dict". Don't do this when "Func" is already a partial
4738 // that was bound explicitly (pt_auto is FALSE).
4739 tv = STACK_TV_BOT(-1);
4740 if (dict_tv != NULL
4741 && dict_tv->v_type == VAR_DICT
4742 && dict_tv->vval.v_dict != NULL
4743 && (tv->v_type == VAR_FUNC
4744 || (tv->v_type == VAR_PARTIAL
4745 && (tv->vval.v_partial->pt_auto
4746 || tv->vval.v_partial->pt_dict == NULL))))
4747 dict_tv->vval.v_dict =
4748 make_partial(dict_tv->vval.v_dict, tv);
4749 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750 }
4751 break;
4752
4753 case ISN_NEGATENR:
4754 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004755 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004756#ifdef FEAT_FLOAT
4757 if (tv->v_type == VAR_FLOAT)
4758 tv->vval.v_float = -tv->vval.v_float;
4759 else
4760#endif
4761 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 break;
4763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 case ISN_CHECKTYPE:
4765 {
4766 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004767 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004768 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004770 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004771 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004772 if (!ectx->ec_where.wt_variable)
4773 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004774 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004775 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004776 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004777 if (r == FAIL)
4778 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004779 if (!ectx->ec_where.wt_variable)
4780 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004781
4782 // number 0 is FALSE, number 1 is TRUE
4783 if (tv->v_type == VAR_NUMBER
4784 && ct->ct_type->tt_type == VAR_BOOL
4785 && (tv->vval.v_number == 0
4786 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004788 tv->v_type = VAR_BOOL;
4789 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004790 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 }
4792 }
4793 break;
4794
Bram Moolenaar9af78762020-06-16 11:34:42 +02004795 case ISN_CHECKLEN:
4796 {
4797 int min_len = iptr->isn_arg.checklen.cl_min_len;
4798 list_T *list = NULL;
4799
4800 tv = STACK_TV_BOT(-1);
4801 if (tv->v_type == VAR_LIST)
4802 list = tv->vval.v_list;
4803 if (list == NULL || list->lv_len < min_len
4804 || (list->lv_len > min_len
4805 && !iptr->isn_arg.checklen.cl_more_OK))
4806 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004807 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004808 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004809 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004810 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004811 }
4812 }
4813 break;
4814
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004815 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004816 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004817 break;
4818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004820 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 {
4822 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004823 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004825 if (iptr->isn_type == ISN_2BOOL)
4826 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004827 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004828 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004829 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004830 n = !n;
4831 }
4832 else
4833 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004834 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004835 SOURCING_LNUM = iptr->isn_lnum;
4836 n = tv_get_bool_chk(tv, &error);
4837 if (error)
4838 goto on_error;
4839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 clear_tv(tv);
4841 tv->v_type = VAR_BOOL;
4842 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4843 }
4844 break;
4845
4846 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004847 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004848 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004849 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4850 iptr->isn_type == ISN_2STRING_ANY,
4851 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004852 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 break;
4854
Bram Moolenaar08597872020-12-10 19:43:40 +01004855 case ISN_RANGE:
4856 {
4857 exarg_T ea;
4858 char *errormsg;
4859
Bram Moolenaarece0b872021-01-08 20:40:45 +01004860 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004861 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004862 ea.addr_type = ADDR_LINES;
4863 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004864 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004865 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004866 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004867
Bram Moolenaar35578162021-08-02 19:10:38 +02004868 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004869 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004870 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004871 tv = STACK_TV_BOT(-1);
4872 tv->v_type = VAR_NUMBER;
4873 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004874 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004875 }
4876 break;
4877
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004878 case ISN_PUT:
4879 {
4880 int regname = iptr->isn_arg.put.put_regname;
4881 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4882 char_u *expr = NULL;
4883 int dir = FORWARD;
4884
Bram Moolenaar08597872020-12-10 19:43:40 +01004885 if (lnum < -2)
4886 {
4887 // line number was put on the stack by ISN_RANGE
4888 tv = STACK_TV_BOT(-1);
4889 curwin->w_cursor.lnum = tv->vval.v_number;
4890 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4891 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004892 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004893 }
4894 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004895 // :put! above cursor
4896 dir = BACKWARD;
4897 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004898 {
4899 curwin->w_cursor.lnum = lnum;
4900 if (lnum == 0)
4901 // check_cursor() below will move to line 1
4902 dir = BACKWARD;
4903 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004904
4905 if (regname == '=')
4906 {
4907 tv = STACK_TV_BOT(-1);
4908 if (tv->v_type == VAR_STRING)
4909 expr = tv->vval.v_string;
4910 else
4911 {
4912 expr = typval2string(tv, TRUE); // allocates value
4913 clear_tv(tv);
4914 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004915 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004916 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004917 check_cursor();
4918 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4919 vim_free(expr);
4920 }
4921 break;
4922
Bram Moolenaar02194d22020-10-24 23:08:38 +02004923 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004924 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4925 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4926 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4927 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004928 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4929 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004930 break;
4931
Bram Moolenaar02194d22020-10-24 23:08:38 +02004932 case ISN_CMDMOD_REV:
4933 // filter regprog is owned by the instruction, don't free it
4934 cmdmod.cmod_filter_regmatch.regprog = NULL;
4935 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004936 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4937 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004938 break;
4939
Bram Moolenaar792f7862020-11-23 08:31:18 +01004940 case ISN_UNPACK:
4941 {
4942 int count = iptr->isn_arg.unpack.unp_count;
4943 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4944 list_T *l;
4945 listitem_T *li;
4946 int i;
4947
4948 // Check there is a valid list to unpack.
4949 tv = STACK_TV_BOT(-1);
4950 if (tv->v_type != VAR_LIST)
4951 {
4952 SOURCING_LNUM = iptr->isn_lnum;
4953 emsg(_(e_for_argument_must_be_sequence_of_lists));
4954 goto on_error;
4955 }
4956 l = tv->vval.v_list;
4957 if (l == NULL
4958 || l->lv_len < (semicolon ? count - 1 : count))
4959 {
4960 SOURCING_LNUM = iptr->isn_lnum;
4961 emsg(_(e_list_value_does_not_have_enough_items));
4962 goto on_error;
4963 }
4964 else if (!semicolon && l->lv_len > count)
4965 {
4966 SOURCING_LNUM = iptr->isn_lnum;
4967 emsg(_(e_list_value_has_more_items_than_targets));
4968 goto on_error;
4969 }
4970
4971 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004972 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004973 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004974 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004975
4976 // Variable after semicolon gets a list with the remaining
4977 // items.
4978 if (semicolon)
4979 {
4980 list_T *rem_list =
4981 list_alloc_with_items(l->lv_len - count + 1);
4982
4983 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004984 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004985 tv = STACK_TV_BOT(-count);
4986 tv->vval.v_list = rem_list;
4987 ++rem_list->lv_refcount;
4988 tv->v_lock = 0;
4989 li = l->lv_first;
4990 for (i = 0; i < count - 1; ++i)
4991 li = li->li_next;
4992 for (i = 0; li != NULL; ++i)
4993 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004994 typval_T tvcopy;
4995
4996 copy_tv(&li->li_tv, &tvcopy);
4997 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004998 li = li->li_next;
4999 }
5000 --count;
5001 }
5002
5003 // Produce the values in reverse order, first item last.
5004 li = l->lv_first;
5005 for (i = 0; i < count; ++i)
5006 {
5007 tv = STACK_TV_BOT(-i - 1);
5008 copy_tv(&li->li_tv, tv);
5009 li = li->li_next;
5010 }
5011
5012 list_unref(l);
5013 }
5014 break;
5015
Bram Moolenaarb2049902021-01-24 12:53:53 +01005016 case ISN_PROF_START:
5017 case ISN_PROF_END:
5018 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005019#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005020 funccall_T cookie;
5021 ufunc_T *cur_ufunc =
5022 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005023 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005024
5025 cookie.func = cur_ufunc;
5026 if (iptr->isn_type == ISN_PROF_START)
5027 {
5028 func_line_start(&cookie, iptr->isn_lnum);
5029 // if we get here the instruction is executed
5030 func_line_exec(&cookie);
5031 }
5032 else
5033 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005034#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005035 }
5036 break;
5037
Bram Moolenaare99d4222021-06-13 14:01:26 +02005038 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005039 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005040 break;
5041
Bram Moolenaar389df252020-07-09 21:20:47 +02005042 case ISN_SHUFFLE:
5043 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005044 typval_T tmp_tv;
5045 int item = iptr->isn_arg.shuffle.shfl_item;
5046 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005047
5048 tmp_tv = *STACK_TV_BOT(-item);
5049 for ( ; up > 0 && item > 1; --up)
5050 {
5051 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5052 --item;
5053 }
5054 *STACK_TV_BOT(-item) = tmp_tv;
5055 }
5056 break;
5057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005059 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005061 ectx->ec_where.wt_index = 0;
5062 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063 break;
5064 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005065 continue;
5066
Bram Moolenaard032f342020-07-18 18:13:02 +02005067func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005068 // Restore previous function. If the frame pointer is where we started
5069 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005070 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005071 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005072
Bram Moolenaar4c137212021-04-19 16:48:48 +02005073 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005074 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005075 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005076 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005077
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005078on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005079 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005080 // If "emsg_silent" is set then ignore the error, unless it was set
5081 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005082 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005083 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005084 {
5085 // If a sequence of instructions causes an error while ":silent!"
5086 // was used, restore the stack length and jump ahead to restoring
5087 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005088 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005089 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005090 while (ectx->ec_stack.ga_len
5091 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005092 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005093 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005094 clear_tv(STACK_TV_BOT(0));
5095 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005096 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5097 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005098 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005099 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005100 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005101on_fatal_error:
5102 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005103 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005104 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005105 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106 }
5107
5108done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005109 ret = OK;
5110theend:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005111 {
5112 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5113 + ectx->ec_dfunc_idx;
5114
5115 if (dfunc->df_defer_var_idx > 0)
5116 invoke_defer_funcs(ectx);
5117 }
5118
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005119 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005120 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5121 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005122}
5123
5124/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005125 * Execute the instructions from a VAR_INSTR typeval and put the result in
5126 * "rettv".
5127 * Return OK or FAIL.
5128 */
5129 int
5130exe_typval_instr(typval_T *tv, typval_T *rettv)
5131{
5132 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5133 isn_T *save_instr = ectx->ec_instr;
5134 int save_iidx = ectx->ec_iidx;
5135 int res;
5136
LemonBoyf3b48952022-05-05 13:53:03 +01005137 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5138 // even when the compilation fails.
5139 rettv->v_type = VAR_UNKNOWN;
5140
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005141 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5142 res = exec_instructions(ectx);
5143 if (res == OK)
5144 {
5145 *rettv = *STACK_TV_BOT(-1);
5146 --ectx->ec_stack.ga_len;
5147 }
5148
5149 ectx->ec_instr = save_instr;
5150 ectx->ec_iidx = save_iidx;
5151
5152 return res;
5153}
5154
5155/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005156 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5157 * "substitute_instr".
5158 */
5159 char_u *
5160exe_substitute_instr(void)
5161{
5162 ectx_T *ectx = substitute_instr->subs_ectx;
5163 isn_T *save_instr = ectx->ec_instr;
5164 int save_iidx = ectx->ec_iidx;
5165 char_u *res;
5166
5167 ectx->ec_instr = substitute_instr->subs_instr;
5168 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005169 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005170 typval_T *tv = STACK_TV_BOT(-1);
5171
Bram Moolenaar27523602021-06-05 21:36:19 +02005172 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005173 --ectx->ec_stack.ga_len;
5174 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005175 }
5176 else
5177 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005178 substitute_instr->subs_status = FAIL;
5179 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181
Bram Moolenaar4c137212021-04-19 16:48:48 +02005182 ectx->ec_instr = save_instr;
5183 ectx->ec_iidx = save_iidx;
5184
5185 return res;
5186}
5187
5188/*
5189 * Call a "def" function from old Vim script.
5190 * Return OK or FAIL.
5191 */
5192 int
5193call_def_function(
5194 ufunc_T *ufunc,
5195 int argc_arg, // nr of arguments
5196 typval_T *argv, // arguments
5197 partial_T *partial, // optional partial for context
5198 typval_T *rettv) // return value
5199{
5200 ectx_T ectx; // execution context
5201 int argc = argc_arg;
5202 typval_T *tv;
5203 int idx;
5204 int ret = FAIL;
5205 int defcount = ufunc->uf_args.ga_len - argc;
5206 sctx_T save_current_sctx = current_sctx;
5207 int did_emsg_before = did_emsg_cumul + did_emsg;
5208 int save_suppress_errthrow = suppress_errthrow;
5209 msglist_T **saved_msg_list = NULL;
5210 msglist_T *private_msg_list = NULL;
5211 int save_emsg_silent_def = emsg_silent_def;
5212 int save_did_emsg_def = did_emsg_def;
5213 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005214 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005215
5216// Get pointer to item in the stack.
5217#undef STACK_TV
5218#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5219
5220// Get pointer to item at the bottom of the stack, -1 is the bottom.
5221#undef STACK_TV_BOT
5222#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5223
5224// Get pointer to a local variable on the stack. Negative for arguments.
5225#undef STACK_TV_VAR
5226#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5227
5228 if (ufunc->uf_def_status == UF_NOT_COMPILED
5229 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005230 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5231 && compile_def_function(ufunc, FALSE,
5232 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005233 {
5234 if (did_emsg_cumul + did_emsg == did_emsg_before)
5235 semsg(_(e_function_is_not_compiled_str),
5236 printable_func_name(ufunc));
5237 return FAIL;
5238 }
5239
5240 {
5241 // Check the function was really compiled.
5242 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5243 + ufunc->uf_dfunc_idx;
5244 if (INSTRUCTIONS(dfunc) == NULL)
5245 {
5246 iemsg("using call_def_function() on not compiled function");
5247 return FAIL;
5248 }
5249 }
5250
5251 // If depth of calling is getting too high, don't execute the function.
5252 orig_funcdepth = funcdepth_get();
5253 if (funcdepth_increment() == FAIL)
5254 return FAIL;
5255
5256 CLEAR_FIELD(ectx);
5257 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5258 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005259 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005260 {
5261 funcdepth_decrement();
5262 return FAIL;
5263 }
5264 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5265 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5266 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005267 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005268
5269 idx = argc - ufunc->uf_args.ga_len;
5270 if (idx > 0 && ufunc->uf_va_name == NULL)
5271 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005272 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
5273 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005274 goto failed_early;
5275 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005276 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5277 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005278 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005279 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
5280 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005281 goto failed_early;
5282 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005283
5284 // Put arguments on the stack, but no more than what the function expects.
5285 // A lambda can be called with more arguments than it uses.
5286 for (idx = 0; idx < argc
5287 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5288 ++idx)
5289 {
5290 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5291 && argv[idx].v_type == VAR_SPECIAL
5292 && argv[idx].vval.v_number == VVAL_NONE)
5293 {
5294 // Use the default value.
5295 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5296 }
5297 else
5298 {
5299 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5300 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005301 ufunc->uf_arg_types[idx], &argv[idx],
5302 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005303 goto failed_early;
5304 copy_tv(&argv[idx], STACK_TV_BOT(0));
5305 }
5306 ++ectx.ec_stack.ga_len;
5307 }
5308
5309 // Turn varargs into a list. Empty list if no args.
5310 if (ufunc->uf_va_name != NULL)
5311 {
5312 int vararg_count = argc - ufunc->uf_args.ga_len;
5313
5314 if (vararg_count < 0)
5315 vararg_count = 0;
5316 else
5317 argc -= vararg_count;
5318 if (exe_newlist(vararg_count, &ectx) == FAIL)
5319 goto failed_early;
5320
5321 // Check the type of the list items.
5322 tv = STACK_TV_BOT(-1);
5323 if (ufunc->uf_va_type != NULL
5324 && ufunc->uf_va_type != &t_list_any
5325 && ufunc->uf_va_type->tt_member != &t_any
5326 && tv->vval.v_list != NULL)
5327 {
5328 type_T *expected = ufunc->uf_va_type->tt_member;
5329 listitem_T *li = tv->vval.v_list->lv_first;
5330
5331 for (idx = 0; idx < vararg_count; ++idx)
5332 {
5333 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005334 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005335 goto failed_early;
5336 li = li->li_next;
5337 }
5338 }
5339
5340 if (defcount > 0)
5341 // Move varargs list to below missing default arguments.
5342 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5343 --ectx.ec_stack.ga_len;
5344 }
5345
5346 // Make space for omitted arguments, will store default value below.
5347 // Any varargs list goes after them.
5348 if (defcount > 0)
5349 for (idx = 0; idx < defcount; ++idx)
5350 {
5351 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5352 ++ectx.ec_stack.ga_len;
5353 }
5354 if (ufunc->uf_va_name != NULL)
5355 ++ectx.ec_stack.ga_len;
5356
5357 // Frame pointer points to just after arguments.
5358 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5359 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5360
5361 {
5362 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5363 + ufunc->uf_dfunc_idx;
5364 ufunc_T *base_ufunc = dfunc->df_ufunc;
5365
5366 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5367 // by copy_func().
5368 if (partial != NULL || base_ufunc->uf_partial != NULL)
5369 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005370 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5371 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005372 goto failed_early;
5373 if (partial != NULL)
5374 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005375 outer_T *outer = get_pt_outer(partial);
5376
5377 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005378 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005379 if (current_ectx != NULL)
5380 {
5381 if (current_ectx->ec_outer_ref != NULL
5382 && current_ectx->ec_outer_ref->or_outer != NULL)
5383 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005384 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005385 }
5386 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005387 }
5388 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005389 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005390 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005391 ++partial->pt_refcount;
5392 ectx.ec_outer_ref->or_partial = partial;
5393 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005394 }
5395 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005396 {
5397 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5398 ++base_ufunc->uf_partial->pt_refcount;
5399 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5400 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005401 }
5402 }
5403
5404 // dummy frame entries
5405 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5406 {
5407 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5408 ++ectx.ec_stack.ga_len;
5409 }
5410
5411 {
5412 // Reserve space for local variables and any closure reference count.
5413 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5414 + ufunc->uf_dfunc_idx;
5415
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005416 // Initialize variables to zero. That avoids having to generate
5417 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005418 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005419 {
5420 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5421 STACK_TV_VAR(idx)->vval.v_number = 0;
5422 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005423 ectx.ec_stack.ga_len += dfunc->df_varcount;
5424 if (dfunc->df_has_closure)
5425 {
5426 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5427 STACK_TV_VAR(idx)->vval.v_number = 0;
5428 ++ectx.ec_stack.ga_len;
5429 }
5430
5431 ectx.ec_instr = INSTRUCTIONS(dfunc);
5432 }
5433
5434 // Following errors are in the function, not the caller.
5435 // Commands behave like vim9script.
5436 estack_push_ufunc(ufunc, 1);
5437 current_sctx = ufunc->uf_script_ctx;
5438 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5439
5440 // Use a specific location for storing error messages to be converted to an
5441 // exception.
5442 saved_msg_list = msg_list;
5443 msg_list = &private_msg_list;
5444
5445 // Do turn errors into exceptions.
5446 suppress_errthrow = FALSE;
5447
5448 // Do not delete the function while executing it.
5449 ++ufunc->uf_calls;
5450
5451 // When ":silent!" was used before calling then we still abort the
5452 // function. If ":silent!" is used in the function then we don't.
5453 emsg_silent_def = emsg_silent;
5454 did_emsg_def = 0;
5455
5456 ectx.ec_where.wt_index = 0;
5457 ectx.ec_where.wt_variable = FALSE;
5458
5459 // Execute the instructions until done.
5460 ret = exec_instructions(&ectx);
5461 if (ret == OK)
5462 {
5463 // function finished, get result from the stack.
5464 if (ufunc->uf_ret_type == &t_void)
5465 {
5466 rettv->v_type = VAR_VOID;
5467 }
5468 else
5469 {
5470 tv = STACK_TV_BOT(-1);
5471 *rettv = *tv;
5472 tv->v_type = VAR_UNKNOWN;
5473 }
5474 }
5475
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005476 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005477 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5478 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005479
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005480 // Deal with any remaining closures, they may be in use somewhere.
5481 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005482 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005483 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005484 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005485 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005486
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005487 estack_pop();
5488 current_sctx = save_current_sctx;
5489
Bram Moolenaar2336c372021-12-06 15:06:54 +00005490 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5491 // Function was unreferenced while being used, free it now.
5492 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005493
Bram Moolenaar352134b2020-10-17 22:04:08 +02005494 if (*msg_list != NULL && saved_msg_list != NULL)
5495 {
5496 msglist_T **plist = saved_msg_list;
5497
5498 // Append entries from the current msg_list (uncaught exceptions) to
5499 // the saved msg_list.
5500 while (*plist != NULL)
5501 plist = &(*plist)->next;
5502
5503 *plist = *msg_list;
5504 }
5505 msg_list = saved_msg_list;
5506
Bram Moolenaar4c137212021-04-19 16:48:48 +02005507 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005508 {
5509 cmdmod.cmod_filter_regmatch.regprog = NULL;
5510 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005511 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005512 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005513 emsg_silent_def = save_emsg_silent_def;
5514 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005515
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005516failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005517 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005518 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5519 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005520 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005522 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005523 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005524 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005525 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005526 if (ectx.ec_outer_ref->or_outer_allocated)
5527 vim_free(ectx.ec_outer_ref->or_outer);
5528 partial_unref(ectx.ec_outer_ref->or_partial);
5529 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005530 }
5531
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005532 // Not sure if this is necessary.
5533 suppress_errthrow = save_suppress_errthrow;
5534
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005535 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5536 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005537 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005538 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005539 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005540 return ret;
5541}
5542
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005543/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005544 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5545 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5546 * "pfx" is prefixed to every line.
5547 */
5548 static void
5549list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5550{
5551 int line_idx = 0;
5552 int prev_current = 0;
5553 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005554 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005555
5556 for (current = 0; current < instr_count; ++current)
5557 {
5558 isn_T *iptr = &instr[current];
5559 char *line;
5560
5561 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005562 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005563 while (line_idx < iptr->isn_lnum
5564 && line_idx < ufunc->uf_lines.ga_len)
5565 {
5566 if (current > prev_current)
5567 {
5568 msg_puts("\n\n");
5569 prev_current = current;
5570 }
5571 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5572 if (line != NULL)
5573 msg(line);
5574 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005575 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5576 {
5577 int first_def_arg = ufunc->uf_args.ga_len
5578 - ufunc->uf_def_args.ga_len;
5579
5580 if (def_arg_idx > 0)
5581 msg_puts("\n\n");
5582 msg_start();
5583 msg_puts(" ");
5584 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5585 first_def_arg + def_arg_idx]);
5586 msg_puts(" = ");
5587 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5588 msg_clr_eos();
5589 msg_end();
5590 }
5591 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005592
5593 switch (iptr->isn_type)
5594 {
5595 case ISN_EXEC:
5596 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5597 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005598 case ISN_EXEC_SPLIT:
5599 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5600 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005601 case ISN_EXECRANGE:
5602 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5603 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005604 case ISN_LEGACY_EVAL:
5605 smsg("%s%4d EVAL legacy %s", pfx, current,
5606 iptr->isn_arg.string);
5607 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005608 case ISN_REDIRSTART:
5609 smsg("%s%4d REDIR", pfx, current);
5610 break;
5611 case ISN_REDIREND:
5612 smsg("%s%4d REDIR END%s", pfx, current,
5613 iptr->isn_arg.number ? " append" : "");
5614 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005615 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005616#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005617 smsg("%s%4d CEXPR pre %s", pfx, current,
5618 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005619#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005620 break;
5621 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005622#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005623 {
5624 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5625
5626 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5627 cexpr_get_auname(cer->cer_cmdidx),
5628 cer->cer_forceit ? "!" : "",
5629 cer->cer_cmdline);
5630 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005631#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005632 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005633 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005634 smsg("%s%4d INSTR", pfx, current);
5635 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5636 msg(" -------------");
5637 break;
5638 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005639 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005640 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5641
5642 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005643 }
5644 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005645 case ISN_SUBSTITUTE:
5646 {
5647 subs_T *subs = &iptr->isn_arg.subs;
5648
5649 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5650 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5651 msg(" -------------");
5652 }
5653 break;
5654 case ISN_EXECCONCAT:
5655 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5656 (varnumber_T)iptr->isn_arg.number);
5657 break;
5658 case ISN_ECHO:
5659 {
5660 echo_T *echo = &iptr->isn_arg.echo;
5661
5662 smsg("%s%4d %s %d", pfx, current,
5663 echo->echo_with_white ? "ECHO" : "ECHON",
5664 echo->echo_count);
5665 }
5666 break;
5667 case ISN_EXECUTE:
5668 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005669 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005670 break;
5671 case ISN_ECHOMSG:
5672 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005673 (varnumber_T)(iptr->isn_arg.number));
5674 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01005675 case ISN_ECHOWINDOW:
5676 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
5677 (varnumber_T)(iptr->isn_arg.number));
5678 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02005679 case ISN_ECHOCONSOLE:
5680 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5681 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005682 break;
5683 case ISN_ECHOERR:
5684 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005685 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005686 break;
5687 case ISN_LOAD:
5688 {
5689 if (iptr->isn_arg.number < 0)
5690 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5691 (varnumber_T)(iptr->isn_arg.number
5692 + STACK_FRAME_SIZE));
5693 else
5694 smsg("%s%4d LOAD $%lld", pfx, current,
5695 (varnumber_T)(iptr->isn_arg.number));
5696 }
5697 break;
5698 case ISN_LOADOUTER:
5699 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005700 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005701 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5702 iptr->isn_arg.outer.outer_depth,
5703 iptr->isn_arg.outer.outer_idx
5704 + STACK_FRAME_SIZE);
5705 else
5706 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5707 iptr->isn_arg.outer.outer_depth,
5708 iptr->isn_arg.outer.outer_idx);
5709 }
5710 break;
5711 case ISN_LOADV:
5712 smsg("%s%4d LOADV v:%s", pfx, current,
5713 get_vim_var_name(iptr->isn_arg.number));
5714 break;
5715 case ISN_LOADSCRIPT:
5716 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005717 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5718 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5719 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005720
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005721 sv = get_script_svar(sref, -1);
5722 if (sv == NULL)
5723 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5724 pfx, current, si->sn_name);
5725 else
5726 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005727 sv->sv_name,
5728 sref->sref_idx,
5729 si->sn_name);
5730 }
5731 break;
5732 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005733 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005734 {
5735 scriptitem_T *si = SCRIPT_ITEM(
5736 iptr->isn_arg.loadstore.ls_sid);
5737
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005738 smsg("%s%4d %s s:%s from %s", pfx, current,
5739 iptr->isn_type == ISN_LOADS ? "LOADS"
5740 : "LOADEXPORT",
5741 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005742 }
5743 break;
5744 case ISN_LOADAUTO:
5745 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5746 break;
5747 case ISN_LOADG:
5748 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5749 break;
5750 case ISN_LOADB:
5751 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5752 break;
5753 case ISN_LOADW:
5754 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5755 break;
5756 case ISN_LOADT:
5757 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5758 break;
5759 case ISN_LOADGDICT:
5760 smsg("%s%4d LOAD g:", pfx, current);
5761 break;
5762 case ISN_LOADBDICT:
5763 smsg("%s%4d LOAD b:", pfx, current);
5764 break;
5765 case ISN_LOADWDICT:
5766 smsg("%s%4d LOAD w:", pfx, current);
5767 break;
5768 case ISN_LOADTDICT:
5769 smsg("%s%4d LOAD t:", pfx, current);
5770 break;
5771 case ISN_LOADOPT:
5772 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5773 break;
5774 case ISN_LOADENV:
5775 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5776 break;
5777 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005778 smsg("%s%4d LOADREG @%c", pfx, current,
5779 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005780 break;
5781
5782 case ISN_STORE:
5783 if (iptr->isn_arg.number < 0)
5784 smsg("%s%4d STORE arg[%lld]", pfx, current,
5785 iptr->isn_arg.number + STACK_FRAME_SIZE);
5786 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005787 smsg("%s%4d STORE $%lld", pfx, current,
5788 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005789 break;
5790 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005791 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5792 iptr->isn_arg.outer.outer_depth,
5793 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005794 break;
5795 case ISN_STOREV:
5796 smsg("%s%4d STOREV v:%s", pfx, current,
5797 get_vim_var_name(iptr->isn_arg.number));
5798 break;
5799 case ISN_STOREAUTO:
5800 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5801 break;
5802 case ISN_STOREG:
5803 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5804 break;
5805 case ISN_STOREB:
5806 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5807 break;
5808 case ISN_STOREW:
5809 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5810 break;
5811 case ISN_STORET:
5812 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5813 break;
5814 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005815 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005816 {
5817 scriptitem_T *si = SCRIPT_ITEM(
5818 iptr->isn_arg.loadstore.ls_sid);
5819
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005820 smsg("%s%4d %s %s in %s", pfx, current,
5821 iptr->isn_type == ISN_STORES
5822 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005823 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5824 }
5825 break;
5826 case ISN_STORESCRIPT:
5827 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005828 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5829 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5830 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005831
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005832 sv = get_script_svar(sref, -1);
5833 if (sv == NULL)
5834 smsg("%s%4d STORESCRIPT [deleted] in %s",
5835 pfx, current, si->sn_name);
5836 else
5837 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005838 sv->sv_name,
5839 sref->sref_idx,
5840 si->sn_name);
5841 }
5842 break;
5843 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005844 case ISN_STOREFUNCOPT:
5845 smsg("%s%4d %s &%s", pfx, current,
5846 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005847 iptr->isn_arg.storeopt.so_name);
5848 break;
5849 case ISN_STOREENV:
5850 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5851 break;
5852 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005853 smsg("%s%4d STOREREG @%c", pfx, current,
5854 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005855 break;
5856 case ISN_STORENR:
5857 smsg("%s%4d STORE %lld in $%d", pfx, current,
5858 iptr->isn_arg.storenr.stnr_val,
5859 iptr->isn_arg.storenr.stnr_idx);
5860 break;
5861
5862 case ISN_STOREINDEX:
5863 smsg("%s%4d STOREINDEX %s", pfx, current,
5864 vartype_name(iptr->isn_arg.vartype));
5865 break;
5866
5867 case ISN_STORERANGE:
5868 smsg("%s%4d STORERANGE", pfx, current);
5869 break;
5870
5871 // constants
5872 case ISN_PUSHNR:
5873 smsg("%s%4d PUSHNR %lld", pfx, current,
5874 (varnumber_T)(iptr->isn_arg.number));
5875 break;
5876 case ISN_PUSHBOOL:
5877 case ISN_PUSHSPEC:
5878 smsg("%s%4d PUSH %s", pfx, current,
5879 get_var_special_name(iptr->isn_arg.number));
5880 break;
5881 case ISN_PUSHF:
5882#ifdef FEAT_FLOAT
5883 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5884#endif
5885 break;
5886 case ISN_PUSHS:
5887 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5888 break;
5889 case ISN_PUSHBLOB:
5890 {
5891 char_u *r;
5892 char_u numbuf[NUMBUFLEN];
5893 char_u *tofree;
5894
5895 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5896 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5897 vim_free(tofree);
5898 }
5899 break;
5900 case ISN_PUSHFUNC:
5901 {
5902 char *name = (char *)iptr->isn_arg.string;
5903
5904 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5905 name == NULL ? "[none]" : name);
5906 }
5907 break;
5908 case ISN_PUSHCHANNEL:
5909#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005910 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005911#endif
5912 break;
5913 case ISN_PUSHJOB:
5914#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005915 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005916#endif
5917 break;
5918 case ISN_PUSHEXC:
5919 smsg("%s%4d PUSH v:exception", pfx, current);
5920 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005921 case ISN_AUTOLOAD:
5922 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5923 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005924 case ISN_UNLET:
5925 smsg("%s%4d UNLET%s %s", pfx, current,
5926 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5927 iptr->isn_arg.unlet.ul_name);
5928 break;
5929 case ISN_UNLETENV:
5930 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5931 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5932 iptr->isn_arg.unlet.ul_name);
5933 break;
5934 case ISN_UNLETINDEX:
5935 smsg("%s%4d UNLETINDEX", pfx, current);
5936 break;
5937 case ISN_UNLETRANGE:
5938 smsg("%s%4d UNLETRANGE", pfx, current);
5939 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005940 case ISN_LOCKUNLOCK:
5941 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5942 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005943 case ISN_LOCKCONST:
5944 smsg("%s%4d LOCKCONST", pfx, current);
5945 break;
5946 case ISN_NEWLIST:
5947 smsg("%s%4d NEWLIST size %lld", pfx, current,
5948 (varnumber_T)(iptr->isn_arg.number));
5949 break;
5950 case ISN_NEWDICT:
5951 smsg("%s%4d NEWDICT size %lld", pfx, current,
5952 (varnumber_T)(iptr->isn_arg.number));
5953 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005954 case ISN_NEWPARTIAL:
5955 smsg("%s%4d NEWPARTIAL", pfx, current);
5956 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005957
5958 // function call
5959 case ISN_BCALL:
5960 {
5961 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5962
5963 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5964 internal_func_name(cbfunc->cbf_idx),
5965 cbfunc->cbf_argcount);
5966 }
5967 break;
5968 case ISN_DCALL:
5969 {
5970 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5971 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5972 + cdfunc->cdf_idx;
5973
5974 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005975 printable_func_name(df->df_ufunc),
5976 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005977 }
5978 break;
5979 case ISN_UCALL:
5980 {
5981 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5982
5983 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5984 cufunc->cuf_name, cufunc->cuf_argcount);
5985 }
5986 break;
5987 case ISN_PCALL:
5988 {
5989 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5990
5991 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5992 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5993 }
5994 break;
5995 case ISN_PCALL_END:
5996 smsg("%s%4d PCALL end", pfx, current);
5997 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005998 case ISN_DEFER:
5999 smsg("%s%4d DEFER %d args", pfx, current,
6000 (int)iptr->isn_arg.defer.defer_argcount);
6001 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006002 case ISN_RETURN:
6003 smsg("%s%4d RETURN", pfx, current);
6004 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006005 case ISN_RETURN_VOID:
6006 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006007 break;
6008 case ISN_FUNCREF:
6009 {
6010 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00006011 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006012
Bram Moolenaar38453522021-11-28 22:00:12 +00006013 if (funcref->fr_func_name == NULL)
6014 {
6015 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6016 + funcref->fr_dfunc_idx;
6017 name = df->df_ufunc->uf_name;
6018 }
6019 else
6020 name = funcref->fr_func_name;
6021 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006022 }
6023 break;
6024
6025 case ISN_NEWFUNC:
6026 {
6027 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
6028
6029 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6030 newfunc->nf_lambda, newfunc->nf_global);
6031 }
6032 break;
6033
6034 case ISN_DEF:
6035 {
6036 char_u *name = iptr->isn_arg.string;
6037
6038 smsg("%s%4d DEF %s", pfx, current,
6039 name == NULL ? (char_u *)"" : name);
6040 }
6041 break;
6042
6043 case ISN_JUMP:
6044 {
6045 char *when = "?";
6046
6047 switch (iptr->isn_arg.jump.jump_when)
6048 {
6049 case JUMP_ALWAYS:
6050 when = "JUMP";
6051 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006052 case JUMP_NEVER:
6053 iemsg("JUMP_NEVER should not be used");
6054 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006055 case JUMP_AND_KEEP_IF_TRUE:
6056 when = "JUMP_AND_KEEP_IF_TRUE";
6057 break;
6058 case JUMP_IF_FALSE:
6059 when = "JUMP_IF_FALSE";
6060 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006061 case JUMP_IF_COND_FALSE:
6062 when = "JUMP_IF_COND_FALSE";
6063 break;
6064 case JUMP_IF_COND_TRUE:
6065 when = "JUMP_IF_COND_TRUE";
6066 break;
6067 }
6068 smsg("%s%4d %s -> %d", pfx, current, when,
6069 iptr->isn_arg.jump.jump_where);
6070 }
6071 break;
6072
6073 case ISN_JUMP_IF_ARG_SET:
6074 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6075 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6076 iptr->isn_arg.jump.jump_where);
6077 break;
6078
6079 case ISN_FOR:
6080 {
6081 forloop_T *forloop = &iptr->isn_arg.forloop;
6082
6083 smsg("%s%4d FOR $%d -> %d", pfx, current,
6084 forloop->for_idx, forloop->for_end);
6085 }
6086 break;
6087
6088 case ISN_TRY:
6089 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006090 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006091
6092 if (try->try_ref->try_finally == 0)
6093 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6094 pfx, current,
6095 try->try_ref->try_catch,
6096 try->try_ref->try_endtry);
6097 else
6098 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6099 pfx, current,
6100 try->try_ref->try_catch,
6101 try->try_ref->try_finally,
6102 try->try_ref->try_endtry);
6103 }
6104 break;
6105 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006106 smsg("%s%4d CATCH", pfx, current);
6107 break;
6108 case ISN_TRYCONT:
6109 {
6110 trycont_T *trycont = &iptr->isn_arg.trycont;
6111
6112 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6113 trycont->tct_levels,
6114 trycont->tct_levels == 1 ? "" : "s",
6115 trycont->tct_where);
6116 }
6117 break;
6118 case ISN_FINALLY:
6119 smsg("%s%4d FINALLY", pfx, current);
6120 break;
6121 case ISN_ENDTRY:
6122 smsg("%s%4d ENDTRY", pfx, current);
6123 break;
6124 case ISN_THROW:
6125 smsg("%s%4d THROW", pfx, current);
6126 break;
6127
6128 // expression operations on number
6129 case ISN_OPNR:
6130 case ISN_OPFLOAT:
6131 case ISN_OPANY:
6132 {
6133 char *what;
6134 char *ins;
6135
6136 switch (iptr->isn_arg.op.op_type)
6137 {
6138 case EXPR_MULT: what = "*"; break;
6139 case EXPR_DIV: what = "/"; break;
6140 case EXPR_REM: what = "%"; break;
6141 case EXPR_SUB: what = "-"; break;
6142 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006143 case EXPR_LSHIFT: what = "<<"; break;
6144 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006145 default: what = "???"; break;
6146 }
6147 switch (iptr->isn_type)
6148 {
6149 case ISN_OPNR: ins = "OPNR"; break;
6150 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6151 case ISN_OPANY: ins = "OPANY"; break;
6152 default: ins = "???"; break;
6153 }
6154 smsg("%s%4d %s %s", pfx, current, ins, what);
6155 }
6156 break;
6157
6158 case ISN_COMPAREBOOL:
6159 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006160 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006161 case ISN_COMPARENR:
6162 case ISN_COMPAREFLOAT:
6163 case ISN_COMPARESTRING:
6164 case ISN_COMPAREBLOB:
6165 case ISN_COMPARELIST:
6166 case ISN_COMPAREDICT:
6167 case ISN_COMPAREFUNC:
6168 case ISN_COMPAREANY:
6169 {
6170 char *p;
6171 char buf[10];
6172 char *type;
6173
6174 switch (iptr->isn_arg.op.op_type)
6175 {
6176 case EXPR_EQUAL: p = "=="; break;
6177 case EXPR_NEQUAL: p = "!="; break;
6178 case EXPR_GREATER: p = ">"; break;
6179 case EXPR_GEQUAL: p = ">="; break;
6180 case EXPR_SMALLER: p = "<"; break;
6181 case EXPR_SEQUAL: p = "<="; break;
6182 case EXPR_MATCH: p = "=~"; break;
6183 case EXPR_IS: p = "is"; break;
6184 case EXPR_ISNOT: p = "isnot"; break;
6185 case EXPR_NOMATCH: p = "!~"; break;
6186 default: p = "???"; break;
6187 }
6188 STRCPY(buf, p);
6189 if (iptr->isn_arg.op.op_ic == TRUE)
6190 strcat(buf, "?");
6191 switch(iptr->isn_type)
6192 {
6193 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6194 case ISN_COMPARESPECIAL:
6195 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006196 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006197 case ISN_COMPARENR: type = "COMPARENR"; break;
6198 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6199 case ISN_COMPARESTRING:
6200 type = "COMPARESTRING"; break;
6201 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6202 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6203 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6204 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6205 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6206 default: type = "???"; break;
6207 }
6208
6209 smsg("%s%4d %s %s", pfx, current, type, buf);
6210 }
6211 break;
6212
6213 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6214 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6215
6216 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006217 case ISN_CONCAT:
6218 smsg("%s%4d CONCAT size %lld", pfx, current,
6219 (varnumber_T)(iptr->isn_arg.number));
6220 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006221 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6222 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6223 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6224 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6225 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6226 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6227 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6228 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6229 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6230 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6231 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006232 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006233 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6234 iptr->isn_arg.getitem.gi_index,
6235 iptr->isn_arg.getitem.gi_with_op ?
6236 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006237 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6238 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6239 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006240 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6241 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6242
Bram Moolenaar4c137212021-04-19 16:48:48 +02006243 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6244
Bram Moolenaar4c137212021-04-19 16:48:48 +02006245 case ISN_CHECKTYPE:
6246 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006247 checktype_T *ct = &iptr->isn_arg.type;
6248 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006249
6250 if (ct->ct_arg_idx == 0)
6251 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6252 type_name(ct->ct_type, &tofree),
6253 (int)ct->ct_off);
6254 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006255 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006256 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006257 type_name(ct->ct_type, &tofree),
6258 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006259 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006260 (int)ct->ct_arg_idx);
6261 vim_free(tofree);
6262 break;
6263 }
6264 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6265 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6266 iptr->isn_arg.checklen.cl_min_len);
6267 break;
6268 case ISN_SETTYPE:
6269 {
6270 char *tofree;
6271
6272 smsg("%s%4d SETTYPE %s", pfx, current,
6273 type_name(iptr->isn_arg.type.ct_type, &tofree));
6274 vim_free(tofree);
6275 break;
6276 }
6277 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006278 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6279 smsg("%s%4d INVERT %d (!val)", pfx, current,
6280 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006281 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006282 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6283 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006284 break;
6285 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006286 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006287 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006288 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6289 pfx, current,
6290 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006291 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006292 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6293 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006294 break;
6295 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006296 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006297 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006298 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006299 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6300 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006301 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006302 else
6303 smsg("%s%4d PUT %c %ld", pfx, current,
6304 iptr->isn_arg.put.put_regname,
6305 (long)iptr->isn_arg.put.put_lnum);
6306 break;
6307
Bram Moolenaar4c137212021-04-19 16:48:48 +02006308 case ISN_CMDMOD:
6309 {
6310 char_u *buf;
6311 size_t len = produce_cmdmods(
6312 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6313
6314 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006315 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006316 {
6317 (void)produce_cmdmods(
6318 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6319 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6320 vim_free(buf);
6321 }
6322 break;
6323 }
6324 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6325
6326 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006327 smsg("%s%4d PROFILE START line %d", pfx, current,
6328 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006329 break;
6330
6331 case ISN_PROF_END:
6332 smsg("%s%4d PROFILE END", pfx, current);
6333 break;
6334
Bram Moolenaare99d4222021-06-13 14:01:26 +02006335 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006336 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6337 iptr->isn_arg.debug.dbg_break_lnum + 1,
6338 iptr->isn_lnum,
6339 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006340 break;
6341
Bram Moolenaar4c137212021-04-19 16:48:48 +02006342 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6343 iptr->isn_arg.unpack.unp_count,
6344 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6345 break;
6346 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6347 iptr->isn_arg.shuffle.shfl_item,
6348 iptr->isn_arg.shuffle.shfl_up);
6349 break;
6350 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6351
6352 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6353 return;
6354 }
6355
6356 out_flush(); // output one line at a time
6357 ui_breakcheck();
6358 if (got_int)
6359 break;
6360 }
6361}
6362
6363/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006364 * Handle command line completion for the :disassemble command.
6365 */
6366 void
6367set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6368{
6369 char_u *p;
6370
6371 // Default: expand user functions, "debug" and "profile"
6372 xp->xp_context = EXPAND_DISASSEMBLE;
6373 xp->xp_pattern = arg;
6374
6375 // first argument already typed: only user function names
6376 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6377 {
6378 xp->xp_context = EXPAND_USER_FUNC;
6379 xp->xp_pattern = skipwhite(p);
6380 }
6381}
6382
6383/*
6384 * Function given to ExpandGeneric() to obtain the list of :disassemble
6385 * arguments.
6386 */
6387 char_u *
6388get_disassemble_argument(expand_T *xp, int idx)
6389{
6390 if (idx == 0)
6391 return (char_u *)"debug";
6392 if (idx == 1)
6393 return (char_u *)"profile";
6394 return get_user_func_name(xp, idx - 2);
6395}
6396
6397/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006398 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006399 * We don't really need this at runtime, but we do have tests that require it,
6400 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401 */
6402 void
6403ex_disassemble(exarg_T *eap)
6404{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006405 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006406 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006407 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006408 isn_T *instr = NULL; // init to shut up gcc warning
6409 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006410 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006411
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006412 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006413 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006414 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006415 if (func_needs_compiling(ufunc, compile_type)
6416 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006417 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006418 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006420 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006421 return;
6422 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006423 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006424
6425 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006426 switch (compile_type)
6427 {
6428 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006429#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006430 instr = dfunc->df_instr_prof;
6431 instr_count = dfunc->df_instr_prof_count;
6432 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006433#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006434 // FALLTHROUGH
6435 case CT_NONE:
6436 instr = dfunc->df_instr;
6437 instr_count = dfunc->df_instr_count;
6438 break;
6439 case CT_DEBUG:
6440 instr = dfunc->df_instr_debug;
6441 instr_count = dfunc->df_instr_debug_count;
6442 break;
6443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006444
Bram Moolenaar4c137212021-04-19 16:48:48 +02006445 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446}
6447
6448/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006449 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006450 * list, etc. Mostly like what JavaScript does, except that empty list and
6451 * empty dictionary are FALSE.
6452 */
6453 int
6454tv2bool(typval_T *tv)
6455{
6456 switch (tv->v_type)
6457 {
6458 case VAR_NUMBER:
6459 return tv->vval.v_number != 0;
6460 case VAR_FLOAT:
6461#ifdef FEAT_FLOAT
6462 return tv->vval.v_float != 0.0;
6463#else
6464 break;
6465#endif
6466 case VAR_PARTIAL:
6467 return tv->vval.v_partial != NULL;
6468 case VAR_FUNC:
6469 case VAR_STRING:
6470 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6471 case VAR_LIST:
6472 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6473 case VAR_DICT:
6474 return tv->vval.v_dict != NULL
6475 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6476 case VAR_BOOL:
6477 case VAR_SPECIAL:
6478 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6479 case VAR_JOB:
6480#ifdef FEAT_JOB_CHANNEL
6481 return tv->vval.v_job != NULL;
6482#else
6483 break;
6484#endif
6485 case VAR_CHANNEL:
6486#ifdef FEAT_JOB_CHANNEL
6487 return tv->vval.v_channel != NULL;
6488#else
6489 break;
6490#endif
6491 case VAR_BLOB:
6492 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6493 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006494 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006495 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006496 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497 break;
6498 }
6499 return FALSE;
6500}
6501
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006502 void
6503emsg_using_string_as(typval_T *tv, int as_number)
6504{
6505 semsg(_(as_number ? e_using_string_as_number_str
6506 : e_using_string_as_bool_str),
6507 tv->vval.v_string == NULL
6508 ? (char_u *)"" : tv->vval.v_string);
6509}
6510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511/*
6512 * If "tv" is a string give an error and return FAIL.
6513 */
6514 int
6515check_not_string(typval_T *tv)
6516{
6517 if (tv->v_type == VAR_STRING)
6518 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006519 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 clear_tv(tv);
6521 return FAIL;
6522 }
6523 return OK;
6524}
6525
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006526
6527#endif // FEAT_EVAL