blob: a1311ee177e46643c627c956cb804792dc0a428e [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010067// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
LemonBoy372bcce2022-04-25 12:43:20 +0100123 * Create a new string from "count" items at the bottom of the stack.
124 * A trailing NUL is appended.
125 * When "count" is zero an empty string is added to the stack.
126 */
127 static int
128exe_concat(int count, ectx_T *ectx)
129{
130 int idx;
131 int len = 0;
132 typval_T *tv;
133 garray_T ga;
134
135 ga_init2(&ga, sizeof(char), 1);
136 // Preallocate enough space for the whole string to avoid having to grow
137 // and copy.
138 for (idx = 0; idx < count; ++idx)
139 {
140 tv = STACK_TV_BOT(idx - count);
141 if (tv->vval.v_string != NULL)
142 len += (int)STRLEN(tv->vval.v_string);
143 }
144
145 if (ga_grow(&ga, len + 1) == FAIL)
146 return FAIL;
147
148 for (idx = 0; idx < count; ++idx)
149 {
150 tv = STACK_TV_BOT(idx - count);
151 ga_concat(&ga, tv->vval.v_string);
152 clear_tv(tv);
153 }
154
155 // add a terminating NUL
156 ga_append(&ga, NUL);
157
158 ectx->ec_stack.ga_len -= count - 1;
159 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
160
161 return OK;
162}
163
164/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200165 * Create a new list from "count" items at the bottom of the stack.
166 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100167 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200168 */
169 static int
170exe_newlist(int count, ectx_T *ectx)
171{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100172 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200173 int idx;
174 typval_T *tv;
175
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100176 if (count >= 0)
177 {
178 list = list_alloc_with_items(count);
179 if (list == NULL)
180 return FAIL;
181 for (idx = 0; idx < count; ++idx)
182 list_set_item(list, idx, STACK_TV_BOT(idx - count));
183 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200184
185 if (count > 0)
186 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200187 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100188 {
189 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100191 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200192 else
193 ++ectx->ec_stack.ga_len;
194 tv = STACK_TV_BOT(-1);
195 tv->v_type = VAR_LIST;
196 tv->vval.v_list = list;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 if (list != NULL)
198 ++list->lv_refcount;
199 return OK;
200}
201
202/*
203 * Implementation of ISN_NEWDICT.
204 * Returns FAIL on total failure, MAYBE on error.
205 */
206 static int
207exe_newdict(int count, ectx_T *ectx)
208{
209 dict_T *dict = NULL;
210 dictitem_T *item;
211 char_u *key;
212 int idx;
213 typval_T *tv;
214
215 if (count >= 0)
216 {
217 dict = dict_alloc();
218 if (unlikely(dict == NULL))
219 return FAIL;
220 for (idx = 0; idx < count; ++idx)
221 {
222 // have already checked key type is VAR_STRING
223 tv = STACK_TV_BOT(2 * (idx - count));
224 // check key is unique
225 key = tv->vval.v_string == NULL
226 ? (char_u *)"" : tv->vval.v_string;
227 item = dict_find(dict, key, -1);
228 if (item != NULL)
229 {
230 semsg(_(e_duplicate_key_in_dicitonary), key);
231 dict_unref(dict);
232 return MAYBE;
233 }
234 item = dictitem_alloc(key);
235 clear_tv(tv);
236 if (unlikely(item == NULL))
237 {
238 dict_unref(dict);
239 return FAIL;
240 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100241 tv = STACK_TV_BOT(2 * (idx - count) + 1);
242 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100243 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100244 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100245 if (dict_add(dict, item) == FAIL)
246 {
247 // can this ever happen?
248 dict_unref(dict);
249 return FAIL;
250 }
251 }
252 }
253
254 if (count > 0)
255 ectx->ec_stack.ga_len -= 2 * count - 1;
256 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
257 return FAIL;
258 else
259 ++ectx->ec_stack.ga_len;
260 tv = STACK_TV_BOT(-1);
261 tv->v_type = VAR_DICT;
262 tv->v_lock = 0;
263 tv->vval.v_dict = dict;
264 if (dict != NULL)
265 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200266 return OK;
267}
268
269/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200270 * If debug_tick changed check if "ufunc" has a breakpoint and update
271 * "uf_has_breakpoint".
272 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000273 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200274update_has_breakpoint(ufunc_T *ufunc)
275{
276 if (ufunc->uf_debug_tick != debug_tick)
277 {
278 linenr_T breakpoint;
279
280 ufunc->uf_debug_tick = debug_tick;
281 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
282 ufunc->uf_has_breakpoint = breakpoint > 0;
283 }
284}
285
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200286static garray_T dict_stack = GA_EMPTY;
287
288/*
289 * Put a value on the dict stack. This consumes "tv".
290 */
291 static int
292dict_stack_save(typval_T *tv)
293{
294 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000295 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200296 if (ga_grow(&dict_stack, 1) == FAIL)
297 return FAIL;
298 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
299 ++dict_stack.ga_len;
300 return OK;
301}
302
303/*
304 * Get the typval at top of the dict stack.
305 */
306 static typval_T *
307dict_stack_get_tv(void)
308{
309 if (dict_stack.ga_len == 0)
310 return NULL;
311 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
312}
313
314/*
315 * Get the dict at top of the dict stack.
316 */
317 static dict_T *
318dict_stack_get_dict(void)
319{
320 typval_T *tv;
321
322 if (dict_stack.ga_len == 0)
323 return NULL;
324 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
325 if (tv->v_type == VAR_DICT)
326 return tv->vval.v_dict;
327 return NULL;
328}
329
330/*
331 * Drop an item from the dict stack.
332 */
333 static void
334dict_stack_drop(void)
335{
336 if (dict_stack.ga_len == 0)
337 {
338 iemsg("Dict stack underflow");
339 return;
340 }
341 --dict_stack.ga_len;
342 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
343}
344
345/*
346 * Drop items from the dict stack until the length is equal to "len".
347 */
348 static void
349dict_stack_clear(int len)
350{
351 while (dict_stack.ga_len > len)
352 dict_stack_drop();
353}
354
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200355/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000356 * Get a pointer to useful "pt_outer" of "pt".
357 */
358 static outer_T *
359get_pt_outer(partial_T *pt)
360{
361 partial_T *ptref = pt->pt_outer_partial;
362
363 if (ptref == NULL)
364 return &pt->pt_outer;
365
366 // partial using partial (recursively)
367 while (ptref->pt_outer_partial != NULL)
368 ptref = ptref->pt_outer_partial;
369 return &ptref->pt_outer;
370}
371
372/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100374 * This adds a stack frame and sets the instruction pointer to the start of the
375 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200376 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100377 *
378 * Stack has:
379 * - current arguments (already there)
380 * - omitted optional argument (default values) added here
381 * - stack frame:
382 * - pointer to calling function
383 * - Index of next instruction in calling function
384 * - previous frame pointer
385 * - reserved space for local variables
386 */
387 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100388call_dfunc(
389 int cdf_idx,
390 partial_T *pt,
391 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100392 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394 int argcount = argcount_arg;
395 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
396 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200397 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 int arg_to_add;
399 int vararg_count = 0;
400 int varcount;
401 int idx;
402 estack_T *entry;
403 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200404 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000405 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100406
407 if (dfunc->df_deleted)
408 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100409 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000410 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100411 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412 return FAIL;
413 }
414
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100415#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100416 if (do_profiling == PROF_YES)
417 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200418 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100419 {
420 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
421 + profile_info_ga.ga_len;
422 ++profile_info_ga.ga_len;
423 CLEAR_POINTER(info);
424 profile_may_start_func(info, ufunc,
425 (((dfunc_T *)def_functions.ga_data)
426 + ectx->ec_dfunc_idx)->df_ufunc);
427 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100428 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100429#endif
430
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200431 // When debugging and using "cont" switches to the not-debugged
432 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000433 compile_type = get_compile_type(ufunc);
434 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200435 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000436 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200437
438 // compile_def_function() may cause def_functions.ga_data to change
439 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
440 }
441 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200442 {
443 if (did_emsg_cumul + did_emsg == did_emsg_before)
444 semsg(_(e_function_is_not_compiled_str),
445 printable_func_name(ufunc));
446 return FAIL;
447 }
448
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200449 if (ufunc->uf_va_name != NULL)
450 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200451 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200452 // Stack at time of call with 2 varargs:
453 // normal_arg
454 // optional_arg
455 // vararg_1
456 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200457 // After creating the list:
458 // normal_arg
459 // optional_arg
460 // vararg-list
461 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200462 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200463 // After creating the list
464 // normal_arg
465 // (space for optional_arg)
466 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200467 vararg_count = argcount - ufunc->uf_args.ga_len;
468 if (vararg_count < 0)
469 vararg_count = 0;
470 else
471 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200472 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200473 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200474
475 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200476 }
477
Bram Moolenaarfe270812020-04-11 22:31:27 +0200478 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200479 if (arg_to_add < 0)
480 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100481 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
482 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200483 return FAIL;
484 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000485 else if (arg_to_add > ufunc->uf_def_args.ga_len)
486 {
487 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
488
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100489 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
490 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000491 return FAIL;
492 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200493
494 // Reserve space for:
495 // - missing arguments
496 // - stack frame
497 // - local variables
498 // - if needed: a counter for number of closures created in
499 // ectx->ec_funcrefs.
500 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200501 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502 return FAIL;
503
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100504 // If depth of calling is getting too high, don't execute the function.
505 if (funcdepth_increment() == FAIL)
506 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200507 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100508
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100509 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200510 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100511 {
512 floc = ALLOC_ONE(funclocal_T);
513 if (floc == NULL)
514 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200515 *floc = ectx->ec_funclocal;
516 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100517 }
518
Bram Moolenaarfe270812020-04-11 22:31:27 +0200519 // Move the vararg-list to below the missing optional arguments.
520 if (vararg_count > 0 && arg_to_add > 0)
521 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100522
523 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200524 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200525 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200526 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527
528 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100529 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
530 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200531 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200532 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
533 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100534 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100535 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200536 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537
538 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200539 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000540 {
541 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
542
543 tv->v_type = VAR_NUMBER;
544 tv->vval.v_number = 0;
545 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200546 if (dfunc->df_has_closure)
547 {
548 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
549
550 tv->v_type = VAR_NUMBER;
551 tv->vval.v_number = 0;
552 }
553 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100555 if (pt != NULL || ufunc->uf_partial != NULL
556 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100557 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200558 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100559
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200560 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100561 return FAIL;
562 if (pt != NULL)
563 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000564 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200565 ++pt->pt_refcount;
566 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100567 }
568 else if (ufunc->uf_partial != NULL)
569 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000570 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200571 ++ufunc->uf_partial->pt_refcount;
572 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100573 }
574 else
575 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200576 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200577 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200578 {
579 vim_free(ref);
580 return FAIL;
581 }
582 ref->or_outer_allocated = TRUE;
583 ref->or_outer->out_stack = &ectx->ec_stack;
584 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
585 if (ectx->ec_outer_ref != NULL)
586 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100587 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200588 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100589 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100590 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200591 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100592
Bram Moolenaarc970e422021-03-17 15:03:04 +0100593 ++ufunc->uf_calls;
594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595 // Set execution state to the start of the called function.
596 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100597 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100598 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200599 if (entry != NULL)
600 {
601 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200602 // Save the current context so it can be restored on return.
603 entry->es_save_sctx = current_sctx;
604 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200605 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100606
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200607 // Start execution at the first instruction.
608 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
610 return OK;
611}
612
613// Get pointer to item in the stack.
614#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
615
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000616// Double linked list of funcstack_T in use.
617static funcstack_T *first_funcstack = NULL;
618
619 static void
620add_funcstack_to_list(funcstack_T *funcstack)
621{
622 // Link in list of funcstacks.
623 if (first_funcstack != NULL)
624 first_funcstack->fs_prev = funcstack;
625 funcstack->fs_next = first_funcstack;
626 funcstack->fs_prev = NULL;
627 first_funcstack = funcstack;
628}
629
630 static void
631remove_funcstack_from_list(funcstack_T *funcstack)
632{
633 if (funcstack->fs_prev == NULL)
634 first_funcstack = funcstack->fs_next;
635 else
636 funcstack->fs_prev->fs_next = funcstack->fs_next;
637 if (funcstack->fs_next != NULL)
638 funcstack->fs_next->fs_prev = funcstack->fs_prev;
639}
640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200642 * Used when returning from a function: Check if any closure is still
643 * referenced. If so then move the arguments and variables to a separate piece
644 * of stack to be used when the closure is called.
645 * When "free_arguments" is TRUE the arguments are to be freed.
646 * Returns FAIL when out of memory.
647 */
648 static int
649handle_closure_in_use(ectx_T *ectx, int free_arguments)
650{
651 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
652 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200653 int argcount;
654 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200655 int idx;
656 typval_T *tv;
657 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200658 garray_T *gap = &ectx->ec_funcrefs;
659 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200660
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200661 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200662 return OK; // function was freed
663 if (dfunc->df_has_closure == 0)
664 return OK; // no closures
665 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
666 closure_count = tv->vval.v_number;
667 if (closure_count == 0)
668 return OK; // no funcrefs created
669
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200670 argcount = ufunc_argcount(dfunc->df_ufunc);
671 top = ectx->ec_frame_idx - argcount;
672
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200673 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200674 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200675 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200676 partial_T *pt;
677 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200678
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200679 if (off < 0)
680 continue; // count is off or already done
681 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200682 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200683 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200684 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200685 int i;
686
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000687 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200688 // unreferenced on return.
689 for (i = 0; i < dfunc->df_varcount; ++i)
690 {
691 typval_T *stv = STACK_TV(ectx->ec_frame_idx
692 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200693 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200694 --refcount;
695 }
696 if (refcount > 1)
697 {
698 closure_in_use = TRUE;
699 break;
700 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200701 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200702 }
703
704 if (closure_in_use)
705 {
706 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
707 typval_T *stack;
708
709 // A closure is using the arguments and/or local variables.
710 // Move them to the called function.
711 if (funcstack == NULL)
712 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000713
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200714 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
715 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200716 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
717 funcstack->fs_ga.ga_data = stack;
718 if (stack == NULL)
719 {
720 vim_free(funcstack);
721 return FAIL;
722 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000723 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200724
725 // Move or copy the arguments.
726 for (idx = 0; idx < argcount; ++idx)
727 {
728 tv = STACK_TV(top + idx);
729 if (free_arguments)
730 {
731 *(stack + idx) = *tv;
732 tv->v_type = VAR_UNKNOWN;
733 }
734 else
735 copy_tv(tv, stack + idx);
736 }
737 // Move the local variables.
738 for (idx = 0; idx < dfunc->df_varcount; ++idx)
739 {
740 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200741
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200742 // A partial created for a local function, that is also used as a
743 // local variable, has a reference count for the variable, thus
744 // will never go down to zero. When all these refcounts are one
745 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000746 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200747 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
748 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200749 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200750
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200751 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200752 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
753 gap->ga_len - closure_count + i])
754 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200755 }
756
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200757 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200758 tv->v_type = VAR_UNKNOWN;
759 }
760
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200761 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200762 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200763 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
764 - closure_count + idx];
765 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200766 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200767 ++funcstack->fs_refcount;
768 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100769 pt->pt_outer.out_stack = &funcstack->fs_ga;
770 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200771 }
772 }
773 }
774
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200775 for (idx = 0; idx < closure_count; ++idx)
776 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
777 - closure_count + idx]);
778 gap->ga_len -= closure_count;
779 if (gap->ga_len == 0)
780 ga_clear(gap);
781
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200782 return OK;
783}
784
785/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200786 * Called when a partial is freed or its reference count goes down to one. The
787 * funcstack may be the only reference to the partials in the local variables.
788 * Go over all of them, the funcref and can be freed if all partials
789 * referencing the funcstack have a reference count of one.
790 */
791 void
792funcstack_check_refcount(funcstack_T *funcstack)
793{
794 int i;
795 garray_T *gap = &funcstack->fs_ga;
796 int done = 0;
797
798 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
799 return;
800 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
801 {
802 typval_T *tv = ((typval_T *)gap->ga_data) + i;
803
804 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
805 && tv->vval.v_partial->pt_funcstack == funcstack
806 && tv->vval.v_partial->pt_refcount == 1)
807 ++done;
808 }
809 if (done == funcstack->fs_min_refcount)
810 {
811 typval_T *stack = gap->ga_data;
812
813 // All partials referencing the funcstack have a reference count of
814 // one, thus the funcstack is no longer of use.
815 for (i = 0; i < gap->ga_len; ++i)
816 clear_tv(stack + i);
817 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000818 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200819 vim_free(funcstack);
820 }
821}
822
823/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000824 * For garbage collecting: set references in all variables referenced by
825 * all funcstacks.
826 */
827 int
828set_ref_in_funcstacks(int copyID)
829{
830 funcstack_T *funcstack;
831
832 for (funcstack = first_funcstack; funcstack != NULL;
833 funcstack = funcstack->fs_next)
834 {
835 typval_T *stack = funcstack->fs_ga.ga_data;
836 int i;
837
838 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
839 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
840 return TRUE; // abort
841 }
842 return FALSE;
843}
844
845/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 * Return from the current function.
847 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200848 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200849func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100852 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200853 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
854 + ectx->ec_dfunc_idx;
855 int argcount = ufunc_argcount(dfunc->df_ufunc);
856 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200857 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100858 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
859 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200860 funclocal_T *floc;
861#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100862 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
863 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864
Bram Moolenaar12d26532021-02-19 19:13:21 +0100865 if (do_profiling == PROF_YES)
866 {
867 ufunc_T *caller = prev_dfunc->df_ufunc;
868
869 if (dfunc->df_ufunc->uf_profiling
870 || (caller != NULL && caller->uf_profiling))
871 {
872 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
873 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
874 --profile_info_ga.ga_len;
875 }
876 }
877#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000878
879 // No check for uf_refcount being zero, cannot think of a way that would
880 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100881 --dfunc->df_ufunc->uf_calls;
882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200884 entry = estack_pop();
885 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200886 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200888 if (handle_closure_in_use(ectx, TRUE) == FAIL)
889 return FAIL;
890
891 // Clear the arguments.
892 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
893 clear_tv(STACK_TV(idx));
894
895 // Clear local variables and temp values, but not the return value.
896 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100897 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100899
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100900 // The return value should be on top of the stack. However, when aborting
901 // it may not be there and ec_frame_idx is the top of the stack.
902 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100903 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100904 ret_idx = 0;
905
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200906 if (ectx->ec_outer_ref != NULL)
907 {
908 if (ectx->ec_outer_ref->or_outer_allocated)
909 vim_free(ectx->ec_outer_ref->or_outer);
910 partial_unref(ectx->ec_outer_ref->or_partial);
911 vim_free(ectx->ec_outer_ref);
912 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100913
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100914 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100915 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100916 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
917 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200918 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
919 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200920 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100921 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100922 floc = (void *)STACK_TV(ectx->ec_frame_idx
923 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200924 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100925 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
926 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200927
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100928 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200929 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100930 else
931 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200932 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100933 vim_free(floc);
934 }
935
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100936 if (ret_idx > 0)
937 {
938 // Reset the stack to the position before the call, with a spot for the
939 // return value, moved there from above the frame.
940 ectx->ec_stack.ga_len = top + 1;
941 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
942 }
943 else
944 // Reset the stack to the position before the call.
945 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200946
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100947 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200948 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200949 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950}
951
952#undef STACK_TV
953
954/*
955 * Prepare arguments and rettv for calling a builtin or user function.
956 */
957 static int
958call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
959{
960 int idx;
961 typval_T *tv;
962
963 // Move arguments from bottom of the stack to argvars[] and add terminator.
964 for (idx = 0; idx < argcount; ++idx)
965 argvars[idx] = *STACK_TV_BOT(idx - argcount);
966 argvars[argcount].v_type = VAR_UNKNOWN;
967
968 // Result replaces the arguments on the stack.
969 if (argcount > 0)
970 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200971 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 return FAIL;
973 else
974 ++ectx->ec_stack.ga_len;
975
976 // Default return value is zero.
977 tv = STACK_TV_BOT(-1);
978 tv->v_type = VAR_NUMBER;
979 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +0100980 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981
982 return OK;
983}
984
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200985// Ugly global to avoid passing the execution context around through many
986// layers.
987static ectx_T *current_ectx = NULL;
988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989/*
990 * Call a builtin function by index.
991 */
992 static int
993call_bfunc(int func_idx, int argcount, ectx_T *ectx)
994{
995 typval_T argvars[MAX_FUNC_ARGS];
996 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100997 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200998 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200999 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000
1001 if (call_prepare(argcount, argvars, ectx) == FAIL)
1002 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001003 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001005 // Call the builtin function. Set "current_ectx" so that when it
1006 // recursively invokes call_def_function() a closure context can be set.
1007 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001009 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001010 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011
1012 // Clear the arguments.
1013 for (idx = 0; idx < argcount; ++idx)
1014 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001015
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001016 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001017 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 return OK;
1019}
1020
1021/*
1022 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001023 * If the function is compiled this will add a stack frame and set the
1024 * instruction pointer at the start of the function.
1025 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001026 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001027 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 */
1029 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001030call_ufunc(
1031 ufunc_T *ufunc,
1032 partial_T *pt,
1033 int argcount,
1034 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001035 isn_T *iptr,
1036 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037{
1038 typval_T argvars[MAX_FUNC_ARGS];
1039 funcexe_T funcexe;
1040 int error;
1041 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001042 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001043 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044
Bram Moolenaare99d4222021-06-13 14:01:26 +02001045 if (func_needs_compiling(ufunc, compile_type)
1046 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1047 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001048 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001049 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001050 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001051 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001052 if (error != FCERR_UNKNOWN)
1053 {
1054 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001055 semsg(_(e_too_many_arguments_for_function_str),
1056 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001057 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001058 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001059 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001060 return FAIL;
1061 }
1062
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001063 // The function has been compiled, can call it quickly. For a function
1064 // that was defined later: we can call it directly next time.
1065 if (iptr != NULL)
1066 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001067 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001068 iptr->isn_type = ISN_DCALL;
1069 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1070 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1071 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001072 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074
1075 if (call_prepare(argcount, argvars, ectx) == FAIL)
1076 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001077 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001078 funcexe.fe_evaluate = TRUE;
1079 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080
1081 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001083 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084
1085 // Clear the arguments.
1086 for (idx = 0; idx < argcount; ++idx)
1087 clear_tv(&argvars[idx]);
1088
1089 if (error != FCERR_NONE)
1090 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001091 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 return FAIL;
1093 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001094 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001095 // Error other than from calling the function itself.
1096 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 return OK;
1098}
1099
1100/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001101 * If command modifiers were applied restore them.
1102 */
1103 static void
1104may_restore_cmdmod(funclocal_T *funclocal)
1105{
1106 if (funclocal->floc_restore_cmdmod)
1107 {
1108 cmdmod.cmod_filter_regmatch.regprog = NULL;
1109 undo_cmdmod(&cmdmod);
1110 cmdmod = funclocal->floc_save_cmdmod;
1111 funclocal->floc_restore_cmdmod = FALSE;
1112 }
1113}
1114
1115/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001116 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1117 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001118 */
1119 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001120vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001121{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001122 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001123}
1124
1125/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 * Execute a function by "name".
1127 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001128 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 * Returns FAIL if not found without an error message.
1130 */
1131 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001132call_by_name(
1133 char_u *name,
1134 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001135 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001136 isn_T *iptr,
1137 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138{
1139 ufunc_T *ufunc;
1140
1141 if (builtin_function(name, -1))
1142 {
1143 int func_idx = find_internal_func(name);
1144
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001145 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001147 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 return FAIL;
1149 return call_bfunc(func_idx, argcount, ectx);
1150 }
1151
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001152 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001153
1154 if (ufunc == NULL)
1155 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001156 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001157
1158 if (script_autoload(name, TRUE))
1159 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001160 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001161
1162 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001163 return FAIL; // bail out if loading the script caused an error
1164 }
1165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001167 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001168 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001169 {
1170 int i;
1171 typval_T *argv = STACK_TV_BOT(0) - argcount;
1172
1173 // The function can change at runtime, check that the argument
1174 // types are correct.
1175 for (i = 0; i < argcount; ++i)
1176 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001177 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001178
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001179 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001180 type = ufunc->uf_arg_types[i];
1181 else if (ufunc->uf_va_type != NULL)
1182 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001183 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001184 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001185 return FAIL;
1186 }
1187 }
1188
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001189 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191
1192 return FAIL;
1193}
1194
1195 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001196call_partial(
1197 typval_T *tv,
1198 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001199 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001201 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001202 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001204 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001205 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206
1207 if (tv->v_type == VAR_PARTIAL)
1208 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001209 partial_T *pt = tv->vval.v_partial;
1210 int i;
1211
1212 if (pt->pt_argc > 0)
1213 {
1214 // Make space for arguments from the partial, shift the "argcount"
1215 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001216 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001217 return FAIL;
1218 for (i = 1; i <= argcount; ++i)
1219 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1220 ectx->ec_stack.ga_len += pt->pt_argc;
1221 argcount += pt->pt_argc;
1222
1223 // copy the arguments from the partial onto the stack
1224 for (i = 0; i < pt->pt_argc; ++i)
1225 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1226 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001227 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228
1229 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001230 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232 name = pt->pt_name;
1233 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001234 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001236 if (name != NULL)
1237 {
1238 char_u fname_buf[FLEN_FIXED + 1];
1239 char_u *tofree = NULL;
1240 int error = FCERR_NONE;
1241 char_u *fname;
1242
1243 // May need to translate <SNR>123_ to K_SNR.
1244 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1245 if (error != FCERR_NONE)
1246 res = FAIL;
1247 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001248 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001249 vim_free(tofree);
1250 }
1251
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001252 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 {
1254 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001255 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001256 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 return FAIL;
1258 }
1259 return OK;
1260}
1261
1262/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001263 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1264 * TRUE.
1265 */
1266 static int
1267error_if_locked(int lock, char *error)
1268{
1269 if (lock & (VAR_LOCKED | VAR_FIXED))
1270 {
1271 emsg(_(error));
1272 return TRUE;
1273 }
1274 return FALSE;
1275}
1276
1277/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001278 * Give an error if "tv" is not a number and return FAIL.
1279 */
1280 static int
1281check_for_number(typval_T *tv)
1282{
1283 if (tv->v_type != VAR_NUMBER)
1284 {
1285 semsg(_(e_expected_str_but_got_str),
1286 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1287 return FAIL;
1288 }
1289 return OK;
1290}
1291
1292/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001293 * Store "tv" in variable "name".
1294 * This is for s: and g: variables.
1295 */
1296 static void
1297store_var(char_u *name, typval_T *tv)
1298{
1299 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001300 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001301
Bram Moolenaard877a572021-04-01 19:42:48 +02001302 if (tv->v_lock)
1303 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001304 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001305 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001306 restore_funccal();
1307}
1308
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001309/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001310 * Convert "tv" to a string.
1311 * Return FAIL if not allowed.
1312 */
1313 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001314do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001315{
1316 if (tv->v_type != VAR_STRING)
1317 {
1318 char_u *str;
1319
1320 if (is_2string_any)
1321 {
1322 switch (tv->v_type)
1323 {
1324 case VAR_SPECIAL:
1325 case VAR_BOOL:
1326 case VAR_NUMBER:
1327 case VAR_FLOAT:
1328 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001329
1330 case VAR_LIST:
1331 if (tolerant)
1332 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001333 char_u *s, *e, *p;
1334 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001335
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001336 ga_init2(&ga, sizeof(char_u *), 1);
1337
1338 // Convert to NL separated items, then
1339 // escape the items and replace the NL with
1340 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001341 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001342 if (str == NULL)
1343 return FAIL;
1344 s = str;
1345 while ((e = vim_strchr(s, '\n')) != NULL)
1346 {
1347 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001348 p = vim_strsave_fnameescape(s,
1349 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001350 if (p != NULL)
1351 {
1352 ga_concat(&ga, p);
1353 ga_concat(&ga, (char_u *)" ");
1354 vim_free(p);
1355 }
1356 s = e + 1;
1357 }
1358 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001359 clear_tv(tv);
1360 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001361 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001362 return OK;
1363 }
1364 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001365 default: to_string_error(tv->v_type);
1366 return FAIL;
1367 }
1368 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001369 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001370 clear_tv(tv);
1371 tv->v_type = VAR_STRING;
1372 tv->vval.v_string = str;
1373 }
1374 return OK;
1375}
1376
1377/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001378 * When the value of "sv" is a null list of dict, allocate it.
1379 */
1380 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001381allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001382{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001383 typval_T *tv = sv->sv_tv;
1384
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001385 switch (tv->v_type)
1386 {
1387 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001388 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001389 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001390 break;
1391 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001392 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001393 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001394 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001395 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001396 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001397 (void)rettv_blob_alloc(tv);
1398 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001399 default:
1400 break;
1401 }
1402}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001403
Bram Moolenaare7525c52021-01-09 13:20:37 +01001404/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001405 * Return the character "str[index]" where "index" is the character index,
1406 * including composing characters.
1407 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001408 */
1409 char_u *
1410char_from_string(char_u *str, varnumber_T index)
1411{
1412 size_t nbyte = 0;
1413 varnumber_T nchar = index;
1414 size_t slen;
1415
1416 if (str == NULL)
1417 return NULL;
1418 slen = STRLEN(str);
1419
Bram Moolenaarff871402021-03-26 13:34:05 +01001420 // Do the same as for a list: a negative index counts from the end.
1421 // Optimization to check the first byte to be below 0x80 (and no composing
1422 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001423 if (index < 0)
1424 {
1425 int clen = 0;
1426
1427 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001428 {
1429 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1430 ++nbyte;
1431 else if (enc_utf8)
1432 nbyte += utfc_ptr2len(str + nbyte);
1433 else
1434 nbyte += mb_ptr2len(str + nbyte);
1435 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001436 nchar = clen + index;
1437 if (nchar < 0)
1438 // unlike list: index out of range results in empty string
1439 return NULL;
1440 }
1441
1442 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001443 {
1444 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1445 ++nbyte;
1446 else if (enc_utf8)
1447 nbyte += utfc_ptr2len(str + nbyte);
1448 else
1449 nbyte += mb_ptr2len(str + nbyte);
1450 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001451 if (nbyte >= slen)
1452 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001453 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001454}
1455
1456/*
1457 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001458 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001459 * If going over the end return "str_len".
1460 * If "idx" is negative count from the end, -1 is the last character.
1461 * When going over the start return -1.
1462 */
1463 static long
1464char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1465{
1466 varnumber_T nchar = idx;
1467 size_t nbyte = 0;
1468
1469 if (nchar >= 0)
1470 {
1471 while (nchar > 0 && nbyte < str_len)
1472 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001473 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001474 --nchar;
1475 }
1476 }
1477 else
1478 {
1479 nbyte = str_len;
1480 while (nchar < 0 && nbyte > 0)
1481 {
1482 --nbyte;
1483 nbyte -= mb_head_off(str, str + nbyte);
1484 ++nchar;
1485 }
1486 if (nchar < 0)
1487 return -1;
1488 }
1489 return (long)nbyte;
1490}
1491
1492/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001493 * Return the slice "str[first : last]" using character indexes. Composing
1494 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001495 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001496 * Return NULL when the result is empty.
1497 */
1498 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001499string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001500{
1501 long start_byte, end_byte;
1502 size_t slen;
1503
1504 if (str == NULL)
1505 return NULL;
1506 slen = STRLEN(str);
1507 start_byte = char_idx2byte(str, slen, first);
1508 if (start_byte < 0)
1509 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001510 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001511 end_byte = (long)slen;
1512 else
1513 {
1514 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001515 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001516 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001517 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001518 }
1519
1520 if (start_byte >= (long)slen || end_byte <= start_byte)
1521 return NULL;
1522 return vim_strnsave(str + start_byte, end_byte - start_byte);
1523}
1524
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001525/*
1526 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1527 * When "dfunc_idx" is negative don't give an error.
1528 * Returns NULL for an error.
1529 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001530 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001531get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001532{
1533 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001534 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1535 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001536 svar_T *sv;
1537
1538 if (sref->sref_seq != si->sn_script_seq)
1539 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001540 // The script was reloaded after the function was compiled, the
1541 // script_idx may not be valid.
1542 if (dfunc != NULL)
1543 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1544 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001545 return NULL;
1546 }
1547 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001548 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001549 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001550 if (dfunc != NULL)
1551 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001552 return NULL;
1553 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001554
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001555 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1556 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001557 {
1558 if (dfunc != NULL)
1559 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1560 return NULL;
1561 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001562 return sv;
1563}
1564
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001565/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001566 * Function passed to do_cmdline() for splitting a script joined by NL
1567 * characters.
1568 */
1569 static char_u *
1570get_split_sourceline(
1571 int c UNUSED,
1572 void *cookie,
1573 int indent UNUSED,
1574 getline_opt_T options UNUSED)
1575{
1576 source_cookie_T *sp = (source_cookie_T *)cookie;
1577 char_u *p;
1578 char_u *line;
1579
Bram Moolenaar20677332021-06-06 17:02:53 +02001580 p = vim_strchr(sp->nextline, '\n');
1581 if (p == NULL)
1582 {
1583 line = vim_strsave(sp->nextline);
1584 sp->nextline += STRLEN(sp->nextline);
1585 }
1586 else
1587 {
1588 line = vim_strnsave(sp->nextline, p - sp->nextline);
1589 sp->nextline = p + 1;
1590 }
1591 return line;
1592}
1593
1594/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 * Execute a function by "name".
1596 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001597 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 */
1599 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001600call_eval_func(
1601 char_u *name,
1602 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001603 ectx_T *ectx,
1604 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605{
Bram Moolenaared677f52020-08-12 16:38:10 +02001606 int called_emsg_before = called_emsg;
1607 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001609 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001610 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001612 dictitem_T *v;
1613
1614 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001615 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1616 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001617 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001618 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001619 return FAIL;
1620 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001621 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001623 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624}
1625
1626/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001627 * When a function reference is used, fill a partial with the information
1628 * needed, especially when it is used as a closure.
1629 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001630 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001631fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1632{
1633 pt->pt_func = ufunc;
1634 pt->pt_refcount = 1;
1635
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001636 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001637 {
1638 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1639 + ectx->ec_dfunc_idx;
1640
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001641 // The closure may need to find arguments and local variables in the
1642 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001643 pt->pt_outer.out_stack = &ectx->ec_stack;
1644 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001645 if (ectx->ec_outer_ref != NULL)
1646 {
1647 // The current context already has a context, link to that one.
1648 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1649 if (ectx->ec_outer_ref->or_partial != NULL)
1650 {
1651 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1652 ++pt->pt_outer.out_up_partial->pt_refcount;
1653 }
1654 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001655
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001656 // If this function returns and the closure is still being used, we
1657 // need to make a copy of the context (arguments and local variables).
1658 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001659 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001660 {
1661 vim_free(pt);
1662 return FAIL;
1663 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001664 // Extra variable keeps the count of closures created in the current
1665 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001666 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1667 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1668
1669 ((partial_T **)ectx->ec_funcrefs.ga_data)
1670 [ectx->ec_funcrefs.ga_len] = pt;
1671 ++pt->pt_refcount;
1672 ++ectx->ec_funcrefs.ga_len;
1673 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001674 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001675 return OK;
1676}
1677
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001678/*
1679 * Execute iptr->isn_arg.string as an Ex command.
1680 */
1681 static int
1682exec_command(isn_T *iptr)
1683{
1684 source_cookie_T cookie;
1685
1686 SOURCING_LNUM = iptr->isn_lnum;
1687 // Pass getsourceline to get an error for a missing ":end"
1688 // command.
1689 CLEAR_FIELD(cookie);
1690 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1691 if (do_cmdline(iptr->isn_arg.string,
1692 getsourceline, &cookie,
1693 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1694 || did_emsg)
1695 return FAIL;
1696 return OK;
1697}
1698
Bram Moolenaar89445512022-04-14 12:58:23 +01001699/*
1700 * If script "sid" is not loaded yet then load it now.
1701 * Caller must make sure "sid" is a valid script ID.
1702 * "loaded" is set to TRUE if the script had to be loaded.
1703 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1704 */
1705 int
1706may_load_script(int sid, int *loaded)
1707{
1708 scriptitem_T *si = SCRIPT_ITEM(sid);
1709
1710 if (si->sn_state == SN_STATE_NOT_LOADED)
1711 {
1712 *loaded = TRUE;
1713 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1714 {
1715 semsg(_(e_cant_open_file_str), si->sn_name);
1716 return FAIL;
1717 }
1718 }
1719 return OK;
1720}
1721
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001722// used for v_instr of typval of VAR_INSTR
1723struct instr_S {
1724 ectx_T *instr_ectx;
1725 isn_T *instr_instr;
1726};
1727
Bram Moolenaar4c137212021-04-19 16:48:48 +02001728// used for substitute_instr
1729typedef struct subs_expr_S {
1730 ectx_T *subs_ectx;
1731 isn_T *subs_instr;
1732 int subs_status;
1733} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734
1735// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001736#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737
1738// Get pointer to item at the bottom of the stack, -1 is the bottom.
1739#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001740#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001742// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001743#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001745// Set when calling do_debug().
1746static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001747static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001748
1749/*
1750 * When debugging lookup "name" and return the typeval.
1751 * When not found return NULL.
1752 */
1753 typval_T *
1754lookup_debug_var(char_u *name)
1755{
1756 int idx;
1757 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001758 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001759 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001760 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001761
1762 if (ectx == NULL)
1763 return NULL;
1764 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1765
1766 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001767 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001768 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001769 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1770
1771 // the variable name may be NULL when not available in this block
1772 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001773 return STACK_TV_VAR(idx);
1774 }
1775
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001776 // Go through argument names.
1777 ufunc = dfunc->df_ufunc;
1778 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1779 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1780 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1781 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1782 - varargs_off + idx);
1783 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1784 return STACK_TV(ectx->ec_frame_idx - 1);
1785
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001786 return NULL;
1787}
1788
Bram Moolenaar26a44842021-09-02 18:49:06 +02001789/*
1790 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1791 * breakpoint was set in that function or when there is any expression.
1792 */
1793 int
1794may_break_in_function(ufunc_T *ufunc)
1795{
1796 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1797}
1798
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001799 static void
1800handle_debug(isn_T *iptr, ectx_T *ectx)
1801{
1802 char_u *line;
1803 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1804 + ectx->ec_dfunc_idx)->df_ufunc;
1805 isn_T *ni;
1806 int end_lnum = iptr->isn_lnum;
1807 garray_T ga;
1808 int lnum;
1809
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001810 if (ex_nesting_level > debug_break_level)
1811 {
1812 linenr_T breakpoint;
1813
Bram Moolenaar26a44842021-09-02 18:49:06 +02001814 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001815 return;
1816
1817 // check for the next breakpoint if needed
1818 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001819 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001820 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1821 return;
1822 }
1823
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001824 SOURCING_LNUM = iptr->isn_lnum;
1825 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001826 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001827
1828 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1829 if (ni->isn_type == ISN_DEBUG
1830 || ni->isn_type == ISN_RETURN
1831 || ni->isn_type == ISN_RETURN_VOID)
1832 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001833 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001834 break;
1835 }
1836
1837 if (end_lnum > iptr->isn_lnum)
1838 {
1839 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001840 for (lnum = iptr->isn_lnum; lnum < end_lnum
1841 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001842 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001843 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001844
Bram Moolenaar303215d2021-07-07 20:10:43 +02001845 if (p == NULL)
1846 continue; // left over from continuation line
1847 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001848 if (*p == '#')
1849 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001850 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001851 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1852 if (STRNCMP(p, "def ", 4) == 0)
1853 break;
1854 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001855 line = ga_concat_strings(&ga, " ");
1856 vim_free(ga.ga_data);
1857 }
1858 else
1859 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001860
Dominique Pelle6e969552021-06-17 13:53:41 +02001861 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001862 debug_context = NULL;
1863
1864 if (end_lnum > iptr->isn_lnum)
1865 vim_free(line);
1866}
1867
Bram Moolenaar4c137212021-04-19 16:48:48 +02001868/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001869 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001870 * Returns OK, FAIL or NOTDONE (uncatchable error).
1871 */
1872 static int
1873execute_storeindex(isn_T *iptr, ectx_T *ectx)
1874{
1875 vartype_T dest_type = iptr->isn_arg.vartype;
1876 typval_T *tv;
1877 typval_T *tv_idx = STACK_TV_BOT(-2);
1878 typval_T *tv_dest = STACK_TV_BOT(-1);
1879 int status = OK;
1880
1881 // Stack contains:
1882 // -3 value to be stored
1883 // -2 index
1884 // -1 dict or list
1885 tv = STACK_TV_BOT(-3);
1886 SOURCING_LNUM = iptr->isn_lnum;
1887 if (dest_type == VAR_ANY)
1888 {
1889 dest_type = tv_dest->v_type;
1890 if (dest_type == VAR_DICT)
1891 status = do_2string(tv_idx, TRUE, FALSE);
1892 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1893 {
1894 emsg(_(e_number_expected));
1895 status = FAIL;
1896 }
1897 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001898
1899 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001900 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001901 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001902 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001903 long lidx = (long)tv_idx->vval.v_number;
1904 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001905
Bram Moolenaare08be092022-02-17 13:08:26 +00001906 if (list == NULL)
1907 {
1908 emsg(_(e_list_not_set));
1909 return FAIL;
1910 }
1911 if (lidx < 0 && list->lv_len + lidx >= 0)
1912 // negative index is relative to the end
1913 lidx = list->lv_len + lidx;
1914 if (lidx < 0 || lidx > list->lv_len)
1915 {
1916 semsg(_(e_list_index_out_of_range_nr), lidx);
1917 return FAIL;
1918 }
1919 if (lidx < list->lv_len)
1920 {
1921 listitem_T *li = list_find(list, lidx);
1922
1923 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001924 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001925 return FAIL;
1926 // overwrite existing list item
1927 clear_tv(&li->li_tv);
1928 li->li_tv = *tv;
1929 }
1930 else
1931 {
1932 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1933 return FAIL;
1934 // append to list, only fails when out of memory
1935 if (list_append_tv(list, tv) == FAIL)
1936 return NOTDONE;
1937 clear_tv(tv);
1938 }
1939 }
1940 else if (dest_type == VAR_DICT)
1941 {
1942 char_u *key = tv_idx->vval.v_string;
1943 dict_T *dict = tv_dest->vval.v_dict;
1944 dictitem_T *di;
1945
1946 SOURCING_LNUM = iptr->isn_lnum;
1947 if (dict == NULL)
1948 {
1949 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001950 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001951 }
1952 if (key == NULL)
1953 key = (char_u *)"";
1954 di = dict_find(dict, key, -1);
1955 if (di != NULL)
1956 {
1957 if (error_if_locked(di->di_tv.v_lock,
1958 e_cannot_change_dict_item))
1959 return FAIL;
1960 // overwrite existing value
1961 clear_tv(&di->di_tv);
1962 di->di_tv = *tv;
1963 }
1964 else
1965 {
1966 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1967 return FAIL;
1968 // add to dict, only fails when out of memory
1969 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1970 return NOTDONE;
1971 clear_tv(tv);
1972 }
1973 }
1974 else if (dest_type == VAR_BLOB)
1975 {
1976 long lidx = (long)tv_idx->vval.v_number;
1977 blob_T *blob = tv_dest->vval.v_blob;
1978 varnumber_T nr;
1979 int error = FALSE;
1980 int len;
1981
1982 if (blob == NULL)
1983 {
1984 emsg(_(e_blob_not_set));
1985 return FAIL;
1986 }
1987 len = blob_len(blob);
1988 if (lidx < 0 && len + lidx >= 0)
1989 // negative index is relative to the end
1990 lidx = len + lidx;
1991
1992 // Can add one byte at the end.
1993 if (lidx < 0 || lidx > len)
1994 {
1995 semsg(_(e_blob_index_out_of_range_nr), lidx);
1996 return FAIL;
1997 }
1998 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1999 return FAIL;
2000 nr = tv_get_number_chk(tv, &error);
2001 if (error)
2002 return FAIL;
2003 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002004 }
2005 else
2006 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002007 status = FAIL;
2008 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002009 }
2010 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002011
2012 clear_tv(tv_idx);
2013 clear_tv(tv_dest);
2014 ectx->ec_stack.ga_len -= 3;
2015 if (status == FAIL)
2016 {
2017 clear_tv(tv);
2018 return FAIL;
2019 }
2020 return OK;
2021}
2022
2023/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002024 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002025 */
2026 static int
2027execute_storerange(isn_T *iptr, ectx_T *ectx)
2028{
2029 typval_T *tv;
2030 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2031 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2032 typval_T *tv_dest = STACK_TV_BOT(-1);
2033 int status = OK;
2034
2035 // Stack contains:
2036 // -4 value to be stored
2037 // -3 first index or "none"
2038 // -2 second index or "none"
2039 // -1 destination list or blob
2040 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002041 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002042 if (tv_dest->v_type == VAR_LIST)
2043 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002044 long n1;
2045 long n2;
2046 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002047
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002048 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2049 if (tv_idx2->v_type == VAR_SPECIAL
2050 && tv_idx2->vval.v_number == VVAL_NONE)
2051 n2 = list_len(tv_dest->vval.v_list) - 1;
2052 else
2053 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2054
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002055 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002056 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002057 status = FAIL;
2058 else
2059 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002060 status = check_range_index_two(tv_dest->vval.v_list,
2061 &n1, li1, &n2, FALSE);
2062 if (status != FAIL)
2063 status = list_assign_range(
2064 tv_dest->vval.v_list,
2065 tv->vval.v_list,
2066 n1,
2067 n2,
2068 tv_idx2->v_type == VAR_SPECIAL,
2069 (char_u *)"=",
2070 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002071 }
2072 }
2073 else if (tv_dest->v_type == VAR_BLOB)
2074 {
2075 varnumber_T n1;
2076 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002077 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002078
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002079 n1 = tv_get_number_chk(tv_idx1, NULL);
2080 if (tv_idx2->v_type == VAR_SPECIAL
2081 && tv_idx2->vval.v_number == VVAL_NONE)
2082 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2083 else
2084 n2 = tv_get_number_chk(tv_idx2, NULL);
2085 bloblen = blob_len(tv_dest->vval.v_blob);
2086
2087 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2088 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002089 status = FAIL;
2090 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002091 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002092 }
2093 else
2094 {
2095 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002096 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002097 }
2098
2099 clear_tv(tv_idx1);
2100 clear_tv(tv_idx2);
2101 clear_tv(tv_dest);
2102 ectx->ec_stack.ga_len -= 4;
2103 clear_tv(tv);
2104
2105 return status;
2106}
2107
2108/*
2109 * Unlet item in list or dict variable.
2110 */
2111 static int
2112execute_unletindex(isn_T *iptr, ectx_T *ectx)
2113{
2114 typval_T *tv_idx = STACK_TV_BOT(-2);
2115 typval_T *tv_dest = STACK_TV_BOT(-1);
2116 int status = OK;
2117
2118 // Stack contains:
2119 // -2 index
2120 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002121 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002122 if (tv_dest->v_type == VAR_DICT)
2123 {
2124 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002125 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002126 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002127 semsg(_(e_expected_str_but_got_str),
2128 vartype_name(VAR_STRING),
2129 vartype_name(tv_idx->v_type));
2130 status = FAIL;
2131 }
2132 else
2133 {
2134 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002135 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002136 dictitem_T *di = NULL;
2137
2138 if (d != NULL && value_check_lock(
2139 d->dv_lock, NULL, FALSE))
2140 status = FAIL;
2141 else
2142 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002143 if (tv_idx->v_type == VAR_STRING)
2144 {
2145 key = tv_idx->vval.v_string;
2146 if (key == NULL)
2147 key = (char_u *)"";
2148 }
2149 else
2150 {
2151 key = tv_get_string(tv_idx);
2152 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002153 if (d != NULL)
2154 di = dict_find(d, key, (int)STRLEN(key));
2155 if (di == NULL)
2156 {
2157 // NULL dict is equivalent to empty dict
2158 semsg(_(e_key_not_present_in_dictionary),
2159 key);
2160 status = FAIL;
2161 }
2162 else if (var_check_fixed(di->di_flags,
2163 NULL, FALSE)
2164 || var_check_ro(di->di_flags,
2165 NULL, FALSE))
2166 status = FAIL;
2167 else
2168 dictitem_remove(d, di);
2169 }
2170 }
2171 }
2172 else if (tv_dest->v_type == VAR_LIST)
2173 {
2174 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002175 if (check_for_number(tv_idx) == FAIL)
2176 {
2177 status = FAIL;
2178 }
2179 else
2180 {
2181 list_T *l = tv_dest->vval.v_list;
2182 long n = (long)tv_idx->vval.v_number;
2183
2184 if (l != NULL && value_check_lock(
2185 l->lv_lock, NULL, FALSE))
2186 status = FAIL;
2187 else
2188 {
2189 listitem_T *li = list_find(l, n);
2190
2191 if (li == NULL)
2192 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002193 semsg(_(e_list_index_out_of_range_nr), n);
2194 status = FAIL;
2195 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002196 else
2197 listitem_remove(l, li);
2198 }
2199 }
2200 }
2201 else
2202 {
2203 status = FAIL;
2204 semsg(_(e_cannot_index_str),
2205 vartype_name(tv_dest->v_type));
2206 }
2207
2208 clear_tv(tv_idx);
2209 clear_tv(tv_dest);
2210 ectx->ec_stack.ga_len -= 2;
2211
2212 return status;
2213}
2214
2215/*
2216 * Unlet a range of items in a list variable.
2217 */
2218 static int
2219execute_unletrange(isn_T *iptr, ectx_T *ectx)
2220{
2221 // Stack contains:
2222 // -3 index1
2223 // -2 index2
2224 // -1 dict or list
2225 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2226 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2227 typval_T *tv_dest = STACK_TV_BOT(-1);
2228 int status = OK;
2229
2230 if (tv_dest->v_type == VAR_LIST)
2231 {
2232 // indexes must be a number
2233 SOURCING_LNUM = iptr->isn_lnum;
2234 if (check_for_number(tv_idx1) == FAIL
2235 || (tv_idx2->v_type != VAR_SPECIAL
2236 && check_for_number(tv_idx2) == FAIL))
2237 {
2238 status = FAIL;
2239 }
2240 else
2241 {
2242 list_T *l = tv_dest->vval.v_list;
2243 long n1 = (long)tv_idx1->vval.v_number;
2244 long n2 = tv_idx2->v_type == VAR_SPECIAL
2245 ? 0 : (long)tv_idx2->vval.v_number;
2246 listitem_T *li;
2247
2248 li = list_find_index(l, &n1);
2249 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002250 {
2251 semsg(_(e_list_index_out_of_range_nr),
2252 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002253 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002254 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002255 else
2256 {
2257 if (n1 < 0)
2258 n1 = list_idx_of_item(l, li);
2259 if (n2 < 0)
2260 {
2261 listitem_T *li2 = list_find(l, n2);
2262
2263 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002264 {
2265 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002266 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002267 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002268 else
2269 n2 = list_idx_of_item(l, li2);
2270 }
2271 if (status != FAIL
2272 && tv_idx2->v_type != VAR_SPECIAL
2273 && n2 < n1)
2274 {
2275 semsg(_(e_list_index_out_of_range_nr), n2);
2276 status = FAIL;
2277 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002278 if (status != FAIL)
2279 list_unlet_range(l, li, n1,
2280 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002281 }
2282 }
2283 }
2284 else
2285 {
2286 status = FAIL;
2287 SOURCING_LNUM = iptr->isn_lnum;
2288 semsg(_(e_cannot_index_str),
2289 vartype_name(tv_dest->v_type));
2290 }
2291
2292 clear_tv(tv_idx1);
2293 clear_tv(tv_idx2);
2294 clear_tv(tv_dest);
2295 ectx->ec_stack.ga_len -= 3;
2296
2297 return status;
2298}
2299
2300/*
2301 * Top of a for loop.
2302 */
2303 static int
2304execute_for(isn_T *iptr, ectx_T *ectx)
2305{
2306 typval_T *tv;
2307 typval_T *ltv = STACK_TV_BOT(-1);
2308 typval_T *idxtv =
2309 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2310
2311 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2312 return FAIL;
2313 if (ltv->v_type == VAR_LIST)
2314 {
2315 list_T *list = ltv->vval.v_list;
2316
2317 // push the next item from the list
2318 ++idxtv->vval.v_number;
2319 if (list == NULL
2320 || idxtv->vval.v_number >= list->lv_len)
2321 {
2322 // past the end of the list, jump to "endfor"
2323 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2324 may_restore_cmdmod(&ectx->ec_funclocal);
2325 }
2326 else if (list->lv_first == &range_list_item)
2327 {
2328 // non-materialized range() list
2329 tv = STACK_TV_BOT(0);
2330 tv->v_type = VAR_NUMBER;
2331 tv->v_lock = 0;
2332 tv->vval.v_number = list_find_nr(
2333 list, idxtv->vval.v_number, NULL);
2334 ++ectx->ec_stack.ga_len;
2335 }
2336 else
2337 {
2338 listitem_T *li = list_find(list,
2339 idxtv->vval.v_number);
2340
2341 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2342 ++ectx->ec_stack.ga_len;
2343 }
2344 }
2345 else if (ltv->v_type == VAR_STRING)
2346 {
2347 char_u *str = ltv->vval.v_string;
2348
2349 // The index is for the last byte of the previous
2350 // character.
2351 ++idxtv->vval.v_number;
2352 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2353 {
2354 // past the end of the string, jump to "endfor"
2355 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2356 may_restore_cmdmod(&ectx->ec_funclocal);
2357 }
2358 else
2359 {
2360 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2361
2362 // Push the next character from the string.
2363 tv = STACK_TV_BOT(0);
2364 tv->v_type = VAR_STRING;
2365 tv->vval.v_string = vim_strnsave(
2366 str + idxtv->vval.v_number, clen);
2367 ++ectx->ec_stack.ga_len;
2368 idxtv->vval.v_number += clen - 1;
2369 }
2370 }
2371 else if (ltv->v_type == VAR_BLOB)
2372 {
2373 blob_T *blob = ltv->vval.v_blob;
2374
2375 // When we get here the first time make a copy of the
2376 // blob, so that the iteration still works when it is
2377 // changed.
2378 if (idxtv->vval.v_number == -1 && blob != NULL)
2379 {
2380 blob_copy(blob, ltv);
2381 blob_unref(blob);
2382 blob = ltv->vval.v_blob;
2383 }
2384
2385 // The index is for the previous byte.
2386 ++idxtv->vval.v_number;
2387 if (blob == NULL
2388 || idxtv->vval.v_number >= blob_len(blob))
2389 {
2390 // past the end of the blob, jump to "endfor"
2391 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2392 may_restore_cmdmod(&ectx->ec_funclocal);
2393 }
2394 else
2395 {
2396 // Push the next byte from the blob.
2397 tv = STACK_TV_BOT(0);
2398 tv->v_type = VAR_NUMBER;
2399 tv->vval.v_number = blob_get(blob,
2400 idxtv->vval.v_number);
2401 ++ectx->ec_stack.ga_len;
2402 }
2403 }
2404 else
2405 {
2406 semsg(_(e_for_loop_on_str_not_supported),
2407 vartype_name(ltv->v_type));
2408 return FAIL;
2409 }
2410 return OK;
2411}
2412
2413/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002414 * Load instruction for w:/b:/g:/t: variable.
2415 * "isn_type" is used instead of "iptr->isn_type".
2416 */
2417 static int
2418load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2419{
2420 dictitem_T *di = NULL;
2421 hashtab_T *ht = NULL;
2422 char namespace;
2423
2424 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2425 return NOTDONE;
2426 switch (isn_type)
2427 {
2428 case ISN_LOADG:
2429 ht = get_globvar_ht();
2430 namespace = 'g';
2431 break;
2432 case ISN_LOADB:
2433 ht = &curbuf->b_vars->dv_hashtab;
2434 namespace = 'b';
2435 break;
2436 case ISN_LOADW:
2437 ht = &curwin->w_vars->dv_hashtab;
2438 namespace = 'w';
2439 break;
2440 case ISN_LOADT:
2441 ht = &curtab->tp_vars->dv_hashtab;
2442 namespace = 't';
2443 break;
2444 default: // Cannot reach here
2445 return NOTDONE;
2446 }
2447 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2448
Bram Moolenaar06b77222022-01-25 15:51:56 +00002449 if (di == NULL)
2450 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002451 if (isn_type == ISN_LOADG)
2452 {
2453 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2454
2455 // g:Something could be a function
2456 if (ufunc != NULL)
2457 {
2458 typval_T *tv = STACK_TV_BOT(0);
2459
2460 ++ectx->ec_stack.ga_len;
2461 tv->v_type = VAR_FUNC;
2462 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2463 if (tv->vval.v_string == NULL)
2464 return FAIL;
2465 STRCPY(tv->vval.v_string, "g:");
2466 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2467 return OK;
2468 }
2469 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002470 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002471 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002472 // no check if the item exists in the script but
2473 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002474 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002475 else
2476 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002477 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002478 return FAIL;
2479 }
2480 else
2481 {
2482 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2483 ++ectx->ec_stack.ga_len;
2484 }
2485 return OK;
2486}
2487
2488/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002489 * Execute instructions in execution context "ectx".
2490 * Return OK or FAIL;
2491 */
2492 static int
2493exec_instructions(ectx_T *ectx)
2494{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002495 int ret = FAIL;
2496 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002497 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002498
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002499 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002500 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002501
Bram Moolenaarff652882021-05-16 15:24:49 +02002502 // Only catch exceptions in this instruction list.
2503 ectx->ec_trylevel_at_start = trylevel;
2504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 for (;;)
2506 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002507 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002509 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002510
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002511 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002512 {
2513 line_breakcheck();
2514 breakcheck_count = 0;
2515 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002516 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002517 {
2518 // Turn CTRL-C into an exception.
2519 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002520 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002521 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002522 did_throw = TRUE;
2523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002525 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002526 {
2527 // Turn an error message into an exception.
2528 did_emsg = FALSE;
2529 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002530 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002531 did_throw = TRUE;
2532 *msg_list = NULL;
2533 }
2534
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002535 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002537 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002538 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002539 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540
2541 // An exception jumps to the first catch, finally, or returns from
2542 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002543 while (index > 0)
2544 {
2545 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002546 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002547 break;
2548 // In the catch and finally block of this try we have to go up
2549 // one level.
2550 --index;
2551 trycmd = NULL;
2552 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002553 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002555 if (trycmd->tcd_in_catch)
2556 {
2557 // exception inside ":catch", jump to ":finally" once
2558 ectx->ec_iidx = trycmd->tcd_finally_idx;
2559 trycmd->tcd_finally_idx = 0;
2560 }
2561 else
2562 // jump to first ":catch"
2563 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002564 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002565 did_throw = FALSE; // don't come back here until :endtry
2566 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 }
2568 else
2569 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002570 // Not inside try or need to return from current functions.
2571 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002572 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002573 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002574 tv = STACK_TV_BOT(0);
2575 tv->v_type = VAR_NUMBER;
2576 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002577 ++ectx->ec_stack.ga_len;
2578 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002580 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002581 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002582 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002583 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 goto done;
2585 }
2586
Bram Moolenaar4c137212021-04-19 16:48:48 +02002587 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002588 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 }
2590 continue;
2591 }
2592
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002593 /*
2594 * Big switch on the instruction. Most compilers will be turning this
2595 * into an efficient lookup table, since the "case" values are an enum
2596 * with sequential numbers. It may look ugly, but it should be the
2597 * most efficient way.
2598 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002599 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 switch (iptr->isn_type)
2601 {
2602 // execute Ex command line
2603 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002604 if (exec_command(iptr) == FAIL)
2605 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 break;
2607
Bram Moolenaar20677332021-06-06 17:02:53 +02002608 // execute Ex command line split at NL characters.
2609 case ISN_EXEC_SPLIT:
2610 {
2611 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002612 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002613
2614 SOURCING_LNUM = iptr->isn_lnum;
2615 CLEAR_FIELD(cookie);
2616 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2617 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002618 line = get_split_sourceline(0, &cookie, 0, 0);
2619 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002620 get_split_sourceline, &cookie,
2621 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2622 == FAIL
2623 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002624 {
2625 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002626 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002627 }
2628 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002629 }
2630 break;
2631
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002632 // execute Ex command line that is only a range
2633 case ISN_EXECRANGE:
2634 {
2635 exarg_T ea;
2636 char *error = NULL;
2637
2638 CLEAR_FIELD(ea);
2639 ea.cmdidx = CMD_SIZE;
2640 ea.addr_type = ADDR_LINES;
2641 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002642 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002643 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002644 if (ea.cmd == NULL)
2645 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002646 // error is always NULL when using ADDR_LINES
2647 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002648 if (error != NULL)
2649 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002650 emsg(error);
2651 goto on_error;
2652 }
2653 }
2654 break;
2655
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002656 // Evaluate an expression with legacy syntax, push it onto the
2657 // stack.
2658 case ISN_LEGACY_EVAL:
2659 {
2660 char_u *arg = iptr->isn_arg.string;
2661 int res;
2662 int save_flags = cmdmod.cmod_flags;
2663
Bram Moolenaar35578162021-08-02 19:10:38 +02002664 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002665 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002666 tv = STACK_TV_BOT(0);
2667 init_tv(tv);
2668 cmdmod.cmod_flags |= CMOD_LEGACY;
2669 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2670 cmdmod.cmod_flags = save_flags;
2671 if (res == FAIL)
2672 goto on_error;
2673 ++ectx->ec_stack.ga_len;
2674 }
2675 break;
2676
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002677 // push typeval VAR_INSTR with instructions to be executed
2678 case ISN_INSTR:
2679 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002680 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002681 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002682 tv = STACK_TV_BOT(0);
2683 tv->vval.v_instr = ALLOC_ONE(instr_T);
2684 if (tv->vval.v_instr == NULL)
2685 goto on_error;
2686 ++ectx->ec_stack.ga_len;
2687
2688 tv->v_type = VAR_INSTR;
2689 tv->vval.v_instr->instr_ectx = ectx;
2690 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2691 }
2692 break;
2693
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002694 case ISN_SOURCE:
2695 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002696 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002697
Bram Moolenaar89445512022-04-14 12:58:23 +01002698 SOURCING_LNUM = iptr->isn_lnum;
2699 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002700 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002701 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002702 }
2703 break;
2704
Bram Moolenaar4c137212021-04-19 16:48:48 +02002705 // execute :substitute with an expression
2706 case ISN_SUBSTITUTE:
2707 {
2708 subs_T *subs = &iptr->isn_arg.subs;
2709 source_cookie_T cookie;
2710 struct subs_expr_S *save_instr = substitute_instr;
2711 struct subs_expr_S subs_instr;
2712 int res;
2713
2714 subs_instr.subs_ectx = ectx;
2715 subs_instr.subs_instr = subs->subs_instr;
2716 subs_instr.subs_status = OK;
2717 substitute_instr = &subs_instr;
2718
2719 SOURCING_LNUM = iptr->isn_lnum;
2720 // This is very much like ISN_EXEC
2721 CLEAR_FIELD(cookie);
2722 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2723 res = do_cmdline(subs->subs_cmd,
2724 getsourceline, &cookie,
2725 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2726 substitute_instr = save_instr;
2727
2728 if (res == FAIL || did_emsg
2729 || subs_instr.subs_status == FAIL)
2730 goto on_error;
2731 }
2732 break;
2733
2734 case ISN_FINISH:
2735 goto done;
2736
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002737 case ISN_REDIRSTART:
2738 // create a dummy entry for var_redir_str()
2739 if (alloc_redir_lval() == FAIL)
2740 goto on_error;
2741
2742 // The output is stored in growarray "redir_ga" until
2743 // redirection ends.
2744 init_redir_ga();
2745 redir_vname = 1;
2746 break;
2747
2748 case ISN_REDIREND:
2749 {
2750 char_u *res = get_clear_redir_ga();
2751
2752 // End redirection, put redirected text on the stack.
2753 clear_redir_lval();
2754 redir_vname = 0;
2755
Bram Moolenaar35578162021-08-02 19:10:38 +02002756 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002757 {
2758 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002759 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002760 }
2761 tv = STACK_TV_BOT(0);
2762 tv->v_type = VAR_STRING;
2763 tv->vval.v_string = res;
2764 ++ectx->ec_stack.ga_len;
2765 }
2766 break;
2767
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002768 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002769#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002770 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002771 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2772 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002773 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002774#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002775 break;
2776
2777 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002778#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002779 {
2780 exarg_T ea;
2781 int res;
2782
2783 CLEAR_FIELD(ea);
2784 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2785 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2786 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2787 --ectx->ec_stack.ga_len;
2788 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01002789 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002790 res = cexpr_core(&ea, tv);
2791 clear_tv(tv);
2792 if (res == FAIL)
2793 goto on_error;
2794 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002795#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002796 break;
2797
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002798 // execute Ex command from pieces on the stack
2799 case ISN_EXECCONCAT:
2800 {
2801 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002802 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002803 int pass;
2804 int i;
2805 char_u *cmd = NULL;
2806 char_u *str;
2807
2808 for (pass = 1; pass <= 2; ++pass)
2809 {
2810 for (i = 0; i < count; ++i)
2811 {
2812 tv = STACK_TV_BOT(i - count);
2813 str = tv->vval.v_string;
2814 if (str != NULL && *str != NUL)
2815 {
2816 if (pass == 2)
2817 STRCPY(cmd + len, str);
2818 len += STRLEN(str);
2819 }
2820 if (pass == 2)
2821 clear_tv(tv);
2822 }
2823 if (pass == 1)
2824 {
2825 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002826 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002827 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002828 len = 0;
2829 }
2830 }
2831
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002832 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002833 do_cmdline_cmd(cmd);
2834 vim_free(cmd);
2835 }
2836 break;
2837
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 // execute :echo {string} ...
2839 case ISN_ECHO:
2840 {
2841 int count = iptr->isn_arg.echo.echo_count;
2842 int atstart = TRUE;
2843 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002844 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845
2846 for (idx = 0; idx < count; ++idx)
2847 {
2848 tv = STACK_TV_BOT(idx - count);
2849 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2850 &atstart, &needclr);
2851 clear_tv(tv);
2852 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002853 if (needclr)
2854 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002855 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 }
2857 break;
2858
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002859 // :execute {string} ...
2860 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002861 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002862 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002863 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002864 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002865 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002866 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002867 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002868 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002869 {
2870 int count = iptr->isn_arg.number;
2871 garray_T ga;
2872 char_u buf[NUMBUFLEN];
2873 char_u *p;
2874 int len;
2875 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002876 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002877
2878 ga_init2(&ga, 1, 80);
2879 for (idx = 0; idx < count; ++idx)
2880 {
2881 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002882 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002883 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002884 if (tv->v_type == VAR_CHANNEL
2885 || tv->v_type == VAR_JOB)
2886 {
2887 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002888 semsg(_(e_using_invalid_value_as_string_str),
2889 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002890 break;
2891 }
2892 else
2893 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002894 }
2895 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002896 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002897
2898 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002899 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002900 failed = TRUE;
2901 else
2902 {
2903 if (ga.ga_len > 0)
2904 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2905 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2906 ga.ga_len += len;
2907 }
2908 clear_tv(tv);
2909 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002910 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002911 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002912 {
2913 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002914 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002915 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002916
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002917 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002918 {
2919 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002920 {
2921 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002922 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002923 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002924 {
2925 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002926 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002927 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002928 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002929 else
2930 {
2931 msg_sb_eol();
2932 if (iptr->isn_type == ISN_ECHOMSG)
2933 {
2934 msg_attr(ga.ga_data, echo_attr);
2935 out_flush();
2936 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01002937#ifdef HAS_MESSAGE_WINDOW
2938 else if (iptr->isn_type == ISN_ECHOWINDOW)
2939 {
2940 start_echowindow();
2941 msg_attr(ga.ga_data, echo_attr);
2942 end_echowindow();
2943 }
2944#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02002945 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2946 {
2947 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2948 TRUE);
2949 ui_write((char_u *)"\r\n", 2, TRUE);
2950 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002951 else
2952 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002953 SOURCING_LNUM = iptr->isn_lnum;
2954 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002955 }
2956 }
2957 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002958 ga_clear(&ga);
2959 }
2960 break;
2961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 // load local variable or argument
2963 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002964 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002965 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002967 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 break;
2969
2970 // load v: variable
2971 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002972 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002973 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002975 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 break;
2977
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002978 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 case ISN_LOADSCRIPT:
2980 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002981 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 svar_T *sv;
2983
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002984 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002985 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002986 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002987 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002988 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002989 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002991 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 }
2993 break;
2994
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002995 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002997 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002999 int sid = iptr->isn_arg.loadstore.ls_sid;
3000 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003001 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 if (di == NULL)
3005 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003006 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003007 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003008 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 }
3010 else
3011 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003012 if (iptr->isn_type == ISN_LOADEXPORT)
3013 {
3014 int idx = get_script_item_idx(sid, name, 0,
3015 NULL, NULL);
3016 svar_T *sv;
3017
3018 if (idx >= 0)
3019 {
3020 sv = ((svar_T *)SCRIPT_ITEM(sid)
3021 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003022 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003023 {
3024 SOURCING_LNUM = iptr->isn_lnum;
3025 semsg(_(e_item_not_exported_in_script_str),
3026 name);
3027 goto on_error;
3028 }
3029 }
3030 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003031 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003032 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003034 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 }
3036 }
3037 break;
3038
Bram Moolenaard3aac292020-04-19 14:32:17 +02003039 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003041 case ISN_LOADB:
3042 case ISN_LOADW:
3043 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003045 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003046
Bram Moolenaar06b77222022-01-25 15:51:56 +00003047 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003048 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003049 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003050 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 break;
3054
Bram Moolenaar03290b82020-12-19 16:30:44 +01003055 // load autoload variable
3056 case ISN_LOADAUTO:
3057 {
3058 char_u *name = iptr->isn_arg.string;
3059
Bram Moolenaar35578162021-08-02 19:10:38 +02003060 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003061 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003062 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003063 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003064 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003065 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003066 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003067 }
3068 break;
3069
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003070 // load g:/b:/w:/t: namespace
3071 case ISN_LOADGDICT:
3072 case ISN_LOADBDICT:
3073 case ISN_LOADWDICT:
3074 case ISN_LOADTDICT:
3075 {
3076 dict_T *d = NULL;
3077
3078 switch (iptr->isn_type)
3079 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003080 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3081 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3082 case ISN_LOADWDICT: d = curwin->w_vars; break;
3083 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003084 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003085 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003086 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003087 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003088 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003089 tv = STACK_TV_BOT(0);
3090 tv->v_type = VAR_DICT;
3091 tv->v_lock = 0;
3092 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003093 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003094 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003095 }
3096 break;
3097
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 // load &option
3099 case ISN_LOADOPT:
3100 {
3101 typval_T optval;
3102 char_u *name = iptr->isn_arg.string;
3103
Bram Moolenaara8c17702020-04-01 21:17:24 +02003104 // This is not expected to fail, name is checked during
3105 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003106 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003107 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003108 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003109 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003111 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 }
3113 break;
3114
3115 // load $ENV
3116 case ISN_LOADENV:
3117 {
3118 typval_T optval;
3119 char_u *name = iptr->isn_arg.string;
3120
Bram Moolenaar35578162021-08-02 19:10:38 +02003121 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003122 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003123 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003124 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003126 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 }
3128 break;
3129
3130 // load @register
3131 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003132 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003133 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 tv = STACK_TV_BOT(0);
3135 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003136 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003137 // This may result in NULL, which should be equivalent to an
3138 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 tv->vval.v_string = get_reg_contents(
3140 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003141 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 break;
3143
3144 // store local variable
3145 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003146 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 tv = STACK_TV_VAR(iptr->isn_arg.number);
3148 clear_tv(tv);
3149 *tv = *STACK_TV_BOT(0);
3150 break;
3151
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003152 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003153 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003154 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003155 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003156 int sid = iptr->isn_arg.loadstore.ls_sid;
3157 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003158 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003159 dictitem_T *di = find_var_in_ht(ht, 0,
3160 iptr->isn_type == ISN_STORES
3161 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003162
Bram Moolenaar4c137212021-04-19 16:48:48 +02003163 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003164 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003165 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003166 {
3167 if (iptr->isn_type == ISN_STOREEXPORT)
3168 {
3169 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003170 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003171 goto on_error;
3172 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003173 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003174 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003175 else
3176 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003177 if (iptr->isn_type == ISN_STOREEXPORT)
3178 {
3179 int idx = get_script_item_idx(sid, name, 0,
3180 NULL, NULL);
3181
Bram Moolenaar06651632022-04-27 17:54:25 +01003182 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003183 if (idx >= 0)
3184 {
3185 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3186 ->sn_var_vals.ga_data) + idx;
3187
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003188 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003189 {
3190 semsg(_(e_item_not_exported_in_script_str),
3191 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003192 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003193 goto on_error;
3194 }
3195 }
3196 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003197 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003198 {
3199 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003200 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003201 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003202 clear_tv(&di->di_tv);
3203 di->di_tv = *STACK_TV_BOT(0);
3204 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003205 }
3206 break;
3207
3208 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 case ISN_STORESCRIPT:
3210 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003211 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3212 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003214 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003215 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003216 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003217 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003218
3219 // "const" and "final" are checked at compile time, locking
3220 // the value needs to be checked here.
3221 SOURCING_LNUM = iptr->isn_lnum;
3222 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003223 {
3224 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003225 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003226 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 clear_tv(sv->sv_tv);
3229 *sv->sv_tv = *STACK_TV_BOT(0);
3230 }
3231 break;
3232
3233 // store option
3234 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003235 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003237 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3238 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 long n = 0;
3240 char_u *s = NULL;
3241 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003242 char_u numbuf[NUMBUFLEN];
3243 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244
Bram Moolenaar4c137212021-04-19 16:48:48 +02003245 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 tv = STACK_TV_BOT(0);
3247 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003248 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003250 if (s == NULL)
3251 s = (char_u *)"";
3252 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003253 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3254 {
3255 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003256 // If the option can be set to a function reference or
3257 // a lambda and the passed value is a function
3258 // reference, then convert it to the name (string) of
3259 // the function reference.
3260 s = tv2string(tv, &tofree, numbuf, 0);
3261 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003262 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003263 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003264 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003265 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003266 goto on_error;
3267 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003270 // must be VAR_NUMBER, CHECKTYPE makes sure
3271 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003272 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003273 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003274 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 if (msg != NULL)
3276 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003277 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003279 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 }
3282 break;
3283
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003284 // store $ENV
3285 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003286 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003287 tv = STACK_TV_BOT(0);
3288 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3289 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003290 break;
3291
3292 // store @r
3293 case ISN_STOREREG:
3294 {
3295 int reg = iptr->isn_arg.number;
3296
Bram Moolenaar4c137212021-04-19 16:48:48 +02003297 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003298 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003299 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003300 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003301 }
3302 break;
3303
3304 // store v: variable
3305 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003306 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003307 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3308 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003309 // should not happen, type is checked when compiling
3310 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003311 break;
3312
Bram Moolenaard3aac292020-04-19 14:32:17 +02003313 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003315 case ISN_STOREB:
3316 case ISN_STOREW:
3317 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003319 dictitem_T *di;
3320 hashtab_T *ht;
3321 char_u *name = iptr->isn_arg.string + 2;
3322
Bram Moolenaard3aac292020-04-19 14:32:17 +02003323 switch (iptr->isn_type)
3324 {
3325 case ISN_STOREG:
3326 ht = get_globvar_ht();
3327 break;
3328 case ISN_STOREB:
3329 ht = &curbuf->b_vars->dv_hashtab;
3330 break;
3331 case ISN_STOREW:
3332 ht = &curwin->w_vars->dv_hashtab;
3333 break;
3334 case ISN_STORET:
3335 ht = &curtab->tp_vars->dv_hashtab;
3336 break;
3337 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003338 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003339 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340
Bram Moolenaar4c137212021-04-19 16:48:48 +02003341 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003342 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003344 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 else
3346 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003347 SOURCING_LNUM = iptr->isn_lnum;
3348 if (var_check_permission(di, name) == FAIL)
3349 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 clear_tv(&di->di_tv);
3351 di->di_tv = *STACK_TV_BOT(0);
3352 }
3353 }
3354 break;
3355
Bram Moolenaar03290b82020-12-19 16:30:44 +01003356 // store an autoload variable
3357 case ISN_STOREAUTO:
3358 SOURCING_LNUM = iptr->isn_lnum;
3359 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3360 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003361 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003362 break;
3363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 // store number in local variable
3365 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003366 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 clear_tv(tv);
3368 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003369 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 break;
3371
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003372 // store value in list or dict variable
3373 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003374 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003375 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003376
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003377 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003378 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003379 if (res == NOTDONE)
3380 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003381 }
3382 break;
3383
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003384 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003385 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003386 if (execute_storerange(iptr, ectx) == FAIL)
3387 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003388 break;
3389
Bram Moolenaar0186e582021-01-10 18:33:11 +01003390 // load or store variable or argument from outer scope
3391 case ISN_LOADOUTER:
3392 case ISN_STOREOUTER:
3393 {
3394 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003395 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3396 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003397
3398 while (depth > 1 && outer != NULL)
3399 {
3400 outer = outer->out_up;
3401 --depth;
3402 }
3403 if (outer == NULL)
3404 {
3405 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003406 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3407 || ectx->ec_outer_ref == NULL)
3408 // Possibly :def function called from legacy
3409 // context.
3410 emsg(_(e_closure_called_from_invalid_context));
3411 else
3412 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003413 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003414 }
3415 tv = ((typval_T *)outer->out_stack->ga_data)
3416 + outer->out_frame_idx + STACK_FRAME_SIZE
3417 + iptr->isn_arg.outer.outer_idx;
3418 if (iptr->isn_type == ISN_LOADOUTER)
3419 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003420 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003421 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003422 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003423 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003424 }
3425 else
3426 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003427 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003428 clear_tv(tv);
3429 *tv = *STACK_TV_BOT(0);
3430 }
3431 }
3432 break;
3433
Bram Moolenaar752fc692021-01-04 21:57:11 +01003434 // unlet item in list or dict variable
3435 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003436 if (execute_unletindex(iptr, ectx) == FAIL)
3437 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003438 break;
3439
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003440 // unlet range of items in list variable
3441 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003442 if (execute_unletrange(iptr, ectx) == FAIL)
3443 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003444 break;
3445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 // push constant
3447 case ISN_PUSHNR:
3448 case ISN_PUSHBOOL:
3449 case ISN_PUSHSPEC:
3450 case ISN_PUSHF:
3451 case ISN_PUSHS:
3452 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003453 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003454 case ISN_PUSHCHANNEL:
3455 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003456 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003457 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003459 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003460 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 switch (iptr->isn_type)
3462 {
3463 case ISN_PUSHNR:
3464 tv->v_type = VAR_NUMBER;
3465 tv->vval.v_number = iptr->isn_arg.number;
3466 break;
3467 case ISN_PUSHBOOL:
3468 tv->v_type = VAR_BOOL;
3469 tv->vval.v_number = iptr->isn_arg.number;
3470 break;
3471 case ISN_PUSHSPEC:
3472 tv->v_type = VAR_SPECIAL;
3473 tv->vval.v_number = iptr->isn_arg.number;
3474 break;
3475#ifdef FEAT_FLOAT
3476 case ISN_PUSHF:
3477 tv->v_type = VAR_FLOAT;
3478 tv->vval.v_float = iptr->isn_arg.fnumber;
3479 break;
3480#endif
3481 case ISN_PUSHBLOB:
3482 blob_copy(iptr->isn_arg.blob, tv);
3483 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003484 case ISN_PUSHFUNC:
3485 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003486 if (iptr->isn_arg.string == NULL)
3487 tv->vval.v_string = NULL;
3488 else
3489 tv->vval.v_string =
3490 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003491 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003492 case ISN_PUSHCHANNEL:
3493#ifdef FEAT_JOB_CHANNEL
3494 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003495 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003496#endif
3497 break;
3498 case ISN_PUSHJOB:
3499#ifdef FEAT_JOB_CHANNEL
3500 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003501 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003502#endif
3503 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 default:
3505 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003506 tv->vval.v_string = iptr->isn_arg.string == NULL
3507 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 }
3509 break;
3510
Bram Moolenaar06b77222022-01-25 15:51:56 +00003511 case ISN_AUTOLOAD:
3512 {
3513 char_u *name = iptr->isn_arg.string;
3514
3515 (void)script_autoload(name, FALSE);
3516 if (find_func(name, TRUE))
3517 {
3518 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3519 goto theend;
3520 tv = STACK_TV_BOT(0);
3521 tv->v_lock = 0;
3522 ++ectx->ec_stack.ga_len;
3523 tv->v_type = VAR_FUNC;
3524 tv->vval.v_string = vim_strsave(name);
3525 }
3526 else
3527 {
3528 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3529
3530 if (res == NOTDONE)
3531 goto theend;
3532 if (res == FAIL)
3533 goto on_error;
3534 }
3535 }
3536 break;
3537
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003538 case ISN_UNLET:
3539 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3540 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003541 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003542 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003543 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003544 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003545 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003546
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003547 case ISN_LOCKUNLOCK:
3548 {
3549 typval_T *lval_root_save = lval_root;
3550 int res;
3551
3552 // Stack has the local variable, argument the whole :lock
3553 // or :unlock command, like ISN_EXEC.
3554 --ectx->ec_stack.ga_len;
3555 lval_root = STACK_TV_BOT(0);
3556 res = exec_command(iptr);
3557 clear_tv(lval_root);
3558 lval_root = lval_root_save;
3559 if (res == FAIL)
3560 goto on_error;
3561 }
3562 break;
3563
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003564 case ISN_LOCKCONST:
3565 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3566 break;
3567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 // create a list from items on the stack; uses a single allocation
3569 // for the list header and the items
3570 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003571 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003572 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 break;
3574
3575 // create a dict from items on the stack
3576 case ISN_NEWDICT:
3577 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003578 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003580 SOURCING_LNUM = iptr->isn_lnum;
3581 res = exe_newdict(iptr->isn_arg.number, ectx);
3582 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003583 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003584 if (res == MAYBE)
3585 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 }
3587 break;
3588
LemonBoy372bcce2022-04-25 12:43:20 +01003589 case ISN_CONCAT:
3590 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3591 goto theend;
3592 break;
3593
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003594 // create a partial with NULL value
3595 case ISN_NEWPARTIAL:
3596 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3597 goto theend;
3598 ++ectx->ec_stack.ga_len;
3599 tv = STACK_TV_BOT(-1);
3600 tv->v_type = VAR_PARTIAL;
3601 tv->v_lock = 0;
3602 tv->vval.v_partial = NULL;
3603 break;
3604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 // call a :def function
3606 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003607 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003608 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3609 NULL,
3610 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003611 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003612 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 break;
3614
3615 // call a builtin function
3616 case ISN_BCALL:
3617 SOURCING_LNUM = iptr->isn_lnum;
3618 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3619 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003620 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003621 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 break;
3623
3624 // call a funcref or partial
3625 case ISN_PCALL:
3626 {
3627 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3628 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003629 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630
3631 SOURCING_LNUM = iptr->isn_lnum;
3632 if (pfunc->cpf_top)
3633 {
3634 // funcref is above the arguments
3635 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3636 }
3637 else
3638 {
3639 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003640 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003641 partial_tv = *STACK_TV_BOT(0);
3642 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003644 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003645 if (tv == &partial_tv)
3646 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003648 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 }
3650 break;
3651
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003652 case ISN_PCALL_END:
3653 // PCALL finished, arguments have been consumed and replaced by
3654 // the return value. Now clear the funcref from the stack,
3655 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003656 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003657 clear_tv(STACK_TV_BOT(-1));
3658 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3659 break;
3660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 // call a user defined function or funcref/partial
3662 case ISN_UCALL:
3663 {
3664 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3665
3666 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003667 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003668 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003669 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 }
3671 break;
3672
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003673 // return from a :def function call without a value
3674 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003675 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003676 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003677 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003678 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003679 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003680 tv->vval.v_number = 0;
3681 tv->v_lock = 0;
3682 // FALLTHROUGH
3683
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003684 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 case ISN_RETURN:
3686 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003687 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003688 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003689
3690 if (trystack->ga_len > 0)
3691 trycmd = ((trycmd_T *)trystack->ga_data)
3692 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003693 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003694 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003696 // jump to ":finally" or ":endtry"
3697 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003698 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003699 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003700 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 trycmd->tcd_return = TRUE;
3702 }
3703 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003704 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 }
3706 break;
3707
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003708 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 case ISN_FUNCREF:
3710 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003711 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003712 ufunc_T *ufunc;
3713 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003716 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003717 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003718 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003719 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003720 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003721 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003722 if (funcref->fr_func_name == NULL)
3723 {
3724 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3725 + funcref->fr_dfunc_idx;
3726
3727 ufunc = pt_dfunc->df_ufunc;
3728 }
3729 else
3730 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003731 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003732 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003733 if (ufunc == NULL)
3734 {
3735 SOURCING_LNUM = iptr->isn_lnum;
3736 iemsg("ufunc unexpectedly NULL for FUNCREF");
3737 goto theend;
3738 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003739 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003740 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003742 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 tv->vval.v_partial = pt;
3744 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003745 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 }
3747 break;
3748
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003749 // Create a global function from a lambda.
3750 case ISN_NEWFUNC:
3751 {
3752 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3753
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003754 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003755 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003756 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003757 }
3758 break;
3759
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003760 // List functions
3761 case ISN_DEF:
3762 if (iptr->isn_arg.string == NULL)
3763 list_functions(NULL);
3764 else
3765 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003766 exarg_T ea;
3767 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003768
3769 CLEAR_FIELD(ea);
3770 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003771 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3772 define_function(&ea, NULL, &lines_to_free);
3773 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003774 }
3775 break;
3776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 // jump if a condition is met
3778 case ISN_JUMP:
3779 {
3780 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003781 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 int jump = TRUE;
3783
3784 if (when != JUMP_ALWAYS)
3785 {
3786 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003787 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003788 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003789 || when == JUMP_IF_COND_TRUE)
3790 {
3791 SOURCING_LNUM = iptr->isn_lnum;
3792 jump = tv_get_bool_chk(tv, &error);
3793 if (error)
3794 goto on_error;
3795 }
3796 else
3797 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01003798 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003800 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 {
3802 // drop the value from the stack
3803 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003804 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 }
3806 }
3807 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003808 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 }
3810 break;
3811
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003812 // Jump if an argument with a default value was already set and not
3813 // v:none.
3814 case ISN_JUMP_IF_ARG_SET:
3815 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3816 if (tv->v_type != VAR_UNKNOWN
3817 && !(tv->v_type == VAR_SPECIAL
3818 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003819 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003820 break;
3821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 // top of a for loop
3823 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003824 if (execute_for(iptr, ectx) == FAIL)
3825 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 break;
3827
3828 // start of ":try" block
3829 case ISN_TRY:
3830 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003831 trycmd_T *trycmd = NULL;
3832
Bram Moolenaar35578162021-08-02 19:10:38 +02003833 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003834 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003835 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3836 + ectx->ec_trystack.ga_len;
3837 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003839 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003840 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3841 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003842 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003843 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003844 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003845 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003846 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003847 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 }
3849 break;
3850
3851 case ISN_PUSHEXC:
3852 if (current_exception == NULL)
3853 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003854 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003856 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003858 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003859 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003861 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003863 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 tv->vval.v_string = vim_strsave(
3865 (char_u *)current_exception->value);
3866 break;
3867
3868 case ISN_CATCH:
3869 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003870 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01003871 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872
Bram Moolenaar4c137212021-04-19 16:48:48 +02003873 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01003874 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01003876 trycmd->tcd_caught = TRUE;
3877 trycmd->tcd_did_throw = FALSE;
3878
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003880 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 catch_exception(current_exception);
3882 }
3883 break;
3884
Bram Moolenaarc150c092021-02-13 15:02:46 +01003885 case ISN_TRYCONT:
3886 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003887 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003888 trycont_T *trycont = &iptr->isn_arg.trycont;
3889 int i;
3890 trycmd_T *trycmd;
3891 int iidx = trycont->tct_where;
3892
3893 if (trystack->ga_len < trycont->tct_levels)
3894 {
3895 siemsg("TRYCONT: expected %d levels, found %d",
3896 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003897 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003898 }
3899 // Make :endtry jump to any outer try block and the last
3900 // :endtry inside the loop to the loop start.
3901 for (i = trycont->tct_levels; i > 0; --i)
3902 {
3903 trycmd = ((trycmd_T *)trystack->ga_data)
3904 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003905 // Add one to tcd_cont to be able to jump to
3906 // instruction with index zero.
3907 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003908 iidx = trycmd->tcd_finally_idx == 0
3909 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003910 }
3911 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003912 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003913 }
3914 break;
3915
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003916 case ISN_FINALLY:
3917 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003918 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003919 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3920 + trystack->ga_len - 1;
3921
3922 // Reset the index to avoid a return statement jumps here
3923 // again.
3924 trycmd->tcd_finally_idx = 0;
3925 break;
3926 }
3927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 // end of ":try" block
3929 case ISN_ENDTRY:
3930 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003931 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01003932 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933
Bram Moolenaar06651632022-04-27 17:54:25 +01003934 --trystack->ga_len;
3935 --trylevel;
3936 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
3937 if (trycmd->tcd_did_throw)
3938 did_throw = TRUE;
3939 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 {
Bram Moolenaar06651632022-04-27 17:54:25 +01003941 // discard the exception
3942 if (caught_stack == current_exception)
3943 caught_stack = caught_stack->caught;
3944 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 }
Bram Moolenaar06651632022-04-27 17:54:25 +01003946
3947 if (trycmd->tcd_return)
3948 goto func_return;
3949
3950 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
3951 {
3952 --ectx->ec_stack.ga_len;
3953 clear_tv(STACK_TV_BOT(0));
3954 }
3955 if (trycmd->tcd_cont != 0)
3956 // handling :continue: jump to outer try block or
3957 // start of the loop
3958 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 }
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
Bram Moolenaar06651632022-04-27 17:54:25 +01004024 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4025 res = arg1 == arg2;
4026 else
4027 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028
Bram Moolenaar4c137212021-04-19 16:48:48 +02004029 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 tv1->v_type = VAR_BOOL;
4031 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4032 }
4033 break;
4034
Bram Moolenaar7a222242022-03-01 19:23:24 +00004035 case ISN_COMPARENULL:
4036 {
4037 typval_T *tv1 = STACK_TV_BOT(-2);
4038 typval_T *tv2 = STACK_TV_BOT(-1);
4039 int res;
4040
4041 res = typval_compare_null(tv1, tv2);
4042 if (res == MAYBE)
4043 goto on_error;
4044 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4045 res = !res;
4046 clear_tv(tv1);
4047 clear_tv(tv2);
4048 --ectx->ec_stack.ga_len;
4049 tv1->v_type = VAR_BOOL;
4050 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4051 }
4052 break;
4053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 // Operation with two number arguments
4055 case ISN_OPNR:
4056 case ISN_COMPARENR:
4057 {
4058 typval_T *tv1 = STACK_TV_BOT(-2);
4059 typval_T *tv2 = STACK_TV_BOT(-1);
4060 varnumber_T arg1 = tv1->vval.v_number;
4061 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004062 varnumber_T res = 0;
4063 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004065 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4066 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4067 {
4068 if (arg2 < 0)
4069 {
4070 SOURCING_LNUM = iptr->isn_lnum;
4071 emsg(_(e_bitshift_ops_must_be_postive));
4072 goto on_error;
4073 }
4074 }
4075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 switch (iptr->isn_arg.op.op_type)
4077 {
4078 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004079 case EXPR_DIV: if (arg2 == 0)
4080 div_zero = TRUE;
4081 else
4082 res = arg1 / arg2;
4083 break;
4084 case EXPR_REM: if (arg2 == 0)
4085 div_zero = TRUE;
4086 else
4087 res = arg1 % arg2;
4088 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 case EXPR_SUB: res = arg1 - arg2; break;
4090 case EXPR_ADD: res = arg1 + arg2; break;
4091
4092 case EXPR_EQUAL: res = arg1 == arg2; break;
4093 case EXPR_NEQUAL: res = arg1 != arg2; break;
4094 case EXPR_GREATER: res = arg1 > arg2; break;
4095 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4096 case EXPR_SMALLER: res = arg1 < arg2; break;
4097 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004098 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4099 res = 0;
4100 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004101 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004102 break;
4103 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4104 res = 0;
4105 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004106 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004107 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004108 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 }
4110
Bram Moolenaar4c137212021-04-19 16:48:48 +02004111 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 if (iptr->isn_type == ISN_COMPARENR)
4113 {
4114 tv1->v_type = VAR_BOOL;
4115 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4116 }
4117 else
4118 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004119 if (div_zero)
4120 {
4121 SOURCING_LNUM = iptr->isn_lnum;
4122 emsg(_(e_divide_by_zero));
4123 goto on_error;
4124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 }
4126 break;
4127
4128 // Computation with two float arguments
4129 case ISN_OPFLOAT:
4130 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004131#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 {
4133 typval_T *tv1 = STACK_TV_BOT(-2);
4134 typval_T *tv2 = STACK_TV_BOT(-1);
4135 float_T arg1 = tv1->vval.v_float;
4136 float_T arg2 = tv2->vval.v_float;
4137 float_T res = 0;
4138 int cmp = FALSE;
4139
4140 switch (iptr->isn_arg.op.op_type)
4141 {
4142 case EXPR_MULT: res = arg1 * arg2; break;
4143 case EXPR_DIV: res = arg1 / arg2; break;
4144 case EXPR_SUB: res = arg1 - arg2; break;
4145 case EXPR_ADD: res = arg1 + arg2; break;
4146
4147 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4148 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4149 case EXPR_GREATER: cmp = arg1 > arg2; break;
4150 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4151 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4152 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4153 default: cmp = 0; break;
4154 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004155 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 if (iptr->isn_type == ISN_COMPAREFLOAT)
4157 {
4158 tv1->v_type = VAR_BOOL;
4159 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4160 }
4161 else
4162 tv1->vval.v_float = res;
4163 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004164#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 break;
4166
4167 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004168 case ISN_COMPAREDICT:
4169 case ISN_COMPAREFUNC:
4170 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 case ISN_COMPAREBLOB:
4172 {
4173 typval_T *tv1 = STACK_TV_BOT(-2);
4174 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004175 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4176 int ic = iptr->isn_arg.op.op_ic;
4177 int res = FALSE;
4178 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179
Bram Moolenaar265f8112021-12-19 12:33:05 +00004180 SOURCING_LNUM = iptr->isn_lnum;
4181 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004183 status = typval_compare_list(tv1, tv2,
4184 exprtype, ic, &res);
4185 }
4186 else if (iptr->isn_type == ISN_COMPAREDICT)
4187 {
4188 status = typval_compare_dict(tv1, tv2,
4189 exprtype, ic, &res);
4190 }
4191 else if (iptr->isn_type == ISN_COMPAREFUNC)
4192 {
4193 status = typval_compare_func(tv1, tv2,
4194 exprtype, ic, &res);
4195 }
4196 else if (iptr->isn_type == ISN_COMPARESTRING)
4197 {
4198 status = typval_compare_string(tv1, tv2,
4199 exprtype, ic, &res);
4200 }
4201 else
4202 {
4203 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004205 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 clear_tv(tv1);
4207 clear_tv(tv2);
4208 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004209 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4210 if (status == FAIL)
4211 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 }
4213 break;
4214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 case ISN_COMPAREANY:
4216 {
4217 typval_T *tv1 = STACK_TV_BOT(-2);
4218 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004219 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004221 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222
Bram Moolenaareb26f432020-09-14 16:50:05 +02004223 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004224 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004226 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004227 if (status == FAIL)
4228 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 }
4230 break;
4231
4232 case ISN_ADDLIST:
4233 case ISN_ADDBLOB:
4234 {
4235 typval_T *tv1 = STACK_TV_BOT(-2);
4236 typval_T *tv2 = STACK_TV_BOT(-1);
4237
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004238 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004240 {
4241 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4242 && tv1->vval.v_list != NULL)
4243 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4244 NULL);
4245 else
4246 eval_addlist(tv1, tv2);
4247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 else
4249 eval_addblob(tv1, tv2);
4250 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004251 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 }
4253 break;
4254
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004255 case ISN_LISTAPPEND:
4256 {
4257 typval_T *tv1 = STACK_TV_BOT(-2);
4258 typval_T *tv2 = STACK_TV_BOT(-1);
4259 list_T *l = tv1->vval.v_list;
4260
4261 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004262 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004263 if (l == NULL)
4264 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004265 emsg(_(e_cannot_add_to_null_list));
4266 goto on_error;
4267 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004268 if (value_check_lock(l->lv_lock, NULL, FALSE))
4269 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004270 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004271 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004272 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004273 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004274 }
4275 break;
4276
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004277 case ISN_BLOBAPPEND:
4278 {
4279 typval_T *tv1 = STACK_TV_BOT(-2);
4280 typval_T *tv2 = STACK_TV_BOT(-1);
4281 blob_T *b = tv1->vval.v_blob;
4282 int error = FALSE;
4283 varnumber_T n;
4284
4285 // add a number to a blob
4286 if (b == NULL)
4287 {
4288 SOURCING_LNUM = iptr->isn_lnum;
4289 emsg(_(e_cannot_add_to_null_blob));
4290 goto on_error;
4291 }
4292 n = tv_get_number_chk(tv2, &error);
4293 if (error)
4294 goto on_error;
4295 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004296 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004297 }
4298 break;
4299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 // Computation with two arguments of unknown type
4301 case ISN_OPANY:
4302 {
4303 typval_T *tv1 = STACK_TV_BOT(-2);
4304 typval_T *tv2 = STACK_TV_BOT(-1);
4305 varnumber_T n1, n2;
4306#ifdef FEAT_FLOAT
4307 float_T f1 = 0, f2 = 0;
4308#endif
4309 int error = FALSE;
4310
4311 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4312 {
4313 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4314 {
4315 eval_addlist(tv1, tv2);
4316 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004317 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 break;
4319 }
4320 else if (tv1->v_type == VAR_BLOB
4321 && tv2->v_type == VAR_BLOB)
4322 {
4323 eval_addblob(tv1, tv2);
4324 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004325 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 break;
4327 }
4328 }
4329#ifdef FEAT_FLOAT
4330 if (tv1->v_type == VAR_FLOAT)
4331 {
4332 f1 = tv1->vval.v_float;
4333 n1 = 0;
4334 }
4335 else
4336#endif
4337 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004338 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 n1 = tv_get_number_chk(tv1, &error);
4340 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004341 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342#ifdef FEAT_FLOAT
4343 if (tv2->v_type == VAR_FLOAT)
4344 f1 = n1;
4345#endif
4346 }
4347#ifdef FEAT_FLOAT
4348 if (tv2->v_type == VAR_FLOAT)
4349 {
4350 f2 = tv2->vval.v_float;
4351 n2 = 0;
4352 }
4353 else
4354#endif
4355 {
4356 n2 = tv_get_number_chk(tv2, &error);
4357 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004358 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359#ifdef FEAT_FLOAT
4360 if (tv1->v_type == VAR_FLOAT)
4361 f2 = n2;
4362#endif
4363 }
4364#ifdef FEAT_FLOAT
4365 // if there is a float on either side the result is a float
4366 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4367 {
4368 switch (iptr->isn_arg.op.op_type)
4369 {
4370 case EXPR_MULT: f1 = f1 * f2; break;
4371 case EXPR_DIV: f1 = f1 / f2; break;
4372 case EXPR_SUB: f1 = f1 - f2; break;
4373 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004374 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004375 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004376 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 }
4378 clear_tv(tv1);
4379 clear_tv(tv2);
4380 tv1->v_type = VAR_FLOAT;
4381 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004382 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 }
4384 else
4385#endif
4386 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004387 int failed = FALSE;
4388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 switch (iptr->isn_arg.op.op_type)
4390 {
4391 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004392 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4393 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004394 goto on_error;
4395 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 case EXPR_SUB: n1 = n1 - n2; break;
4397 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004398 default: n1 = num_modulus(n1, n2, &failed);
4399 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004400 goto on_error;
4401 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 }
4403 clear_tv(tv1);
4404 clear_tv(tv2);
4405 tv1->v_type = VAR_NUMBER;
4406 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004407 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 }
4409 }
4410 break;
4411
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004412 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004413 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004414 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004415 int is_slice = iptr->isn_type == ISN_STRSLICE;
4416 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004417 char_u *res;
4418
4419 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004420 // string slice: string is at stack-3, first index at
4421 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004422 if (is_slice)
4423 {
4424 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004425 n1 = tv->vval.v_number;
4426 }
4427
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004428 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004429 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004430
Bram Moolenaar4c137212021-04-19 16:48:48 +02004431 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004432 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004433 if (is_slice)
4434 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004435 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004436 else
4437 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004438 // single character (including composing characters).
4439 // If the index is too big or negative the result is
4440 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004441 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004442 vim_free(tv->vval.v_string);
4443 tv->vval.v_string = res;
4444 }
4445 break;
4446
4447 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004448 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004449 case ISN_BLOBINDEX:
4450 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004452 int is_slice = iptr->isn_type == ISN_LISTSLICE
4453 || iptr->isn_type == ISN_BLOBSLICE;
4454 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4455 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004456 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004457 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458
4459 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004460 // list slice: list is at stack-3, indexes at stack-2 and
4461 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004462 // Same for blob.
4463 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464
4465 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004466 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004468
4469 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 {
Bram Moolenaared591872020-08-15 22:14:53 +02004471 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004472 n1 = tv->vval.v_number;
4473 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 }
Bram Moolenaared591872020-08-15 22:14:53 +02004475
Bram Moolenaar4c137212021-04-19 16:48:48 +02004476 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004477 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004478 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004479 if (is_blob)
4480 {
4481 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4482 n1, n2, FALSE, tv) == FAIL)
4483 goto on_error;
4484 }
4485 else
4486 {
4487 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4488 n1, n2, FALSE, tv, TRUE) == FAIL)
4489 goto on_error;
4490 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 }
4492 break;
4493
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004494 case ISN_ANYINDEX:
4495 case ISN_ANYSLICE:
4496 {
4497 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4498 typval_T *var1, *var2;
4499 int res;
4500
4501 // index: composite is at stack-2, index at stack-1
4502 // slice: composite is at stack-3, indexes at stack-2 and
4503 // stack-1
4504 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004505 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004506 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4507 goto on_error;
4508 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4509 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004510 res = eval_index_inner(tv, is_slice, var1, var2,
4511 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004512 clear_tv(var1);
4513 if (is_slice)
4514 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004515 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004516 if (res == FAIL)
4517 goto on_error;
4518 }
4519 break;
4520
Bram Moolenaar9af78762020-06-16 11:34:42 +02004521 case ISN_SLICE:
4522 {
4523 list_T *list;
4524 int count = iptr->isn_arg.number;
4525
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004526 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004527 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004528 list = tv->vval.v_list;
4529
4530 // no error for short list, expect it to be checked earlier
4531 if (list != NULL && list->lv_len >= count)
4532 {
4533 list_T *newlist = list_slice(list,
4534 count, list->lv_len - 1);
4535
4536 if (newlist != NULL)
4537 {
4538 list_unref(list);
4539 tv->vval.v_list = newlist;
4540 ++newlist->lv_refcount;
4541 }
4542 }
4543 }
4544 break;
4545
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004546 case ISN_GETITEM:
4547 {
4548 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004549 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004550
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004551 // Get list item: list is at stack-1, push item.
4552 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004553 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4554 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004555
Bram Moolenaar35578162021-08-02 19:10:38 +02004556 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004557 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004558 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004559 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004560
4561 // Useful when used in unpack assignment. Reset at
4562 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004563 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004564 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004565 }
4566 break;
4567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 case ISN_MEMBER:
4569 {
4570 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004571 char_u *key;
4572 dictitem_T *di;
4573
4574 // dict member: dict is at stack-2, key at stack-1
4575 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004576 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004577 dict = tv->vval.v_dict;
4578
4579 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004580 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004581 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004582 if (key == NULL)
4583 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004584
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004585 if ((di = dict_find(dict, key, -1)) == NULL)
4586 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004587 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004588 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004589
4590 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004591 // stack contents makes sense and the dict stack is
4592 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004593 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004594 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004595 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004596 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004597 tv->v_type = VAR_NUMBER;
4598 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004599 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004600 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004601 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004602 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004603 // Put the dict used on the dict stack, it might be used by
4604 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004605 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004606 if (dict_stack_save(tv) == FAIL)
4607 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004608 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004609 }
4610 break;
4611
4612 // dict member with string key
4613 case ISN_STRINGMEMBER:
4614 {
4615 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 dictitem_T *di;
4617
4618 tv = STACK_TV_BOT(-1);
4619 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4620 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004621 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004622 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004623 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 }
4625 dict = tv->vval.v_dict;
4626
4627 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4628 == NULL)
4629 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004630 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004631 semsg(_(e_key_not_present_in_dictionary),
4632 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004633 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004635 // Put the dict used on the dict stack, it might be used by
4636 // a dict function later.
4637 if (dict_stack_save(tv) == FAIL)
4638 goto on_fatal_error;
4639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004641 }
4642 break;
4643
4644 case ISN_CLEARDICT:
4645 dict_stack_drop();
4646 break;
4647
4648 case ISN_USEDICT:
4649 {
4650 typval_T *dict_tv = dict_stack_get_tv();
4651
4652 // Turn "dict.Func" into a partial for "Func" bound to
4653 // "dict". Don't do this when "Func" is already a partial
4654 // that was bound explicitly (pt_auto is FALSE).
4655 tv = STACK_TV_BOT(-1);
4656 if (dict_tv != NULL
4657 && dict_tv->v_type == VAR_DICT
4658 && dict_tv->vval.v_dict != NULL
4659 && (tv->v_type == VAR_FUNC
4660 || (tv->v_type == VAR_PARTIAL
4661 && (tv->vval.v_partial->pt_auto
4662 || tv->vval.v_partial->pt_dict == NULL))))
4663 dict_tv->vval.v_dict =
4664 make_partial(dict_tv->vval.v_dict, tv);
4665 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 }
4667 break;
4668
4669 case ISN_NEGATENR:
4670 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004671 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004672#ifdef FEAT_FLOAT
4673 if (tv->v_type == VAR_FLOAT)
4674 tv->vval.v_float = -tv->vval.v_float;
4675 else
4676#endif
4677 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 break;
4679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 case ISN_CHECKTYPE:
4681 {
4682 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004683 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004684 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004686 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004687 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004688 if (!ectx->ec_where.wt_variable)
4689 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004690 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004691 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004692 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004693 if (r == FAIL)
4694 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004695 if (!ectx->ec_where.wt_variable)
4696 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004697
4698 // number 0 is FALSE, number 1 is TRUE
4699 if (tv->v_type == VAR_NUMBER
4700 && ct->ct_type->tt_type == VAR_BOOL
4701 && (tv->vval.v_number == 0
4702 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004704 tv->v_type = VAR_BOOL;
4705 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004706 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 }
4708 }
4709 break;
4710
Bram Moolenaar9af78762020-06-16 11:34:42 +02004711 case ISN_CHECKLEN:
4712 {
4713 int min_len = iptr->isn_arg.checklen.cl_min_len;
4714 list_T *list = NULL;
4715
4716 tv = STACK_TV_BOT(-1);
4717 if (tv->v_type == VAR_LIST)
4718 list = tv->vval.v_list;
4719 if (list == NULL || list->lv_len < min_len
4720 || (list->lv_len > min_len
4721 && !iptr->isn_arg.checklen.cl_more_OK))
4722 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004723 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004724 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004725 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004726 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004727 }
4728 }
4729 break;
4730
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004731 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004732 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004733 break;
4734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004736 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 {
4738 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004739 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004741 if (iptr->isn_type == ISN_2BOOL)
4742 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004743 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004744 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004745 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004746 n = !n;
4747 }
4748 else
4749 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004750 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004751 SOURCING_LNUM = iptr->isn_lnum;
4752 n = tv_get_bool_chk(tv, &error);
4753 if (error)
4754 goto on_error;
4755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756 clear_tv(tv);
4757 tv->v_type = VAR_BOOL;
4758 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4759 }
4760 break;
4761
4762 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004763 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004764 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004765 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4766 iptr->isn_type == ISN_2STRING_ANY,
4767 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004768 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 break;
4770
Bram Moolenaar08597872020-12-10 19:43:40 +01004771 case ISN_RANGE:
4772 {
4773 exarg_T ea;
4774 char *errormsg;
4775
Bram Moolenaarece0b872021-01-08 20:40:45 +01004776 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004777 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004778 ea.addr_type = ADDR_LINES;
4779 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004780 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004781 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004782 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004783
Bram Moolenaar35578162021-08-02 19:10:38 +02004784 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004785 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004786 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004787 tv = STACK_TV_BOT(-1);
4788 tv->v_type = VAR_NUMBER;
4789 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004790 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004791 }
4792 break;
4793
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004794 case ISN_PUT:
4795 {
4796 int regname = iptr->isn_arg.put.put_regname;
4797 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4798 char_u *expr = NULL;
4799 int dir = FORWARD;
4800
Bram Moolenaar08597872020-12-10 19:43:40 +01004801 if (lnum < -2)
4802 {
4803 // line number was put on the stack by ISN_RANGE
4804 tv = STACK_TV_BOT(-1);
4805 curwin->w_cursor.lnum = tv->vval.v_number;
4806 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4807 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004808 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004809 }
4810 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004811 // :put! above cursor
4812 dir = BACKWARD;
4813 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004814 {
4815 curwin->w_cursor.lnum = lnum;
4816 if (lnum == 0)
4817 // check_cursor() below will move to line 1
4818 dir = BACKWARD;
4819 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004820
4821 if (regname == '=')
4822 {
4823 tv = STACK_TV_BOT(-1);
4824 if (tv->v_type == VAR_STRING)
4825 expr = tv->vval.v_string;
4826 else
4827 {
4828 expr = typval2string(tv, TRUE); // allocates value
4829 clear_tv(tv);
4830 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004831 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004832 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004833 check_cursor();
4834 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4835 vim_free(expr);
4836 }
4837 break;
4838
Bram Moolenaar02194d22020-10-24 23:08:38 +02004839 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004840 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4841 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4842 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4843 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004844 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4845 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004846 break;
4847
Bram Moolenaar02194d22020-10-24 23:08:38 +02004848 case ISN_CMDMOD_REV:
4849 // filter regprog is owned by the instruction, don't free it
4850 cmdmod.cmod_filter_regmatch.regprog = NULL;
4851 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004852 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4853 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004854 break;
4855
Bram Moolenaar792f7862020-11-23 08:31:18 +01004856 case ISN_UNPACK:
4857 {
4858 int count = iptr->isn_arg.unpack.unp_count;
4859 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4860 list_T *l;
4861 listitem_T *li;
4862 int i;
4863
4864 // Check there is a valid list to unpack.
4865 tv = STACK_TV_BOT(-1);
4866 if (tv->v_type != VAR_LIST)
4867 {
4868 SOURCING_LNUM = iptr->isn_lnum;
4869 emsg(_(e_for_argument_must_be_sequence_of_lists));
4870 goto on_error;
4871 }
4872 l = tv->vval.v_list;
4873 if (l == NULL
4874 || l->lv_len < (semicolon ? count - 1 : count))
4875 {
4876 SOURCING_LNUM = iptr->isn_lnum;
4877 emsg(_(e_list_value_does_not_have_enough_items));
4878 goto on_error;
4879 }
4880 else if (!semicolon && l->lv_len > count)
4881 {
4882 SOURCING_LNUM = iptr->isn_lnum;
4883 emsg(_(e_list_value_has_more_items_than_targets));
4884 goto on_error;
4885 }
4886
4887 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004888 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004889 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004890 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004891
4892 // Variable after semicolon gets a list with the remaining
4893 // items.
4894 if (semicolon)
4895 {
4896 list_T *rem_list =
4897 list_alloc_with_items(l->lv_len - count + 1);
4898
4899 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004900 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004901 tv = STACK_TV_BOT(-count);
4902 tv->vval.v_list = rem_list;
4903 ++rem_list->lv_refcount;
4904 tv->v_lock = 0;
4905 li = l->lv_first;
4906 for (i = 0; i < count - 1; ++i)
4907 li = li->li_next;
4908 for (i = 0; li != NULL; ++i)
4909 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004910 typval_T tvcopy;
4911
4912 copy_tv(&li->li_tv, &tvcopy);
4913 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004914 li = li->li_next;
4915 }
4916 --count;
4917 }
4918
4919 // Produce the values in reverse order, first item last.
4920 li = l->lv_first;
4921 for (i = 0; i < count; ++i)
4922 {
4923 tv = STACK_TV_BOT(-i - 1);
4924 copy_tv(&li->li_tv, tv);
4925 li = li->li_next;
4926 }
4927
4928 list_unref(l);
4929 }
4930 break;
4931
Bram Moolenaarb2049902021-01-24 12:53:53 +01004932 case ISN_PROF_START:
4933 case ISN_PROF_END:
4934 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004935#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004936 funccall_T cookie;
4937 ufunc_T *cur_ufunc =
4938 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004939 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004940
4941 cookie.func = cur_ufunc;
4942 if (iptr->isn_type == ISN_PROF_START)
4943 {
4944 func_line_start(&cookie, iptr->isn_lnum);
4945 // if we get here the instruction is executed
4946 func_line_exec(&cookie);
4947 }
4948 else
4949 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004950#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004951 }
4952 break;
4953
Bram Moolenaare99d4222021-06-13 14:01:26 +02004954 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004955 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004956 break;
4957
Bram Moolenaar389df252020-07-09 21:20:47 +02004958 case ISN_SHUFFLE:
4959 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004960 typval_T tmp_tv;
4961 int item = iptr->isn_arg.shuffle.shfl_item;
4962 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004963
4964 tmp_tv = *STACK_TV_BOT(-item);
4965 for ( ; up > 0 && item > 1; --up)
4966 {
4967 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4968 --item;
4969 }
4970 *STACK_TV_BOT(-item) = tmp_tv;
4971 }
4972 break;
4973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004975 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004977 ectx->ec_where.wt_index = 0;
4978 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 break;
4980 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004981 continue;
4982
Bram Moolenaard032f342020-07-18 18:13:02 +02004983func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004984 // Restore previous function. If the frame pointer is where we started
4985 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004986 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004987 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004988
Bram Moolenaar4c137212021-04-19 16:48:48 +02004989 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004990 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004991 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004992 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004993
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004994on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004995 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004996 // If "emsg_silent" is set then ignore the error, unless it was set
4997 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004998 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004999 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005000 {
5001 // If a sequence of instructions causes an error while ":silent!"
5002 // was used, restore the stack length and jump ahead to restoring
5003 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005004 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005005 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005006 while (ectx->ec_stack.ga_len
5007 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005008 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005009 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005010 clear_tv(STACK_TV_BOT(0));
5011 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005012 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5013 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005014 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005015 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005016 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005017on_fatal_error:
5018 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005019 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005020 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005021 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022 }
5023
5024done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005025 ret = OK;
5026theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005027 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005028 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5029 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005030}
5031
5032/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005033 * Execute the instructions from a VAR_INSTR typeval and put the result in
5034 * "rettv".
5035 * Return OK or FAIL.
5036 */
5037 int
5038exe_typval_instr(typval_T *tv, typval_T *rettv)
5039{
5040 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5041 isn_T *save_instr = ectx->ec_instr;
5042 int save_iidx = ectx->ec_iidx;
5043 int res;
5044
LemonBoyf3b48952022-05-05 13:53:03 +01005045 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5046 // even when the compilation fails.
5047 rettv->v_type = VAR_UNKNOWN;
5048
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005049 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5050 res = exec_instructions(ectx);
5051 if (res == OK)
5052 {
5053 *rettv = *STACK_TV_BOT(-1);
5054 --ectx->ec_stack.ga_len;
5055 }
5056
5057 ectx->ec_instr = save_instr;
5058 ectx->ec_iidx = save_iidx;
5059
5060 return res;
5061}
5062
5063/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005064 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5065 * "substitute_instr".
5066 */
5067 char_u *
5068exe_substitute_instr(void)
5069{
5070 ectx_T *ectx = substitute_instr->subs_ectx;
5071 isn_T *save_instr = ectx->ec_instr;
5072 int save_iidx = ectx->ec_iidx;
5073 char_u *res;
5074
5075 ectx->ec_instr = substitute_instr->subs_instr;
5076 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005077 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005078 typval_T *tv = STACK_TV_BOT(-1);
5079
Bram Moolenaar27523602021-06-05 21:36:19 +02005080 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005081 --ectx->ec_stack.ga_len;
5082 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005083 }
5084 else
5085 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005086 substitute_instr->subs_status = FAIL;
5087 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089
Bram Moolenaar4c137212021-04-19 16:48:48 +02005090 ectx->ec_instr = save_instr;
5091 ectx->ec_iidx = save_iidx;
5092
5093 return res;
5094}
5095
5096/*
5097 * Call a "def" function from old Vim script.
5098 * Return OK or FAIL.
5099 */
5100 int
5101call_def_function(
5102 ufunc_T *ufunc,
5103 int argc_arg, // nr of arguments
5104 typval_T *argv, // arguments
5105 partial_T *partial, // optional partial for context
5106 typval_T *rettv) // return value
5107{
5108 ectx_T ectx; // execution context
5109 int argc = argc_arg;
5110 typval_T *tv;
5111 int idx;
5112 int ret = FAIL;
5113 int defcount = ufunc->uf_args.ga_len - argc;
5114 sctx_T save_current_sctx = current_sctx;
5115 int did_emsg_before = did_emsg_cumul + did_emsg;
5116 int save_suppress_errthrow = suppress_errthrow;
5117 msglist_T **saved_msg_list = NULL;
5118 msglist_T *private_msg_list = NULL;
5119 int save_emsg_silent_def = emsg_silent_def;
5120 int save_did_emsg_def = did_emsg_def;
5121 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005122 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005123
5124// Get pointer to item in the stack.
5125#undef STACK_TV
5126#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5127
5128// Get pointer to item at the bottom of the stack, -1 is the bottom.
5129#undef STACK_TV_BOT
5130#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5131
5132// Get pointer to a local variable on the stack. Negative for arguments.
5133#undef STACK_TV_VAR
5134#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5135
5136 if (ufunc->uf_def_status == UF_NOT_COMPILED
5137 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005138 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5139 && compile_def_function(ufunc, FALSE,
5140 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005141 {
5142 if (did_emsg_cumul + did_emsg == did_emsg_before)
5143 semsg(_(e_function_is_not_compiled_str),
5144 printable_func_name(ufunc));
5145 return FAIL;
5146 }
5147
5148 {
5149 // Check the function was really compiled.
5150 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5151 + ufunc->uf_dfunc_idx;
5152 if (INSTRUCTIONS(dfunc) == NULL)
5153 {
5154 iemsg("using call_def_function() on not compiled function");
5155 return FAIL;
5156 }
5157 }
5158
5159 // If depth of calling is getting too high, don't execute the function.
5160 orig_funcdepth = funcdepth_get();
5161 if (funcdepth_increment() == FAIL)
5162 return FAIL;
5163
5164 CLEAR_FIELD(ectx);
5165 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5166 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005167 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005168 {
5169 funcdepth_decrement();
5170 return FAIL;
5171 }
5172 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5173 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5174 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005175 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005176
5177 idx = argc - ufunc->uf_args.ga_len;
5178 if (idx > 0 && ufunc->uf_va_name == NULL)
5179 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005180 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
5181 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005182 goto failed_early;
5183 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005184 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5185 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005186 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005187 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
5188 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005189 goto failed_early;
5190 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005191
5192 // Put arguments on the stack, but no more than what the function expects.
5193 // A lambda can be called with more arguments than it uses.
5194 for (idx = 0; idx < argc
5195 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5196 ++idx)
5197 {
5198 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5199 && argv[idx].v_type == VAR_SPECIAL
5200 && argv[idx].vval.v_number == VVAL_NONE)
5201 {
5202 // Use the default value.
5203 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5204 }
5205 else
5206 {
5207 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5208 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005209 ufunc->uf_arg_types[idx], &argv[idx],
5210 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005211 goto failed_early;
5212 copy_tv(&argv[idx], STACK_TV_BOT(0));
5213 }
5214 ++ectx.ec_stack.ga_len;
5215 }
5216
5217 // Turn varargs into a list. Empty list if no args.
5218 if (ufunc->uf_va_name != NULL)
5219 {
5220 int vararg_count = argc - ufunc->uf_args.ga_len;
5221
5222 if (vararg_count < 0)
5223 vararg_count = 0;
5224 else
5225 argc -= vararg_count;
5226 if (exe_newlist(vararg_count, &ectx) == FAIL)
5227 goto failed_early;
5228
5229 // Check the type of the list items.
5230 tv = STACK_TV_BOT(-1);
5231 if (ufunc->uf_va_type != NULL
5232 && ufunc->uf_va_type != &t_list_any
5233 && ufunc->uf_va_type->tt_member != &t_any
5234 && tv->vval.v_list != NULL)
5235 {
5236 type_T *expected = ufunc->uf_va_type->tt_member;
5237 listitem_T *li = tv->vval.v_list->lv_first;
5238
5239 for (idx = 0; idx < vararg_count; ++idx)
5240 {
5241 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005242 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005243 goto failed_early;
5244 li = li->li_next;
5245 }
5246 }
5247
5248 if (defcount > 0)
5249 // Move varargs list to below missing default arguments.
5250 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5251 --ectx.ec_stack.ga_len;
5252 }
5253
5254 // Make space for omitted arguments, will store default value below.
5255 // Any varargs list goes after them.
5256 if (defcount > 0)
5257 for (idx = 0; idx < defcount; ++idx)
5258 {
5259 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5260 ++ectx.ec_stack.ga_len;
5261 }
5262 if (ufunc->uf_va_name != NULL)
5263 ++ectx.ec_stack.ga_len;
5264
5265 // Frame pointer points to just after arguments.
5266 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5267 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5268
5269 {
5270 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5271 + ufunc->uf_dfunc_idx;
5272 ufunc_T *base_ufunc = dfunc->df_ufunc;
5273
5274 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5275 // by copy_func().
5276 if (partial != NULL || base_ufunc->uf_partial != NULL)
5277 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005278 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5279 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005280 goto failed_early;
5281 if (partial != NULL)
5282 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005283 outer_T *outer = get_pt_outer(partial);
5284
5285 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005286 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005287 if (current_ectx != NULL)
5288 {
5289 if (current_ectx->ec_outer_ref != NULL
5290 && current_ectx->ec_outer_ref->or_outer != NULL)
5291 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005292 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005293 }
5294 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005295 }
5296 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005297 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005298 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005299 ++partial->pt_refcount;
5300 ectx.ec_outer_ref->or_partial = partial;
5301 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005302 }
5303 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005304 {
5305 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5306 ++base_ufunc->uf_partial->pt_refcount;
5307 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5308 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005309 }
5310 }
5311
5312 // dummy frame entries
5313 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5314 {
5315 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5316 ++ectx.ec_stack.ga_len;
5317 }
5318
5319 {
5320 // Reserve space for local variables and any closure reference count.
5321 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5322 + ufunc->uf_dfunc_idx;
5323
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005324 // Initialize variables to zero. That avoids having to generate
5325 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005326 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005327 {
5328 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5329 STACK_TV_VAR(idx)->vval.v_number = 0;
5330 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005331 ectx.ec_stack.ga_len += dfunc->df_varcount;
5332 if (dfunc->df_has_closure)
5333 {
5334 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5335 STACK_TV_VAR(idx)->vval.v_number = 0;
5336 ++ectx.ec_stack.ga_len;
5337 }
5338
5339 ectx.ec_instr = INSTRUCTIONS(dfunc);
5340 }
5341
5342 // Following errors are in the function, not the caller.
5343 // Commands behave like vim9script.
5344 estack_push_ufunc(ufunc, 1);
5345 current_sctx = ufunc->uf_script_ctx;
5346 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5347
5348 // Use a specific location for storing error messages to be converted to an
5349 // exception.
5350 saved_msg_list = msg_list;
5351 msg_list = &private_msg_list;
5352
5353 // Do turn errors into exceptions.
5354 suppress_errthrow = FALSE;
5355
5356 // Do not delete the function while executing it.
5357 ++ufunc->uf_calls;
5358
5359 // When ":silent!" was used before calling then we still abort the
5360 // function. If ":silent!" is used in the function then we don't.
5361 emsg_silent_def = emsg_silent;
5362 did_emsg_def = 0;
5363
5364 ectx.ec_where.wt_index = 0;
5365 ectx.ec_where.wt_variable = FALSE;
5366
5367 // Execute the instructions until done.
5368 ret = exec_instructions(&ectx);
5369 if (ret == OK)
5370 {
5371 // function finished, get result from the stack.
5372 if (ufunc->uf_ret_type == &t_void)
5373 {
5374 rettv->v_type = VAR_VOID;
5375 }
5376 else
5377 {
5378 tv = STACK_TV_BOT(-1);
5379 *rettv = *tv;
5380 tv->v_type = VAR_UNKNOWN;
5381 }
5382 }
5383
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005384 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005385 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5386 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005387
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005388 // Deal with any remaining closures, they may be in use somewhere.
5389 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005390 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005391 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005392 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005393 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005394
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005395 estack_pop();
5396 current_sctx = save_current_sctx;
5397
Bram Moolenaar2336c372021-12-06 15:06:54 +00005398 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5399 // Function was unreferenced while being used, free it now.
5400 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005401
Bram Moolenaar352134b2020-10-17 22:04:08 +02005402 if (*msg_list != NULL && saved_msg_list != NULL)
5403 {
5404 msglist_T **plist = saved_msg_list;
5405
5406 // Append entries from the current msg_list (uncaught exceptions) to
5407 // the saved msg_list.
5408 while (*plist != NULL)
5409 plist = &(*plist)->next;
5410
5411 *plist = *msg_list;
5412 }
5413 msg_list = saved_msg_list;
5414
Bram Moolenaar4c137212021-04-19 16:48:48 +02005415 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005416 {
5417 cmdmod.cmod_filter_regmatch.regprog = NULL;
5418 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005419 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005420 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005421 emsg_silent_def = save_emsg_silent_def;
5422 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005423
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005424failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005425 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5427 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005428 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005430 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005431 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005432 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005433 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005434 if (ectx.ec_outer_ref->or_outer_allocated)
5435 vim_free(ectx.ec_outer_ref->or_outer);
5436 partial_unref(ectx.ec_outer_ref->or_partial);
5437 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005438 }
5439
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005440 // Not sure if this is necessary.
5441 suppress_errthrow = save_suppress_errthrow;
5442
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005443 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5444 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005445 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005446 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005447 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448 return ret;
5449}
5450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005451/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005452 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5453 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5454 * "pfx" is prefixed to every line.
5455 */
5456 static void
5457list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5458{
5459 int line_idx = 0;
5460 int prev_current = 0;
5461 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005462 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005463
5464 for (current = 0; current < instr_count; ++current)
5465 {
5466 isn_T *iptr = &instr[current];
5467 char *line;
5468
5469 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005470 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005471 while (line_idx < iptr->isn_lnum
5472 && line_idx < ufunc->uf_lines.ga_len)
5473 {
5474 if (current > prev_current)
5475 {
5476 msg_puts("\n\n");
5477 prev_current = current;
5478 }
5479 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5480 if (line != NULL)
5481 msg(line);
5482 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005483 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5484 {
5485 int first_def_arg = ufunc->uf_args.ga_len
5486 - ufunc->uf_def_args.ga_len;
5487
5488 if (def_arg_idx > 0)
5489 msg_puts("\n\n");
5490 msg_start();
5491 msg_puts(" ");
5492 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5493 first_def_arg + def_arg_idx]);
5494 msg_puts(" = ");
5495 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5496 msg_clr_eos();
5497 msg_end();
5498 }
5499 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005500
5501 switch (iptr->isn_type)
5502 {
5503 case ISN_EXEC:
5504 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5505 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005506 case ISN_EXEC_SPLIT:
5507 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5508 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005509 case ISN_EXECRANGE:
5510 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5511 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005512 case ISN_LEGACY_EVAL:
5513 smsg("%s%4d EVAL legacy %s", pfx, current,
5514 iptr->isn_arg.string);
5515 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005516 case ISN_REDIRSTART:
5517 smsg("%s%4d REDIR", pfx, current);
5518 break;
5519 case ISN_REDIREND:
5520 smsg("%s%4d REDIR END%s", pfx, current,
5521 iptr->isn_arg.number ? " append" : "");
5522 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005523 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005524#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005525 smsg("%s%4d CEXPR pre %s", pfx, current,
5526 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005527#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005528 break;
5529 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005530#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005531 {
5532 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5533
5534 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5535 cexpr_get_auname(cer->cer_cmdidx),
5536 cer->cer_forceit ? "!" : "",
5537 cer->cer_cmdline);
5538 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005539#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005540 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005541 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005542 smsg("%s%4d INSTR", pfx, current);
5543 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5544 msg(" -------------");
5545 break;
5546 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005547 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005548 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5549
5550 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005551 }
5552 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005553 case ISN_SUBSTITUTE:
5554 {
5555 subs_T *subs = &iptr->isn_arg.subs;
5556
5557 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5558 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5559 msg(" -------------");
5560 }
5561 break;
5562 case ISN_EXECCONCAT:
5563 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5564 (varnumber_T)iptr->isn_arg.number);
5565 break;
5566 case ISN_ECHO:
5567 {
5568 echo_T *echo = &iptr->isn_arg.echo;
5569
5570 smsg("%s%4d %s %d", pfx, current,
5571 echo->echo_with_white ? "ECHO" : "ECHON",
5572 echo->echo_count);
5573 }
5574 break;
5575 case ISN_EXECUTE:
5576 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005577 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005578 break;
5579 case ISN_ECHOMSG:
5580 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005581 (varnumber_T)(iptr->isn_arg.number));
5582 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01005583 case ISN_ECHOWINDOW:
5584 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
5585 (varnumber_T)(iptr->isn_arg.number));
5586 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02005587 case ISN_ECHOCONSOLE:
5588 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5589 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005590 break;
5591 case ISN_ECHOERR:
5592 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005593 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005594 break;
5595 case ISN_LOAD:
5596 {
5597 if (iptr->isn_arg.number < 0)
5598 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5599 (varnumber_T)(iptr->isn_arg.number
5600 + STACK_FRAME_SIZE));
5601 else
5602 smsg("%s%4d LOAD $%lld", pfx, current,
5603 (varnumber_T)(iptr->isn_arg.number));
5604 }
5605 break;
5606 case ISN_LOADOUTER:
5607 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005608 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005609 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5610 iptr->isn_arg.outer.outer_depth,
5611 iptr->isn_arg.outer.outer_idx
5612 + STACK_FRAME_SIZE);
5613 else
5614 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5615 iptr->isn_arg.outer.outer_depth,
5616 iptr->isn_arg.outer.outer_idx);
5617 }
5618 break;
5619 case ISN_LOADV:
5620 smsg("%s%4d LOADV v:%s", pfx, current,
5621 get_vim_var_name(iptr->isn_arg.number));
5622 break;
5623 case ISN_LOADSCRIPT:
5624 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005625 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5626 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5627 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005628
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005629 sv = get_script_svar(sref, -1);
5630 if (sv == NULL)
5631 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5632 pfx, current, si->sn_name);
5633 else
5634 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005635 sv->sv_name,
5636 sref->sref_idx,
5637 si->sn_name);
5638 }
5639 break;
5640 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005641 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005642 {
5643 scriptitem_T *si = SCRIPT_ITEM(
5644 iptr->isn_arg.loadstore.ls_sid);
5645
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005646 smsg("%s%4d %s s:%s from %s", pfx, current,
5647 iptr->isn_type == ISN_LOADS ? "LOADS"
5648 : "LOADEXPORT",
5649 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005650 }
5651 break;
5652 case ISN_LOADAUTO:
5653 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5654 break;
5655 case ISN_LOADG:
5656 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5657 break;
5658 case ISN_LOADB:
5659 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5660 break;
5661 case ISN_LOADW:
5662 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5663 break;
5664 case ISN_LOADT:
5665 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5666 break;
5667 case ISN_LOADGDICT:
5668 smsg("%s%4d LOAD g:", pfx, current);
5669 break;
5670 case ISN_LOADBDICT:
5671 smsg("%s%4d LOAD b:", pfx, current);
5672 break;
5673 case ISN_LOADWDICT:
5674 smsg("%s%4d LOAD w:", pfx, current);
5675 break;
5676 case ISN_LOADTDICT:
5677 smsg("%s%4d LOAD t:", pfx, current);
5678 break;
5679 case ISN_LOADOPT:
5680 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5681 break;
5682 case ISN_LOADENV:
5683 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5684 break;
5685 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005686 smsg("%s%4d LOADREG @%c", pfx, current,
5687 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005688 break;
5689
5690 case ISN_STORE:
5691 if (iptr->isn_arg.number < 0)
5692 smsg("%s%4d STORE arg[%lld]", pfx, current,
5693 iptr->isn_arg.number + STACK_FRAME_SIZE);
5694 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005695 smsg("%s%4d STORE $%lld", pfx, current,
5696 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005697 break;
5698 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005699 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5700 iptr->isn_arg.outer.outer_depth,
5701 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005702 break;
5703 case ISN_STOREV:
5704 smsg("%s%4d STOREV v:%s", pfx, current,
5705 get_vim_var_name(iptr->isn_arg.number));
5706 break;
5707 case ISN_STOREAUTO:
5708 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5709 break;
5710 case ISN_STOREG:
5711 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5712 break;
5713 case ISN_STOREB:
5714 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5715 break;
5716 case ISN_STOREW:
5717 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5718 break;
5719 case ISN_STORET:
5720 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5721 break;
5722 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005723 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005724 {
5725 scriptitem_T *si = SCRIPT_ITEM(
5726 iptr->isn_arg.loadstore.ls_sid);
5727
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005728 smsg("%s%4d %s %s in %s", pfx, current,
5729 iptr->isn_type == ISN_STORES
5730 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005731 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5732 }
5733 break;
5734 case ISN_STORESCRIPT:
5735 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005736 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5737 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5738 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005739
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005740 sv = get_script_svar(sref, -1);
5741 if (sv == NULL)
5742 smsg("%s%4d STORESCRIPT [deleted] in %s",
5743 pfx, current, si->sn_name);
5744 else
5745 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005746 sv->sv_name,
5747 sref->sref_idx,
5748 si->sn_name);
5749 }
5750 break;
5751 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005752 case ISN_STOREFUNCOPT:
5753 smsg("%s%4d %s &%s", pfx, current,
5754 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005755 iptr->isn_arg.storeopt.so_name);
5756 break;
5757 case ISN_STOREENV:
5758 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5759 break;
5760 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005761 smsg("%s%4d STOREREG @%c", pfx, current,
5762 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005763 break;
5764 case ISN_STORENR:
5765 smsg("%s%4d STORE %lld in $%d", pfx, current,
5766 iptr->isn_arg.storenr.stnr_val,
5767 iptr->isn_arg.storenr.stnr_idx);
5768 break;
5769
5770 case ISN_STOREINDEX:
5771 smsg("%s%4d STOREINDEX %s", pfx, current,
5772 vartype_name(iptr->isn_arg.vartype));
5773 break;
5774
5775 case ISN_STORERANGE:
5776 smsg("%s%4d STORERANGE", pfx, current);
5777 break;
5778
5779 // constants
5780 case ISN_PUSHNR:
5781 smsg("%s%4d PUSHNR %lld", pfx, current,
5782 (varnumber_T)(iptr->isn_arg.number));
5783 break;
5784 case ISN_PUSHBOOL:
5785 case ISN_PUSHSPEC:
5786 smsg("%s%4d PUSH %s", pfx, current,
5787 get_var_special_name(iptr->isn_arg.number));
5788 break;
5789 case ISN_PUSHF:
5790#ifdef FEAT_FLOAT
5791 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5792#endif
5793 break;
5794 case ISN_PUSHS:
5795 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5796 break;
5797 case ISN_PUSHBLOB:
5798 {
5799 char_u *r;
5800 char_u numbuf[NUMBUFLEN];
5801 char_u *tofree;
5802
5803 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5804 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5805 vim_free(tofree);
5806 }
5807 break;
5808 case ISN_PUSHFUNC:
5809 {
5810 char *name = (char *)iptr->isn_arg.string;
5811
5812 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5813 name == NULL ? "[none]" : name);
5814 }
5815 break;
5816 case ISN_PUSHCHANNEL:
5817#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005818 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005819#endif
5820 break;
5821 case ISN_PUSHJOB:
5822#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005823 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005824#endif
5825 break;
5826 case ISN_PUSHEXC:
5827 smsg("%s%4d PUSH v:exception", pfx, current);
5828 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005829 case ISN_AUTOLOAD:
5830 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5831 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005832 case ISN_UNLET:
5833 smsg("%s%4d UNLET%s %s", pfx, current,
5834 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5835 iptr->isn_arg.unlet.ul_name);
5836 break;
5837 case ISN_UNLETENV:
5838 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5839 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5840 iptr->isn_arg.unlet.ul_name);
5841 break;
5842 case ISN_UNLETINDEX:
5843 smsg("%s%4d UNLETINDEX", pfx, current);
5844 break;
5845 case ISN_UNLETRANGE:
5846 smsg("%s%4d UNLETRANGE", pfx, current);
5847 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005848 case ISN_LOCKUNLOCK:
5849 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5850 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005851 case ISN_LOCKCONST:
5852 smsg("%s%4d LOCKCONST", pfx, current);
5853 break;
5854 case ISN_NEWLIST:
5855 smsg("%s%4d NEWLIST size %lld", pfx, current,
5856 (varnumber_T)(iptr->isn_arg.number));
5857 break;
5858 case ISN_NEWDICT:
5859 smsg("%s%4d NEWDICT size %lld", pfx, current,
5860 (varnumber_T)(iptr->isn_arg.number));
5861 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005862 case ISN_NEWPARTIAL:
5863 smsg("%s%4d NEWPARTIAL", pfx, current);
5864 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005865
5866 // function call
5867 case ISN_BCALL:
5868 {
5869 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5870
5871 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5872 internal_func_name(cbfunc->cbf_idx),
5873 cbfunc->cbf_argcount);
5874 }
5875 break;
5876 case ISN_DCALL:
5877 {
5878 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5879 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5880 + cdfunc->cdf_idx;
5881
5882 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005883 printable_func_name(df->df_ufunc),
5884 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005885 }
5886 break;
5887 case ISN_UCALL:
5888 {
5889 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5890
5891 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5892 cufunc->cuf_name, cufunc->cuf_argcount);
5893 }
5894 break;
5895 case ISN_PCALL:
5896 {
5897 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5898
5899 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5900 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5901 }
5902 break;
5903 case ISN_PCALL_END:
5904 smsg("%s%4d PCALL end", pfx, current);
5905 break;
5906 case ISN_RETURN:
5907 smsg("%s%4d RETURN", pfx, current);
5908 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005909 case ISN_RETURN_VOID:
5910 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005911 break;
5912 case ISN_FUNCREF:
5913 {
5914 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005915 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005916
Bram Moolenaar38453522021-11-28 22:00:12 +00005917 if (funcref->fr_func_name == NULL)
5918 {
5919 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5920 + funcref->fr_dfunc_idx;
5921 name = df->df_ufunc->uf_name;
5922 }
5923 else
5924 name = funcref->fr_func_name;
5925 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005926 }
5927 break;
5928
5929 case ISN_NEWFUNC:
5930 {
5931 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5932
5933 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5934 newfunc->nf_lambda, newfunc->nf_global);
5935 }
5936 break;
5937
5938 case ISN_DEF:
5939 {
5940 char_u *name = iptr->isn_arg.string;
5941
5942 smsg("%s%4d DEF %s", pfx, current,
5943 name == NULL ? (char_u *)"" : name);
5944 }
5945 break;
5946
5947 case ISN_JUMP:
5948 {
5949 char *when = "?";
5950
5951 switch (iptr->isn_arg.jump.jump_when)
5952 {
5953 case JUMP_ALWAYS:
5954 when = "JUMP";
5955 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005956 case JUMP_NEVER:
5957 iemsg("JUMP_NEVER should not be used");
5958 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005959 case JUMP_AND_KEEP_IF_TRUE:
5960 when = "JUMP_AND_KEEP_IF_TRUE";
5961 break;
5962 case JUMP_IF_FALSE:
5963 when = "JUMP_IF_FALSE";
5964 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005965 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;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006047 case EXPR_LSHIFT: what = "<<"; break;
6048 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006049 default: what = "???"; break;
6050 }
6051 switch (iptr->isn_type)
6052 {
6053 case ISN_OPNR: ins = "OPNR"; break;
6054 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6055 case ISN_OPANY: ins = "OPANY"; break;
6056 default: ins = "???"; break;
6057 }
6058 smsg("%s%4d %s %s", pfx, current, ins, what);
6059 }
6060 break;
6061
6062 case ISN_COMPAREBOOL:
6063 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006064 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006065 case ISN_COMPARENR:
6066 case ISN_COMPAREFLOAT:
6067 case ISN_COMPARESTRING:
6068 case ISN_COMPAREBLOB:
6069 case ISN_COMPARELIST:
6070 case ISN_COMPAREDICT:
6071 case ISN_COMPAREFUNC:
6072 case ISN_COMPAREANY:
6073 {
6074 char *p;
6075 char buf[10];
6076 char *type;
6077
6078 switch (iptr->isn_arg.op.op_type)
6079 {
6080 case EXPR_EQUAL: p = "=="; break;
6081 case EXPR_NEQUAL: p = "!="; break;
6082 case EXPR_GREATER: p = ">"; break;
6083 case EXPR_GEQUAL: p = ">="; break;
6084 case EXPR_SMALLER: p = "<"; break;
6085 case EXPR_SEQUAL: p = "<="; break;
6086 case EXPR_MATCH: p = "=~"; break;
6087 case EXPR_IS: p = "is"; break;
6088 case EXPR_ISNOT: p = "isnot"; break;
6089 case EXPR_NOMATCH: p = "!~"; break;
6090 default: p = "???"; break;
6091 }
6092 STRCPY(buf, p);
6093 if (iptr->isn_arg.op.op_ic == TRUE)
6094 strcat(buf, "?");
6095 switch(iptr->isn_type)
6096 {
6097 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6098 case ISN_COMPARESPECIAL:
6099 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006100 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006101 case ISN_COMPARENR: type = "COMPARENR"; break;
6102 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6103 case ISN_COMPARESTRING:
6104 type = "COMPARESTRING"; break;
6105 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6106 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6107 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6108 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6109 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6110 default: type = "???"; break;
6111 }
6112
6113 smsg("%s%4d %s %s", pfx, current, type, buf);
6114 }
6115 break;
6116
6117 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6118 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6119
6120 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006121 case ISN_CONCAT:
6122 smsg("%s%4d CONCAT size %lld", pfx, current,
6123 (varnumber_T)(iptr->isn_arg.number));
6124 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006125 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6126 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6127 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6128 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6129 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6130 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6131 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6132 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6133 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6134 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6135 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006136 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006137 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6138 iptr->isn_arg.getitem.gi_index,
6139 iptr->isn_arg.getitem.gi_with_op ?
6140 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006141 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6142 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6143 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006144 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6145 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6146
Bram Moolenaar4c137212021-04-19 16:48:48 +02006147 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6148
Bram Moolenaar4c137212021-04-19 16:48:48 +02006149 case ISN_CHECKTYPE:
6150 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006151 checktype_T *ct = &iptr->isn_arg.type;
6152 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006153
6154 if (ct->ct_arg_idx == 0)
6155 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6156 type_name(ct->ct_type, &tofree),
6157 (int)ct->ct_off);
6158 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006159 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006160 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006161 type_name(ct->ct_type, &tofree),
6162 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006163 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006164 (int)ct->ct_arg_idx);
6165 vim_free(tofree);
6166 break;
6167 }
6168 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6169 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6170 iptr->isn_arg.checklen.cl_min_len);
6171 break;
6172 case ISN_SETTYPE:
6173 {
6174 char *tofree;
6175
6176 smsg("%s%4d SETTYPE %s", pfx, current,
6177 type_name(iptr->isn_arg.type.ct_type, &tofree));
6178 vim_free(tofree);
6179 break;
6180 }
6181 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006182 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6183 smsg("%s%4d INVERT %d (!val)", pfx, current,
6184 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006185 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006186 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6187 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006188 break;
6189 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006190 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006191 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006192 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6193 pfx, current,
6194 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006195 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006196 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6197 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006198 break;
6199 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006200 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006201 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006202 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006203 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6204 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006205 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006206 else
6207 smsg("%s%4d PUT %c %ld", pfx, current,
6208 iptr->isn_arg.put.put_regname,
6209 (long)iptr->isn_arg.put.put_lnum);
6210 break;
6211
Bram Moolenaar4c137212021-04-19 16:48:48 +02006212 case ISN_CMDMOD:
6213 {
6214 char_u *buf;
6215 size_t len = produce_cmdmods(
6216 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6217
6218 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006219 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006220 {
6221 (void)produce_cmdmods(
6222 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6223 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6224 vim_free(buf);
6225 }
6226 break;
6227 }
6228 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6229
6230 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006231 smsg("%s%4d PROFILE START line %d", pfx, current,
6232 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006233 break;
6234
6235 case ISN_PROF_END:
6236 smsg("%s%4d PROFILE END", pfx, current);
6237 break;
6238
Bram Moolenaare99d4222021-06-13 14:01:26 +02006239 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006240 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6241 iptr->isn_arg.debug.dbg_break_lnum + 1,
6242 iptr->isn_lnum,
6243 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006244 break;
6245
Bram Moolenaar4c137212021-04-19 16:48:48 +02006246 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6247 iptr->isn_arg.unpack.unp_count,
6248 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6249 break;
6250 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6251 iptr->isn_arg.shuffle.shfl_item,
6252 iptr->isn_arg.shuffle.shfl_up);
6253 break;
6254 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6255
6256 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6257 return;
6258 }
6259
6260 out_flush(); // output one line at a time
6261 ui_breakcheck();
6262 if (got_int)
6263 break;
6264 }
6265}
6266
6267/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006268 * Handle command line completion for the :disassemble command.
6269 */
6270 void
6271set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6272{
6273 char_u *p;
6274
6275 // Default: expand user functions, "debug" and "profile"
6276 xp->xp_context = EXPAND_DISASSEMBLE;
6277 xp->xp_pattern = arg;
6278
6279 // first argument already typed: only user function names
6280 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6281 {
6282 xp->xp_context = EXPAND_USER_FUNC;
6283 xp->xp_pattern = skipwhite(p);
6284 }
6285}
6286
6287/*
6288 * Function given to ExpandGeneric() to obtain the list of :disassemble
6289 * arguments.
6290 */
6291 char_u *
6292get_disassemble_argument(expand_T *xp, int idx)
6293{
6294 if (idx == 0)
6295 return (char_u *)"debug";
6296 if (idx == 1)
6297 return (char_u *)"profile";
6298 return get_user_func_name(xp, idx - 2);
6299}
6300
6301/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006302 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006303 * We don't really need this at runtime, but we do have tests that require it,
6304 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305 */
6306 void
6307ex_disassemble(exarg_T *eap)
6308{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006309 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006310 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006312 isn_T *instr = NULL; // init to shut up gcc warning
6313 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006314 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006315
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006316 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006317 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006319 if (func_needs_compiling(ufunc, compile_type)
6320 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006321 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006322 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006324 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 return;
6326 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006327 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328
6329 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006330 switch (compile_type)
6331 {
6332 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006333#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006334 instr = dfunc->df_instr_prof;
6335 instr_count = dfunc->df_instr_prof_count;
6336 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006337#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006338 // FALLTHROUGH
6339 case CT_NONE:
6340 instr = dfunc->df_instr;
6341 instr_count = dfunc->df_instr_count;
6342 break;
6343 case CT_DEBUG:
6344 instr = dfunc->df_instr_debug;
6345 instr_count = dfunc->df_instr_debug_count;
6346 break;
6347 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348
Bram Moolenaar4c137212021-04-19 16:48:48 +02006349 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350}
6351
6352/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006353 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354 * list, etc. Mostly like what JavaScript does, except that empty list and
6355 * empty dictionary are FALSE.
6356 */
6357 int
6358tv2bool(typval_T *tv)
6359{
6360 switch (tv->v_type)
6361 {
6362 case VAR_NUMBER:
6363 return tv->vval.v_number != 0;
6364 case VAR_FLOAT:
6365#ifdef FEAT_FLOAT
6366 return tv->vval.v_float != 0.0;
6367#else
6368 break;
6369#endif
6370 case VAR_PARTIAL:
6371 return tv->vval.v_partial != NULL;
6372 case VAR_FUNC:
6373 case VAR_STRING:
6374 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6375 case VAR_LIST:
6376 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6377 case VAR_DICT:
6378 return tv->vval.v_dict != NULL
6379 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6380 case VAR_BOOL:
6381 case VAR_SPECIAL:
6382 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6383 case VAR_JOB:
6384#ifdef FEAT_JOB_CHANNEL
6385 return tv->vval.v_job != NULL;
6386#else
6387 break;
6388#endif
6389 case VAR_CHANNEL:
6390#ifdef FEAT_JOB_CHANNEL
6391 return tv->vval.v_channel != NULL;
6392#else
6393 break;
6394#endif
6395 case VAR_BLOB:
6396 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6397 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006398 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006400 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401 break;
6402 }
6403 return FALSE;
6404}
6405
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006406 void
6407emsg_using_string_as(typval_T *tv, int as_number)
6408{
6409 semsg(_(as_number ? e_using_string_as_number_str
6410 : e_using_string_as_bool_str),
6411 tv->vval.v_string == NULL
6412 ? (char_u *)"" : tv->vval.v_string);
6413}
6414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415/*
6416 * If "tv" is a string give an error and return FAIL.
6417 */
6418 int
6419check_not_string(typval_T *tv)
6420{
6421 if (tv->v_type == VAR_STRING)
6422 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006423 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006424 clear_tv(tv);
6425 return FAIL;
6426 }
6427 return OK;
6428}
6429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006430
6431#endif // FEAT_EVAL