blob: bf55d18cd97aeaf8cf2a0952bbdba77ed299f6bb [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
67// 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)
74
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 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200481 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200482 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200483 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200484 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200485 return FAIL;
486 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000487 else if (arg_to_add > ufunc->uf_def_args.ga_len)
488 {
489 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
490
491 if (missing == 1)
492 emsg(_(e_one_argument_too_few));
493 else
494 semsg(_(e_nr_arguments_too_few), missing);
495 return FAIL;
496 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200497
498 // Reserve space for:
499 // - missing arguments
500 // - stack frame
501 // - local variables
502 // - if needed: a counter for number of closures created in
503 // ectx->ec_funcrefs.
504 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200505 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 return FAIL;
507
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100508 // If depth of calling is getting too high, don't execute the function.
509 if (funcdepth_increment() == FAIL)
510 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200511 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100512
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100513 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200514 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100515 {
516 floc = ALLOC_ONE(funclocal_T);
517 if (floc == NULL)
518 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200519 *floc = ectx->ec_funclocal;
520 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100521 }
522
Bram Moolenaarfe270812020-04-11 22:31:27 +0200523 // Move the vararg-list to below the missing optional arguments.
524 if (vararg_count > 0 && arg_to_add > 0)
525 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100526
527 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200528 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200529 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200530 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531
532 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100533 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
534 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200535 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200536 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
537 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100538 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100539 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200540 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541
542 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200543 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000544 {
545 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
546
547 tv->v_type = VAR_NUMBER;
548 tv->vval.v_number = 0;
549 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200550 if (dfunc->df_has_closure)
551 {
552 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
553
554 tv->v_type = VAR_NUMBER;
555 tv->vval.v_number = 0;
556 }
557 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100559 if (pt != NULL || ufunc->uf_partial != NULL
560 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100561 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200562 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100563
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200564 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100565 return FAIL;
566 if (pt != NULL)
567 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000568 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200569 ++pt->pt_refcount;
570 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100571 }
572 else if (ufunc->uf_partial != NULL)
573 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000574 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200575 ++ufunc->uf_partial->pt_refcount;
576 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100577 }
578 else
579 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200580 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200581 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200582 {
583 vim_free(ref);
584 return FAIL;
585 }
586 ref->or_outer_allocated = TRUE;
587 ref->or_outer->out_stack = &ectx->ec_stack;
588 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
589 if (ectx->ec_outer_ref != NULL)
590 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100591 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200592 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100593 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100594 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200595 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100596
Bram Moolenaarc970e422021-03-17 15:03:04 +0100597 ++ufunc->uf_calls;
598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 // Set execution state to the start of the called function.
600 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100601 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100602 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200603 if (entry != NULL)
604 {
605 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200606 // Save the current context so it can be restored on return.
607 entry->es_save_sctx = current_sctx;
608 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200609 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100610
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200611 // Start execution at the first instruction.
612 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613
614 return OK;
615}
616
617// Get pointer to item in the stack.
618#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
619
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000620// Double linked list of funcstack_T in use.
621static funcstack_T *first_funcstack = NULL;
622
623 static void
624add_funcstack_to_list(funcstack_T *funcstack)
625{
626 // Link in list of funcstacks.
627 if (first_funcstack != NULL)
628 first_funcstack->fs_prev = funcstack;
629 funcstack->fs_next = first_funcstack;
630 funcstack->fs_prev = NULL;
631 first_funcstack = funcstack;
632}
633
634 static void
635remove_funcstack_from_list(funcstack_T *funcstack)
636{
637 if (funcstack->fs_prev == NULL)
638 first_funcstack = funcstack->fs_next;
639 else
640 funcstack->fs_prev->fs_next = funcstack->fs_next;
641 if (funcstack->fs_next != NULL)
642 funcstack->fs_next->fs_prev = funcstack->fs_prev;
643}
644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200646 * Used when returning from a function: Check if any closure is still
647 * referenced. If so then move the arguments and variables to a separate piece
648 * of stack to be used when the closure is called.
649 * When "free_arguments" is TRUE the arguments are to be freed.
650 * Returns FAIL when out of memory.
651 */
652 static int
653handle_closure_in_use(ectx_T *ectx, int free_arguments)
654{
655 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
656 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200657 int argcount;
658 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200659 int idx;
660 typval_T *tv;
661 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200662 garray_T *gap = &ectx->ec_funcrefs;
663 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200664
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200665 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200666 return OK; // function was freed
667 if (dfunc->df_has_closure == 0)
668 return OK; // no closures
669 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
670 closure_count = tv->vval.v_number;
671 if (closure_count == 0)
672 return OK; // no funcrefs created
673
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200674 argcount = ufunc_argcount(dfunc->df_ufunc);
675 top = ectx->ec_frame_idx - argcount;
676
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200677 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200678 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200679 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200680 partial_T *pt;
681 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200682
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200683 if (off < 0)
684 continue; // count is off or already done
685 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200686 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200687 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200688 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200689 int i;
690
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000691 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200692 // unreferenced on return.
693 for (i = 0; i < dfunc->df_varcount; ++i)
694 {
695 typval_T *stv = STACK_TV(ectx->ec_frame_idx
696 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200697 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200698 --refcount;
699 }
700 if (refcount > 1)
701 {
702 closure_in_use = TRUE;
703 break;
704 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200705 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200706 }
707
708 if (closure_in_use)
709 {
710 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
711 typval_T *stack;
712
713 // A closure is using the arguments and/or local variables.
714 // Move them to the called function.
715 if (funcstack == NULL)
716 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000717
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200718 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
719 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
721 funcstack->fs_ga.ga_data = stack;
722 if (stack == NULL)
723 {
724 vim_free(funcstack);
725 return FAIL;
726 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000727 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200728
729 // Move or copy the arguments.
730 for (idx = 0; idx < argcount; ++idx)
731 {
732 tv = STACK_TV(top + idx);
733 if (free_arguments)
734 {
735 *(stack + idx) = *tv;
736 tv->v_type = VAR_UNKNOWN;
737 }
738 else
739 copy_tv(tv, stack + idx);
740 }
741 // Move the local variables.
742 for (idx = 0; idx < dfunc->df_varcount; ++idx)
743 {
744 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200745
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200746 // A partial created for a local function, that is also used as a
747 // local variable, has a reference count for the variable, thus
748 // will never go down to zero. When all these refcounts are one
749 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000750 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200751 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
752 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200753 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200754
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200755 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200756 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
757 gap->ga_len - closure_count + i])
758 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200759 }
760
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200761 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200762 tv->v_type = VAR_UNKNOWN;
763 }
764
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200765 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200766 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200767 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
768 - closure_count + idx];
769 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200770 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200771 ++funcstack->fs_refcount;
772 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100773 pt->pt_outer.out_stack = &funcstack->fs_ga;
774 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200775 }
776 }
777 }
778
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200779 for (idx = 0; idx < closure_count; ++idx)
780 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
781 - closure_count + idx]);
782 gap->ga_len -= closure_count;
783 if (gap->ga_len == 0)
784 ga_clear(gap);
785
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200786 return OK;
787}
788
789/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200790 * Called when a partial is freed or its reference count goes down to one. The
791 * funcstack may be the only reference to the partials in the local variables.
792 * Go over all of them, the funcref and can be freed if all partials
793 * referencing the funcstack have a reference count of one.
794 */
795 void
796funcstack_check_refcount(funcstack_T *funcstack)
797{
798 int i;
799 garray_T *gap = &funcstack->fs_ga;
800 int done = 0;
801
802 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
803 return;
804 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
805 {
806 typval_T *tv = ((typval_T *)gap->ga_data) + i;
807
808 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
809 && tv->vval.v_partial->pt_funcstack == funcstack
810 && tv->vval.v_partial->pt_refcount == 1)
811 ++done;
812 }
813 if (done == funcstack->fs_min_refcount)
814 {
815 typval_T *stack = gap->ga_data;
816
817 // All partials referencing the funcstack have a reference count of
818 // one, thus the funcstack is no longer of use.
819 for (i = 0; i < gap->ga_len; ++i)
820 clear_tv(stack + i);
821 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000822 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200823 vim_free(funcstack);
824 }
825}
826
827/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000828 * For garbage collecting: set references in all variables referenced by
829 * all funcstacks.
830 */
831 int
832set_ref_in_funcstacks(int copyID)
833{
834 funcstack_T *funcstack;
835
836 for (funcstack = first_funcstack; funcstack != NULL;
837 funcstack = funcstack->fs_next)
838 {
839 typval_T *stack = funcstack->fs_ga.ga_data;
840 int i;
841
842 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
843 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
844 return TRUE; // abort
845 }
846 return FALSE;
847}
848
849/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 * Return from the current function.
851 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200852 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200853func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100855 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100856 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200857 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
858 + ectx->ec_dfunc_idx;
859 int argcount = ufunc_argcount(dfunc->df_ufunc);
860 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200861 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100862 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
863 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200864 funclocal_T *floc;
865#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100866 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
867 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868
Bram Moolenaar12d26532021-02-19 19:13:21 +0100869 if (do_profiling == PROF_YES)
870 {
871 ufunc_T *caller = prev_dfunc->df_ufunc;
872
873 if (dfunc->df_ufunc->uf_profiling
874 || (caller != NULL && caller->uf_profiling))
875 {
876 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
877 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
878 --profile_info_ga.ga_len;
879 }
880 }
881#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000882
883 // No check for uf_refcount being zero, cannot think of a way that would
884 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100885 --dfunc->df_ufunc->uf_calls;
886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200888 entry = estack_pop();
889 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200890 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200892 if (handle_closure_in_use(ectx, TRUE) == FAIL)
893 return FAIL;
894
895 // Clear the arguments.
896 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
897 clear_tv(STACK_TV(idx));
898
899 // Clear local variables and temp values, but not the return value.
900 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100901 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100903
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100904 // The return value should be on top of the stack. However, when aborting
905 // it may not be there and ec_frame_idx is the top of the stack.
906 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100907 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100908 ret_idx = 0;
909
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200910 if (ectx->ec_outer_ref != NULL)
911 {
912 if (ectx->ec_outer_ref->or_outer_allocated)
913 vim_free(ectx->ec_outer_ref->or_outer);
914 partial_unref(ectx->ec_outer_ref->or_partial);
915 vim_free(ectx->ec_outer_ref);
916 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100917
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100918 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100919 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100920 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
921 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200922 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
923 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200924 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100925 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100926 floc = (void *)STACK_TV(ectx->ec_frame_idx
927 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200928 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100929 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
930 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200931
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100932 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200933 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100934 else
935 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200936 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100937 vim_free(floc);
938 }
939
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100940 if (ret_idx > 0)
941 {
942 // Reset the stack to the position before the call, with a spot for the
943 // return value, moved there from above the frame.
944 ectx->ec_stack.ga_len = top + 1;
945 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
946 }
947 else
948 // Reset the stack to the position before the call.
949 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200950
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100951 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200952 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200953 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954}
955
956#undef STACK_TV
957
958/*
959 * Prepare arguments and rettv for calling a builtin or user function.
960 */
961 static int
962call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
963{
964 int idx;
965 typval_T *tv;
966
967 // Move arguments from bottom of the stack to argvars[] and add terminator.
968 for (idx = 0; idx < argcount; ++idx)
969 argvars[idx] = *STACK_TV_BOT(idx - argcount);
970 argvars[argcount].v_type = VAR_UNKNOWN;
971
972 // Result replaces the arguments on the stack.
973 if (argcount > 0)
974 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200975 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 return FAIL;
977 else
978 ++ectx->ec_stack.ga_len;
979
980 // Default return value is zero.
981 tv = STACK_TV_BOT(-1);
982 tv->v_type = VAR_NUMBER;
983 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +0100984 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985
986 return OK;
987}
988
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200989// Ugly global to avoid passing the execution context around through many
990// layers.
991static ectx_T *current_ectx = NULL;
992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993/*
994 * Call a builtin function by index.
995 */
996 static int
997call_bfunc(int func_idx, int argcount, ectx_T *ectx)
998{
999 typval_T argvars[MAX_FUNC_ARGS];
1000 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001001 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001002 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001003 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004
1005 if (call_prepare(argcount, argvars, ectx) == FAIL)
1006 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001007 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001009 // Call the builtin function. Set "current_ectx" so that when it
1010 // recursively invokes call_def_function() a closure context can be set.
1011 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001013 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001014 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015
1016 // Clear the arguments.
1017 for (idx = 0; idx < argcount; ++idx)
1018 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001019
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001020 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001021 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 return OK;
1023}
1024
1025/*
1026 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001027 * If the function is compiled this will add a stack frame and set the
1028 * instruction pointer at the start of the function.
1029 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001030 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001031 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 */
1033 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001034call_ufunc(
1035 ufunc_T *ufunc,
1036 partial_T *pt,
1037 int argcount,
1038 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001039 isn_T *iptr,
1040 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041{
1042 typval_T argvars[MAX_FUNC_ARGS];
1043 funcexe_T funcexe;
1044 int error;
1045 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001046 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001047 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048
Bram Moolenaare99d4222021-06-13 14:01:26 +02001049 if (func_needs_compiling(ufunc, compile_type)
1050 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1051 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001052 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001053 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001054 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001055 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001056 if (error != FCERR_UNKNOWN)
1057 {
1058 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001059 semsg(_(e_too_many_arguments_for_function_str),
1060 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001061 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001062 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001063 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001064 return FAIL;
1065 }
1066
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001067 // The function has been compiled, can call it quickly. For a function
1068 // that was defined later: we can call it directly next time.
1069 if (iptr != NULL)
1070 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001071 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001072 iptr->isn_type = ISN_DCALL;
1073 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1074 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1075 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001076 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078
1079 if (call_prepare(argcount, argvars, ectx) == FAIL)
1080 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001081 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001082 funcexe.fe_evaluate = TRUE;
1083 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084
1085 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001087 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088
1089 // Clear the arguments.
1090 for (idx = 0; idx < argcount; ++idx)
1091 clear_tv(&argvars[idx]);
1092
1093 if (error != FCERR_NONE)
1094 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001095 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 return FAIL;
1097 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001098 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001099 // Error other than from calling the function itself.
1100 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 return OK;
1102}
1103
1104/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001105 * If command modifiers were applied restore them.
1106 */
1107 static void
1108may_restore_cmdmod(funclocal_T *funclocal)
1109{
1110 if (funclocal->floc_restore_cmdmod)
1111 {
1112 cmdmod.cmod_filter_regmatch.regprog = NULL;
1113 undo_cmdmod(&cmdmod);
1114 cmdmod = funclocal->floc_save_cmdmod;
1115 funclocal->floc_restore_cmdmod = FALSE;
1116 }
1117}
1118
1119/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001120 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1121 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001122 */
1123 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001124vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001125{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001126 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001127}
1128
1129/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 * Execute a function by "name".
1131 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001132 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 * Returns FAIL if not found without an error message.
1134 */
1135 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001136call_by_name(
1137 char_u *name,
1138 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001139 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001140 isn_T *iptr,
1141 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142{
1143 ufunc_T *ufunc;
1144
1145 if (builtin_function(name, -1))
1146 {
1147 int func_idx = find_internal_func(name);
1148
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001149 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001151 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 return FAIL;
1153 return call_bfunc(func_idx, argcount, ectx);
1154 }
1155
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001156 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001157
1158 if (ufunc == NULL)
1159 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001160 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001161
1162 if (script_autoload(name, TRUE))
1163 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001164 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001165
1166 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001167 return FAIL; // bail out if loading the script caused an error
1168 }
1169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001171 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001172 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001173 {
1174 int i;
1175 typval_T *argv = STACK_TV_BOT(0) - argcount;
1176
1177 // The function can change at runtime, check that the argument
1178 // types are correct.
1179 for (i = 0; i < argcount; ++i)
1180 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001181 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001182
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001183 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001184 type = ufunc->uf_arg_types[i];
1185 else if (ufunc->uf_va_type != NULL)
1186 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001187 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001188 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001189 return FAIL;
1190 }
1191 }
1192
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001193 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195
1196 return FAIL;
1197}
1198
1199 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001200call_partial(
1201 typval_T *tv,
1202 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001203 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001205 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001206 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001208 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001209 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210
1211 if (tv->v_type == VAR_PARTIAL)
1212 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001213 partial_T *pt = tv->vval.v_partial;
1214 int i;
1215
1216 if (pt->pt_argc > 0)
1217 {
1218 // Make space for arguments from the partial, shift the "argcount"
1219 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001220 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001221 return FAIL;
1222 for (i = 1; i <= argcount; ++i)
1223 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1224 ectx->ec_stack.ga_len += pt->pt_argc;
1225 argcount += pt->pt_argc;
1226
1227 // copy the arguments from the partial onto the stack
1228 for (i = 0; i < pt->pt_argc; ++i)
1229 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1230 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001231 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232
1233 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001234 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001236 name = pt->pt_name;
1237 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001238 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001240 if (name != NULL)
1241 {
1242 char_u fname_buf[FLEN_FIXED + 1];
1243 char_u *tofree = NULL;
1244 int error = FCERR_NONE;
1245 char_u *fname;
1246
1247 // May need to translate <SNR>123_ to K_SNR.
1248 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1249 if (error != FCERR_NONE)
1250 res = FAIL;
1251 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001252 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001253 vim_free(tofree);
1254 }
1255
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001256 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 {
1258 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001259 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001260 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 return FAIL;
1262 }
1263 return OK;
1264}
1265
1266/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001267 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1268 * TRUE.
1269 */
1270 static int
1271error_if_locked(int lock, char *error)
1272{
1273 if (lock & (VAR_LOCKED | VAR_FIXED))
1274 {
1275 emsg(_(error));
1276 return TRUE;
1277 }
1278 return FALSE;
1279}
1280
1281/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001282 * Give an error if "tv" is not a number and return FAIL.
1283 */
1284 static int
1285check_for_number(typval_T *tv)
1286{
1287 if (tv->v_type != VAR_NUMBER)
1288 {
1289 semsg(_(e_expected_str_but_got_str),
1290 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1291 return FAIL;
1292 }
1293 return OK;
1294}
1295
1296/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001297 * Store "tv" in variable "name".
1298 * This is for s: and g: variables.
1299 */
1300 static void
1301store_var(char_u *name, typval_T *tv)
1302{
1303 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001304 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001305
Bram Moolenaard877a572021-04-01 19:42:48 +02001306 if (tv->v_lock)
1307 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001308 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001309 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001310 restore_funccal();
1311}
1312
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001313/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001314 * Convert "tv" to a string.
1315 * Return FAIL if not allowed.
1316 */
1317 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001318do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001319{
1320 if (tv->v_type != VAR_STRING)
1321 {
1322 char_u *str;
1323
1324 if (is_2string_any)
1325 {
1326 switch (tv->v_type)
1327 {
1328 case VAR_SPECIAL:
1329 case VAR_BOOL:
1330 case VAR_NUMBER:
1331 case VAR_FLOAT:
1332 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001333
1334 case VAR_LIST:
1335 if (tolerant)
1336 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001337 char_u *s, *e, *p;
1338 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001339
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001340 ga_init2(&ga, sizeof(char_u *), 1);
1341
1342 // Convert to NL separated items, then
1343 // escape the items and replace the NL with
1344 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001345 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001346 if (str == NULL)
1347 return FAIL;
1348 s = str;
1349 while ((e = vim_strchr(s, '\n')) != NULL)
1350 {
1351 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001352 p = vim_strsave_fnameescape(s,
1353 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001354 if (p != NULL)
1355 {
1356 ga_concat(&ga, p);
1357 ga_concat(&ga, (char_u *)" ");
1358 vim_free(p);
1359 }
1360 s = e + 1;
1361 }
1362 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001363 clear_tv(tv);
1364 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001365 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001366 return OK;
1367 }
1368 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001369 default: to_string_error(tv->v_type);
1370 return FAIL;
1371 }
1372 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001373 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001374 clear_tv(tv);
1375 tv->v_type = VAR_STRING;
1376 tv->vval.v_string = str;
1377 }
1378 return OK;
1379}
1380
1381/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001382 * When the value of "sv" is a null list of dict, allocate it.
1383 */
1384 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001385allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001386{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001387 typval_T *tv = sv->sv_tv;
1388
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001389 switch (tv->v_type)
1390 {
1391 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001392 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001393 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001394 break;
1395 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001396 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001397 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001398 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001399 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001400 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001401 (void)rettv_blob_alloc(tv);
1402 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001403 default:
1404 break;
1405 }
1406}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001407
Bram Moolenaare7525c52021-01-09 13:20:37 +01001408/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001409 * Return the character "str[index]" where "index" is the character index,
1410 * including composing characters.
1411 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001412 */
1413 char_u *
1414char_from_string(char_u *str, varnumber_T index)
1415{
1416 size_t nbyte = 0;
1417 varnumber_T nchar = index;
1418 size_t slen;
1419
1420 if (str == NULL)
1421 return NULL;
1422 slen = STRLEN(str);
1423
Bram Moolenaarff871402021-03-26 13:34:05 +01001424 // Do the same as for a list: a negative index counts from the end.
1425 // Optimization to check the first byte to be below 0x80 (and no composing
1426 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001427 if (index < 0)
1428 {
1429 int clen = 0;
1430
1431 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001432 {
1433 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1434 ++nbyte;
1435 else if (enc_utf8)
1436 nbyte += utfc_ptr2len(str + nbyte);
1437 else
1438 nbyte += mb_ptr2len(str + nbyte);
1439 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001440 nchar = clen + index;
1441 if (nchar < 0)
1442 // unlike list: index out of range results in empty string
1443 return NULL;
1444 }
1445
1446 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001447 {
1448 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1449 ++nbyte;
1450 else if (enc_utf8)
1451 nbyte += utfc_ptr2len(str + nbyte);
1452 else
1453 nbyte += mb_ptr2len(str + nbyte);
1454 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001455 if (nbyte >= slen)
1456 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001457 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001458}
1459
1460/*
1461 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001462 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001463 * If going over the end return "str_len".
1464 * If "idx" is negative count from the end, -1 is the last character.
1465 * When going over the start return -1.
1466 */
1467 static long
1468char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1469{
1470 varnumber_T nchar = idx;
1471 size_t nbyte = 0;
1472
1473 if (nchar >= 0)
1474 {
1475 while (nchar > 0 && nbyte < str_len)
1476 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001477 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001478 --nchar;
1479 }
1480 }
1481 else
1482 {
1483 nbyte = str_len;
1484 while (nchar < 0 && nbyte > 0)
1485 {
1486 --nbyte;
1487 nbyte -= mb_head_off(str, str + nbyte);
1488 ++nchar;
1489 }
1490 if (nchar < 0)
1491 return -1;
1492 }
1493 return (long)nbyte;
1494}
1495
1496/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001497 * Return the slice "str[first : last]" using character indexes. Composing
1498 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001499 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001500 * Return NULL when the result is empty.
1501 */
1502 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001503string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001504{
1505 long start_byte, end_byte;
1506 size_t slen;
1507
1508 if (str == NULL)
1509 return NULL;
1510 slen = STRLEN(str);
1511 start_byte = char_idx2byte(str, slen, first);
1512 if (start_byte < 0)
1513 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001514 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001515 end_byte = (long)slen;
1516 else
1517 {
1518 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001519 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001520 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001521 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001522 }
1523
1524 if (start_byte >= (long)slen || end_byte <= start_byte)
1525 return NULL;
1526 return vim_strnsave(str + start_byte, end_byte - start_byte);
1527}
1528
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001529/*
1530 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1531 * When "dfunc_idx" is negative don't give an error.
1532 * Returns NULL for an error.
1533 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001534 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001535get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001536{
1537 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001538 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1539 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001540 svar_T *sv;
1541
1542 if (sref->sref_seq != si->sn_script_seq)
1543 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001544 // The script was reloaded after the function was compiled, the
1545 // script_idx may not be valid.
1546 if (dfunc != NULL)
1547 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1548 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001549 return NULL;
1550 }
1551 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001552 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001553 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001554 if (dfunc != NULL)
1555 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001556 return NULL;
1557 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001558
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001559 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1560 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001561 {
1562 if (dfunc != NULL)
1563 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1564 return NULL;
1565 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001566 return sv;
1567}
1568
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001569/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001570 * Function passed to do_cmdline() for splitting a script joined by NL
1571 * characters.
1572 */
1573 static char_u *
1574get_split_sourceline(
1575 int c UNUSED,
1576 void *cookie,
1577 int indent UNUSED,
1578 getline_opt_T options UNUSED)
1579{
1580 source_cookie_T *sp = (source_cookie_T *)cookie;
1581 char_u *p;
1582 char_u *line;
1583
Bram Moolenaar20677332021-06-06 17:02:53 +02001584 p = vim_strchr(sp->nextline, '\n');
1585 if (p == NULL)
1586 {
1587 line = vim_strsave(sp->nextline);
1588 sp->nextline += STRLEN(sp->nextline);
1589 }
1590 else
1591 {
1592 line = vim_strnsave(sp->nextline, p - sp->nextline);
1593 sp->nextline = p + 1;
1594 }
1595 return line;
1596}
1597
1598/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 * Execute a function by "name".
1600 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001601 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 */
1603 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001604call_eval_func(
1605 char_u *name,
1606 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001607 ectx_T *ectx,
1608 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609{
Bram Moolenaared677f52020-08-12 16:38:10 +02001610 int called_emsg_before = called_emsg;
1611 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001613 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001614 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001616 dictitem_T *v;
1617
1618 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001619 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1620 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001621 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001622 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001623 return FAIL;
1624 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001625 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001627 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628}
1629
1630/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001631 * When a function reference is used, fill a partial with the information
1632 * needed, especially when it is used as a closure.
1633 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001634 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001635fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1636{
1637 pt->pt_func = ufunc;
1638 pt->pt_refcount = 1;
1639
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001640 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001641 {
1642 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1643 + ectx->ec_dfunc_idx;
1644
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001645 // The closure may need to find arguments and local variables in the
1646 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001647 pt->pt_outer.out_stack = &ectx->ec_stack;
1648 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001649 if (ectx->ec_outer_ref != NULL)
1650 {
1651 // The current context already has a context, link to that one.
1652 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1653 if (ectx->ec_outer_ref->or_partial != NULL)
1654 {
1655 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1656 ++pt->pt_outer.out_up_partial->pt_refcount;
1657 }
1658 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001659
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001660 // If this function returns and the closure is still being used, we
1661 // need to make a copy of the context (arguments and local variables).
1662 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001663 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001664 {
1665 vim_free(pt);
1666 return FAIL;
1667 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001668 // Extra variable keeps the count of closures created in the current
1669 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001670 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1671 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1672
1673 ((partial_T **)ectx->ec_funcrefs.ga_data)
1674 [ectx->ec_funcrefs.ga_len] = pt;
1675 ++pt->pt_refcount;
1676 ++ectx->ec_funcrefs.ga_len;
1677 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001678 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001679 return OK;
1680}
1681
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001682/*
1683 * Execute iptr->isn_arg.string as an Ex command.
1684 */
1685 static int
1686exec_command(isn_T *iptr)
1687{
1688 source_cookie_T cookie;
1689
1690 SOURCING_LNUM = iptr->isn_lnum;
1691 // Pass getsourceline to get an error for a missing ":end"
1692 // command.
1693 CLEAR_FIELD(cookie);
1694 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1695 if (do_cmdline(iptr->isn_arg.string,
1696 getsourceline, &cookie,
1697 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1698 || did_emsg)
1699 return FAIL;
1700 return OK;
1701}
1702
Bram Moolenaar89445512022-04-14 12:58:23 +01001703/*
1704 * If script "sid" is not loaded yet then load it now.
1705 * Caller must make sure "sid" is a valid script ID.
1706 * "loaded" is set to TRUE if the script had to be loaded.
1707 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1708 */
1709 int
1710may_load_script(int sid, int *loaded)
1711{
1712 scriptitem_T *si = SCRIPT_ITEM(sid);
1713
1714 if (si->sn_state == SN_STATE_NOT_LOADED)
1715 {
1716 *loaded = TRUE;
1717 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1718 {
1719 semsg(_(e_cant_open_file_str), si->sn_name);
1720 return FAIL;
1721 }
1722 }
1723 return OK;
1724}
1725
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001726// used for v_instr of typval of VAR_INSTR
1727struct instr_S {
1728 ectx_T *instr_ectx;
1729 isn_T *instr_instr;
1730};
1731
Bram Moolenaar4c137212021-04-19 16:48:48 +02001732// used for substitute_instr
1733typedef struct subs_expr_S {
1734 ectx_T *subs_ectx;
1735 isn_T *subs_instr;
1736 int subs_status;
1737} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738
1739// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001740#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741
1742// Get pointer to item at the bottom of the stack, -1 is the bottom.
1743#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001744#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 +01001745
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001746// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001747#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 +01001748
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001749// Set when calling do_debug().
1750static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001751static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001752
1753/*
1754 * When debugging lookup "name" and return the typeval.
1755 * When not found return NULL.
1756 */
1757 typval_T *
1758lookup_debug_var(char_u *name)
1759{
1760 int idx;
1761 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001762 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001763 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001764 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001765
1766 if (ectx == NULL)
1767 return NULL;
1768 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1769
1770 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001771 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001772 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001773 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1774
1775 // the variable name may be NULL when not available in this block
1776 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001777 return STACK_TV_VAR(idx);
1778 }
1779
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001780 // Go through argument names.
1781 ufunc = dfunc->df_ufunc;
1782 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1783 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1784 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1785 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1786 - varargs_off + idx);
1787 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1788 return STACK_TV(ectx->ec_frame_idx - 1);
1789
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001790 return NULL;
1791}
1792
Bram Moolenaar26a44842021-09-02 18:49:06 +02001793/*
1794 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1795 * breakpoint was set in that function or when there is any expression.
1796 */
1797 int
1798may_break_in_function(ufunc_T *ufunc)
1799{
1800 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1801}
1802
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001803 static void
1804handle_debug(isn_T *iptr, ectx_T *ectx)
1805{
1806 char_u *line;
1807 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1808 + ectx->ec_dfunc_idx)->df_ufunc;
1809 isn_T *ni;
1810 int end_lnum = iptr->isn_lnum;
1811 garray_T ga;
1812 int lnum;
1813
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001814 if (ex_nesting_level > debug_break_level)
1815 {
1816 linenr_T breakpoint;
1817
Bram Moolenaar26a44842021-09-02 18:49:06 +02001818 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001819 return;
1820
1821 // check for the next breakpoint if needed
1822 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001823 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001824 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1825 return;
1826 }
1827
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001828 SOURCING_LNUM = iptr->isn_lnum;
1829 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001830 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001831
1832 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1833 if (ni->isn_type == ISN_DEBUG
1834 || ni->isn_type == ISN_RETURN
1835 || ni->isn_type == ISN_RETURN_VOID)
1836 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001837 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001838 break;
1839 }
1840
1841 if (end_lnum > iptr->isn_lnum)
1842 {
1843 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001844 for (lnum = iptr->isn_lnum; lnum < end_lnum
1845 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001846 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001847 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001848
Bram Moolenaar303215d2021-07-07 20:10:43 +02001849 if (p == NULL)
1850 continue; // left over from continuation line
1851 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001852 if (*p == '#')
1853 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001854 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001855 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1856 if (STRNCMP(p, "def ", 4) == 0)
1857 break;
1858 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001859 line = ga_concat_strings(&ga, " ");
1860 vim_free(ga.ga_data);
1861 }
1862 else
1863 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001864
Dominique Pelle6e969552021-06-17 13:53:41 +02001865 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001866 debug_context = NULL;
1867
1868 if (end_lnum > iptr->isn_lnum)
1869 vim_free(line);
1870}
1871
Bram Moolenaar4c137212021-04-19 16:48:48 +02001872/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001873 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001874 * Returns OK, FAIL or NOTDONE (uncatchable error).
1875 */
1876 static int
1877execute_storeindex(isn_T *iptr, ectx_T *ectx)
1878{
1879 vartype_T dest_type = iptr->isn_arg.vartype;
1880 typval_T *tv;
1881 typval_T *tv_idx = STACK_TV_BOT(-2);
1882 typval_T *tv_dest = STACK_TV_BOT(-1);
1883 int status = OK;
1884
1885 // Stack contains:
1886 // -3 value to be stored
1887 // -2 index
1888 // -1 dict or list
1889 tv = STACK_TV_BOT(-3);
1890 SOURCING_LNUM = iptr->isn_lnum;
1891 if (dest_type == VAR_ANY)
1892 {
1893 dest_type = tv_dest->v_type;
1894 if (dest_type == VAR_DICT)
1895 status = do_2string(tv_idx, TRUE, FALSE);
1896 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1897 {
1898 emsg(_(e_number_expected));
1899 status = FAIL;
1900 }
1901 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001902
1903 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001904 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001905 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001906 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001907 long lidx = (long)tv_idx->vval.v_number;
1908 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001909
Bram Moolenaare08be092022-02-17 13:08:26 +00001910 if (list == NULL)
1911 {
1912 emsg(_(e_list_not_set));
1913 return FAIL;
1914 }
1915 if (lidx < 0 && list->lv_len + lidx >= 0)
1916 // negative index is relative to the end
1917 lidx = list->lv_len + lidx;
1918 if (lidx < 0 || lidx > list->lv_len)
1919 {
1920 semsg(_(e_list_index_out_of_range_nr), lidx);
1921 return FAIL;
1922 }
1923 if (lidx < list->lv_len)
1924 {
1925 listitem_T *li = list_find(list, lidx);
1926
1927 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001928 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001929 return FAIL;
1930 // overwrite existing list item
1931 clear_tv(&li->li_tv);
1932 li->li_tv = *tv;
1933 }
1934 else
1935 {
1936 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1937 return FAIL;
1938 // append to list, only fails when out of memory
1939 if (list_append_tv(list, tv) == FAIL)
1940 return NOTDONE;
1941 clear_tv(tv);
1942 }
1943 }
1944 else if (dest_type == VAR_DICT)
1945 {
1946 char_u *key = tv_idx->vval.v_string;
1947 dict_T *dict = tv_dest->vval.v_dict;
1948 dictitem_T *di;
1949
1950 SOURCING_LNUM = iptr->isn_lnum;
1951 if (dict == NULL)
1952 {
1953 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001954 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001955 }
1956 if (key == NULL)
1957 key = (char_u *)"";
1958 di = dict_find(dict, key, -1);
1959 if (di != NULL)
1960 {
1961 if (error_if_locked(di->di_tv.v_lock,
1962 e_cannot_change_dict_item))
1963 return FAIL;
1964 // overwrite existing value
1965 clear_tv(&di->di_tv);
1966 di->di_tv = *tv;
1967 }
1968 else
1969 {
1970 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1971 return FAIL;
1972 // add to dict, only fails when out of memory
1973 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1974 return NOTDONE;
1975 clear_tv(tv);
1976 }
1977 }
1978 else if (dest_type == VAR_BLOB)
1979 {
1980 long lidx = (long)tv_idx->vval.v_number;
1981 blob_T *blob = tv_dest->vval.v_blob;
1982 varnumber_T nr;
1983 int error = FALSE;
1984 int len;
1985
1986 if (blob == NULL)
1987 {
1988 emsg(_(e_blob_not_set));
1989 return FAIL;
1990 }
1991 len = blob_len(blob);
1992 if (lidx < 0 && len + lidx >= 0)
1993 // negative index is relative to the end
1994 lidx = len + lidx;
1995
1996 // Can add one byte at the end.
1997 if (lidx < 0 || lidx > len)
1998 {
1999 semsg(_(e_blob_index_out_of_range_nr), lidx);
2000 return FAIL;
2001 }
2002 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2003 return FAIL;
2004 nr = tv_get_number_chk(tv, &error);
2005 if (error)
2006 return FAIL;
2007 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002008 }
2009 else
2010 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002011 status = FAIL;
2012 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002013 }
2014 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002015
2016 clear_tv(tv_idx);
2017 clear_tv(tv_dest);
2018 ectx->ec_stack.ga_len -= 3;
2019 if (status == FAIL)
2020 {
2021 clear_tv(tv);
2022 return FAIL;
2023 }
2024 return OK;
2025}
2026
2027/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002028 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002029 */
2030 static int
2031execute_storerange(isn_T *iptr, ectx_T *ectx)
2032{
2033 typval_T *tv;
2034 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2035 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2036 typval_T *tv_dest = STACK_TV_BOT(-1);
2037 int status = OK;
2038
2039 // Stack contains:
2040 // -4 value to be stored
2041 // -3 first index or "none"
2042 // -2 second index or "none"
2043 // -1 destination list or blob
2044 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002045 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002046 if (tv_dest->v_type == VAR_LIST)
2047 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002048 long n1;
2049 long n2;
2050 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002051
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002052 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2053 if (tv_idx2->v_type == VAR_SPECIAL
2054 && tv_idx2->vval.v_number == VVAL_NONE)
2055 n2 = list_len(tv_dest->vval.v_list) - 1;
2056 else
2057 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2058
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002059 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002060 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002061 status = FAIL;
2062 else
2063 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002064 status = check_range_index_two(tv_dest->vval.v_list,
2065 &n1, li1, &n2, FALSE);
2066 if (status != FAIL)
2067 status = list_assign_range(
2068 tv_dest->vval.v_list,
2069 tv->vval.v_list,
2070 n1,
2071 n2,
2072 tv_idx2->v_type == VAR_SPECIAL,
2073 (char_u *)"=",
2074 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002075 }
2076 }
2077 else if (tv_dest->v_type == VAR_BLOB)
2078 {
2079 varnumber_T n1;
2080 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002081 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002082
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002083 n1 = tv_get_number_chk(tv_idx1, NULL);
2084 if (tv_idx2->v_type == VAR_SPECIAL
2085 && tv_idx2->vval.v_number == VVAL_NONE)
2086 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2087 else
2088 n2 = tv_get_number_chk(tv_idx2, NULL);
2089 bloblen = blob_len(tv_dest->vval.v_blob);
2090
2091 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2092 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002093 status = FAIL;
2094 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002095 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002096 }
2097 else
2098 {
2099 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002100 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002101 }
2102
2103 clear_tv(tv_idx1);
2104 clear_tv(tv_idx2);
2105 clear_tv(tv_dest);
2106 ectx->ec_stack.ga_len -= 4;
2107 clear_tv(tv);
2108
2109 return status;
2110}
2111
2112/*
2113 * Unlet item in list or dict variable.
2114 */
2115 static int
2116execute_unletindex(isn_T *iptr, ectx_T *ectx)
2117{
2118 typval_T *tv_idx = STACK_TV_BOT(-2);
2119 typval_T *tv_dest = STACK_TV_BOT(-1);
2120 int status = OK;
2121
2122 // Stack contains:
2123 // -2 index
2124 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002125 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002126 if (tv_dest->v_type == VAR_DICT)
2127 {
2128 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002129 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002130 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002131 semsg(_(e_expected_str_but_got_str),
2132 vartype_name(VAR_STRING),
2133 vartype_name(tv_idx->v_type));
2134 status = FAIL;
2135 }
2136 else
2137 {
2138 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002139 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002140 dictitem_T *di = NULL;
2141
2142 if (d != NULL && value_check_lock(
2143 d->dv_lock, NULL, FALSE))
2144 status = FAIL;
2145 else
2146 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002147 if (tv_idx->v_type == VAR_STRING)
2148 {
2149 key = tv_idx->vval.v_string;
2150 if (key == NULL)
2151 key = (char_u *)"";
2152 }
2153 else
2154 {
2155 key = tv_get_string(tv_idx);
2156 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002157 if (d != NULL)
2158 di = dict_find(d, key, (int)STRLEN(key));
2159 if (di == NULL)
2160 {
2161 // NULL dict is equivalent to empty dict
2162 semsg(_(e_key_not_present_in_dictionary),
2163 key);
2164 status = FAIL;
2165 }
2166 else if (var_check_fixed(di->di_flags,
2167 NULL, FALSE)
2168 || var_check_ro(di->di_flags,
2169 NULL, FALSE))
2170 status = FAIL;
2171 else
2172 dictitem_remove(d, di);
2173 }
2174 }
2175 }
2176 else if (tv_dest->v_type == VAR_LIST)
2177 {
2178 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002179 if (check_for_number(tv_idx) == FAIL)
2180 {
2181 status = FAIL;
2182 }
2183 else
2184 {
2185 list_T *l = tv_dest->vval.v_list;
2186 long n = (long)tv_idx->vval.v_number;
2187
2188 if (l != NULL && value_check_lock(
2189 l->lv_lock, NULL, FALSE))
2190 status = FAIL;
2191 else
2192 {
2193 listitem_T *li = list_find(l, n);
2194
2195 if (li == NULL)
2196 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002197 semsg(_(e_list_index_out_of_range_nr), n);
2198 status = FAIL;
2199 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002200 else
2201 listitem_remove(l, li);
2202 }
2203 }
2204 }
2205 else
2206 {
2207 status = FAIL;
2208 semsg(_(e_cannot_index_str),
2209 vartype_name(tv_dest->v_type));
2210 }
2211
2212 clear_tv(tv_idx);
2213 clear_tv(tv_dest);
2214 ectx->ec_stack.ga_len -= 2;
2215
2216 return status;
2217}
2218
2219/*
2220 * Unlet a range of items in a list variable.
2221 */
2222 static int
2223execute_unletrange(isn_T *iptr, ectx_T *ectx)
2224{
2225 // Stack contains:
2226 // -3 index1
2227 // -2 index2
2228 // -1 dict or list
2229 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2230 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2231 typval_T *tv_dest = STACK_TV_BOT(-1);
2232 int status = OK;
2233
2234 if (tv_dest->v_type == VAR_LIST)
2235 {
2236 // indexes must be a number
2237 SOURCING_LNUM = iptr->isn_lnum;
2238 if (check_for_number(tv_idx1) == FAIL
2239 || (tv_idx2->v_type != VAR_SPECIAL
2240 && check_for_number(tv_idx2) == FAIL))
2241 {
2242 status = FAIL;
2243 }
2244 else
2245 {
2246 list_T *l = tv_dest->vval.v_list;
2247 long n1 = (long)tv_idx1->vval.v_number;
2248 long n2 = tv_idx2->v_type == VAR_SPECIAL
2249 ? 0 : (long)tv_idx2->vval.v_number;
2250 listitem_T *li;
2251
2252 li = list_find_index(l, &n1);
2253 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002254 {
2255 semsg(_(e_list_index_out_of_range_nr),
2256 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002257 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002258 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002259 else
2260 {
2261 if (n1 < 0)
2262 n1 = list_idx_of_item(l, li);
2263 if (n2 < 0)
2264 {
2265 listitem_T *li2 = list_find(l, n2);
2266
2267 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002268 {
2269 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002270 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002271 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002272 else
2273 n2 = list_idx_of_item(l, li2);
2274 }
2275 if (status != FAIL
2276 && tv_idx2->v_type != VAR_SPECIAL
2277 && n2 < n1)
2278 {
2279 semsg(_(e_list_index_out_of_range_nr), n2);
2280 status = FAIL;
2281 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002282 if (status != FAIL)
2283 list_unlet_range(l, li, n1,
2284 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002285 }
2286 }
2287 }
2288 else
2289 {
2290 status = FAIL;
2291 SOURCING_LNUM = iptr->isn_lnum;
2292 semsg(_(e_cannot_index_str),
2293 vartype_name(tv_dest->v_type));
2294 }
2295
2296 clear_tv(tv_idx1);
2297 clear_tv(tv_idx2);
2298 clear_tv(tv_dest);
2299 ectx->ec_stack.ga_len -= 3;
2300
2301 return status;
2302}
2303
2304/*
2305 * Top of a for loop.
2306 */
2307 static int
2308execute_for(isn_T *iptr, ectx_T *ectx)
2309{
2310 typval_T *tv;
2311 typval_T *ltv = STACK_TV_BOT(-1);
2312 typval_T *idxtv =
2313 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2314
2315 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2316 return FAIL;
2317 if (ltv->v_type == VAR_LIST)
2318 {
2319 list_T *list = ltv->vval.v_list;
2320
2321 // push the next item from the list
2322 ++idxtv->vval.v_number;
2323 if (list == NULL
2324 || idxtv->vval.v_number >= list->lv_len)
2325 {
2326 // past the end of the list, jump to "endfor"
2327 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2328 may_restore_cmdmod(&ectx->ec_funclocal);
2329 }
2330 else if (list->lv_first == &range_list_item)
2331 {
2332 // non-materialized range() list
2333 tv = STACK_TV_BOT(0);
2334 tv->v_type = VAR_NUMBER;
2335 tv->v_lock = 0;
2336 tv->vval.v_number = list_find_nr(
2337 list, idxtv->vval.v_number, NULL);
2338 ++ectx->ec_stack.ga_len;
2339 }
2340 else
2341 {
2342 listitem_T *li = list_find(list,
2343 idxtv->vval.v_number);
2344
2345 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2346 ++ectx->ec_stack.ga_len;
2347 }
2348 }
2349 else if (ltv->v_type == VAR_STRING)
2350 {
2351 char_u *str = ltv->vval.v_string;
2352
2353 // The index is for the last byte of the previous
2354 // character.
2355 ++idxtv->vval.v_number;
2356 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2357 {
2358 // past the end of the string, jump to "endfor"
2359 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2360 may_restore_cmdmod(&ectx->ec_funclocal);
2361 }
2362 else
2363 {
2364 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2365
2366 // Push the next character from the string.
2367 tv = STACK_TV_BOT(0);
2368 tv->v_type = VAR_STRING;
2369 tv->vval.v_string = vim_strnsave(
2370 str + idxtv->vval.v_number, clen);
2371 ++ectx->ec_stack.ga_len;
2372 idxtv->vval.v_number += clen - 1;
2373 }
2374 }
2375 else if (ltv->v_type == VAR_BLOB)
2376 {
2377 blob_T *blob = ltv->vval.v_blob;
2378
2379 // When we get here the first time make a copy of the
2380 // blob, so that the iteration still works when it is
2381 // changed.
2382 if (idxtv->vval.v_number == -1 && blob != NULL)
2383 {
2384 blob_copy(blob, ltv);
2385 blob_unref(blob);
2386 blob = ltv->vval.v_blob;
2387 }
2388
2389 // The index is for the previous byte.
2390 ++idxtv->vval.v_number;
2391 if (blob == NULL
2392 || idxtv->vval.v_number >= blob_len(blob))
2393 {
2394 // past the end of the blob, jump to "endfor"
2395 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2396 may_restore_cmdmod(&ectx->ec_funclocal);
2397 }
2398 else
2399 {
2400 // Push the next byte from the blob.
2401 tv = STACK_TV_BOT(0);
2402 tv->v_type = VAR_NUMBER;
2403 tv->vval.v_number = blob_get(blob,
2404 idxtv->vval.v_number);
2405 ++ectx->ec_stack.ga_len;
2406 }
2407 }
2408 else
2409 {
2410 semsg(_(e_for_loop_on_str_not_supported),
2411 vartype_name(ltv->v_type));
2412 return FAIL;
2413 }
2414 return OK;
2415}
2416
2417/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002418 * Load instruction for w:/b:/g:/t: variable.
2419 * "isn_type" is used instead of "iptr->isn_type".
2420 */
2421 static int
2422load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2423{
2424 dictitem_T *di = NULL;
2425 hashtab_T *ht = NULL;
2426 char namespace;
2427
2428 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2429 return NOTDONE;
2430 switch (isn_type)
2431 {
2432 case ISN_LOADG:
2433 ht = get_globvar_ht();
2434 namespace = 'g';
2435 break;
2436 case ISN_LOADB:
2437 ht = &curbuf->b_vars->dv_hashtab;
2438 namespace = 'b';
2439 break;
2440 case ISN_LOADW:
2441 ht = &curwin->w_vars->dv_hashtab;
2442 namespace = 'w';
2443 break;
2444 case ISN_LOADT:
2445 ht = &curtab->tp_vars->dv_hashtab;
2446 namespace = 't';
2447 break;
2448 default: // Cannot reach here
2449 return NOTDONE;
2450 }
2451 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2452
Bram Moolenaar06b77222022-01-25 15:51:56 +00002453 if (di == NULL)
2454 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002455 if (isn_type == ISN_LOADG)
2456 {
2457 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2458
2459 // g:Something could be a function
2460 if (ufunc != NULL)
2461 {
2462 typval_T *tv = STACK_TV_BOT(0);
2463
2464 ++ectx->ec_stack.ga_len;
2465 tv->v_type = VAR_FUNC;
2466 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2467 if (tv->vval.v_string == NULL)
2468 return FAIL;
2469 STRCPY(tv->vval.v_string, "g:");
2470 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2471 return OK;
2472 }
2473 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002474 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002475 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002476 // no check if the item exists in the script but
2477 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002478 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002479 else
2480 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002481 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002482 return FAIL;
2483 }
2484 else
2485 {
2486 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2487 ++ectx->ec_stack.ga_len;
2488 }
2489 return OK;
2490}
2491
2492/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002493 * Execute instructions in execution context "ectx".
2494 * Return OK or FAIL;
2495 */
2496 static int
2497exec_instructions(ectx_T *ectx)
2498{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002499 int ret = FAIL;
2500 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002501 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002502
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002503 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002504 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002505
Bram Moolenaarff652882021-05-16 15:24:49 +02002506 // Only catch exceptions in this instruction list.
2507 ectx->ec_trylevel_at_start = trylevel;
2508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 for (;;)
2510 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002511 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002513 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002514
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002515 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002516 {
2517 line_breakcheck();
2518 breakcheck_count = 0;
2519 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002520 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002521 {
2522 // Turn CTRL-C into an exception.
2523 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002524 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002525 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002526 did_throw = TRUE;
2527 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002529 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002530 {
2531 // Turn an error message into an exception.
2532 did_emsg = FALSE;
2533 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002534 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002535 did_throw = TRUE;
2536 *msg_list = NULL;
2537 }
2538
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002539 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002541 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002542 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002543 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544
2545 // An exception jumps to the first catch, finally, or returns from
2546 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002547 while (index > 0)
2548 {
2549 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002550 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002551 break;
2552 // In the catch and finally block of this try we have to go up
2553 // one level.
2554 --index;
2555 trycmd = NULL;
2556 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002557 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002559 if (trycmd->tcd_in_catch)
2560 {
2561 // exception inside ":catch", jump to ":finally" once
2562 ectx->ec_iidx = trycmd->tcd_finally_idx;
2563 trycmd->tcd_finally_idx = 0;
2564 }
2565 else
2566 // jump to first ":catch"
2567 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002568 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002569 did_throw = FALSE; // don't come back here until :endtry
2570 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 }
2572 else
2573 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002574 // Not inside try or need to return from current functions.
2575 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002576 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002577 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002578 tv = STACK_TV_BOT(0);
2579 tv->v_type = VAR_NUMBER;
2580 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002581 ++ectx->ec_stack.ga_len;
2582 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002584 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002585 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002586 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002587 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 goto done;
2589 }
2590
Bram Moolenaar4c137212021-04-19 16:48:48 +02002591 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002592 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 }
2594 continue;
2595 }
2596
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002597 /*
2598 * Big switch on the instruction. Most compilers will be turning this
2599 * into an efficient lookup table, since the "case" values are an enum
2600 * with sequential numbers. It may look ugly, but it should be the
2601 * most efficient way.
2602 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002603 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 switch (iptr->isn_type)
2605 {
2606 // execute Ex command line
2607 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002608 if (exec_command(iptr) == FAIL)
2609 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 break;
2611
Bram Moolenaar20677332021-06-06 17:02:53 +02002612 // execute Ex command line split at NL characters.
2613 case ISN_EXEC_SPLIT:
2614 {
2615 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002616 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002617
2618 SOURCING_LNUM = iptr->isn_lnum;
2619 CLEAR_FIELD(cookie);
2620 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2621 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002622 line = get_split_sourceline(0, &cookie, 0, 0);
2623 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002624 get_split_sourceline, &cookie,
2625 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2626 == FAIL
2627 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002628 {
2629 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002630 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002631 }
2632 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002633 }
2634 break;
2635
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002636 // execute Ex command line that is only a range
2637 case ISN_EXECRANGE:
2638 {
2639 exarg_T ea;
2640 char *error = NULL;
2641
2642 CLEAR_FIELD(ea);
2643 ea.cmdidx = CMD_SIZE;
2644 ea.addr_type = ADDR_LINES;
2645 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002646 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002647 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002648 if (ea.cmd == NULL)
2649 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002650 // error is always NULL when using ADDR_LINES
2651 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002652 if (error != NULL)
2653 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002654 emsg(error);
2655 goto on_error;
2656 }
2657 }
2658 break;
2659
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002660 // Evaluate an expression with legacy syntax, push it onto the
2661 // stack.
2662 case ISN_LEGACY_EVAL:
2663 {
2664 char_u *arg = iptr->isn_arg.string;
2665 int res;
2666 int save_flags = cmdmod.cmod_flags;
2667
Bram Moolenaar35578162021-08-02 19:10:38 +02002668 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002669 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002670 tv = STACK_TV_BOT(0);
2671 init_tv(tv);
2672 cmdmod.cmod_flags |= CMOD_LEGACY;
2673 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2674 cmdmod.cmod_flags = save_flags;
2675 if (res == FAIL)
2676 goto on_error;
2677 ++ectx->ec_stack.ga_len;
2678 }
2679 break;
2680
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002681 // push typeval VAR_INSTR with instructions to be executed
2682 case ISN_INSTR:
2683 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002684 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002685 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002686 tv = STACK_TV_BOT(0);
2687 tv->vval.v_instr = ALLOC_ONE(instr_T);
2688 if (tv->vval.v_instr == NULL)
2689 goto on_error;
2690 ++ectx->ec_stack.ga_len;
2691
2692 tv->v_type = VAR_INSTR;
2693 tv->vval.v_instr->instr_ectx = ectx;
2694 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2695 }
2696 break;
2697
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002698 case ISN_SOURCE:
2699 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002700 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002701
Bram Moolenaar89445512022-04-14 12:58:23 +01002702 SOURCING_LNUM = iptr->isn_lnum;
2703 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002704 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002705 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002706 }
2707 break;
2708
Bram Moolenaar4c137212021-04-19 16:48:48 +02002709 // execute :substitute with an expression
2710 case ISN_SUBSTITUTE:
2711 {
2712 subs_T *subs = &iptr->isn_arg.subs;
2713 source_cookie_T cookie;
2714 struct subs_expr_S *save_instr = substitute_instr;
2715 struct subs_expr_S subs_instr;
2716 int res;
2717
2718 subs_instr.subs_ectx = ectx;
2719 subs_instr.subs_instr = subs->subs_instr;
2720 subs_instr.subs_status = OK;
2721 substitute_instr = &subs_instr;
2722
2723 SOURCING_LNUM = iptr->isn_lnum;
2724 // This is very much like ISN_EXEC
2725 CLEAR_FIELD(cookie);
2726 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2727 res = do_cmdline(subs->subs_cmd,
2728 getsourceline, &cookie,
2729 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2730 substitute_instr = save_instr;
2731
2732 if (res == FAIL || did_emsg
2733 || subs_instr.subs_status == FAIL)
2734 goto on_error;
2735 }
2736 break;
2737
2738 case ISN_FINISH:
2739 goto done;
2740
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002741 case ISN_REDIRSTART:
2742 // create a dummy entry for var_redir_str()
2743 if (alloc_redir_lval() == FAIL)
2744 goto on_error;
2745
2746 // The output is stored in growarray "redir_ga" until
2747 // redirection ends.
2748 init_redir_ga();
2749 redir_vname = 1;
2750 break;
2751
2752 case ISN_REDIREND:
2753 {
2754 char_u *res = get_clear_redir_ga();
2755
2756 // End redirection, put redirected text on the stack.
2757 clear_redir_lval();
2758 redir_vname = 0;
2759
Bram Moolenaar35578162021-08-02 19:10:38 +02002760 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002761 {
2762 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002763 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002764 }
2765 tv = STACK_TV_BOT(0);
2766 tv->v_type = VAR_STRING;
2767 tv->vval.v_string = res;
2768 ++ectx->ec_stack.ga_len;
2769 }
2770 break;
2771
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002772 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002773#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002774 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002775 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2776 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002777 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002778#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002779 break;
2780
2781 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002782#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002783 {
2784 exarg_T ea;
2785 int res;
2786
2787 CLEAR_FIELD(ea);
2788 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2789 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2790 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2791 --ectx->ec_stack.ga_len;
2792 tv = STACK_TV_BOT(0);
2793 res = cexpr_core(&ea, tv);
2794 clear_tv(tv);
2795 if (res == FAIL)
2796 goto on_error;
2797 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002798#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002799 break;
2800
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002801 // execute Ex command from pieces on the stack
2802 case ISN_EXECCONCAT:
2803 {
2804 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002805 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002806 int pass;
2807 int i;
2808 char_u *cmd = NULL;
2809 char_u *str;
2810
2811 for (pass = 1; pass <= 2; ++pass)
2812 {
2813 for (i = 0; i < count; ++i)
2814 {
2815 tv = STACK_TV_BOT(i - count);
2816 str = tv->vval.v_string;
2817 if (str != NULL && *str != NUL)
2818 {
2819 if (pass == 2)
2820 STRCPY(cmd + len, str);
2821 len += STRLEN(str);
2822 }
2823 if (pass == 2)
2824 clear_tv(tv);
2825 }
2826 if (pass == 1)
2827 {
2828 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002829 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002830 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002831 len = 0;
2832 }
2833 }
2834
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002835 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002836 do_cmdline_cmd(cmd);
2837 vim_free(cmd);
2838 }
2839 break;
2840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 // execute :echo {string} ...
2842 case ISN_ECHO:
2843 {
2844 int count = iptr->isn_arg.echo.echo_count;
2845 int atstart = TRUE;
2846 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002847 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848
2849 for (idx = 0; idx < count; ++idx)
2850 {
2851 tv = STACK_TV_BOT(idx - count);
2852 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2853 &atstart, &needclr);
2854 clear_tv(tv);
2855 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002856 if (needclr)
2857 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002858 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 }
2860 break;
2861
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002862 // :execute {string} ...
2863 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002864 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002865 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002866 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002867 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002868 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002869 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002870 {
2871 int count = iptr->isn_arg.number;
2872 garray_T ga;
2873 char_u buf[NUMBUFLEN];
2874 char_u *p;
2875 int len;
2876 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002877 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002878
2879 ga_init2(&ga, 1, 80);
2880 for (idx = 0; idx < count; ++idx)
2881 {
2882 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002883 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002884 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002885 if (tv->v_type == VAR_CHANNEL
2886 || tv->v_type == VAR_JOB)
2887 {
2888 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002889 semsg(_(e_using_invalid_value_as_string_str),
2890 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002891 break;
2892 }
2893 else
2894 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002895 }
2896 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002897 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002898
2899 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002900 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002901 failed = TRUE;
2902 else
2903 {
2904 if (ga.ga_len > 0)
2905 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2906 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2907 ga.ga_len += len;
2908 }
2909 clear_tv(tv);
2910 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002911 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002912 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002913 {
2914 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002915 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002916 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002917
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002918 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002919 {
2920 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002921 {
2922 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002923 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002924 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002925 {
2926 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002927 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002928 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002929 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002930 else
2931 {
2932 msg_sb_eol();
2933 if (iptr->isn_type == ISN_ECHOMSG)
2934 {
2935 msg_attr(ga.ga_data, echo_attr);
2936 out_flush();
2937 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002938 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2939 {
2940 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2941 TRUE);
2942 ui_write((char_u *)"\r\n", 2, TRUE);
2943 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002944 else
2945 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002946 SOURCING_LNUM = iptr->isn_lnum;
2947 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002948 }
2949 }
2950 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002951 ga_clear(&ga);
2952 }
2953 break;
2954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 // load local variable or argument
2956 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002957 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002958 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002960 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 break;
2962
2963 // load v: variable
2964 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002965 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002966 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002968 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 break;
2970
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002971 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 case ISN_LOADSCRIPT:
2973 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002974 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 svar_T *sv;
2976
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002977 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002978 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002979 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002980 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002981 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002982 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002984 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 }
2986 break;
2987
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002988 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002990 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002992 int sid = iptr->isn_arg.loadstore.ls_sid;
2993 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002994 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 if (di == NULL)
2998 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002999 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003000 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003001 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 }
3003 else
3004 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003005 if (iptr->isn_type == ISN_LOADEXPORT)
3006 {
3007 int idx = get_script_item_idx(sid, name, 0,
3008 NULL, NULL);
3009 svar_T *sv;
3010
3011 if (idx >= 0)
3012 {
3013 sv = ((svar_T *)SCRIPT_ITEM(sid)
3014 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003015 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003016 {
3017 SOURCING_LNUM = iptr->isn_lnum;
3018 semsg(_(e_item_not_exported_in_script_str),
3019 name);
3020 goto on_error;
3021 }
3022 }
3023 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003024 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003025 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003027 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 }
3029 }
3030 break;
3031
Bram Moolenaard3aac292020-04-19 14:32:17 +02003032 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003034 case ISN_LOADB:
3035 case ISN_LOADW:
3036 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003038 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003039
Bram Moolenaar06b77222022-01-25 15:51:56 +00003040 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003041 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003042 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003043 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 break;
3047
Bram Moolenaar03290b82020-12-19 16:30:44 +01003048 // load autoload variable
3049 case ISN_LOADAUTO:
3050 {
3051 char_u *name = iptr->isn_arg.string;
3052
Bram Moolenaar35578162021-08-02 19:10:38 +02003053 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003054 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003055 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003056 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003057 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003058 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003059 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003060 }
3061 break;
3062
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003063 // load g:/b:/w:/t: namespace
3064 case ISN_LOADGDICT:
3065 case ISN_LOADBDICT:
3066 case ISN_LOADWDICT:
3067 case ISN_LOADTDICT:
3068 {
3069 dict_T *d = NULL;
3070
3071 switch (iptr->isn_type)
3072 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003073 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3074 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3075 case ISN_LOADWDICT: d = curwin->w_vars; break;
3076 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003077 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003078 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003079 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003080 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003081 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003082 tv = STACK_TV_BOT(0);
3083 tv->v_type = VAR_DICT;
3084 tv->v_lock = 0;
3085 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003086 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003087 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003088 }
3089 break;
3090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 // load &option
3092 case ISN_LOADOPT:
3093 {
3094 typval_T optval;
3095 char_u *name = iptr->isn_arg.string;
3096
Bram Moolenaara8c17702020-04-01 21:17:24 +02003097 // This is not expected to fail, name is checked during
3098 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003099 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003100 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003101 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003102 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003104 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 }
3106 break;
3107
3108 // load $ENV
3109 case ISN_LOADENV:
3110 {
3111 typval_T optval;
3112 char_u *name = iptr->isn_arg.string;
3113
Bram Moolenaar35578162021-08-02 19:10:38 +02003114 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003115 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003116 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003117 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003119 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 }
3121 break;
3122
3123 // load @register
3124 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003125 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003126 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 tv = STACK_TV_BOT(0);
3128 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003129 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003130 // This may result in NULL, which should be equivalent to an
3131 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 tv->vval.v_string = get_reg_contents(
3133 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003134 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 break;
3136
3137 // store local variable
3138 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003139 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 tv = STACK_TV_VAR(iptr->isn_arg.number);
3141 clear_tv(tv);
3142 *tv = *STACK_TV_BOT(0);
3143 break;
3144
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003145 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003146 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003147 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003148 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003149 int sid = iptr->isn_arg.loadstore.ls_sid;
3150 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003151 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003152 dictitem_T *di = find_var_in_ht(ht, 0,
3153 iptr->isn_type == ISN_STORES
3154 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003155
Bram Moolenaar4c137212021-04-19 16:48:48 +02003156 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003157 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003158 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003159 {
3160 if (iptr->isn_type == ISN_STOREEXPORT)
3161 {
3162 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003163 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003164 goto on_error;
3165 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003166 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003167 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003168 else
3169 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003170 if (iptr->isn_type == ISN_STOREEXPORT)
3171 {
3172 int idx = get_script_item_idx(sid, name, 0,
3173 NULL, NULL);
3174
3175 if (idx >= 0)
3176 {
3177 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3178 ->sn_var_vals.ga_data) + idx;
3179
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003180 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003181 {
3182 semsg(_(e_item_not_exported_in_script_str),
3183 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003184 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003185 goto on_error;
3186 }
3187 }
3188 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003189 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003190 {
3191 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003192 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003193 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003194 clear_tv(&di->di_tv);
3195 di->di_tv = *STACK_TV_BOT(0);
3196 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003197 }
3198 break;
3199
3200 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 case ISN_STORESCRIPT:
3202 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003203 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3204 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003206 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003207 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003208 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003209 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003210
3211 // "const" and "final" are checked at compile time, locking
3212 // the value needs to be checked here.
3213 SOURCING_LNUM = iptr->isn_lnum;
3214 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003215 {
3216 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003217 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003218 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 clear_tv(sv->sv_tv);
3221 *sv->sv_tv = *STACK_TV_BOT(0);
3222 }
3223 break;
3224
3225 // store option
3226 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003227 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003229 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3230 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 long n = 0;
3232 char_u *s = NULL;
3233 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003234 char_u numbuf[NUMBUFLEN];
3235 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236
Bram Moolenaar4c137212021-04-19 16:48:48 +02003237 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 tv = STACK_TV_BOT(0);
3239 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003240 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003242 if (s == NULL)
3243 s = (char_u *)"";
3244 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003245 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3246 {
3247 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003248 // If the option can be set to a function reference or
3249 // a lambda and the passed value is a function
3250 // reference, then convert it to the name (string) of
3251 // the function reference.
3252 s = tv2string(tv, &tofree, numbuf, 0);
3253 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003254 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003255 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003256 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003257 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003258 goto on_error;
3259 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003260 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003262 // must be VAR_NUMBER, CHECKTYPE makes sure
3263 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003264 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003265 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003266 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 if (msg != NULL)
3268 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003269 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003271 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 }
3274 break;
3275
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003276 // store $ENV
3277 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003278 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003279 tv = STACK_TV_BOT(0);
3280 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3281 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003282 break;
3283
3284 // store @r
3285 case ISN_STOREREG:
3286 {
3287 int reg = iptr->isn_arg.number;
3288
Bram Moolenaar4c137212021-04-19 16:48:48 +02003289 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003290 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003291 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003292 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003293 }
3294 break;
3295
3296 // store v: variable
3297 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003298 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003299 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3300 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003301 // should not happen, type is checked when compiling
3302 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003303 break;
3304
Bram Moolenaard3aac292020-04-19 14:32:17 +02003305 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003307 case ISN_STOREB:
3308 case ISN_STOREW:
3309 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003311 dictitem_T *di;
3312 hashtab_T *ht;
3313 char_u *name = iptr->isn_arg.string + 2;
3314
Bram Moolenaard3aac292020-04-19 14:32:17 +02003315 switch (iptr->isn_type)
3316 {
3317 case ISN_STOREG:
3318 ht = get_globvar_ht();
3319 break;
3320 case ISN_STOREB:
3321 ht = &curbuf->b_vars->dv_hashtab;
3322 break;
3323 case ISN_STOREW:
3324 ht = &curwin->w_vars->dv_hashtab;
3325 break;
3326 case ISN_STORET:
3327 ht = &curtab->tp_vars->dv_hashtab;
3328 break;
3329 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003330 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332
Bram Moolenaar4c137212021-04-19 16:48:48 +02003333 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003334 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003336 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 else
3338 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003339 SOURCING_LNUM = iptr->isn_lnum;
3340 if (var_check_permission(di, name) == FAIL)
3341 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 clear_tv(&di->di_tv);
3343 di->di_tv = *STACK_TV_BOT(0);
3344 }
3345 }
3346 break;
3347
Bram Moolenaar03290b82020-12-19 16:30:44 +01003348 // store an autoload variable
3349 case ISN_STOREAUTO:
3350 SOURCING_LNUM = iptr->isn_lnum;
3351 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3352 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003353 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003354 break;
3355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 // store number in local variable
3357 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003358 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 clear_tv(tv);
3360 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003361 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 break;
3363
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003364 // store value in list or dict variable
3365 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003366 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003367 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003368
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003369 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003370 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003371 if (res == NOTDONE)
3372 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003373 }
3374 break;
3375
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003376 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003377 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003378 if (execute_storerange(iptr, ectx) == FAIL)
3379 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003380 break;
3381
Bram Moolenaar0186e582021-01-10 18:33:11 +01003382 // load or store variable or argument from outer scope
3383 case ISN_LOADOUTER:
3384 case ISN_STOREOUTER:
3385 {
3386 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003387 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3388 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003389
3390 while (depth > 1 && outer != NULL)
3391 {
3392 outer = outer->out_up;
3393 --depth;
3394 }
3395 if (outer == NULL)
3396 {
3397 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003398 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3399 || ectx->ec_outer_ref == NULL)
3400 // Possibly :def function called from legacy
3401 // context.
3402 emsg(_(e_closure_called_from_invalid_context));
3403 else
3404 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003405 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003406 }
3407 tv = ((typval_T *)outer->out_stack->ga_data)
3408 + outer->out_frame_idx + STACK_FRAME_SIZE
3409 + iptr->isn_arg.outer.outer_idx;
3410 if (iptr->isn_type == ISN_LOADOUTER)
3411 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003412 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003413 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003414 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003415 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003416 }
3417 else
3418 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003419 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003420 clear_tv(tv);
3421 *tv = *STACK_TV_BOT(0);
3422 }
3423 }
3424 break;
3425
Bram Moolenaar752fc692021-01-04 21:57:11 +01003426 // unlet item in list or dict variable
3427 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003428 if (execute_unletindex(iptr, ectx) == FAIL)
3429 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003430 break;
3431
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003432 // unlet range of items in list variable
3433 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003434 if (execute_unletrange(iptr, ectx) == FAIL)
3435 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003436 break;
3437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 // push constant
3439 case ISN_PUSHNR:
3440 case ISN_PUSHBOOL:
3441 case ISN_PUSHSPEC:
3442 case ISN_PUSHF:
3443 case ISN_PUSHS:
3444 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003445 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003446 case ISN_PUSHCHANNEL:
3447 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003448 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003449 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003451 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003452 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 switch (iptr->isn_type)
3454 {
3455 case ISN_PUSHNR:
3456 tv->v_type = VAR_NUMBER;
3457 tv->vval.v_number = iptr->isn_arg.number;
3458 break;
3459 case ISN_PUSHBOOL:
3460 tv->v_type = VAR_BOOL;
3461 tv->vval.v_number = iptr->isn_arg.number;
3462 break;
3463 case ISN_PUSHSPEC:
3464 tv->v_type = VAR_SPECIAL;
3465 tv->vval.v_number = iptr->isn_arg.number;
3466 break;
3467#ifdef FEAT_FLOAT
3468 case ISN_PUSHF:
3469 tv->v_type = VAR_FLOAT;
3470 tv->vval.v_float = iptr->isn_arg.fnumber;
3471 break;
3472#endif
3473 case ISN_PUSHBLOB:
3474 blob_copy(iptr->isn_arg.blob, tv);
3475 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003476 case ISN_PUSHFUNC:
3477 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003478 if (iptr->isn_arg.string == NULL)
3479 tv->vval.v_string = NULL;
3480 else
3481 tv->vval.v_string =
3482 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003483 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003484 case ISN_PUSHCHANNEL:
3485#ifdef FEAT_JOB_CHANNEL
3486 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003487 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003488#endif
3489 break;
3490 case ISN_PUSHJOB:
3491#ifdef FEAT_JOB_CHANNEL
3492 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003493 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003494#endif
3495 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 default:
3497 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003498 tv->vval.v_string = iptr->isn_arg.string == NULL
3499 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 }
3501 break;
3502
Bram Moolenaar06b77222022-01-25 15:51:56 +00003503 case ISN_AUTOLOAD:
3504 {
3505 char_u *name = iptr->isn_arg.string;
3506
3507 (void)script_autoload(name, FALSE);
3508 if (find_func(name, TRUE))
3509 {
3510 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3511 goto theend;
3512 tv = STACK_TV_BOT(0);
3513 tv->v_lock = 0;
3514 ++ectx->ec_stack.ga_len;
3515 tv->v_type = VAR_FUNC;
3516 tv->vval.v_string = vim_strsave(name);
3517 }
3518 else
3519 {
3520 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3521
3522 if (res == NOTDONE)
3523 goto theend;
3524 if (res == FAIL)
3525 goto on_error;
3526 }
3527 }
3528 break;
3529
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003530 case ISN_UNLET:
3531 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3532 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003533 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003534 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003535 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003536 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003537 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003538
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003539 case ISN_LOCKUNLOCK:
3540 {
3541 typval_T *lval_root_save = lval_root;
3542 int res;
3543
3544 // Stack has the local variable, argument the whole :lock
3545 // or :unlock command, like ISN_EXEC.
3546 --ectx->ec_stack.ga_len;
3547 lval_root = STACK_TV_BOT(0);
3548 res = exec_command(iptr);
3549 clear_tv(lval_root);
3550 lval_root = lval_root_save;
3551 if (res == FAIL)
3552 goto on_error;
3553 }
3554 break;
3555
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003556 case ISN_LOCKCONST:
3557 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3558 break;
3559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 // create a list from items on the stack; uses a single allocation
3561 // for the list header and the items
3562 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003563 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003564 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 break;
3566
3567 // create a dict from items on the stack
3568 case ISN_NEWDICT:
3569 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003570 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003572 SOURCING_LNUM = iptr->isn_lnum;
3573 res = exe_newdict(iptr->isn_arg.number, ectx);
3574 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003575 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003576 if (res == MAYBE)
3577 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 }
3579 break;
3580
LemonBoy372bcce2022-04-25 12:43:20 +01003581 case ISN_CONCAT:
3582 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3583 goto theend;
3584 break;
3585
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003586 // create a partial with NULL value
3587 case ISN_NEWPARTIAL:
3588 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3589 goto theend;
3590 ++ectx->ec_stack.ga_len;
3591 tv = STACK_TV_BOT(-1);
3592 tv->v_type = VAR_PARTIAL;
3593 tv->v_lock = 0;
3594 tv->vval.v_partial = NULL;
3595 break;
3596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 // call a :def function
3598 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003599 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003600 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3601 NULL,
3602 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003603 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003604 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 break;
3606
3607 // call a builtin function
3608 case ISN_BCALL:
3609 SOURCING_LNUM = iptr->isn_lnum;
3610 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3611 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003612 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003613 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 break;
3615
3616 // call a funcref or partial
3617 case ISN_PCALL:
3618 {
3619 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3620 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003621 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622
3623 SOURCING_LNUM = iptr->isn_lnum;
3624 if (pfunc->cpf_top)
3625 {
3626 // funcref is above the arguments
3627 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3628 }
3629 else
3630 {
3631 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003632 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003633 partial_tv = *STACK_TV_BOT(0);
3634 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003636 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003637 if (tv == &partial_tv)
3638 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003640 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 }
3642 break;
3643
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003644 case ISN_PCALL_END:
3645 // PCALL finished, arguments have been consumed and replaced by
3646 // the return value. Now clear the funcref from the stack,
3647 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003648 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003649 clear_tv(STACK_TV_BOT(-1));
3650 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3651 break;
3652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 // call a user defined function or funcref/partial
3654 case ISN_UCALL:
3655 {
3656 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3657
3658 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003659 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003660 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003661 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 }
3663 break;
3664
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003665 // return from a :def function call without a value
3666 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003667 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003668 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003669 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003670 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003671 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003672 tv->vval.v_number = 0;
3673 tv->v_lock = 0;
3674 // FALLTHROUGH
3675
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003676 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 case ISN_RETURN:
3678 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003679 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003680 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003681
3682 if (trystack->ga_len > 0)
3683 trycmd = ((trycmd_T *)trystack->ga_data)
3684 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003685 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003686 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003688 // jump to ":finally" or ":endtry"
3689 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003690 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003691 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003692 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 trycmd->tcd_return = TRUE;
3694 }
3695 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003696 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 }
3698 break;
3699
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003700 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 case ISN_FUNCREF:
3702 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003703 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003704 ufunc_T *ufunc;
3705 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003708 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003709 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003710 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003711 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003712 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003713 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003714 if (funcref->fr_func_name == NULL)
3715 {
3716 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3717 + funcref->fr_dfunc_idx;
3718
3719 ufunc = pt_dfunc->df_ufunc;
3720 }
3721 else
3722 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003723 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003724 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003725 if (ufunc == NULL)
3726 {
3727 SOURCING_LNUM = iptr->isn_lnum;
3728 iemsg("ufunc unexpectedly NULL for FUNCREF");
3729 goto theend;
3730 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003731 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003732 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003734 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 tv->vval.v_partial = pt;
3736 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003737 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 }
3739 break;
3740
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003741 // Create a global function from a lambda.
3742 case ISN_NEWFUNC:
3743 {
3744 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3745
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003746 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003747 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003748 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003749 }
3750 break;
3751
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003752 // List functions
3753 case ISN_DEF:
3754 if (iptr->isn_arg.string == NULL)
3755 list_functions(NULL);
3756 else
3757 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003758 exarg_T ea;
3759 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003760
3761 CLEAR_FIELD(ea);
3762 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003763 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3764 define_function(&ea, NULL, &lines_to_free);
3765 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003766 }
3767 break;
3768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 // jump if a condition is met
3770 case ISN_JUMP:
3771 {
3772 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003773 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 int jump = TRUE;
3775
3776 if (when != JUMP_ALWAYS)
3777 {
3778 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003779 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003780 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003781 || when == JUMP_IF_COND_TRUE)
3782 {
3783 SOURCING_LNUM = iptr->isn_lnum;
3784 jump = tv_get_bool_chk(tv, &error);
3785 if (error)
3786 goto on_error;
3787 }
3788 else
3789 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003791 || when == JUMP_AND_KEEP_IF_FALSE
3792 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003794 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 {
3796 // drop the value from the stack
3797 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003798 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 }
3800 }
3801 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003802 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 }
3804 break;
3805
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003806 // Jump if an argument with a default value was already set and not
3807 // v:none.
3808 case ISN_JUMP_IF_ARG_SET:
3809 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3810 if (tv->v_type != VAR_UNKNOWN
3811 && !(tv->v_type == VAR_SPECIAL
3812 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003813 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003814 break;
3815
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 // top of a for loop
3817 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003818 if (execute_for(iptr, ectx) == FAIL)
3819 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 break;
3821
3822 // start of ":try" block
3823 case ISN_TRY:
3824 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003825 trycmd_T *trycmd = NULL;
3826
Bram Moolenaar35578162021-08-02 19:10:38 +02003827 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003828 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003829 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3830 + ectx->ec_trystack.ga_len;
3831 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003833 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003834 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3835 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003836 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003837 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003838 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003839 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003840 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003841 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 }
3843 break;
3844
3845 case ISN_PUSHEXC:
3846 if (current_exception == NULL)
3847 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003848 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003850 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003852 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003853 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003855 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003857 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 tv->vval.v_string = vim_strsave(
3859 (char_u *)current_exception->value);
3860 break;
3861
3862 case ISN_CATCH:
3863 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003864 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865
Bram Moolenaar4c137212021-04-19 16:48:48 +02003866 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 if (trystack->ga_len > 0)
3868 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003869 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 + trystack->ga_len - 1;
3871 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003872 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 }
3874 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003875 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 catch_exception(current_exception);
3877 }
3878 break;
3879
Bram Moolenaarc150c092021-02-13 15:02:46 +01003880 case ISN_TRYCONT:
3881 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003882 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003883 trycont_T *trycont = &iptr->isn_arg.trycont;
3884 int i;
3885 trycmd_T *trycmd;
3886 int iidx = trycont->tct_where;
3887
3888 if (trystack->ga_len < trycont->tct_levels)
3889 {
3890 siemsg("TRYCONT: expected %d levels, found %d",
3891 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003892 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003893 }
3894 // Make :endtry jump to any outer try block and the last
3895 // :endtry inside the loop to the loop start.
3896 for (i = trycont->tct_levels; i > 0; --i)
3897 {
3898 trycmd = ((trycmd_T *)trystack->ga_data)
3899 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003900 // Add one to tcd_cont to be able to jump to
3901 // instruction with index zero.
3902 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003903 iidx = trycmd->tcd_finally_idx == 0
3904 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003905 }
3906 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003907 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003908 }
3909 break;
3910
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003911 case ISN_FINALLY:
3912 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003913 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003914 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3915 + trystack->ga_len - 1;
3916
3917 // Reset the index to avoid a return statement jumps here
3918 // again.
3919 trycmd->tcd_finally_idx = 0;
3920 break;
3921 }
3922
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 // end of ":try" block
3924 case ISN_ENDTRY:
3925 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003926 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927
3928 if (trystack->ga_len > 0)
3929 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003930 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 --trystack->ga_len;
3933 --trylevel;
3934 trycmd = ((trycmd_T *)trystack->ga_data)
3935 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003936 if (trycmd->tcd_did_throw)
3937 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003938 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 {
3940 // discard the exception
3941 if (caught_stack == current_exception)
3942 caught_stack = caught_stack->caught;
3943 discard_current_exception();
3944 }
3945
3946 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003947 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003948
Bram Moolenaar4c137212021-04-19 16:48:48 +02003949 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003950 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003951 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003952 clear_tv(STACK_TV_BOT(0));
3953 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003954 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003955 // handling :continue: jump to outer try block or
3956 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003957 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 }
3959 }
3960 break;
3961
3962 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003963 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003964 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003965
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003966 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3967 {
3968 // throwing an exception while using "silent!" causes
3969 // the function to abort but not display an error.
3970 tv = STACK_TV_BOT(-1);
3971 clear_tv(tv);
3972 tv->v_type = VAR_NUMBER;
3973 tv->vval.v_number = 0;
3974 goto done;
3975 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003976 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003977 tv = STACK_TV_BOT(0);
3978 if (tv->vval.v_string == NULL
3979 || *skipwhite(tv->vval.v_string) == NUL)
3980 {
3981 vim_free(tv->vval.v_string);
3982 SOURCING_LNUM = iptr->isn_lnum;
3983 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003984 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003985 }
3986
3987 // Inside a "catch" we need to first discard the caught
3988 // exception.
3989 if (trystack->ga_len > 0)
3990 {
3991 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3992 + trystack->ga_len - 1;
3993 if (trycmd->tcd_caught && current_exception != NULL)
3994 {
3995 // discard the exception
3996 if (caught_stack == current_exception)
3997 caught_stack = caught_stack->caught;
3998 discard_current_exception();
3999 trycmd->tcd_caught = FALSE;
4000 }
4001 }
4002
Bram Moolenaar90a57162022-02-12 14:23:17 +00004003 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004004 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4005 == FAIL)
4006 {
4007 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004008 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004009 }
4010 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 break;
4013
4014 // compare with special values
4015 case ISN_COMPAREBOOL:
4016 case ISN_COMPARESPECIAL:
4017 {
4018 typval_T *tv1 = STACK_TV_BOT(-2);
4019 typval_T *tv2 = STACK_TV_BOT(-1);
4020 varnumber_T arg1 = tv1->vval.v_number;
4021 varnumber_T arg2 = tv2->vval.v_number;
4022 int res;
4023
4024 switch (iptr->isn_arg.op.op_type)
4025 {
4026 case EXPR_EQUAL: res = arg1 == arg2; break;
4027 case EXPR_NEQUAL: res = arg1 != arg2; break;
4028 default: res = 0; break;
4029 }
4030
Bram Moolenaar4c137212021-04-19 16:48:48 +02004031 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 tv1->v_type = VAR_BOOL;
4033 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4034 }
4035 break;
4036
Bram Moolenaar7a222242022-03-01 19:23:24 +00004037 case ISN_COMPARENULL:
4038 {
4039 typval_T *tv1 = STACK_TV_BOT(-2);
4040 typval_T *tv2 = STACK_TV_BOT(-1);
4041 int res;
4042
4043 res = typval_compare_null(tv1, tv2);
4044 if (res == MAYBE)
4045 goto on_error;
4046 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4047 res = !res;
4048 clear_tv(tv1);
4049 clear_tv(tv2);
4050 --ectx->ec_stack.ga_len;
4051 tv1->v_type = VAR_BOOL;
4052 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4053 }
4054 break;
4055
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 // Operation with two number arguments
4057 case ISN_OPNR:
4058 case ISN_COMPARENR:
4059 {
4060 typval_T *tv1 = STACK_TV_BOT(-2);
4061 typval_T *tv2 = STACK_TV_BOT(-1);
4062 varnumber_T arg1 = tv1->vval.v_number;
4063 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004064 varnumber_T res = 0;
4065 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066
4067 switch (iptr->isn_arg.op.op_type)
4068 {
4069 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004070 case EXPR_DIV: if (arg2 == 0)
4071 div_zero = TRUE;
4072 else
4073 res = arg1 / arg2;
4074 break;
4075 case EXPR_REM: if (arg2 == 0)
4076 div_zero = TRUE;
4077 else
4078 res = arg1 % arg2;
4079 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 case EXPR_SUB: res = arg1 - arg2; break;
4081 case EXPR_ADD: res = arg1 + arg2; break;
4082
4083 case EXPR_EQUAL: res = arg1 == arg2; break;
4084 case EXPR_NEQUAL: res = arg1 != arg2; break;
4085 case EXPR_GREATER: res = arg1 > arg2; break;
4086 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4087 case EXPR_SMALLER: res = arg1 < arg2; break;
4088 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004089 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 }
4091
Bram Moolenaar4c137212021-04-19 16:48:48 +02004092 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 if (iptr->isn_type == ISN_COMPARENR)
4094 {
4095 tv1->v_type = VAR_BOOL;
4096 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4097 }
4098 else
4099 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004100 if (div_zero)
4101 {
4102 SOURCING_LNUM = iptr->isn_lnum;
4103 emsg(_(e_divide_by_zero));
4104 goto on_error;
4105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 }
4107 break;
4108
4109 // Computation with two float arguments
4110 case ISN_OPFLOAT:
4111 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004112#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 {
4114 typval_T *tv1 = STACK_TV_BOT(-2);
4115 typval_T *tv2 = STACK_TV_BOT(-1);
4116 float_T arg1 = tv1->vval.v_float;
4117 float_T arg2 = tv2->vval.v_float;
4118 float_T res = 0;
4119 int cmp = FALSE;
4120
4121 switch (iptr->isn_arg.op.op_type)
4122 {
4123 case EXPR_MULT: res = arg1 * arg2; break;
4124 case EXPR_DIV: res = arg1 / arg2; break;
4125 case EXPR_SUB: res = arg1 - arg2; break;
4126 case EXPR_ADD: res = arg1 + arg2; break;
4127
4128 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4129 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4130 case EXPR_GREATER: cmp = arg1 > arg2; break;
4131 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4132 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4133 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4134 default: cmp = 0; break;
4135 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004136 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 if (iptr->isn_type == ISN_COMPAREFLOAT)
4138 {
4139 tv1->v_type = VAR_BOOL;
4140 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4141 }
4142 else
4143 tv1->vval.v_float = res;
4144 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004145#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 break;
4147
4148 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004149 case ISN_COMPAREDICT:
4150 case ISN_COMPAREFUNC:
4151 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 case ISN_COMPAREBLOB:
4153 {
4154 typval_T *tv1 = STACK_TV_BOT(-2);
4155 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004156 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4157 int ic = iptr->isn_arg.op.op_ic;
4158 int res = FALSE;
4159 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160
Bram Moolenaar265f8112021-12-19 12:33:05 +00004161 SOURCING_LNUM = iptr->isn_lnum;
4162 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004164 status = typval_compare_list(tv1, tv2,
4165 exprtype, ic, &res);
4166 }
4167 else if (iptr->isn_type == ISN_COMPAREDICT)
4168 {
4169 status = typval_compare_dict(tv1, tv2,
4170 exprtype, ic, &res);
4171 }
4172 else if (iptr->isn_type == ISN_COMPAREFUNC)
4173 {
4174 status = typval_compare_func(tv1, tv2,
4175 exprtype, ic, &res);
4176 }
4177 else if (iptr->isn_type == ISN_COMPARESTRING)
4178 {
4179 status = typval_compare_string(tv1, tv2,
4180 exprtype, ic, &res);
4181 }
4182 else
4183 {
4184 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004186 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 clear_tv(tv1);
4188 clear_tv(tv2);
4189 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004190 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4191 if (status == FAIL)
4192 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 }
4194 break;
4195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 case ISN_COMPAREANY:
4197 {
4198 typval_T *tv1 = STACK_TV_BOT(-2);
4199 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004200 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004202 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203
Bram Moolenaareb26f432020-09-14 16:50:05 +02004204 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004205 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004207 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004208 if (status == FAIL)
4209 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 }
4211 break;
4212
4213 case ISN_ADDLIST:
4214 case ISN_ADDBLOB:
4215 {
4216 typval_T *tv1 = STACK_TV_BOT(-2);
4217 typval_T *tv2 = STACK_TV_BOT(-1);
4218
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004219 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004221 {
4222 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4223 && tv1->vval.v_list != NULL)
4224 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4225 NULL);
4226 else
4227 eval_addlist(tv1, tv2);
4228 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 else
4230 eval_addblob(tv1, tv2);
4231 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004232 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 }
4234 break;
4235
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004236 case ISN_LISTAPPEND:
4237 {
4238 typval_T *tv1 = STACK_TV_BOT(-2);
4239 typval_T *tv2 = STACK_TV_BOT(-1);
4240 list_T *l = tv1->vval.v_list;
4241
4242 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004243 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004244 if (l == NULL)
4245 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004246 emsg(_(e_cannot_add_to_null_list));
4247 goto on_error;
4248 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004249 if (value_check_lock(l->lv_lock, NULL, FALSE))
4250 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004251 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004252 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004253 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004254 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004255 }
4256 break;
4257
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004258 case ISN_BLOBAPPEND:
4259 {
4260 typval_T *tv1 = STACK_TV_BOT(-2);
4261 typval_T *tv2 = STACK_TV_BOT(-1);
4262 blob_T *b = tv1->vval.v_blob;
4263 int error = FALSE;
4264 varnumber_T n;
4265
4266 // add a number to a blob
4267 if (b == NULL)
4268 {
4269 SOURCING_LNUM = iptr->isn_lnum;
4270 emsg(_(e_cannot_add_to_null_blob));
4271 goto on_error;
4272 }
4273 n = tv_get_number_chk(tv2, &error);
4274 if (error)
4275 goto on_error;
4276 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004277 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004278 }
4279 break;
4280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 // Computation with two arguments of unknown type
4282 case ISN_OPANY:
4283 {
4284 typval_T *tv1 = STACK_TV_BOT(-2);
4285 typval_T *tv2 = STACK_TV_BOT(-1);
4286 varnumber_T n1, n2;
4287#ifdef FEAT_FLOAT
4288 float_T f1 = 0, f2 = 0;
4289#endif
4290 int error = FALSE;
4291
4292 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4293 {
4294 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4295 {
4296 eval_addlist(tv1, tv2);
4297 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004298 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 break;
4300 }
4301 else if (tv1->v_type == VAR_BLOB
4302 && tv2->v_type == VAR_BLOB)
4303 {
4304 eval_addblob(tv1, tv2);
4305 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004306 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 break;
4308 }
4309 }
4310#ifdef FEAT_FLOAT
4311 if (tv1->v_type == VAR_FLOAT)
4312 {
4313 f1 = tv1->vval.v_float;
4314 n1 = 0;
4315 }
4316 else
4317#endif
4318 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004319 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 n1 = tv_get_number_chk(tv1, &error);
4321 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004322 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323#ifdef FEAT_FLOAT
4324 if (tv2->v_type == VAR_FLOAT)
4325 f1 = n1;
4326#endif
4327 }
4328#ifdef FEAT_FLOAT
4329 if (tv2->v_type == VAR_FLOAT)
4330 {
4331 f2 = tv2->vval.v_float;
4332 n2 = 0;
4333 }
4334 else
4335#endif
4336 {
4337 n2 = tv_get_number_chk(tv2, &error);
4338 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004339 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340#ifdef FEAT_FLOAT
4341 if (tv1->v_type == VAR_FLOAT)
4342 f2 = n2;
4343#endif
4344 }
4345#ifdef FEAT_FLOAT
4346 // if there is a float on either side the result is a float
4347 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4348 {
4349 switch (iptr->isn_arg.op.op_type)
4350 {
4351 case EXPR_MULT: f1 = f1 * f2; break;
4352 case EXPR_DIV: f1 = f1 / f2; break;
4353 case EXPR_SUB: f1 = f1 - f2; break;
4354 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004355 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004356 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004357 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 }
4359 clear_tv(tv1);
4360 clear_tv(tv2);
4361 tv1->v_type = VAR_FLOAT;
4362 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004363 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 }
4365 else
4366#endif
4367 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004368 int failed = FALSE;
4369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 switch (iptr->isn_arg.op.op_type)
4371 {
4372 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004373 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4374 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004375 goto on_error;
4376 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 case EXPR_SUB: n1 = n1 - n2; break;
4378 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004379 default: n1 = num_modulus(n1, n2, &failed);
4380 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004381 goto on_error;
4382 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 }
4384 clear_tv(tv1);
4385 clear_tv(tv2);
4386 tv1->v_type = VAR_NUMBER;
4387 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004388 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 }
4390 }
4391 break;
4392
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004393 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004394 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004395 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004396 int is_slice = iptr->isn_type == ISN_STRSLICE;
4397 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004398 char_u *res;
4399
4400 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004401 // string slice: string is at stack-3, first index at
4402 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004403 if (is_slice)
4404 {
4405 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004406 n1 = tv->vval.v_number;
4407 }
4408
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004409 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004410 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004411
Bram Moolenaar4c137212021-04-19 16:48:48 +02004412 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004413 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004414 if (is_slice)
4415 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004416 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004417 else
4418 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004419 // single character (including composing characters).
4420 // If the index is too big or negative the result is
4421 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004422 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004423 vim_free(tv->vval.v_string);
4424 tv->vval.v_string = res;
4425 }
4426 break;
4427
4428 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004429 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004430 case ISN_BLOBINDEX:
4431 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004433 int is_slice = iptr->isn_type == ISN_LISTSLICE
4434 || iptr->isn_type == ISN_BLOBSLICE;
4435 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4436 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004437 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004438 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439
4440 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004441 // list slice: list is at stack-3, indexes at stack-2 and
4442 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004443 // Same for blob.
4444 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445
4446 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004447 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004449
4450 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 {
Bram Moolenaared591872020-08-15 22:14:53 +02004452 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004453 n1 = tv->vval.v_number;
4454 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 }
Bram Moolenaared591872020-08-15 22:14:53 +02004456
Bram Moolenaar4c137212021-04-19 16:48:48 +02004457 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004458 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004459 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004460 if (is_blob)
4461 {
4462 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4463 n1, n2, FALSE, tv) == FAIL)
4464 goto on_error;
4465 }
4466 else
4467 {
4468 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4469 n1, n2, FALSE, tv, TRUE) == FAIL)
4470 goto on_error;
4471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 }
4473 break;
4474
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004475 case ISN_ANYINDEX:
4476 case ISN_ANYSLICE:
4477 {
4478 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4479 typval_T *var1, *var2;
4480 int res;
4481
4482 // index: composite is at stack-2, index at stack-1
4483 // slice: composite is at stack-3, indexes at stack-2 and
4484 // stack-1
4485 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004486 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004487 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4488 goto on_error;
4489 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4490 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004491 res = eval_index_inner(tv, is_slice, var1, var2,
4492 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004493 clear_tv(var1);
4494 if (is_slice)
4495 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004496 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004497 if (res == FAIL)
4498 goto on_error;
4499 }
4500 break;
4501
Bram Moolenaar9af78762020-06-16 11:34:42 +02004502 case ISN_SLICE:
4503 {
4504 list_T *list;
4505 int count = iptr->isn_arg.number;
4506
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004507 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004508 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004509 list = tv->vval.v_list;
4510
4511 // no error for short list, expect it to be checked earlier
4512 if (list != NULL && list->lv_len >= count)
4513 {
4514 list_T *newlist = list_slice(list,
4515 count, list->lv_len - 1);
4516
4517 if (newlist != NULL)
4518 {
4519 list_unref(list);
4520 tv->vval.v_list = newlist;
4521 ++newlist->lv_refcount;
4522 }
4523 }
4524 }
4525 break;
4526
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004527 case ISN_GETITEM:
4528 {
4529 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004530 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004531
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004532 // Get list item: list is at stack-1, push item.
4533 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004534 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4535 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004536
Bram Moolenaar35578162021-08-02 19:10:38 +02004537 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004538 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004539 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004540 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004541
4542 // Useful when used in unpack assignment. Reset at
4543 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004544 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004545 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004546 }
4547 break;
4548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 case ISN_MEMBER:
4550 {
4551 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004552 char_u *key;
4553 dictitem_T *di;
4554
4555 // dict member: dict is at stack-2, key at stack-1
4556 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004557 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004558 dict = tv->vval.v_dict;
4559
4560 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004561 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004562 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004563 if (key == NULL)
4564 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004565
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004566 if ((di = dict_find(dict, key, -1)) == NULL)
4567 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004568 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004569 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004570
4571 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004572 // stack contents makes sense and the dict stack is
4573 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004574 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004575 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004576 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004577 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004578 tv->v_type = VAR_NUMBER;
4579 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004580 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004581 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004582 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004583 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004584 // Put the dict used on the dict stack, it might be used by
4585 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004586 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004587 if (dict_stack_save(tv) == FAIL)
4588 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004589 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004590 }
4591 break;
4592
4593 // dict member with string key
4594 case ISN_STRINGMEMBER:
4595 {
4596 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 dictitem_T *di;
4598
4599 tv = STACK_TV_BOT(-1);
4600 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4601 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004602 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004603 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004604 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 }
4606 dict = tv->vval.v_dict;
4607
4608 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4609 == NULL)
4610 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004611 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004612 semsg(_(e_key_not_present_in_dictionary),
4613 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004614 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004616 // Put the dict used on the dict stack, it might be used by
4617 // a dict function later.
4618 if (dict_stack_save(tv) == FAIL)
4619 goto on_fatal_error;
4620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004622 }
4623 break;
4624
4625 case ISN_CLEARDICT:
4626 dict_stack_drop();
4627 break;
4628
4629 case ISN_USEDICT:
4630 {
4631 typval_T *dict_tv = dict_stack_get_tv();
4632
4633 // Turn "dict.Func" into a partial for "Func" bound to
4634 // "dict". Don't do this when "Func" is already a partial
4635 // that was bound explicitly (pt_auto is FALSE).
4636 tv = STACK_TV_BOT(-1);
4637 if (dict_tv != NULL
4638 && dict_tv->v_type == VAR_DICT
4639 && dict_tv->vval.v_dict != NULL
4640 && (tv->v_type == VAR_FUNC
4641 || (tv->v_type == VAR_PARTIAL
4642 && (tv->vval.v_partial->pt_auto
4643 || tv->vval.v_partial->pt_dict == NULL))))
4644 dict_tv->vval.v_dict =
4645 make_partial(dict_tv->vval.v_dict, tv);
4646 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 }
4648 break;
4649
4650 case ISN_NEGATENR:
4651 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004652 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004653#ifdef FEAT_FLOAT
4654 if (tv->v_type == VAR_FLOAT)
4655 tv->vval.v_float = -tv->vval.v_float;
4656 else
4657#endif
4658 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 break;
4660
4661 case ISN_CHECKNR:
4662 {
4663 int error = FALSE;
4664
4665 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004666 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004668 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 (void)tv_get_number_chk(tv, &error);
4670 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004671 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 }
4673 break;
4674
4675 case ISN_CHECKTYPE:
4676 {
4677 checktype_T *ct = &iptr->isn_arg.type;
4678
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004679 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004680 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004681 if (!ectx->ec_where.wt_variable)
4682 ectx->ec_where.wt_index = ct->ct_arg_idx;
4683 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4684 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004685 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004686 if (!ectx->ec_where.wt_variable)
4687 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004688
4689 // number 0 is FALSE, number 1 is TRUE
4690 if (tv->v_type == VAR_NUMBER
4691 && ct->ct_type->tt_type == VAR_BOOL
4692 && (tv->vval.v_number == 0
4693 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004695 tv->v_type = VAR_BOOL;
4696 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004697 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 }
4699 }
4700 break;
4701
Bram Moolenaar9af78762020-06-16 11:34:42 +02004702 case ISN_CHECKLEN:
4703 {
4704 int min_len = iptr->isn_arg.checklen.cl_min_len;
4705 list_T *list = NULL;
4706
4707 tv = STACK_TV_BOT(-1);
4708 if (tv->v_type == VAR_LIST)
4709 list = tv->vval.v_list;
4710 if (list == NULL || list->lv_len < min_len
4711 || (list->lv_len > min_len
4712 && !iptr->isn_arg.checklen.cl_more_OK))
4713 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004714 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004715 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004716 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004717 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004718 }
4719 }
4720 break;
4721
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004722 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004723 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004724 break;
4725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004727 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728 {
4729 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004730 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004732 if (iptr->isn_type == ISN_2BOOL)
4733 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004734 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004735 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004736 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004737 n = !n;
4738 }
4739 else
4740 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004741 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004742 SOURCING_LNUM = iptr->isn_lnum;
4743 n = tv_get_bool_chk(tv, &error);
4744 if (error)
4745 goto on_error;
4746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 clear_tv(tv);
4748 tv->v_type = VAR_BOOL;
4749 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4750 }
4751 break;
4752
4753 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004754 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004755 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004756 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4757 iptr->isn_type == ISN_2STRING_ANY,
4758 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004759 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 break;
4761
Bram Moolenaar08597872020-12-10 19:43:40 +01004762 case ISN_RANGE:
4763 {
4764 exarg_T ea;
4765 char *errormsg;
4766
Bram Moolenaarece0b872021-01-08 20:40:45 +01004767 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004768 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004769 ea.addr_type = ADDR_LINES;
4770 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004771 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004772 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004773 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004774
Bram Moolenaar35578162021-08-02 19:10:38 +02004775 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004776 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004777 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004778 tv = STACK_TV_BOT(-1);
4779 tv->v_type = VAR_NUMBER;
4780 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004781 if (ea.addr_count == 0)
4782 tv->vval.v_number = curwin->w_cursor.lnum;
4783 else
4784 tv->vval.v_number = ea.line2;
4785 }
4786 break;
4787
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004788 case ISN_PUT:
4789 {
4790 int regname = iptr->isn_arg.put.put_regname;
4791 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4792 char_u *expr = NULL;
4793 int dir = FORWARD;
4794
Bram Moolenaar08597872020-12-10 19:43:40 +01004795 if (lnum < -2)
4796 {
4797 // line number was put on the stack by ISN_RANGE
4798 tv = STACK_TV_BOT(-1);
4799 curwin->w_cursor.lnum = tv->vval.v_number;
4800 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4801 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004802 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004803 }
4804 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004805 // :put! above cursor
4806 dir = BACKWARD;
4807 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004808 {
4809 curwin->w_cursor.lnum = lnum;
4810 if (lnum == 0)
4811 // check_cursor() below will move to line 1
4812 dir = BACKWARD;
4813 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004814
4815 if (regname == '=')
4816 {
4817 tv = STACK_TV_BOT(-1);
4818 if (tv->v_type == VAR_STRING)
4819 expr = tv->vval.v_string;
4820 else
4821 {
4822 expr = typval2string(tv, TRUE); // allocates value
4823 clear_tv(tv);
4824 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004825 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004826 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004827 check_cursor();
4828 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4829 vim_free(expr);
4830 }
4831 break;
4832
Bram Moolenaar02194d22020-10-24 23:08:38 +02004833 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004834 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4835 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4836 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4837 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004838 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4839 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004840 break;
4841
Bram Moolenaar02194d22020-10-24 23:08:38 +02004842 case ISN_CMDMOD_REV:
4843 // filter regprog is owned by the instruction, don't free it
4844 cmdmod.cmod_filter_regmatch.regprog = NULL;
4845 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004846 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4847 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004848 break;
4849
Bram Moolenaar792f7862020-11-23 08:31:18 +01004850 case ISN_UNPACK:
4851 {
4852 int count = iptr->isn_arg.unpack.unp_count;
4853 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4854 list_T *l;
4855 listitem_T *li;
4856 int i;
4857
4858 // Check there is a valid list to unpack.
4859 tv = STACK_TV_BOT(-1);
4860 if (tv->v_type != VAR_LIST)
4861 {
4862 SOURCING_LNUM = iptr->isn_lnum;
4863 emsg(_(e_for_argument_must_be_sequence_of_lists));
4864 goto on_error;
4865 }
4866 l = tv->vval.v_list;
4867 if (l == NULL
4868 || l->lv_len < (semicolon ? count - 1 : count))
4869 {
4870 SOURCING_LNUM = iptr->isn_lnum;
4871 emsg(_(e_list_value_does_not_have_enough_items));
4872 goto on_error;
4873 }
4874 else if (!semicolon && l->lv_len > count)
4875 {
4876 SOURCING_LNUM = iptr->isn_lnum;
4877 emsg(_(e_list_value_has_more_items_than_targets));
4878 goto on_error;
4879 }
4880
4881 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004882 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004883 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004884 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004885
4886 // Variable after semicolon gets a list with the remaining
4887 // items.
4888 if (semicolon)
4889 {
4890 list_T *rem_list =
4891 list_alloc_with_items(l->lv_len - count + 1);
4892
4893 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004894 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004895 tv = STACK_TV_BOT(-count);
4896 tv->vval.v_list = rem_list;
4897 ++rem_list->lv_refcount;
4898 tv->v_lock = 0;
4899 li = l->lv_first;
4900 for (i = 0; i < count - 1; ++i)
4901 li = li->li_next;
4902 for (i = 0; li != NULL; ++i)
4903 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004904 typval_T tvcopy;
4905
4906 copy_tv(&li->li_tv, &tvcopy);
4907 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004908 li = li->li_next;
4909 }
4910 --count;
4911 }
4912
4913 // Produce the values in reverse order, first item last.
4914 li = l->lv_first;
4915 for (i = 0; i < count; ++i)
4916 {
4917 tv = STACK_TV_BOT(-i - 1);
4918 copy_tv(&li->li_tv, tv);
4919 li = li->li_next;
4920 }
4921
4922 list_unref(l);
4923 }
4924 break;
4925
Bram Moolenaarb2049902021-01-24 12:53:53 +01004926 case ISN_PROF_START:
4927 case ISN_PROF_END:
4928 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004929#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004930 funccall_T cookie;
4931 ufunc_T *cur_ufunc =
4932 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004933 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004934
4935 cookie.func = cur_ufunc;
4936 if (iptr->isn_type == ISN_PROF_START)
4937 {
4938 func_line_start(&cookie, iptr->isn_lnum);
4939 // if we get here the instruction is executed
4940 func_line_exec(&cookie);
4941 }
4942 else
4943 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004944#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004945 }
4946 break;
4947
Bram Moolenaare99d4222021-06-13 14:01:26 +02004948 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004949 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004950 break;
4951
Bram Moolenaar389df252020-07-09 21:20:47 +02004952 case ISN_SHUFFLE:
4953 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004954 typval_T tmp_tv;
4955 int item = iptr->isn_arg.shuffle.shfl_item;
4956 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004957
4958 tmp_tv = *STACK_TV_BOT(-item);
4959 for ( ; up > 0 && item > 1; --up)
4960 {
4961 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4962 --item;
4963 }
4964 *STACK_TV_BOT(-item) = tmp_tv;
4965 }
4966 break;
4967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004969 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004971 ectx->ec_where.wt_index = 0;
4972 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 break;
4974 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004975 continue;
4976
Bram Moolenaard032f342020-07-18 18:13:02 +02004977func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004978 // Restore previous function. If the frame pointer is where we started
4979 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004980 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004981 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004982
Bram Moolenaar4c137212021-04-19 16:48:48 +02004983 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004984 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004985 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004986 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004987
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004988on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004989 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004990 // If "emsg_silent" is set then ignore the error, unless it was set
4991 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004992 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004993 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004994 {
4995 // If a sequence of instructions causes an error while ":silent!"
4996 // was used, restore the stack length and jump ahead to restoring
4997 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004998 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004999 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005000 while (ectx->ec_stack.ga_len
5001 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005002 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005003 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005004 clear_tv(STACK_TV_BOT(0));
5005 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005006 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5007 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005008 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005009 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005010 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005011on_fatal_error:
5012 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005013 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005014 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005015 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 }
5017
5018done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005019 ret = OK;
5020theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005021 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005022 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5023 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005024}
5025
5026/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005027 * Execute the instructions from a VAR_INSTR typeval and put the result in
5028 * "rettv".
5029 * Return OK or FAIL.
5030 */
5031 int
5032exe_typval_instr(typval_T *tv, typval_T *rettv)
5033{
5034 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5035 isn_T *save_instr = ectx->ec_instr;
5036 int save_iidx = ectx->ec_iidx;
5037 int res;
5038
5039 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 {
5170 if (idx == 1)
5171 emsg(_(e_one_argument_too_many));
5172 else
5173 semsg(_(e_nr_arguments_too_many), idx);
5174 goto failed_early;
5175 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005176 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5177 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005178 {
5179 if (idx == -1)
5180 emsg(_(e_one_argument_too_few));
5181 else
5182 semsg(_(e_nr_arguments_too_few), -idx);
5183 goto failed_early;
5184 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005185
5186 // Put arguments on the stack, but no more than what the function expects.
5187 // A lambda can be called with more arguments than it uses.
5188 for (idx = 0; idx < argc
5189 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5190 ++idx)
5191 {
5192 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5193 && argv[idx].v_type == VAR_SPECIAL
5194 && argv[idx].vval.v_number == VVAL_NONE)
5195 {
5196 // Use the default value.
5197 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5198 }
5199 else
5200 {
5201 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5202 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005203 ufunc->uf_arg_types[idx], &argv[idx],
5204 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005205 goto failed_early;
5206 copy_tv(&argv[idx], STACK_TV_BOT(0));
5207 }
5208 ++ectx.ec_stack.ga_len;
5209 }
5210
5211 // Turn varargs into a list. Empty list if no args.
5212 if (ufunc->uf_va_name != NULL)
5213 {
5214 int vararg_count = argc - ufunc->uf_args.ga_len;
5215
5216 if (vararg_count < 0)
5217 vararg_count = 0;
5218 else
5219 argc -= vararg_count;
5220 if (exe_newlist(vararg_count, &ectx) == FAIL)
5221 goto failed_early;
5222
5223 // Check the type of the list items.
5224 tv = STACK_TV_BOT(-1);
5225 if (ufunc->uf_va_type != NULL
5226 && ufunc->uf_va_type != &t_list_any
5227 && ufunc->uf_va_type->tt_member != &t_any
5228 && tv->vval.v_list != NULL)
5229 {
5230 type_T *expected = ufunc->uf_va_type->tt_member;
5231 listitem_T *li = tv->vval.v_list->lv_first;
5232
5233 for (idx = 0; idx < vararg_count; ++idx)
5234 {
5235 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005236 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005237 goto failed_early;
5238 li = li->li_next;
5239 }
5240 }
5241
5242 if (defcount > 0)
5243 // Move varargs list to below missing default arguments.
5244 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5245 --ectx.ec_stack.ga_len;
5246 }
5247
5248 // Make space for omitted arguments, will store default value below.
5249 // Any varargs list goes after them.
5250 if (defcount > 0)
5251 for (idx = 0; idx < defcount; ++idx)
5252 {
5253 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5254 ++ectx.ec_stack.ga_len;
5255 }
5256 if (ufunc->uf_va_name != NULL)
5257 ++ectx.ec_stack.ga_len;
5258
5259 // Frame pointer points to just after arguments.
5260 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5261 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5262
5263 {
5264 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5265 + ufunc->uf_dfunc_idx;
5266 ufunc_T *base_ufunc = dfunc->df_ufunc;
5267
5268 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5269 // by copy_func().
5270 if (partial != NULL || base_ufunc->uf_partial != NULL)
5271 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005272 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5273 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005274 goto failed_early;
5275 if (partial != NULL)
5276 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005277 outer_T *outer = get_pt_outer(partial);
5278
5279 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005280 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005281 if (current_ectx != NULL)
5282 {
5283 if (current_ectx->ec_outer_ref != NULL
5284 && current_ectx->ec_outer_ref->or_outer != NULL)
5285 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005286 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005287 }
5288 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005289 }
5290 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005291 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005292 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005293 ++partial->pt_refcount;
5294 ectx.ec_outer_ref->or_partial = partial;
5295 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005296 }
5297 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005298 {
5299 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5300 ++base_ufunc->uf_partial->pt_refcount;
5301 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5302 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005303 }
5304 }
5305
5306 // dummy frame entries
5307 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5308 {
5309 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5310 ++ectx.ec_stack.ga_len;
5311 }
5312
5313 {
5314 // Reserve space for local variables and any closure reference count.
5315 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5316 + ufunc->uf_dfunc_idx;
5317
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005318 // Initialize variables to zero. That avoids having to generate
5319 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005320 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005321 {
5322 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5323 STACK_TV_VAR(idx)->vval.v_number = 0;
5324 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005325 ectx.ec_stack.ga_len += dfunc->df_varcount;
5326 if (dfunc->df_has_closure)
5327 {
5328 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5329 STACK_TV_VAR(idx)->vval.v_number = 0;
5330 ++ectx.ec_stack.ga_len;
5331 }
5332
5333 ectx.ec_instr = INSTRUCTIONS(dfunc);
5334 }
5335
5336 // Following errors are in the function, not the caller.
5337 // Commands behave like vim9script.
5338 estack_push_ufunc(ufunc, 1);
5339 current_sctx = ufunc->uf_script_ctx;
5340 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5341
5342 // Use a specific location for storing error messages to be converted to an
5343 // exception.
5344 saved_msg_list = msg_list;
5345 msg_list = &private_msg_list;
5346
5347 // Do turn errors into exceptions.
5348 suppress_errthrow = FALSE;
5349
5350 // Do not delete the function while executing it.
5351 ++ufunc->uf_calls;
5352
5353 // When ":silent!" was used before calling then we still abort the
5354 // function. If ":silent!" is used in the function then we don't.
5355 emsg_silent_def = emsg_silent;
5356 did_emsg_def = 0;
5357
5358 ectx.ec_where.wt_index = 0;
5359 ectx.ec_where.wt_variable = FALSE;
5360
5361 // Execute the instructions until done.
5362 ret = exec_instructions(&ectx);
5363 if (ret == OK)
5364 {
5365 // function finished, get result from the stack.
5366 if (ufunc->uf_ret_type == &t_void)
5367 {
5368 rettv->v_type = VAR_VOID;
5369 }
5370 else
5371 {
5372 tv = STACK_TV_BOT(-1);
5373 *rettv = *tv;
5374 tv->v_type = VAR_UNKNOWN;
5375 }
5376 }
5377
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005378 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005379 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5380 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005381
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005382 // Deal with any remaining closures, they may be in use somewhere.
5383 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005384 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005385 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005386 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005387 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005388
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005389 estack_pop();
5390 current_sctx = save_current_sctx;
5391
Bram Moolenaar2336c372021-12-06 15:06:54 +00005392 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5393 // Function was unreferenced while being used, free it now.
5394 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005395
Bram Moolenaar352134b2020-10-17 22:04:08 +02005396 if (*msg_list != NULL && saved_msg_list != NULL)
5397 {
5398 msglist_T **plist = saved_msg_list;
5399
5400 // Append entries from the current msg_list (uncaught exceptions) to
5401 // the saved msg_list.
5402 while (*plist != NULL)
5403 plist = &(*plist)->next;
5404
5405 *plist = *msg_list;
5406 }
5407 msg_list = saved_msg_list;
5408
Bram Moolenaar4c137212021-04-19 16:48:48 +02005409 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005410 {
5411 cmdmod.cmod_filter_regmatch.regprog = NULL;
5412 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005413 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005414 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005415 emsg_silent_def = save_emsg_silent_def;
5416 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005417
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005418failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005419 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005420 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5421 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005422 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005425 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005426 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005427 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005428 if (ectx.ec_outer_ref->or_outer_allocated)
5429 vim_free(ectx.ec_outer_ref->or_outer);
5430 partial_unref(ectx.ec_outer_ref->or_partial);
5431 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005432 }
5433
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005434 // Not sure if this is necessary.
5435 suppress_errthrow = save_suppress_errthrow;
5436
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005437 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5438 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005439 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005440 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005441 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 return ret;
5443}
5444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005445/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005446 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5447 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5448 * "pfx" is prefixed to every line.
5449 */
5450 static void
5451list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5452{
5453 int line_idx = 0;
5454 int prev_current = 0;
5455 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005456 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005457
5458 for (current = 0; current < instr_count; ++current)
5459 {
5460 isn_T *iptr = &instr[current];
5461 char *line;
5462
5463 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005464 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005465 while (line_idx < iptr->isn_lnum
5466 && line_idx < ufunc->uf_lines.ga_len)
5467 {
5468 if (current > prev_current)
5469 {
5470 msg_puts("\n\n");
5471 prev_current = current;
5472 }
5473 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5474 if (line != NULL)
5475 msg(line);
5476 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005477 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5478 {
5479 int first_def_arg = ufunc->uf_args.ga_len
5480 - ufunc->uf_def_args.ga_len;
5481
5482 if (def_arg_idx > 0)
5483 msg_puts("\n\n");
5484 msg_start();
5485 msg_puts(" ");
5486 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5487 first_def_arg + def_arg_idx]);
5488 msg_puts(" = ");
5489 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5490 msg_clr_eos();
5491 msg_end();
5492 }
5493 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005494
5495 switch (iptr->isn_type)
5496 {
5497 case ISN_EXEC:
5498 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5499 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005500 case ISN_EXEC_SPLIT:
5501 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5502 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005503 case ISN_EXECRANGE:
5504 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5505 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005506 case ISN_LEGACY_EVAL:
5507 smsg("%s%4d EVAL legacy %s", pfx, current,
5508 iptr->isn_arg.string);
5509 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005510 case ISN_REDIRSTART:
5511 smsg("%s%4d REDIR", pfx, current);
5512 break;
5513 case ISN_REDIREND:
5514 smsg("%s%4d REDIR END%s", pfx, current,
5515 iptr->isn_arg.number ? " append" : "");
5516 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005517 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005518#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005519 smsg("%s%4d CEXPR pre %s", pfx, current,
5520 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005521#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005522 break;
5523 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005524#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005525 {
5526 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5527
5528 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5529 cexpr_get_auname(cer->cer_cmdidx),
5530 cer->cer_forceit ? "!" : "",
5531 cer->cer_cmdline);
5532 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005533#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005534 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005535 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005536 smsg("%s%4d INSTR", pfx, current);
5537 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5538 msg(" -------------");
5539 break;
5540 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005541 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005542 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5543
5544 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005545 }
5546 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005547 case ISN_SUBSTITUTE:
5548 {
5549 subs_T *subs = &iptr->isn_arg.subs;
5550
5551 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5552 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5553 msg(" -------------");
5554 }
5555 break;
5556 case ISN_EXECCONCAT:
5557 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5558 (varnumber_T)iptr->isn_arg.number);
5559 break;
5560 case ISN_ECHO:
5561 {
5562 echo_T *echo = &iptr->isn_arg.echo;
5563
5564 smsg("%s%4d %s %d", pfx, current,
5565 echo->echo_with_white ? "ECHO" : "ECHON",
5566 echo->echo_count);
5567 }
5568 break;
5569 case ISN_EXECUTE:
5570 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005571 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005572 break;
5573 case ISN_ECHOMSG:
5574 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005575 (varnumber_T)(iptr->isn_arg.number));
5576 break;
5577 case ISN_ECHOCONSOLE:
5578 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5579 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005580 break;
5581 case ISN_ECHOERR:
5582 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005583 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005584 break;
5585 case ISN_LOAD:
5586 {
5587 if (iptr->isn_arg.number < 0)
5588 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5589 (varnumber_T)(iptr->isn_arg.number
5590 + STACK_FRAME_SIZE));
5591 else
5592 smsg("%s%4d LOAD $%lld", pfx, current,
5593 (varnumber_T)(iptr->isn_arg.number));
5594 }
5595 break;
5596 case ISN_LOADOUTER:
5597 {
5598 if (iptr->isn_arg.number < 0)
5599 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5600 iptr->isn_arg.outer.outer_depth,
5601 iptr->isn_arg.outer.outer_idx
5602 + STACK_FRAME_SIZE);
5603 else
5604 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5605 iptr->isn_arg.outer.outer_depth,
5606 iptr->isn_arg.outer.outer_idx);
5607 }
5608 break;
5609 case ISN_LOADV:
5610 smsg("%s%4d LOADV v:%s", pfx, current,
5611 get_vim_var_name(iptr->isn_arg.number));
5612 break;
5613 case ISN_LOADSCRIPT:
5614 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005615 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5616 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5617 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005618
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005619 sv = get_script_svar(sref, -1);
5620 if (sv == NULL)
5621 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5622 pfx, current, si->sn_name);
5623 else
5624 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005625 sv->sv_name,
5626 sref->sref_idx,
5627 si->sn_name);
5628 }
5629 break;
5630 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005631 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005632 {
5633 scriptitem_T *si = SCRIPT_ITEM(
5634 iptr->isn_arg.loadstore.ls_sid);
5635
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005636 smsg("%s%4d %s s:%s from %s", pfx, current,
5637 iptr->isn_type == ISN_LOADS ? "LOADS"
5638 : "LOADEXPORT",
5639 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005640 }
5641 break;
5642 case ISN_LOADAUTO:
5643 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5644 break;
5645 case ISN_LOADG:
5646 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5647 break;
5648 case ISN_LOADB:
5649 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5650 break;
5651 case ISN_LOADW:
5652 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5653 break;
5654 case ISN_LOADT:
5655 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5656 break;
5657 case ISN_LOADGDICT:
5658 smsg("%s%4d LOAD g:", pfx, current);
5659 break;
5660 case ISN_LOADBDICT:
5661 smsg("%s%4d LOAD b:", pfx, current);
5662 break;
5663 case ISN_LOADWDICT:
5664 smsg("%s%4d LOAD w:", pfx, current);
5665 break;
5666 case ISN_LOADTDICT:
5667 smsg("%s%4d LOAD t:", pfx, current);
5668 break;
5669 case ISN_LOADOPT:
5670 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5671 break;
5672 case ISN_LOADENV:
5673 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5674 break;
5675 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005676 smsg("%s%4d LOADREG @%c", pfx, current,
5677 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005678 break;
5679
5680 case ISN_STORE:
5681 if (iptr->isn_arg.number < 0)
5682 smsg("%s%4d STORE arg[%lld]", pfx, current,
5683 iptr->isn_arg.number + STACK_FRAME_SIZE);
5684 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005685 smsg("%s%4d STORE $%lld", pfx, current,
5686 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005687 break;
5688 case ISN_STOREOUTER:
5689 {
5690 if (iptr->isn_arg.number < 0)
5691 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5692 iptr->isn_arg.outer.outer_depth,
5693 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5694 else
5695 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5696 iptr->isn_arg.outer.outer_depth,
5697 iptr->isn_arg.outer.outer_idx);
5698 }
5699 break;
5700 case ISN_STOREV:
5701 smsg("%s%4d STOREV v:%s", pfx, current,
5702 get_vim_var_name(iptr->isn_arg.number));
5703 break;
5704 case ISN_STOREAUTO:
5705 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5706 break;
5707 case ISN_STOREG:
5708 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5709 break;
5710 case ISN_STOREB:
5711 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5712 break;
5713 case ISN_STOREW:
5714 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5715 break;
5716 case ISN_STORET:
5717 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5718 break;
5719 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005720 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005721 {
5722 scriptitem_T *si = SCRIPT_ITEM(
5723 iptr->isn_arg.loadstore.ls_sid);
5724
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005725 smsg("%s%4d %s %s in %s", pfx, current,
5726 iptr->isn_type == ISN_STORES
5727 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005728 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5729 }
5730 break;
5731 case ISN_STORESCRIPT:
5732 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005733 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5734 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5735 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005736
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005737 sv = get_script_svar(sref, -1);
5738 if (sv == NULL)
5739 smsg("%s%4d STORESCRIPT [deleted] in %s",
5740 pfx, current, si->sn_name);
5741 else
5742 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005743 sv->sv_name,
5744 sref->sref_idx,
5745 si->sn_name);
5746 }
5747 break;
5748 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005749 case ISN_STOREFUNCOPT:
5750 smsg("%s%4d %s &%s", pfx, current,
5751 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005752 iptr->isn_arg.storeopt.so_name);
5753 break;
5754 case ISN_STOREENV:
5755 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5756 break;
5757 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005758 smsg("%s%4d STOREREG @%c", pfx, current,
5759 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005760 break;
5761 case ISN_STORENR:
5762 smsg("%s%4d STORE %lld in $%d", pfx, current,
5763 iptr->isn_arg.storenr.stnr_val,
5764 iptr->isn_arg.storenr.stnr_idx);
5765 break;
5766
5767 case ISN_STOREINDEX:
5768 smsg("%s%4d STOREINDEX %s", pfx, current,
5769 vartype_name(iptr->isn_arg.vartype));
5770 break;
5771
5772 case ISN_STORERANGE:
5773 smsg("%s%4d STORERANGE", pfx, current);
5774 break;
5775
5776 // constants
5777 case ISN_PUSHNR:
5778 smsg("%s%4d PUSHNR %lld", pfx, current,
5779 (varnumber_T)(iptr->isn_arg.number));
5780 break;
5781 case ISN_PUSHBOOL:
5782 case ISN_PUSHSPEC:
5783 smsg("%s%4d PUSH %s", pfx, current,
5784 get_var_special_name(iptr->isn_arg.number));
5785 break;
5786 case ISN_PUSHF:
5787#ifdef FEAT_FLOAT
5788 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5789#endif
5790 break;
5791 case ISN_PUSHS:
5792 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5793 break;
5794 case ISN_PUSHBLOB:
5795 {
5796 char_u *r;
5797 char_u numbuf[NUMBUFLEN];
5798 char_u *tofree;
5799
5800 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5801 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5802 vim_free(tofree);
5803 }
5804 break;
5805 case ISN_PUSHFUNC:
5806 {
5807 char *name = (char *)iptr->isn_arg.string;
5808
5809 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5810 name == NULL ? "[none]" : name);
5811 }
5812 break;
5813 case ISN_PUSHCHANNEL:
5814#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005815 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005816#endif
5817 break;
5818 case ISN_PUSHJOB:
5819#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005820 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005821#endif
5822 break;
5823 case ISN_PUSHEXC:
5824 smsg("%s%4d PUSH v:exception", pfx, current);
5825 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005826 case ISN_AUTOLOAD:
5827 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5828 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005829 case ISN_UNLET:
5830 smsg("%s%4d UNLET%s %s", pfx, current,
5831 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5832 iptr->isn_arg.unlet.ul_name);
5833 break;
5834 case ISN_UNLETENV:
5835 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5836 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5837 iptr->isn_arg.unlet.ul_name);
5838 break;
5839 case ISN_UNLETINDEX:
5840 smsg("%s%4d UNLETINDEX", pfx, current);
5841 break;
5842 case ISN_UNLETRANGE:
5843 smsg("%s%4d UNLETRANGE", pfx, current);
5844 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005845 case ISN_LOCKUNLOCK:
5846 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5847 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005848 case ISN_LOCKCONST:
5849 smsg("%s%4d LOCKCONST", pfx, current);
5850 break;
5851 case ISN_NEWLIST:
5852 smsg("%s%4d NEWLIST size %lld", pfx, current,
5853 (varnumber_T)(iptr->isn_arg.number));
5854 break;
5855 case ISN_NEWDICT:
5856 smsg("%s%4d NEWDICT size %lld", pfx, current,
5857 (varnumber_T)(iptr->isn_arg.number));
5858 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005859 case ISN_NEWPARTIAL:
5860 smsg("%s%4d NEWPARTIAL", pfx, current);
5861 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005862
5863 // function call
5864 case ISN_BCALL:
5865 {
5866 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5867
5868 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5869 internal_func_name(cbfunc->cbf_idx),
5870 cbfunc->cbf_argcount);
5871 }
5872 break;
5873 case ISN_DCALL:
5874 {
5875 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5876 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5877 + cdfunc->cdf_idx;
5878
5879 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005880 printable_func_name(df->df_ufunc),
5881 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005882 }
5883 break;
5884 case ISN_UCALL:
5885 {
5886 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5887
5888 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5889 cufunc->cuf_name, cufunc->cuf_argcount);
5890 }
5891 break;
5892 case ISN_PCALL:
5893 {
5894 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5895
5896 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5897 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5898 }
5899 break;
5900 case ISN_PCALL_END:
5901 smsg("%s%4d PCALL end", pfx, current);
5902 break;
5903 case ISN_RETURN:
5904 smsg("%s%4d RETURN", pfx, current);
5905 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005906 case ISN_RETURN_VOID:
5907 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005908 break;
5909 case ISN_FUNCREF:
5910 {
5911 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005912 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005913
Bram Moolenaar38453522021-11-28 22:00:12 +00005914 if (funcref->fr_func_name == NULL)
5915 {
5916 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5917 + funcref->fr_dfunc_idx;
5918 name = df->df_ufunc->uf_name;
5919 }
5920 else
5921 name = funcref->fr_func_name;
5922 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005923 }
5924 break;
5925
5926 case ISN_NEWFUNC:
5927 {
5928 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5929
5930 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5931 newfunc->nf_lambda, newfunc->nf_global);
5932 }
5933 break;
5934
5935 case ISN_DEF:
5936 {
5937 char_u *name = iptr->isn_arg.string;
5938
5939 smsg("%s%4d DEF %s", pfx, current,
5940 name == NULL ? (char_u *)"" : name);
5941 }
5942 break;
5943
5944 case ISN_JUMP:
5945 {
5946 char *when = "?";
5947
5948 switch (iptr->isn_arg.jump.jump_when)
5949 {
5950 case JUMP_ALWAYS:
5951 when = "JUMP";
5952 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005953 case JUMP_NEVER:
5954 iemsg("JUMP_NEVER should not be used");
5955 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005956 case JUMP_AND_KEEP_IF_TRUE:
5957 when = "JUMP_AND_KEEP_IF_TRUE";
5958 break;
5959 case JUMP_IF_FALSE:
5960 when = "JUMP_IF_FALSE";
5961 break;
5962 case JUMP_AND_KEEP_IF_FALSE:
5963 when = "JUMP_AND_KEEP_IF_FALSE";
5964 break;
5965 case JUMP_IF_COND_FALSE:
5966 when = "JUMP_IF_COND_FALSE";
5967 break;
5968 case JUMP_IF_COND_TRUE:
5969 when = "JUMP_IF_COND_TRUE";
5970 break;
5971 }
5972 smsg("%s%4d %s -> %d", pfx, current, when,
5973 iptr->isn_arg.jump.jump_where);
5974 }
5975 break;
5976
5977 case ISN_JUMP_IF_ARG_SET:
5978 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5979 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5980 iptr->isn_arg.jump.jump_where);
5981 break;
5982
5983 case ISN_FOR:
5984 {
5985 forloop_T *forloop = &iptr->isn_arg.forloop;
5986
5987 smsg("%s%4d FOR $%d -> %d", pfx, current,
5988 forloop->for_idx, forloop->for_end);
5989 }
5990 break;
5991
5992 case ISN_TRY:
5993 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005994 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005995
5996 if (try->try_ref->try_finally == 0)
5997 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5998 pfx, current,
5999 try->try_ref->try_catch,
6000 try->try_ref->try_endtry);
6001 else
6002 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6003 pfx, current,
6004 try->try_ref->try_catch,
6005 try->try_ref->try_finally,
6006 try->try_ref->try_endtry);
6007 }
6008 break;
6009 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006010 smsg("%s%4d CATCH", pfx, current);
6011 break;
6012 case ISN_TRYCONT:
6013 {
6014 trycont_T *trycont = &iptr->isn_arg.trycont;
6015
6016 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6017 trycont->tct_levels,
6018 trycont->tct_levels == 1 ? "" : "s",
6019 trycont->tct_where);
6020 }
6021 break;
6022 case ISN_FINALLY:
6023 smsg("%s%4d FINALLY", pfx, current);
6024 break;
6025 case ISN_ENDTRY:
6026 smsg("%s%4d ENDTRY", pfx, current);
6027 break;
6028 case ISN_THROW:
6029 smsg("%s%4d THROW", pfx, current);
6030 break;
6031
6032 // expression operations on number
6033 case ISN_OPNR:
6034 case ISN_OPFLOAT:
6035 case ISN_OPANY:
6036 {
6037 char *what;
6038 char *ins;
6039
6040 switch (iptr->isn_arg.op.op_type)
6041 {
6042 case EXPR_MULT: what = "*"; break;
6043 case EXPR_DIV: what = "/"; break;
6044 case EXPR_REM: what = "%"; break;
6045 case EXPR_SUB: what = "-"; break;
6046 case EXPR_ADD: what = "+"; break;
6047 default: what = "???"; break;
6048 }
6049 switch (iptr->isn_type)
6050 {
6051 case ISN_OPNR: ins = "OPNR"; break;
6052 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6053 case ISN_OPANY: ins = "OPANY"; break;
6054 default: ins = "???"; break;
6055 }
6056 smsg("%s%4d %s %s", pfx, current, ins, what);
6057 }
6058 break;
6059
6060 case ISN_COMPAREBOOL:
6061 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006062 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006063 case ISN_COMPARENR:
6064 case ISN_COMPAREFLOAT:
6065 case ISN_COMPARESTRING:
6066 case ISN_COMPAREBLOB:
6067 case ISN_COMPARELIST:
6068 case ISN_COMPAREDICT:
6069 case ISN_COMPAREFUNC:
6070 case ISN_COMPAREANY:
6071 {
6072 char *p;
6073 char buf[10];
6074 char *type;
6075
6076 switch (iptr->isn_arg.op.op_type)
6077 {
6078 case EXPR_EQUAL: p = "=="; break;
6079 case EXPR_NEQUAL: p = "!="; break;
6080 case EXPR_GREATER: p = ">"; break;
6081 case EXPR_GEQUAL: p = ">="; break;
6082 case EXPR_SMALLER: p = "<"; break;
6083 case EXPR_SEQUAL: p = "<="; break;
6084 case EXPR_MATCH: p = "=~"; break;
6085 case EXPR_IS: p = "is"; break;
6086 case EXPR_ISNOT: p = "isnot"; break;
6087 case EXPR_NOMATCH: p = "!~"; break;
6088 default: p = "???"; break;
6089 }
6090 STRCPY(buf, p);
6091 if (iptr->isn_arg.op.op_ic == TRUE)
6092 strcat(buf, "?");
6093 switch(iptr->isn_type)
6094 {
6095 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6096 case ISN_COMPARESPECIAL:
6097 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006098 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006099 case ISN_COMPARENR: type = "COMPARENR"; break;
6100 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6101 case ISN_COMPARESTRING:
6102 type = "COMPARESTRING"; break;
6103 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6104 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6105 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6106 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6107 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6108 default: type = "???"; break;
6109 }
6110
6111 smsg("%s%4d %s %s", pfx, current, type, buf);
6112 }
6113 break;
6114
6115 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6116 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6117
6118 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006119 case ISN_CONCAT:
6120 smsg("%s%4d CONCAT size %lld", pfx, current,
6121 (varnumber_T)(iptr->isn_arg.number));
6122 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006123 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6124 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6125 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6126 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6127 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6128 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6129 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6130 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6131 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6132 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6133 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006134 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006135 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6136 iptr->isn_arg.getitem.gi_index,
6137 iptr->isn_arg.getitem.gi_with_op ?
6138 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006139 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6140 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6141 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006142 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6143 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6144
Bram Moolenaar4c137212021-04-19 16:48:48 +02006145 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6146
6147 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6148 case ISN_CHECKTYPE:
6149 {
6150 checktype_T *ct = &iptr->isn_arg.type;
6151 char *tofree;
6152
6153 if (ct->ct_arg_idx == 0)
6154 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6155 type_name(ct->ct_type, &tofree),
6156 (int)ct->ct_off);
6157 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006158 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6159 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006160 type_name(ct->ct_type, &tofree),
6161 (int)ct->ct_off,
6162 (int)ct->ct_arg_idx);
6163 vim_free(tofree);
6164 break;
6165 }
6166 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6167 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6168 iptr->isn_arg.checklen.cl_min_len);
6169 break;
6170 case ISN_SETTYPE:
6171 {
6172 char *tofree;
6173
6174 smsg("%s%4d SETTYPE %s", pfx, current,
6175 type_name(iptr->isn_arg.type.ct_type, &tofree));
6176 vim_free(tofree);
6177 break;
6178 }
6179 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006180 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6181 smsg("%s%4d INVERT %d (!val)", pfx, current,
6182 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006183 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006184 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6185 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006186 break;
6187 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006188 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006189 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006190 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6191 pfx, current,
6192 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006193 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006194 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6195 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006196 break;
6197 case ISN_PUT:
6198 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6199 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006200 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006201 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6202 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006203 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006204 else
6205 smsg("%s%4d PUT %c %ld", pfx, current,
6206 iptr->isn_arg.put.put_regname,
6207 (long)iptr->isn_arg.put.put_lnum);
6208 break;
6209
Bram Moolenaar4c137212021-04-19 16:48:48 +02006210 case ISN_CMDMOD:
6211 {
6212 char_u *buf;
6213 size_t len = produce_cmdmods(
6214 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6215
6216 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006217 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006218 {
6219 (void)produce_cmdmods(
6220 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6221 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6222 vim_free(buf);
6223 }
6224 break;
6225 }
6226 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6227
6228 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006229 smsg("%s%4d PROFILE START line %d", pfx, current,
6230 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006231 break;
6232
6233 case ISN_PROF_END:
6234 smsg("%s%4d PROFILE END", pfx, current);
6235 break;
6236
Bram Moolenaare99d4222021-06-13 14:01:26 +02006237 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006238 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6239 iptr->isn_arg.debug.dbg_break_lnum + 1,
6240 iptr->isn_lnum,
6241 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006242 break;
6243
Bram Moolenaar4c137212021-04-19 16:48:48 +02006244 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6245 iptr->isn_arg.unpack.unp_count,
6246 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6247 break;
6248 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6249 iptr->isn_arg.shuffle.shfl_item,
6250 iptr->isn_arg.shuffle.shfl_up);
6251 break;
6252 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6253
6254 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6255 return;
6256 }
6257
6258 out_flush(); // output one line at a time
6259 ui_breakcheck();
6260 if (got_int)
6261 break;
6262 }
6263}
6264
6265/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006266 * Handle command line completion for the :disassemble command.
6267 */
6268 void
6269set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6270{
6271 char_u *p;
6272
6273 // Default: expand user functions, "debug" and "profile"
6274 xp->xp_context = EXPAND_DISASSEMBLE;
6275 xp->xp_pattern = arg;
6276
6277 // first argument already typed: only user function names
6278 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6279 {
6280 xp->xp_context = EXPAND_USER_FUNC;
6281 xp->xp_pattern = skipwhite(p);
6282 }
6283}
6284
6285/*
6286 * Function given to ExpandGeneric() to obtain the list of :disassemble
6287 * arguments.
6288 */
6289 char_u *
6290get_disassemble_argument(expand_T *xp, int idx)
6291{
6292 if (idx == 0)
6293 return (char_u *)"debug";
6294 if (idx == 1)
6295 return (char_u *)"profile";
6296 return get_user_func_name(xp, idx - 2);
6297}
6298
6299/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006300 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006301 * We don't really need this at runtime, but we do have tests that require it,
6302 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 */
6304 void
6305ex_disassemble(exarg_T *eap)
6306{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006307 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006308 char_u *fname;
6309 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310 dfunc_T *dfunc;
6311 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006312 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006313 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006314 compiletype_T compile_type = CT_NONE;
6315
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006316 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006317 {
6318 compile_type = CT_PROFILE;
6319 arg = skipwhite(arg + 7);
6320 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006321 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006322 {
6323 compile_type = CT_DEBUG;
6324 arg = skipwhite(arg + 5);
6325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006327 if (STRNCMP(arg, "<lambda>", 8) == 0)
6328 {
6329 arg += 8;
6330 (void)getdigits(&arg);
6331 fname = vim_strnsave(eap->arg, arg - eap->arg);
6332 }
6333 else
6334 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006335 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006336 if (fname == NULL)
6337 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006338 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006339 return;
6340 }
6341
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006342 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006343 if (ufunc == NULL)
6344 {
6345 char_u *p = untrans_function_name(fname);
6346
6347 if (p != NULL)
6348 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006349 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006350 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006351 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352 if (ufunc == NULL)
6353 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006354 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355 return;
6356 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006357 if (func_needs_compiling(ufunc, compile_type)
6358 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006359 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006360 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006362 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006363 return;
6364 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006365 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366
6367 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006368 switch (compile_type)
6369 {
6370 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006371#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006372 instr = dfunc->df_instr_prof;
6373 instr_count = dfunc->df_instr_prof_count;
6374 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006375#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006376 // FALLTHROUGH
6377 case CT_NONE:
6378 instr = dfunc->df_instr;
6379 instr_count = dfunc->df_instr_count;
6380 break;
6381 case CT_DEBUG:
6382 instr = dfunc->df_instr_debug;
6383 instr_count = dfunc->df_instr_debug_count;
6384 break;
6385 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006386
Bram Moolenaar4c137212021-04-19 16:48:48 +02006387 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006388}
6389
6390/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006391 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392 * list, etc. Mostly like what JavaScript does, except that empty list and
6393 * empty dictionary are FALSE.
6394 */
6395 int
6396tv2bool(typval_T *tv)
6397{
6398 switch (tv->v_type)
6399 {
6400 case VAR_NUMBER:
6401 return tv->vval.v_number != 0;
6402 case VAR_FLOAT:
6403#ifdef FEAT_FLOAT
6404 return tv->vval.v_float != 0.0;
6405#else
6406 break;
6407#endif
6408 case VAR_PARTIAL:
6409 return tv->vval.v_partial != NULL;
6410 case VAR_FUNC:
6411 case VAR_STRING:
6412 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6413 case VAR_LIST:
6414 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6415 case VAR_DICT:
6416 return tv->vval.v_dict != NULL
6417 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6418 case VAR_BOOL:
6419 case VAR_SPECIAL:
6420 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6421 case VAR_JOB:
6422#ifdef FEAT_JOB_CHANNEL
6423 return tv->vval.v_job != NULL;
6424#else
6425 break;
6426#endif
6427 case VAR_CHANNEL:
6428#ifdef FEAT_JOB_CHANNEL
6429 return tv->vval.v_channel != NULL;
6430#else
6431 break;
6432#endif
6433 case VAR_BLOB:
6434 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6435 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006436 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006437 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006438 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006439 break;
6440 }
6441 return FALSE;
6442}
6443
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006444 void
6445emsg_using_string_as(typval_T *tv, int as_number)
6446{
6447 semsg(_(as_number ? e_using_string_as_number_str
6448 : e_using_string_as_bool_str),
6449 tv->vval.v_string == NULL
6450 ? (char_u *)"" : tv->vval.v_string);
6451}
6452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006453/*
6454 * If "tv" is a string give an error and return FAIL.
6455 */
6456 int
6457check_not_string(typval_T *tv)
6458{
6459 if (tv->v_type == VAR_STRING)
6460 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006461 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006462 clear_tv(tv);
6463 return FAIL;
6464 }
6465 return OK;
6466}
6467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468
6469#endif // FEAT_EVAL