blob: 5410aa28411a69e0d8b97e470418f61ddfc3739e [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 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200481 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200482 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200483 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200484 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200485 return FAIL;
486 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000487 else if (arg_to_add > ufunc->uf_def_args.ga_len)
488 {
489 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
490
491 if (missing == 1)
492 emsg(_(e_one_argument_too_few));
493 else
494 semsg(_(e_nr_arguments_too_few), missing);
495 return FAIL;
496 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200497
498 // Reserve space for:
499 // - missing arguments
500 // - stack frame
501 // - local variables
502 // - if needed: a counter for number of closures created in
503 // ectx->ec_funcrefs.
504 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200505 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 return FAIL;
507
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100508 // If depth of calling is getting too high, don't execute the function.
509 if (funcdepth_increment() == FAIL)
510 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200511 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100512
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100513 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200514 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100515 {
516 floc = ALLOC_ONE(funclocal_T);
517 if (floc == NULL)
518 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200519 *floc = ectx->ec_funclocal;
520 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100521 }
522
Bram Moolenaarfe270812020-04-11 22:31:27 +0200523 // Move the vararg-list to below the missing optional arguments.
524 if (vararg_count > 0 && arg_to_add > 0)
525 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100526
527 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200528 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200529 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200530 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531
532 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100533 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
534 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200535 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200536 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
537 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100538 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100539 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200540 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541
542 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200543 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000544 {
545 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
546
547 tv->v_type = VAR_NUMBER;
548 tv->vval.v_number = 0;
549 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200550 if (dfunc->df_has_closure)
551 {
552 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
553
554 tv->v_type = VAR_NUMBER;
555 tv->vval.v_number = 0;
556 }
557 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100559 if (pt != NULL || ufunc->uf_partial != NULL
560 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100561 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200562 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100563
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200564 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100565 return FAIL;
566 if (pt != NULL)
567 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000568 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200569 ++pt->pt_refcount;
570 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100571 }
572 else if (ufunc->uf_partial != NULL)
573 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000574 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200575 ++ufunc->uf_partial->pt_refcount;
576 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100577 }
578 else
579 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200580 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200581 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200582 {
583 vim_free(ref);
584 return FAIL;
585 }
586 ref->or_outer_allocated = TRUE;
587 ref->or_outer->out_stack = &ectx->ec_stack;
588 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
589 if (ectx->ec_outer_ref != NULL)
590 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100591 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200592 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100593 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100594 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200595 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100596
Bram Moolenaarc970e422021-03-17 15:03:04 +0100597 ++ufunc->uf_calls;
598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 // Set execution state to the start of the called function.
600 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100601 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100602 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200603 if (entry != NULL)
604 {
605 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200606 // Save the current context so it can be restored on return.
607 entry->es_save_sctx = current_sctx;
608 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200609 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100610
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200611 // Start execution at the first instruction.
612 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613
614 return OK;
615}
616
617// Get pointer to item in the stack.
618#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
619
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000620// Double linked list of funcstack_T in use.
621static funcstack_T *first_funcstack = NULL;
622
623 static void
624add_funcstack_to_list(funcstack_T *funcstack)
625{
626 // Link in list of funcstacks.
627 if (first_funcstack != NULL)
628 first_funcstack->fs_prev = funcstack;
629 funcstack->fs_next = first_funcstack;
630 funcstack->fs_prev = NULL;
631 first_funcstack = funcstack;
632}
633
634 static void
635remove_funcstack_from_list(funcstack_T *funcstack)
636{
637 if (funcstack->fs_prev == NULL)
638 first_funcstack = funcstack->fs_next;
639 else
640 funcstack->fs_prev->fs_next = funcstack->fs_next;
641 if (funcstack->fs_next != NULL)
642 funcstack->fs_next->fs_prev = funcstack->fs_prev;
643}
644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200646 * Used when returning from a function: Check if any closure is still
647 * referenced. If so then move the arguments and variables to a separate piece
648 * of stack to be used when the closure is called.
649 * When "free_arguments" is TRUE the arguments are to be freed.
650 * Returns FAIL when out of memory.
651 */
652 static int
653handle_closure_in_use(ectx_T *ectx, int free_arguments)
654{
655 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
656 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200657 int argcount;
658 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200659 int idx;
660 typval_T *tv;
661 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200662 garray_T *gap = &ectx->ec_funcrefs;
663 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200664
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200665 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200666 return OK; // function was freed
667 if (dfunc->df_has_closure == 0)
668 return OK; // no closures
669 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
670 closure_count = tv->vval.v_number;
671 if (closure_count == 0)
672 return OK; // no funcrefs created
673
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200674 argcount = ufunc_argcount(dfunc->df_ufunc);
675 top = ectx->ec_frame_idx - argcount;
676
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200677 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200678 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200679 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200680 partial_T *pt;
681 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200682
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200683 if (off < 0)
684 continue; // count is off or already done
685 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200686 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200687 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200688 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200689 int i;
690
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000691 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200692 // unreferenced on return.
693 for (i = 0; i < dfunc->df_varcount; ++i)
694 {
695 typval_T *stv = STACK_TV(ectx->ec_frame_idx
696 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200697 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200698 --refcount;
699 }
700 if (refcount > 1)
701 {
702 closure_in_use = TRUE;
703 break;
704 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200705 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200706 }
707
708 if (closure_in_use)
709 {
710 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
711 typval_T *stack;
712
713 // A closure is using the arguments and/or local variables.
714 // Move them to the called function.
715 if (funcstack == NULL)
716 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000717
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200718 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
719 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
721 funcstack->fs_ga.ga_data = stack;
722 if (stack == NULL)
723 {
724 vim_free(funcstack);
725 return FAIL;
726 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000727 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200728
729 // Move or copy the arguments.
730 for (idx = 0; idx < argcount; ++idx)
731 {
732 tv = STACK_TV(top + idx);
733 if (free_arguments)
734 {
735 *(stack + idx) = *tv;
736 tv->v_type = VAR_UNKNOWN;
737 }
738 else
739 copy_tv(tv, stack + idx);
740 }
741 // Move the local variables.
742 for (idx = 0; idx < dfunc->df_varcount; ++idx)
743 {
744 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200745
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200746 // A partial created for a local function, that is also used as a
747 // local variable, has a reference count for the variable, thus
748 // will never go down to zero. When all these refcounts are one
749 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000750 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200751 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
752 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200753 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200754
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200755 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200756 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
757 gap->ga_len - closure_count + i])
758 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200759 }
760
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200761 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200762 tv->v_type = VAR_UNKNOWN;
763 }
764
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200765 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200766 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200767 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
768 - closure_count + idx];
769 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200770 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200771 ++funcstack->fs_refcount;
772 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100773 pt->pt_outer.out_stack = &funcstack->fs_ga;
774 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200775 }
776 }
777 }
778
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200779 for (idx = 0; idx < closure_count; ++idx)
780 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
781 - closure_count + idx]);
782 gap->ga_len -= closure_count;
783 if (gap->ga_len == 0)
784 ga_clear(gap);
785
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200786 return OK;
787}
788
789/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200790 * Called when a partial is freed or its reference count goes down to one. The
791 * funcstack may be the only reference to the partials in the local variables.
792 * Go over all of them, the funcref and can be freed if all partials
793 * referencing the funcstack have a reference count of one.
794 */
795 void
796funcstack_check_refcount(funcstack_T *funcstack)
797{
798 int i;
799 garray_T *gap = &funcstack->fs_ga;
800 int done = 0;
801
802 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
803 return;
804 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
805 {
806 typval_T *tv = ((typval_T *)gap->ga_data) + i;
807
808 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
809 && tv->vval.v_partial->pt_funcstack == funcstack
810 && tv->vval.v_partial->pt_refcount == 1)
811 ++done;
812 }
813 if (done == funcstack->fs_min_refcount)
814 {
815 typval_T *stack = gap->ga_data;
816
817 // All partials referencing the funcstack have a reference count of
818 // one, thus the funcstack is no longer of use.
819 for (i = 0; i < gap->ga_len; ++i)
820 clear_tv(stack + i);
821 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000822 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200823 vim_free(funcstack);
824 }
825}
826
827/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000828 * For garbage collecting: set references in all variables referenced by
829 * all funcstacks.
830 */
831 int
832set_ref_in_funcstacks(int copyID)
833{
834 funcstack_T *funcstack;
835
836 for (funcstack = first_funcstack; funcstack != NULL;
837 funcstack = funcstack->fs_next)
838 {
839 typval_T *stack = funcstack->fs_ga.ga_data;
840 int i;
841
842 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
843 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
844 return TRUE; // abort
845 }
846 return FALSE;
847}
848
849/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 * Return from the current function.
851 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200852 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200853func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100855 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100856 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200857 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
858 + ectx->ec_dfunc_idx;
859 int argcount = ufunc_argcount(dfunc->df_ufunc);
860 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200861 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100862 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
863 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200864 funclocal_T *floc;
865#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100866 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
867 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868
Bram Moolenaar12d26532021-02-19 19:13:21 +0100869 if (do_profiling == PROF_YES)
870 {
871 ufunc_T *caller = prev_dfunc->df_ufunc;
872
873 if (dfunc->df_ufunc->uf_profiling
874 || (caller != NULL && caller->uf_profiling))
875 {
876 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
877 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
878 --profile_info_ga.ga_len;
879 }
880 }
881#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000882
883 // No check for uf_refcount being zero, cannot think of a way that would
884 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100885 --dfunc->df_ufunc->uf_calls;
886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200888 entry = estack_pop();
889 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200890 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200892 if (handle_closure_in_use(ectx, TRUE) == FAIL)
893 return FAIL;
894
895 // Clear the arguments.
896 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
897 clear_tv(STACK_TV(idx));
898
899 // Clear local variables and temp values, but not the return value.
900 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100901 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100903
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100904 // The return value should be on top of the stack. However, when aborting
905 // it may not be there and ec_frame_idx is the top of the stack.
906 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100907 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100908 ret_idx = 0;
909
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200910 if (ectx->ec_outer_ref != NULL)
911 {
912 if (ectx->ec_outer_ref->or_outer_allocated)
913 vim_free(ectx->ec_outer_ref->or_outer);
914 partial_unref(ectx->ec_outer_ref->or_partial);
915 vim_free(ectx->ec_outer_ref);
916 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100917
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100918 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100919 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100920 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
921 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200922 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
923 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200924 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100925 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100926 floc = (void *)STACK_TV(ectx->ec_frame_idx
927 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200928 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100929 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
930 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200931
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100932 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200933 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100934 else
935 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200936 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100937 vim_free(floc);
938 }
939
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100940 if (ret_idx > 0)
941 {
942 // Reset the stack to the position before the call, with a spot for the
943 // return value, moved there from above the frame.
944 ectx->ec_stack.ga_len = top + 1;
945 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
946 }
947 else
948 // Reset the stack to the position before the call.
949 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200950
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100951 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200952 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200953 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954}
955
956#undef STACK_TV
957
958/*
959 * Prepare arguments and rettv for calling a builtin or user function.
960 */
961 static int
962call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
963{
964 int idx;
965 typval_T *tv;
966
967 // Move arguments from bottom of the stack to argvars[] and add terminator.
968 for (idx = 0; idx < argcount; ++idx)
969 argvars[idx] = *STACK_TV_BOT(idx - argcount);
970 argvars[argcount].v_type = VAR_UNKNOWN;
971
972 // Result replaces the arguments on the stack.
973 if (argcount > 0)
974 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200975 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 return FAIL;
977 else
978 ++ectx->ec_stack.ga_len;
979
980 // Default return value is zero.
981 tv = STACK_TV_BOT(-1);
982 tv->v_type = VAR_NUMBER;
983 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +0100984 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985
986 return OK;
987}
988
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200989// Ugly global to avoid passing the execution context around through many
990// layers.
991static ectx_T *current_ectx = NULL;
992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993/*
994 * Call a builtin function by index.
995 */
996 static int
997call_bfunc(int func_idx, int argcount, ectx_T *ectx)
998{
999 typval_T argvars[MAX_FUNC_ARGS];
1000 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001001 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001002 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001003 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004
1005 if (call_prepare(argcount, argvars, ectx) == FAIL)
1006 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001007 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001009 // Call the builtin function. Set "current_ectx" so that when it
1010 // recursively invokes call_def_function() a closure context can be set.
1011 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001013 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001014 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015
1016 // Clear the arguments.
1017 for (idx = 0; idx < argcount; ++idx)
1018 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001019
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001020 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001021 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 return OK;
1023}
1024
1025/*
1026 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001027 * If the function is compiled this will add a stack frame and set the
1028 * instruction pointer at the start of the function.
1029 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001030 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001031 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 */
1033 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001034call_ufunc(
1035 ufunc_T *ufunc,
1036 partial_T *pt,
1037 int argcount,
1038 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001039 isn_T *iptr,
1040 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041{
1042 typval_T argvars[MAX_FUNC_ARGS];
1043 funcexe_T funcexe;
1044 int error;
1045 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001046 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001047 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048
Bram Moolenaare99d4222021-06-13 14:01:26 +02001049 if (func_needs_compiling(ufunc, compile_type)
1050 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1051 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001052 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001053 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001054 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001055 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001056 if (error != FCERR_UNKNOWN)
1057 {
1058 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001059 semsg(_(e_too_many_arguments_for_function_str),
1060 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001061 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001062 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001063 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001064 return FAIL;
1065 }
1066
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001067 // The function has been compiled, can call it quickly. For a function
1068 // that was defined later: we can call it directly next time.
1069 if (iptr != NULL)
1070 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001071 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001072 iptr->isn_type = ISN_DCALL;
1073 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1074 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1075 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001076 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078
1079 if (call_prepare(argcount, argvars, ectx) == FAIL)
1080 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001081 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001082 funcexe.fe_evaluate = TRUE;
1083 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084
1085 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001087 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088
1089 // Clear the arguments.
1090 for (idx = 0; idx < argcount; ++idx)
1091 clear_tv(&argvars[idx]);
1092
1093 if (error != FCERR_NONE)
1094 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001095 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 return FAIL;
1097 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001098 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001099 // Error other than from calling the function itself.
1100 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 return OK;
1102}
1103
1104/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001105 * If command modifiers were applied restore them.
1106 */
1107 static void
1108may_restore_cmdmod(funclocal_T *funclocal)
1109{
1110 if (funclocal->floc_restore_cmdmod)
1111 {
1112 cmdmod.cmod_filter_regmatch.regprog = NULL;
1113 undo_cmdmod(&cmdmod);
1114 cmdmod = funclocal->floc_save_cmdmod;
1115 funclocal->floc_restore_cmdmod = FALSE;
1116 }
1117}
1118
1119/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001120 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1121 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001122 */
1123 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001124vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001125{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001126 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001127}
1128
1129/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 * Execute a function by "name".
1131 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001132 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 * Returns FAIL if not found without an error message.
1134 */
1135 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001136call_by_name(
1137 char_u *name,
1138 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001139 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001140 isn_T *iptr,
1141 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142{
1143 ufunc_T *ufunc;
1144
1145 if (builtin_function(name, -1))
1146 {
1147 int func_idx = find_internal_func(name);
1148
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001149 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001151 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 return FAIL;
1153 return call_bfunc(func_idx, argcount, ectx);
1154 }
1155
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001156 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001157
1158 if (ufunc == NULL)
1159 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001160 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001161
1162 if (script_autoload(name, TRUE))
1163 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001164 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001165
1166 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001167 return FAIL; // bail out if loading the script caused an error
1168 }
1169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001171 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001172 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001173 {
1174 int i;
1175 typval_T *argv = STACK_TV_BOT(0) - argcount;
1176
1177 // The function can change at runtime, check that the argument
1178 // types are correct.
1179 for (i = 0; i < argcount; ++i)
1180 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001181 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001182
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001183 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001184 type = ufunc->uf_arg_types[i];
1185 else if (ufunc->uf_va_type != NULL)
1186 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001187 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001188 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001189 return FAIL;
1190 }
1191 }
1192
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001193 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195
1196 return FAIL;
1197}
1198
1199 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001200call_partial(
1201 typval_T *tv,
1202 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001203 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001205 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001206 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001208 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001209 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210
1211 if (tv->v_type == VAR_PARTIAL)
1212 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001213 partial_T *pt = tv->vval.v_partial;
1214 int i;
1215
1216 if (pt->pt_argc > 0)
1217 {
1218 // Make space for arguments from the partial, shift the "argcount"
1219 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001220 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001221 return FAIL;
1222 for (i = 1; i <= argcount; ++i)
1223 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1224 ectx->ec_stack.ga_len += pt->pt_argc;
1225 argcount += pt->pt_argc;
1226
1227 // copy the arguments from the partial onto the stack
1228 for (i = 0; i < pt->pt_argc; ++i)
1229 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1230 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001231 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232
1233 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001234 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001236 name = pt->pt_name;
1237 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001238 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001240 if (name != NULL)
1241 {
1242 char_u fname_buf[FLEN_FIXED + 1];
1243 char_u *tofree = NULL;
1244 int error = FCERR_NONE;
1245 char_u *fname;
1246
1247 // May need to translate <SNR>123_ to K_SNR.
1248 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1249 if (error != FCERR_NONE)
1250 res = FAIL;
1251 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001252 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001253 vim_free(tofree);
1254 }
1255
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001256 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 {
1258 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001259 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001260 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 return FAIL;
1262 }
1263 return OK;
1264}
1265
1266/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001267 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1268 * TRUE.
1269 */
1270 static int
1271error_if_locked(int lock, char *error)
1272{
1273 if (lock & (VAR_LOCKED | VAR_FIXED))
1274 {
1275 emsg(_(error));
1276 return TRUE;
1277 }
1278 return FALSE;
1279}
1280
1281/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001282 * Give an error if "tv" is not a number and return FAIL.
1283 */
1284 static int
1285check_for_number(typval_T *tv)
1286{
1287 if (tv->v_type != VAR_NUMBER)
1288 {
1289 semsg(_(e_expected_str_but_got_str),
1290 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1291 return FAIL;
1292 }
1293 return OK;
1294}
1295
1296/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001297 * Store "tv" in variable "name".
1298 * This is for s: and g: variables.
1299 */
1300 static void
1301store_var(char_u *name, typval_T *tv)
1302{
1303 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001304 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001305
Bram Moolenaard877a572021-04-01 19:42:48 +02001306 if (tv->v_lock)
1307 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001308 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001309 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001310 restore_funccal();
1311}
1312
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001313/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001314 * Convert "tv" to a string.
1315 * Return FAIL if not allowed.
1316 */
1317 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001318do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001319{
1320 if (tv->v_type != VAR_STRING)
1321 {
1322 char_u *str;
1323
1324 if (is_2string_any)
1325 {
1326 switch (tv->v_type)
1327 {
1328 case VAR_SPECIAL:
1329 case VAR_BOOL:
1330 case VAR_NUMBER:
1331 case VAR_FLOAT:
1332 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001333
1334 case VAR_LIST:
1335 if (tolerant)
1336 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001337 char_u *s, *e, *p;
1338 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001339
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001340 ga_init2(&ga, sizeof(char_u *), 1);
1341
1342 // Convert to NL separated items, then
1343 // escape the items and replace the NL with
1344 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001345 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001346 if (str == NULL)
1347 return FAIL;
1348 s = str;
1349 while ((e = vim_strchr(s, '\n')) != NULL)
1350 {
1351 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001352 p = vim_strsave_fnameescape(s,
1353 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001354 if (p != NULL)
1355 {
1356 ga_concat(&ga, p);
1357 ga_concat(&ga, (char_u *)" ");
1358 vim_free(p);
1359 }
1360 s = e + 1;
1361 }
1362 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001363 clear_tv(tv);
1364 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001365 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001366 return OK;
1367 }
1368 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001369 default: to_string_error(tv->v_type);
1370 return FAIL;
1371 }
1372 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001373 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001374 clear_tv(tv);
1375 tv->v_type = VAR_STRING;
1376 tv->vval.v_string = str;
1377 }
1378 return OK;
1379}
1380
1381/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001382 * When the value of "sv" is a null list of dict, allocate it.
1383 */
1384 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001385allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001386{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001387 typval_T *tv = sv->sv_tv;
1388
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001389 switch (tv->v_type)
1390 {
1391 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001392 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001393 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001394 break;
1395 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001396 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001397 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001398 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001399 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001400 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001401 (void)rettv_blob_alloc(tv);
1402 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001403 default:
1404 break;
1405 }
1406}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001407
Bram Moolenaare7525c52021-01-09 13:20:37 +01001408/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001409 * Return the character "str[index]" where "index" is the character index,
1410 * including composing characters.
1411 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001412 */
1413 char_u *
1414char_from_string(char_u *str, varnumber_T index)
1415{
1416 size_t nbyte = 0;
1417 varnumber_T nchar = index;
1418 size_t slen;
1419
1420 if (str == NULL)
1421 return NULL;
1422 slen = STRLEN(str);
1423
Bram Moolenaarff871402021-03-26 13:34:05 +01001424 // Do the same as for a list: a negative index counts from the end.
1425 // Optimization to check the first byte to be below 0x80 (and no composing
1426 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001427 if (index < 0)
1428 {
1429 int clen = 0;
1430
1431 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001432 {
1433 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1434 ++nbyte;
1435 else if (enc_utf8)
1436 nbyte += utfc_ptr2len(str + nbyte);
1437 else
1438 nbyte += mb_ptr2len(str + nbyte);
1439 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001440 nchar = clen + index;
1441 if (nchar < 0)
1442 // unlike list: index out of range results in empty string
1443 return NULL;
1444 }
1445
1446 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001447 {
1448 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1449 ++nbyte;
1450 else if (enc_utf8)
1451 nbyte += utfc_ptr2len(str + nbyte);
1452 else
1453 nbyte += mb_ptr2len(str + nbyte);
1454 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001455 if (nbyte >= slen)
1456 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001457 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001458}
1459
1460/*
1461 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001462 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001463 * If going over the end return "str_len".
1464 * If "idx" is negative count from the end, -1 is the last character.
1465 * When going over the start return -1.
1466 */
1467 static long
1468char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1469{
1470 varnumber_T nchar = idx;
1471 size_t nbyte = 0;
1472
1473 if (nchar >= 0)
1474 {
1475 while (nchar > 0 && nbyte < str_len)
1476 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001477 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001478 --nchar;
1479 }
1480 }
1481 else
1482 {
1483 nbyte = str_len;
1484 while (nchar < 0 && nbyte > 0)
1485 {
1486 --nbyte;
1487 nbyte -= mb_head_off(str, str + nbyte);
1488 ++nchar;
1489 }
1490 if (nchar < 0)
1491 return -1;
1492 }
1493 return (long)nbyte;
1494}
1495
1496/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001497 * Return the slice "str[first : last]" using character indexes. Composing
1498 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001499 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001500 * Return NULL when the result is empty.
1501 */
1502 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001503string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001504{
1505 long start_byte, end_byte;
1506 size_t slen;
1507
1508 if (str == NULL)
1509 return NULL;
1510 slen = STRLEN(str);
1511 start_byte = char_idx2byte(str, slen, first);
1512 if (start_byte < 0)
1513 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001514 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001515 end_byte = (long)slen;
1516 else
1517 {
1518 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001519 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001520 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001521 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001522 }
1523
1524 if (start_byte >= (long)slen || end_byte <= start_byte)
1525 return NULL;
1526 return vim_strnsave(str + start_byte, end_byte - start_byte);
1527}
1528
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001529/*
1530 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1531 * When "dfunc_idx" is negative don't give an error.
1532 * Returns NULL for an error.
1533 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001534 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001535get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001536{
1537 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001538 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1539 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001540 svar_T *sv;
1541
1542 if (sref->sref_seq != si->sn_script_seq)
1543 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001544 // The script was reloaded after the function was compiled, the
1545 // script_idx may not be valid.
1546 if (dfunc != NULL)
1547 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1548 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001549 return NULL;
1550 }
1551 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001552 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001553 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001554 if (dfunc != NULL)
1555 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001556 return NULL;
1557 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001558
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001559 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1560 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001561 {
1562 if (dfunc != NULL)
1563 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1564 return NULL;
1565 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001566 return sv;
1567}
1568
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001569/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001570 * Function passed to do_cmdline() for splitting a script joined by NL
1571 * characters.
1572 */
1573 static char_u *
1574get_split_sourceline(
1575 int c UNUSED,
1576 void *cookie,
1577 int indent UNUSED,
1578 getline_opt_T options UNUSED)
1579{
1580 source_cookie_T *sp = (source_cookie_T *)cookie;
1581 char_u *p;
1582 char_u *line;
1583
Bram Moolenaar20677332021-06-06 17:02:53 +02001584 p = vim_strchr(sp->nextline, '\n');
1585 if (p == NULL)
1586 {
1587 line = vim_strsave(sp->nextline);
1588 sp->nextline += STRLEN(sp->nextline);
1589 }
1590 else
1591 {
1592 line = vim_strnsave(sp->nextline, p - sp->nextline);
1593 sp->nextline = p + 1;
1594 }
1595 return line;
1596}
1597
1598/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 * Execute a function by "name".
1600 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001601 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 */
1603 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001604call_eval_func(
1605 char_u *name,
1606 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001607 ectx_T *ectx,
1608 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609{
Bram Moolenaared677f52020-08-12 16:38:10 +02001610 int called_emsg_before = called_emsg;
1611 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001613 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001614 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001616 dictitem_T *v;
1617
1618 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001619 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1620 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001621 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001622 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001623 return FAIL;
1624 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001625 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001627 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628}
1629
1630/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001631 * When a function reference is used, fill a partial with the information
1632 * needed, especially when it is used as a closure.
1633 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001634 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001635fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1636{
1637 pt->pt_func = ufunc;
1638 pt->pt_refcount = 1;
1639
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001640 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001641 {
1642 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1643 + ectx->ec_dfunc_idx;
1644
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001645 // The closure may need to find arguments and local variables in the
1646 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001647 pt->pt_outer.out_stack = &ectx->ec_stack;
1648 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001649 if (ectx->ec_outer_ref != NULL)
1650 {
1651 // The current context already has a context, link to that one.
1652 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1653 if (ectx->ec_outer_ref->or_partial != NULL)
1654 {
1655 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1656 ++pt->pt_outer.out_up_partial->pt_refcount;
1657 }
1658 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001659
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001660 // If this function returns and the closure is still being used, we
1661 // need to make a copy of the context (arguments and local variables).
1662 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001663 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001664 {
1665 vim_free(pt);
1666 return FAIL;
1667 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001668 // Extra variable keeps the count of closures created in the current
1669 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001670 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1671 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1672
1673 ((partial_T **)ectx->ec_funcrefs.ga_data)
1674 [ectx->ec_funcrefs.ga_len] = pt;
1675 ++pt->pt_refcount;
1676 ++ectx->ec_funcrefs.ga_len;
1677 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001678 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001679 return OK;
1680}
1681
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001682/*
1683 * Execute iptr->isn_arg.string as an Ex command.
1684 */
1685 static int
1686exec_command(isn_T *iptr)
1687{
1688 source_cookie_T cookie;
1689
1690 SOURCING_LNUM = iptr->isn_lnum;
1691 // Pass getsourceline to get an error for a missing ":end"
1692 // command.
1693 CLEAR_FIELD(cookie);
1694 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1695 if (do_cmdline(iptr->isn_arg.string,
1696 getsourceline, &cookie,
1697 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1698 || did_emsg)
1699 return FAIL;
1700 return OK;
1701}
1702
Bram Moolenaar89445512022-04-14 12:58:23 +01001703/*
1704 * If script "sid" is not loaded yet then load it now.
1705 * Caller must make sure "sid" is a valid script ID.
1706 * "loaded" is set to TRUE if the script had to be loaded.
1707 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1708 */
1709 int
1710may_load_script(int sid, int *loaded)
1711{
1712 scriptitem_T *si = SCRIPT_ITEM(sid);
1713
1714 if (si->sn_state == SN_STATE_NOT_LOADED)
1715 {
1716 *loaded = TRUE;
1717 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1718 {
1719 semsg(_(e_cant_open_file_str), si->sn_name);
1720 return FAIL;
1721 }
1722 }
1723 return OK;
1724}
1725
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001726// used for v_instr of typval of VAR_INSTR
1727struct instr_S {
1728 ectx_T *instr_ectx;
1729 isn_T *instr_instr;
1730};
1731
Bram Moolenaar4c137212021-04-19 16:48:48 +02001732// used for substitute_instr
1733typedef struct subs_expr_S {
1734 ectx_T *subs_ectx;
1735 isn_T *subs_instr;
1736 int subs_status;
1737} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738
1739// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001740#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741
1742// Get pointer to item at the bottom of the stack, -1 is the bottom.
1743#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001744#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001746// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001747#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001749// Set when calling do_debug().
1750static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001751static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001752
1753/*
1754 * When debugging lookup "name" and return the typeval.
1755 * When not found return NULL.
1756 */
1757 typval_T *
1758lookup_debug_var(char_u *name)
1759{
1760 int idx;
1761 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001762 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001763 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001764 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001765
1766 if (ectx == NULL)
1767 return NULL;
1768 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1769
1770 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001771 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001772 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001773 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1774
1775 // the variable name may be NULL when not available in this block
1776 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001777 return STACK_TV_VAR(idx);
1778 }
1779
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001780 // Go through argument names.
1781 ufunc = dfunc->df_ufunc;
1782 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1783 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1784 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1785 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1786 - varargs_off + idx);
1787 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1788 return STACK_TV(ectx->ec_frame_idx - 1);
1789
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001790 return NULL;
1791}
1792
Bram Moolenaar26a44842021-09-02 18:49:06 +02001793/*
1794 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1795 * breakpoint was set in that function or when there is any expression.
1796 */
1797 int
1798may_break_in_function(ufunc_T *ufunc)
1799{
1800 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1801}
1802
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001803 static void
1804handle_debug(isn_T *iptr, ectx_T *ectx)
1805{
1806 char_u *line;
1807 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1808 + ectx->ec_dfunc_idx)->df_ufunc;
1809 isn_T *ni;
1810 int end_lnum = iptr->isn_lnum;
1811 garray_T ga;
1812 int lnum;
1813
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001814 if (ex_nesting_level > debug_break_level)
1815 {
1816 linenr_T breakpoint;
1817
Bram Moolenaar26a44842021-09-02 18:49:06 +02001818 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001819 return;
1820
1821 // check for the next breakpoint if needed
1822 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001823 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001824 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1825 return;
1826 }
1827
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001828 SOURCING_LNUM = iptr->isn_lnum;
1829 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001830 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001831
1832 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1833 if (ni->isn_type == ISN_DEBUG
1834 || ni->isn_type == ISN_RETURN
1835 || ni->isn_type == ISN_RETURN_VOID)
1836 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001837 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001838 break;
1839 }
1840
1841 if (end_lnum > iptr->isn_lnum)
1842 {
1843 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001844 for (lnum = iptr->isn_lnum; lnum < end_lnum
1845 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001846 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001847 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001848
Bram Moolenaar303215d2021-07-07 20:10:43 +02001849 if (p == NULL)
1850 continue; // left over from continuation line
1851 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001852 if (*p == '#')
1853 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001854 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001855 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1856 if (STRNCMP(p, "def ", 4) == 0)
1857 break;
1858 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001859 line = ga_concat_strings(&ga, " ");
1860 vim_free(ga.ga_data);
1861 }
1862 else
1863 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001864
Dominique Pelle6e969552021-06-17 13:53:41 +02001865 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001866 debug_context = NULL;
1867
1868 if (end_lnum > iptr->isn_lnum)
1869 vim_free(line);
1870}
1871
Bram Moolenaar4c137212021-04-19 16:48:48 +02001872/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001873 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001874 * Returns OK, FAIL or NOTDONE (uncatchable error).
1875 */
1876 static int
1877execute_storeindex(isn_T *iptr, ectx_T *ectx)
1878{
1879 vartype_T dest_type = iptr->isn_arg.vartype;
1880 typval_T *tv;
1881 typval_T *tv_idx = STACK_TV_BOT(-2);
1882 typval_T *tv_dest = STACK_TV_BOT(-1);
1883 int status = OK;
1884
1885 // Stack contains:
1886 // -3 value to be stored
1887 // -2 index
1888 // -1 dict or list
1889 tv = STACK_TV_BOT(-3);
1890 SOURCING_LNUM = iptr->isn_lnum;
1891 if (dest_type == VAR_ANY)
1892 {
1893 dest_type = tv_dest->v_type;
1894 if (dest_type == VAR_DICT)
1895 status = do_2string(tv_idx, TRUE, FALSE);
1896 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1897 {
1898 emsg(_(e_number_expected));
1899 status = FAIL;
1900 }
1901 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001902
1903 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001904 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001905 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001906 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001907 long lidx = (long)tv_idx->vval.v_number;
1908 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001909
Bram Moolenaare08be092022-02-17 13:08:26 +00001910 if (list == NULL)
1911 {
1912 emsg(_(e_list_not_set));
1913 return FAIL;
1914 }
1915 if (lidx < 0 && list->lv_len + lidx >= 0)
1916 // negative index is relative to the end
1917 lidx = list->lv_len + lidx;
1918 if (lidx < 0 || lidx > list->lv_len)
1919 {
1920 semsg(_(e_list_index_out_of_range_nr), lidx);
1921 return FAIL;
1922 }
1923 if (lidx < list->lv_len)
1924 {
1925 listitem_T *li = list_find(list, lidx);
1926
1927 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001928 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001929 return FAIL;
1930 // overwrite existing list item
1931 clear_tv(&li->li_tv);
1932 li->li_tv = *tv;
1933 }
1934 else
1935 {
1936 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1937 return FAIL;
1938 // append to list, only fails when out of memory
1939 if (list_append_tv(list, tv) == FAIL)
1940 return NOTDONE;
1941 clear_tv(tv);
1942 }
1943 }
1944 else if (dest_type == VAR_DICT)
1945 {
1946 char_u *key = tv_idx->vval.v_string;
1947 dict_T *dict = tv_dest->vval.v_dict;
1948 dictitem_T *di;
1949
1950 SOURCING_LNUM = iptr->isn_lnum;
1951 if (dict == NULL)
1952 {
1953 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001954 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001955 }
1956 if (key == NULL)
1957 key = (char_u *)"";
1958 di = dict_find(dict, key, -1);
1959 if (di != NULL)
1960 {
1961 if (error_if_locked(di->di_tv.v_lock,
1962 e_cannot_change_dict_item))
1963 return FAIL;
1964 // overwrite existing value
1965 clear_tv(&di->di_tv);
1966 di->di_tv = *tv;
1967 }
1968 else
1969 {
1970 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1971 return FAIL;
1972 // add to dict, only fails when out of memory
1973 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1974 return NOTDONE;
1975 clear_tv(tv);
1976 }
1977 }
1978 else if (dest_type == VAR_BLOB)
1979 {
1980 long lidx = (long)tv_idx->vval.v_number;
1981 blob_T *blob = tv_dest->vval.v_blob;
1982 varnumber_T nr;
1983 int error = FALSE;
1984 int len;
1985
1986 if (blob == NULL)
1987 {
1988 emsg(_(e_blob_not_set));
1989 return FAIL;
1990 }
1991 len = blob_len(blob);
1992 if (lidx < 0 && len + lidx >= 0)
1993 // negative index is relative to the end
1994 lidx = len + lidx;
1995
1996 // Can add one byte at the end.
1997 if (lidx < 0 || lidx > len)
1998 {
1999 semsg(_(e_blob_index_out_of_range_nr), lidx);
2000 return FAIL;
2001 }
2002 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2003 return FAIL;
2004 nr = tv_get_number_chk(tv, &error);
2005 if (error)
2006 return FAIL;
2007 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002008 }
2009 else
2010 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002011 status = FAIL;
2012 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002013 }
2014 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002015
2016 clear_tv(tv_idx);
2017 clear_tv(tv_dest);
2018 ectx->ec_stack.ga_len -= 3;
2019 if (status == FAIL)
2020 {
2021 clear_tv(tv);
2022 return FAIL;
2023 }
2024 return OK;
2025}
2026
2027/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002028 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002029 */
2030 static int
2031execute_storerange(isn_T *iptr, ectx_T *ectx)
2032{
2033 typval_T *tv;
2034 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2035 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2036 typval_T *tv_dest = STACK_TV_BOT(-1);
2037 int status = OK;
2038
2039 // Stack contains:
2040 // -4 value to be stored
2041 // -3 first index or "none"
2042 // -2 second index or "none"
2043 // -1 destination list or blob
2044 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002045 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002046 if (tv_dest->v_type == VAR_LIST)
2047 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002048 long n1;
2049 long n2;
2050 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002051
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002052 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2053 if (tv_idx2->v_type == VAR_SPECIAL
2054 && tv_idx2->vval.v_number == VVAL_NONE)
2055 n2 = list_len(tv_dest->vval.v_list) - 1;
2056 else
2057 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2058
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002059 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002060 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002061 status = FAIL;
2062 else
2063 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002064 status = check_range_index_two(tv_dest->vval.v_list,
2065 &n1, li1, &n2, FALSE);
2066 if (status != FAIL)
2067 status = list_assign_range(
2068 tv_dest->vval.v_list,
2069 tv->vval.v_list,
2070 n1,
2071 n2,
2072 tv_idx2->v_type == VAR_SPECIAL,
2073 (char_u *)"=",
2074 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002075 }
2076 }
2077 else if (tv_dest->v_type == VAR_BLOB)
2078 {
2079 varnumber_T n1;
2080 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002081 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002082
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002083 n1 = tv_get_number_chk(tv_idx1, NULL);
2084 if (tv_idx2->v_type == VAR_SPECIAL
2085 && tv_idx2->vval.v_number == VVAL_NONE)
2086 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2087 else
2088 n2 = tv_get_number_chk(tv_idx2, NULL);
2089 bloblen = blob_len(tv_dest->vval.v_blob);
2090
2091 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2092 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002093 status = FAIL;
2094 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002095 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002096 }
2097 else
2098 {
2099 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002100 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002101 }
2102
2103 clear_tv(tv_idx1);
2104 clear_tv(tv_idx2);
2105 clear_tv(tv_dest);
2106 ectx->ec_stack.ga_len -= 4;
2107 clear_tv(tv);
2108
2109 return status;
2110}
2111
2112/*
2113 * Unlet item in list or dict variable.
2114 */
2115 static int
2116execute_unletindex(isn_T *iptr, ectx_T *ectx)
2117{
2118 typval_T *tv_idx = STACK_TV_BOT(-2);
2119 typval_T *tv_dest = STACK_TV_BOT(-1);
2120 int status = OK;
2121
2122 // Stack contains:
2123 // -2 index
2124 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002125 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002126 if (tv_dest->v_type == VAR_DICT)
2127 {
2128 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002129 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002130 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002131 semsg(_(e_expected_str_but_got_str),
2132 vartype_name(VAR_STRING),
2133 vartype_name(tv_idx->v_type));
2134 status = FAIL;
2135 }
2136 else
2137 {
2138 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002139 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002140 dictitem_T *di = NULL;
2141
2142 if (d != NULL && value_check_lock(
2143 d->dv_lock, NULL, FALSE))
2144 status = FAIL;
2145 else
2146 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002147 if (tv_idx->v_type == VAR_STRING)
2148 {
2149 key = tv_idx->vval.v_string;
2150 if (key == NULL)
2151 key = (char_u *)"";
2152 }
2153 else
2154 {
2155 key = tv_get_string(tv_idx);
2156 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002157 if (d != NULL)
2158 di = dict_find(d, key, (int)STRLEN(key));
2159 if (di == NULL)
2160 {
2161 // NULL dict is equivalent to empty dict
2162 semsg(_(e_key_not_present_in_dictionary),
2163 key);
2164 status = FAIL;
2165 }
2166 else if (var_check_fixed(di->di_flags,
2167 NULL, FALSE)
2168 || var_check_ro(di->di_flags,
2169 NULL, FALSE))
2170 status = FAIL;
2171 else
2172 dictitem_remove(d, di);
2173 }
2174 }
2175 }
2176 else if (tv_dest->v_type == VAR_LIST)
2177 {
2178 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002179 if (check_for_number(tv_idx) == FAIL)
2180 {
2181 status = FAIL;
2182 }
2183 else
2184 {
2185 list_T *l = tv_dest->vval.v_list;
2186 long n = (long)tv_idx->vval.v_number;
2187
2188 if (l != NULL && value_check_lock(
2189 l->lv_lock, NULL, FALSE))
2190 status = FAIL;
2191 else
2192 {
2193 listitem_T *li = list_find(l, n);
2194
2195 if (li == NULL)
2196 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002197 semsg(_(e_list_index_out_of_range_nr), n);
2198 status = FAIL;
2199 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002200 else
2201 listitem_remove(l, li);
2202 }
2203 }
2204 }
2205 else
2206 {
2207 status = FAIL;
2208 semsg(_(e_cannot_index_str),
2209 vartype_name(tv_dest->v_type));
2210 }
2211
2212 clear_tv(tv_idx);
2213 clear_tv(tv_dest);
2214 ectx->ec_stack.ga_len -= 2;
2215
2216 return status;
2217}
2218
2219/*
2220 * Unlet a range of items in a list variable.
2221 */
2222 static int
2223execute_unletrange(isn_T *iptr, ectx_T *ectx)
2224{
2225 // Stack contains:
2226 // -3 index1
2227 // -2 index2
2228 // -1 dict or list
2229 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2230 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2231 typval_T *tv_dest = STACK_TV_BOT(-1);
2232 int status = OK;
2233
2234 if (tv_dest->v_type == VAR_LIST)
2235 {
2236 // indexes must be a number
2237 SOURCING_LNUM = iptr->isn_lnum;
2238 if (check_for_number(tv_idx1) == FAIL
2239 || (tv_idx2->v_type != VAR_SPECIAL
2240 && check_for_number(tv_idx2) == FAIL))
2241 {
2242 status = FAIL;
2243 }
2244 else
2245 {
2246 list_T *l = tv_dest->vval.v_list;
2247 long n1 = (long)tv_idx1->vval.v_number;
2248 long n2 = tv_idx2->v_type == VAR_SPECIAL
2249 ? 0 : (long)tv_idx2->vval.v_number;
2250 listitem_T *li;
2251
2252 li = list_find_index(l, &n1);
2253 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002254 {
2255 semsg(_(e_list_index_out_of_range_nr),
2256 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002257 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002258 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002259 else
2260 {
2261 if (n1 < 0)
2262 n1 = list_idx_of_item(l, li);
2263 if (n2 < 0)
2264 {
2265 listitem_T *li2 = list_find(l, n2);
2266
2267 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002268 {
2269 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002270 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002271 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002272 else
2273 n2 = list_idx_of_item(l, li2);
2274 }
2275 if (status != FAIL
2276 && tv_idx2->v_type != VAR_SPECIAL
2277 && n2 < n1)
2278 {
2279 semsg(_(e_list_index_out_of_range_nr), n2);
2280 status = FAIL;
2281 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002282 if (status != FAIL)
2283 list_unlet_range(l, li, n1,
2284 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002285 }
2286 }
2287 }
2288 else
2289 {
2290 status = FAIL;
2291 SOURCING_LNUM = iptr->isn_lnum;
2292 semsg(_(e_cannot_index_str),
2293 vartype_name(tv_dest->v_type));
2294 }
2295
2296 clear_tv(tv_idx1);
2297 clear_tv(tv_idx2);
2298 clear_tv(tv_dest);
2299 ectx->ec_stack.ga_len -= 3;
2300
2301 return status;
2302}
2303
2304/*
2305 * Top of a for loop.
2306 */
2307 static int
2308execute_for(isn_T *iptr, ectx_T *ectx)
2309{
2310 typval_T *tv;
2311 typval_T *ltv = STACK_TV_BOT(-1);
2312 typval_T *idxtv =
2313 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2314
2315 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2316 return FAIL;
2317 if (ltv->v_type == VAR_LIST)
2318 {
2319 list_T *list = ltv->vval.v_list;
2320
2321 // push the next item from the list
2322 ++idxtv->vval.v_number;
2323 if (list == NULL
2324 || idxtv->vval.v_number >= list->lv_len)
2325 {
2326 // past the end of the list, jump to "endfor"
2327 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2328 may_restore_cmdmod(&ectx->ec_funclocal);
2329 }
2330 else if (list->lv_first == &range_list_item)
2331 {
2332 // non-materialized range() list
2333 tv = STACK_TV_BOT(0);
2334 tv->v_type = VAR_NUMBER;
2335 tv->v_lock = 0;
2336 tv->vval.v_number = list_find_nr(
2337 list, idxtv->vval.v_number, NULL);
2338 ++ectx->ec_stack.ga_len;
2339 }
2340 else
2341 {
2342 listitem_T *li = list_find(list,
2343 idxtv->vval.v_number);
2344
2345 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2346 ++ectx->ec_stack.ga_len;
2347 }
2348 }
2349 else if (ltv->v_type == VAR_STRING)
2350 {
2351 char_u *str = ltv->vval.v_string;
2352
2353 // The index is for the last byte of the previous
2354 // character.
2355 ++idxtv->vval.v_number;
2356 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2357 {
2358 // past the end of the string, jump to "endfor"
2359 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2360 may_restore_cmdmod(&ectx->ec_funclocal);
2361 }
2362 else
2363 {
2364 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2365
2366 // Push the next character from the string.
2367 tv = STACK_TV_BOT(0);
2368 tv->v_type = VAR_STRING;
2369 tv->vval.v_string = vim_strnsave(
2370 str + idxtv->vval.v_number, clen);
2371 ++ectx->ec_stack.ga_len;
2372 idxtv->vval.v_number += clen - 1;
2373 }
2374 }
2375 else if (ltv->v_type == VAR_BLOB)
2376 {
2377 blob_T *blob = ltv->vval.v_blob;
2378
2379 // When we get here the first time make a copy of the
2380 // blob, so that the iteration still works when it is
2381 // changed.
2382 if (idxtv->vval.v_number == -1 && blob != NULL)
2383 {
2384 blob_copy(blob, ltv);
2385 blob_unref(blob);
2386 blob = ltv->vval.v_blob;
2387 }
2388
2389 // The index is for the previous byte.
2390 ++idxtv->vval.v_number;
2391 if (blob == NULL
2392 || idxtv->vval.v_number >= blob_len(blob))
2393 {
2394 // past the end of the blob, jump to "endfor"
2395 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2396 may_restore_cmdmod(&ectx->ec_funclocal);
2397 }
2398 else
2399 {
2400 // Push the next byte from the blob.
2401 tv = STACK_TV_BOT(0);
2402 tv->v_type = VAR_NUMBER;
2403 tv->vval.v_number = blob_get(blob,
2404 idxtv->vval.v_number);
2405 ++ectx->ec_stack.ga_len;
2406 }
2407 }
2408 else
2409 {
2410 semsg(_(e_for_loop_on_str_not_supported),
2411 vartype_name(ltv->v_type));
2412 return FAIL;
2413 }
2414 return OK;
2415}
2416
2417/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002418 * Load instruction for w:/b:/g:/t: variable.
2419 * "isn_type" is used instead of "iptr->isn_type".
2420 */
2421 static int
2422load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2423{
2424 dictitem_T *di = NULL;
2425 hashtab_T *ht = NULL;
2426 char namespace;
2427
2428 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2429 return NOTDONE;
2430 switch (isn_type)
2431 {
2432 case ISN_LOADG:
2433 ht = get_globvar_ht();
2434 namespace = 'g';
2435 break;
2436 case ISN_LOADB:
2437 ht = &curbuf->b_vars->dv_hashtab;
2438 namespace = 'b';
2439 break;
2440 case ISN_LOADW:
2441 ht = &curwin->w_vars->dv_hashtab;
2442 namespace = 'w';
2443 break;
2444 case ISN_LOADT:
2445 ht = &curtab->tp_vars->dv_hashtab;
2446 namespace = 't';
2447 break;
2448 default: // Cannot reach here
2449 return NOTDONE;
2450 }
2451 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2452
Bram Moolenaar06b77222022-01-25 15:51:56 +00002453 if (di == NULL)
2454 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002455 if (isn_type == ISN_LOADG)
2456 {
2457 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2458
2459 // g:Something could be a function
2460 if (ufunc != NULL)
2461 {
2462 typval_T *tv = STACK_TV_BOT(0);
2463
2464 ++ectx->ec_stack.ga_len;
2465 tv->v_type = VAR_FUNC;
2466 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2467 if (tv->vval.v_string == NULL)
2468 return FAIL;
2469 STRCPY(tv->vval.v_string, "g:");
2470 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2471 return OK;
2472 }
2473 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002474 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002475 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002476 // no check if the item exists in the script but
2477 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002478 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002479 else
2480 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002481 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002482 return FAIL;
2483 }
2484 else
2485 {
2486 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2487 ++ectx->ec_stack.ga_len;
2488 }
2489 return OK;
2490}
2491
2492/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002493 * Execute instructions in execution context "ectx".
2494 * Return OK or FAIL;
2495 */
2496 static int
2497exec_instructions(ectx_T *ectx)
2498{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002499 int ret = FAIL;
2500 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002501 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002502
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002503 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002504 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002505
Bram Moolenaarff652882021-05-16 15:24:49 +02002506 // Only catch exceptions in this instruction list.
2507 ectx->ec_trylevel_at_start = trylevel;
2508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 for (;;)
2510 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002511 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002513 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002514
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002515 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002516 {
2517 line_breakcheck();
2518 breakcheck_count = 0;
2519 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002520 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002521 {
2522 // Turn CTRL-C into an exception.
2523 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002524 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002525 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002526 did_throw = TRUE;
2527 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002529 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002530 {
2531 // Turn an error message into an exception.
2532 did_emsg = FALSE;
2533 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002534 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002535 did_throw = TRUE;
2536 *msg_list = NULL;
2537 }
2538
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002539 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002541 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002542 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002543 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544
2545 // An exception jumps to the first catch, finally, or returns from
2546 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002547 while (index > 0)
2548 {
2549 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002550 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002551 break;
2552 // In the catch and finally block of this try we have to go up
2553 // one level.
2554 --index;
2555 trycmd = NULL;
2556 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002557 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002559 if (trycmd->tcd_in_catch)
2560 {
2561 // exception inside ":catch", jump to ":finally" once
2562 ectx->ec_iidx = trycmd->tcd_finally_idx;
2563 trycmd->tcd_finally_idx = 0;
2564 }
2565 else
2566 // jump to first ":catch"
2567 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002568 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002569 did_throw = FALSE; // don't come back here until :endtry
2570 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 }
2572 else
2573 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002574 // Not inside try or need to return from current functions.
2575 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002576 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002577 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002578 tv = STACK_TV_BOT(0);
2579 tv->v_type = VAR_NUMBER;
2580 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002581 ++ectx->ec_stack.ga_len;
2582 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002584 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002585 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002586 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002587 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 goto done;
2589 }
2590
Bram Moolenaar4c137212021-04-19 16:48:48 +02002591 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002592 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 }
2594 continue;
2595 }
2596
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002597 /*
2598 * Big switch on the instruction. Most compilers will be turning this
2599 * into an efficient lookup table, since the "case" values are an enum
2600 * with sequential numbers. It may look ugly, but it should be the
2601 * most efficient way.
2602 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002603 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 switch (iptr->isn_type)
2605 {
2606 // execute Ex command line
2607 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002608 if (exec_command(iptr) == FAIL)
2609 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 break;
2611
Bram Moolenaar20677332021-06-06 17:02:53 +02002612 // execute Ex command line split at NL characters.
2613 case ISN_EXEC_SPLIT:
2614 {
2615 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002616 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002617
2618 SOURCING_LNUM = iptr->isn_lnum;
2619 CLEAR_FIELD(cookie);
2620 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2621 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002622 line = get_split_sourceline(0, &cookie, 0, 0);
2623 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002624 get_split_sourceline, &cookie,
2625 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2626 == FAIL
2627 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002628 {
2629 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002630 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002631 }
2632 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002633 }
2634 break;
2635
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002636 // execute Ex command line that is only a range
2637 case ISN_EXECRANGE:
2638 {
2639 exarg_T ea;
2640 char *error = NULL;
2641
2642 CLEAR_FIELD(ea);
2643 ea.cmdidx = CMD_SIZE;
2644 ea.addr_type = ADDR_LINES;
2645 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002646 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002647 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002648 if (ea.cmd == NULL)
2649 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002650 // error is always NULL when using ADDR_LINES
2651 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002652 if (error != NULL)
2653 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002654 emsg(error);
2655 goto on_error;
2656 }
2657 }
2658 break;
2659
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002660 // Evaluate an expression with legacy syntax, push it onto the
2661 // stack.
2662 case ISN_LEGACY_EVAL:
2663 {
2664 char_u *arg = iptr->isn_arg.string;
2665 int res;
2666 int save_flags = cmdmod.cmod_flags;
2667
Bram Moolenaar35578162021-08-02 19:10:38 +02002668 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002669 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002670 tv = STACK_TV_BOT(0);
2671 init_tv(tv);
2672 cmdmod.cmod_flags |= CMOD_LEGACY;
2673 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2674 cmdmod.cmod_flags = save_flags;
2675 if (res == FAIL)
2676 goto on_error;
2677 ++ectx->ec_stack.ga_len;
2678 }
2679 break;
2680
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002681 // push typeval VAR_INSTR with instructions to be executed
2682 case ISN_INSTR:
2683 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002684 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002685 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002686 tv = STACK_TV_BOT(0);
2687 tv->vval.v_instr = ALLOC_ONE(instr_T);
2688 if (tv->vval.v_instr == NULL)
2689 goto on_error;
2690 ++ectx->ec_stack.ga_len;
2691
2692 tv->v_type = VAR_INSTR;
2693 tv->vval.v_instr->instr_ectx = ectx;
2694 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2695 }
2696 break;
2697
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002698 case ISN_SOURCE:
2699 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002700 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002701
Bram Moolenaar89445512022-04-14 12:58:23 +01002702 SOURCING_LNUM = iptr->isn_lnum;
2703 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002704 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002705 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002706 }
2707 break;
2708
Bram Moolenaar4c137212021-04-19 16:48:48 +02002709 // execute :substitute with an expression
2710 case ISN_SUBSTITUTE:
2711 {
2712 subs_T *subs = &iptr->isn_arg.subs;
2713 source_cookie_T cookie;
2714 struct subs_expr_S *save_instr = substitute_instr;
2715 struct subs_expr_S subs_instr;
2716 int res;
2717
2718 subs_instr.subs_ectx = ectx;
2719 subs_instr.subs_instr = subs->subs_instr;
2720 subs_instr.subs_status = OK;
2721 substitute_instr = &subs_instr;
2722
2723 SOURCING_LNUM = iptr->isn_lnum;
2724 // This is very much like ISN_EXEC
2725 CLEAR_FIELD(cookie);
2726 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2727 res = do_cmdline(subs->subs_cmd,
2728 getsourceline, &cookie,
2729 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2730 substitute_instr = save_instr;
2731
2732 if (res == FAIL || did_emsg
2733 || subs_instr.subs_status == FAIL)
2734 goto on_error;
2735 }
2736 break;
2737
2738 case ISN_FINISH:
2739 goto done;
2740
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002741 case ISN_REDIRSTART:
2742 // create a dummy entry for var_redir_str()
2743 if (alloc_redir_lval() == FAIL)
2744 goto on_error;
2745
2746 // The output is stored in growarray "redir_ga" until
2747 // redirection ends.
2748 init_redir_ga();
2749 redir_vname = 1;
2750 break;
2751
2752 case ISN_REDIREND:
2753 {
2754 char_u *res = get_clear_redir_ga();
2755
2756 // End redirection, put redirected text on the stack.
2757 clear_redir_lval();
2758 redir_vname = 0;
2759
Bram Moolenaar35578162021-08-02 19:10:38 +02002760 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002761 {
2762 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002763 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002764 }
2765 tv = STACK_TV_BOT(0);
2766 tv->v_type = VAR_STRING;
2767 tv->vval.v_string = res;
2768 ++ectx->ec_stack.ga_len;
2769 }
2770 break;
2771
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002772 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002773#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002774 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002775 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2776 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002777 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002778#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002779 break;
2780
2781 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002782#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002783 {
2784 exarg_T ea;
2785 int res;
2786
2787 CLEAR_FIELD(ea);
2788 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2789 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2790 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2791 --ectx->ec_stack.ga_len;
2792 tv = STACK_TV_BOT(0);
2793 res = cexpr_core(&ea, tv);
2794 clear_tv(tv);
2795 if (res == FAIL)
2796 goto on_error;
2797 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002798#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002799 break;
2800
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002801 // execute Ex command from pieces on the stack
2802 case ISN_EXECCONCAT:
2803 {
2804 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002805 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002806 int pass;
2807 int i;
2808 char_u *cmd = NULL;
2809 char_u *str;
2810
2811 for (pass = 1; pass <= 2; ++pass)
2812 {
2813 for (i = 0; i < count; ++i)
2814 {
2815 tv = STACK_TV_BOT(i - count);
2816 str = tv->vval.v_string;
2817 if (str != NULL && *str != NUL)
2818 {
2819 if (pass == 2)
2820 STRCPY(cmd + len, str);
2821 len += STRLEN(str);
2822 }
2823 if (pass == 2)
2824 clear_tv(tv);
2825 }
2826 if (pass == 1)
2827 {
2828 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002829 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002830 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002831 len = 0;
2832 }
2833 }
2834
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002835 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002836 do_cmdline_cmd(cmd);
2837 vim_free(cmd);
2838 }
2839 break;
2840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 // execute :echo {string} ...
2842 case ISN_ECHO:
2843 {
2844 int count = iptr->isn_arg.echo.echo_count;
2845 int atstart = TRUE;
2846 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002847 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848
2849 for (idx = 0; idx < count; ++idx)
2850 {
2851 tv = STACK_TV_BOT(idx - count);
2852 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2853 &atstart, &needclr);
2854 clear_tv(tv);
2855 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002856 if (needclr)
2857 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002858 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 }
2860 break;
2861
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002862 // :execute {string} ...
2863 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002864 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002865 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002866 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002867 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002868 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002869 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002870 {
2871 int count = iptr->isn_arg.number;
2872 garray_T ga;
2873 char_u buf[NUMBUFLEN];
2874 char_u *p;
2875 int len;
2876 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002877 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002878
2879 ga_init2(&ga, 1, 80);
2880 for (idx = 0; idx < count; ++idx)
2881 {
2882 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002883 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002884 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002885 if (tv->v_type == VAR_CHANNEL
2886 || tv->v_type == VAR_JOB)
2887 {
2888 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002889 semsg(_(e_using_invalid_value_as_string_str),
2890 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002891 break;
2892 }
2893 else
2894 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002895 }
2896 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002897 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002898
2899 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002900 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002901 failed = TRUE;
2902 else
2903 {
2904 if (ga.ga_len > 0)
2905 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2906 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2907 ga.ga_len += len;
2908 }
2909 clear_tv(tv);
2910 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002911 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002912 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002913 {
2914 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002915 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002916 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002917
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002918 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002919 {
2920 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002921 {
2922 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002923 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002924 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002925 {
2926 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002927 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002928 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002929 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002930 else
2931 {
2932 msg_sb_eol();
2933 if (iptr->isn_type == ISN_ECHOMSG)
2934 {
2935 msg_attr(ga.ga_data, echo_attr);
2936 out_flush();
2937 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002938 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2939 {
2940 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2941 TRUE);
2942 ui_write((char_u *)"\r\n", 2, TRUE);
2943 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002944 else
2945 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002946 SOURCING_LNUM = iptr->isn_lnum;
2947 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002948 }
2949 }
2950 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002951 ga_clear(&ga);
2952 }
2953 break;
2954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 // load local variable or argument
2956 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002957 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002958 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002960 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 break;
2962
2963 // load v: variable
2964 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002965 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002966 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002968 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 break;
2970
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002971 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 case ISN_LOADSCRIPT:
2973 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002974 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 svar_T *sv;
2976
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002977 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002978 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002979 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002980 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002981 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002982 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002984 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 }
2986 break;
2987
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002988 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002990 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002992 int sid = iptr->isn_arg.loadstore.ls_sid;
2993 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002994 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 if (di == NULL)
2998 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002999 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003000 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003001 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 }
3003 else
3004 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003005 if (iptr->isn_type == ISN_LOADEXPORT)
3006 {
3007 int idx = get_script_item_idx(sid, name, 0,
3008 NULL, NULL);
3009 svar_T *sv;
3010
3011 if (idx >= 0)
3012 {
3013 sv = ((svar_T *)SCRIPT_ITEM(sid)
3014 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003015 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003016 {
3017 SOURCING_LNUM = iptr->isn_lnum;
3018 semsg(_(e_item_not_exported_in_script_str),
3019 name);
3020 goto on_error;
3021 }
3022 }
3023 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003024 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003025 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003027 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 }
3029 }
3030 break;
3031
Bram Moolenaard3aac292020-04-19 14:32:17 +02003032 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003034 case ISN_LOADB:
3035 case ISN_LOADW:
3036 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003038 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003039
Bram Moolenaar06b77222022-01-25 15:51:56 +00003040 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003041 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003042 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003043 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 break;
3047
Bram Moolenaar03290b82020-12-19 16:30:44 +01003048 // load autoload variable
3049 case ISN_LOADAUTO:
3050 {
3051 char_u *name = iptr->isn_arg.string;
3052
Bram Moolenaar35578162021-08-02 19:10:38 +02003053 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003054 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003055 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003056 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003057 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003058 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003059 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003060 }
3061 break;
3062
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003063 // load g:/b:/w:/t: namespace
3064 case ISN_LOADGDICT:
3065 case ISN_LOADBDICT:
3066 case ISN_LOADWDICT:
3067 case ISN_LOADTDICT:
3068 {
3069 dict_T *d = NULL;
3070
3071 switch (iptr->isn_type)
3072 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003073 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3074 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3075 case ISN_LOADWDICT: d = curwin->w_vars; break;
3076 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003077 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003078 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003079 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003080 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003081 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003082 tv = STACK_TV_BOT(0);
3083 tv->v_type = VAR_DICT;
3084 tv->v_lock = 0;
3085 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003086 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003087 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003088 }
3089 break;
3090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 // load &option
3092 case ISN_LOADOPT:
3093 {
3094 typval_T optval;
3095 char_u *name = iptr->isn_arg.string;
3096
Bram Moolenaara8c17702020-04-01 21:17:24 +02003097 // This is not expected to fail, name is checked during
3098 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003099 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003100 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003101 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003102 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003104 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 }
3106 break;
3107
3108 // load $ENV
3109 case ISN_LOADENV:
3110 {
3111 typval_T optval;
3112 char_u *name = iptr->isn_arg.string;
3113
Bram Moolenaar35578162021-08-02 19:10:38 +02003114 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003115 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003116 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003117 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003119 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 }
3121 break;
3122
3123 // load @register
3124 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003125 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003126 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 tv = STACK_TV_BOT(0);
3128 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003129 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003130 // This may result in NULL, which should be equivalent to an
3131 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 tv->vval.v_string = get_reg_contents(
3133 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003134 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 break;
3136
3137 // store local variable
3138 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003139 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 tv = STACK_TV_VAR(iptr->isn_arg.number);
3141 clear_tv(tv);
3142 *tv = *STACK_TV_BOT(0);
3143 break;
3144
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003145 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003146 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003147 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003148 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003149 int sid = iptr->isn_arg.loadstore.ls_sid;
3150 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003151 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003152 dictitem_T *di = find_var_in_ht(ht, 0,
3153 iptr->isn_type == ISN_STORES
3154 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003155
Bram Moolenaar4c137212021-04-19 16:48:48 +02003156 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003157 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003158 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003159 {
3160 if (iptr->isn_type == ISN_STOREEXPORT)
3161 {
3162 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003163 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003164 goto on_error;
3165 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003166 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003167 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003168 else
3169 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003170 if (iptr->isn_type == ISN_STOREEXPORT)
3171 {
3172 int idx = get_script_item_idx(sid, name, 0,
3173 NULL, NULL);
3174
Bram Moolenaar06651632022-04-27 17:54:25 +01003175 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003176 if (idx >= 0)
3177 {
3178 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3179 ->sn_var_vals.ga_data) + idx;
3180
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003181 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003182 {
3183 semsg(_(e_item_not_exported_in_script_str),
3184 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003185 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003186 goto on_error;
3187 }
3188 }
3189 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003190 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003191 {
3192 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003193 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003194 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003195 clear_tv(&di->di_tv);
3196 di->di_tv = *STACK_TV_BOT(0);
3197 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003198 }
3199 break;
3200
3201 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 case ISN_STORESCRIPT:
3203 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003204 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3205 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003207 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003208 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003209 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003210 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003211
3212 // "const" and "final" are checked at compile time, locking
3213 // the value needs to be checked here.
3214 SOURCING_LNUM = iptr->isn_lnum;
3215 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003216 {
3217 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003218 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003219 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 clear_tv(sv->sv_tv);
3222 *sv->sv_tv = *STACK_TV_BOT(0);
3223 }
3224 break;
3225
3226 // store option
3227 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003228 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003230 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3231 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 long n = 0;
3233 char_u *s = NULL;
3234 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003235 char_u numbuf[NUMBUFLEN];
3236 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237
Bram Moolenaar4c137212021-04-19 16:48:48 +02003238 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 tv = STACK_TV_BOT(0);
3240 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003241 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003243 if (s == NULL)
3244 s = (char_u *)"";
3245 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003246 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3247 {
3248 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003249 // If the option can be set to a function reference or
3250 // a lambda and the passed value is a function
3251 // reference, then convert it to the name (string) of
3252 // the function reference.
3253 s = tv2string(tv, &tofree, numbuf, 0);
3254 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003255 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003256 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003257 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003258 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003259 goto on_error;
3260 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003261 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003263 // must be VAR_NUMBER, CHECKTYPE makes sure
3264 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003265 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003266 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003267 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 if (msg != NULL)
3269 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003270 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003272 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 }
3275 break;
3276
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003277 // store $ENV
3278 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003279 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003280 tv = STACK_TV_BOT(0);
3281 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3282 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003283 break;
3284
3285 // store @r
3286 case ISN_STOREREG:
3287 {
3288 int reg = iptr->isn_arg.number;
3289
Bram Moolenaar4c137212021-04-19 16:48:48 +02003290 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003291 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003292 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003293 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003294 }
3295 break;
3296
3297 // store v: variable
3298 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003299 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003300 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3301 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003302 // should not happen, type is checked when compiling
3303 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003304 break;
3305
Bram Moolenaard3aac292020-04-19 14:32:17 +02003306 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003308 case ISN_STOREB:
3309 case ISN_STOREW:
3310 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003312 dictitem_T *di;
3313 hashtab_T *ht;
3314 char_u *name = iptr->isn_arg.string + 2;
3315
Bram Moolenaard3aac292020-04-19 14:32:17 +02003316 switch (iptr->isn_type)
3317 {
3318 case ISN_STOREG:
3319 ht = get_globvar_ht();
3320 break;
3321 case ISN_STOREB:
3322 ht = &curbuf->b_vars->dv_hashtab;
3323 break;
3324 case ISN_STOREW:
3325 ht = &curwin->w_vars->dv_hashtab;
3326 break;
3327 case ISN_STORET:
3328 ht = &curtab->tp_vars->dv_hashtab;
3329 break;
3330 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003331 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333
Bram Moolenaar4c137212021-04-19 16:48:48 +02003334 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003335 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003337 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 else
3339 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003340 SOURCING_LNUM = iptr->isn_lnum;
3341 if (var_check_permission(di, name) == FAIL)
3342 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 clear_tv(&di->di_tv);
3344 di->di_tv = *STACK_TV_BOT(0);
3345 }
3346 }
3347 break;
3348
Bram Moolenaar03290b82020-12-19 16:30:44 +01003349 // store an autoload variable
3350 case ISN_STOREAUTO:
3351 SOURCING_LNUM = iptr->isn_lnum;
3352 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3353 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003354 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003355 break;
3356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 // store number in local variable
3358 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003359 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 clear_tv(tv);
3361 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003362 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 break;
3364
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003365 // store value in list or dict variable
3366 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003367 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003368 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003369
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003370 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003371 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003372 if (res == NOTDONE)
3373 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003374 }
3375 break;
3376
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003377 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003378 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003379 if (execute_storerange(iptr, ectx) == FAIL)
3380 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003381 break;
3382
Bram Moolenaar0186e582021-01-10 18:33:11 +01003383 // load or store variable or argument from outer scope
3384 case ISN_LOADOUTER:
3385 case ISN_STOREOUTER:
3386 {
3387 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003388 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3389 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003390
3391 while (depth > 1 && outer != NULL)
3392 {
3393 outer = outer->out_up;
3394 --depth;
3395 }
3396 if (outer == NULL)
3397 {
3398 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003399 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3400 || ectx->ec_outer_ref == NULL)
3401 // Possibly :def function called from legacy
3402 // context.
3403 emsg(_(e_closure_called_from_invalid_context));
3404 else
3405 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003406 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003407 }
3408 tv = ((typval_T *)outer->out_stack->ga_data)
3409 + outer->out_frame_idx + STACK_FRAME_SIZE
3410 + iptr->isn_arg.outer.outer_idx;
3411 if (iptr->isn_type == ISN_LOADOUTER)
3412 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003413 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003414 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003415 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003416 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003417 }
3418 else
3419 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003420 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003421 clear_tv(tv);
3422 *tv = *STACK_TV_BOT(0);
3423 }
3424 }
3425 break;
3426
Bram Moolenaar752fc692021-01-04 21:57:11 +01003427 // unlet item in list or dict variable
3428 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003429 if (execute_unletindex(iptr, ectx) == FAIL)
3430 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003431 break;
3432
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003433 // unlet range of items in list variable
3434 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003435 if (execute_unletrange(iptr, ectx) == FAIL)
3436 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003437 break;
3438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 // push constant
3440 case ISN_PUSHNR:
3441 case ISN_PUSHBOOL:
3442 case ISN_PUSHSPEC:
3443 case ISN_PUSHF:
3444 case ISN_PUSHS:
3445 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003446 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003447 case ISN_PUSHCHANNEL:
3448 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003449 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003450 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003452 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003453 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 switch (iptr->isn_type)
3455 {
3456 case ISN_PUSHNR:
3457 tv->v_type = VAR_NUMBER;
3458 tv->vval.v_number = iptr->isn_arg.number;
3459 break;
3460 case ISN_PUSHBOOL:
3461 tv->v_type = VAR_BOOL;
3462 tv->vval.v_number = iptr->isn_arg.number;
3463 break;
3464 case ISN_PUSHSPEC:
3465 tv->v_type = VAR_SPECIAL;
3466 tv->vval.v_number = iptr->isn_arg.number;
3467 break;
3468#ifdef FEAT_FLOAT
3469 case ISN_PUSHF:
3470 tv->v_type = VAR_FLOAT;
3471 tv->vval.v_float = iptr->isn_arg.fnumber;
3472 break;
3473#endif
3474 case ISN_PUSHBLOB:
3475 blob_copy(iptr->isn_arg.blob, tv);
3476 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003477 case ISN_PUSHFUNC:
3478 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003479 if (iptr->isn_arg.string == NULL)
3480 tv->vval.v_string = NULL;
3481 else
3482 tv->vval.v_string =
3483 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003484 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003485 case ISN_PUSHCHANNEL:
3486#ifdef FEAT_JOB_CHANNEL
3487 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003488 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003489#endif
3490 break;
3491 case ISN_PUSHJOB:
3492#ifdef FEAT_JOB_CHANNEL
3493 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003494 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003495#endif
3496 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 default:
3498 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003499 tv->vval.v_string = iptr->isn_arg.string == NULL
3500 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 }
3502 break;
3503
Bram Moolenaar06b77222022-01-25 15:51:56 +00003504 case ISN_AUTOLOAD:
3505 {
3506 char_u *name = iptr->isn_arg.string;
3507
3508 (void)script_autoload(name, FALSE);
3509 if (find_func(name, TRUE))
3510 {
3511 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3512 goto theend;
3513 tv = STACK_TV_BOT(0);
3514 tv->v_lock = 0;
3515 ++ectx->ec_stack.ga_len;
3516 tv->v_type = VAR_FUNC;
3517 tv->vval.v_string = vim_strsave(name);
3518 }
3519 else
3520 {
3521 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3522
3523 if (res == NOTDONE)
3524 goto theend;
3525 if (res == FAIL)
3526 goto on_error;
3527 }
3528 }
3529 break;
3530
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003531 case ISN_UNLET:
3532 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3533 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003534 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003535 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003536 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003537 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003538 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003539
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003540 case ISN_LOCKUNLOCK:
3541 {
3542 typval_T *lval_root_save = lval_root;
3543 int res;
3544
3545 // Stack has the local variable, argument the whole :lock
3546 // or :unlock command, like ISN_EXEC.
3547 --ectx->ec_stack.ga_len;
3548 lval_root = STACK_TV_BOT(0);
3549 res = exec_command(iptr);
3550 clear_tv(lval_root);
3551 lval_root = lval_root_save;
3552 if (res == FAIL)
3553 goto on_error;
3554 }
3555 break;
3556
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003557 case ISN_LOCKCONST:
3558 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3559 break;
3560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 // create a list from items on the stack; uses a single allocation
3562 // for the list header and the items
3563 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003564 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003565 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 break;
3567
3568 // create a dict from items on the stack
3569 case ISN_NEWDICT:
3570 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003571 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003573 SOURCING_LNUM = iptr->isn_lnum;
3574 res = exe_newdict(iptr->isn_arg.number, ectx);
3575 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003576 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003577 if (res == MAYBE)
3578 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 }
3580 break;
3581
LemonBoy372bcce2022-04-25 12:43:20 +01003582 case ISN_CONCAT:
3583 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3584 goto theend;
3585 break;
3586
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003587 // create a partial with NULL value
3588 case ISN_NEWPARTIAL:
3589 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3590 goto theend;
3591 ++ectx->ec_stack.ga_len;
3592 tv = STACK_TV_BOT(-1);
3593 tv->v_type = VAR_PARTIAL;
3594 tv->v_lock = 0;
3595 tv->vval.v_partial = NULL;
3596 break;
3597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 // call a :def function
3599 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003600 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003601 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3602 NULL,
3603 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003604 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003605 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 break;
3607
3608 // call a builtin function
3609 case ISN_BCALL:
3610 SOURCING_LNUM = iptr->isn_lnum;
3611 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3612 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003613 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003614 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 break;
3616
3617 // call a funcref or partial
3618 case ISN_PCALL:
3619 {
3620 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3621 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003622 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623
3624 SOURCING_LNUM = iptr->isn_lnum;
3625 if (pfunc->cpf_top)
3626 {
3627 // funcref is above the arguments
3628 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3629 }
3630 else
3631 {
3632 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003633 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003634 partial_tv = *STACK_TV_BOT(0);
3635 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003637 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003638 if (tv == &partial_tv)
3639 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003641 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 }
3643 break;
3644
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003645 case ISN_PCALL_END:
3646 // PCALL finished, arguments have been consumed and replaced by
3647 // the return value. Now clear the funcref from the stack,
3648 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003649 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003650 clear_tv(STACK_TV_BOT(-1));
3651 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3652 break;
3653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 // call a user defined function or funcref/partial
3655 case ISN_UCALL:
3656 {
3657 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3658
3659 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003660 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003661 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003662 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 }
3664 break;
3665
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003666 // return from a :def function call without a value
3667 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003668 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003669 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003670 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003671 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003672 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003673 tv->vval.v_number = 0;
3674 tv->v_lock = 0;
3675 // FALLTHROUGH
3676
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003677 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 case ISN_RETURN:
3679 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003680 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003681 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003682
3683 if (trystack->ga_len > 0)
3684 trycmd = ((trycmd_T *)trystack->ga_data)
3685 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003686 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003687 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003689 // jump to ":finally" or ":endtry"
3690 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003691 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003692 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003693 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 trycmd->tcd_return = TRUE;
3695 }
3696 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003697 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 }
3699 break;
3700
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003701 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 case ISN_FUNCREF:
3703 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003704 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003705 ufunc_T *ufunc;
3706 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003709 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003710 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003711 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003712 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003713 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003714 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003715 if (funcref->fr_func_name == NULL)
3716 {
3717 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3718 + funcref->fr_dfunc_idx;
3719
3720 ufunc = pt_dfunc->df_ufunc;
3721 }
3722 else
3723 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003724 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003725 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003726 if (ufunc == NULL)
3727 {
3728 SOURCING_LNUM = iptr->isn_lnum;
3729 iemsg("ufunc unexpectedly NULL for FUNCREF");
3730 goto theend;
3731 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003732 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003733 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003735 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 tv->vval.v_partial = pt;
3737 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003738 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 }
3740 break;
3741
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003742 // Create a global function from a lambda.
3743 case ISN_NEWFUNC:
3744 {
3745 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3746
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003747 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003748 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003749 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003750 }
3751 break;
3752
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003753 // List functions
3754 case ISN_DEF:
3755 if (iptr->isn_arg.string == NULL)
3756 list_functions(NULL);
3757 else
3758 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003759 exarg_T ea;
3760 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003761
3762 CLEAR_FIELD(ea);
3763 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003764 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3765 define_function(&ea, NULL, &lines_to_free);
3766 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003767 }
3768 break;
3769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 // jump if a condition is met
3771 case ISN_JUMP:
3772 {
3773 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003774 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 int jump = TRUE;
3776
3777 if (when != JUMP_ALWAYS)
3778 {
3779 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003780 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003781 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003782 || when == JUMP_IF_COND_TRUE)
3783 {
3784 SOURCING_LNUM = iptr->isn_lnum;
3785 jump = tv_get_bool_chk(tv, &error);
3786 if (error)
3787 goto on_error;
3788 }
3789 else
3790 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01003791 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003793 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 {
3795 // drop the value from the stack
3796 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003797 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 }
3799 }
3800 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003801 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 }
3803 break;
3804
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003805 // Jump if an argument with a default value was already set and not
3806 // v:none.
3807 case ISN_JUMP_IF_ARG_SET:
3808 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3809 if (tv->v_type != VAR_UNKNOWN
3810 && !(tv->v_type == VAR_SPECIAL
3811 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003812 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003813 break;
3814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 // top of a for loop
3816 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003817 if (execute_for(iptr, ectx) == FAIL)
3818 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 break;
3820
3821 // start of ":try" block
3822 case ISN_TRY:
3823 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003824 trycmd_T *trycmd = NULL;
3825
Bram Moolenaar35578162021-08-02 19:10:38 +02003826 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003827 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003828 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3829 + ectx->ec_trystack.ga_len;
3830 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003832 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003833 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3834 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003835 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003836 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003837 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003838 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003839 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003840 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 }
3842 break;
3843
3844 case ISN_PUSHEXC:
3845 if (current_exception == NULL)
3846 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003847 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003849 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003851 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003852 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003854 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003856 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 tv->vval.v_string = vim_strsave(
3858 (char_u *)current_exception->value);
3859 break;
3860
3861 case ISN_CATCH:
3862 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003863 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01003864 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865
Bram Moolenaar4c137212021-04-19 16:48:48 +02003866 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01003867 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01003869 trycmd->tcd_caught = TRUE;
3870 trycmd->tcd_did_throw = FALSE;
3871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003873 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 catch_exception(current_exception);
3875 }
3876 break;
3877
Bram Moolenaarc150c092021-02-13 15:02:46 +01003878 case ISN_TRYCONT:
3879 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003880 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003881 trycont_T *trycont = &iptr->isn_arg.trycont;
3882 int i;
3883 trycmd_T *trycmd;
3884 int iidx = trycont->tct_where;
3885
3886 if (trystack->ga_len < trycont->tct_levels)
3887 {
3888 siemsg("TRYCONT: expected %d levels, found %d",
3889 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003890 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003891 }
3892 // Make :endtry jump to any outer try block and the last
3893 // :endtry inside the loop to the loop start.
3894 for (i = trycont->tct_levels; i > 0; --i)
3895 {
3896 trycmd = ((trycmd_T *)trystack->ga_data)
3897 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003898 // Add one to tcd_cont to be able to jump to
3899 // instruction with index zero.
3900 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003901 iidx = trycmd->tcd_finally_idx == 0
3902 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003903 }
3904 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003905 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003906 }
3907 break;
3908
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003909 case ISN_FINALLY:
3910 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003911 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003912 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3913 + trystack->ga_len - 1;
3914
3915 // Reset the index to avoid a return statement jumps here
3916 // again.
3917 trycmd->tcd_finally_idx = 0;
3918 break;
3919 }
3920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 // end of ":try" block
3922 case ISN_ENDTRY:
3923 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003924 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01003925 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926
Bram Moolenaar06651632022-04-27 17:54:25 +01003927 --trystack->ga_len;
3928 --trylevel;
3929 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
3930 if (trycmd->tcd_did_throw)
3931 did_throw = TRUE;
3932 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 {
Bram Moolenaar06651632022-04-27 17:54:25 +01003934 // discard the exception
3935 if (caught_stack == current_exception)
3936 caught_stack = caught_stack->caught;
3937 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 }
Bram Moolenaar06651632022-04-27 17:54:25 +01003939
3940 if (trycmd->tcd_return)
3941 goto func_return;
3942
3943 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
3944 {
3945 --ectx->ec_stack.ga_len;
3946 clear_tv(STACK_TV_BOT(0));
3947 }
3948 if (trycmd->tcd_cont != 0)
3949 // handling :continue: jump to outer try block or
3950 // start of the loop
3951 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 }
3953 break;
3954
3955 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003956 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003957 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003958
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003959 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3960 {
3961 // throwing an exception while using "silent!" causes
3962 // the function to abort but not display an error.
3963 tv = STACK_TV_BOT(-1);
3964 clear_tv(tv);
3965 tv->v_type = VAR_NUMBER;
3966 tv->vval.v_number = 0;
3967 goto done;
3968 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003969 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003970 tv = STACK_TV_BOT(0);
3971 if (tv->vval.v_string == NULL
3972 || *skipwhite(tv->vval.v_string) == NUL)
3973 {
3974 vim_free(tv->vval.v_string);
3975 SOURCING_LNUM = iptr->isn_lnum;
3976 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003977 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003978 }
3979
3980 // Inside a "catch" we need to first discard the caught
3981 // exception.
3982 if (trystack->ga_len > 0)
3983 {
3984 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3985 + trystack->ga_len - 1;
3986 if (trycmd->tcd_caught && current_exception != NULL)
3987 {
3988 // discard the exception
3989 if (caught_stack == current_exception)
3990 caught_stack = caught_stack->caught;
3991 discard_current_exception();
3992 trycmd->tcd_caught = FALSE;
3993 }
3994 }
3995
Bram Moolenaar90a57162022-02-12 14:23:17 +00003996 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003997 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3998 == FAIL)
3999 {
4000 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004001 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004002 }
4003 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 break;
4006
4007 // compare with special values
4008 case ISN_COMPAREBOOL:
4009 case ISN_COMPARESPECIAL:
4010 {
4011 typval_T *tv1 = STACK_TV_BOT(-2);
4012 typval_T *tv2 = STACK_TV_BOT(-1);
4013 varnumber_T arg1 = tv1->vval.v_number;
4014 varnumber_T arg2 = tv2->vval.v_number;
4015 int res;
4016
Bram Moolenaar06651632022-04-27 17:54:25 +01004017 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4018 res = arg1 == arg2;
4019 else
4020 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021
Bram Moolenaar4c137212021-04-19 16:48:48 +02004022 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 tv1->v_type = VAR_BOOL;
4024 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4025 }
4026 break;
4027
Bram Moolenaar7a222242022-03-01 19:23:24 +00004028 case ISN_COMPARENULL:
4029 {
4030 typval_T *tv1 = STACK_TV_BOT(-2);
4031 typval_T *tv2 = STACK_TV_BOT(-1);
4032 int res;
4033
4034 res = typval_compare_null(tv1, tv2);
4035 if (res == MAYBE)
4036 goto on_error;
4037 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4038 res = !res;
4039 clear_tv(tv1);
4040 clear_tv(tv2);
4041 --ectx->ec_stack.ga_len;
4042 tv1->v_type = VAR_BOOL;
4043 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4044 }
4045 break;
4046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 // Operation with two number arguments
4048 case ISN_OPNR:
4049 case ISN_COMPARENR:
4050 {
4051 typval_T *tv1 = STACK_TV_BOT(-2);
4052 typval_T *tv2 = STACK_TV_BOT(-1);
4053 varnumber_T arg1 = tv1->vval.v_number;
4054 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004055 varnumber_T res = 0;
4056 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004058 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4059 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4060 {
4061 if (arg2 < 0)
4062 {
4063 SOURCING_LNUM = iptr->isn_lnum;
4064 emsg(_(e_bitshift_ops_must_be_postive));
4065 goto on_error;
4066 }
4067 }
4068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 switch (iptr->isn_arg.op.op_type)
4070 {
4071 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004072 case EXPR_DIV: if (arg2 == 0)
4073 div_zero = TRUE;
4074 else
4075 res = arg1 / arg2;
4076 break;
4077 case EXPR_REM: if (arg2 == 0)
4078 div_zero = TRUE;
4079 else
4080 res = arg1 % arg2;
4081 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 case EXPR_SUB: res = arg1 - arg2; break;
4083 case EXPR_ADD: res = arg1 + arg2; break;
4084
4085 case EXPR_EQUAL: res = arg1 == arg2; break;
4086 case EXPR_NEQUAL: res = arg1 != arg2; break;
4087 case EXPR_GREATER: res = arg1 > arg2; break;
4088 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4089 case EXPR_SMALLER: res = arg1 < arg2; break;
4090 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004091 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4092 res = 0;
4093 else
4094 res = arg1 << arg2;
4095 break;
4096 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4097 res = 0;
4098 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004099 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004100 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004101 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 }
4103
Bram Moolenaar4c137212021-04-19 16:48:48 +02004104 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 if (iptr->isn_type == ISN_COMPARENR)
4106 {
4107 tv1->v_type = VAR_BOOL;
4108 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4109 }
4110 else
4111 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004112 if (div_zero)
4113 {
4114 SOURCING_LNUM = iptr->isn_lnum;
4115 emsg(_(e_divide_by_zero));
4116 goto on_error;
4117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 }
4119 break;
4120
4121 // Computation with two float arguments
4122 case ISN_OPFLOAT:
4123 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004124#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 {
4126 typval_T *tv1 = STACK_TV_BOT(-2);
4127 typval_T *tv2 = STACK_TV_BOT(-1);
4128 float_T arg1 = tv1->vval.v_float;
4129 float_T arg2 = tv2->vval.v_float;
4130 float_T res = 0;
4131 int cmp = FALSE;
4132
4133 switch (iptr->isn_arg.op.op_type)
4134 {
4135 case EXPR_MULT: res = arg1 * arg2; break;
4136 case EXPR_DIV: res = arg1 / arg2; break;
4137 case EXPR_SUB: res = arg1 - arg2; break;
4138 case EXPR_ADD: res = arg1 + arg2; break;
4139
4140 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4141 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4142 case EXPR_GREATER: cmp = arg1 > arg2; break;
4143 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4144 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4145 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4146 default: cmp = 0; break;
4147 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004148 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 if (iptr->isn_type == ISN_COMPAREFLOAT)
4150 {
4151 tv1->v_type = VAR_BOOL;
4152 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4153 }
4154 else
4155 tv1->vval.v_float = res;
4156 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004157#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 break;
4159
4160 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004161 case ISN_COMPAREDICT:
4162 case ISN_COMPAREFUNC:
4163 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 case ISN_COMPAREBLOB:
4165 {
4166 typval_T *tv1 = STACK_TV_BOT(-2);
4167 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004168 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4169 int ic = iptr->isn_arg.op.op_ic;
4170 int res = FALSE;
4171 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172
Bram Moolenaar265f8112021-12-19 12:33:05 +00004173 SOURCING_LNUM = iptr->isn_lnum;
4174 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004176 status = typval_compare_list(tv1, tv2,
4177 exprtype, ic, &res);
4178 }
4179 else if (iptr->isn_type == ISN_COMPAREDICT)
4180 {
4181 status = typval_compare_dict(tv1, tv2,
4182 exprtype, ic, &res);
4183 }
4184 else if (iptr->isn_type == ISN_COMPAREFUNC)
4185 {
4186 status = typval_compare_func(tv1, tv2,
4187 exprtype, ic, &res);
4188 }
4189 else if (iptr->isn_type == ISN_COMPARESTRING)
4190 {
4191 status = typval_compare_string(tv1, tv2,
4192 exprtype, ic, &res);
4193 }
4194 else
4195 {
4196 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004198 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 clear_tv(tv1);
4200 clear_tv(tv2);
4201 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004202 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4203 if (status == FAIL)
4204 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 }
4206 break;
4207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 case ISN_COMPAREANY:
4209 {
4210 typval_T *tv1 = STACK_TV_BOT(-2);
4211 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004212 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004214 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215
Bram Moolenaareb26f432020-09-14 16:50:05 +02004216 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004217 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004219 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004220 if (status == FAIL)
4221 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 }
4223 break;
4224
4225 case ISN_ADDLIST:
4226 case ISN_ADDBLOB:
4227 {
4228 typval_T *tv1 = STACK_TV_BOT(-2);
4229 typval_T *tv2 = STACK_TV_BOT(-1);
4230
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004231 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004233 {
4234 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4235 && tv1->vval.v_list != NULL)
4236 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4237 NULL);
4238 else
4239 eval_addlist(tv1, tv2);
4240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 else
4242 eval_addblob(tv1, tv2);
4243 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004244 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 }
4246 break;
4247
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004248 case ISN_LISTAPPEND:
4249 {
4250 typval_T *tv1 = STACK_TV_BOT(-2);
4251 typval_T *tv2 = STACK_TV_BOT(-1);
4252 list_T *l = tv1->vval.v_list;
4253
4254 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004255 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004256 if (l == NULL)
4257 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004258 emsg(_(e_cannot_add_to_null_list));
4259 goto on_error;
4260 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004261 if (value_check_lock(l->lv_lock, NULL, FALSE))
4262 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004263 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004264 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004265 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004266 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004267 }
4268 break;
4269
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004270 case ISN_BLOBAPPEND:
4271 {
4272 typval_T *tv1 = STACK_TV_BOT(-2);
4273 typval_T *tv2 = STACK_TV_BOT(-1);
4274 blob_T *b = tv1->vval.v_blob;
4275 int error = FALSE;
4276 varnumber_T n;
4277
4278 // add a number to a blob
4279 if (b == NULL)
4280 {
4281 SOURCING_LNUM = iptr->isn_lnum;
4282 emsg(_(e_cannot_add_to_null_blob));
4283 goto on_error;
4284 }
4285 n = tv_get_number_chk(tv2, &error);
4286 if (error)
4287 goto on_error;
4288 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004289 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004290 }
4291 break;
4292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 // Computation with two arguments of unknown type
4294 case ISN_OPANY:
4295 {
4296 typval_T *tv1 = STACK_TV_BOT(-2);
4297 typval_T *tv2 = STACK_TV_BOT(-1);
4298 varnumber_T n1, n2;
4299#ifdef FEAT_FLOAT
4300 float_T f1 = 0, f2 = 0;
4301#endif
4302 int error = FALSE;
4303
4304 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4305 {
4306 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4307 {
4308 eval_addlist(tv1, tv2);
4309 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004310 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 break;
4312 }
4313 else if (tv1->v_type == VAR_BLOB
4314 && tv2->v_type == VAR_BLOB)
4315 {
4316 eval_addblob(tv1, tv2);
4317 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004318 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 break;
4320 }
4321 }
4322#ifdef FEAT_FLOAT
4323 if (tv1->v_type == VAR_FLOAT)
4324 {
4325 f1 = tv1->vval.v_float;
4326 n1 = 0;
4327 }
4328 else
4329#endif
4330 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004331 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 n1 = tv_get_number_chk(tv1, &error);
4333 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004334 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335#ifdef FEAT_FLOAT
4336 if (tv2->v_type == VAR_FLOAT)
4337 f1 = n1;
4338#endif
4339 }
4340#ifdef FEAT_FLOAT
4341 if (tv2->v_type == VAR_FLOAT)
4342 {
4343 f2 = tv2->vval.v_float;
4344 n2 = 0;
4345 }
4346 else
4347#endif
4348 {
4349 n2 = tv_get_number_chk(tv2, &error);
4350 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004351 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352#ifdef FEAT_FLOAT
4353 if (tv1->v_type == VAR_FLOAT)
4354 f2 = n2;
4355#endif
4356 }
4357#ifdef FEAT_FLOAT
4358 // if there is a float on either side the result is a float
4359 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4360 {
4361 switch (iptr->isn_arg.op.op_type)
4362 {
4363 case EXPR_MULT: f1 = f1 * f2; break;
4364 case EXPR_DIV: f1 = f1 / f2; break;
4365 case EXPR_SUB: f1 = f1 - f2; break;
4366 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004367 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004368 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004369 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 }
4371 clear_tv(tv1);
4372 clear_tv(tv2);
4373 tv1->v_type = VAR_FLOAT;
4374 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004375 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 }
4377 else
4378#endif
4379 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004380 int failed = FALSE;
4381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 switch (iptr->isn_arg.op.op_type)
4383 {
4384 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004385 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4386 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004387 goto on_error;
4388 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 case EXPR_SUB: n1 = n1 - n2; break;
4390 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004391 default: n1 = num_modulus(n1, n2, &failed);
4392 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004393 goto on_error;
4394 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 }
4396 clear_tv(tv1);
4397 clear_tv(tv2);
4398 tv1->v_type = VAR_NUMBER;
4399 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004400 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 }
4402 }
4403 break;
4404
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004405 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004406 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004407 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004408 int is_slice = iptr->isn_type == ISN_STRSLICE;
4409 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004410 char_u *res;
4411
4412 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004413 // string slice: string is at stack-3, first index at
4414 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004415 if (is_slice)
4416 {
4417 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004418 n1 = tv->vval.v_number;
4419 }
4420
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004421 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004422 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004423
Bram Moolenaar4c137212021-04-19 16:48:48 +02004424 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004425 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004426 if (is_slice)
4427 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004428 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004429 else
4430 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004431 // single character (including composing characters).
4432 // If the index is too big or negative the result is
4433 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004434 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004435 vim_free(tv->vval.v_string);
4436 tv->vval.v_string = res;
4437 }
4438 break;
4439
4440 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004441 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004442 case ISN_BLOBINDEX:
4443 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004445 int is_slice = iptr->isn_type == ISN_LISTSLICE
4446 || iptr->isn_type == ISN_BLOBSLICE;
4447 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4448 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004449 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004450 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451
4452 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004453 // list slice: list is at stack-3, indexes at stack-2 and
4454 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004455 // Same for blob.
4456 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457
4458 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004459 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004461
4462 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 {
Bram Moolenaared591872020-08-15 22:14:53 +02004464 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004465 n1 = tv->vval.v_number;
4466 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 }
Bram Moolenaared591872020-08-15 22:14:53 +02004468
Bram Moolenaar4c137212021-04-19 16:48:48 +02004469 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004470 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004471 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004472 if (is_blob)
4473 {
4474 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4475 n1, n2, FALSE, tv) == FAIL)
4476 goto on_error;
4477 }
4478 else
4479 {
4480 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4481 n1, n2, FALSE, tv, TRUE) == FAIL)
4482 goto on_error;
4483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 }
4485 break;
4486
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004487 case ISN_ANYINDEX:
4488 case ISN_ANYSLICE:
4489 {
4490 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4491 typval_T *var1, *var2;
4492 int res;
4493
4494 // index: composite is at stack-2, index at stack-1
4495 // slice: composite is at stack-3, indexes at stack-2 and
4496 // stack-1
4497 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004498 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004499 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4500 goto on_error;
4501 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4502 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004503 res = eval_index_inner(tv, is_slice, var1, var2,
4504 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004505 clear_tv(var1);
4506 if (is_slice)
4507 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004508 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004509 if (res == FAIL)
4510 goto on_error;
4511 }
4512 break;
4513
Bram Moolenaar9af78762020-06-16 11:34:42 +02004514 case ISN_SLICE:
4515 {
4516 list_T *list;
4517 int count = iptr->isn_arg.number;
4518
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004519 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004520 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004521 list = tv->vval.v_list;
4522
4523 // no error for short list, expect it to be checked earlier
4524 if (list != NULL && list->lv_len >= count)
4525 {
4526 list_T *newlist = list_slice(list,
4527 count, list->lv_len - 1);
4528
4529 if (newlist != NULL)
4530 {
4531 list_unref(list);
4532 tv->vval.v_list = newlist;
4533 ++newlist->lv_refcount;
4534 }
4535 }
4536 }
4537 break;
4538
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004539 case ISN_GETITEM:
4540 {
4541 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004542 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004543
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004544 // Get list item: list is at stack-1, push item.
4545 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004546 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4547 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004548
Bram Moolenaar35578162021-08-02 19:10:38 +02004549 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004550 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004551 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004552 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004553
4554 // Useful when used in unpack assignment. Reset at
4555 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004556 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004557 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004558 }
4559 break;
4560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 case ISN_MEMBER:
4562 {
4563 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004564 char_u *key;
4565 dictitem_T *di;
4566
4567 // dict member: dict is at stack-2, key at stack-1
4568 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004569 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004570 dict = tv->vval.v_dict;
4571
4572 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004573 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004574 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004575 if (key == NULL)
4576 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004577
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004578 if ((di = dict_find(dict, key, -1)) == NULL)
4579 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004580 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004581 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004582
4583 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004584 // stack contents makes sense and the dict stack is
4585 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004586 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004587 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004588 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004589 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004590 tv->v_type = VAR_NUMBER;
4591 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004592 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004593 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004594 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004595 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004596 // Put the dict used on the dict stack, it might be used by
4597 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004598 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004599 if (dict_stack_save(tv) == FAIL)
4600 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004601 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004602 }
4603 break;
4604
4605 // dict member with string key
4606 case ISN_STRINGMEMBER:
4607 {
4608 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 dictitem_T *di;
4610
4611 tv = STACK_TV_BOT(-1);
4612 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4613 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004614 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004615 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004616 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 }
4618 dict = tv->vval.v_dict;
4619
4620 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4621 == NULL)
4622 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004623 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004624 semsg(_(e_key_not_present_in_dictionary),
4625 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004626 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004628 // Put the dict used on the dict stack, it might be used by
4629 // a dict function later.
4630 if (dict_stack_save(tv) == FAIL)
4631 goto on_fatal_error;
4632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004634 }
4635 break;
4636
4637 case ISN_CLEARDICT:
4638 dict_stack_drop();
4639 break;
4640
4641 case ISN_USEDICT:
4642 {
4643 typval_T *dict_tv = dict_stack_get_tv();
4644
4645 // Turn "dict.Func" into a partial for "Func" bound to
4646 // "dict". Don't do this when "Func" is already a partial
4647 // that was bound explicitly (pt_auto is FALSE).
4648 tv = STACK_TV_BOT(-1);
4649 if (dict_tv != NULL
4650 && dict_tv->v_type == VAR_DICT
4651 && dict_tv->vval.v_dict != NULL
4652 && (tv->v_type == VAR_FUNC
4653 || (tv->v_type == VAR_PARTIAL
4654 && (tv->vval.v_partial->pt_auto
4655 || tv->vval.v_partial->pt_dict == NULL))))
4656 dict_tv->vval.v_dict =
4657 make_partial(dict_tv->vval.v_dict, tv);
4658 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 }
4660 break;
4661
4662 case ISN_NEGATENR:
4663 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004664 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004665#ifdef FEAT_FLOAT
4666 if (tv->v_type == VAR_FLOAT)
4667 tv->vval.v_float = -tv->vval.v_float;
4668 else
4669#endif
4670 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 break;
4672
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 case ISN_CHECKTYPE:
4674 {
4675 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004676 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004677 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004679 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004680 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004681 if (!ectx->ec_where.wt_variable)
4682 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004683 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004684 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004685 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004686 if (r == FAIL)
4687 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004688 if (!ectx->ec_where.wt_variable)
4689 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004690
4691 // number 0 is FALSE, number 1 is TRUE
4692 if (tv->v_type == VAR_NUMBER
4693 && ct->ct_type->tt_type == VAR_BOOL
4694 && (tv->vval.v_number == 0
4695 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004697 tv->v_type = VAR_BOOL;
4698 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004699 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 }
4701 }
4702 break;
4703
Bram Moolenaar9af78762020-06-16 11:34:42 +02004704 case ISN_CHECKLEN:
4705 {
4706 int min_len = iptr->isn_arg.checklen.cl_min_len;
4707 list_T *list = NULL;
4708
4709 tv = STACK_TV_BOT(-1);
4710 if (tv->v_type == VAR_LIST)
4711 list = tv->vval.v_list;
4712 if (list == NULL || list->lv_len < min_len
4713 || (list->lv_len > min_len
4714 && !iptr->isn_arg.checklen.cl_more_OK))
4715 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004716 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004717 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004718 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004719 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004720 }
4721 }
4722 break;
4723
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004724 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004725 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004726 break;
4727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004729 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730 {
4731 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004732 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004734 if (iptr->isn_type == ISN_2BOOL)
4735 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004736 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004737 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004738 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004739 n = !n;
4740 }
4741 else
4742 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004743 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004744 SOURCING_LNUM = iptr->isn_lnum;
4745 n = tv_get_bool_chk(tv, &error);
4746 if (error)
4747 goto on_error;
4748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 clear_tv(tv);
4750 tv->v_type = VAR_BOOL;
4751 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4752 }
4753 break;
4754
4755 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004756 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004757 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004758 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4759 iptr->isn_type == ISN_2STRING_ANY,
4760 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004761 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 break;
4763
Bram Moolenaar08597872020-12-10 19:43:40 +01004764 case ISN_RANGE:
4765 {
4766 exarg_T ea;
4767 char *errormsg;
4768
Bram Moolenaarece0b872021-01-08 20:40:45 +01004769 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004770 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004771 ea.addr_type = ADDR_LINES;
4772 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004773 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004774 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004775 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004776
Bram Moolenaar35578162021-08-02 19:10:38 +02004777 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004778 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004779 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004780 tv = STACK_TV_BOT(-1);
4781 tv->v_type = VAR_NUMBER;
4782 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004783 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004784 }
4785 break;
4786
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004787 case ISN_PUT:
4788 {
4789 int regname = iptr->isn_arg.put.put_regname;
4790 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4791 char_u *expr = NULL;
4792 int dir = FORWARD;
4793
Bram Moolenaar08597872020-12-10 19:43:40 +01004794 if (lnum < -2)
4795 {
4796 // line number was put on the stack by ISN_RANGE
4797 tv = STACK_TV_BOT(-1);
4798 curwin->w_cursor.lnum = tv->vval.v_number;
4799 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4800 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004801 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004802 }
4803 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004804 // :put! above cursor
4805 dir = BACKWARD;
4806 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004807 {
4808 curwin->w_cursor.lnum = lnum;
4809 if (lnum == 0)
4810 // check_cursor() below will move to line 1
4811 dir = BACKWARD;
4812 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004813
4814 if (regname == '=')
4815 {
4816 tv = STACK_TV_BOT(-1);
4817 if (tv->v_type == VAR_STRING)
4818 expr = tv->vval.v_string;
4819 else
4820 {
4821 expr = typval2string(tv, TRUE); // allocates value
4822 clear_tv(tv);
4823 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004824 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004825 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004826 check_cursor();
4827 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4828 vim_free(expr);
4829 }
4830 break;
4831
Bram Moolenaar02194d22020-10-24 23:08:38 +02004832 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004833 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4834 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4835 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4836 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004837 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4838 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004839 break;
4840
Bram Moolenaar02194d22020-10-24 23:08:38 +02004841 case ISN_CMDMOD_REV:
4842 // filter regprog is owned by the instruction, don't free it
4843 cmdmod.cmod_filter_regmatch.regprog = NULL;
4844 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004845 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4846 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004847 break;
4848
Bram Moolenaar792f7862020-11-23 08:31:18 +01004849 case ISN_UNPACK:
4850 {
4851 int count = iptr->isn_arg.unpack.unp_count;
4852 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4853 list_T *l;
4854 listitem_T *li;
4855 int i;
4856
4857 // Check there is a valid list to unpack.
4858 tv = STACK_TV_BOT(-1);
4859 if (tv->v_type != VAR_LIST)
4860 {
4861 SOURCING_LNUM = iptr->isn_lnum;
4862 emsg(_(e_for_argument_must_be_sequence_of_lists));
4863 goto on_error;
4864 }
4865 l = tv->vval.v_list;
4866 if (l == NULL
4867 || l->lv_len < (semicolon ? count - 1 : count))
4868 {
4869 SOURCING_LNUM = iptr->isn_lnum;
4870 emsg(_(e_list_value_does_not_have_enough_items));
4871 goto on_error;
4872 }
4873 else if (!semicolon && l->lv_len > count)
4874 {
4875 SOURCING_LNUM = iptr->isn_lnum;
4876 emsg(_(e_list_value_has_more_items_than_targets));
4877 goto on_error;
4878 }
4879
4880 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004881 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004882 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004883 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004884
4885 // Variable after semicolon gets a list with the remaining
4886 // items.
4887 if (semicolon)
4888 {
4889 list_T *rem_list =
4890 list_alloc_with_items(l->lv_len - count + 1);
4891
4892 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004893 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004894 tv = STACK_TV_BOT(-count);
4895 tv->vval.v_list = rem_list;
4896 ++rem_list->lv_refcount;
4897 tv->v_lock = 0;
4898 li = l->lv_first;
4899 for (i = 0; i < count - 1; ++i)
4900 li = li->li_next;
4901 for (i = 0; li != NULL; ++i)
4902 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004903 typval_T tvcopy;
4904
4905 copy_tv(&li->li_tv, &tvcopy);
4906 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004907 li = li->li_next;
4908 }
4909 --count;
4910 }
4911
4912 // Produce the values in reverse order, first item last.
4913 li = l->lv_first;
4914 for (i = 0; i < count; ++i)
4915 {
4916 tv = STACK_TV_BOT(-i - 1);
4917 copy_tv(&li->li_tv, tv);
4918 li = li->li_next;
4919 }
4920
4921 list_unref(l);
4922 }
4923 break;
4924
Bram Moolenaarb2049902021-01-24 12:53:53 +01004925 case ISN_PROF_START:
4926 case ISN_PROF_END:
4927 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004928#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004929 funccall_T cookie;
4930 ufunc_T *cur_ufunc =
4931 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004932 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004933
4934 cookie.func = cur_ufunc;
4935 if (iptr->isn_type == ISN_PROF_START)
4936 {
4937 func_line_start(&cookie, iptr->isn_lnum);
4938 // if we get here the instruction is executed
4939 func_line_exec(&cookie);
4940 }
4941 else
4942 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004943#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004944 }
4945 break;
4946
Bram Moolenaare99d4222021-06-13 14:01:26 +02004947 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004948 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004949 break;
4950
Bram Moolenaar389df252020-07-09 21:20:47 +02004951 case ISN_SHUFFLE:
4952 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004953 typval_T tmp_tv;
4954 int item = iptr->isn_arg.shuffle.shfl_item;
4955 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004956
4957 tmp_tv = *STACK_TV_BOT(-item);
4958 for ( ; up > 0 && item > 1; --up)
4959 {
4960 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4961 --item;
4962 }
4963 *STACK_TV_BOT(-item) = tmp_tv;
4964 }
4965 break;
4966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004968 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004970 ectx->ec_where.wt_index = 0;
4971 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 break;
4973 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004974 continue;
4975
Bram Moolenaard032f342020-07-18 18:13:02 +02004976func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004977 // Restore previous function. If the frame pointer is where we started
4978 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004979 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004980 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004981
Bram Moolenaar4c137212021-04-19 16:48:48 +02004982 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004983 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004984 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004985 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004986
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004987on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004988 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004989 // If "emsg_silent" is set then ignore the error, unless it was set
4990 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004991 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004992 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004993 {
4994 // If a sequence of instructions causes an error while ":silent!"
4995 // was used, restore the stack length and jump ahead to restoring
4996 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004997 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004998 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004999 while (ectx->ec_stack.ga_len
5000 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005001 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005002 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005003 clear_tv(STACK_TV_BOT(0));
5004 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005005 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5006 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005007 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005008 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005009 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005010on_fatal_error:
5011 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005012 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005013 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005014 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 }
5016
5017done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005018 ret = OK;
5019theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005020 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005021 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5022 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005023}
5024
5025/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005026 * Execute the instructions from a VAR_INSTR typeval and put the result in
5027 * "rettv".
5028 * Return OK or FAIL.
5029 */
5030 int
5031exe_typval_instr(typval_T *tv, typval_T *rettv)
5032{
5033 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5034 isn_T *save_instr = ectx->ec_instr;
5035 int save_iidx = ectx->ec_iidx;
5036 int res;
5037
LemonBoyf3b48952022-05-05 13:53:03 +01005038 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5039 // even when the compilation fails.
5040 rettv->v_type = VAR_UNKNOWN;
5041
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005042 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5043 res = exec_instructions(ectx);
5044 if (res == OK)
5045 {
5046 *rettv = *STACK_TV_BOT(-1);
5047 --ectx->ec_stack.ga_len;
5048 }
5049
5050 ectx->ec_instr = save_instr;
5051 ectx->ec_iidx = save_iidx;
5052
5053 return res;
5054}
5055
5056/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005057 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5058 * "substitute_instr".
5059 */
5060 char_u *
5061exe_substitute_instr(void)
5062{
5063 ectx_T *ectx = substitute_instr->subs_ectx;
5064 isn_T *save_instr = ectx->ec_instr;
5065 int save_iidx = ectx->ec_iidx;
5066 char_u *res;
5067
5068 ectx->ec_instr = substitute_instr->subs_instr;
5069 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005070 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005071 typval_T *tv = STACK_TV_BOT(-1);
5072
Bram Moolenaar27523602021-06-05 21:36:19 +02005073 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005074 --ectx->ec_stack.ga_len;
5075 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005076 }
5077 else
5078 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005079 substitute_instr->subs_status = FAIL;
5080 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005081 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082
Bram Moolenaar4c137212021-04-19 16:48:48 +02005083 ectx->ec_instr = save_instr;
5084 ectx->ec_iidx = save_iidx;
5085
5086 return res;
5087}
5088
5089/*
5090 * Call a "def" function from old Vim script.
5091 * Return OK or FAIL.
5092 */
5093 int
5094call_def_function(
5095 ufunc_T *ufunc,
5096 int argc_arg, // nr of arguments
5097 typval_T *argv, // arguments
5098 partial_T *partial, // optional partial for context
5099 typval_T *rettv) // return value
5100{
5101 ectx_T ectx; // execution context
5102 int argc = argc_arg;
5103 typval_T *tv;
5104 int idx;
5105 int ret = FAIL;
5106 int defcount = ufunc->uf_args.ga_len - argc;
5107 sctx_T save_current_sctx = current_sctx;
5108 int did_emsg_before = did_emsg_cumul + did_emsg;
5109 int save_suppress_errthrow = suppress_errthrow;
5110 msglist_T **saved_msg_list = NULL;
5111 msglist_T *private_msg_list = NULL;
5112 int save_emsg_silent_def = emsg_silent_def;
5113 int save_did_emsg_def = did_emsg_def;
5114 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005115 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005116
5117// Get pointer to item in the stack.
5118#undef STACK_TV
5119#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5120
5121// Get pointer to item at the bottom of the stack, -1 is the bottom.
5122#undef STACK_TV_BOT
5123#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5124
5125// Get pointer to a local variable on the stack. Negative for arguments.
5126#undef STACK_TV_VAR
5127#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5128
5129 if (ufunc->uf_def_status == UF_NOT_COMPILED
5130 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005131 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5132 && compile_def_function(ufunc, FALSE,
5133 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005134 {
5135 if (did_emsg_cumul + did_emsg == did_emsg_before)
5136 semsg(_(e_function_is_not_compiled_str),
5137 printable_func_name(ufunc));
5138 return FAIL;
5139 }
5140
5141 {
5142 // Check the function was really compiled.
5143 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5144 + ufunc->uf_dfunc_idx;
5145 if (INSTRUCTIONS(dfunc) == NULL)
5146 {
5147 iemsg("using call_def_function() on not compiled function");
5148 return FAIL;
5149 }
5150 }
5151
5152 // If depth of calling is getting too high, don't execute the function.
5153 orig_funcdepth = funcdepth_get();
5154 if (funcdepth_increment() == FAIL)
5155 return FAIL;
5156
5157 CLEAR_FIELD(ectx);
5158 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5159 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005160 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005161 {
5162 funcdepth_decrement();
5163 return FAIL;
5164 }
5165 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5166 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5167 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005168 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005169
5170 idx = argc - ufunc->uf_args.ga_len;
5171 if (idx > 0 && ufunc->uf_va_name == NULL)
5172 {
5173 if (idx == 1)
5174 emsg(_(e_one_argument_too_many));
5175 else
5176 semsg(_(e_nr_arguments_too_many), idx);
5177 goto failed_early;
5178 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005179 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5180 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005181 {
5182 if (idx == -1)
5183 emsg(_(e_one_argument_too_few));
5184 else
5185 semsg(_(e_nr_arguments_too_few), -idx);
5186 goto failed_early;
5187 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005188
5189 // Put arguments on the stack, but no more than what the function expects.
5190 // A lambda can be called with more arguments than it uses.
5191 for (idx = 0; idx < argc
5192 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5193 ++idx)
5194 {
5195 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5196 && argv[idx].v_type == VAR_SPECIAL
5197 && argv[idx].vval.v_number == VVAL_NONE)
5198 {
5199 // Use the default value.
5200 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5201 }
5202 else
5203 {
5204 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5205 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005206 ufunc->uf_arg_types[idx], &argv[idx],
5207 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005208 goto failed_early;
5209 copy_tv(&argv[idx], STACK_TV_BOT(0));
5210 }
5211 ++ectx.ec_stack.ga_len;
5212 }
5213
5214 // Turn varargs into a list. Empty list if no args.
5215 if (ufunc->uf_va_name != NULL)
5216 {
5217 int vararg_count = argc - ufunc->uf_args.ga_len;
5218
5219 if (vararg_count < 0)
5220 vararg_count = 0;
5221 else
5222 argc -= vararg_count;
5223 if (exe_newlist(vararg_count, &ectx) == FAIL)
5224 goto failed_early;
5225
5226 // Check the type of the list items.
5227 tv = STACK_TV_BOT(-1);
5228 if (ufunc->uf_va_type != NULL
5229 && ufunc->uf_va_type != &t_list_any
5230 && ufunc->uf_va_type->tt_member != &t_any
5231 && tv->vval.v_list != NULL)
5232 {
5233 type_T *expected = ufunc->uf_va_type->tt_member;
5234 listitem_T *li = tv->vval.v_list->lv_first;
5235
5236 for (idx = 0; idx < vararg_count; ++idx)
5237 {
5238 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005239 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005240 goto failed_early;
5241 li = li->li_next;
5242 }
5243 }
5244
5245 if (defcount > 0)
5246 // Move varargs list to below missing default arguments.
5247 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5248 --ectx.ec_stack.ga_len;
5249 }
5250
5251 // Make space for omitted arguments, will store default value below.
5252 // Any varargs list goes after them.
5253 if (defcount > 0)
5254 for (idx = 0; idx < defcount; ++idx)
5255 {
5256 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5257 ++ectx.ec_stack.ga_len;
5258 }
5259 if (ufunc->uf_va_name != NULL)
5260 ++ectx.ec_stack.ga_len;
5261
5262 // Frame pointer points to just after arguments.
5263 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5264 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5265
5266 {
5267 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5268 + ufunc->uf_dfunc_idx;
5269 ufunc_T *base_ufunc = dfunc->df_ufunc;
5270
5271 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5272 // by copy_func().
5273 if (partial != NULL || base_ufunc->uf_partial != NULL)
5274 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005275 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5276 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005277 goto failed_early;
5278 if (partial != NULL)
5279 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005280 outer_T *outer = get_pt_outer(partial);
5281
5282 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005283 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005284 if (current_ectx != NULL)
5285 {
5286 if (current_ectx->ec_outer_ref != NULL
5287 && current_ectx->ec_outer_ref->or_outer != NULL)
5288 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005289 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005290 }
5291 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005292 }
5293 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005294 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005295 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005296 ++partial->pt_refcount;
5297 ectx.ec_outer_ref->or_partial = partial;
5298 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005299 }
5300 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005301 {
5302 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5303 ++base_ufunc->uf_partial->pt_refcount;
5304 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5305 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005306 }
5307 }
5308
5309 // dummy frame entries
5310 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5311 {
5312 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5313 ++ectx.ec_stack.ga_len;
5314 }
5315
5316 {
5317 // Reserve space for local variables and any closure reference count.
5318 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5319 + ufunc->uf_dfunc_idx;
5320
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005321 // Initialize variables to zero. That avoids having to generate
5322 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005323 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005324 {
5325 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5326 STACK_TV_VAR(idx)->vval.v_number = 0;
5327 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005328 ectx.ec_stack.ga_len += dfunc->df_varcount;
5329 if (dfunc->df_has_closure)
5330 {
5331 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5332 STACK_TV_VAR(idx)->vval.v_number = 0;
5333 ++ectx.ec_stack.ga_len;
5334 }
5335
5336 ectx.ec_instr = INSTRUCTIONS(dfunc);
5337 }
5338
5339 // Following errors are in the function, not the caller.
5340 // Commands behave like vim9script.
5341 estack_push_ufunc(ufunc, 1);
5342 current_sctx = ufunc->uf_script_ctx;
5343 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5344
5345 // Use a specific location for storing error messages to be converted to an
5346 // exception.
5347 saved_msg_list = msg_list;
5348 msg_list = &private_msg_list;
5349
5350 // Do turn errors into exceptions.
5351 suppress_errthrow = FALSE;
5352
5353 // Do not delete the function while executing it.
5354 ++ufunc->uf_calls;
5355
5356 // When ":silent!" was used before calling then we still abort the
5357 // function. If ":silent!" is used in the function then we don't.
5358 emsg_silent_def = emsg_silent;
5359 did_emsg_def = 0;
5360
5361 ectx.ec_where.wt_index = 0;
5362 ectx.ec_where.wt_variable = FALSE;
5363
5364 // Execute the instructions until done.
5365 ret = exec_instructions(&ectx);
5366 if (ret == OK)
5367 {
5368 // function finished, get result from the stack.
5369 if (ufunc->uf_ret_type == &t_void)
5370 {
5371 rettv->v_type = VAR_VOID;
5372 }
5373 else
5374 {
5375 tv = STACK_TV_BOT(-1);
5376 *rettv = *tv;
5377 tv->v_type = VAR_UNKNOWN;
5378 }
5379 }
5380
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005381 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005382 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5383 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005384
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005385 // Deal with any remaining closures, they may be in use somewhere.
5386 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005387 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005388 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005389 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005390 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005391
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005392 estack_pop();
5393 current_sctx = save_current_sctx;
5394
Bram Moolenaar2336c372021-12-06 15:06:54 +00005395 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5396 // Function was unreferenced while being used, free it now.
5397 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005398
Bram Moolenaar352134b2020-10-17 22:04:08 +02005399 if (*msg_list != NULL && saved_msg_list != NULL)
5400 {
5401 msglist_T **plist = saved_msg_list;
5402
5403 // Append entries from the current msg_list (uncaught exceptions) to
5404 // the saved msg_list.
5405 while (*plist != NULL)
5406 plist = &(*plist)->next;
5407
5408 *plist = *msg_list;
5409 }
5410 msg_list = saved_msg_list;
5411
Bram Moolenaar4c137212021-04-19 16:48:48 +02005412 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005413 {
5414 cmdmod.cmod_filter_regmatch.regprog = NULL;
5415 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005416 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005417 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005418 emsg_silent_def = save_emsg_silent_def;
5419 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005420
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005421failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005422 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5424 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005425 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005428 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005429 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005430 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005431 if (ectx.ec_outer_ref->or_outer_allocated)
5432 vim_free(ectx.ec_outer_ref->or_outer);
5433 partial_unref(ectx.ec_outer_ref->or_partial);
5434 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005435 }
5436
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005437 // Not sure if this is necessary.
5438 suppress_errthrow = save_suppress_errthrow;
5439
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005440 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5441 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005442 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005443 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005444 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005445 return ret;
5446}
5447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005449 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5450 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5451 * "pfx" is prefixed to every line.
5452 */
5453 static void
5454list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5455{
5456 int line_idx = 0;
5457 int prev_current = 0;
5458 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005459 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005460
5461 for (current = 0; current < instr_count; ++current)
5462 {
5463 isn_T *iptr = &instr[current];
5464 char *line;
5465
5466 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005467 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005468 while (line_idx < iptr->isn_lnum
5469 && line_idx < ufunc->uf_lines.ga_len)
5470 {
5471 if (current > prev_current)
5472 {
5473 msg_puts("\n\n");
5474 prev_current = current;
5475 }
5476 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5477 if (line != NULL)
5478 msg(line);
5479 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005480 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5481 {
5482 int first_def_arg = ufunc->uf_args.ga_len
5483 - ufunc->uf_def_args.ga_len;
5484
5485 if (def_arg_idx > 0)
5486 msg_puts("\n\n");
5487 msg_start();
5488 msg_puts(" ");
5489 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5490 first_def_arg + def_arg_idx]);
5491 msg_puts(" = ");
5492 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5493 msg_clr_eos();
5494 msg_end();
5495 }
5496 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005497
5498 switch (iptr->isn_type)
5499 {
5500 case ISN_EXEC:
5501 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5502 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005503 case ISN_EXEC_SPLIT:
5504 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5505 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005506 case ISN_EXECRANGE:
5507 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5508 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005509 case ISN_LEGACY_EVAL:
5510 smsg("%s%4d EVAL legacy %s", pfx, current,
5511 iptr->isn_arg.string);
5512 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005513 case ISN_REDIRSTART:
5514 smsg("%s%4d REDIR", pfx, current);
5515 break;
5516 case ISN_REDIREND:
5517 smsg("%s%4d REDIR END%s", pfx, current,
5518 iptr->isn_arg.number ? " append" : "");
5519 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005520 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005521#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005522 smsg("%s%4d CEXPR pre %s", pfx, current,
5523 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005524#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005525 break;
5526 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005527#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005528 {
5529 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5530
5531 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5532 cexpr_get_auname(cer->cer_cmdidx),
5533 cer->cer_forceit ? "!" : "",
5534 cer->cer_cmdline);
5535 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005536#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005537 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005538 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005539 smsg("%s%4d INSTR", pfx, current);
5540 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5541 msg(" -------------");
5542 break;
5543 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005544 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005545 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5546
5547 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005548 }
5549 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005550 case ISN_SUBSTITUTE:
5551 {
5552 subs_T *subs = &iptr->isn_arg.subs;
5553
5554 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5555 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5556 msg(" -------------");
5557 }
5558 break;
5559 case ISN_EXECCONCAT:
5560 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5561 (varnumber_T)iptr->isn_arg.number);
5562 break;
5563 case ISN_ECHO:
5564 {
5565 echo_T *echo = &iptr->isn_arg.echo;
5566
5567 smsg("%s%4d %s %d", pfx, current,
5568 echo->echo_with_white ? "ECHO" : "ECHON",
5569 echo->echo_count);
5570 }
5571 break;
5572 case ISN_EXECUTE:
5573 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005574 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005575 break;
5576 case ISN_ECHOMSG:
5577 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005578 (varnumber_T)(iptr->isn_arg.number));
5579 break;
5580 case ISN_ECHOCONSOLE:
5581 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5582 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005583 break;
5584 case ISN_ECHOERR:
5585 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005586 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005587 break;
5588 case ISN_LOAD:
5589 {
5590 if (iptr->isn_arg.number < 0)
5591 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5592 (varnumber_T)(iptr->isn_arg.number
5593 + STACK_FRAME_SIZE));
5594 else
5595 smsg("%s%4d LOAD $%lld", pfx, current,
5596 (varnumber_T)(iptr->isn_arg.number));
5597 }
5598 break;
5599 case ISN_LOADOUTER:
5600 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005601 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005602 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5603 iptr->isn_arg.outer.outer_depth,
5604 iptr->isn_arg.outer.outer_idx
5605 + STACK_FRAME_SIZE);
5606 else
5607 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5608 iptr->isn_arg.outer.outer_depth,
5609 iptr->isn_arg.outer.outer_idx);
5610 }
5611 break;
5612 case ISN_LOADV:
5613 smsg("%s%4d LOADV v:%s", pfx, current,
5614 get_vim_var_name(iptr->isn_arg.number));
5615 break;
5616 case ISN_LOADSCRIPT:
5617 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005618 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5619 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5620 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005621
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005622 sv = get_script_svar(sref, -1);
5623 if (sv == NULL)
5624 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5625 pfx, current, si->sn_name);
5626 else
5627 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005628 sv->sv_name,
5629 sref->sref_idx,
5630 si->sn_name);
5631 }
5632 break;
5633 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005634 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005635 {
5636 scriptitem_T *si = SCRIPT_ITEM(
5637 iptr->isn_arg.loadstore.ls_sid);
5638
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005639 smsg("%s%4d %s s:%s from %s", pfx, current,
5640 iptr->isn_type == ISN_LOADS ? "LOADS"
5641 : "LOADEXPORT",
5642 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005643 }
5644 break;
5645 case ISN_LOADAUTO:
5646 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5647 break;
5648 case ISN_LOADG:
5649 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5650 break;
5651 case ISN_LOADB:
5652 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5653 break;
5654 case ISN_LOADW:
5655 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5656 break;
5657 case ISN_LOADT:
5658 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5659 break;
5660 case ISN_LOADGDICT:
5661 smsg("%s%4d LOAD g:", pfx, current);
5662 break;
5663 case ISN_LOADBDICT:
5664 smsg("%s%4d LOAD b:", pfx, current);
5665 break;
5666 case ISN_LOADWDICT:
5667 smsg("%s%4d LOAD w:", pfx, current);
5668 break;
5669 case ISN_LOADTDICT:
5670 smsg("%s%4d LOAD t:", pfx, current);
5671 break;
5672 case ISN_LOADOPT:
5673 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5674 break;
5675 case ISN_LOADENV:
5676 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5677 break;
5678 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005679 smsg("%s%4d LOADREG @%c", pfx, current,
5680 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005681 break;
5682
5683 case ISN_STORE:
5684 if (iptr->isn_arg.number < 0)
5685 smsg("%s%4d STORE arg[%lld]", pfx, current,
5686 iptr->isn_arg.number + STACK_FRAME_SIZE);
5687 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005688 smsg("%s%4d STORE $%lld", pfx, current,
5689 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005690 break;
5691 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005692 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5693 iptr->isn_arg.outer.outer_depth,
5694 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005695 break;
5696 case ISN_STOREV:
5697 smsg("%s%4d STOREV v:%s", pfx, current,
5698 get_vim_var_name(iptr->isn_arg.number));
5699 break;
5700 case ISN_STOREAUTO:
5701 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5702 break;
5703 case ISN_STOREG:
5704 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5705 break;
5706 case ISN_STOREB:
5707 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5708 break;
5709 case ISN_STOREW:
5710 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5711 break;
5712 case ISN_STORET:
5713 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5714 break;
5715 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005716 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005717 {
5718 scriptitem_T *si = SCRIPT_ITEM(
5719 iptr->isn_arg.loadstore.ls_sid);
5720
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005721 smsg("%s%4d %s %s in %s", pfx, current,
5722 iptr->isn_type == ISN_STORES
5723 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005724 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5725 }
5726 break;
5727 case ISN_STORESCRIPT:
5728 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005729 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5730 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5731 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005732
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005733 sv = get_script_svar(sref, -1);
5734 if (sv == NULL)
5735 smsg("%s%4d STORESCRIPT [deleted] in %s",
5736 pfx, current, si->sn_name);
5737 else
5738 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005739 sv->sv_name,
5740 sref->sref_idx,
5741 si->sn_name);
5742 }
5743 break;
5744 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005745 case ISN_STOREFUNCOPT:
5746 smsg("%s%4d %s &%s", pfx, current,
5747 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005748 iptr->isn_arg.storeopt.so_name);
5749 break;
5750 case ISN_STOREENV:
5751 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5752 break;
5753 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005754 smsg("%s%4d STOREREG @%c", pfx, current,
5755 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005756 break;
5757 case ISN_STORENR:
5758 smsg("%s%4d STORE %lld in $%d", pfx, current,
5759 iptr->isn_arg.storenr.stnr_val,
5760 iptr->isn_arg.storenr.stnr_idx);
5761 break;
5762
5763 case ISN_STOREINDEX:
5764 smsg("%s%4d STOREINDEX %s", pfx, current,
5765 vartype_name(iptr->isn_arg.vartype));
5766 break;
5767
5768 case ISN_STORERANGE:
5769 smsg("%s%4d STORERANGE", pfx, current);
5770 break;
5771
5772 // constants
5773 case ISN_PUSHNR:
5774 smsg("%s%4d PUSHNR %lld", pfx, current,
5775 (varnumber_T)(iptr->isn_arg.number));
5776 break;
5777 case ISN_PUSHBOOL:
5778 case ISN_PUSHSPEC:
5779 smsg("%s%4d PUSH %s", pfx, current,
5780 get_var_special_name(iptr->isn_arg.number));
5781 break;
5782 case ISN_PUSHF:
5783#ifdef FEAT_FLOAT
5784 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5785#endif
5786 break;
5787 case ISN_PUSHS:
5788 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5789 break;
5790 case ISN_PUSHBLOB:
5791 {
5792 char_u *r;
5793 char_u numbuf[NUMBUFLEN];
5794 char_u *tofree;
5795
5796 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5797 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5798 vim_free(tofree);
5799 }
5800 break;
5801 case ISN_PUSHFUNC:
5802 {
5803 char *name = (char *)iptr->isn_arg.string;
5804
5805 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5806 name == NULL ? "[none]" : name);
5807 }
5808 break;
5809 case ISN_PUSHCHANNEL:
5810#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005811 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005812#endif
5813 break;
5814 case ISN_PUSHJOB:
5815#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005816 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005817#endif
5818 break;
5819 case ISN_PUSHEXC:
5820 smsg("%s%4d PUSH v:exception", pfx, current);
5821 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005822 case ISN_AUTOLOAD:
5823 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5824 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005825 case ISN_UNLET:
5826 smsg("%s%4d UNLET%s %s", pfx, current,
5827 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5828 iptr->isn_arg.unlet.ul_name);
5829 break;
5830 case ISN_UNLETENV:
5831 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5832 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5833 iptr->isn_arg.unlet.ul_name);
5834 break;
5835 case ISN_UNLETINDEX:
5836 smsg("%s%4d UNLETINDEX", pfx, current);
5837 break;
5838 case ISN_UNLETRANGE:
5839 smsg("%s%4d UNLETRANGE", pfx, current);
5840 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005841 case ISN_LOCKUNLOCK:
5842 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5843 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005844 case ISN_LOCKCONST:
5845 smsg("%s%4d LOCKCONST", pfx, current);
5846 break;
5847 case ISN_NEWLIST:
5848 smsg("%s%4d NEWLIST size %lld", pfx, current,
5849 (varnumber_T)(iptr->isn_arg.number));
5850 break;
5851 case ISN_NEWDICT:
5852 smsg("%s%4d NEWDICT size %lld", pfx, current,
5853 (varnumber_T)(iptr->isn_arg.number));
5854 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005855 case ISN_NEWPARTIAL:
5856 smsg("%s%4d NEWPARTIAL", pfx, current);
5857 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005858
5859 // function call
5860 case ISN_BCALL:
5861 {
5862 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5863
5864 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5865 internal_func_name(cbfunc->cbf_idx),
5866 cbfunc->cbf_argcount);
5867 }
5868 break;
5869 case ISN_DCALL:
5870 {
5871 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5872 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5873 + cdfunc->cdf_idx;
5874
5875 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005876 printable_func_name(df->df_ufunc),
5877 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005878 }
5879 break;
5880 case ISN_UCALL:
5881 {
5882 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5883
5884 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5885 cufunc->cuf_name, cufunc->cuf_argcount);
5886 }
5887 break;
5888 case ISN_PCALL:
5889 {
5890 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5891
5892 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5893 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5894 }
5895 break;
5896 case ISN_PCALL_END:
5897 smsg("%s%4d PCALL end", pfx, current);
5898 break;
5899 case ISN_RETURN:
5900 smsg("%s%4d RETURN", pfx, current);
5901 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005902 case ISN_RETURN_VOID:
5903 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005904 break;
5905 case ISN_FUNCREF:
5906 {
5907 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005908 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005909
Bram Moolenaar38453522021-11-28 22:00:12 +00005910 if (funcref->fr_func_name == NULL)
5911 {
5912 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5913 + funcref->fr_dfunc_idx;
5914 name = df->df_ufunc->uf_name;
5915 }
5916 else
5917 name = funcref->fr_func_name;
5918 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005919 }
5920 break;
5921
5922 case ISN_NEWFUNC:
5923 {
5924 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5925
5926 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5927 newfunc->nf_lambda, newfunc->nf_global);
5928 }
5929 break;
5930
5931 case ISN_DEF:
5932 {
5933 char_u *name = iptr->isn_arg.string;
5934
5935 smsg("%s%4d DEF %s", pfx, current,
5936 name == NULL ? (char_u *)"" : name);
5937 }
5938 break;
5939
5940 case ISN_JUMP:
5941 {
5942 char *when = "?";
5943
5944 switch (iptr->isn_arg.jump.jump_when)
5945 {
5946 case JUMP_ALWAYS:
5947 when = "JUMP";
5948 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005949 case JUMP_NEVER:
5950 iemsg("JUMP_NEVER should not be used");
5951 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005952 case JUMP_AND_KEEP_IF_TRUE:
5953 when = "JUMP_AND_KEEP_IF_TRUE";
5954 break;
5955 case JUMP_IF_FALSE:
5956 when = "JUMP_IF_FALSE";
5957 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005958 case JUMP_IF_COND_FALSE:
5959 when = "JUMP_IF_COND_FALSE";
5960 break;
5961 case JUMP_IF_COND_TRUE:
5962 when = "JUMP_IF_COND_TRUE";
5963 break;
5964 }
5965 smsg("%s%4d %s -> %d", pfx, current, when,
5966 iptr->isn_arg.jump.jump_where);
5967 }
5968 break;
5969
5970 case ISN_JUMP_IF_ARG_SET:
5971 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5972 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5973 iptr->isn_arg.jump.jump_where);
5974 break;
5975
5976 case ISN_FOR:
5977 {
5978 forloop_T *forloop = &iptr->isn_arg.forloop;
5979
5980 smsg("%s%4d FOR $%d -> %d", pfx, current,
5981 forloop->for_idx, forloop->for_end);
5982 }
5983 break;
5984
5985 case ISN_TRY:
5986 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005987 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005988
5989 if (try->try_ref->try_finally == 0)
5990 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5991 pfx, current,
5992 try->try_ref->try_catch,
5993 try->try_ref->try_endtry);
5994 else
5995 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5996 pfx, current,
5997 try->try_ref->try_catch,
5998 try->try_ref->try_finally,
5999 try->try_ref->try_endtry);
6000 }
6001 break;
6002 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006003 smsg("%s%4d CATCH", pfx, current);
6004 break;
6005 case ISN_TRYCONT:
6006 {
6007 trycont_T *trycont = &iptr->isn_arg.trycont;
6008
6009 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6010 trycont->tct_levels,
6011 trycont->tct_levels == 1 ? "" : "s",
6012 trycont->tct_where);
6013 }
6014 break;
6015 case ISN_FINALLY:
6016 smsg("%s%4d FINALLY", pfx, current);
6017 break;
6018 case ISN_ENDTRY:
6019 smsg("%s%4d ENDTRY", pfx, current);
6020 break;
6021 case ISN_THROW:
6022 smsg("%s%4d THROW", pfx, current);
6023 break;
6024
6025 // expression operations on number
6026 case ISN_OPNR:
6027 case ISN_OPFLOAT:
6028 case ISN_OPANY:
6029 {
6030 char *what;
6031 char *ins;
6032
6033 switch (iptr->isn_arg.op.op_type)
6034 {
6035 case EXPR_MULT: what = "*"; break;
6036 case EXPR_DIV: what = "/"; break;
6037 case EXPR_REM: what = "%"; break;
6038 case EXPR_SUB: what = "-"; break;
6039 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006040 case EXPR_LSHIFT: what = "<<"; break;
6041 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006042 default: what = "???"; break;
6043 }
6044 switch (iptr->isn_type)
6045 {
6046 case ISN_OPNR: ins = "OPNR"; break;
6047 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6048 case ISN_OPANY: ins = "OPANY"; break;
6049 default: ins = "???"; break;
6050 }
6051 smsg("%s%4d %s %s", pfx, current, ins, what);
6052 }
6053 break;
6054
6055 case ISN_COMPAREBOOL:
6056 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006057 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006058 case ISN_COMPARENR:
6059 case ISN_COMPAREFLOAT:
6060 case ISN_COMPARESTRING:
6061 case ISN_COMPAREBLOB:
6062 case ISN_COMPARELIST:
6063 case ISN_COMPAREDICT:
6064 case ISN_COMPAREFUNC:
6065 case ISN_COMPAREANY:
6066 {
6067 char *p;
6068 char buf[10];
6069 char *type;
6070
6071 switch (iptr->isn_arg.op.op_type)
6072 {
6073 case EXPR_EQUAL: p = "=="; break;
6074 case EXPR_NEQUAL: p = "!="; break;
6075 case EXPR_GREATER: p = ">"; break;
6076 case EXPR_GEQUAL: p = ">="; break;
6077 case EXPR_SMALLER: p = "<"; break;
6078 case EXPR_SEQUAL: p = "<="; break;
6079 case EXPR_MATCH: p = "=~"; break;
6080 case EXPR_IS: p = "is"; break;
6081 case EXPR_ISNOT: p = "isnot"; break;
6082 case EXPR_NOMATCH: p = "!~"; break;
6083 default: p = "???"; break;
6084 }
6085 STRCPY(buf, p);
6086 if (iptr->isn_arg.op.op_ic == TRUE)
6087 strcat(buf, "?");
6088 switch(iptr->isn_type)
6089 {
6090 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6091 case ISN_COMPARESPECIAL:
6092 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006093 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006094 case ISN_COMPARENR: type = "COMPARENR"; break;
6095 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6096 case ISN_COMPARESTRING:
6097 type = "COMPARESTRING"; break;
6098 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6099 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6100 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6101 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6102 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6103 default: type = "???"; break;
6104 }
6105
6106 smsg("%s%4d %s %s", pfx, current, type, buf);
6107 }
6108 break;
6109
6110 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6111 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6112
6113 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006114 case ISN_CONCAT:
6115 smsg("%s%4d CONCAT size %lld", pfx, current,
6116 (varnumber_T)(iptr->isn_arg.number));
6117 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006118 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6119 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6120 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6121 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6122 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6123 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6124 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6125 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6126 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6127 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6128 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006129 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006130 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6131 iptr->isn_arg.getitem.gi_index,
6132 iptr->isn_arg.getitem.gi_with_op ?
6133 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006134 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6135 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6136 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006137 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6138 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6139
Bram Moolenaar4c137212021-04-19 16:48:48 +02006140 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6141
Bram Moolenaar4c137212021-04-19 16:48:48 +02006142 case ISN_CHECKTYPE:
6143 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006144 checktype_T *ct = &iptr->isn_arg.type;
6145 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006146
6147 if (ct->ct_arg_idx == 0)
6148 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6149 type_name(ct->ct_type, &tofree),
6150 (int)ct->ct_off);
6151 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006152 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006153 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006154 type_name(ct->ct_type, &tofree),
6155 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006156 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006157 (int)ct->ct_arg_idx);
6158 vim_free(tofree);
6159 break;
6160 }
6161 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6162 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6163 iptr->isn_arg.checklen.cl_min_len);
6164 break;
6165 case ISN_SETTYPE:
6166 {
6167 char *tofree;
6168
6169 smsg("%s%4d SETTYPE %s", pfx, current,
6170 type_name(iptr->isn_arg.type.ct_type, &tofree));
6171 vim_free(tofree);
6172 break;
6173 }
6174 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006175 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6176 smsg("%s%4d INVERT %d (!val)", pfx, current,
6177 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006178 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006179 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6180 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006181 break;
6182 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006183 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006184 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006185 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6186 pfx, current,
6187 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006188 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006189 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6190 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006191 break;
6192 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006193 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006194 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006195 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006196 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6197 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006198 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006199 else
6200 smsg("%s%4d PUT %c %ld", pfx, current,
6201 iptr->isn_arg.put.put_regname,
6202 (long)iptr->isn_arg.put.put_lnum);
6203 break;
6204
Bram Moolenaar4c137212021-04-19 16:48:48 +02006205 case ISN_CMDMOD:
6206 {
6207 char_u *buf;
6208 size_t len = produce_cmdmods(
6209 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6210
6211 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006212 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006213 {
6214 (void)produce_cmdmods(
6215 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6216 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6217 vim_free(buf);
6218 }
6219 break;
6220 }
6221 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6222
6223 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006224 smsg("%s%4d PROFILE START line %d", pfx, current,
6225 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006226 break;
6227
6228 case ISN_PROF_END:
6229 smsg("%s%4d PROFILE END", pfx, current);
6230 break;
6231
Bram Moolenaare99d4222021-06-13 14:01:26 +02006232 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006233 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6234 iptr->isn_arg.debug.dbg_break_lnum + 1,
6235 iptr->isn_lnum,
6236 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006237 break;
6238
Bram Moolenaar4c137212021-04-19 16:48:48 +02006239 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6240 iptr->isn_arg.unpack.unp_count,
6241 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6242 break;
6243 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6244 iptr->isn_arg.shuffle.shfl_item,
6245 iptr->isn_arg.shuffle.shfl_up);
6246 break;
6247 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6248
6249 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6250 return;
6251 }
6252
6253 out_flush(); // output one line at a time
6254 ui_breakcheck();
6255 if (got_int)
6256 break;
6257 }
6258}
6259
6260/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006261 * Handle command line completion for the :disassemble command.
6262 */
6263 void
6264set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6265{
6266 char_u *p;
6267
6268 // Default: expand user functions, "debug" and "profile"
6269 xp->xp_context = EXPAND_DISASSEMBLE;
6270 xp->xp_pattern = arg;
6271
6272 // first argument already typed: only user function names
6273 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6274 {
6275 xp->xp_context = EXPAND_USER_FUNC;
6276 xp->xp_pattern = skipwhite(p);
6277 }
6278}
6279
6280/*
6281 * Function given to ExpandGeneric() to obtain the list of :disassemble
6282 * arguments.
6283 */
6284 char_u *
6285get_disassemble_argument(expand_T *xp, int idx)
6286{
6287 if (idx == 0)
6288 return (char_u *)"debug";
6289 if (idx == 1)
6290 return (char_u *)"profile";
6291 return get_user_func_name(xp, idx - 2);
6292}
6293
6294/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006295 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006296 * We don't really need this at runtime, but we do have tests that require it,
6297 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298 */
6299 void
6300ex_disassemble(exarg_T *eap)
6301{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006302 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006303 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006305 isn_T *instr = NULL; // init to shut up gcc warning
6306 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006307 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006308
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006309 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006310 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006312 if (func_needs_compiling(ufunc, compile_type)
6313 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006314 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006315 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006317 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 return;
6319 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006320 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006321
6322 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006323 switch (compile_type)
6324 {
6325 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006326#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006327 instr = dfunc->df_instr_prof;
6328 instr_count = dfunc->df_instr_prof_count;
6329 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006330#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006331 // FALLTHROUGH
6332 case CT_NONE:
6333 instr = dfunc->df_instr;
6334 instr_count = dfunc->df_instr_count;
6335 break;
6336 case CT_DEBUG:
6337 instr = dfunc->df_instr_debug;
6338 instr_count = dfunc->df_instr_debug_count;
6339 break;
6340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006341
Bram Moolenaar4c137212021-04-19 16:48:48 +02006342 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343}
6344
6345/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006346 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006347 * list, etc. Mostly like what JavaScript does, except that empty list and
6348 * empty dictionary are FALSE.
6349 */
6350 int
6351tv2bool(typval_T *tv)
6352{
6353 switch (tv->v_type)
6354 {
6355 case VAR_NUMBER:
6356 return tv->vval.v_number != 0;
6357 case VAR_FLOAT:
6358#ifdef FEAT_FLOAT
6359 return tv->vval.v_float != 0.0;
6360#else
6361 break;
6362#endif
6363 case VAR_PARTIAL:
6364 return tv->vval.v_partial != NULL;
6365 case VAR_FUNC:
6366 case VAR_STRING:
6367 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6368 case VAR_LIST:
6369 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6370 case VAR_DICT:
6371 return tv->vval.v_dict != NULL
6372 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6373 case VAR_BOOL:
6374 case VAR_SPECIAL:
6375 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6376 case VAR_JOB:
6377#ifdef FEAT_JOB_CHANNEL
6378 return tv->vval.v_job != NULL;
6379#else
6380 break;
6381#endif
6382 case VAR_CHANNEL:
6383#ifdef FEAT_JOB_CHANNEL
6384 return tv->vval.v_channel != NULL;
6385#else
6386 break;
6387#endif
6388 case VAR_BLOB:
6389 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6390 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006391 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006393 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394 break;
6395 }
6396 return FALSE;
6397}
6398
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006399 void
6400emsg_using_string_as(typval_T *tv, int as_number)
6401{
6402 semsg(_(as_number ? e_using_string_as_number_str
6403 : e_using_string_as_bool_str),
6404 tv->vval.v_string == NULL
6405 ? (char_u *)"" : tv->vval.v_string);
6406}
6407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408/*
6409 * If "tv" is a string give an error and return FAIL.
6410 */
6411 int
6412check_not_string(typval_T *tv)
6413{
6414 if (tv->v_type == VAR_STRING)
6415 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006416 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006417 clear_tv(tv);
6418 return FAIL;
6419 }
6420 return OK;
6421}
6422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423
6424#endif // FEAT_EVAL