blob: 9dd5937e47cf34986ce3954f77cd76fc59433210 [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);
2789 res = cexpr_core(&ea, tv);
2790 clear_tv(tv);
2791 if (res == FAIL)
2792 goto on_error;
2793 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002794#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002795 break;
2796
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002797 // execute Ex command from pieces on the stack
2798 case ISN_EXECCONCAT:
2799 {
2800 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002801 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002802 int pass;
2803 int i;
2804 char_u *cmd = NULL;
2805 char_u *str;
2806
2807 for (pass = 1; pass <= 2; ++pass)
2808 {
2809 for (i = 0; i < count; ++i)
2810 {
2811 tv = STACK_TV_BOT(i - count);
2812 str = tv->vval.v_string;
2813 if (str != NULL && *str != NUL)
2814 {
2815 if (pass == 2)
2816 STRCPY(cmd + len, str);
2817 len += STRLEN(str);
2818 }
2819 if (pass == 2)
2820 clear_tv(tv);
2821 }
2822 if (pass == 1)
2823 {
2824 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002825 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002826 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002827 len = 0;
2828 }
2829 }
2830
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002831 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002832 do_cmdline_cmd(cmd);
2833 vim_free(cmd);
2834 }
2835 break;
2836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 // execute :echo {string} ...
2838 case ISN_ECHO:
2839 {
2840 int count = iptr->isn_arg.echo.echo_count;
2841 int atstart = TRUE;
2842 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002843 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844
2845 for (idx = 0; idx < count; ++idx)
2846 {
2847 tv = STACK_TV_BOT(idx - count);
2848 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2849 &atstart, &needclr);
2850 clear_tv(tv);
2851 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002852 if (needclr)
2853 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002854 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 }
2856 break;
2857
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002858 // :execute {string} ...
2859 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002860 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002861 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002862 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002863 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002864 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002865 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002866 {
2867 int count = iptr->isn_arg.number;
2868 garray_T ga;
2869 char_u buf[NUMBUFLEN];
2870 char_u *p;
2871 int len;
2872 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002873 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002874
2875 ga_init2(&ga, 1, 80);
2876 for (idx = 0; idx < count; ++idx)
2877 {
2878 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002879 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002880 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002881 if (tv->v_type == VAR_CHANNEL
2882 || tv->v_type == VAR_JOB)
2883 {
2884 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002885 semsg(_(e_using_invalid_value_as_string_str),
2886 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002887 break;
2888 }
2889 else
2890 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002891 }
2892 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002893 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002894
2895 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002896 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002897 failed = TRUE;
2898 else
2899 {
2900 if (ga.ga_len > 0)
2901 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2902 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2903 ga.ga_len += len;
2904 }
2905 clear_tv(tv);
2906 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002907 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002908 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002909 {
2910 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002911 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002912 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002913
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002914 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002915 {
2916 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002917 {
2918 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002919 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002920 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002921 {
2922 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002923 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002924 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002925 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002926 else
2927 {
2928 msg_sb_eol();
2929 if (iptr->isn_type == ISN_ECHOMSG)
2930 {
2931 msg_attr(ga.ga_data, echo_attr);
2932 out_flush();
2933 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002934 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2935 {
2936 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2937 TRUE);
2938 ui_write((char_u *)"\r\n", 2, TRUE);
2939 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002940 else
2941 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002942 SOURCING_LNUM = iptr->isn_lnum;
2943 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002944 }
2945 }
2946 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002947 ga_clear(&ga);
2948 }
2949 break;
2950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 // load local variable or argument
2952 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002953 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002954 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002956 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 break;
2958
2959 // load v: variable
2960 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002961 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002962 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002964 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 break;
2966
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002967 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 case ISN_LOADSCRIPT:
2969 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002970 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 svar_T *sv;
2972
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002973 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002974 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002975 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002976 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002977 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002978 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002980 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 }
2982 break;
2983
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002984 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002986 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002988 int sid = iptr->isn_arg.loadstore.ls_sid;
2989 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002990 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 if (di == NULL)
2994 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002995 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002996 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002997 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 }
2999 else
3000 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003001 if (iptr->isn_type == ISN_LOADEXPORT)
3002 {
3003 int idx = get_script_item_idx(sid, name, 0,
3004 NULL, NULL);
3005 svar_T *sv;
3006
3007 if (idx >= 0)
3008 {
3009 sv = ((svar_T *)SCRIPT_ITEM(sid)
3010 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003011 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003012 {
3013 SOURCING_LNUM = iptr->isn_lnum;
3014 semsg(_(e_item_not_exported_in_script_str),
3015 name);
3016 goto on_error;
3017 }
3018 }
3019 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003020 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003021 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003023 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 }
3025 }
3026 break;
3027
Bram Moolenaard3aac292020-04-19 14:32:17 +02003028 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003030 case ISN_LOADB:
3031 case ISN_LOADW:
3032 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003034 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003035
Bram Moolenaar06b77222022-01-25 15:51:56 +00003036 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003037 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003038 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003039 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003041
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 break;
3043
Bram Moolenaar03290b82020-12-19 16:30:44 +01003044 // load autoload variable
3045 case ISN_LOADAUTO:
3046 {
3047 char_u *name = iptr->isn_arg.string;
3048
Bram Moolenaar35578162021-08-02 19:10:38 +02003049 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003050 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003051 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003052 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003053 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003054 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003055 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003056 }
3057 break;
3058
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003059 // load g:/b:/w:/t: namespace
3060 case ISN_LOADGDICT:
3061 case ISN_LOADBDICT:
3062 case ISN_LOADWDICT:
3063 case ISN_LOADTDICT:
3064 {
3065 dict_T *d = NULL;
3066
3067 switch (iptr->isn_type)
3068 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003069 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3070 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3071 case ISN_LOADWDICT: d = curwin->w_vars; break;
3072 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003073 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003074 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003075 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003076 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003077 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003078 tv = STACK_TV_BOT(0);
3079 tv->v_type = VAR_DICT;
3080 tv->v_lock = 0;
3081 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003082 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003083 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003084 }
3085 break;
3086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 // load &option
3088 case ISN_LOADOPT:
3089 {
3090 typval_T optval;
3091 char_u *name = iptr->isn_arg.string;
3092
Bram Moolenaara8c17702020-04-01 21:17:24 +02003093 // This is not expected to fail, name is checked during
3094 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003095 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003096 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003097 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003098 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003100 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 }
3102 break;
3103
3104 // load $ENV
3105 case ISN_LOADENV:
3106 {
3107 typval_T optval;
3108 char_u *name = iptr->isn_arg.string;
3109
Bram Moolenaar35578162021-08-02 19:10:38 +02003110 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003111 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003112 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003113 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003115 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 }
3117 break;
3118
3119 // load @register
3120 case ISN_LOADREG:
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 Moolenaar8a7d6542020-01-26 15:56:19 +01003123 tv = STACK_TV_BOT(0);
3124 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003125 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003126 // This may result in NULL, which should be equivalent to an
3127 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 tv->vval.v_string = get_reg_contents(
3129 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003130 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 break;
3132
3133 // store local variable
3134 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003135 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 tv = STACK_TV_VAR(iptr->isn_arg.number);
3137 clear_tv(tv);
3138 *tv = *STACK_TV_BOT(0);
3139 break;
3140
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003141 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003142 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003143 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003144 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003145 int sid = iptr->isn_arg.loadstore.ls_sid;
3146 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003147 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003148 dictitem_T *di = find_var_in_ht(ht, 0,
3149 iptr->isn_type == ISN_STORES
3150 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003151
Bram Moolenaar4c137212021-04-19 16:48:48 +02003152 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003153 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003154 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003155 {
3156 if (iptr->isn_type == ISN_STOREEXPORT)
3157 {
3158 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003159 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003160 goto on_error;
3161 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003162 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003163 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003164 else
3165 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003166 if (iptr->isn_type == ISN_STOREEXPORT)
3167 {
3168 int idx = get_script_item_idx(sid, name, 0,
3169 NULL, NULL);
3170
Bram Moolenaar06651632022-04-27 17:54:25 +01003171 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003172 if (idx >= 0)
3173 {
3174 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3175 ->sn_var_vals.ga_data) + idx;
3176
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003177 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003178 {
3179 semsg(_(e_item_not_exported_in_script_str),
3180 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003181 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003182 goto on_error;
3183 }
3184 }
3185 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003186 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003187 {
3188 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003189 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003190 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003191 clear_tv(&di->di_tv);
3192 di->di_tv = *STACK_TV_BOT(0);
3193 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003194 }
3195 break;
3196
3197 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 case ISN_STORESCRIPT:
3199 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003200 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3201 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003203 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003204 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003205 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003206 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003207
3208 // "const" and "final" are checked at compile time, locking
3209 // the value needs to be checked here.
3210 SOURCING_LNUM = iptr->isn_lnum;
3211 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003212 {
3213 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003214 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003215 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 clear_tv(sv->sv_tv);
3218 *sv->sv_tv = *STACK_TV_BOT(0);
3219 }
3220 break;
3221
3222 // store option
3223 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003224 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003226 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3227 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 long n = 0;
3229 char_u *s = NULL;
3230 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003231 char_u numbuf[NUMBUFLEN];
3232 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233
Bram Moolenaar4c137212021-04-19 16:48:48 +02003234 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 tv = STACK_TV_BOT(0);
3236 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003237 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003239 if (s == NULL)
3240 s = (char_u *)"";
3241 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003242 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3243 {
3244 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003245 // If the option can be set to a function reference or
3246 // a lambda and the passed value is a function
3247 // reference, then convert it to the name (string) of
3248 // the function reference.
3249 s = tv2string(tv, &tofree, numbuf, 0);
3250 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003251 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003252 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003253 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003254 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003255 goto on_error;
3256 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003259 // must be VAR_NUMBER, CHECKTYPE makes sure
3260 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003261 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003262 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003263 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 if (msg != NULL)
3265 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003266 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003268 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 }
3271 break;
3272
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003273 // store $ENV
3274 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003275 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003276 tv = STACK_TV_BOT(0);
3277 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3278 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003279 break;
3280
3281 // store @r
3282 case ISN_STOREREG:
3283 {
3284 int reg = iptr->isn_arg.number;
3285
Bram Moolenaar4c137212021-04-19 16:48:48 +02003286 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003287 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003288 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003289 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003290 }
3291 break;
3292
3293 // store v: variable
3294 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003295 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003296 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3297 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003298 // should not happen, type is checked when compiling
3299 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003300 break;
3301
Bram Moolenaard3aac292020-04-19 14:32:17 +02003302 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003304 case ISN_STOREB:
3305 case ISN_STOREW:
3306 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003308 dictitem_T *di;
3309 hashtab_T *ht;
3310 char_u *name = iptr->isn_arg.string + 2;
3311
Bram Moolenaard3aac292020-04-19 14:32:17 +02003312 switch (iptr->isn_type)
3313 {
3314 case ISN_STOREG:
3315 ht = get_globvar_ht();
3316 break;
3317 case ISN_STOREB:
3318 ht = &curbuf->b_vars->dv_hashtab;
3319 break;
3320 case ISN_STOREW:
3321 ht = &curwin->w_vars->dv_hashtab;
3322 break;
3323 case ISN_STORET:
3324 ht = &curtab->tp_vars->dv_hashtab;
3325 break;
3326 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003327 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003328 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329
Bram Moolenaar4c137212021-04-19 16:48:48 +02003330 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003331 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003333 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 else
3335 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003336 SOURCING_LNUM = iptr->isn_lnum;
3337 if (var_check_permission(di, name) == FAIL)
3338 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 clear_tv(&di->di_tv);
3340 di->di_tv = *STACK_TV_BOT(0);
3341 }
3342 }
3343 break;
3344
Bram Moolenaar03290b82020-12-19 16:30:44 +01003345 // store an autoload variable
3346 case ISN_STOREAUTO:
3347 SOURCING_LNUM = iptr->isn_lnum;
3348 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3349 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003350 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003351 break;
3352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 // store number in local variable
3354 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003355 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 clear_tv(tv);
3357 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003358 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 break;
3360
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003361 // store value in list or dict variable
3362 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003363 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003364 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003365
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003366 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003367 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003368 if (res == NOTDONE)
3369 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003370 }
3371 break;
3372
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003373 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003374 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003375 if (execute_storerange(iptr, ectx) == FAIL)
3376 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003377 break;
3378
Bram Moolenaar0186e582021-01-10 18:33:11 +01003379 // load or store variable or argument from outer scope
3380 case ISN_LOADOUTER:
3381 case ISN_STOREOUTER:
3382 {
3383 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003384 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3385 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003386
3387 while (depth > 1 && outer != NULL)
3388 {
3389 outer = outer->out_up;
3390 --depth;
3391 }
3392 if (outer == NULL)
3393 {
3394 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003395 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3396 || ectx->ec_outer_ref == NULL)
3397 // Possibly :def function called from legacy
3398 // context.
3399 emsg(_(e_closure_called_from_invalid_context));
3400 else
3401 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003402 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003403 }
3404 tv = ((typval_T *)outer->out_stack->ga_data)
3405 + outer->out_frame_idx + STACK_FRAME_SIZE
3406 + iptr->isn_arg.outer.outer_idx;
3407 if (iptr->isn_type == ISN_LOADOUTER)
3408 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003409 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003410 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003411 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003412 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003413 }
3414 else
3415 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003416 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003417 clear_tv(tv);
3418 *tv = *STACK_TV_BOT(0);
3419 }
3420 }
3421 break;
3422
Bram Moolenaar752fc692021-01-04 21:57:11 +01003423 // unlet item in list or dict variable
3424 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003425 if (execute_unletindex(iptr, ectx) == FAIL)
3426 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003427 break;
3428
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003429 // unlet range of items in list variable
3430 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003431 if (execute_unletrange(iptr, ectx) == FAIL)
3432 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003433 break;
3434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 // push constant
3436 case ISN_PUSHNR:
3437 case ISN_PUSHBOOL:
3438 case ISN_PUSHSPEC:
3439 case ISN_PUSHF:
3440 case ISN_PUSHS:
3441 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003442 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003443 case ISN_PUSHCHANNEL:
3444 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003445 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003446 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003448 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003449 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 switch (iptr->isn_type)
3451 {
3452 case ISN_PUSHNR:
3453 tv->v_type = VAR_NUMBER;
3454 tv->vval.v_number = iptr->isn_arg.number;
3455 break;
3456 case ISN_PUSHBOOL:
3457 tv->v_type = VAR_BOOL;
3458 tv->vval.v_number = iptr->isn_arg.number;
3459 break;
3460 case ISN_PUSHSPEC:
3461 tv->v_type = VAR_SPECIAL;
3462 tv->vval.v_number = iptr->isn_arg.number;
3463 break;
3464#ifdef FEAT_FLOAT
3465 case ISN_PUSHF:
3466 tv->v_type = VAR_FLOAT;
3467 tv->vval.v_float = iptr->isn_arg.fnumber;
3468 break;
3469#endif
3470 case ISN_PUSHBLOB:
3471 blob_copy(iptr->isn_arg.blob, tv);
3472 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003473 case ISN_PUSHFUNC:
3474 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003475 if (iptr->isn_arg.string == NULL)
3476 tv->vval.v_string = NULL;
3477 else
3478 tv->vval.v_string =
3479 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003480 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003481 case ISN_PUSHCHANNEL:
3482#ifdef FEAT_JOB_CHANNEL
3483 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003484 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003485#endif
3486 break;
3487 case ISN_PUSHJOB:
3488#ifdef FEAT_JOB_CHANNEL
3489 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003490 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003491#endif
3492 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 default:
3494 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003495 tv->vval.v_string = iptr->isn_arg.string == NULL
3496 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 }
3498 break;
3499
Bram Moolenaar06b77222022-01-25 15:51:56 +00003500 case ISN_AUTOLOAD:
3501 {
3502 char_u *name = iptr->isn_arg.string;
3503
3504 (void)script_autoload(name, FALSE);
3505 if (find_func(name, TRUE))
3506 {
3507 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3508 goto theend;
3509 tv = STACK_TV_BOT(0);
3510 tv->v_lock = 0;
3511 ++ectx->ec_stack.ga_len;
3512 tv->v_type = VAR_FUNC;
3513 tv->vval.v_string = vim_strsave(name);
3514 }
3515 else
3516 {
3517 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3518
3519 if (res == NOTDONE)
3520 goto theend;
3521 if (res == FAIL)
3522 goto on_error;
3523 }
3524 }
3525 break;
3526
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003527 case ISN_UNLET:
3528 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3529 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003530 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003531 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003532 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003533 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003534 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003535
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003536 case ISN_LOCKUNLOCK:
3537 {
3538 typval_T *lval_root_save = lval_root;
3539 int res;
3540
3541 // Stack has the local variable, argument the whole :lock
3542 // or :unlock command, like ISN_EXEC.
3543 --ectx->ec_stack.ga_len;
3544 lval_root = STACK_TV_BOT(0);
3545 res = exec_command(iptr);
3546 clear_tv(lval_root);
3547 lval_root = lval_root_save;
3548 if (res == FAIL)
3549 goto on_error;
3550 }
3551 break;
3552
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003553 case ISN_LOCKCONST:
3554 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3555 break;
3556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 // create a list from items on the stack; uses a single allocation
3558 // for the list header and the items
3559 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003560 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003561 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 break;
3563
3564 // create a dict from items on the stack
3565 case ISN_NEWDICT:
3566 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003567 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003569 SOURCING_LNUM = iptr->isn_lnum;
3570 res = exe_newdict(iptr->isn_arg.number, ectx);
3571 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003572 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003573 if (res == MAYBE)
3574 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 }
3576 break;
3577
LemonBoy372bcce2022-04-25 12:43:20 +01003578 case ISN_CONCAT:
3579 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3580 goto theend;
3581 break;
3582
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003583 // create a partial with NULL value
3584 case ISN_NEWPARTIAL:
3585 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3586 goto theend;
3587 ++ectx->ec_stack.ga_len;
3588 tv = STACK_TV_BOT(-1);
3589 tv->v_type = VAR_PARTIAL;
3590 tv->v_lock = 0;
3591 tv->vval.v_partial = NULL;
3592 break;
3593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 // call a :def function
3595 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003596 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003597 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3598 NULL,
3599 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003600 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003601 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 break;
3603
3604 // call a builtin function
3605 case ISN_BCALL:
3606 SOURCING_LNUM = iptr->isn_lnum;
3607 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3608 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003609 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003610 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 break;
3612
3613 // call a funcref or partial
3614 case ISN_PCALL:
3615 {
3616 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3617 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003618 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619
3620 SOURCING_LNUM = iptr->isn_lnum;
3621 if (pfunc->cpf_top)
3622 {
3623 // funcref is above the arguments
3624 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3625 }
3626 else
3627 {
3628 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003629 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003630 partial_tv = *STACK_TV_BOT(0);
3631 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003633 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003634 if (tv == &partial_tv)
3635 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003637 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 }
3639 break;
3640
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003641 case ISN_PCALL_END:
3642 // PCALL finished, arguments have been consumed and replaced by
3643 // the return value. Now clear the funcref from the stack,
3644 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003645 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003646 clear_tv(STACK_TV_BOT(-1));
3647 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3648 break;
3649
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 // call a user defined function or funcref/partial
3651 case ISN_UCALL:
3652 {
3653 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3654
3655 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003656 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003657 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003658 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 }
3660 break;
3661
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003662 // return from a :def function call without a value
3663 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003664 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003665 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003666 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003667 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003668 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003669 tv->vval.v_number = 0;
3670 tv->v_lock = 0;
3671 // FALLTHROUGH
3672
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003673 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 case ISN_RETURN:
3675 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003676 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003677 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003678
3679 if (trystack->ga_len > 0)
3680 trycmd = ((trycmd_T *)trystack->ga_data)
3681 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003682 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003683 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003685 // jump to ":finally" or ":endtry"
3686 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003687 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003688 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003689 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 trycmd->tcd_return = TRUE;
3691 }
3692 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003693 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 }
3695 break;
3696
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003697 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 case ISN_FUNCREF:
3699 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003700 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003701 ufunc_T *ufunc;
3702 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003705 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003706 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003707 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003708 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003709 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003710 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003711 if (funcref->fr_func_name == NULL)
3712 {
3713 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3714 + funcref->fr_dfunc_idx;
3715
3716 ufunc = pt_dfunc->df_ufunc;
3717 }
3718 else
3719 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003720 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003721 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003722 if (ufunc == NULL)
3723 {
3724 SOURCING_LNUM = iptr->isn_lnum;
3725 iemsg("ufunc unexpectedly NULL for FUNCREF");
3726 goto theend;
3727 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003728 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003729 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003731 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 tv->vval.v_partial = pt;
3733 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003734 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 }
3736 break;
3737
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003738 // Create a global function from a lambda.
3739 case ISN_NEWFUNC:
3740 {
3741 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3742
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003743 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003744 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003745 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003746 }
3747 break;
3748
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003749 // List functions
3750 case ISN_DEF:
3751 if (iptr->isn_arg.string == NULL)
3752 list_functions(NULL);
3753 else
3754 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003755 exarg_T ea;
3756 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003757
3758 CLEAR_FIELD(ea);
3759 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003760 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3761 define_function(&ea, NULL, &lines_to_free);
3762 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003763 }
3764 break;
3765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 // jump if a condition is met
3767 case ISN_JUMP:
3768 {
3769 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003770 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 int jump = TRUE;
3772
3773 if (when != JUMP_ALWAYS)
3774 {
3775 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003776 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003777 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003778 || when == JUMP_IF_COND_TRUE)
3779 {
3780 SOURCING_LNUM = iptr->isn_lnum;
3781 jump = tv_get_bool_chk(tv, &error);
3782 if (error)
3783 goto on_error;
3784 }
3785 else
3786 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01003787 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003789 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 {
3791 // drop the value from the stack
3792 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003793 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 }
3795 }
3796 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003797 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 }
3799 break;
3800
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003801 // Jump if an argument with a default value was already set and not
3802 // v:none.
3803 case ISN_JUMP_IF_ARG_SET:
3804 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3805 if (tv->v_type != VAR_UNKNOWN
3806 && !(tv->v_type == VAR_SPECIAL
3807 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003808 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003809 break;
3810
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 // top of a for loop
3812 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003813 if (execute_for(iptr, ectx) == FAIL)
3814 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 break;
3816
3817 // start of ":try" block
3818 case ISN_TRY:
3819 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003820 trycmd_T *trycmd = NULL;
3821
Bram Moolenaar35578162021-08-02 19:10:38 +02003822 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003823 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003824 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3825 + ectx->ec_trystack.ga_len;
3826 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003828 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003829 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3830 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003831 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003832 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003833 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003834 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003835 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003836 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 }
3838 break;
3839
3840 case ISN_PUSHEXC:
3841 if (current_exception == NULL)
3842 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003843 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003845 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003847 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003848 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003850 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003852 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 tv->vval.v_string = vim_strsave(
3854 (char_u *)current_exception->value);
3855 break;
3856
3857 case ISN_CATCH:
3858 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003859 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01003860 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861
Bram Moolenaar4c137212021-04-19 16:48:48 +02003862 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01003863 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01003865 trycmd->tcd_caught = TRUE;
3866 trycmd->tcd_did_throw = FALSE;
3867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003869 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 catch_exception(current_exception);
3871 }
3872 break;
3873
Bram Moolenaarc150c092021-02-13 15:02:46 +01003874 case ISN_TRYCONT:
3875 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003876 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003877 trycont_T *trycont = &iptr->isn_arg.trycont;
3878 int i;
3879 trycmd_T *trycmd;
3880 int iidx = trycont->tct_where;
3881
3882 if (trystack->ga_len < trycont->tct_levels)
3883 {
3884 siemsg("TRYCONT: expected %d levels, found %d",
3885 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003886 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003887 }
3888 // Make :endtry jump to any outer try block and the last
3889 // :endtry inside the loop to the loop start.
3890 for (i = trycont->tct_levels; i > 0; --i)
3891 {
3892 trycmd = ((trycmd_T *)trystack->ga_data)
3893 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003894 // Add one to tcd_cont to be able to jump to
3895 // instruction with index zero.
3896 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003897 iidx = trycmd->tcd_finally_idx == 0
3898 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003899 }
3900 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003901 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003902 }
3903 break;
3904
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003905 case ISN_FINALLY:
3906 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003907 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003908 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3909 + trystack->ga_len - 1;
3910
3911 // Reset the index to avoid a return statement jumps here
3912 // again.
3913 trycmd->tcd_finally_idx = 0;
3914 break;
3915 }
3916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 // end of ":try" block
3918 case ISN_ENDTRY:
3919 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003920 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01003921 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922
Bram Moolenaar06651632022-04-27 17:54:25 +01003923 --trystack->ga_len;
3924 --trylevel;
3925 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
3926 if (trycmd->tcd_did_throw)
3927 did_throw = TRUE;
3928 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 {
Bram Moolenaar06651632022-04-27 17:54:25 +01003930 // discard the exception
3931 if (caught_stack == current_exception)
3932 caught_stack = caught_stack->caught;
3933 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 }
Bram Moolenaar06651632022-04-27 17:54:25 +01003935
3936 if (trycmd->tcd_return)
3937 goto func_return;
3938
3939 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
3940 {
3941 --ectx->ec_stack.ga_len;
3942 clear_tv(STACK_TV_BOT(0));
3943 }
3944 if (trycmd->tcd_cont != 0)
3945 // handling :continue: jump to outer try block or
3946 // start of the loop
3947 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 }
3949 break;
3950
3951 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003952 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003953 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003954
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003955 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3956 {
3957 // throwing an exception while using "silent!" causes
3958 // the function to abort but not display an error.
3959 tv = STACK_TV_BOT(-1);
3960 clear_tv(tv);
3961 tv->v_type = VAR_NUMBER;
3962 tv->vval.v_number = 0;
3963 goto done;
3964 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003965 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003966 tv = STACK_TV_BOT(0);
3967 if (tv->vval.v_string == NULL
3968 || *skipwhite(tv->vval.v_string) == NUL)
3969 {
3970 vim_free(tv->vval.v_string);
3971 SOURCING_LNUM = iptr->isn_lnum;
3972 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003973 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003974 }
3975
3976 // Inside a "catch" we need to first discard the caught
3977 // exception.
3978 if (trystack->ga_len > 0)
3979 {
3980 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3981 + trystack->ga_len - 1;
3982 if (trycmd->tcd_caught && current_exception != NULL)
3983 {
3984 // discard the exception
3985 if (caught_stack == current_exception)
3986 caught_stack = caught_stack->caught;
3987 discard_current_exception();
3988 trycmd->tcd_caught = FALSE;
3989 }
3990 }
3991
Bram Moolenaar90a57162022-02-12 14:23:17 +00003992 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003993 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3994 == FAIL)
3995 {
3996 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003997 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003998 }
3999 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 break;
4002
4003 // compare with special values
4004 case ISN_COMPAREBOOL:
4005 case ISN_COMPARESPECIAL:
4006 {
4007 typval_T *tv1 = STACK_TV_BOT(-2);
4008 typval_T *tv2 = STACK_TV_BOT(-1);
4009 varnumber_T arg1 = tv1->vval.v_number;
4010 varnumber_T arg2 = tv2->vval.v_number;
4011 int res;
4012
Bram Moolenaar06651632022-04-27 17:54:25 +01004013 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4014 res = arg1 == arg2;
4015 else
4016 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017
Bram Moolenaar4c137212021-04-19 16:48:48 +02004018 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 tv1->v_type = VAR_BOOL;
4020 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4021 }
4022 break;
4023
Bram Moolenaar7a222242022-03-01 19:23:24 +00004024 case ISN_COMPARENULL:
4025 {
4026 typval_T *tv1 = STACK_TV_BOT(-2);
4027 typval_T *tv2 = STACK_TV_BOT(-1);
4028 int res;
4029
4030 res = typval_compare_null(tv1, tv2);
4031 if (res == MAYBE)
4032 goto on_error;
4033 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4034 res = !res;
4035 clear_tv(tv1);
4036 clear_tv(tv2);
4037 --ectx->ec_stack.ga_len;
4038 tv1->v_type = VAR_BOOL;
4039 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4040 }
4041 break;
4042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 // Operation with two number arguments
4044 case ISN_OPNR:
4045 case ISN_COMPARENR:
4046 {
4047 typval_T *tv1 = STACK_TV_BOT(-2);
4048 typval_T *tv2 = STACK_TV_BOT(-1);
4049 varnumber_T arg1 = tv1->vval.v_number;
4050 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004051 varnumber_T res = 0;
4052 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004054 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4055 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4056 {
4057 if (arg2 < 0)
4058 {
4059 SOURCING_LNUM = iptr->isn_lnum;
4060 emsg(_(e_bitshift_ops_must_be_postive));
4061 goto on_error;
4062 }
4063 }
4064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 switch (iptr->isn_arg.op.op_type)
4066 {
4067 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004068 case EXPR_DIV: if (arg2 == 0)
4069 div_zero = TRUE;
4070 else
4071 res = arg1 / arg2;
4072 break;
4073 case EXPR_REM: if (arg2 == 0)
4074 div_zero = TRUE;
4075 else
4076 res = arg1 % arg2;
4077 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 case EXPR_SUB: res = arg1 - arg2; break;
4079 case EXPR_ADD: res = arg1 + arg2; break;
4080
4081 case EXPR_EQUAL: res = arg1 == arg2; break;
4082 case EXPR_NEQUAL: res = arg1 != arg2; break;
4083 case EXPR_GREATER: res = arg1 > arg2; break;
4084 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4085 case EXPR_SMALLER: res = arg1 < arg2; break;
4086 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004087 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4088 res = 0;
4089 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004090 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004091 break;
4092 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4093 res = 0;
4094 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004095 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004096 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004097 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 }
4099
Bram Moolenaar4c137212021-04-19 16:48:48 +02004100 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 if (iptr->isn_type == ISN_COMPARENR)
4102 {
4103 tv1->v_type = VAR_BOOL;
4104 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4105 }
4106 else
4107 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004108 if (div_zero)
4109 {
4110 SOURCING_LNUM = iptr->isn_lnum;
4111 emsg(_(e_divide_by_zero));
4112 goto on_error;
4113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 }
4115 break;
4116
4117 // Computation with two float arguments
4118 case ISN_OPFLOAT:
4119 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004120#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 {
4122 typval_T *tv1 = STACK_TV_BOT(-2);
4123 typval_T *tv2 = STACK_TV_BOT(-1);
4124 float_T arg1 = tv1->vval.v_float;
4125 float_T arg2 = tv2->vval.v_float;
4126 float_T res = 0;
4127 int cmp = FALSE;
4128
4129 switch (iptr->isn_arg.op.op_type)
4130 {
4131 case EXPR_MULT: res = arg1 * arg2; break;
4132 case EXPR_DIV: res = arg1 / arg2; break;
4133 case EXPR_SUB: res = arg1 - arg2; break;
4134 case EXPR_ADD: res = arg1 + arg2; break;
4135
4136 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4137 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4138 case EXPR_GREATER: cmp = arg1 > arg2; break;
4139 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4140 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4141 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4142 default: cmp = 0; break;
4143 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004144 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 if (iptr->isn_type == ISN_COMPAREFLOAT)
4146 {
4147 tv1->v_type = VAR_BOOL;
4148 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4149 }
4150 else
4151 tv1->vval.v_float = res;
4152 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004153#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 break;
4155
4156 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004157 case ISN_COMPAREDICT:
4158 case ISN_COMPAREFUNC:
4159 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 case ISN_COMPAREBLOB:
4161 {
4162 typval_T *tv1 = STACK_TV_BOT(-2);
4163 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004164 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4165 int ic = iptr->isn_arg.op.op_ic;
4166 int res = FALSE;
4167 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168
Bram Moolenaar265f8112021-12-19 12:33:05 +00004169 SOURCING_LNUM = iptr->isn_lnum;
4170 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004172 status = typval_compare_list(tv1, tv2,
4173 exprtype, ic, &res);
4174 }
4175 else if (iptr->isn_type == ISN_COMPAREDICT)
4176 {
4177 status = typval_compare_dict(tv1, tv2,
4178 exprtype, ic, &res);
4179 }
4180 else if (iptr->isn_type == ISN_COMPAREFUNC)
4181 {
4182 status = typval_compare_func(tv1, tv2,
4183 exprtype, ic, &res);
4184 }
4185 else if (iptr->isn_type == ISN_COMPARESTRING)
4186 {
4187 status = typval_compare_string(tv1, tv2,
4188 exprtype, ic, &res);
4189 }
4190 else
4191 {
4192 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004194 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 clear_tv(tv1);
4196 clear_tv(tv2);
4197 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004198 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4199 if (status == FAIL)
4200 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 }
4202 break;
4203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 case ISN_COMPAREANY:
4205 {
4206 typval_T *tv1 = STACK_TV_BOT(-2);
4207 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004208 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004210 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211
Bram Moolenaareb26f432020-09-14 16:50:05 +02004212 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004213 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004215 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004216 if (status == FAIL)
4217 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 }
4219 break;
4220
4221 case ISN_ADDLIST:
4222 case ISN_ADDBLOB:
4223 {
4224 typval_T *tv1 = STACK_TV_BOT(-2);
4225 typval_T *tv2 = STACK_TV_BOT(-1);
4226
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004227 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004229 {
4230 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4231 && tv1->vval.v_list != NULL)
4232 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4233 NULL);
4234 else
4235 eval_addlist(tv1, tv2);
4236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 else
4238 eval_addblob(tv1, tv2);
4239 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004240 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 }
4242 break;
4243
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004244 case ISN_LISTAPPEND:
4245 {
4246 typval_T *tv1 = STACK_TV_BOT(-2);
4247 typval_T *tv2 = STACK_TV_BOT(-1);
4248 list_T *l = tv1->vval.v_list;
4249
4250 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004251 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004252 if (l == NULL)
4253 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004254 emsg(_(e_cannot_add_to_null_list));
4255 goto on_error;
4256 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004257 if (value_check_lock(l->lv_lock, NULL, FALSE))
4258 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004259 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004260 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004261 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004262 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004263 }
4264 break;
4265
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004266 case ISN_BLOBAPPEND:
4267 {
4268 typval_T *tv1 = STACK_TV_BOT(-2);
4269 typval_T *tv2 = STACK_TV_BOT(-1);
4270 blob_T *b = tv1->vval.v_blob;
4271 int error = FALSE;
4272 varnumber_T n;
4273
4274 // add a number to a blob
4275 if (b == NULL)
4276 {
4277 SOURCING_LNUM = iptr->isn_lnum;
4278 emsg(_(e_cannot_add_to_null_blob));
4279 goto on_error;
4280 }
4281 n = tv_get_number_chk(tv2, &error);
4282 if (error)
4283 goto on_error;
4284 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004285 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004286 }
4287 break;
4288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 // Computation with two arguments of unknown type
4290 case ISN_OPANY:
4291 {
4292 typval_T *tv1 = STACK_TV_BOT(-2);
4293 typval_T *tv2 = STACK_TV_BOT(-1);
4294 varnumber_T n1, n2;
4295#ifdef FEAT_FLOAT
4296 float_T f1 = 0, f2 = 0;
4297#endif
4298 int error = FALSE;
4299
4300 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4301 {
4302 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4303 {
4304 eval_addlist(tv1, tv2);
4305 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004306 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 break;
4308 }
4309 else if (tv1->v_type == VAR_BLOB
4310 && tv2->v_type == VAR_BLOB)
4311 {
4312 eval_addblob(tv1, tv2);
4313 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004314 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 break;
4316 }
4317 }
4318#ifdef FEAT_FLOAT
4319 if (tv1->v_type == VAR_FLOAT)
4320 {
4321 f1 = tv1->vval.v_float;
4322 n1 = 0;
4323 }
4324 else
4325#endif
4326 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004327 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 n1 = tv_get_number_chk(tv1, &error);
4329 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004330 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331#ifdef FEAT_FLOAT
4332 if (tv2->v_type == VAR_FLOAT)
4333 f1 = n1;
4334#endif
4335 }
4336#ifdef FEAT_FLOAT
4337 if (tv2->v_type == VAR_FLOAT)
4338 {
4339 f2 = tv2->vval.v_float;
4340 n2 = 0;
4341 }
4342 else
4343#endif
4344 {
4345 n2 = tv_get_number_chk(tv2, &error);
4346 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004347 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348#ifdef FEAT_FLOAT
4349 if (tv1->v_type == VAR_FLOAT)
4350 f2 = n2;
4351#endif
4352 }
4353#ifdef FEAT_FLOAT
4354 // if there is a float on either side the result is a float
4355 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4356 {
4357 switch (iptr->isn_arg.op.op_type)
4358 {
4359 case EXPR_MULT: f1 = f1 * f2; break;
4360 case EXPR_DIV: f1 = f1 / f2; break;
4361 case EXPR_SUB: f1 = f1 - f2; break;
4362 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004363 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004364 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004365 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 }
4367 clear_tv(tv1);
4368 clear_tv(tv2);
4369 tv1->v_type = VAR_FLOAT;
4370 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004371 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 }
4373 else
4374#endif
4375 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004376 int failed = FALSE;
4377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 switch (iptr->isn_arg.op.op_type)
4379 {
4380 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004381 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4382 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004383 goto on_error;
4384 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 case EXPR_SUB: n1 = n1 - n2; break;
4386 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004387 default: n1 = num_modulus(n1, n2, &failed);
4388 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004389 goto on_error;
4390 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 }
4392 clear_tv(tv1);
4393 clear_tv(tv2);
4394 tv1->v_type = VAR_NUMBER;
4395 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004396 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 }
4398 }
4399 break;
4400
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004401 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004402 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004403 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004404 int is_slice = iptr->isn_type == ISN_STRSLICE;
4405 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004406 char_u *res;
4407
4408 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004409 // string slice: string is at stack-3, first index at
4410 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004411 if (is_slice)
4412 {
4413 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004414 n1 = tv->vval.v_number;
4415 }
4416
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004417 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004418 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004419
Bram Moolenaar4c137212021-04-19 16:48:48 +02004420 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004421 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004422 if (is_slice)
4423 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004424 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004425 else
4426 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004427 // single character (including composing characters).
4428 // If the index is too big or negative the result is
4429 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004430 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004431 vim_free(tv->vval.v_string);
4432 tv->vval.v_string = res;
4433 }
4434 break;
4435
4436 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004437 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004438 case ISN_BLOBINDEX:
4439 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004441 int is_slice = iptr->isn_type == ISN_LISTSLICE
4442 || iptr->isn_type == ISN_BLOBSLICE;
4443 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4444 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004445 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004446 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447
4448 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004449 // list slice: list is at stack-3, indexes at stack-2 and
4450 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004451 // Same for blob.
4452 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453
4454 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004455 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004457
4458 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 {
Bram Moolenaared591872020-08-15 22:14:53 +02004460 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004461 n1 = tv->vval.v_number;
4462 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 }
Bram Moolenaared591872020-08-15 22:14:53 +02004464
Bram Moolenaar4c137212021-04-19 16:48:48 +02004465 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004466 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004467 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004468 if (is_blob)
4469 {
4470 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4471 n1, n2, FALSE, tv) == FAIL)
4472 goto on_error;
4473 }
4474 else
4475 {
4476 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4477 n1, n2, FALSE, tv, TRUE) == FAIL)
4478 goto on_error;
4479 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 }
4481 break;
4482
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004483 case ISN_ANYINDEX:
4484 case ISN_ANYSLICE:
4485 {
4486 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4487 typval_T *var1, *var2;
4488 int res;
4489
4490 // index: composite is at stack-2, index at stack-1
4491 // slice: composite is at stack-3, indexes at stack-2 and
4492 // stack-1
4493 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004494 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004495 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4496 goto on_error;
4497 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4498 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004499 res = eval_index_inner(tv, is_slice, var1, var2,
4500 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004501 clear_tv(var1);
4502 if (is_slice)
4503 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004504 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004505 if (res == FAIL)
4506 goto on_error;
4507 }
4508 break;
4509
Bram Moolenaar9af78762020-06-16 11:34:42 +02004510 case ISN_SLICE:
4511 {
4512 list_T *list;
4513 int count = iptr->isn_arg.number;
4514
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004515 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004516 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004517 list = tv->vval.v_list;
4518
4519 // no error for short list, expect it to be checked earlier
4520 if (list != NULL && list->lv_len >= count)
4521 {
4522 list_T *newlist = list_slice(list,
4523 count, list->lv_len - 1);
4524
4525 if (newlist != NULL)
4526 {
4527 list_unref(list);
4528 tv->vval.v_list = newlist;
4529 ++newlist->lv_refcount;
4530 }
4531 }
4532 }
4533 break;
4534
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004535 case ISN_GETITEM:
4536 {
4537 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004538 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004539
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004540 // Get list item: list is at stack-1, push item.
4541 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004542 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4543 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004544
Bram Moolenaar35578162021-08-02 19:10:38 +02004545 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004546 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004547 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004548 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004549
4550 // Useful when used in unpack assignment. Reset at
4551 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004552 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004553 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004554 }
4555 break;
4556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 case ISN_MEMBER:
4558 {
4559 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004560 char_u *key;
4561 dictitem_T *di;
4562
4563 // dict member: dict is at stack-2, key at stack-1
4564 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004565 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004566 dict = tv->vval.v_dict;
4567
4568 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004569 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004570 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004571 if (key == NULL)
4572 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004573
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004574 if ((di = dict_find(dict, key, -1)) == NULL)
4575 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004576 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004577 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004578
4579 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004580 // stack contents makes sense and the dict stack is
4581 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004582 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004583 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004584 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004585 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004586 tv->v_type = VAR_NUMBER;
4587 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004588 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004589 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004590 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004591 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004592 // Put the dict used on the dict stack, it might be used by
4593 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004594 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004595 if (dict_stack_save(tv) == FAIL)
4596 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004597 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004598 }
4599 break;
4600
4601 // dict member with string key
4602 case ISN_STRINGMEMBER:
4603 {
4604 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 dictitem_T *di;
4606
4607 tv = STACK_TV_BOT(-1);
4608 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4609 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004610 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004611 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004612 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 }
4614 dict = tv->vval.v_dict;
4615
4616 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4617 == NULL)
4618 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004619 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004620 semsg(_(e_key_not_present_in_dictionary),
4621 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004622 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004624 // Put the dict used on the dict stack, it might be used by
4625 // a dict function later.
4626 if (dict_stack_save(tv) == FAIL)
4627 goto on_fatal_error;
4628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004630 }
4631 break;
4632
4633 case ISN_CLEARDICT:
4634 dict_stack_drop();
4635 break;
4636
4637 case ISN_USEDICT:
4638 {
4639 typval_T *dict_tv = dict_stack_get_tv();
4640
4641 // Turn "dict.Func" into a partial for "Func" bound to
4642 // "dict". Don't do this when "Func" is already a partial
4643 // that was bound explicitly (pt_auto is FALSE).
4644 tv = STACK_TV_BOT(-1);
4645 if (dict_tv != NULL
4646 && dict_tv->v_type == VAR_DICT
4647 && dict_tv->vval.v_dict != NULL
4648 && (tv->v_type == VAR_FUNC
4649 || (tv->v_type == VAR_PARTIAL
4650 && (tv->vval.v_partial->pt_auto
4651 || tv->vval.v_partial->pt_dict == NULL))))
4652 dict_tv->vval.v_dict =
4653 make_partial(dict_tv->vval.v_dict, tv);
4654 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 }
4656 break;
4657
4658 case ISN_NEGATENR:
4659 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004660 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004661#ifdef FEAT_FLOAT
4662 if (tv->v_type == VAR_FLOAT)
4663 tv->vval.v_float = -tv->vval.v_float;
4664 else
4665#endif
4666 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 break;
4668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 case ISN_CHECKTYPE:
4670 {
4671 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004672 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004673 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004675 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004676 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004677 if (!ectx->ec_where.wt_variable)
4678 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004679 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004680 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004681 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004682 if (r == FAIL)
4683 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004684 if (!ectx->ec_where.wt_variable)
4685 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004686
4687 // number 0 is FALSE, number 1 is TRUE
4688 if (tv->v_type == VAR_NUMBER
4689 && ct->ct_type->tt_type == VAR_BOOL
4690 && (tv->vval.v_number == 0
4691 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004693 tv->v_type = VAR_BOOL;
4694 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004695 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 }
4697 }
4698 break;
4699
Bram Moolenaar9af78762020-06-16 11:34:42 +02004700 case ISN_CHECKLEN:
4701 {
4702 int min_len = iptr->isn_arg.checklen.cl_min_len;
4703 list_T *list = NULL;
4704
4705 tv = STACK_TV_BOT(-1);
4706 if (tv->v_type == VAR_LIST)
4707 list = tv->vval.v_list;
4708 if (list == NULL || list->lv_len < min_len
4709 || (list->lv_len > min_len
4710 && !iptr->isn_arg.checklen.cl_more_OK))
4711 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004712 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004713 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004714 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004715 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004716 }
4717 }
4718 break;
4719
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004720 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004721 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004722 break;
4723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004725 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 {
4727 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004728 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004730 if (iptr->isn_type == ISN_2BOOL)
4731 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004732 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004733 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004734 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004735 n = !n;
4736 }
4737 else
4738 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004739 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004740 SOURCING_LNUM = iptr->isn_lnum;
4741 n = tv_get_bool_chk(tv, &error);
4742 if (error)
4743 goto on_error;
4744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 clear_tv(tv);
4746 tv->v_type = VAR_BOOL;
4747 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4748 }
4749 break;
4750
4751 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004752 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004753 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004754 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4755 iptr->isn_type == ISN_2STRING_ANY,
4756 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004757 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 break;
4759
Bram Moolenaar08597872020-12-10 19:43:40 +01004760 case ISN_RANGE:
4761 {
4762 exarg_T ea;
4763 char *errormsg;
4764
Bram Moolenaarece0b872021-01-08 20:40:45 +01004765 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004766 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004767 ea.addr_type = ADDR_LINES;
4768 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004769 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004770 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004771 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004772
Bram Moolenaar35578162021-08-02 19:10:38 +02004773 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004774 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004775 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004776 tv = STACK_TV_BOT(-1);
4777 tv->v_type = VAR_NUMBER;
4778 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004779 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004780 }
4781 break;
4782
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004783 case ISN_PUT:
4784 {
4785 int regname = iptr->isn_arg.put.put_regname;
4786 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4787 char_u *expr = NULL;
4788 int dir = FORWARD;
4789
Bram Moolenaar08597872020-12-10 19:43:40 +01004790 if (lnum < -2)
4791 {
4792 // line number was put on the stack by ISN_RANGE
4793 tv = STACK_TV_BOT(-1);
4794 curwin->w_cursor.lnum = tv->vval.v_number;
4795 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4796 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004797 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004798 }
4799 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004800 // :put! above cursor
4801 dir = BACKWARD;
4802 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004803 {
4804 curwin->w_cursor.lnum = lnum;
4805 if (lnum == 0)
4806 // check_cursor() below will move to line 1
4807 dir = BACKWARD;
4808 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004809
4810 if (regname == '=')
4811 {
4812 tv = STACK_TV_BOT(-1);
4813 if (tv->v_type == VAR_STRING)
4814 expr = tv->vval.v_string;
4815 else
4816 {
4817 expr = typval2string(tv, TRUE); // allocates value
4818 clear_tv(tv);
4819 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004820 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004821 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004822 check_cursor();
4823 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4824 vim_free(expr);
4825 }
4826 break;
4827
Bram Moolenaar02194d22020-10-24 23:08:38 +02004828 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004829 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4830 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4831 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4832 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004833 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4834 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004835 break;
4836
Bram Moolenaar02194d22020-10-24 23:08:38 +02004837 case ISN_CMDMOD_REV:
4838 // filter regprog is owned by the instruction, don't free it
4839 cmdmod.cmod_filter_regmatch.regprog = NULL;
4840 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004841 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4842 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004843 break;
4844
Bram Moolenaar792f7862020-11-23 08:31:18 +01004845 case ISN_UNPACK:
4846 {
4847 int count = iptr->isn_arg.unpack.unp_count;
4848 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4849 list_T *l;
4850 listitem_T *li;
4851 int i;
4852
4853 // Check there is a valid list to unpack.
4854 tv = STACK_TV_BOT(-1);
4855 if (tv->v_type != VAR_LIST)
4856 {
4857 SOURCING_LNUM = iptr->isn_lnum;
4858 emsg(_(e_for_argument_must_be_sequence_of_lists));
4859 goto on_error;
4860 }
4861 l = tv->vval.v_list;
4862 if (l == NULL
4863 || l->lv_len < (semicolon ? count - 1 : count))
4864 {
4865 SOURCING_LNUM = iptr->isn_lnum;
4866 emsg(_(e_list_value_does_not_have_enough_items));
4867 goto on_error;
4868 }
4869 else if (!semicolon && l->lv_len > count)
4870 {
4871 SOURCING_LNUM = iptr->isn_lnum;
4872 emsg(_(e_list_value_has_more_items_than_targets));
4873 goto on_error;
4874 }
4875
4876 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004877 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004878 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004879 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004880
4881 // Variable after semicolon gets a list with the remaining
4882 // items.
4883 if (semicolon)
4884 {
4885 list_T *rem_list =
4886 list_alloc_with_items(l->lv_len - count + 1);
4887
4888 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004889 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004890 tv = STACK_TV_BOT(-count);
4891 tv->vval.v_list = rem_list;
4892 ++rem_list->lv_refcount;
4893 tv->v_lock = 0;
4894 li = l->lv_first;
4895 for (i = 0; i < count - 1; ++i)
4896 li = li->li_next;
4897 for (i = 0; li != NULL; ++i)
4898 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004899 typval_T tvcopy;
4900
4901 copy_tv(&li->li_tv, &tvcopy);
4902 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004903 li = li->li_next;
4904 }
4905 --count;
4906 }
4907
4908 // Produce the values in reverse order, first item last.
4909 li = l->lv_first;
4910 for (i = 0; i < count; ++i)
4911 {
4912 tv = STACK_TV_BOT(-i - 1);
4913 copy_tv(&li->li_tv, tv);
4914 li = li->li_next;
4915 }
4916
4917 list_unref(l);
4918 }
4919 break;
4920
Bram Moolenaarb2049902021-01-24 12:53:53 +01004921 case ISN_PROF_START:
4922 case ISN_PROF_END:
4923 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004924#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004925 funccall_T cookie;
4926 ufunc_T *cur_ufunc =
4927 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004928 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004929
4930 cookie.func = cur_ufunc;
4931 if (iptr->isn_type == ISN_PROF_START)
4932 {
4933 func_line_start(&cookie, iptr->isn_lnum);
4934 // if we get here the instruction is executed
4935 func_line_exec(&cookie);
4936 }
4937 else
4938 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004939#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004940 }
4941 break;
4942
Bram Moolenaare99d4222021-06-13 14:01:26 +02004943 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004944 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004945 break;
4946
Bram Moolenaar389df252020-07-09 21:20:47 +02004947 case ISN_SHUFFLE:
4948 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004949 typval_T tmp_tv;
4950 int item = iptr->isn_arg.shuffle.shfl_item;
4951 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004952
4953 tmp_tv = *STACK_TV_BOT(-item);
4954 for ( ; up > 0 && item > 1; --up)
4955 {
4956 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4957 --item;
4958 }
4959 *STACK_TV_BOT(-item) = tmp_tv;
4960 }
4961 break;
4962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004964 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004966 ectx->ec_where.wt_index = 0;
4967 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 break;
4969 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004970 continue;
4971
Bram Moolenaard032f342020-07-18 18:13:02 +02004972func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004973 // Restore previous function. If the frame pointer is where we started
4974 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004975 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004976 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004977
Bram Moolenaar4c137212021-04-19 16:48:48 +02004978 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004979 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004980 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004981 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004982
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004983on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004984 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004985 // If "emsg_silent" is set then ignore the error, unless it was set
4986 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004987 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004988 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004989 {
4990 // If a sequence of instructions causes an error while ":silent!"
4991 // was used, restore the stack length and jump ahead to restoring
4992 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004993 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004994 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004995 while (ectx->ec_stack.ga_len
4996 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004997 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004998 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004999 clear_tv(STACK_TV_BOT(0));
5000 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005001 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5002 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005003 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005004 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005005 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005006on_fatal_error:
5007 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005008 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005009 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005010 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 }
5012
5013done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005014 ret = OK;
5015theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005016 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005017 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5018 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005019}
5020
5021/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005022 * Execute the instructions from a VAR_INSTR typeval and put the result in
5023 * "rettv".
5024 * Return OK or FAIL.
5025 */
5026 int
5027exe_typval_instr(typval_T *tv, typval_T *rettv)
5028{
5029 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5030 isn_T *save_instr = ectx->ec_instr;
5031 int save_iidx = ectx->ec_iidx;
5032 int res;
5033
LemonBoyf3b48952022-05-05 13:53:03 +01005034 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5035 // even when the compilation fails.
5036 rettv->v_type = VAR_UNKNOWN;
5037
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005038 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5039 res = exec_instructions(ectx);
5040 if (res == OK)
5041 {
5042 *rettv = *STACK_TV_BOT(-1);
5043 --ectx->ec_stack.ga_len;
5044 }
5045
5046 ectx->ec_instr = save_instr;
5047 ectx->ec_iidx = save_iidx;
5048
5049 return res;
5050}
5051
5052/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005053 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5054 * "substitute_instr".
5055 */
5056 char_u *
5057exe_substitute_instr(void)
5058{
5059 ectx_T *ectx = substitute_instr->subs_ectx;
5060 isn_T *save_instr = ectx->ec_instr;
5061 int save_iidx = ectx->ec_iidx;
5062 char_u *res;
5063
5064 ectx->ec_instr = substitute_instr->subs_instr;
5065 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005066 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005067 typval_T *tv = STACK_TV_BOT(-1);
5068
Bram Moolenaar27523602021-06-05 21:36:19 +02005069 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005070 --ectx->ec_stack.ga_len;
5071 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005072 }
5073 else
5074 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005075 substitute_instr->subs_status = FAIL;
5076 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078
Bram Moolenaar4c137212021-04-19 16:48:48 +02005079 ectx->ec_instr = save_instr;
5080 ectx->ec_iidx = save_iidx;
5081
5082 return res;
5083}
5084
5085/*
5086 * Call a "def" function from old Vim script.
5087 * Return OK or FAIL.
5088 */
5089 int
5090call_def_function(
5091 ufunc_T *ufunc,
5092 int argc_arg, // nr of arguments
5093 typval_T *argv, // arguments
5094 partial_T *partial, // optional partial for context
5095 typval_T *rettv) // return value
5096{
5097 ectx_T ectx; // execution context
5098 int argc = argc_arg;
5099 typval_T *tv;
5100 int idx;
5101 int ret = FAIL;
5102 int defcount = ufunc->uf_args.ga_len - argc;
5103 sctx_T save_current_sctx = current_sctx;
5104 int did_emsg_before = did_emsg_cumul + did_emsg;
5105 int save_suppress_errthrow = suppress_errthrow;
5106 msglist_T **saved_msg_list = NULL;
5107 msglist_T *private_msg_list = NULL;
5108 int save_emsg_silent_def = emsg_silent_def;
5109 int save_did_emsg_def = did_emsg_def;
5110 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005111 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005112
5113// Get pointer to item in the stack.
5114#undef STACK_TV
5115#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5116
5117// Get pointer to item at the bottom of the stack, -1 is the bottom.
5118#undef STACK_TV_BOT
5119#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5120
5121// Get pointer to a local variable on the stack. Negative for arguments.
5122#undef STACK_TV_VAR
5123#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5124
5125 if (ufunc->uf_def_status == UF_NOT_COMPILED
5126 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005127 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5128 && compile_def_function(ufunc, FALSE,
5129 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005130 {
5131 if (did_emsg_cumul + did_emsg == did_emsg_before)
5132 semsg(_(e_function_is_not_compiled_str),
5133 printable_func_name(ufunc));
5134 return FAIL;
5135 }
5136
5137 {
5138 // Check the function was really compiled.
5139 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5140 + ufunc->uf_dfunc_idx;
5141 if (INSTRUCTIONS(dfunc) == NULL)
5142 {
5143 iemsg("using call_def_function() on not compiled function");
5144 return FAIL;
5145 }
5146 }
5147
5148 // If depth of calling is getting too high, don't execute the function.
5149 orig_funcdepth = funcdepth_get();
5150 if (funcdepth_increment() == FAIL)
5151 return FAIL;
5152
5153 CLEAR_FIELD(ectx);
5154 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5155 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005156 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005157 {
5158 funcdepth_decrement();
5159 return FAIL;
5160 }
5161 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5162 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5163 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005164 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005165
5166 idx = argc - ufunc->uf_args.ga_len;
5167 if (idx > 0 && ufunc->uf_va_name == NULL)
5168 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005169 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
5170 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005171 goto failed_early;
5172 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005173 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5174 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005175 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005176 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
5177 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005178 goto failed_early;
5179 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005180
5181 // Put arguments on the stack, but no more than what the function expects.
5182 // A lambda can be called with more arguments than it uses.
5183 for (idx = 0; idx < argc
5184 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5185 ++idx)
5186 {
5187 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5188 && argv[idx].v_type == VAR_SPECIAL
5189 && argv[idx].vval.v_number == VVAL_NONE)
5190 {
5191 // Use the default value.
5192 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5193 }
5194 else
5195 {
5196 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5197 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005198 ufunc->uf_arg_types[idx], &argv[idx],
5199 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005200 goto failed_early;
5201 copy_tv(&argv[idx], STACK_TV_BOT(0));
5202 }
5203 ++ectx.ec_stack.ga_len;
5204 }
5205
5206 // Turn varargs into a list. Empty list if no args.
5207 if (ufunc->uf_va_name != NULL)
5208 {
5209 int vararg_count = argc - ufunc->uf_args.ga_len;
5210
5211 if (vararg_count < 0)
5212 vararg_count = 0;
5213 else
5214 argc -= vararg_count;
5215 if (exe_newlist(vararg_count, &ectx) == FAIL)
5216 goto failed_early;
5217
5218 // Check the type of the list items.
5219 tv = STACK_TV_BOT(-1);
5220 if (ufunc->uf_va_type != NULL
5221 && ufunc->uf_va_type != &t_list_any
5222 && ufunc->uf_va_type->tt_member != &t_any
5223 && tv->vval.v_list != NULL)
5224 {
5225 type_T *expected = ufunc->uf_va_type->tt_member;
5226 listitem_T *li = tv->vval.v_list->lv_first;
5227
5228 for (idx = 0; idx < vararg_count; ++idx)
5229 {
5230 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005231 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005232 goto failed_early;
5233 li = li->li_next;
5234 }
5235 }
5236
5237 if (defcount > 0)
5238 // Move varargs list to below missing default arguments.
5239 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5240 --ectx.ec_stack.ga_len;
5241 }
5242
5243 // Make space for omitted arguments, will store default value below.
5244 // Any varargs list goes after them.
5245 if (defcount > 0)
5246 for (idx = 0; idx < defcount; ++idx)
5247 {
5248 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5249 ++ectx.ec_stack.ga_len;
5250 }
5251 if (ufunc->uf_va_name != NULL)
5252 ++ectx.ec_stack.ga_len;
5253
5254 // Frame pointer points to just after arguments.
5255 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5256 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5257
5258 {
5259 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5260 + ufunc->uf_dfunc_idx;
5261 ufunc_T *base_ufunc = dfunc->df_ufunc;
5262
5263 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5264 // by copy_func().
5265 if (partial != NULL || base_ufunc->uf_partial != NULL)
5266 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005267 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5268 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005269 goto failed_early;
5270 if (partial != NULL)
5271 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005272 outer_T *outer = get_pt_outer(partial);
5273
5274 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005275 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005276 if (current_ectx != NULL)
5277 {
5278 if (current_ectx->ec_outer_ref != NULL
5279 && current_ectx->ec_outer_ref->or_outer != NULL)
5280 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005281 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005282 }
5283 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005284 }
5285 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005286 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005287 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005288 ++partial->pt_refcount;
5289 ectx.ec_outer_ref->or_partial = partial;
5290 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005291 }
5292 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005293 {
5294 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5295 ++base_ufunc->uf_partial->pt_refcount;
5296 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5297 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005298 }
5299 }
5300
5301 // dummy frame entries
5302 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5303 {
5304 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5305 ++ectx.ec_stack.ga_len;
5306 }
5307
5308 {
5309 // Reserve space for local variables and any closure reference count.
5310 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5311 + ufunc->uf_dfunc_idx;
5312
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005313 // Initialize variables to zero. That avoids having to generate
5314 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005315 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005316 {
5317 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5318 STACK_TV_VAR(idx)->vval.v_number = 0;
5319 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005320 ectx.ec_stack.ga_len += dfunc->df_varcount;
5321 if (dfunc->df_has_closure)
5322 {
5323 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5324 STACK_TV_VAR(idx)->vval.v_number = 0;
5325 ++ectx.ec_stack.ga_len;
5326 }
5327
5328 ectx.ec_instr = INSTRUCTIONS(dfunc);
5329 }
5330
5331 // Following errors are in the function, not the caller.
5332 // Commands behave like vim9script.
5333 estack_push_ufunc(ufunc, 1);
5334 current_sctx = ufunc->uf_script_ctx;
5335 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5336
5337 // Use a specific location for storing error messages to be converted to an
5338 // exception.
5339 saved_msg_list = msg_list;
5340 msg_list = &private_msg_list;
5341
5342 // Do turn errors into exceptions.
5343 suppress_errthrow = FALSE;
5344
5345 // Do not delete the function while executing it.
5346 ++ufunc->uf_calls;
5347
5348 // When ":silent!" was used before calling then we still abort the
5349 // function. If ":silent!" is used in the function then we don't.
5350 emsg_silent_def = emsg_silent;
5351 did_emsg_def = 0;
5352
5353 ectx.ec_where.wt_index = 0;
5354 ectx.ec_where.wt_variable = FALSE;
5355
5356 // Execute the instructions until done.
5357 ret = exec_instructions(&ectx);
5358 if (ret == OK)
5359 {
5360 // function finished, get result from the stack.
5361 if (ufunc->uf_ret_type == &t_void)
5362 {
5363 rettv->v_type = VAR_VOID;
5364 }
5365 else
5366 {
5367 tv = STACK_TV_BOT(-1);
5368 *rettv = *tv;
5369 tv->v_type = VAR_UNKNOWN;
5370 }
5371 }
5372
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005373 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005374 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5375 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005376
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005377 // Deal with any remaining closures, they may be in use somewhere.
5378 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005379 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005380 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005381 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005382 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005383
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005384 estack_pop();
5385 current_sctx = save_current_sctx;
5386
Bram Moolenaar2336c372021-12-06 15:06:54 +00005387 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5388 // Function was unreferenced while being used, free it now.
5389 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005390
Bram Moolenaar352134b2020-10-17 22:04:08 +02005391 if (*msg_list != NULL && saved_msg_list != NULL)
5392 {
5393 msglist_T **plist = saved_msg_list;
5394
5395 // Append entries from the current msg_list (uncaught exceptions) to
5396 // the saved msg_list.
5397 while (*plist != NULL)
5398 plist = &(*plist)->next;
5399
5400 *plist = *msg_list;
5401 }
5402 msg_list = saved_msg_list;
5403
Bram Moolenaar4c137212021-04-19 16:48:48 +02005404 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005405 {
5406 cmdmod.cmod_filter_regmatch.regprog = NULL;
5407 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005408 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005409 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005410 emsg_silent_def = save_emsg_silent_def;
5411 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005412
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005413failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005414 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005415 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5416 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005417 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005419 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005420 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005421 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005422 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005423 if (ectx.ec_outer_ref->or_outer_allocated)
5424 vim_free(ectx.ec_outer_ref->or_outer);
5425 partial_unref(ectx.ec_outer_ref->or_partial);
5426 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005427 }
5428
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005429 // Not sure if this is necessary.
5430 suppress_errthrow = save_suppress_errthrow;
5431
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005432 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5433 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005434 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005435 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005436 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005437 return ret;
5438}
5439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005440/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005441 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5442 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5443 * "pfx" is prefixed to every line.
5444 */
5445 static void
5446list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5447{
5448 int line_idx = 0;
5449 int prev_current = 0;
5450 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005451 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005452
5453 for (current = 0; current < instr_count; ++current)
5454 {
5455 isn_T *iptr = &instr[current];
5456 char *line;
5457
5458 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005459 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005460 while (line_idx < iptr->isn_lnum
5461 && line_idx < ufunc->uf_lines.ga_len)
5462 {
5463 if (current > prev_current)
5464 {
5465 msg_puts("\n\n");
5466 prev_current = current;
5467 }
5468 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5469 if (line != NULL)
5470 msg(line);
5471 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005472 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5473 {
5474 int first_def_arg = ufunc->uf_args.ga_len
5475 - ufunc->uf_def_args.ga_len;
5476
5477 if (def_arg_idx > 0)
5478 msg_puts("\n\n");
5479 msg_start();
5480 msg_puts(" ");
5481 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5482 first_def_arg + def_arg_idx]);
5483 msg_puts(" = ");
5484 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5485 msg_clr_eos();
5486 msg_end();
5487 }
5488 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005489
5490 switch (iptr->isn_type)
5491 {
5492 case ISN_EXEC:
5493 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5494 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005495 case ISN_EXEC_SPLIT:
5496 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5497 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005498 case ISN_EXECRANGE:
5499 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5500 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005501 case ISN_LEGACY_EVAL:
5502 smsg("%s%4d EVAL legacy %s", pfx, current,
5503 iptr->isn_arg.string);
5504 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005505 case ISN_REDIRSTART:
5506 smsg("%s%4d REDIR", pfx, current);
5507 break;
5508 case ISN_REDIREND:
5509 smsg("%s%4d REDIR END%s", pfx, current,
5510 iptr->isn_arg.number ? " append" : "");
5511 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005512 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005513#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005514 smsg("%s%4d CEXPR pre %s", pfx, current,
5515 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005516#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005517 break;
5518 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005519#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005520 {
5521 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5522
5523 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5524 cexpr_get_auname(cer->cer_cmdidx),
5525 cer->cer_forceit ? "!" : "",
5526 cer->cer_cmdline);
5527 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005528#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005529 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005530 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005531 smsg("%s%4d INSTR", pfx, current);
5532 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5533 msg(" -------------");
5534 break;
5535 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005536 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005537 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5538
5539 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005540 }
5541 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005542 case ISN_SUBSTITUTE:
5543 {
5544 subs_T *subs = &iptr->isn_arg.subs;
5545
5546 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5547 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5548 msg(" -------------");
5549 }
5550 break;
5551 case ISN_EXECCONCAT:
5552 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5553 (varnumber_T)iptr->isn_arg.number);
5554 break;
5555 case ISN_ECHO:
5556 {
5557 echo_T *echo = &iptr->isn_arg.echo;
5558
5559 smsg("%s%4d %s %d", pfx, current,
5560 echo->echo_with_white ? "ECHO" : "ECHON",
5561 echo->echo_count);
5562 }
5563 break;
5564 case ISN_EXECUTE:
5565 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005566 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005567 break;
5568 case ISN_ECHOMSG:
5569 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005570 (varnumber_T)(iptr->isn_arg.number));
5571 break;
5572 case ISN_ECHOCONSOLE:
5573 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5574 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005575 break;
5576 case ISN_ECHOERR:
5577 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005578 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005579 break;
5580 case ISN_LOAD:
5581 {
5582 if (iptr->isn_arg.number < 0)
5583 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5584 (varnumber_T)(iptr->isn_arg.number
5585 + STACK_FRAME_SIZE));
5586 else
5587 smsg("%s%4d LOAD $%lld", pfx, current,
5588 (varnumber_T)(iptr->isn_arg.number));
5589 }
5590 break;
5591 case ISN_LOADOUTER:
5592 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005593 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005594 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5595 iptr->isn_arg.outer.outer_depth,
5596 iptr->isn_arg.outer.outer_idx
5597 + STACK_FRAME_SIZE);
5598 else
5599 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5600 iptr->isn_arg.outer.outer_depth,
5601 iptr->isn_arg.outer.outer_idx);
5602 }
5603 break;
5604 case ISN_LOADV:
5605 smsg("%s%4d LOADV v:%s", pfx, current,
5606 get_vim_var_name(iptr->isn_arg.number));
5607 break;
5608 case ISN_LOADSCRIPT:
5609 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005610 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5611 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5612 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005613
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005614 sv = get_script_svar(sref, -1);
5615 if (sv == NULL)
5616 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5617 pfx, current, si->sn_name);
5618 else
5619 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005620 sv->sv_name,
5621 sref->sref_idx,
5622 si->sn_name);
5623 }
5624 break;
5625 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005626 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005627 {
5628 scriptitem_T *si = SCRIPT_ITEM(
5629 iptr->isn_arg.loadstore.ls_sid);
5630
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005631 smsg("%s%4d %s s:%s from %s", pfx, current,
5632 iptr->isn_type == ISN_LOADS ? "LOADS"
5633 : "LOADEXPORT",
5634 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005635 }
5636 break;
5637 case ISN_LOADAUTO:
5638 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5639 break;
5640 case ISN_LOADG:
5641 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5642 break;
5643 case ISN_LOADB:
5644 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5645 break;
5646 case ISN_LOADW:
5647 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5648 break;
5649 case ISN_LOADT:
5650 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5651 break;
5652 case ISN_LOADGDICT:
5653 smsg("%s%4d LOAD g:", pfx, current);
5654 break;
5655 case ISN_LOADBDICT:
5656 smsg("%s%4d LOAD b:", pfx, current);
5657 break;
5658 case ISN_LOADWDICT:
5659 smsg("%s%4d LOAD w:", pfx, current);
5660 break;
5661 case ISN_LOADTDICT:
5662 smsg("%s%4d LOAD t:", pfx, current);
5663 break;
5664 case ISN_LOADOPT:
5665 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5666 break;
5667 case ISN_LOADENV:
5668 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5669 break;
5670 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005671 smsg("%s%4d LOADREG @%c", pfx, current,
5672 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005673 break;
5674
5675 case ISN_STORE:
5676 if (iptr->isn_arg.number < 0)
5677 smsg("%s%4d STORE arg[%lld]", pfx, current,
5678 iptr->isn_arg.number + STACK_FRAME_SIZE);
5679 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005680 smsg("%s%4d STORE $%lld", pfx, current,
5681 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005682 break;
5683 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005684 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5685 iptr->isn_arg.outer.outer_depth,
5686 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005687 break;
5688 case ISN_STOREV:
5689 smsg("%s%4d STOREV v:%s", pfx, current,
5690 get_vim_var_name(iptr->isn_arg.number));
5691 break;
5692 case ISN_STOREAUTO:
5693 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5694 break;
5695 case ISN_STOREG:
5696 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5697 break;
5698 case ISN_STOREB:
5699 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5700 break;
5701 case ISN_STOREW:
5702 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5703 break;
5704 case ISN_STORET:
5705 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5706 break;
5707 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005708 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005709 {
5710 scriptitem_T *si = SCRIPT_ITEM(
5711 iptr->isn_arg.loadstore.ls_sid);
5712
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005713 smsg("%s%4d %s %s in %s", pfx, current,
5714 iptr->isn_type == ISN_STORES
5715 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005716 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5717 }
5718 break;
5719 case ISN_STORESCRIPT:
5720 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005721 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5722 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5723 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005724
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005725 sv = get_script_svar(sref, -1);
5726 if (sv == NULL)
5727 smsg("%s%4d STORESCRIPT [deleted] in %s",
5728 pfx, current, si->sn_name);
5729 else
5730 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005731 sv->sv_name,
5732 sref->sref_idx,
5733 si->sn_name);
5734 }
5735 break;
5736 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005737 case ISN_STOREFUNCOPT:
5738 smsg("%s%4d %s &%s", pfx, current,
5739 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005740 iptr->isn_arg.storeopt.so_name);
5741 break;
5742 case ISN_STOREENV:
5743 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5744 break;
5745 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005746 smsg("%s%4d STOREREG @%c", pfx, current,
5747 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005748 break;
5749 case ISN_STORENR:
5750 smsg("%s%4d STORE %lld in $%d", pfx, current,
5751 iptr->isn_arg.storenr.stnr_val,
5752 iptr->isn_arg.storenr.stnr_idx);
5753 break;
5754
5755 case ISN_STOREINDEX:
5756 smsg("%s%4d STOREINDEX %s", pfx, current,
5757 vartype_name(iptr->isn_arg.vartype));
5758 break;
5759
5760 case ISN_STORERANGE:
5761 smsg("%s%4d STORERANGE", pfx, current);
5762 break;
5763
5764 // constants
5765 case ISN_PUSHNR:
5766 smsg("%s%4d PUSHNR %lld", pfx, current,
5767 (varnumber_T)(iptr->isn_arg.number));
5768 break;
5769 case ISN_PUSHBOOL:
5770 case ISN_PUSHSPEC:
5771 smsg("%s%4d PUSH %s", pfx, current,
5772 get_var_special_name(iptr->isn_arg.number));
5773 break;
5774 case ISN_PUSHF:
5775#ifdef FEAT_FLOAT
5776 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5777#endif
5778 break;
5779 case ISN_PUSHS:
5780 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5781 break;
5782 case ISN_PUSHBLOB:
5783 {
5784 char_u *r;
5785 char_u numbuf[NUMBUFLEN];
5786 char_u *tofree;
5787
5788 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5789 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5790 vim_free(tofree);
5791 }
5792 break;
5793 case ISN_PUSHFUNC:
5794 {
5795 char *name = (char *)iptr->isn_arg.string;
5796
5797 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5798 name == NULL ? "[none]" : name);
5799 }
5800 break;
5801 case ISN_PUSHCHANNEL:
5802#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005803 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005804#endif
5805 break;
5806 case ISN_PUSHJOB:
5807#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005808 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005809#endif
5810 break;
5811 case ISN_PUSHEXC:
5812 smsg("%s%4d PUSH v:exception", pfx, current);
5813 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005814 case ISN_AUTOLOAD:
5815 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5816 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005817 case ISN_UNLET:
5818 smsg("%s%4d UNLET%s %s", pfx, current,
5819 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5820 iptr->isn_arg.unlet.ul_name);
5821 break;
5822 case ISN_UNLETENV:
5823 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5824 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5825 iptr->isn_arg.unlet.ul_name);
5826 break;
5827 case ISN_UNLETINDEX:
5828 smsg("%s%4d UNLETINDEX", pfx, current);
5829 break;
5830 case ISN_UNLETRANGE:
5831 smsg("%s%4d UNLETRANGE", pfx, current);
5832 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005833 case ISN_LOCKUNLOCK:
5834 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5835 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005836 case ISN_LOCKCONST:
5837 smsg("%s%4d LOCKCONST", pfx, current);
5838 break;
5839 case ISN_NEWLIST:
5840 smsg("%s%4d NEWLIST size %lld", pfx, current,
5841 (varnumber_T)(iptr->isn_arg.number));
5842 break;
5843 case ISN_NEWDICT:
5844 smsg("%s%4d NEWDICT size %lld", pfx, current,
5845 (varnumber_T)(iptr->isn_arg.number));
5846 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005847 case ISN_NEWPARTIAL:
5848 smsg("%s%4d NEWPARTIAL", pfx, current);
5849 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005850
5851 // function call
5852 case ISN_BCALL:
5853 {
5854 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5855
5856 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5857 internal_func_name(cbfunc->cbf_idx),
5858 cbfunc->cbf_argcount);
5859 }
5860 break;
5861 case ISN_DCALL:
5862 {
5863 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5864 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5865 + cdfunc->cdf_idx;
5866
5867 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005868 printable_func_name(df->df_ufunc),
5869 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005870 }
5871 break;
5872 case ISN_UCALL:
5873 {
5874 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5875
5876 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5877 cufunc->cuf_name, cufunc->cuf_argcount);
5878 }
5879 break;
5880 case ISN_PCALL:
5881 {
5882 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5883
5884 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5885 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5886 }
5887 break;
5888 case ISN_PCALL_END:
5889 smsg("%s%4d PCALL end", pfx, current);
5890 break;
5891 case ISN_RETURN:
5892 smsg("%s%4d RETURN", pfx, current);
5893 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005894 case ISN_RETURN_VOID:
5895 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005896 break;
5897 case ISN_FUNCREF:
5898 {
5899 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005900 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005901
Bram Moolenaar38453522021-11-28 22:00:12 +00005902 if (funcref->fr_func_name == NULL)
5903 {
5904 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5905 + funcref->fr_dfunc_idx;
5906 name = df->df_ufunc->uf_name;
5907 }
5908 else
5909 name = funcref->fr_func_name;
5910 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005911 }
5912 break;
5913
5914 case ISN_NEWFUNC:
5915 {
5916 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5917
5918 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5919 newfunc->nf_lambda, newfunc->nf_global);
5920 }
5921 break;
5922
5923 case ISN_DEF:
5924 {
5925 char_u *name = iptr->isn_arg.string;
5926
5927 smsg("%s%4d DEF %s", pfx, current,
5928 name == NULL ? (char_u *)"" : name);
5929 }
5930 break;
5931
5932 case ISN_JUMP:
5933 {
5934 char *when = "?";
5935
5936 switch (iptr->isn_arg.jump.jump_when)
5937 {
5938 case JUMP_ALWAYS:
5939 when = "JUMP";
5940 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005941 case JUMP_NEVER:
5942 iemsg("JUMP_NEVER should not be used");
5943 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005944 case JUMP_AND_KEEP_IF_TRUE:
5945 when = "JUMP_AND_KEEP_IF_TRUE";
5946 break;
5947 case JUMP_IF_FALSE:
5948 when = "JUMP_IF_FALSE";
5949 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005950 case JUMP_IF_COND_FALSE:
5951 when = "JUMP_IF_COND_FALSE";
5952 break;
5953 case JUMP_IF_COND_TRUE:
5954 when = "JUMP_IF_COND_TRUE";
5955 break;
5956 }
5957 smsg("%s%4d %s -> %d", pfx, current, when,
5958 iptr->isn_arg.jump.jump_where);
5959 }
5960 break;
5961
5962 case ISN_JUMP_IF_ARG_SET:
5963 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5964 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5965 iptr->isn_arg.jump.jump_where);
5966 break;
5967
5968 case ISN_FOR:
5969 {
5970 forloop_T *forloop = &iptr->isn_arg.forloop;
5971
5972 smsg("%s%4d FOR $%d -> %d", pfx, current,
5973 forloop->for_idx, forloop->for_end);
5974 }
5975 break;
5976
5977 case ISN_TRY:
5978 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005979 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005980
5981 if (try->try_ref->try_finally == 0)
5982 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5983 pfx, current,
5984 try->try_ref->try_catch,
5985 try->try_ref->try_endtry);
5986 else
5987 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5988 pfx, current,
5989 try->try_ref->try_catch,
5990 try->try_ref->try_finally,
5991 try->try_ref->try_endtry);
5992 }
5993 break;
5994 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005995 smsg("%s%4d CATCH", pfx, current);
5996 break;
5997 case ISN_TRYCONT:
5998 {
5999 trycont_T *trycont = &iptr->isn_arg.trycont;
6000
6001 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6002 trycont->tct_levels,
6003 trycont->tct_levels == 1 ? "" : "s",
6004 trycont->tct_where);
6005 }
6006 break;
6007 case ISN_FINALLY:
6008 smsg("%s%4d FINALLY", pfx, current);
6009 break;
6010 case ISN_ENDTRY:
6011 smsg("%s%4d ENDTRY", pfx, current);
6012 break;
6013 case ISN_THROW:
6014 smsg("%s%4d THROW", pfx, current);
6015 break;
6016
6017 // expression operations on number
6018 case ISN_OPNR:
6019 case ISN_OPFLOAT:
6020 case ISN_OPANY:
6021 {
6022 char *what;
6023 char *ins;
6024
6025 switch (iptr->isn_arg.op.op_type)
6026 {
6027 case EXPR_MULT: what = "*"; break;
6028 case EXPR_DIV: what = "/"; break;
6029 case EXPR_REM: what = "%"; break;
6030 case EXPR_SUB: what = "-"; break;
6031 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006032 case EXPR_LSHIFT: what = "<<"; break;
6033 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006034 default: what = "???"; break;
6035 }
6036 switch (iptr->isn_type)
6037 {
6038 case ISN_OPNR: ins = "OPNR"; break;
6039 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6040 case ISN_OPANY: ins = "OPANY"; break;
6041 default: ins = "???"; break;
6042 }
6043 smsg("%s%4d %s %s", pfx, current, ins, what);
6044 }
6045 break;
6046
6047 case ISN_COMPAREBOOL:
6048 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006049 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006050 case ISN_COMPARENR:
6051 case ISN_COMPAREFLOAT:
6052 case ISN_COMPARESTRING:
6053 case ISN_COMPAREBLOB:
6054 case ISN_COMPARELIST:
6055 case ISN_COMPAREDICT:
6056 case ISN_COMPAREFUNC:
6057 case ISN_COMPAREANY:
6058 {
6059 char *p;
6060 char buf[10];
6061 char *type;
6062
6063 switch (iptr->isn_arg.op.op_type)
6064 {
6065 case EXPR_EQUAL: p = "=="; break;
6066 case EXPR_NEQUAL: p = "!="; break;
6067 case EXPR_GREATER: p = ">"; break;
6068 case EXPR_GEQUAL: p = ">="; break;
6069 case EXPR_SMALLER: p = "<"; break;
6070 case EXPR_SEQUAL: p = "<="; break;
6071 case EXPR_MATCH: p = "=~"; break;
6072 case EXPR_IS: p = "is"; break;
6073 case EXPR_ISNOT: p = "isnot"; break;
6074 case EXPR_NOMATCH: p = "!~"; break;
6075 default: p = "???"; break;
6076 }
6077 STRCPY(buf, p);
6078 if (iptr->isn_arg.op.op_ic == TRUE)
6079 strcat(buf, "?");
6080 switch(iptr->isn_type)
6081 {
6082 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6083 case ISN_COMPARESPECIAL:
6084 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006085 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006086 case ISN_COMPARENR: type = "COMPARENR"; break;
6087 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6088 case ISN_COMPARESTRING:
6089 type = "COMPARESTRING"; break;
6090 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6091 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6092 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6093 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6094 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6095 default: type = "???"; break;
6096 }
6097
6098 smsg("%s%4d %s %s", pfx, current, type, buf);
6099 }
6100 break;
6101
6102 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6103 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6104
6105 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006106 case ISN_CONCAT:
6107 smsg("%s%4d CONCAT size %lld", pfx, current,
6108 (varnumber_T)(iptr->isn_arg.number));
6109 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006110 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6111 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6112 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6113 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6114 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6115 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6116 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6117 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6118 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6119 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6120 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006121 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006122 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6123 iptr->isn_arg.getitem.gi_index,
6124 iptr->isn_arg.getitem.gi_with_op ?
6125 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006126 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6127 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6128 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006129 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6130 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6131
Bram Moolenaar4c137212021-04-19 16:48:48 +02006132 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6133
Bram Moolenaar4c137212021-04-19 16:48:48 +02006134 case ISN_CHECKTYPE:
6135 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006136 checktype_T *ct = &iptr->isn_arg.type;
6137 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006138
6139 if (ct->ct_arg_idx == 0)
6140 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6141 type_name(ct->ct_type, &tofree),
6142 (int)ct->ct_off);
6143 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006144 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006145 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006146 type_name(ct->ct_type, &tofree),
6147 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006148 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006149 (int)ct->ct_arg_idx);
6150 vim_free(tofree);
6151 break;
6152 }
6153 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6154 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6155 iptr->isn_arg.checklen.cl_min_len);
6156 break;
6157 case ISN_SETTYPE:
6158 {
6159 char *tofree;
6160
6161 smsg("%s%4d SETTYPE %s", pfx, current,
6162 type_name(iptr->isn_arg.type.ct_type, &tofree));
6163 vim_free(tofree);
6164 break;
6165 }
6166 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006167 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6168 smsg("%s%4d INVERT %d (!val)", pfx, current,
6169 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006170 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006171 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6172 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006173 break;
6174 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006175 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006176 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006177 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6178 pfx, current,
6179 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006180 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006181 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6182 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006183 break;
6184 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006185 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006186 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006187 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006188 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6189 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006190 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006191 else
6192 smsg("%s%4d PUT %c %ld", pfx, current,
6193 iptr->isn_arg.put.put_regname,
6194 (long)iptr->isn_arg.put.put_lnum);
6195 break;
6196
Bram Moolenaar4c137212021-04-19 16:48:48 +02006197 case ISN_CMDMOD:
6198 {
6199 char_u *buf;
6200 size_t len = produce_cmdmods(
6201 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6202
6203 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006204 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006205 {
6206 (void)produce_cmdmods(
6207 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6208 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6209 vim_free(buf);
6210 }
6211 break;
6212 }
6213 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6214
6215 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006216 smsg("%s%4d PROFILE START line %d", pfx, current,
6217 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006218 break;
6219
6220 case ISN_PROF_END:
6221 smsg("%s%4d PROFILE END", pfx, current);
6222 break;
6223
Bram Moolenaare99d4222021-06-13 14:01:26 +02006224 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006225 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6226 iptr->isn_arg.debug.dbg_break_lnum + 1,
6227 iptr->isn_lnum,
6228 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006229 break;
6230
Bram Moolenaar4c137212021-04-19 16:48:48 +02006231 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6232 iptr->isn_arg.unpack.unp_count,
6233 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6234 break;
6235 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6236 iptr->isn_arg.shuffle.shfl_item,
6237 iptr->isn_arg.shuffle.shfl_up);
6238 break;
6239 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6240
6241 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6242 return;
6243 }
6244
6245 out_flush(); // output one line at a time
6246 ui_breakcheck();
6247 if (got_int)
6248 break;
6249 }
6250}
6251
6252/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006253 * Handle command line completion for the :disassemble command.
6254 */
6255 void
6256set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6257{
6258 char_u *p;
6259
6260 // Default: expand user functions, "debug" and "profile"
6261 xp->xp_context = EXPAND_DISASSEMBLE;
6262 xp->xp_pattern = arg;
6263
6264 // first argument already typed: only user function names
6265 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6266 {
6267 xp->xp_context = EXPAND_USER_FUNC;
6268 xp->xp_pattern = skipwhite(p);
6269 }
6270}
6271
6272/*
6273 * Function given to ExpandGeneric() to obtain the list of :disassemble
6274 * arguments.
6275 */
6276 char_u *
6277get_disassemble_argument(expand_T *xp, int idx)
6278{
6279 if (idx == 0)
6280 return (char_u *)"debug";
6281 if (idx == 1)
6282 return (char_u *)"profile";
6283 return get_user_func_name(xp, idx - 2);
6284}
6285
6286/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006287 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006288 * We don't really need this at runtime, but we do have tests that require it,
6289 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290 */
6291 void
6292ex_disassemble(exarg_T *eap)
6293{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006294 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006295 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006297 isn_T *instr = NULL; // init to shut up gcc warning
6298 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006299 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006300
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006301 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006302 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006304 if (func_needs_compiling(ufunc, compile_type)
6305 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006306 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006307 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006309 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310 return;
6311 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006312 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313
6314 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006315 switch (compile_type)
6316 {
6317 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006318#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006319 instr = dfunc->df_instr_prof;
6320 instr_count = dfunc->df_instr_prof_count;
6321 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006322#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006323 // FALLTHROUGH
6324 case CT_NONE:
6325 instr = dfunc->df_instr;
6326 instr_count = dfunc->df_instr_count;
6327 break;
6328 case CT_DEBUG:
6329 instr = dfunc->df_instr_debug;
6330 instr_count = dfunc->df_instr_debug_count;
6331 break;
6332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333
Bram Moolenaar4c137212021-04-19 16:48:48 +02006334 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006335}
6336
6337/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006338 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006339 * list, etc. Mostly like what JavaScript does, except that empty list and
6340 * empty dictionary are FALSE.
6341 */
6342 int
6343tv2bool(typval_T *tv)
6344{
6345 switch (tv->v_type)
6346 {
6347 case VAR_NUMBER:
6348 return tv->vval.v_number != 0;
6349 case VAR_FLOAT:
6350#ifdef FEAT_FLOAT
6351 return tv->vval.v_float != 0.0;
6352#else
6353 break;
6354#endif
6355 case VAR_PARTIAL:
6356 return tv->vval.v_partial != NULL;
6357 case VAR_FUNC:
6358 case VAR_STRING:
6359 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6360 case VAR_LIST:
6361 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6362 case VAR_DICT:
6363 return tv->vval.v_dict != NULL
6364 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6365 case VAR_BOOL:
6366 case VAR_SPECIAL:
6367 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6368 case VAR_JOB:
6369#ifdef FEAT_JOB_CHANNEL
6370 return tv->vval.v_job != NULL;
6371#else
6372 break;
6373#endif
6374 case VAR_CHANNEL:
6375#ifdef FEAT_JOB_CHANNEL
6376 return tv->vval.v_channel != NULL;
6377#else
6378 break;
6379#endif
6380 case VAR_BLOB:
6381 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6382 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006383 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006384 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006385 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006386 break;
6387 }
6388 return FALSE;
6389}
6390
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006391 void
6392emsg_using_string_as(typval_T *tv, int as_number)
6393{
6394 semsg(_(as_number ? e_using_string_as_number_str
6395 : e_using_string_as_bool_str),
6396 tv->vval.v_string == NULL
6397 ? (char_u *)"" : tv->vval.v_string);
6398}
6399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400/*
6401 * If "tv" is a string give an error and return FAIL.
6402 */
6403 int
6404check_not_string(typval_T *tv)
6405{
6406 if (tv->v_type == VAR_STRING)
6407 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006408 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409 clear_tv(tv);
6410 return FAIL;
6411 }
6412 return OK;
6413}
6414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415
6416#endif // FEAT_EVAL