blob: 2f94f05c54a7107050067da48a0b17eeb0f156c9 [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#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 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
LemonBoy372bcce2022-04-25 12:43:20 +0100123 * Create a new string from "count" items at the bottom of the stack.
124 * A trailing NUL is appended.
125 * When "count" is zero an empty string is added to the stack.
126 */
127 static int
128exe_concat(int count, ectx_T *ectx)
129{
130 int idx;
131 int len = 0;
132 typval_T *tv;
133 garray_T ga;
134
135 ga_init2(&ga, sizeof(char), 1);
136 // Preallocate enough space for the whole string to avoid having to grow
137 // and copy.
138 for (idx = 0; idx < count; ++idx)
139 {
140 tv = STACK_TV_BOT(idx - count);
141 if (tv->vval.v_string != NULL)
142 len += (int)STRLEN(tv->vval.v_string);
143 }
144
145 if (ga_grow(&ga, len + 1) == FAIL)
146 return FAIL;
147
148 for (idx = 0; idx < count; ++idx)
149 {
150 tv = STACK_TV_BOT(idx - count);
151 ga_concat(&ga, tv->vval.v_string);
152 clear_tv(tv);
153 }
154
155 // add a terminating NUL
156 ga_append(&ga, NUL);
157
158 ectx->ec_stack.ga_len -= count - 1;
159 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
160
161 return OK;
162}
163
164/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200165 * Create a new list from "count" items at the bottom of the stack.
166 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100167 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200168 */
169 static int
170exe_newlist(int count, ectx_T *ectx)
171{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100172 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200173 int idx;
174 typval_T *tv;
175
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100176 if (count >= 0)
177 {
178 list = list_alloc_with_items(count);
179 if (list == NULL)
180 return FAIL;
181 for (idx = 0; idx < count; ++idx)
182 list_set_item(list, idx, STACK_TV_BOT(idx - count));
183 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200184
185 if (count > 0)
186 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200187 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100188 {
189 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100191 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200192 else
193 ++ectx->ec_stack.ga_len;
194 tv = STACK_TV_BOT(-1);
195 tv->v_type = VAR_LIST;
196 tv->vval.v_list = list;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 if (list != NULL)
198 ++list->lv_refcount;
199 return OK;
200}
201
202/*
203 * Implementation of ISN_NEWDICT.
204 * Returns FAIL on total failure, MAYBE on error.
205 */
206 static int
207exe_newdict(int count, ectx_T *ectx)
208{
209 dict_T *dict = NULL;
210 dictitem_T *item;
211 char_u *key;
212 int idx;
213 typval_T *tv;
214
215 if (count >= 0)
216 {
217 dict = dict_alloc();
218 if (unlikely(dict == NULL))
219 return FAIL;
220 for (idx = 0; idx < count; ++idx)
221 {
222 // have already checked key type is VAR_STRING
223 tv = STACK_TV_BOT(2 * (idx - count));
224 // check key is unique
225 key = tv->vval.v_string == NULL
226 ? (char_u *)"" : tv->vval.v_string;
227 item = dict_find(dict, key, -1);
228 if (item != NULL)
229 {
230 semsg(_(e_duplicate_key_in_dicitonary), key);
231 dict_unref(dict);
232 return MAYBE;
233 }
234 item = dictitem_alloc(key);
235 clear_tv(tv);
236 if (unlikely(item == NULL))
237 {
238 dict_unref(dict);
239 return FAIL;
240 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100241 tv = STACK_TV_BOT(2 * (idx - count) + 1);
242 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100243 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100244 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100245 if (dict_add(dict, item) == FAIL)
246 {
247 // can this ever happen?
248 dict_unref(dict);
249 return FAIL;
250 }
251 }
252 }
253
254 if (count > 0)
255 ectx->ec_stack.ga_len -= 2 * count - 1;
256 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
257 return FAIL;
258 else
259 ++ectx->ec_stack.ga_len;
260 tv = STACK_TV_BOT(-1);
261 tv->v_type = VAR_DICT;
262 tv->v_lock = 0;
263 tv->vval.v_dict = dict;
264 if (dict != NULL)
265 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200266 return OK;
267}
268
269/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200270 * If debug_tick changed check if "ufunc" has a breakpoint and update
271 * "uf_has_breakpoint".
272 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000273 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200274update_has_breakpoint(ufunc_T *ufunc)
275{
276 if (ufunc->uf_debug_tick != debug_tick)
277 {
278 linenr_T breakpoint;
279
280 ufunc->uf_debug_tick = debug_tick;
281 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
282 ufunc->uf_has_breakpoint = breakpoint > 0;
283 }
284}
285
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200286static garray_T dict_stack = GA_EMPTY;
287
288/*
289 * Put a value on the dict stack. This consumes "tv".
290 */
291 static int
292dict_stack_save(typval_T *tv)
293{
294 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000295 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200296 if (ga_grow(&dict_stack, 1) == FAIL)
297 return FAIL;
298 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
299 ++dict_stack.ga_len;
300 return OK;
301}
302
303/*
304 * Get the typval at top of the dict stack.
305 */
306 static typval_T *
307dict_stack_get_tv(void)
308{
309 if (dict_stack.ga_len == 0)
310 return NULL;
311 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
312}
313
314/*
315 * Get the dict at top of the dict stack.
316 */
317 static dict_T *
318dict_stack_get_dict(void)
319{
320 typval_T *tv;
321
322 if (dict_stack.ga_len == 0)
323 return NULL;
324 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
325 if (tv->v_type == VAR_DICT)
326 return tv->vval.v_dict;
327 return NULL;
328}
329
330/*
331 * Drop an item from the dict stack.
332 */
333 static void
334dict_stack_drop(void)
335{
336 if (dict_stack.ga_len == 0)
337 {
338 iemsg("Dict stack underflow");
339 return;
340 }
341 --dict_stack.ga_len;
342 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
343}
344
345/*
346 * Drop items from the dict stack until the length is equal to "len".
347 */
348 static void
349dict_stack_clear(int len)
350{
351 while (dict_stack.ga_len > len)
352 dict_stack_drop();
353}
354
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200355/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000356 * Get a pointer to useful "pt_outer" of "pt".
357 */
358 static outer_T *
359get_pt_outer(partial_T *pt)
360{
361 partial_T *ptref = pt->pt_outer_partial;
362
363 if (ptref == NULL)
364 return &pt->pt_outer;
365
366 // partial using partial (recursively)
367 while (ptref->pt_outer_partial != NULL)
368 ptref = ptref->pt_outer_partial;
369 return &ptref->pt_outer;
370}
371
372/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100374 * This adds a stack frame and sets the instruction pointer to the start of the
375 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200376 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100377 *
378 * Stack has:
379 * - current arguments (already there)
380 * - omitted optional argument (default values) added here
381 * - stack frame:
382 * - pointer to calling function
383 * - Index of next instruction in calling function
384 * - previous frame pointer
385 * - reserved space for local variables
386 */
387 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100388call_dfunc(
389 int cdf_idx,
390 partial_T *pt,
391 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100392 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394 int argcount = argcount_arg;
395 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
396 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200397 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 int arg_to_add;
399 int vararg_count = 0;
400 int varcount;
401 int idx;
402 estack_T *entry;
403 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200404 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000405 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100406
407 if (dfunc->df_deleted)
408 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100409 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000410 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100411 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412 return FAIL;
413 }
414
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100415#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100416 if (do_profiling == PROF_YES)
417 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200418 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100419 {
420 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
421 + profile_info_ga.ga_len;
422 ++profile_info_ga.ga_len;
423 CLEAR_POINTER(info);
424 profile_may_start_func(info, ufunc,
425 (((dfunc_T *)def_functions.ga_data)
426 + ectx->ec_dfunc_idx)->df_ufunc);
427 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100428 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100429#endif
430
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200431 // When debugging and using "cont" switches to the not-debugged
432 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000433 compile_type = get_compile_type(ufunc);
434 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200435 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000436 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200437
438 // compile_def_function() may cause def_functions.ga_data to change
439 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
440 }
441 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200442 {
443 if (did_emsg_cumul + did_emsg == did_emsg_before)
444 semsg(_(e_function_is_not_compiled_str),
445 printable_func_name(ufunc));
446 return FAIL;
447 }
448
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200449 if (ufunc->uf_va_name != NULL)
450 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200451 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200452 // Stack at time of call with 2 varargs:
453 // normal_arg
454 // optional_arg
455 // vararg_1
456 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200457 // After creating the list:
458 // normal_arg
459 // optional_arg
460 // vararg-list
461 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200462 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200463 // After creating the list
464 // normal_arg
465 // (space for optional_arg)
466 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200467 vararg_count = argcount - ufunc->uf_args.ga_len;
468 if (vararg_count < 0)
469 vararg_count = 0;
470 else
471 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200472 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200473 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200474
475 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200476 }
477
Bram Moolenaarfe270812020-04-11 22:31:27 +0200478 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200479 if (arg_to_add < 0)
480 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100481 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
482 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200483 return FAIL;
484 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000485 else if (arg_to_add > ufunc->uf_def_args.ga_len)
486 {
487 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
488
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100489 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
490 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000491 return FAIL;
492 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200493
494 // Reserve space for:
495 // - missing arguments
496 // - stack frame
497 // - local variables
498 // - if needed: a counter for number of closures created in
499 // ectx->ec_funcrefs.
500 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200501 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502 return FAIL;
503
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100504 // If depth of calling is getting too high, don't execute the function.
505 if (funcdepth_increment() == FAIL)
506 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200507 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100508
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100509 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200510 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100511 {
512 floc = ALLOC_ONE(funclocal_T);
513 if (floc == NULL)
514 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200515 *floc = ectx->ec_funclocal;
516 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100517 }
518
Bram Moolenaarfe270812020-04-11 22:31:27 +0200519 // Move the vararg-list to below the missing optional arguments.
520 if (vararg_count > 0 && arg_to_add > 0)
521 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100522
523 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200524 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200525 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200526 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527
528 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100529 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
530 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200531 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200532 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
533 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100534 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100535 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200536 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537
538 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200539 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000540 {
541 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
542
543 tv->v_type = VAR_NUMBER;
544 tv->vval.v_number = 0;
545 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200546 if (dfunc->df_has_closure)
547 {
548 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
549
550 tv->v_type = VAR_NUMBER;
551 tv->vval.v_number = 0;
552 }
553 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100555 if (pt != NULL || ufunc->uf_partial != NULL
556 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100557 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200558 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100559
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200560 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100561 return FAIL;
562 if (pt != NULL)
563 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000564 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200565 ++pt->pt_refcount;
566 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100567 }
568 else if (ufunc->uf_partial != NULL)
569 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000570 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200571 ++ufunc->uf_partial->pt_refcount;
572 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100573 }
574 else
575 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200576 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200577 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200578 {
579 vim_free(ref);
580 return FAIL;
581 }
582 ref->or_outer_allocated = TRUE;
583 ref->or_outer->out_stack = &ectx->ec_stack;
584 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
585 if (ectx->ec_outer_ref != NULL)
586 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100587 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200588 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100589 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100590 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200591 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100592
Bram Moolenaarc970e422021-03-17 15:03:04 +0100593 ++ufunc->uf_calls;
594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595 // Set execution state to the start of the called function.
596 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100597 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100598 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200599 if (entry != NULL)
600 {
601 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200602 // Save the current context so it can be restored on return.
603 entry->es_save_sctx = current_sctx;
604 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200605 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100606
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200607 // Start execution at the first instruction.
608 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
610 return OK;
611}
612
613// Get pointer to item in the stack.
614#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
615
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000616// Double linked list of funcstack_T in use.
617static funcstack_T *first_funcstack = NULL;
618
619 static void
620add_funcstack_to_list(funcstack_T *funcstack)
621{
622 // Link in list of funcstacks.
623 if (first_funcstack != NULL)
624 first_funcstack->fs_prev = funcstack;
625 funcstack->fs_next = first_funcstack;
626 funcstack->fs_prev = NULL;
627 first_funcstack = funcstack;
628}
629
630 static void
631remove_funcstack_from_list(funcstack_T *funcstack)
632{
633 if (funcstack->fs_prev == NULL)
634 first_funcstack = funcstack->fs_next;
635 else
636 funcstack->fs_prev->fs_next = funcstack->fs_next;
637 if (funcstack->fs_next != NULL)
638 funcstack->fs_next->fs_prev = funcstack->fs_prev;
639}
640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200642 * Used when returning from a function: Check if any closure is still
643 * referenced. If so then move the arguments and variables to a separate piece
644 * of stack to be used when the closure is called.
645 * When "free_arguments" is TRUE the arguments are to be freed.
646 * Returns FAIL when out of memory.
647 */
648 static int
649handle_closure_in_use(ectx_T *ectx, int free_arguments)
650{
651 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
652 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200653 int argcount;
654 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200655 int idx;
656 typval_T *tv;
657 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200658 garray_T *gap = &ectx->ec_funcrefs;
659 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200660
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200661 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200662 return OK; // function was freed
663 if (dfunc->df_has_closure == 0)
664 return OK; // no closures
665 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
666 closure_count = tv->vval.v_number;
667 if (closure_count == 0)
668 return OK; // no funcrefs created
669
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200670 argcount = ufunc_argcount(dfunc->df_ufunc);
671 top = ectx->ec_frame_idx - argcount;
672
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200673 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200674 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200675 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200676 partial_T *pt;
677 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200678
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200679 if (off < 0)
680 continue; // count is off or already done
681 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200682 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200683 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200684 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200685 int i;
686
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000687 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200688 // unreferenced on return.
689 for (i = 0; i < dfunc->df_varcount; ++i)
690 {
691 typval_T *stv = STACK_TV(ectx->ec_frame_idx
692 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200693 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200694 --refcount;
695 }
696 if (refcount > 1)
697 {
698 closure_in_use = TRUE;
699 break;
700 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200701 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200702 }
703
704 if (closure_in_use)
705 {
706 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
707 typval_T *stack;
708
709 // A closure is using the arguments and/or local variables.
710 // Move them to the called function.
711 if (funcstack == NULL)
712 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000713
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200714 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
715 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200716 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
717 funcstack->fs_ga.ga_data = stack;
718 if (stack == NULL)
719 {
720 vim_free(funcstack);
721 return FAIL;
722 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000723 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200724
725 // Move or copy the arguments.
726 for (idx = 0; idx < argcount; ++idx)
727 {
728 tv = STACK_TV(top + idx);
729 if (free_arguments)
730 {
731 *(stack + idx) = *tv;
732 tv->v_type = VAR_UNKNOWN;
733 }
734 else
735 copy_tv(tv, stack + idx);
736 }
737 // Move the local variables.
738 for (idx = 0; idx < dfunc->df_varcount; ++idx)
739 {
740 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200741
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200742 // A partial created for a local function, that is also used as a
743 // local variable, has a reference count for the variable, thus
744 // will never go down to zero. When all these refcounts are one
745 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000746 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200747 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
748 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200749 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200750
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200751 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200752 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
753 gap->ga_len - closure_count + i])
754 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200755 }
756
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200757 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200758 tv->v_type = VAR_UNKNOWN;
759 }
760
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200761 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200762 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200763 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
764 - closure_count + idx];
765 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200766 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200767 ++funcstack->fs_refcount;
768 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100769 pt->pt_outer.out_stack = &funcstack->fs_ga;
770 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200771 }
772 }
773 }
774
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200775 for (idx = 0; idx < closure_count; ++idx)
776 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
777 - closure_count + idx]);
778 gap->ga_len -= closure_count;
779 if (gap->ga_len == 0)
780 ga_clear(gap);
781
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200782 return OK;
783}
784
785/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200786 * Called when a partial is freed or its reference count goes down to one. The
787 * funcstack may be the only reference to the partials in the local variables.
788 * Go over all of them, the funcref and can be freed if all partials
789 * referencing the funcstack have a reference count of one.
790 */
791 void
792funcstack_check_refcount(funcstack_T *funcstack)
793{
794 int i;
795 garray_T *gap = &funcstack->fs_ga;
796 int done = 0;
797
798 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
799 return;
800 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
801 {
802 typval_T *tv = ((typval_T *)gap->ga_data) + i;
803
804 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
805 && tv->vval.v_partial->pt_funcstack == funcstack
806 && tv->vval.v_partial->pt_refcount == 1)
807 ++done;
808 }
809 if (done == funcstack->fs_min_refcount)
810 {
811 typval_T *stack = gap->ga_data;
812
813 // All partials referencing the funcstack have a reference count of
814 // one, thus the funcstack is no longer of use.
815 for (i = 0; i < gap->ga_len; ++i)
816 clear_tv(stack + i);
817 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000818 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200819 vim_free(funcstack);
820 }
821}
822
823/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000824 * For garbage collecting: set references in all variables referenced by
825 * all funcstacks.
826 */
827 int
828set_ref_in_funcstacks(int copyID)
829{
830 funcstack_T *funcstack;
831
832 for (funcstack = first_funcstack; funcstack != NULL;
833 funcstack = funcstack->fs_next)
834 {
835 typval_T *stack = funcstack->fs_ga.ga_data;
836 int i;
837
838 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
839 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
840 return TRUE; // abort
841 }
842 return FALSE;
843}
844
845/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 * Return from the current function.
847 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200848 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200849func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100852 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200853 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
854 + ectx->ec_dfunc_idx;
855 int argcount = ufunc_argcount(dfunc->df_ufunc);
856 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200857 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100858 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
859 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200860 funclocal_T *floc;
861#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100862 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
863 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864
Bram Moolenaar12d26532021-02-19 19:13:21 +0100865 if (do_profiling == PROF_YES)
866 {
867 ufunc_T *caller = prev_dfunc->df_ufunc;
868
869 if (dfunc->df_ufunc->uf_profiling
870 || (caller != NULL && caller->uf_profiling))
871 {
872 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
873 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
874 --profile_info_ga.ga_len;
875 }
876 }
877#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000878
879 // No check for uf_refcount being zero, cannot think of a way that would
880 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100881 --dfunc->df_ufunc->uf_calls;
882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200884 entry = estack_pop();
885 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200886 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200888 if (handle_closure_in_use(ectx, TRUE) == FAIL)
889 return FAIL;
890
891 // Clear the arguments.
892 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
893 clear_tv(STACK_TV(idx));
894
895 // Clear local variables and temp values, but not the return value.
896 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100897 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100899
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100900 // The return value should be on top of the stack. However, when aborting
901 // it may not be there and ec_frame_idx is the top of the stack.
902 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100903 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100904 ret_idx = 0;
905
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200906 if (ectx->ec_outer_ref != NULL)
907 {
908 if (ectx->ec_outer_ref->or_outer_allocated)
909 vim_free(ectx->ec_outer_ref->or_outer);
910 partial_unref(ectx->ec_outer_ref->or_partial);
911 vim_free(ectx->ec_outer_ref);
912 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100913
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100914 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100915 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100916 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
917 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200918 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
919 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200920 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100921 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100922 floc = (void *)STACK_TV(ectx->ec_frame_idx
923 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200924 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100925 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
926 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200927
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100928 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200929 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100930 else
931 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200932 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100933 vim_free(floc);
934 }
935
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100936 if (ret_idx > 0)
937 {
938 // Reset the stack to the position before the call, with a spot for the
939 // return value, moved there from above the frame.
940 ectx->ec_stack.ga_len = top + 1;
941 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
942 }
943 else
944 // Reset the stack to the position before the call.
945 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200946
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100947 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200948 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200949 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950}
951
952#undef STACK_TV
953
954/*
955 * Prepare arguments and rettv for calling a builtin or user function.
956 */
957 static int
958call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
959{
960 int idx;
961 typval_T *tv;
962
963 // Move arguments from bottom of the stack to argvars[] and add terminator.
964 for (idx = 0; idx < argcount; ++idx)
965 argvars[idx] = *STACK_TV_BOT(idx - argcount);
966 argvars[argcount].v_type = VAR_UNKNOWN;
967
968 // Result replaces the arguments on the stack.
969 if (argcount > 0)
970 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200971 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 return FAIL;
973 else
974 ++ectx->ec_stack.ga_len;
975
976 // Default return value is zero.
977 tv = STACK_TV_BOT(-1);
978 tv->v_type = VAR_NUMBER;
979 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +0100980 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981
982 return OK;
983}
984
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200985// Ugly global to avoid passing the execution context around through many
986// layers.
987static ectx_T *current_ectx = NULL;
988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989/*
990 * Call a builtin function by index.
991 */
992 static int
993call_bfunc(int func_idx, int argcount, ectx_T *ectx)
994{
995 typval_T argvars[MAX_FUNC_ARGS];
996 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100997 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200998 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200999 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000
1001 if (call_prepare(argcount, argvars, ectx) == FAIL)
1002 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001003 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001005 // Call the builtin function. Set "current_ectx" so that when it
1006 // recursively invokes call_def_function() a closure context can be set.
1007 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001009 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001010 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011
1012 // Clear the arguments.
1013 for (idx = 0; idx < argcount; ++idx)
1014 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001015
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001016 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001017 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 return OK;
1019}
1020
1021/*
1022 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001023 * If the function is compiled this will add a stack frame and set the
1024 * instruction pointer at the start of the function.
1025 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001026 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001027 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 */
1029 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001030call_ufunc(
1031 ufunc_T *ufunc,
1032 partial_T *pt,
1033 int argcount,
1034 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001035 isn_T *iptr,
1036 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037{
1038 typval_T argvars[MAX_FUNC_ARGS];
1039 funcexe_T funcexe;
1040 int error;
1041 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001042 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001043 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044
Bram Moolenaare99d4222021-06-13 14:01:26 +02001045 if (func_needs_compiling(ufunc, compile_type)
1046 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1047 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001048 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001049 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001050 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001051 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001052 if (error != FCERR_UNKNOWN)
1053 {
1054 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001055 semsg(_(e_too_many_arguments_for_function_str),
1056 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001057 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001058 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001059 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001060 return FAIL;
1061 }
1062
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001063 // The function has been compiled, can call it quickly. For a function
1064 // that was defined later: we can call it directly next time.
1065 if (iptr != NULL)
1066 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001067 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001068 iptr->isn_type = ISN_DCALL;
1069 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1070 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1071 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001072 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074
1075 if (call_prepare(argcount, argvars, ectx) == FAIL)
1076 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001077 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001078 funcexe.fe_evaluate = TRUE;
1079 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080
1081 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001083 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084
1085 // Clear the arguments.
1086 for (idx = 0; idx < argcount; ++idx)
1087 clear_tv(&argvars[idx]);
1088
1089 if (error != FCERR_NONE)
1090 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001091 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 return FAIL;
1093 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001094 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001095 // Error other than from calling the function itself.
1096 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 return OK;
1098}
1099
1100/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001101 * If command modifiers were applied restore them.
1102 */
1103 static void
1104may_restore_cmdmod(funclocal_T *funclocal)
1105{
1106 if (funclocal->floc_restore_cmdmod)
1107 {
1108 cmdmod.cmod_filter_regmatch.regprog = NULL;
1109 undo_cmdmod(&cmdmod);
1110 cmdmod = funclocal->floc_save_cmdmod;
1111 funclocal->floc_restore_cmdmod = FALSE;
1112 }
1113}
1114
1115/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001116 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1117 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001118 */
1119 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001120vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001121{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001122 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001123}
1124
1125/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 * Execute a function by "name".
1127 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001128 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 * Returns FAIL if not found without an error message.
1130 */
1131 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001132call_by_name(
1133 char_u *name,
1134 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001135 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001136 isn_T *iptr,
1137 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138{
1139 ufunc_T *ufunc;
1140
1141 if (builtin_function(name, -1))
1142 {
1143 int func_idx = find_internal_func(name);
1144
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001145 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001147 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 return FAIL;
1149 return call_bfunc(func_idx, argcount, ectx);
1150 }
1151
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001152 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001153
1154 if (ufunc == NULL)
1155 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001156 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001157
1158 if (script_autoload(name, TRUE))
1159 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001160 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001161
1162 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001163 return FAIL; // bail out if loading the script caused an error
1164 }
1165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001167 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001168 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001169 {
1170 int i;
1171 typval_T *argv = STACK_TV_BOT(0) - argcount;
1172
1173 // The function can change at runtime, check that the argument
1174 // types are correct.
1175 for (i = 0; i < argcount; ++i)
1176 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001177 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001178
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001179 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001180 type = ufunc->uf_arg_types[i];
1181 else if (ufunc->uf_va_type != NULL)
1182 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001183 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001184 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001185 return FAIL;
1186 }
1187 }
1188
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001189 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191
1192 return FAIL;
1193}
1194
1195 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001196call_partial(
1197 typval_T *tv,
1198 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001199 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001201 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001202 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001204 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001205 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206
1207 if (tv->v_type == VAR_PARTIAL)
1208 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001209 partial_T *pt = tv->vval.v_partial;
1210 int i;
1211
1212 if (pt->pt_argc > 0)
1213 {
1214 // Make space for arguments from the partial, shift the "argcount"
1215 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001216 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001217 return FAIL;
1218 for (i = 1; i <= argcount; ++i)
1219 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1220 ectx->ec_stack.ga_len += pt->pt_argc;
1221 argcount += pt->pt_argc;
1222
1223 // copy the arguments from the partial onto the stack
1224 for (i = 0; i < pt->pt_argc; ++i)
1225 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1226 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001227 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228
1229 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001230 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232 name = pt->pt_name;
1233 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001234 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001236 if (name != NULL)
1237 {
1238 char_u fname_buf[FLEN_FIXED + 1];
1239 char_u *tofree = NULL;
1240 int error = FCERR_NONE;
1241 char_u *fname;
1242
1243 // May need to translate <SNR>123_ to K_SNR.
1244 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1245 if (error != FCERR_NONE)
1246 res = FAIL;
1247 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001248 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001249 vim_free(tofree);
1250 }
1251
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001252 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 {
1254 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001255 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001256 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 return FAIL;
1258 }
1259 return OK;
1260}
1261
1262/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001263 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1264 * TRUE.
1265 */
1266 static int
1267error_if_locked(int lock, char *error)
1268{
1269 if (lock & (VAR_LOCKED | VAR_FIXED))
1270 {
1271 emsg(_(error));
1272 return TRUE;
1273 }
1274 return FALSE;
1275}
1276
1277/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001278 * Give an error if "tv" is not a number and return FAIL.
1279 */
1280 static int
1281check_for_number(typval_T *tv)
1282{
1283 if (tv->v_type != VAR_NUMBER)
1284 {
1285 semsg(_(e_expected_str_but_got_str),
1286 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1287 return FAIL;
1288 }
1289 return OK;
1290}
1291
1292/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001293 * Store "tv" in variable "name".
1294 * This is for s: and g: variables.
1295 */
1296 static void
1297store_var(char_u *name, typval_T *tv)
1298{
1299 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001300 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001301
Bram Moolenaard877a572021-04-01 19:42:48 +02001302 if (tv->v_lock)
1303 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001304 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001305 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001306 restore_funccal();
1307}
1308
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001309/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001310 * Convert "tv" to a string.
1311 * Return FAIL if not allowed.
1312 */
1313 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001314do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001315{
1316 if (tv->v_type != VAR_STRING)
1317 {
1318 char_u *str;
1319
1320 if (is_2string_any)
1321 {
1322 switch (tv->v_type)
1323 {
1324 case VAR_SPECIAL:
1325 case VAR_BOOL:
1326 case VAR_NUMBER:
1327 case VAR_FLOAT:
1328 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001329
1330 case VAR_LIST:
1331 if (tolerant)
1332 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001333 char_u *s, *e, *p;
1334 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001335
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001336 ga_init2(&ga, sizeof(char_u *), 1);
1337
1338 // Convert to NL separated items, then
1339 // escape the items and replace the NL with
1340 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001341 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001342 if (str == NULL)
1343 return FAIL;
1344 s = str;
1345 while ((e = vim_strchr(s, '\n')) != NULL)
1346 {
1347 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001348 p = vim_strsave_fnameescape(s,
1349 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001350 if (p != NULL)
1351 {
1352 ga_concat(&ga, p);
1353 ga_concat(&ga, (char_u *)" ");
1354 vim_free(p);
1355 }
1356 s = e + 1;
1357 }
1358 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001359 clear_tv(tv);
1360 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001361 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001362 return OK;
1363 }
1364 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001365 default: to_string_error(tv->v_type);
1366 return FAIL;
1367 }
1368 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001369 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001370 clear_tv(tv);
1371 tv->v_type = VAR_STRING;
1372 tv->vval.v_string = str;
1373 }
1374 return OK;
1375}
1376
1377/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001378 * When the value of "sv" is a null list of dict, allocate it.
1379 */
1380 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001381allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001382{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001383 typval_T *tv = sv->sv_tv;
1384
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001385 switch (tv->v_type)
1386 {
1387 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001388 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001389 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001390 break;
1391 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001392 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001393 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001394 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001395 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001396 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001397 (void)rettv_blob_alloc(tv);
1398 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001399 default:
1400 break;
1401 }
1402}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001403
Bram Moolenaare7525c52021-01-09 13:20:37 +01001404/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001405 * Return the character "str[index]" where "index" is the character index,
1406 * including composing characters.
1407 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001408 */
1409 char_u *
1410char_from_string(char_u *str, varnumber_T index)
1411{
1412 size_t nbyte = 0;
1413 varnumber_T nchar = index;
1414 size_t slen;
1415
1416 if (str == NULL)
1417 return NULL;
1418 slen = STRLEN(str);
1419
Bram Moolenaarff871402021-03-26 13:34:05 +01001420 // Do the same as for a list: a negative index counts from the end.
1421 // Optimization to check the first byte to be below 0x80 (and no composing
1422 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001423 if (index < 0)
1424 {
1425 int clen = 0;
1426
1427 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001428 {
1429 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1430 ++nbyte;
1431 else if (enc_utf8)
1432 nbyte += utfc_ptr2len(str + nbyte);
1433 else
1434 nbyte += mb_ptr2len(str + nbyte);
1435 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001436 nchar = clen + index;
1437 if (nchar < 0)
1438 // unlike list: index out of range results in empty string
1439 return NULL;
1440 }
1441
1442 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001443 {
1444 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1445 ++nbyte;
1446 else if (enc_utf8)
1447 nbyte += utfc_ptr2len(str + nbyte);
1448 else
1449 nbyte += mb_ptr2len(str + nbyte);
1450 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001451 if (nbyte >= slen)
1452 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001453 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001454}
1455
1456/*
1457 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001458 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001459 * If going over the end return "str_len".
1460 * If "idx" is negative count from the end, -1 is the last character.
1461 * When going over the start return -1.
1462 */
1463 static long
1464char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1465{
1466 varnumber_T nchar = idx;
1467 size_t nbyte = 0;
1468
1469 if (nchar >= 0)
1470 {
1471 while (nchar > 0 && nbyte < str_len)
1472 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001473 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001474 --nchar;
1475 }
1476 }
1477 else
1478 {
1479 nbyte = str_len;
1480 while (nchar < 0 && nbyte > 0)
1481 {
1482 --nbyte;
1483 nbyte -= mb_head_off(str, str + nbyte);
1484 ++nchar;
1485 }
1486 if (nchar < 0)
1487 return -1;
1488 }
1489 return (long)nbyte;
1490}
1491
1492/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001493 * Return the slice "str[first : last]" using character indexes. Composing
1494 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001495 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001496 * Return NULL when the result is empty.
1497 */
1498 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001499string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001500{
1501 long start_byte, end_byte;
1502 size_t slen;
1503
1504 if (str == NULL)
1505 return NULL;
1506 slen = STRLEN(str);
1507 start_byte = char_idx2byte(str, slen, first);
1508 if (start_byte < 0)
1509 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001510 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001511 end_byte = (long)slen;
1512 else
1513 {
1514 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001515 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001516 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001517 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001518 }
1519
1520 if (start_byte >= (long)slen || end_byte <= start_byte)
1521 return NULL;
1522 return vim_strnsave(str + start_byte, end_byte - start_byte);
1523}
1524
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001525/*
1526 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1527 * When "dfunc_idx" is negative don't give an error.
1528 * Returns NULL for an error.
1529 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001530 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001531get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001532{
1533 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001534 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1535 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001536 svar_T *sv;
1537
1538 if (sref->sref_seq != si->sn_script_seq)
1539 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001540 // The script was reloaded after the function was compiled, the
1541 // script_idx may not be valid.
1542 if (dfunc != NULL)
1543 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1544 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001545 return NULL;
1546 }
1547 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001548 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001549 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001550 if (dfunc != NULL)
1551 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001552 return NULL;
1553 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001554
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001555 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1556 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001557 {
1558 if (dfunc != NULL)
1559 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1560 return NULL;
1561 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001562 return sv;
1563}
1564
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001565/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001566 * Function passed to do_cmdline() for splitting a script joined by NL
1567 * characters.
1568 */
1569 static char_u *
1570get_split_sourceline(
1571 int c UNUSED,
1572 void *cookie,
1573 int indent UNUSED,
1574 getline_opt_T options UNUSED)
1575{
1576 source_cookie_T *sp = (source_cookie_T *)cookie;
1577 char_u *p;
1578 char_u *line;
1579
Bram Moolenaar20677332021-06-06 17:02:53 +02001580 p = vim_strchr(sp->nextline, '\n');
1581 if (p == NULL)
1582 {
1583 line = vim_strsave(sp->nextline);
1584 sp->nextline += STRLEN(sp->nextline);
1585 }
1586 else
1587 {
1588 line = vim_strnsave(sp->nextline, p - sp->nextline);
1589 sp->nextline = p + 1;
1590 }
1591 return line;
1592}
1593
1594/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 * Execute a function by "name".
1596 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001597 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 */
1599 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001600call_eval_func(
1601 char_u *name,
1602 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001603 ectx_T *ectx,
1604 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605{
Bram Moolenaared677f52020-08-12 16:38:10 +02001606 int called_emsg_before = called_emsg;
1607 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001609 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001610 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001612 dictitem_T *v;
1613
1614 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001615 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1616 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001617 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001618 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001619 return FAIL;
1620 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001621 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001623 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624}
1625
1626/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001627 * When a function reference is used, fill a partial with the information
1628 * needed, especially when it is used as a closure.
1629 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001630 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001631fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1632{
1633 pt->pt_func = ufunc;
1634 pt->pt_refcount = 1;
1635
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001636 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001637 {
1638 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1639 + ectx->ec_dfunc_idx;
1640
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001641 // The closure may need to find arguments and local variables in the
1642 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001643 pt->pt_outer.out_stack = &ectx->ec_stack;
1644 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001645 if (ectx->ec_outer_ref != NULL)
1646 {
1647 // The current context already has a context, link to that one.
1648 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1649 if (ectx->ec_outer_ref->or_partial != NULL)
1650 {
1651 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1652 ++pt->pt_outer.out_up_partial->pt_refcount;
1653 }
1654 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001655
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001656 // If this function returns and the closure is still being used, we
1657 // need to make a copy of the context (arguments and local variables).
1658 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001659 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001660 {
1661 vim_free(pt);
1662 return FAIL;
1663 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001664 // Extra variable keeps the count of closures created in the current
1665 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001666 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1667 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1668
1669 ((partial_T **)ectx->ec_funcrefs.ga_data)
1670 [ectx->ec_funcrefs.ga_len] = pt;
1671 ++pt->pt_refcount;
1672 ++ectx->ec_funcrefs.ga_len;
1673 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001674 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001675 return OK;
1676}
1677
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001678/*
1679 * Execute iptr->isn_arg.string as an Ex command.
1680 */
1681 static int
1682exec_command(isn_T *iptr)
1683{
1684 source_cookie_T cookie;
1685
1686 SOURCING_LNUM = iptr->isn_lnum;
1687 // Pass getsourceline to get an error for a missing ":end"
1688 // command.
1689 CLEAR_FIELD(cookie);
1690 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1691 if (do_cmdline(iptr->isn_arg.string,
1692 getsourceline, &cookie,
1693 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1694 || did_emsg)
1695 return FAIL;
1696 return OK;
1697}
1698
Bram Moolenaar89445512022-04-14 12:58:23 +01001699/*
1700 * If script "sid" is not loaded yet then load it now.
1701 * Caller must make sure "sid" is a valid script ID.
1702 * "loaded" is set to TRUE if the script had to be loaded.
1703 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1704 */
1705 int
1706may_load_script(int sid, int *loaded)
1707{
1708 scriptitem_T *si = SCRIPT_ITEM(sid);
1709
1710 if (si->sn_state == SN_STATE_NOT_LOADED)
1711 {
1712 *loaded = TRUE;
1713 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1714 {
1715 semsg(_(e_cant_open_file_str), si->sn_name);
1716 return FAIL;
1717 }
1718 }
1719 return OK;
1720}
1721
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001722// used for v_instr of typval of VAR_INSTR
1723struct instr_S {
1724 ectx_T *instr_ectx;
1725 isn_T *instr_instr;
1726};
1727
Bram Moolenaar4c137212021-04-19 16:48:48 +02001728// used for substitute_instr
1729typedef struct subs_expr_S {
1730 ectx_T *subs_ectx;
1731 isn_T *subs_instr;
1732 int subs_status;
1733} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734
1735// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001736#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737
1738// Get pointer to item at the bottom of the stack, -1 is the bottom.
1739#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001740#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 +01001741
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001742// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001743#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001745// Set when calling do_debug().
1746static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001747static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001748
1749/*
1750 * When debugging lookup "name" and return the typeval.
1751 * When not found return NULL.
1752 */
1753 typval_T *
1754lookup_debug_var(char_u *name)
1755{
1756 int idx;
1757 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001758 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001759 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001760 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001761
1762 if (ectx == NULL)
1763 return NULL;
1764 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1765
1766 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001767 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001768 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001769 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1770
1771 // the variable name may be NULL when not available in this block
1772 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001773 return STACK_TV_VAR(idx);
1774 }
1775
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001776 // Go through argument names.
1777 ufunc = dfunc->df_ufunc;
1778 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1779 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1780 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1781 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1782 - varargs_off + idx);
1783 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1784 return STACK_TV(ectx->ec_frame_idx - 1);
1785
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001786 return NULL;
1787}
1788
Bram Moolenaar26a44842021-09-02 18:49:06 +02001789/*
1790 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1791 * breakpoint was set in that function or when there is any expression.
1792 */
1793 int
1794may_break_in_function(ufunc_T *ufunc)
1795{
1796 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1797}
1798
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001799 static void
1800handle_debug(isn_T *iptr, ectx_T *ectx)
1801{
1802 char_u *line;
1803 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1804 + ectx->ec_dfunc_idx)->df_ufunc;
1805 isn_T *ni;
1806 int end_lnum = iptr->isn_lnum;
1807 garray_T ga;
1808 int lnum;
1809
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001810 if (ex_nesting_level > debug_break_level)
1811 {
1812 linenr_T breakpoint;
1813
Bram Moolenaar26a44842021-09-02 18:49:06 +02001814 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001815 return;
1816
1817 // check for the next breakpoint if needed
1818 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001819 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001820 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1821 return;
1822 }
1823
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001824 SOURCING_LNUM = iptr->isn_lnum;
1825 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001826 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001827
1828 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1829 if (ni->isn_type == ISN_DEBUG
1830 || ni->isn_type == ISN_RETURN
1831 || ni->isn_type == ISN_RETURN_VOID)
1832 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001833 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001834 break;
1835 }
1836
1837 if (end_lnum > iptr->isn_lnum)
1838 {
1839 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001840 for (lnum = iptr->isn_lnum; lnum < end_lnum
1841 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001842 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001843 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001844
Bram Moolenaar303215d2021-07-07 20:10:43 +02001845 if (p == NULL)
1846 continue; // left over from continuation line
1847 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001848 if (*p == '#')
1849 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001850 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001851 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1852 if (STRNCMP(p, "def ", 4) == 0)
1853 break;
1854 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001855 line = ga_concat_strings(&ga, " ");
1856 vim_free(ga.ga_data);
1857 }
1858 else
1859 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001860
Dominique Pelle6e969552021-06-17 13:53:41 +02001861 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001862 debug_context = NULL;
1863
1864 if (end_lnum > iptr->isn_lnum)
1865 vim_free(line);
1866}
1867
Bram Moolenaar4c137212021-04-19 16:48:48 +02001868/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001869 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001870 * Returns OK, FAIL or NOTDONE (uncatchable error).
1871 */
1872 static int
1873execute_storeindex(isn_T *iptr, ectx_T *ectx)
1874{
1875 vartype_T dest_type = iptr->isn_arg.vartype;
1876 typval_T *tv;
1877 typval_T *tv_idx = STACK_TV_BOT(-2);
1878 typval_T *tv_dest = STACK_TV_BOT(-1);
1879 int status = OK;
1880
1881 // Stack contains:
1882 // -3 value to be stored
1883 // -2 index
1884 // -1 dict or list
1885 tv = STACK_TV_BOT(-3);
1886 SOURCING_LNUM = iptr->isn_lnum;
1887 if (dest_type == VAR_ANY)
1888 {
1889 dest_type = tv_dest->v_type;
1890 if (dest_type == VAR_DICT)
1891 status = do_2string(tv_idx, TRUE, FALSE);
1892 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1893 {
1894 emsg(_(e_number_expected));
1895 status = FAIL;
1896 }
1897 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001898
1899 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001900 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001901 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001902 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001903 long lidx = (long)tv_idx->vval.v_number;
1904 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001905
Bram Moolenaare08be092022-02-17 13:08:26 +00001906 if (list == NULL)
1907 {
1908 emsg(_(e_list_not_set));
1909 return FAIL;
1910 }
1911 if (lidx < 0 && list->lv_len + lidx >= 0)
1912 // negative index is relative to the end
1913 lidx = list->lv_len + lidx;
1914 if (lidx < 0 || lidx > list->lv_len)
1915 {
1916 semsg(_(e_list_index_out_of_range_nr), lidx);
1917 return FAIL;
1918 }
1919 if (lidx < list->lv_len)
1920 {
1921 listitem_T *li = list_find(list, lidx);
1922
1923 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001924 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001925 return FAIL;
1926 // overwrite existing list item
1927 clear_tv(&li->li_tv);
1928 li->li_tv = *tv;
1929 }
1930 else
1931 {
1932 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1933 return FAIL;
1934 // append to list, only fails when out of memory
1935 if (list_append_tv(list, tv) == FAIL)
1936 return NOTDONE;
1937 clear_tv(tv);
1938 }
1939 }
1940 else if (dest_type == VAR_DICT)
1941 {
1942 char_u *key = tv_idx->vval.v_string;
1943 dict_T *dict = tv_dest->vval.v_dict;
1944 dictitem_T *di;
1945
1946 SOURCING_LNUM = iptr->isn_lnum;
1947 if (dict == NULL)
1948 {
1949 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001950 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001951 }
1952 if (key == NULL)
1953 key = (char_u *)"";
1954 di = dict_find(dict, key, -1);
1955 if (di != NULL)
1956 {
1957 if (error_if_locked(di->di_tv.v_lock,
1958 e_cannot_change_dict_item))
1959 return FAIL;
1960 // overwrite existing value
1961 clear_tv(&di->di_tv);
1962 di->di_tv = *tv;
1963 }
1964 else
1965 {
1966 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1967 return FAIL;
1968 // add to dict, only fails when out of memory
1969 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1970 return NOTDONE;
1971 clear_tv(tv);
1972 }
1973 }
1974 else if (dest_type == VAR_BLOB)
1975 {
1976 long lidx = (long)tv_idx->vval.v_number;
1977 blob_T *blob = tv_dest->vval.v_blob;
1978 varnumber_T nr;
1979 int error = FALSE;
1980 int len;
1981
1982 if (blob == NULL)
1983 {
1984 emsg(_(e_blob_not_set));
1985 return FAIL;
1986 }
1987 len = blob_len(blob);
1988 if (lidx < 0 && len + lidx >= 0)
1989 // negative index is relative to the end
1990 lidx = len + lidx;
1991
1992 // Can add one byte at the end.
1993 if (lidx < 0 || lidx > len)
1994 {
1995 semsg(_(e_blob_index_out_of_range_nr), lidx);
1996 return FAIL;
1997 }
1998 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1999 return FAIL;
2000 nr = tv_get_number_chk(tv, &error);
2001 if (error)
2002 return FAIL;
2003 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002004 }
2005 else
2006 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002007 status = FAIL;
2008 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002009 }
2010 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002011
2012 clear_tv(tv_idx);
2013 clear_tv(tv_dest);
2014 ectx->ec_stack.ga_len -= 3;
2015 if (status == FAIL)
2016 {
2017 clear_tv(tv);
2018 return FAIL;
2019 }
2020 return OK;
2021}
2022
2023/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002024 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002025 */
2026 static int
2027execute_storerange(isn_T *iptr, ectx_T *ectx)
2028{
2029 typval_T *tv;
2030 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2031 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2032 typval_T *tv_dest = STACK_TV_BOT(-1);
2033 int status = OK;
2034
2035 // Stack contains:
2036 // -4 value to be stored
2037 // -3 first index or "none"
2038 // -2 second index or "none"
2039 // -1 destination list or blob
2040 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002041 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002042 if (tv_dest->v_type == VAR_LIST)
2043 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002044 long n1;
2045 long n2;
2046 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002047
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002048 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2049 if (tv_idx2->v_type == VAR_SPECIAL
2050 && tv_idx2->vval.v_number == VVAL_NONE)
2051 n2 = list_len(tv_dest->vval.v_list) - 1;
2052 else
2053 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2054
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002055 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002056 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002057 status = FAIL;
2058 else
2059 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002060 status = check_range_index_two(tv_dest->vval.v_list,
2061 &n1, li1, &n2, FALSE);
2062 if (status != FAIL)
2063 status = list_assign_range(
2064 tv_dest->vval.v_list,
2065 tv->vval.v_list,
2066 n1,
2067 n2,
2068 tv_idx2->v_type == VAR_SPECIAL,
2069 (char_u *)"=",
2070 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002071 }
2072 }
2073 else if (tv_dest->v_type == VAR_BLOB)
2074 {
2075 varnumber_T n1;
2076 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002077 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002078
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002079 n1 = tv_get_number_chk(tv_idx1, NULL);
2080 if (tv_idx2->v_type == VAR_SPECIAL
2081 && tv_idx2->vval.v_number == VVAL_NONE)
2082 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2083 else
2084 n2 = tv_get_number_chk(tv_idx2, NULL);
2085 bloblen = blob_len(tv_dest->vval.v_blob);
2086
2087 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2088 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002089 status = FAIL;
2090 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002091 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002092 }
2093 else
2094 {
2095 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002096 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002097 }
2098
2099 clear_tv(tv_idx1);
2100 clear_tv(tv_idx2);
2101 clear_tv(tv_dest);
2102 ectx->ec_stack.ga_len -= 4;
2103 clear_tv(tv);
2104
2105 return status;
2106}
2107
2108/*
2109 * Unlet item in list or dict variable.
2110 */
2111 static int
2112execute_unletindex(isn_T *iptr, ectx_T *ectx)
2113{
2114 typval_T *tv_idx = STACK_TV_BOT(-2);
2115 typval_T *tv_dest = STACK_TV_BOT(-1);
2116 int status = OK;
2117
2118 // Stack contains:
2119 // -2 index
2120 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002121 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002122 if (tv_dest->v_type == VAR_DICT)
2123 {
2124 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002125 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002126 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002127 semsg(_(e_expected_str_but_got_str),
2128 vartype_name(VAR_STRING),
2129 vartype_name(tv_idx->v_type));
2130 status = FAIL;
2131 }
2132 else
2133 {
2134 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002135 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002136 dictitem_T *di = NULL;
2137
2138 if (d != NULL && value_check_lock(
2139 d->dv_lock, NULL, FALSE))
2140 status = FAIL;
2141 else
2142 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002143 if (tv_idx->v_type == VAR_STRING)
2144 {
2145 key = tv_idx->vval.v_string;
2146 if (key == NULL)
2147 key = (char_u *)"";
2148 }
2149 else
2150 {
2151 key = tv_get_string(tv_idx);
2152 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002153 if (d != NULL)
2154 di = dict_find(d, key, (int)STRLEN(key));
2155 if (di == NULL)
2156 {
2157 // NULL dict is equivalent to empty dict
2158 semsg(_(e_key_not_present_in_dictionary),
2159 key);
2160 status = FAIL;
2161 }
2162 else if (var_check_fixed(di->di_flags,
2163 NULL, FALSE)
2164 || var_check_ro(di->di_flags,
2165 NULL, FALSE))
2166 status = FAIL;
2167 else
2168 dictitem_remove(d, di);
2169 }
2170 }
2171 }
2172 else if (tv_dest->v_type == VAR_LIST)
2173 {
2174 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002175 if (check_for_number(tv_idx) == FAIL)
2176 {
2177 status = FAIL;
2178 }
2179 else
2180 {
2181 list_T *l = tv_dest->vval.v_list;
2182 long n = (long)tv_idx->vval.v_number;
2183
2184 if (l != NULL && value_check_lock(
2185 l->lv_lock, NULL, FALSE))
2186 status = FAIL;
2187 else
2188 {
2189 listitem_T *li = list_find(l, n);
2190
2191 if (li == NULL)
2192 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002193 semsg(_(e_list_index_out_of_range_nr), n);
2194 status = FAIL;
2195 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002196 else
2197 listitem_remove(l, li);
2198 }
2199 }
2200 }
2201 else
2202 {
2203 status = FAIL;
2204 semsg(_(e_cannot_index_str),
2205 vartype_name(tv_dest->v_type));
2206 }
2207
2208 clear_tv(tv_idx);
2209 clear_tv(tv_dest);
2210 ectx->ec_stack.ga_len -= 2;
2211
2212 return status;
2213}
2214
2215/*
2216 * Unlet a range of items in a list variable.
2217 */
2218 static int
2219execute_unletrange(isn_T *iptr, ectx_T *ectx)
2220{
2221 // Stack contains:
2222 // -3 index1
2223 // -2 index2
2224 // -1 dict or list
2225 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2226 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2227 typval_T *tv_dest = STACK_TV_BOT(-1);
2228 int status = OK;
2229
2230 if (tv_dest->v_type == VAR_LIST)
2231 {
2232 // indexes must be a number
2233 SOURCING_LNUM = iptr->isn_lnum;
2234 if (check_for_number(tv_idx1) == FAIL
2235 || (tv_idx2->v_type != VAR_SPECIAL
2236 && check_for_number(tv_idx2) == FAIL))
2237 {
2238 status = FAIL;
2239 }
2240 else
2241 {
2242 list_T *l = tv_dest->vval.v_list;
2243 long n1 = (long)tv_idx1->vval.v_number;
2244 long n2 = tv_idx2->v_type == VAR_SPECIAL
2245 ? 0 : (long)tv_idx2->vval.v_number;
2246 listitem_T *li;
2247
2248 li = list_find_index(l, &n1);
2249 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002250 {
2251 semsg(_(e_list_index_out_of_range_nr),
2252 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002253 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002254 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002255 else
2256 {
2257 if (n1 < 0)
2258 n1 = list_idx_of_item(l, li);
2259 if (n2 < 0)
2260 {
2261 listitem_T *li2 = list_find(l, n2);
2262
2263 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002264 {
2265 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002266 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002267 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002268 else
2269 n2 = list_idx_of_item(l, li2);
2270 }
2271 if (status != FAIL
2272 && tv_idx2->v_type != VAR_SPECIAL
2273 && n2 < n1)
2274 {
2275 semsg(_(e_list_index_out_of_range_nr), n2);
2276 status = FAIL;
2277 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002278 if (status != FAIL)
2279 list_unlet_range(l, li, n1,
2280 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002281 }
2282 }
2283 }
2284 else
2285 {
2286 status = FAIL;
2287 SOURCING_LNUM = iptr->isn_lnum;
2288 semsg(_(e_cannot_index_str),
2289 vartype_name(tv_dest->v_type));
2290 }
2291
2292 clear_tv(tv_idx1);
2293 clear_tv(tv_idx2);
2294 clear_tv(tv_dest);
2295 ectx->ec_stack.ga_len -= 3;
2296
2297 return status;
2298}
2299
2300/*
2301 * Top of a for loop.
2302 */
2303 static int
2304execute_for(isn_T *iptr, ectx_T *ectx)
2305{
2306 typval_T *tv;
2307 typval_T *ltv = STACK_TV_BOT(-1);
2308 typval_T *idxtv =
2309 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2310
2311 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2312 return FAIL;
2313 if (ltv->v_type == VAR_LIST)
2314 {
2315 list_T *list = ltv->vval.v_list;
2316
2317 // push the next item from the list
2318 ++idxtv->vval.v_number;
2319 if (list == NULL
2320 || idxtv->vval.v_number >= list->lv_len)
2321 {
2322 // past the end of the list, jump to "endfor"
2323 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2324 may_restore_cmdmod(&ectx->ec_funclocal);
2325 }
2326 else if (list->lv_first == &range_list_item)
2327 {
2328 // non-materialized range() list
2329 tv = STACK_TV_BOT(0);
2330 tv->v_type = VAR_NUMBER;
2331 tv->v_lock = 0;
2332 tv->vval.v_number = list_find_nr(
2333 list, idxtv->vval.v_number, NULL);
2334 ++ectx->ec_stack.ga_len;
2335 }
2336 else
2337 {
2338 listitem_T *li = list_find(list,
2339 idxtv->vval.v_number);
2340
2341 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2342 ++ectx->ec_stack.ga_len;
2343 }
2344 }
2345 else if (ltv->v_type == VAR_STRING)
2346 {
2347 char_u *str = ltv->vval.v_string;
2348
2349 // The index is for the last byte of the previous
2350 // character.
2351 ++idxtv->vval.v_number;
2352 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2353 {
2354 // past the end of the string, jump to "endfor"
2355 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2356 may_restore_cmdmod(&ectx->ec_funclocal);
2357 }
2358 else
2359 {
2360 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2361
2362 // Push the next character from the string.
2363 tv = STACK_TV_BOT(0);
2364 tv->v_type = VAR_STRING;
2365 tv->vval.v_string = vim_strnsave(
2366 str + idxtv->vval.v_number, clen);
2367 ++ectx->ec_stack.ga_len;
2368 idxtv->vval.v_number += clen - 1;
2369 }
2370 }
2371 else if (ltv->v_type == VAR_BLOB)
2372 {
2373 blob_T *blob = ltv->vval.v_blob;
2374
2375 // When we get here the first time make a copy of the
2376 // blob, so that the iteration still works when it is
2377 // changed.
2378 if (idxtv->vval.v_number == -1 && blob != NULL)
2379 {
2380 blob_copy(blob, ltv);
2381 blob_unref(blob);
2382 blob = ltv->vval.v_blob;
2383 }
2384
2385 // The index is for the previous byte.
2386 ++idxtv->vval.v_number;
2387 if (blob == NULL
2388 || idxtv->vval.v_number >= blob_len(blob))
2389 {
2390 // past the end of the blob, jump to "endfor"
2391 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2392 may_restore_cmdmod(&ectx->ec_funclocal);
2393 }
2394 else
2395 {
2396 // Push the next byte from the blob.
2397 tv = STACK_TV_BOT(0);
2398 tv->v_type = VAR_NUMBER;
2399 tv->vval.v_number = blob_get(blob,
2400 idxtv->vval.v_number);
2401 ++ectx->ec_stack.ga_len;
2402 }
2403 }
2404 else
2405 {
2406 semsg(_(e_for_loop_on_str_not_supported),
2407 vartype_name(ltv->v_type));
2408 return FAIL;
2409 }
2410 return OK;
2411}
2412
2413/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002414 * Load instruction for w:/b:/g:/t: variable.
2415 * "isn_type" is used instead of "iptr->isn_type".
2416 */
2417 static int
2418load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2419{
2420 dictitem_T *di = NULL;
2421 hashtab_T *ht = NULL;
2422 char namespace;
2423
2424 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2425 return NOTDONE;
2426 switch (isn_type)
2427 {
2428 case ISN_LOADG:
2429 ht = get_globvar_ht();
2430 namespace = 'g';
2431 break;
2432 case ISN_LOADB:
2433 ht = &curbuf->b_vars->dv_hashtab;
2434 namespace = 'b';
2435 break;
2436 case ISN_LOADW:
2437 ht = &curwin->w_vars->dv_hashtab;
2438 namespace = 'w';
2439 break;
2440 case ISN_LOADT:
2441 ht = &curtab->tp_vars->dv_hashtab;
2442 namespace = 't';
2443 break;
2444 default: // Cannot reach here
2445 return NOTDONE;
2446 }
2447 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2448
Bram Moolenaar06b77222022-01-25 15:51:56 +00002449 if (di == NULL)
2450 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002451 if (isn_type == ISN_LOADG)
2452 {
2453 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2454
2455 // g:Something could be a function
2456 if (ufunc != NULL)
2457 {
2458 typval_T *tv = STACK_TV_BOT(0);
2459
2460 ++ectx->ec_stack.ga_len;
2461 tv->v_type = VAR_FUNC;
2462 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2463 if (tv->vval.v_string == NULL)
2464 return FAIL;
2465 STRCPY(tv->vval.v_string, "g:");
2466 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2467 return OK;
2468 }
2469 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002470 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002471 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002472 // no check if the item exists in the script but
2473 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002474 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002475 else
2476 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002477 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002478 return FAIL;
2479 }
2480 else
2481 {
2482 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2483 ++ectx->ec_stack.ga_len;
2484 }
2485 return OK;
2486}
2487
2488/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002489 * Execute instructions in execution context "ectx".
2490 * Return OK or FAIL;
2491 */
2492 static int
2493exec_instructions(ectx_T *ectx)
2494{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002495 int ret = FAIL;
2496 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002497 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002498
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002499 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002500 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002501
Bram Moolenaarff652882021-05-16 15:24:49 +02002502 // Only catch exceptions in this instruction list.
2503 ectx->ec_trylevel_at_start = trylevel;
2504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 for (;;)
2506 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002507 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002509 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002510
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002511 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002512 {
2513 line_breakcheck();
2514 breakcheck_count = 0;
2515 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002516 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002517 {
2518 // Turn CTRL-C into an exception.
2519 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002520 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002521 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002522 did_throw = TRUE;
2523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002525 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002526 {
2527 // Turn an error message into an exception.
2528 did_emsg = FALSE;
2529 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002530 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002531 did_throw = TRUE;
2532 *msg_list = NULL;
2533 }
2534
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002535 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002537 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002538 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002539 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540
2541 // An exception jumps to the first catch, finally, or returns from
2542 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002543 while (index > 0)
2544 {
2545 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002546 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002547 break;
2548 // In the catch and finally block of this try we have to go up
2549 // one level.
2550 --index;
2551 trycmd = NULL;
2552 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002553 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002555 if (trycmd->tcd_in_catch)
2556 {
2557 // exception inside ":catch", jump to ":finally" once
2558 ectx->ec_iidx = trycmd->tcd_finally_idx;
2559 trycmd->tcd_finally_idx = 0;
2560 }
2561 else
2562 // jump to first ":catch"
2563 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002564 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002565 did_throw = FALSE; // don't come back here until :endtry
2566 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 }
2568 else
2569 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002570 // Not inside try or need to return from current functions.
2571 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002572 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002573 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002574 tv = STACK_TV_BOT(0);
2575 tv->v_type = VAR_NUMBER;
2576 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002577 ++ectx->ec_stack.ga_len;
2578 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002580 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002581 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002582 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002583 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 goto done;
2585 }
2586
Bram Moolenaar4c137212021-04-19 16:48:48 +02002587 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002588 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 }
2590 continue;
2591 }
2592
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002593 /*
2594 * Big switch on the instruction. Most compilers will be turning this
2595 * into an efficient lookup table, since the "case" values are an enum
2596 * with sequential numbers. It may look ugly, but it should be the
2597 * most efficient way.
2598 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002599 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 switch (iptr->isn_type)
2601 {
2602 // execute Ex command line
2603 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002604 if (exec_command(iptr) == FAIL)
2605 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 break;
2607
Bram Moolenaar20677332021-06-06 17:02:53 +02002608 // execute Ex command line split at NL characters.
2609 case ISN_EXEC_SPLIT:
2610 {
2611 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002612 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002613
2614 SOURCING_LNUM = iptr->isn_lnum;
2615 CLEAR_FIELD(cookie);
2616 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2617 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002618 line = get_split_sourceline(0, &cookie, 0, 0);
2619 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002620 get_split_sourceline, &cookie,
2621 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2622 == FAIL
2623 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002624 {
2625 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002626 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002627 }
2628 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002629 }
2630 break;
2631
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002632 // execute Ex command line that is only a range
2633 case ISN_EXECRANGE:
2634 {
2635 exarg_T ea;
2636 char *error = NULL;
2637
2638 CLEAR_FIELD(ea);
2639 ea.cmdidx = CMD_SIZE;
2640 ea.addr_type = ADDR_LINES;
2641 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002642 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002643 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002644 if (ea.cmd == NULL)
2645 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002646 // error is always NULL when using ADDR_LINES
2647 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002648 if (error != NULL)
2649 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002650 emsg(error);
2651 goto on_error;
2652 }
2653 }
2654 break;
2655
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002656 // Evaluate an expression with legacy syntax, push it onto the
2657 // stack.
2658 case ISN_LEGACY_EVAL:
2659 {
2660 char_u *arg = iptr->isn_arg.string;
2661 int res;
2662 int save_flags = cmdmod.cmod_flags;
2663
Bram Moolenaar35578162021-08-02 19:10:38 +02002664 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002665 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002666 tv = STACK_TV_BOT(0);
2667 init_tv(tv);
2668 cmdmod.cmod_flags |= CMOD_LEGACY;
2669 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2670 cmdmod.cmod_flags = save_flags;
2671 if (res == FAIL)
2672 goto on_error;
2673 ++ectx->ec_stack.ga_len;
2674 }
2675 break;
2676
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002677 // push typeval VAR_INSTR with instructions to be executed
2678 case ISN_INSTR:
2679 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002680 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002681 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002682 tv = STACK_TV_BOT(0);
2683 tv->vval.v_instr = ALLOC_ONE(instr_T);
2684 if (tv->vval.v_instr == NULL)
2685 goto on_error;
2686 ++ectx->ec_stack.ga_len;
2687
2688 tv->v_type = VAR_INSTR;
2689 tv->vval.v_instr->instr_ectx = ectx;
2690 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2691 }
2692 break;
2693
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002694 case ISN_SOURCE:
2695 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002696 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002697
Bram Moolenaar89445512022-04-14 12:58:23 +01002698 SOURCING_LNUM = iptr->isn_lnum;
2699 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002700 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002701 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002702 }
2703 break;
2704
Bram Moolenaar4c137212021-04-19 16:48:48 +02002705 // execute :substitute with an expression
2706 case ISN_SUBSTITUTE:
2707 {
2708 subs_T *subs = &iptr->isn_arg.subs;
2709 source_cookie_T cookie;
2710 struct subs_expr_S *save_instr = substitute_instr;
2711 struct subs_expr_S subs_instr;
2712 int res;
2713
2714 subs_instr.subs_ectx = ectx;
2715 subs_instr.subs_instr = subs->subs_instr;
2716 subs_instr.subs_status = OK;
2717 substitute_instr = &subs_instr;
2718
2719 SOURCING_LNUM = iptr->isn_lnum;
2720 // This is very much like ISN_EXEC
2721 CLEAR_FIELD(cookie);
2722 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2723 res = do_cmdline(subs->subs_cmd,
2724 getsourceline, &cookie,
2725 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2726 substitute_instr = save_instr;
2727
2728 if (res == FAIL || did_emsg
2729 || subs_instr.subs_status == FAIL)
2730 goto on_error;
2731 }
2732 break;
2733
2734 case ISN_FINISH:
2735 goto done;
2736
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002737 case ISN_REDIRSTART:
2738 // create a dummy entry for var_redir_str()
2739 if (alloc_redir_lval() == FAIL)
2740 goto on_error;
2741
2742 // The output is stored in growarray "redir_ga" until
2743 // redirection ends.
2744 init_redir_ga();
2745 redir_vname = 1;
2746 break;
2747
2748 case ISN_REDIREND:
2749 {
2750 char_u *res = get_clear_redir_ga();
2751
2752 // End redirection, put redirected text on the stack.
2753 clear_redir_lval();
2754 redir_vname = 0;
2755
Bram Moolenaar35578162021-08-02 19:10:38 +02002756 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002757 {
2758 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002759 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002760 }
2761 tv = STACK_TV_BOT(0);
2762 tv->v_type = VAR_STRING;
2763 tv->vval.v_string = res;
2764 ++ectx->ec_stack.ga_len;
2765 }
2766 break;
2767
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002768 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002769#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002770 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002771 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2772 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002773 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002774#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002775 break;
2776
2777 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002778#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002779 {
2780 exarg_T ea;
2781 int res;
2782
2783 CLEAR_FIELD(ea);
2784 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2785 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2786 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2787 --ectx->ec_stack.ga_len;
2788 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01002789 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002790 res = cexpr_core(&ea, tv);
2791 clear_tv(tv);
2792 if (res == FAIL)
2793 goto on_error;
2794 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002795#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002796 break;
2797
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002798 // execute Ex command from pieces on the stack
2799 case ISN_EXECCONCAT:
2800 {
2801 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002802 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002803 int pass;
2804 int i;
2805 char_u *cmd = NULL;
2806 char_u *str;
2807
2808 for (pass = 1; pass <= 2; ++pass)
2809 {
2810 for (i = 0; i < count; ++i)
2811 {
2812 tv = STACK_TV_BOT(i - count);
2813 str = tv->vval.v_string;
2814 if (str != NULL && *str != NUL)
2815 {
2816 if (pass == 2)
2817 STRCPY(cmd + len, str);
2818 len += STRLEN(str);
2819 }
2820 if (pass == 2)
2821 clear_tv(tv);
2822 }
2823 if (pass == 1)
2824 {
2825 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002826 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002827 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002828 len = 0;
2829 }
2830 }
2831
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002832 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002833 do_cmdline_cmd(cmd);
2834 vim_free(cmd);
2835 }
2836 break;
2837
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 // execute :echo {string} ...
2839 case ISN_ECHO:
2840 {
2841 int count = iptr->isn_arg.echo.echo_count;
2842 int atstart = TRUE;
2843 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002844 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845
2846 for (idx = 0; idx < count; ++idx)
2847 {
2848 tv = STACK_TV_BOT(idx - count);
2849 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2850 &atstart, &needclr);
2851 clear_tv(tv);
2852 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002853 if (needclr)
2854 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002855 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 }
2857 break;
2858
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002859 // :execute {string} ...
2860 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002861 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002862 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002863 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002864 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002865 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002866 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002867 {
2868 int count = iptr->isn_arg.number;
2869 garray_T ga;
2870 char_u buf[NUMBUFLEN];
2871 char_u *p;
2872 int len;
2873 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002874 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002875
2876 ga_init2(&ga, 1, 80);
2877 for (idx = 0; idx < count; ++idx)
2878 {
2879 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002880 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002881 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002882 if (tv->v_type == VAR_CHANNEL
2883 || tv->v_type == VAR_JOB)
2884 {
2885 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002886 semsg(_(e_using_invalid_value_as_string_str),
2887 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002888 break;
2889 }
2890 else
2891 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002892 }
2893 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002894 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002895
2896 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002897 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002898 failed = TRUE;
2899 else
2900 {
2901 if (ga.ga_len > 0)
2902 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2903 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2904 ga.ga_len += len;
2905 }
2906 clear_tv(tv);
2907 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002908 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002909 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002910 {
2911 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002912 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002913 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002914
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002915 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002916 {
2917 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002918 {
2919 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002920 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002921 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002922 {
2923 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002924 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002925 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002926 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002927 else
2928 {
2929 msg_sb_eol();
2930 if (iptr->isn_type == ISN_ECHOMSG)
2931 {
2932 msg_attr(ga.ga_data, echo_attr);
2933 out_flush();
2934 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002935 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2936 {
2937 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2938 TRUE);
2939 ui_write((char_u *)"\r\n", 2, TRUE);
2940 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002941 else
2942 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002943 SOURCING_LNUM = iptr->isn_lnum;
2944 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002945 }
2946 }
2947 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002948 ga_clear(&ga);
2949 }
2950 break;
2951
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 // load local variable or argument
2953 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002954 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002955 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002957 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 break;
2959
2960 // load v: variable
2961 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002962 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002963 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002965 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 break;
2967
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002968 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 case ISN_LOADSCRIPT:
2970 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002971 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 svar_T *sv;
2973
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002974 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002975 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002976 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002977 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002978 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002979 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002981 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 }
2983 break;
2984
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002985 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002987 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002989 int sid = iptr->isn_arg.loadstore.ls_sid;
2990 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002991 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 if (di == NULL)
2995 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002996 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002997 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002998 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 }
3000 else
3001 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003002 if (iptr->isn_type == ISN_LOADEXPORT)
3003 {
3004 int idx = get_script_item_idx(sid, name, 0,
3005 NULL, NULL);
3006 svar_T *sv;
3007
3008 if (idx >= 0)
3009 {
3010 sv = ((svar_T *)SCRIPT_ITEM(sid)
3011 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003012 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003013 {
3014 SOURCING_LNUM = iptr->isn_lnum;
3015 semsg(_(e_item_not_exported_in_script_str),
3016 name);
3017 goto on_error;
3018 }
3019 }
3020 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003021 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003022 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003024 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 }
3026 }
3027 break;
3028
Bram Moolenaard3aac292020-04-19 14:32:17 +02003029 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003031 case ISN_LOADB:
3032 case ISN_LOADW:
3033 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003035 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003036
Bram Moolenaar06b77222022-01-25 15:51:56 +00003037 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003038 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003039 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003040 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 break;
3044
Bram Moolenaar03290b82020-12-19 16:30:44 +01003045 // load autoload variable
3046 case ISN_LOADAUTO:
3047 {
3048 char_u *name = iptr->isn_arg.string;
3049
Bram Moolenaar35578162021-08-02 19:10:38 +02003050 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003051 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003052 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003053 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003054 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003055 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003056 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003057 }
3058 break;
3059
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003060 // load g:/b:/w:/t: namespace
3061 case ISN_LOADGDICT:
3062 case ISN_LOADBDICT:
3063 case ISN_LOADWDICT:
3064 case ISN_LOADTDICT:
3065 {
3066 dict_T *d = NULL;
3067
3068 switch (iptr->isn_type)
3069 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003070 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3071 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3072 case ISN_LOADWDICT: d = curwin->w_vars; break;
3073 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003074 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003075 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003076 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003077 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003078 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003079 tv = STACK_TV_BOT(0);
3080 tv->v_type = VAR_DICT;
3081 tv->v_lock = 0;
3082 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003083 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003084 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003085 }
3086 break;
3087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 // load &option
3089 case ISN_LOADOPT:
3090 {
3091 typval_T optval;
3092 char_u *name = iptr->isn_arg.string;
3093
Bram Moolenaara8c17702020-04-01 21:17:24 +02003094 // This is not expected to fail, name is checked during
3095 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003096 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003097 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003098 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003099 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003101 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 }
3103 break;
3104
3105 // load $ENV
3106 case ISN_LOADENV:
3107 {
3108 typval_T optval;
3109 char_u *name = iptr->isn_arg.string;
3110
Bram Moolenaar35578162021-08-02 19:10:38 +02003111 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003112 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003113 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003114 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003116 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 }
3118 break;
3119
3120 // load @register
3121 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003122 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003123 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 tv = STACK_TV_BOT(0);
3125 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003126 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003127 // This may result in NULL, which should be equivalent to an
3128 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 tv->vval.v_string = get_reg_contents(
3130 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003131 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 break;
3133
3134 // store local variable
3135 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003136 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 tv = STACK_TV_VAR(iptr->isn_arg.number);
3138 clear_tv(tv);
3139 *tv = *STACK_TV_BOT(0);
3140 break;
3141
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003142 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003143 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003144 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003145 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003146 int sid = iptr->isn_arg.loadstore.ls_sid;
3147 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003148 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003149 dictitem_T *di = find_var_in_ht(ht, 0,
3150 iptr->isn_type == ISN_STORES
3151 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003152
Bram Moolenaar4c137212021-04-19 16:48:48 +02003153 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003154 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003155 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003156 {
3157 if (iptr->isn_type == ISN_STOREEXPORT)
3158 {
3159 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003160 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003161 goto on_error;
3162 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003163 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003164 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003165 else
3166 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003167 if (iptr->isn_type == ISN_STOREEXPORT)
3168 {
3169 int idx = get_script_item_idx(sid, name, 0,
3170 NULL, NULL);
3171
Bram Moolenaar06651632022-04-27 17:54:25 +01003172 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003173 if (idx >= 0)
3174 {
3175 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3176 ->sn_var_vals.ga_data) + idx;
3177
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003178 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003179 {
3180 semsg(_(e_item_not_exported_in_script_str),
3181 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003182 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003183 goto on_error;
3184 }
3185 }
3186 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003187 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003188 {
3189 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003190 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003191 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003192 clear_tv(&di->di_tv);
3193 di->di_tv = *STACK_TV_BOT(0);
3194 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003195 }
3196 break;
3197
3198 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 case ISN_STORESCRIPT:
3200 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003201 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3202 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003204 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003205 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003206 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003207 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003208
3209 // "const" and "final" are checked at compile time, locking
3210 // the value needs to be checked here.
3211 SOURCING_LNUM = iptr->isn_lnum;
3212 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003213 {
3214 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003215 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003216 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 clear_tv(sv->sv_tv);
3219 *sv->sv_tv = *STACK_TV_BOT(0);
3220 }
3221 break;
3222
3223 // store option
3224 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003225 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003227 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3228 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 long n = 0;
3230 char_u *s = NULL;
3231 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003232 char_u numbuf[NUMBUFLEN];
3233 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234
Bram Moolenaar4c137212021-04-19 16:48:48 +02003235 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 tv = STACK_TV_BOT(0);
3237 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003238 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003240 if (s == NULL)
3241 s = (char_u *)"";
3242 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003243 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3244 {
3245 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003246 // If the option can be set to a function reference or
3247 // a lambda and the passed value is a function
3248 // reference, then convert it to the name (string) of
3249 // the function reference.
3250 s = tv2string(tv, &tofree, numbuf, 0);
3251 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003252 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003253 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003254 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003255 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003256 goto on_error;
3257 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003260 // must be VAR_NUMBER, CHECKTYPE makes sure
3261 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003262 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003263 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003264 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 if (msg != NULL)
3266 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003267 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003269 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 }
3272 break;
3273
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003274 // store $ENV
3275 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003276 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003277 tv = STACK_TV_BOT(0);
3278 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3279 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003280 break;
3281
3282 // store @r
3283 case ISN_STOREREG:
3284 {
3285 int reg = iptr->isn_arg.number;
3286
Bram Moolenaar4c137212021-04-19 16:48:48 +02003287 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003288 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003289 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003290 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003291 }
3292 break;
3293
3294 // store v: variable
3295 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003296 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003297 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3298 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003299 // should not happen, type is checked when compiling
3300 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003301 break;
3302
Bram Moolenaard3aac292020-04-19 14:32:17 +02003303 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003305 case ISN_STOREB:
3306 case ISN_STOREW:
3307 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003309 dictitem_T *di;
3310 hashtab_T *ht;
3311 char_u *name = iptr->isn_arg.string + 2;
3312
Bram Moolenaard3aac292020-04-19 14:32:17 +02003313 switch (iptr->isn_type)
3314 {
3315 case ISN_STOREG:
3316 ht = get_globvar_ht();
3317 break;
3318 case ISN_STOREB:
3319 ht = &curbuf->b_vars->dv_hashtab;
3320 break;
3321 case ISN_STOREW:
3322 ht = &curwin->w_vars->dv_hashtab;
3323 break;
3324 case ISN_STORET:
3325 ht = &curtab->tp_vars->dv_hashtab;
3326 break;
3327 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003328 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330
Bram Moolenaar4c137212021-04-19 16:48:48 +02003331 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003332 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003334 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 else
3336 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003337 SOURCING_LNUM = iptr->isn_lnum;
3338 if (var_check_permission(di, name) == FAIL)
3339 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 clear_tv(&di->di_tv);
3341 di->di_tv = *STACK_TV_BOT(0);
3342 }
3343 }
3344 break;
3345
Bram Moolenaar03290b82020-12-19 16:30:44 +01003346 // store an autoload variable
3347 case ISN_STOREAUTO:
3348 SOURCING_LNUM = iptr->isn_lnum;
3349 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3350 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003351 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003352 break;
3353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 // store number in local variable
3355 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003356 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 clear_tv(tv);
3358 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003359 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 break;
3361
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003362 // store value in list or dict variable
3363 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003364 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003365 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003366
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003367 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003368 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003369 if (res == NOTDONE)
3370 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003371 }
3372 break;
3373
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003374 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003375 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003376 if (execute_storerange(iptr, ectx) == FAIL)
3377 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003378 break;
3379
Bram Moolenaar0186e582021-01-10 18:33:11 +01003380 // load or store variable or argument from outer scope
3381 case ISN_LOADOUTER:
3382 case ISN_STOREOUTER:
3383 {
3384 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003385 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3386 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003387
3388 while (depth > 1 && outer != NULL)
3389 {
3390 outer = outer->out_up;
3391 --depth;
3392 }
3393 if (outer == NULL)
3394 {
3395 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003396 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3397 || ectx->ec_outer_ref == NULL)
3398 // Possibly :def function called from legacy
3399 // context.
3400 emsg(_(e_closure_called_from_invalid_context));
3401 else
3402 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003403 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003404 }
3405 tv = ((typval_T *)outer->out_stack->ga_data)
3406 + outer->out_frame_idx + STACK_FRAME_SIZE
3407 + iptr->isn_arg.outer.outer_idx;
3408 if (iptr->isn_type == ISN_LOADOUTER)
3409 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003410 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003411 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003412 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003413 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003414 }
3415 else
3416 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003417 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003418 clear_tv(tv);
3419 *tv = *STACK_TV_BOT(0);
3420 }
3421 }
3422 break;
3423
Bram Moolenaar752fc692021-01-04 21:57:11 +01003424 // unlet item in list or dict variable
3425 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003426 if (execute_unletindex(iptr, ectx) == FAIL)
3427 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003428 break;
3429
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003430 // unlet range of items in list variable
3431 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003432 if (execute_unletrange(iptr, ectx) == FAIL)
3433 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003434 break;
3435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 // push constant
3437 case ISN_PUSHNR:
3438 case ISN_PUSHBOOL:
3439 case ISN_PUSHSPEC:
3440 case ISN_PUSHF:
3441 case ISN_PUSHS:
3442 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003443 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003444 case ISN_PUSHCHANNEL:
3445 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003446 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003447 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003449 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003450 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 switch (iptr->isn_type)
3452 {
3453 case ISN_PUSHNR:
3454 tv->v_type = VAR_NUMBER;
3455 tv->vval.v_number = iptr->isn_arg.number;
3456 break;
3457 case ISN_PUSHBOOL:
3458 tv->v_type = VAR_BOOL;
3459 tv->vval.v_number = iptr->isn_arg.number;
3460 break;
3461 case ISN_PUSHSPEC:
3462 tv->v_type = VAR_SPECIAL;
3463 tv->vval.v_number = iptr->isn_arg.number;
3464 break;
3465#ifdef FEAT_FLOAT
3466 case ISN_PUSHF:
3467 tv->v_type = VAR_FLOAT;
3468 tv->vval.v_float = iptr->isn_arg.fnumber;
3469 break;
3470#endif
3471 case ISN_PUSHBLOB:
3472 blob_copy(iptr->isn_arg.blob, tv);
3473 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003474 case ISN_PUSHFUNC:
3475 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003476 if (iptr->isn_arg.string == NULL)
3477 tv->vval.v_string = NULL;
3478 else
3479 tv->vval.v_string =
3480 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003481 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003482 case ISN_PUSHCHANNEL:
3483#ifdef FEAT_JOB_CHANNEL
3484 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003485 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003486#endif
3487 break;
3488 case ISN_PUSHJOB:
3489#ifdef FEAT_JOB_CHANNEL
3490 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003491 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003492#endif
3493 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 default:
3495 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003496 tv->vval.v_string = iptr->isn_arg.string == NULL
3497 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 }
3499 break;
3500
Bram Moolenaar06b77222022-01-25 15:51:56 +00003501 case ISN_AUTOLOAD:
3502 {
3503 char_u *name = iptr->isn_arg.string;
3504
3505 (void)script_autoload(name, FALSE);
3506 if (find_func(name, TRUE))
3507 {
3508 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3509 goto theend;
3510 tv = STACK_TV_BOT(0);
3511 tv->v_lock = 0;
3512 ++ectx->ec_stack.ga_len;
3513 tv->v_type = VAR_FUNC;
3514 tv->vval.v_string = vim_strsave(name);
3515 }
3516 else
3517 {
3518 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3519
3520 if (res == NOTDONE)
3521 goto theend;
3522 if (res == FAIL)
3523 goto on_error;
3524 }
3525 }
3526 break;
3527
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003528 case ISN_UNLET:
3529 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3530 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003531 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003532 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003533 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003534 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003535 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003536
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003537 case ISN_LOCKUNLOCK:
3538 {
3539 typval_T *lval_root_save = lval_root;
3540 int res;
3541
3542 // Stack has the local variable, argument the whole :lock
3543 // or :unlock command, like ISN_EXEC.
3544 --ectx->ec_stack.ga_len;
3545 lval_root = STACK_TV_BOT(0);
3546 res = exec_command(iptr);
3547 clear_tv(lval_root);
3548 lval_root = lval_root_save;
3549 if (res == FAIL)
3550 goto on_error;
3551 }
3552 break;
3553
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003554 case ISN_LOCKCONST:
3555 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3556 break;
3557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 // create a list from items on the stack; uses a single allocation
3559 // for the list header and the items
3560 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003561 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003562 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 break;
3564
3565 // create a dict from items on the stack
3566 case ISN_NEWDICT:
3567 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003568 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003570 SOURCING_LNUM = iptr->isn_lnum;
3571 res = exe_newdict(iptr->isn_arg.number, ectx);
3572 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003573 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003574 if (res == MAYBE)
3575 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 }
3577 break;
3578
LemonBoy372bcce2022-04-25 12:43:20 +01003579 case ISN_CONCAT:
3580 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3581 goto theend;
3582 break;
3583
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003584 // create a partial with NULL value
3585 case ISN_NEWPARTIAL:
3586 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3587 goto theend;
3588 ++ectx->ec_stack.ga_len;
3589 tv = STACK_TV_BOT(-1);
3590 tv->v_type = VAR_PARTIAL;
3591 tv->v_lock = 0;
3592 tv->vval.v_partial = NULL;
3593 break;
3594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 // call a :def function
3596 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003597 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003598 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3599 NULL,
3600 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003601 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003602 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 break;
3604
3605 // call a builtin function
3606 case ISN_BCALL:
3607 SOURCING_LNUM = iptr->isn_lnum;
3608 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3609 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003610 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003611 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 break;
3613
3614 // call a funcref or partial
3615 case ISN_PCALL:
3616 {
3617 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3618 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003619 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620
3621 SOURCING_LNUM = iptr->isn_lnum;
3622 if (pfunc->cpf_top)
3623 {
3624 // funcref is above the arguments
3625 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3626 }
3627 else
3628 {
3629 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003630 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003631 partial_tv = *STACK_TV_BOT(0);
3632 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003634 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003635 if (tv == &partial_tv)
3636 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003638 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 }
3640 break;
3641
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003642 case ISN_PCALL_END:
3643 // PCALL finished, arguments have been consumed and replaced by
3644 // the return value. Now clear the funcref from the stack,
3645 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003646 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003647 clear_tv(STACK_TV_BOT(-1));
3648 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3649 break;
3650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 // call a user defined function or funcref/partial
3652 case ISN_UCALL:
3653 {
3654 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3655
3656 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003657 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003658 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003659 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 }
3661 break;
3662
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003663 // return from a :def function call without a value
3664 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003665 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003666 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003667 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003668 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003669 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003670 tv->vval.v_number = 0;
3671 tv->v_lock = 0;
3672 // FALLTHROUGH
3673
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003674 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 case ISN_RETURN:
3676 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003677 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003678 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003679
3680 if (trystack->ga_len > 0)
3681 trycmd = ((trycmd_T *)trystack->ga_data)
3682 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003683 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003684 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003686 // jump to ":finally" or ":endtry"
3687 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003688 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003689 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003690 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 trycmd->tcd_return = TRUE;
3692 }
3693 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003694 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 }
3696 break;
3697
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003698 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 case ISN_FUNCREF:
3700 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003701 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003702 ufunc_T *ufunc;
3703 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003706 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003707 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003708 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003709 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003710 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003711 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003712 if (funcref->fr_func_name == NULL)
3713 {
3714 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3715 + funcref->fr_dfunc_idx;
3716
3717 ufunc = pt_dfunc->df_ufunc;
3718 }
3719 else
3720 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003721 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003722 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003723 if (ufunc == NULL)
3724 {
3725 SOURCING_LNUM = iptr->isn_lnum;
3726 iemsg("ufunc unexpectedly NULL for FUNCREF");
3727 goto theend;
3728 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003729 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003730 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003732 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 tv->vval.v_partial = pt;
3734 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003735 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 }
3737 break;
3738
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003739 // Create a global function from a lambda.
3740 case ISN_NEWFUNC:
3741 {
3742 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3743
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003744 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003745 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003746 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003747 }
3748 break;
3749
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003750 // List functions
3751 case ISN_DEF:
3752 if (iptr->isn_arg.string == NULL)
3753 list_functions(NULL);
3754 else
3755 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003756 exarg_T ea;
3757 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003758
3759 CLEAR_FIELD(ea);
3760 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003761 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3762 define_function(&ea, NULL, &lines_to_free);
3763 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003764 }
3765 break;
3766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 // jump if a condition is met
3768 case ISN_JUMP:
3769 {
3770 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003771 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 int jump = TRUE;
3773
3774 if (when != JUMP_ALWAYS)
3775 {
3776 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003777 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003778 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003779 || when == JUMP_IF_COND_TRUE)
3780 {
3781 SOURCING_LNUM = iptr->isn_lnum;
3782 jump = tv_get_bool_chk(tv, &error);
3783 if (error)
3784 goto on_error;
3785 }
3786 else
3787 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01003788 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003790 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 {
3792 // drop the value from the stack
3793 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003794 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 }
3796 }
3797 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003798 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 }
3800 break;
3801
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003802 // Jump if an argument with a default value was already set and not
3803 // v:none.
3804 case ISN_JUMP_IF_ARG_SET:
3805 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3806 if (tv->v_type != VAR_UNKNOWN
3807 && !(tv->v_type == VAR_SPECIAL
3808 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003809 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003810 break;
3811
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 // top of a for loop
3813 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003814 if (execute_for(iptr, ectx) == FAIL)
3815 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 break;
3817
3818 // start of ":try" block
3819 case ISN_TRY:
3820 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003821 trycmd_T *trycmd = NULL;
3822
Bram Moolenaar35578162021-08-02 19:10:38 +02003823 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003824 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003825 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3826 + ectx->ec_trystack.ga_len;
3827 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003829 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003830 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3831 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003832 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003833 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003834 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003835 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003836 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003837 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 }
3839 break;
3840
3841 case ISN_PUSHEXC:
3842 if (current_exception == NULL)
3843 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003844 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003846 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003848 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003849 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003851 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003853 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 tv->vval.v_string = vim_strsave(
3855 (char_u *)current_exception->value);
3856 break;
3857
3858 case ISN_CATCH:
3859 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003860 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01003861 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862
Bram Moolenaar4c137212021-04-19 16:48:48 +02003863 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01003864 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01003866 trycmd->tcd_caught = TRUE;
3867 trycmd->tcd_did_throw = FALSE;
3868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003870 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 catch_exception(current_exception);
3872 }
3873 break;
3874
Bram Moolenaarc150c092021-02-13 15:02:46 +01003875 case ISN_TRYCONT:
3876 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003877 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003878 trycont_T *trycont = &iptr->isn_arg.trycont;
3879 int i;
3880 trycmd_T *trycmd;
3881 int iidx = trycont->tct_where;
3882
3883 if (trystack->ga_len < trycont->tct_levels)
3884 {
3885 siemsg("TRYCONT: expected %d levels, found %d",
3886 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003887 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003888 }
3889 // Make :endtry jump to any outer try block and the last
3890 // :endtry inside the loop to the loop start.
3891 for (i = trycont->tct_levels; i > 0; --i)
3892 {
3893 trycmd = ((trycmd_T *)trystack->ga_data)
3894 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003895 // Add one to tcd_cont to be able to jump to
3896 // instruction with index zero.
3897 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003898 iidx = trycmd->tcd_finally_idx == 0
3899 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003900 }
3901 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003902 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003903 }
3904 break;
3905
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003906 case ISN_FINALLY:
3907 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003908 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003909 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3910 + trystack->ga_len - 1;
3911
3912 // Reset the index to avoid a return statement jumps here
3913 // again.
3914 trycmd->tcd_finally_idx = 0;
3915 break;
3916 }
3917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 // end of ":try" block
3919 case ISN_ENDTRY:
3920 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003921 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01003922 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923
Bram Moolenaar06651632022-04-27 17:54:25 +01003924 --trystack->ga_len;
3925 --trylevel;
3926 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
3927 if (trycmd->tcd_did_throw)
3928 did_throw = TRUE;
3929 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 {
Bram Moolenaar06651632022-04-27 17:54:25 +01003931 // discard the exception
3932 if (caught_stack == current_exception)
3933 caught_stack = caught_stack->caught;
3934 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 }
Bram Moolenaar06651632022-04-27 17:54:25 +01003936
3937 if (trycmd->tcd_return)
3938 goto func_return;
3939
3940 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
3941 {
3942 --ectx->ec_stack.ga_len;
3943 clear_tv(STACK_TV_BOT(0));
3944 }
3945 if (trycmd->tcd_cont != 0)
3946 // handling :continue: jump to outer try block or
3947 // start of the loop
3948 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 }
3950 break;
3951
3952 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003953 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003954 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003955
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003956 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3957 {
3958 // throwing an exception while using "silent!" causes
3959 // the function to abort but not display an error.
3960 tv = STACK_TV_BOT(-1);
3961 clear_tv(tv);
3962 tv->v_type = VAR_NUMBER;
3963 tv->vval.v_number = 0;
3964 goto done;
3965 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003966 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003967 tv = STACK_TV_BOT(0);
3968 if (tv->vval.v_string == NULL
3969 || *skipwhite(tv->vval.v_string) == NUL)
3970 {
3971 vim_free(tv->vval.v_string);
3972 SOURCING_LNUM = iptr->isn_lnum;
3973 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003974 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003975 }
3976
3977 // Inside a "catch" we need to first discard the caught
3978 // exception.
3979 if (trystack->ga_len > 0)
3980 {
3981 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3982 + trystack->ga_len - 1;
3983 if (trycmd->tcd_caught && current_exception != NULL)
3984 {
3985 // discard the exception
3986 if (caught_stack == current_exception)
3987 caught_stack = caught_stack->caught;
3988 discard_current_exception();
3989 trycmd->tcd_caught = FALSE;
3990 }
3991 }
3992
Bram Moolenaar90a57162022-02-12 14:23:17 +00003993 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003994 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3995 == FAIL)
3996 {
3997 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003998 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003999 }
4000 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 break;
4003
4004 // compare with special values
4005 case ISN_COMPAREBOOL:
4006 case ISN_COMPARESPECIAL:
4007 {
4008 typval_T *tv1 = STACK_TV_BOT(-2);
4009 typval_T *tv2 = STACK_TV_BOT(-1);
4010 varnumber_T arg1 = tv1->vval.v_number;
4011 varnumber_T arg2 = tv2->vval.v_number;
4012 int res;
4013
Bram Moolenaar06651632022-04-27 17:54:25 +01004014 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4015 res = arg1 == arg2;
4016 else
4017 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018
Bram Moolenaar4c137212021-04-19 16:48:48 +02004019 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 tv1->v_type = VAR_BOOL;
4021 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4022 }
4023 break;
4024
Bram Moolenaar7a222242022-03-01 19:23:24 +00004025 case ISN_COMPARENULL:
4026 {
4027 typval_T *tv1 = STACK_TV_BOT(-2);
4028 typval_T *tv2 = STACK_TV_BOT(-1);
4029 int res;
4030
4031 res = typval_compare_null(tv1, tv2);
4032 if (res == MAYBE)
4033 goto on_error;
4034 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4035 res = !res;
4036 clear_tv(tv1);
4037 clear_tv(tv2);
4038 --ectx->ec_stack.ga_len;
4039 tv1->v_type = VAR_BOOL;
4040 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4041 }
4042 break;
4043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 // Operation with two number arguments
4045 case ISN_OPNR:
4046 case ISN_COMPARENR:
4047 {
4048 typval_T *tv1 = STACK_TV_BOT(-2);
4049 typval_T *tv2 = STACK_TV_BOT(-1);
4050 varnumber_T arg1 = tv1->vval.v_number;
4051 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004052 varnumber_T res = 0;
4053 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004055 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4056 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4057 {
4058 if (arg2 < 0)
4059 {
4060 SOURCING_LNUM = iptr->isn_lnum;
4061 emsg(_(e_bitshift_ops_must_be_postive));
4062 goto on_error;
4063 }
4064 }
4065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 switch (iptr->isn_arg.op.op_type)
4067 {
4068 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004069 case EXPR_DIV: if (arg2 == 0)
4070 div_zero = TRUE;
4071 else
4072 res = arg1 / arg2;
4073 break;
4074 case EXPR_REM: if (arg2 == 0)
4075 div_zero = TRUE;
4076 else
4077 res = arg1 % arg2;
4078 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 case EXPR_SUB: res = arg1 - arg2; break;
4080 case EXPR_ADD: res = arg1 + arg2; break;
4081
4082 case EXPR_EQUAL: res = arg1 == arg2; break;
4083 case EXPR_NEQUAL: res = arg1 != arg2; break;
4084 case EXPR_GREATER: res = arg1 > arg2; break;
4085 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4086 case EXPR_SMALLER: res = arg1 < arg2; break;
4087 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004088 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4089 res = 0;
4090 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004091 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004092 break;
4093 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4094 res = 0;
4095 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004096 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004097 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004098 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 }
4100
Bram Moolenaar4c137212021-04-19 16:48:48 +02004101 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 if (iptr->isn_type == ISN_COMPARENR)
4103 {
4104 tv1->v_type = VAR_BOOL;
4105 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4106 }
4107 else
4108 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004109 if (div_zero)
4110 {
4111 SOURCING_LNUM = iptr->isn_lnum;
4112 emsg(_(e_divide_by_zero));
4113 goto on_error;
4114 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 }
4116 break;
4117
4118 // Computation with two float arguments
4119 case ISN_OPFLOAT:
4120 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004121#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 {
4123 typval_T *tv1 = STACK_TV_BOT(-2);
4124 typval_T *tv2 = STACK_TV_BOT(-1);
4125 float_T arg1 = tv1->vval.v_float;
4126 float_T arg2 = tv2->vval.v_float;
4127 float_T res = 0;
4128 int cmp = FALSE;
4129
4130 switch (iptr->isn_arg.op.op_type)
4131 {
4132 case EXPR_MULT: res = arg1 * arg2; break;
4133 case EXPR_DIV: res = arg1 / arg2; break;
4134 case EXPR_SUB: res = arg1 - arg2; break;
4135 case EXPR_ADD: res = arg1 + arg2; break;
4136
4137 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4138 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4139 case EXPR_GREATER: cmp = arg1 > arg2; break;
4140 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4141 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4142 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4143 default: cmp = 0; break;
4144 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004145 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 if (iptr->isn_type == ISN_COMPAREFLOAT)
4147 {
4148 tv1->v_type = VAR_BOOL;
4149 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4150 }
4151 else
4152 tv1->vval.v_float = res;
4153 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004154#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 break;
4156
4157 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004158 case ISN_COMPAREDICT:
4159 case ISN_COMPAREFUNC:
4160 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 case ISN_COMPAREBLOB:
4162 {
4163 typval_T *tv1 = STACK_TV_BOT(-2);
4164 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004165 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4166 int ic = iptr->isn_arg.op.op_ic;
4167 int res = FALSE;
4168 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169
Bram Moolenaar265f8112021-12-19 12:33:05 +00004170 SOURCING_LNUM = iptr->isn_lnum;
4171 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004173 status = typval_compare_list(tv1, tv2,
4174 exprtype, ic, &res);
4175 }
4176 else if (iptr->isn_type == ISN_COMPAREDICT)
4177 {
4178 status = typval_compare_dict(tv1, tv2,
4179 exprtype, ic, &res);
4180 }
4181 else if (iptr->isn_type == ISN_COMPAREFUNC)
4182 {
4183 status = typval_compare_func(tv1, tv2,
4184 exprtype, ic, &res);
4185 }
4186 else if (iptr->isn_type == ISN_COMPARESTRING)
4187 {
4188 status = typval_compare_string(tv1, tv2,
4189 exprtype, ic, &res);
4190 }
4191 else
4192 {
4193 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004195 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 clear_tv(tv1);
4197 clear_tv(tv2);
4198 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004199 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4200 if (status == FAIL)
4201 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 }
4203 break;
4204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 case ISN_COMPAREANY:
4206 {
4207 typval_T *tv1 = STACK_TV_BOT(-2);
4208 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004209 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004211 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212
Bram Moolenaareb26f432020-09-14 16:50:05 +02004213 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004214 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004216 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004217 if (status == FAIL)
4218 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 }
4220 break;
4221
4222 case ISN_ADDLIST:
4223 case ISN_ADDBLOB:
4224 {
4225 typval_T *tv1 = STACK_TV_BOT(-2);
4226 typval_T *tv2 = STACK_TV_BOT(-1);
4227
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004228 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004230 {
4231 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4232 && tv1->vval.v_list != NULL)
4233 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4234 NULL);
4235 else
4236 eval_addlist(tv1, tv2);
4237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 else
4239 eval_addblob(tv1, tv2);
4240 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004241 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 }
4243 break;
4244
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004245 case ISN_LISTAPPEND:
4246 {
4247 typval_T *tv1 = STACK_TV_BOT(-2);
4248 typval_T *tv2 = STACK_TV_BOT(-1);
4249 list_T *l = tv1->vval.v_list;
4250
4251 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004252 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004253 if (l == NULL)
4254 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004255 emsg(_(e_cannot_add_to_null_list));
4256 goto on_error;
4257 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004258 if (value_check_lock(l->lv_lock, NULL, FALSE))
4259 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004260 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004261 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004262 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004263 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004264 }
4265 break;
4266
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004267 case ISN_BLOBAPPEND:
4268 {
4269 typval_T *tv1 = STACK_TV_BOT(-2);
4270 typval_T *tv2 = STACK_TV_BOT(-1);
4271 blob_T *b = tv1->vval.v_blob;
4272 int error = FALSE;
4273 varnumber_T n;
4274
4275 // add a number to a blob
4276 if (b == NULL)
4277 {
4278 SOURCING_LNUM = iptr->isn_lnum;
4279 emsg(_(e_cannot_add_to_null_blob));
4280 goto on_error;
4281 }
4282 n = tv_get_number_chk(tv2, &error);
4283 if (error)
4284 goto on_error;
4285 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004286 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004287 }
4288 break;
4289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 // Computation with two arguments of unknown type
4291 case ISN_OPANY:
4292 {
4293 typval_T *tv1 = STACK_TV_BOT(-2);
4294 typval_T *tv2 = STACK_TV_BOT(-1);
4295 varnumber_T n1, n2;
4296#ifdef FEAT_FLOAT
4297 float_T f1 = 0, f2 = 0;
4298#endif
4299 int error = FALSE;
4300
4301 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4302 {
4303 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4304 {
4305 eval_addlist(tv1, tv2);
4306 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004307 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 break;
4309 }
4310 else if (tv1->v_type == VAR_BLOB
4311 && tv2->v_type == VAR_BLOB)
4312 {
4313 eval_addblob(tv1, tv2);
4314 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004315 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 break;
4317 }
4318 }
4319#ifdef FEAT_FLOAT
4320 if (tv1->v_type == VAR_FLOAT)
4321 {
4322 f1 = tv1->vval.v_float;
4323 n1 = 0;
4324 }
4325 else
4326#endif
4327 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004328 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 n1 = tv_get_number_chk(tv1, &error);
4330 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004331 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332#ifdef FEAT_FLOAT
4333 if (tv2->v_type == VAR_FLOAT)
4334 f1 = n1;
4335#endif
4336 }
4337#ifdef FEAT_FLOAT
4338 if (tv2->v_type == VAR_FLOAT)
4339 {
4340 f2 = tv2->vval.v_float;
4341 n2 = 0;
4342 }
4343 else
4344#endif
4345 {
4346 n2 = tv_get_number_chk(tv2, &error);
4347 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004348 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349#ifdef FEAT_FLOAT
4350 if (tv1->v_type == VAR_FLOAT)
4351 f2 = n2;
4352#endif
4353 }
4354#ifdef FEAT_FLOAT
4355 // if there is a float on either side the result is a float
4356 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4357 {
4358 switch (iptr->isn_arg.op.op_type)
4359 {
4360 case EXPR_MULT: f1 = f1 * f2; break;
4361 case EXPR_DIV: f1 = f1 / f2; break;
4362 case EXPR_SUB: f1 = f1 - f2; break;
4363 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004364 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004365 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004366 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 }
4368 clear_tv(tv1);
4369 clear_tv(tv2);
4370 tv1->v_type = VAR_FLOAT;
4371 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004372 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 }
4374 else
4375#endif
4376 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004377 int failed = FALSE;
4378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 switch (iptr->isn_arg.op.op_type)
4380 {
4381 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004382 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4383 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004384 goto on_error;
4385 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 case EXPR_SUB: n1 = n1 - n2; break;
4387 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004388 default: n1 = num_modulus(n1, n2, &failed);
4389 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004390 goto on_error;
4391 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 }
4393 clear_tv(tv1);
4394 clear_tv(tv2);
4395 tv1->v_type = VAR_NUMBER;
4396 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004397 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 }
4399 }
4400 break;
4401
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004402 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004403 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004404 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004405 int is_slice = iptr->isn_type == ISN_STRSLICE;
4406 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004407 char_u *res;
4408
4409 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004410 // string slice: string is at stack-3, first index at
4411 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004412 if (is_slice)
4413 {
4414 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004415 n1 = tv->vval.v_number;
4416 }
4417
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004418 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004419 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004420
Bram Moolenaar4c137212021-04-19 16:48:48 +02004421 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004422 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004423 if (is_slice)
4424 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004425 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004426 else
4427 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004428 // single character (including composing characters).
4429 // If the index is too big or negative the result is
4430 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004431 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004432 vim_free(tv->vval.v_string);
4433 tv->vval.v_string = res;
4434 }
4435 break;
4436
4437 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004438 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004439 case ISN_BLOBINDEX:
4440 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004442 int is_slice = iptr->isn_type == ISN_LISTSLICE
4443 || iptr->isn_type == ISN_BLOBSLICE;
4444 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4445 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004446 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004447 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448
4449 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004450 // list slice: list is at stack-3, indexes at stack-2 and
4451 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004452 // Same for blob.
4453 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454
4455 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004456 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004458
4459 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 {
Bram Moolenaared591872020-08-15 22:14:53 +02004461 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004462 n1 = tv->vval.v_number;
4463 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 }
Bram Moolenaared591872020-08-15 22:14:53 +02004465
Bram Moolenaar4c137212021-04-19 16:48:48 +02004466 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004467 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004468 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004469 if (is_blob)
4470 {
4471 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4472 n1, n2, FALSE, tv) == FAIL)
4473 goto on_error;
4474 }
4475 else
4476 {
4477 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4478 n1, n2, FALSE, tv, TRUE) == FAIL)
4479 goto on_error;
4480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 }
4482 break;
4483
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004484 case ISN_ANYINDEX:
4485 case ISN_ANYSLICE:
4486 {
4487 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4488 typval_T *var1, *var2;
4489 int res;
4490
4491 // index: composite is at stack-2, index at stack-1
4492 // slice: composite is at stack-3, indexes at stack-2 and
4493 // stack-1
4494 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004495 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004496 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4497 goto on_error;
4498 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4499 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004500 res = eval_index_inner(tv, is_slice, var1, var2,
4501 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004502 clear_tv(var1);
4503 if (is_slice)
4504 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004505 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004506 if (res == FAIL)
4507 goto on_error;
4508 }
4509 break;
4510
Bram Moolenaar9af78762020-06-16 11:34:42 +02004511 case ISN_SLICE:
4512 {
4513 list_T *list;
4514 int count = iptr->isn_arg.number;
4515
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004516 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004517 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004518 list = tv->vval.v_list;
4519
4520 // no error for short list, expect it to be checked earlier
4521 if (list != NULL && list->lv_len >= count)
4522 {
4523 list_T *newlist = list_slice(list,
4524 count, list->lv_len - 1);
4525
4526 if (newlist != NULL)
4527 {
4528 list_unref(list);
4529 tv->vval.v_list = newlist;
4530 ++newlist->lv_refcount;
4531 }
4532 }
4533 }
4534 break;
4535
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004536 case ISN_GETITEM:
4537 {
4538 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004539 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004540
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004541 // Get list item: list is at stack-1, push item.
4542 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004543 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4544 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004545
Bram Moolenaar35578162021-08-02 19:10:38 +02004546 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004547 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004548 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004549 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004550
4551 // Useful when used in unpack assignment. Reset at
4552 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004553 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004554 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004555 }
4556 break;
4557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 case ISN_MEMBER:
4559 {
4560 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004561 char_u *key;
4562 dictitem_T *di;
4563
4564 // dict member: dict is at stack-2, key at stack-1
4565 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004566 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004567 dict = tv->vval.v_dict;
4568
4569 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004570 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004571 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004572 if (key == NULL)
4573 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004574
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004575 if ((di = dict_find(dict, key, -1)) == NULL)
4576 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004577 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004578 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004579
4580 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004581 // stack contents makes sense and the dict stack is
4582 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004583 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004584 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004585 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004586 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004587 tv->v_type = VAR_NUMBER;
4588 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004589 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004590 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004591 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004592 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004593 // Put the dict used on the dict stack, it might be used by
4594 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004595 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004596 if (dict_stack_save(tv) == FAIL)
4597 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004598 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004599 }
4600 break;
4601
4602 // dict member with string key
4603 case ISN_STRINGMEMBER:
4604 {
4605 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 dictitem_T *di;
4607
4608 tv = STACK_TV_BOT(-1);
4609 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4610 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004611 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004612 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004613 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 }
4615 dict = tv->vval.v_dict;
4616
4617 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4618 == NULL)
4619 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004620 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004621 semsg(_(e_key_not_present_in_dictionary),
4622 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004623 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004625 // Put the dict used on the dict stack, it might be used by
4626 // a dict function later.
4627 if (dict_stack_save(tv) == FAIL)
4628 goto on_fatal_error;
4629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004631 }
4632 break;
4633
4634 case ISN_CLEARDICT:
4635 dict_stack_drop();
4636 break;
4637
4638 case ISN_USEDICT:
4639 {
4640 typval_T *dict_tv = dict_stack_get_tv();
4641
4642 // Turn "dict.Func" into a partial for "Func" bound to
4643 // "dict". Don't do this when "Func" is already a partial
4644 // that was bound explicitly (pt_auto is FALSE).
4645 tv = STACK_TV_BOT(-1);
4646 if (dict_tv != NULL
4647 && dict_tv->v_type == VAR_DICT
4648 && dict_tv->vval.v_dict != NULL
4649 && (tv->v_type == VAR_FUNC
4650 || (tv->v_type == VAR_PARTIAL
4651 && (tv->vval.v_partial->pt_auto
4652 || tv->vval.v_partial->pt_dict == NULL))))
4653 dict_tv->vval.v_dict =
4654 make_partial(dict_tv->vval.v_dict, tv);
4655 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 }
4657 break;
4658
4659 case ISN_NEGATENR:
4660 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004661 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004662#ifdef FEAT_FLOAT
4663 if (tv->v_type == VAR_FLOAT)
4664 tv->vval.v_float = -tv->vval.v_float;
4665 else
4666#endif
4667 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 break;
4669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 case ISN_CHECKTYPE:
4671 {
4672 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004673 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004674 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004676 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004677 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004678 if (!ectx->ec_where.wt_variable)
4679 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004680 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004681 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004682 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004683 if (r == FAIL)
4684 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004685 if (!ectx->ec_where.wt_variable)
4686 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004687
4688 // number 0 is FALSE, number 1 is TRUE
4689 if (tv->v_type == VAR_NUMBER
4690 && ct->ct_type->tt_type == VAR_BOOL
4691 && (tv->vval.v_number == 0
4692 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004694 tv->v_type = VAR_BOOL;
4695 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004696 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 }
4698 }
4699 break;
4700
Bram Moolenaar9af78762020-06-16 11:34:42 +02004701 case ISN_CHECKLEN:
4702 {
4703 int min_len = iptr->isn_arg.checklen.cl_min_len;
4704 list_T *list = NULL;
4705
4706 tv = STACK_TV_BOT(-1);
4707 if (tv->v_type == VAR_LIST)
4708 list = tv->vval.v_list;
4709 if (list == NULL || list->lv_len < min_len
4710 || (list->lv_len > min_len
4711 && !iptr->isn_arg.checklen.cl_more_OK))
4712 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004713 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004714 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004715 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004716 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004717 }
4718 }
4719 break;
4720
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004721 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004722 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004723 break;
4724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004726 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 {
4728 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004729 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004731 if (iptr->isn_type == ISN_2BOOL)
4732 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004733 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004734 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004735 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004736 n = !n;
4737 }
4738 else
4739 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004740 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004741 SOURCING_LNUM = iptr->isn_lnum;
4742 n = tv_get_bool_chk(tv, &error);
4743 if (error)
4744 goto on_error;
4745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 clear_tv(tv);
4747 tv->v_type = VAR_BOOL;
4748 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4749 }
4750 break;
4751
4752 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004753 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004754 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004755 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4756 iptr->isn_type == ISN_2STRING_ANY,
4757 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004758 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759 break;
4760
Bram Moolenaar08597872020-12-10 19:43:40 +01004761 case ISN_RANGE:
4762 {
4763 exarg_T ea;
4764 char *errormsg;
4765
Bram Moolenaarece0b872021-01-08 20:40:45 +01004766 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004767 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004768 ea.addr_type = ADDR_LINES;
4769 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004770 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004771 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004772 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004773
Bram Moolenaar35578162021-08-02 19:10:38 +02004774 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004775 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004776 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004777 tv = STACK_TV_BOT(-1);
4778 tv->v_type = VAR_NUMBER;
4779 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004780 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004781 }
4782 break;
4783
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004784 case ISN_PUT:
4785 {
4786 int regname = iptr->isn_arg.put.put_regname;
4787 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4788 char_u *expr = NULL;
4789 int dir = FORWARD;
4790
Bram Moolenaar08597872020-12-10 19:43:40 +01004791 if (lnum < -2)
4792 {
4793 // line number was put on the stack by ISN_RANGE
4794 tv = STACK_TV_BOT(-1);
4795 curwin->w_cursor.lnum = tv->vval.v_number;
4796 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4797 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004798 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004799 }
4800 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004801 // :put! above cursor
4802 dir = BACKWARD;
4803 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004804 {
4805 curwin->w_cursor.lnum = lnum;
4806 if (lnum == 0)
4807 // check_cursor() below will move to line 1
4808 dir = BACKWARD;
4809 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004810
4811 if (regname == '=')
4812 {
4813 tv = STACK_TV_BOT(-1);
4814 if (tv->v_type == VAR_STRING)
4815 expr = tv->vval.v_string;
4816 else
4817 {
4818 expr = typval2string(tv, TRUE); // allocates value
4819 clear_tv(tv);
4820 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004821 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004822 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004823 check_cursor();
4824 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4825 vim_free(expr);
4826 }
4827 break;
4828
Bram Moolenaar02194d22020-10-24 23:08:38 +02004829 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004830 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4831 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4832 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4833 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004834 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4835 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004836 break;
4837
Bram Moolenaar02194d22020-10-24 23:08:38 +02004838 case ISN_CMDMOD_REV:
4839 // filter regprog is owned by the instruction, don't free it
4840 cmdmod.cmod_filter_regmatch.regprog = NULL;
4841 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004842 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4843 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004844 break;
4845
Bram Moolenaar792f7862020-11-23 08:31:18 +01004846 case ISN_UNPACK:
4847 {
4848 int count = iptr->isn_arg.unpack.unp_count;
4849 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4850 list_T *l;
4851 listitem_T *li;
4852 int i;
4853
4854 // Check there is a valid list to unpack.
4855 tv = STACK_TV_BOT(-1);
4856 if (tv->v_type != VAR_LIST)
4857 {
4858 SOURCING_LNUM = iptr->isn_lnum;
4859 emsg(_(e_for_argument_must_be_sequence_of_lists));
4860 goto on_error;
4861 }
4862 l = tv->vval.v_list;
4863 if (l == NULL
4864 || l->lv_len < (semicolon ? count - 1 : count))
4865 {
4866 SOURCING_LNUM = iptr->isn_lnum;
4867 emsg(_(e_list_value_does_not_have_enough_items));
4868 goto on_error;
4869 }
4870 else if (!semicolon && l->lv_len > count)
4871 {
4872 SOURCING_LNUM = iptr->isn_lnum;
4873 emsg(_(e_list_value_has_more_items_than_targets));
4874 goto on_error;
4875 }
4876
4877 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004878 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004879 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004880 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004881
4882 // Variable after semicolon gets a list with the remaining
4883 // items.
4884 if (semicolon)
4885 {
4886 list_T *rem_list =
4887 list_alloc_with_items(l->lv_len - count + 1);
4888
4889 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004890 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004891 tv = STACK_TV_BOT(-count);
4892 tv->vval.v_list = rem_list;
4893 ++rem_list->lv_refcount;
4894 tv->v_lock = 0;
4895 li = l->lv_first;
4896 for (i = 0; i < count - 1; ++i)
4897 li = li->li_next;
4898 for (i = 0; li != NULL; ++i)
4899 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004900 typval_T tvcopy;
4901
4902 copy_tv(&li->li_tv, &tvcopy);
4903 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004904 li = li->li_next;
4905 }
4906 --count;
4907 }
4908
4909 // Produce the values in reverse order, first item last.
4910 li = l->lv_first;
4911 for (i = 0; i < count; ++i)
4912 {
4913 tv = STACK_TV_BOT(-i - 1);
4914 copy_tv(&li->li_tv, tv);
4915 li = li->li_next;
4916 }
4917
4918 list_unref(l);
4919 }
4920 break;
4921
Bram Moolenaarb2049902021-01-24 12:53:53 +01004922 case ISN_PROF_START:
4923 case ISN_PROF_END:
4924 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004925#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004926 funccall_T cookie;
4927 ufunc_T *cur_ufunc =
4928 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004929 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004930
4931 cookie.func = cur_ufunc;
4932 if (iptr->isn_type == ISN_PROF_START)
4933 {
4934 func_line_start(&cookie, iptr->isn_lnum);
4935 // if we get here the instruction is executed
4936 func_line_exec(&cookie);
4937 }
4938 else
4939 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004940#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004941 }
4942 break;
4943
Bram Moolenaare99d4222021-06-13 14:01:26 +02004944 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004945 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004946 break;
4947
Bram Moolenaar389df252020-07-09 21:20:47 +02004948 case ISN_SHUFFLE:
4949 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004950 typval_T tmp_tv;
4951 int item = iptr->isn_arg.shuffle.shfl_item;
4952 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004953
4954 tmp_tv = *STACK_TV_BOT(-item);
4955 for ( ; up > 0 && item > 1; --up)
4956 {
4957 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4958 --item;
4959 }
4960 *STACK_TV_BOT(-item) = tmp_tv;
4961 }
4962 break;
4963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004965 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004967 ectx->ec_where.wt_index = 0;
4968 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 break;
4970 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004971 continue;
4972
Bram Moolenaard032f342020-07-18 18:13:02 +02004973func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004974 // Restore previous function. If the frame pointer is where we started
4975 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004976 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004977 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004978
Bram Moolenaar4c137212021-04-19 16:48:48 +02004979 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004980 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004981 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004982 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004983
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004984on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004985 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004986 // If "emsg_silent" is set then ignore the error, unless it was set
4987 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004988 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004989 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004990 {
4991 // If a sequence of instructions causes an error while ":silent!"
4992 // was used, restore the stack length and jump ahead to restoring
4993 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004994 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004995 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004996 while (ectx->ec_stack.ga_len
4997 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004998 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004999 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005000 clear_tv(STACK_TV_BOT(0));
5001 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005002 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5003 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005004 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005005 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005006 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005007on_fatal_error:
5008 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005009 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005010 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005011 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 }
5013
5014done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005015 ret = OK;
5016theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005017 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005018 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5019 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005020}
5021
5022/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005023 * Execute the instructions from a VAR_INSTR typeval and put the result in
5024 * "rettv".
5025 * Return OK or FAIL.
5026 */
5027 int
5028exe_typval_instr(typval_T *tv, typval_T *rettv)
5029{
5030 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5031 isn_T *save_instr = ectx->ec_instr;
5032 int save_iidx = ectx->ec_iidx;
5033 int res;
5034
LemonBoyf3b48952022-05-05 13:53:03 +01005035 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5036 // even when the compilation fails.
5037 rettv->v_type = VAR_UNKNOWN;
5038
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005039 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5040 res = exec_instructions(ectx);
5041 if (res == OK)
5042 {
5043 *rettv = *STACK_TV_BOT(-1);
5044 --ectx->ec_stack.ga_len;
5045 }
5046
5047 ectx->ec_instr = save_instr;
5048 ectx->ec_iidx = save_iidx;
5049
5050 return res;
5051}
5052
5053/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005054 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5055 * "substitute_instr".
5056 */
5057 char_u *
5058exe_substitute_instr(void)
5059{
5060 ectx_T *ectx = substitute_instr->subs_ectx;
5061 isn_T *save_instr = ectx->ec_instr;
5062 int save_iidx = ectx->ec_iidx;
5063 char_u *res;
5064
5065 ectx->ec_instr = substitute_instr->subs_instr;
5066 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005067 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005068 typval_T *tv = STACK_TV_BOT(-1);
5069
Bram Moolenaar27523602021-06-05 21:36:19 +02005070 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005071 --ectx->ec_stack.ga_len;
5072 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005073 }
5074 else
5075 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005076 substitute_instr->subs_status = FAIL;
5077 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005078 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079
Bram Moolenaar4c137212021-04-19 16:48:48 +02005080 ectx->ec_instr = save_instr;
5081 ectx->ec_iidx = save_iidx;
5082
5083 return res;
5084}
5085
5086/*
5087 * Call a "def" function from old Vim script.
5088 * Return OK or FAIL.
5089 */
5090 int
5091call_def_function(
5092 ufunc_T *ufunc,
5093 int argc_arg, // nr of arguments
5094 typval_T *argv, // arguments
5095 partial_T *partial, // optional partial for context
5096 typval_T *rettv) // return value
5097{
5098 ectx_T ectx; // execution context
5099 int argc = argc_arg;
5100 typval_T *tv;
5101 int idx;
5102 int ret = FAIL;
5103 int defcount = ufunc->uf_args.ga_len - argc;
5104 sctx_T save_current_sctx = current_sctx;
5105 int did_emsg_before = did_emsg_cumul + did_emsg;
5106 int save_suppress_errthrow = suppress_errthrow;
5107 msglist_T **saved_msg_list = NULL;
5108 msglist_T *private_msg_list = NULL;
5109 int save_emsg_silent_def = emsg_silent_def;
5110 int save_did_emsg_def = did_emsg_def;
5111 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005112 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005113
5114// Get pointer to item in the stack.
5115#undef STACK_TV
5116#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5117
5118// Get pointer to item at the bottom of the stack, -1 is the bottom.
5119#undef STACK_TV_BOT
5120#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5121
5122// Get pointer to a local variable on the stack. Negative for arguments.
5123#undef STACK_TV_VAR
5124#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5125
5126 if (ufunc->uf_def_status == UF_NOT_COMPILED
5127 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005128 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5129 && compile_def_function(ufunc, FALSE,
5130 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005131 {
5132 if (did_emsg_cumul + did_emsg == did_emsg_before)
5133 semsg(_(e_function_is_not_compiled_str),
5134 printable_func_name(ufunc));
5135 return FAIL;
5136 }
5137
5138 {
5139 // Check the function was really compiled.
5140 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5141 + ufunc->uf_dfunc_idx;
5142 if (INSTRUCTIONS(dfunc) == NULL)
5143 {
5144 iemsg("using call_def_function() on not compiled function");
5145 return FAIL;
5146 }
5147 }
5148
5149 // If depth of calling is getting too high, don't execute the function.
5150 orig_funcdepth = funcdepth_get();
5151 if (funcdepth_increment() == FAIL)
5152 return FAIL;
5153
5154 CLEAR_FIELD(ectx);
5155 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5156 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005157 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005158 {
5159 funcdepth_decrement();
5160 return FAIL;
5161 }
5162 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5163 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5164 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005165 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005166
5167 idx = argc - ufunc->uf_args.ga_len;
5168 if (idx > 0 && ufunc->uf_va_name == NULL)
5169 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005170 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
5171 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005172 goto failed_early;
5173 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005174 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5175 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005176 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005177 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
5178 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005179 goto failed_early;
5180 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005181
5182 // Put arguments on the stack, but no more than what the function expects.
5183 // A lambda can be called with more arguments than it uses.
5184 for (idx = 0; idx < argc
5185 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5186 ++idx)
5187 {
5188 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5189 && argv[idx].v_type == VAR_SPECIAL
5190 && argv[idx].vval.v_number == VVAL_NONE)
5191 {
5192 // Use the default value.
5193 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5194 }
5195 else
5196 {
5197 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5198 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005199 ufunc->uf_arg_types[idx], &argv[idx],
5200 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005201 goto failed_early;
5202 copy_tv(&argv[idx], STACK_TV_BOT(0));
5203 }
5204 ++ectx.ec_stack.ga_len;
5205 }
5206
5207 // Turn varargs into a list. Empty list if no args.
5208 if (ufunc->uf_va_name != NULL)
5209 {
5210 int vararg_count = argc - ufunc->uf_args.ga_len;
5211
5212 if (vararg_count < 0)
5213 vararg_count = 0;
5214 else
5215 argc -= vararg_count;
5216 if (exe_newlist(vararg_count, &ectx) == FAIL)
5217 goto failed_early;
5218
5219 // Check the type of the list items.
5220 tv = STACK_TV_BOT(-1);
5221 if (ufunc->uf_va_type != NULL
5222 && ufunc->uf_va_type != &t_list_any
5223 && ufunc->uf_va_type->tt_member != &t_any
5224 && tv->vval.v_list != NULL)
5225 {
5226 type_T *expected = ufunc->uf_va_type->tt_member;
5227 listitem_T *li = tv->vval.v_list->lv_first;
5228
5229 for (idx = 0; idx < vararg_count; ++idx)
5230 {
5231 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005232 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005233 goto failed_early;
5234 li = li->li_next;
5235 }
5236 }
5237
5238 if (defcount > 0)
5239 // Move varargs list to below missing default arguments.
5240 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5241 --ectx.ec_stack.ga_len;
5242 }
5243
5244 // Make space for omitted arguments, will store default value below.
5245 // Any varargs list goes after them.
5246 if (defcount > 0)
5247 for (idx = 0; idx < defcount; ++idx)
5248 {
5249 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5250 ++ectx.ec_stack.ga_len;
5251 }
5252 if (ufunc->uf_va_name != NULL)
5253 ++ectx.ec_stack.ga_len;
5254
5255 // Frame pointer points to just after arguments.
5256 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5257 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5258
5259 {
5260 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5261 + ufunc->uf_dfunc_idx;
5262 ufunc_T *base_ufunc = dfunc->df_ufunc;
5263
5264 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5265 // by copy_func().
5266 if (partial != NULL || base_ufunc->uf_partial != NULL)
5267 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005268 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5269 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005270 goto failed_early;
5271 if (partial != NULL)
5272 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005273 outer_T *outer = get_pt_outer(partial);
5274
5275 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005276 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005277 if (current_ectx != NULL)
5278 {
5279 if (current_ectx->ec_outer_ref != NULL
5280 && current_ectx->ec_outer_ref->or_outer != NULL)
5281 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005282 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005283 }
5284 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005285 }
5286 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005287 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005288 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005289 ++partial->pt_refcount;
5290 ectx.ec_outer_ref->or_partial = partial;
5291 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005292 }
5293 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005294 {
5295 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5296 ++base_ufunc->uf_partial->pt_refcount;
5297 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5298 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005299 }
5300 }
5301
5302 // dummy frame entries
5303 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5304 {
5305 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5306 ++ectx.ec_stack.ga_len;
5307 }
5308
5309 {
5310 // Reserve space for local variables and any closure reference count.
5311 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5312 + ufunc->uf_dfunc_idx;
5313
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005314 // Initialize variables to zero. That avoids having to generate
5315 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005316 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005317 {
5318 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5319 STACK_TV_VAR(idx)->vval.v_number = 0;
5320 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005321 ectx.ec_stack.ga_len += dfunc->df_varcount;
5322 if (dfunc->df_has_closure)
5323 {
5324 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5325 STACK_TV_VAR(idx)->vval.v_number = 0;
5326 ++ectx.ec_stack.ga_len;
5327 }
5328
5329 ectx.ec_instr = INSTRUCTIONS(dfunc);
5330 }
5331
5332 // Following errors are in the function, not the caller.
5333 // Commands behave like vim9script.
5334 estack_push_ufunc(ufunc, 1);
5335 current_sctx = ufunc->uf_script_ctx;
5336 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5337
5338 // Use a specific location for storing error messages to be converted to an
5339 // exception.
5340 saved_msg_list = msg_list;
5341 msg_list = &private_msg_list;
5342
5343 // Do turn errors into exceptions.
5344 suppress_errthrow = FALSE;
5345
5346 // Do not delete the function while executing it.
5347 ++ufunc->uf_calls;
5348
5349 // When ":silent!" was used before calling then we still abort the
5350 // function. If ":silent!" is used in the function then we don't.
5351 emsg_silent_def = emsg_silent;
5352 did_emsg_def = 0;
5353
5354 ectx.ec_where.wt_index = 0;
5355 ectx.ec_where.wt_variable = FALSE;
5356
5357 // Execute the instructions until done.
5358 ret = exec_instructions(&ectx);
5359 if (ret == OK)
5360 {
5361 // function finished, get result from the stack.
5362 if (ufunc->uf_ret_type == &t_void)
5363 {
5364 rettv->v_type = VAR_VOID;
5365 }
5366 else
5367 {
5368 tv = STACK_TV_BOT(-1);
5369 *rettv = *tv;
5370 tv->v_type = VAR_UNKNOWN;
5371 }
5372 }
5373
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005374 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005375 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5376 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005377
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005378 // Deal with any remaining closures, they may be in use somewhere.
5379 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005380 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005381 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005382 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005383 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005384
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005385 estack_pop();
5386 current_sctx = save_current_sctx;
5387
Bram Moolenaar2336c372021-12-06 15:06:54 +00005388 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5389 // Function was unreferenced while being used, free it now.
5390 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005391
Bram Moolenaar352134b2020-10-17 22:04:08 +02005392 if (*msg_list != NULL && saved_msg_list != NULL)
5393 {
5394 msglist_T **plist = saved_msg_list;
5395
5396 // Append entries from the current msg_list (uncaught exceptions) to
5397 // the saved msg_list.
5398 while (*plist != NULL)
5399 plist = &(*plist)->next;
5400
5401 *plist = *msg_list;
5402 }
5403 msg_list = saved_msg_list;
5404
Bram Moolenaar4c137212021-04-19 16:48:48 +02005405 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005406 {
5407 cmdmod.cmod_filter_regmatch.regprog = NULL;
5408 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005409 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005410 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005411 emsg_silent_def = save_emsg_silent_def;
5412 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005413
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005414failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005415 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005416 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5417 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005418 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005420 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005421 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005422 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005423 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005424 if (ectx.ec_outer_ref->or_outer_allocated)
5425 vim_free(ectx.ec_outer_ref->or_outer);
5426 partial_unref(ectx.ec_outer_ref->or_partial);
5427 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005428 }
5429
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005430 // Not sure if this is necessary.
5431 suppress_errthrow = save_suppress_errthrow;
5432
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005433 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5434 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005435 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005436 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005437 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005438 return ret;
5439}
5440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005441/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005442 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5443 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5444 * "pfx" is prefixed to every line.
5445 */
5446 static void
5447list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5448{
5449 int line_idx = 0;
5450 int prev_current = 0;
5451 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005452 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005453
5454 for (current = 0; current < instr_count; ++current)
5455 {
5456 isn_T *iptr = &instr[current];
5457 char *line;
5458
5459 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005460 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005461 while (line_idx < iptr->isn_lnum
5462 && line_idx < ufunc->uf_lines.ga_len)
5463 {
5464 if (current > prev_current)
5465 {
5466 msg_puts("\n\n");
5467 prev_current = current;
5468 }
5469 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5470 if (line != NULL)
5471 msg(line);
5472 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005473 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5474 {
5475 int first_def_arg = ufunc->uf_args.ga_len
5476 - ufunc->uf_def_args.ga_len;
5477
5478 if (def_arg_idx > 0)
5479 msg_puts("\n\n");
5480 msg_start();
5481 msg_puts(" ");
5482 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5483 first_def_arg + def_arg_idx]);
5484 msg_puts(" = ");
5485 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5486 msg_clr_eos();
5487 msg_end();
5488 }
5489 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005490
5491 switch (iptr->isn_type)
5492 {
5493 case ISN_EXEC:
5494 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5495 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005496 case ISN_EXEC_SPLIT:
5497 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5498 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005499 case ISN_EXECRANGE:
5500 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5501 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005502 case ISN_LEGACY_EVAL:
5503 smsg("%s%4d EVAL legacy %s", pfx, current,
5504 iptr->isn_arg.string);
5505 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005506 case ISN_REDIRSTART:
5507 smsg("%s%4d REDIR", pfx, current);
5508 break;
5509 case ISN_REDIREND:
5510 smsg("%s%4d REDIR END%s", pfx, current,
5511 iptr->isn_arg.number ? " append" : "");
5512 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005513 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005514#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005515 smsg("%s%4d CEXPR pre %s", pfx, current,
5516 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005517#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005518 break;
5519 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005520#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005521 {
5522 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5523
5524 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5525 cexpr_get_auname(cer->cer_cmdidx),
5526 cer->cer_forceit ? "!" : "",
5527 cer->cer_cmdline);
5528 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005529#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005530 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005531 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005532 smsg("%s%4d INSTR", pfx, current);
5533 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5534 msg(" -------------");
5535 break;
5536 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005537 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005538 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5539
5540 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005541 }
5542 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005543 case ISN_SUBSTITUTE:
5544 {
5545 subs_T *subs = &iptr->isn_arg.subs;
5546
5547 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5548 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5549 msg(" -------------");
5550 }
5551 break;
5552 case ISN_EXECCONCAT:
5553 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5554 (varnumber_T)iptr->isn_arg.number);
5555 break;
5556 case ISN_ECHO:
5557 {
5558 echo_T *echo = &iptr->isn_arg.echo;
5559
5560 smsg("%s%4d %s %d", pfx, current,
5561 echo->echo_with_white ? "ECHO" : "ECHON",
5562 echo->echo_count);
5563 }
5564 break;
5565 case ISN_EXECUTE:
5566 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005567 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005568 break;
5569 case ISN_ECHOMSG:
5570 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005571 (varnumber_T)(iptr->isn_arg.number));
5572 break;
5573 case ISN_ECHOCONSOLE:
5574 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5575 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005576 break;
5577 case ISN_ECHOERR:
5578 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005579 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005580 break;
5581 case ISN_LOAD:
5582 {
5583 if (iptr->isn_arg.number < 0)
5584 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5585 (varnumber_T)(iptr->isn_arg.number
5586 + STACK_FRAME_SIZE));
5587 else
5588 smsg("%s%4d LOAD $%lld", pfx, current,
5589 (varnumber_T)(iptr->isn_arg.number));
5590 }
5591 break;
5592 case ISN_LOADOUTER:
5593 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005594 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005595 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5596 iptr->isn_arg.outer.outer_depth,
5597 iptr->isn_arg.outer.outer_idx
5598 + STACK_FRAME_SIZE);
5599 else
5600 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5601 iptr->isn_arg.outer.outer_depth,
5602 iptr->isn_arg.outer.outer_idx);
5603 }
5604 break;
5605 case ISN_LOADV:
5606 smsg("%s%4d LOADV v:%s", pfx, current,
5607 get_vim_var_name(iptr->isn_arg.number));
5608 break;
5609 case ISN_LOADSCRIPT:
5610 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005611 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5612 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5613 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005614
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005615 sv = get_script_svar(sref, -1);
5616 if (sv == NULL)
5617 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5618 pfx, current, si->sn_name);
5619 else
5620 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005621 sv->sv_name,
5622 sref->sref_idx,
5623 si->sn_name);
5624 }
5625 break;
5626 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005627 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005628 {
5629 scriptitem_T *si = SCRIPT_ITEM(
5630 iptr->isn_arg.loadstore.ls_sid);
5631
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005632 smsg("%s%4d %s s:%s from %s", pfx, current,
5633 iptr->isn_type == ISN_LOADS ? "LOADS"
5634 : "LOADEXPORT",
5635 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005636 }
5637 break;
5638 case ISN_LOADAUTO:
5639 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5640 break;
5641 case ISN_LOADG:
5642 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5643 break;
5644 case ISN_LOADB:
5645 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5646 break;
5647 case ISN_LOADW:
5648 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5649 break;
5650 case ISN_LOADT:
5651 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5652 break;
5653 case ISN_LOADGDICT:
5654 smsg("%s%4d LOAD g:", pfx, current);
5655 break;
5656 case ISN_LOADBDICT:
5657 smsg("%s%4d LOAD b:", pfx, current);
5658 break;
5659 case ISN_LOADWDICT:
5660 smsg("%s%4d LOAD w:", pfx, current);
5661 break;
5662 case ISN_LOADTDICT:
5663 smsg("%s%4d LOAD t:", pfx, current);
5664 break;
5665 case ISN_LOADOPT:
5666 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5667 break;
5668 case ISN_LOADENV:
5669 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5670 break;
5671 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005672 smsg("%s%4d LOADREG @%c", pfx, current,
5673 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005674 break;
5675
5676 case ISN_STORE:
5677 if (iptr->isn_arg.number < 0)
5678 smsg("%s%4d STORE arg[%lld]", pfx, current,
5679 iptr->isn_arg.number + STACK_FRAME_SIZE);
5680 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005681 smsg("%s%4d STORE $%lld", pfx, current,
5682 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005683 break;
5684 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005685 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5686 iptr->isn_arg.outer.outer_depth,
5687 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005688 break;
5689 case ISN_STOREV:
5690 smsg("%s%4d STOREV v:%s", pfx, current,
5691 get_vim_var_name(iptr->isn_arg.number));
5692 break;
5693 case ISN_STOREAUTO:
5694 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5695 break;
5696 case ISN_STOREG:
5697 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5698 break;
5699 case ISN_STOREB:
5700 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5701 break;
5702 case ISN_STOREW:
5703 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5704 break;
5705 case ISN_STORET:
5706 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5707 break;
5708 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005709 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005710 {
5711 scriptitem_T *si = SCRIPT_ITEM(
5712 iptr->isn_arg.loadstore.ls_sid);
5713
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005714 smsg("%s%4d %s %s in %s", pfx, current,
5715 iptr->isn_type == ISN_STORES
5716 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005717 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5718 }
5719 break;
5720 case ISN_STORESCRIPT:
5721 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005722 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5723 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5724 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005725
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005726 sv = get_script_svar(sref, -1);
5727 if (sv == NULL)
5728 smsg("%s%4d STORESCRIPT [deleted] in %s",
5729 pfx, current, si->sn_name);
5730 else
5731 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005732 sv->sv_name,
5733 sref->sref_idx,
5734 si->sn_name);
5735 }
5736 break;
5737 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005738 case ISN_STOREFUNCOPT:
5739 smsg("%s%4d %s &%s", pfx, current,
5740 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005741 iptr->isn_arg.storeopt.so_name);
5742 break;
5743 case ISN_STOREENV:
5744 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5745 break;
5746 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005747 smsg("%s%4d STOREREG @%c", pfx, current,
5748 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005749 break;
5750 case ISN_STORENR:
5751 smsg("%s%4d STORE %lld in $%d", pfx, current,
5752 iptr->isn_arg.storenr.stnr_val,
5753 iptr->isn_arg.storenr.stnr_idx);
5754 break;
5755
5756 case ISN_STOREINDEX:
5757 smsg("%s%4d STOREINDEX %s", pfx, current,
5758 vartype_name(iptr->isn_arg.vartype));
5759 break;
5760
5761 case ISN_STORERANGE:
5762 smsg("%s%4d STORERANGE", pfx, current);
5763 break;
5764
5765 // constants
5766 case ISN_PUSHNR:
5767 smsg("%s%4d PUSHNR %lld", pfx, current,
5768 (varnumber_T)(iptr->isn_arg.number));
5769 break;
5770 case ISN_PUSHBOOL:
5771 case ISN_PUSHSPEC:
5772 smsg("%s%4d PUSH %s", pfx, current,
5773 get_var_special_name(iptr->isn_arg.number));
5774 break;
5775 case ISN_PUSHF:
5776#ifdef FEAT_FLOAT
5777 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5778#endif
5779 break;
5780 case ISN_PUSHS:
5781 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5782 break;
5783 case ISN_PUSHBLOB:
5784 {
5785 char_u *r;
5786 char_u numbuf[NUMBUFLEN];
5787 char_u *tofree;
5788
5789 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5790 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5791 vim_free(tofree);
5792 }
5793 break;
5794 case ISN_PUSHFUNC:
5795 {
5796 char *name = (char *)iptr->isn_arg.string;
5797
5798 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5799 name == NULL ? "[none]" : name);
5800 }
5801 break;
5802 case ISN_PUSHCHANNEL:
5803#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005804 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005805#endif
5806 break;
5807 case ISN_PUSHJOB:
5808#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005809 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005810#endif
5811 break;
5812 case ISN_PUSHEXC:
5813 smsg("%s%4d PUSH v:exception", pfx, current);
5814 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005815 case ISN_AUTOLOAD:
5816 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5817 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005818 case ISN_UNLET:
5819 smsg("%s%4d UNLET%s %s", pfx, current,
5820 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5821 iptr->isn_arg.unlet.ul_name);
5822 break;
5823 case ISN_UNLETENV:
5824 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5825 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5826 iptr->isn_arg.unlet.ul_name);
5827 break;
5828 case ISN_UNLETINDEX:
5829 smsg("%s%4d UNLETINDEX", pfx, current);
5830 break;
5831 case ISN_UNLETRANGE:
5832 smsg("%s%4d UNLETRANGE", pfx, current);
5833 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005834 case ISN_LOCKUNLOCK:
5835 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5836 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005837 case ISN_LOCKCONST:
5838 smsg("%s%4d LOCKCONST", pfx, current);
5839 break;
5840 case ISN_NEWLIST:
5841 smsg("%s%4d NEWLIST size %lld", pfx, current,
5842 (varnumber_T)(iptr->isn_arg.number));
5843 break;
5844 case ISN_NEWDICT:
5845 smsg("%s%4d NEWDICT size %lld", pfx, current,
5846 (varnumber_T)(iptr->isn_arg.number));
5847 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005848 case ISN_NEWPARTIAL:
5849 smsg("%s%4d NEWPARTIAL", pfx, current);
5850 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005851
5852 // function call
5853 case ISN_BCALL:
5854 {
5855 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5856
5857 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5858 internal_func_name(cbfunc->cbf_idx),
5859 cbfunc->cbf_argcount);
5860 }
5861 break;
5862 case ISN_DCALL:
5863 {
5864 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5865 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5866 + cdfunc->cdf_idx;
5867
5868 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005869 printable_func_name(df->df_ufunc),
5870 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005871 }
5872 break;
5873 case ISN_UCALL:
5874 {
5875 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5876
5877 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5878 cufunc->cuf_name, cufunc->cuf_argcount);
5879 }
5880 break;
5881 case ISN_PCALL:
5882 {
5883 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5884
5885 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5886 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5887 }
5888 break;
5889 case ISN_PCALL_END:
5890 smsg("%s%4d PCALL end", pfx, current);
5891 break;
5892 case ISN_RETURN:
5893 smsg("%s%4d RETURN", pfx, current);
5894 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005895 case ISN_RETURN_VOID:
5896 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005897 break;
5898 case ISN_FUNCREF:
5899 {
5900 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005901 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005902
Bram Moolenaar38453522021-11-28 22:00:12 +00005903 if (funcref->fr_func_name == NULL)
5904 {
5905 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5906 + funcref->fr_dfunc_idx;
5907 name = df->df_ufunc->uf_name;
5908 }
5909 else
5910 name = funcref->fr_func_name;
5911 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005912 }
5913 break;
5914
5915 case ISN_NEWFUNC:
5916 {
5917 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5918
5919 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5920 newfunc->nf_lambda, newfunc->nf_global);
5921 }
5922 break;
5923
5924 case ISN_DEF:
5925 {
5926 char_u *name = iptr->isn_arg.string;
5927
5928 smsg("%s%4d DEF %s", pfx, current,
5929 name == NULL ? (char_u *)"" : name);
5930 }
5931 break;
5932
5933 case ISN_JUMP:
5934 {
5935 char *when = "?";
5936
5937 switch (iptr->isn_arg.jump.jump_when)
5938 {
5939 case JUMP_ALWAYS:
5940 when = "JUMP";
5941 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005942 case JUMP_NEVER:
5943 iemsg("JUMP_NEVER should not be used");
5944 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005945 case JUMP_AND_KEEP_IF_TRUE:
5946 when = "JUMP_AND_KEEP_IF_TRUE";
5947 break;
5948 case JUMP_IF_FALSE:
5949 when = "JUMP_IF_FALSE";
5950 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005951 case JUMP_IF_COND_FALSE:
5952 when = "JUMP_IF_COND_FALSE";
5953 break;
5954 case JUMP_IF_COND_TRUE:
5955 when = "JUMP_IF_COND_TRUE";
5956 break;
5957 }
5958 smsg("%s%4d %s -> %d", pfx, current, when,
5959 iptr->isn_arg.jump.jump_where);
5960 }
5961 break;
5962
5963 case ISN_JUMP_IF_ARG_SET:
5964 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5965 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5966 iptr->isn_arg.jump.jump_where);
5967 break;
5968
5969 case ISN_FOR:
5970 {
5971 forloop_T *forloop = &iptr->isn_arg.forloop;
5972
5973 smsg("%s%4d FOR $%d -> %d", pfx, current,
5974 forloop->for_idx, forloop->for_end);
5975 }
5976 break;
5977
5978 case ISN_TRY:
5979 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005980 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005981
5982 if (try->try_ref->try_finally == 0)
5983 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5984 pfx, current,
5985 try->try_ref->try_catch,
5986 try->try_ref->try_endtry);
5987 else
5988 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5989 pfx, current,
5990 try->try_ref->try_catch,
5991 try->try_ref->try_finally,
5992 try->try_ref->try_endtry);
5993 }
5994 break;
5995 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005996 smsg("%s%4d CATCH", pfx, current);
5997 break;
5998 case ISN_TRYCONT:
5999 {
6000 trycont_T *trycont = &iptr->isn_arg.trycont;
6001
6002 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6003 trycont->tct_levels,
6004 trycont->tct_levels == 1 ? "" : "s",
6005 trycont->tct_where);
6006 }
6007 break;
6008 case ISN_FINALLY:
6009 smsg("%s%4d FINALLY", pfx, current);
6010 break;
6011 case ISN_ENDTRY:
6012 smsg("%s%4d ENDTRY", pfx, current);
6013 break;
6014 case ISN_THROW:
6015 smsg("%s%4d THROW", pfx, current);
6016 break;
6017
6018 // expression operations on number
6019 case ISN_OPNR:
6020 case ISN_OPFLOAT:
6021 case ISN_OPANY:
6022 {
6023 char *what;
6024 char *ins;
6025
6026 switch (iptr->isn_arg.op.op_type)
6027 {
6028 case EXPR_MULT: what = "*"; break;
6029 case EXPR_DIV: what = "/"; break;
6030 case EXPR_REM: what = "%"; break;
6031 case EXPR_SUB: what = "-"; break;
6032 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006033 case EXPR_LSHIFT: what = "<<"; break;
6034 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006035 default: what = "???"; break;
6036 }
6037 switch (iptr->isn_type)
6038 {
6039 case ISN_OPNR: ins = "OPNR"; break;
6040 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6041 case ISN_OPANY: ins = "OPANY"; break;
6042 default: ins = "???"; break;
6043 }
6044 smsg("%s%4d %s %s", pfx, current, ins, what);
6045 }
6046 break;
6047
6048 case ISN_COMPAREBOOL:
6049 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006050 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006051 case ISN_COMPARENR:
6052 case ISN_COMPAREFLOAT:
6053 case ISN_COMPARESTRING:
6054 case ISN_COMPAREBLOB:
6055 case ISN_COMPARELIST:
6056 case ISN_COMPAREDICT:
6057 case ISN_COMPAREFUNC:
6058 case ISN_COMPAREANY:
6059 {
6060 char *p;
6061 char buf[10];
6062 char *type;
6063
6064 switch (iptr->isn_arg.op.op_type)
6065 {
6066 case EXPR_EQUAL: p = "=="; break;
6067 case EXPR_NEQUAL: p = "!="; break;
6068 case EXPR_GREATER: p = ">"; break;
6069 case EXPR_GEQUAL: p = ">="; break;
6070 case EXPR_SMALLER: p = "<"; break;
6071 case EXPR_SEQUAL: p = "<="; break;
6072 case EXPR_MATCH: p = "=~"; break;
6073 case EXPR_IS: p = "is"; break;
6074 case EXPR_ISNOT: p = "isnot"; break;
6075 case EXPR_NOMATCH: p = "!~"; break;
6076 default: p = "???"; break;
6077 }
6078 STRCPY(buf, p);
6079 if (iptr->isn_arg.op.op_ic == TRUE)
6080 strcat(buf, "?");
6081 switch(iptr->isn_type)
6082 {
6083 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6084 case ISN_COMPARESPECIAL:
6085 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006086 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006087 case ISN_COMPARENR: type = "COMPARENR"; break;
6088 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6089 case ISN_COMPARESTRING:
6090 type = "COMPARESTRING"; break;
6091 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6092 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6093 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6094 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6095 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6096 default: type = "???"; break;
6097 }
6098
6099 smsg("%s%4d %s %s", pfx, current, type, buf);
6100 }
6101 break;
6102
6103 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6104 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6105
6106 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006107 case ISN_CONCAT:
6108 smsg("%s%4d CONCAT size %lld", pfx, current,
6109 (varnumber_T)(iptr->isn_arg.number));
6110 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006111 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6112 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6113 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6114 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6115 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6116 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6117 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6118 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6119 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6120 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6121 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006122 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006123 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6124 iptr->isn_arg.getitem.gi_index,
6125 iptr->isn_arg.getitem.gi_with_op ?
6126 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006127 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6128 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6129 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006130 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6131 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6132
Bram Moolenaar4c137212021-04-19 16:48:48 +02006133 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6134
Bram Moolenaar4c137212021-04-19 16:48:48 +02006135 case ISN_CHECKTYPE:
6136 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006137 checktype_T *ct = &iptr->isn_arg.type;
6138 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006139
6140 if (ct->ct_arg_idx == 0)
6141 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6142 type_name(ct->ct_type, &tofree),
6143 (int)ct->ct_off);
6144 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006145 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006146 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006147 type_name(ct->ct_type, &tofree),
6148 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006149 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006150 (int)ct->ct_arg_idx);
6151 vim_free(tofree);
6152 break;
6153 }
6154 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6155 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6156 iptr->isn_arg.checklen.cl_min_len);
6157 break;
6158 case ISN_SETTYPE:
6159 {
6160 char *tofree;
6161
6162 smsg("%s%4d SETTYPE %s", pfx, current,
6163 type_name(iptr->isn_arg.type.ct_type, &tofree));
6164 vim_free(tofree);
6165 break;
6166 }
6167 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006168 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6169 smsg("%s%4d INVERT %d (!val)", pfx, current,
6170 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006171 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006172 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6173 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006174 break;
6175 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006176 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006177 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006178 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6179 pfx, current,
6180 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006181 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006182 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6183 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006184 break;
6185 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006186 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006187 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006188 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006189 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6190 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006191 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006192 else
6193 smsg("%s%4d PUT %c %ld", pfx, current,
6194 iptr->isn_arg.put.put_regname,
6195 (long)iptr->isn_arg.put.put_lnum);
6196 break;
6197
Bram Moolenaar4c137212021-04-19 16:48:48 +02006198 case ISN_CMDMOD:
6199 {
6200 char_u *buf;
6201 size_t len = produce_cmdmods(
6202 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6203
6204 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006205 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006206 {
6207 (void)produce_cmdmods(
6208 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6209 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6210 vim_free(buf);
6211 }
6212 break;
6213 }
6214 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6215
6216 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006217 smsg("%s%4d PROFILE START line %d", pfx, current,
6218 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006219 break;
6220
6221 case ISN_PROF_END:
6222 smsg("%s%4d PROFILE END", pfx, current);
6223 break;
6224
Bram Moolenaare99d4222021-06-13 14:01:26 +02006225 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006226 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6227 iptr->isn_arg.debug.dbg_break_lnum + 1,
6228 iptr->isn_lnum,
6229 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006230 break;
6231
Bram Moolenaar4c137212021-04-19 16:48:48 +02006232 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6233 iptr->isn_arg.unpack.unp_count,
6234 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6235 break;
6236 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6237 iptr->isn_arg.shuffle.shfl_item,
6238 iptr->isn_arg.shuffle.shfl_up);
6239 break;
6240 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6241
6242 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6243 return;
6244 }
6245
6246 out_flush(); // output one line at a time
6247 ui_breakcheck();
6248 if (got_int)
6249 break;
6250 }
6251}
6252
6253/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006254 * Handle command line completion for the :disassemble command.
6255 */
6256 void
6257set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6258{
6259 char_u *p;
6260
6261 // Default: expand user functions, "debug" and "profile"
6262 xp->xp_context = EXPAND_DISASSEMBLE;
6263 xp->xp_pattern = arg;
6264
6265 // first argument already typed: only user function names
6266 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6267 {
6268 xp->xp_context = EXPAND_USER_FUNC;
6269 xp->xp_pattern = skipwhite(p);
6270 }
6271}
6272
6273/*
6274 * Function given to ExpandGeneric() to obtain the list of :disassemble
6275 * arguments.
6276 */
6277 char_u *
6278get_disassemble_argument(expand_T *xp, int idx)
6279{
6280 if (idx == 0)
6281 return (char_u *)"debug";
6282 if (idx == 1)
6283 return (char_u *)"profile";
6284 return get_user_func_name(xp, idx - 2);
6285}
6286
6287/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006288 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006289 * We don't really need this at runtime, but we do have tests that require it,
6290 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291 */
6292 void
6293ex_disassemble(exarg_T *eap)
6294{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006295 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006296 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006298 isn_T *instr = NULL; // init to shut up gcc warning
6299 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006300 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006301
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006302 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006303 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006305 if (func_needs_compiling(ufunc, compile_type)
6306 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006307 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006308 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006310 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 return;
6312 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006313 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006314
6315 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006316 switch (compile_type)
6317 {
6318 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006319#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006320 instr = dfunc->df_instr_prof;
6321 instr_count = dfunc->df_instr_prof_count;
6322 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006323#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006324 // FALLTHROUGH
6325 case CT_NONE:
6326 instr = dfunc->df_instr;
6327 instr_count = dfunc->df_instr_count;
6328 break;
6329 case CT_DEBUG:
6330 instr = dfunc->df_instr_debug;
6331 instr_count = dfunc->df_instr_debug_count;
6332 break;
6333 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334
Bram Moolenaar4c137212021-04-19 16:48:48 +02006335 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006336}
6337
6338/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006339 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006340 * list, etc. Mostly like what JavaScript does, except that empty list and
6341 * empty dictionary are FALSE.
6342 */
6343 int
6344tv2bool(typval_T *tv)
6345{
6346 switch (tv->v_type)
6347 {
6348 case VAR_NUMBER:
6349 return tv->vval.v_number != 0;
6350 case VAR_FLOAT:
6351#ifdef FEAT_FLOAT
6352 return tv->vval.v_float != 0.0;
6353#else
6354 break;
6355#endif
6356 case VAR_PARTIAL:
6357 return tv->vval.v_partial != NULL;
6358 case VAR_FUNC:
6359 case VAR_STRING:
6360 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6361 case VAR_LIST:
6362 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6363 case VAR_DICT:
6364 return tv->vval.v_dict != NULL
6365 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6366 case VAR_BOOL:
6367 case VAR_SPECIAL:
6368 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6369 case VAR_JOB:
6370#ifdef FEAT_JOB_CHANNEL
6371 return tv->vval.v_job != NULL;
6372#else
6373 break;
6374#endif
6375 case VAR_CHANNEL:
6376#ifdef FEAT_JOB_CHANNEL
6377 return tv->vval.v_channel != NULL;
6378#else
6379 break;
6380#endif
6381 case VAR_BLOB:
6382 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6383 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006384 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006385 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006386 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387 break;
6388 }
6389 return FALSE;
6390}
6391
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006392 void
6393emsg_using_string_as(typval_T *tv, int as_number)
6394{
6395 semsg(_(as_number ? e_using_string_as_number_str
6396 : e_using_string_as_bool_str),
6397 tv->vval.v_string == NULL
6398 ? (char_u *)"" : tv->vval.v_string);
6399}
6400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401/*
6402 * If "tv" is a string give an error and return FAIL.
6403 */
6404 int
6405check_not_string(typval_T *tv)
6406{
6407 if (tv->v_type == VAR_STRING)
6408 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006409 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006410 clear_tv(tv);
6411 return FAIL;
6412 }
6413 return OK;
6414}
6415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006416
6417#endif // FEAT_EVAL